Search in sources :

Example 1 with CometApiException

use of ml.comet.experiment.exception.CometApiException in project comet-java-sdk by comet-ml.

the class Connection method executeRequestSyncWithRetries.

/**
 * Synchronously executes provided request. It will attempt to execute request {@link #maxAuthRetries} in
 * case of failure. If all attempts failed the empty optional will be returned or {@link CometApiException}
 * will be thrown in case of {@code throwOnFailure} is {@code true}.
 *
 * @param request        the request to be executed
 * @param throwOnFailure if {@code true} throws exception on failure. Otherwise, empty {@link Optional} will be
 *                       returned.
 * @return the response body or empty {@link Optional}.
 * @throws CometApiException if throwOnFailure set to {@code true} and request was failed.
 */
Optional<String> executeRequestSyncWithRetries(@NonNull Request request, boolean throwOnFailure) throws CometApiException {
    request.getHeaders().add(COMET_SDK_API_HEADER, apiKey);
    String endpoint = request.getUrl();
    org.asynchttpclient.Response response = null;
    for (int i = 1; i < this.maxAuthRetries; i++) {
        if (this.asyncHttpClient.isClosed()) {
            this.logger.warn("failed to execute request {}, the connection already closed.", request);
            if (throwOnFailure) {
                throw new CometApiException("failed to execute request, the connection already closed.");
            }
            return Optional.empty();
        }
        int statusCode = 0;
        try {
            // execute request and wait for completion until default REQUEST_TIMEOUT_MS exceeded
            response = this.asyncHttpClient.executeRequest(request).get();
            statusCode = response.getStatusCode();
            // check status code for possible errors
            ConnectionUtils.checkResponseStatus(response);
            // success - log debug and continue with result
            if (this.logger.isDebugEnabled()) {
                this.logger.debug("for endpoint {} got response {}", endpoint, response.getResponseBody());
            }
            break;
        } catch (CometApiException apiException) {
            // connection attempt failed - check if to retry
            String body = response != null ? response.getStatusText() : RESPONSE_NO_BODY;
            if (i < this.maxAuthRetries - 1) {
                // sleep for a while and repeat
                this.logger.debug("for endpoint {} got response {}, retrying", endpoint, body);
                try {
                    Thread.sleep((2 ^ i) * 1000L);
                } catch (InterruptedException ex) {
                    this.logger.error("Interrupted while sleeping before next attempt of executing request to {}", endpoint, ex);
                }
            } else {
                // maximal number of attempts exceeded - throw or return
                this.logger.error("For endpoint {} got the response '{}', the last retry failed from {} attempts", endpoint, body, this.maxAuthRetries);
                if (throwOnFailure) {
                    throw apiException;
                }
                return Optional.empty();
            }
        } catch (CometWebJavaSdkException ex) {
            // remote endpoint signalled processing error - throw or return
            this.logger.error("Failed to execute request: {}, remote endpoint raised error", request, ex);
            if (throwOnFailure) {
                throw new CometApiException(statusCode, ex.getMessage(), ex.getSdkErrorCode());
            }
            return Optional.empty();
        } catch (Throwable e) {
            // unexpected error - throw or return
            this.logger.error("Failed to execute request: {}, unexpected error", request, e);
            if (throwOnFailure) {
                throw new CometApiException("failed to execute request, unexpected error", e);
            }
            return Optional.empty();
        }
    }
    if (response == null) {
        return Optional.empty();
    } else {
        return Optional.of(response.getResponseBody());
    }
}
Also used : Response(org.asynchttpclient.Response) CometWebJavaSdkException(ml.comet.experiment.impl.rest.CometWebJavaSdkException) CometApiException(ml.comet.experiment.exception.CometApiException)

Example 2 with CometApiException

use of ml.comet.experiment.exception.CometApiException in project comet-java-sdk by comet-ml.

the class BaseExperiment method registerExperiment.

/**
 * Synchronously registers experiment at the Comet server.
 *
 * @throws CometGeneralException if failed to register experiment.
 */
