Search in sources :

Example 71 with ObserveHandle

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

the class KafkaLogReaderIT method testReadFromOldestVerifyIsAtHead.

@Test(timeout = 30_000L)
public void testReadFromOldestVerifyIsAtHead() throws InterruptedException {
    final EmbeddedKafkaBroker embeddedKafka = rule.getEmbeddedKafka();
    final int numPartitions = 3;
    embeddedKafka.addTopics(new NewTopic("foo", numPartitions, (short) 1));
    final CommitLogReader commitLogReader = Optionals.get(operator.getCommitLogReader(fooDescriptor));
    // Write everything up front, so we can be sure that we really seek all the way to beginning.
    final int numElements = 100;
    await(writeElements(numElements));
    AtomicReference<ObserveHandle> handle = new AtomicReference<>();
    CountDownLatch firstLatch = new CountDownLatch(1);
    CountDownLatch lastLatch = new CountDownLatch(1);
    CommitLogObserver observer = new CommitLogObserver() {

        int consumed = 0;

        @Override
        public boolean onNext(StreamElement ingest, OnNextContext context) {
            context.confirm();
            if (consumed == 0) {
                ExceptionUtils.ignoringInterrupted(firstLatch::await);
            }
            if (++consumed == numElements) {
                lastLatch.countDown();
            }
            return true;
        }
    };
    handle.set(commitLogReader.observe("test-reader", Position.OLDEST, observer));
    assertFalse(ObserveHandleUtils.isAtHead(handle.get(), commitLogReader));
    firstLatch.countDown();
    lastLatch.await();
    assertTrue(ObserveHandleUtils.isAtHead(handle.get(), commitLogReader));
}
Also used : CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) StreamElement(cz.o2.proxima.storage.StreamElement) NewTopic(org.apache.kafka.clients.admin.NewTopic) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) EmbeddedKafkaBroker(org.springframework.kafka.test.EmbeddedKafkaBroker) Test(org.junit.Test)

Example 72 with ObserveHandle

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

the class LocalKafkaCommitLogDescriptorTest method testFetchOffsets.

@Test(timeout = 10_000)
public void testFetchOffsets() throws InterruptedException {
    Accessor accessor = kafka.createAccessor(direct, createTestFamily(entity, storageUri, partitionsCfg(3)));
    LocalKafkaWriter writer = accessor.newWriter();
    CommitLogReader reader = Optionals.get(accessor.getCommitLogReader(context()));
    final CountDownLatch latch = new CountDownLatch(2);
    final StreamElement[] updates = new StreamElement[] { StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), System.currentTimeMillis(), new byte[] { 1, 2 }), StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key2", attr.getName(), System.currentTimeMillis(), new byte[] { 1, 2, 3 }) };
    Arrays.stream(updates).forEach(update -> writer.write(update, (succ, exc) -> {
    }));
    CommitLogObserver observer = new CommitLogObserver() {

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

        @Override
        public void onCompleted() {
            fail("This should not be called");
        }

        @Override
        public boolean onError(Throwable error) {
            throw new RuntimeException(error);
        }
    };
    ObserveHandle handle = reader.observeBulkOffsets(reader.fetchOffsets(Position.OLDEST, reader.getPartitions()).values(), observer);
    latch.await();
    assertEquals(handle.getCommittedOffsets().stream().map(TopicOffset.class::cast).sorted(Comparator.comparing(tp -> tp.getPartition().getId())).collect(Collectors.toList()), reader.fetchOffsets(Position.NEWEST, reader.getPartitions()).values().stream().map(TopicOffset.class::cast).sorted(Comparator.comparing(tp -> tp.getPartition().getId())).collect(Collectors.toList()));
}
Also used : Arrays(java.util.Arrays) LocalKafkaLogReader(cz.o2.proxima.direct.kafka.LocalKafkaCommitLogDescriptor.LocalKafkaLogReader) LocalKafkaWriter(cz.o2.proxima.direct.kafka.LocalKafkaCommitLogDescriptor.LocalKafkaWriter) Partition(cz.o2.proxima.storage.Partition) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) CachedView(cz.o2.proxima.direct.view.CachedView) StreamElement(cz.o2.proxima.storage.StreamElement) WatermarkEstimator(cz.o2.proxima.time.WatermarkEstimator) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Pair(cz.o2.proxima.util.Pair) Serde(org.apache.kafka.common.serialization.Serde) UnaryFunction(cz.o2.proxima.functional.UnaryFunction) Duration(java.time.Duration) Map(java.util.Map) Serdes(org.apache.kafka.common.serialization.Serdes) URI(java.net.URI) WatermarkEstimatorFactory(cz.o2.proxima.time.WatermarkEstimatorFactory) Optionals(cz.o2.proxima.util.Optionals) WatermarkIdlePolicyFactory(cz.o2.proxima.time.WatermarkIdlePolicyFactory) TopicPartition(org.apache.kafka.common.TopicPartition) Collection(java.util.Collection) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) UUID(java.util.UUID) Accessor(cz.o2.proxima.direct.kafka.LocalKafkaCommitLogDescriptor.Accessor) Collectors(java.util.stream.Collectors) RebalanceInProgressException(org.apache.kafka.common.errors.RebalanceInProgressException) Executors(java.util.concurrent.Executors) Serializable(java.io.Serializable) CommitLogObservers(cz.o2.proxima.direct.commitlog.CommitLogObservers) Objects(java.util.Objects) CountDownLatch(java.util.concurrent.CountDownLatch) ConsumerRebalanceListener(org.apache.kafka.clients.consumer.ConsumerRebalanceListener) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) ConfigRepository(cz.o2.proxima.repository.ConfigRepository) Stream(java.util.stream.Stream) KeyValue(cz.o2.proxima.direct.randomaccess.KeyValue) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) OnNextContext(cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Context(cz.o2.proxima.direct.core.Context) IntStream(java.util.stream.IntStream) TestUtils.createTestFamily(cz.o2.proxima.util.TestUtils.createTestFamily) KeyPartitioner(cz.o2.proxima.storage.commitlog.KeyPartitioner) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) HashMap(java.util.HashMap) OnlineAttributeWriter(cz.o2.proxima.direct.core.OnlineAttributeWriter) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Lists(com.google.common.collect.Lists) ConfigFactory(com.typesafe.config.ConfigFactory) Watermarks(cz.o2.proxima.time.Watermarks) Factory(cz.o2.proxima.functional.Factory) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) ExecutorService(java.util.concurrent.ExecutorService) Nullable(javax.annotation.Nullable) WatermarkIdlePolicy(cz.o2.proxima.time.WatermarkIdlePolicy) Before(org.junit.Before) Properties(java.util.Properties) Repository(cz.o2.proxima.repository.Repository) Iterator(java.util.Iterator) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) MoreObjects(com.google.common.base.MoreObjects) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) Test(org.junit.Test) AttributeFamilyDescriptor(cz.o2.proxima.repository.AttributeFamilyDescriptor) Offset(cz.o2.proxima.direct.commitlog.Offset) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) AtomicLong(java.util.concurrent.atomic.AtomicLong) AttributeDescriptorBase(cz.o2.proxima.repository.AttributeDescriptorBase) Partitioner(cz.o2.proxima.storage.commitlog.Partitioner) Assert(org.junit.Assert) Comparator(java.util.Comparator) Collections(java.util.Collections) Position(cz.o2.proxima.storage.commitlog.Position) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) LocalKafkaWriter(cz.o2.proxima.direct.kafka.LocalKafkaCommitLogDescriptor.LocalKafkaWriter) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) OnNextContext(cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) Accessor(cz.o2.proxima.direct.kafka.LocalKafkaCommitLogDescriptor.Accessor) Test(org.junit.Test)

