shardingsphere icon indicating copy to clipboard operation
shardingsphere copied to clipboard

服务治理 重新创建 sql执行引擎停止失败后 不能执行新任务 问题

Open cwpgh opened this issue 8 years ago • 6 comments

个人小意见:线程池关闭应该平滑过渡 nginx 好像就是就有这一块设计

cwpgh avatar Jan 22 '18 08:01 cwpgh

我没有理解你的意思,能否提供版本号,报错信息,sql,表结构,表数据,配置信息以及执行代码等详细信息。 请尽量在提交issue时说明白上下文,以及填写我们提供的信息模板,谢谢

terrymanu avatar Jan 22 '18 13:01 terrymanu

测试代码如下:如果线程池关闭失败,就不能处理后续多用户请求

import java.util.ArrayList;
import java.util.List;

import io.shardingjdbc.core.constant.SQLType;
import io.shardingjdbc.core.executor.BaseStatementUnit;
import io.shardingjdbc.core.executor.ExecuteCallback;
import io.shardingjdbc.core.executor.ExecutorEngine;
import io.shardingjdbc.core.executor.type.statement.StatementUnit;
import io.shardingjdbc.core.jdbc.core.connection.ShardingConnection;
import io.shardingjdbc.core.jdbc.core.statement.ShardingStatement;
import io.shardingjdbc.core.routing.SQLExecutionUnit;

public final class Test {
   static boolean success=false;//是否可以成功关闭
	
	public static void main(String[] args) {
		
		final List<ExecutorEngine> engs=new ArrayList<>();
		engs.add(new ExecutorEngine(2));//分片上下文
		
		new Thread("接收用户请求线程") {
			@Override
			public void run() {
				int task=0;
				while(true) {//相当于socket监听
					task++;
					System.out.println("收到用户"+task+"请求");
					execue(engs,task);//处理用户请求
					
					try {
						Thread.sleep(1000);//1s一用户请求
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
				}
			}
		}.start();
		
		 new Thread("服务治理通知处理线程") {
				public void run() {
					try {
						Thread.sleep(15000);//休眠15秒后收到刷新配置通知
					} catch (InterruptedException e1) {
						e1.printStackTrace();
					}
					
					try {
						//关闭开关
						 System.out.println("关闭引擎");
						 engs.get(0).close();
						 System.out.println("关闭引擎成功");
						 engs.set(0, new ExecutorEngine(2));//重新生成
					} catch (Exception e1) {
						e1.printStackTrace();
						 System.out.println("引擎关闭失败");
					}
				}
			}.start();
	}
	static void execue(final List<ExecutorEngine> engs,final int task){
		new  Thread("用户"+task+"请求处理线程") {
			 public void run() {
				 List<StatementUnit> li=new ArrayList<>();
				 li.add(new StatementUnit(new SQLExecutionUnit("1",String.valueOf(task)+"_1"),new ShardingStatement(new ShardingConnection(null)))) ;
				 li.add(new StatementUnit(new SQLExecutionUnit("2",String.valueOf(task)+"_2"),new ShardingStatement(new ShardingConnection(null)))) ;
				 System.out.println(Thread.currentThread().getName());
				 engs.get(0).executeStatement(SQLType.DDL, li, new ExecuteCallback<String>() {
						@Override
						public String execute(BaseStatementUnit baseStatementUnit) throws Exception {
							
							System.out.println("执行线程任务"+baseStatementUnit.getSqlExecutionUnit().getSql());
							if(baseStatementUnit.getSqlExecutionUnit().getDataSource().equals("2")) {
								while(!success) {
									//模拟网络阻塞操作,这个时间可长可短,
								}
							}
							return null;
						}
				});
			 }
		 }.start();
	}
}

cwpgh avatar Jan 23 '18 03:01 cwpgh

ExecutorEngine是sharding jdbc内部使用的类,不提供外部服务。外围服务都在api包中。 我不确定这个issue是否有价值花费精力修改。 能否确定一下,在sharding jdbc的使用中,而不是外部自行使用,是否有问题?

terrymanu avatar Jan 24 '18 05:01 terrymanu

我知道这是内部类,我写这个只是模仿这个场景

 public void renew(final ShardingRule newShardingRule, final Properties newProps) throws SQLException {
        ShardingProperties newShardingProperties = new ShardingProperties(null == newProps ? new Properties() : newProps);
        int originalExecutorSize = shardingProperties.getValue(ShardingPropertiesConstant.EXECUTOR_SIZE);
        int newExecutorSize = newShardingProperties.getValue(ShardingPropertiesConstant.EXECUTOR_SIZE);
        if (originalExecutorSize != newExecutorSize) {
            executorEngine.close();
            executorEngine = new ExecutorEngine(newExecutorSize);
        }
        boolean newShowSQL = newShardingProperties.getValue(ShardingPropertiesConstant.SQL_SHOW);
        shardingProperties = newShardingProperties;
        shardingContext = new ShardingContext(newShardingRule, getDatabaseType(), executorEngine, newShowSQL);
    }

这是服务治理刷新配置的时候,有关闭线程池,但是关闭线程池没有平滑的处理

最终问题就是开头说的 线程池关闭的问题, 1.已经提交的任务怎么处理 2.关闭失败怎么处理 这都没有做,nginx重启

cwpgh avatar Jan 24 '18 09:01 cwpgh

可否提供pull request

terrymanu avatar Jan 24 '18 12:01 terrymanu

关于ExecutorEngine的close方法 建议先调用shutdown方法等待已经入队的任务执行完毕 然后调用awaitTermination方法等待一定时间 如果等待结束或出现异常调用shutdownNow并处理对shutdownNow返回的未执行的任务的处理 以上执行后还是没有关闭的抛出ShardingJdbcException(抛异常的同时还要创建线程用来关闭ExecutorEngine以及重新创建ExecutorEngine来处理后续任务)

其中等待时间和是否调用shutdownNow建议增加配置交给使用者来决定

maxiaoguang64 avatar Mar 05 '18 07:03 maxiaoguang64