use of io.confluent.examples.streams.interactivequeries.HostStoreInfo in project kafka-streams-examples by confluentinc.
the class MusicPlaysRestService method topFiveSongs.
private List<SongPlayCountBean> topFiveSongs(final String key, final String storeName) {
final ReadOnlyKeyValueStore<String, KafkaMusicExample.TopFiveSongs> topFiveStore = streams.store(storeName, QueryableStoreTypes.<String, KafkaMusicExample.TopFiveSongs>keyValueStore());
// Get the value from the store
final KafkaMusicExample.TopFiveSongs value = topFiveStore.get(key);
if (value == null) {
throw new NotFoundException(String.format("Unable to find value in %s for key %s", storeName, key));
}
final List<SongPlayCountBean> results = new ArrayList<>();
value.forEach(songPlayCount -> {
final HostStoreInfo host = metadataService.streamsMetadataForStoreAndKey(KafkaMusicExample.ALL_SONGS, songPlayCount.getSongId(), serializer);
// on the instance it is on.
if (!thisHost(host)) {
final SongBean song = client.target(String.format("http://%s:%d/kafka-music/song/%d", host.getHost(), host.getPort(), songPlayCount.getSongId())).request(MediaType.APPLICATION_JSON_TYPE).get(SongBean.class);
results.add(new SongPlayCountBean(song.getArtist(), song.getAlbum(), song.getName(), songPlayCount.getPlays()));
} else {
// look in the local store
final ReadOnlyKeyValueStore<Long, Song> songStore = streams.store(KafkaMusicExample.ALL_SONGS, QueryableStoreTypes.<Long, Song>keyValueStore());
final Song song = songStore.get(songPlayCount.getSongId());
results.add(new SongPlayCountBean(song.getArtist(), song.getAlbum(), song.getName(), songPlayCount.getPlays()));
}
});
return results;
}
use of io.confluent.examples.streams.interactivequeries.HostStoreInfo in project kafka-streams-examples by confluentinc.
the class OrdersService method getPostValidationWithTimeout.
@GET
@ManagedAsync
@Path("orders/{id}/validated")
public void getPostValidationWithTimeout(@PathParam("id") final String id, @QueryParam("timeout") @DefaultValue(CALL_TIMEOUT) Long timeout, @Suspended final AsyncResponse asyncResponse) {
setTimeout(timeout, asyncResponse);
HostStoreInfo hostForKey = getKeyLocationOrBlock(id, asyncResponse);
if (hostForKey == null) {
// request timed out so return
return;
}
// Retrieve the order locally or reach out to a different instance if the required partition is hosted elsewhere.
if (thisHost(hostForKey)) {
fetchLocal(id, asyncResponse, (k, v) -> (v.getState() == OrderState.VALIDATED || v.getState() == OrderState.FAILED));
} else {
fetchFromOtherHost(new Paths(hostForKey.getHost(), hostForKey.getPort()).urlGetValidated(id), asyncResponse, timeout);
}
}
use of io.confluent.examples.streams.interactivequeries.HostStoreInfo in project kafka-streams-examples by confluentinc.
the class OrdersService method getWithTimeout.
/**
* Perform a "Long-Poll" styled get. This method will attempt to get the value for the passed key
* blocking until the key is available or passed timeout is reached. Non-blocking IO is used to
* implement this, but the API will block the calling thread if no metastore data is available
* (for example on startup or during a rebalance)
*
* @param id - the key of the value to retrieve
* @param timeout - the timeout for the long-poll
* @param asyncResponse - async response used to trigger the poll early should the appropriate
* value become available
*/
@GET
@ManagedAsync
@Path("/orders/{id}")
@Produces({ MediaType.APPLICATION_JSON, MediaType.TEXT_PLAIN })
public void getWithTimeout(@PathParam("id") final String id, @QueryParam("timeout") @DefaultValue(CALL_TIMEOUT) Long timeout, @Suspended final AsyncResponse asyncResponse) {
setTimeout(timeout, asyncResponse);
HostStoreInfo hostForKey = getKeyLocationOrBlock(id, asyncResponse);
if (hostForKey == null) {
// request timed out so return
return;
}
// Retrieve the order locally or reach out to a different instance if the required partition is hosted elsewhere.
if (thisHost(hostForKey)) {
fetchLocal(id, asyncResponse, (k, v) -> true);
} else {
String path = new Paths(hostForKey.getHost(), hostForKey.getPort()).urlGet(id);
fetchFromOtherHost(path, asyncResponse, timeout);
}
}
Aggregations