Search in sources :

Example 1 with HostStoreInfo

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;
}
Also used : ArrayList(java.util.ArrayList) NotFoundException(javax.ws.rs.NotFoundException) HostStoreInfo(io.confluent.examples.streams.interactivequeries.HostStoreInfo) Song(io.confluent.examples.streams.avro.Song)

Example 2 with HostStoreInfo

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);
    }
}
Also used : Paths(io.confluent.examples.streams.microservices.util.Paths) HostStoreInfo(io.confluent.examples.streams.interactivequeries.HostStoreInfo) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET) ManagedAsync(org.glassfish.jersey.server.ManagedAsync)

Example 3 with HostStoreInfo

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);
    }
}
Also used : Paths(io.confluent.examples.streams.microservices.util.Paths) HostStoreInfo(io.confluent.examples.streams.interactivequeries.HostStoreInfo) Path(javax.ws.rs.Path) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET) ManagedAsync(org.glassfish.jersey.server.ManagedAsync)

Aggregations

HostStoreInfo (io.confluent.examples.streams.interactivequeries.HostStoreInfo)3 Paths (io.confluent.examples.streams.microservices.util.Paths)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 ManagedAsync (org.glassfish.jersey.server.ManagedAsync)2 Song (io.confluent.examples.streams.avro.Song)1 ArrayList (java.util.ArrayList)1 NotFoundException (javax.ws.rs.NotFoundException)1 Produces (javax.ws.rs.Produces)1