Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
tianshouzhi committed Nov 8, 2017
1 parent 9d2133c commit d1df5ab
Show file tree
Hide file tree
Showing 55 changed files with 873 additions and 783 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
* Created by tianshouzhi on 2017/10/13.
*/
public interface DragonDataSource extends DataSource, AutoCloseable {
String getDsName();

void setDsName(String dsName);

void init() throws DragonException;

void close() throws DragonException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,22 @@ public abstract class DragonDataSourceAdapter extends WrapperAdapter implements

protected int loginTimeout = 0;

private PrintWriter logWriter;
protected PrintWriter logWriter;

protected volatile boolean init = false;

protected String dsName;

@Override
public String getDsName() {
return dsName;
}

@Override
public void setDsName(String dsName) {
this.dsName = dsName;
}

@Override
public PrintWriter getLogWriter() throws SQLException {
return logWriter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ private Connection buildNewReadConnectionIfNeed() throws SQLException {

public Connection buildNewWriteConnectionIfNeed() throws SQLException {
if (this.realConnection == null || this.realConnection.isReadOnly()) {
DatasourceUtil.close(dragonHADatasource.getHADSName(), getRealDSName(),realConnection);
DatasourceUtil.close(dragonHADatasource.getDsName(), getRealDSName(),realConnection);
this.realDSName = this.dragonHADatasource.getRouterManager().routeWrite();
this.realConnection = this.dragonHADatasource.getConnectionByRealDSName(realDSName);
setConnectionParams(this.realConnection);
Expand Down Expand Up @@ -386,7 +386,7 @@ public String getRealDSName() {
}

public String getFullName(){
return dragonHADatasource.getHADSName()+"."+realDSName;
return dragonHADatasource.getDsName()+"."+realDSName;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.tianshouzhi.dragon.common.jdbc.datasource.DragonDataSourceAdapter;
import com.tianshouzhi.dragon.common.log.Log;
import com.tianshouzhi.dragon.common.log.LoggerFactory;
import com.tianshouzhi.dragon.common.util.StringUtils;
import com.tianshouzhi.dragon.ha.config.HAConfigManager;
import com.tianshouzhi.dragon.ha.config.HADataSourceConfig;
import com.tianshouzhi.dragon.ha.config.HALocalConfigManager;
Expand All @@ -12,120 +13,146 @@
import com.tianshouzhi.dragon.ha.exception.DragonHAException;
import com.tianshouzhi.dragon.ha.jdbc.connection.DragonHAConnection;
import com.tianshouzhi.dragon.ha.router.RouterManager;
import com.tianshouzhi.dragon.ha.router.RouterStrategy;
import com.tianshouzhi.dragon.ha.util.DatasourceUtil;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
* Created by TIANSHOUZHI336 on 2016/12/2.
*/
public class DragonHADatasource extends DragonDataSourceAdapter {

private String haDSName;

private static final Log LOGGER = LoggerFactory.getLogger(DragonHADatasource.class);

private Map<String, RealDataSourceWrapper> realDSWrapperMap = new ConcurrentHashMap<String, RealDataSourceWrapper>(4);

private HAConfigManager configManager;

private boolean lazyInit = true;

private volatile RouterManager routerManager;

public DragonHADatasource() {
this.haDSName=DatasourceUtil.generateDataSourceName("DragonHADatasource");
}

@Override
protected void doInit() throws Exception {
if (realDSWrapperMap.isEmpty()) {// 没有通过编程式方式设置datasource
if (configManager == null) {
throw new DragonHAException("configManager can't be null !");
} else {
HADataSourceConfig haDataSourceConfig = configManager.getHADataSourceConfig();
Map<String, RealDataSourceConfig> realDataSourceConfigMap = haDataSourceConfig.getRealDataSourceConfigMap();
for (Map.Entry<String, RealDataSourceConfig> configEntry : realDataSourceConfigMap.entrySet()) {
String key = configEntry.getKey();
RealDataSourceConfig config = configEntry.getValue();
addRealDatasource(key, config.getReadWeight(), config.getWriteWeight(), config.getRealDsProperties(),
config.getRealDsClass());
}
}
}
if (!lazyInit) {
for (RealDataSourceWrapper realDataSourceWrapper : realDSWrapperMap.values()) {
try{
realDataSourceWrapper.init();
}catch (Exception e){
throw new DragonHAException("init real datasource"+realDataSourceWrapper.getFullName()+"error",e);
}
}
}
this.routerManager = new RouterManager(this);
}

@Override
protected DragonHAConnection doGetConnection(String username, String password) throws SQLException {
return new DragonHAConnection(username, password, this);
}

@Override
public void close() throws DragonException {
LOGGER.info("close dragon ha datasource start ...");
for (RealDataSourceWrapper realDataSourceWrapper : this.realDSWrapperMap.values()) {
LOGGER.info("close real datasource[" + realDataSourceWrapper.getRealDSName() + "]...");
realDataSourceWrapper.close();
LOGGER.info("close real datasource[" + realDataSourceWrapper.getRealDSName() + "] success...");
}
LOGGER.info("close dragon ha datasource ...");
}

public void addRealDatasource(String index, int readWeight, int writeWeight, DataSource dataSource) {
realDSWrapperMap.put(index, new RealDataSourceWrapper(index, readWeight, writeWeight, dataSource));
}

public void addRealDatasource(String index, int readWeight, int writeWeight, Properties properties, String dsClass) {
RealDataSourceWrapper realDataSourceWrapper = new RealDataSourceWrapper(haDSName,index, readWeight, writeWeight,
properties, dsClass);
realDSWrapperMap.put(index, realDataSourceWrapper);
}

public RouterManager getRouterManager() {
return routerManager;
}

public Connection getConnectionByRealDSName(String realDSName) throws SQLException {
RealDataSourceWrapper realDataSourceWrapper = this.realDSWrapperMap.get(realDSName);
if (realDataSourceWrapper == null) {
throw new DragonHAException("not valid datasource found with realDSName:" + realDSName);
}
if(!DataSourceMonitor.isAvailable(haDSName,realDataSourceWrapper)){
throw new DragonHAException(realDataSourceWrapper.getFullName()+" is not available!!!");
}
try{
return realDataSourceWrapper.getConnection();
}catch (SQLException e){
DataSourceMonitor.monitor(e, haDSName,realDataSourceWrapper);
throw e;
}
}

public void setLocalConfigFile(String configFile) {
this.haDSName = DatasourceUtil.generateDataSourceName("DragonHADatasource("+configFile+")");
this.configManager = new HALocalConfigManager(configFile);
}

public Map<String,RealDataSourceWrapper> getRealDataSourceWrapperMap(){
return realDSWrapperMap;
}

public String getHADSName() {
return haDSName;
}
private static final Log LOGGER = LoggerFactory.getLogger(DragonHADatasource.class);

private boolean lazyInit = true;

private String configFile;

private Map<String, RealDataSourceWrapper> realDSMap = new ConcurrentHashMap<String, RealDataSourceWrapper>(4);

private HAConfigManager configManager;

private RouterManager routerManager;

private RouterStrategy routerStrategy=RouterStrategy.ROUND_ROBIN;

@Override
protected void doInit() throws Exception {
initDsName();
LOGGER.info("init dragon ha datasource(" + dsName + ")");
initConfigManager();
initRealDSMap();
initRouterManager();
}

private void initDsName() {
if (StringUtils.isNotBlank(dsName)) {
return;
}else{
this.dsName = DatasourceUtil.generateDataSourceName("DragonHADatasource");
}
}

private void initConfigManager() {
if (StringUtils.isNotBlank(configFile)) {
this.configManager = new HALocalConfigManager(configFile);
}
}

private void initRealDSMap() {
if (realDSMap.isEmpty()) {// 没有通过编程式方式设置datasource
if (configManager == null) {
throw new DragonHAException("configManager can't be null !");
} else {
HADataSourceConfig haDataSourceConfig = configManager.getHADataSourceConfig();
Map<String, RealDataSourceConfig> realDataSourceConfigMap = haDataSourceConfig.getRealDataSourceConfigMap();
for (Map.Entry<String, RealDataSourceConfig> configEntry : realDataSourceConfigMap.entrySet()) {
String realDsName = configEntry.getKey();
RealDataSourceConfig config = configEntry.getValue();
addRealDatasource(this.dsName,realDsName, config.getReadWeight(), config.getWriteWeight(), config.getRealDsProperties(), config.getRealDsClass());
}
}
}

//check lazyInit
if (!lazyInit) {
for (RealDataSourceWrapper realDataSourceWrapper : realDSMap.values()) {
try {
realDataSourceWrapper.init();
} catch (Exception e) {
throw new DragonHAException("init real datasource" + realDataSourceWrapper.getFullName() + "error", e);
}
}
}
}

private void initRouterManager() {
this.routerManager = new RouterManager(this);
}

public void addRealDatasource(String index, int readWeight, int writeWeight, DataSource dataSource) {
realDSMap.put(index, new RealDataSourceWrapper(index, readWeight, writeWeight, dataSource));
}

public void addRealDatasource(String haDSName,String realDSName, int readWeight, int writeWeight, Properties properties, String dsClass) {
RealDataSourceWrapper realDataSourceWrapper = new RealDataSourceWrapper(haDSName, realDSName, readWeight, writeWeight,
properties, dsClass);
realDSMap.put(realDSName, realDataSourceWrapper);
}

@Override
protected DragonHAConnection doGetConnection(String username, String password) throws SQLException {
return new DragonHAConnection(username, password, this);
}

public Connection getConnectionByRealDSName(String realDSName) throws SQLException {
RealDataSourceWrapper realDataSourceWrapper = this.realDSMap.get(realDSName);
if (realDataSourceWrapper == null) {
throw new DragonHAException("not valid datasource found with realDSName:" + realDSName);
}
if (!DataSourceMonitor.isAvailable(dsName, realDataSourceWrapper)) {
throw new DragonHAException(realDataSourceWrapper.getFullName() + " is not available!!!");
}
try {
return realDataSourceWrapper.getConnection();
} catch (SQLException e) {
DataSourceMonitor.monitor(e, dsName, realDataSourceWrapper);
throw e;
}
}

@Override
public void close() throws DragonException {
LOGGER.info(" close dragon ha datasource(" + getDsName() + ")...");
for (RealDataSourceWrapper realDataSourceWrapper : this.realDSMap.values()) {
realDataSourceWrapper.close();
}
}

public void setConfigFile(String configFile) {
this.configFile = configFile;
}

public Map<String, RealDataSourceWrapper> getRealDSMap() {
return realDSMap;
}

public boolean isLazyInit() {
return lazyInit;
}

public void setLazyInit(boolean lazyInit) {
this.lazyInit = lazyInit;
}

public RouterManager getRouterManager() {
return routerManager;
}
}
Loading

0 comments on commit d1df5ab

Please sign in to comment.