Example usage for java.util.concurrent TimeUnit NANOSECONDS

List of usage examples for java.util.concurrent TimeUnit NANOSECONDS

Introduction

In this page you can find the example usage for java.util.concurrent TimeUnit NANOSECONDS.

Prototype

TimeUnit NANOSECONDS

To view the source code for java.util.concurrent TimeUnit NANOSECONDS.

Click Source Link

Document

Time unit representing one thousandth of a microsecond.

Usage

From source file:org.apache.drill.exec.store.mongo.MongoGroupScan.java

@Override
public List<EndpointAffinity> getOperatorAffinity() {
    watch.reset();/*from w  w  w. j a va 2  s. com*/
    watch.start();

    Map<String, DrillbitEndpoint> endpointMap = Maps.newHashMap();
    for (DrillbitEndpoint endpoint : storagePlugin.getContext().getBits()) {
        endpointMap.put(endpoint.getAddress(), endpoint);
        logger.debug("Endpoint address: {}", endpoint.getAddress());
    }

    Map<DrillbitEndpoint, EndpointAffinity> affinityMap = Maps.newHashMap();
    // As of now, considering only the first replica, though there may be
    // multiple replicas for each chunk.
    for (Set<ServerAddress> addressList : chunksMapping.values()) {
        // Each replica can be on multiple machines, take the first one, which
        // meets affinity.
        for (ServerAddress address : addressList) {
            DrillbitEndpoint ep = endpointMap.get(address.getHost());
            if (ep != null) {
                EndpointAffinity affinity = affinityMap.get(ep);
                if (affinity == null) {
                    affinityMap.put(ep, new EndpointAffinity(ep, 1));
                } else {
                    affinity.addAffinity(1);
                }
                break;
            }
        }
    }
    logger.debug("Took {} s to get operator affinity", watch.elapsed(TimeUnit.NANOSECONDS) / 1000);
    logger.debug("Affined drillbits : " + affinityMap.values());
    return Lists.newArrayList(affinityMap.values());
}

From source file:org.apache.hadoop.hdfs.client.ShortCircuitCache.java

/**
 * Demote old evictable mmaps into the regular eviction map.
 *
 * You must hold the cache lock while calling this function.
 *
 * @param now   Current time in monotonic milliseconds.
 * @return      Number of replicas demoted.
 *//*from w  w w  . j a va2 s .  c o m*/
private int demoteOldEvictableMmaped(long now) {
    int numDemoted = 0;
    boolean needMoreSpace = false;
    Long evictionTimeNs = Long.valueOf(0);

    while (true) {
        Entry<Long, ShortCircuitReplica> entry = evictableMmapped.ceilingEntry(evictionTimeNs);
        if (entry == null)
            break;
        evictionTimeNs = entry.getKey();
        long evictionTimeMs = TimeUnit.MILLISECONDS.convert(evictionTimeNs, TimeUnit.NANOSECONDS);
        if (evictionTimeMs + maxEvictableMmapedLifespanMs >= now) {
            if (evictableMmapped.size() < maxEvictableMmapedSize) {
                break;
            }
            needMoreSpace = true;
        }
        ShortCircuitReplica replica = entry.getValue();
        if (LOG.isTraceEnabled()) {
            String rationale = needMoreSpace ? "because we need more space" : "because it's too old";
            LOG.trace("demoteOldEvictable: demoting " + replica + ": " + rationale + ": "
                    + StringUtils.getStackTrace(Thread.currentThread()));
        }
        removeEvictable(replica, evictableMmapped);
        munmap(replica);
        insertEvictable(evictionTimeNs, replica, evictable);
        numDemoted++;
    }
    return numDemoted;
}

From source file:com.vmware.identity.interop.ldap.LdapConnection.java

