Search in sources :

Example 1 with OnlineAttributeWriter

use of cz.o2.proxima.direct.core.OnlineAttributeWriter in project proxima-platform by O2-Czech-Republic.

the class LocalKafkaCommitLogDescriptorTest method testWriteNull.

@Test
public void testWriteNull() {
    LocalKafkaCommitLogDescriptor.Accessor accessor;
    accessor = kafka.createAccessor(direct, createTestFamily(entity, storageUri, partitionsCfg(2)));
    OnlineAttributeWriter writer = Optionals.get(accessor.getWriter(context())).online();
    long now = 1234567890000L;
    KafkaConsumer<Object, Object> consumer = accessor.createConsumerFactory().create();
    writer.write(StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), now, new byte[] { 1 }), (succ, exc) -> {
    });
    writer.write(StreamElement.delete(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), now + 1000), (succ, exc) -> {
    });
    ConsumerRecords<Object, Object> polled = consumer.poll(Duration.ofMillis(100));
    assertEquals(2, polled.count());
    int matched = 0;
    for (ConsumerRecord<Object, Object> r : polled) {
        if (r.timestamp() == now) {
            assertEquals(1, ((byte[]) r.value()).length);
            matched++;
        } else if (r.timestamp() == now + 1000) {
            assertNull(r.value());
            matched++;
        }
    }
    assertEquals(2, matched);
}
Also used : Accessor(cz.o2.proxima.direct.kafka.LocalKafkaCommitLogDescriptor.Accessor) OnlineAttributeWriter(cz.o2.proxima.direct.core.OnlineAttributeWriter) Test(org.junit.Test)

Example 2 with OnlineAttributeWriter

use of cz.o2.proxima.direct.core.OnlineAttributeWriter in project proxima-platform by O2-Czech-Republic.

the class KafkaLogReaderIT method writePoisonedPills.

/**
 * Write poisoned pills (element with timestamp = {@link Watermarks#MAX_WATERMARK}) to all
 * partitions.
 *
 * @param numPartitions Number of partitions in topic.
 * @return Completion latch.
 */
private CountDownLatch writePoisonedPills(int numPartitions) {
    final OnlineAttributeWriter writer = Optionals.get(operator.getWriter(fooDescriptor));
    // We assume test uses default partitioner.
    final KeyPartitioner keyPartitioner = new KeyPartitioner();
    final Set<Integer> poisonedPartitions = new HashSet<>();
    final CountDownLatch done = new CountDownLatch(numPartitions);
    for (int i = 0; poisonedPartitions.size() < numPartitions; i++) {
        final StreamElement poisonedPill = StreamElement.upsert(entity, fooDescriptor, UUID.randomUUID().toString(), String.format("poisoned-pill-%d", i), fooDescriptor.getName(), Watermarks.MAX_WATERMARK, "value".getBytes(StandardCharsets.UTF_8));
        final int partition = (keyPartitioner.getPartitionId(poisonedPill) & Integer.MAX_VALUE) % numPartitions;
        if (poisonedPartitions.add(partition)) {
            writer.write(poisonedPill, ((success, error) -> {
                if (success) {
                    done.countDown();
                }
            }));
        }
    }
    return done;
}
Also used : Arrays(java.util.Arrays) Partition(cz.o2.proxima.storage.Partition) ConfigValueFactory(com.typesafe.config.ConfigValueFactory) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) Random(java.util.Random) ExceptionUtils(cz.o2.proxima.util.ExceptionUtils) StreamElement(cz.o2.proxima.storage.StreamElement) Map(java.util.Map) Optionals(cz.o2.proxima.util.Optionals) KafkaStreamElementSerializer(cz.o2.proxima.direct.kafka.KafkaStreamElement.KafkaStreamElementSerializer) Set(java.util.Set) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) UUID(java.util.UUID) ConfigResolveOptions(com.typesafe.config.ConfigResolveOptions) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Objects(java.util.Objects) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Pattern(java.util.regex.Pattern) KeyPartitioner(cz.o2.proxima.storage.commitlog.KeyPartitioner) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) HashMap(java.util.HashMap) OnlineAttributeWriter(cz.o2.proxima.direct.core.OnlineAttributeWriter) EmbeddedKafkaBroker(org.springframework.kafka.test.EmbeddedKafkaBroker) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) KafkaProducer(org.apache.kafka.clients.producer.KafkaProducer) 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) Before(org.junit.Before) Properties(java.util.Properties) Repository(cz.o2.proxima.repository.Repository) Config(com.typesafe.config.Config) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) NewTopic(org.apache.kafka.clients.admin.NewTopic) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) EmbeddedKafkaRule(org.springframework.kafka.test.rule.EmbeddedKafkaRule) Rule(org.junit.Rule) Ignore(org.junit.Ignore) Assert(org.junit.Assert) Collections(java.util.Collections) Position(cz.o2.proxima.storage.commitlog.Position) KeyPartitioner(cz.o2.proxima.storage.commitlog.KeyPartitioner) OnlineAttributeWriter(cz.o2.proxima.direct.core.OnlineAttributeWriter) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) HashSet(java.util.HashSet)

