use of org.apache.kafka.streams.errors.ProcessorStateException in project apache-kafka-on-k8s by banzaicloud.
the class ProcessorStateManagerTest method shouldThrowProcessorStateExceptionOnFlushIfStoreThrowsAnException.
@Test
public void shouldThrowProcessorStateExceptionOnFlushIfStoreThrowsAnException() throws IOException {
final ProcessorStateManager stateManager = new ProcessorStateManager(taskId, Collections.singleton(changelogTopicPartition), false, stateDirectory, Collections.singletonMap(storeName, changelogTopic), changelogReader, false, logContext);
final MockStateStore stateStore = new MockStateStore(storeName, true) {
@Override
public void flush() {
throw new RuntimeException("KABOOM!");
}
};
stateManager.register(stateStore, stateStore.stateRestoreCallback);
try {
stateManager.flush();
fail("Should throw ProcessorStateException if store flush throws exception");
} catch (final ProcessorStateException e) {
// pass
}
}
use of org.apache.kafka.streams.errors.ProcessorStateException in project apache-kafka-on-k8s by banzaicloud.
the class ProcessorStateManagerTest method shouldCloseAllStoresEvenIfStoreThrowsExcepiton.
@Test
public void shouldCloseAllStoresEvenIfStoreThrowsExcepiton() throws IOException {
final ProcessorStateManager stateManager = new ProcessorStateManager(taskId, Collections.singleton(changelogTopicPartition), false, stateDirectory, Collections.singletonMap(storeName, changelogTopic), changelogReader, false, logContext);
final AtomicBoolean closedStore = new AtomicBoolean(false);
final MockStateStore stateStore1 = new MockStateStore(storeName, true) {
@Override
public void close() {
throw new RuntimeException("KABOOM!");
}
};
final MockStateStore stateStore2 = new MockStateStore(storeName + "2", true) {
@Override
public void close() {
closedStore.set(true);
}
};
stateManager.register(stateStore1, stateStore1.stateRestoreCallback);
stateManager.register(stateStore2, stateStore2.stateRestoreCallback);
try {
stateManager.close(Collections.<TopicPartition, Long>emptyMap());
} catch (final ProcessorStateException expected) {
/* ignode */
}
Assert.assertTrue(closedStore.get());
}
use of org.apache.kafka.streams.errors.ProcessorStateException in project apache-kafka-on-k8s by banzaicloud.
the class ProcessorStateManagerTest method shouldThrowProcessorStateExceptionOnCloseIfStoreThrowsAnException.
@Test
public void shouldThrowProcessorStateExceptionOnCloseIfStoreThrowsAnException() throws IOException {
final ProcessorStateManager stateManager = new ProcessorStateManager(taskId, Collections.singleton(changelogTopicPartition), false, stateDirectory, Collections.singletonMap(storeName, changelogTopic), changelogReader, false, logContext);
final MockStateStore stateStore = new MockStateStore(storeName, true) {
@Override
public void close() {
throw new RuntimeException("KABOOM!");
}
};
stateManager.register(stateStore, stateStore.stateRestoreCallback);
try {
stateManager.close(Collections.<TopicPartition, Long>emptyMap());
fail("Should throw ProcessorStateException if store close throws exception");
} catch (final ProcessorStateException e) {
// pass
}
}
use of org.apache.kafka.streams.errors.ProcessorStateException in project apache-kafka-on-k8s by banzaicloud.
the class ProcessorStateManager method close.
/**
* {@link StateStore#close() Close} all stores (even in case of failure).
* Log all exception and re-throw the first exception that did occur at the end.
* @throws ProcessorStateException if any error happens when closing the state stores
*/
@Override
public void close(final Map<TopicPartition, Long> ackedOffsets) throws ProcessorStateException {
ProcessorStateException firstException = null;
// are not closed by a ProcessorNode yet
if (!stores.isEmpty()) {
log.debug("Closing its state manager and all the registered state stores");
for (final StateStore store : stores.values()) {
log.debug("Closing storage engine {}", store.name());
try {
store.close();
} catch (final Exception e) {
if (firstException == null) {
firstException = new ProcessorStateException(String.format("%sFailed to close state store %s", logPrefix, store.name()), e);
}
log.error("Failed to close state store {}: ", store.name(), e);
}
}
if (ackedOffsets != null) {
checkpoint(ackedOffsets);
}
stores.clear();
}
if (firstException != null) {
throw firstException;
}
}
use of org.apache.kafka.streams.errors.ProcessorStateException in project apache-kafka-on-k8s by banzaicloud.
the class ProcessorStateManager method updateStandbyStates.
List<ConsumerRecord<byte[], byte[]>> updateStandbyStates(final TopicPartition storePartition, final List<ConsumerRecord<byte[], byte[]>> records) {
final long limit = offsetLimit(storePartition);
List<ConsumerRecord<byte[], byte[]>> remainingRecords = null;
final List<KeyValue<byte[], byte[]>> restoreRecords = new ArrayList<>();
// restore states from changelog records
final BatchingStateRestoreCallback restoreCallback = getBatchingRestoreCallback(restoreCallbacks.get(storePartition.topic()));
long lastOffset = -1L;
int count = 0;
for (final ConsumerRecord<byte[], byte[]> record : records) {
if (record.offset() < limit) {
restoreRecords.add(KeyValue.pair(record.key(), record.value()));
lastOffset = record.offset();
} else {
if (remainingRecords == null) {
remainingRecords = new ArrayList<>(records.size() - count);
}
remainingRecords.add(record);
}
count++;
}
if (!restoreRecords.isEmpty()) {
try {
restoreCallback.restoreAll(restoreRecords);
} catch (final Exception e) {
throw new ProcessorStateException(String.format("%sException caught while trying to restore state from %s", logPrefix, storePartition), e);
}
}
// record the restored offset for its change log partition
restoredOffsets.put(storePartition, lastOffset + 1);
return remainingRecords;
}
Aggregations