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.annotation.HighFrequencyInvocation;
22 import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupContext;
23 import org.apache.shardingsphere.infra.executor.kernel.model.ExecutionGroupReportContext;
24 import org.apache.shardingsphere.infra.executor.sql.execute.engine.SQLExecutionUnit;
25 import org.apache.shardingsphere.infra.metadata.user.Grantee;
26 import org.apache.shardingsphere.infra.session.query.QueryContext;
27
28 import java.util.Collections;
29 import java.util.UUID;
30 import java.util.concurrent.ThreadLocalRandom;
31
32
33
34
35 @HighFrequencyInvocation
36 public final class ProcessEngine {
37
38
39
40
41
42
43
44 public String connect(final String databaseName) {
45 return connect(new ExecutionGroupReportContext(getProcessId(), databaseName));
46 }
47
48
49
50
51
52
53
54
55 public String connect(final String databaseName, final Grantee grantee) {
56 return connect(new ExecutionGroupReportContext(getProcessId(), databaseName, grantee));
57 }
58
59 private String connect(final ExecutionGroupReportContext reportContext) {
60 ExecutionGroupContext<? extends SQLExecutionUnit> executionGroupContext = new ExecutionGroupContext<>(Collections.emptyList(), reportContext);
61 ProcessRegistry.getInstance().add(new Process(executionGroupContext));
62 return executionGroupContext.getReportContext().getProcessId();
63 }
64
65 private String getProcessId() {
66 return new UUID(ThreadLocalRandom.current().nextLong(), ThreadLocalRandom.current().nextLong()).toString().replace("-", "");
67 }
68
69
70
71
72
73
74 public void disconnect(final String processId) {
75 ProcessRegistry.getInstance().remove(processId);
76 }
77
78
79
80
81
82
83
84 public void executeSQL(final ExecutionGroupContext<? extends SQLExecutionUnit> executionGroupContext, final QueryContext queryContext) {
85 ProcessRegistry.getInstance().add(new Process(queryContext.getSql(), executionGroupContext));
86 }
87
88
89
90
91
92
93
94 public void completeSQLUnitExecution(final SQLExecutionUnit executionUnit, final String processId) {
95 if (Strings.isNullOrEmpty(processId)) {
96 return;
97 }
98 Process process = ProcessRegistry.getInstance().get(processId);
99 if (null == process) {
100 return;
101 }
102 process.completeExecutionUnit();
103 process.removeProcessStatement(executionUnit.getExecutionUnit());
104 }
105
106
107
108
109
110
111 public void completeSQLExecution(final String processId) {
112 if (Strings.isNullOrEmpty(processId)) {
113 return;
114 }
115 Process process = ProcessRegistry.getInstance().get(processId);
116 if (null == process) {
117 return;
118 }
119 ExecutionGroupContext<? extends SQLExecutionUnit> executionGroupContext = new ExecutionGroupContext<>(
120 Collections.emptyList(), new ExecutionGroupReportContext(processId, process.getDatabaseName(), new Grantee(process.getUsername(), process.getHostname())));
121 ProcessRegistry.getInstance().add(new Process(executionGroupContext));
122 }
123 }