Example 3 with OnlineAttributeWriter

use of cz.o2.proxima.direct.core.OnlineAttributeWriter in project proxima-platform by O2-Czech-Republic.

the class ModelTest method testReadWrite.

@Test
public void testReadWrite() throws InterruptedException {
    Regular<BaseEvent> desc = model.getEvent().getDataDescriptor();
    Optional<OnlineAttributeWriter> writer = model.directOperator().getWriter(desc);
    writer.get().write(desc.upsert(UUID.randomUUID().toString(), System.currentTimeMillis(), BaseEvent.newBuilder().setUserName("user").build()), (succ, exc) -> {
    });
    Optional<CommitLogReader> reader = model.directOperator().getCommitLogReader(desc);
    List<String> users = new ArrayList<>();
    CountDownLatch latch = new CountDownLatch(1);
    reader.get().observeBulk("consumer", Position.OLDEST, true, new CommitLogObserver() {

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

        @Override
        public void onCompleted() {
            latch.countDown();
        }

        @Override
        public boolean onNext(StreamElement ingest, OnNextContext context) {
            Optional<BaseEvent> baseEvent = desc.valueOf(ingest);
            users.add(baseEvent.get().getUserName());
            return true;
        }
    });
    latch.await();
    assertEquals("user", users.get(0));
}
Also used : Optional(java.util.Optional) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) ArrayList(java.util.ArrayList) BaseEvent(cz.o2.proxima.example.event.Event.BaseEvent) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) OnlineAttributeWriter(cz.o2.proxima.direct.core.OnlineAttributeWriter) Test(org.junit.Test)

Example 4 with OnlineAttributeWriter

use of cz.o2.proxima.direct.core.OnlineAttributeWriter in project proxima-platform by O2-Czech-Republic.

the class DirectBatchUnboundedSourceTest method testOffsetWithNoAdvance.

@Test
public void testOffsetWithNoAdvance() throws Exception {
    PipelineOptions opts = PipelineOptionsFactory.create();
    Repository repo = Repository.ofTest(ConfigFactory.load("test-reference.conf").resolve());
    DirectBatchUnboundedSource source = DirectBatchUnboundedSource.of(repo.asFactory(), Optionals.get(direct.getBatchLogReader(armed)), Collections.singletonList(repo.getEntity("gateway").getAttribute("armed")), Long.MIN_VALUE, Long.MAX_VALUE, Collections.emptyMap());
    List<? extends UnboundedSource<StreamElement, Checkpoint>> split = source.split(1, opts);
    assertEquals(1, split.size());
    OnlineAttributeWriter writer = Optionals.get(direct.getWriter(armed));
    long now = System.currentTimeMillis();
    writer.write(StreamElement.upsert(gateway, armed, UUID.randomUUID().toString(), "key", armed.getName(), now, new byte[] {}), (succ, exc) -> {
    });
    UnboundedReader<StreamElement> reader = split.get(0).createReader(opts, null);
    while (!reader.advance()) {
        TimeUnit.MILLISECONDS.sleep(10);
    }
    Checkpoint checkpoint = (Checkpoint) reader.getCheckpointMark();
    assertNotNull(checkpoint);
    assertEquals(1, checkpoint.getSkipFromFirst());
    reader = split.get(0).createReader(opts, checkpoint);
    checkpoint = (Checkpoint) reader.getCheckpointMark();
    TestUtils.assertSerializable(source);
    TestUtils.assertHashCodeAndEquals(source, SerializableUtils.clone(source));
    assertEquals(checkpoint, reader.getCheckpointMark());
    TestUtils.assertSerializable(checkpoint);
    TestUtils.assertHashCodeAndEquals(checkpoint, SerializableUtils.clone(checkpoint));
}
Also used : Repository(cz.o2.proxima.repository.Repository) Checkpoint(cz.o2.proxima.beam.direct.io.DirectBatchUnboundedSource.Checkpoint) PipelineOptions(org.apache.beam.sdk.options.PipelineOptions) OnlineAttributeWriter(cz.o2.proxima.direct.core.OnlineAttributeWriter) StreamElement(cz.o2.proxima.storage.StreamElement) Test(org.junit.Test)

