Search in sources :

Example 6 with Watch

use of sirius.kernel.commons.Watch in project sirius-db by scireum.

the class Finder method processCursor.

private void processCursor(MongoIterable<Document> cursor, Predicate<Doc> processor, String collection) {
    Watch watch = Watch.start();
    TaskContext taskContext = TaskContext.get();
    Monoflop shouldHandleTracing = Monoflop.create();
    for (Document doc : cursor) {
        if (shouldHandleTracing.firstCall()) {
            handleTracingAndReporting(collection, watch);
        }
        boolean keepGoing = processor.test(new Doc(doc));
        if (!keepGoing || !taskContext.isActive()) {
            return;
        }
    }
    // the Microtiming...
    if (shouldHandleTracing.firstCall()) {
        handleTracingAndReporting(collection, watch);
    }
}
Also used : TaskContext(sirius.kernel.async.TaskContext) Monoflop(sirius.kernel.commons.Monoflop) Watch(sirius.kernel.commons.Watch) Document(org.bson.Document)

Example 7 with Watch

use of sirius.kernel.commons.Watch in project sirius-db by scireum.

the class Updater method execute.

private UpdateResult execute(String collection, boolean forMany) {
    Document updateObject = prepareUpdate(collection);
    Watch w = Watch.start();
    try {
        if (Mongo.LOG.isFINE()) {
            Mongo.LOG.FINE("UPDATE: %s\nFilter: %s\n Update:%s", collection, filterObject, updateObject);
        }
        UpdateOptions updateOptions = new UpdateOptions().upsert(this.upsert);
        if (forMany) {
            return mongo.db(database).getCollection(collection).updateMany(filterObject, updateObject, updateOptions);
        } else {
            return mongo.db(database).getCollection(collection).updateOne(filterObject, updateObject, updateOptions);
        }
    } finally {
        mongo.callDuration.addValue(w.elapsedMillis());
        if (Microtiming.isEnabled()) {
            w.submitMicroTiming("mongo", "UPDATE - " + collection + ": " + filterObject.keySet());
        }
        traceIfRequired(collection, w);
    }
}
Also used : Watch(sirius.kernel.commons.Watch) Document(org.bson.Document) UpdateOptions(com.mongodb.client.model.UpdateOptions)

Example 8 with Watch

use of sirius.kernel.commons.Watch in project sirius-db by scireum.

the class RequestBuilder method tryExecute.

protected RequestBuilder tryExecute(String uri) throws OptimisticLockException {
    Watch w = Watch.start();
    try (Operation op = new Operation(() -> Strings.apply("Elastic: %s %s", method, uri), Duration.ofSeconds(30))) {
        Request request = setupRequest(uri);
        responseEntity = restClient.performRequest(request).getEntity();
        return this;
    } catch (ResponseException e) {
        return handleResponseException(e);
    } catch (IOException e) {
        throw Exceptions.handle().to(Elastic.LOG).error(e).withSystemErrorMessage("An IO exception occurred when performing a request against elasticsearch: %s").handle();
    } finally {
        elastic.callDuration.addValue(w.elapsedMillis());
        if (Microtiming.isEnabled()) {
            w.submitMicroTiming("ELASTIC", method + ": " + uri);
        }
        if (w.elapsedMillis() > Elastic.getLogQueryThresholdMillis()) {
            elastic.numSlowQueries.inc();
            DB.SLOW_DB_LOG.INFO("A slow Elasticsearch query was executed (%s): %s\n%s\n%s", w.duration(), method + ": " + uri, Strings.limit(buildContent().orElse("no content"), MAX_CONTENT_LONG_LENGTH), ExecutionPoint.snapshot().toString());
        }
    }
}
Also used : ResponseException(org.elasticsearch.client.ResponseException) Watch(sirius.kernel.commons.Watch) Request(org.elasticsearch.client.Request) Operation(sirius.kernel.async.Operation) IOException(java.io.IOException)

Example 9 with Watch

use of sirius.kernel.commons.Watch in project sirius-db by scireum.

the class BatchSQLQuery method iterate.

