001/*
002 * (C) Copyright 2017 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     bdelbosc
018 */
019package org.nuxeo.lib.stream.computation;
020
021import java.time.Duration;
022
023import org.nuxeo.lib.stream.log.Latency;
024
025/**
026 * Run a topology of computations according to some settings.
027 *
028 * @since 9.3
029 */
030public interface StreamProcessor {
031
032    /**
033     * Initialize streams, but don't run the computations
034     */
035    StreamProcessor init(Topology topology, Settings settings);
036
037    /**
038     * Run the initialized computations.
039     */
040    void start();
041
042    /**
043     * Try to stop computations gracefully after processing a record or a timer within the timeout duration. If this can
044     * not be done within the timeout, shutdown and returns false.
045     */
046    boolean stop(Duration timeout);
047
048    /**
049     * Stop computations when input streams are empty. The timeout is applied for each computation, the total duration
050     * can be up to nb computations * timeout
051     * <p/>
052     * Returns {@code true} if computations are stopped during the timeout delay.
053     */
054    boolean drainAndStop(Duration timeout);
055
056    /**
057     * Shutdown immediately.
058     */
059    void shutdown();
060
061    /**
062     * Returns the low watermark for the computation. Any message with an offset below the low watermark has been
063     * processed by this computation and its ancestors. The returned watermark is local to this processing node, if the
064     * computation is distributed the global low watermark is the minimum of all nodes low watermark.
065     */
066    long getLowWatermark(String computationName);
067
068    /**
069     * Returns the low watermark for all the computations of the topology. Any message with an offset below the low
070     * watermark has been processed. The returned watermark is local to this processing node.
071     */
072    long getLowWatermark();
073
074    /**
075     * Returns the latency for a computation. This works also for distributed computations.
076     *
077     * @since 10.1
078     */
079    Latency getLatency(String computationName);
080
081    /**
082     * Returns true if all messages with a lower timestamp has been processed by the topology.
083     */
084    boolean isDone(long timestamp);
085
086    /**
087     * Wait for the computations to have assigned partitions ready to process records. The processor must be started.
088     * This is useful for writing unit test.
089     * <p/>
090     * Returns {@code true} if all computations have assigned partitions during the timeout delay.
091     */
092    boolean waitForAssignments(Duration timeout) throws InterruptedException;
093
094    /**
095     * True if there is no active processing threads.
096     *
097     * @since 10.1
098     */
099    boolean isTerminated();
100}