Issue 731: unravel dependency cycle to only use interfaces

This commit is contained in:
Adrian Cole 2012-01-15 11:30:22 -08:00
parent de1e8a7ce3
commit 63320f15b6
6 changed files with 256 additions and 59 deletions

View File

@ -30,6 +30,7 @@ import org.jclouds.cloudloadbalancers.CloudLoadBalancersClient;
import org.jclouds.cloudloadbalancers.config.CloudLoadBalancersRestClientModule; import org.jclouds.cloudloadbalancers.config.CloudLoadBalancersRestClientModule;
import org.jclouds.cloudloadbalancers.functions.ConvertLB; import org.jclouds.cloudloadbalancers.functions.ConvertLB;
import org.jclouds.cloudloadbalancers.reference.Region; import org.jclouds.cloudloadbalancers.reference.Region;
import org.jclouds.domain.Credentials;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.RequiresHttp; import org.jclouds.http.RequiresHttp;
import org.jclouds.internal.ClassMethodArgs; import org.jclouds.internal.ClassMethodArgs;
@ -78,7 +79,7 @@ public abstract class BaseCloudLoadBalancersAsyncClientTest<T> extends RestClien
install(new OpenStackAuthenticationModule() { install(new OpenStackAuthenticationModule() {
@Override @Override
protected Supplier<AuthenticationResponse> provideAuthenticationResponseSupplier( protected Supplier<AuthenticationResponse> provideAuthenticationResponseSupplier(
final LoadingCache<String,AuthenticationResponse> cache) { LoadingCache<Credentials, AuthenticationResponse> cache, Credentials in) {
return Suppliers.ofInstance(new AuthenticationResponse("token", ImmutableMap.<String, URI> of())); return Suppliers.ofInstance(new AuthenticationResponse("token", ImmutableMap.<String, URI> of()));
} }
}); });

View File

