private Timer resetHeartbeat() { return Threads.timer(Clock.SYSTEM, heartbeatInterval); } }
public ChangeEventSourceCoordinator(OffsetContext previousOffset, ErrorHandler errorHandler, Class<? extends SourceConnector> connectorType, String logicalName, ChangeEventSourceFactory changeEventSourceFactory, EventDispatcher<?> eventDispatcher) { this.previousOffset = previousOffset; this.errorHandler = errorHandler; this.changeEventSourceFactory = changeEventSourceFactory; this.executor = Threads.newSingleThreadExecutor(connectorType, logicalName, "change-event-source-coordinator"); this.eventDispatcher = eventDispatcher; }
public static ExecutorService newFixedThreadPool(Class<? extends SourceConnector> connector, String connectorId, String name, int threadCount) { return Executors.newFixedThreadPool(threadCount, threadFactory(connector, connectorId, name, true)); } }
/** * Create a thread that will call the supplied function when the {@link TimeSince elapsed time} has exceeded the * specified amount. The supplied {@link TimeSince} object will be {@link TimeSince#reset() reset} when the * new thread is started, and should also be {@link TimeSince#reset() reset} any time the elapsed time should be reset to 0. * <p> * The thread checks the elapsed time every 100 milliseconds. * * @param threadName the name of the new thread; may not be null * @param timeout the maximum amount of time that can elapse before the thread is interrupted; must be positive * @param timeoutUnit the unit for {@code timeout}; may not be null * @param elapsedTimer the component used to measure the elapsed time; may not be null * @param uponTimeout the function to be called when the maximum amount of time has elapsed; may not be null * @return the new thread that has not yet been {@link Thread#start() started}; never null */ public static Thread timeout(String threadName, long timeout, TimeUnit timeoutUnit, TimeSince elapsedTimer, Runnable uponTimeout) { return timeout(threadName, timeout, timeoutUnit, 100, TimeUnit.MILLISECONDS, elapsedTimer::elapsedTime, elapsedTimer::reset, uponTimeout); }
/** * Obtain a {@link Timer} that uses the given clock to indicate that a pre-defined time period expired. * * @param clock the clock; may not be null * @param time a time interval to expire * @return the {@link Timer} object; never null */ public static Timer timer(Clock clock, Duration time) { final TimeSince start = timeSince(clock); start.reset(); return new Timer() { @Override public boolean expired() { return start.elapsedTime() > time.toMillis(); } @Override public Duration remaining() { return time.minus(start.elapsedTime(), ChronoUnit.MILLIS); } }; }
/** * Create a thread that will interrupt the calling thread when the {@link TimeSince elapsed time} has exceeded the * specified amount. The supplied {@link TimeSince} object will be {@link TimeSince#reset() reset} when the * new thread is started, and should also be {@link TimeSince#reset() reset} any time the elapsed time should be reset to 0. * * @param threadName the name of the new thread; may not be null * @param timeout the maximum amount of time that can elapse before the thread is interrupted; must be positive * @param timeoutUnit the unit for {@code timeout}; may not be null * @param elapsedTimer the component used to measure the elapsed time; may not be null * @return the new thread that has not yet been {@link Thread#start() started}; never null */ public static Thread interruptAfterTimeout(String threadName, long timeout, TimeUnit timeoutUnit, TimeSince elapsedTimer) { Thread threadToInterrupt = Thread.currentThread(); return interruptAfterTimeout(threadName, timeout, timeoutUnit, elapsedTimer, threadToInterrupt); }
/** * Create a thread that will call the supplied function when the {@link TimeSince elapsed time} has exceeded the * specified amount. The supplied {@link TimeSince} object will be {@link TimeSince#reset() reset} when the * new thread is started, and should also be {@link TimeSince#reset() reset} any time the elapsed time should be reset to 0. * <p> * The thread checks the elapsed time every 100 milliseconds. * * @param threadName the name of the new thread; may not be null * @param timeout the maximum amount of time that can elapse before the thread is interrupted; must be positive * @param timeoutUnit the unit for {@code timeout}; may not be null * @param sleepInterval the amount of time for the new thread to sleep after checking the elapsed time; must be positive * @param sleepUnit the unit for {@code sleepInterval}; may not be null * @param elapsedTimer the component used to measure the elapsed time; may not be null * @param uponTimeout the function to be called when the maximum amount of time has elapsed; may not be null * @return the new thread that has not yet been {@link Thread#start() started}; never null */ public static Thread timeout(String threadName, long timeout, TimeUnit timeoutUnit, long sleepInterval, TimeUnit sleepUnit, TimeSince elapsedTimer, Runnable uponTimeout) { return timeout(threadName, timeout, timeoutUnit, sleepInterval, sleepUnit, elapsedTimer::elapsedTime, elapsedTimer::reset, uponTimeout); }
/** * Obtain a {@link Timer} that uses the given clock to indicate that a pre-defined time period expired. * * @param clock the clock; may not be null * @param time a time interval to expire * @return the {@link Timer} object; never null */ public static Timer timer(Clock clock, Duration time) { final TimeSince start = timeSince(clock); start.reset(); return new Timer() { @Override public boolean expired() { return start.elapsedTime() > time.toMillis(); } @Override public Duration remaining() { return time.minus(start.elapsedTime(), ChronoUnit.MILLIS); } }; }
/** * Create a thread that will interrupt the calling thread when the {@link TimeSince elapsed time} has exceeded the * specified amount. The supplied {@link TimeSince} object will be {@link TimeSince#reset() reset} when the * new thread is started, and should also be {@link TimeSince#reset() reset} any time the elapsed time should be reset to 0. * * @param threadName the name of the new thread; may not be null * @param timeout the maximum amount of time that can elapse before the thread is interrupted; must be positive * @param timeoutUnit the unit for {@code timeout}; may not be null * @param elapsedTimer the component used to measure the elapsed time; may not be null * @return the new thread that has not yet been {@link Thread#start() started}; never null */ public static Thread interruptAfterTimeout(String threadName, long timeout, TimeUnit timeoutUnit, TimeSince elapsedTimer) { Thread threadToInterrupt = Thread.currentThread(); return interruptAfterTimeout(threadName, timeout, timeoutUnit, elapsedTimer, threadToInterrupt); }
private Timer getTableScanLogTimer() { return Threads.timer(clock, LOG_INTERVAL); }
public ErrorHandler(Class<? extends SourceConnector> connectorType, String logicalName, ChangeEventQueue<?> queue, Runnable onThrowable) { this.queue = queue; this.onThrowable = onThrowable; this.executor = Threads.newSingleThreadExecutor(connectorType, logicalName, "error-handler"); this.producerThrowable = new AtomicReference<>(); }
/** * Create a thread that will interrupt the given thread when the {@link TimeSince elapsed time} has exceeded the * specified amount. The supplied {@link TimeSince} object will be {@link TimeSince#reset() reset} when the * new thread is started, and should also be {@link TimeSince#reset() reset} any time the elapsed time should be reset to 0. * * @param threadName the name of the new thread; may not be null * @param timeout the maximum amount of time that can elapse before the thread is interrupted; must be positive * @param timeoutUnit the unit for {@code timeout}; may not be null * @param elapsedTimer the component used to measure the elapsed time; may not be null * @param threadToInterrupt the thread that should be interrupted upon timeout; may not be null * @return the new thread that has not yet been {@link Thread#start() started}; never null */ public static Thread interruptAfterTimeout(String threadName, long timeout, TimeUnit timeoutUnit, TimeSince elapsedTimer, Thread threadToInterrupt) { return timeout(threadName, timeout, timeoutUnit, 100, TimeUnit.MILLISECONDS, elapsedTimer::elapsedTime, elapsedTimer::reset, () -> threadToInterrupt.interrupt()); }
public static ExecutorService newSingleThreadExecutor(Class<? extends SourceConnector> connector, String connectorId, String name) { return Executors.newSingleThreadExecutor(threadFactory(connector, connectorId, name, false)); }
@Override public void start() { super.start(); this.timer = Threads.timer(Clock.SYSTEM, timeout); }
/** * Start the snapshot and return immediately. Once started, the records read from the database can be retrieved using * {@link #poll()} until that method returns {@code null}. */ @Override protected void doStart() { executorService = Threads.newSingleThreadExecutor(MySqlConnector.class, context.getConnectorConfig().getLogicalName(), "snapshot"); executorService.execute(this::execute); }
/** * Create a thread that will call the supplied function when the {@link TimeSince elapsed time} has exceeded the * specified amount. The supplied {@link TimeSince} object will be {@link TimeSince#reset() reset} when the * new thread is started, and should also be {@link TimeSince#reset() reset} any time the elapsed time should be reset to 0. * <p> * The thread checks the elapsed time every 100 milliseconds. * * @param threadName the name of the new thread; may not be null * @param timeout the maximum amount of time that can elapse before the thread is interrupted; must be positive * @param timeoutUnit the unit for {@code timeout}; may not be null * @param elapsedTimer the component used to measure the elapsed time; may not be null * @param uponTimeout the function to be called when the maximum amount of time has elapsed; may not be null * @return the new thread that has not yet been {@link Thread#start() started}; never null */ public static Thread timeout(String threadName, long timeout, TimeUnit timeoutUnit, TimeSince elapsedTimer, Runnable uponTimeout) { return timeout(threadName, timeout, timeoutUnit, 100, TimeUnit.MILLISECONDS, elapsedTimer::elapsedTime, elapsedTimer::reset, uponTimeout); }
client.setThreadFactory(Threads.threadFactory(MySqlConnector.class, context.getConnectorConfig().getLogicalName(), "binlog-client", false)); client.setServerId(serverId); client.setSSLMode(sslModeFor(connectionContext.sslMode()));
/** * Returns the next batch of elements from this queue. May be empty in case no * elements have arrived in the maximum waiting time. * * @throws InterruptedException * if this thread has been interrupted while waiting for more * elements to arrive */ public List<T> poll() throws InterruptedException { LoggingContext.PreviousContext previousContext = loggingContextSupplier.get(); try { LOGGER.debug("polling records..."); List<T> records = new ArrayList<>(); final Timer timeout = Threads.timer(Clock.SYSTEM, Temporals.max(pollInterval, ConfigurationDefaults.RETURN_CONTROL_INTERVAL)); while (!timeout.expired() && queue.drainTo(records, maxBatchSize) == 0) { throwProducerFailureIfPresent(); LOGGER.debug("no records available yet, sleeping a bit..."); // no records yet, so wait a bit metronome.pause(); LOGGER.debug("checking for more records..."); } return records; } finally { previousContext.restore(); } }