Skip to content

Commit

Permalink
feat(java): waitForApiKey (#798)
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp authored Jul 7, 2022
1 parent 23ba0fc commit 64a34c7
Show file tree
Hide file tree
Showing 9 changed files with 268 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public <T> CompletableFuture<T> executeAsync(Call call, final Type returnType) {
new Callback() {
@Override
public void onFailure(Call call, IOException e) {
future.completeExceptionally(new AlgoliaRuntimeException(e));
future.completeExceptionally(e.getCause());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.algolia.utils;

import com.algolia.exceptions.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.function.IntUnaryOperator;
import java.util.function.Predicate;
import java.util.function.Supplier;
Expand All @@ -14,22 +12,17 @@ public class TaskUtils {
return Math.min(retries * 200, 5000);
};

public static <TResponse> void retryUntil(
Supplier<CompletableFuture<TResponse>> func,
public static <TResponse> TResponse retryUntil(
Supplier<TResponse> func,
Predicate<TResponse> validate,
int maxRetries,
IntUnaryOperator timeout
) throws AlgoliaRuntimeException {
int retryCount = 0;
while (retryCount < maxRetries) {
try {
TResponse resp = func.get().get();
if (validate.test(resp)) {
return;
}
} catch (InterruptedException | ExecutionException e) {
// if the task is interrupted, just return
return;
TResponse resp = func.get();
if (validate.test(resp)) {
return resp;
}
try {
Thread.sleep(timeout.applyAsInt(retryCount));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.algolia.utils;

public enum ApiKeyOperation {
ADD,
DELETE,
UPDATE,
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,48 +35,52 @@ public Response intercept(Chain chain) throws IOException {
(useReadTransporter != null || request.method().equals("GET")) ? CallType.READ : CallType.WRITE
)
.iterator();
while (hostsIter.hasNext()) {
StatefulHost currentHost = hostsIter.next();
try {
while (hostsIter.hasNext()) {
StatefulHost currentHost = hostsIter.next();

// Building the request URL
HttpUrl newUrl = request.url().newBuilder().scheme(currentHost.getScheme()).host(currentHost.getHost()).build();
request = request.newBuilder().url(newUrl).build();
// Building the request URL
HttpUrl newUrl = request.url().newBuilder().scheme(currentHost.getScheme()).host(currentHost.getHost()).build();
request = request.newBuilder().url(newUrl).build();

// Computing timeout with the retry count
chain.withConnectTimeout(chain.connectTimeoutMillis() + currentHost.getRetryCount() * 1000, TimeUnit.MILLISECONDS);
// Computing timeout with the retry count
chain.withConnectTimeout(chain.connectTimeoutMillis() + currentHost.getRetryCount() * 1000, TimeUnit.MILLISECONDS);

try {
Response response = chain.proceed(request);
currentHost.setLastUse(Utils.nowUTC());
// no timeout
if (response.isSuccessful()) {
try {
Response response = chain.proceed(request);
currentHost.setLastUse(Utils.nowUTC());
// no timeout
if (response.isSuccessful()) {
currentHost.setUp(true);
return response;
}
if (isRetryable(response)) {
currentHost.setUp(false);
response.close();
continue;
}
String message = response.message();
if (response.body() != null) {
message = response.body().string();
}
throw new AlgoliaApiException(message, response.code());
} catch (AlgoliaApiException e) {
throw e;
} catch (SocketTimeoutException e) {
// timeout
currentHost.setUp(true);
return response;
currentHost.setLastUse(Utils.nowUTC());
currentHost.incrementRetryCount();
} catch (UnknownHostException e) {
throw new AlgoliaApiException(e.getMessage(), 404);
} catch (Exception e) {
throw new AlgoliaApiException(e.getMessage(), 400);
}
if (isRetryable(response)) {
currentHost.setUp(false);
response.close();
continue;
}
String message = response.message();
if (response.body() != null) {
message = response.body().string();
}
throw new AlgoliaApiException(message, response.code());
} catch (AlgoliaApiException e) {
throw e;
} catch (SocketTimeoutException e) {
// timeout
currentHost.setUp(true);
currentHost.setLastUse(Utils.nowUTC());
currentHost.incrementRetryCount();
} catch (UnknownHostException e) {
throw new AlgoliaApiException(e.getMessage(), 404);
} catch (Exception e) {
throw new AlgoliaApiException(e.getMessage(), 400);
}
throw new AlgoliaRetryException("All hosts are unreachable");
} catch (Exception e) {
throw new IOException(e);
}
throw new AlgoliaRetryException("All hosts are unreachable");
}
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export const DEFAULT_TIMEOUT = (retryCount: number): number =>
* @param createRetryablePromiseOptions.func - The function to run, which returns a promise.
* @param createRetryablePromiseOptions.validate - The validator function. It receives the resolved return of `func`.
* @param createRetryablePromiseOptions.maxRetries - The maximum number of retries. 50 by default.
* @param createRetryablePromiseOptions.timeout - The function to decide how long to wait between tries.
* @param createRetryablePromiseOptions.timeout - The function to decide how long to wait between retries.
*/
export function createRetryablePromise<TResponse>({
func,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type CreateRetryablePromiseOptions<TResponse> = {
maxRetries?: number;

/**
* The function to decide how long to wait between tries.
* The function to decide how long to wait between retries.
*/
timeout?: (retryCount: number) => number;
};
215 changes: 210 additions & 5 deletions templates/java/libraries/okhttp-gson/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -278,24 +278,229 @@ public class {{classname}} extends ApiClient {
{{/operation}}

{{#isSearchClient}}
public void waitForTask(String indexName, Long taskID, RequestOptions requestOptions, int maxRetries, IntUnaryOperator timeout) {
/**
* Helper: Wait for a task to complete with `indexName` and `taskID`.
*
* @summary Wait for a task to complete.
* @param indexName The `indexName` where the operation was performed.
* @param taskID The `taskID` returned in the method response.
* @param maxRetries The maximum number of retry. 50 by default. (optional)
* @param timeout The function to decide how long to wait between retries. min(retries * 200, 5000) by default. (optional)
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)
*/
public void waitForTask(String indexName, Long taskID, int maxRetries, IntUnaryOperator timeout, RequestOptions requestOptions) {
TaskUtils.retryUntil(() -> {
return this.getTaskAsync(indexName, taskID, requestOptions);
return this.getTask(indexName, taskID, requestOptions);
}, (GetTaskResponse task) -> {
return task.getStatus() == TaskStatus.PUBLISHED;
}, maxRetries, timeout);
}

/**
* Helper: Wait for a task to complete with `indexName` and `taskID`.
*
* @summary Wait for a task to complete.
* @param indexName The `indexName` where the operation was performed.
* @param taskID The `taskID` returned in the method response.
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)
*/
public void waitForTask(String indexName, Long taskID, RequestOptions requestOptions) {
this.waitForTask(indexName, taskID, requestOptions, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT);
this.waitForTask(indexName, taskID, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, requestOptions);
}

/**
* Helper: Wait for a task to complete with `indexName` and `taskID`.
*
* @summary Wait for a task to complete.
* @param indexName The `indexName` where the operation was performed.
* @param taskID The `taskID` returned in the method response.
* @param maxRetries The maximum number of retry. 50 by default. (optional)
* @param timeout The function to decide how long to wait between retries. min(retries * 200, 5000) by default. (optional)
*/
public void waitForTask(String indexName, Long taskID, int maxRetries, IntUnaryOperator timeout) {
this.waitForTask(indexName, taskID, null, maxRetries, timeout);
this.waitForTask(indexName, taskID, maxRetries, timeout, null);
}

/**
* Helper: Wait for a task to complete with `indexName` and `taskID`.
*
* @summary Wait for a task to complete.
* @param indexName The `indexName` where the operation was performed.
* @param taskID The `taskID` returned in the method response.
*/
public void waitForTask(String indexName, Long taskID) {
this.waitForTask(indexName, taskID, null, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT);
this.waitForTask(indexName, taskID, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null);
}

/**
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
*
* @summary Wait for an API key task to be processed.
* @param operation The `operation` that was done on a `key`.
* @param key The `key` that has been added, deleted or updated.
* @param apiKey Necessary to know if an `update` operation has been processed, compare fields of the response with it.
* @param maxRetries The maximum number of retry. 50 by default. (optional)
* @param timeout The function to decide how long to wait between retries. min(retries * 200, 5000) by default. (optional)
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)
*/
public Key waitForApiKey(
ApiKeyOperation operation,
String key,
ApiKey apiKey,
int maxRetries,
IntUnaryOperator timeout,
RequestOptions requestOptions
) {
if (operation == ApiKeyOperation.UPDATE) {
if (apiKey == null) {
throw new AlgoliaRetryException("`apiKey` is required when waiting for an `update` operation.");
}

// when updating an api key, we poll the api until we receive a different key
return TaskUtils.retryUntil(
() -> {
return this.getApiKey(key, requestOptions);
},
(Key respKey) -> {
// we need to convert to an ApiKey object to use the `equals` method
ApiKey sameType = new ApiKey()
.setAcl(respKey.getAcl())
.setDescription(respKey.getDescription())
.setIndexes(respKey.getIndexes())
.setMaxHitsPerQuery(respKey.getMaxHitsPerQuery())
.setMaxQueriesPerIPPerHour(respKey.getMaxQueriesPerIPPerHour())
.setQueryParameters(respKey.getQueryParameters())
.setReferers(respKey.getReferers())
.setValidity(respKey.getValidity());
return apiKey.equals(sameType);
},
maxRetries,
timeout
);
}

// bypass lambda restriction to modify final object
final Key[] addedKey = new Key[] { null };

// check the status of the getApiKey method
TaskUtils.retryUntil(
() -> {
try {
addedKey[0] = this.getApiKey(key, requestOptions);
// magic number to signify we found the key
return -2;
} catch (AlgoliaApiException e) {
return e.getHttpErrorCode();
}
},
(Integer status) -> {
switch (operation) {
case ADD:
// stop either when the key is created or when we don't receive 404
return status == -2 || status != 404;
case DELETE:
// stop when the key is not found
return status == 404;
default:
// continue
return false;
}
},
maxRetries,
timeout
);
return addedKey[0];
}
/**
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
*
* @summary Wait for an API key task to be processed.
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
* @param key The `key` that has been added, deleted or updated.
* @param maxRetries The maximum number of retry. 50 by default. (optional)
* @param timeout The function to decide how long to wait between retries. min(retries * 200, 5000) by default. (optional)
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)
*/
public Key waitForApiKey(ApiKeyOperation operation, String key, int maxRetries, IntUnaryOperator timeout, RequestOptions requestOptions) {
return this.waitForApiKey(operation, key, null, maxRetries, timeout, requestOptions);
}
/**
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
*
* @summary Wait for an API key task to be processed.
* @param operation The `operation` that was done on a `key`.
* @param key The `key` that has been added, deleted or updated.
* @param apiKey Necessary to know if an `update` operation has been processed, compare fields of the response with it.
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)
*/
public Key waitForApiKey(ApiKeyOperation operation, String key, ApiKey apiKey, RequestOptions requestOptions) {
return this.waitForApiKey(operation, key, apiKey, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, requestOptions);
}
/**
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
*
* @summary Wait for an API key task to be processed.
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
* @param key The `key` that has been added, deleted or updated.
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)
*/
public Key waitForApiKey(ApiKeyOperation operation, String key, RequestOptions requestOptions) {
return this.waitForApiKey(operation, key, null, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, requestOptions);
}
/**
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
*
* @summary Wait for an API key task to be processed.
* @param operation The `operation` that was done on a `key`.
* @param key The `key` that has been added, deleted or updated.
* @param apiKey Necessary to know if an `update` operation has been processed, compare fields of the response with it.
* @param maxRetries The maximum number of retry. 50 by default. (optional)
* @param timeout The function to decide how long to wait between retries. min(retries * 200, 5000) by default. (optional)
*/
public Key waitForApiKey(ApiKeyOperation operation, String key, ApiKey apiKey, int maxRetries, IntUnaryOperator timeout) {
return this.waitForApiKey(operation, key, apiKey, maxRetries, timeout, null);
}
/**
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
*
* @summary Wait for an API key task to be processed.
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
* @param key The `key` that has been added, deleted or updated.
* @param maxRetries The maximum number of retry. 50 by default. (optional)
* @param timeout The function to decide how long to wait between retries. min(retries * 200, 5000) by default. (optional)
*/
public Key waitForApiKey(ApiKeyOperation operation, String key, int maxRetries, IntUnaryOperator timeout) {
return this.waitForApiKey(operation, key, null, maxRetries, timeout, null);
}
/**
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
*
* @summary Wait for an API key task to be processed.
* @param operation The `operation` that was done on a `key`.
* @param key The `key` that has been added, deleted or updated.
* @param apiKey Necessary to know if an `update` operation has been processed, compare fields of the response with it.
*/
public Key waitForApiKey(ApiKeyOperation operation, String key, ApiKey apiKey) {
return this.waitForApiKey(operation, key, apiKey, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null);
}
/**
* Helper: Wait for an API key to be added, updated or deleted based on a given `operation`.
*
* @summary Wait for an API key task to be processed.
* @param operation The `operation` that was done on a `key`. (ADD or DELETE only)
* @param key The `key` that has been added, deleted or updated.
*/
public Key waitForApiKey(ApiKeyOperation operation, String key) {
return this.waitForApiKey(operation, key, null, TaskUtils.DEFAULT_MAX_RETRIES, TaskUtils.DEFAULT_TIMEOUT, null);
}
{{/isSearchClient}}
}
Expand Down
Loading

0 comments on commit 64a34c7

Please sign in to comment.