Search in sources :

Example 66 with CommitLogReader

use of cz.o2.proxima.direct.commitlog.CommitLogReader in project proxima-platform by O2-Czech-Republic.

the class DirectDataOperatorTest method testWriteIntoReplicatedProxyAttribute.

@Test(timeout = 10000)
public void testWriteIntoReplicatedProxyAttribute() throws InterruptedException {
    repo.reloadConfig(true, ConfigFactory.load().withFallback(ConfigFactory.load("test-replication.conf")).withFallback(ConfigFactory.load("test-reference.conf")).resolve());
    EntityDescriptor dummy = repo.getEntity("dummy");
    AttributeDescriptor<Object> data = dummy.getAttribute("data", true);
    AttributeDescriptor<Object> dataReplicated = dummy.getAttribute("_dummyReplicationProxiedSlave_replicated$_d", true);
    CountDownLatch latch = new CountDownLatch(2);
    CommitLogReader reader = direct.getCommitLogReader(data).orElseThrow(() -> new IllegalStateException("Missing commit log reader for data"));
    reader.observe("dummy", new CommitLogObserver() {

        @Override
        public boolean onNext(StreamElement ingest, OnNextContext context) {
            assertEquals(ingest.getAttributeDescriptor(), data);
            latch.countDown();
            return true;
        }

        @Override
        public boolean onError(Throwable error) {
            throw new RuntimeException(error);
        }
    });
    OnlineAttributeWriter writer = direct.getWriter(dataReplicated).get();
    writer.write(StreamElement.upsert(dummy, dataReplicated, "uuid", "gw", dataReplicated.getName(), System.currentTimeMillis(), new byte[] { 1, 2 }), (succ, exc) -> {
        assertTrue(succ);
        latch.countDown();
    });
    latch.await();
}
Also used : LimitedCommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReaders.LimitedCommitLogReader) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) Test(org.junit.Test)

Example 67 with CommitLogReader

use of cz.o2.proxima.direct.commitlog.CommitLogReader in project proxima-platform by O2-Czech-Republic.

the class DirectDataOperatorTest method testReplicationWriteReadonlyObserve.

@SuppressWarnings("unchecked")
@Test(timeout = 10000)
public void testReplicationWriteReadonlyObserve() throws InterruptedException {
    repo.reloadConfig(true, ConfigFactory.load().withFallback(ConfigFactory.parseString("replications.gateway-replication.read-only = true")).withFallback(ConfigFactory.load("test-replication.conf")).withFallback(ConfigFactory.load("test-reference.conf")).resolve());
    EntityDescriptor gateway = repo.getEntity("gateway");
    AttributeDescriptor<Object> armed = gateway.getAttribute("armed");
    TimeUnit.MILLISECONDS.sleep(300);
    CommitLogReader reader = direct.getFamiliesForAttribute(armed).stream().filter(af -> af.getDesc().getAccess().canReadCommitLog()).findAny().flatMap(DirectAttributeFamilyDescriptor::getCommitLogReader).orElseThrow(() -> new IllegalStateException("Missing random access reader for armed"));
    CountDownLatch latch = new CountDownLatch(1);
    reader.observe("dummy", new CommitLogObserver() {

        @Override
        public boolean onNext(StreamElement ingest, OnNextContext context) {
            assertEquals(ingest.getAttributeDescriptor(), armed);
            latch.countDown();
            context.confirm();
            return true;
        }

        @Override
        public boolean onError(Throwable error) {
            throw new RuntimeException(error);
        }
    });
    assertTrue(direct.getWriter(armed).isPresent());
    OnlineAttributeWriter writer = direct.getWriter(armed).get();
    writer.write(StreamElement.upsert(gateway, armed, "uuid", "gw", armed.getName(), System.currentTimeMillis(), new byte[] { 1, 2 }), (succ, exc) -> {
        assertTrue(succ);
    });
    latch.await();
}
Also used : Arrays(java.util.Arrays) TestTracker(cz.o2.proxima.storage.watermark.GlobalWatermarkThroughputLimiterTest.TestTracker) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) StorageType(cz.o2.proxima.storage.StorageType) DummyFilter(cz.o2.proxima.util.DummyFilter) EventDataToDummy(cz.o2.proxima.transform.EventDataToDummy) CachedView(cz.o2.proxima.direct.view.CachedView) StreamElement(cz.o2.proxima.storage.StreamElement) TransformationRunner(cz.o2.proxima.util.TransformationRunner) Map(java.util.Map) AttributeFamilyProxyDescriptor(cz.o2.proxima.repository.AttributeFamilyProxyDescriptor) Assert.fail(org.junit.Assert.fail) URI(java.net.URI) TransformationDescriptor(cz.o2.proxima.repository.TransformationDescriptor) Optionals(cz.o2.proxima.util.Optionals) TestUtils(cz.o2.proxima.util.TestUtils) Set(java.util.Set) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) GlobalWatermarkThroughputLimiter(cz.o2.proxima.storage.watermark.GlobalWatermarkThroughputLimiter) Objects(java.util.Objects) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) ConfigRepository(cz.o2.proxima.repository.ConfigRepository) KeyValue(cz.o2.proxima.direct.randomaccess.KeyValue) Assert.assertFalse(org.junit.Assert.assertFalse) Accept(cz.o2.proxima.storage.internal.AbstractDataAccessorFactory.Accept) Optional(java.util.Optional) ElementWiseTransformation(cz.o2.proxima.transform.ElementWiseTransformation) UnsupportedEncodingException(java.io.UnsupportedEncodingException) ThroughputLimiter(cz.o2.proxima.storage.ThroughputLimiter) AttributeProxyDescriptor(cz.o2.proxima.repository.AttributeProxyDescriptor) Iterables(com.google.common.collect.Iterables) LimitedCommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReaders.LimitedCommitLogReader) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) ConfigFactory(com.typesafe.config.ConfigFactory) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) RandomAccessReader(cz.o2.proxima.direct.randomaccess.RandomAccessReader) PassthroughFilter(cz.o2.proxima.storage.PassthroughFilter) Config(com.typesafe.config.Config) Assert.assertNotNull(org.junit.Assert.assertNotNull) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) Assert.assertTrue(org.junit.Assert.assertTrue) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) IOException(java.io.IOException) Test(org.junit.Test) AttributeFamilyDescriptor(cz.o2.proxima.repository.AttributeFamilyDescriptor) NotSerializableException(java.io.NotSerializableException) ReplicationRunner.runAttributeReplicas(cz.o2.proxima.util.ReplicationRunner.runAttributeReplicas) TimeUnit(java.util.concurrent.TimeUnit) GlobalWatermarkTracker(cz.o2.proxima.storage.watermark.GlobalWatermarkTracker) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) LimitedCommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReaders.LimitedCommitLogReader) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) Test(org.junit.Test)

