From: | "Yason TR" <yason(dot)tr(at)gmx(dot)com> |
---|---|
To: | "Achilleas Mantzios" <achill(at)matrix(dot)gatewaynet(dot)com> |
Cc: | pgsql-general(at)postgresql(dot)org |
Subject: | Re: JDBC: logical replication and LSN feedback |
Date: | 2017-09-19 14:53:28 |
Message-ID: | trinity-bae9a6bb-0dbf-4def-bdc4-d2bf3fa17160-1505832808354@3c-app-mailcom-bs15 |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general pgsql-jdbc |
<html><head></head><body><div style="font-family: Verdana;font-size: 12.0px;"><div>Should we read "In the event that replication has been restarted, it's will start from last successfully processed LSN that was sent via feedback to database." that this last succesfully event will be included (again) after a restart of the replication, or that the next event starting from the this last successfully event will be sent?</div>
<div> </div>
<div>I would expect the second, as this makes the most sense (because the consumers only want each event once), but I am not sure.</div>
<div> </div>
<div>Thanks a lot and kind regards,</div>
<div> </div>
<div>Yason TR</div>
<div>
<div name="quote" style="margin:10px 5px 5px 10px; padding: 10px 0 10px 10px; border-left:2px solid #C3D9E5; word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;">
<div style="margin:0 0 10px 0;"><b>Sent:</b> Tuesday, September 19, 2017 at 4:14 PM<br/>
<b>From:</b> "Achilleas Mantzios" <achill(at)matrix(dot)gatewaynet(dot)com><br/>
<b>To:</b> pgsql-general(at)postgresql(dot)org<br/>
<b>Subject:</b> Re: [GENERAL] JDBC: logical replication and LSN feedback</div>
<div name="quoted-content">On 19/09/2017 16:37, Yason TR wrote:<br/>
> Hi all,<br/>
><br/>
> I am developing an application which connects to a logical replication slot, to consume the WAL events. These WAL events are then forwarded to a MQ broker.<br/>
><br/>
> The heart of the code can be seen as:<br/>
><br/>
> while (true) {<br/>
> Connection connection = null;<br/>
> PGReplicationStream stream = null;<br/>
><br/>
> try {<br/>
> connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/db", properties);<br/>
> stream = connection.unwrap(PGConnection.class).getReplicationAPI().replicationStream().logical().withSlotName("slot").start();<br/>
><br/>
> while (true) {<br/>
> final ByteBuffer buffer = stream.read();<br/>
><br/>
> // ... MQ logic here ... omitted ...<br/>
><br/>
> stream.setAppliedLSN(stream.getLastReceiveLSN());<br/>
> stream.setFlushedLSN(stream.getLastReceiveLSN());<br/>
> }<br/>
> } catch (final SQLException e) {<br/>
> // ... log exception ... omitted ...<br/>
> } finally {<br/>
> // ... close stream and connection ... omitted ...<br/>
> }<br/>
> }<br/>
><br/>
> I notice some behavior which I cannot explain and would like to understand so I can alter my code:<br/>
><br/>
> - When I restart the application, I notice that the application is retrieving the last event from the previous run again. The result is that this event is sent twice to the MQ broker after a restart of the application. Why is that? Isn't calling `setAppliedLSN(stream.getLastReceiveLSN())` and/or `setFlushedLSN(stream.getLastReceiveLSN())` enough to acknowledge an event, so it will removed from the WAL log and it will not be resent?<br/>
><br/>
> - When receiving an event, the corresponding LSN from that event (which is sent in the payload) is not the same as the result of `stream.getLastReceivedLSN()`. Why is that? Which one should I use? Maybe this is correlated to my first question.<br/>
><br/>
> - What is the difference between `setAppliedLSN(LSN)` and `setFlushedLSN(LSN)`? The Javadocs are not really helpful here.<br/>
<br/>
The stages of a wal location generally go like : sent -> write -> flush -> replay , at least in terms of physical replication.<br/>
I guess applied=replayed ?<br/>
<br/>
Note that from the docs : <a href="https://jdbc.postgresql.org/documentation/head/replication.html#logical-replication" target="_blank">https://jdbc.postgresql.org/documentation/head/replication.html#logical-replication</a><br/>
it says :<br/>
"<br/>
In the event that replication has been restarted, it's will start from last successfully processed LSN that was sent via feedback to database.<br/>
"<br/>
<br/>
><br/>
> FYI, I also asked this question on <a href="https://stackoverflow.com/questions/46301578/postgres-jdbc-logical-replication-lsn-feedback" target="_blank">https://stackoverflow.com/questions/46301578/postgres-jdbc-logical-replication-lsn-feedback</a>.<br/>
><br/>
> Thanks a lot and kind regards,<br/>
><br/>
> Yason TR<br/>
><br/>
><br/>
<br/>
--<br/>
Achilleas Mantzios<br/>
IT DEV Lead<br/>
IT DEPT<br/>
Dynacom Tankers Mgmt<br/>
<br/>
<br/>
<br/>
--<br/>
Sent via pgsql-general mailing list (pgsql-general(at)postgresql(dot)org)<br/>
To make changes to your subscription:<br/>
<a href="http://www.postgresql.org/mailpref/pgsql-general" target="_blank">http://www.postgresql.org/mailpref/pgsql-general</a></div>
</div>
</div></div></body></html>
Attachment | Content-Type | Size |
---|---|---|
unknown_filename | text/html | 4.7 KB |
From | Date | Subject | |
---|---|---|---|
Next Message | Alban Hertroys | 2017-09-19 16:09:33 | Re: Insert large number of records |
Previous Message | Achilleas Mantzios | 2017-09-19 14:14:16 | Re: JDBC: logical replication and LSN feedback |
From | Date | Subject | |
---|---|---|---|
Next Message | Jorge Solorzano | 2017-09-19 15:36:52 | [pgjdbc/pgjdbc] 646a86: chore: use mainly Trusty in Travis, reorder CI job... |
Previous Message | Achilleas Mantzios | 2017-09-19 14:14:16 | Re: JDBC: logical replication and LSN feedback |