Example usage for com.google.common.base Stopwatch Stopwatch

List of usage examples for com.google.common.base Stopwatch Stopwatch

Introduction

In this page you can find the example usage for com.google.common.base Stopwatch Stopwatch.

Prototype

Stopwatch() 

Source Link

Usage

From source file:org.sonatype.nexus.proxy.storage.remote.httpclient.HttpClientRemoteStorage.java

/**
 * Executes the HTTP request.//from  w  w w  .j  ava 2  s . co m
 * <p/>
 * In case of any exception thrown by HttpClient, it will release the connection. In other cases it is the duty of
 * caller to do it, or process the input stream.
 *
 * @param repository to execute the HTTP method fpr
 * @param request resource store request that triggered the HTTP request
 * @param httpRequest HTTP request to be executed
 * @return response of making the request
 * @throws RemoteStorageException If an error occurred during execution of HTTP request
 */
@VisibleForTesting
HttpResponse executeRequest(final ProxyRepository repository, final ResourceStoreRequest request,
        final HttpUriRequest httpRequest) throws RemoteStorageException {
    final Stopwatch stopwatch = timingLog.isDebugEnabled() ? new Stopwatch().start() : null;
    try {
        return doExecuteRequest(repository, request, httpRequest);
    } finally {
        if (stopwatch != null) {
            stopwatch.stop();
            if (timingLog.isDebugEnabled()) {
                timingLog.debug("[{}] {} {} took {}", repository.getId(), httpRequest.getMethod(),
                        httpRequest.getURI(), stopwatch);
            }
        }
    }
}

From source file:cosmos.impl.CosmosImpl.java

@Override
public CloseableIterable<MultimapRecord> fetch(Store id)
        throws TableNotFoundException, UnexpectedStateException {
    checkNotNull(id);/*from ww w.j ava 2  s.  co m*/

    final String description = "Cosmos:fetch";
    Stopwatch sw = new Stopwatch().start();

    try {
        State s = PersistedStores.getState(id);

        if (!State.LOADING.equals(s) && !State.LOADED.equals(s)) {
            throw unexpectedState(id, new State[] { State.LOADING, State.LOADED }, s);
        }

        BatchScanner bs = id.connector().createBatchScanner(id.dataTable(), id.auths(), id.readThreads());
        bs.setRanges(Collections.singleton(Range.prefix(id.uuid())));
        bs.fetchColumnFamily(Defaults.DOCID_FIELD_NAME_TEXT);

        // Handles stoping the stopwatch
        return CloseableIterable.transform(bs, new IndexToMultimapRecord(this, id), id.tracer(), description,
                sw);
    } catch (TableNotFoundException e) {
        // In the exceptional case, stop the timer
        sw.stop();
        id.tracer().addTiming(description, sw.elapsed(TimeUnit.MILLISECONDS));
        throw e;
    } catch (UnexpectedStateException e) {
        // In the exceptional case, stop the timer
        sw.stop();
        id.tracer().addTiming(description, sw.elapsed(TimeUnit.MILLISECONDS));
        throw e;
    } catch (RuntimeException e) {
        // In the exceptional case, stop the timer
        sw.stop();
        id.tracer().addTiming(description, sw.elapsed(TimeUnit.MILLISECONDS));
        throw e;
    }
    // no finally as the trace is stopped by the CloseableIterable
}

From source file:org.jenkinsci.plugins.relution_publisher.builder.ArtifactFileUploader.java

private JsonObject uploadAsset(final File directory, final String fileName)
        throws URISyntaxException, InterruptedException {

    try {//from ww w . j  a  v a  2s .  com
        final Stopwatch sw = new Stopwatch();
        final File file = new File(directory, fileName);
        final ApiRequest request = RequestFactory.createUploadRequest(this.store, file);

        this.log.write(this, "Uploading \"%s\" (%,d Byte)", fileName, file.length());

        sw.start();
        final ApiResponse response = this.requestManager.execute(request, this.log);
        sw.stop();

        final String speed = this.getUploadSpeed(sw, file);
        this.log.write(this, "Upload of file completed (%s, %s).", sw, speed);

        return this.extractAsset(response);

    } catch (final IOException e) {
        this.log.write(this, "Upload of file failed, error during execution:\n\n%s\n", e);
        Builds.setResult(this, Result.UNSTABLE, this.log);

    } catch (final ExecutionException e) {
        this.log.write(this, "Upload of file failed, error during execution:\n\n%s\n", e);
        Builds.setResult(this, Result.UNSTABLE, this.log);

    }
    return null;
}

