Example usage for org.joda.time Duration Duration

List of usage examples for org.joda.time Duration Duration

Introduction

In this page you can find the example usage for org.joda.time Duration Duration.

Prototype

public Duration(Object duration) 

Source Link

Document

Creates a duration from the specified object using the org.joda.time.convert.ConverterManager ConverterManager .

Usage

From source file:io.druid.db.DatabaseRuleManager.java

License:Open Source License

@LifecycleStart
public void start() {
    synchronized (lock) {
        if (started) {
            return;
        }//from w ww .  j  ava2 s. c  om

        this.exec = Execs.scheduledSingleThreaded("DatabaseRuleManager-Exec--%d");

        createDefaultRule(dbi, getRulesTable(), config.get().getDefaultRule(), jsonMapper);
        ScheduledExecutors.scheduleWithFixedDelay(exec, new Duration(0),
                config.get().getPollDuration().toStandardDuration(), new Runnable() {
                    @Override
                    public void run() {
                        poll();
                    }
                });

        started = true;
    }
}

From source file:io.druid.indexer.TimeWindowMovingAverageCollector.java

License:Apache License

public void start() {
    ScheduledExecutors.scheduleWithFixedDelay(scheduledExecutorService, new Duration(windowSizeMillis),
            new Duration(windowSizeMillis), new Runnable() {
                @Override//from   w  w w  .jav a 2 s .co  m
                @SuppressWarnings("ObjectEquality")
                public void run() {
                    if (stopTime != null) {
                        return;
                    }
                    Map<String, Double> metrics = metricsGetter.getMetrics();
                    if (metrics == STOP_SIGNAL) {
                        stop();
                    } else {
                        statsBuffer.add(metrics);
                    }
                }
            });
    startTime = DateTimes.nowUtc();
}

From source file:io.druid.indexing.common.actions.RemoteTaskActionClient.java

License:Apache License

@Override
public <RetType> RetType submit(TaskAction<RetType> taskAction) throws IOException {
    log.info("Performing action for task[%s]: %s", task.getId(), taskAction);

    byte[] dataToSend = jsonMapper.writeValueAsBytes(new TaskActionHolder(task, taskAction));

    final RetryPolicy retryPolicy = retryPolicyFactory.makeRetryPolicy();

    while (true) {
        try {//from   ww  w . j a  v a2 s  .  c om
            final Server server;
            final URI serviceUri;
            try {
                server = getServiceInstance();
                serviceUri = makeServiceUri(server);
            } catch (Exception e) {
                // Want to retry, so throw an IOException.
                throw new IOException("Failed to locate service uri", e);
            }

            final StatusResponseHolder response;

            log.info("Submitting action for task[%s] to overlord[%s]: %s", task.getId(), serviceUri,
                    taskAction);

            try {
                response = httpClient.go(new Request(HttpMethod.POST, serviceUri.toURL()).setContent(
                        MediaType.APPLICATION_JSON, dataToSend), new StatusResponseHandler(Charsets.UTF_8))
                        .get();
            } catch (Exception e) {
                Throwables.propagateIfInstanceOf(e.getCause(), IOException.class);
                Throwables.propagateIfInstanceOf(e.getCause(), ChannelException.class);
                throw Throwables.propagate(e);
            }

            if (response.getStatus().getCode() / 200 == 1) {
                final Map<String, Object> responseDict = jsonMapper.readValue(response.getContent(),
                        new TypeReference<Map<String, Object>>() {
                        });
                return jsonMapper.convertValue(responseDict.get("result"), taskAction.getReturnTypeReference());
            } else {
                // Want to retry, so throw an IOException.
                throw new IOException(String.format(
                        "Scary HTTP status returned: %s. Check your overlord[%s] logs for exceptions.",
                        response.getStatus(), server.getHost()));
            }
        } catch (IOException | ChannelException e) {
            log.warn(e, "Exception submitting action for task[%s]", task.getId());

            final Duration delay = retryPolicy.getAndIncrementRetryDelay();
            if (delay == null) {
                throw e;
            } else {
                try {
                    final long sleepTime = jitter(delay.getMillis());
                    log.info("Will try again in [%s].", new Duration(sleepTime).toString());
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e2) {
                    throw Throwables.propagate(e2);
                }
            }
        }
    }
}