private <T> T execute(Callable<T> ldapOp) {
    T result = null;//from w ww . ja  v  a2s  .co m
    long startedAt = System.nanoTime();
    try {
        result = ldapOp.call();
    } catch (LdapException le) {
        throw le;
    } catch (Exception e) {
        throw new LdapException(LdapErrors.LDAP_OTHER.getCode(), e.getMessage());
    } finally {
        if (perfLog.isTraceEnabled()) {
            perfLog.trace(String.format("ldap operation took [%d]ms",
                    TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startedAt)));
        }
    }

    return result;
}

From source file:com.netflix.spinnaker.front50.model.GcsStorageService.java

private <T extends Timestamped> T deserialize(StorageObject object, Class<T> clas, boolean current_version)
        throws java.io.UnsupportedEncodingException {
    try {/*  w  w w  .  j a va  2 s  .c o  m*/
        ByteArrayOutputStream output = new java.io.ByteArrayOutputStream();
        Storage.Objects.Get getter = obj_api.get(object.getBucket(), object.getName());
        if (!current_version) {
            getter.setGeneration(object.getGeneration());
        }

        Closure timeExecuteClosure = new Closure<String>(this, this) {
            public Object doCall() throws Exception {
                Clock clock = registry.clock();
                long startTime = clock.monotonicTime();
                int statusCode = -1;

                try {
                    getter.executeMediaAndDownloadTo(output);
                    statusCode = getter.getLastStatusCode();
                    if (statusCode < 0) {
                        // getLastStatusCode is returning -1
                        statusCode = 200;
                    }
                } catch (HttpResponseException e) {
                    statusCode = e.getStatusCode();
                    throw e;
                } catch (Exception e) {
                    log.error("mediaDownload exception from {}", object.getName(), e);
                    throw e;
                } finally {
                    long nanos = clock.monotonicTime() - startTime;
                    String status = Integer.toString(statusCode).charAt(0) + "xx";
                    Id id = mediaDownloadTimer.withTags("status", status, "statusCode",
                            Integer.toString(statusCode));
                    registry.timer(id).record(nanos, TimeUnit.NANOSECONDS);
                }
                return Closure.DONE;
            }
        };
        doRetry(timeExecuteClosure, "deserialize", object.getName());

        String json = output.toString("UTF8");
        return objectMapper.readValue(json, clas);
    } catch (Exception ex) {
        if (current_version) {
            log.error("Error reading {}: ", value("object", object.getName()), ex);
        } else {
            log.error("Error reading {} generation={}: ", value("object", object.getName()),
                    value("generation", object.getGeneration()), ex);
        }
        return null;
    }
}

From source file:org.elasticsearch.client.RestClient.java

