Copilot commented on code in PR #24955:
URL: https://github.com/apache/pulsar/pull/24955#discussion_r2508189672


##########
testmocks/src/main/java/org/apache/bookkeeper/client/PulsarMockLedgerHandle.java:
##########
@@ -53,7 +54,7 @@
  */
 public class PulsarMockLedgerHandle extends LedgerHandle {
 
-    final ArrayList<LedgerEntryImpl> entries = Lists.newArrayList();
+    final List<LedgerEntryImpl> entries =  Collections.synchronizedList(new 
ArrayList<>());

Review Comment:
   There are two spaces after the equals sign. This should be a single space 
for consistency with standard Java formatting conventions.
   ```suggestion
       final List<LedgerEntryImpl> entries = Collections.synchronizedList(new 
ArrayList<>());
   ```



##########
testmocks/src/main/java/org/apache/bookkeeper/client/PulsarMockLedgerHandle.java:
##########
@@ -193,6 +198,7 @@ public void asyncAddEntry(final ByteBuf data, final 
AddCallback cb, final Object
                     data.readBytes(storedData);
                     entries.add(LedgerEntryImpl.create(ledgerId, lastEntry,
                                                        storedData.length, 
Unpooled.wrappedBuffer(storedData)));
+                    totalLengthCounter.addAndGet(storedData.length);

Review Comment:
   The addition of an entry to the `entries` list and the increment of 
`totalLengthCounter` are not atomic. This creates a race condition where 
`getLength()` could return an inconsistent value if called between these two 
operations. Consider incrementing the counter before adding to the list, or use 
a synchronized block to ensure atomicity of both operations together.



##########
testmocks/src/main/java/org/apache/bookkeeper/client/PulsarMockLedgerHandle.java:
##########
@@ -160,6 +164,7 @@ public long addEntry(byte[] data) throws 
InterruptedException, BKException {
 
         lastEntry = entries.size();
         entries.add(LedgerEntryImpl.create(ledgerId, lastEntry, data.length, 
Unpooled.wrappedBuffer(data)));
+        totalLengthCounter.addAndGet(data.length);

Review Comment:
   The addition of an entry to the `entries` list and the increment of 
`totalLengthCounter` are not atomic. This creates a race condition where 
`getLength()` could return an inconsistent value if called between these two 
operations. Consider incrementing the counter before adding to the list, or use 
a synchronized block to ensure atomicity of both operations together.
   ```suggestion
           synchronized (this) {
               lastEntry = entries.size();
               entries.add(LedgerEntryImpl.create(ledgerId, lastEntry, 
data.length, Unpooled.wrappedBuffer(data)));
               totalLengthCounter.addAndGet(data.length);
           }
   ```



-- 
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