This is an automated email from the ASF dual-hosted git repository.

zhaijia pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/bookkeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new 388d4a4  ISSUE #694: ReadHandle doesn't have getLength() method
388d4a4 is described below

commit 388d4a4c73dfe0f5ed0adc9f77f37ffd9457d117
Author: Jia Zhai <[email protected]>
AuthorDate: Sun Nov 12 16:18:04 2017 +0800

    ISSUE #694: ReadHandle doesn't have getLength() method
    
    Descriptions of the changes in this PR:
    
    - add `getLength()` to ReadHandle interface
    - add `getLastAddPushed` to WriteHandle interface
    - add unit tests and fix issue in MockBookKeeperTestCase
    
    Author: Jia Zhai <[email protected]>
    Author: Sijie Guo <[email protected]>
    
    Reviewers: Enrico Olivelli <[email protected]>, Jia Zhai <None>
    
    This closes #715 from sijie/api/support_get_length, closes #694
---
 .../org/apache/bookkeeper/client/LedgerHandle.java | 20 +++---------
 .../apache/bookkeeper/client/api/ReadHandle.java   | 17 ++++++++--
 .../apache/bookkeeper/client/api/WriteHandle.java  |  8 +++++
 .../bookkeeper/client/MockBookKeeperTestCase.java  |  9 ++++--
 .../bookkeeper/client/api/BookKeeperApiTest.java   | 37 +++++++++++++++-------
 5 files changed, 59 insertions(+), 32 deletions(-)

diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/LedgerHandle.java
 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/LedgerHandle.java
index acb97e0..e71ae9c 100644
--- 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/LedgerHandle.java
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/LedgerHandle.java
@@ -192,17 +192,9 @@ public class LedgerHandle implements WriteHandle {
     }
 
     /**
-     * Get the last confirmed entry id on this ledger. It reads
-     * the local state of the ledger handle, which is different
-     * from the readLastConfirmed call. In the case the ledger
-     * is not closed and the client is a reader, it is necessary
-     * to call readLastConfirmed to obtain an estimate of the
-     * last add operation that has been confirmed.
-     *
-     * @see #readLastConfirmed()
-     *
-     * @return the last confirmed entry id or {@link #INVALID_ENTRY_ID 
INVALID_ENTRY_ID} if no entry has been confirmed
+     * {@inheritDoc}
      */
+    @Override
     public synchronized long getLastAddConfirmed() {
         return lastAddConfirmed;
     }
@@ -212,12 +204,10 @@ public class LedgerHandle implements WriteHandle {
     }
 
     /**
-     * Get the entry id of the last entry that has been enqueued for addition 
(but
-     * may not have possibly been persited to the ledger)
-     *
-     * @return the id of the last entry pushed or {@link #INVALID_ENTRY_ID 
INVALID_ENTRY_ID} if no entry has been pushed
+     * {@inheritDoc}
      */
-    synchronized public long getLastAddPushed() {
+    @Override
+    public synchronized long getLastAddPushed() {
         return lastAddPushed;
     }
 
diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/api/ReadHandle.java
 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/api/ReadHandle.java
index 4c1df51..00dfac9 100644
--- 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/api/ReadHandle.java
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/api/ReadHandle.java
@@ -92,10 +92,23 @@ public interface ReadHandle extends Handle {
     CompletableFuture<Long> tryReadLastAddConfirmed();
 
     /**
-     * Get the local value for LastAddConfirmed.
+     * Get the last confirmed entry id on this ledger. It reads the local 
state of the ledger handle,
+     * which is different from the {@link #readLastAddConfirmed()} call.
+
+     * <p>In the case the ledger is not closed and the client is a reader, it 
is necessary to
+     * call {@link #readLastAddConfirmed()} to obtain a fresh value of last 
add confirmed entry id.
+     *
+     * @see #readLastAddConfirmed()
      *
-     * @return the local value for LastAddConfirmed
+     * @return the local value for LastAddConfirmed or -1L if no entry has 
been confirmed.
      */
     long getLastAddConfirmed();
 
+    /**
+     * Returns the length of the data written in this ledger so much, in bytes.
+     *
+     * @return the length of the data written in this ledger, in bytes.
+     */
+    long getLength();
+
 }
diff --git 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/api/WriteHandle.java
 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/api/WriteHandle.java
index 47e1f9c..78c249f 100644
--- 
a/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/api/WriteHandle.java
+++ 
b/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/api/WriteHandle.java
@@ -52,4 +52,12 @@ public interface WriteHandle extends ReadHandle {
         return append(Unpooled.wrappedBuffer(data));
     }
 
+    /**
+     * Get the entry id of the last entry that has been enqueued for addition 
(but
+     * may not have possibly been persisted to the ledger).
+     *
+     * @return the entry id of the last entry pushed or -1 if no entry has 
been pushed.
+     */
+    long getLastAddPushed();
+
 }
