Search in sources :

Example 1 with ThroughputLimiter

use of cz.o2.proxima.storage.ThroughputLimiter in project proxima-platform by O2-Czech-Republic.

the class CommitLogReaderTest method withNumRecordsPerSec.

public static ThroughputLimiter withNumRecordsPerSec(int recordsPerSec) {
    final Duration pauseDuration = Duration.ofMillis(1000L / recordsPerSec);
    return new ThroughputLimiter() {

        @Override
        public Duration getPauseTime(Context context) {
            assertEquals(1, context.getConsumedPartitions().size());
            assertEquals(0, Iterables.getOnlyElement(context.getConsumedPartitions()).getId());
            assertTrue(context.getMinWatermark() < Long.MAX_VALUE);
            return pauseDuration;
        }

        @Override
        public void close() {
        }
    };
}
Also used : OnNextContext(cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext) ThroughputLimiter(cz.o2.proxima.storage.ThroughputLimiter) Duration(java.time.Duration)

Example 2 with ThroughputLimiter

use of cz.o2.proxima.storage.ThroughputLimiter in project proxima-platform by O2-Czech-Republic.

the class CommitLogReaderTest method testThroughputLimitedCommitLogReaderAsFactory.

@Test
public void testThroughputLimitedCommitLogReaderAsFactory() {
    ThroughputLimiter limiter = ThroughputLimiter.NoOpThroughputLimiter.INSTANCE;
    CommitLogReader limitedReader = CommitLogReaders.withThroughputLimit(reader, limiter);
    CommitLogReader cloned = limitedReader.asFactory().apply(repo);
    assertTrue(limitedReader.getClass().isAssignableFrom(cloned.getClass()));
}
Also used : ThroughputLimiter(cz.o2.proxima.storage.ThroughputLimiter) LimitedCommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReaders.LimitedCommitLogReader) Test(org.junit.Test)

Example 3 with ThroughputLimiter

use of cz.o2.proxima.storage.ThroughputLimiter in project proxima-platform by O2-Czech-Republic.

the class DirectDataOperatorTest method testLimiterDataAccessor.

@Test
public void testLimiterDataAccessor() {
    repo.reloadConfig(true, ConfigFactory.load("test-limiter.conf").withFallback(ConfigFactory.load("test-reference.conf")).resolve());
    DirectDataOperator direct = repo.getOrCreateOperator(DirectDataOperator.class);
    DirectAttributeFamilyDescriptor family = direct.getFamilyByName("event-storage-stream");
    Optional<CommitLogReader> maybeReader = family.getCommitLogReader();
    assertTrue(maybeReader.isPresent());
    CommitLogReader reader = maybeReader.get();
    assertTrue("Expected reader of class LimitedCommitLogReader, got " + reader.getClass(), reader instanceof LimitedCommitLogReader);
    LimitedCommitLogReader limitedReader = (LimitedCommitLogReader) reader;
    ThroughputLimiter limiter = limitedReader.getLimiter();
    assertNotNull(limiter);
    assertTrue(limiter instanceof GlobalWatermarkThroughputLimiter);
    GlobalWatermarkThroughputLimiter watermarkLimiter = (GlobalWatermarkThroughputLimiter) limiter;
    GlobalWatermarkTracker tracker = watermarkLimiter.getTracker();
    assertTrue(tracker instanceof TestTracker);
    TestTracker testTracker = (TestTracker) tracker;
    assertEquals(2, testTracker.getTestConf());
}
Also used : GlobalWatermarkThroughputLimiter(cz.o2.proxima.storage.watermark.GlobalWatermarkThroughputLimiter) ThroughputLimiter(cz.o2.proxima.storage.ThroughputLimiter) TestTracker(cz.o2.proxima.storage.watermark.GlobalWatermarkThroughputLimiterTest.TestTracker) LimitedCommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReaders.LimitedCommitLogReader) CommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReader) LimitedCommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReaders.LimitedCommitLogReader) GlobalWatermarkTracker(cz.o2.proxima.storage.watermark.GlobalWatermarkTracker) GlobalWatermarkThroughputLimiter(cz.o2.proxima.storage.watermark.GlobalWatermarkThroughputLimiter) Test(org.junit.Test)

Example 4 with ThroughputLimiter

use of cz.o2.proxima.storage.ThroughputLimiter in project proxima-platform by O2-Czech-Republic.

the class CommitLogReaderTest method testThroughputLimitedCommitLogIdles.