From source file:com.metamx.druid.indexing.coordinator.RemoteTaskRunner.java

/**
 * Creates a ZK entry under a specific path associated with a worker. The worker is responsible for
 * removing the task ZK entry and creating a task status ZK entry.
 *
 * @param theWorker          The worker the task is assigned to
 * @param taskRunnerWorkItem The task to be assigned
 *///  ww w.  jav  a  2s  . c om
private void announceTask(Worker theWorker, RemoteTaskRunnerWorkItem taskRunnerWorkItem) throws Exception {
    final Task task = taskRunnerWorkItem.getTask();

    log.info("Coordinator asking Worker[%s] to add task[%s]", theWorker.getHost(), task.getId());

    byte[] rawBytes = jsonMapper.writeValueAsBytes(task);
    if (rawBytes.length > config.getMaxNumBytes()) {
        throw new ISE("Length of raw bytes for task too large[%,d > %,d]", rawBytes.length,
                config.getMaxNumBytes());
    }

    String taskPath = JOINER.join(config.getIndexerTaskPath(), theWorker.getHost(), task.getId());

    if (cf.checkExists().forPath(taskPath) == null) {
        cf.create().withMode(CreateMode.EPHEMERAL).forPath(taskPath, rawBytes);
    }

    RemoteTaskRunnerWorkItem workItem = pendingTasks.remove(task.getId());
    if (workItem == null) {
        log.makeAlert("WTF?! Got a null work item from pending tasks?! How can this be?!")
                .addData("taskId", task.getId()).emit();
        return;
    }

    RemoteTaskRunnerWorkItem newWorkItem = workItem.withWorker(theWorker);
    runningTasks.put(task.getId(), newWorkItem);
    log.info("Task %s switched from pending to running (on [%s])", task.getId(),
            newWorkItem.getWorker().getHost());

    // Syncing state with Zookeeper - don't assign new tasks until the task we just assigned is actually running
    // on a worker - this avoids overflowing a worker with tasks
    Stopwatch timeoutStopwatch = new Stopwatch();
    timeoutStopwatch.start();
    synchronized (statusLock) {
        while (!isWorkerRunningTask(theWorker, task)) {
            statusLock.wait(config.getTaskAssignmentTimeoutDuration().getMillis());
            if (timeoutStopwatch.elapsed(TimeUnit.MILLISECONDS) >= config.getTaskAssignmentTimeoutDuration()
                    .getMillis()) {
                log.error("Something went wrong! %s never ran task %s after %s!", theWorker.getHost(),
                        task.getId(), config.getTaskAssignmentTimeoutDuration());

                taskRunnerWorkItem.setResult(TaskStatus.failure(taskRunnerWorkItem.getTask().getId()));
                break;
            }
        }
    }
}

From source file:cosmos.impl.CosmosImpl.java

