/** * Get the current time in nanoseconds. * @return the current time in nanoseconds. */ default long currentTimeInNanos() { return currentTimeInMillis() * 1000000L; }
public CdcSourceTaskContext(String connectorType, String connectorName, Supplier<Collection<? extends DataCollectionId>> collectionsSupplier) { this.connectorType = connectorType; this.connectorName = connectorName; this.collectionsSupplier = collectionsSupplier != null ? collectionsSupplier : Collections::emptyList; this.clock = Clock.system(); }
@Override public void pause() throws InterruptedException { while (next > timeSystem.currentTimeInNanos()) { LockSupport.parkNanos(next - timeSystem.currentTimeInNanos()); if ( Thread.currentThread().isInterrupted() ) { throw new InterruptedException(); } } next = next + periodInNanos; }
long snapshotStart = clock().currentTimeInMillis(); Connection jdbcConnection = null; try (PostgresConnection connection = taskContext.createConnection()) { sourceInfo.update(xlogStart, clock().currentTimeInMicros(), txId, null); long exportStart = clock().currentTimeInMillis(); logger.info("\t exporting data from table '{}'", tableId); try { rs -> readTable(tableId, rs, consumer, rowsCounter)); logger.info("\t finished exporting '{}' records for '{}'; total duration '{}'", rowsCounter.get(), tableId, Strings.duration(clock().currentTimeInMillis() - exportStart)); rowsCounter.set(0); } catch (SQLException e) { logger.info("Snapshot completed in '{}'", Strings.duration(clock().currentTimeInMillis() - snapshotStart)); Heartbeat .create( rollbackTransaction(jdbcConnection); logger.warn("Snapshot aborted after '{}'", Strings.duration(clock().currentTimeInMillis() - snapshotStart));
protected void generateReadRecord(TableId tableId, Object[] rowData) { if (rowData.length == 0) { return; } TableSchema tableSchema = schema().schemaFor(tableId); assert tableSchema != null; Object key = tableSchema.keyFromColumnData(rowData); Struct value = tableSchema.valueFromColumnData(rowData); if (key == null || value == null) { return; } Schema keySchema = tableSchema.keySchema(); sourceInfo.update(clock().currentTimeInMicros(), tableId); Map<String, ?> partition = sourceInfo.partition(); Map<String, ?> offset = sourceInfo.offset(); String topicName = topicSelector().topicNameFor(tableId); Envelope envelope = tableSchema.getEnvelopeSchema(); currentRecord.set(new SourceRecord(partition, offset, topicName, null, keySchema, key, envelope.schema(), envelope.read(value, sourceInfo.source(), clock().currentTimeInMillis()))); }
@Override public long elapsedTime() { long elapsed = clock.currentTimeInMillis() - lastTimeInMillis; return elapsed <= 0L ? 0L : elapsed; } };
long snapshotStart = clock().currentTimeInMillis(); Connection jdbcConnection = null; try (PostgresConnection connection = taskContext.createConnection()) { sourceInfo.update(xlogStart, clock().currentTimeInMicros(), txId, null); long exportStart = clock().currentTimeInMillis(); logger.info("\t exporting data from table '{}'", tableId); try { rs -> readTable(tableId, rs, consumer, rowsCounter)); logger.info("\t finished exporting '{}' records for '{}'; total duration '{}'", rowsCounter.get(), tableId, Strings.duration(clock().currentTimeInMillis() - exportStart)); rowsCounter.set(0); } catch (SQLException e) { logger.info("Snapshot completed in '{}'", Strings.duration(clock().currentTimeInMillis() - snapshotStart)); Heartbeat .create( rollbackTransaction(jdbcConnection); logger.warn("Snapshot aborted after '{}'", Strings.duration(clock().currentTimeInMillis() - snapshotStart));
@Override public EmbeddedEngine build() { if (classLoader == null) classLoader = getClass().getClassLoader(); if (clock == null) clock = Clock.system(); Objects.requireNonNull(config, "A connector configuration must be specified."); Objects.requireNonNull(handler, "A connector consumer or changeHandler must be specified."); return new EmbeddedEngine(config, classLoader, clock, handler, completionCallback, connectorCallback, offsetCommitPolicy); }
long periodInNanos = unit.toNanos(period); return new Metronome() { private long next = timeSystem.currentTimeInNanos() + periodInNanos;
@Override public void reset() { lastTimeInMillis = clock.currentTimeInMillis(); }
protected void generateReadRecord(TableId tableId, Object[] rowData) { // Clear the existing record to prevent reprocessing stale data. currentRecord.set(null); if (rowData.length == 0) { return; } TableSchema tableSchema = schema().schemaFor(tableId); assert tableSchema != null; Object key = tableSchema.keyFromColumnData(rowData); Struct value = tableSchema.valueFromColumnData(rowData); if (key == null || value == null) { return; } Schema keySchema = tableSchema.keySchema(); sourceInfo.update(clock().currentTimeInMicros(), tableId); Map<String, ?> partition = sourceInfo.partition(); Map<String, ?> offset = sourceInfo.offset(); String topicName = topicSelector().topicNameFor(tableId); Envelope envelope = tableSchema.getEnvelopeSchema(); currentRecord.set(new SourceRecord(partition, offset, topicName, null, keySchema, key, envelope.schema(), envelope.read(value, sourceInfo.source(), clock().currentTimeInMillis()))); }
/** * @param monitor the component used to periodically obtain the replica set specifications; may not be null * @param period the time period between polling checks; must be non-negative * @param unit the time unit for the {@code period}; may not be null * @param clock the clock to use; may be null if the system clock should be used * @param onStartup the function to call when the thread is started; may be null if not needed * @param onChange the function to call when the set of replica set specifications has changed; may be null if not needed */ public ReplicaSetMonitorThread(Supplier<ReplicaSets> monitor, long period, TimeUnit unit, Clock clock, Runnable onStartup, Consumer<ReplicaSets> onChange) { if (clock == null) clock = Clock.system(); this.monitor = monitor; this.metronome = Metronome.sleeper(period, unit, clock); this.onChange = onChange != null ? onChange : (rsSpecs) -> {}; this.onStartup = onStartup != null ? onStartup : () -> {}; }
@Override public void pause() throws InterruptedException { while (next > timeSystem.currentTimeInNanos()) { LockSupport.parkNanos(next - timeSystem.currentTimeInNanos()); if ( Thread.currentThread().isInterrupted() ) { throw new InterruptedException(); } } next = next + periodInNanos; }
@Override public void pause() throws InterruptedException { while (next > timeSystem.currentTimeInMillis()) { Thread.sleep(next - timeSystem.currentTimeInMillis()); } next = next + periodInMillis; }
public static void waitForSnapshotToBeCompleted() throws InterruptedException { int waitForSeconds = 60; final MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer(); final Metronome metronome = Metronome.sleeper(Duration.ofSeconds(1), Clock.system()); while (true) { if (waitForSeconds-- <= 0) { Assert.fail("Snapshot was not completed on time"); } try { final boolean completed = (boolean)mbeanServer.getAttribute(new ObjectName("debezium.sql_server:type=connector-metrics,context=snapshot,server=server1"), "SnapshotCompleted"); if (completed) { break; } } catch (InstanceNotFoundException e) { // Metrics has not started yet } catch (Exception e) { throw new IllegalStateException(e); } metronome.pause(); } } }
long periodInNanos = unit.toNanos(period); return new Metronome() { private long next = timeSystem.currentTimeInNanos() + periodInNanos;
@Override public boolean hasElapsed() { if (nextTimestamp == 0L) { // Initialize ... nextTimestamp = clock.currentTimeInMillis() + delayInMilliseconds; counter = 1L; return true; } long current = clock.currentTimeInMillis(); if (current >= nextTimestamp) { do { if (counter < Long.MAX_VALUE) ++counter; nextTimestamp += (delayInMilliseconds * counter); } while (nextTimestamp <= current); return true; } return false; } };
final Clock clock = Clock.system();
@Override public boolean hasElapsed() { if (nextTimestamp == 0L) { // Initialize ... nextTimestamp = clock.currentTimeInMillis() + delayInMilliseconds; return true; } long current = clock.currentTimeInMillis(); if (current >= nextTimestamp) { do { long multiple = 1 + (current - nextTimestamp) / delayInMilliseconds; nextTimestamp += multiple * delayInMilliseconds; } while (current > nextTimestamp); return true; } return false; } };
public CdcSourceTaskContext(String connectorType, String connectorName, Supplier<Collection<? extends DataCollectionId>> collectionsSupplier) { this.connectorType = connectorType; this.connectorName = connectorName; this.collectionsSupplier = collectionsSupplier != null ? collectionsSupplier : Collections::emptyList; this.clock = Clock.system(); }