From source file:io.druid.indexing.common.RetryPolicy.java

License:Apache License

public Duration getAndIncrementRetryDelay() {
    if (hasExceededRetryThreshold()) {
        return null;
    }//ww w .jav  a2 s .c o  m

    Duration retVal = currRetryDelay;
    currRetryDelay = new Duration(Math.min(currRetryDelay.getMillis() * 2, maxRetryDelay.getMillis()));
    ++retryCount;
    return retVal;
}

From source file:io.druid.indexing.jdbc.JDBCIndexTaskClient.java

License:Apache License

public Map<Integer, Long> pause(final String id, final long timeout) {
    log.debug("Pause task[%s] timeout[%d]", id, timeout);

    try {//w ww.java2 s  .c  om
        final FullResponseHolder response = submitRequest(id, HttpMethod.POST, "pause",
                timeout > 0 ? String.format("timeout=%d", timeout) : null, true);

        if (response.getStatus().equals(HttpResponseStatus.OK)) {
            log.info("Task [%s] paused successfully", id);
            return jsonMapper.readValue(response.getContent(), new TypeReference<Map<Integer, Long>>() {
            });
        }

        final RetryPolicy retryPolicy = retryPolicyFactory.makeRetryPolicy();
        while (true) {
            if (getStatus(id) == JDBCIndexTask.Status.PAUSED) {
                return getCurrentOffsets(id, true);
            }

            final Duration delay = retryPolicy.getAndIncrementRetryDelay();
            if (delay == null) {
                log.error("Task [%s] failed to pause, aborting", id);
                throw new ISE("Task [%s] failed to pause, aborting", id);
            } else {
                final long sleepTime = delay.getMillis();
                log.info("Still waiting for task [%s] to pause; will try again in [%s]", id,
                        new Duration(sleepTime).toString());
                Thread.sleep(sleepTime);
            }
        }
    } catch (NoTaskLocationException e) {
        log.error("Exception [%s] while pausing Task [%s]", e.getMessage(), id);
        return ImmutableMap.of();
    } catch (IOException | InterruptedException e) {
        log.error("Exception [%s] while pausing Task [%s]", e.getMessage(), id);
        throw Throwables.propagate(e);
    }
}

From source file:io.druid.indexing.jdbc.JDBCIndexTaskClient.java

License:Apache License

