You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@bookkeeper.apache.org by GitBox <gi...@apache.org> on 2017/11/12 08:18:17 UTC
[GitHub] jiazhai closed pull request #715: Issue 694: ReadHandle doesn't have getLength() method
jiazhai closed pull request #715: Issue 694: ReadHandle doesn't have getLength() method
URL: https://github.com/apache/bookkeeper/pull/715
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
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 acb97e040..e71ae9c04 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 long getId() {
}
/**
- * 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 @@ synchronized void setLastAddConfirmed(long lac) {
}
/**
- * 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 4c1df5178..00dfac9ba 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 @@
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 47e1f9cd1..78c249f0c 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 @@
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 338c2b7d3..91ba83042 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 void setup() throws Exception {
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 void setup() throws Exception {
}
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 @@ protected void setupBookieClientAddEntry() {
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 @@ protected void setupBookieClientAddEntry() {
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 e878249a1..61fac9409 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 void testWriteHandle() throws Exception {
.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 void testWriteAdvHandle() throws Exception {
.withEnsembleSize(3)
.withPassword(password)
.makeAdv()
- .execute());) {
+ .execute())) {
assertEquals(ledgerId, writer.getId());
// test writer is able to write
@@ -82,6 +85,7 @@ public void testWriteAdvHandle() throws Exception {
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 void testWriteAdvHandleWithFixedLedgerId() throws Exception {
.withPassword(password)
.makeAdv()
.withLedgerId(1234)
- .execute());) {
+ .execute())) {
assertEquals(1234, writer.getId());
// test writer is able to write
@@ -105,6 +109,7 @@ public void testWriteAdvHandleWithFixedLedgerId() throws Exception {
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 void testWriteAdvHandleBKDuplicateEntryId() throws Exception {
.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 void testOpenLedgerUnauthorized() throws Exception {
.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 void testOpenLedgerDigestUnmatched() throws Exception {
.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 void testOpenLedgerRead() throws Exception {
.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 void testOpenLedgerRead() throws Exception {
.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 void testOpenLedgerWithRecovery() throws Exception {
.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 void testOpenLedgerWithRecovery() throws Exception {
.withRecovery(true)
.withLedgerId(lId)
.execute())) {
+ assertEquals(1L, reader.getLastAddConfirmed());
}
result(writer.append(ByteBuffer.wrap(data)));
@@ -230,8 +241,9 @@ public void testDeleteLedger() throws Exception {
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 void testCannotDeleteLedgerTwice() throws Exception {
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());
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
With regards,
Apache Git Services