use of core.framework.util.StopWatch in project core-ng-project by neowu.
the class RedisImpl method expire.
@Override
public void expire(String key, Duration expiration) {
StopWatch watch = new StopWatch();
PoolItem<RedisConnection> item = pool.borrowItem();
try {
RedisConnection connection = item.resource;
connection.write(Protocol.Command.EXPIRE, encode(key), encode(expiration.getSeconds()));
connection.readLong();
} catch (IOException e) {
item.broken = true;
throw new UncheckedIOException(e);
} finally {
pool.returnItem(item);
long elapsedTime = watch.elapsedTime();
ActionLogContext.track("redis", elapsedTime, 0, 1);
logger.debug("expire, key={}, expiration={}, elapsedTime={}", key, expiration, elapsedTime);
checkSlowOperation(elapsedTime);
}
}
use of core.framework.util.StopWatch in project core-ng-project by neowu.
the class RedisImpl method forEach.
@Override
public void forEach(String pattern, Consumer<String> consumer) {
StopWatch watch = new StopWatch();
PoolItem<RedisConnection> item = pool.borrowItem();
int count = 0;
try {
RedisConnection connection = item.resource;
// use 500 as batch
byte[] batchSize = encode("500");
String cursor = "0";
do {
connection.write(Protocol.Command.SCAN, encode(cursor), MATCH, encode(pattern), COUNT, batchSize);
Object[] response = connection.readArray();
cursor = decode((byte[]) response[0]);
Object[] keys = (Object[]) response[1];
for (Object key : keys) {
consumer.accept(decode((byte[]) key));
}
count += keys.length;
} while (!"0".equals(cursor));
} catch (IOException e) {
item.broken = true;
throw new UncheckedIOException(e);
} finally {
pool.returnItem(item);
long elapsedTime = watch.elapsedTime();
ActionLogContext.track("redis", elapsedTime, count, 0);
logger.debug("forEach, pattern={}, count={}, elapsedTime={}", pattern, count, elapsedTime);
}
}
use of core.framework.util.StopWatch in project core-ng-project by neowu.
the class RedisImpl method set.
@Override
public void set(String key, String value) {
StopWatch watch = new StopWatch();
PoolItem<RedisConnection> item = pool.borrowItem();
try {
RedisConnection connection = item.resource;
connection.write(Protocol.Command.SET, encode(key), encode(value));
connection.readSimpleString();
} catch (IOException e) {
item.broken = true;
throw new UncheckedIOException(e);
} finally {
pool.returnItem(item);
long elapsedTime = watch.elapsedTime();
ActionLogContext.track("redis", elapsedTime, 0, 1);
logger.debug("set, key={}, value={}, elapsedTime={}", key, value, elapsedTime);
checkSlowOperation(elapsedTime);
}
}
use of core.framework.util.StopWatch in project core-ng-project by neowu.
the class RedisImpl method del.
@Override
public boolean del(String key) {
StopWatch watch = new StopWatch();
PoolItem<RedisConnection> item = pool.borrowItem();
try {
RedisConnection connection = item.resource;
connection.write(Protocol.Command.DEL, encode(key));
return connection.readLong() == 1;
} catch (IOException e) {
item.broken = true;
throw new UncheckedIOException(e);
} finally {
pool.returnItem(item);
long elapsedTime = watch.elapsedTime();
ActionLogContext.track("redis", elapsedTime, 0, 1);
logger.debug("del, key={}, elapsedTime={}", key, elapsedTime);
checkSlowOperation(elapsedTime);
}
}
use of core.framework.util.StopWatch in project core-ng-project by neowu.
the class KafkaMessageListenerThread method process.
private void process(Consumer<String, byte[]> consumer, ConsumerRecords<String, byte[]> kafkaRecords) {
StopWatch watch = new StopWatch();
int count = 0;
int size = 0;
try {
Map<String, List<ConsumerRecord<String, byte[]>>> messages = Maps.newLinkedHashMap();
for (ConsumerRecord<String, byte[]> record : kafkaRecords) {
messages.computeIfAbsent(record.topic(), key -> Lists.newArrayList()).add(record);
count++;
size += record.value().length;
}
for (Map.Entry<String, List<ConsumerRecord<String, byte[]>>> entry : messages.entrySet()) {
String topic = entry.getKey();
List<ConsumerRecord<String, byte[]>> records = entry.getValue();
KafkaMessageListener.BulkMessageHandlerHolder<?> bulkHandlerHolder = bulkHandlerHolders.get(topic);
if (bulkHandlerHolder != null) {
handle(topic, bulkHandlerHolder, records, longProcessThreshold(batchLongProcessThresholdInNano, records.size(), count));
} else {
KafkaMessageListener.MessageHandlerHolder<?> handlerHolder = handlerHolders.get(topic);
if (handlerHolder != null) {
handle(topic, handlerHolder, records, longProcessThreshold(batchLongProcessThresholdInNano, 1, count));
}
}
}
} finally {
consumer.commitAsync();
logger.info("process kafka records, count={}, size={}, elapsedTime={}", count, size, watch.elapsedTime());
}
}
Aggregations