Search in sources :

Example 6 with GlobalStreamThread

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);
    }
}
Also used : GlobalStreamThread(org.apache.kafka.streams.processor.internals.GlobalStreamThread) StreamThread(org.apache.kafka.streams.processor.internals.StreamThread) AssignorError(org.apache.kafka.streams.processor.internals.assignment.AssignorError)

Example 7 with GlobalStreamThread

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;
}
Also used : GlobalStreamThread(org.apache.kafka.streams.processor.internals.GlobalStreamThread) StreamThread(org.apache.kafka.streams.processor.internals.StreamThread) GlobalStreamThread(org.apache.kafka.streams.processor.internals.GlobalStreamThread) StreamThread(org.apache.kafka.streams.processor.internals.StreamThread)

Example 8 with GlobalStreamThread

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")));
    }
}
Also used : GlobalStreamThread(org.apache.kafka.streams.processor.internals.GlobalStreamThread) LogCaptureAppender(org.apache.kafka.streams.processor.internals.testutil.LogCaptureAppender) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Aggregations

GlobalStreamThread (org.apache.kafka.streams.processor.internals.GlobalStreamThread)8 StreamThread (org.apache.kafka.streams.processor.internals.StreamThread)6 Test (org.junit.Test)3 IntegrationTest (org.apache.kafka.test.IntegrationTest)2 TestCondition (org.apache.kafka.test.TestCondition)2 AssignorError (org.apache.kafka.streams.processor.internals.assignment.AssignorError)1 LogCaptureAppender (org.apache.kafka.streams.processor.internals.testutil.LogCaptureAppender)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1