Search in sources :

Example 26 with Stopwatch

use of com.google.common.base.Stopwatch in project distributedlog by twitter.

the class ProxyClientManager method run.

@Override
public void run(Timeout timeout) throws Exception {
    if (timeout.isCancelled() || closed) {
        return;
    }
    if (periodicHandshakeEnabled) {
        final boolean syncOwnerships;
        syncOwnerships = lastOwnershipSyncStopwatch.elapsed(TimeUnit.MILLISECONDS) >= clientConfig.getPeriodicOwnershipSyncIntervalMs();
        final Set<SocketAddress> hostsSnapshot = hostProvider.getHosts();
        final AtomicInteger numHosts = new AtomicInteger(hostsSnapshot.size());
        final AtomicInteger numStreams = new AtomicInteger(0);
        final AtomicInteger numSuccesses = new AtomicInteger(0);
        final AtomicInteger numFailures = new AtomicInteger(0);
        final ConcurrentMap<SocketAddress, Integer> streamDistributions = new ConcurrentHashMap<SocketAddress, Integer>();
        final Stopwatch stopwatch = Stopwatch.createStarted();
        for (SocketAddress host : hostsSnapshot) {
            final SocketAddress address = host;
            final ProxyClient client = getClient(address);
            handshake(address, client, new FutureEventListener<ServerInfo>() {

                @Override
                public void onSuccess(ServerInfo serverInfo) {
                    numStreams.addAndGet(serverInfo.getOwnershipsSize());
                    numSuccesses.incrementAndGet();
                    notifyHandshakeSuccess(address, client, serverInfo, false, stopwatch);
                    if (clientConfig.isHandshakeTracingEnabled()) {
                        streamDistributions.putIfAbsent(address, serverInfo.getOwnershipsSize());
                    }
                    complete();
                }

                @Override
                public void onFailure(Throwable cause) {
                    numFailures.incrementAndGet();
                    notifyHandshakeFailure(address, client, cause, stopwatch);
                    complete();
                }

                private void complete() {
                    if (0 == numHosts.decrementAndGet()) {
                        if (syncOwnerships) {
                            logger.info("Periodic handshaked with {} hosts : {} streams returned," + " {} hosts succeeded, {} hosts failed", new Object[] { hostsSnapshot.size(), numStreams.get(), numSuccesses.get(), numFailures.get() });
                            if (clientConfig.isHandshakeTracingEnabled()) {
                                logger.info("Periodic handshaked stream distribution : {}", streamDistributions);
                            }
                        }
                    }
                }
            }, false, syncOwnerships);
        }
        if (syncOwnerships) {
            lastOwnershipSyncStopwatch.reset().start();
        }
    }
    scheduleHandshake();
}
Also used : ServerInfo(com.twitter.distributedlog.thrift.service.ServerInfo) Stopwatch(com.google.common.base.Stopwatch) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SocketAddress(java.net.SocketAddress) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 27 with Stopwatch

use of com.google.common.base.Stopwatch in project distributedlog by twitter.

the class ProxyClientManager method handshake.

/**
     * Handshake with all proxies.
     *
     * NOTE: this is a synchronous call.
     */
public void handshake() {
    Set<SocketAddress> hostsSnapshot = hostProvider.getHosts();
    logger.info("Handshaking with {} hosts.", hostsSnapshot.size());
    final CountDownLatch latch = new CountDownLatch(hostsSnapshot.size());
    final Stopwatch stopwatch = Stopwatch.createStarted();
    for (SocketAddress host : hostsSnapshot) {
        final SocketAddress address = host;
        final ProxyClient client = getClient(address);
        handshake(address, client, new FutureEventListener<ServerInfo>() {

            @Override
            public void onSuccess(ServerInfo serverInfo) {
                notifyHandshakeSuccess(address, client, serverInfo, true, stopwatch);
                latch.countDown();
            }

            @Override
            public void onFailure(Throwable cause) {
                notifyHandshakeFailure(address, client, cause, stopwatch);
                latch.countDown();
            }
        }, true, true);
    }
    try {
        latch.await(1, TimeUnit.MINUTES);
    } catch (InterruptedException e) {
        logger.warn("Interrupted on handshaking with servers : ", e);
    }
}
Also used : ServerInfo(com.twitter.distributedlog.thrift.service.ServerInfo) Stopwatch(com.google.common.base.Stopwatch) SocketAddress(java.net.SocketAddress) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 28 with Stopwatch

