Skip to content

Commit

Permalink
Refactor ShardingSphereDriverUtils
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu committed Jun 14, 2024
1 parent 233d142 commit 3ed8ebc
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,7 @@ public Optional<ContextManager> getContextManager() {
if (isEnhancedForProxy) {
return Optional.ofNullable(ProxyContext.getInstance().getContextManager());
}
Optional<Map<String, ShardingSphereDataSource>> dataSourceMap = ShardingSphereDriverUtils.findShardingSphereDataSources();
if (dataSourceMap.isPresent() && !dataSourceMap.get().isEmpty()) {
return Optional.ofNullable(AgentReflectionUtils.getFieldValue(dataSourceMap.get().values().iterator().next(), "contextManager"));
}
return Optional.empty();
Map<String, ShardingSphereDataSource> dataSourceMap = ShardingSphereDriverUtils.findShardingSphereDataSources();
return dataSourceMap.isEmpty() ? Optional.empty() : Optional.ofNullable(AgentReflectionUtils.getFieldValue(dataSourceMap.values().iterator().next(), "contextManager"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.sql.DataSource;
import java.sql.Driver;
import java.sql.DriverManager;
import java.util.Collections;
import java.util.Enumeration;
import java.util.LinkedHashMap;
import java.util.Map;
Expand All @@ -43,22 +44,23 @@ public final class ShardingSphereDriverUtils {
*
* @return found data source
*/
public static Optional<Map<String, ShardingSphereDataSource>> findShardingSphereDataSources() {
return findShardingSphereDriver().flatMap(ShardingSphereDriverUtils::findShardingSphereDataSources);
public static Map<String, ShardingSphereDataSource> findShardingSphereDataSources() {
return findShardingSphereDriver().map(ShardingSphereDriverUtils::findShardingSphereDataSources).orElse(Collections.emptyMap());
}

private static Optional<Map<String, ShardingSphereDataSource>> findShardingSphereDataSources(final Driver driver) {
private static Map<String, ShardingSphereDataSource> findShardingSphereDataSources(final Driver driver) {
DriverDataSourceCache dataSourceCache = AgentReflectionUtils.getFieldValue(driver, "dataSourceCache");
Map<String, DataSource> dataSourceMap = AgentReflectionUtils.getFieldValue(dataSourceCache, "dataSourceMap");
Map<String, ShardingSphereDataSource> result = new LinkedHashMap<>();
Map<String, ShardingSphereDataSource> result = new LinkedHashMap<>(dataSourceMap.size(), 1F);
for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
if (entry.getValue() instanceof ShardingSphereDataSource) {
result.put(entry.getKey(), (ShardingSphereDataSource) entry.getValue());
}
}
return Optional.of(result);
return result;
}

@SuppressWarnings("UseOfJDBCDriverClass")
private static Optional<ShardingSphereDriver> findShardingSphereDriver() {
Enumeration<Driver> driverEnumeration = DriverManager.getDrivers();
while (driverEnumeration.hasMoreElements()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,10 @@ public final class JDBCMetaDataInfoExporter implements MetricsExporter {

@Override
public Optional<GaugeMetricFamilyMetricsCollector> export(final String pluginType) {
Optional<Map<String, ShardingSphereDataSource>> dataSourceMap = ShardingSphereDriverUtils.findShardingSphereDataSources();
if (!dataSourceMap.isPresent()) {
return Optional.empty();
}
Map<String, ShardingSphereDataSource> dataSourceMap = ShardingSphereDriverUtils.findShardingSphereDataSources();
GaugeMetricFamilyMetricsCollector result = MetricsCollectorRegistry.get(config, pluginType);
result.cleanMetrics();
for (Entry<String, ShardingSphereDataSource> entry : dataSourceMap.get().entrySet()) {
for (Entry<String, ShardingSphereDataSource> entry : dataSourceMap.entrySet()) {
ShardingSphereDataSource dataSource = entry.getValue();
String databaseName = AgentReflectionUtils.getFieldValue(dataSource, "databaseName");
ContextManager contextManager = AgentReflectionUtils.getFieldValue(dataSource, "contextManager");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,10 @@ public final class JDBCStateExporter implements MetricsExporter {

@Override
public Optional<GaugeMetricFamilyMetricsCollector> export(final String pluginType) {
Optional<Map<String, ShardingSphereDataSource>> dataSourceMap = ShardingSphereDriverUtils.findShardingSphereDataSources();
if (!dataSourceMap.isPresent()) {
return Optional.empty();
}
Map<String, ShardingSphereDataSource> dataSourceMap = ShardingSphereDriverUtils.findShardingSphereDataSources();
GaugeMetricFamilyMetricsCollector result = MetricsCollectorRegistry.get(config, pluginType);
result.cleanMetrics();
for (Entry<String, ShardingSphereDataSource> entry : dataSourceMap.get().entrySet()) {
for (Entry<String, ShardingSphereDataSource> entry : dataSourceMap.entrySet()) {
ShardingSphereDataSource dataSource = entry.getValue();
ContextManager contextManager = AgentReflectionUtils.getFieldValue(dataSource, "contextManager");
result.addMetric(Collections.emptyList(), contextManager.getComputeNodeInstanceContext().getInstance().getState().getCurrentState().ordinal());
Expand Down

0 comments on commit 3ed8ebc

Please sign in to comment.