Search in sources :

Example 1 with Watermarks

use of cz.o2.proxima.time.Watermarks 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 2 with Watermarks

use of cz.o2.proxima.time.Watermarks in project proxima-platform by O2-Czech-Republic.

the class AbstractBulkFileSystemAttributeWriterTest method testWriteLate.

@Test(timeout = 10000)
public synchronized void testWriteLate() throws Exception {
    CountDownLatch latch = new CountDownLatch(2);
    long now = 1500000000000L;
    StreamElement[] elements = { // not late
    StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key1", "attr", now + 200, new byte[] { 1 }), // not late
    StreamElement.upsert(entity, wildcard, UUID.randomUUID().toString(), "key2", "wildcard.1", now, new byte[] { 1, 2 }), // not late, flushes previous two
    StreamElement.upsert(entity, wildcard, UUID.randomUUID().toString(), "key3", "wildcard.1", now + params.getRollPeriod() + 1, new byte[] { 1, 2, 3 }), // late, not flushed
    StreamElement.upsert(entity, wildcard, UUID.randomUUID().toString(), "key4", "wildcard.1", now + params.getRollPeriod() / 2 + 1, new byte[] { 1, 2, 3, 4 }), // not late, flush
    StreamElement.upsert(entity, wildcard, UUID.randomUUID().toString(), "key5", "wildcard.1", now + 3 * params.getRollPeriod(), new byte[] { 1, 2, 3, 4, 5 }) };
    List<Long> watermarks = new ArrayList<>(Arrays.asList(now, now, now + 2 * params.getRollPeriod() + params.getAllowedLateness() + 1, now + 2 * params.getRollPeriod() + params.getAllowedLateness() + 1, now + 3 * params.getRollPeriod() + params.getAllowedLateness()));
    Set<Long> committed = Collections.synchronizedSet(new HashSet<>());
    Arrays.stream(elements).forEach(e -> write(e, watermarks.remove(0), (succ, exc) -> {
        assertTrue("Exception " + exc, succ);
        assertNull(exc);
        committed.add(e.getStamp());
        latch.countDown();
    }));
    long watermark = now + 4 * params.getRollPeriod() + 1 + params.getAllowedLateness();
    while (true) {
        writer.bulk().updateWatermark(watermark);
        if (latch.await(50, TimeUnit.MILLISECONDS)) {
            break;
        }
    }
    assertEquals("Written: " + written.keySet(), 3, written.size());
    validate(written.get(now + params.getRollPeriod()), elements[0], elements[1]);
    validate(written.get(now + params.getRollPeriod() + 1), elements[2], elements[3]);
    validate(written.get(now + 4 * params.getRollPeriod()), elements[4]);
    assertEquals("Expected three paths, got " + flushedPaths, 3, flushedPaths.size());
    assertEquals(2, committed.size());
    assertEquals(Sets.newHashSet(now, now + 3 * params.getRollPeriod()), committed);
}
Also used : Iterables(com.google.common.collect.Iterables) Arrays(java.util.Arrays) URISyntaxException(java.net.URISyntaxException) RunWith(org.junit.runner.RunWith) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Value(lombok.Value) HashSet(java.util.HashSet) ExceptionUtils(cz.o2.proxima.util.ExceptionUtils) Lists(com.google.common.collect.Lists) StreamElement(cz.o2.proxima.storage.StreamElement) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) After(org.junit.After) ConfigFactory(com.typesafe.config.ConfigFactory) URI(java.net.URI) Parameterized(org.junit.runners.Parameterized) Before(org.junit.Before) BulkAttributeWriter(cz.o2.proxima.direct.core.BulkAttributeWriter) Repository(cz.o2.proxima.repository.Repository) Iterator(java.util.Iterator) Collection(java.util.Collection) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) UUID(java.util.UUID) CommitCallback(cz.o2.proxima.direct.core.CommitCallback) Sets(com.google.common.collect.Sets) File(java.io.File) Serializable(java.io.Serializable) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Rule(org.junit.Rule) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) TemporaryFolder(org.junit.rules.TemporaryFolder) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 3 with Watermarks

use of cz.o2.proxima.time.Watermarks in project proxima-platform by O2-Czech-Republic.

the class AbstractBulkFileSystemAttributeWriterTest method testFlushingOnOutOfOrder.

