forked from StarRocks/starrocks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
ShowProcAction.java
134 lines (117 loc) · 5.36 KB
/
ShowProcAction.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// This file is made available under Elastic License 2.0.
// This file is based on code available under the Apache license here:
// https://github.com/apache/incubator-doris/blob/master/fe/fe-core/src/main/java/org/apache/doris/http/rest/ShowProcAction.java
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package com.starrocks.http.rest;
import com.google.common.base.Strings;
import com.google.gson.Gson;
import com.starrocks.analysis.RedirectStatus;
import com.starrocks.common.AnalysisException;
import com.starrocks.common.DdlException;
import com.starrocks.common.proc.ProcNodeInterface;
import com.starrocks.common.proc.ProcResult;
import com.starrocks.common.proc.ProcService;
import com.starrocks.http.ActionController;
import com.starrocks.http.BaseRequest;
import com.starrocks.http.BaseResponse;
import com.starrocks.http.IllegalArgException;
import com.starrocks.mysql.privilege.PrivPredicate;
import com.starrocks.qe.ConnectContext;
import com.starrocks.qe.LeaderOpExecutor;
import com.starrocks.qe.OriginStatement;
import com.starrocks.qe.ShowResultSet;
import com.starrocks.server.GlobalStateMgr;
import io.netty.handler.codec.http.HttpMethod;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.List;
// Format:
// http://username:password@192.168.1.1:8030/api/show_proc?path=/
public class ShowProcAction extends RestBaseAction {
private static final Logger LOG = LogManager.getLogger(ShowProcAction.class);
public ShowProcAction(ActionController controller) {
super(controller);
}
public static void registerAction(ActionController controller) throws IllegalArgException {
controller.registerHandler(HttpMethod.GET, "/api/show_proc", new ShowProcAction(controller));
}
@Override
public void executeWithoutPassword(BaseRequest request, BaseResponse response) throws DdlException {
// check authority
checkGlobalAuth(ConnectContext.get().getCurrentUserIdentity(), PrivPredicate.ADMIN);
String path = request.getSingleParameter("path");
String forward = request.getSingleParameter("forward");
boolean isForward = false;
if (!Strings.isNullOrEmpty(forward) && forward.equals("true")) {
isForward = true;
}
// forward to master if necessary
if (!GlobalStateMgr.getCurrentState().isLeader() && isForward) {
String showProcStmt = "SHOW PROC \"" + path + "\"";
// ConnectContext build in RestBaseAction
ConnectContext context = ConnectContext.get();
LeaderOpExecutor leaderOpExecutor = new LeaderOpExecutor(new OriginStatement(showProcStmt, 0), context,
RedirectStatus.FORWARD_NO_SYNC);
LOG.debug("need to transfer to Leader. stmt: {}", context.getStmtId());
try {
leaderOpExecutor.execute();
} catch (Exception e) {
LOG.warn("failed to forward stmt", e);
response.appendContent("Failed to forward stmt: " + e.getMessage());
sendResult(request, response);
return;
}
ShowResultSet resultSet = leaderOpExecutor.getProxyResultSet();
if (resultSet == null) {
response.appendContent("Failed to get result set");
sendResult(request, response);
return;
}
Gson gson = new Gson();
response.setContentType("application/json");
response.getContent().append(gson.toJson(resultSet.getResultRows()));
} else {
ProcNodeInterface procNode = null;
ProcService instance = ProcService.getInstance();
try {
if (Strings.isNullOrEmpty(path)) {
procNode = instance.open("/");
} else {
procNode = instance.open(path);
}
} catch (AnalysisException e) {
LOG.warn(e.getMessage());
response.getContent().append("[]");
}
if (procNode != null) {
ProcResult result;
try {
result = procNode.fetchResult();
List<List<String>> rows = result.getRows();
Gson gson = new Gson();
response.setContentType("application/json");
response.getContent().append(gson.toJson(rows));
} catch (AnalysisException e) {
LOG.warn(e.getMessage());
response.getContent().append("[]");
}
}
}
sendResult(request, response);
}
}