Search in sources :

Example 1 with LagInfoEntity

use of io.confluent.ksql.rest.entity.LagInfoEntity in project ksql by confluentinc.

the class MaximumLagFilter method create.

/**
 * Creates a FreshnessFilter
 * @param lagReportingAgent The optional lag reporting agent.
 * @param routingOptions The routing options
 * @param hosts The set of all hosts that have the store, including actives and standbys
 * @param applicationQueryId The query id of the persistent query that materialized the table
 * @param storeName The state store name of the materialized table
 * @param partition The partition of the topic
 * @return a new FreshnessFilter, unless lag reporting is disabled.
 */
public static Optional<MaximumLagFilter> create(final Optional<LagReportingAgent> lagReportingAgent, final RoutingOptions routingOptions, final List<KsqlHostInfo> hosts, final String applicationQueryId, final String storeName, final int partition) {
    if (!lagReportingAgent.isPresent()) {
        return Optional.empty();
    }
    final QueryStateStoreId queryStateStoreId = QueryStateStoreId.of(applicationQueryId, storeName);
    final ImmutableMap<KsqlHostInfo, Optional<LagInfoEntity>> lagByHost = hosts.stream().collect(ImmutableMap.toImmutableMap(Function.identity(), host -> lagReportingAgent.get().getLagInfoForHost(host, queryStateStoreId, partition)));
    final OptionalLong maxEndOffset = lagByHost.values().stream().filter(Optional::isPresent).map(Optional::get).mapToLong(LagInfoEntity::getEndOffsetPosition).max();
    return Optional.of(new MaximumLagFilter(routingOptions, lagByHost, maxEndOffset));
}
Also used : OptionalLong(java.util.OptionalLong) List(java.util.List) ImmutableMap(com.google.common.collect.ImmutableMap) Objects.requireNonNull(java.util.Objects.requireNonNull) RoutingOptions(io.confluent.ksql.execution.streams.RoutingOptions) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) LagInfoEntity(io.confluent.ksql.rest.entity.LagInfoEntity) QueryStateStoreId(io.confluent.ksql.rest.entity.QueryStateStoreId) Function(java.util.function.Function) RoutingFilter(io.confluent.ksql.execution.streams.RoutingFilter) KsqlHostInfo(io.confluent.ksql.util.KsqlHostInfo) Optional(java.util.Optional) KsqlHostInfo(io.confluent.ksql.util.KsqlHostInfo) QueryStateStoreId(io.confluent.ksql.rest.entity.QueryStateStoreId) OptionalLong(java.util.OptionalLong)

Example 2 with LagInfoEntity

use of io.confluent.ksql.rest.entity.LagInfoEntity in project ksql by confluentinc.

the class LagReportingAgentTest method shouldReceiveLags_listAllCurrentPositions.

