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;
}
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);
}
}
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);
}
}
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);
}
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();
}
}
Aggregations