Search in sources :

Example 1 with NonRetryableException

use of org.komamitsu.fluency.NonRetryableException in project fluency by komamitsu.

the class TreasureDataSender method createDatabase.

private void createDatabase(String database) {
    for (int i = 0; i < RETRY_COUNT_FOR_DB_CREATE_DELETE_CONFLICT; i++) {
        try {
            client.createDatabase(database);
            LOG.info("Created database. database={}", database);
            return;
        } catch (TDClientHttpException e) {
            switch(e.getStatusCode()) {
                case 409:
                    LOG.info("The database already exists. database={}", database);
                    if (checkDatabaseAndWaitIfNeeded(database)) {
                        return;
                    }
                    // Retrying...
                    break;
                case 401:
                case 403:
                case 404:
                    throw new NonRetryableException(String.format("Failed to create database. database=%s", database), e);
            }
        } catch (NonRetryableException e) {
            throw e;
        } catch (Throwable e) {
            throw new RetryableException(String.format("Failed to create database. database=%s", database), e);
        }
    }
    // Retry over
    throw new NonRetryableException(String.format("It seems you don't have a proper permission on the database. database=%s", database));
}
Also used : TDClientHttpException(com.treasuredata.client.TDClientHttpException) NonRetryableException(org.komamitsu.fluency.NonRetryableException) NonRetryableException(org.komamitsu.fluency.NonRetryableException) RetryableException(org.komamitsu.fluency.RetryableException)

Example 2 with NonRetryableException

use of org.komamitsu.fluency.NonRetryableException in project fluency by komamitsu.

the class TreasureDataSender method buildClient.

@VisibleForTesting
protected TDClient buildClient() {
    URI uri;
    try {
        uri = new URI(config.getEndpoint());
    } catch (URISyntaxException e) {
        throw new NonRetryableException(String.format("Invalid endpoint. %s", config.getEndpoint()), e);
    }
    String host = uri.getHost() != null ? uri.getHost() : config.getEndpoint();
    TDClientBuilder builder = new TDClientBuilder(false).setEndpoint(host).setApiKey(config.getApikey()).setRetryLimit(config.getRetryMax()).setRetryInitialIntervalMillis(config.getRetryIntervalMs()).setRetryMaxIntervalMillis(config.getMaxRetryIntervalMs()).setRetryMultiplier(config.getRetryFactor());
    if (uri.getScheme() != null && uri.getScheme().equals("http")) {
        builder.setUseSSL(false);
    }
    if (uri.getPort() > 0) {
        builder.setPort(uri.getPort());
    }
    return builder.build();
}
Also used : NonRetryableException(org.komamitsu.fluency.NonRetryableException) TDClientBuilder(com.treasuredata.client.TDClientBuilder) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with NonRetryableException

use of org.komamitsu.fluency.NonRetryableException in project fluency by komamitsu.

the class TreasureDataSender method createTable.

private void createTable(String database, String table) {
    while (true) {
        try {
            client.createTable(database, table);
            LOG.info("Created table. database={}, table={}", database, table);
            return;
        } catch (TDClientHttpException e) {
            switch(e.getStatusCode()) {
                case 409:
                    LOG.info("The table already exists. database={}, table={}", database, table);
                    return;
                case 401:
                case 403:
                    throw new NonRetryableException(String.format("Failed to create table. database=%s, table=%s", database, table), e);
                case 404:
                    createDatabase(database);
                    // Retry to create the table
                    break;
                default:
                    throw new RetryableException(String.format("Failed to create table. database=%s, table=%s", database, table), e);
            }
        } catch (NonRetryableException e) {
            throw e;
        } catch (Throwable e) {
            throw new RetryableException(String.format("Failed to create table. database=%s, table=%s", database, table), e);
        }
    }
}
Also used : TDClientHttpException(com.treasuredata.client.TDClientHttpException) NonRetryableException(org.komamitsu.fluency.NonRetryableException) NonRetryableException(org.komamitsu.fluency.NonRetryableException) RetryableException(org.komamitsu.fluency.RetryableException)

