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.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   * Process engine.
34   */
35  @HighFrequencyInvocation
36  public final class ProcessEngine {
37      
38      /**
39       * Connect.
40       *
41       * @param databaseName database name
42       * @return process ID
43       */
44      public String connect(final String databaseName) {
45          return connect(new ExecutionGroupReportContext(getProcessId(), databaseName));
46      }
47      
48      /**
49       * Connect.
50       *
51       * @param databaseName database name
52       * @param grantee grantee
53       * @return process ID
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       * Disconnect.
71       *
72       * @param processId process ID
73       */
74      public void disconnect(final String processId) {
75          ProcessRegistry.getInstance().remove(processId);
76      }
77      
78      /**
79       * Execute SQL.
80       *
81       * @param executionGroupContext execution group context
82       * @param queryContext query context
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       * Complete SQL unit execution.
90       *
91       * @param executionUnit execution unit
92       * @param processId process ID
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      * Complete SQL execution.
108      *
109      * @param processId process ID
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 }