use of org.apache.kafka.streams.processor.internals.GlobalStreamThread in project kafka by apache.
the class KafkaStreams method replaceStreamThread.
private void replaceStreamThread(final Throwable throwable) {
if (globalStreamThread != null && Thread.currentThread().getName().equals(globalStreamThread.getName())) {
log.warn("The global thread cannot be replaced. Reverting to shutting down the client.");
log.error("Encountered the following exception during processing " + " The streams client is going to shut down now. ", throwable);
closeToError();
}
final StreamThread deadThread = (StreamThread) Thread.currentThread();
deadThread.shutdown();
addStreamThread();
if (throwable instanceof RuntimeException) {
throw (RuntimeException) throwable;
} else if (throwable instanceof Error) {
throw (Error) throwable;
} else {
throw new RuntimeException("Unexpected checked exception caught in the uncaught exception handler", throwable);
}
}
use of org.apache.kafka.streams.processor.internals.GlobalStreamThread in project kafka by apache.
the class KafkaStreams method close.
/**
* Shutdown this {@code KafkaStreams} by signaling all the threads to stop, and then wait up to the timeout for the
* threads to join.
* A {@code timeout} of 0 means to wait forever.
*
* @param timeout how long to wait for the threads to shutdown
* @param timeUnit unit of time used for timeout
* @return {@code true} if all threads were successfully stopped—{@code false} if the timeout was reached
* before all threads stopped
*/
public synchronized boolean close(final long timeout, final TimeUnit timeUnit) {
log.debug("{} Stopping Kafka Stream process.", logPrefix);
if (state.isCreatedOrRunning()) {
setState(State.PENDING_SHUTDOWN);
// save the current thread so that if it is a stream thread
// we don't attempt to join it and cause a deadlock
final Thread shutdown = new Thread(new Runnable() {
@Override
public void run() {
// signal the threads to stop and wait
for (final StreamThread thread : threads) {
// avoid deadlocks by stopping any further state reports
// from the thread since we're shutting down
thread.setStateListener(null);
thread.close();
}
if (globalStreamThread != null) {
globalStreamThread.close();
if (!globalStreamThread.stillRunning()) {
try {
globalStreamThread.join();
} catch (final InterruptedException e) {
Thread.interrupted();
}
}
}
for (final StreamThread thread : threads) {
try {
if (!thread.stillRunning()) {
thread.join();
}
} catch (final InterruptedException ex) {
Thread.interrupted();
}
}
metrics.close();
log.info("{} Stopped Kafka Streams process.", logPrefix);
}
}, "kafka-streams-close-thread");
shutdown.setDaemon(true);
shutdown.start();
try {
shutdown.join(TimeUnit.MILLISECONDS.convert(timeout, timeUnit));
} catch (final InterruptedException e) {
Thread.interrupted();
}
setState(State.NOT_RUNNING);
return !shutdown.isAlive();
}
return true;
}
use of org.apache.kafka.streams.processor.internals.GlobalStreamThread in project kafka by apache.
the class KafkaStreamsTest method testStateGlobalThreadClose.
@Test
public void testStateGlobalThreadClose() throws Exception {
// make sure we have the global state thread running too
final StreamsBuilder builder = getBuilderWithSource();
builder.globalTable("anyTopic");
try (final LogCaptureAppender appender = LogCaptureAppender.createAndRegister(KafkaStreams.class);
final KafkaStreams streams = new KafkaStreams(builder.build(), props, supplier, time)) {
streams.start();
waitForCondition(() -> streams.state() == KafkaStreams.State.RUNNING, "Streams never started.");
final GlobalStreamThread globalStreamThread = streams.globalStreamThread;
globalStreamThread.shutdown();
waitForCondition(() -> globalStreamThread.state() == GlobalStreamThread.State.DEAD, "Thread never stopped.");
globalStreamThread.join();
// shutting down the global thread from "external" will yield an error in KafkaStreams
waitForCondition(() -> streams.state() == KafkaStreams.State.PENDING_ERROR, "Thread never stopped.");
streams.close();
waitForCondition(() -> streams.state() == KafkaStreams.State.ERROR, "Thread never stopped.");
assertThat(appender.getMessages(), hasItem(containsString("ERROR")));
}
}
Aggregations