List of usage examples for org.joda.time Duration getMillis
public long getMillis()
From source file:org.apache.beam.runners.samza.SamzaPipelineResult.java
License:Apache License
@Override public State waitUntilFinish(@Nullable Duration duration) { try {/*from w ww.ja va 2 s.co m*/ if (duration == null) { runner.waitForFinish(); } else { runner.waitForFinish(java.time.Duration.ofMillis(duration.getMillis())); } } catch (Exception e) { throw new Pipeline.PipelineExecutionException(e); } final StateInfo stateInfo = getStateInfo(); if (listener != null && (stateInfo.state == State.DONE || stateInfo.state == State.FAILED)) { listener.onFinish(); } if (stateInfo.state == State.FAILED) { throw stateInfo.error; } LOG.info("Pipeline finished. Final state: {}", stateInfo.state); return stateInfo.state; }
From source file:org.apache.beam.sdk.io.synthetic.delay.SyntheticDelay.java
License:Apache License
/** * Implements a mechanism to delay a thread in various fashions. * {@code CPU}: Burn CPU while * waiting. * {@code SLEEP}: Sleep uninterruptibly while waiting. * {@code MIXED}: Switch between * burning CPU and sleeping every millisecond to emulate a desired CPU utilization specified by * {@code cpuUtilizationInMixedDelay}./*from w ww. jav a 2 s . co m*/ * * @return Millis spent sleeping, does not include time spent spinning. */ public static long delay(Duration delay, double cpuUtilizationInMixedDelay, SyntheticOptions.DelayType delayType, Random rnd) { switch (delayType) { case CPU: cpuDelay(delay.getMillis()); return 0; case SLEEP: Uninterruptibles.sleepUninterruptibly(Math.max(0L, delay.getMillis()), TimeUnit.MILLISECONDS); return delay.getMillis(); case MIXED: // Mixed mode: for each millisecond of delay randomly choose to spin or sleep. // This is enforced at millisecond granularity since that is the minimum duration that // Thread.sleep() can sleep. Millisecond is also the unit of processing delay. long sleepMillis = 0; for (long i = 0; i < delay.getMillis(); i++) { if (rnd.nextDouble() < cpuUtilizationInMixedDelay) { delay(new Duration(1), 0.0, SyntheticOptions.DelayType.CPU, rnd); } else { sleepMillis += delay(new Duration(1), 0.0, SyntheticOptions.DelayType.SLEEP, rnd); } } return sleepMillis; default: throw new IllegalArgumentException("Unknown delay type " + delayType); } }
From source file:org.apache.beam.sdk.io.synthetic.SyntheticStep.java
License:Apache License
private KV<byte[], byte[]> outputElement(byte[] inputKey, byte[] inputValue, long inputValueHashcode, int index, Random random) {/*from w w w .j a v a2s. c o m*/ long seed = options.hashFunction().hashLong(inputValueHashcode + index).asLong(); Duration delay = Duration.millis(options.nextDelay(seed)); long millisecondsSpentSleeping = 0; while (delay.getMillis() > 0) { millisecondsSpentSleeping += delay(delay, options.cpuUtilizationInMixedDelay, options.delayType, random); if (isWithinThroughputLimit()) { break; } else { // try an extra delay of 1 millisecond delay = Duration.millis(1); } } reportThrottlingTimeMetrics(millisecondsSpentSleeping); if (options.preservesInputKeyDistribution) { // Generate the new byte array value whose hashcode will be // used as seed to initialize a Random object in next stages. byte[] newValue = new byte[inputValue.length]; random.nextBytes(newValue); return KV.of(inputKey, newValue); } else { return options.genKvPair(seed); } }
From source file:org.apache.beam.sdk.io.synthetic.SyntheticUtils.java
License:Apache License
/** * Implements a mechanism to delay a thread in various fashions. * {@code CPU}: Burn CPU while * waiting. * {@code SLEEP}: Sleep uninterruptibly while waiting. * {@code MIXED}: Switch between * burning CPU and sleeping every millisecond to emulate a desired CPU utilization specified by * {@code cpuUtilizationInMixedDelay}./*w ww. ja v a 2s. com*/ * * @return Millis spent sleeping, does not include time spent spinning. */ static long delay(Duration delay, double cpuUtilizationInMixedDelay, SyntheticOptions.DelayType delayType, Random rnd) { switch (delayType) { case CPU: cpuDelay(delay.getMillis()); return 0; case SLEEP: Uninterruptibles.sleepUninterruptibly(Math.max(0L, delay.getMillis()), TimeUnit.MILLISECONDS); return delay.getMillis(); case MIXED: // Mixed mode: for each millisecond of delay randomly choose to spin or sleep. // This is enforced at millisecond granularity since that is the minimum duration that // Thread.sleep() can sleep. Millisecond is also the unit of processing delay. long sleepMillis = 0; for (long i = 0; i < delay.getMillis(); i++) { if (rnd.nextDouble() < cpuUtilizationInMixedDelay) { delay(new Duration(1), 0.0, SyntheticOptions.DelayType.CPU, rnd); } else { sleepMillis += delay(new Duration(1), 0.0, SyntheticOptions.DelayType.SLEEP, rnd); } } return sleepMillis; default: throw new IllegalArgumentException("Unknown delay type " + delayType); } }
From source file:org.apache.beam.sdk.nexmark.queries.NexmarkQueryModel.java
License:Apache License
/** * Return the start of the most recent window of {@code size} and {@code period} which ends * strictly before {@code timestamp}./*from ww w .ja v a 2s .c o m*/ */ static Instant windowStart(Duration size, Duration period, Instant timestamp) { long ts = timestamp.getMillis(); long p = period.getMillis(); long lim = ts - ts % p; long s = size.getMillis(); return new Instant(lim - s); }
From source file:org.apache.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 w w w. j av a 2 s. c o m*/ final FullResponseHolder fullResponseHolder; log.info("Submitting action for task[%s] to overlord: [%s].", task.getId(), taskAction); fullResponseHolder = druidLeaderClient .go(druidLeaderClient.makeRequest(HttpMethod.POST, "/druid/indexer/v1/action") .setContent(MediaType.APPLICATION_JSON, dataToSend)); if (fullResponseHolder.getStatus().getCode() / 100 == 2) { final Map<String, Object> responseDict = jsonMapper.readValue(fullResponseHolder.getContent(), JacksonUtils.TYPE_REFERENCE_MAP_STRING_OBJECT); return jsonMapper.convertValue(responseDict.get("result"), taskAction.getReturnTypeReference()); } else { // Want to retry, so throw an IOException. throw new IOE("Error with status[%s] and message[%s]. Check overlord logs for details.", fullResponseHolder.getStatus(), fullResponseHolder.getContent()); } } 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 new RuntimeException(e2); } } } catch (InterruptedException e) { throw new RuntimeException(e); } } }
From source file:org.apache.druid.indexing.common.IndexTaskClient.java
License:Apache License
/** * Sends an HTTP request to the task of the specified {@code taskId} and returns a response if it succeeded. *///from w w w. ja va 2s . c o m private FullResponseHolder submitRequest(String taskId, @Nullable String mediaType, // nullable if content is empty HttpMethod method, String encodedPathSuffix, @Nullable String encodedQueryString, byte[] content, boolean retry) throws IOException, ChannelException, NoTaskLocationException { final RetryPolicy retryPolicy = retryPolicyFactory.makeRetryPolicy(); while (true) { String path = StringUtils.format("%s/%s/%s", BASE_PATH, StringUtils.urlEncode(taskId), encodedPathSuffix); Optional<TaskStatus> status = taskInfoProvider.getTaskStatus(taskId); if (!status.isPresent() || !status.get().isRunnable()) { throw new TaskNotRunnableException( StringUtils.format("Aborting request because task [%s] is not runnable", taskId)); } final TaskLocation location = taskInfoProvider.getTaskLocation(taskId); if (location.equals(TaskLocation.unknown())) { throw new NoTaskLocationException( StringUtils.format("No TaskLocation available for task [%s]", taskId)); } final Request request = createRequest(taskId, location, path, encodedQueryString, method, mediaType, content); FullResponseHolder response = null; try { // 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(request.getUrl().getHost(), request.getUrl().getPort()); response = submitRequest(request); 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] and content [%s]", responseCode, response.getContent()); } } 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 = StringUtils .urlDecode(response.getResponse().headers().get(ChatHandlerResource.TASK_ID_HEADER)); if (headerId != null && !headerId.equals(taskId)) { log.warn("Expected worker to have taskId [%s] but has taskId [%s], will retry in [%d]s", taskId, headerId, TASK_MISMATCH_RETRY_DELAY_SECONDS); delay = Duration.standardSeconds(TASK_MISMATCH_RETRY_DELAY_SECONDS); } else { delay = retryPolicy.getAndIncrementRetryDelay(); } } else { delay = retryPolicy.getAndIncrementRetryDelay(); } final String urlForLog = request.getUrl().toString(); 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()); throw e; } else if (delay == null) { log.warn(e, "Retries exhausted for [%s], last exception:", urlForLog); throw 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) { Thread.currentThread().interrupt(); e.addSuppressed(e2); throw new RuntimeException(e); } } } 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", taskId); throw e; } catch (Exception e) { log.warn(e, "Exception while sending request"); throw e; } } }
From source file:org.apache.druid.indexing.firehose.IngestSegmentFirehoseFactory.java
License:Apache License
private List<TimelineObjectHolder<String, DataSegment>> getTimelineForInterval() { Preconditions.checkNotNull(interval); // This call used to use the TaskActionClient, so for compatibility we use the same retry configuration // as TaskActionClient. final RetryPolicy retryPolicy = retryPolicyFactory.makeRetryPolicy(); List<DataSegment> usedSegments; while (true) { try {/*from w w w . ja v a2 s . c o m*/ usedSegments = coordinatorClient.getDatabaseSegmentDataSourceSegments(dataSource, Collections.singletonList(interval)); break; } catch (Throwable e) { log.warn(e, "Exception getting database segments"); final Duration delay = retryPolicy.getAndIncrementRetryDelay(); if (delay == null) { throw e; } else { final long sleepTime = jitter(delay.getMillis()); log.info("Will try again in [%s].", new Duration(sleepTime).toString()); try { Thread.sleep(sleepTime); } catch (InterruptedException e2) { throw new RuntimeException(e2); } } } } return VersionedIntervalTimeline.forSegments(usedSegments).lookup(interval); }
From source file:org.apache.druid.indexing.materializedview.MaterializedViewSupervisor.java
License:Apache License
@Override public void start() { synchronized (stateLock) { Preconditions.checkState(!started, "already started"); DataSourceMetadata metadata = metadataStorageCoordinator.getDataSourceMetadata(dataSource); if (null == metadata) { metadataStorageCoordinator.insertDataSourceMetadata(dataSource, new DerivativeDataSourceMetadata( spec.getBaseDataSource(), spec.getDimensions(), spec.getMetrics())); }/* w w w . ja va2 s .co m*/ exec = MoreExecutors.listeningDecorator(Execs.scheduledSingleThreaded(supervisorId)); final Duration delay = config.getTaskCheckDuration().toStandardDuration(); future = exec.scheduleWithFixedDelay(MaterializedViewSupervisor.this::run, 0, delay.getMillis(), TimeUnit.MILLISECONDS); started = true; } }
From source file:org.apache.druid.indexing.seekablestream.SeekableStreamIndexTaskClient.java
License:Apache License
public Map<PartitionIdType, SequenceOffsetType> pause(final String id) { log.debug("Pause task[%s]", id); try {//ww w.j av a2 s.co m final FullResponseHolder response = submitRequestWithEmptyContent(id, HttpMethod.POST, "pause", null, true); final HttpResponseStatus responseStatus = response.getStatus(); final String responseContent = response.getContent(); if (responseStatus.equals(HttpResponseStatus.OK)) { log.info("Task [%s] paused successfully", id); return deserializeMap(responseContent, Map.class, getPartitionType(), getSequenceType()); } else if (responseStatus.equals(HttpResponseStatus.ACCEPTED)) { // The task received the pause request, but its status hasn't been changed yet. while (true) { final SeekableStreamIndexTaskRunner.Status status = getStatus(id); if (status == SeekableStreamIndexTaskRunner.Status.PAUSED) { return getCurrentOffsets(id, true); } final Duration delay = newRetryPolicy().getAndIncrementRetryDelay(); if (delay == null) { throw new ISE("Task [%s] failed to change its status from [%s] to [%s], aborting", id, status, SeekableStreamIndexTaskRunner.Status.PAUSED); } else { final long sleepTime = delay.getMillis(); log.info("Still waiting for task [%s] to change its status to [%s]; will try again in [%s]", id, SeekableStreamIndexTaskRunner.Status.PAUSED, new Duration(sleepTime).toString()); Thread.sleep(sleepTime); } } } else { throw new ISE("Pause request for task [%s] failed with response [%s] : [%s]", id, responseStatus, responseContent); } } catch (NoTaskLocationException e) { log.error("Exception [%s] while pausing Task [%s]", e.getMessage(), id); return ImmutableMap.of(); } catch (IOException | InterruptedException e) { throw new RE(e, "Exception [%s] while pausing Task [%s]", e.getMessage(), id); } }