org.apache.kafka.streams.state.ReadOnlyWindowStore Java Examples

The following examples show how to use org.apache.kafka.streams.state.ReadOnlyWindowStore. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: KafkaStorageHttpService.java    From zipkin-storage-kafka with Apache License 2.0 5 votes vote down vote up
@Get("/dependencies")
public AggregatedHttpResponse getDependencies(
    @Param("endTs") long endTs,
    @Param("lookback") long lookback
) {
  try {
    if (!storage.dependencyQueryEnabled) return AggregatedHttpResponse.of(HttpStatus.NOT_FOUND);
    ReadOnlyWindowStore<Long, DependencyLink> store =
        storage.getDependencyStorageStream()
            .store(DEPENDENCIES_STORE_NAME, QueryableStoreTypes.windowStore());
    List<DependencyLink> links = new ArrayList<>();
    Instant from = Instant.ofEpochMilli(endTs - lookback);
    Instant to = Instant.ofEpochMilli(endTs);
    try (KeyValueIterator<Windowed<Long>, DependencyLink> iterator = store.fetchAll(from, to)) {
      iterator.forEachRemaining(keyValue -> links.add(keyValue.value));
    }
    List<DependencyLink> mergedLinks = DependencyLinker.merge(links);
    LOG.debug("Dependencies found from={}-to={}: {}", from, to, mergedLinks.size());
    return AggregatedHttpResponse.of(
        HttpStatus.OK,
        MediaType.JSON,
        DependencyLinkBytesEncoder.JSON_V1.encodeList(mergedLinks));
  } catch (InvalidStateStoreException e) {
    LOG.debug("State store is not ready", e);
    return AggregatedHttpResponse.of(HttpStatus.SERVICE_UNAVAILABLE);
  }
}
 
Example #2
Source File: WindowQueryVerticleTest.java    From kiqr with Apache License 2.0 4 votes vote down vote up
@Test
public void illegalStateStoreExceptionOnQuery(TestContext context){
    KafkaStreams streamMock = mock(KafkaStreams.class);
    ReadOnlyWindowStore<Object, Object> storeMock = mock(ReadOnlyWindowStore.class);
    when(streamMock.store(eq("store"), any(QueryableStoreType.class))).thenReturn(storeMock);
    when(storeMock.fetch(any(), anyLong(), anyLong())).thenThrow(InvalidStateStoreException.class);

    rule.vertx().deployVerticle(new WindowedQueryVerticle("host", streamMock), context.asyncAssertSuccess(deployment->{

        WindowedQuery query = new WindowedQuery("store", Serdes.String().getClass().getName(), "key".getBytes(),  Serdes.String().getClass().getName(), 0, 1);

        rule.vertx().eventBus().send(Config.WINDOWED_QUERY_ADDRESS_PREFIX + "host", query, context.asyncAssertFailure(handler ->{

            context.assertTrue(handler instanceof ReplyException);
            ReplyException ex = (ReplyException) handler;
            context.assertEquals(500, ex.failureCode());

        }));

    }));

}
 
Example #3
Source File: WindowQueryVerticleTest.java    From kiqr with Apache License 2.0 4 votes vote down vote up
@Test
public void unexpectedExceptionOnQuery(TestContext context){
    KafkaStreams streamMock = mock(KafkaStreams.class);
    ReadOnlyWindowStore<Object, Object> storeMock = mock(ReadOnlyWindowStore.class);
    when(streamMock.store(eq("store"), any(QueryableStoreType.class))).thenReturn(storeMock);
    when(storeMock.fetch(any(), anyLong(), anyLong())).thenThrow(IllegalArgumentException.class);


    rule.vertx().deployVerticle(new WindowedQueryVerticle("host", streamMock), context.asyncAssertSuccess(deployment->{

        WindowedQuery query = new WindowedQuery("store", Serdes.String().getClass().getName(), "key".getBytes(),  Serdes.String().getClass().getName(), 0, 1);

        rule.vertx().eventBus().send(Config.WINDOWED_QUERY_ADDRESS_PREFIX + "host", query, context.asyncAssertFailure(handler ->{

            context.assertTrue(handler instanceof ReplyException);
            ReplyException ex = (ReplyException) handler;
            context.assertEquals(500, ex.failureCode());

        }));

    }));

}
 
