Search in sources :

Example 61 with StopWatch

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);
    }
}
Also used : UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) StopWatch(core.framework.util.StopWatch)

Example 62 with StopWatch

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);
    }
}
Also used : UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) StopWatch(core.framework.util.StopWatch)

Example 63 with StopWatch

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);
    }
}
Also used : UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) StopWatch(core.framework.util.StopWatch)

Example 64 with StopWatch

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);
    }
}
Also used : UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) StopWatch(core.framework.util.StopWatch)

Example 65 with StopWatch

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());
    }
}
Also used : Markers(core.framework.log.Markers) Consumer(org.apache.kafka.clients.consumer.Consumer) Logger(org.slf4j.Logger) Message(core.framework.kafka.Message) Headers(org.apache.kafka.common.header.Headers) LoggerFactory(org.slf4j.LoggerFactory) ActionLog(core.framework.impl.log.ActionLog) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Threads(core.framework.util.Threads) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) BytesParam(core.framework.impl.log.filter.BytesParam) StopWatch(core.framework.util.StopWatch) ArrayList(java.util.ArrayList) Lists(core.framework.util.Lists) List(java.util.List) Header(org.apache.kafka.common.header.Header) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) Duration(java.time.Duration) Map(java.util.Map) Maps(core.framework.util.Maps) LogManager(core.framework.impl.log.LogManager) Charsets(core.framework.util.Charsets) ConsumerRecord(org.apache.kafka.clients.consumer.ConsumerRecord) StopWatch(core.framework.util.StopWatch) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Aggregations

StopWatch (core.framework.util.StopWatch)79 IOException (java.io.IOException)21 UncheckedIOException (java.io.UncheckedIOException)21 SearchException (core.framework.search.SearchException)10 ElasticsearchException (org.elasticsearch.ElasticsearchException)10 ArrayList (java.util.ArrayList)6 HTMLTemplate (core.framework.impl.template.HTMLTemplate)4 BytesParam (core.framework.impl.log.filter.BytesParam)3 Map (java.util.Map)3 BsonDocument (org.bson.BsonDocument)3 BulkWriteOptions (com.mongodb.client.model.BulkWriteOptions)2 UpdateOptions (com.mongodb.client.model.UpdateOptions)2 DeleteResult (com.mongodb.client.result.DeleteResult)2 TemplateContext (core.framework.impl.template.TemplateContext)2 Headers (org.apache.kafka.common.header.Headers)2 Bson (org.bson.conversions.Bson)2 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)2 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)2 Settings (org.elasticsearch.common.settings.Settings)2 MongoClient (com.mongodb.MongoClient)1