poorbarcode commented on code in PR #130:
URL: 
https://github.com/apache/flink-connector-pulsar/pull/130#discussion_r4332552261


##########
flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/sink/committer/PulsarCommittableSerializer.java:
##########
@@ -20,46 +20,70 @@
 
 import org.apache.flink.core.io.SimpleVersionedSerializer;
 
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;

Review Comment:
   Switched V2 to an explicit binary DataOutputStream/DataInputStream format. 
This removes the Jackson runtime dependency and the shading concern.



##########
flink-sql-connector-pulsar/pom.xml:
##########
@@ -72,6 +80,8 @@ under the License.
                                                        <artifactSet>
                                                                <includes>
                                                                        
<include>com.fasterxml.jackson.core:jackson-annotations</include>
+                                                                       
<include>com.fasterxml.jackson.core:jackson-core</include>
+                                                                       
<include>com.fasterxml.jackson.core:jackson-databind</include>

Review Comment:
   Moot after switching V2 to the binary format. The Jackson dependencies, 
artifact includes, and NOTICE entries have been removed from the SQL connector.



##########
flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/sink/committer/PulsarCommittableSerializer.java:
##########
@@ -20,46 +20,70 @@
 
 import org.apache.flink.core.io.SimpleVersionedSerializer;
 
+import com.fasterxml.jackson.databind.DeserializationFeature;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import com.fasterxml.jackson.databind.SerializationFeature;
 import org.apache.pulsar.client.api.transaction.TxnID;
 
 import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
 import java.io.DataInputStream;
-import java.io.DataOutputStream;
 import java.io.IOException;
 
 /** A serializer used to serialize {@link PulsarCommittable}. */
 public class PulsarCommittableSerializer implements 
SimpleVersionedSerializer<PulsarCommittable> {
 
-    private static final int CURRENT_VERSION = 1;
+    private static final int VERSION_V1 = 1;
+    private static final int VERSION_V2 = 2;
+    private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
+
+    static {
+        
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, 
false);
+        
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, 
false);
+        
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_INVALID_SUBTYPE, false);
+        
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_NULL_FOR_PRIMITIVES, 
false);
+        
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_NULL_CREATOR_PROPERTIES, 
false);
+        
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES,
 false);
+        
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_UNRESOLVED_OBJECT_IDS, 
false);
+        
OBJECT_MAPPER.configure(DeserializationFeature.FAIL_ON_TRAILING_TOKENS, false);
+        OBJECT_MAPPER.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, 
false);
+        
OBJECT_MAPPER.configure(SerializationFeature.FAIL_ON_UNWRAPPED_TYPE_IDENTIFIERS,
 false);
+    }
 
     @Override
     public int getVersion() {
-        return CURRENT_VERSION;
+        return VERSION_V2;
     }
 
     @Override
     public byte[] serialize(PulsarCommittable obj) throws IOException {
-        try (final ByteArrayOutputStream baos = new ByteArrayOutputStream();
-                final DataOutputStream out = new DataOutputStream(baos)) {
-            TxnID txnID = obj.getTxnID();
-            out.writeLong(txnID.getMostSigBits());
-            out.writeLong(txnID.getLeastSigBits());
-            out.writeUTF(obj.getTopic());
-            out.flush();
-            return baos.toByteArray();
-        }
+        return OBJECT_MAPPER.writeValueAsBytes(obj);
     }
 
     @Override
     public PulsarCommittable deserialize(int version, byte[] serialized) 
throws IOException {
+        if (version == VERSION_V1) {
+            return deserializeV1(serialized);
+        }
+        return deserializeV2(serialized);
+    }
+
+    private PulsarCommittable deserializeV1(byte[] serialized) throws 
IOException {
         try (final ByteArrayInputStream bais = new 
ByteArrayInputStream(serialized);
                 final DataInputStream in = new DataInputStream(bais)) {
             long mostSigBits = in.readLong();
             long leastSigBits = in.readLong();
             TxnID txnID = new TxnID(mostSigBits, leastSigBits);
-            String topic = in.readUTF();
-            return new PulsarCommittable(txnID, topic);
+            return new PulsarCommittable(txnID);
         }
     }
+
+    private PulsarCommittable deserializeV2(byte[] serialized) throws 
IOException {
+        PulsarCommittablePojo pulsarCommittablePojo =
+                OBJECT_MAPPER.readValue(serialized, 
PulsarCommittablePojo.class);
+        TxnID txnID =
+                new TxnID(
+                        pulsarCommittablePojo.getTxnID().getMostSigBits(),
+                        pulsarCommittablePojo.getTxnID().getLeastSigBits());
+        return new PulsarCommittable(txnID, 
pulsarCommittablePojo.getLatestPublishedMessages());

Review Comment:
   The binary V2 payload now carries the original committable state version. 
Added a V1 -> V2-format -> V1 round-trip assertion to ensure legacy 
committables retain version 1.



##########
flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/sink/writer/PulsarWriter.java:
##########
@@ -167,14 +174,25 @@ public void write(IN element, Context context) throws 
IOException, InterruptedEx
             CompletableFuture<MessageId> future = builder.sendAsync();
             future.whenComplete(
                     (id, ex) -> {
-                        pendingMessages.decrementAndGet();
                         if (ex != null) {
                             mailboxExecutor.execute(
                                     () -> throwSendingException(topic, ex),
                                     "Failed to send data to Pulsar");
                         } else {
+                            // Safe cast: all MessageId implementations in 
Pulsar implement
+                            // MessageIdAdv.
+                            MessageIdAdv messageIdAdv = (MessageIdAdv) id;
+                            latestPublishedMessages.put(
+                                    topic,
+                                    new MessageIdPojo(
+                                            messageIdAdv.getLedgerId(),
+                                            messageIdAdv.getEntryId(),
+                                            messageIdAdv.getBatchSize(),
+                                            messageIdAdv.getBatchIndex(),
+                                            messageIdAdv.getPartitionIndex()));
                             LOG.debug("Sent message to Pulsar {} with message 
id {}", topic, id);
                         }
+                        pendingMessages.decrementAndGet();

Review Comment:
   Updated the callback to decrement pendingMessages in a finally block, 
preserving the ordering while preventing a failed callback path from hanging 
flush(). Also reworded the MessageIdAdv comment to describe the producer send 
path specifically.



##########
flink-connector-pulsar/src/main/java/org/apache/flink/connector/pulsar/sink/writer/PulsarWriter.java:
##########
@@ -167,14 +174,25 @@ public void write(IN element, Context context) throws 
IOException, InterruptedEx
             CompletableFuture<MessageId> future = builder.sendAsync();
             future.whenComplete(
                     (id, ex) -> {
-                        pendingMessages.decrementAndGet();
                         if (ex != null) {
                             mailboxExecutor.execute(
                                     () -> throwSendingException(topic, ex),
                                     "Failed to send data to Pulsar");
                         } else {
+                            // Safe cast: all MessageId implementations in 
Pulsar implement
+                            // MessageIdAdv.
+                            MessageIdAdv messageIdAdv = (MessageIdAdv) id;
+                            latestPublishedMessages.put(

Review Comment:
   Updated the per-topic value atomically with ConcurrentHashMap.compute and 
MessageIdAdv comparison, so out-of-order callbacks retain the maximum ID. I 
intentionally did not add negative-ID filtering because this successful 
producer-send callback is expected to return actual published positions; please 
let me know if a broker-dedup acknowledgement with -1 is delivered through this 
callback.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to