1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.shardingsphere.infra.executor.sql.process;
19
20 import com.google.common.base.Strings;
21 import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupContext;
22 import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupReportContext;
23 import org.apache.shardingsphere.infra.executor.sql.execute.engine.SQLExecutionUnit;
24 import org.apache.shardingsphere.infra.metadata.user.Grantee;
25 import org.apache.shardingsphere.infra.session.query.QueryContext;
26 import org.apache.shardingsphere.sql.parser.sql.common.statement.SQLStatement;
27 import org.apache.shardingsphere.sql.parser.sql.common.statement.ddl.DDLStatement;
28 import org.apache.shardingsphere.sql.parser.sql.common.statement.dml.DMLStatement;
29 import org.apache.shardingsphere.sql.parser.sql.dialect.statement.mysql.MySQLStatement;
30
31 import java.util.Collections;
32 import java.util.UUID;
33 import java.util.concurrent.ThreadLocalRandom;
34
35
36
37
38 public final class ProcessEngine {
39
40
41
42
43
44
45
46
47 public String connect(final Grantee grantee, final String databaseName) {
48 String processId = new UUID(ThreadLocalRandom.current().nextLong(), ThreadLocalRandom.current().nextLong()).toString().replace("-", "");
49 ExecutionGroupContext<? extends SQLExecutionUnit> executionGroupContext =
50 new ExecutionGroupContext<>(Collections.emptyList(), new ExecutionGroupReportContext(processId, databaseName, grantee));
51 Process process = new Process(executionGroupContext);
52 ProcessRegistry.getInstance().add(process);
53 return executionGroupContext.getReportContext().getProcessId();
54 }
55
56
57
58
59
60
61 public void disconnect(final String processId) {
62 ProcessRegistry.getInstance().remove(processId);
63 }
64
65
66
67
68
69
70
71 public void executeSQL(final ExecutionGroupContext<? extends SQLExecutionUnit> executionGroupContext, final QueryContext queryContext) {
72 if (isMySQLDDLOrDMLStatement(queryContext.getSqlStatementContext().getSqlStatement())) {
73 ProcessRegistry.getInstance().add(new Process(queryContext.getSql(), executionGroupContext));
74 }
75 }
76
77
78
79
80
81
82
83 public void completeSQLUnitExecution(final SQLExecutionUnit executionUnit, final String processId) {
84 if (Strings.isNullOrEmpty(processId)) {
85 return;
86 }
87 Process process = ProcessRegistry.getInstance().get(processId);
88 if (null == process) {
89 return;
90 }
91 process.completeExecutionUnit();
92 process.removeProcessStatement(executionUnit.getExecutionUnit());
93 }
94
95
96
97
98
99
100 public void completeSQLExecution(final String processId) {
101 if (Strings.isNullOrEmpty(processId)) {
102 return;
103 }
104 Process process = ProcessRegistry.getInstance().get(processId);
105 if (null == process) {
106 return;
107 }
108 ExecutionGroupContext<? extends SQLExecutionUnit> executionGroupContext = new ExecutionGroupContext<>(
109 Collections.emptyList(), new ExecutionGroupReportContext(processId, process.getDatabaseName(), new Grantee(process.getUsername(), process.getHostname())));
110 ProcessRegistry.getInstance().add(new Process(executionGroupContext));
111 }
112
113 private boolean isMySQLDDLOrDMLStatement(final SQLStatement sqlStatement) {
114 return sqlStatement instanceof MySQLStatement && (sqlStatement instanceof DDLStatement || sqlStatement instanceof DMLStatement);
115 }
116 }