Search in sources :

Example 51 with ObserveHandle

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

the class InMemStorageTest method testFetchOffsets.

private void testFetchOffsets(int numPartitions) throws InterruptedException {
    InMemStorage storage = new InMemStorage();
    DataAccessor accessor = storage.createAccessor(direct, createFamilyDescriptor(URI.create("inmem:///test"), numPartitions));
    CommitLogReader reader = Optionals.get(accessor.getCommitLogReader(direct.getContext()));
    AttributeWriterBase writer = Optionals.get(accessor.getWriter(direct.getContext()));
    long now = System.currentTimeMillis();
    List<StreamElement> updates = new ArrayList<>();
    for (int i = 0; i < 2 * numPartitions; i++) {
        updates.add(StreamElement.upsert(entity, data, UUID.randomUUID().toString(), "key" + (i + 1), data.getName(), now + i, new byte[] { 1, 2, 3 }));
    }
    updates.forEach(el -> writer.online().write(el, (succ, exc) -> {
    }));
    Map<Partition, Offset> startingOffsets = reader.fetchOffsets(Position.OLDEST, reader.getPartitions());
    assertEquals(numPartitions, startingOffsets.size());
    List<StreamElement> elements = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(1);
    CountDownLatch startLatch = new CountDownLatch(1);
    CommitLogObserver observer = LogObserverUtils.toList(elements, ign -> latch.countDown(), el -> {
        ExceptionUtils.ignoringInterrupted(startLatch::await);
        return true;
    });
    ObserveHandle handle = reader.observeBulkOffsets(startingOffsets.values(), true, observer);
    assertFalse(ObserveHandleUtils.isAtHead(handle, reader));
    startLatch.countDown();
    latch.await();
    assertEquals(2 * numPartitions, elements.size());
    List<Offset> committed = handle.getCommittedOffsets();
    Map<Partition, Offset> endOffsets2 = reader.fetchOffsets(Position.NEWEST, committed.stream().map(Offset::getPartition).collect(Collectors.toList()));
    assertTrue(ObserveHandleUtils.isAtHead(handle, reader));
    assertEquals(IntStream.range(0, 2 * numPartitions).mapToObj(i -> "key" + (i + 1)).collect(Collectors.toList()), elements.stream().map(StreamElement::getKey).collect(Collectors.toList()));
    elements.clear();
    Map<Partition, Offset> endOffsets = reader.fetchOffsets(Position.NEWEST, reader.getPartitions());
    assertEquals(numPartitions, endOffsets.size());
    CountDownLatch latch2 = new CountDownLatch(1);
    observer = LogObserverUtils.toList(elements, ign -> latch2.countDown());
    reader.observeBulkOffsets(endOffsets.values(), true, observer);
    latch2.await();
    assertEquals(numPartitions, elements.size());
    assertEquals(IntStream.range(numPartitions, 2 * numPartitions).mapToObj(i -> "key" + (i + 1)).collect(Collectors.toList()), elements.stream().map(StreamElement::getKey).collect(Collectors.toList()));
}
Also used : Partitioners(cz.o2.proxima.storage.commitlog.Partitioners) Arrays(java.util.Arrays) AttributeWriterBase(cz.o2.proxima.direct.core.AttributeWriterBase) BatchLogReader(cz.o2.proxima.direct.batch.BatchLogReader) Partition(cz.o2.proxima.storage.Partition) KeyAttributePartitioner(cz.o2.proxima.storage.commitlog.KeyAttributePartitioner) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) StorageType(cz.o2.proxima.storage.StorageType) ExceptionUtils(cz.o2.proxima.util.ExceptionUtils) StreamElement(cz.o2.proxima.storage.StreamElement) WatermarkEstimator(cz.o2.proxima.time.WatermarkEstimator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) SerializationException(cz.o2.proxima.scheme.SerializationException) AccessType(cz.o2.proxima.storage.AccessType) URI(java.net.URI) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Optionals(cz.o2.proxima.util.Optionals) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) UUID(java.util.UUID) CommitCallback(cz.o2.proxima.direct.core.CommitCallback) Collectors(java.util.stream.Collectors) Serializable(java.io.Serializable) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) IntStream(java.util.stream.IntStream) Iterables(com.google.common.collect.Iterables) ConsumedOffset(cz.o2.proxima.direct.storage.InMemStorage.ConsumedOffset) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) DataAccessor(cz.o2.proxima.direct.core.DataAccessor) ConfigFactory(com.typesafe.config.ConfigFactory) Watermarks(cz.o2.proxima.time.Watermarks) ObserveHandleUtils(cz.o2.proxima.direct.commitlog.ObserveHandleUtils) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) 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) BatchLogObserver(cz.o2.proxima.direct.batch.BatchLogObserver) Test(org.junit.Test) AttributeFamilyDescriptor(cz.o2.proxima.repository.AttributeFamilyDescriptor) LogObserverUtils(cz.o2.proxima.direct.commitlog.LogObserverUtils) Offset(cz.o2.proxima.direct.commitlog.Offset) TimeUnit(java.util.concurrent.TimeUnit) Partitioner(cz.o2.proxima.storage.commitlog.Partitioner) Preconditions(com.google.common.base.Preconditions) Assert(org.junit.Assert) Collections(java.util.Collections) Position(cz.o2.proxima.storage.commitlog.Position) Partition(cz.o2.proxima.storage.Partition) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) DataAccessor(cz.o2.proxima.direct.core.DataAccessor) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) AttributeWriterBase(cz.o2.proxima.direct.core.AttributeWriterBase) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) ConsumedOffset(cz.o2.proxima.direct.storage.InMemStorage.ConsumedOffset) Offset(cz.o2.proxima.direct.commitlog.Offset) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver)

