Search in sources :

Example 1 with Context

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

the class SQLRouteEngine method parseSQL.

SQLParsedResult parseSQL(final String logicSql, final List<Object> parameters) {
    if (HintManagerHolder.isDatabaseShardingOnly()) {
        return buildHintParsedResult(logicSql);
    }
    Context context = MetricsContext.start("Parse SQL");
    SQLParsedResult result = SQLParserFactory.create(databaseType, logicSql, parameters, shardingRule).parse();
    MetricsContext.stop(context);
    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) SQLParsedResult(com.dangdang.ddframe.rdb.sharding.parser.result.SQLParsedResult)

Example 2 with Context

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

the class PreparedStatementExecutor method execute.

/**
     * 执行SQL请求.
     * 
     * @return true表示执行DQL, false表示执行的DML
     */
public boolean execute() {
    Context context = MetricsContext.start("ShardingPreparedStatement-execute");
    eventPostman.postExecutionEvents();
    final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
    final Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
    try {
        if (1 == preparedStatementExecutorWrappers.size()) {
            PreparedStatementExecutorWrapper preparedStatementExecutorWrapper = preparedStatementExecutorWrappers.iterator().next();
            return executeInternal(preparedStatementExecutorWrapper, isExceptionThrown, dataMap);
        }
        List<Boolean> result = executorEngine.execute(preparedStatementExecutorWrappers, new ExecuteUnit<PreparedStatementExecutorWrapper, Boolean>() {

            @Override
            public Boolean execute(final PreparedStatementExecutorWrapper input) throws Exception {
                synchronized (input.getPreparedStatement().getConnection()) {
                    return executeInternal(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) PreparedStatementExecutorWrapper(com.dangdang.ddframe.rdb.sharding.executor.wrapper.PreparedStatementExecutorWrapper) SQLException(java.sql.SQLException)

Example 3 with Context

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

the class StatementExecutor method executeUpdate.

private int executeUpdate(final Updater updater) {
    Context context = MetricsContext.start("ShardingStatement-executeUpdate");
    eventPostman.postExecutionEvents();
    final boolean isExceptionThrown = ExecutorExceptionHandler.isExceptionThrown();
    final Map<String, Object> dataMap = ExecutorDataMap.getDataMap();
    try {
        if (1 == statementExecutorWrappers.size()) {
            return executeUpdateInternal(updater, statementExecutorWrappers.iterator().next(), isExceptionThrown, dataMap);
        }
        return executorEngine.execute(statementExecutorWrappers, new ExecuteUnit<StatementExecutorWrapper, Integer>() {

            @Override
            public Integer execute(final StatementExecutorWrapper input) throws Exception {
                synchronized (input.getStatement().getConnection()) {
                    return executeUpdateInternal(updater, input, isExceptionThrown, dataMap);
                }
            }
        }, new MergeUnit<Integer, Integer>() {

            @Override
            public Integer merge(final List<Integer> results) {
                if (null == results) {
                    return 0;
                }
                int result = 0;
                for (int each : results) {
                    result += each;
                }
                return result;
            }
        });
    } 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 4 with Context

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

the class SimpleAnalyzedOplogBatchExecutorTest method testVisit_CudAnalyzedOplog_Success.

@Test
public void testVisit_CudAnalyzedOplog_Success() throws Exception {
    //GIVEN
    OplogOperation lastOp = mock(OplogOperation.class);
    CudAnalyzedOplogBatch batch = mock(CudAnalyzedOplogBatch.class);
    ApplierContext applierContext = new ApplierContext.Builder().setReapplying(true).setUpdatesAsUpserts(true).build();
    given(batch.getOriginalBatch()).willReturn(Lists.newArrayList(mock(OplogOperation.class), mock(OplogOperation.class), mock(OplogOperation.class), lastOp));
    Timer timer = mock(Timer.class);
    Context context = mock(Context.class);
    given(metrics.getCudBatchTimer()).willReturn(timer);
    given(timer.time()).willReturn(context);
    doNothing().when(executor).execute(eq(batch), any());
    //WHEN
    OplogOperation result = executor.visit(batch, applierContext);
    //THEN
    then(metrics).should().getCudBatchTimer();
    then(timer).should().time();
    then(context).should().close();
    then(executor).should(times(1)).execute(batch, applierContext);
    assertEquals(lastOp, result);
}
Also used : ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Context(com.codahale.metrics.Timer.Context) Timer(com.codahale.metrics.Timer) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation) ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Test(org.junit.Test)

Example 5 with Context

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

the class SimpleAnalyzedOplogBatchExecutorTest method testVisit_SingleOp_NotRepyingRollback.

/**
   * Test the behaviour of the method
   * {@link SimpleAnalyzedOplogBatchExecutor#visit(com.torodb.mongodb.repl.oplogreplier.batch.SingleOpAnalyzedOplogBatch, com.torodb.mongodb.repl.oplogreplier.ApplierContext) that visits a single op}
   * when
   * {@link SimpleAnalyzedOplogBatchExecutor#execute(com.eightkdata.mongowp.server.api.oplog.OplogOperation, com.torodb.mongodb.repl.oplogreplier.ApplierContext) the execution}
   * fails until the given attempt.
   *
   *
   * @param myRetrier
   * @param atteptsToSucceed
   * @return true if the execution finishes or false if it throw an exception.
   * @throws Exception
   */
private boolean testVisit_SingleOp_NotRepyingRollback(Retrier myRetrier, int atteptsToSucceed) throws Exception {
    //GIVEN
    OplogOperation operation = mock(OplogOperation.class);
    SingleOpAnalyzedOplogBatch batch = new SingleOpAnalyzedOplogBatch(operation);
    ApplierContext applierContext = new ApplierContext.Builder().setReapplying(true).setUpdatesAsUpserts(false).build();
    executor = spy(new SimpleAnalyzedOplogBatchExecutor(metrics, applier, server, myRetrier, namespaceJobExecutor));
    Timer timer = mock(Timer.class);
    Context context = mock(Context.class);
    given(metrics.getSingleOpTimer(operation)).willReturn(timer);
    given(timer.time()).willReturn(context);
    doAnswer(new Answer() {

        int attempts = 0;

        @Override
        public Object answer(InvocationOnMock invocation) throws Throwable {
            try {
                ApplierContext context = invocation.getArgument(1);
                if (attempts == 0) {
                    assert !context.treatUpdateAsUpsert() : "on this test, first attept should be not trying updates as upserts";
                    throw new RollbackException("Forcing a rollback on the first attempt");
                }
                assert context.treatUpdateAsUpsert() : "on this test, only the first attept should be " + "not trying updates as upserts, but " + attempts + " is not trying updates as upserts";
                if (attempts < (atteptsToSucceed - 1)) {
                    throw new RollbackException("forcing a rollback on the " + attempts + "th attempt");
                }
                return null;
            } finally {
                attempts++;
            }
        }
    }).when(executor).execute(eq(operation), any());
    try {
        //WHEN
        OplogOperation result = executor.visit(batch, applierContext);
        //THEN
        then(executor).should(times(atteptsToSucceed)).execute(eq(operation), any());
        assertEquals(operation, result);
        return true;
    } catch (RetrierGiveUpException ignore) {
        return false;
    } finally {
        then(metrics).should().getSingleOpTimer(operation);
        then(timer).should().time();
    }
}
Also used : ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Context(com.codahale.metrics.Timer.Context) RollbackException(com.torodb.core.transaction.RollbackException) RetrierGiveUpException(com.torodb.core.retrier.RetrierGiveUpException) ApplierContext(com.torodb.mongodb.repl.oplogreplier.ApplierContext) Answer(org.mockito.stubbing.Answer) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Timer(com.codahale.metrics.Timer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) OplogOperation(com.eightkdata.mongowp.server.api.oplog.OplogOperation)

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