Skip to content

Commit

Permalink
for #1172, refactor spi signature
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Sep 24, 2018
1 parent 4b8cd78 commit f0b7a94
Show file tree
Hide file tree
Showing 14 changed files with 56 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ private T execute0(final StatementExecuteUnit statementExecuteUnit) throws SQLEx
List<List<Object>> parameterSets = statementExecuteUnit.getRouteUnit().getSqlUnit().getParameterSets();
DataSourceMetaData dataSourceMetaData = DataSourceMetaDataFactory.newInstance(databaseType, statementExecuteUnit.getStatement().getConnection().getMetaData().getURL());
for (List<Object> each : parameterSets) {
SQLExecutionEventHandlerLoader.getInstance().handle(new SQLExecutionStartEvent(statementExecuteUnit.getRouteUnit(), each, dataSourceMetaData));
SQLExecutionEventHandlerLoader.getInstance().start(new SQLExecutionStartEvent(statementExecuteUnit.getRouteUnit(), each, dataSourceMetaData));
// TODO remove after BED removed
shardingEventBus.post(SQLExecutionEventFactory.createEvent(sqlType, statementExecuteUnit, each, dataSourceMetaData));
}
Expand All @@ -91,7 +91,7 @@ private T execute0(final StatementExecuteUnit statementExecuteUnit) throws SQLEx
for (List<Object> each : parameterSets) {
SQLExecutionEvent finishEvent = SQLExecutionEventFactory.createEvent(sqlType, statementExecuteUnit, each, dataSourceMetaData);
finishEvent.setExecuteSuccess();
SQLExecutionEventHandlerLoader.getInstance().handle(new SQLExecutionFinishEvent());
SQLExecutionEventHandlerLoader.getInstance().finish(new SQLExecutionFinishEvent());
// TODO remove after BED removed
shardingEventBus.post(finishEvent);
}
Expand All @@ -100,7 +100,7 @@ private T execute0(final StatementExecuteUnit statementExecuteUnit) throws SQLEx
for (List<Object> each : parameterSets) {
SQLExecutionEvent finishEvent = SQLExecutionEventFactory.createEvent(sqlType, statementExecuteUnit, each, dataSourceMetaData);
finishEvent.setExecuteFailure(ex);
SQLExecutionEventHandlerLoader.getInstance().handle(new SQLExecutionFinishEvent());
SQLExecutionEventHandlerLoader.getInstance().finish(new SQLExecutionFinishEvent());
// TODO remove after BED removed
shardingEventBus.post(finishEvent);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,18 +85,17 @@ public final class ParsingSQLRouter implements ShardingRouter {

@Override
public SQLStatement parse(final String logicSQL, final boolean useCache) {
ParsingEventHandlerLoader.getInstance().handle(new ParsingStartEvent(logicSQL));
ParsingEventHandlerLoader.getInstance().start(new ParsingStartEvent(logicSQL));
ParsingFinishEvent finishEvent = new ParsingFinishEvent();
try {
SQLStatement sqlStatement = new SQLParsingEngine(databaseType, logicSQL, shardingRule, shardingTableMetaData).parse(useCache);
return sqlStatement;
return new SQLParsingEngine(databaseType, logicSQL, shardingRule, shardingTableMetaData).parse(useCache);
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
finishEvent.setException(ex);
throw ex;
} finally {
ParsingEventHandlerLoader.getInstance().handle(finishEvent);
ParsingEventHandlerLoader.getInstance().finish(finishEvent);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public interface ShardingEventHandler<S extends ShardingStartEvent, F extends Sh
*
* @param event sharding start event
*/
void handle(S event);
void start(S event);

/**
* Handle sharding finish event.
*
* @param event sharding finish event
*/
void handle(F event);
void finish(F event);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public static CloseConnectionEventHandlerLoader getInstance() {
*
* @param event get connection start event
*/
public void handle(final CloseConnectionStartEvent event) {
public void start(final CloseConnectionStartEvent event) {
for (CloseConnectionEventHandler each : serviceLoader) {
each.handle(event);
each.start(event);
}
}

Expand All @@ -59,9 +59,9 @@ public void handle(final CloseConnectionStartEvent event) {
*
* @param event get connection finish event
*/
public void handle(final CloseConnectionFinishEvent event) {
public void finish(final CloseConnectionFinishEvent event) {
for (CloseConnectionEventHandler each : serviceLoader) {
each.handle(event);
each.finish(event);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public static GetConnectionEventHandlerLoader getInstance() {
*
* @param event get connection start event
*/
public void handle(final GetConnectionStartEvent event) {
public void start(final GetConnectionStartEvent event) {
for (GetConnectionEventHandler each : serviceLoader) {
each.handle(event);
each.start(event);
}
}

Expand All @@ -59,9 +59,9 @@ public void handle(final GetConnectionStartEvent event) {
*
* @param event get connection finish event
*/
public void handle(final GetConnectionFinishEvent event) {
public void finish(final GetConnectionFinishEvent event) {
for (GetConnectionEventHandler each : serviceLoader) {
each.handle(event);
each.finish(event);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public static SQLExecutionEventHandlerLoader getInstance() {
*
* @param event SQL execution start event
*/
public void handle(final SQLExecutionStartEvent event) {
public void start(final SQLExecutionStartEvent event) {
for (SQLExecutionEventHandler each : serviceLoader) {
each.handle(event);
each.start(event);
}
}

Expand All @@ -59,9 +59,9 @@ public void handle(final SQLExecutionStartEvent event) {
*
* @param event SQL execution finish event
*/
public void handle(final SQLExecutionFinishEvent event) {
public void finish(final SQLExecutionFinishEvent event) {
for (SQLExecutionEventHandler each : serviceLoader) {
each.handle(event);
each.finish(event);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public static ParsingEventHandlerLoader getInstance() {
*
* @param event parsing start event
*/
public void handle(final ParsingStartEvent event) {
public void start(final ParsingStartEvent event) {
for (ParsingEventHandler each : serviceLoader) {
each.handle(event);
each.start(event);
}
}

Expand All @@ -59,9 +59,9 @@ public void handle(final ParsingStartEvent event) {
*
* @param event parsing finish event
*/
public void handle(final ParsingFinishEvent event) {
public void finish(final ParsingFinishEvent event) {
for (ParsingEventHandler each : serviceLoader) {
each.handle(event);
each.finish(event);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public final Connection getConnection(final String dataSourceName) throws SQLExc
* @throws SQLException SQL exception
*/
public final List<Connection> getConnections(final ConnectionMode connectionMode, final String dataSourceName, final int connectionSize) throws SQLException {
GetConnectionEventHandlerLoader.getInstance().handle(new GetConnectionStartEvent(dataSourceName));
GetConnectionEventHandlerLoader.getInstance().start(new GetConnectionStartEvent(dataSourceName));
DataSource dataSource = getDataSourceMap().get(dataSourceName);
Preconditions.checkState(null != dataSource, "Missing the data source name: '%s'", dataSourceName);
Collection<Connection> connections;
Expand All @@ -124,7 +124,7 @@ public final List<Connection> getConnections(final ConnectionMode connectionMode
cachedConnections.putAll(dataSourceName, result);
}
}
GetConnectionEventHandlerLoader.getInstance().handle(
GetConnectionEventHandlerLoader.getInstance().finish(
new GetConnectionFinishEvent(connections.size(), DataSourceMetaDataFactory.newInstance(databaseType, result.get(0).getMetaData().getURL())));
return result;
}
Expand Down Expand Up @@ -226,10 +226,10 @@ public final void close() throws SQLException {
forceExecuteTemplateForClose.execute(cachedConnections.entries(), new ForceExecuteCallback<Map.Entry<String, Connection>>() {

@Override
public void execute(final Entry<String, Connection> cachedConnectionsEntrySet) throws SQLException {
Connection connection = cachedConnectionsEntrySet.getValue();
CloseConnectionEventHandlerLoader.getInstance().handle(
new CloseConnectionStartEvent(cachedConnectionsEntrySet.getKey(), DataSourceMetaDataFactory.newInstance(databaseType, connection.getMetaData().getURL())));
public void execute(final Entry<String, Connection> cachedConnections) throws SQLException {
Connection connection = cachedConnections.getValue();
CloseConnectionEventHandlerLoader.getInstance().start(
new CloseConnectionStartEvent(cachedConnections.getKey(), DataSourceMetaDataFactory.newInstance(databaseType, connection.getMetaData().getURL())));
CloseConnectionFinishEvent finishEvent = new CloseConnectionFinishEvent();
try {
connection.close();
Expand All @@ -239,7 +239,7 @@ public void execute(final Entry<String, Connection> cachedConnectionsEntrySet) t
finishEvent.setException(ex);
throw ex;
} finally {
CloseConnectionEventHandlerLoader.getInstance().handle(finishEvent);
CloseConnectionEventHandlerLoader.getInstance().finish(finishEvent);
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ public abstract class OpenTracingHandler<S extends ShardingStartEvent, F extends
private final String operationName;

@Override
public final void handle(final S event) {
public final void start(final S event) {
spanHolder.set(initSpan(event, createSpanBuilder()));
}

@Override
public final void handle(final F event) {
public final void finish(final F event) {
if (null != event.getException()) {
setErrorInfo(event);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public void assertExecuteSuccess() {
DataSourceMetaData dataSourceMetaData = mock(DataSourceMetaData.class);
when(dataSourceMetaData.getHostName()).thenReturn("localhost");
when(dataSourceMetaData.getPort()).thenReturn(8888);
loader.handle(new CloseConnectionStartEvent("test_ds_name", dataSourceMetaData));
loader.handle(new CloseConnectionFinishEvent());
loader.start(new CloseConnectionStartEvent("test_ds_name", dataSourceMetaData));
loader.finish(new CloseConnectionFinishEvent());
assertThat(getTracer().finishedSpans().size(), is(1));
MockSpan actual = getTracer().finishedSpans().get(0);
assertThat(actual.operationName(), is("/Sharding-Sphere/closeConnection/"));
Expand All @@ -62,10 +62,10 @@ public void assertExecuteFailure() {
DataSourceMetaData dataSourceMetaData = mock(DataSourceMetaData.class);
when(dataSourceMetaData.getHostName()).thenReturn("localhost");
when(dataSourceMetaData.getPort()).thenReturn(8888);
loader.handle(new CloseConnectionStartEvent("test_ds_name", dataSourceMetaData));
loader.start(new CloseConnectionStartEvent("test_ds_name", dataSourceMetaData));
CloseConnectionFinishEvent finishEvent = new CloseConnectionFinishEvent();
finishEvent.setException(new RuntimeException("close connection error"));
loader.handle(finishEvent);
loader.finish(finishEvent);
assertThat(getTracer().finishedSpans().size(), is(1));
MockSpan actual = getTracer().finishedSpans().get(0);
assertThat(actual.operationName(), is("/Sharding-Sphere/closeConnection/"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public final class OpenTracingGetConnectionEventHandlerTest extends BaseOpenTrac

@Test
public void assertExecuteSuccess() {
loader.handle(new GetConnectionStartEvent("test_ds_name"));
loader.start(new GetConnectionStartEvent("test_ds_name"));
DataSourceMetaData dataSourceMetaData = mock(DataSourceMetaData.class);
when(dataSourceMetaData.getHostName()).thenReturn("localhost");
when(dataSourceMetaData.getPort()).thenReturn(8888);
loader.handle(new GetConnectionFinishEvent(3, dataSourceMetaData));
loader.finish(new GetConnectionFinishEvent(3, dataSourceMetaData));
assertThat(getTracer().finishedSpans().size(), is(1));
MockSpan actual = getTracer().finishedSpans().get(0);
assertThat(actual.operationName(), is("/Sharding-Sphere/getConnection/"));
Expand All @@ -60,10 +60,10 @@ public void assertExecuteSuccess() {

@Test
public void assertExecuteFailure() {
loader.handle(new GetConnectionStartEvent("test_ds_name"));
loader.start(new GetConnectionStartEvent("test_ds_name"));
GetConnectionFinishEvent finishEvent = new GetConnectionFinishEvent(3, null);
finishEvent.setException(new RuntimeException("get connection error"));
loader.handle(finishEvent);
loader.finish(finishEvent);
assertThat(getTracer().finishedSpans().size(), is(1));
MockSpan actual = getTracer().finishedSpans().get(0);
assertThat(actual.operationName(), is("/Sharding-Sphere/getConnection/"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public void assertExecuteSuccessForTrunkThread() {
when(dataSourceMetaData.getHostName()).thenReturn("localhost");
when(dataSourceMetaData.getPort()).thenReturn(8888);
SQLUnit sqlUnit = new SQLUnit("SELECT * FROM XXX;", Collections.<List<Object>>emptyList());
loader.handle(new SQLExecutionStartEvent(new RouteUnit("ds_test", sqlUnit), Arrays.<Object>asList("1", 2), dataSourceMetaData));
loader.handle(new SQLExecutionFinishEvent());
loader.start(new SQLExecutionStartEvent(new RouteUnit("ds_test", sqlUnit), Arrays.<Object>asList("1", 2), dataSourceMetaData));
loader.finish(new SQLExecutionFinishEvent());
assertThat(getTracer().finishedSpans().size(), is(1));
MockSpan actual = getTracer().finishedSpans().get(0);
assertThat(actual.operationName(), is("/Sharding-Sphere/executeSQL/"));
Expand All @@ -85,9 +85,9 @@ public void assertExecuteSuccessForBranchThread() {
when(dataSourceMetaData.getHostName()).thenReturn("localhost");
when(dataSourceMetaData.getPort()).thenReturn(8888);
SQLUnit sqlUnit = new SQLUnit("SELECT * FROM XXX;", Collections.<List<Object>>emptyList());
loader.handle(new SQLExecutionStartEvent(new RouteUnit("ds_test", sqlUnit), Arrays.<Object>asList("1", 2), dataSourceMetaData));
loader.start(new SQLExecutionStartEvent(new RouteUnit("ds_test", sqlUnit), Arrays.<Object>asList("1", 2), dataSourceMetaData));
assertNotNull(OpenTracingRootInvokeHandler.getActiveSpan().get());
loader.handle(new SQLExecutionFinishEvent());
loader.finish(new SQLExecutionFinishEvent());
assertThat(getTracer().finishedSpans().size(), is(1));
MockSpan actual = getTracer().finishedSpans().get(0);
assertThat(actual.operationName(), is("/Sharding-Sphere/executeSQL/"));
Expand All @@ -111,10 +111,10 @@ public void assertExecuteFailure() {
when(dataSourceMetaData.getHostName()).thenReturn("localhost");
when(dataSourceMetaData.getPort()).thenReturn(8888);
SQLUnit sqlUnit = new SQLUnit("SELECT * FROM XXX;", Collections.<List<Object>>emptyList());
loader.handle(new SQLExecutionStartEvent(new RouteUnit("ds_test", sqlUnit), Collections.emptyList(), dataSourceMetaData));
loader.start(new SQLExecutionStartEvent(new RouteUnit("ds_test", sqlUnit), Collections.emptyList(), dataSourceMetaData));
SQLExecutionFinishEvent finishEvent = new SQLExecutionFinishEvent();
finishEvent.setException(new RuntimeException("SQL execution error"));
loader.handle(finishEvent);
loader.finish(finishEvent);
assertThat(getTracer().finishedSpans().size(), is(1));
MockSpan actual = getTracer().finishedSpans().get(0);
assertThat(actual.operationName(), is("/Sharding-Sphere/executeSQL/"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public final class OpenTracingParsingEventHandlerTest extends BaseOpenTracingHan

@Test
public void assertExecuteSuccess() {
loader.handle(new ParsingStartEvent("SELECT * FROM XXX;"));
loader.handle(new ParsingFinishEvent());
loader.start(new ParsingStartEvent("SELECT * FROM XXX;"));
loader.finish(new ParsingFinishEvent());
assertThat(getTracer().finishedSpans().size(), is(1));
MockSpan actual = getTracer().finishedSpans().get(0);
assertThat(actual.operationName(), is("/Sharding-Sphere/parseSQL/"));
Expand All @@ -52,10 +52,10 @@ public void assertExecuteSuccess() {

@Test
public void assertExecuteFailure() {
loader.handle(new ParsingStartEvent("SELECT * FROM XXX;"));
loader.start(new ParsingStartEvent("SELECT * FROM XXX;"));
ParsingFinishEvent finishEvent = new ParsingFinishEvent();
finishEvent.setException(new ShardingException("parse SQL error"));
loader.handle(finishEvent);
loader.finish(finishEvent);
assertThat(getTracer().finishedSpans().size(), is(1));
MockSpan actual = getTracer().finishedSpans().get(0);
assertThat(actual.operationName(), is("/Sharding-Sphere/parseSQL/"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,18 +71,18 @@ public final class BackendConnection implements AutoCloseable {
*/
public List<Connection> getConnections(final ConnectionMode connectionMode, final String dataSourceName, final int connectionSize) throws SQLException {
try {
GetConnectionEventHandlerLoader.getInstance().handle(new GetConnectionStartEvent(dataSourceName));
GetConnectionEventHandlerLoader.getInstance().start(new GetConnectionStartEvent(dataSourceName));
List<Connection> result = ruleRegistry.getBackendDataSource().getConnections(connectionMode, dataSourceName, connectionSize);
cachedConnections.addAll(result);
GetConnectionEventHandlerLoader.getInstance().handle(
GetConnectionEventHandlerLoader.getInstance().finish(
new GetConnectionFinishEvent(result.size(), DataSourceMetaDataFactory.newInstance(DatabaseType.MySQL, result.get(0).getMetaData().getURL())));
return result;
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
GetConnectionFinishEvent finishEvent = new GetConnectionFinishEvent(0, null);
finishEvent.setException(ex);
GetConnectionEventHandlerLoader.getInstance().handle(finishEvent);
GetConnectionEventHandlerLoader.getInstance().finish(finishEvent);
throw ex;
}
}
Expand Down Expand Up @@ -156,14 +156,14 @@ private Collection<SQLException> closeConnections() {
for (Connection each : cachedConnections) {
CloseConnectionFinishEvent finishEvent = new CloseConnectionFinishEvent();
try {
CloseConnectionEventHandlerLoader.getInstance().handle(
CloseConnectionEventHandlerLoader.getInstance().start(
new CloseConnectionStartEvent(each.getCatalog(), DataSourceMetaDataFactory.newInstance(DatabaseType.MySQL, each.getMetaData().getURL())));
each.close();
} catch (SQLException ex) {
finishEvent.setException(ex);
result.add(ex);
} finally {
CloseConnectionEventHandlerLoader.getInstance().handle(finishEvent);
CloseConnectionEventHandlerLoader.getInstance().finish(finishEvent);
}
}
return result;
Expand Down

0 comments on commit f0b7a94

Please sign in to comment.