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.proxy.backend.mysql.handler.admin.executor;
19  
20  import com.google.common.eventbus.Subscribe;
21  import lombok.Getter;
22  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResult;
23  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.QueryResultMetaData;
24  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.raw.metadata.RawQueryResultColumnMetaData;
25  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.raw.metadata.RawQueryResultMetaData;
26  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.impl.raw.type.RawMemoryQueryResult;
27  import org.apache.shardingsphere.infra.executor.sql.execute.result.query.type.memory.row.MemoryQueryResultDataRow;
28  import org.apache.shardingsphere.infra.executor.sql.process.Process;
29  import org.apache.shardingsphere.infra.merge.result.MergedResult;
30  import org.apache.shardingsphere.infra.merge.result.impl.transparent.TransparentMergedResult;
31  import org.apache.shardingsphere.infra.util.eventbus.EventSubscriber;
32  import org.apache.shardingsphere.mode.process.event.ShowProcessListRequestEvent;
33  import org.apache.shardingsphere.mode.process.event.ShowProcessListResponseEvent;
34  import org.apache.shardingsphere.proxy.backend.context.ProxyContext;
35  import org.apache.shardingsphere.proxy.backend.handler.admin.executor.DatabaseAdminQueryExecutor;
36  import org.apache.shardingsphere.proxy.backend.session.ConnectionSession;
37  
38  import java.sql.Types;
39  import java.util.ArrayList;
40  import java.util.Collection;
41  import java.util.Collections;
42  import java.util.List;
43  import java.util.concurrent.TimeUnit;
44  import java.util.stream.Collectors;
45  
46  /**
47   * Show process list executor.
48   */
49  public final class ShowProcessListExecutor implements DatabaseAdminQueryExecutor, EventSubscriber {
50      
51      private final boolean showFullProcesslist;
52      
53      private Collection<Process> processes;
54      
55      @Getter
56      private QueryResultMetaData queryResultMetaData;
57      
58      @Getter
59      private MergedResult mergedResult;
60      
61      public ShowProcessListExecutor(final boolean showFullProcesslist) {
62          this.showFullProcesslist = showFullProcesslist;
63          ProxyContext.getInstance().getContextManager().getInstanceContext().getEventBusContext().register(this);
64      }
65      
66      /**
67       * Receive and handle response event.
68       *
69       * @param event show process list response event
70       */
71      @SuppressWarnings("unused")
72      @Subscribe
73      public void receiveProcessListData(final ShowProcessListResponseEvent event) {
74          processes = event.getProcesses();
75      }
76      
77      @Override
78      public void execute(final ConnectionSession connectionSession) {
79          queryResultMetaData = createQueryResultMetaData();
80          mergedResult = new TransparentMergedResult(getQueryResult());
81      }
82      
83      private QueryResult getQueryResult() {
84          ProxyContext.getInstance().getContextManager().getInstanceContext().getEventBusContext().post(new ShowProcessListRequestEvent());
85          if (null == processes || processes.isEmpty()) {
86              return new RawMemoryQueryResult(queryResultMetaData, Collections.emptyList());
87          }
88          List<MemoryQueryResultDataRow> rows = processes.stream().map(this::getMemoryQueryResultDataRow).collect(Collectors.toList());
89          return new RawMemoryQueryResult(queryResultMetaData, rows);
90      }
91      
92      private MemoryQueryResultDataRow getMemoryQueryResultDataRow(final Process process) {
93          List<Object> rowValues = new ArrayList<>(8);
94          rowValues.add(process.getId());
95          rowValues.add(process.getUsername());
96          rowValues.add(process.getHostname());
97          rowValues.add(process.getDatabaseName());
98          rowValues.add(process.isIdle() ? "Sleep" : "Execute");
99          rowValues.add(TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis() - process.getStartMillis()));
100         String sql = null;
101         if (process.isIdle()) {
102             rowValues.add("");
103         } else {
104             int processDoneCount = process.getCompletedUnitCount().get();
105             String statePrefix = "Executing ";
106             rowValues.add(statePrefix + processDoneCount + "/" + process.getTotalUnitCount().get());
107             sql = process.getSql();
108         }
109         if (null != sql && sql.length() > 100 && !showFullProcesslist) {
110             sql = sql.substring(0, 100);
111         }
112         rowValues.add(null != sql ? sql : "");
113         return new MemoryQueryResultDataRow(rowValues);
114     }
115     
116     private QueryResultMetaData createQueryResultMetaData() {
117         List<RawQueryResultColumnMetaData> columns = new ArrayList<>(8);
118         columns.add(new RawQueryResultColumnMetaData("", "Id", "Id", Types.VARCHAR, "VARCHAR", 20, 0));
119         columns.add(new RawQueryResultColumnMetaData("", "User", "User", Types.VARCHAR, "VARCHAR", 20, 0));
120         columns.add(new RawQueryResultColumnMetaData("", "Host", "Host", Types.VARCHAR, "VARCHAR", 64, 0));
121         columns.add(new RawQueryResultColumnMetaData("", "db", "db", Types.VARCHAR, "VARCHAR", 64, 0));
122         columns.add(new RawQueryResultColumnMetaData("", "Command", "Command", Types.VARCHAR, "VARCHAR", 64, 0));
123         columns.add(new RawQueryResultColumnMetaData("", "Time", "Time", Types.VARCHAR, "VARCHAR", 10, 0));
124         columns.add(new RawQueryResultColumnMetaData("", "State", "State", Types.VARCHAR, "VARCHAR", 64, 0));
125         columns.add(new RawQueryResultColumnMetaData("", "Info", "Info", Types.VARCHAR, "VARCHAR", 120, 0));
126         return new RawQueryResultMetaData(columns);
127     }
128 }