Search in sources :

Example 66 with StopWatch

use of core.framework.util.StopWatch in project core-ng-project by neowu.

the class KafkaMessagePublisher method publish.

@Override
public void publish(String topic, String key, T value) {
    if (topic == null)
        throw new Error("topic must not be null");
    // if key is null, kafka will pick random partition which breaks determinacy
    if (key == null)
        throw new Error("key must not be null");
    validator.validate(value);
    StopWatch watch = new StopWatch();
    byte[] message = writer.toJSON(value);
    try {
        ProducerRecord<String, byte[]> record = new ProducerRecord<>(topic, key, message);
        Headers headers = record.headers();
        headers.add(KafkaHeaders.HEADER_CLIENT_IP, Strings.bytes(Network.localHostAddress()));
        if (logManager.appName != null)
            headers.add(KafkaHeaders.HEADER_CLIENT, Strings.bytes(logManager.appName));
        linkContext(headers);
        producer.send(record);
    } finally {
        long elapsedTime = watch.elapsedTime();
        // kafka producer send message in background, the main purpose of track is to count how many message sent in action
        ActionLogContext.track("kafka", elapsedTime);
        logger.debug("publish, topic={}, key={}, message={}, elapsedTime={}", topic, key, new BytesParam(message), elapsedTime);
    }
}
Also used : BytesParam(core.framework.impl.log.filter.BytesParam) Headers(org.apache.kafka.common.header.Headers) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) StopWatch(core.framework.util.StopWatch)

Example 67 with StopWatch

use of core.framework.util.StopWatch in project core-ng-project by neowu.

the class HTMLTemplateEngine method add.

private void add(TemplateSource source, Class<?> modelClass) {
    StopWatch watch = new StopWatch();
    String name = source.name();
    try {
        HTMLTemplate previous = templates.putIfAbsent(name, new HTMLTemplateBuilder(source, modelClass).build());
        if (previous != null)
            throw Exceptions.error("template is already added, name={}", name);
    } finally {
        logger.info("add, name={}, modelClass={}, elapsedTime={}", name, modelClass.getCanonicalName(), watch.elapsedTime());
    }
}
Also used : HTMLTemplateBuilder(core.framework.impl.template.HTMLTemplateBuilder) HTMLTemplate(core.framework.impl.template.HTMLTemplate) StopWatch(core.framework.util.StopWatch)

Example 68 with StopWatch

use of core.framework.util.StopWatch in project core-ng-project by neowu.

the class MessageProcessor method consume.

private <T> void consume(String topic, ConsumerRecords<String, byte[]> records, JSONReader<T> reader, java.util.function.Consumer<List<T>> consumer) {
    int messageSize = 0;
    List<T> messages = new ArrayList<>();
    for (ConsumerRecord<String, byte[]> record : records.records(topic)) {
        byte[] body = record.value();
        messages.add(reader.fromJSON(body));
        messageSize += body.length;
    }
    if (messages.isEmpty())
        return;
    StopWatch watch = new StopWatch();
    try {
        consumer.accept(messages);
    } finally {
        long elapsedTime = watch.elapsedTime();
        logger.info("consume messages, topic={}, count={}, size={}, elapsedTime={}", topic, messages.size(), messageSize, elapsedTime);
    }
}
Also used : TOPIC_STAT(core.framework.impl.log.message.LogTopics.TOPIC_STAT) ArrayList(java.util.ArrayList) StopWatch(core.framework.util.StopWatch)

Example 69 with StopWatch

use of core.framework.util.StopWatch in project core-ng-project by neowu.

the class TemplateManager method add.

public void add(String templatePath, Class<?> modelClass) {
    StopWatch watch = new StopWatch();
    try {
        Map<String, HTMLTemplate> previous = templates.putIfAbsent(templatePath, load(templatePath, modelClass));
        if (previous != null)
            throw Exceptions.error("template was registered, templatePath={}", templatePath);
        if (webDirectory.localEnv) {
            Path path = webDirectory.path(templatePath);
            templateLastModifiedTimes.put(templatePath, Files.lastModified(path));
        }
    } finally {
        logger.info("add, templatePath={}, modelClass={}, elapsedTime={}", templatePath, modelClass.getCanonicalName(), watch.elapsedTime());
    }
}
Also used : Path(java.nio.file.Path) HTMLTemplate(core.framework.impl.template.HTMLTemplate) StopWatch(core.framework.util.StopWatch)

Example 70 with StopWatch

use of core.framework.util.StopWatch in project core-ng-project by neowu.

the class ElasticSearchTypeImpl method get.

@Override
public Optional<T> get(GetRequest request) {
    StopWatch watch = new StopWatch();
    String index = request.index == null ? this.index : request.index;
    int hits = 0;
    try {
        GetResponse response = client().prepareGet(index, type, request.id).get();
        if (!response.isExists())
            return Optional.empty();
        hits = 1;
        return Optional.of(reader.fromJSON(response.getSourceAsBytes()));
    } catch (ElasticsearchException e) {
        // due to elastic search uses async executor to run, we have to wrap the exception to retain the original place caused the exception
        throw new SearchException(e);
    } finally {
        long elapsedTime = watch.elapsedTime();
        ActionLogContext.track("elasticsearch", elapsedTime, hits, 0);
        logger.debug("get, index={}, type={}, id={}, elapsedTime={}", index, type, request.id, elapsedTime);
        checkSlowOperation(elapsedTime);
    }
}
Also used : SearchException(core.framework.search.SearchException) ElasticsearchException(org.elasticsearch.ElasticsearchException) GetResponse(org.elasticsearch.action.get.GetResponse) StopWatch(core.framework.util.StopWatch)

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