Search in sources :

Example 31 with ObserveHandle

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

the class ReplicationControllerTest method testBulkReplication.

@Test
public void testBulkReplication() throws InterruptedException {
    final List<StreamElement> written = new ArrayList<>();
    final CommitLogReader reader = Optionals.get(direct.getCommitLogReader(data));
    final CommitLogObserver observer = controller.createBulkObserver("consumer", reader, Sets.newHashSet(data), new PassthroughFilter(), fakeBulkWriter(written, stamp -> stamp == now - InMemStorage.getBoundedOutOfOrderness() + 100));
    try (ObserveHandle ignored = reader.observeBulk("consumer", observer)) {
        for (int i = 0; i < 10; i++) {
            writeEvent(now + 20 * i);
        }
        assertEquals(6, written.size());
    }
    livenessLatch.await();
    assertEquals(1.0, Metrics.LIVENESS.getValue(), 0.0001);
}
Also used : CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) PassthroughFilter(cz.o2.proxima.storage.PassthroughFilter) Partition(cz.o2.proxima.storage.Partition) OffsetCommitter(cz.o2.proxima.direct.commitlog.CommitLogObserver.OffsetCommitter) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) CompletableFuture(java.util.concurrent.CompletableFuture) OnlineAttributeWriter(cz.o2.proxima.direct.core.OnlineAttributeWriter) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) WatermarkEstimator(cz.o2.proxima.time.WatermarkEstimator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) After(org.junit.After) ConfigFactory(com.typesafe.config.ConfigFactory) UnaryPredicate(cz.o2.proxima.functional.UnaryPredicate) URI(java.net.URI) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) PassthroughFilter(cz.o2.proxima.storage.PassthroughFilter) Metrics(cz.o2.proxima.server.metrics.Metrics) Optionals(cz.o2.proxima.util.Optionals) Before(org.junit.Before) BulkAttributeWriter(cz.o2.proxima.direct.core.BulkAttributeWriter) InMemStorage(cz.o2.proxima.direct.storage.InMemStorage) Repository(cz.o2.proxima.repository.Repository) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) Test(org.junit.Test) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) UUID(java.util.UUID) CommitCallback(cz.o2.proxima.direct.core.CommitCallback) Sets(com.google.common.collect.Sets) Offset(cz.o2.proxima.direct.commitlog.Offset) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) TransformationObserver(cz.o2.proxima.direct.transform.TransformationObserver) OnNextContext(cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) ArrayList(java.util.ArrayList) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) StreamElement(cz.o2.proxima.storage.StreamElement) Test(org.junit.Test)

Example 32 with ObserveHandle

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

the class ReplicationControllerTest method testSimpleEventReplication.

@Test(timeout = 5000)
public void testSimpleEventReplication() throws InterruptedException {
    List<StreamElement> written = new ArrayList<>();
    final CommitLogReader reader = Optionals.get(direct.getCommitLogReader(data));
    final CommitLogObserver observer = controller.createOnlineObserver("consumer", direct.getCommitLogReader(data).orElseThrow(() -> new IllegalArgumentException("Missing commit log reader for data")), Sets.newHashSet(data), new PassthroughFilter(), fakeOnlineWriter(written));
    try (ObserveHandle ignored = reader.observe("consumer", observer)) {
        writeEvent();
        assertEquals(1, written.size());
    }
    livenessLatch.await();
    assertEquals(1.0, Metrics.LIVENESS.getValue(), 0.0001);
}
Also used : CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) PassthroughFilter(cz.o2.proxima.storage.PassthroughFilter) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) ArrayList(java.util.ArrayList) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) StreamElement(cz.o2.proxima.storage.StreamElement) Test(org.junit.Test)

Example 33 with ObserveHandle

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

the class ListCommitLogTest method testObserveNonExternalizableWatermark.

@Test
public void testObserveNonExternalizableWatermark() throws InterruptedException {
    int numElements = 10;
    CommitLogReader reader = ListCommitLog.ofNonExternalizable(data(numElements), direct.getContext());
    CountDownLatch first = new CountDownLatch(1);
    List<Long> watermarks = new ArrayList<>();
    ObserveHandle handle = reader.observeBulk(null, new CommitLogObserver() {

        int received = 0;

        @Override
        public boolean onError(Throwable error) {
            throw new RuntimeException(error);
        }

        @Override
        public boolean onNext(StreamElement ingest, OnNextContext context) {
            watermarks.add(context.getWatermark());
            if (++received == numElements) {
                context.confirm();
            }
            return true;
        }

        @Override
        public void onCompleted() {
            first.countDown();
        }
    });
    first.await();
    assertEquals(numElements, watermarks.size());
    long min = watermarks.get(0);
    for (int i = 1; i < numElements; i++) {
        assertEquals(min, (long) watermarks.get(i));
    }
}
Also used : ListObserveHandle(cz.o2.proxima.direct.storage.ListCommitLog.ListObserveHandle) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) Test(org.junit.Test)

Example 34 with ObserveHandle

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

the class ListCommitLogTest method testObserveNonExternalizableUnnamedPauseContinueNoCommit.

