Search in sources :

Example 6 with PushOffsetRange

use of io.confluent.ksql.util.PushOffsetRange in project ksql by confluentinc.

the class PushRoutingTest method shouldSucceed_gapDetectedLocal_disableAlos.

@Test
public void shouldSucceed_gapDetectedLocal_disableAlos() throws ExecutionException, InterruptedException {
    // Given:
    when(pushRoutingOptions.alosEnabled()).thenReturn(false);
    final AtomicReference<Set<KsqlNode>> nodes = new AtomicReference<>(ImmutableSet.of(ksqlNodeLocal, ksqlNodeRemote));
    final PushRouting routing = new PushRouting(sqr -> nodes.get(), 50, true);
    AtomicReference<TestLocalPublisher> localPublisher = new AtomicReference<>();
    AtomicInteger localCount = new AtomicInteger(0);
    when(pushPhysicalPlanManager.execute()).thenAnswer(a -> {
        localPublisher.set(new TestLocalPublisher(context));
        localCount.incrementAndGet();
        context.runOnContext(v -> {
            localPublisher.get().accept(LOCAL_ROW2);
        });
        return localPublisher.get();
    });
    doAnswer(a -> {
        final Optional<PushOffsetRange> newOffsetRange = a.getArgument(0);
        assertThat(newOffsetRange.isPresent(), is(true));
        assertThat(newOffsetRange.get().getEndOffsets(), is(ImmutableList.of(0L, 3L)));
        return null;
    }).when(pushPhysicalPlanManager).reset(any());
    // When:
    final PushConnectionsHandle handle = handlePushRouting(routing);
    context.runOnContext(v -> {
        localPublisher.get().accept(LOCAL_CONTINUATION_TOKEN1);
        localPublisher.get().accept(LOCAL_ROW1);
        localPublisher.get().accept(LOCAL_CONTINUATION_TOKEN_GAP);
    });
    Set<List<?>> rows = waitOnRows(2);
    handle.close();
    // Then:
    verify(pushPhysicalPlanManager, times(1)).execute();
    assertThat(rows.contains(LOCAL_ROW1.value().values()), is(true));
    assertThat(rows.contains(LOCAL_ROW2.value().values()), is(true));
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) PushConnectionsHandle(io.confluent.ksql.physical.scalablepush.PushRouting.PushConnectionsHandle) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) PushOffsetRange(io.confluent.ksql.util.PushOffsetRange) Test(org.junit.Test)

Example 7 with PushOffsetRange

use of io.confluent.ksql.util.PushOffsetRange in project ksql by confluentinc.

the class ScalablePushConsumer method handleProgressToken.

private void handleProgressToken(final PushOffsetVector startOffsetVector, final PushOffsetVector endOffsetVector) {
    final PushOffsetRange range = new PushOffsetRange(Optional.of(startOffsetVector), endOffsetVector);
    for (ProcessingQueue queue : processingQueues.values()) {
        final QueryRow row = OffsetsRow.of(clock.millis(), range);
        queue.offer(row);
    }
}
Also used : QueryRow(io.confluent.ksql.physical.common.QueryRow) ProcessingQueue(io.confluent.ksql.physical.scalablepush.ProcessingQueue) PushOffsetRange(io.confluent.ksql.util.PushOffsetRange)

Example 8 with PushOffsetRange

use of io.confluent.ksql.util.PushOffsetRange in project ksql by confluentinc.

the class ScalablePushRegistryTest method setUp.

