Search in sources :

Example 61 with CommitLogReader

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

the class ReplicationController method consumeLog.

private void consumeLog(DirectAttributeFamilyDescriptor primaryFamily, DirectAttributeFamilyDescriptor replicaFamily) {
    final CommitLogReader commitLog = primaryFamily.getCommitLogReader().orElseThrow(() -> new IllegalStateException(String.format("Failed to find commit-log reader in family %s.", primaryFamily)));
    final AttributeWriterBase writer = replicaFamily.getWriter().orElseThrow(() -> new IllegalStateException(String.format("Unable to get writer for family %s.", replicaFamily.getDesc().getName())));
    final StorageFilter filter = replicaFamily.getDesc().getFilter();
    final Set<AttributeDescriptor<?>> allowedAttributes = new HashSet<>(replicaFamily.getAttributes());
    final String name = replicaFamily.getDesc().getReplicationConsumerNameFactory().apply();
    log.info("Using consumer name {} to replicate family {}", name, replicaFamily.getDesc().getName());
    registerWriterTo(name, commitLog, allowedAttributes, filter, writer);
    log.info("Started consumer {} consuming from log {} with URI {} into {} attributes {}", name, commitLog, commitLog.getUri(), writer.getUri(), allowedAttributes);
}
Also used : StorageFilter(cz.o2.proxima.storage.StorageFilter) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) AttributeWriterBase(cz.o2.proxima.direct.core.AttributeWriterBase) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) HashSet(java.util.HashSet)

Example 62 with CommitLogReader

use of cz.o2.proxima.direct.commitlog.CommitLogReader 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 63 with CommitLogReader

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

the class SingleTopicMultipleReplicationsTest method testProxyReadWildcardReplicated.

@Test(timeout = 5000)
public void testProxyReadWildcardReplicated() throws InterruptedException {
    Optional<AttributeFamilyDescriptor> wildcardPrimary = repo.getFamiliesForAttribute(wildcard).stream().filter(af -> af.getType() == StorageType.PRIMARY).findFirst();
    assertTrue(wildcardPrimary.isPresent());
    AttributeFamilyDescriptor family = wildcardPrimary.get();
    assertEquals("proxy::proxy::replication_wildcard-replication_replicated::" + "replication_wildcard-replication_write::proxy::" + "replication_wildcard-replication_replicated::" + "replication_wildcard-replication_write", family.getName());
    assertTrue(family.getAccess().canReadCommitLog());
    CommitLogReader reader = direct.resolveRequired(family).getCommitLogReader().orElseThrow(() -> new IllegalStateException("Missing commit-log in " + family));
    CountDownLatch latch = new CountDownLatch(1);
    reader.observe("dummy", new CommitLogObserver() {

        @Override
        public boolean onNext(StreamElement ingest, OnNextContext context) {
            assertEquals(wildcard, ingest.getAttributeDescriptor());
            assertEquals("wildcard.1", ingest.getAttribute());
            latch.countDown();
            context.confirm();
            return false;
        }

        @Override
        public boolean onError(Throwable error) {
            throw new RuntimeException(error);
        }
    }).waitUntilReady();
    OnlineAttributeWriter writer = direct.getWriter(wildcardInput).orElseThrow(() -> new IllegalStateException("Missing writer for " + wildcardInput));
    writer.write(StreamElement.upsert(entity, wildcard, uuid(), "key", wildcard.toAttributePrefix() + "1", now, value()), (succ, exc) -> {
    });
    assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : Repository(cz.o2.proxima.repository.Repository) ElementWiseProxyTransform(cz.o2.proxima.transform.ElementWiseProxyTransform) 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) Test(org.junit.Test) AttributeFamilyDescriptor(cz.o2.proxima.repository.AttributeFamilyDescriptor) UUID(java.util.UUID) OnlineAttributeWriter(cz.o2.proxima.direct.core.OnlineAttributeWriter) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) StreamElement(cz.o2.proxima.storage.StreamElement) TransformationRunner(cz.o2.proxima.util.TransformationRunner) ConfigFactory(com.typesafe.config.ConfigFactory) Optional(java.util.Optional) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) TransformationDescriptor(cz.o2.proxima.repository.TransformationDescriptor) Position(cz.o2.proxima.storage.commitlog.Position) Before(org.junit.Before) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) OnlineAttributeWriter(cz.o2.proxima.direct.core.OnlineAttributeWriter) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) AttributeFamilyDescriptor(cz.o2.proxima.repository.AttributeFamilyDescriptor) Test(org.junit.Test)

Example 64 with CommitLogReader

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

the class SingleTopicMultipleReplicationsTest method testProxyObserveBulkPartitionsWildcardReplicated.

@Test(timeout = 5000)
public void testProxyObserveBulkPartitionsWildcardReplicated() throws InterruptedException {
    Optional<AttributeFamilyDescriptor> wildcardPrimary = repo.getFamiliesForAttribute(wildcard).stream().filter(af -> af.getType() == StorageType.PRIMARY).findFirst();
    assertTrue(wildcardPrimary.isPresent());
    AttributeFamilyDescriptor family = wildcardPrimary.get();
    assertTrue(family.getAccess().canReadCommitLog());
    CommitLogReader reader = direct.resolveRequired(family).getCommitLogReader().orElseThrow(() -> new IllegalStateException("Missing commit-log in " + family));
    CountDownLatch latch = new CountDownLatch(1);
    reader.observeBulkPartitions(reader.getPartitions(), Position.CURRENT, new CommitLogObserver() {

        @Override
        public boolean onNext(StreamElement ingest, OnNextContext context) {
            assertEquals(wildcard, ingest.getAttributeDescriptor());
            assertEquals("wildcard.1", ingest.getAttribute());
            latch.countDown();
            return false;
        }

        @Override
        public boolean onError(Throwable error) {
            throw new RuntimeException(error);
        }
    }).waitUntilReady();
    OnlineAttributeWriter writer = direct.getWriter(wildcardInput).orElseThrow(() -> new IllegalStateException("Missing writer for " + wildcardInput));
    writer.write(StreamElement.upsert(entity, wildcard, uuid(), "key", wildcard.toAttributePrefix() + "1", now, value()), (succ, exc) -> {
    });
    assertTrue(latch.await(1, TimeUnit.SECONDS));
}
Also used : Repository(cz.o2.proxima.repository.Repository) ElementWiseProxyTransform(cz.o2.proxima.transform.ElementWiseProxyTransform) 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) Test(org.junit.Test) AttributeFamilyDescriptor(cz.o2.proxima.repository.AttributeFamilyDescriptor) UUID(java.util.UUID) OnlineAttributeWriter(cz.o2.proxima.direct.core.OnlineAttributeWriter) ArrayList(java.util.ArrayList) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) StreamElement(cz.o2.proxima.storage.StreamElement) TransformationRunner(cz.o2.proxima.util.TransformationRunner) ConfigFactory(com.typesafe.config.ConfigFactory) Optional(java.util.Optional) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) TransformationDescriptor(cz.o2.proxima.repository.TransformationDescriptor) Position(cz.o2.proxima.storage.commitlog.Position) Before(org.junit.Before) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) OnlineAttributeWriter(cz.o2.proxima.direct.core.OnlineAttributeWriter) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) AttributeFamilyDescriptor(cz.o2.proxima.repository.AttributeFamilyDescriptor) Test(org.junit.Test)

Example 65 with CommitLogReader

use of cz.o2.proxima.direct.commitlog.CommitLogReader 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)

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