private FullResponseHolder submitRequest(String id, HttpMethod method, String pathSuffix, String query,
        byte[] content, boolean retry) {
    final RetryPolicy retryPolicy = retryPolicyFactory.makeRetryPolicy();
    while (true) {
        FullResponseHolder response = null;
        Request request = null;/*from  w  ww .  ja  va 2 s  .c  om*/
        TaskLocation location = TaskLocation.unknown();
        String path = String.format("%s/%s/%s", BASE_PATH, id, pathSuffix);

        Optional<TaskStatus> status = taskInfoProvider.getTaskStatus(id);
        if (!status.isPresent() || !status.get().isRunnable()) {
            throw new TaskNotRunnableException(
                    String.format("Aborting request because task [%s] is not runnable", id));
        }

        try {
            location = taskInfoProvider.getTaskLocation(id);
            if (location.equals(TaskLocation.unknown())) {
                throw new NoTaskLocationException(String.format("No TaskLocation available for task [%s]", id));
            }

            // Netty throws some annoying exceptions if a connection can't be opened, which happens relatively frequently
            // for tasks that happen to still be starting up, so test the connection first to keep the logs clean.
            checkConnection(location.getHost(), location.getPort());

            try {
                URI serviceUri = new URI("http", null, location.getHost(), location.getPort(), path, query,
                        null);
                request = new Request(method, serviceUri.toURL());

                // used to validate that we are talking to the correct worker
                request.addHeader(ChatHandlerResource.TASK_ID_HEADER, id);

                if (content.length > 0) {
                    request.setContent(MediaType.APPLICATION_JSON, content);
                }

                log.debug("HTTP %s: %s", method.getName(), serviceUri.toString());
                response = httpClient.go(request, new FullResponseHandler(Charsets.UTF_8), httpTimeout).get();
            } catch (Exception e) {
                Throwables.propagateIfInstanceOf(e.getCause(), IOException.class);
                Throwables.propagateIfInstanceOf(e.getCause(), ChannelException.class);
                throw Throwables.propagate(e);
            }

            int responseCode = response.getStatus().getCode();
            if (responseCode / 100 == 2) {
                return response;
            } else if (responseCode == 400) { // don't bother retrying if it's a bad request
                throw new IAE("Received 400 Bad Request with body: %s", response.getContent());
            } else {
                throw new IOException(String.format("Received status [%d]", responseCode));
            }
        } catch (IOException | ChannelException e) {

            // Since workers are free to move tasks around to different ports, there is a chance that a task may have been
            // moved but our view of its location has not been updated yet from ZK. To detect this case, we send a header
            // identifying our expected recipient in the request; if this doesn't correspond to the worker we messaged, the
            // worker will return an HTTP 404 with its ID in the response header. If we get a mismatching task ID, then
            // we will wait for a short period then retry the request indefinitely, expecting the task's location to
            // eventually be updated.

            final Duration delay;
            if (response != null && response.getStatus().equals(HttpResponseStatus.NOT_FOUND)) {
                String headerId = response.getResponse().headers().get(ChatHandlerResource.TASK_ID_HEADER);
                if (headerId != null && !headerId.equals(id)) {
                    log.warn("Expected worker to have taskId [%s] but has taskId [%s], will retry in [%d]s", id,
                            headerId, TASK_MISMATCH_RETRY_DELAY_SECONDS);
                    delay = Duration.standardSeconds(TASK_MISMATCH_RETRY_DELAY_SECONDS);
                } else {
                    delay = retryPolicy.getAndIncrementRetryDelay();
                }
            } else {
                delay = retryPolicy.getAndIncrementRetryDelay();
            }

            String urlForLog = (request != null ? request.getUrl().toString()
                    : String.format("http://%s:%d%s", location.getHost(), location.getPort(), path));
            if (!retry) {
                // if retry=false, we probably aren't too concerned if the operation doesn't succeed (i.e. the request was
                // for informational purposes only) so don't log a scary stack trace
                log.info("submitRequest failed for [%s], with message [%s]", urlForLog, e.getMessage());
                Throwables.propagate(e);
            } else if (delay == null) {
                log.warn(e, "Retries exhausted for [%s], last exception:", urlForLog);
                Throwables.propagate(e);
            } else {
                try {
                    final long sleepTime = delay.getMillis();
                    log.debug("Bad response HTTP [%s] from [%s]; will try again in [%s] (body/exception: [%s])",
                            (response != null ? response.getStatus().getCode() : "no response"), urlForLog,
                            new Duration(sleepTime).toString(),
                            (response != null ? response.getContent() : e.getMessage()));
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e2) {
                    Throwables.propagate(e2);
                }
            }
        } catch (NoTaskLocationException e) {
            log.info(
                    "No TaskLocation available for task [%s], this task may not have been assigned to a worker yet or "
                            + "may have already completed",
                    id);
            throw e;
        } catch (Exception e) {
            log.warn(e, "Exception while sending request");
            throw e;
        }
    }
}

From source file:io.druid.indexing.kafka.KafkaIndexTaskClient.java

License:Apache License

