View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Process engine.
37   */
38  public final class ProcessEngine {
39      
40      /**
41       * Connect.
42       *
43       * @param grantee grantee
44       * @param databaseName database name
45       * @return process ID
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       * Disconnect.
58       *
59       * @param processId process ID
60       */
61      public void disconnect(final String processId) {
62          ProcessRegistry.getInstance().remove(processId);
63      }
64      
65      /**
66       * Execute SQL.
67       *
68       * @param executionGroupContext execution group context
69       * @param queryContext query context
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       * Complete SQL unit execution.
79       * 
80       * @param executionUnit execution unit
81       * @param processId process ID
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       * Complete SQL execution.
97       * 
98       * @param processId process ID
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 }