Skip to content

Commit

Permalink
Refactor structure of tracing module (#2356)
Browse files Browse the repository at this point in the history
* Rename RDBTracingStorageConfiguration

* Refactor structure of tracing module

* Refactor structure of tracing module

* Refactor structure of tracing module
  • Loading branch information
terrymanu authored Oct 31, 2023
1 parent 2034bfd commit 8d45e6f
Show file tree
Hide file tree
Showing 50 changed files with 232 additions and 281 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.elasticjob.tracing.rdb.datasource;
package org.apache.shardingsphere.elasticjob.tracing.rdb.config;

import com.google.common.base.CaseFormat;
import com.google.common.base.Joiner;
Expand All @@ -24,7 +24,9 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.apache.shardingsphere.elasticjob.kernel.tracing.api.TracingStorageConfiguration;
import org.apache.shardingsphere.elasticjob.kernel.tracing.config.TracingStorageConfiguration;
import org.apache.shardingsphere.elasticjob.tracing.rdb.storage.datasource.DataSourceRegistry;
import org.apache.shardingsphere.elasticjob.tracing.rdb.storage.datasource.JDBCParameterDecorator;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;

import javax.sql.DataSource;
Expand All @@ -38,11 +40,11 @@
import java.util.Optional;

/**
* Data source configuration.
* RDB tracing storage configuration.
*/
@RequiredArgsConstructor
@Getter
public final class DataSourceConfiguration implements TracingStorageConfiguration<DataSource> {
public final class RDBTracingStorageConfiguration implements TracingStorageConfiguration<DataSource> {

private static final String GETTER_PREFIX = "get";

Expand All @@ -67,8 +69,8 @@ public final class DataSourceConfiguration implements TracingStorageConfiguratio
* @param dataSource data source
* @return data source configuration
*/
public static DataSourceConfiguration getDataSourceConfiguration(final DataSource dataSource) {
DataSourceConfiguration result = new DataSourceConfiguration(dataSource.getClass().getName());
public static RDBTracingStorageConfiguration getDataSourceConfiguration(final DataSource dataSource) {
RDBTracingStorageConfiguration result = new RDBTracingStorageConfiguration(dataSource.getClass().getName());
result.props.putAll(findAllGetterProperties(dataSource));
return result;
}
Expand Down Expand Up @@ -97,11 +99,6 @@ private static Collection<Method> findAllGetterMethods(final Class<?> clazz) {
return result;
}

@Override
public DataSource getStorage() {
return DataSourceRegistry.getInstance().getDataSource(this);
}

/**
* Create data source.
*
Expand Down Expand Up @@ -135,12 +132,17 @@ private Optional<Method> findSetterMethod(final Method[] methods, final String p
return Optional.empty();
}

@Override
public DataSource getStorage() {
return DataSourceRegistry.getInstance().getDataSource(this);
}

@Override
public boolean equals(final Object obj) {
return this == obj || null != obj && getClass() == obj.getClass() && equalsByProperties((DataSourceConfiguration) obj);
return this == obj || null != obj && getClass() == obj.getClass() && equalsByProperties((RDBTracingStorageConfiguration) obj);
}

private boolean equalsByProperties(final DataSourceConfiguration dataSourceConfig) {
private boolean equalsByProperties(final RDBTracingStorageConfiguration dataSourceConfig) {
return dataSourceClassName.equals(dataSourceConfig.dataSourceClassName) && props.equals(dataSourceConfig.props);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.apache.shardingsphere.elasticjob.kernel.tracing.event.JobExecutionEvent;
import org.apache.shardingsphere.elasticjob.kernel.tracing.event.JobStatusTraceEvent;
import org.apache.shardingsphere.elasticjob.kernel.tracing.listener.TracingListener;
import org.apache.shardingsphere.elasticjob.tracing.rdb.storage.RDBJobEventStorage;
import org.apache.shardingsphere.elasticjob.tracing.rdb.storage.repository.RDBJobEventRepository;

import javax.sql.DataSource;
import java.sql.SQLException;
Expand All @@ -30,10 +30,10 @@
*/
public final class RDBTracingListener implements TracingListener {

private final RDBJobEventStorage repository;
private final RDBJobEventRepository repository;

public RDBTracingListener(final DataSource dataSource) throws SQLException {
repository = RDBJobEventStorage.getInstance(dataSource);
repository = RDBJobEventRepository.getInstance(dataSource);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,34 @@
* limitations under the License.
*/

package org.apache.shardingsphere.elasticjob.tracing.rdb.datasource;
package org.apache.shardingsphere.elasticjob.tracing.rdb.storage.converter;

import lombok.extern.slf4j.Slf4j;
import org.apache.shardingsphere.elasticjob.kernel.tracing.api.TracingStorageConfiguration;
import org.apache.shardingsphere.elasticjob.kernel.tracing.config.TracingStorageConfiguration;
import org.apache.shardingsphere.elasticjob.kernel.tracing.exception.TracingStorageUnavailableException;
import org.apache.shardingsphere.elasticjob.kernel.tracing.storage.TracingStorageConverter;
import org.apache.shardingsphere.elasticjob.tracing.rdb.config.RDBTracingStorageConfiguration;
import org.apache.shardingsphere.elasticjob.tracing.rdb.storage.datasource.DataSourceRegistry;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
* {@link TracingStorageConverter} for {@link DataSource}.
* RDB tracing storage converter.
*/
@Slf4j
public final class DataSourceTracingStorageConverter implements TracingStorageConverter<DataSource> {
public final class RDBTracingStorageConverter implements TracingStorageConverter<DataSource> {

@Override
public TracingStorageConfiguration<DataSource> convertObjectToConfiguration(final DataSource dataSource) {
public TracingStorageConfiguration<DataSource> convertToConfiguration(final DataSource dataSource) {
try (Connection connection = dataSource.getConnection()) {
log.trace("Try to get connection from {}", connection.getMetaData().getURL());
} catch (final SQLException ex) {
log.error(ex.getLocalizedMessage(), ex);
throw new TracingStorageUnavailableException(ex);
}
DataSourceConfiguration result = DataSourceConfiguration.getDataSourceConfiguration(dataSource);
RDBTracingStorageConfiguration result = RDBTracingStorageConfiguration.getDataSourceConfiguration(dataSource);
DataSourceRegistry.getInstance().registerDataSource(result, dataSource);
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,24 +15,25 @@
* limitations under the License.
*/

package org.apache.shardingsphere.elasticjob.tracing.rdb.datasource;
package org.apache.shardingsphere.elasticjob.tracing.rdb.storage.datasource;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.elasticjob.tracing.rdb.config.RDBTracingStorageConfiguration;

import javax.sql.DataSource;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

/**
* Mapping tracing storage configuration} to data source.
* Mapping tracing storage configuration to data source.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class DataSourceRegistry {

private static volatile DataSourceRegistry instance;

private final ConcurrentMap<DataSourceConfiguration, DataSource> dataSources = new ConcurrentHashMap<>();
private final ConcurrentMap<RDBTracingStorageConfiguration, DataSource> dataSources = new ConcurrentHashMap<>();

/**
* Get instance of {@link DataSourceRegistry}.
Expand All @@ -50,17 +51,23 @@ public static DataSourceRegistry getInstance() {
return instance;
}

void registerDataSource(final DataSourceConfiguration dataSourceConfig, final DataSource dataSource) {
/**
* Register data source.
*
* @param dataSourceConfig data source configuration
* @param dataSource data source
*/
public void registerDataSource(final RDBTracingStorageConfiguration dataSourceConfig, final DataSource dataSource) {
dataSources.putIfAbsent(dataSourceConfig, dataSource);
}

/**
* Get {@link DataSource} by {@link DataSourceConfiguration}.
* Get {@link DataSource} by {@link RDBTracingStorageConfiguration}.
*
* @param dataSourceConfig data source configuration
* @return instance of {@link DataSource}
*/
public DataSource getDataSource(final DataSourceConfiguration dataSourceConfig) {
return dataSources.computeIfAbsent(dataSourceConfig, DataSourceConfiguration::createDataSource);
public DataSource getDataSource(final RDBTracingStorageConfiguration dataSourceConfig) {
return dataSources.computeIfAbsent(dataSourceConfig, RDBTracingStorageConfiguration::createDataSource);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

package org.apache.shardingsphere.elasticjob.tracing.rdb.datasource;
package org.apache.shardingsphere.elasticjob.tracing.rdb.storage.datasource;

import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI;

Expand Down
Loading

0 comments on commit 8d45e6f

Please sign in to comment.