Search in sources :

Example 1 with ShardingResult

use of org.sagacity.sqltoy.model.ShardingResult in project sagacity-sqltoy by chenrenfei.

the class DialectExecutor method call.

/**
 * 任务的具体过程,一旦任务传给ExecutorService的submit方法,则该方法自动在一个线程上执行。
 *
 * @return
 */
public ShardingResult call() {
    String dataSourceName = shardingGroupModel.getShardingModel().getDataSourceName();
    String tableName = shardingGroupModel.getShardingModel().getTableName();
    ShardingResult result = new ShardingResult();
    // 异常捕获掉,确保其他节点可以正常执行
    try {
        result.setRows(handler.execute(sqltoyContext, shardingGroupModel));
    } catch (Exception e) {
        e.printStackTrace();
        result.setSuccess(false);
        result.setMessage("分库分表执行,DataSource节点:" + dataSourceName + ",table=" + tableName + " 发生异常:" + e.getMessage());
        logger.error("分库分表执行,DataSource节点:{},table={} 发生异常:{}", dataSourceName, tableName, e.getMessage());
    }
    return result;
}
Also used : ShardingResult(org.sagacity.sqltoy.model.ShardingResult)

Example 2 with ShardingResult

use of org.sagacity.sqltoy.model.ShardingResult in project sagacity-sqltoy by chenrenfei.

the class ParallelUtils method execute.

/**
 * @todo 将集合进行根据sharding字段的值提取sharding策略并按照策略将集合分组,然后并行执行
 * @param sqlToyContext
 * @param entities
 * @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();
    // 如果额外策略配置了线程数量,则按照指定的线程数量执行
    if (threads > shardingConfig.getMaxConcurrents() && shardingConfig.getMaxConcurrents() > 1)
        threads = shardingConfig.getMaxConcurrents();
    ExecutorService pool = Executors.newFixedThreadPool(threads);
    List<Future<ShardingResult>> futureResult = new ArrayList<Future<ShardingResult>>();
    for (final ShardingGroupModel group : shardingGroups) {
        Future<ShardingResult> future = pool.submit(new DialectExecutor(sqlToyContext, group, handler));
        futureResult.add(future);
    }
    pool.shutdown();
    // 设置最大等待时长
    if (shardingConfig.getMaxWaitSeconds() > 0)
        pool.awaitTermination(shardingConfig.getMaxWaitSeconds(), TimeUnit.SECONDS);
    // 提取各个线程返回的结果进行合并
    try {
        for (Future<ShardingResult> future : futureResult) {
            ShardingResult item = future.get();
            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;
}
Also used : ShardingResult(org.sagacity.sqltoy.model.ShardingResult) EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) ArrayList(java.util.ArrayList) ShardingGroupModel(org.sagacity.sqltoy.model.ShardingGroupModel) ShardingConfig(org.sagacity.sqltoy.config.model.ShardingConfig) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ArrayList(java.util.ArrayList) List(java.util.List)

Example 3 with ShardingResult

use of org.sagacity.sqltoy.model.ShardingResult in project sagacity-sqltoy by chenrenfei.

the class DialectExecutor method call.

/**
 * 任务的具体过程,一旦任务传给ExecutorService的submit方法,则该方法自动在一个线程上执行。
 *
 * @return
 */
public ShardingResult call() {
    String dataSourceName = shardingGroupModel.getShardingModel().getDataSourceName();
    String tableName = shardingGroupModel.getShardingModel().getTableName();
    ShardingResult result = new ShardingResult();
    // 异常捕获掉,确保其他节点可以正常执行
    try {
        result.setRows(handler.execute(sqltoyContext, shardingGroupModel));
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        e.printStackTrace();
        result.setSuccess(false);
        result.setMessage("分库分表执行,DataSource节点:" + dataSourceName + ",table=" + tableName + " 发生异常:" + e.getMessage());
        logger.error("分库分表执行,DataSource节点:{},table={} 发生异常:{}", dataSourceName, tableName, e.getMessage());
    } finally {
        SqlExecuteStat.destroy();
    }
    return result;
}
Also used : ShardingResult(org.sagacity.sqltoy.model.ShardingResult)

Example 4 with ShardingResult

use of org.sagacity.sqltoy.model.ShardingResult in project sagacity-sqltoy by chenrenfei.

the class DialectExecutor method call.

/**
 * @todo 任务的具体过程,一旦任务传给ExecutorService的submit方法,则该方法自动在一个线程上执行。
 * @return
 */
public ShardingResult call() {
    String dataSourceName = shardingGroupModel.getShardingModel().getDataSourceName();
    String tableName = shardingGroupModel.getShardingModel().getTableName();
    ShardingResult result = new ShardingResult();
    // 异常捕获掉,确保其他线程可以正常执行
    try {
        logger.debug("执行分库分表,DataSource节点:{},table={}", dataSourceName, tableName);
        result.setRows(handler.execute(sqltoyContext, shardingGroupModel));
    } catch (Exception e) {
        SqlExecuteStat.error(e);
        e.printStackTrace();
        result.setSuccess(false);
        result.setMessage("执行分库分表,DataSource节点:" + dataSourceName + ",table=" + tableName + " 发生异常:" + e.getMessage());
        logger.error("执行分库分表,DataSource节点:{},table={} 发生异常:{}", dataSourceName, tableName, e.getMessage());
    } finally {
        SqlExecuteStat.destroy();
    }
    return result;
}
Also used : ShardingResult(org.sagacity.sqltoy.model.ShardingResult)

Example 5 with ShardingResult

use of org.sagacity.sqltoy.model.ShardingResult 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;
}
Also used : ShardingResult(org.sagacity.sqltoy.model.ShardingResult) EntityMeta(org.sagacity.sqltoy.config.model.EntityMeta) DialectExecutor(org.sagacity.sqltoy.dialect.executor.DialectExecutor) ArrayList(java.util.ArrayList) ShardingGroupModel(org.sagacity.sqltoy.config.model.ShardingGroupModel) ShardingConfig(org.sagacity.sqltoy.config.model.ShardingConfig) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

ShardingResult (org.sagacity.sqltoy.model.ShardingResult)5 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 EntityMeta (org.sagacity.sqltoy.config.model.EntityMeta)2 ShardingConfig (org.sagacity.sqltoy.config.model.ShardingConfig)2 ShardingGroupModel (org.sagacity.sqltoy.config.model.ShardingGroupModel)1 DialectExecutor (org.sagacity.sqltoy.dialect.executor.DialectExecutor)1 ShardingGroupModel (org.sagacity.sqltoy.model.ShardingGroupModel)1