private void performRequestAsync(final long startTime, final HostTuple<Iterator<HttpHost>> hostTuple,
        final HttpRequestBase request, final Set<Integer> ignoreErrorCodes,
        final HttpAsyncResponseConsumerFactory httpAsyncResponseConsumerFactory,
        final FailureTrackingResponseListener listener) {
    final HttpHost host = hostTuple.hosts.next();
    //we stream the request body if the entity allows for it
    final HttpAsyncRequestProducer requestProducer = HttpAsyncMethods.create(host, request);
    final HttpAsyncResponseConsumer<HttpResponse> asyncResponseConsumer = httpAsyncResponseConsumerFactory
            .createHttpAsyncResponseConsumer();
    final HttpClientContext context = HttpClientContext.create();
    context.setAuthCache(hostTuple.authCache);
    client.execute(requestProducer, asyncResponseConsumer, context, new FutureCallback<HttpResponse>() {
        @Override//w w w  .ja  v  a 2 s .  c  o  m
        public void completed(HttpResponse httpResponse) {
            try {
                RequestLogger.logResponse(logger, request, host, httpResponse);
                int statusCode = httpResponse.getStatusLine().getStatusCode();
                Response response = new Response(request.getRequestLine(), host, httpResponse);
                if (isSuccessfulResponse(statusCode)
                        || ignoreErrorCodes.contains(response.getStatusLine().getStatusCode())) {
                    onResponse(host);
                    listener.onSuccess(response);
                } else {
                    ResponseException responseException = new ResponseException(response);
                    if (isRetryStatus(statusCode)) {
                        //mark host dead and retry against next one
                        onFailure(host);
                        retryIfPossible(responseException);
                    } else {
                        //mark host alive and don't retry, as the error should be a request problem
                        onResponse(host);
                        listener.onDefinitiveFailure(responseException);
                    }
                }
            } catch (Exception e) {
                listener.onDefinitiveFailure(e);
            }
        }

        @Override
        public void failed(Exception failure) {
            try {
                RequestLogger.logFailedRequest(logger, request, host, failure);
                onFailure(host);
                retryIfPossible(failure);
            } catch (Exception e) {
                listener.onDefinitiveFailure(e);
            }
        }

        private void retryIfPossible(Exception exception) {
            if (hostTuple.hosts.hasNext()) {
                //in case we are retrying, check whether maxRetryTimeout has been reached
                long timeElapsedMillis = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - startTime);
                long timeout = maxRetryTimeoutMillis - timeElapsedMillis;
                if (timeout <= 0) {
                    IOException retryTimeoutException = new IOException(
                            "request retries exceeded max retry timeout [" + maxRetryTimeoutMillis + "]");
                    listener.onDefinitiveFailure(retryTimeoutException);
                } else {
                    listener.trackFailure(exception);
                    request.reset();
                    performRequestAsync(startTime, hostTuple, request, ignoreErrorCodes,
                            httpAsyncResponseConsumerFactory, listener);
                }
            } else {
                listener.onDefinitiveFailure(exception);
            }
        }

        @Override
        public void cancelled() {
            listener.onDefinitiveFailure(new ExecutionException("request was cancelled", null));
        }
    });
}

From source file:com.streamsets.pipeline.stage.origin.jdbc.AbstractTableJdbcSource.java

private synchronized boolean shutdownExecutorIfNeeded() {
    AtomicBoolean interrupted = new AtomicBoolean(false);
    Optional.ofNullable(executorService).ifPresent(executor -> {
        if (!executor.isTerminated()) {
            LOG.info("Shutting down executor service");
            executor.shutdown();//from w  w w  . ja v  a 2  s  . com
            try {
                executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
            } catch (InterruptedException e) {
                LOG.warn("Shutdown interrupted");
                interrupted.set(true);
            }
        }
    });
    return interrupted.get();
}

From source file:org.apache.geode.internal.InternalDataSerializer.java