void registerExperiment() throws CometGeneralException {
    if (StringUtils.isNotBlank(this.experimentKey)) {
        getLogger().debug("Not registering a new experiment. Using previous experiment key {}", this.experimentKey);
        return;
    }
    // do synchronous call to register experiment
    try {
        CreateExperimentResponse result = this.restApiClient.registerExperiment(new CreateExperimentRequest(this.workspaceName, this.projectName, this.experimentName)).blockingGet();
        if (StringUtils.isBlank(result.getExperimentKey())) {
            throw new CometGeneralException(getString(FAILED_REGISTER_EXPERIMENT));
        }
        this.experimentKey = result.getExperimentKey();
        this.experimentLink = result.getLink();
        this.workspaceName = result.getWorkspaceName();
        this.projectName = result.getProjectName();
        if (StringUtils.isBlank(this.experimentName)) {
            this.experimentName = result.getName();
        }
    } catch (CometApiException ex) {
        this.getLogger().error(getString(FAILED_REGISTER_EXPERIMENT), ex);
        throw new CometGeneralException(getString(FAILED_REGISTER_EXPERIMENT), ex);
    }
    getLogger().info(getString(EXPERIMENT_CREATED, this.workspaceName, this.projectName, this.experimentName));
    getLogger().info(getString(EXPERIMENT_LIVE, this.experimentLink));
}
Also used : CreateExperimentRequest(ml.comet.experiment.impl.rest.CreateExperimentRequest) CreateExperimentResponse(ml.comet.experiment.impl.rest.CreateExperimentResponse) CometGeneralException(ml.comet.experiment.exception.CometGeneralException) CometApiException(ml.comet.experiment.exception.CometApiException)

Example 3 with CometApiException

use of ml.comet.experiment.exception.CometApiException in project comet-java-sdk by comet-ml.

the class ConnectionTest method testDownloadAsync_onCometApiException.

@Test
public void testDownloadAsync_onCometApiException(@NonNull WireMockRuntimeInfo wmRuntimeInfo) throws IOException {
    // create test error response
    // 
    stubFor(get(urlPathEqualTo(SOME_ENDPOINT)).willReturn(aResponse().withStatus(404)));
    // execute request and check results
    // 
    File downloadFile = Files.createTempFile(null, null).toFile();
    downloadFile.deleteOnExit();
    Connection connection = new Connection(wmRuntimeInfo.getHttpBaseUrl(), TEST_API_KEY, MAX_AUTH_RETRIES_DEFAULT, logger);
    ListenableFuture<Response> responseListenableFuture = connection.downloadAsync(downloadFile, SOME_ENDPOINT, SOME_PARAMS);
    assertNotNull(responseListenableFuture, "future expected");
    // wait for result
    CompletableFuture<Response> completableFuture = responseListenableFuture.toCompletableFuture();
    Exception exception = assertThrows(ExecutionException.class, () -> completableFuture.get(5, TimeUnit.SECONDS));
    // check that inventory was fully processed
    assertEquals(0, connection.getRequestsInventory().get(), "inventory must be empty");
    // check that correct exception returned
    assertTrue(exception.getCause() instanceof CometApiException, "wrong exception returned");
}
Also used : WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) Response(org.asynchttpclient.Response) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) CometWebJavaSdkException(ml.comet.experiment.impl.rest.CometWebJavaSdkException) ExecutionException(java.util.concurrent.ExecutionException) CometApiException(ml.comet.experiment.exception.CometApiException) CometApiException(ml.comet.experiment.exception.CometApiException) Test(org.junit.jupiter.api.Test) WireMockTest(com.github.tomakehurst.wiremock.junit5.WireMockTest)

Example 4 with CometApiException

use of ml.comet.experiment.exception.CometApiException in project comet-java-sdk by comet-ml.

the class ConnectionTest method testSendPostWithRetries_throwOnFailure_onCometWebException.

/**
 * Tests that correct exception raised when Comet web error code received and max retry attempts exceeded
 * with throwOnFailure set to false.
 */