@Test(timeout = 10000)
public void testThroughputLimitedCommitLogIdles() throws InterruptedException {
    ThroughputLimiter limiter = ThroughputLimiter.NoOpThroughputLimiter.INSTANCE;
    CommitLogReader limitedReader = CommitLogReaders.withThroughputLimit(reader, limiter);
    UnaryFunction<CountDownLatch, CommitLogObserver> observerFactory = myLatch -> new CommitLogObserver() {

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

        @Override
        public boolean onNext(StreamElement ingest, OnNextContext context) {
            return false;
        }

        @Override
        public void onIdle(OnIdleContext context) {
            myLatch.countDown();
        }
    };
    CountDownLatch latch = new CountDownLatch(1);
    CommitLogObserver observer = observerFactory.apply(latch);
    long nanoTime = System.nanoTime();
    ObserveHandle handle = limitedReader.observe("dummy", observer);
    latch.await();
    handle.close();
    latch = new CountDownLatch(1);
    observer = observerFactory.apply(latch);
    long durationMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - nanoTime);
    handle.close();
    limitedReader = CommitLogReaders.withThroughputLimit(reader, withNumRecordsPerSec(1));
    handle = limitedReader.observe("dummy", observer);
    // no idle called this time
    assertFalse(latch.await(2 * durationMillis, TimeUnit.MILLISECONDS));
}
Also used : Iterables(com.google.common.collect.Iterables) AttributeWriterBase(cz.o2.proxima.direct.core.AttributeWriterBase) Partition(cz.o2.proxima.storage.Partition) OffsetCommitter(cz.o2.proxima.direct.commitlog.CommitLogObserver.OffsetCommitter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EntityDescriptor(cz.o2.proxima.repository.EntityDescriptor) Mockito.spy(org.mockito.Mockito.spy) LimitedCommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReaders.LimitedCommitLogReader) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) ExceptionUtils(cz.o2.proxima.util.ExceptionUtils) StreamElement(cz.o2.proxima.storage.StreamElement) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UnaryFunction(cz.o2.proxima.functional.UnaryFunction) Duration(java.time.Duration) After(org.junit.After) ConfigFactory(com.typesafe.config.ConfigFactory) Optionals(cz.o2.proxima.util.Optionals) Before(org.junit.Before) LongStream(java.util.stream.LongStream) Repository(cz.o2.proxima.repository.Repository) BiFunction(cz.o2.proxima.functional.BiFunction) AttributeDescriptor(cz.o2.proxima.repository.AttributeDescriptor) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) DirectAttributeFamilyDescriptor(cz.o2.proxima.direct.core.DirectAttributeFamilyDescriptor) OnNextContext(cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext) DirectDataOperator(cz.o2.proxima.direct.core.DirectDataOperator) Assert(org.junit.Assert) Collections(java.util.Collections) Position(cz.o2.proxima.storage.commitlog.Position) ThroughputLimiter(cz.o2.proxima.storage.ThroughputLimiter) ThroughputLimiter(cz.o2.proxima.storage.ThroughputLimiter) OnNextContext(cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext) LimitedCommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReaders.LimitedCommitLogReader) StreamElement(cz.o2.proxima.storage.StreamElement) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with ThroughputLimiter

use of cz.o2.proxima.storage.ThroughputLimiter in project proxima-platform by O2-Czech-Republic.

the class CommitLogReaderTest method testThroughputLimitedCommitLogWithObserve.

private void testThroughputLimitedCommitLogWithObserve(BiFunction<CommitLogReader, Runnable, ObserveHandle> observe) throws InterruptedException {
    final int numElements = 50;
    ThroughputLimiter limiter = withNumRecordsPerSec(2 * numElements);
    CommitLogReader limitedReader = CommitLogReaders.withThroughputLimit(reader, limiter);
    assertEquals(limitedReader.getPartitions(), reader.getPartitions());
    assertEquals(limitedReader.getUri(), reader.getUri());
    CountDownLatch latch = new CountDownLatch(numElements);
    AtomicLong lastMillis = new AtomicLong(System.currentTimeMillis());
    List<Long> durations = new ArrayList<>();
    ObserveHandle handle = observe.apply(limitedReader, () -> {
        latch.countDown();
        long now = System.currentTimeMillis();
        long lastGet = lastMillis.getAndUpdate(current -> now);
        durations.add(now - lastGet);
    });
    for (int i = 0; i < 50; i++) {
        writer.online().write(StreamElement.upsert(entity, attr, UUID.randomUUID().toString(), "key", attr.getName(), System.currentTimeMillis(), new byte[] { 1, 2 }), (succ, exc) -> {
        });
    }
    latch.await();
    assertEquals(numElements, durations.size());
    assertEquals(durations.toString(), numElements, durations.stream().filter(s -> s >= 500 / numElements).count());
    handle.close();
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) ThroughputLimiter(cz.o2.proxima.storage.ThroughputLimiter) LimitedCommitLogReader(cz.o2.proxima.direct.commitlog.CommitLogReaders.LimitedCommitLogReader) AtomicLong(java.util.concurrent.atomic.AtomicLong) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

ThroughputLimiter (cz.o2.proxima.storage.ThroughputLimiter)5 LimitedCommitLogReader (cz.o2.proxima.direct.commitlog.CommitLogReaders.LimitedCommitLogReader)4 Duration (java.time.Duration)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 Test (org.junit.Test)3 OnNextContext (cz.o2.proxima.direct.commitlog.CommitLogObserver.OnNextContext)2 StreamElement (cz.o2.proxima.storage.StreamElement)2 ArrayList (java.util.ArrayList)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Iterables (com.google.common.collect.Iterables)1 ConfigFactory (com.typesafe.config.ConfigFactory)1 OffsetCommitter (cz.o2.proxima.direct.commitlog.CommitLogObserver.OffsetCommitter)1 CommitLogReader (cz.o2.proxima.direct.commitlog.CommitLogReader)1 AttributeWriterBase (cz.o2.proxima.direct.core.AttributeWriterBase)1 DirectAttributeFamilyDescriptor (cz.o2.proxima.direct.core.DirectAttributeFamilyDescriptor)1 DirectDataOperator (cz.o2.proxima.direct.core.DirectDataOperator)1 BiFunction (cz.o2.proxima.functional.BiFunction)1