-
Notifications
You must be signed in to change notification settings - Fork 1.6k
/
PostgresReplicationConnection.java
921 lines (834 loc) · 40.2 KB
/
PostgresReplicationConnection.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
/*
* 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.postgresql.connection;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.kafka.connect.errors.ConnectException;
import org.postgresql.core.BaseConnection;
import org.postgresql.core.ServerVersion;
import org.postgresql.replication.PGReplicationStream;
import org.postgresql.replication.fluent.logical.ChainedLogicalStreamBuilder;
import org.postgresql.util.PSQLException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.debezium.DebeziumException;
import io.debezium.config.Configuration;
import io.debezium.connector.postgresql.PostgresConnectorConfig;
import io.debezium.connector.postgresql.PostgresSchema;
import io.debezium.connector.postgresql.TypeRegistry;
import io.debezium.connector.postgresql.spi.SlotCreationResult;
import io.debezium.jdbc.JdbcConnection;
import io.debezium.jdbc.JdbcConnectionException;
import io.debezium.relational.RelationalTableFilters;
import io.debezium.relational.TableId;
import io.debezium.util.Clock;
import io.debezium.util.Metronome;
import java.nio.ByteBuffer;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Statement;
import java.time.Duration;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Function;
import java.util.stream.Collectors;
import static java.lang.Math.toIntExact;
/**
* Implementation of a {@link ReplicationConnection} for Postgresql. Note that replication
* connections in PG cannot execute regular statements but only a limited number of
* replication-related commands.
*
* @author Horia Chiorean (hchiorea@redhat.com)
*/
public class PostgresReplicationConnection extends JdbcConnection implements ReplicationConnection {
private static final String TABLE_INCLUDE_LIST = "table.include.list";
private static Logger LOGGER = LoggerFactory.getLogger(PostgresReplicationConnection.class);
private final String slotName;
private final String publicationName;
private final RelationalTableFilters tableFilter;
private final PostgresConnectorConfig.AutoCreateMode publicationAutocreateMode;
private final PostgresConnectorConfig.LogicalDecoder plugin;
private final boolean dropSlotOnClose;
private final PostgresConnectorConfig originalConfig;
private final Duration statusUpdateInterval;
private final MessageDecoder messageDecoder;
private final TypeRegistry typeRegistry;
private final Properties streamParams;
private Lsn defaultStartingPos;
private SlotCreationResult slotCreationInfo;
private boolean hasInitedSlot;
/**
* Creates a new replication connection with the given params.
*
* @param config the JDBC configuration for the connection; may not be null
* @param slotName the name of the DB slot for logical replication; may not be null
* @param publicationName the name of the DB publication for logical replication; may not be
* null
* @param tableFilter the tables to watch of the DB publication for logical replication; may not
* be null
* @param publicationAutocreateMode the mode for publication autocreation; may not be null
* @param plugin decoder matching the server side plug-in used for streaming changes; may not be
* null
* @param dropSlotOnClose whether the replication slot should be dropped once the connection is
* closed
* @param statusUpdateInterval the interval at which the replication connection should
* periodically send status
* @param doSnapshot whether the connector is doing snapshot
* @param typeRegistry registry with PostgreSQL types
* @param streamParams additional parameters to pass to the replication stream
* @param schema the schema; must not be null
* <p>updates to the server
*/
private PostgresReplicationConnection(
PostgresConnectorConfig config,
String slotName,
String publicationName,
RelationalTableFilters tableFilter,
PostgresConnectorConfig.AutoCreateMode publicationAutocreateMode,
PostgresConnectorConfig.LogicalDecoder plugin,
boolean dropSlotOnClose,
boolean doSnapshot,
Duration statusUpdateInterval,
TypeRegistry typeRegistry,
Properties streamParams,
PostgresSchema schema) {
super(
config.getJdbcConfig(),
PostgresConnection.FACTORY,
null,
PostgresReplicationConnection::defaultSettings);
this.originalConfig = config;
this.slotName = slotName;
this.publicationName = publicationName;
this.tableFilter = tableFilter;
this.publicationAutocreateMode = publicationAutocreateMode;
this.plugin = plugin;
this.dropSlotOnClose = dropSlotOnClose;
this.statusUpdateInterval = statusUpdateInterval;
this.messageDecoder = plugin.messageDecoder(new MessageDecoderContext(config, schema));
this.typeRegistry = typeRegistry;
this.streamParams = streamParams;
this.slotCreationInfo = null;
this.hasInitedSlot = false;
}
private ServerInfo.ReplicationSlot getSlotInfo() throws SQLException, InterruptedException {
try (PostgresConnection connection =
new PostgresConnection(originalConfig.getJdbcConfig())) {
return connection.readReplicationSlotInfo(slotName, plugin.getPostgresPluginName());
}
}
protected void initPublication() {
String createPublicationStmt;
String tableFilterString = null;
String tableListStr = this.originalConfig.getConfig().asMap().get(TABLE_INCLUDE_LIST);
final List<String> configTableList =
new ArrayList<>(Arrays.asList(tableListStr.split(",")));
configTableList.removeAll(this.getPublicationExistedTableList());
if (PostgresConnectorConfig.LogicalDecoder.PGOUTPUT.equals(plugin)) {
LOGGER.info("Initializing PgOutput logical decoder publication");
try {
String selectPublication =
String.format(
"SELECT COUNT(1) FROM pg_publication WHERE pubname = '%s'",
publicationName);
try (Statement stmt = pgConnection().createStatement();
ResultSet rs = stmt.executeQuery(selectPublication)) {
if (rs.next()) {
Long count = rs.getLong(1);
if (count == 0L) {
LOGGER.info(
"Creating new publication '{}' for plugin '{}'",
publicationName,
plugin);
switch (publicationAutocreateMode) {
case DISABLED:
throw new ConnectException(
"Publication autocreation is disabled, please create one and restart the connector.");
case ALL_TABLES:
createPublicationStmt =
String.format(
"CREATE PUBLICATION %s FOR ALL TABLES;",
publicationName);
LOGGER.info(
"Creating Publication with statement '{}'",
createPublicationStmt);
// Publication doesn't exist, create it.
stmt.execute(createPublicationStmt);
break;
case FILTERED:
try {
Set<TableId> tablesToCapture =
determineCapturedTables(configTableList);
tableFilterString =
tablesToCapture.stream()
.map(TableId::toDoubleQuotedString)
.collect(Collectors.joining(", "));
if (tableFilterString.isEmpty()) {
throw new DebeziumException(
String.format(
"No table filters found for filtered publication %s",
publicationName));
}
createPublicationStmt =
String.format(
"CREATE PUBLICATION %s FOR TABLE %s;",
publicationName, tableFilterString);
LOGGER.info(
"Creating Publication with statement '{}'",
createPublicationStmt);
// Publication doesn't exist, create it but restrict to the
// tableFilter.
stmt.execute(createPublicationStmt);
} catch (Exception e) {
throw new ConnectException(
String.format(
"Unable to create filtered publication %s for %s",
publicationName, tableFilterString),
e);
}
break;
}
} else {
if (CollectionUtils.isNotEmpty(configTableList)) {
switch (publicationAutocreateMode) {
case DISABLED:
throw new ConnectException(
"Publication autocreation is disabled, please create one and restart the connector.");
case ALL_TABLES:
break;
case FILTERED:
try {
Set<TableId> tablesToCapture =
determineCapturedTables(configTableList);
tableFilterString =
tablesToCapture.stream()
.map(TableId::toDoubleQuotedString)
.collect(Collectors.joining(", "));
if (tableFilterString.isEmpty()) {
throw new DebeziumException(
String.format(
"No table filters found for filtered publication %s",
publicationName));
}
createPublicationStmt =
String.format(
"ALTER PUBLICATION %s ADD TABLE %s;",
publicationName, tableFilterString);
LOGGER.info(
"ALTER PUBLICATION with statement '{}'",
createPublicationStmt);
// Publication doesn't exist, create it but restrict to
// the tableFilter.
stmt.execute(createPublicationStmt);
} catch (Exception e) {
throw new ConnectException(
String.format(
"Unable to create filtered publication %s for %s",
publicationName, tableFilterString),
e);
}
break;
}
}
}
}
}
} catch (SQLException e) {
throw new JdbcConnectionException(e);
}
// This is what ties the publication definition to the replication stream
streamParams.put("proto_version", 1);
streamParams.put("publication_names", publicationName);
}
}
private List<String> getPublicationExistedTableList() {
List<String> tableList = new ArrayList<>();
try {
String selectPublication =
String.format(
"SELECT * FROM pg_publication_tables WHERE pubname = '%s'; ",
publicationName);
try (Statement stmt = pgConnection().createStatement();
ResultSet rs = stmt.executeQuery(selectPublication)) {
while (rs.next()) {
String schemaName = rs.getString("schemaname");
String tableName = rs.getString("tablename");
tableList.add(schemaName + "." + tableName);
}
}
} catch (SQLException e) {
throw new JdbcConnectionException(e);
}
return tableList;
}
private Set<TableId> determineCapturedTables(List<String> tableList) {
Set<TableId> tableIdSet = new HashSet<>();
if (CollectionUtils.isEmpty(tableList)) {
return tableIdSet;
}
tableList.forEach(
tableNameStr -> {
String schemaName = tableNameStr.split("\\.")[0];
String tableName = tableNameStr.split("\\.")[1];
tableIdSet.add(new TableId("", schemaName, tableName));
});
return tableIdSet;
}
protected void initReplicationSlot() throws SQLException, InterruptedException {
ServerInfo.ReplicationSlot slotInfo = getSlotInfo();
boolean shouldCreateSlot = ServerInfo.ReplicationSlot.INVALID == slotInfo;
try {
// there's no info for this plugin and slot so create a new slot
if (shouldCreateSlot) {
this.createReplicationSlot();
}
// replication connection does not support parsing of SQL statements so we need to
// create
// the connection without executing on connect statements - see JDBC opt
// preferQueryMode=simple
pgConnection();
final String identifySystemStatement = "IDENTIFY_SYSTEM";
LOGGER.debug(
"running '{}' to validate replication connection", identifySystemStatement);
final Lsn xlogStart =
queryAndMap(
identifySystemStatement,
rs -> {
if (!rs.next()) {
throw new IllegalStateException(
"The DB connection is not a valid replication connection");
}
String xlogpos = rs.getString("xlogpos");
LOGGER.debug("received latest xlogpos '{}'", xlogpos);
return Lsn.valueOf(xlogpos);
});
if (slotCreationInfo != null) {
this.defaultStartingPos = slotCreationInfo.startLsn();
} else if (shouldCreateSlot || !slotInfo.hasValidFlushedLsn()) {
// this is a new slot or we weren't able to read a valid flush LSN pos, so we always
// start from the xlog pos that was reported
this.defaultStartingPos = xlogStart;
} else {
Lsn latestFlushedLsn = slotInfo.latestFlushedLsn();
this.defaultStartingPos =
latestFlushedLsn.compareTo(xlogStart) < 0 ? latestFlushedLsn : xlogStart;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("found previous flushed LSN '{}'", latestFlushedLsn);
}
}
hasInitedSlot = true;
} catch (SQLException e) {
throw new JdbcConnectionException(e);
}
}
// Temporary replication slots is a new feature of PostgreSQL 10
private boolean useTemporarySlot() throws SQLException {
// Temporary replication slots cannot be used due to connection restart
// when finding WAL position
// return dropSlotOnClose && pgConnection().haveMinimumServerVersion(ServerVersion.v10);
return false;
}
/**
* creating a replication connection and starting to stream involves a few steps: 1. we create
* the connection and ensure that a. the slot exists b. the slot isn't currently being used 2.
* we query to get our potential start position in the slot (lsn) 3. we try and start streaming,
* depending on our options (such as in wal2json) this may fail, which can result in the
* connection being killed and we need to start the process over if we are using a temporary
* slot 4. actually start the streamer
*
* <p>This method takes care of all of these and this method queries for a default starting
* position If you know where you are starting from you should call {@link #startStreaming(Lsn,
* WalPositionLocator)}, this method delegates to that method
*
* @return
* @throws SQLException
* @throws InterruptedException
*/
@Override
public ReplicationStream startStreaming(WalPositionLocator walPosition)
throws SQLException, InterruptedException {
return startStreaming(null, walPosition);
}
@Override
public ReplicationStream startStreaming(Lsn offset, WalPositionLocator walPosition)
throws SQLException, InterruptedException {
initConnection();
connect();
if (offset == null || !offset.isValid()) {
offset = defaultStartingPos;
}
Lsn lsn = offset;
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("starting streaming from LSN '{}'", lsn);
}
int maxRetries = config().getInteger(PostgresConnectorConfig.MAX_RETRIES);
int delay = config().getInteger(PostgresConnectorConfig.RETRY_DELAY_MS);
int tryCount = 0;
while (true) {
try {
return createReplicationStream(lsn, walPosition);
} catch (Exception e) {
String message = "Failed to start replication stream at " + lsn;
if (++tryCount > maxRetries) {
if (e.getMessage().matches(".*replication slot .* is active.*")) {
message +=
"; when setting up multiple connectors for the same database host, please make sure to use a distinct replication slot name for each.";
}
throw new DebeziumException(message, e);
} else {
LOGGER.warn(
message + ", waiting for {} ms and retrying, attempt number {} over {}",
delay,
tryCount,
maxRetries);
final Metronome metronome =
Metronome.sleeper(Duration.ofMillis(delay), Clock.SYSTEM);
metronome.pause();
}
}
}
}
@Override
public void initConnection() throws SQLException, InterruptedException {
// See https://www.postgresql.org/docs/current/logical-replication-quick-setup.html
// For pgoutput specifically, the publication must be created before the slot.
initPublication();
if (!hasInitedSlot) {
initReplicationSlot();
}
}
@Override
public Optional<SlotCreationResult> createReplicationSlot() throws SQLException {
// note that some of these options are only supported in Postgres 9.4+, additionally
// the options are not yet exported by the jdbc api wrapper, therefore, we just do
// this ourselves but eventually this should be moved back to the jdbc API
// see https://github.com/pgjdbc/pgjdbc/issues/1305
LOGGER.debug("Creating new replication slot '{}' for plugin '{}'", slotName, plugin);
String tempPart = "";
// Exported snapshots are supported in Postgres 9.4+
boolean canExportSnapshot = pgConnection().haveMinimumServerVersion(ServerVersion.v9_4);
if ((dropSlotOnClose) && !canExportSnapshot) {
LOGGER.warn(
"A slot marked as temporary or with an exported snapshot was created, "
+ "but not on a supported version of Postgres, ignoring!");
}
if (useTemporarySlot()) {
tempPart = "TEMPORARY";
}
// See https://www.postgresql.org/docs/current/logical-replication-quick-setup.html
// For pgoutput specifically, the publication must be created prior to the slot.
initPublication();
try (Statement stmt = pgConnection().createStatement()) {
String createCommand =
String.format(
"CREATE_REPLICATION_SLOT %s %s LOGICAL %s",
slotName, tempPart, plugin.getPostgresPluginName());
LOGGER.info("Creating replication slot with command {}", createCommand);
stmt.execute(createCommand);
// when we are in Postgres 9.4+, we can parse the slot creation info,
// otherwise, it returns nothing
if (canExportSnapshot) {
this.slotCreationInfo = parseSlotCreation(stmt.getResultSet());
}
return Optional.ofNullable(slotCreationInfo);
}
}
protected BaseConnection pgConnection() throws SQLException {
return (BaseConnection) connection(false);
}
private SlotCreationResult parseSlotCreation(ResultSet rs) {
try {
if (rs.next()) {
String slotName = rs.getString("slot_name");
String startPoint = rs.getString("consistent_point");
String snapName = rs.getString("snapshot_name");
String pluginName = rs.getString("output_plugin");
return new SlotCreationResult(slotName, startPoint, snapName, pluginName);
} else {
throw new ConnectException("No replication slot found");
}
} catch (SQLException ex) {
throw new ConnectException("Unable to parse create_replication_slot response", ex);
}
}
private ReplicationStream createReplicationStream(
final Lsn startLsn, WalPositionLocator walPosition)
throws SQLException, InterruptedException {
PGReplicationStream s;
try {
try {
s =
startPgReplicationStream(
startLsn,
plugin.forceRds()
? messageDecoder::optionsWithoutMetadata
: messageDecoder::optionsWithMetadata);
messageDecoder.setContainsMetadata(plugin.forceRds() ? false : true);
} catch (PSQLException e) {
LOGGER.debug(
"Could not register for streaming, retrying without optional options", e);
// re-init the slot after a failed start of slot, as this
// may have closed the slot
if (useTemporarySlot()) {
initReplicationSlot();
}
s =
startPgReplicationStream(
startLsn,
plugin.forceRds()
? messageDecoder::optionsWithoutMetadata
: messageDecoder::optionsWithMetadata);
messageDecoder.setContainsMetadata(plugin.forceRds() ? false : true);
}
} catch (PSQLException e) {
if (e.getMessage().matches("(?s)ERROR: option .* is unknown.*")) {
// It is possible we are connecting to an old wal2json plug-in
LOGGER.warn(
"Could not register for streaming with metadata in messages, falling back to messages without metadata");
// re-init the slot after a failed start of slot, as this
// may have closed the slot
if (useTemporarySlot()) {
initReplicationSlot();
}
s = startPgReplicationStream(startLsn, messageDecoder::optionsWithoutMetadata);
messageDecoder.setContainsMetadata(false);
} else if (e.getMessage()
.matches("(?s)ERROR: requested WAL segment .* has already been removed.*")) {
LOGGER.error("Cannot rewind to last processed WAL position", e);
throw new ConnectException(
"The offset to start reading from has been removed from the database write-ahead log. Create a new snapshot and consider setting of PostgreSQL parameter wal_keep_segments = 0.");
} else {
throw e;
}
}
final PGReplicationStream stream = s;
return new ReplicationStream() {
private static final int CHECK_WARNINGS_AFTER_COUNT = 100;
private int warningCheckCounter = CHECK_WARNINGS_AFTER_COUNT;
private ExecutorService keepAliveExecutor = null;
private AtomicBoolean keepAliveRunning;
private final Metronome metronome =
Metronome.sleeper(statusUpdateInterval, Clock.SYSTEM);
// make sure this is volatile since multiple threads may be interested in this value
private volatile Lsn lastReceivedLsn;
@Override
public void read(ReplicationMessageProcessor processor)
throws SQLException, InterruptedException {
processWarnings(false);
ByteBuffer read = stream.read();
final Lsn lastReceiveLsn = Lsn.valueOf(stream.getLastReceiveLSN());
LOGGER.trace(
"Streaming requested from LSN {}, received LSN {}",
startLsn,
lastReceiveLsn);
if (messageDecoder.shouldMessageBeSkipped(
read, lastReceiveLsn, startLsn, walPosition)) {
return;
}
deserializeMessages(read, processor);
}
@Override
public boolean readPending(ReplicationMessageProcessor processor)
throws SQLException, InterruptedException {
processWarnings(false);
ByteBuffer read = stream.readPending();
final Lsn lastReceiveLsn = Lsn.valueOf(stream.getLastReceiveLSN());
LOGGER.trace(
"Streaming requested from LSN {}, received LSN {}",
startLsn,
lastReceiveLsn);
if (read == null) {
return false;
}
if (messageDecoder.shouldMessageBeSkipped(
read, lastReceiveLsn, startLsn, walPosition)) {
return true;
}
deserializeMessages(read, processor);
return true;
}
private void deserializeMessages(
ByteBuffer buffer, ReplicationMessageProcessor processor)
throws SQLException, InterruptedException {
lastReceivedLsn = Lsn.valueOf(stream.getLastReceiveLSN());
LOGGER.trace("Received message at LSN {}", lastReceivedLsn);
messageDecoder.processMessage(buffer, processor, typeRegistry);
}
@Override
public void close() throws SQLException {
processWarnings(true);
stream.close();
}
@Override
public void flushLsn(Lsn lsn) throws SQLException {
doFlushLsn(lsn);
}
private void doFlushLsn(Lsn lsn) throws SQLException {
stream.setFlushedLSN(lsn.asLogSequenceNumber());
stream.setAppliedLSN(lsn.asLogSequenceNumber());
stream.forceUpdateStatus();
}
@Override
public Lsn lastReceivedLsn() {
return lastReceivedLsn;
}
@Override
public void startKeepAlive(ExecutorService service) {
if (keepAliveExecutor == null) {
keepAliveExecutor = service;
keepAliveRunning = new AtomicBoolean(true);
keepAliveExecutor.submit(
() -> {
while (keepAliveRunning.get()) {
try {
LOGGER.trace(
"Forcing status update with replication stream");
stream.forceUpdateStatus();
metronome.pause();
} catch (Exception exp) {
throw new RuntimeException(
"received unexpected exception will perform keep alive",
exp);
}
}
});
}
}
@Override
public void stopKeepAlive() {
if (keepAliveExecutor != null) {
keepAliveRunning.set(false);
keepAliveExecutor.shutdownNow();
keepAliveExecutor = null;
}
}
private void processWarnings(final boolean forced) throws SQLException {
if (--warningCheckCounter == 0 || forced) {
warningCheckCounter = CHECK_WARNINGS_AFTER_COUNT;
for (SQLWarning w = connection().getWarnings();
w != null;
w = w.getNextWarning()) {
LOGGER.debug(
"Server-side message: '{}', state = {}, code = {}",
w.getMessage(),
w.getSQLState(),
w.getErrorCode());
}
connection().clearWarnings();
}
}
@Override
public Lsn startLsn() {
return startLsn;
}
};
}
private PGReplicationStream startPgReplicationStream(
final Lsn lsn,
Function<ChainedLogicalStreamBuilder, ChainedLogicalStreamBuilder> configurator)
throws SQLException {
assert lsn != null;
ChainedLogicalStreamBuilder streamBuilder =
pgConnection()
.getReplicationAPI()
.replicationStream()
.logical()
.withSlotName(slotName)
.withStartPosition(lsn.asLogSequenceNumber())
.withSlotOptions(streamParams);
streamBuilder = configurator.apply(streamBuilder);
if (statusUpdateInterval != null && statusUpdateInterval.toMillis() > 0) {
streamBuilder.withStatusInterval(
toIntExact(statusUpdateInterval.toMillis()), TimeUnit.MILLISECONDS);
}
PGReplicationStream stream = streamBuilder.start();
// TODO DBZ-508 get rid of this
// Needed by tests when connections are opened and closed in a fast sequence
try {
Thread.sleep(10);
} catch (Exception e) {
}
stream.forceUpdateStatus();
return stream;
}
@Override
public synchronized void close() {
close(true);
}
public synchronized void close(boolean dropSlot) {
try {
LOGGER.debug("Closing message decoder");
messageDecoder.close();
} catch (Throwable e) {
LOGGER.error("Unexpected error while closing message decoder", e);
}
try {
LOGGER.debug("Closing replication connection");
super.close();
} catch (Throwable e) {
LOGGER.error("Unexpected error while closing Postgres connection", e);
}
if (dropSlotOnClose && dropSlot) {
// we're dropping the replication slot via a regular - i.e. not a replication -
// connection
try (PostgresConnection connection =
new PostgresConnection(originalConfig.getJdbcConfig())) {
connection.dropReplicationSlot(slotName);
} catch (Throwable e) {
LOGGER.error("Unexpected error while dropping replication slot", e);
}
}
}
@Override
public void reconnect() throws SQLException {
close(false);
// Don't re-execute initial commands on reconnection
connection(false);
}
protected static void defaultSettings(Configuration.Builder builder) {
// first copy the parent's default settings...
PostgresConnection.defaultSettings(builder);
// then set some additional replication specific settings
builder.with("replication", "database")
.with(
"preferQueryMode",
"simple"); // replication protocol only supports simple query mode
}
protected static class ReplicationConnectionBuilder implements Builder {
private final PostgresConnectorConfig config;
private String slotName = DEFAULT_SLOT_NAME;
private String publicationName = DEFAULT_PUBLICATION_NAME;
private RelationalTableFilters tableFilter;
private PostgresConnectorConfig.AutoCreateMode publicationAutocreateMode =
PostgresConnectorConfig.AutoCreateMode.FILTERED;
private PostgresConnectorConfig.LogicalDecoder plugin =
PostgresConnectorConfig.LogicalDecoder.DECODERBUFS;
private PostgresConnectorConfig.TruncateHandlingMode truncateHandlingMode;
private boolean dropSlotOnClose = DEFAULT_DROP_SLOT_ON_CLOSE;
private Duration statusUpdateIntervalVal;
private boolean doSnapshot;
private TypeRegistry typeRegistry;
private PostgresSchema schema;
private Properties slotStreamParams = new Properties();
protected ReplicationConnectionBuilder(PostgresConnectorConfig config) {
assert config != null;
this.config = config;
}
@Override
public ReplicationConnectionBuilder withSlot(final String slotName) {
assert slotName != null;
this.slotName = slotName;
return this;
}
public ReplicationConnectionBuilder withC(final String slotName) {
assert slotName != null;
this.slotName = slotName;
return this;
}
@Override
public Builder withPublication(String publicationName) {
assert publicationName != null;
this.publicationName = publicationName;
return this;
}
@Override
public Builder withTableFilter(RelationalTableFilters tableFilter) {
assert tableFilter != null;
this.tableFilter = tableFilter;
return this;
}
@Override
public Builder withPublicationAutocreateMode(
PostgresConnectorConfig.AutoCreateMode publicationAutocreateMode) {
assert publicationName != null;
this.publicationAutocreateMode = publicationAutocreateMode;
return this;
}
@Override
public ReplicationConnectionBuilder withPlugin(
final PostgresConnectorConfig.LogicalDecoder plugin) {
assert plugin != null;
this.plugin = plugin;
return this;
}
@Override
public Builder withTruncateHandlingMode(
PostgresConnectorConfig.TruncateHandlingMode truncateHandlingMode) {
assert truncateHandlingMode != null;
this.truncateHandlingMode = truncateHandlingMode;
return this;
}
@Override
public ReplicationConnectionBuilder dropSlotOnClose(final boolean dropSlotOnClose) {
this.dropSlotOnClose = dropSlotOnClose;
return this;
}
@Override
public ReplicationConnectionBuilder streamParams(final String slotStreamParams) {
if (slotStreamParams != null && !slotStreamParams.isEmpty()) {
this.slotStreamParams = new Properties();
String[] paramsWithValues = slotStreamParams.split(";");
for (String paramsWithValue : paramsWithValues) {
String[] paramAndValue = paramsWithValue.split("=");
if (paramAndValue.length == 2) {
this.slotStreamParams.setProperty(paramAndValue[0], paramAndValue[1]);
} else {
LOGGER.warn(
"The following STREAM_PARAMS value is invalid: {}",
paramsWithValue);
}
}
}
return this;
}
@Override
public ReplicationConnectionBuilder statusUpdateInterval(
final Duration statusUpdateInterval) {
this.statusUpdateIntervalVal = statusUpdateInterval;
return this;
}
@Override
public Builder doSnapshot(boolean doSnapshot) {
this.doSnapshot = doSnapshot;
return this;
}
@Override
public ReplicationConnection build() {
assert plugin != null : "Decoding plugin name is not set";
return new PostgresReplicationConnection(
config,
slotName,
publicationName,
tableFilter,
publicationAutocreateMode,
plugin,
dropSlotOnClose,
doSnapshot,
statusUpdateIntervalVal,
typeRegistry,
slotStreamParams,
schema);
}
@Override
public Builder withTypeRegistry(TypeRegistry typeRegistry) {
this.typeRegistry = typeRegistry;
return this;
}
@Override
public Builder withSchema(PostgresSchema schema) {
this.schema = schema;
return this;
}
}
}