Example 68 with CommitLogReader

use of cz.o2.proxima.direct.commitlog.CommitLogReader in project proxima-platform by O2-Czech-Republic.

the class DirectDataOperatorTest method testLimiterDataAccessor.

@Test
public void testLimiterDataAccessor() {
    repo.reloadConfig(true, ConfigFactory.load("test-limiter.conf").withFallback(ConfigFactory.load("test-reference.conf")).resolve());
    DirectDataOperator direct = repo.getOrCreateOperator(DirectDataOperator.class);
    DirectAttributeFamilyDescriptor family = direct.getFamilyByName("event-storage-stream");
    Optional<CommitLogReader> maybeReader = family.getCommitLogReader();
    assertTrue(maybeReader.isPresent());
    CommitLogReader reader = maybeReader.get();
    assertTrue("Expected reader of class LimitedCommitLogReader, got " + reader.getClass(), reader instanceof LimitedCommitLogReader);
    LimitedCommitLogReader limitedReader = (LimitedCommitLogReader) reader;
    ThroughputLimiter limiter = limitedReader.getLimiter();
    assertNotNull(limiter);
    assertTrue(limiter instanceof GlobalWatermarkThroughputLimiter);
    GlobalWatermarkThroughputLimiter watermarkLimiter = (GlobalWatermarkThroughputLimiter) limiter;
    GlobalWatermarkTracker tracker = watermarkLimiter.getTracker();
    assertTrue(tracker instanceof TestTracker);
    TestTracker testTracker = (TestTracker) tracker;
    assertEquals(2, testTracker.getTestConf());
}
Also used : GlobalWatermarkThroughputLimiter(cz.o2.proxima.storage.watermark.GlobalWatermarkThroughputLimiter) ThroughputLimiter(cz.o2.proxima.storage.ThroughputLimiter) TestTracker(cz.o2.proxima.storage.watermark.GlobalWatermarkThroughputLimiterTest.TestTracker) LimitedCommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReaders.LimitedCommitLogReader) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) LimitedCommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReaders.LimitedCommitLogReader) GlobalWatermarkTracker(cz.o2.proxima.storage.watermark.GlobalWatermarkTracker) GlobalWatermarkThroughputLimiter(cz.o2.proxima.storage.watermark.GlobalWatermarkThroughputLimiter) Test(org.junit.Test)

Example 69 with CommitLogReader

use of cz.o2.proxima.direct.commitlog.CommitLogReader in project proxima-platform by O2-Czech-Republic.

the class CommitLogReaderTest method testThroughputLimitedCommitLogIdles.

