Search in sources :

Example 6 with StreamThread

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

the class StreamThreadStateStoreProviderTest method before.

@Before
public void before() throws IOException {
    final TopologyBuilder builder = new TopologyBuilder();
    builder.addSource("the-source", "the-source");
    builder.addProcessor("the-processor", new MockProcessorSupplier(), "the-source");
    builder.addStateStore(Stores.create("kv-store").withStringKeys().withStringValues().inMemory().build(), "the-processor");
    builder.addStateStore(Stores.create("window-store").withStringKeys().withStringValues().persistent().windowed(10, 10, 2, false).build(), "the-processor");
    final Properties properties = new Properties();
    final String applicationId = "applicationId";
    properties.put(StreamsConfig.APPLICATION_ID_CONFIG, applicationId);
    properties.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, "localhost:9092");
    stateDir = TestUtils.tempDirectory();
    final String stateConfigDir = stateDir.getPath();
    properties.put(StreamsConfig.STATE_DIR_CONFIG, stateConfigDir);
    final StreamsConfig streamsConfig = new StreamsConfig(properties);
    final MockClientSupplier clientSupplier = new MockClientSupplier();
    configureRestoreConsumer(clientSupplier, "applicationId-kv-store-changelog");
    configureRestoreConsumer(clientSupplier, "applicationId-window-store-changelog");
    builder.setApplicationId(applicationId);
    final ProcessorTopology topology = builder.build(null);
    final Map<TaskId, StreamTask> tasks = new HashMap<>();
    stateDirectory = new StateDirectory(applicationId, stateConfigDir, new MockTime());
    taskOne = createStreamsTask(applicationId, streamsConfig, clientSupplier, topology, new TaskId(0, 0));
    tasks.put(new TaskId(0, 0), taskOne);
    taskTwo = createStreamsTask(applicationId, streamsConfig, clientSupplier, topology, new TaskId(0, 1));
    tasks.put(new TaskId(0, 1), taskTwo);
    storesAvailable = true;
    thread = new StreamThread(builder, streamsConfig, clientSupplier, applicationId, "clientId", UUID.randomUUID(), new Metrics(), Time.SYSTEM, new StreamsMetadataState(builder, StreamsMetadataState.UNKNOWN_HOST), 0) {

        @Override
        public Map<TaskId, StreamTask> tasks() {
            return tasks;
        }

        @Override
        public boolean isInitialized() {
            return storesAvailable;
        }
    };
    provider = new StreamThreadStateStoreProvider(thread);
}
Also used : ProcessorTopology(org.apache.kafka.streams.processor.internals.ProcessorTopology) TaskId(org.apache.kafka.streams.processor.TaskId) TopologyBuilder(org.apache.kafka.streams.processor.TopologyBuilder) HashMap(java.util.HashMap) StreamThread(org.apache.kafka.streams.processor.internals.StreamThread) Properties(java.util.Properties) MockStreamsMetrics(org.apache.kafka.streams.processor.internals.MockStreamsMetrics) Metrics(org.apache.kafka.common.metrics.Metrics) StreamsMetadataState(org.apache.kafka.streams.processor.internals.StreamsMetadataState) MockProcessorSupplier(org.apache.kafka.test.MockProcessorSupplier) MockClientSupplier(org.apache.kafka.test.MockClientSupplier) HashMap(java.util.HashMap) Map(java.util.Map) StreamTask(org.apache.kafka.streams.processor.internals.StreamTask) MockTime(org.apache.kafka.common.utils.MockTime) StreamsConfig(org.apache.kafka.streams.StreamsConfig) StateDirectory(org.apache.kafka.streams.processor.internals.StateDirectory) Before(org.junit.Before)

Example 7 with StreamThread

use of org.apache.kafka.streams.processor.internals.StreamThread 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&mdash;{@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)

Aggregations

StreamThread (org.apache.kafka.streams.processor.internals.StreamThread)7 Metrics (org.apache.kafka.common.metrics.Metrics)4 StreamsConfig (org.apache.kafka.streams.StreamsConfig)4 Field (java.lang.reflect.Field)3 KafkaStreams (org.apache.kafka.streams.KafkaStreams)3 KStreamBuilder (org.apache.kafka.streams.kstream.KStreamBuilder)3 DefaultKafkaClientSupplier (org.apache.kafka.streams.processor.internals.DefaultKafkaClientSupplier)3 GlobalStreamThread (org.apache.kafka.streams.processor.internals.GlobalStreamThread)3 TestCondition (org.apache.kafka.test.TestCondition)3 Test (org.junit.Test)3 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 MockTime (org.apache.kafka.common.utils.MockTime)1 TaskId (org.apache.kafka.streams.processor.TaskId)1 TopologyBuilder (org.apache.kafka.streams.processor.TopologyBuilder)1 MockStreamsMetrics (org.apache.kafka.streams.processor.internals.MockStreamsMetrics)1 ProcessorTopology (org.apache.kafka.streams.processor.internals.ProcessorTopology)1 StateDirectory (org.apache.kafka.streams.processor.internals.StateDirectory)1 StreamTask (org.apache.kafka.streams.processor.internals.StreamTask)1