Honor PROPERTY_REQUEST_TIMEOUT in clearContainer

This commit is contained in:
Andrew Gaul 2012-11-02 15:08:08 -07:00
parent 34d9d05eca
commit b16ca139a4
3 changed files with 47 additions and 6 deletions

View File

@ -27,6 +27,7 @@ import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import javax.inject.Named;
@ -62,12 +63,10 @@ public class DeleteAllKeysInList implements ClearListStrategy, ClearContainerStr
private final ExecutorService userExecutor;
protected final AsyncBlobStore connection;
/**
* maximum duration of an blob Request
*/
/** Maximum duration in milliseconds of a request. */
@Inject(optional = true)
@Named(Constants.PROPERTY_REQUEST_TIMEOUT)
protected Long maxTime;
protected Long maxTime = Long.MAX_VALUE;
@Inject
DeleteAllKeysInList(@Named(Constants.PROPERTY_USER_THREADS) ExecutorService userExecutor,
@ -98,7 +97,7 @@ public class DeleteAllKeysInList implements ClearListStrategy, ClearContainerStr
Future<PageSet<? extends StorageMetadata>> listFuture =
connection.list(containerName, options);
try {
listing = listFuture.get();
listing = listFuture.get(maxTime, TimeUnit.MILLISECONDS);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
break;
@ -109,6 +108,13 @@ public class DeleteAllKeysInList implements ClearListStrategy, ClearContainerStr
}
retryHandler.imposeBackoffExponentialDelay(numErrors, message);
continue;
} catch (TimeoutException te) {
++numErrors;
if (numErrors == maxErrors) {
throw propagate(te);
}
retryHandler.imposeBackoffExponentialDelay(numErrors, message);
continue;
} finally {
listFuture.cancel(true);
}

View File

@ -18,16 +18,30 @@
*/
package org.jclouds.blobstore.strategy.internal;
import static org.easymock.EasyMock.anyLong;
import static org.easymock.EasyMock.anyObject;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.fail;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.TimeUnit;
import org.jclouds.ContextBuilder;
import org.jclouds.blobstore.AsyncBlobStore;
import org.jclouds.blobstore.BlobStore;
import org.jclouds.blobstore.options.ListContainerOptions;
import org.jclouds.blobstore.domain.PageSet;
import org.jclouds.blobstore.domain.StorageMetadata;
import org.jclouds.util.Throwables2;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.google.common.io.Closeables;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.inject.Injector;
/**
@ -74,6 +88,27 @@ public class DeleteAllKeysInListTest {
assertEquals(blobstore.countBlobs(containerName), 1111);
}
public void testListTimeoutException() throws Exception {
ListenableFuture<PageSet<? extends StorageMetadata>> future = createMock(ListenableFuture.class);
expect(future.get(anyLong(), anyObject(TimeUnit.class))).andThrow(new RuntimeException(new TimeoutException()));
expect(future.cancel(true)).andReturn(true);
replay(future);
AsyncBlobStore asyncBlobStore = createMock(AsyncBlobStore.class);
expect(asyncBlobStore.list(anyObject(String.class), anyObject(ListContainerOptions.class))).andReturn(future);
replay(asyncBlobStore);
deleter = new DeleteAllKeysInList(null, asyncBlobStore, null);
try {
deleter.execute(containerName, ListContainerOptions.NONE);
fail();
} catch (Exception e) {
if (Throwables2.getFirstThrowableOfType(e, TimeoutException.class) == null) {
throw e;
}
}
}
/**
* Create a container "container" with 1111 blobs named "blob-%d". Create a
* subdirectory "directory" which contains 2222 more blobs named

View File

@ -153,7 +153,7 @@ public interface Constants {
/**
* Long property.
* <p/>
* longest time a single request can take before throwing an exception.
* Maximum duration in milliseconds a single request can take before throwing an exception.
*/
public static final String PROPERTY_REQUEST_TIMEOUT = "jclouds.request-timeout";
/**