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();
}
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);
}
}
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;
}
}
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) {
}
}
}
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);
}
Aggregations