Example 52 with ObserveHandle

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

the class InMemStorageTest method testObserveCancel.

@Test(timeout = 10000)
public void testObserveCancel() {
    InMemStorage storage = new InMemStorage();
    DataAccessor accessor = storage.createAccessor(direct, createFamilyDescriptor(URI.create("inmem:///inmemstoragetest")));
    CommitLogReader reader = Optionals.get(accessor.getCommitLogReader(direct.getContext()));
    AttributeWriterBase writer = Optionals.get(accessor.getWriter(direct.getContext()));
    List<Byte> received = new ArrayList<>();
    ObserveHandle handle = reader.observePartitions(reader.getPartitions(), new CommitLogObserver() {

        @Override
        public void onRepartition(CommitLogObserver.OnRepartitionContext context) {
            assertEquals(1, context.partitions().size());
        }

        @Override
        public boolean onNext(StreamElement ingest, CommitLogObserver.OnNextContext context) {
            assertEquals(0, context.getPartition().getId());
            assertEquals("key", ingest.getKey());
            context.confirm();
            received.add(ingest.getValue()[0]);
            return false;
        }

        @Override
        public boolean onError(Throwable error) {
            throw new RuntimeException(error);
        }
    });
    writer.online().write(StreamElement.upsert(entity, data, UUID.randomUUID().toString(), "key", data.getName(), System.currentTimeMillis(), new byte[] { 1 }), (succ, exc) -> {
    });
    List<Offset> offsets = handle.getCurrentOffsets();
    assertEquals(1, offsets.size());
    assertEquals(Collections.singletonList((byte) 1), received);
    handle.close();
    writer.online().write(StreamElement.upsert(entity, data, UUID.randomUUID().toString(), "key", data.getName(), System.currentTimeMillis(), new byte[] { 2 }), (succ, exc) -> {
    });
    assertEquals(Collections.singletonList((byte) 1), received);
}
Also used : ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) DataAccessor(cz.o2.proxima.direct.core.DataAccessor) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) AttributeWriterBase(cz.o2.proxima.direct.core.AttributeWriterBase) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) ConsumedOffset(cz.o2.proxima.direct.storage.InMemStorage.ConsumedOffset) Offset(cz.o2.proxima.direct.commitlog.Offset) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) Test(org.junit.Test)