@Override
public CloseableIterable<MultimapRecord> fetch(Store id, Column column, String value)
        throws TableNotFoundException, UnexpectedStateException {
    checkNotNull(id);// w  w w .j  a  v a 2s.c  o  m
    checkNotNull(column);
    checkNotNull(value);

    final String description = "Cosmos:fetchWithColumnValue";
    Stopwatch sw = new Stopwatch().start();

    try {
        State s = PersistedStores.getState(id);

        if (!State.LOADING.equals(s) && !State.LOADED.equals(s)) {
            throw unexpectedState(id, new State[] { State.LOADING, State.LOADED }, s);
        }

        BatchScanner bs = id.connector().createBatchScanner(id.dataTable(), id.auths(), id.readThreads());
        bs.setRanges(Collections.singleton(Range.exact(id.uuid() + Defaults.NULL_BYTE_STR + value)));
        bs.fetchColumnFamily(new Text(column.name()));

        // Filter on cq-prefix to only look at the ordering we want
        IteratorSetting filter = new IteratorSetting(50, "cqFilter", OrderFilter.class);
        filter.addOption(OrderFilter.PREFIX, Order.direction(Order.ASCENDING));
        bs.addScanIterator(filter);

        return CloseableIterable.transform(bs, new IndexToMultimapRecord(this, id), id.tracer(), description,
                sw);
    } catch (TableNotFoundException e) {
        // In the exceptional case, stop the timer
        sw.stop();
        id.tracer().addTiming(description, sw.elapsed(TimeUnit.MILLISECONDS));
        throw e;
    } catch (UnexpectedStateException e) {
        // In the exceptional case, stop the timer
        sw.stop();
        id.tracer().addTiming(description, sw.elapsed(TimeUnit.MILLISECONDS));
        throw e;
    } catch (RuntimeException e) {
        // In the exceptional case, stop the timer
        sw.stop();
        id.tracer().addTiming(description, sw.elapsed(TimeUnit.MILLISECONDS));
        throw e;
    }
    // no finally as the trace is stopped by the CloseableIterable
}

From source file:com.twitter.hraven.rest.RestJSONResource.java

@GET
@Path("appVersion/{cluster}/{user}/{appId}/")
@Produces(MediaType.APPLICATION_JSON)/* w  ww.  j  av  a2  s. c  o m*/
public List<VersionInfo> getDistinctVersions(@PathParam("cluster") String cluster,
        @PathParam("user") String user, @PathParam("appId") String appId, @QueryParam("limit") int limit)
        throws IOException {
    Stopwatch timer = new Stopwatch().start();

    if (LOG.isTraceEnabled()) {
        LOG.trace("Fetching App Versions for cluster=" + cluster + " user=" + user + " app=" + appId);
    }
    serializationContext.set(new SerializationContext(SerializationContext.DetailLevel.EVERYTHING));
    List<VersionInfo> distinctVersions = serviceThreadLocalAppVersion.get().getDistinctVersions(
            StringUtils.trimToEmpty(cluster), StringUtils.trimToEmpty(user), StringUtils.trimToEmpty(appId));
    timer.stop();

    LOG.info("For appVersion/{cluster}/{user}/{appId}/ with input query " + "appVersion/" + cluster + SLASH
            + user + SLASH + appId + "?limit=" + limit + " fetched #number of VersionInfo "
            + distinctVersions.size() + " in ");//+ timer);

    // export latency metrics
    HravenResponseMetrics.APPVERSIONS_API_LATENCY_VALUE.set(timer.elapsed(TimeUnit.MILLISECONDS));
    return distinctVersions;
}

From source file:com.twitter.hraven.datasource.AppSummaryService.java

/**
 * gets list of all apps in the specified time frame from the aggregate tables
 * @param cluster/*  w  w  w .j  a  va2s. com*/
 * @param user
 * @param startTime
 * @param endTime
 * @param limit
 * @return {@link List < AppSummary >}
 * @throws IOException
 */