@Before
public void setUp() {
    when(ksqlTopic.getKafkaTopicName()).thenReturn(TOPIC);
    when(kafkaConsumerFactory.create(any(), any(), any(), any(), any(), any())).thenReturn(kafkaConsumer);
    catchupCoordinator = new TestCatchupCoordinator();
    latestConsumer = new TestLatestConsumer(TOPIC, false, SCHEMA, kafkaConsumer, catchupCoordinator, assignment -> {
    }, ksqlConfig, Clock.systemUTC());
    latestConsumer2 = new TestLatestConsumer(TOPIC, false, SCHEMA, kafkaConsumer, catchupCoordinator, assignment -> {
    }, ksqlConfig, Clock.systemUTC());
    catchupConsumer = new TestCatchupConsumer(TOPIC, false, SCHEMA, kafkaConsumer, () -> latestConsumer, catchupCoordinator, pushOffsetRange, Clock.systemUTC(), pq -> {
    });
    when(latestConsumerFactory.create(any(), anyBoolean(), any(), any(), any(), any(), any(), any())).thenReturn(latestConsumer, latestConsumer2);
    when(catchupConsumerFactory.create(any(), anyBoolean(), any(), any(), any(), any(), any(), any(), anyLong(), any())).thenReturn(catchupConsumer);
    when(ksqlTopic.getKeyFormat()).thenReturn(keyFormat);
    when(keyFormat.isWindowed()).thenReturn(false);
    realExecutorService = Executors.newFixedThreadPool(2);
    doAnswer(a -> {
        final Runnable runnable = a.getArgument(0);
        startLatestRunnable.set(runnable);
        realExecutorService.submit(runnable);
        return null;
    }).when(executorService).submit(any(Runnable.class));
    doAnswer(a -> {
        final Runnable runnable = a.getArgument(0);
        realExecutorService.submit(runnable);
        return null;
    }).when(catchupService).submit(any(Runnable.class));
    when(processingQueue.getQueryId()).thenReturn(new QueryId("q1"));
    when(processingQueue2.getQueryId()).thenReturn(new QueryId("q2"));
    registry = new ScalablePushRegistry(locator, SCHEMA, false, ImmutableMap.of(), ksqlTopic, serviceContext, ksqlConfig, SOURCE_APP_ID, kafkaConsumerFactory, latestConsumerFactory, catchupConsumerFactory, executorService, catchupService);
    when(ksqlConfig.getInt(KsqlConfig.KSQL_QUERY_PUSH_V2_MAX_CATCHUP_CONSUMERS)).thenReturn(10);
}
Also used : CatchupCoordinator(io.confluent.ksql.physical.scalablepush.consumer.CatchupCoordinator) ColumnName(io.confluent.ksql.name.ColumnName) AssertEventually.assertThatEventually(io.confluent.ksql.test.util.AssertEventually.assertThatEventually) ServiceContext(io.confluent.ksql.services.ServiceContext) ArgumentMatchers.contains(org.mockito.ArgumentMatchers.contains) PushLocator(io.confluent.ksql.physical.scalablepush.locator.PushLocator) Mockito.doThrow(org.mockito.Mockito.doThrow) CatchupConsumer(io.confluent.ksql.physical.scalablepush.consumer.CatchupConsumer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) After(org.junit.After) QueryId(io.confluent.ksql.query.QueryId) Mockito.doReturn(org.mockito.Mockito.doReturn) TopicPartition(org.apache.kafka.common.TopicPartition) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) KsqlConfig(io.confluent.ksql.util.KsqlConfig) LogicalSchema(io.confluent.ksql.schema.ksql.LogicalSchema) Executors(java.util.concurrent.Executors) CatchupMetadata(io.confluent.ksql.physical.scalablepush.ScalablePushRegistry.CatchupMetadata) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) KafkaConsumer(org.apache.kafka.clients.consumer.KafkaConsumer) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) StreamsConfig(org.apache.kafka.streams.StreamsConfig) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) KeyFormat(io.confluent.ksql.serde.KeyFormat) Mock(org.mockito.Mock) Assert.assertThrows(org.junit.Assert.assertThrows) RunWith(org.junit.runner.RunWith) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) CatchupConsumerFactory(io.confluent.ksql.physical.scalablepush.consumer.CatchupConsumer.CatchupConsumerFactory) PushOffsetRange(io.confluent.ksql.util.PushOffsetRange) KafkaConsumerFactoryInterface(io.confluent.ksql.physical.scalablepush.consumer.KafkaConsumerFactory.KafkaConsumerFactoryInterface) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) LatestConsumer(io.confluent.ksql.physical.scalablepush.consumer.LatestConsumer) LatestConsumerFactory(io.confluent.ksql.physical.scalablepush.consumer.LatestConsumer.LatestConsumerFactory) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Mockito.never(org.mockito.Mockito.never) GenericRow(io.confluent.ksql.GenericRow) KsqlTopic(io.confluent.ksql.execution.ddl.commands.KsqlTopic) Clock(java.time.Clock) SqlTypes(io.confluent.ksql.schema.ksql.types.SqlTypes) Collections(java.util.Collections) QueryId(io.confluent.ksql.query.QueryId) Before(org.junit.Before)

Example 9 with PushOffsetRange

use of io.confluent.ksql.util.PushOffsetRange in project ksql by confluentinc.

the class CatchupConsumerTest method shouldRunConsumer_timeoutWaitingForAssignment.