Example 73 with ObserveHandle

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

the class HBaseLogReaderTest method testObserveCancel.

@Test(timeout = 5000)
public void testObserveCancel() throws InterruptedException, IOException {
    long now = 1500000000000L;
    write("a", "dummy", "a", now);
    write("firs", "wildcard.1", "firs", now);
    write("fir", "dummy", "fir", now);
    write("first", "dummy", "first", now);
    List<Partition> partitions = reader.getPartitions();
    CountDownLatch latch = new CountDownLatch(1);
    AtomicReference<ObserveHandle> handle = new AtomicReference<>();
    handle.set(reader.observe(partitions.subList(0, 1), Collections.singletonList(attr), new BatchLogObserver() {

        @Override
        public boolean onNext(StreamElement element) {
            handle.get().close();
            return true;
        }

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

        @Override
        public void onCompleted() {
            fail("onCompleted should not heve been called");
        }
    }));
    latch.await();
}
Also used : Partition(cz.o2.proxima.storage.Partition) ObserveHandle(cz.o2.proxima.direct.batch.ObserveHandle) StreamElement(cz.o2.proxima.storage.StreamElement) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) BatchLogObserver(cz.o2.proxima.direct.batch.BatchLogObserver) Test(org.junit.Test)

Example 74 with ObserveHandle

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

the class WebsocketReader method observe.

private ObserveHandle observe(Consumer<StreamElement> onNext, Consumer<Throwable> onError) {
    WebSocketClient client = new WebSocketClient(getUri()) {

        @Override
        public void onOpen(ServerHandshake sh) {
            send(hello);
        }

        @Override
        public void onMessage(String m) {
            StreamElement elem = StreamElement.upsert(getEntityDescriptor(), attr, UUID.randomUUID().toString(), keyExtractor.apply(m), attr.getName(), System.currentTimeMillis(), m.getBytes(StandardCharsets.UTF_8));
            onNext.accept(elem);
        }

        @Override
        public void onClose(int code, String reason, boolean remote) {
            if (remote) {
                onError.accept(new RuntimeException("Server error: " + code + ": " + reason));
            }
        }

        @Override
        public void onError(Exception excptn) {
            onError.accept(excptn);
        }
    };
    client.connect();
    return new ObserveHandle() {

        @Override
        public void close() {
            client.close();
        }

        @Override
        public List<Offset> getCommittedOffsets() {
            return Arrays.asList(OFFSET);
        }

        @Override
        public void resetOffsets(List<Offset> offsets) {
        // nop
        }

        @Override
        public List<Offset> getCurrentOffsets() {
            return getCommittedOffsets();
        }

        @Override
        public void waitUntilReady() throws InterruptedException {
        // nop
        }
    };
}
Also used : ServerHandshake(org.java_websocket.handshake.ServerHandshake) ObserveHandle(cz.o2.proxima.direct.commitlog.ObserveHandle) StreamElement(cz.o2.proxima.storage.StreamElement) List(java.util.List) WebSocketClient(org.java_websocket.client.WebSocketClient) Offset(cz.o2.proxima.direct.commitlog.Offset)

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