Example 53 with ObserveHandle

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

the class ReplicationControllerTest method testSimpleEventReplicationWithFilter.

@Test
public void testSimpleEventReplicationWithFilter() {
    final 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), ingest -> false, fakeOnlineWriter(written));
    try (ObserveHandle ignored = reader.observe("consumer", observer)) {
        writeEvent();
        assertEquals(0, written.size());
    }
}
Also used : CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) 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 54 with ObserveHandle

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

the class ReplicationRunner method runAttributeReplicas.

/**
 * Run replications of attributes to replica attribute families.
 *
 * @param direct {@link DirectDataOperator} direct data operator
 * @param onReplicated callback called for each replicated element
 */
public static void runAttributeReplicas(DirectDataOperator direct, Consumer<StreamElement> onReplicated) {
    direct.getAllFamilies().filter(af -> af.getDesc().getType() == StorageType.REPLICA).filter(af -> !af.getDesc().getAccess().isReadonly() && !af.getDesc().isProxy()).forEach(af -> {
        List<AttributeDescriptor<?>> attributes = af.getAttributes();
        final AttributeWriterBase writer = Optionals.get(af.getWriter());
        final CommitLogReader primaryCommitLogReader = Optionals.get(attributes.stream().flatMap(a -> direct.getFamiliesForAttribute(a).stream()).filter(f -> f.getDesc().getType() == StorageType.PRIMARY).distinct().findFirst().flatMap(DirectAttributeFamilyDescriptor::getCommitLogReader));
        final ObserveHandle handle;
        if (writer instanceof OnlineAttributeWriter) {
            final OnlineAttributeWriter onlineWriter = writer.online();
            handle = primaryCommitLogReader.observe(af.getDesc().getName(), (CommitLogObserver) (ingest, context) -> {
                log.debug("Replicating input {} to {}", ingest, writer);
                onlineWriter.write(ingest, (succ, exc) -> {
                    context.commit(succ, exc);
                    onReplicated.accept(ingest);
                });
                return true;
            });
        } else {
            final BulkAttributeWriter bulkWriter = writer.bulk();
            handle = primaryCommitLogReader.observe(af.getDesc().getName(), (CommitLogObserver) (ingest, context) -> {
                log.debug("Replicating input {} to {}", ingest, writer);
                bulkWriter.write(ingest, context.getWatermark(), (succ, exc) -> {
                    context.commit(succ, exc);
                    onReplicated.accept(ingest);
                });
                return true;
            });
        }
        ExceptionUtils.unchecked(handle::waitUntilReady);
        log.info("Started attribute replica {}", af.getDesc().getName());
    });
}
Also used : BulkAttributeWriter(cz.o2.proxima.direct.core.BulkAttributeWriter) AttributeWriterBase(cz.o2.proxima.direct.core.AttributeWriterBase) Consumer(cz.o2.proxima.functional.Consumer) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) StorageType(cz.o2.proxima.storage.StorageType) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) OnlineAttributeWriter(cz.o2.proxima.direct.core.OnlineAttributeWriter) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) StreamElement(cz.o2.proxima.storage.StreamElement) DirectAttributeFamilyDescriptor(cz.o2.proxima.direct.core.DirectAttributeFamilyDescriptor) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) OnlineAttributeWriter(cz.o2.proxima.direct.core.OnlineAttributeWriter) AttributeWriterBase(cz.o2.proxima.direct.core.AttributeWriterBase) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) BulkAttributeWriter(cz.o2.proxima.direct.core.BulkAttributeWriter)

Example 55 with ObserveHandle

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

the class BlobLogReader method observe.

@Override
public ObserveHandle observe(List<Partition> partitions, List<AttributeDescriptor<?>> attributes, BatchLogObserver observer) {
    TerminationContext terminationContext = new TerminationContext(observer);
    observeInternal(partitions, attributes, observer, terminationContext);
    return terminationContext.asObserveHandle();
}
Also used : TerminationContext(cz.o2.proxima.direct.batch.TerminationContext)

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