/
AbstractLogMinerEventProcessor.java
1134 lines (1017 loc) · 48.4 KB
/
AbstractLogMinerEventProcessor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright Debezium Authors.
*
* Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
*/
package io.debezium.connector.oracle.logminer.processor;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneOffset;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.debezium.DebeziumException;
import io.debezium.connector.oracle.OracleConnection;
import io.debezium.connector.oracle.OracleConnection.NonRelationalTableException;
import io.debezium.connector.oracle.OracleConnectorConfig;
import io.debezium.connector.oracle.OracleDatabaseSchema;
import io.debezium.connector.oracle.OracleOffsetContext;
import io.debezium.connector.oracle.OraclePartition;
import io.debezium.connector.oracle.OracleSchemaChangeEventEmitter;
import io.debezium.connector.oracle.OracleStreamingChangeEventSourceMetrics;
import io.debezium.connector.oracle.Scn;
import io.debezium.connector.oracle.logminer.LogMinerChangeRecordEmitter;
import io.debezium.connector.oracle.logminer.events.DmlEvent;
import io.debezium.connector.oracle.logminer.events.EventType;
import io.debezium.connector.oracle.logminer.events.LobEraseEvent;
import io.debezium.connector.oracle.logminer.events.LobWriteEvent;
import io.debezium.connector.oracle.logminer.events.LogMinerEvent;
import io.debezium.connector.oracle.logminer.events.LogMinerEventRow;
import io.debezium.connector.oracle.logminer.events.SelectLobLocatorEvent;
import io.debezium.connector.oracle.logminer.events.TruncateEvent;
import io.debezium.connector.oracle.logminer.parser.DmlParserException;
import io.debezium.connector.oracle.logminer.parser.LogMinerDmlEntry;
import io.debezium.connector.oracle.logminer.parser.LogMinerDmlEntryImpl;
import io.debezium.connector.oracle.logminer.parser.LogMinerDmlParser;
import io.debezium.connector.oracle.logminer.parser.SelectLobParser;
import io.debezium.data.Envelope;
import io.debezium.pipeline.EventDispatcher;
import io.debezium.pipeline.source.spi.ChangeEventSource.ChangeEventSourceContext;
import io.debezium.relational.Table;
import io.debezium.relational.TableId;
import io.debezium.util.Clock;
import io.debezium.util.Strings;
/**
* An abstract implementation of {@link LogMinerEventProcessor} that all processors should extend.
*
* @author Chris Cranford
*/
public abstract class AbstractLogMinerEventProcessor<T extends AbstractTransaction> implements LogMinerEventProcessor {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractLogMinerEventProcessor.class);
private static final String NO_SEQUENCE_TRX_ID_SUFFIX = "ffffffff";
private final ChangeEventSourceContext context;
private final OracleConnectorConfig connectorConfig;
private final OracleDatabaseSchema schema;
private final OraclePartition partition;
private final OracleOffsetContext offsetContext;
private final EventDispatcher<OraclePartition, TableId> dispatcher;
private final OracleStreamingChangeEventSourceMetrics metrics;
private final LogMinerDmlParser dmlParser;
private final SelectLobParser selectLobParser;
protected final Counters counters;
private Scn currentOffsetScn = Scn.NULL;
private Map<Integer, Scn> currentOffsetCommitScns = new HashMap<>();
private Scn lastProcessedScn = Scn.NULL;
private boolean sequenceUnavailable = false;
public AbstractLogMinerEventProcessor(ChangeEventSourceContext context,
OracleConnectorConfig connectorConfig,
OracleDatabaseSchema schema,
OraclePartition partition,
OracleOffsetContext offsetContext,
EventDispatcher<OraclePartition, TableId> dispatcher,
OracleStreamingChangeEventSourceMetrics metrics) {
this.context = context;
this.connectorConfig = connectorConfig;
this.schema = schema;
this.partition = partition;
this.offsetContext = offsetContext;
this.dispatcher = dispatcher;
this.metrics = metrics;
this.counters = new Counters();
this.dmlParser = new LogMinerDmlParser();
this.selectLobParser = new SelectLobParser();
}
protected OracleConnectorConfig getConfig() {
return connectorConfig;
}
protected OracleDatabaseSchema getSchema() {
return schema;
}
/**
* Check whether a transaction has been recently processed through either a commit or rollback.
*
* @param transactionId the unique transaction id
* @return true if the transaction has been recently processed, false otherwise
*/
protected boolean isRecentlyProcessed(String transactionId) {
return false;
}
/**
* Checks whether the LogMinerEvent row for a schema change can be emitted.
*
* @param row the result set row
* @return true if the schema change has been seen, false otherwise.
*/
protected boolean hasSchemaChangeBeenSeen(LogMinerEventRow row) {
return false;
}
/**
* Return the last processed system change number handled by the processor.
*
* @return the last processed system change number, never {@code null}.
*/
protected Scn getLastProcessedScn() {
return lastProcessedScn;
}
/**
* Returns the {@code TransactionCache} implementation.
* @return the transaction cache, never {@code null}
*/
protected abstract Map<String, T> getTransactionCache();
/**
* Creates a new transaction based on the supplied {@code START} event.
*
* @param row the event row, must not be {@code null}
* @return the implementation-specific {@link Transaction} instance
*/
protected abstract T createTransaction(LogMinerEventRow row);
/**
* Removes a specific transaction event by database row identifier.
*
* @param row the event row that contains the row identifier, must not be {@code null}
*/
protected abstract void removeEventWithRowId(LogMinerEventRow row);
/**
* Returns the number of events associated with the specified transaction.
*
* @param transaction the transaction, must not be {@code null}
* @return the number of events in the transaction
*/
protected abstract int getTransactionEventCount(T transaction);
// todo: can this be removed in favor of a single implementation?
protected boolean isTrxIdRawValue() {
return true;
}
@Override
public Scn process(Scn startScn, Scn endScn) throws SQLException, InterruptedException {
counters.reset();
try (PreparedStatement statement = createQueryStatement()) {
LOGGER.debug("Fetching results for SCN [{}, {}]", startScn, endScn);
statement.setFetchSize(getConfig().getQueryFetchSize());
statement.setFetchDirection(ResultSet.FETCH_FORWARD);
statement.setString(1, startScn.toString());
statement.setString(2, endScn.toString());
Instant queryStart = Instant.now();
try (ResultSet resultSet = statement.executeQuery()) {
metrics.setLastDurationOfBatchCapturing(Duration.between(queryStart, Instant.now()));
Instant startProcessTime = Instant.now();
processResults(this.partition, resultSet);
Duration totalTime = Duration.between(startProcessTime, Instant.now());
metrics.setLastCapturedDmlCount(counters.dmlCount);
if (counters.dmlCount > 0 || counters.commitCount > 0 || counters.rollbackCount > 0) {
warnPotentiallyStuckScn(currentOffsetScn, currentOffsetCommitScns);
currentOffsetScn = offsetContext.getScn();
if (offsetContext.getCommitScn() != null) {
currentOffsetCommitScns = offsetContext.getCommitScn().getCommitScnForAllRedoThreads();
}
}
LOGGER.debug("{}.", counters);
LOGGER.debug("Processed in {} ms. Lag: {}. Offset SCN: {}, Offset Commit SCN: {}, Active Transactions: {}, Sleep: {}",
totalTime.toMillis(), metrics.getLagFromSourceInMilliseconds(), offsetContext.getScn(),
offsetContext.getCommitScn(), metrics.getNumberOfActiveTransactions(),
metrics.getMillisecondToSleepBetweenMiningQuery());
metrics.addProcessedRows(counters.rows);
return calculateNewStartScn(endScn, offsetContext.getCommitScn().getMaxCommittedScn());
}
}
}
/**
* Create the JDBC query that will be used to fetch the mining result set.
*
* @return a prepared query statement, never {@code null}
* @throws SQLException if a database exception occurred creating the statement
*/
protected abstract PreparedStatement createQueryStatement() throws SQLException;
/**
* Calculates the new starting system change number based on the current processing range.
*
* @param endScn the end system change number for the previously mined range, never {@code null}
* @param maxCommittedScn the maximum committed system change number, never {@code null}
* @return the system change number to start then next mining iteration from, never {@code null}
* @throws InterruptedException if the current thread is interrupted
*/
protected abstract Scn calculateNewStartScn(Scn endScn, Scn maxCommittedScn) throws InterruptedException;
/**
* Processes the LogMiner results.
*
* @param resultSet the result set from a LogMiner query
* @throws SQLException if a database exception occurred
* @throws InterruptedException if the dispatcher was interrupted sending an event
*/
protected void processResults(OraclePartition partition, ResultSet resultSet) throws SQLException, InterruptedException {
while (context.isRunning() && hasNextWithMetricsUpdate(resultSet)) {
counters.rows++;
processRow(partition, LogMinerEventRow.fromResultSet(resultSet, getConfig().getCatalogName(), isTrxIdRawValue()));
}
}
/**
* Processes a single LogMinerEventRow.
*
* @param row the event row, must not be {@code null}
* @throws SQLException if a database exception occurred
* @throws InterruptedException if the dispatcher was interrupted sending an event
*/
protected void processRow(OraclePartition partition, LogMinerEventRow row) throws SQLException, InterruptedException {
if (!row.getEventType().equals(EventType.MISSING_SCN)) {
lastProcessedScn = row.getScn();
}
// filter out all events that are captured as part of the initial snapshot
if (row.getScn().compareTo(offsetContext.getSnapshotScn()) < 0) {
Map<String, Scn> snapshotPendingTransactions = offsetContext.getSnapshotPendingTransactions();
if (snapshotPendingTransactions == null || !snapshotPendingTransactions.containsKey(row.getTransactionId())) {
LOGGER.debug("Skipping event {} (SCN {}) because it is already encompassed by the initial snapshot", row.getEventType(), row.getScn());
return;
}
}
switch (row.getEventType()) {
case MISSING_SCN:
handleMissingScn(row);
case START:
handleStart(row);
break;
case COMMIT:
handleCommit(partition, row);
break;
case ROLLBACK:
handleRollback(row);
break;
case DDL:
handleSchemaChange(row);
break;
case SELECT_LOB_LOCATOR:
handleSelectLobLocator(row);
break;
case LOB_WRITE:
handleLobWrite(row);
break;
case LOB_ERASE:
handleLobErase(row);
break;
case INSERT:
case UPDATE:
case DELETE:
handleDataEvent(row);
break;
case UNSUPPORTED:
handleUnsupportedEvent(row);
break;
}
}
/**
* Handle processing a LogMinerEventRow for a {@code MISSING_SCN} event.
*
* @param row the result set row
*/
protected void handleMissingScn(LogMinerEventRow row) {
LOGGER.warn("Missing SCN detected. {}", row);
}
/**
* Handle processing a LogMinerEventRow for a {@code START} event.
*
* @param row the result set row
*/
protected void handleStart(LogMinerEventRow row) {
final String transactionId = row.getTransactionId();
final AbstractTransaction transaction = getTransactionCache().get(transactionId);
if (transaction == null && !isRecentlyProcessed(transactionId)) {
getTransactionCache().put(transactionId, createTransaction(row));
metrics.setActiveTransactions(getTransactionCache().size());
}
else if (transaction != null && !isRecentlyProcessed(transactionId)) {
LOGGER.trace("Transaction {} is not yet committed and START event detected.", transactionId);
transaction.start();
}
}
/**
* Handle processing a LogMinerEventRow for a {@code COMMIT} event.
*
* @param row the result set row
* @throws InterruptedException if the event dispatcher was interrupted sending events
*/
protected void handleCommit(OraclePartition partition, LogMinerEventRow row) throws InterruptedException {
final String transactionId = row.getTransactionId();
if (isRecentlyProcessed(transactionId)) {
LOGGER.debug("\tTransaction is already committed, skipped.");
return;
}
final T transaction = getAndRemoveTransactionFromCache(transactionId);
if (transaction == null) {
LOGGER.trace("Transaction {} not found, commit skipped.", transactionId);
return;
}
// Calculate the smallest SCN that remains in the transaction cache
final Scn smallestScn = getTransactionCacheMinimumScn();
metrics.setOldestScn(smallestScn.isNull() ? Scn.valueOf(-1) : smallestScn);
final Scn commitScn = row.getScn();
if (offsetContext.getCommitScn().hasCommitAlreadyBeenHandled(row)) {
final Scn lastCommittedScn = offsetContext.getCommitScn().getCommitScnForRedoThread(row.getThread());
LOGGER.debug("Transaction {} has already been processed. "
+ "Offset Commit SCN {}, Transaction Commit SCN {}, Last Seen Commit SCN {}.",
transactionId, offsetContext.getCommitScn(), commitScn, lastCommittedScn);
removeTransactionAndEventsFromCache(transaction);
metrics.setActiveTransactions(getTransactionCache().size());
return;
}
counters.commitCount++;
int numEvents = getTransactionEventCount(transaction);
LOGGER.trace("Commit (smallest SCN {}) {}", smallestScn, row);
LOGGER.trace("Transaction {} has {} events", transactionId, numEvents);
final ZoneOffset databaseOffset = metrics.getDatabaseOffset();
final boolean skipExcludedUserName = isTransactionUserExcluded(transaction);
TransactionCommitConsumer.Handler<LogMinerEvent> delegate = new TransactionCommitConsumer.Handler<>() {
private int numEvents = getTransactionEventCount(transaction);
@Override
public void accept(LogMinerEvent event, long eventsProcessed) throws InterruptedException {
// Update SCN in offset context only if processed SCN less than SCN of other transactions
if (smallestScn.isNull() || commitScn.compareTo(smallestScn) < 0) {
offsetContext.setScn(event.getScn());
metrics.setOldestScn(event.getScn());
}
offsetContext.setEventScn(event.getScn());
offsetContext.setTransactionId(transactionId);
offsetContext.setUserName(transaction.getUserName());
offsetContext.setSourceTime(event.getChangeTime().minusSeconds(databaseOffset.getTotalSeconds()));
offsetContext.setTableId(event.getTableId());
offsetContext.setRedoThread(row.getThread());
if (eventsProcessed == numEvents) {
// reached the last event update the commit scn in the offsets
offsetContext.getCommitScn().recordCommit(row);
}
final DmlEvent dmlEvent = (DmlEvent) event;
if (!skipExcludedUserName) {
LogMinerChangeRecordEmitter logMinerChangeRecordEmitter;
if (dmlEvent instanceof TruncateEvent) {
// a truncate event is seen by logminer as a DDL event type.
// So force this here to be a Truncate Operation.
logMinerChangeRecordEmitter = new LogMinerChangeRecordEmitter(
connectorConfig,
partition,
offsetContext,
Envelope.Operation.TRUNCATE,
dmlEvent.getDmlEntry().getOldValues(),
dmlEvent.getDmlEntry().getNewValues(),
getSchema().tableFor(event.getTableId()),
getSchema(),
Clock.system());
}
else {
logMinerChangeRecordEmitter = new LogMinerChangeRecordEmitter(
connectorConfig,
partition,
offsetContext,
dmlEvent.getEventType(),
dmlEvent.getDmlEntry().getOldValues(),
dmlEvent.getDmlEntry().getNewValues(),
getSchema().tableFor(event.getTableId()),
getSchema(),
Clock.system());
}
dispatcher.dispatchDataChangeEvent(partition, event.getTableId(), logMinerChangeRecordEmitter);
}
}
};
Instant start = Instant.now();
int dispatchedEventCount = 0;
if (numEvents > 0) {
try (TransactionCommitConsumer commitConsumer = new TransactionCommitConsumer(delegate, connectorConfig, schema)) {
final Iterator<LogMinerEvent> iterator = getTransactionEventIterator(transaction);
while (iterator.hasNext()) {
if (!context.isRunning()) {
return;
}
final LogMinerEvent event = iterator.next();
LOGGER.trace("Dispatching event {} {}", ++dispatchedEventCount, event.getEventType());
commitConsumer.accept(event);
}
}
}
offsetContext.setEventScn(commitScn);
if (getTransactionEventCount(transaction) > 0 && !skipExcludedUserName) {
dispatcher.dispatchTransactionCommittedEvent(partition, offsetContext, transaction.getChangeTime());
}
else {
dispatcher.dispatchHeartbeatEvent(partition, offsetContext);
}
metrics.calculateLagMetrics(row.getChangeTime());
finalizeTransactionCommit(transactionId, commitScn);
removeTransactionAndEventsFromCache(transaction);
metrics.incrementCommittedTransactions();
metrics.setActiveTransactions(getTransactionCache().size());
metrics.incrementCommittedDmlCount(dispatchedEventCount);
metrics.setCommittedScn(commitScn);
metrics.setOffsetScn(offsetContext.getScn());
metrics.setLastCommitDuration(Duration.between(start, Instant.now()));
}
/**
* Gets a transaction instance from the transaction cache while also removing its cache entry.
*
* @param transactionId the transaction's unique identifier, should not be {@code null}
* @return the transaction instance if found, {@code null} if the transaction wasn't found
*/
protected abstract T getAndRemoveTransactionFromCache(String transactionId);
/**
* Removes the transaction and all its associated event entries from the connector's caches.
*
* @param transaction the transaction instance, should never be {@code null}
*/
protected abstract void removeTransactionAndEventsFromCache(T transaction);
/**
* Get an iterator over the events that are part of the specified transaction.
*
* @param transaction the transaction instance, should never be {@code null}
* @return an iterator over the transaction's events, never {@code null}
*/
protected abstract Iterator<LogMinerEvent> getTransactionEventIterator(T transaction);
/**
* Finalizes the commit of a transaction.
*
* @param transactionId the transaction's unique identifier, should not be {@code null}
* @param commitScn the transaction's system change number, should not be {@code null}
*/
protected abstract void finalizeTransactionCommit(String transactionId, Scn commitScn);
/**
* Check whether the supplied username associated with the specified transaction is excluded.
*
* @param transaction the transaction, never {@code null}
* @return true if the transaction should be skipped; false if transaction should be emitted
*/
protected boolean isTransactionUserExcluded(T transaction) {
if (transaction != null) {
if (transaction.getUserName() == null && getTransactionEventCount(transaction) > 0) {
LOGGER.debug("Detected transaction with null username {}", transaction);
return false;
}
else if (connectorConfig.getLogMiningUsernameExcludes().contains(transaction.getUserName())) {
LOGGER.trace("Skipped transaction with excluded username {}", transaction);
return true;
}
}
return false;
}
/**
* Handle processing a LogMinerEventRow for a {@code ROLLBACK} event.
*
* @param row the result set row
*/
protected void handleRollback(LogMinerEventRow row) {
if (getTransactionCache().containsKey(row.getTransactionId())) {
LOGGER.trace("Transaction {} was rolled back.", row.getTransactionId());
finalizeTransactionRollback(row.getTransactionId(), row.getScn());
metrics.setActiveTransactions(getTransactionCache().size());
metrics.incrementRolledBackTransactions();
metrics.addRolledBackTransactionId(row.getTransactionId());
counters.rollbackCount++;
}
else {
LOGGER.trace("Could not rollback transaction {}, was not found in cache.", row.getTransactionId());
}
}
/**
* Finalizes the rollback the specified transaction
*
* @param transactionId the unique transaction identifier, never {@code null}
* @param rollbackScn the rollback transaction's system change number, never {@code null}
*/
protected abstract void finalizeTransactionRollback(String transactionId, Scn rollbackScn);
/**
* Handle processing a LogMinerEventRow for a {@code DDL} event.
*
* @param row the result set row
* @throws InterruptedException if the event dispatcher is interrupted sending the event
*/
protected void handleSchemaChange(LogMinerEventRow row) throws InterruptedException {
if (hasSchemaChangeBeenSeen(row)) {
LOGGER.trace("DDL: Scn {}, SQL '{}' has already been processed, skipped.", row.getScn(), row.getRedoSql());
return;
}
if (offsetContext.getCommitScn().hasCommitAlreadyBeenHandled(row)) {
final Scn commitScn = offsetContext.getCommitScn().getCommitScnForRedoThread(row.getThread());
LOGGER.trace("DDL: SQL '{}' skipped with {} (SCN) <= {} (commit SCN for redo thread {})",
row.getRedoSql(), row.getScn(), commitScn, row.getThread());
return;
}
LOGGER.trace("DDL: '{}' {}", row.getRedoSql(), row);
if (row.getTableName() != null) {
counters.ddlCount++;
final TableId tableId = row.getTableId();
final int activeTransactions = getTransactionCache().size();
boolean advanceLowerScnBoundary = false;
if (activeTransactions == 0) {
// The DDL isn't wrapped in a transaction, fast-forward the lower boundary
advanceLowerScnBoundary = true;
}
else if (activeTransactions == 1) {
final String transactionId = getTransactionCache().keySet().iterator().next();
if (transactionId.equals(row.getTransactionId())) {
// The row's transaction is the current and only active transaction.
advanceLowerScnBoundary = true;
}
}
if (advanceLowerScnBoundary) {
LOGGER.debug("Schema change advanced offset SCN to {}", row.getScn());
offsetContext.setScn(row.getScn());
}
// Should always advance the commit SCN point with schema changes
LOGGER.debug("Schema change advanced offset commit SCN to {} for thread {}", row.getScn(), row.getThread());
offsetContext.getCommitScn().recordCommit(row);
offsetContext.setEventScn(row.getScn());
offsetContext.setRedoThread(row.getThread());
dispatcher.dispatchSchemaChangeEvent(partition,
tableId,
new OracleSchemaChangeEventEmitter(
getConfig(),
partition,
offsetContext,
tableId,
tableId.catalog(),
tableId.schema(),
row.getRedoSql(),
getSchema(),
row.getChangeTime(),
metrics,
() -> processTruncateEvent(row)));
}
}
private void processTruncateEvent(LogMinerEventRow row) {
LOGGER.debug("Handling truncate event");
addToTransaction(row.getTransactionId(), row, () -> {
final LogMinerDmlEntry dmlEntry = LogMinerDmlEntryImpl.forValuelessDdl();
dmlEntry.setObjectName(row.getTableName());
dmlEntry.setObjectOwner(row.getTablespaceName());
return new TruncateEvent(row, dmlEntry);
});
}
/**
* Handle processing a LogMinerEventRow for a {@code SEL_LOB_LOCATOR} event.
*
* @param row the result set row
*/
protected void handleSelectLobLocator(LogMinerEventRow row) {
if (!getConfig().isLobEnabled()) {
LOGGER.trace("LOB support is disabled, SEL_LOB_LOCATOR '{}' skipped.", row.getRedoSql());
return;
}
LOGGER.trace("SEL_LOB_LOCATOR: {}", row);
final TableId tableId = row.getTableId();
final Table table = getSchema().tableFor(tableId);
if (table == null) {
LOGGER.warn("SEL_LOB_LOCATOR for table '{}' is not known, skipped.", tableId);
return;
}
addToTransaction(row.getTransactionId(),
row,
() -> {
final LogMinerDmlEntry dmlEntry = selectLobParser.parse(row.getRedoSql(), table);
dmlEntry.setObjectName(row.getTableName());
dmlEntry.setObjectOwner(row.getTablespaceName());
return new SelectLobLocatorEvent(row,
dmlEntry,
selectLobParser.getColumnName(),
selectLobParser.isBinary());
});
metrics.incrementRegisteredDmlCount();
}
/**
* Handle processing a LogMinerEventRow for a {@code LOB_WRITE} event.
*
* @param row the result set row
*/
protected void handleLobWrite(LogMinerEventRow row) {
if (!getConfig().isLobEnabled()) {
LOGGER.trace("LOB support is disabled, LOB_WRITE scn={}, tableId={} skipped", row.getScn(), row.getTableId());
return;
}
LOGGER.trace("LOB_WRITE: scn={}, tableId={}, changeTime={}, transactionId={}",
row.getScn(), row.getTableId(), row.getChangeTime(), row.getTransactionId());
final TableId tableId = row.getTableId();
final Table table = getSchema().tableFor(tableId);
if (table == null) {
LOGGER.warn("LOB_WRITE for table '{}' is not known, skipped", tableId);
return;
}
if (row.getRedoSql() != null) {
addToTransaction(row.getTransactionId(), row, () -> {
final ParsedLobWriteSql parsed = parseLobWriteSql(row.getRedoSql());
return new LobWriteEvent(row, parsed.data, parsed.offset, parsed.length);
});
}
}
/**
* Handle processing a LogMinerEventRow for a {@code LOB_ERASE} event.
*
* @param row the result set row
*/
private void handleLobErase(LogMinerEventRow row) {
if (!getConfig().isLobEnabled()) {
LOGGER.trace("LOB support is disabled, LOB_ERASE '{}' skipped", row);
return;
}
LOGGER.trace("LOB_ERASE: {}", row);
final TableId tableId = row.getTableId();
final Table table = getSchema().tableFor(tableId);
if (table == null) {
LOGGER.warn("LOB_ERASE for table '{}' is not known, skipped", tableId);
return;
}
addToTransaction(row.getTransactionId(), row, () -> new LobEraseEvent(row));
}
/**
* Handle processing a LogMinerEventRow for a {@code INSERT}, {@code UPDATE}, or {@code DELETE} event.
*
* @param row the result set row
* @throws SQLException if a database exception occurs
* @throws InterruptedException if the dispatch of an event is interrupted
*/
protected void handleDataEvent(LogMinerEventRow row) throws SQLException, InterruptedException {
if (row.getRedoSql() == null) {
return;
}
LOGGER.trace("DML: {}", row);
LOGGER.trace("\t{}", row.getRedoSql());
// Oracle LogMiner reports LONG data types as STATUS=2 on UPDATE statements but there is no
// value in the INFO column, and the record can be managed by the connector successfully,
// so to be backward compatible, we only explicitly trigger this behavior if there is an
// error reason for STATUS=2 in the INFO column as well as STATUS=2.
if (row.getStatus() == 2 && !Strings.isNullOrBlank(row.getInfo())) {
// The SQL in the SQL_REDO column is not valid and cannot be parsed.
switch (connectorConfig.getEventProcessingFailureHandlingMode()) {
case FAIL:
LOGGER.error("Oracle LogMiner is unable to re-construct the SQL for '{}'", row);
throw new DebeziumException("Oracle failed to re-construct redo SQL '" + row.getRedoSql() + "'");
case WARN:
LOGGER.warn("Oracle LogMiner event '{}' cannot be parsed. This event will be ignored and skipped.", row);
return;
default:
// In this case, we explicitly log the situation in "debug" only and not as an error/warn.
LOGGER.debug("Oracle LogMiner event '{}' cannot be parsed. This event will be ignored and skipped.", row);
return;
}
}
counters.dmlCount++;
switch (row.getEventType()) {
case INSERT:
counters.insertCount++;
break;
case UPDATE:
counters.updateCount++;
break;
case DELETE:
counters.deleteCount++;
break;
}
final Table table = getTableForDataEvent(row);
if (table == null) {
return;
}
if (row.isRollbackFlag()) {
// There is a use case where a constraint violation will result in a DML event being
// written to the redo log subsequently followed by another DML event that is marked
// with a rollback flag to indicate that the prior event should be omitted. In this
// use case, the transaction can still be committed, so we need to manually rollback
// the previous DML event when this use case occurs.
removeEventWithRowId(row);
return;
}
addToTransaction(row.getTransactionId(), row, () -> {
final LogMinerDmlEntry dmlEntry = parseDmlStatement(row.getRedoSql(), table);
dmlEntry.setObjectName(row.getTableName());
dmlEntry.setObjectOwner(row.getTablespaceName());
return new DmlEvent(row, dmlEntry);
});
metrics.incrementRegisteredDmlCount();
}
protected void handleUnsupportedEvent(LogMinerEventRow row) {
if (!Strings.isNullOrEmpty(row.getTableName())) {
LOGGER.warn("An unsupported operation detected for table '{}' in transaction {} with SCN {} on redo thread {}.",
row.getTableId(),
row.getTransactionId(),
row.getScn(),
row.getThread());
}
}
/**
* Checks to see whether the offset's {@code scn} is remaining the same across multiple mining sessions
* while the offset's {@code commit_scn} is changing between sessions.
*
* @param previousOffsetScn the previous offset system change number
* @param previousOffsetCommitScns the previous offset commit system change number
*/
protected void warnPotentiallyStuckScn(Scn previousOffsetScn, Map<Integer, Scn> previousOffsetCommitScns) {
if (offsetContext != null && offsetContext.getCommitScn() != null) {
final Scn scn = offsetContext.getScn();
final Map<Integer, Scn> commitScns = offsetContext.getCommitScn().getCommitScnForAllRedoThreads();
if (previousOffsetScn.equals(scn) && !previousOffsetCommitScns.equals(commitScns)) {
counters.stuckCount++;
if (counters.stuckCount == 25) {
LOGGER.warn("Offset SCN {} has not changed in 25 mining session iterations. " +
"This indicates long running transaction(s) are active. Commit SCNs {}.",
previousOffsetScn,
previousOffsetCommitScns);
metrics.incrementScnFreezeCount();
}
}
else {
counters.stuckCount = 0;
}
}
}
private Table getTableForDataEvent(LogMinerEventRow row) throws SQLException, InterruptedException {
final TableId tableId = row.getTableId();
Table table = getSchema().tableFor(tableId);
if (table == null) {
if (!getConfig().getTableFilters().dataCollectionFilter().isIncluded(tableId)) {
return null;
}
table = dispatchSchemaChangeEventAndGetTableForNewCapturedTable(tableId, offsetContext, dispatcher);
}
return table;
}
/**
* Checks whether the result-set has any more data available.
* When a new row is available, the streaming metrics is updated with the fetch timings.
*
* @param resultSet the result set to check if any more rows exist
* @return true if another row exists, false otherwise
* @throws SQLException if there was a database exception
*/
private boolean hasNextWithMetricsUpdate(ResultSet resultSet) throws SQLException {
Instant start = Instant.now();
boolean result = false;
try {
if (resultSet.next()) {
metrics.addCurrentResultSetNext(Duration.between(start, Instant.now()));
result = true;
}
// Reset sequence unavailability on successful read from the result set
if (sequenceUnavailable) {
LOGGER.debug("The previous batch's unavailable log problem has been cleared.");
sequenceUnavailable = false;
}
}
catch (SQLException e) {
// Oracle's online redo logs can be defined with dynamic names using the instance
// configuration property LOG_ARCHIVE_FORMAT.
//
// Dynamically named online redo logs can lead to ORA-00310 errors if a log switch
// happens while the processor is iterating the LogMiner session's result set and
// LogMiner can no longer read the next batch of records from the log.
//
// LogMiner only validates that there are no gaps and that the logs are available
// when the session is first started and any change in the logs later will raise
// these types of errors.
//
// Catching the ORA-00310 and treating it as the end of the result set will allow
// the connector's outer loop to re-evaluate the log state and start a new LogMiner
// session with the new logs. The connector will then begin streaming from where
// it left off. If any other exception is caught here, it'll be thrown.
if (!e.getMessage().startsWith("ORA-00310")) {
// throw any non ORA-00310 error, old behavior
throw e;
}
else if (sequenceUnavailable) {
// If an ORA-00310 error was raised on the previous iteration and wasn't cleared
// after re-evaluation of the log availability and the mining session, we will
// explicitly stop the connector to avoid an infinite loop.
LOGGER.error("The log availability error '{}' wasn't cleared, stop requested.", e.getMessage());
throw e;
}
LOGGER.debug("A mined log is no longer available: {}", e.getMessage());
LOGGER.warn("Restarting mining session after a log became unavailable.");
// Track that we gracefully stopped due to a ORA-00310.
// Will be used to detect an infinite loop of this error across sequential iterations
sequenceUnavailable = true;
}
return result;
}
/**
* Add a transaction to the transaction map if allowed.
*
* @param transactionId the unqiue transaction id
* @param row the LogMiner event row
* @param eventSupplier the supplier of the event to create if the event is allowed to be added
*/
protected abstract void addToTransaction(String transactionId, LogMinerEventRow row, Supplier<LogMinerEvent> eventSupplier);
/**
* Dispatch a schema change event for a new table and get the newly created relational table model.
*
* @param tableId the unique table identifier, must not be {@code null}
* @param offsetContext the offset context
* @param dispatcher the event dispatcher
* @return the relational table model
* @throws SQLException if a database exception occurred
* @throws InterruptedException if the event dispatch was interrupted
*/
private Table dispatchSchemaChangeEventAndGetTableForNewCapturedTable(TableId tableId,
OracleOffsetContext offsetContext,
EventDispatcher<OraclePartition, TableId> dispatcher)
throws SQLException, InterruptedException {
final String tableDdl;
try {
tableDdl = getTableMetadataDdl(tableId);
}
catch (NonRelationalTableException e) {
LOGGER.warn("Table {} is not a relational table and will be skipped.", tableId);
metrics.incrementWarningCount();
return null;
}
LOGGER.info("Table '{}' is new and will now be captured.", tableId);
offsetContext.event(tableId, Instant.now());
dispatcher.dispatchSchemaChangeEvent(partition,
tableId,
new OracleSchemaChangeEventEmitter(connectorConfig,
partition,
offsetContext,
tableId,
tableId.catalog(),
tableId.schema(),
tableDdl,
getSchema(),
Instant.now(),
metrics,
null));
return getSchema().tableFor(tableId);
}
/**
* Get the specified table's create DDL statement.
*
* @param tableId the table identifier, must not be {@code null}
* @return the table's create DDL statement, never {@code null}
* @throws SQLException if an exception occurred obtaining the DDL statement
* @throws NonRelationalTableException if the table is not a relational table
*/
private String getTableMetadataDdl(TableId tableId) throws SQLException, NonRelationalTableException {
counters.tableMetadataCount++;
LOGGER.info("Getting database metadata for table '{}'", tableId);
// A separate connection must be used for this out-of-bands query while processing LogMiner results.
// This should have negligible overhead since this use case should happen rarely.
try (OracleConnection connection = new OracleConnection(connectorConfig.getJdbcConfig(), false)) {
connection.setAutoCommit(false);
final String pdbName = getConfig().getPdbName();
if (pdbName != null) {
connection.setSessionToPdb(pdbName);
}
return connection.getTableMetadataDdl(tableId);
}
}
/**
* Parse a DML redo SQL statement.
*
* @param redoSql the redo SQL statement
* @param table the table the SQL statement is for
* @return a parse object for the redo SQL statement
*/
private LogMinerDmlEntry parseDmlStatement(String redoSql, Table table) {
LogMinerDmlEntry dmlEntry;
try {
Instant parseStart = Instant.now();
dmlEntry = dmlParser.parse(redoSql, table);
metrics.addCurrentParseTime(Duration.between(parseStart, Instant.now()));
}
catch (DmlParserException e) {
String message = "DML statement couldn't be parsed." +
" Please open a Jira issue with the statement '" + redoSql + "'.";
throw new DmlParserException(message, e);
}
if (dmlEntry.getOldValues().length == 0) {
if (EventType.UPDATE == dmlEntry.getEventType() || EventType.DELETE == dmlEntry.getEventType()) {
LOGGER.warn("The DML event '{}' contained no before state.", redoSql);
metrics.incrementWarningCount();
}
}
return dmlEntry;
}
private static Pattern LOB_WRITE_SQL_PATTERN = Pattern.compile(
"(?s).* := ((?:HEXTORAW\\()?'.*'(?:\\))?);\\s*dbms_lob.write\\([^,]+,\\s*(\\d+)\\s*,\\s*(\\d+)\\s*,[^,]+\\);.*");
/**
* Parses a {@code LOB_WRITE} operation SQL fragment.
*