private static void initializeWellKnownSerializers() {
    // ArrayBlockingQueue does not have zero-arg constructor
    // LinkedBlockingQueue does have zero-arg constructor but no way to get capacity

    classesToSerializers.put("java.lang.String", new WellKnownPdxDS() {
        @Override/*w w  w.j  ava2s.c  o m*/
        public boolean toData(Object o, DataOutput out) throws IOException {
            try {
                writeString((String) o, out);
            } catch (UTFDataFormatException ex) {
                // See bug 30428
                String s = "While writing a String of length " + ((String) o).length();
                UTFDataFormatException ex2 = new UTFDataFormatException(s);
                ex2.initCause(ex);
                throw ex2;
            }
            return true;
        }
    });
    classesToSerializers.put("java.net.InetAddress", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            InetAddress address = (InetAddress) o;
            out.writeByte(INET_ADDRESS);
            writeInetAddress(address, out);
            return true;
        }
    });
    classesToSerializers.put("java.net.Inet4Address", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            InetAddress address = (InetAddress) o;
            out.writeByte(INET_ADDRESS);
            writeInetAddress(address, out);
            return true;
        }
    });
    classesToSerializers.put("java.net.Inet6Address", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            InetAddress address = (InetAddress) o;
            out.writeByte(INET_ADDRESS);
            writeInetAddress(address, out);
            return true;
        }
    });
    classesToSerializers.put("java.lang.Class", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Class c = (Class) o;
            if (c.isPrimitive()) {
                writePrimitiveClass(c, out);
            } else {
                out.writeByte(CLASS);
                writeClass(c, out);
            }
            return true;
        }
    });
    classesToSerializers.put("java.lang.Boolean", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Boolean value = (Boolean) o;
            out.writeByte(BOOLEAN);
            writeBoolean(value, out);
            return true;
        }
    });
    classesToSerializers.put("java.lang.Character", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Character value = (Character) o;
            out.writeByte(CHARACTER);
            writeCharacter(value, out);
            return true;
        }
    });
    classesToSerializers.put("java.lang.Byte", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Byte value = (Byte) o;
            out.writeByte(BYTE);
            writeByte(value, out);
            return true;
        }
    });
    classesToSerializers.put("java.lang.Short", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Short value = (Short) o;
            out.writeByte(SHORT);
            writeShort(value, out);
            return true;
        }
    });
    classesToSerializers.put("java.lang.Integer", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Integer value = (Integer) o;
            out.writeByte(INTEGER);
            writeInteger(value, out);
            return true;
        }
    });
    classesToSerializers.put("java.lang.Long", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Long value = (Long) o;
            out.writeByte(LONG);
            writeLong(value, out);
            return true;
        }
    });
    classesToSerializers.put("java.lang.Float", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Float value = (Float) o;
            out.writeByte(FLOAT);
            writeFloat(value, out);
            return true;
        }
    });
    classesToSerializers.put("java.lang.Double", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Double value = (Double) o;
            out.writeByte(DOUBLE);
            writeDouble(value, out);
            return true;
        }
    });
    classesToSerializers.put("[Z", // boolean[]
            new WellKnownPdxDS() {
                @Override
                public boolean toData(Object o, DataOutput out) throws IOException {
                    out.writeByte(BOOLEAN_ARRAY);
                    writeBooleanArray((boolean[]) o, out);
                    return true;
                }
            });
    classesToSerializers.put("[B", // byte[]
            new WellKnownPdxDS() {
                @Override
                public boolean toData(Object o, DataOutput out) throws IOException {
                    byte[] array = (byte[]) o;
                    out.writeByte(BYTE_ARRAY);
                    writeByteArray(array, out);
                    return true;
                }
            });
    classesToSerializers.put("[C", // char[]
            new WellKnownPdxDS() {
                @Override
                public boolean toData(Object o, DataOutput out) throws IOException {
                    out.writeByte(CHAR_ARRAY);
                    writeCharArray((char[]) o, out);
                    return true;
                }
            });
    classesToSerializers.put("[D", // double[]
            new WellKnownPdxDS() {
                @Override
                public boolean toData(Object o, DataOutput out) throws IOException {
                    double[] array = (double[]) o;
                    out.writeByte(DOUBLE_ARRAY);
                    writeDoubleArray(array, out);
                    return true;
                }
            });
    classesToSerializers.put("[F", // float[]
            new WellKnownPdxDS() {
                @Override
                public boolean toData(Object o, DataOutput out) throws IOException {
                    float[] array = (float[]) o;
                    out.writeByte(FLOAT_ARRAY);
                    writeFloatArray(array, out);
                    return true;
                }
            });
    classesToSerializers.put("[I", // int[]
            new WellKnownPdxDS() {
                @Override
                public boolean toData(Object o, DataOutput out) throws IOException {
                    int[] array = (int[]) o;
                    out.writeByte(INT_ARRAY);
                    writeIntArray(array, out);
                    return true;
                }
            });
    classesToSerializers.put("[J", // long[]
            new WellKnownPdxDS() {
                @Override
                public boolean toData(Object o, DataOutput out) throws IOException {
                    long[] array = (long[]) o;
                    out.writeByte(LONG_ARRAY);
                    writeLongArray(array, out);
                    return true;
                }
            });
    classesToSerializers.put("[S", // short[]
            new WellKnownPdxDS() {
                @Override
                public boolean toData(Object o, DataOutput out) throws IOException {
                    short[] array = (short[]) o;
                    out.writeByte(SHORT_ARRAY);
                    writeShortArray(array, out);
                    return true;
                }
            });
    classesToSerializers.put("[Ljava.lang.String;", // String[]
            new WellKnownPdxDS() {
                @Override
                public boolean toData(Object o, DataOutput out) throws IOException {
                    String[] array = (String[]) o;
                    out.writeByte(STRING_ARRAY);
                    writeStringArray(array, out);
                    return true;
                }
            });
    classesToSerializers.put(TimeUnit.NANOSECONDS.getClass().getName(), new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(TIME_UNIT);
            out.writeByte(TIME_UNIT_NANOSECONDS);
            return true;
        }
    });
    classesToSerializers.put(TimeUnit.MICROSECONDS.getClass().getName(), new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(TIME_UNIT);
            out.writeByte(TIME_UNIT_MICROSECONDS);
            return true;
        }
    });
    classesToSerializers.put(TimeUnit.MILLISECONDS.getClass().getName(), new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(TIME_UNIT);
            out.writeByte(TIME_UNIT_MILLISECONDS);
            return true;
        }
    });
    classesToSerializers.put(TimeUnit.SECONDS.getClass().getName(), new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(TIME_UNIT);
            out.writeByte(TIME_UNIT_SECONDS);
            return true;
        }
    });
    classesToSerializers.put("java.util.Date", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Date date = (Date) o;
            out.writeByte(DATE);
            writeDate(date, out);
            return true;
        }
    });
    classesToSerializers.put("java.io.File", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            File file = (File) o;
            out.writeByte(FILE);
            writeFile(file, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.ArrayList", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            ArrayList list = (ArrayList) o;
            out.writeByte(ARRAY_LIST);
            writeArrayList(list, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.LinkedList", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            LinkedList list = (LinkedList) o;
            out.writeByte(LINKED_LIST);
            writeLinkedList(list, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.Vector", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(VECTOR);
            writeVector((Vector) o, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.Stack", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(STACK);
            writeStack((Stack) o, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.HashSet", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            HashSet list = (HashSet) o;
            out.writeByte(HASH_SET);
            writeHashSet(list, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.LinkedHashSet", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(LINKED_HASH_SET);
            writeLinkedHashSet((LinkedHashSet) o, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.HashMap", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            HashMap list = (HashMap) o;
            out.writeByte(HASH_MAP);
            writeHashMap(list, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.IdentityHashMap", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(IDENTITY_HASH_MAP);
            writeIdentityHashMap((IdentityHashMap) o, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.Hashtable", new WellKnownPdxDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(HASH_TABLE);
            writeHashtable((Hashtable) o, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.Properties", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            Properties props = (Properties) o;
            out.writeByte(PROPERTIES);
            writeProperties(props, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.TreeMap", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(TREE_MAP);
            writeTreeMap((TreeMap) o, out);
            return true;
        }
    });
    classesToSerializers.put("java.util.TreeSet", new WellKnownDS() {
        @Override
        public boolean toData(Object o, DataOutput out) throws IOException {
            out.writeByte(TREE_SET);
            writeTreeSet((TreeSet) o, out);
            return true;
        }
    });
    if (is662SerializationEnabled()) {
        classesToSerializers.put("java.math.BigInteger", new WellKnownDS() {
            @Override
            public boolean toData(Object o, DataOutput out) throws IOException {
                out.writeByte(BIG_INTEGER);
                writeBigInteger((BigInteger) o, out);
                return true;
            }
        });
        classesToSerializers.put("java.math.BigDecimal", new WellKnownDS() {
            @Override
            public boolean toData(Object o, DataOutput out) throws IOException {
                out.writeByte(BIG_DECIMAL);
                writeBigDecimal((BigDecimal) o, out);
                return true;
            }
        });
        classesToSerializers.put("java.util.UUID", new WellKnownDS() {
            @Override
            public boolean toData(Object o, DataOutput out) throws IOException {
                out.writeByte(UUID);
                writeUUID((UUID) o, out);
                return true;
            }
        });
        classesToSerializers.put("java.sql.Timestamp", new WellKnownDS() {
            @Override
            public boolean toData(Object o, DataOutput out) throws IOException {
                out.writeByte(TIMESTAMP);
                writeTimestamp((Timestamp) o, out);
                return true;
            }
        });
    }
}

From source file:com.netflix.genie.core.services.impl.JobCoordinatorServiceImpl.java

private Command getCommand(final JobRequest jobRequest, final Cluster cluster) throws GenieException {
    final long start = System.nanoTime();
    final Map<String, String> tags = MetricsUtils.newSuccessTagsMap();
    try {/*from  w  ww.j a  va2 s.c  o m*/
        final String clusterId = cluster.getId().orElseThrow(() -> new GenieServerException("No cluster id."));
        final String jobId = jobRequest.getId().orElseThrow(() -> new GenieServerException("No job id"));
        log.info("Selecting command attached to cluster {} for job {} ", clusterId, jobId);
        final Set<String> commandCriteria = jobRequest.getCommandCriteria();
        // TODO: what happens if the get method throws an error we don't mark the job failed here
        for (final Command command : this.clusterService.getCommandsForCluster(clusterId,
                this.commandStatuses)) {
            if (command.getTags().containsAll(jobRequest.getCommandCriteria())) {
                log.info("Selected command {} for job {} ", command.getId().orElse(NO_ID_FOUND),
                        jobRequest.getId().orElse(NO_ID_FOUND));
                return command;
            }
        }

        throw new GeniePreconditionException("No command found matching all command criteria ["
                + commandCriteria + "] attached to cluster with id: " + cluster.getId().orElse(NO_ID_FOUND));

    } catch (Throwable t) {
        MetricsUtils.addFailureTagsWithException(tags, t);
        throw t;
    } finally {
        this.registry.timer(selectCommandTimerId.withTags(tags)).record(System.nanoTime() - start,
                TimeUnit.NANOSECONDS);
    }
}

From source file:io.nats.client.ITClusterTest.java

@Test
public void testTimeoutOnNoServers() throws Exception {
    int maxRecon = 10;
    int reconWait = 100;
    String[] servers = testServers;
    final String hostOs = System.getProperty("os.name").toLowerCase();
    boolean windows = (hostOs.contains("win"));
    windows = true;//from   w  w w .j  av  a 2s. co m
    if (windows) {
        servers = Arrays.copyOf(testServers, 2);
        maxRecon = 2;
    }

    Options opts = new Options.Builder(defaultOptions()).dontRandomize()
            // 1 second total time wait
            .maxReconnect(maxRecon).reconnectWait(reconWait).build();
    opts.servers = Nats.processUrlArray(servers);

    final CountDownLatch dcLatch = new CountDownLatch(1);
    opts.disconnectedCb = new DisconnectedCallback() {
        public void onDisconnect(ConnectionEvent ev) {
            ev.getConnection().setDisconnectedCallback(null);
            dcLatch.countDown();
        }
    };

    final CountDownLatch ccLatch = new CountDownLatch(1);
    opts.closedCb = new ClosedCallback() {
        public void onClose(ConnectionEvent ev) {
            ccLatch.countDown();
        }
    };

    try (NatsServer s1 = runServerOnPort(1222)) {
        try (Connection c = opts.connect()) {
            s1.shutdown();

            // wait for disconnect
            assertTrue("Did not receive a disconnect callback message", dcLatch.await(5, TimeUnit.SECONDS));

            long t0 = System.nanoTime();

            // Wait for ClosedCB
            assertTrue("Did not receive a closed callback signal", ccLatch.await(5, TimeUnit.SECONDS));

            if (windows) {
                long elapsedMsec = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - t0);

                // Use 500ms as variable time delta
                long variable = 500;
                long expected = opts.getMaxReconnect() * opts.getReconnectWait();

                assertFalse("Waited too long for Closed state: " + elapsedMsec,
                        elapsedMsec > (expected + variable));
            }
        }
    }
}