diff --git 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/MockBookKeeperTestCase.java
 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/MockBookKeeperTestCase.java
index 338c2b7..91ba830 100644
--- 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/MockBookKeeperTestCase.java
+++ 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/MockBookKeeperTestCase.java
@@ -134,9 +134,9 @@ public abstract class MockBookKeeperTestCase {
         when(bk.getStatsLogger()).thenReturn(nullStatsLogger);
         when(bk.getLedgerManager()).thenReturn(ledgerManager);
         when(bk.getLedgerIdGenerator()).thenReturn(ledgerIdGenerator);
+        when(bk.getReturnRc(anyInt())).thenAnswer(invocationOnMock -> 
invocationOnMock.getArgument(0));
 
         setupLedgerIdGenerator();
-
         setupCreateLedgerMetadata();
         setupReadLedgerMetadata();
         setupWriteLedgerMetadata();
@@ -148,7 +148,7 @@ public abstract class MockBookKeeperTestCase {
     }
 
     protected NullStatsLogger setupLoggers() {
-        NullStatsLogger nullStatsLogger = new NullStatsLogger();
+        NullStatsLogger nullStatsLogger = NullStatsLogger.INSTANCE;
         
when(bk.getOpenOpLogger()).thenReturn(nullStatsLogger.getOpStatsLogger("mock"));
         
when(bk.getRecoverOpLogger()).thenReturn(nullStatsLogger.getOpStatsLogger("mock"));
         
when(bk.getAddOpLogger()).thenReturn(nullStatsLogger.getOpStatsLogger("mock"));
@@ -398,6 +398,9 @@ public abstract class MockBookKeeperTestCase {
             long entryId = (Long) args[3];
             ByteBuf toSend = (ByteBuf) args[4];
             Object ctx = args[6];
+            int options = (int) args[7];
+            boolean isRecoveryAdd =
+                ((short) options & BookieProtocol.FLAG_RECOVERY_ADD) == 
BookieProtocol.FLAG_RECOVERY_ADD;
 
             executor.submitOrdered(ledgerId, () -> {
                 byte[] entry;
@@ -408,7 +411,7 @@ public abstract class MockBookKeeperTestCase {
                     return;
                 }
                 boolean fenced = fencedLedgers.contains(ledgerId);
-                if (fenced) {
+                if (fenced && !isRecoveryAdd) {
                     
callback.writeComplete(BKException.Code.LedgerFencedException,
                         ledgerId, entryId, bookieSocketAddress, ctx);
                 } else {
diff --git 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperApiTest.java
 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperApiTest.java
index e878249..61fac94 100644
--- 
a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperApiTest.java
+++ 
b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/api/BookKeeperApiTest.java
@@ -52,13 +52,16 @@ public class BookKeeperApiTest extends 
MockBookKeeperTestCase {
                 .withWriteQuorumSize(2)
                 .withEnsembleSize(3)
                 .withPassword(password)
-                .execute());) {
+                .execute())) {
 
             // test writer is able to write
             result(writer.append(ByteBuffer.wrap(data)));
+            assertEquals(0L, writer.getLastAddPushed());
             result(writer.append(Unpooled.wrappedBuffer(data)));
+            assertEquals(1L, writer.getLastAddPushed());
             long expectedEntryId = 
result(writer.append(ByteBuffer.wrap(data)));
             assertEquals(expectedEntryId, writer.getLastAddConfirmed());
+            assertEquals(3 * data.length, writer.getLength());
         }
     }
 
@@ -73,7 +76,7 @@ public class BookKeeperApiTest extends MockBookKeeperTestCase 
{
                 .withEnsembleSize(3)
                 .withPassword(password)
                 .makeAdv()
-                .execute());) {
+                .execute())) {
             assertEquals(ledgerId, writer.getId());
 
             // test writer is able to write
@@ -82,6 +85,7 @@ public class BookKeeperApiTest extends MockBookKeeperTestCase 
{
             result(writer.write(entryId++, Unpooled.wrappedBuffer(data)));
             long expectedEntryId = result(writer.write(entryId++, 
ByteBuffer.wrap(data)));
             assertEquals(expectedEntryId, writer.getLastAddConfirmed());
+            assertEquals(3 * data.length, writer.getLength());
         }
     }
 
@@ -96,7 +100,7 @@ public class BookKeeperApiTest extends 
MockBookKeeperTestCase {
                 .withPassword(password)
                 .makeAdv()
                 .withLedgerId(1234)
-                .execute());) {
+                .execute())) {
             assertEquals(1234, writer.getId());
 
             // test writer is able to write
@@ -105,6 +109,7 @@ public class BookKeeperApiTest extends 
MockBookKeeperTestCase {
             result(writer.write(entryId++, Unpooled.wrappedBuffer(data)));
             long expectedEntryId = result(writer.write(entryId++, 
ByteBuffer.wrap(data)));
             assertEquals(expectedEntryId, writer.getLastAddConfirmed());
+            assertEquals(3 * data.length, writer.getLength());
         }
     }
 
