Table of Contents |
---|
Status
Current state: Under Discussion Accepted(vote)
Discussion thread: here
JIRA:
Jira | ||||||||
---|---|---|---|---|---|---|---|---|
|
...
Motivation
Currently, IQ throws InvalidStateStoreException for any types of error, that means a user cannot handle different types of error.
Because of that, we should throw different exceptions for each type.
Proposed Changes
Three categories exception to user
- StateStoreRetryableException: just wait, plain retry
- StateStoreMigratedException: need rediscover
- StateStoreFailException: fatal error, cannot retry or rediscover
To distinguish different types of error, we need to handle all InvalidStateStoreException better during these public methods invoked. The main change is to introduce new exceptions that extend from InvalidStateStoreException. InvalidStateStoreException is not thrown at all anymore, but only new sub-classes.
Code Block | ||
---|---|---|
| ||
public class StateStoreMigratedExceptionStreamsNotStartedException extends InvalidStateStoreException public class StateStoreRetryableExceptionStreamsRebalancingException extends InvalidStateStoreException public class StateStoreFailExceptionStateStoreMigratedException extends InvalidStateStoreException |
Call Trace 1: KafkaStreams#store()
KafkaStreams#store()
==> QueryableStoreProvider#getStore()
==> GlobalStateStore#stores()
==> StreamThreadStateStoreProvider#stores()
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
public <T> List<T> stores(final String storeName, final QueryableStoreType<T> queryableStoreType) {
final StateStore store = globalStateStores.get(storeName);
if (store == null || !queryableStoreType.accepts(store)) {
return Collections.emptyList();
}
if (!store.isOpen()) {
// Before:
// throw new InvalidStateStoreException("the state store, " + storeName + ", is not open.");
throw new StateStoreClosedException("the state store, " + storeName + ", is not open.");
}
return (List<T>) Collections.singletonList(store);
} |
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
public <T> List<T> stores(final String storeName, final QueryableStoreType<T> queryableStoreType) {
if (streamThread.state() == StreamThread.State.DEAD) {
return Collections.emptyList();
}
if (!streamThread.isRunningAndNotRebalancing()) {
// Before:
// throw new InvalidStateStoreException("the state store, " + storeName + ", may have migrated to another instance.");
throw new StateStoreMigratedException("the state store, " + storeName + ", may have migrated to another instance.");
}
final List<T> stores = new ArrayList<>();
for (Task streamTask : streamThread.tasks().values()) {
final StateStore store = streamTask.getStore(storeName);
if (store != null && queryableStoreType.accepts(store)) {
if (!store.isOpen()) {
// Before:
// throw new InvalidStateStoreException("the state store, " + storeName + ", may have migrated to another instance.");
throw new StateStoreMigratedException("the state store, " + storeName + ", may have migrated to another instance.");
}
stores.add((T) store);
}
}
return stores;
} |
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
public <T> T store(final String storeName, final QueryableStoreType<T> queryableStoreType) {
validateIsRunning();
try {
return queryableStoreProvider.getStore(storeName, queryableStoreType);
} catch (InvalidStateStoreException e) {
if (state==State.RUNNING || state==State.REBALANCING) {
if (e instanceof StateStoreClosedException)
throw new StateStoreRetryableException(e);
else // e instanceof StateStoreMigratedException
throw e;
} else {
// state==State.PENDING_SHUTDOWN || state==State.ERROR || state==State.NOT_RUNNING
throw new StateStoreFailException(e);
}
}
} |
Call Trace 2: CompositeReadOnlyKeyValueStore#get()
CompositeReadOnlyKeyValueStore#get()
==> WrappingStoreProvider#stores()
==> StreamThreadStateStoreProvider#stores()
==> MeteredKeyValueBytesStore#get()
==> InnerMeteredKeyValueStore#get()
==> CachingKeyValueStore#get()
==> WrappedStateStore.AbstractStateStore#validateStoreOpen()
==> RocksDBStore#isOpen()
==> CachingKeyValueStore#getInternal()
==> ChangeLoggingKeyValueBytesStore#get()
==> RocksDBStore#get()
==> RocksDBStore#validateStoreOpen()
==> RocksDBStore#getInternal()
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
|
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
public <T> List<T> stores(final String storeName, QueryableStoreType<T> type) {
final List<T> allStores = new ArrayList<>();
for (StateStoreProvider provider : storeProviders) {
final List<T> stores =
provider.stores(storeName, type);
allStores.addAll(stores);
}
if (allStores.isEmpty()) {
// Before: throw new InvalidStateStoreException("the state store, " + storeName + ", may have migrated to another instance.");
throw new StateStoreMigratedException("the state store, " + storeName + ", may have migrated to another instance.");
}
return allStores;
} |
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
private void validateStoreOpen() {
if (!open) {
// Before: throw new InvalidStateStoreException("Store " + this.name + " is currently closed");
throw new StateStoreClosedException("Store " + this.name + " is currently closed");
}
} |
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
void validateStoreOpen() {
if (!innerState.isOpen()) {
// Before: throw new InvalidStateStoreException("Store " + innerState.name() + " is currently closed.");
throw new StateStoreClosedException("Store " + innerState.name() + " is currently closed.");
}
} |
Code Block | ||||||
---|---|---|---|---|---|---|
| ||||||
public V get(final K key) {
Objects.requireNonNull(key);
final List<ReadOnlyKeyValueStore<K, V>> stores;
try {
try {
stores = storeProvider.stores(storeName, storeType);
} catch (StateStoreClosedException e) {
if (streams.state()== KafkaStreams.State.RUNNING || streams.state()== KafkaStreams.State.REBALANCING)
throw new StateStoreRetryableException(e);
else
throw e;
}
for (ReadOnlyKeyValueStore<K, V> store : stores) {
try {
final V result = store.get(key);
if (result != null) {
return result;
}
} catch (StateStoreClosedException e) {
// Before: throw new InvalidStateStoreException("State store is not available anymore and may have been migrated to another instance; please re-discover its location from the state metadata.");
throw new StateStoreMigratedException("State store is not available anymore and may have been migrated to another instance; please re-discover its location from the state metadata.");
}
}
} catch (InvalidStateStoreException e) {
if (streams.state()== KafkaStreams.State.PENDING_SHUTDOWN
|| streams.state()== KafkaStreams.State.ERROR
|| streams.state()==KafkaStreams.State.NOT_RUNNING) {
throw new StateStoreFailException(e);
} else
throw e;
}
return null;
} |
public class StateStoreNotAvailableException extends InvalidStateStoreException
public class UnknownStateStoreException extends InvalidStateStoreException
public class InvalidStateStorePartitionException extends InvalidStateStoreException
|
- StreamsNotStartedException: will be thrown when stream thread state is CREATED, the user can retry until to RUNNING.
- StreamsRebalancingException: will be thrown when stream thread is not running and stream state is REBALANCING, the user just retry and wait until rebalance finished (RUNNING).
- StateStoreMigratedException: will be thrown when state store already closed and stream state is RUNNING. The user need to rediscover the store and cannot blindly retry as the store handle is invalid and a new store handle must be retrived.
- StateStoreNotAvailableException: will be thrown when state store closed and stream state is PENDING_SHUTDOWN / NOT_RUNNING / ERROR. The user cannot retry when this exception is thrown.
- UnknownStateStoreException: will be thrown when passing an unknown state store. The user cannot retry when this exception is thrown.
- InvalidStateStorePartitionException: will be thrown when user requested partition is not available on the stream instance.
The following is the public methods that users will call to get state store instance:
- KafkaStreams
- @Deprecated store(storeName, queryableStoreType)
- store(storeQureyParams)
Info |
---|
All the above methods could be throw exceptions: StreamsNotStartedException, StreamsRebalancingException, StateStoreMigratedException, StateStoreNotAvailableException, UnknownStateStoreException, InvalidStateStorePartitionException |
The following is the public methods that users will call to get store values:
- interface ReadOnlyKeyValueStore(class CompositeReadOnlyKeyValueStore)
- get(key)
- range(from, to)
- all()
- approximateNumEntries()
- interface ReadOnlySessionStore(class CompositeReadOnlySessionStore)
- fetch(key)
- fetch(from, to)
- interface ReadOnlyWindowStore(class CompositeReadOnlyWindowStore)
- fetch(key, time)
fetch(key, from, to)
- fetch(from, to, fromTime, toTime)
- all()
- fetchAll(from, to)
- @Deprecated fetch(key, timeFrom, timeTo)
- @Deprecated fetch(from, to, timeFrom, timeTo)
- @Deprecated fetchAll(timeFrom, timeTo)
- interface KeyValueIterator(class DelegatingPeekingKeyValueIterator)
- next()
- hasNext()
- peekNextKey()
Info |
---|
All the above methods could be throw following exceptions: StreamsRebalancingException, StateStoreMigratedException, StateStoreNotAvailableException, InvalidStateStorePartitionException |
Compatibility, Deprecation, and Migration Plan
- All new exceptions extend from InvalidStateStoreException, this change will be fully backward compatible.
Rejected Alternatives
None.
...