Search in sources :

Example 21 with Context

use of com.codahale.metrics.Timer.Context in project sharding-jdbc by dangdangdotcom.

the class StatementExecutor method execute.

private boolean execute(final Executor executor) {
    Context context = MetricsContext.start("ShardingStatement-execute");
    eventPostman.postExecutionEvents();
    final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
    final Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
    try {
        if (1 == statementExecutorWrappers.size()) {
            return executeInternal(executor, statementExecutorWrappers.iterator().next(), isExceptionThrown, dataMap);
        }
        List<Boolean> result = executorEngine.execute(statementExecutorWrappers, new ExecuteUnit<StatementExecutorWrapper, Boolean>() {

            @Override
            public Boolean execute(final StatementExecutorWrapper input) throws Exception {
                synchronized (input.getStatement().getConnection()) {
                    return executeInternal(executor, input, isExceptionThrown, dataMap);
                }
            }
        });
        return (null == result || result.isEmpty()) ? false : result.get(0);
    } finally {
        MetricsContext.stop(context);
    }
}
Also used : MetricsContext(com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext) Context(com.codahale.metrics.Timer.Context) SQLException(java.sql.SQLException) StatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper)

Example 22 with Context

use of com.codahale.metrics.Timer.Context in project sharding-jdbc by dangdangdotcom.

the class StatementExecutor method executeQuery.

/**
     * 执行SQL查询.
     * 
     * @return 结果集列表
     */
public List<ResultSet> executeQuery() {
    Context context = MetricsContext.start("ShardingStatement-executeQuery");
    eventPostman.postExecutionEvents();
    final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
    final Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
    List<ResultSet> result;
    try {
        if (1 == statementExecutorWrappers.size()) {
            return Collections.singletonList(executeQueryInternal(statementExecutorWrappers.iterator().next(), isExceptionThrown, dataMap));
        }
        result = executorEngine.execute(statementExecutorWrappers, new ExecuteUnit<StatementExecutorWrapper, ResultSet>() {

            @Override
            public ResultSet execute(final StatementExecutorWrapper input) throws Exception {
                synchronized (input.getStatement().getConnection()) {
                    return executeQueryInternal(input, isExceptionThrown, dataMap);
                }
            }
        });
    } finally {
        MetricsContext.stop(context);
    }
    return result;
}
Also used : MetricsContext(com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext) Context(com.codahale.metrics.Timer.Context) ResultSet(java.sql.ResultSet) StatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper)

Example 23 with Context

use of com.codahale.metrics.Timer.Context in project sharding-jdbc by dangdangdotcom.

the class ShardingConnection method getConnectionInternal.

private Connection getConnectionInternal(final String dataSourceName, final SQLStatementType sqlStatementType) throws SQLException {
    Optional<Connection> connectionOptional = fetchCachedConnectionBySqlStatementType(dataSourceName, sqlStatementType);
    if (connectionOptional.isPresent()) {
        return connectionOptional.get();
    }
    Context metricsContext = MetricsContext.start(Joiner.on("-").join("ShardingConnection-getConnection", dataSourceName));
    DataSource dataSource = shardingContext.getShardingRule().getDataSourceRule().getDataSource(dataSourceName);
    Preconditions.checkState(null != dataSource, "Missing the rule of %s in DataSourceRule", dataSourceName);
    String realDataSourceName = dataSourceName;
    if (dataSource instanceof MasterSlaveDataSource) {
        dataSource = ((MasterSlaveDataSource) dataSource).getDataSource(sqlStatementType);
        realDataSourceName = getRealDataSourceName(dataSourceName, sqlStatementType);
    }
    Connection result = dataSource.getConnection();
    MetricsContext.stop(metricsContext);
    connectionMap.put(realDataSourceName, result);
    return result;
}
Also used : MetricsContext(com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext) Context(com.codahale.metrics.Timer.Context) Connection(java.sql.Connection) DataSource(javax.sql.DataSource)

Example 24 with Context