use of com.google.common.base.Stopwatch in project distributedlog by twitter.

the class ProxyClientManager method createClient.

/**
     * Create a client to proxy <code>address</code>.
     *
     * @param address
     *          proxy address
     * @return proxy client
     */
public ProxyClient createClient(final SocketAddress address) {
    final ProxyClient sc = clientBuilder.build(address);
    ProxyClient oldSC = address2Services.putIfAbsent(address, sc);
    if (null != oldSC) {
        sc.close();
        return oldSC;
    } else {
        final Stopwatch stopwatch = Stopwatch.createStarted();
        FutureEventListener<ServerInfo> listener = new FutureEventListener<ServerInfo>() {

            @Override
            public void onSuccess(ServerInfo serverInfo) {
                notifyHandshakeSuccess(address, sc, serverInfo, true, stopwatch);
            }

            @Override
            public void onFailure(Throwable cause) {
                notifyHandshakeFailure(address, sc, cause, stopwatch);
            }
        };
        // send a ping messaging after creating connections.
        handshake(address, sc, listener, true, true);
        return sc;
    }
}
Also used : ServerInfo(com.twitter.distributedlog.thrift.service.ServerInfo) Stopwatch(com.google.common.base.Stopwatch) FutureEventListener(com.twitter.util.FutureEventListener)

Example 29 with Stopwatch

use of com.google.common.base.Stopwatch in project distributedlog by twitter.

the class AsyncReaderBenchmark method benchmark.

@Override
protected void benchmark(DistributedLogNamespace namespace, String logName, StatsLogger statsLogger) {
    DistributedLogManager dlm = null;
    while (null == dlm) {
        try {
            dlm = namespace.openLog(streamName);
        } catch (IOException ioe) {
            logger.warn("Failed to create dlm for stream {} : ", streamName, ioe);
        }
        if (null == dlm) {
            try {
                TimeUnit.MILLISECONDS.sleep(conf.getZKSessionTimeoutMilliseconds());
            } catch (InterruptedException e) {
            }
        }
    }
    logger.info("Created dlm for stream {}.", streamName);
    // Stats
    OpStatsLogger openReaderStats = statsLogger.getOpStatsLogger("open_reader");
    OpStatsLogger blockingReadStats = statsLogger.getOpStatsLogger("blocking_read");
    Counter readCounter = statsLogger.getCounter("reads");
    AsyncLogReader reader = null;
    DLSN lastDLSN = null;
    Long lastTxId = null;
    while (null == reader) {
        // initialize the last txid
        if (null == lastTxId) {
            switch(readMode) {
                case OLDEST:
                    lastTxId = 0L;
                    lastDLSN = DLSN.InitialDLSN;
                    break;
                case LATEST:
                    lastTxId = Long.MAX_VALUE;
                    try {
                        lastDLSN = dlm.getLastDLSN();
                    } catch (IOException ioe) {
                        continue;
                    }
                    break;
                case REWIND:
                    lastTxId = System.currentTimeMillis() - rewindMs;
                    lastDLSN = null;
                    break;
                case POSITION:
                    lastTxId = fromTxId;
                    lastDLSN = null;
                    break;
                default:
                    logger.warn("Unsupported mode {}", readMode);
                    printUsage();
                    System.exit(0);
                    break;
            }
            logger.info("Reading from transaction id = {}, dlsn = {}", lastTxId, lastDLSN);
        }
        // Open the reader
        Stopwatch stopwatch = Stopwatch.createStarted();
        try {
            if (null == lastDLSN) {
                reader = FutureUtils.result(dlm.openAsyncLogReader(lastTxId));
            } else {
                reader = FutureUtils.result(dlm.openAsyncLogReader(lastDLSN));
            }
            long elapsedMs = stopwatch.elapsed(TimeUnit.MICROSECONDS);
            openReaderStats.registerSuccessfulEvent(elapsedMs);
            logger.info("It took {} ms to position the reader to transaction id = {}, dlsn = {}", lastTxId, lastDLSN);
        } catch (IOException ioe) {
            openReaderStats.registerFailedEvent(stopwatch.elapsed(TimeUnit.MICROSECONDS));
            logger.warn("Failed to create reader for stream {} reading from tx id = {}, dlsn = {}.", new Object[] { streamName, lastTxId, lastDLSN });
        }
        if (null == reader) {
            try {
                TimeUnit.MILLISECONDS.sleep(conf.getZKSessionTimeoutMilliseconds());
            } catch (InterruptedException e) {
            }
            continue;
        }
        List<LogRecordWithDLSN> records;
        stopwatch = Stopwatch.createUnstarted();
        while (true) {
            try {
                stopwatch.start();
                records = FutureUtils.result(reader.readBulk(batchSize));
                long elapsedMicros = stopwatch.stop().elapsed(TimeUnit.MICROSECONDS);
                blockingReadStats.registerSuccessfulEvent(elapsedMicros);
                if (!records.isEmpty()) {
                    readCounter.add(records.size());
                    LogRecordWithDLSN lastRecord = records.get(records.size() - 1);
                    lastTxId = lastRecord.getTransactionId();
                    lastDLSN = lastRecord.getDlsn();
                }
                stopwatch.reset();
            } catch (IOException e) {
                logger.warn("Encountered reading record from stream {} : ", streamName, e);
                reader = null;
                break;
            }
        }
        try {
            TimeUnit.MILLISECONDS.sleep(conf.getZKSessionTimeoutMilliseconds());
        } catch (InterruptedException e) {
        }
    }
}
Also used : LogRecordWithDLSN(com.twitter.distributedlog.LogRecordWithDLSN) DLSN(com.twitter.distributedlog.DLSN) LogRecordWithDLSN(com.twitter.distributedlog.LogRecordWithDLSN) AsyncLogReader(com.twitter.distributedlog.AsyncLogReader) Stopwatch(com.google.common.base.Stopwatch) IOException(java.io.IOException) Counter(org.apache.bookkeeper.stats.Counter) DistributedLogManager(com.twitter.distributedlog.DistributedLogManager) OpStatsLogger(org.apache.bookkeeper.stats.OpStatsLogger)

