/** * Get the current time in nanoseconds. * @return the current time in nanoseconds. */ default long currentTimeInNanos() { return currentTimeInMillis() * 1000000L; }
@Override public long elapsedTime() { long elapsed = clock.currentTimeInMillis() - lastTimeInMillis; return elapsed <= 0L ? 0L : elapsed; } };
@Override public void reset() { lastTimeInMillis = clock.currentTimeInMillis(); }
@Override public void pause() throws InterruptedException { while (next > timeSystem.currentTimeInMillis()) { Thread.sleep(next - timeSystem.currentTimeInMillis()); } next = next + periodInMillis; }
@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; } };
@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; } };
/** * Get the current time in microseconds. * @return the current time in microseconds. */ default long currentTimeInMicros() { return TimeUnit.MICROSECONDS.convert(currentTimeInMillis(), TimeUnit.MILLISECONDS); }
@Override public long getSnapshotDurationInSeconds() { final long startMillis = startTime.get(); if (startMillis <= 0L) { return 0; } long stopMillis = stopTime.get(); if (stopMillis == 0L) { stopMillis = clock.currentTimeInMillis(); } return (stopMillis - startMillis) / 1000L; }
@Override public long getMilliSecondsSinceLastEvent() { return (lastEventTimestamp.get() == -1) ? -1 : (clock.currentTimeInMillis() - lastEventTimestamp.get()); }
private void updateCommonEventMetrics() { totalNumberOfEventsSeen.incrementAndGet(); lastEventTimestamp.set(clock.currentTimeInMillis()); }
@Override public void snapshotCompleted() { this.snapshotCompleted.set(true); this.snapshotAborted.set(false); this.snapshotRunning.set(false); this.stopTime.set(clock.currentTimeInMillis()); }
@Override public void snapshotAborted() { this.snapshotCompleted.set(false); this.snapshotAborted.set(true); this.snapshotRunning.set(false); this.stopTime.set(clock.currentTimeInMillis()); }
@Override public void snapshotStarted() { this.snapshotRunning.set(true); this.snapshotCompleted.set(false); this.snapshotAborted.set(false); this.startTime.set(clock.currentTimeInMillis()); this.stopTime.set(0L); }
/** * Determine if we should flush offsets to storage, and if so then attempt to flush offsets. * * @param offsetWriter the offset storage writer; may not be null * @param policy the offset commit policy; may not be null * @param commitTimeout the timeout to wait for commit results * @param task the task which produced the records for which the offsets have been committed */ protected void maybeFlush(OffsetStorageWriter offsetWriter, OffsetCommitPolicy policy, Duration commitTimeout, SourceTask task) { // Determine if we need to commit to offset storage ... long timeSinceLastCommitMillis = clock.currentTimeInMillis() - timeOfLastCommitMillis; if (policy.performCommit(recordsSinceLastCommit, Duration.ofMillis(timeSinceLastCommitMillis))) { commitOffsets(offsetWriter, commitTimeout, task); } }
@Override protected ChangeRecordEmitter getChangeRecordEmitter(SnapshotContext snapshotContext, Object[] row) { ((SqlServerOffsetContext) snapshotContext.offset).setSourceTime(Instant.ofEpochMilli(getClock().currentTimeInMillis())); return new SnapshotChangeRecordEmitter(snapshotContext.offset, row, getClock()); }
private void emitReadRecord(Receiver receiver, TableSchema tableSchema) throws InterruptedException { Object[] newColumnValues = getNewColumnValues(); Object newKey = tableSchema.keyFromColumnData(newColumnValues); Struct newValue = tableSchema.valueFromColumnData(newColumnValues); Struct envelope = tableSchema.getEnvelopeSchema().read(newValue, offsetContext.getSourceInfo(), clock.currentTimeInMillis()); receiver.changeRecord(tableSchema, Operation.READ, newKey, envelope, offsetContext); }
private void emitUpdateRecord(Receiver receiver, TableSchema tableSchema) throws InterruptedException { Object[] oldColumnValues = getOldColumnValues(); Object[] newColumnValues = getNewColumnValues(); Object oldKey = tableSchema.keyFromColumnData(oldColumnValues); Object newKey = tableSchema.keyFromColumnData(newColumnValues); Struct newValue = tableSchema.valueFromColumnData(newColumnValues); Struct oldValue = tableSchema.valueFromColumnData(oldColumnValues); // regular update if (Objects.equals(oldKey, newKey)) { Struct envelope = tableSchema.getEnvelopeSchema().update(oldValue, newValue, offsetContext.getSourceInfo(), clock.currentTimeInMillis()); receiver.changeRecord(tableSchema, Operation.UPDATE, newKey, envelope, offsetContext); } // PK update -> emit as delete and re-insert with new key else { Struct envelope = tableSchema.getEnvelopeSchema().delete(oldValue, offsetContext.getSourceInfo(), clock.currentTimeInMillis()); receiver.changeRecord(tableSchema, Operation.DELETE, oldKey, envelope, offsetContext); envelope = tableSchema.getEnvelopeSchema().create(newValue, offsetContext.getSourceInfo(), clock.currentTimeInMillis()); receiver.changeRecord(tableSchema, Operation.CREATE, newKey, envelope, offsetContext); } }
private void emitCreateRecord(Receiver receiver, TableSchema tableSchema) throws InterruptedException { Object[] newColumnValues = getNewColumnValues(); Object newKey = tableSchema.keyFromColumnData(newColumnValues); Struct newValue = tableSchema.valueFromColumnData(newColumnValues); Struct envelope = tableSchema.getEnvelopeSchema().create(newValue, offsetContext.getSourceInfo(), clock.currentTimeInMillis()); receiver.changeRecord(tableSchema, Operation.CREATE, newKey, envelope, offsetContext); }
private void emitDeleteRecord(Receiver receiver, TableSchema tableSchema) throws InterruptedException { Object[] oldColumnValues = getOldColumnValues(); Object oldKey = tableSchema.keyFromColumnData(oldColumnValues); Struct oldValue = tableSchema.valueFromColumnData(oldColumnValues); Struct envelope = tableSchema.getEnvelopeSchema().delete(oldValue, offsetContext.getSourceInfo(), clock.currentTimeInMillis()); receiver.changeRecord(tableSchema, Operation.DELETE, oldKey, envelope, offsetContext); }
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()))); }