Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed BulkGetFuture.get() API to honor operationTimeout. #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/net/spy/memcached/MemcachedClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -1319,7 +1319,7 @@ public <T> BulkFuture<Map<String, T>> asyncGetBulk(Iterator<String> keyIter,
int initialLatchCount = chunks.isEmpty() ? 0 : 1;
final CountDownLatch latch = new CountDownLatch(initialLatchCount);
final Collection<Operation> ops = new ArrayList<Operation>(chunks.size());
final BulkGetFuture<T> rv = new BulkGetFuture<T>(m, ops, latch, executorService);
final BulkGetFuture<T> rv = new BulkGetFuture<T>(m, ops, latch, operationTimeout, executorService);

GetOperation.Callback cb = new GetOperation.Callback() {
@Override
Expand Down
8 changes: 5 additions & 3 deletions src/main/java/net/spy/memcached/internal/BulkGetFuture.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,18 @@ public class BulkGetFuture<T>
private final Map<String, Future<T>> rvMap;
private final Collection<Operation> ops;
private final CountDownLatch latch;
private final long defaultTimeoutMillis;
private OperationStatus status;
private boolean cancelled = false;
private boolean timeout = false;

public BulkGetFuture(Map<String, Future<T>> m, Collection<Operation> getOps,
CountDownLatch l, ExecutorService service) {
CountDownLatch l, long defaultTimeoutMillis, ExecutorService service) {
super(service);
rvMap = m;
ops = getOps;
latch = l;
this.defaultTimeoutMillis = defaultTimeoutMillis;
status = null;
}

Expand All @@ -86,9 +88,9 @@ public boolean cancel(boolean ign) {

public Map<String, T> get() throws InterruptedException, ExecutionException {
try {
return get(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
return get(defaultTimeoutMillis, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
throw new RuntimeException("Timed out waiting forever", e);
throw new ExecutionException("Bulk operation timed out after " + defaultTimeoutMillis + " millis", e);
}
}

Expand Down
37 changes: 37 additions & 0 deletions src/test/java/net/spy/memcached/TimeoutTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@

package net.spy.memcached;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeoutException;

/**
* A TimeoutTest.
*/
Expand Down Expand Up @@ -90,6 +94,39 @@ public void run() {
});
}

public void testAsyncGetBulkCustomTimeout() {
tryTimeout("asyncGetBulk", new Runnable() {
public void run() {
try {
client.asyncGetBulk("k", "k2").get(500, TimeUnit.MILLISECONDS);
} catch (TimeoutException e) {
throw new OperationTimeoutException("Bulk op timed out - custom timeout", e);
} catch (Exception e) {
throw new RuntimeException("Unexpected exception in bulk op", e);
}
}
});
}

public void testAsyncGetBulkDefaultTimeout() {
tryTimeout("asyncGetBulk", new Runnable() {
public void run() {
try {
client.asyncGetBulk("k", "k2").get();
} catch (ExecutionException e) {
if (e.getCause() instanceof TimeoutException) {
throw new OperationTimeoutException("Bulk op timed out - default timeout", e.getCause());
}
else {
throw new RuntimeException("Unexpected execution exception in bulk op", e);
}
} catch (Exception e) {
throw new RuntimeException("Unexpected exception in bulk op", e);
}
}
});
}

public void testIncrTimeout() {
tryTimeout("incr", new Runnable() {
public void run() {
Expand Down