@Test(timeout = 10000)
public void testObserveNonExternalizableUnnamedPauseContinueNoCommit() throws InterruptedException {
    CommitLogReader reader = ListCommitLog.ofNonExternalizable(data(10), direct.getContext());
    List<StreamElement> data = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(1);
    ObserveHandle handle = reader.observe(null, toList(data, b -> latch.countDown(), v -> v.getValue()[0] < 5));
    latch.await();
    assertEquals(6, data.size());
    assertFalse(handle.getCommittedOffsets().isEmpty());
    assertFalse(handle.getCurrentOffsets().isEmpty());
    CountDownLatch nextLatch = new CountDownLatch(1);
    reader.observeBulkOffsets(handle.getCurrentOffsets(), toList(data, b -> nextLatch.countDown()));
    nextLatch.await();
    assertEquals(10, data.size());
}
Also used : IntStream(java.util.stream.IntStream) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) HashMap(java.util.HashMap) Function(java.util.function.Function) LogObserverUtils.toList(cz.o2.proxima.direct.commitlog.LogObserverUtils.toList) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) WatermarkEstimator(cz.o2.proxima.time.WatermarkEstimator) ConfigFactory(com.typesafe.config.ConfigFactory) SerializationException(cz.o2.proxima.scheme.SerializationException) TypeReference(com.fasterxml.jackson.core.type.TypeReference) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) ExecutorService(java.util.concurrent.ExecutorService) ListObserveHandle(cz.o2.proxima.direct.storage.ListCommitLog.ListObserveHandle) Repository(cz.o2.proxima.repository.Repository) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) Offset(cz.o2.proxima.direct.commitlog.Offset) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) Collections(java.util.Collections) ListObserveHandle(cz.o2.proxima.direct.storage.ListCommitLog.ListObserveHandle) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 35 with ObserveHandle

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

the class TransformationRunner method runTransformation.

/**
 * Run given transformation in local JVM.
 *
 * @param direct the operator to run transformations with
 * @param name name of the transformation
 * @param desc the transformation to run
 * @param onReplicated callback to be called before write to replicated target
 * @return {@link ObserveHandle} of the transformation
 */
public static ObserveHandle runTransformation(DirectDataOperator direct, String name, TransformationDescriptor desc, Consumer<StreamElement> onReplicated) {
    final CommitLogObserver observer;
    if (desc.getTransformation().isContextual()) {
        observer = new TransformationObserver.Contextual(direct, name, desc.getTransformation().as(DirectElementWiseTransform.class), desc.getOutputTransactionMode() == OutputTransactionMode.ENABLED, desc.getFilter()) {

            @Override
            protected void onReplicated(StreamElement element) {
                onReplicated.accept(element);
            }
        };
    } else {
        observer = new TransformationObserver.NonContextual(direct, name, desc.getTransformation().asElementWiseTransform(), desc.getOutputTransactionMode() == OutputTransactionMode.ENABLED, desc.getFilter()) {

            @Override
            protected void onReplicated(StreamElement element) {
                onReplicated.accept(element);
            }
        };
    }
    CommitLogReader reader = desc.getAttributes().stream().flatMap(attr -> findFamilyDescriptorForAttribute(direct, attr)).findAny().flatMap(DirectAttributeFamilyDescriptor::getCommitLogReader).orElseThrow(() -> new IllegalStateException("No commit log reader for attributes of transformation " + desc));
    log.debug("Starting to observe reader {} with observer {} as {}", reader, observer, name);
    return reader.observe(name, observer);
}
Also used : CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) Consumer(cz.o2.proxima.functional.Consumer) Repository(cz.o2.proxima.repository.Repository) InputTransactionMode(cz.o2.proxima.repository.TransformationDescriptor.InputTransactionMode) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) StorageType(cz.o2.proxima.storage.StorageType) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) DirectElementWiseTransform(cz.o2.proxima.direct.transform.DirectElementWiseTransform) Slf4j(lombok.extern.slf4j.Slf4j) Stream(java.util.stream.Stream) StreamElement(cz.o2.proxima.storage.StreamElement) DirectAttributeFamilyDescriptor(cz.o2.proxima.direct.core.DirectAttributeFamilyDescriptor) TransformationObserver(cz.o2.proxima.direct.transform.TransformationObserver) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) TransformationDescriptor(cz.o2.proxima.repository.TransformationDescriptor) OutputTransactionMode(cz.o2.proxima.repository.TransformationDescriptor.OutputTransactionMode) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) StreamElement(cz.o2.proxima.storage.StreamElement) TransformationObserver(cz.o2.proxima.direct.transform.TransformationObserver)

Aggregations

StreamElement (cz.o2.proxima.storage.StreamElement)58 ObserveHandle (cz.o2.proxima.direct.commitlog.ObserveHandle)56 Test (org.junit.Test)52 CommitLogObserver (cz.o2.proxima.direct.commitlog.CommitLogObserver)47 CommitLogReader (cz.o2.proxima.direct.commitlog.CommitLogReader)45 CountDownLatch (java.util.concurrent.CountDownLatch)43 ArrayList (java.util.ArrayList)38 Offset (cz.o2.proxima.direct.commitlog.Offset)32 AtomicReference (java.util.concurrent.atomic.AtomicReference)30 List (java.util.List)29 Partition (cz.o2.proxima.storage.Partition)26 HashMap (java.util.HashMap)26 DirectDataOperator (cz.o2.proxima.direct.core.DirectDataOperator)25 AttributeDescriptor (cz.o2.proxima.repository.AttributeDescriptor)24 UUID (java.util.UUID)24 WatermarkEstimator (cz.o2.proxima.time.WatermarkEstimator)23 Collections (java.util.Collections)23 Collectors (java.util.stream.Collectors)23 Repository (cz.o2.proxima.repository.Repository)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22