public Map<Integer, Long> pause(final String id, final long timeout) {
    log.debug("Pause task[%s] timeout[%d]", id, timeout);

    try {//from  w  w w.  j  a v a  2  s. c  om
        final FullResponseHolder response = submitRequest(id, HttpMethod.POST, "pause",
                timeout > 0 ? StringUtils.format("timeout=%d", timeout) : null, true);

        if (response.getStatus().equals(HttpResponseStatus.OK)) {
            log.info("Task [%s] paused successfully", id);
            return jsonMapper.readValue(response.getContent(), new TypeReference<Map<Integer, Long>>() {
            });
        }

        final RetryPolicy retryPolicy = retryPolicyFactory.makeRetryPolicy();
        while (true) {
            if (getStatus(id) == KafkaIndexTask.Status.PAUSED) {
                return getCurrentOffsets(id, true);
            }

            final Duration delay = retryPolicy.getAndIncrementRetryDelay();
            if (delay == null) {
                log.error("Task [%s] failed to pause, aborting", id);
                throw new ISE("Task [%s] failed to pause, aborting", id);
            } else {
                final long sleepTime = delay.getMillis();
                log.info("Still waiting for task [%s] to pause; will try again in [%s]", id,
                        new Duration(sleepTime).toString());
                Thread.sleep(sleepTime);
            }
        }
    } catch (NoTaskLocationException e) {
        log.error("Exception [%s] while pausing Task [%s]", e.getMessage(), id);
        return ImmutableMap.of();
    } catch (IOException | InterruptedException e) {
        log.error("Exception [%s] while pausing Task [%s]", e.getMessage(), id);
        throw Throwables.propagate(e);
    }
}

From source file:io.druid.indexing.kafka.KafkaIndexTaskClient.java

License:Apache License

private FullResponseHolder submitRequest(String id, HttpMethod method, String pathSuffix, String query,
        byte[] content, boolean retry) {
    final RetryPolicy retryPolicy = retryPolicyFactory.makeRetryPolicy();
    while (true) {
        FullResponseHolder response = null;
        Request request = null;/*from w  w w.  j  a  va 2 s.  c o  m*/
        TaskLocation location = TaskLocation.unknown();
        String path = StringUtils.format("%s/%s/%s", BASE_PATH, id, pathSuffix);

        Optional<TaskStatus> status = taskInfoProvider.getTaskStatus(id);
        if (!status.isPresent() || !status.get().isRunnable()) {
            throw new TaskNotRunnableException(
                    StringUtils.format("Aborting request because task [%s] is not runnable", id));
        }

        String host = location.getHost();
        String scheme = "";
        int port = -1;

        try {
            location = taskInfoProvider.getTaskLocation(id);
            if (location.equals(TaskLocation.unknown())) {
                throw new NoTaskLocationException(
                        StringUtils.format("No TaskLocation available for task [%s]", id));
            }

            host = location.getHost();
            scheme = location.getTlsPort() >= 0 ? "https" : "http";
            port = location.getTlsPort() >= 0 ? location.getTlsPort() : location.getPort();

            // Netty throws some annoying exceptions if a connection can't be opened, which happens relatively frequently
            // for tasks that happen to still be starting up, so test the connection first to keep the logs clean.
            checkConnection(host, port);

            try {
                URI serviceUri = new URI(scheme, null, host, port, path, query, null);
                request = new Request(method, serviceUri.toURL());

                // used to validate that we are talking to the correct worker
                request.addHeader(ChatHandlerResource.TASK_ID_HEADER, id);

                if (content.length > 0) {
                    request.setContent(MediaType.APPLICATION_JSON, content);
                }

                log.debug("HTTP %s: %s", method.getName(), serviceUri.toString());
                response = httpClient.go(request, new FullResponseHandler(Charsets.UTF_8), httpTimeout).get();
            } catch (Exception e) {
                Throwables.propagateIfInstanceOf(e.getCause(), IOException.class);
                Throwables.propagateIfInstanceOf(e.getCause(), ChannelException.class);
                throw Throwables.propagate(e);
            }

            int responseCode = response.getStatus().getCode();
            if (responseCode / 100 == 2) {
                return response;
            } else if (responseCode == 400) { // don't bother retrying if it's a bad request
                throw new IAE("Received 400 Bad Request with body: %s", response.getContent());
            } else {
                throw new IOE("Received status [%d]", responseCode);
            }
        } catch (IOException | ChannelException e) {

            // Since workers are free to move tasks around to different ports, there is a chance that a task may have been
            // moved but our view of its location has not been updated yet from ZK. To detect this case, we send a header
            // identifying our expected recipient in the request; if this doesn't correspond to the worker we messaged, the
            // worker will return an HTTP 404 with its ID in the response header. If we get a mismatching task ID, then
            // we will wait for a short period then retry the request indefinitely, expecting the task's location to
            // eventually be updated.

            final Duration delay;
            if (response != null && response.getStatus().equals(HttpResponseStatus.NOT_FOUND)) {
                String headerId = response.getResponse().headers().get(ChatHandlerResource.TASK_ID_HEADER);
                if (headerId != null && !headerId.equals(id)) {
                    log.warn("Expected worker to have taskId [%s] but has taskId [%s], will retry in [%d]s", id,
                            headerId, TASK_MISMATCH_RETRY_DELAY_SECONDS);
                    delay = Duration.standardSeconds(TASK_MISMATCH_RETRY_DELAY_SECONDS);
                } else {
                    delay = retryPolicy.getAndIncrementRetryDelay();
                }
            } else {
                delay = retryPolicy.getAndIncrementRetryDelay();
            }
            String urlForLog = (request != null ? request.getUrl().toString()
                    : StringUtils.format("%s://%s:%d%s", scheme, host, port, path));
            if (!retry) {
                // if retry=false, we probably aren't too concerned if the operation doesn't succeed (i.e. the request was
                // for informational purposes only) so don't log a scary stack trace
                log.info("submitRequest failed for [%s], with message [%s]", urlForLog, e.getMessage());
                Throwables.propagate(e);
            } else if (delay == null) {
                log.warn(e, "Retries exhausted for [%s], last exception:", urlForLog);
                Throwables.propagate(e);
            } else {
                try {
                    final long sleepTime = delay.getMillis();
                    log.debug("Bad response HTTP [%s] from [%s]; will try again in [%s] (body/exception: [%s])",
                            (response != null ? response.getStatus().getCode() : "no response"), urlForLog,
                            new Duration(sleepTime).toString(),
                            (response != null ? response.getContent() : e.getMessage()));
                    Thread.sleep(sleepTime);
                } catch (InterruptedException e2) {
                    Throwables.propagate(e2);
                }
            }
        } catch (NoTaskLocationException e) {
            log.info(
                    "No TaskLocation available for task [%s], this task may not have been assigned to a worker yet or "
                            + "may have already completed",
                    id);
            throw e;
        } catch (Exception e) {
            log.warn(e, "Exception while sending request");
            throw e;
        }
    }
}

