KEYCLOAK-11424 DBAllocatorUnavailableException

This commit is contained in:
vramik 2020-02-11 22:15:20 +01:00 committed by Hynek Mlnařík
parent 8aa5019efe
commit e4baef41d1
4 changed files with 18 additions and 5 deletions

View file

@ -129,6 +129,7 @@ public class AllocateDBMojo extends AbstractMojo {
return; return;
} }
logger.info("Total retries: " + totalRetries + "; backOffTime: " + backoffTimeSeconds);
try { try {
IncrementalBackoffRetryPolicy retryPolicy = new IncrementalBackoffRetryPolicy(totalRetries, backoffTimeSeconds, TimeUnit.SECONDS); IncrementalBackoffRetryPolicy retryPolicy = new IncrementalBackoffRetryPolicy(totalRetries, backoffTimeSeconds, TimeUnit.SECONDS);
DBAllocatorServiceClient client = new DBAllocatorServiceClient(dbAllocatorURI, retryPolicy); DBAllocatorServiceClient client = new DBAllocatorServiceClient(dbAllocatorURI, retryPolicy);

View file

@ -70,6 +70,7 @@ public class ReleaseDBMojo extends AbstractMojo {
return; return;
} }
logger.info("Total retries: " + totalRetries + "; backOffTime: " + backoffTimeSeconds);
try { try {
IncrementalBackoffRetryPolicy retryPolicy = new IncrementalBackoffRetryPolicy(totalRetries, backoffTimeSeconds, TimeUnit.SECONDS); IncrementalBackoffRetryPolicy retryPolicy = new IncrementalBackoffRetryPolicy(totalRetries, backoffTimeSeconds, TimeUnit.SECONDS);
DBAllocatorServiceClient client = new DBAllocatorServiceClient(dbAllocatorURI, retryPolicy); DBAllocatorServiceClient client = new DBAllocatorServiceClient(dbAllocatorURI, retryPolicy);

View file

@ -21,6 +21,7 @@ import java.net.URI;
import java.util.Objects; import java.util.Objects;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import org.jboss.logging.Logger;
public class DBAllocatorServiceClient { public class DBAllocatorServiceClient {
@ -29,6 +30,7 @@ public class DBAllocatorServiceClient {
private final Client restClient; private final Client restClient;
private final URI allocatorServletURI; private final URI allocatorServletURI;
private final BackoffRetryPolicy retryPolicy; private final BackoffRetryPolicy retryPolicy;
private final Logger logger = Logger.getLogger(DBAllocatorServiceClient.class);
public DBAllocatorServiceClient(String allocatorServletURI, BackoffRetryPolicy retryPolicy) { public DBAllocatorServiceClient(String allocatorServletURI, BackoffRetryPolicy retryPolicy) {
Objects.requireNonNull(allocatorServletURI, "DB Allocator URI must not be null"); Objects.requireNonNull(allocatorServletURI, "DB Allocator URI must not be null");
@ -68,6 +70,7 @@ public class DBAllocatorServiceClient {
.queryParam("expiry", expirationTimeUnit.toMinutes(expiration)) .queryParam("expiry", expirationTimeUnit.toMinutes(expiration))
.request(); .request();
logger.info("Calling " + allocatorServletURI);
Response response = retryPolicy.retryTillHttpOk(() -> target.get()); Response response = retryPolicy.retryTillHttpOk(() -> target.get());
Properties properties = new Properties(); Properties properties = new Properties();
String content = response.readEntity(String.class); String content = response.readEntity(String.class);

View file

@ -7,6 +7,7 @@ import javax.ws.rs.core.Response;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.LockSupport; import java.util.concurrent.locks.LockSupport;
import org.jboss.logging.Logger;
public class IncrementalBackoffRetryPolicy implements BackoffRetryPolicy { public class IncrementalBackoffRetryPolicy implements BackoffRetryPolicy {
@ -17,6 +18,7 @@ public class IncrementalBackoffRetryPolicy implements BackoffRetryPolicy {
private final int totalRetries; private final int totalRetries;
private final int backoffTime; private final int backoffTime;
private final TimeUnit backoffTimeUnit; private final TimeUnit backoffTimeUnit;
private final Logger logger = Logger.getLogger(IncrementalBackoffRetryPolicy.class);
public IncrementalBackoffRetryPolicy() { public IncrementalBackoffRetryPolicy() {
this(DEFAULT_TOTAL_RETRIES, DEFAULT_BACKOFF_TIME, DEFAULT_BACKOFF_TIME_UNIT); this(DEFAULT_TOTAL_RETRIES, DEFAULT_BACKOFF_TIME, DEFAULT_BACKOFF_TIME_UNIT);
@ -33,9 +35,9 @@ public class IncrementalBackoffRetryPolicy implements BackoffRetryPolicy {
return retryTillHttpOk(callableSupplier, totalRetries, backoffTime, backoffTimeUnit); return retryTillHttpOk(callableSupplier, totalRetries, backoffTime, backoffTimeUnit);
} }
public Response retryTillHttpOk(Callable<Response> callableSupplier, int totalRetries, int backoffTime, TimeUnit backoffTimeUnit) throws DBAllocatorUnavailableException { private Response retryTillHttpOk(Callable<Response> callableSupplier, int totalRetries, int backoffTime, TimeUnit backoffTimeUnit) throws DBAllocatorUnavailableException {
int retryCount = 0; int retryCount = 0;
Response response = null; Response response;
while(true) { while(true) {
try { try {
response = callableSupplier.call(); response = callableSupplier.call();
@ -43,14 +45,20 @@ public class IncrementalBackoffRetryPolicy implements BackoffRetryPolicy {
response = null; response = null;
} }
if (response != null && response.getStatus() == 200) { if (response != null) {
return response; logger.info("Response status: " + response.getStatus());
if (response.getStatus() == 200) {
return response;
}
} }
logger.info("retryCount: " + (retryCount + 1) + ", totalRetries: " + totalRetries);
if (++retryCount > totalRetries) { if (++retryCount > totalRetries) {
logger.info("retryCount exceeded: " + retryCount);
throw new DBAllocatorUnavailableException(response); throw new DBAllocatorUnavailableException(response);
} }
logger.info("backoffTime * retryCount: " + backoffTime * retryCount);
LockSupport.parkNanos(backoffTimeUnit.toNanos(backoffTime * retryCount)); LockSupport.parkNanos(backoffTimeUnit.toNanos(backoffTime * retryCount));
} }
} }