@Test(timeout = 10000)
public void testThroughputLimitedCommitLogIdles() throws InterruptedException {
    ThroughputLimiter limiter = ThroughputLimiter.NoOpThroughputLimiter.INSTANCE;
    CommitLogReader limitedReader = CommitLogReaders.withThroughputLimit(reader, limiter);
    UnaryFunction<CountDownLatch, CommitLogObserver> observerFactory = myLatch -> new CommitLogObserver() {

        @Override
        public boolean onError(Throwable error) {
            return false;
        }

        @Override
        public boolean onNext(StreamElement ingest, OnNextContext context) {
            return false;
        }

        @Override
        public void onIdle(OnIdleContext context) {
            myLatch.countDown();
        }
    };
    CountDownLatch latch = new CountDownLatch(1);
    CommitLogObserver observer = observerFactory.apply(latch);
    long nanoTime = System.nanoTime();
    ObserveHandle handle = limitedReader.observe("dummy", observer);
    latch.await();
    handle.close();
    latch = new CountDownLatch(1);
    observer = observerFactory.apply(latch);
    long durationMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - nanoTime);
    handle.close();
    limitedReader = CommitLogReaders.withThroughputLimit(reader, withNumRecordsPerSec(1));
    handle = limitedReader.observe("dummy", observer);
    // no idle called this time
    assertFalse(latch.await(2 * durationMillis, TimeUnit.MILLISECONDS));
}
Also used : Iterables(com.google.common.collect.Iterables) AttributeWriterBase(cz.o2.proxima.direct.core.AttributeWriterBase) Partition(cz.o2.proxima.storage.Partition) OffsetCommitter(cz.o2.proxima.direct.commitlog.CommitLogObserver.OffsetCommitter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) Mockito.spy(org.mockito.Mockito.spy) LimitedCommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReaders.LimitedCommitLogReader) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) ExceptionUtils(cz.o2.proxima.util.ExceptionUtils) StreamElement(cz.o2.proxima.storage.StreamElement) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UnaryFunction(cz.o2.proxima.functional.UnaryFunction) Duration(java.time.Duration) After(org.junit.After) ConfigFactory(com.typesafe.config.ConfigFactory) Optionals(cz.o2.proxima.util.Optionals) Before(org.junit.Before) LongStream(java.util.stream.LongStream) Repository(cz.o2.proxima.repository.Repository) BiFunction(cz.o2.proxima.functional.BiFunction) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) DirectAttributeFamilyDescriptor(cz.o2.proxima.direct.core.DirectAttributeFamilyDescriptor) OnNextContext(cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) Collections(java.util.Collections) Position(cz.o2.proxima.storage.commitlog.Position) ThroughputLimiter(cz.o2.proxima.storage.ThroughputLimiter) ThroughputLimiter(cz.o2.proxima.storage.ThroughputLimiter) OnNextContext(cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext) LimitedCommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReaders.LimitedCommitLogReader) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 70 with CommitLogReader

use of cz.o2.proxima.direct.commitlog.CommitLogReader in project proxima-platform by O2-Czech-Republic.

the class CommitLogReaderTest method testThroughputLimitedCommitLogWithObserve.

private void testThroughputLimitedCommitLogWithObserve(BiFunction<CommitLogReader, Runnable, ObserveHandle> observe) throws InterruptedException {
    final int numElements = 50;
    ThroughputLimiter limiter = withNumRecordsPerSec(2 * numElements);
    CommitLogReader limitedReader = CommitLogReaders.withThroughputLimit(reader, limiter);
    assertEquals(limitedReader.getPartitions(), reader.getPartitions());
    assertEquals(limitedReader.getUri(), reader.getUri());
    CountDownLatch latch = new CountDownLatch(numElements);
    AtomicLong lastMillis = new AtomicLong(System.currentTimeMillis());
    List<Long> durations = new ArrayList<>();
    ObserveHandle handle = observe.apply(limitedReader, () -> {
        latch.countDown();
        long now = System.currentTimeMillis();
        long lastGet = lastMillis.getAndUpdate(current -> now);
        durations.add(now - lastGet);
    });
    for (int i = 0; i < 50; i++) {
        writer.online().write(StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), System.currentTimeMillis(), new byte[] { 1, 2 }), (succ, exc) -> {
        });
    }
    latch.await();
    assertEquals(numElements, durations.size());
    assertEquals(durations.toString(), numElements, durations.stream().filter(s -> s >= 500 / numElements).count());
    handle.close();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ThroughputLimiter(cz.o2.proxima.storage.ThroughputLimiter) LimitedCommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReaders.LimitedCommitLogReader) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

CommitLogReader (cz.o2.proxima.direct.commitlog.CommitLogReader)80 Test (org.junit.Test)70 StreamElement (cz.o2.proxima.storage.StreamElement)67 CommitLogObserver (cz.o2.proxima.direct.commitlog.CommitLogObserver)63 CountDownLatch (java.util.concurrent.CountDownLatch)58 ObserveHandle (cz.o2.proxima.direct.commitlog.ObserveHandle)45 ArrayList (java.util.ArrayList)41 EntityDescriptor (cz.o2.proxima.repository.EntityDescriptor)34 AttributeDescriptor (cz.o2.proxima.repository.AttributeDescriptor)33 List (java.util.List)31 UUID (java.util.UUID)29 ConfigFactory (com.typesafe.config.ConfigFactory)28 DirectDataOperator (cz.o2.proxima.direct.core.DirectDataOperator)28 Offset (cz.o2.proxima.direct.commitlog.Offset)27 Collections (java.util.Collections)27 Collectors (java.util.stream.Collectors)27 OnNextContext (cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext)26 AtomicReference (java.util.concurrent.atomic.AtomicReference)26 Repository (cz.o2.proxima.repository.Repository)25 Accessor (cz.o2.proxima.direct.kafka.LocalKafkaCommitLogDescriptor.Accessor)24