mirror of https://github.com/apache/jclouds.git
Session renew should fail after a limited number of failed attempts
This commit is contained in:
parent
068647955a
commit
4a5cd6539e
|
@ -122,7 +122,7 @@ import org.jclouds.cloudstack.filters.AuthenticationFilter;
|
||||||
import org.jclouds.cloudstack.filters.QuerySigner;
|
import org.jclouds.cloudstack.filters.QuerySigner;
|
||||||
import org.jclouds.cloudstack.functions.LoginWithPasswordCredentials;
|
import org.jclouds.cloudstack.functions.LoginWithPasswordCredentials;
|
||||||
import org.jclouds.cloudstack.handlers.CloudStackErrorHandler;
|
import org.jclouds.cloudstack.handlers.CloudStackErrorHandler;
|
||||||
import org.jclouds.cloudstack.handlers.RetryOnRenewAndLogoutOnClose;
|
import org.jclouds.cloudstack.handlers.InvalidateSessionAndRetryOn401AndLogoutOnClose;
|
||||||
import org.jclouds.concurrent.RetryOnTimeOutExceptionFunction;
|
import org.jclouds.concurrent.RetryOnTimeOutExceptionFunction;
|
||||||
import org.jclouds.domain.Credentials;
|
import org.jclouds.domain.Credentials;
|
||||||
import org.jclouds.http.HttpErrorHandler;
|
import org.jclouds.http.HttpErrorHandler;
|
||||||
|
@ -242,7 +242,7 @@ public class CloudStackRestClientModule extends RestClientModule<CloudStackClien
|
||||||
|
|
||||||
// session client is used directly for filters and retry handlers, so let's bind it explicitly
|
// session client is used directly for filters and retry handlers, so let's bind it explicitly
|
||||||
bindClientAndAsyncClient(binder(), SessionClient.class, SessionAsyncClient.class);
|
bindClientAndAsyncClient(binder(), SessionClient.class, SessionAsyncClient.class);
|
||||||
bind(HttpRetryHandler.class).annotatedWith(ClientError.class).to(RetryOnRenewAndLogoutOnClose.class);
|
bind(HttpRetryHandler.class).annotatedWith(ClientError.class).to(InvalidateSessionAndRetryOn401AndLogoutOnClose.class);
|
||||||
|
|
||||||
super.configure();
|
super.configure();
|
||||||
}
|
}
|
||||||
|
|
|
@ -26,7 +26,7 @@ import org.jclouds.cloudstack.features.SessionClient;
|
||||||
import org.jclouds.domain.Credentials;
|
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.handlers.BackoffLimitedRetryHandler;
|
||||||
import org.jclouds.logging.Logger;
|
import org.jclouds.logging.Logger;
|
||||||
|
|
||||||
import javax.annotation.PreDestroy;
|
import javax.annotation.PreDestroy;
|
||||||
|
@ -38,10 +38,9 @@ import static org.jclouds.http.HttpUtils.releasePayload;
|
||||||
* This will parse and set an appropriate exception on the command object.
|
* This will parse and set an appropriate exception on the command object.
|
||||||
*
|
*
|
||||||
* @author Adrian Cole
|
* @author Adrian Cole
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
@Singleton
|
@Singleton
|
||||||
public class RetryOnRenewAndLogoutOnClose implements HttpRetryHandler {
|
public class InvalidateSessionAndRetryOn401AndLogoutOnClose extends BackoffLimitedRetryHandler {
|
||||||
@Resource
|
@Resource
|
||||||
protected Logger logger = Logger.NULL;
|
protected Logger logger = Logger.NULL;
|
||||||
|
|
||||||
|
@ -49,7 +48,7 @@ public class RetryOnRenewAndLogoutOnClose implements HttpRetryHandler {
|
||||||
private final SessionClient sessionClient;
|
private final SessionClient sessionClient;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
protected RetryOnRenewAndLogoutOnClose(LoadingCache<Credentials, LoginResponse> authenticationResponseCache,
|
protected InvalidateSessionAndRetryOn401AndLogoutOnClose(LoadingCache<Credentials, LoginResponse> authenticationResponseCache,
|
||||||
SessionClient sessionClient) {
|
SessionClient sessionClient) {
|
||||||
this.authenticationResponseCache = authenticationResponseCache;
|
this.authenticationResponseCache = authenticationResponseCache;
|
||||||
this.sessionClient = sessionClient;
|
this.sessionClient = sessionClient;
|
||||||
|
@ -61,7 +60,7 @@ public class RetryOnRenewAndLogoutOnClose implements HttpRetryHandler {
|
||||||
switch (response.getStatusCode()) {
|
switch (response.getStatusCode()) {
|
||||||
case 401:
|
case 401:
|
||||||
authenticationResponseCache.invalidateAll();
|
authenticationResponseCache.invalidateAll();
|
||||||
return true;
|
return super.shouldRetryRequest(command, response);
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|
|
@ -18,14 +18,7 @@
|
||||||
*/
|
*/
|
||||||
package org.jclouds.cloudstack.handlers;
|
package org.jclouds.cloudstack.handlers;
|
||||||
|
|
||||||
import static org.easymock.EasyMock.expect;
|
import com.google.inject.Guice;
|
||||||
import static org.easymock.EasyMock.reportMatcher;
|
|
||||||
import static org.easymock.classextension.EasyMock.createMock;
|
|
||||||
import static org.easymock.classextension.EasyMock.replay;
|
|
||||||
import static org.easymock.classextension.EasyMock.verify;
|
|
||||||
|
|
||||||
import java.net.URI;
|
|
||||||
|
|
||||||
import org.easymock.IArgumentMatcher;
|
import org.easymock.IArgumentMatcher;
|
||||||
import org.jclouds.http.HttpCommand;
|
import org.jclouds.http.HttpCommand;
|
||||||
import org.jclouds.http.HttpRequest;
|
import org.jclouds.http.HttpRequest;
|
||||||
|
@ -36,10 +29,15 @@ import org.jclouds.rest.ResourceNotFoundException;
|
||||||
import org.jclouds.util.Strings2;
|
import org.jclouds.util.Strings2;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
import com.google.inject.Guice;
|
import java.net.URI;
|
||||||
|
|
||||||
|
import static org.easymock.EasyMock.expect;
|
||||||
|
import static org.easymock.EasyMock.reportMatcher;
|
||||||
|
import static org.easymock.classextension.EasyMock.createMock;
|
||||||
|
import static org.easymock.classextension.EasyMock.replay;
|
||||||
|
import static org.easymock.classextension.EasyMock.verify;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @author Adrian Cole
|
* @author Adrian Cole
|
||||||
*/
|
*/
|
||||||
@Test(groups = {"unit"})
|
@Test(groups = {"unit"})
|
||||||
|
@ -91,7 +89,14 @@ public class CloudStackErrorHandlerTest {
|
||||||
assertCodeMakes("GET", URI.create("https://cloudstack.com/foo"), 409, "", "Conflict", IllegalStateException.class);
|
assertCodeMakes("GET", URI.create("https://cloudstack.com/foo"), 409, "", "Conflict", IllegalStateException.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test void test537MakesIllegalStateException() {
|
@Test
|
||||||
|
public void test531MakesAuthorizationException() {
|
||||||
|
assertCodeMakes("GET", URI.create("https://cloudstack.com/foo"), 531, "", "Unauthoized",
|
||||||
|
AuthorizationException.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void test537MakesIllegalStateException() {
|
||||||
assertCodeMakes(
|
assertCodeMakes(
|
||||||
"GET",
|
"GET",
|
||||||
URI.create("http://10.26.26.155:8080/client/api?response=json&command=createIpForwardingRule&ipaddressid=37&startport=22&protocol=tcp"),
|
URI.create("http://10.26.26.155:8080/client/api?response=json&command=createIpForwardingRule&ipaddressid=37&startport=22&protocol=tcp"),
|
||||||
|
|
|
@ -19,6 +19,7 @@
|
||||||
package org.jclouds.cloudstack.handlers;
|
package org.jclouds.cloudstack.handlers;
|
||||||
|
|
||||||
import com.google.common.cache.LoadingCache;
|
import com.google.common.cache.LoadingCache;
|
||||||
|
import org.easymock.IAnswer;
|
||||||
import org.jclouds.cloudstack.domain.LoginResponse;
|
import org.jclouds.cloudstack.domain.LoginResponse;
|
||||||
import org.jclouds.cloudstack.features.SessionClient;
|
import org.jclouds.cloudstack.features.SessionClient;
|
||||||
import org.jclouds.domain.Credentials;
|
import org.jclouds.domain.Credentials;
|
||||||
|
@ -27,37 +28,61 @@ import org.jclouds.http.HttpResponse;
|
||||||
import org.jclouds.io.Payloads;
|
import org.jclouds.io.Payloads;
|
||||||
import org.testng.annotations.Test;
|
import org.testng.annotations.Test;
|
||||||
|
|
||||||
|
import java.util.concurrent.atomic.AtomicInteger;
|
||||||
|
|
||||||
import static org.easymock.EasyMock.createMock;
|
import static org.easymock.EasyMock.createMock;
|
||||||
|
import static org.easymock.EasyMock.expect;
|
||||||
import static org.easymock.EasyMock.expectLastCall;
|
import static org.easymock.EasyMock.expectLastCall;
|
||||||
import static org.easymock.EasyMock.replay;
|
import static org.easymock.EasyMock.replay;
|
||||||
import static org.easymock.EasyMock.verify;
|
import static org.easymock.EasyMock.verify;
|
||||||
|
import static org.testng.Assert.assertFalse;
|
||||||
import static org.testng.Assert.assertTrue;
|
import static org.testng.Assert.assertTrue;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests behavior of {@code RetryOnRenewAndLogoutOnClose} handler
|
* Tests behavior of {@code InvalidateSessionAndRetryOn401AndLogoutOnClose} handler
|
||||||
*
|
*
|
||||||
* @author grkvlt@apache.org
|
* @author grkvlt@apache.org
|
||||||
*/
|
*/
|
||||||
@Test(groups = "unit", testName = "RetryOnRenewAndLogoutOnCloseTest")
|
@Test(groups = "unit", testName = "InvalidateSessionAndRetryOn401AndLogoutOnCloseTest")
|
||||||
public class RetryOnRenewAndLogoutOnCloseTest {
|
public class InvalidateSessionAndRetryOn401AndLogoutOnCloseTest {
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
@Test
|
@Test
|
||||||
public void test401ShouldRetry() {
|
public void test401ShouldRetryAndFailAfterFiveAttempts() {
|
||||||
HttpCommand command = createMock(HttpCommand.class);
|
HttpCommand command = createMock(HttpCommand.class);
|
||||||
SessionClient sessionClient = createMock(SessionClient.class);
|
SessionClient sessionClient = createMock(SessionClient.class);
|
||||||
LoadingCache<Credentials, LoginResponse> cache = createMock(LoadingCache.class);
|
LoadingCache<Credentials, LoginResponse> cache = createMock(LoadingCache.class);
|
||||||
|
|
||||||
cache.invalidateAll();
|
cache.invalidateAll();
|
||||||
expectLastCall();
|
expectLastCall().anyTimes();
|
||||||
|
|
||||||
|
final AtomicInteger counter = new AtomicInteger();
|
||||||
|
expect(command.incrementFailureCount()).andAnswer(new IAnswer<Integer>() {
|
||||||
|
@Override
|
||||||
|
public Integer answer() throws Throwable {
|
||||||
|
return counter.incrementAndGet();
|
||||||
|
}
|
||||||
|
}).anyTimes();
|
||||||
|
expect(command.isReplayable()).andReturn(true).anyTimes();
|
||||||
|
expect(command.getFailureCount()).andAnswer(new IAnswer<Integer>() {
|
||||||
|
@Override
|
||||||
|
public Integer answer() throws Throwable {
|
||||||
|
return counter.get();
|
||||||
|
}
|
||||||
|
}).anyTimes();
|
||||||
|
|
||||||
replay(cache, command);
|
replay(cache, command);
|
||||||
|
|
||||||
HttpResponse response = HttpResponse.builder().payload(
|
HttpResponse response = HttpResponse.builder().payload(
|
||||||
Payloads.newStringPayload("Not relevant")).statusCode(401).build();
|
Payloads.newStringPayload("Not relevant")).statusCode(401).build();
|
||||||
|
|
||||||
RetryOnRenewAndLogoutOnClose retry = new RetryOnRenewAndLogoutOnClose(cache, sessionClient);
|
InvalidateSessionAndRetryOn401AndLogoutOnClose retry =
|
||||||
|
new InvalidateSessionAndRetryOn401AndLogoutOnClose(cache, sessionClient);
|
||||||
|
|
||||||
|
for (int i = 0; i < 5; i++) {
|
||||||
assertTrue(retry.shouldRetryRequest(command, response));
|
assertTrue(retry.shouldRetryRequest(command, response));
|
||||||
|
}
|
||||||
|
assertFalse(retry.shouldRetryRequest(command, response));
|
||||||
|
|
||||||
verify(cache, command);
|
verify(cache, command);
|
||||||
}
|
}
|
Loading…
Reference in New Issue