public List<AppSummary> getAllApps(String cluster, String user, long startTime, long endTime, int limit)
        throws IOException {
    // set the time to top of the day minus 1 to make sure that timestamp is included
    long topDayEndTime = Long.MAX_VALUE - getTimestamp(endTime, AggregationConstants.AGGREGATION_TYPE.DAILY)
            - 1;
    // set the time to top of the day plus 1 to make sure that timestamp is included
    long topDayStartTime = Long.MAX_VALUE - getTimestamp(startTime, AggregationConstants.AGGREGATION_TYPE.DAILY)
            + 1;

    byte[] startRow = ByteUtil.join(Constants.SEP_BYTES, Bytes.toBytes(cluster), Bytes.toBytes(topDayEndTime));
    byte[] endRow = ByteUtil.join(Constants.SEP_BYTES, Bytes.toBytes(cluster), Bytes.toBytes(topDayStartTime));

    // start scanning agg table at cluster!inv timestamp![user]
    Scan scan = new Scan();

    if (StringUtils.isNotBlank(user)) {
        startRow = ByteUtil.join(Constants.SEP_BYTES, startRow, Bytes.toBytes(user));
        endRow = ByteUtil.join(Constants.SEP_BYTES, endRow, Bytes.toBytes(user));
        FilterList filters = new FilterList(FilterList.Operator.MUST_PASS_ALL);
        filters.addFilter(new SingleColumnValueFilter(Constants.INFO_FAM_BYTES, AggregationConstants.USER_BYTES,
                CompareFilter.CompareOp.EQUAL, Bytes.toBytes(user)));
        scan.setFilter(filters);
    }
    scan.setStartRow(startRow);
    scan.setStopRow(endRow);
    LOG.info(" scan is " + scan.toJSON());

    Map<AppKey, AppSummary> amap = new HashMap<AppKey, AppSummary>();
    Stopwatch apptimer = new Stopwatch();

    ResultScanner scanner = null;
    try {
        Stopwatch timer = new Stopwatch().start();
        int rowCount = 0;
        long colCount = 0;
        long resultSize = 0;
        scanner = aggDailyTable.getScanner(scan);
        for (Result result : scanner) {
            if (result != null && !result.isEmpty()) {
                rowCount++;
                colCount += result.size();
                resultSize += result.getWritableSize();
                apptimer.start();
                byte[] rowKey = result.getRow();
                AppAggregationKey appAggKey = aggConv.fromBytes(rowKey);
                AppKey ak = new AppKey(cluster, appAggKey.getUserName(), appAggKey.getAppId());
                AppSummary as1 = null;
                if (amap.containsKey(ak)) {
                    as1 = amap.get(ak);
                } else {
                    as1 = new AppSummary(ak);
                    as1.setFirstRunId(appAggKey.getAggregationId());
                    as1.setLastRunId(appAggKey.getAggregationId());
                }
                if (appAggKey.getAggregationId() < as1.getFirstRunId()) {
                    as1.setFirstRunId(appAggKey.getAggregationId());
                }
                if (appAggKey.getAggregationId() > as1.getLastRunId()) {
                    as1.setLastRunId(appAggKey.getAggregationId());
                }
                amap.put(ak, populateAppSummary(result, as1));
                if (amap.size() >= limit) {
                    break;
                }
                apptimer.stop();
            }
        }
        timer.stop();
        LOG.info(" Fetched from hbase " + rowCount + " rows, " + colCount + " columns, " + resultSize
                + " bytes ( " + resultSize / (1024 * 1024) + ") MB, in \n total timer of " + timer
                + " elapsedMillis:" + timer.elapsed(TimeUnit.MILLISECONDS)
                + " that includes \n appSummary population timer of " + apptimer + " elapsedMillis"
                + apptimer.elapsed(TimeUnit.MILLISECONDS) + " \n hbase scan time is "
                + (timer.elapsed(TimeUnit.MILLISECONDS) - apptimer.elapsed(TimeUnit.MILLISECONDS)));
    } finally {
        if (scanner != null) {
            scanner.close();
        }
    }
    LOG.info("Number of distinct apps " + amap.size());
    return new ArrayList<AppSummary>(amap.values());
}

From source file:cosmos.impl.CosmosImpl.java

