You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "wernerdv (via GitHub)" <gi...@apache.org> on 2025/12/06 19:21:57 UTC

[PR] MINOR: Clean up ProducerStateEntry [kafka]

wernerdv opened a new pull request, #21096:
URL: https://github.com/apache/kafka/pull/21096

   A small cleanup in ProducerStateEntry: fixed access modifiers for methods.


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Clean up ProducerStateEntry [kafka]

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #21096:
URL: https://github.com/apache/kafka/pull/21096#discussion_r2632406196


##########
storage/src/main/java/org/apache/kafka/storage/internals/log/ProducerStateEntry.java:
##########
@@ -55,11 +55,11 @@ public ProducerStateEntry(long producerId, short producerEpoch, int coordinatorE
         firstBatchMetadata.ifPresent(batchMetadata::add);

Review Comment:
   Yeah, I don't have a strong preference on exactly how it is done. I just want to make sure that it is the case that the timestamp passed into this method (originally) was always the same as the batch timestamp. Just want to make sure we aren't changing the logic. 



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Clean up ProducerStateEntry [kafka]

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #21096:
URL: https://github.com/apache/kafka/pull/21096#discussion_r2628649330


##########
storage/src/main/java/org/apache/kafka/storage/internals/log/ProducerStateEntry.java:
##########
@@ -42,24 +41,28 @@ public class ProducerStateEntry {
     private long lastTimestamp;
     private OptionalLong currentTxnFirstOffset;
 
-    public static ProducerStateEntry empty(long producerId) {
+    static ProducerStateEntry empty(long producerId) {
         return new ProducerStateEntry(producerId, RecordBatch.NO_PRODUCER_EPOCH, -1, RecordBatch.NO_TIMESTAMP, OptionalLong.empty(), Optional.empty());
     }
 
-    public ProducerStateEntry(long producerId, short producerEpoch, int coordinatorEpoch, long lastTimestamp, OptionalLong currentTxnFirstOffset, Optional<BatchMetadata> firstBatchMetadata) {
+    public ProducerStateEntry(long producerId, short producerEpoch, int coordinatorEpoch, long lastTimestamp,
+                              OptionalLong currentTxnFirstOffset, Optional<BatchMetadata> firstBatchMetadata) {
         this.producerId = producerId;
         this.producerEpoch = producerEpoch;
         this.coordinatorEpoch = coordinatorEpoch;
         this.lastTimestamp = lastTimestamp;
         this.currentTxnFirstOffset = currentTxnFirstOffset;
-        firstBatchMetadata.ifPresent(batchMetadata::add);
+        firstBatchMetadata.ifPresent(batch -> {

Review Comment:
   This part is a little weird, so I left a comment on the AK pr: https://github.com/apache/kafka/pull/21096#discussion_r2628647232



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Clean up ProducerStateEntry [kafka]

Posted by "wernerdv (via GitHub)" <gi...@apache.org>.
wernerdv commented on code in PR #21096:
URL: https://github.com/apache/kafka/pull/21096#discussion_r2631678606


##########
storage/src/main/java/org/apache/kafka/storage/internals/log/ProducerStateEntry.java:
##########
@@ -55,11 +55,11 @@ public ProducerStateEntry(long producerId, short producerEpoch, int coordinatorE
         firstBatchMetadata.ifPresent(batchMetadata::add);

Review Comment:
   I liked your suggestion - I’ve opened a PR https://github.com/apache/kafka/pull/21174. 
   Please take a look when you have a chance.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Clean up ProducerStateEntry [kafka]

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #21096:
URL: https://github.com/apache/kafka/pull/21096#discussion_r2628649330


##########
storage/src/main/java/org/apache/kafka/storage/internals/log/ProducerStateEntry.java:
##########
@@ -42,24 +41,28 @@ public class ProducerStateEntry {
     private long lastTimestamp;
     private OptionalLong currentTxnFirstOffset;
 
-    public static ProducerStateEntry empty(long producerId) {
+    static ProducerStateEntry empty(long producerId) {
         return new ProducerStateEntry(producerId, RecordBatch.NO_PRODUCER_EPOCH, -1, RecordBatch.NO_TIMESTAMP, OptionalLong.empty(), Optional.empty());
     }
 
-    public ProducerStateEntry(long producerId, short producerEpoch, int coordinatorEpoch, long lastTimestamp, OptionalLong currentTxnFirstOffset, Optional<BatchMetadata> firstBatchMetadata) {
+    public ProducerStateEntry(long producerId, short producerEpoch, int coordinatorEpoch, long lastTimestamp,
+                              OptionalLong currentTxnFirstOffset, Optional<BatchMetadata> firstBatchMetadata) {
         this.producerId = producerId;
         this.producerEpoch = producerEpoch;
         this.coordinatorEpoch = coordinatorEpoch;
         this.lastTimestamp = lastTimestamp;
         this.currentTxnFirstOffset = currentTxnFirstOffset;
-        firstBatchMetadata.ifPresent(batchMetadata::add);
+        firstBatchMetadata.ifPresent(batch -> {

Review Comment:
   This part is a little weird, so I left a comment on the AK pr: https://github.com/apache/kafka/pull/21096#discussion_r2628647232



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Clean up ProducerStateEntry [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #21096:
URL: https://github.com/apache/kafka/pull/21096#discussion_r2630590274


##########
storage/src/main/java/org/apache/kafka/storage/internals/log/ProducerStateEntry.java:
##########
@@ -55,11 +55,11 @@ public ProducerStateEntry(long producerId, short producerEpoch, int coordinatorE
         firstBatchMetadata.ifPresent(batchMetadata::add);

Review Comment:
   Yes.
   
   My point was to remove the "first" batch from constructor and add it after creating the entry. For example:
   
   var entry = new ProducerStateEntry ...
   
   entry.addBatch(new BatchMetadata(...))
   
   This can streamline the constructor.
   
   (Sorry for the formatting. I typed this on my phone)
   



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Clean up ProducerStateEntry [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #21096:
URL: https://github.com/apache/kafka/pull/21096#discussion_r2599021249


##########
storage/src/main/java/org/apache/kafka/storage/internals/log/ProducerStateEntry.java:
##########
@@ -55,11 +55,11 @@ public ProducerStateEntry(long producerId, short producerEpoch, int coordinatorE
         firstBatchMetadata.ifPresent(batchMetadata::add);

Review Comment:
   Should we update the `lastTimestamp` based on `firstBatchMetadata`? 



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Clean up ProducerStateEntry [kafka]

Posted by "wernerdv (via GitHub)" <gi...@apache.org>.
wernerdv commented on code in PR #21096:
URL: https://github.com/apache/kafka/pull/21096#discussion_r2599562854


##########
storage/src/main/java/org/apache/kafka/storage/internals/log/ProducerStateEntry.java:
##########
@@ -55,11 +55,11 @@ public ProducerStateEntry(long producerId, short producerEpoch, int coordinatorE
         firstBatchMetadata.ifPresent(batchMetadata::add);

Review Comment:
   Yep, done.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Clean up ProducerStateEntry [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 merged PR #21096:
URL: https://github.com/apache/kafka/pull/21096


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Clean up ProducerStateEntry [kafka]

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #21096:
URL: https://github.com/apache/kafka/pull/21096#discussion_r2628647232


##########
storage/src/main/java/org/apache/kafka/storage/internals/log/ProducerStateEntry.java:
##########
@@ -55,11 +55,11 @@ public ProducerStateEntry(long producerId, short producerEpoch, int coordinatorE
         firstBatchMetadata.ifPresent(batchMetadata::add);

Review Comment:
   In the code, it looks like we tend to use the same value in the batchMetadata timestamp as the we do when creating the state (or the -1 value if no batchMetadata) 
   
   I think in some cases we don't have the batchMetadata, and we use a different method to update it there anyway. This code is pretty complicated, so maybe I'm missing something, but just want to make sure we aren't making it more complicated.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Clean up ProducerStateEntry [kafka]

Posted by "wernerdv (via GitHub)" <gi...@apache.org>.
wernerdv commented on code in PR #21096:
URL: https://github.com/apache/kafka/pull/21096#discussion_r2597917262


##########
storage/src/main/java/org/apache/kafka/storage/internals/log/ProducerStateEntry.java:
##########
@@ -71,36 +71,32 @@ public long lastDataOffset() {
         return isEmpty() ? -1L : batchMetadata.getLast().lastOffset();
     }
 
-    public int lastOffsetDelta() {
+    int lastOffsetDelta() {
         return isEmpty() ? 0 : batchMetadata.getLast().offsetDelta();
     }
 
-    public boolean isEmpty() {
+    boolean isEmpty() {
         return batchMetadata.isEmpty();
     }
 
     /**
      * Returns a new instance with the provided parameters (when present) and the values from the current instance
      * otherwise.
      */
-    public ProducerStateEntry withProducerIdAndBatchMetadata(long producerId, Optional<BatchMetadata> batchMetadata) {
-        return new ProducerStateEntry(producerId, this.producerEpoch(), this.coordinatorEpoch, this.lastTimestamp,
-            this.currentTxnFirstOffset, batchMetadata);
+    ProducerStateEntry withProducerIdAndBatchMetadata(long producerId, Optional<BatchMetadata> batchMetadata) {

Review Comment:
   Fixed



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Clean up ProducerStateEntry [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #21096:
URL: https://github.com/apache/kafka/pull/21096#discussion_r2629108165


##########
storage/src/main/java/org/apache/kafka/storage/internals/log/ProducerStateEntry.java:
##########
@@ -55,11 +55,11 @@ public ProducerStateEntry(long producerId, short producerEpoch, int coordinatorE
         firstBatchMetadata.ifPresent(batchMetadata::add);

Review Comment:
   My original thought was that the handling of BatchMetadata should be consistent between the constructor and addBatch. Perhaps a better cleanup would be to remove the manual assignment logic in the constructor and simply call addBatch explicitly
   
   But I am also happy with just reverting the timestamp change for now



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Clean up ProducerStateEntry [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #21096:
URL: https://github.com/apache/kafka/pull/21096#discussion_r2630350347


##########
storage/src/main/java/org/apache/kafka/storage/internals/log/ProducerStateEntry.java:
##########
@@ -55,11 +55,11 @@ public ProducerStateEntry(long producerId, short producerEpoch, int coordinatorE
         firstBatchMetadata.ifPresent(batchMetadata::add);

Review Comment:
   If we have to handle the "first" batch in constructor, I would prefer to adopt @jolshan auggestion.



##########
storage/src/main/java/org/apache/kafka/storage/internals/log/ProducerStateEntry.java:
##########
@@ -55,11 +55,11 @@ public ProducerStateEntry(long producerId, short producerEpoch, int coordinatorE
         firstBatchMetadata.ifPresent(batchMetadata::add);

Review Comment:
   If we have to handle the "first" batch in constructor, I would prefer to adopt @jolshan suggestion.



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Clean up ProducerStateEntry [kafka]

Posted by "wernerdv (via GitHub)" <gi...@apache.org>.
wernerdv commented on PR #21096:
URL: https://github.com/apache/kafka/pull/21096#issuecomment-3620993403

   @dajac @chia7712 PTAL.


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Clean up ProducerStateEntry [kafka]

Posted by "chia7712 (via GitHub)" <gi...@apache.org>.
chia7712 commented on code in PR #21096:
URL: https://github.com/apache/kafka/pull/21096#discussion_r2597824391


##########
storage/src/main/java/org/apache/kafka/storage/internals/log/ProducerStateEntry.java:
##########
@@ -71,36 +71,32 @@ public long lastDataOffset() {
         return isEmpty() ? -1L : batchMetadata.getLast().lastOffset();
     }
 
-    public int lastOffsetDelta() {
+    int lastOffsetDelta() {
         return isEmpty() ? 0 : batchMetadata.getLast().offsetDelta();
     }
 
-    public boolean isEmpty() {
+    boolean isEmpty() {
         return batchMetadata.isEmpty();
     }
 
     /**
      * Returns a new instance with the provided parameters (when present) and the values from the current instance
      * otherwise.
      */
-    public ProducerStateEntry withProducerIdAndBatchMetadata(long producerId, Optional<BatchMetadata> batchMetadata) {
-        return new ProducerStateEntry(producerId, this.producerEpoch(), this.coordinatorEpoch, this.lastTimestamp,
-            this.currentTxnFirstOffset, batchMetadata);
+    ProducerStateEntry withProducerIdAndBatchMetadata(long producerId, Optional<BatchMetadata> batchMetadata) {

Review Comment:
   it appears `batchMetadata` is not used in production. could you refactor it as well?



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Clean up ProducerStateEntry [kafka]

Posted by "wernerdv (via GitHub)" <gi...@apache.org>.
wernerdv commented on PR #21096:
URL: https://github.com/apache/kafka/pull/21096#issuecomment-3635332048

   @chia7712 Thanks for the review.


-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Clean up ProducerStateEntry [kafka]

Posted by "jolshan (via GitHub)" <gi...@apache.org>.
jolshan commented on code in PR #21096:
URL: https://github.com/apache/kafka/pull/21096#discussion_r2628627119


##########
storage/src/main/java/org/apache/kafka/storage/internals/log/ProducerStateEntry.java:
##########
@@ -55,11 +55,11 @@ public ProducerStateEntry(long producerId, short producerEpoch, int coordinatorE
         firstBatchMetadata.ifPresent(batchMetadata::add);

Review Comment:
   Hmm -- can you explain a bit more about this change? We have two updates to this.lastTimestamp now. Do we know if these values can be different? 



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Clean up ProducerStateEntry [kafka]

Posted by "wernerdv (via GitHub)" <gi...@apache.org>.
wernerdv commented on code in PR #21096:
URL: https://github.com/apache/kafka/pull/21096#discussion_r2630171480


##########
storage/src/main/java/org/apache/kafka/storage/internals/log/ProducerStateEntry.java:
##########
@@ -55,11 +55,11 @@ public ProducerStateEntry(long producerId, short producerEpoch, int coordinatorE
         firstBatchMetadata.ifPresent(batchMetadata::add);

Review Comment:
   What are your thoughts on this approach?
   ```
       public ProducerStateEntry(long producerId, short producerEpoch, int coordinatorEpoch, long lastTimestamp,
                                 OptionalLong currentTxnFirstOffset, Optional<BatchMetadata> firstBatchMetadata) {
           this.producerId = producerId;
           this.producerEpoch = producerEpoch;
           this.coordinatorEpoch = coordinatorEpoch;
           this.currentTxnFirstOffset = currentTxnFirstOffset;
           firstBatchMetadata.ifPresentOrElse(
               batch -> addBatch(producerEpoch, batch.lastSeq(), batch.lastOffset(), batch.offsetDelta(), batch.timestamp()),
               () -> this.lastTimestamp = lastTimestamp
           );
       }
   ```



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] MINOR: Clean up ProducerStateEntry [kafka]

Posted by "wernerdv (via GitHub)" <gi...@apache.org>.
wernerdv commented on code in PR #21096:
URL: https://github.com/apache/kafka/pull/21096#discussion_r2630367699


##########
storage/src/main/java/org/apache/kafka/storage/internals/log/ProducerStateEntry.java:
##########
@@ -55,11 +55,11 @@ public ProducerStateEntry(long producerId, short producerEpoch, int coordinatorE
         firstBatchMetadata.ifPresent(batchMetadata::add);

Review Comment:
   Revert to `firstBatchMetadata.ifPresent(batchMetadata::add)`; ?



-- 
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: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org