use of com.codahale.metrics.Timer.Context in project sharding-jdbc by dangdangdotcom.

the class SQLRouteEngine method routeSQL.

SQLRouteResult routeSQL(final SQLParsedResult parsedResult, final List<Object> parameters) {
    Context context = MetricsContext.start("Route SQL");
    SQLRouteResult result = new SQLRouteResult(parsedResult.getRouteContext().getSqlStatementType(), parsedResult.getMergeContext(), parsedResult.getGeneratedKeyContext());
    for (ConditionContext each : parsedResult.getConditionContexts()) {
        RoutingResult routingResult = routeSQL(each, parsedResult);
        result.getExecutionUnits().addAll(routingResult.getSQLExecutionUnits(parsedResult.getRouteContext().getSqlBuilder()));
    }
    amendSQLAccordingToRouteResult(parsedResult, parameters, result);
    MetricsContext.stop(context);
    log.debug("final route result is {} target", result.getExecutionUnits().size());
    for (SQLExecutionUnit each : result.getExecutionUnits()) {
        log.debug("{}:{} {}", each.getDataSource(), each.getSql(), parameters);
    }
    log.debug("merge context:{}", result.getMergeContext());
    return result;
}
Also used : Context(com.codahale.metrics.Timer.Context) ConditionContext(com.dangdang.ddframe.rdb.sharding.parser.result.router.ConditionContext) RouteContext(com.dangdang.ddframe.rdb.sharding.parser.result.router.RouteContext) MetricsContext(com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext) ConditionContext(com.dangdang.ddframe.rdb.sharding.parser.result.router.ConditionContext)

Example 25 with Context

use of com.codahale.metrics.Timer.Context in project torodb by torodb.

the class SimpleAnalyzedOplogBatchExecutorTest method testExecute_NamespaceJob.

@Test
public void testExecute_NamespaceJob() throws Exception {
    //GIVEN
    ApplierContext applierContext = new ApplierContext.Builder().setReapplying(true).setUpdatesAsUpserts(true).build();
    Context context = mock(Context.class);
    NamespaceJob job = mock(NamespaceJob.class);
    given(metrics.getNamespaceBatchTimer().time()).willReturn(context);
    //WHEN
    executor.execute(job, applierContext, conn);
    //THEN
    then(metrics).should(atLeastOnce()).getNamespaceBatchTimer();
    then(metrics.getNamespaceBatchTimer()).should().time();
    then(context).should().close();
    //TODO: This might be changed once the backend throws UniqueIndexViolation
    then(namespaceJobExecutor).should().apply(eq(job), eq(writeTrans), eq(applierContext), any(Boolean.class));
}
Also used : ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Context(com.codahale.metrics.Timer.Context) ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Test(org.junit.Test)

Aggregations

Context (com.codahale.metrics.Timer.Context)37 Timer (com.codahale.metrics.Timer)11 MetricsContext (com.dangdang.ddframe.rdb.sharding.metrics.MetricsContext)10 ApplierContext (com.torodb.mongodb.repl.oplogreplier.ApplierContext)9 OplogOperation (com.eightkdata.mongowp.server.api.oplog.OplogOperation)8 Test (org.junit.Test)7 RetrierGiveUpException (com.torodb.core.retrier.RetrierGiveUpException)6 SQLException (java.sql.SQLException)5 PreparedStatementExecutorWrapper (com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper)4 RetrierAbortException (com.torodb.core.retrier.RetrierAbortException)4 MetricRegistry (com.codahale.metrics.MetricRegistry)3 StatementExecutorWrapper (com.dangdang.ddframe.rdb.sharding.executor.wrapper.StatementExecutorWrapper)3 RollbackException (com.torodb.core.transaction.RollbackException)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 List (java.util.List)3 ExecutionException (java.util.concurrent.ExecutionException)3 ExecutorService (java.util.concurrent.ExecutorService)3 ConsoleReporter (com.codahale.metrics.ConsoleReporter)2 Counter (com.codahale.metrics.Counter)2