@Test
public void shouldRunConsumer_timeoutWaitingForAssignment() {
    // Given:
    PushOffsetRange offsetRange = new PushOffsetRange(Optional.empty(), new PushOffsetVector(ImmutableList.of(1L, 2L)));
    when(latestConsumer.getAssignment()).thenReturn(null);
    when(clock.millis()).thenReturn(CURRENT_TIME_MS, CURRENT_TIME_MS + WAIT_FOR_ASSIGNMENT_MS - 1, CURRENT_TIME_MS + WAIT_FOR_ASSIGNMENT_MS + 1);
    try (CatchupConsumer consumer = new CatchupConsumer(TOPIC, false, SCHEMA, kafkaConsumer, () -> latestConsumer, catchupCoordinator, offsetRange, clock, sleepFn, waitFn, 0, pq -> caughtUp = true)) {
        // When:
        consumer.register(queue);
        final Exception e = assertThrows(KsqlException.class, consumer::run);
        // Then:
        assertThat(e.getMessage(), containsString("Timed out waiting for assignment from Latest"));
    }
}
Also used : PushOffsetVector(io.confluent.ksql.util.PushOffsetVector) KsqlException(io.confluent.ksql.util.KsqlException) PushOffsetRange(io.confluent.ksql.util.PushOffsetRange) Test(org.junit.Test)

Example 10 with PushOffsetRange

use of io.confluent.ksql.util.PushOffsetRange in project ksql by confluentinc.

the class PushRoutingTest method shouldSucceed_gapDetectedLocal_retry.

@Test
public void shouldSucceed_gapDetectedLocal_retry() throws ExecutionException, InterruptedException {
    // Given:
    final AtomicReference<Set<KsqlNode>> nodes = new AtomicReference<>(ImmutableSet.of(ksqlNodeLocal, ksqlNodeRemote));
    final PushRouting routing = new PushRouting(sqr -> nodes.get(), 50, true);
    AtomicReference<TestLocalPublisher> localPublisher = new AtomicReference<>();
    AtomicInteger localCount = new AtomicInteger(0);
    when(pushPhysicalPlanManager.execute()).thenAnswer(a -> {
        localPublisher.set(new TestLocalPublisher(context));
        localCount.incrementAndGet();
        if (localCount.get() == 2) {
            localPublisher.get().accept(LOCAL_ROW2);
        }
        return localPublisher.get();
    });
    doAnswer(a -> {
        final Optional<PushOffsetRange> newOffsetRange = a.getArgument(0);
        assertThat(newOffsetRange.isPresent(), is(true));
        assertThat(newOffsetRange.get().getEndOffsets(), is(ImmutableList.of(0L, 3L)));
        return null;
    }).when(pushPhysicalPlanManager).reset(any());
    // When:
    final PushConnectionsHandle handle = handlePushRouting(routing);
    context.runOnContext(v -> {
        localPublisher.get().accept(LOCAL_CONTINUATION_TOKEN1);
        localPublisher.get().accept(LOCAL_ROW1);
        localPublisher.get().accept(LOCAL_CONTINUATION_TOKEN_GAP);
    });
    Set<List<?>> rows = waitOnRows(2);
    handle.close();
    // Then:
    verify(pushPhysicalPlanManager, times(2)).execute();
    assertThat(rows.contains(LOCAL_ROW1.value().values()), is(true));
    assertThat(rows.contains(LOCAL_ROW2.value().values()), is(true));
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) HashSet(java.util.HashSet) PushConnectionsHandle(io.confluent.ksql.physical.scalablepush.PushRouting.PushConnectionsHandle) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) PushOffsetRange(io.confluent.ksql.util.PushOffsetRange) Test(org.junit.Test)

Aggregations

PushOffsetRange (io.confluent.ksql.util.PushOffsetRange)10 Test (org.junit.Test)8 ImmutableList (com.google.common.collect.ImmutableList)5 ImmutableSet (com.google.common.collect.ImmutableSet)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 PushOffsetVector (io.confluent.ksql.util.PushOffsetVector)4 List (java.util.List)4 Set (java.util.Set)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 PushConnectionsHandle (io.confluent.ksql.physical.scalablepush.PushRouting.PushConnectionsHandle)3 QueryId (io.confluent.ksql.query.QueryId)3 KsqlException (io.confluent.ksql.util.KsqlException)3 HashSet (java.util.HashSet)3 Optional (java.util.Optional)3 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)2 GenericRow (io.confluent.ksql.GenericRow)2 KsqlTopic (io.confluent.ksql.execution.ddl.commands.KsqlTopic)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2