Merge pull request #1185 from jclouds/process-named-timeouts

Process named timeouts
This commit is contained in:
Adrian Cole 2013-01-16 11:32:08 -08:00
commit c1590325f2
10 changed files with 170 additions and 130 deletions

View File

@ -82,7 +82,7 @@ public class EC2ApiMetadata extends BaseRestApiMetadata {
public static Properties defaultProperties() {
Properties properties = BaseRestApiMetadata.defaultProperties();
properties.setProperty(PROPERTY_TIMEOUTS_PREFIX + "default", MINUTES.toMillis(3) + "");
properties.setProperty(PROPERTY_TIMEOUTS_PREFIX + "AMIClient.describeImagesInRegion", MINUTES.toMillis(5) + "");
properties.setProperty(PROPERTY_TIMEOUTS_PREFIX + "ec2:DescribeImages", MINUTES.toMillis(5) + "");
properties.setProperty(PROPERTY_AUTH_TAG, "AWS");
properties.setProperty(PROPERTY_HEADER_TAG, "amz");
properties.setProperty(PROPERTY_EC2_AMI_OWNERS, "*");

View File

@ -18,7 +18,6 @@
*/
package org.jclouds.s3;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.jclouds.Constants.PROPERTY_API_VERSION;
import static org.jclouds.Constants.PROPERTY_RELAX_HOSTNAME;
@ -86,10 +85,9 @@ public class S3ApiMetadata extends BaseRestApiMetadata {
Properties properties = BaseRestApiMetadata.defaultProperties();
properties.setProperty(PROPERTY_TIMEOUTS_PREFIX + "default", SECONDS.toMillis(90) + "");
// 512KB/s for max size of 5GB
properties.setProperty(PROPERTY_TIMEOUTS_PREFIX + "S3Client.getObject", SECONDS.toMillis(5242880 / 512) + "");
// 128KB/s for max size of 5GB
properties.setProperty(PROPERTY_TIMEOUTS_PREFIX + "S3Client.putObject", SECONDS.toMillis(5242880 / 128) + "");
properties.setProperty(PROPERTY_TIMEOUTS_PREFIX + "S3Client.copyObject", MINUTES.toMillis(10) + "");
properties.setProperty(PROPERTY_TIMEOUTS_PREFIX + "s3:GetObject", SECONDS.toMillis(5242880 / 512) + "");
// 128KB/s for max size of 5GB; applies also to copy object, upload part
properties.setProperty(PROPERTY_TIMEOUTS_PREFIX + "s3:PutObject", SECONDS.toMillis(5242880 / 128) + "");
properties.setProperty(PROPERTY_API_VERSION, S3AsyncClient.VERSION);
properties.setProperty(PROPERTY_AUTH_TAG, "AWS");
properties.setProperty(PROPERTY_HEADER_TAG, S3Headers.DEFAULT_AMAZON_HEADERTAG);

View File

@ -65,7 +65,7 @@ public class SQSApiMetadata extends BaseRestApiMetadata {
Properties properties = BaseRestApiMetadata.defaultProperties();
properties.setProperty(PROPERTY_TIMEOUTS_PREFIX + "default", SECONDS.toMillis(30) + "");
// this will gracefully attempt to resolve name issues
properties.setProperty(PROPERTY_TIMEOUTS_PREFIX + "QueueApi.create", SECONDS.toMillis(61) + "");
properties.setProperty(PROPERTY_TIMEOUTS_PREFIX + "sqs:CreateQueue", SECONDS.toMillis(61) + "");
properties.setProperty(CREATE_QUEUE_MAX_RETRIES, "60");
properties.setProperty(CREATE_QUEUE_RETRY_INTERVAL, "1000");
properties.setProperty(PROPERTY_AUTH_TAG, "AWS");

View File

@ -117,9 +117,18 @@ public class BlockOnFuture implements Function<ListenableFuture<?>, Object> {
// override timeout by values configured in properties(in ms)
private Optional<Long> timeoutInNanos(Invokable<?, ?> invoked, Map<String, Long> timeouts) {
String className = enclosingType.getRawType().getSimpleName();
Optional<Long> timeoutMillis = fromNullable(timeouts.get(className + "." + invoked.getName())).or(
fromNullable(timeouts.get(className))).or(fromNullable(timeouts.get("default")));
Optional<Long> defaultMillis = fromNullable(timeouts.get("default"));
Optional<Long> timeoutMillis;
if (invoked.isAnnotationPresent(Named.class)) {
String commandName = invoked.getAnnotation(Named.class).value();
timeoutMillis = fromNullable(timeouts.get(commandName)).or(defaultMillis);
} else {
// TODO: remove old logic, once Named annotations are present on all methods
String className = enclosingType.getRawType().getSimpleName().replace("AsyncClient", "Client")
.replace("AsyncApi", "Api");
timeoutMillis = fromNullable(timeouts.get(className + "." + invoked.getName())).or(
fromNullable(timeouts.get(className))).or(defaultMillis);
}
if (timeoutMillis.isPresent())
return Optional.of(TimeUnit.MILLISECONDS.toNanos(timeoutMillis.get()));
return Optional.absent();

View File

@ -101,11 +101,11 @@ public class InvokeHttpMethod<S, A> implements Function<Invocation, Object> {
if (isFuture(in.getInvokable())) {
return createFuture(in);
}
@SuppressWarnings("rawtypes")
Invokable async = checkNotNull(sync2AsyncInvokables.getIfPresent(in.getInvokable()), "invokable %s not in %s",
in.getInvokable(), sync2AsyncInvokables);
checkState(isFuture(async), "not a future: %s", async);
return blocker.create(enclosingType, in).apply(createFuture(Invocation.create(async, in.getArgs())));
Invocation async = Invocation.create(
checkNotNull(sync2AsyncInvokables.getIfPresent(in.getInvokable()), "invokable %s not in %s",
in.getInvokable(), sync2AsyncInvokables), in.getArgs());
checkState(isFuture(async.getInvokable()), "not a future: %s", async);
return blocker.create(enclosingType, async).apply(createFuture(async));
}
private boolean isFuture(Invokable<?, ?> in) {

View File

@ -1,107 +0,0 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.concurrent.internal;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.testng.Assert.assertEquals;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableMap;
import com.google.common.util.concurrent.ListenableFuture;
/**
* Tests behavior of ListenableFutureExceptionParser
*
* @author Adrian Cole
*/
@Test(groups = "unit", enabled = false, singleThreaded = true)
public class SyncProxyTest {
static ListenableFuture<String> future;
@SuppressWarnings("unchecked")
@BeforeMethod
void createMockedFuture() throws InterruptedException, ExecutionException, TimeoutException {
future = createMock(ListenableFuture.class);
expect(future.get(250000000, TimeUnit.NANOSECONDS)).andReturn("foo");
replay(future);
}
public static class Async {
public ListenableFuture<String> get() {
return future;
}
}
private static interface Sync {
String get();
}
public void testWithDefaultPropTimeout() throws Exception {
Sync withOverride = syncProxyForTimeouts(ImmutableMap.of("default", 250L));
assertEquals(withOverride.get(), "foo");
verify(future);
}
public void testWithClassPropTimeout() throws Exception {
Sync withOverride = syncProxyForTimeouts(ImmutableMap.of("default", 50L, "Sync", 250L));
assertEquals(withOverride.get(), "foo");
verify(future);
}
public void testWithMethodPropTimeout() throws Exception {
Sync withOverride = syncProxyForTimeouts(ImmutableMap.of("default", 50L, "Sync", 100L, "Sync.get", 250L));
assertEquals(withOverride.get(), "foo");
verify(future);
}
@SuppressWarnings("unchecked")
public void testWithMethodWithNoTimeoutsCallGetDirectly() throws Exception {
future = createMock(ListenableFuture.class);
expect(future.get()).andReturn("foo");
replay(future);
Sync noOverrides = syncProxyForTimeouts(ImmutableMap.<String, Long> of());
assertEquals(noOverrides.get(), "foo");
verify(future);
}
private Sync syncProxyForTimeouts(ImmutableMap<String, Long> timeouts) throws NoSuchMethodException {
// LoadingCache<ForwardInvocationToInterface, Object> cache = CacheBuilder.newBuilder().build(
// CacheLoader.from(Functions.<Object> constant(null)));
// return FunctionalReflection.newProxy(Sync.class, new SyncProxy(new AlwaysPresentImplicitOptionalConverter(),
// cache, ImmutableMap.<Class<?>, Class<?>> of(Sync.class, Async.class), timeouts, Sync.class, new Async()));
////
// Function<InvocationSuccess, Optional<Object>> optionalConverter, SyncProxy.Factory factory,
// AsyncRestClientProxy.Caller.Factory asyncFactory, Map<Class<?>, Class<?>> sync2Async,
// @Named("TIMEOUTS") Map<String, Long> timeouts, @Assisted Class<?> declaring, @Assisted Object async
return null;
}
}

View File

@ -0,0 +1,145 @@
/**
* Licensed to jclouds, Inc. (jclouds) under one or more
* contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. jclouds licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.jclouds.rest.internal;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.replay;
import static org.easymock.EasyMock.verify;
import static org.testng.Assert.assertEquals;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import javax.inject.Named;
import org.jclouds.reflect.Invocation;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.reflect.TypeToken;
import com.google.common.util.concurrent.ListenableFuture;
/**
*
* @author Adrian Cole
*/
@Test(groups = "unit", singleThreaded = true)
public class BlockOnFutureTest {
static ListenableFuture<String> future;
public static class ThingAsyncApi {
public ListenableFuture<String> get() {
return future;
}
@Named("ns:get")
public ListenableFuture<String> namedGet() {
return future;
}
}
private TypeToken<ThingAsyncApi> enclosingType;
private Invocation get;
private Invocation namedGet;
@BeforeClass
void setupInvocations() throws SecurityException, NoSuchMethodException {
enclosingType = TypeToken.of(ThingAsyncApi.class);
get = Invocation.create(enclosingType.method(ThingAsyncApi.class.getDeclaredMethod("get")), ImmutableList.of());
namedGet = Invocation.create(enclosingType.method(ThingAsyncApi.class.getDeclaredMethod("namedGet")),
ImmutableList.of());
}
@SuppressWarnings("unchecked")
@BeforeMethod
void createMockedFuture() throws InterruptedException, ExecutionException, TimeoutException {
future = createMock(ListenableFuture.class);
expect(future.get(250000000, TimeUnit.NANOSECONDS)).andReturn("foo");
replay(future);
}
public void testUnnamedMethodWithDefaultPropTimeout() throws Exception {
Function<ListenableFuture<?>, Object> withOverride = new BlockOnFuture(ImmutableMap.of("default", 250L),
enclosingType, get);
assertEquals(withOverride.apply(future), "foo");
verify(future);
}
public void testUnnamedMethodWithClassPropTimeout() throws Exception {
Function<ListenableFuture<?>, Object> withOverride = new BlockOnFuture(ImmutableMap.of("default", 50L,
"ThingApi", 250L), enclosingType, get);
assertEquals(withOverride.apply(future), "foo");
verify(future);
}
public void testUnnamedMethodWithMethodPropTimeout() throws Exception {
Function<ListenableFuture<?>, Object> withOverride = new BlockOnFuture(ImmutableMap.of("default", 50L,
"ThingApi", 100L, "ThingApi.get", 250L), enclosingType, get);
assertEquals(withOverride.apply(future), "foo");
verify(future);
}
@SuppressWarnings("unchecked")
public void testUnnamedMethodWithNoTimeoutsCallGetDirectly() throws Exception {
future = createMock(ListenableFuture.class);
expect(future.get()).andReturn("foo");
replay(future);
Function<ListenableFuture<?>, Object> noOverrides = new BlockOnFuture(ImmutableMap.<String, Long> of(),
enclosingType, get);
assertEquals(noOverrides.apply(future), "foo");
verify(future);
}
public void testNamedMethodWithDefaultPropTimeout() throws Exception {
Function<ListenableFuture<?>, Object> withOverride = new BlockOnFuture(ImmutableMap.of("default", 250L),
enclosingType, namedGet);
assertEquals(withOverride.apply(future), "foo");
verify(future);
}
public void testNamedMethodWithMethodPropTimeout() throws Exception {
Function<ListenableFuture<?>, Object> withOverride = new BlockOnFuture(ImmutableMap.of("default", 50L,
"ThingApi", 100L, "ns:get", 250L), enclosingType, namedGet);
assertEquals(withOverride.apply(future), "foo");
verify(future);
}
@SuppressWarnings("unchecked")
public void testNamedMethodWithNoTimeoutsCallGetDirectly() throws Exception {
future = createMock(ListenableFuture.class);
expect(future.get()).andReturn("foo");
replay(future);
Function<ListenableFuture<?>, Object> noOverrides = new BlockOnFuture(ImmutableMap.<String, Long> of(),
enclosingType, namedGet);
assertEquals(noOverrides.apply(future), "foo");
verify(future);
}
}

View File

@ -57,7 +57,7 @@ public class CloudStackEC2ApiMetadata extends EC2ApiMetadata {
public static Properties defaultProperties() {
Properties properties = EC2ApiMetadata.defaultProperties();
properties.setProperty(PROPERTY_TIMEOUTS_PREFIX + "AMIClient.describeImagesInRegion", MINUTES.toMillis(15) + "");
properties.setProperty(PROPERTY_TIMEOUTS_PREFIX + "ec2:DescribeImages", MINUTES.toMillis(15) + "");
return properties;
}

View File

@ -64,8 +64,8 @@ public class AWSEC2ApiMetadata extends EC2ApiMetadata {
public static Properties defaultProperties() {
Properties properties = EC2ApiMetadata.defaultProperties();
properties.setProperty(PROPERTY_TIMEOUTS_PREFIX + "default", SECONDS.toMillis(90) + "");
properties.setProperty(PROPERTY_TIMEOUTS_PREFIX + "AWSAMIClient.describeImagesInRegion", MINUTES.toMillis(5) + "");
properties.setProperty(PROPERTY_TIMEOUTS_PREFIX + "SpotInstanceClient.describeSpotPriceHistoryInRegion", MINUTES.toMillis(2) + "");
properties.setProperty(PROPERTY_TIMEOUTS_PREFIX + "ec2:DescribeImages", MINUTES.toMillis(5) + "");
properties.setProperty(PROPERTY_TIMEOUTS_PREFIX + "ec2:DescribeSpotInstanceRequests", MINUTES.toMillis(2) + "");
properties.remove(PROPERTY_EC2_AMI_OWNERS);
// auth fail sometimes happens in EC2, as the rc.local script that injects the
// authorized key executes after ssh has started.

View File

@ -18,9 +18,6 @@
*/
package org.jclouds.aws.s3;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.jclouds.Constants.PROPERTY_TIMEOUTS_PREFIX;
import java.util.Properties;
import org.jclouds.apis.ApiMetadata;
@ -60,8 +57,6 @@ public class AWSS3ApiMetadata extends S3ApiMetadata {
public static Properties defaultProperties() {
Properties properties = S3ApiMetadata.defaultProperties();
// 128KB/s for max size of 5GB
properties.setProperty(PROPERTY_TIMEOUTS_PREFIX + "AWSS3Client.uploadPart", SECONDS.toMillis(5242880 / 128) + "");
return properties;
}