@Override
public CloseableIterable<MultimapRecord> fetch(Store id, Index ordering, boolean duplicateUidsAllowed)
        throws TableNotFoundException, UnexpectedStateException, UnindexedColumnException {
    checkNotNull(id);/* w w w  .ja v a  2 s  .c  o  m*/
    checkNotNull(ordering);

    final String description = "Cosmos:fetchWithIndex";
    Stopwatch sw = new Stopwatch().start();

    try {
        State s = PersistedStores.getState(id);

        if (!State.LOADING.equals(s) && !State.LOADED.equals(s)) {
            throw unexpectedState(id, new State[] { State.LOADING, State.LOADED }, s);
        }

        Index.define(ordering.column());

        if (!id.columnsToIndex().contains(ordering)) {
            log.error("{} is not indexed by {}", ordering, id);

            throw new UnindexedColumnException();
        }

        Scanner scanner = id.connector().createScanner(id.dataTable(), id.auths());
        scanner.setRange(Range.prefix(id.uuid()));
        scanner.fetchColumnFamily(new Text(ordering.column().name()));
        scanner.setBatchSize(200);

        // Filter on cq-prefix to only look at the ordering we want
        IteratorSetting filter = new IteratorSetting(50, "cqFilter", OrderFilter.class);
        filter.addOption(OrderFilter.PREFIX, Order.direction(ordering.order()));
        scanner.addScanIterator(filter);

        // If the client has told us they don't want duplicate records, lets not give them duplicate records
        if (duplicateUidsAllowed) {
            return CloseableIterable.transform(scanner, new IndexToMultimapRecord(this, id), id.tracer(),
                    description, sw);
        } else {
            return CloseableIterable.filterAndTransform(scanner, new DedupingPredicate(),
                    new IndexToMultimapRecord(this, id), id.tracer(), description, sw);
        }
    } catch (TableNotFoundException e) {
        // In the exceptional case, stop the timer
        sw.stop();
        id.tracer().addTiming(description, sw.elapsed(TimeUnit.MILLISECONDS));
        throw e;
    } catch (UnexpectedStateException e) {
        // In the exceptional case, stop the timer
        sw.stop();
        id.tracer().addTiming(description, sw.elapsed(TimeUnit.MILLISECONDS));
        throw e;
    } catch (RuntimeException e) {
        // In the exceptional case, stop the timer
        sw.stop();
        id.tracer().addTiming(description, sw.elapsed(TimeUnit.MILLISECONDS));
        throw e;
    }
    // no finally as the trace is stopped by the CloseableIterable
}

From source file:uk.ac.open.kmi.iserve.sal.manager.impl.ConcurrentSparqlGraphStoreManager.java

@Override
public Set<URI> listResourcesByQuery(String queryStr, String variableName) {

    ImmutableSet.Builder<URI> result = ImmutableSet.builder();
    // If the SPARQL endpoint does not exist return immediately.
    if (this.getSparqlQueryEndpoint() == null || queryStr == null || queryStr.isEmpty()) {
        return result.build();
    }// w w  w.ja va  2s. co  m

    // Query the engine
    log.debug("Evaluating SPARQL query in Knowledge Base: \n {}", queryStr);
    Query query = QueryFactory.create(queryStr);
    QueryExecution qe = QueryExecutionFactory.sparqlService(this.getSparqlQueryEndpoint().toASCIIString(),
            query);
    MonitoredQueryExecution qexec = new MonitoredQueryExecution(qe);

    try {
        Stopwatch stopwatch = new Stopwatch();
        stopwatch.start();

        ResultSet qResults = qexec.execSelect();

        stopwatch.stop();
        log.debug("Time taken for querying the registry: {}", stopwatch);

        Resource resource;
        URI matchUri;
        // Iterate over the results obtained
        while (qResults.hasNext()) {
            QuerySolution soln = qResults.nextSolution();

            // Get the match URL
            resource = soln.getResource(variableName);

            if (resource != null && resource.isURIResource()) {
                matchUri = new URI(resource.getURI());
                result.add(matchUri);
            } else {
                log.warn("Skipping result as the URL is null");
                break;
            }
        }
    } catch (URISyntaxException e) {
        log.error("Error obtaining match result. Expected a correct URI", e);
    } finally {
        qexec.close();
    }
    return result.build();
}

From source file:org.apache.hadoop.hbase.zookeeper.MetaTableLocator.java

/**
 * Wait until the meta region is available and is not in transition.
 * @param zkw//from   w  ww.  j a  v a  2s  .co  m
 * @param replicaId
 * @param timeout
 * @return ServerName or null if we timed out.
 * @throws InterruptedException
 */
public ServerName blockUntilAvailable(final ZooKeeperWatcher zkw, int replicaId, final long timeout)
        throws InterruptedException {
    if (timeout < 0)
        throw new IllegalArgumentException();
    if (zkw == null)
        throw new IllegalArgumentException();
    Stopwatch sw = new Stopwatch().start();
    ServerName sn = null;
    try {
        while (true) {
            sn = getMetaRegionLocation(zkw, replicaId);
            if (sn != null || sw.elapsedMillis() > timeout - HConstants.SOCKET_RETRY_WAIT_MS) {
                break;
            }
            Thread.sleep(HConstants.SOCKET_RETRY_WAIT_MS);
        }
    } finally {
        sw.stop();
    }
    return sn;
}