@Test
public void shouldReceiveLags_listAllCurrentPositions() {
    // When:
    lagReportingAgent.receiveHostLag(hostLag(HOST_ENTITY1, LAG_MAP1, 100));
    lagReportingAgent.receiveHostLag(hostLag(HOST_ENTITY2, LAG_MAP2, 200));
    lagReportingAgent.onHostStatusUpdated(HOSTS_ALIVE);
    // Then:
    ImmutableMap<KsqlHostInfoEntity, HostStoreLags> allLags = lagReportingAgent.getAllLags();
    LagInfoEntity lag = allLags.get(HOST_ENTITY1).getStateStoreLags(QUERY_STORE_A).flatMap(s -> s.getLagByPartition(1)).get();
    assertEquals(M1_A1_CUR, lag.getCurrentOffsetPosition());
    assertEquals(M1_A1_END, lag.getEndOffsetPosition());
    assertEquals(M1_A1_LAG, lag.getOffsetLag());
    lag = allLags.get(HOST_ENTITY1).getStateStoreLags(QUERY_STORE_A).flatMap(s -> s.getLagByPartition(3)).get();
    assertEquals(M1_A3_CUR, lag.getCurrentOffsetPosition());
    assertEquals(M1_A3_END, lag.getEndOffsetPosition());
    assertEquals(M1_A3_LAG, lag.getOffsetLag());
    lag = allLags.get(HOST_ENTITY1).getStateStoreLags(QUERY_STORE_B).flatMap(s -> s.getLagByPartition(4)).get();
    assertEquals(M1_B4_CUR, lag.getCurrentOffsetPosition());
    assertEquals(M1_B4_END, lag.getEndOffsetPosition());
    assertEquals(M1_B4_LAG, lag.getOffsetLag());
    lag = allLags.get(HOST_ENTITY2).getStateStoreLags(QUERY_STORE_A).flatMap(s -> s.getLagByPartition(1)).get();
    assertEquals(M2_A1_CUR, lag.getCurrentOffsetPosition());
    assertEquals(M2_A1_END, lag.getEndOffsetPosition());
    assertEquals(M2_A1_LAG, lag.getOffsetLag());
    lag = allLags.get(HOST_ENTITY2).getStateStoreLags(QUERY_STORE_B).flatMap(s -> s.getLagByPartition(4)).get();
    assertEquals(M2_B4_CUR, lag.getCurrentOffsetPosition());
    assertEquals(M2_B4_END, lag.getEndOffsetPosition());
    assertEquals(M2_B4_LAG, lag.getOffsetLag());
}
Also used : StateStoreLags(io.confluent.ksql.rest.entity.StateStoreLags) Mock(org.mockito.Mock) HostStoreLags(io.confluent.ksql.rest.entity.HostStoreLags) ServiceContext(io.confluent.ksql.services.ServiceContext) RunWith(org.junit.runner.RunWith) LagInfoEntity(io.confluent.ksql.rest.entity.LagInfoEntity) KsqlHostInfo(io.confluent.ksql.util.KsqlHostInfo) KsqlHostInfoEntity(io.confluent.ksql.rest.entity.KsqlHostInfoEntity) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) LagReportingMessage(io.confluent.ksql.rest.entity.LagReportingMessage) URI(java.net.URI) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) Before(org.junit.Before) ImmutableMap(com.google.common.collect.ImmutableMap) KsqlEngine(io.confluent.ksql.engine.KsqlEngine) Builder(io.confluent.ksql.rest.server.LagReportingAgent.Builder) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) SimpleKsqlClient(io.confluent.ksql.services.SimpleKsqlClient) Mockito.verify(org.mockito.Mockito.verify) SendLagService(io.confluent.ksql.rest.server.LagReportingAgent.SendLagService) LagInfo(org.apache.kafka.streams.LagInfo) Assert.assertFalse(org.junit.Assert.assertFalse) HostStatus(io.confluent.ksql.util.HostStatus) Clock(java.time.Clock) Optional(java.util.Optional) QueryStateStoreId(io.confluent.ksql.rest.entity.QueryStateStoreId) MockitoJUnitRunner(org.mockito.junit.MockitoJUnitRunner) Assert.assertEquals(org.junit.Assert.assertEquals) Mockito.eq(org.mockito.Mockito.eq) HostStoreLags(io.confluent.ksql.rest.entity.HostStoreLags) LagInfoEntity(io.confluent.ksql.rest.entity.LagInfoEntity) KsqlHostInfoEntity(io.confluent.ksql.rest.entity.KsqlHostInfoEntity) Test(org.junit.Test)

Example 3 with LagInfoEntity

use of io.confluent.ksql.rest.entity.LagInfoEntity in project ksql by confluentinc.

the class HighAvailabilityTestUtil method getOffsets.

// Gets (current, end) offsets for the given host.  Makes the simplified assumption that there's
// just one state store.
public static Pair<Long, Long> getOffsets(final KsqlHostInfoEntity server, final Map<KsqlHostInfoEntity, HostStatusEntity> clusterStatus) {
    HostStatusEntity hostStatusEntity = clusterStatus.get(server);
    long end = hostStatusEntity.getHostStoreLags().getStateStoreLags().values().stream().flatMap(stateStoreLags -> stateStoreLags.getLagByPartition().values().stream()).mapToLong(LagInfoEntity::getEndOffsetPosition).max().orElse(0);
    long current = hostStatusEntity.getHostStoreLags().getStateStoreLags().values().stream().flatMap(stateStoreLags -> stateStoreLags.getLagByPartition().values().stream()).mapToLong(LagInfoEntity::getCurrentOffsetPosition).max().orElse(0);
    return Pair.of(current, end);
}
Also used : StreamedRow(io.confluent.ksql.rest.entity.StreamedRow) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClusterStatusResponse(io.confluent.ksql.rest.entity.ClusterStatusResponse) LagInfoEntity(io.confluent.ksql.rest.entity.LagInfoEntity) RestResponse(io.confluent.ksql.rest.client.RestResponse) HeartbeatResponse(io.confluent.ksql.rest.entity.HeartbeatResponse) Function(java.util.function.Function) KsqlEntity(io.confluent.ksql.rest.entity.KsqlEntity) KsqlHostInfoEntity(io.confluent.ksql.rest.entity.KsqlHostInfoEntity) KsqlRestClient(io.confluent.ksql.rest.client.KsqlRestClient) Pair(io.confluent.ksql.util.Pair) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) LagReportingMessage(io.confluent.ksql.rest.entity.LagReportingMessage) HostStatusEntity(io.confluent.ksql.rest.entity.HostStatusEntity) KsqlRequestConfig(io.confluent.ksql.util.KsqlRequestConfig) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) TestKsqlRestApp(io.confluent.ksql.rest.server.TestKsqlRestApp) BasicCredentials(io.confluent.ksql.rest.client.BasicCredentials) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) Entry(java.util.Map.Entry) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) LagInfoEntity(io.confluent.ksql.rest.entity.LagInfoEntity) HostStatusEntity(io.confluent.ksql.rest.entity.HostStatusEntity)

Example 4 with LagInfoEntity

use of io.confluent.ksql.rest.entity.LagInfoEntity in project ksql by confluentinc.