Example #4
Source File: WindowQueryVerticleTest.java    From kiqr with Apache License 2.0 3 votes vote down vote up
@Test
public void successWithSingleResult(TestContext context){
    KafkaStreams streamMock = mock(KafkaStreams.class);
    ReadOnlyWindowStore<Object, Object> storeMock = mock(ReadOnlyWindowStore.class);
    KeyValueIterator<Object, Object> iteratorMock = mock(KeyValueIterator.class);

    when(streamMock.store(eq("store"), any(QueryableStoreType.class))).thenReturn(storeMock);
    SimpleWindowStoreIterator iterator = new SimpleWindowStoreIterator(new KeyValue<Long, Object>(1L, "value"));



    when(storeMock.fetch(any(), anyLong(), anyLong())).thenReturn(iterator);

    rule.vertx().deployVerticle(new WindowedQueryVerticle("host", streamMock), context.asyncAssertSuccess(deployment->{

        WindowedQuery query = new WindowedQuery("store", Serdes.String().getClass().getName(), "key".getBytes(),  Serdes.String().getClass().getName(), 0, 1);

        rule.vertx().eventBus().send(Config.WINDOWED_QUERY_ADDRESS_PREFIX + "host", query, context.asyncAssertSuccess(reply ->{

            context.assertTrue(reply.body() instanceof WindowedQueryResponse);
            WindowedQueryResponse response = (WindowedQueryResponse) reply.body();
            context.assertEquals(1, response.getValues().size());
            context.assertTrue(response.getValues().containsKey(1L));
            context.assertEquals("dmFsdWU=", response.getValues().get(1L));//"value" as UT8/b64
            context.assertTrue(iterator.closed);

        }));


    }));


}
 
Example #5
Source File: WindowQueryVerticleTest.java    From kiqr with Apache License 2.0 3 votes vote down vote up
@Test
public void successWithDoubleResult(TestContext context){
    KafkaStreams streamMock = mock(KafkaStreams.class);
    ReadOnlyWindowStore<Object, Object> storeMock = mock(ReadOnlyWindowStore.class);
    when(streamMock.store(eq("store"), any(QueryableStoreType.class))).thenReturn(storeMock);

    SimpleWindowStoreIterator iterator = new SimpleWindowStoreIterator(new KeyValue<Long, Object>(1L, "value"), new KeyValue<Long, Object>(2L, "value2"));

    when(storeMock.fetch(any(), anyLong(), anyLong())).thenReturn(iterator);


    rule.vertx().deployVerticle(new WindowedQueryVerticle("host", streamMock), context.asyncAssertSuccess(deployment->{

        WindowedQuery query = new WindowedQuery("store", Serdes.String().getClass().getName(), "key".getBytes(),  Serdes.String().getClass().getName(), 0, 1);

        rule.vertx().eventBus().send(Config.WINDOWED_QUERY_ADDRESS_PREFIX + "host", query, context.asyncAssertSuccess(reply ->{

            context.assertTrue(reply.body() instanceof WindowedQueryResponse);
            WindowedQueryResponse response = (WindowedQueryResponse) reply.body();
            context.assertEquals(2, response.getValues().size());
            context.assertTrue(response.getValues().containsKey(1L));
            context.assertEquals("dmFsdWU=", response.getValues().get(1L));//"value" as UT8/b64

            context.assertTrue(response.getValues().containsKey(2L));
            context.assertEquals("dmFsdWUy", response.getValues().get(2L));//"value" as UT8/b64

            context.assertTrue(iterator.closed);

        }));

    }));

}
 
Example #6
Source File: WindowQueryVerticleTest.java    From kiqr with Apache License 2.0 3 votes vote down vote up
@Test
public void notFoundWithNoResult(TestContext context){
    KafkaStreams streamMock = mock(KafkaStreams.class);
    ReadOnlyWindowStore<Object, Object> storeMock = mock(ReadOnlyWindowStore.class);
    KeyValueIterator<Object, Object> iteratorMock = mock(KeyValueIterator.class);
    when(streamMock.store(eq("store"), any(QueryableStoreType.class))).thenReturn(storeMock);
    SimpleWindowStoreIterator iterator = new SimpleWindowStoreIterator();

    when(storeMock.fetch(any(), anyLong(), anyLong())).thenReturn(iterator);


    rule.vertx().deployVerticle(new WindowedQueryVerticle("host", streamMock), context.asyncAssertSuccess(deployment->{

        WindowedQuery query = new WindowedQuery("store", Serdes.String().getClass().getName(), "key".getBytes(),  Serdes.String().getClass().getName(), 0, 1);

        rule.vertx().eventBus().send(Config.WINDOWED_QUERY_ADDRESS_PREFIX + "host", query, context.asyncAssertSuccess(reply ->{

            context.assertTrue(reply.body() instanceof WindowedQueryResponse);
            WindowedQueryResponse response = (WindowedQueryResponse) reply.body();
            context.assertEquals(0, response.getValues().size());
            context.assertTrue(iterator.closed);

        }));

    }));

}