@Test
public void testSendPostWithRetries_throwOnFailure_onCometWebException(@NonNull WireMockRuntimeInfo wmRuntimeInfo) {
    // create test error response
    // 
    stubFor(post(urlPathEqualTo(SOME_ENDPOINT)).willReturn(aResponse().withBody(JsonUtils.toJson(COMET_WEB_JAVA_SDK_EXCEPTION)).withStatus(BAD_REQUEST_ERROR_CODE).withHeader(CONTENT_TYPE.toString(), APPLICATION_JSON.toString())));
    // execute request and check results
    // 
    String baseUrl = wmRuntimeInfo.getHttpBaseUrl();
    Connection connection = new Connection(baseUrl, TEST_API_KEY, MAX_AUTH_RETRIES_DEFAULT, logger);
    CometApiException cometApiException = assertThrows(CometApiException.class, () -> connection.sendPostWithRetries(SOME_REQUEST_STRING, SOME_ENDPOINT, true));
    // check that inventory was fully processed
    assertEquals(0, connection.getRequestsInventory().get(), "inventory must be empty");
    // check that correct exception returned
    checkWebJavaSdkException(cometApiException);
}
Also used : CometApiException(ml.comet.experiment.exception.CometApiException) Test(org.junit.jupiter.api.Test) WireMockTest(com.github.tomakehurst.wiremock.junit5.WireMockTest)

Example 5 with CometApiException

use of ml.comet.experiment.exception.CometApiException in project comet-java-sdk by comet-ml.

the class ConnectionTest method testDownloadAsync_onCometWebException.

@Test
public void testDownloadAsync_onCometWebException(@NonNull WireMockRuntimeInfo wmRuntimeInfo) throws IOException {
    // create test error response
    // 
    stubFor(get(urlPathEqualTo(SOME_ENDPOINT)).willReturn(aResponse().withBody(JsonUtils.toJson(COMET_WEB_JAVA_SDK_EXCEPTION)).withStatus(BAD_REQUEST_ERROR_CODE).withHeader(CONTENT_TYPE.toString(), APPLICATION_JSON.toString())));
    // execute request and check results
    // 
    File downloadFile = Files.createTempFile(null, null).toFile();
    downloadFile.deleteOnExit();
    Connection connection = new Connection(wmRuntimeInfo.getHttpBaseUrl(), TEST_API_KEY, MAX_AUTH_RETRIES_DEFAULT, logger);
    ListenableFuture<Response> responseListenableFuture = connection.downloadAsync(downloadFile, SOME_ENDPOINT, SOME_PARAMS);
    assertNotNull(responseListenableFuture, "future expected");
    // wait for result
    CompletableFuture<Response> completableFuture = responseListenableFuture.toCompletableFuture();
    Exception exception = assertThrows(ExecutionException.class, () -> completableFuture.get(5, TimeUnit.SECONDS));
    // check that inventory was fully processed
    assertEquals(0, connection.getRequestsInventory().get(), "inventory must be empty");
    // check that correct exception returned
    assertTrue(exception.getCause() instanceof CometApiException, "wrong exception returned");
    // check exception values
    checkWebJavaSdkException((CometApiException) exception.getCause());
}
Also used : WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) Response(org.asynchttpclient.Response) File(java.io.File) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) CometWebJavaSdkException(ml.comet.experiment.impl.rest.CometWebJavaSdkException) ExecutionException(java.util.concurrent.ExecutionException) CometApiException(ml.comet.experiment.exception.CometApiException) CometApiException(ml.comet.experiment.exception.CometApiException) Test(org.junit.jupiter.api.Test) WireMockTest(com.github.tomakehurst.wiremock.junit5.WireMockTest)

Aggregations

CometApiException (ml.comet.experiment.exception.CometApiException)12 WireMockTest (com.github.tomakehurst.wiremock.junit5.WireMockTest)7 Test (org.junit.jupiter.api.Test)7 IOException (java.io.IOException)6 ExecutionException (java.util.concurrent.ExecutionException)6 WireMock.aResponse (com.github.tomakehurst.wiremock.client.WireMock.aResponse)4 File (java.io.File)4 FileNotFoundException (java.io.FileNotFoundException)4 Path (java.nio.file.Path)3 LogMessages.getString (ml.comet.experiment.impl.resources.LogMessages.getString)3 CometWebJavaSdkException (ml.comet.experiment.impl.rest.CometWebJavaSdkException)3 RestApiResponse (ml.comet.experiment.impl.rest.RestApiResponse)3 Response (org.asynchttpclient.Response)3 Single (io.reactivex.rxjava3.core.Single)2 Disposable (io.reactivex.rxjava3.disposables.Disposable)2 BiFunction (io.reactivex.rxjava3.functions.BiFunction)2 Function (io.reactivex.rxjava3.functions.Function)2 StandardCharsets (java.nio.charset.StandardCharsets)2 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)2 Files (java.nio.file.Files)2