Search in sources :

Example 41 with RE

use of org.apache.druid.java.util.common.RE in project druid by druid-io.

the class AWSRDSTokenPasswordProvider method getPassword.

@JsonIgnore
@Override
public String getPassword() {
    try {
        RdsIamAuthTokenGenerator generator = RdsIamAuthTokenGenerator.builder().credentials(awsCredentialsProvider).region(region).build();
        String authToken = generator.getAuthToken(GetIamAuthTokenRequest.builder().hostname(host).port(port).userName(user).build());
        return authToken;
    } catch (Exception ex) {
        LOGGER.error(ex, "Couldn't generate AWS token.");
        throw new RE(ex, "Couldn't generate AWS token.");
    }
}
Also used : RE(org.apache.druid.java.util.common.RE) RdsIamAuthTokenGenerator(com.amazonaws.services.rds.auth.RdsIamAuthTokenGenerator) JsonIgnore(com.fasterxml.jackson.annotation.JsonIgnore)

Example 42 with RE

use of org.apache.druid.java.util.common.RE in project druid by druid-io.

the class BasicAuthUtils method hashPassword.

public static byte[] hashPassword(final char[] password, final byte[] salt, final int iterations) {
    try {
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        SecretKey key = keyFactory.generateSecret(new PBEKeySpec(password, salt, iterations, KEY_LENGTH));
        return key.getEncoded();
    } catch (InvalidKeySpecException ikse) {
        log.error("Invalid keyspec");
        throw new RuntimeException("Invalid keyspec", ikse);
    } catch (NoSuchAlgorithmException nsae) {
        log.error("%s not supported on this system.", ALGORITHM);
        throw new RE(nsae, "%s not supported on this system.", ALGORITHM);
    }
}
Also used : PBEKeySpec(javax.crypto.spec.PBEKeySpec) SecretKey(javax.crypto.SecretKey) RE(org.apache.druid.java.util.common.RE) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SecretKeyFactory(javax.crypto.SecretKeyFactory)

Example 43 with RE

use of org.apache.druid.java.util.common.RE in project druid by druid-io.

the class WorkerHolder method assignTask.

public boolean assignTask(Task task) {
    if (disabled.get()) {
        log.info("Received task[%s] assignment on worker[%s] when worker is disabled.", task.getId(), worker.getHost());
        return false;
    }
    URL url = TaskRunnerUtils.makeWorkerURL(worker, "/druid-internal/v1/worker/assignTask");
    int numTries = config.getAssignRequestMaxRetries();
    try {
        return RetryUtils.retry(() -> {
            try {
                final StatusResponseHolder response = httpClient.go(new Request(HttpMethod.POST, url).addHeader(HttpHeaders.Names.CONTENT_TYPE, SmileMediaTypes.APPLICATION_JACKSON_SMILE).setContent(smileMapper.writeValueAsBytes(task)), StatusResponseHandler.getInstance(), config.getAssignRequestHttpTimeout().toStandardDuration()).get();
                if (response.getStatus().getCode() == 200) {
                    return true;
                } else {
                    throw new RE("Failed to assign task[%s] to worker[%s]. Response Code[%s] and Message[%s]. Retrying...", task.getId(), worker.getHost(), response.getStatus().getCode(), response.getContent());
                }
            } catch (ExecutionException ex) {
                throw new RE(ex, "Request to assign task[%s] to worker[%s] failed. Retrying...", task.getId(), worker.getHost());
            }
        }, e -> !(e instanceof InterruptedException), numTries);
    } catch (Exception ex) {
        log.info("Not sure whether task[%s] was successfully assigned to worker[%s].", task.getId(), worker.getHost());
        return true;
    }
}
Also used : RE(org.apache.druid.java.util.common.RE) Request(org.apache.druid.java.util.http.client.Request) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL) ExecutionException(java.util.concurrent.ExecutionException)

Example 44 with RE

use of org.apache.druid.java.util.common.RE in project druid by druid-io.

the class OverlordResourceTest method testDisableWorkerWhenWorkerAPIRaisesError.

@Test
public void testDisableWorkerWhenWorkerAPIRaisesError() {
    final String host = "worker-host";
    workerTaskRunnerQueryAdapter.disableWorker(host);
    EasyMock.expectLastCall().andThrow(new RE("Worker API returns error!")).once();
    EasyMock.replay(taskRunner, taskMaster, taskStorageQueryAdapter, indexerMetadataStorageAdapter, req, workerTaskRunnerQueryAdapter);
    final Response response = overlordResource.disableWorker(host);
    Assert.assertEquals(HttpResponseStatus.INTERNAL_SERVER_ERROR.getCode(), response.getStatus());
    Assert.assertEquals(ImmutableMap.of("error", "Worker API returns error!"), response.getEntity());
}
Also used : Response(javax.ws.rs.core.Response) RE(org.apache.druid.java.util.common.RE) Test(org.junit.Test)

