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