From source file:io.druid.security.basic.authentication.db.cache.CoordinatorPollingBasicAuthenticatorCacheManager.java

License:Apache License

@LifecycleStart
public void start() {
    if (!lifecycleLock.canStart()) {
        throw new ISE("can't start.");
    }/*from   w  w w  .  j av  a  2s .c om*/

    LOG.info("Starting DefaultBasicAuthenticatorCacheManager.");

    try {
        initUserMaps();

        ScheduledExecutors.scheduleWithFixedDelay(exec, new Duration(commonCacheConfig.getPollingPeriod()),
                new Duration(commonCacheConfig.getPollingPeriod()), () -> {
                    try {
                        long randomDelay = ThreadLocalRandom.current().nextLong(0,
                                commonCacheConfig.getMaxRandomDelay());
                        LOG.debug("Inserting random polling delay of [%s] ms", randomDelay);
                        Thread.sleep(randomDelay);

                        LOG.debug("Scheduled cache poll is running");
                        for (String authenticatorPrefix : authenticatorPrefixes) {
                            Map<String, BasicAuthenticatorUser> userMap = fetchUserMapFromCoordinator(
                                    authenticatorPrefix, false);
                            if (userMap != null) {
                                cachedUserMaps.put(authenticatorPrefix, userMap);
                            }
                        }
                        LOG.debug("Scheduled cache poll is done");
                    } catch (Throwable t) {
                        LOG.makeAlert(t, "Error occured while polling for cachedUserMaps.").emit();
                    }
                });

        lifecycleLock.started();
        LOG.info("Started DefaultBasicAuthenticatorCacheManager.");
    } finally {
        lifecycleLock.exitStart();
    }
}