@Override
public void iterate(Predicate<Row> handler, @Nullable Limit limit) throws SQLException {
    Watch w = Watch.start();
    try (ResultSet rs = query.prepareStmt().executeQuery()) {
        query.avarage.addValue(w.elapsedMillis());
        TaskContext tc = TaskContext.get();
        processResultSet(handler, limit, rs, tc);
    }
}
Also used : TaskContext(sirius.kernel.async.TaskContext) Watch(sirius.kernel.commons.Watch) ResultSet(java.sql.ResultSet)

Example 10 with Watch

use of sirius.kernel.commons.Watch in project sirius-db by scireum.

the class DeleteQuery method delete.

/**
 * Deletes all entities where the compared mappings match the ones given in the <tt>example</tt>.
 *
 * @param example      the example entity used to determine which other entities to delete
 * @param invokeChecks <tt>true</tt> to signal that before- and after delete checks should be executed,
 *                     <tt>false</tt> otherwise
 * @param addBatch     determines if the query should be executed instantly (<tt>false</tt>) or added to the
 *                     batch update (<tt>true</tt>).
 */
@SuppressWarnings("unchecked")
public void delete(@Nonnull E example, boolean invokeChecks, boolean addBatch) {
    try {
        if (example.isNew()) {
            return;
        }
        if (this.type == null) {
            this.type = (Class<E>) example.getClass();
        }
        Watch w = Watch.start();
        if (invokeChecks) {
            getDescriptor().beforeDelete(example);
        }
        PreparedStatement stmt = prepareStmt();
        int i = 1;
        for (Tuple<Operator, Property> filter : getPropertyFilters()) {
            stmt.setObject(i++, filter.getSecond().getValueForDatasource(OMA.class, example));
        }
        if (descriptor.isVersioned()) {
            if (example.getVersion() == 0) {
                throw Exceptions.handle().to(OMA.LOG).withSystemErrorMessage("Cannot execute a DeleteQuery for the versioned entity %s without a version!", descriptor.getType()).handle();
            }
            stmt.setObject(i, example.getVersion());
        }
        if (addBatch) {
            addBatch();
        } else {
            stmt.executeUpdate();
            stmt.getConnection().commit();
            avarage.addValue(w.elapsedMillis());
        }
        if (invokeChecks) {
            getDescriptor().afterDelete(example);
        }
    } catch (SQLException e) {
        context.safeClose();
        throw Exceptions.handle().to(OMA.LOG).error(e).withSystemErrorMessage("A database error occurred while executing a DeleteQuery for %s: %s (%s)", type.getName()).handle();
    } catch (Exception e) {
        throw Exceptions.handle().to(OMA.LOG).error(e).withSystemErrorMessage("An error occurred while executing a DeleteQuery for %s: %s (%s)", type.getName()).handle();
    }
}
Also used : Operator(sirius.db.jdbc.Operator) OMA(sirius.db.jdbc.OMA) SQLException(java.sql.SQLException) Watch(sirius.kernel.commons.Watch) PreparedStatement(java.sql.PreparedStatement) Property(sirius.db.mixing.Property) SQLException(java.sql.SQLException)

Aggregations

Watch (sirius.kernel.commons.Watch)53 PreparedStatement (java.sql.PreparedStatement)8 SQLException (java.sql.SQLException)8 Connection (java.sql.Connection)6 HandledException (sirius.kernel.health.HandledException)6 Document (org.bson.Document)5 ResultSet (java.sql.ResultSet)4 TaskContext (sirius.kernel.async.TaskContext)4 ArrayList (java.util.ArrayList)3 OMA (sirius.db.jdbc.OMA)3 Property (sirius.db.mixing.Property)3 BasicDBObject (com.mongodb.BasicDBObject)2 ProcessContext (sirius.biz.process.ProcessContext)2 ObjectStorageSpace (sirius.biz.storage.layer1.ObjectStorageSpace)2 Operator (sirius.db.jdbc.Operator)2 Operation (sirius.kernel.async.Operation)2 Context (sirius.kernel.commons.Context)2 Limit (sirius.kernel.commons.Limit)2 AS400 (com.ibm.as400.access.AS400)1 ProgramCall (com.ibm.as400.access.ProgramCall)1