@ -29,14 +29,17 @@ import javax.inject.Named;
import javax.inject.Singleton; import javax.inject.Singleton;
import org.jclouds.Constants; import org.jclouds.Constants;
import org.jclouds.concurrent.RetryOnTimeOutExceptionSupplier; import org.jclouds.concurrent.RetryOnTimeOutExceptionFunction;
import org.jclouds.date.TimeStamp; import org.jclouds.date.TimeStamp;
import org.jclouds.domain.Credentials;
import org.jclouds.http.RequiresHttp; import org.jclouds.http.RequiresHttp;
import org.jclouds.location.Provider;
import org.jclouds.openstack.Authentication; import org.jclouds.openstack.Authentication;
import org.jclouds.openstack.OpenStackAuthAsyncClient; import org.jclouds.openstack.OpenStackAuthAsyncClient;
import org.jclouds.openstack.OpenStackAuthAsyncClient.AuthenticationResponse; import org.jclouds.openstack.OpenStackAuthAsyncClient.AuthenticationResponse;
import org.jclouds.rest.AsyncClientFactory; import org.jclouds.rest.AsyncClientFactory;
import com.google.common.base.Function;
import com.google.common.base.Supplier; import com.google.common.base.Supplier;
import com.google.common.base.Suppliers; import com.google.common.base.Suppliers;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
@ -46,6 +49,7 @@ import com.google.common.cache.LoadingCache;
import com.google.common.util.concurrent.UncheckedExecutionException; import com.google.common.util.concurrent.UncheckedExecutionException;
import com.google.inject.AbstractModule; import com.google.inject.AbstractModule;
import com.google.inject.Provides; import com.google.inject.Provides;
import com.google.inject.TypeLiteral;
/** /**
* Configures the Rackspace authentication service connection, including logging and http transport. * Configures the Rackspace authentication service connection, including logging and http transport.
@ -57,6 +61,8 @@ public class OpenStackAuthenticationModule extends AbstractModule {
@Override @Override
protected void configure() { protected void configure() {
bind(new TypeLiteral<Function<Credentials, AuthenticationResponse>>() {
}).to(GetAuthenticationResponse.class);
} }
/** /**
@ -74,68 +80,57 @@ public class OpenStackAuthenticationModule extends AbstractModule {
}; };
} }
@Provides
@Provider
protected Credentials provideAuthenticationCredentials(@Named(Constants.PROPERTY_IDENTITY) String user,
@Named(Constants.PROPERTY_CREDENTIAL) String key) {
return new Credentials(user, key);
}
@Singleton @Singleton
public static class GetAuthenticationResponse implements Supplier<AuthenticationResponse> { public static class GetAuthenticationResponse extends
protected final OpenStackAuthAsyncClient client; RetryOnTimeOutExceptionFunction<Credentials, AuthenticationResponse> {
protected final String user;
protected final String key;
@Inject @Inject
public GetAuthenticationResponse(AsyncClientFactory factory, @Named(Constants.PROPERTY_IDENTITY) String user, public GetAuthenticationResponse(final AsyncClientFactory factory) {
@Named(Constants.PROPERTY_CREDENTIAL) String key) { super(new Function<Credentials, AuthenticationResponse>() {
this.client = factory.create(OpenStackAuthAsyncClient.class);
this.user = user;
this.key = key;
}
@Override @Override
public AuthenticationResponse get() { public AuthenticationResponse apply(Credentials input) {
try { try {
Future<AuthenticationResponse> response = authenticate(); Future<AuthenticationResponse> response = factory.create(OpenStackAuthAsyncClient.class)
return response.get(30, TimeUnit.SECONDS); .authenticate(input.identity, input.credential);
} catch (Exception e) { return response.get(30, TimeUnit.SECONDS);
Throwables.propagate(e); } catch (Exception e) {
assert false : e; throw Throwables.propagate(e);
return null; }
} }
} });
protected Future<AuthenticationResponse> authenticate() {
return client.authenticate(user, key);
} }
} }
@Provides @Provides
@Singleton @Singleton
public LoadingCache<String,AuthenticationResponse> provideAuthenticationResponseCache2( public LoadingCache<Credentials, AuthenticationResponse> provideAuthenticationResponseCache2(
final GetAuthenticationResponse getAuthenticationResponse) { final Function<Credentials, AuthenticationResponse> getAuthenticationResponse,
@Provider final Credentials creds) {
final RetryOnTimeOutExceptionSupplier<AuthenticationResponse> delegate =
new RetryOnTimeOutExceptionSupplier<AuthenticationResponse>(getAuthenticationResponse); LoadingCache<Credentials, AuthenticationResponse> cache = CacheBuilder.newBuilder().expireAfterWrite(23,
TimeUnit.HOURS).build(CacheLoader.from(getAuthenticationResponse));
CacheLoader<String, AuthenticationResponse> cacheLoader = new CacheLoader<String, AuthenticationResponse>() {
@Override
public AuthenticationResponse load(String key) throws Exception {
return delegate.get();
}
};
LoadingCache<String, AuthenticationResponse> cache = CacheBuilder.newBuilder().expireAfterWrite(23, TimeUnit.HOURS)
.build(cacheLoader);
return cache; return cache;
} }
@Provides @Provides
@Singleton @Singleton
protected Supplier<AuthenticationResponse> provideAuthenticationResponseSupplier( protected Supplier<AuthenticationResponse> provideAuthenticationResponseSupplier(
final LoadingCache<String,AuthenticationResponse> cache) { final LoadingCache<Credentials, AuthenticationResponse> cache, @Provider final Credentials creds) {
return new Supplier<AuthenticationResponse>() { return new Supplier<AuthenticationResponse>() {
@Override @Override
public AuthenticationResponse get() { public AuthenticationResponse get() {
try { try {
return cache.get("key"); return cache.get(creds);
} catch (UncheckedExecutionException e) { } catch (UncheckedExecutionException e) {
throw Throwables.propagate(e.getCause()); throw Throwables.propagate(e.getCause());
} catch (ExecutionException e) { } catch (ExecutionException e) {

View File

@ -24,6 +24,7 @@ import java.io.IOException;
import javax.annotation.Resource; import javax.annotation.Resource;
import org.jclouds.domain.Credentials;
import org.jclouds.http.HttpCommand; import org.jclouds.http.HttpCommand;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
import org.jclouds.http.HttpRetryHandler; import org.jclouds.http.HttpRetryHandler;
@ -48,12 +49,8 @@ public class RetryOnRenew implements HttpRetryHandler {
@Resource @Resource
protected Logger logger = Logger.NULL; protected Logger logger = Logger.NULL;
// This doesn't work yet
// @Inject
// Supplier<AuthenticationResponse> providedAuthenticationResponseCache;
@Inject @Inject
LoadingCache<String,AuthenticationResponse> authenticationResponseCache; LoadingCache<Credentials, AuthenticationResponse> authenticationResponseCache;
@Override @Override
public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) { public boolean shouldRetryRequest(HttpCommand command, HttpResponse response) {
@ -63,8 +60,8 @@ public class RetryOnRenew implements HttpRetryHandler {
case 401: case 401:
// Do not retry on 401 from authentication request // Do not retry on 401 from authentication request
Multimap<String, String> headers = command.getCurrentRequest().getHeaders(); Multimap<String, String> headers = command.getCurrentRequest().getHeaders();
if (headers != null && headers.containsKey(AuthHeaders.AUTH_USER) && headers.containsKey(AuthHeaders.AUTH_KEY) && if (headers != null && headers.containsKey(AuthHeaders.AUTH_USER)
!headers.containsKey(AuthHeaders.AUTH_TOKEN)) { && headers.containsKey(AuthHeaders.AUTH_KEY) && !headers.containsKey(AuthHeaders.AUTH_TOKEN)) {
retry = false; retry = false;
} else { } else {
String content = parsePayloadOrNull(response); String content = parsePayloadOrNull(response);
@ -79,12 +76,12 @@ public class RetryOnRenew implements HttpRetryHandler {
break; break;
} }
return retry; return retry;
} finally { } finally {
releasePayload(response); releasePayload(response);
} }
} }
String parsePayloadOrNull(HttpResponse response) { String parsePayloadOrNull(HttpResponse response) {
if (response.getPayload() != null) { if (response.getPayload() != null) {
try { try {

View File

@ -18,13 +18,14 @@
*/ */
package org.jclouds.openstack.handlers; package org.jclouds.openstack.handlers;
import static org.easymock.EasyMock.createMock;
import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.expect;
import static org.easymock.EasyMock.expectLastCall; import static org.easymock.EasyMock.expectLastCall;
import static org.easymock.classextension.EasyMock.createMock; import static org.easymock.EasyMock.replay;
import static org.easymock.classextension.EasyMock.replay; import static org.easymock.EasyMock.verify;
import static org.easymock.classextension.EasyMock.verify;
import static org.testng.Assert.assertTrue; import static org.testng.Assert.assertTrue;
import org.jclouds.domain.Credentials;
import org.jclouds.http.HttpCommand; import org.jclouds.http.HttpCommand;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
@ -47,23 +48,23 @@ public class RetryOnRenewTest {
HttpRequest request = createMock(HttpRequest.class); HttpRequest request = createMock(HttpRequest.class);
HttpResponse response = createMock(HttpResponse.class); HttpResponse response = createMock(HttpResponse.class);
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
LoadingCache<String,AuthenticationResponse> cache = createMock(LoadingCache.class); LoadingCache<Credentials, AuthenticationResponse> cache = createMock(LoadingCache.class);
expect(command.getCurrentRequest()).andReturn(request); expect(command.getCurrentRequest()).andReturn(request);
cache.invalidateAll(); cache.invalidateAll();
expectLastCall(); expectLastCall();
expect(response.getPayload()).andReturn(Payloads.newStringPayload("token expired, please renew")).anyTimes(); expect(response.getPayload()).andReturn(Payloads.newStringPayload("token expired, please renew")).anyTimes();
expect(response.getStatusCode()).andReturn(401).atLeastOnce(); expect(response.getStatusCode()).andReturn(401).atLeastOnce();
replay(command); replay(command);
replay(response); replay(response);
replay(cache); replay(cache);
RetryOnRenew retry = new RetryOnRenew(); RetryOnRenew retry = new RetryOnRenew();
retry.authenticationResponseCache = cache; retry.authenticationResponseCache = cache;
assertTrue(retry.shouldRetryRequest(command, response)); assertTrue(retry.shouldRetryRequest(command, response));
verify(command); verify(command);

View File

@ -0,0 +1,77 @@
/**
* 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;
import static com.google.common.base.Preconditions.checkNotNull;
import static com.google.common.base.Throwables.propagate;
import java.util.concurrent.TimeoutException;
import org.jclouds.util.Throwables2;
import com.google.common.base.Function;
import com.google.common.collect.ForwardingObject;
/**
*
* @author Adrian Cole
*/
public class RetryOnTimeOutExceptionFunction<K,V> extends ForwardingObject implements Function<K,V>{
private final Function<K,V> delegate;
public RetryOnTimeOutExceptionFunction(Function<K,V> delegate) {
this.delegate = checkNotNull(delegate, "delegate");
}
//TODO: backoff limited retry handler
@Override
public V apply(K key) {
TimeoutException ex = null;
for (int i = 0; i < 3; i++) {
try {
ex = null;
return delegate().apply(key);
} catch (Exception e) {
if ((ex = Throwables2.getFirstThrowableOfType(e, TimeoutException.class)) != null)
continue;
throw propagate(e);
}
}
if (ex != null)
throw propagate(ex);
assert false;
return null;
}
@Override
public boolean equals(Object obj) {
return delegate.equals(obj);
}
@Override
public int hashCode() {
return delegate.hashCode();
}
@Override
protected Function<K,V> delegate() {
return delegate;
}
}

View File

@ -0,0 +1,126 @@
/**
* 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;
import static org.easymock.EasyMock.expect;
import static org.easymock.classextension.EasyMock.createMock;
import static org.easymock.classextension.EasyMock.replay;
import static org.easymock.classextension.EasyMock.verify;
import static org.testng.Assert.assertEquals;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeoutException;
import org.jclouds.rest.AuthorizationException;
import org.testng.annotations.Test;
import com.google.common.base.Function;
/**
* Tests behavior of RetryOnTimeOutExceptionFunction
*
* @author Adrian Cole
*/
@Test(groups = "unit", singleThreaded = true, testName = "RetryOnTimeOutExceptionFunctionTest")
public class RetryOnTimeOutExceptionFunctionTest {
ExecutorService executorService = MoreExecutors.sameThreadExecutor();
@SuppressWarnings("unchecked")
@Test
public void testGetThrowsOriginalExceptionButRetriesOnTimeoutException() throws InterruptedException, ExecutionException {
Function<String, String> delegate = createMock(Function.class);
TimeoutException timeout = createMock(TimeoutException.class);
RuntimeException throwable = new RuntimeException(timeout);
expect(delegate.apply("baz")).andThrow(throwable);
expect(timeout.getCause()).andReturn(null).anyTimes();
expect(delegate.apply("baz")).andThrow(throwable);
expect(delegate.apply("baz")).andThrow(throwable);
replay(delegate);
replay(timeout);
RetryOnTimeOutExceptionFunction<String, String> supplier = new RetryOnTimeOutExceptionFunction<String, String>(
delegate);
try {
supplier.apply("baz");
assert false;
} catch (RuntimeException e) {
assertEquals(e.getCause(), timeout);
}
verify(delegate);
verify(timeout);
}
@SuppressWarnings("unchecked")
@Test
public void testGetAllowsTwoFailuresOnTimeoutException() throws InterruptedException, ExecutionException {
Function<String, String> delegate = createMock(Function.class);
TimeoutException timeout = createMock(TimeoutException.class);
RuntimeException throwable = new RuntimeException(timeout);
expect(delegate.apply("baz")).andThrow(throwable);
expect(timeout.getCause()).andReturn(null).anyTimes();
expect(delegate.apply("baz")).andThrow(throwable);
expect(delegate.apply("baz")).andReturn("foo");
replay(delegate);
replay(timeout);
RetryOnTimeOutExceptionFunction<String, String> supplier = new RetryOnTimeOutExceptionFunction<String, String>(
delegate);
assertEquals(supplier.apply("baz"), "foo");
verify(delegate);
verify(timeout);
}
@SuppressWarnings("unchecked")
@Test
public void testGetAllowsNoFailuresOnOtherExceptions() throws InterruptedException, ExecutionException {
Function<String, String> delegate = createMock(Function.class);
AuthorizationException auth = createMock(AuthorizationException.class);
RuntimeException throwable = new RuntimeException(auth);
expect(delegate.apply("baz")).andThrow(throwable);
expect(auth.getCause()).andReturn(null).anyTimes();
replay(delegate);
replay(auth);
RetryOnTimeOutExceptionFunction<String, String> supplier = new RetryOnTimeOutExceptionFunction<String, String>(
delegate);
try {
supplier.apply("baz");
assert false;
} catch (RuntimeException e) {
assertEquals(e.getCause(), auth);
}
verify(delegate);
verify(auth);
}
}