Example 4 with NonRetryableException

use of org.komamitsu.fluency.NonRetryableException in project fluency by komamitsu.

the class TreasureDataSenderTest method sendWithLackOfPermissionOnDatabase.

@Test
public void sendWithLackOfPermissionOnDatabase() throws IOException {
    doThrow(new TDClientHttpNotFoundException("Not Found!!!!")).when(client).importFile(anyString(), anyString(), any(File.class), anyString());
    doThrow(new TDClientHttpNotFoundException("Not Found!!!!")).when(client).createTable(anyString(), anyString());
    doThrow(new TDClientHttpConflictException("Conflict!!!!")).when(client).createDatabase(anyString());
    doReturn(false).when(client).existsDatabase(anyString());
    try {
        sender.send(DB_AND_TABLE, ByteBuffer.wrap(DATA));
        fail();
    } catch (NonRetryableException e) {
        assertTrue(true);
    }
    ArgumentCaptor<String> uniqueIdArgumentCaptor = ArgumentCaptor.forClass(String.class);
    verify(client, times(1)).importFile(eq(DB), eq(TABLE), any(File.class), uniqueIdArgumentCaptor.capture());
    verify(client, times(4)).createDatabase(eq(DB));
    verify(client, times(4)).existsDatabase(eq(DB));
    verify(client, times(1)).createTable(eq(DB), eq(TABLE));
    UUID.fromString(uniqueIdArgumentCaptor.getValue());
}
Also used : NonRetryableException(org.komamitsu.fluency.NonRetryableException) TDClientHttpConflictException(com.treasuredata.client.TDClientHttpConflictException) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) File(java.io.File) TDClientHttpNotFoundException(com.treasuredata.client.TDClientHttpNotFoundException) Test(org.junit.jupiter.api.Test)

Example 5 with NonRetryableException

use of org.komamitsu.fluency.NonRetryableException in project fluency by komamitsu.

the class AwsS3Sender method uploadData.

private void uploadData(String bucket, String key, File file) {
    LOG.debug("Upload data to S3: bucket={}, key={}, fileSize={}", bucket, key, file.length());
    try {
        PutObjectRequest.Builder builder = PutObjectRequest.builder().bucket(bucket).key(key);
        client.putObject(builder.build(), RequestBody.fromFile(file));
    } catch (NonRetryableException e) {
        throw e;
    } catch (Throwable e) {
        throw new RetryableException(String.format("Failed to upload data. bucket=%s, key=%s", bucket, key), e);
    }
}
Also used : NonRetryableException(org.komamitsu.fluency.NonRetryableException) NonRetryableException(org.komamitsu.fluency.NonRetryableException) RetryableException(org.komamitsu.fluency.RetryableException) PutObjectRequest(software.amazon.awssdk.services.s3.model.PutObjectRequest)

Aggregations

NonRetryableException (org.komamitsu.fluency.NonRetryableException)6 RetryableException (org.komamitsu.fluency.RetryableException)3 TDClientHttpException (com.treasuredata.client.TDClientHttpException)2 URI (java.net.URI)2 URISyntaxException (java.net.URISyntaxException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 TDClientBuilder (com.treasuredata.client.TDClientBuilder)1 TDClientHttpConflictException (com.treasuredata.client.TDClientHttpConflictException)1 TDClientHttpNotFoundException (com.treasuredata.client.TDClientHttpNotFoundException)1 File (java.io.File)1 Test (org.junit.jupiter.api.Test)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1 VisibleForTesting (org.msgpack.core.annotations.VisibleForTesting)1 AwsBasicCredentials (software.amazon.awssdk.auth.credentials.AwsBasicCredentials)1 PutObjectRequest (software.amazon.awssdk.services.s3.model.PutObjectRequest)1