From source file:io.druid.security.basic.authentication.db.updater.CoordinatorBasicAuthenticatorMetadataStorageUpdater.java

License:Apache License

@LifecycleStart
public void start() {
    if (!lifecycleLock.canStart()) {
        throw new ISE("can't start.");
    }/*from www.j  a  va2 s  .co  m*/

    if (authenticatorMapper == null || authenticatorMapper.getAuthenticatorMap() == null) {
        return;
    }

    try {
        LOG.info("Starting CoordinatorBasicAuthenticatorMetadataStorageUpdater.");
        for (Map.Entry<String, Authenticator> entry : authenticatorMapper.getAuthenticatorMap().entrySet()) {
            Authenticator authenticator = entry.getValue();
            if (authenticator instanceof BasicHTTPAuthenticator) {
                String authenticatorName = entry.getKey();
                authenticatorPrefixes.add(authenticatorName);
                BasicHTTPAuthenticator basicHTTPAuthenticator = (BasicHTTPAuthenticator) authenticator;
                BasicAuthDBConfig dbConfig = basicHTTPAuthenticator.getDbConfig();
                byte[] userMapBytes = getCurrentUserMapBytes(authenticatorName);
                Map<String, BasicAuthenticatorUser> userMap = BasicAuthUtils
                        .deserializeAuthenticatorUserMap(objectMapper, userMapBytes);
                cachedUserMaps.put(authenticatorName,
                        new BasicAuthenticatorUserMapBundle(userMap, userMapBytes));

                if (dbConfig.getInitialAdminPassword() != null
                        && !userMap.containsKey(BasicAuthUtils.ADMIN_NAME)) {
                    createUserInternal(authenticatorName, BasicAuthUtils.ADMIN_NAME);
                    setUserCredentialsInternal(authenticatorName, BasicAuthUtils.ADMIN_NAME,
                            new BasicAuthenticatorCredentialUpdate(dbConfig.getInitialAdminPassword(),
                                    BasicAuthUtils.DEFAULT_KEY_ITERATIONS));
                }

                if (dbConfig.getInitialInternalClientPassword() != null
                        && !userMap.containsKey(BasicAuthUtils.INTERNAL_USER_NAME)) {
                    createUserInternal(authenticatorName, BasicAuthUtils.INTERNAL_USER_NAME);
                    setUserCredentialsInternal(authenticatorName, BasicAuthUtils.INTERNAL_USER_NAME,
                            new BasicAuthenticatorCredentialUpdate(dbConfig.getInitialInternalClientPassword(),
                                    BasicAuthUtils.DEFAULT_KEY_ITERATIONS));
                }
            }
        }

        ScheduledExecutors.scheduleWithFixedDelay(exec, new Duration(commonCacheConfig.getPollingPeriod()),
                new Duration(commonCacheConfig.getPollingPeriod()), new Callable<ScheduledExecutors.Signal>() {
                    @Override
                    public ScheduledExecutors.Signal call() throws Exception {
                        if (stopped) {
                            return ScheduledExecutors.Signal.STOP;
                        }
                        try {
                            LOG.debug("Scheduled db poll is running");
                            for (String authenticatorPrefix : authenticatorPrefixes) {

                                byte[] userMapBytes = getCurrentUserMapBytes(authenticatorPrefix);
                                Map<String, BasicAuthenticatorUser> userMap = BasicAuthUtils
                                        .deserializeAuthenticatorUserMap(objectMapper, userMapBytes);
                                if (userMapBytes != null) {
                                    cachedUserMaps.put(authenticatorPrefix,
                                            new BasicAuthenticatorUserMapBundle(userMap, userMapBytes));
                                }
                            }
                            LOG.debug("Scheduled db poll is done");
                        } catch (Throwable t) {
                            LOG.makeAlert(t, "Error occured while polling for cachedUserMaps.").emit();
                        }
                        return ScheduledExecutors.Signal.REPEAT;
                    }
                });

        lifecycleLock.started();
    } finally {
        lifecycleLock.exitStart();
    }
}