@SuppressWarnings("unchecked") @Override public <P> P getPart(Class<P> clazz) { Collection<?> items = parts.get(clazz); if (items.isEmpty()) { return null; } if (items.size() > 1) { Injector.LOG.WARN("Retrieving a Part for %s from multiple implementations (%s) - picking a random one!" + " Use @Replace to clarify! Context: %s", clazz.getName(), items, ExecutionPoint.snapshot().toString()); } return (P) items.iterator().next(); }
private static ExecutionPoint snapshot(boolean fast) { ExecutionPoint result = new ExecutionPoint(); result.threadName = Thread.currentThread().getName(); result.stacktraceHolder = new Exception(); if (!fast) { result.mdc = CallContext.getCurrent().getMDC(); } return result; }
/** * Generates a new instance of the current thread. * <p> * This does skip saving the MDC and only records the current stack trace, as this is blazing fast. * * @return a new instance representing the current state of the current thread */ public static ExecutionPoint fastSnapshot() { return snapshot(true); }
/** * Generates a new instance of the current thread. * * @return a new instance representing the current state of the current thread */ public static ExecutionPoint snapshot() { return snapshot(false); }
@Override public void close() throws SQLException { try (Operation op = new Operation(() -> database.name + ".close()", Duration.ofSeconds(5))) { delegate.close(); } catch (SQLException e) { // Most likely this exception will be a false alert because DBCP 1.2.2 cannot deal with connections which // are closed by their driver (due to network issues). // The next release of DBCP will fix this problem. The exception is logged at INFO level in case a "real" // problem occurred. If we wouldn't call delegate.close, the connection would remain active and might block // the pool. Databases.LOG.INFO("Error closing connection"); Databases.LOG.INFO(e); } finally { watch.submitMicroTiming("SQL", "Connection Duration: " + database.name); if (watch.elapsedMillis() > Databases.getLogConnectionThresholdMillis()) { DB.SLOW_DB_LOG.INFO("A long running connection was detected (%s): Opened:\n%s\n\nClosed:\n%s", watch.duration(), connected.toString(), ExecutionPoint.snapshot().toString()); } } }
protected void updateStatistics(String sql, Watch w) { w.submitMicroTiming("SQL", sql); Databases.numQueries.inc(); Databases.queryDuration.addValue(w.elapsedMillis()); if (w.elapsedMillis() > Databases.getLogQueryThresholdMillis()) { Databases.numSlowQueries.inc(); DB.SLOW_DB_LOG.INFO("A slow JDBC query was executed (%s): %s\n%s", w.duration(), sql, ExecutionPoint.snapshot().toString()); } }
private void checkRouting() { if (elastic.isRouted(descriptor)) { if (Strings.isEmpty(routing) && !unrouted) { Elastic.LOG.WARN("Trying query an entity of type '%s' without providing a routing!" + " This will most probably return an invalid result!\n%s\n", descriptor.getType().getName(), this, ExecutionPoint.snapshot()); } } else if (Strings.isFilled(routing)) { Elastic.LOG.WARN("Trying query an entity of type '%s' while providing a routing! This entity is unrouted!" + " This will most probably return an invalid result!\n%s\n", descriptor.getType().getName(), this, ExecutionPoint.snapshot()); } }
protected void updateStatistics(String sql, Watch w) { w.submitMicroTiming("SQL", sql); Databases.numQueries.inc(); Databases.queryDuration.addValue(w.elapsedMillis()); if (w.elapsedMillis() > Databases.getLogQueryThresholdMillis()) { Databases.numSlowQueries.inc(); DB.SLOW_DB_LOG.INFO("A slow JDBC query was executed (%s): %s\n%s", w.duration(), sql, ExecutionPoint.snapshot().toString()); } }
/** * As a scroll cursor can timeout, we monitor the call interval and emit a warning if a timeout might have occurred. * * @param lastScroll the timestamp when the last scoll was executed * @return the next timestamp */ private long performScrollMonitoring(long lastScroll) { long now = System.currentTimeMillis(); if (lastScroll > 0) { long deltaInSeconds = TimeUnit.SECONDS.convert(now - lastScroll, TimeUnit.MILLISECONDS); // Warn if processing of one scroll took longer thant our keep alive.... if (deltaInSeconds > SCROLL_TTL_SECONDS) { Exceptions.handle() .withSystemErrorMessage( "A scroll query against elasticserach took too long to process its data! " + "The result is probably inconsistent! Query: %s\n%s", this, ExecutionPoint.snapshot()) .to(Elastic.LOG) .handle(); } } return now; }
@Override public int[] executeBatch() throws SQLException { Watch w = Watch.start(); try (Operation op = new Operation(() -> "executeBatch: " + preparedSQL, Duration.ofSeconds(30))) { int[] result = delegate.executeBatch(); w.submitMicroTiming("BATCH-SQL", preparedSQL); Databases.numQueries.inc(); Databases.queryDuration.addValue(w.elapsedMillis()); if (w.elapsedMillis() > Databases.getLogQueryThresholdMillis()) { Databases.numSlowQueries.inc(); DB.SLOW_DB_LOG.INFO("A slow JDBC batch query was executed (%s): %s (%s rows)\n%s", w.duration(), preparedSQL, result.length, ExecutionPoint.snapshot().toString()); } return result; } }
private <E extends Entity> void applyRouting(String action, E entity, EntityDescriptor descriptor, Consumer<String> routingTarget) { if (descriptor.hasRouting()) { Object routingKey = descriptor.getProperty(descriptor.getRouting()).writeToSource(entity); if (Strings.isEmpty(routingKey)) { LOG.WARN("%s an entity of type %s (%s) without routing information! Location: %s", action, entity.getClass().getName(), entity.getId(), ExecutionPoint.snapshot()); } else { routingTarget.accept(String.valueOf(routingKey)); } } }
protected void traceIfRequired(String collection, Watch w) { if (mongo.tracing && w.elapsedMillis() >= mongo.traceLimit) { String location = determineLocation(); Doc explanation = explain(collection); mongo.traceData.put(location, Tuple.create(collection + ": " + filterObject.toString() + " [" + w.duration() + "]", explanation.toString())); } if (w.elapsedMillis() > mongo.getLogQueryThresholdMillis()) { mongo.numSlowQueries.inc(); DB.SLOW_DB_LOG.INFO("A slow MongoDB query was executed (%s): %s\n%s\n%s", w.duration(), collection, filterObject, ExecutionPoint.snapshot().toString()); } }
@SuppressWarnings("unchecked") @Override protected <E extends ElasticEntity> Optional<E> findEntity(Object id, EntityDescriptor ed, Function<String, Value> context) throws Exception { String routing = context.apply(CONTEXT_ROUTING).getString(); if (routing == null && isRouted(ed)) { LOG.WARN("Trying to FIND an entity of type '%s' with id '%s' without providing a routing! " + "This will most probably return an invalid result!\n%s", ed.getType().getName(), id, ExecutionPoint.snapshot()); } String index = isStoredPerYear(ed) ? determineYearIndex(ed, id.toString().substring(0, 4)) : determineIndex(ed, null); JSONObject obj = getLowLevelClient().get(index, determineTypeName(ed), id.toString(), routing, true); if (obj == null || !Boolean.TRUE.equals(obj.getBoolean(RESPONSE_FOUND))) { return Optional.empty(); } E result = (E) make(ed, obj); return Optional.of(result); }
lang, value, ExecutionPoint.snapshot());
@Override public long count() { if (skip > 0 || limit > 0) { Elastic.LOG.WARN("COUNT queries support neither skip nor limit: %s\n%s", this, ExecutionPoint.snapshot()); } checkRouting(); List<String> indices = determineIndices(); if (indices.isEmpty()) { return 0; } JSONObject countResponse = client.count(indices, elastic.determineTypeName(descriptor), routing, buildSimplePayload()); return countResponse.getLong(KEY_COUNT); }
@Override public boolean exists() { if (skip > 0 || limit > 0) { Elastic.LOG.WARN("EXISTS queries support neither skip nor limit: %s\n%s", this, ExecutionPoint.snapshot()); } checkRouting(); List<String> indices = determineIndices(); if (indices.isEmpty()) { return false; } JSONObject existsResponse = client.exists(indices, elastic.determineTypeName(descriptor), routing, buildSimplePayload()); return existsResponse.getJSONObject(KEY_HITS).getInteger(KEY_TOTAL) >= 1; }
IndexAccess.LOG.WARN("Default limit was hit when using Query.queryList or Query.queryResultList! " + "Please provide an explicit limit or use Query.iterate to remove this warning. " + "Query: %s, Location: %s", this, ExecutionPoint.snapshot());
IndexAccess.LOG.WARN("Default limit was hit when using Query.queryList or Query.queryResultList! " + "Please provide an explicit limit or use Query.iterate to remove this warning. " + "Query: %s, Location: %s", this, ExecutionPoint.snapshot());