the class LagReportingAgentFunctionalTest method shouldExchangeLags.

@Test(timeout = 60000)
public void shouldExchangeLags() {
    // Given:
    waitForClusterCondition(LagReportingAgentFunctionalTest::allServersDiscovered);
    // When:
    ClusterStatusResponse resp = waitForClusterCondition(LagReportingAgentFunctionalTest::allLagsReported);
    StateStoreLags stateStoreLags = resp.getClusterStatus().entrySet().iterator().next().getValue().getHostStoreLags().getStateStoreLags(STORE_0).get();
    // Then:
    // Read the raw Kafka data from the topic to verify the reported lags
    final List<ConsumerRecord<byte[], byte[]>> records = TEST_HARNESS.verifyAvailableRecords("_confluent-ksql-default_query_CTAS_USER_VIEWS_3-" + "Aggregate-Aggregate-Materialize-changelog", NUM_ROWS);
    Map<Integer, Optional<ConsumerRecord<byte[], byte[]>>> partitionToMaxOffset = records.stream().collect(Collectors.groupingBy(ConsumerRecord::partition, Collectors.maxBy(Comparator.comparingLong(ConsumerRecord::offset))));
    Assert.assertEquals(2, partitionToMaxOffset.size());
    Optional<LagInfoEntity> lagInfoEntity0 = stateStoreLags.getLagByPartition(0);
    Optional<LagInfoEntity> lagInfoEntity1 = stateStoreLags.getLagByPartition(1);
    long partition0Offset = lagInfoEntity0.get().getCurrentOffsetPosition();
    long partition1Offset = lagInfoEntity1.get().getCurrentOffsetPosition();
    Assert.assertEquals(partition0Offset, partitionToMaxOffset.get(0).get().offset() + 1);
    Assert.assertEquals(partition1Offset, partitionToMaxOffset.get(1).get().offset() + 1);
}
Also used : LagInfoEntity(io.confluent.ksql.rest.entity.LagInfoEntity) Optional(java.util.Optional) ClusterStatusResponse(io.confluent.ksql.rest.entity.ClusterStatusResponse) StateStoreLags(io.confluent.ksql.rest.entity.StateStoreLags) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) IntegrationTest(io.confluent.common.utils.IntegrationTest) Test(org.junit.Test)

Example 5 with LagInfoEntity

use of io.confluent.ksql.rest.entity.LagInfoEntity in project ksql by confluentinc.

the class MaximumLagFilter method filter.

@Override
public Host filter(final KsqlHostInfo hostInfo) {
    final long allowedOffsetLag = routingOptions.getMaxOffsetLagAllowed();
    final Optional<LagInfoEntity> lagInfoEntity = lagByHost.get(hostInfo);
    if (!lagInfoEntity.isPresent()) {
        // available to promote HA.
        return Host.exclude(hostInfo, "Lag information is not present for host.");
    }
    final LagInfoEntity hostLag = lagInfoEntity.get();
    Preconditions.checkState(maxEndOffset.isPresent(), "Should have a maxEndOffset");
    // Compute the lag from the maximum end offset reported by all hosts.  This is so that
    // hosts that have fallen behind are held to the same end offset when computing lag.
    final long endOffset = maxEndOffset.getAsLong();
    final long offsetLag = Math.max(endOffset - hostLag.getCurrentOffsetPosition(), 0);
    if (offsetLag <= allowedOffsetLag) {
        return Host.include(hostInfo);
    } else {
        return Host.exclude(hostInfo, String.format("Host excluded because lag %s exceeds maximum allowed lag %s.", offsetLag, allowedOffsetLag));
    }
}
Also used : LagInfoEntity(io.confluent.ksql.rest.entity.LagInfoEntity)

Aggregations

LagInfoEntity (io.confluent.ksql.rest.entity.LagInfoEntity)5 Optional (java.util.Optional)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 ClusterStatusResponse (io.confluent.ksql.rest.entity.ClusterStatusResponse)2 KsqlHostInfoEntity (io.confluent.ksql.rest.entity.KsqlHostInfoEntity)2 LagReportingMessage (io.confluent.ksql.rest.entity.LagReportingMessage)2 QueryStateStoreId (io.confluent.ksql.rest.entity.QueryStateStoreId)2 StateStoreLags (io.confluent.ksql.rest.entity.StateStoreLags)2 KsqlHostInfo (io.confluent.ksql.util.KsqlHostInfo)2 List (java.util.List)2 Map (java.util.Map)2 Function (java.util.function.Function)2 Test (org.junit.Test)2 Preconditions (com.google.common.base.Preconditions)1 ImmutableList (com.google.common.collect.ImmutableList)1 IntegrationTest (io.confluent.common.utils.IntegrationTest)1 KsqlEngine (io.confluent.ksql.engine.KsqlEngine)1 RoutingFilter (io.confluent.ksql.execution.streams.RoutingFilter)1 RoutingOptions (io.confluent.ksql.execution.streams.RoutingOptions)1 BasicCredentials (io.confluent.ksql.rest.client.BasicCredentials)1