use of org.sagacity.sqltoy.config.model.ShardingConfig in project sagacity-sqltoy by chenrenfei.
the class ShardingUtils method groupShardings.
/**
* @todo 批量sharding策略处理
* @param sqlToyContext
* @param entities
* @param entityMeta
* @param dataSource
* @return
* @throws Exception
*/
public static Collection<ShardingGroupModel> groupShardings(SqlToyContext sqlToyContext, List<?> entities, EntityMeta entityMeta, DataSource dataSource) throws Exception {
ShardingConfig shardingConfig = entityMeta.getShardingConfig();
ShardingModel shardingModel = null;
String entityTable = entityMeta.getTableName();
// 没有sharding配置,则作为单个分组返回
if (shardingConfig == null) {
Collection<ShardingGroupModel> result = new ArrayList<ShardingGroupModel>();
ShardingGroupModel model = new ShardingGroupModel();
shardingModel = new ShardingModel();
shardingModel.setDataSource(dataSource);
shardingModel.setTableName(entityTable);
model.setShardingModel(shardingModel);
model.setEntities(entities);
result.add(model);
return result;
}
Class entityClass = entityMeta.getEntityClass();
// 分库
boolean hasDB = false;
ShardingStrategy dbStrategy = null;
List<Object[]> shardingDBValues = null;
ShardingStrategyConfig dbConfig = shardingConfig.getShardingDBStrategy();
if (dbConfig != null) {
hasDB = true;
dbStrategy = sqlToyContext.getShardingStrategy(dbConfig.getStrategy());
if (dbStrategy == null) {
throw new IllegalArgumentException("POJO 对象:" + entityClass.getName() + " Sharding DB Strategy:" + dbConfig.getStrategy() + " 未定义,请检查!");
}
shardingDBValues = BeanUtil.reflectBeansToInnerAry(entities, dbConfig.getFields(), null, null);
}
// 分表
boolean hasTable = false;
ShardingStrategy tableStrategy = null;
ShardingStrategyConfig tableConfig = shardingConfig.getShardingTableStrategy();
List<Object[]> shardingTableValues = null;
if (tableConfig != null) {
hasTable = true;
tableStrategy = sqlToyContext.getShardingStrategy(tableConfig.getStrategy());
if (tableStrategy == null) {
throw new IllegalArgumentException("POJO 对象:" + entityClass.getName() + " Sharding Table Strategy:" + tableConfig.getStrategy() + " 未定义,请检查!");
}
shardingTableValues = BeanUtil.reflectBeansToInnerAry(entities, tableConfig.getFields(), null, null);
}
Map<String, ShardingGroupModel> shardingGroupMaps = new HashMap<String, ShardingGroupModel>();
IgnoreCaseLinkedMap<String, Object> valueMap;
ShardingDBModel shardingDBModel = null;
// 数据分组key(dataSourceName+tableName)
String dataGroupKey;
String tableName = null;
String dataSourceName = null;
for (int i = 0; i < entities.size(); i++) {
// 分库
if (hasDB) {
valueMap = hashParams(dbConfig.getAliasNames(), shardingDBValues.get(i));
shardingDBModel = dbStrategy.getShardingDB(sqlToyContext, entityClass, entityTable, dbConfig.getDecisionType(), valueMap);
dataSourceName = shardingDBModel.getDataSourceName();
}
// 分表
if (hasTable) {
valueMap = hashParams(tableConfig.getAliasNames(), shardingTableValues.get(i));
tableName = tableStrategy.getShardingTable(sqlToyContext, entityClass, entityTable, tableConfig.getDecisionType(), valueMap);
}
// 分组key
dataGroupKey = dataSourceName + tableName;
// 归并到相同分组
if (shardingGroupMaps.containsKey(dataGroupKey)) {
shardingGroupMaps.get(dataGroupKey).getEntities().add(entities.get(i));
} else {
// 不同分组
ShardingGroupModel groupModel = new ShardingGroupModel();
groupModel.setKey(dataGroupKey);
// 创建数据分组集合
List items = new ArrayList();
items.add(entities.get(i));
groupModel.setEntities(items);
shardingModel = new ShardingModel();
// 分库,设置分组对应的数据库
if (hasDB) {
shardingModel.setDataSourceName(dataSourceName);
if (shardingDBModel.getDataSource() == null) {
shardingModel.setDataSource(sqlToyContext.getDataSourceBean(shardingDBModel.getDataSourceName()));
} else {
shardingModel.setDataSource(shardingDBModel.getDataSource());
}
} else {
shardingModel.setDataSource(dataSource);
}
// 分表,设置表名
if (hasTable && StringUtil.isNotBlank(tableName)) {
shardingModel.setTableName(tableName);
}
groupModel.setShardingModel(shardingModel);
shardingGroupMaps.put(dataGroupKey, groupModel);
}
}
return shardingGroupMaps.values();
}
use of org.sagacity.sqltoy.config.model.ShardingConfig in project sagacity-sqltoy by chenrenfei.
the class ParallelUtils method execute.
/**
* @todo 将集合进行根据sharding字段的值提取sharding策略并按照策略将集合分组,然后并行执行
* @param sqlToyContext
* @param entities
* @param wrapIdValue
* @param dataSource
* @param handler
* @return
* @throws Exception
*/
public static List execute(final SqlToyContext sqlToyContext, List entities, boolean wrapIdValue, DataSource dataSource, ParallelCallbackHandler handler) throws Exception {
// 获取对象的媒体信息
EntityMeta entityMeta = sqlToyContext.getEntityMeta(entities.get(0).getClass());
// 主键值需要提前按照主键策略赋予(sequence 和assign模式的不会实际执行赋值)
if (wrapIdValue) {
ShardingUtils.assignPKs(sqlToyContext, entityMeta, entities);
}
// 将批量集合数据按sharding策略处理后的库和表组成的key分组
Collection<ShardingGroupModel> shardingGroups = ShardingUtils.groupShardings(sqlToyContext, entities, entityMeta, dataSource);
// 单分组直接执行
if (shardingGroups.size() == 1) {
return handler.execute(sqlToyContext, shardingGroups.iterator().next());
}
// 开始多线程并行执行
ShardingConfig shardingConfig = entityMeta.getShardingConfig();
List results = new ArrayList();
// 并行线程数量
int threads = shardingGroups.size();
// 是否全局异常回滚
boolean globalRollback = shardingConfig.isGlobalRollback();
// 如果额外策略配置了线程数量,则按照指定的线程数量执行
if (threads > shardingConfig.getMaxConcurrents() && shardingConfig.getMaxConcurrents() > 1) {
threads = shardingConfig.getMaxConcurrents();
}
ExecutorService pool = Executors.newFixedThreadPool(threads);
List<Future<ShardingResult>> futureResults = new ArrayList<Future<ShardingResult>>();
Future<ShardingResult> future;
for (ShardingGroupModel group : shardingGroups) {
future = pool.submit(new DialectExecutor(sqlToyContext, group, handler));
futureResults.add(future);
}
pool.shutdown();
// 设置最大等待时长
if (shardingConfig.getMaxWaitSeconds() > 0) {
pool.awaitTermination(shardingConfig.getMaxWaitSeconds(), TimeUnit.SECONDS);
} else {
pool.awaitTermination(SqlToyConstants.PARALLEL_MAXWAIT_SECONDS, TimeUnit.SECONDS);
}
// 提取各个线程返回的结果进行合并
try {
ShardingResult item;
for (Future<ShardingResult> futureResult : futureResults) {
item = futureResult.get();
// 全局异常则抛出,让事务进行全部回滚。
if (item != null && !item.isSuccess() && globalRollback) {
throw new RuntimeException(item.getMessage());
}
if (item != null && item.getRows() != null && !item.getRows().isEmpty()) {
results.addAll(item.getRows());
}
}
} catch (Exception e) {
e.printStackTrace();
throw e;
} finally {
pool.shutdownNow();
}
return results;
}
Aggregations