Search in sources :

Example 1 with NamedTopologyStoreQueryParameters

use of org.apache.kafka.streams.processor.internals.namedtopology.NamedTopologyStoreQueryParameters in project kafka by apache.

the class StreamThreadStateStoreProvider method stores.

@SuppressWarnings("unchecked")
public <T> List<T> stores(final StoreQueryParameters storeQueryParams) {
    final StreamThread.State state = streamThread.state();
    if (state == StreamThread.State.DEAD) {
        return Collections.emptyList();
    }
    final String storeName = storeQueryParams.storeName();
    final QueryableStoreType<T> queryableStoreType = storeQueryParams.queryableStoreType();
    final String topologyName = storeQueryParams instanceof NamedTopologyStoreQueryParameters ? ((NamedTopologyStoreQueryParameters) storeQueryParams).topologyName() : null;
    if (storeQueryParams.staleStoresEnabled() ? state.isAlive() : state == StreamThread.State.RUNNING) {
        final Collection<Task> tasks = storeQueryParams.staleStoresEnabled() ? streamThread.allTasks().values() : streamThread.activeTasks();
        if (storeQueryParams.partition() != null) {
            for (final Task task : tasks) {
                if (task.id().partition() == storeQueryParams.partition() && (topologyName == null || topologyName.equals(task.id().topologyName())) && task.getStore(storeName) != null && storeName.equals(task.getStore(storeName).name())) {
                    final T typedStore = validateAndCastStores(task.getStore(storeName), queryableStoreType, storeName, task.id());
                    return Collections.singletonList(typedStore);
                }
            }
            return Collections.emptyList();
        } else {
            final List<T> list = new ArrayList<>();
            for (final Task task : tasks) {
                final StateStore store = task.getStore(storeName);
                if (store == null) {
                // then this task doesn't have that store
                } else {
                    final T typedStore = validateAndCastStores(store, queryableStoreType, storeName, task.id());
                    list.add(typedStore);
                }
            }
            return list;
        }
    } else {
        throw new InvalidStateStoreException("Cannot get state store " + storeName + " because the stream thread is " + state + ", not RUNNING" + (storeQueryParams.staleStoresEnabled() ? " or REBALANCING" : ""));
    }
}
Also used : Task(org.apache.kafka.streams.processor.internals.Task) InvalidStateStoreException(org.apache.kafka.streams.errors.InvalidStateStoreException) StreamThread(org.apache.kafka.streams.processor.internals.StreamThread) ArrayList(java.util.ArrayList) StateStore(org.apache.kafka.streams.processor.StateStore) NamedTopologyStoreQueryParameters(org.apache.kafka.streams.processor.internals.namedtopology.NamedTopologyStoreQueryParameters)

Aggregations

ArrayList (java.util.ArrayList)1 InvalidStateStoreException (org.apache.kafka.streams.errors.InvalidStateStoreException)1 StateStore (org.apache.kafka.streams.processor.StateStore)1 StreamThread (org.apache.kafka.streams.processor.internals.StreamThread)1 Task (org.apache.kafka.streams.processor.internals.Task)1 NamedTopologyStoreQueryParameters (org.apache.kafka.streams.processor.internals.namedtopology.NamedTopologyStoreQueryParameters)1