@Test(timeout = 10000)
public synchronized void testFlushingOnOutOfOrder() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    long now = 1500000000000L;
    StreamElement[] elements = { StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", "attr", now + 200, new byte[] { 1 }), StreamElement.upsert(entity, wildcard, UUID.randomUUID().toString(), "key", "wildcard.1", now, new byte[] { 1, 2 }), StreamElement.upsert(entity, wildcard, UUID.randomUUID().toString(), "key", "wildcard.1", now + params.getRollPeriod(), new byte[] { 1, 2, 3 }), StreamElement.upsert(entity, wildcard, UUID.randomUUID().toString(), "key", "wildcard.1", now + params.getRollPeriod() / 2, new byte[] { 1, 2, 3, 4 }), StreamElement.upsert(entity, wildcard, UUID.randomUUID().toString(), "key", "wildcard.1", now + 3 * params.getRollPeriod(), new byte[] { 1, 2, 3, 4, 5 }) };
    List<Long> watermarks = new ArrayList<>(Arrays.asList(now, now, now + params.getRollPeriod() / 2, now + params.getRollPeriod() / 2, now + 3 * params.getRollPeriod() - params.getRollPeriod() / 2));
    Arrays.stream(elements).forEach(e -> write(e, watermarks.remove(0), (succ, exc) -> {
        assertTrue("Exception " + exc, succ);
        assertNull(exc);
        assertEquals(now + params.getRollPeriod() / 2, e.getStamp());
        latch.countDown();
    }));
    latch.await();
    assertEquals(2, written.size());
    validate(written.get(now + params.getRollPeriod()), elements[0], elements[1], elements[3]);
    validate(written.get(now + 2 * params.getRollPeriod()), elements[2]);
    assertEquals("Expected two paths, got " + flushedPaths, 2, flushedPaths.size());
    assertTrue("Invalid range for " + Iterables.get(flushedPaths, 0).toString(), params.getNamingConvention().isInRange(Iterables.get(flushedPaths, 0).toString(), now, now + 1));
    assertTrue(Iterables.get(flushedPaths, 0).contains("/path/2017/07/"));
    assertTrue(params.getNamingConvention().isInRange(Iterables.get(flushedPaths, 1).toString(), now + params.getRollPeriod(), now + params.getRollPeriod() + 1));
    assertTrue(Iterables.get(flushedPaths, 1).contains("/path/2017/07/"));
}
Also used : Iterables(com.google.common.collect.Iterables) Arrays(java.util.Arrays) URISyntaxException(java.net.URISyntaxException) RunWith(org.junit.runner.RunWith) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) TreeSet(java.util.TreeSet) ArrayList(java.util.ArrayList) Value(lombok.Value) HashSet(java.util.HashSet) ExceptionUtils(cz.o2.proxima.util.ExceptionUtils) Lists(com.google.common.collect.Lists) StreamElement(cz.o2.proxima.storage.StreamElement) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) Map(java.util.Map) After(org.junit.After) ConfigFactory(com.typesafe.config.ConfigFactory) URI(java.net.URI) Parameterized(org.junit.runners.Parameterized) Before(org.junit.Before) BulkAttributeWriter(cz.o2.proxima.direct.core.BulkAttributeWriter) Repository(cz.o2.proxima.repository.Repository) Iterator(java.util.Iterator) Collection(java.util.Collection) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) Set(java.util.Set) IOException(java.io.IOException) Test(org.junit.Test) UUID(java.util.UUID) CommitCallback(cz.o2.proxima.direct.core.CommitCallback) Sets(com.google.common.collect.Sets) File(java.io.File) Serializable(java.io.Serializable) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Rule(org.junit.Rule) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) TemporaryFolder(org.junit.rules.TemporaryFolder) ArrayList(java.util.ArrayList) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 4 with Watermarks

use of cz.o2.proxima.time.Watermarks in project proxima-platform by O2-Czech-Republic.

the class ListCommitLogTest method testObserveWithCustomWatemarkEstimator.

@Test(timeout = 1000)
public void testObserveWithCustomWatemarkEstimator() throws InterruptedException {
    long now = System.currentTimeMillis() - 1000;
    int numElements = 10;
    CommitLogReader reader = ListCommitLog.of(data(numElements), new TestWatermarkEstimator(now), direct.getContext());
    CountDownLatch latch = new CountDownLatch(1);
    List<Long> watermarks = new ArrayList<>();
    ObserveHandle handle = reader.observe(null, new CommitLogObserver() {

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

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

        @Override
        public void onCompleted() {
            latch.countDown();
        }
    });
    latch.await();
    assertEquals(numElements, watermarks.size());
    List<Long> expected = IntStream.range(0, numElements).mapToObj(i -> now + i).collect(Collectors.toList());
    assertEquals(expected, watermarks);
}
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) CommitLogObserver(cz.o2.proxima.direct.commitlog.CommitLogObserver) Test(org.junit.Test)

Example 5 with Watermarks

use of cz.o2.proxima.time.Watermarks 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)

Aggregations

StreamElement (cz.o2.proxima.storage.StreamElement)8 ArrayList (java.util.ArrayList)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 Test (org.junit.Test)8 ConfigFactory (com.typesafe.config.ConfigFactory)7 AttributeDescriptor (cz.o2.proxima.repository.AttributeDescriptor)7 EntityDescriptor (cz.o2.proxima.repository.EntityDescriptor)7 Repository (cz.o2.proxima.repository.Repository)7 List (java.util.List)7 UUID (java.util.UUID)7 DirectDataOperator (cz.o2.proxima.direct.core.DirectDataOperator)6 Arrays (java.util.Arrays)6 Collections (java.util.Collections)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 Lists (com.google.common.collect.Lists)5 CommitCallback (cz.o2.proxima.direct.core.CommitCallback)5 IOException (java.io.IOException)5 Serializable (java.io.Serializable)5 URI (java.net.URI)5