Example 45 with RE

use of org.apache.druid.java.util.common.RE in project druid by druid-io.

the class DruidLeaderClient method go.

/**
 * Executes a Request object aimed at the leader. Throws IOException if the leader cannot be located.
 */
public <T, H extends FullResponseHolder<T>> H go(Request request, HttpResponseHandler<H, H> responseHandler) throws IOException, InterruptedException {
    Preconditions.checkState(lifecycleLock.awaitStarted(1, TimeUnit.MILLISECONDS));
    for (int counter = 0; counter < MAX_RETRIES; counter++) {
        final H fullResponseHolder;
        try {
            try {
                fullResponseHolder = httpClient.go(request, responseHandler).get();
            } catch (ExecutionException e) {
                // Unwrap IOExceptions and ChannelExceptions, re-throw others
                Throwables.propagateIfInstanceOf(e.getCause(), IOException.class);
                Throwables.propagateIfInstanceOf(e.getCause(), ChannelException.class);
                throw new RE(e, "HTTP request to[%s] failed", request.getUrl());
            }
        } catch (IOException | ChannelException ex) {
            // can happen if the node is stopped.
            log.warn(ex, "Request[%s] failed.", request.getUrl());
            try {
                if (request.getUrl().getQuery() == null) {
                    request = withUrl(request, new URL(StringUtils.format("%s%s", getCurrentKnownLeader(false), request.getUrl().getPath())));
                } else {
                    request = withUrl(request, new URL(StringUtils.format("%s%s?%s", getCurrentKnownLeader(false), request.getUrl().getPath(), request.getUrl().getQuery())));
                }
                continue;
            } catch (MalformedURLException e) {
                // Not an IOException; this is our own fault.
                throw new ISE(e, "failed to build url with path[%] and query string [%s].", request.getUrl().getPath(), request.getUrl().getQuery());
            }
        }
        if (HttpResponseStatus.TEMPORARY_REDIRECT.equals(fullResponseHolder.getResponse().getStatus())) {
            String redirectUrlStr = fullResponseHolder.getResponse().headers().get("Location");
            if (redirectUrlStr == null) {
                throw new IOE("No redirect location is found in response from url[%s].", request.getUrl());
            }
            log.info("Request[%s] received redirect response to location [%s].", request.getUrl(), redirectUrlStr);
            final URL redirectUrl;
            try {
                redirectUrl = new URL(redirectUrlStr);
            } catch (MalformedURLException ex) {
                throw new IOE(ex, "Malformed redirect location is found in response from url[%s], new location[%s].", request.getUrl(), redirectUrlStr);
            }
            // update known leader location
            currentKnownLeader.set(StringUtils.format("%s://%s:%s", redirectUrl.getProtocol(), redirectUrl.getHost(), redirectUrl.getPort()));
            request = withUrl(request, redirectUrl);
        } else {
            return fullResponseHolder;
        }
    }
    throw new IOE("Retries exhausted, couldn't fulfill request to [%s].", request.getUrl());
}
Also used : MalformedURLException(java.net.MalformedURLException) RE(org.apache.druid.java.util.common.RE) ISE(org.apache.druid.java.util.common.ISE) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) URL(java.net.URL) IOE(org.apache.druid.java.util.common.IOE) ChannelException(org.jboss.netty.channel.ChannelException)

Aggregations

RE (org.apache.druid.java.util.common.RE)46 IOException (java.io.IOException)11 Request (org.apache.druid.java.util.http.client.Request)10 URL (java.net.URL)8 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)6 InputStream (java.io.InputStream)5 ExecutionException (java.util.concurrent.ExecutionException)5 ISE (org.apache.druid.java.util.common.ISE)5 Map (java.util.Map)4 StatusResponseHolder (org.apache.druid.java.util.http.client.response.StatusResponseHolder)4 JavaType (com.fasterxml.jackson.databind.JavaType)3 HashMap (java.util.HashMap)3 DataSegment (org.apache.druid.timeline.DataSegment)3 OSSException (com.aliyun.oss.OSSException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ApiException (io.kubernetes.client.openapi.ApiException)2 File (java.io.File)2