Example 5 with OnlineAttributeWriter

use of cz.o2.proxima.direct.core.OnlineAttributeWriter in project proxima-platform by O2-Czech-Republic.

the class DirectUnboundedSourceTest method testCheckpointWithNoAdvance.

@Test
public void testCheckpointWithNoAdvance() throws Exception {
    OnlineAttributeWriter writer = Optionals.get(direct.getWriter(armed));
    CommitLogReader commitLogReader = Optionals.get(direct.getCommitLogReader(armed));
    DirectUnboundedSource source = DirectUnboundedSource.of(repo.asFactory(), "name", commitLogReader, Position.OLDEST, true, Long.MAX_VALUE);
    long now = System.currentTimeMillis();
    writer.write(StreamElement.upsert(gateway, armed, UUID.randomUUID().toString(), "key", armed.getName(), now, new byte[] {}), (succ, exc) -> {
    });
    PipelineOptions opts = PipelineOptionsFactory.create();
    UnboundedSource<StreamElement, Checkpoint> split = source.split(1, opts).get(0);
    UnboundedReader<StreamElement> reader = split.createReader(opts, null);
    boolean start = reader.start();
    while (!start && !reader.advance()) {
        TimeUnit.MILLISECONDS.sleep(10);
    }
    Checkpoint mark = (Checkpoint) reader.getCheckpointMark();
    assertNotNull(mark);
    reader = split.createReader(opts, mark);
    TestUtils.assertSerializable(source);
    TestUtils.assertHashCodeAndEquals(source, SerializableUtils.clone(source));
    assertEquals(mark, reader.getCheckpointMark());
    TestUtils.assertSerializable(mark);
    TestUtils.assertHashCodeAndEquals(mark, SerializableUtils.clone(mark));
}
Also used : Checkpoint(cz.o2.proxima.beam.direct.io.DirectUnboundedSource.Checkpoint) PipelineOptions(org.apache.beam.sdk.options.PipelineOptions) OnlineAttributeWriter(cz.o2.proxima.direct.core.OnlineAttributeWriter) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) StreamElement(cz.o2.proxima.storage.StreamElement) Test(org.junit.Test)

Aggregations

OnlineAttributeWriter (cz.o2.proxima.direct.core.OnlineAttributeWriter)32 StreamElement (cz.o2.proxima.storage.StreamElement)32 Test (org.junit.Test)32 CountDownLatch (java.util.concurrent.CountDownLatch)29 EntityDescriptor (cz.o2.proxima.repository.EntityDescriptor)22 CommitLogReader (cz.o2.proxima.direct.commitlog.CommitLogReader)20 AttributeDescriptor (cz.o2.proxima.repository.AttributeDescriptor)18 CommitLogObserver (cz.o2.proxima.direct.commitlog.CommitLogObserver)17 ConfigFactory (com.typesafe.config.ConfigFactory)16 ArrayList (java.util.ArrayList)16 UUID (java.util.UUID)15 CachedView (cz.o2.proxima.direct.view.CachedView)14 List (java.util.List)14 AtomicReference (java.util.concurrent.atomic.AtomicReference)14 KeyValue (cz.o2.proxima.direct.randomaccess.KeyValue)13 Collections (java.util.Collections)13 Set (java.util.Set)13 Config (com.typesafe.config.Config)12 TimeUnit (java.util.concurrent.TimeUnit)12 StorageType (cz.o2.proxima.storage.StorageType)11