-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathApp.java
More file actions
338 lines (295 loc) · 12.1 KB
/
App.java
File metadata and controls
338 lines (295 loc) · 12.1 KB
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
package com.postgresintl.logicaldecoding;
import java.nio.ByteBuffer;
import java.sql.*;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import com.postgresintl.logicaldecoding.model.Attribute;
import com.postgresintl.logicaldecoding.model.Relation;
import org.postgresql.PGConnection;
import org.postgresql.PGProperty;
import org.postgresql.core.BaseConnection;
import org.postgresql.core.ServerVersion;
import org.postgresql.replication.LogSequenceNumber;
import org.postgresql.replication.PGReplicationStream;
/**
* Logical Decoding App
*
*/
public class App
{
private final static String SLOT_NAME="slot";
private final static String HOST="127.0.0.1";
private final static String PORT="5432";
private final static String DATABASE="test";
Connection connection;
Connection replicationConnection;
private static String toString(ByteBuffer buffer) {
byte cmd = buffer.get();
switch (cmd) {
case 'R':
Relation relation = new Relation();
relation.setOid(buffer.getInt());
relation.setSchema(getString(buffer));
relation.setName(getString(buffer));
byte replicaIdent = buffer.get();
short numAttrs = buffer.getShort();
for (int i = 0; i < numAttrs;i++){
byte replicaIdentityFull = buffer.get();
String attrName=getString(buffer);
int attrId = buffer.getInt();
int attrMode = buffer.getInt();
relation.addAttribute(new Attribute(attrId, attrName, attrMode, replicaIdentityFull));
}
return relation.toString();
case 'B':
LogSequenceNumber finalLSN = LogSequenceNumber.valueOf(buffer.getLong());
Timestamp commitTime = new Timestamp(buffer.getLong());
int transactionId = buffer.getInt();
return "BEGIN finalLSN: " + finalLSN.toString() + " Commit Time: " + commitTime + " XID: " + transactionId;
case 'C':
// COMMIT
byte unusedFlag = buffer.get();
LogSequenceNumber commitLSN = LogSequenceNumber.valueOf( buffer.getLong() );
LogSequenceNumber endLSN = LogSequenceNumber.valueOf( buffer.getLong() );
commitTime = new Timestamp(buffer.getLong());
return "COMMIT commitLSN:" + commitLSN.toString() + " endLSN:" + endLSN.toString() + " commitTime: " + commitTime;
case 'U': // UPDATE
case 'D': // DELETE
StringBuffer sb = new StringBuffer(cmd=='U'?"UPDATE: ":"DELETE: ");
int oid = buffer.getInt();
/*
this can be O or K if Delete or possibly N if UPDATE
K means key
O means old data
N means new data
*/
char keyOrTuple = (char)buffer.get();
getTuple(buffer, sb);
return sb.toString();
case 'I':
sb = new StringBuffer();
// oid of relation that is being inserted
oid = buffer.getInt();
// should be an N
char isNew = (char)buffer.get();
getTuple(buffer, sb);
return sb.toString();
}
return "";
}
private static void getTuple(ByteBuffer buffer, StringBuffer sb) {
short numAttrs;
numAttrs = buffer.getShort();
for (int i = 0; i < numAttrs; i++) {
byte c = buffer.get();
switch (c) {
case 'n': // null
sb.append("NULL, ");
break;
case 'u': // unchanged toast column
break;
case 't': // textual data
int strLen = buffer.getInt();
byte[] bytes = new byte[strLen];
buffer.get(bytes, 0, strLen);
String value = new String(bytes);
sb.append(value).append(", ");
break;
default:
sb.append("command: ").append((char) c);
}
}
}
private static String getString(ByteBuffer buffer){
StringBuffer sb = new StringBuffer();
while ( true ){
byte c = buffer.get();
if ( c == 0 ) {
break;
}
sb.append((char)c);
}
return sb.toString();
}
private String createUrl(){
return "jdbc:postgresql://"+HOST+':'+PORT+'/'+DATABASE;
}
public void createConnection()
{
try
{
connection = DriverManager.getConnection(createUrl(),"test","test");
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
public void dropPublication(String publication) throws SQLException {
try (PreparedStatement preparedStatement =
connection.prepareStatement("DROP PUBLICATION " + publication ) )
{
preparedStatement.execute();
}
}
public void createPublication(String publication) throws SQLException {
try (PreparedStatement preparedStatement =
connection.prepareStatement("CREATE PUBLICATION " + publication + " FOR ALL TABLES") )
{
preparedStatement.execute();
}
}
public void createLogicalReplicationSlot(String slotName, String outputPlugin ) throws InterruptedException, SQLException, TimeoutException
{
//drop previous slot
dropReplicationSlot(connection, slotName);
try (PreparedStatement preparedStatement =
connection.prepareStatement("SELECT * FROM pg_create_logical_replication_slot(?, ?)") )
{
preparedStatement.setString(1, slotName);
preparedStatement.setString(2, outputPlugin);
try (ResultSet rs = preparedStatement.executeQuery())
{
while (rs.next())
{
System.out.println("Slot Name: " + rs.getString(1));
System.out.println("Xlog Position: " + rs.getString(2));
}
}
}
}
public void dropReplicationSlot(Connection connection, String slotName)
throws SQLException, InterruptedException, TimeoutException
{
try (PreparedStatement preparedStatement = connection.prepareStatement(
"select pg_terminate_backend(active_pid) from pg_replication_slots "
+ "where active = true and slot_name = ?"))
{
preparedStatement.setString(1, slotName);
preparedStatement.execute();
}
waitStopReplicationSlot(connection, slotName);
try (PreparedStatement preparedStatement = connection.prepareStatement("select pg_drop_replication_slot(slot_name) "
+ "from pg_replication_slots where slot_name = ?")) {
preparedStatement.setString(1, slotName);
preparedStatement.execute();
}
}
public boolean isReplicationSlotActive(Connection connection, String slotName)
throws SQLException
{
try (PreparedStatement preparedStatement = connection.prepareStatement("select active from pg_replication_slots where slot_name = ?")){
preparedStatement.setString(1, slotName);
try (ResultSet rs = preparedStatement.executeQuery())
{
return rs.next() && rs.getBoolean(1);
}
}
}
private void waitStopReplicationSlot(Connection connection, String slotName)
throws InterruptedException, TimeoutException, SQLException
{
long startWaitTime = System.currentTimeMillis();
boolean stillActive;
long timeInWait = 0;
do {
stillActive = isReplicationSlotActive(connection, slotName);
if (stillActive) {
TimeUnit.MILLISECONDS.sleep(100L);
timeInWait = System.currentTimeMillis() - startWaitTime;
}
} while (stillActive && timeInWait <= 30000);
if (stillActive) {
throw new TimeoutException("Wait stop replication slot " + timeInWait + " timeout occurs");
}
}
public void receiveChangesOccursBeforStartReplication() throws Exception {
PGConnection pgConnection = (PGConnection) replicationConnection;
LogSequenceNumber lsn = getCurrentLSN();
Statement st = connection.createStatement();
st.execute("insert into test_logical_table(name) values('previous value')");
st.execute("insert into test_logical_table(name) values('previous value')");
st.execute("insert into test_logical_table(name) values('previous value')");
st.close();
PGReplicationStream stream =
pgConnection
.getReplicationAPI()
.replicationStream()
.logical()
.withSlotName(SLOT_NAME)
.withStartPosition(lsn)
.withSlotOption("proto_version",1)
.withSlotOption("publication_names", "pub1")
// .withSlotOption("include-xids", true)
// .withSlotOption("skip-empty-xacts", true)
.withStatusInterval(10, TimeUnit.SECONDS)
.start();
ByteBuffer buffer;
while(true)
{
buffer = stream.readPending();
if (buffer == null) {
TimeUnit.MILLISECONDS.sleep(10L);
continue;
}
System.out.println( toString(buffer));
//feedback
stream.setAppliedLSN(stream.getLastReceiveLSN());
stream.setFlushedLSN(stream.getLastReceiveLSN());
}
}
private LogSequenceNumber getCurrentLSN() throws SQLException
{
try (Statement st = connection.createStatement())
{
try (ResultSet rs = st.executeQuery("select "
+ (((BaseConnection) connection).haveMinimumServerVersion(ServerVersion.v10)
? "pg_current_wal_lsn()" : "pg_current_xlog_location()"))) {
if (rs.next()) {
String lsn = rs.getString(1);
return LogSequenceNumber.valueOf(lsn);
} else {
return LogSequenceNumber.INVALID_LSN;
}
}
}
}
private void openReplicationConnection() throws Exception {
Properties properties = new Properties();
properties.setProperty("user","test");
properties.setProperty("password","test");
PGProperty.ASSUME_MIN_SERVER_VERSION.set(properties, "9.4");
PGProperty.REPLICATION.set(properties, "database");
PGProperty.PREFER_QUERY_MODE.set(properties, "simple");
replicationConnection = DriverManager.getConnection(createUrl(),properties);
}
private boolean isServerCompatible() {
return ((BaseConnection)connection).haveMinimumServerVersion(ServerVersion.v9_5);
}
public static void main( String[] args )
{
String pluginName = "pgoutput";
App app = new App();
app.createConnection();
if (!app.isServerCompatible() ) {
System.err.println("must have server version greater than 9.4");
System.exit(-1);
}
try {
app.createLogicalReplicationSlot(SLOT_NAME, pluginName );
app.dropPublication("pub1");
app.createPublication("pub1");
app.openReplicationConnection();
app.receiveChangesOccursBeforStartReplication();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (TimeoutException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}