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 2018/09/10 19:58:59 UTC
[GitHub] sijie closed pull request #1665: Issue #1664 Cancel Scheduled
SpeculativeReads
sijie closed pull request #1665: Issue #1664 Cancel Scheduled SpeculativeReads
URL: https://github.com/apache/bookkeeper/pull/1665
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/DefaultSpeculativeRequestExecutionPolicy.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/DefaultSpeculativeRequestExecutionPolicy.java
index 7474e56acb..b2874e5d67 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/DefaultSpeculativeRequestExecutionPolicy.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/DefaultSpeculativeRequestExecutionPolicy.java
@@ -26,6 +26,7 @@
import java.util.concurrent.RejectedExecutionException;
import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import org.slf4j.Logger;
@@ -64,18 +65,19 @@ public DefaultSpeculativeRequestExecutionPolicy(int firstSpeculativeRequestTimeo
*
* @param scheduler The scheduler service to issue the speculative request
* @param requestExecutor The executor is used to issue the actual speculative requests
+ * @return ScheduledFuture, in case caller needs to cancel it.
*/
@Override
- public void initiateSpeculativeRequest(final ScheduledExecutorService scheduler,
+ public ScheduledFuture<?> initiateSpeculativeRequest(final ScheduledExecutorService scheduler,
final SpeculativeRequestExecutor requestExecutor) {
- scheduleSpeculativeRead(scheduler, requestExecutor, firstSpeculativeRequestTimeout);
+ return scheduleSpeculativeRead(scheduler, requestExecutor, firstSpeculativeRequestTimeout);
}
- private void scheduleSpeculativeRead(final ScheduledExecutorService scheduler,
+ private ScheduledFuture<?> scheduleSpeculativeRead(final ScheduledExecutorService scheduler,
final SpeculativeRequestExecutor requestExecutor,
final int speculativeRequestTimeout) {
try {
- scheduler.schedule(new Runnable() {
+ return scheduler.schedule(new Runnable() {
@Override
public void run() {
ListenableFuture<Boolean> issueNextRequest = requestExecutor.issueSpeculativeRequest();
@@ -107,5 +109,6 @@ public void onFailure(Throwable thrown) {
requestExecutor, speculativeRequestTimeout, re);
}
}
+ return null;
}
}
diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/PendingReadOp.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/PendingReadOp.java
index d31a744f67..4ee9c925f7 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/PendingReadOp.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/PendingReadOp.java
@@ -482,6 +482,10 @@ protected void cancelSpeculativeTask(boolean mayInterruptIfRunning) {
}
}
+ public ScheduledFuture<?> getSpeculativeTask() {
+ return speculativeTask;
+ }
+
// I don't think this is ever used in production code -Ivan
PendingReadOp parallelRead(boolean enabled) {
this.parallelRead = enabled;
@@ -518,7 +522,7 @@ void initiate() {
for (LedgerEntryRequest entry : seq) {
entry.read();
if (!parallelRead && clientCtx.getConf().readSpeculativeRequestPolicy.isPresent()) {
- clientCtx.getConf().readSpeculativeRequestPolicy.get()
+ speculativeTask = clientCtx.getConf().readSpeculativeRequestPolicy.get()
.initiateSpeculativeRequest(clientCtx.getScheduler(), entry);
}
}
diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/ReadLastConfirmedAndEntryOp.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/ReadLastConfirmedAndEntryOp.java
index e61e666e1f..cb9de32111 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/ReadLastConfirmedAndEntryOp.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/ReadLastConfirmedAndEntryOp.java
@@ -25,6 +25,7 @@
import java.util.BitSet;
import java.util.List;
import java.util.concurrent.Callable;
+import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.bookkeeper.client.impl.LedgerEntryImpl;
@@ -64,6 +65,7 @@
private long lastAddConfirmed;
private long timeOutInMillis;
private final List<BookieSocketAddress> currentEnsemble;
+ private ScheduledFuture<?> speculativeTask = null;
abstract class ReadLACAndEntryRequest implements AutoCloseable {
@@ -461,6 +463,12 @@ ReadLastConfirmedAndEntryOp parallelRead(boolean enabled) {
return this;
}
+ protected void cancelSpeculativeTask(boolean mayInterruptIfRunning) {
+ if (speculativeTask != null) {
+ speculativeTask.cancel(mayInterruptIfRunning);
+ speculativeTask = null;
+ }
+ }
/**
* Speculative Read Logic.
*/
@@ -491,7 +499,7 @@ public void initiate() {
request.read();
if (!parallelRead && clientCtx.getConf().readLACSpeculativeRequestPolicy.isPresent()) {
- clientCtx.getConf().readLACSpeculativeRequestPolicy.get()
+ speculativeTask = clientCtx.getConf().readLACSpeculativeRequestPolicy.get()
.initiateSpeculativeRequest(clientCtx.getScheduler(), this);
}
}
@@ -521,6 +529,7 @@ void sendReadTo(int bookieIndex, BookieSocketAddress to, ReadLACAndEntryRequest
private void submitCallback(int rc) {
long latencyMicros = MathUtils.elapsedMicroSec(requestTimeNano);
LedgerEntry entry;
+ cancelSpeculativeTask(true);
if (BKException.Code.OK != rc) {
clientCtx.getClientStats().getReadLacAndEntryOpLogger()
.registerFailedEvent(latencyMicros, TimeUnit.MICROSECONDS);
diff --git a/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/SpeculativeRequestExecutionPolicy.java b/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/SpeculativeRequestExecutionPolicy.java
index bff4bb3f8e..e04dc98d90 100644
--- a/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/SpeculativeRequestExecutionPolicy.java
+++ b/bookkeeper-server/src/main/java/org/apache/bookkeeper/client/SpeculativeRequestExecutionPolicy.java
@@ -21,6 +21,7 @@
package org.apache.bookkeeper.client;
import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ScheduledFuture;
/**
* Define a policy for speculative request execution.
@@ -37,6 +38,8 @@
*
* @param scheduler The scheduler service to issue the speculative request
* @param requestExectuor The executor is used to issue the actual speculative requests
+ * @return ScheduledFuture, in case caller needs to cancel it.
*/
- void initiateSpeculativeRequest(ScheduledExecutorService scheduler, SpeculativeRequestExecutor requestExectuor);
+ ScheduledFuture<?> initiateSpeculativeRequest(ScheduledExecutorService scheduler,
+ SpeculativeRequestExecutor requestExectuor);
}
diff --git a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/TestSpeculativeRead.java b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/TestSpeculativeRead.java
index 5d251a954a..0ae13ba546 100644
--- a/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/TestSpeculativeRead.java
+++ b/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/TestSpeculativeRead.java
@@ -297,6 +297,26 @@ public void testSpeculativeReadFirstReadCompleteIsOk() throws Exception {
}
}
+ /**
+ * Unit test to check if the scheduled speculative task gets cancelled
+ * on successful read.
+ */
+ @Test
+ public void testSpeculativeReadScheduledTaskCancel() throws Exception {
+ long id = getLedgerToRead(3, 2);
+ int timeout = 1000;
+ BookKeeper bkspec = createClient(timeout);
+ LedgerHandle l = bkspec.openLedger(id, digestType, passwd);
+ PendingReadOp op = null;
+ try {
+ op = new PendingReadOp(l, bkspec.getClientCtx(), 0, 5, false);
+ op.initiate();
+ op.future().get();
+ } finally {
+ assertNull("Speculative Read tasks must be null", op.getSpeculativeTask());
+ }
+ }
+
/**
* Unit test for the speculative read scheduling method.
*/
@@ -353,7 +373,7 @@ public void testSpeculativeReadScheduling() throws Exception {
}
Thread.sleep(1000);
}
- assertTrue("Request should be done", req0.isComplete());
+ assertTrue("Request should be done", req.isComplete());
}
}
----------------------------------------------------------------
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