Re: JDBC: logical replication and LSN feedback

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 &quot;In the event that replication has been restarted, it&#39;s will start from last successfully processed LSN that was sent via feedback to database.&quot; 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>&nbsp;</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>&nbsp;</div>

<div>Thanks a lot and kind regards,</div>

<div>&nbsp;</div>

<div>Yason TR</div>

<div>&nbsp;
<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>&nbsp;Tuesday, September 19, 2017 at 4:14 PM<br/>
<b>From:</b>&nbsp;&quot;Achilleas Mantzios&quot; &lt;achill(at)matrix(dot)gatewaynet(dot)com&gt;<br/>
<b>To:</b>&nbsp;pgsql-general(at)postgresql(dot)org<br/>
<b>Subject:</b>&nbsp;Re: [GENERAL] JDBC: logical replication and LSN feedback</div>

<div name="quoted-content">On 19/09/2017 16:37, Yason TR wrote:<br/>
&gt; Hi all,<br/>
&gt;<br/>
&gt; 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/>
&gt;<br/>
&gt; The heart of the code can be seen as:<br/>
&gt;<br/>
&gt; while (true) {<br/>
&gt; Connection connection = null;<br/>
&gt; PGReplicationStream stream = null;<br/>
&gt;<br/>
&gt; try {<br/>
&gt; connection = DriverManager.getConnection(&quot;jdbc:postgresql://localhost:5432/db&quot;, properties);<br/>
&gt; stream = connection.unwrap(PGConnection.class).getReplicationAPI().replicationStream().logical().withSlotName(&quot;slot&quot;).start();<br/>
&gt;<br/>
&gt; while (true) {<br/>
&gt; final ByteBuffer buffer = stream.read();<br/>
&gt;<br/>
&gt; // ... MQ logic here ... omitted ...<br/>
&gt;<br/>
&gt; stream.setAppliedLSN(stream.getLastReceiveLSN());<br/>
&gt; stream.setFlushedLSN(stream.getLastReceiveLSN());<br/>
&gt; }<br/>
&gt; } catch (final SQLException e) {<br/>
&gt; // ... log exception ... omitted ...<br/>
&gt; } finally {<br/>
&gt; // ... close stream and connection ... omitted ...<br/>
&gt; }<br/>
&gt; }<br/>
&gt;<br/>
&gt; I notice some behavior which I cannot explain and would like to understand so I can alter my code:<br/>
&gt;<br/>
&gt; - 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&#39;t calling &#96;setAppliedLSN(stream.getLastReceiveLSN())&#96; and/or &#96;setFlushedLSN(stream.getLastReceiveLSN())&#96; enough to acknowledge an event, so it will removed from the WAL log and it will not be resent?<br/>
&gt;<br/>
&gt; - When receiving an event, the corresponding LSN from that event (which is sent in the payload) is not the same as the result of &#96;stream.getLastReceivedLSN()&#96;. Why is that? Which one should I use? Maybe this is correlated to my first question.<br/>
&gt;<br/>
&gt; - What is the difference between &#96;setAppliedLSN(LSN)&#96; and &#96;setFlushedLSN(LSN)&#96;? The Javadocs are not really helpful here.<br/>
<br/>
The stages of a wal location generally go like : sent -&gt; write -&gt; flush -&gt; 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/>
&quot;<br/>
In the event that replication has been restarted, it&#39;s will start from last successfully processed LSN that was sent via feedback to database.<br/>
&quot;<br/>
<br/>
&gt;<br/>
&gt; 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/>
&gt;<br/>
&gt; Thanks a lot and kind regards,<br/>
&gt;<br/>
&gt; Yason TR<br/>
&gt;<br/>
&gt;<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

In response to

Responses

Browse pgsql-general by date

  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

Browse pgsql-jdbc by date

  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