@@ -118,10 +123,11 @@ public class BookKeeperApiTest extends 
MockBookKeeperTestCase {
                 .withPassword(password)
                 .makeAdv()
                 .withLedgerId(1234)
-                .execute());) {
+                .execute())) {
             assertEquals(1234, writer.getId());
             long entryId = 0;
             result(writer.write(entryId++, ByteBuffer.wrap(data)));
+            assertEquals(data.length, writer.getLength());
             result(writer.write(entryId - 1, ByteBuffer.wrap(data)));
         }
     }
@@ -135,10 +141,11 @@ public class BookKeeperApiTest extends 
MockBookKeeperTestCase {
                 .withWriteQuorumSize(2)
                 .withEnsembleSize(3)
                 .withPassword(password)
-                .execute());) {
+                .execute())) {
             lId = writer.getId();
+            assertEquals(-1L, writer.getLastAddPushed());
         }
-        try (ReadHandle reader
+        try (ReadHandle ignored
             = result(newOpenLedgerOp()
                 .withPassword("bad-password".getBytes(UTF_8))
                 .withLedgerId(lId)
@@ -156,10 +163,11 @@ public class BookKeeperApiTest extends 
MockBookKeeperTestCase {
                 .withEnsembleSize(3)
                 .withDigestType(DigestType.MAC)
                 .withPassword(password)
-                .execute());) {
+                .execute())) {
             lId = writer.getId();
+            assertEquals(-1L, writer.getLastAddPushed());
         }
-        try (ReadHandle reader = result(newOpenLedgerOp()
+        try (ReadHandle ignored = result(newOpenLedgerOp()
             .withDigestType(DigestType.CRC32)
             .withPassword(password)
             .withLedgerId(lId)
@@ -176,7 +184,7 @@ public class BookKeeperApiTest extends 
MockBookKeeperTestCase {
                 .withWriteQuorumSize(2)
                 .withEnsembleSize(3)
                 .withPassword(password)
-                .execute());) {
+                .execute())) {
             lId = writer.getId();
             // write data and populate LastAddConfirmed
             result(writer.append(ByteBuffer.wrap(data)));
@@ -190,6 +198,7 @@ public class BookKeeperApiTest extends 
MockBookKeeperTestCase {
             .withLedgerId(lId)
             .execute())) {
             assertEquals(2, reader.getLastAddConfirmed());
+            assertEquals(3 * data.length, reader.getLength());
             assertEquals(2, result(reader.readLastAddConfirmed()).intValue());
             assertEquals(2, 
result(reader.tryReadLastAddConfirmed()).intValue());
             checkEntries(result(reader.read(0, reader.getLastAddConfirmed())), 
data);
@@ -205,11 +214,12 @@ public class BookKeeperApiTest extends 
MockBookKeeperTestCase {
             .withWriteQuorumSize(2)
             .withEnsembleSize(3)
             .withPassword(password)
-            .execute());) {
+            .execute())) {
             lId = writer.getId();
 
             result(writer.append(ByteBuffer.wrap(data)));
             result(writer.append(ByteBuffer.wrap(data)));
+            assertEquals(1L, writer.getLastAddPushed());
 
             // open with fencing
             try (ReadHandle reader = result(newOpenLedgerOp()
@@ -217,6 +227,7 @@ public class BookKeeperApiTest extends 
MockBookKeeperTestCase {
                 .withRecovery(true)
                 .withLedgerId(lId)
                 .execute())) {
+                assertEquals(1L, reader.getLastAddConfirmed());
             }
 
             result(writer.append(ByteBuffer.wrap(data)));
@@ -230,8 +241,9 @@ public class BookKeeperApiTest extends 
MockBookKeeperTestCase {
 
         try (WriteHandle writer = result(newCreateLedgerOp()
             .withPassword(password)
-            .execute());) {
+            .execute())) {
             lId = writer.getId();
+            assertEquals(-1L, writer.getLastAddPushed());
         }
 
         result(newDeleteLedgerOp().withLedgerId(lId).execute());
@@ -248,8 +260,9 @@ public class BookKeeperApiTest extends 
MockBookKeeperTestCase {
 
         try (WriteHandle writer = result(newCreateLedgerOp()
             .withPassword(password)
-            .execute());) {
+            .execute())) {
             lId = writer.getId();
+            assertEquals(-1L, writer.getLastAddPushed());
         }
         result(newDeleteLedgerOp().withLedgerId(lId).execute());
         result(newDeleteLedgerOp().withLedgerId(lId).execute());

-- 
To stop receiving notification emails like this one, please contact
['"[email protected]" <[email protected]>'].

Reply via email to