Example 30 with Stopwatch

use of com.google.common.base.Stopwatch in project caffeine by ben-manes.

the class ProfilerHook method scheduleStatusTask.

@SuppressWarnings("FutureReturnValueIgnored")
private void scheduleStatusTask() {
    Stopwatch stopwatch = Stopwatch.createStarted();
    Executors.newSingleThreadScheduledExecutor().scheduleWithFixedDelay(() -> {
        long count = calls.longValue();
        long rate = count / stopwatch.elapsed(TimeUnit.SECONDS);
        System.out.printf("%s - %,d [%,d / sec]%n", stopwatch, count, rate);
    }, DISPLAY_DELAY_SEC, DISPLAY_DELAY_SEC, TimeUnit.SECONDS);
}
Also used : Stopwatch(com.google.common.base.Stopwatch)

Aggregations

Stopwatch (com.google.common.base.Stopwatch)296 IOException (java.io.IOException)75 ArrayList (java.util.ArrayList)29 ExecutionException (java.util.concurrent.ExecutionException)27 Test (org.junit.Test)17 Map (java.util.Map)16 File (java.io.File)15 DocumentStoreException (org.apache.jackrabbit.oak.plugins.document.DocumentStoreException)15 Path (org.apache.hadoop.fs.Path)14 HashMap (java.util.HashMap)13 List (java.util.List)12 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)11 DrillRuntimeException (org.apache.drill.common.exceptions.DrillRuntimeException)11 DBCollection (com.mongodb.DBCollection)9 ISE (io.druid.java.util.common.ISE)9 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)8 OptionsParser (com.google.devtools.common.options.OptionsParser)8 MongoException (com.mongodb.MongoException)8 Connection (java.sql.Connection)8 CountDownLatch (java.util.concurrent.CountDownLatch)8