Skip to content

Commit

Permalink
[enchement](api)Change the behavior of follower http api redirect to …
Browse files Browse the repository at this point in the history
…master to follower request master.
  • Loading branch information
hubgeter committed Dec 25, 2024
1 parent cd42ec1 commit 9d2a2fb
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.RedirectView;

import java.lang.reflect.Type;
import java.util.List;
Expand Down Expand Up @@ -86,16 +85,19 @@ private static GroupId checkAndGetGroupId(HttpServletRequest request) throws Ddl
public Object executeWithoutPassword(HttpServletRequest request, HttpServletResponse response)
throws Exception {
executeCheckPassword(request, response);
RedirectView redirectView = redirectToMasterOrException(request, response);
if (redirectView != null) {
return redirectView;
}
checkGlobalAuth(ConnectContext.get().getCurrentUserIdentity(), PrivPredicate.ADMIN);
return null;
}

@RequestMapping(path = "/api/colocate", method = RequestMethod.GET)
public Object colocate(HttpServletRequest request, HttpServletResponse response) throws Exception {
if (needRedirect(request.getScheme())) {
return redirectToHttps(request);
}

if (!Env.getCurrentEnv().isMaster()) {
return forwardToMaster(request);
}
executeWithoutPassword(request, response);
return ResponseEntityBuilder.ok(Env.getCurrentColocateIndex());
}
Expand All @@ -107,6 +109,10 @@ public Object group_stable(HttpServletRequest request, HttpServletResponse respo
return redirectToHttps(request);
}

if (!Env.getCurrentEnv().isMaster()) {
return forwardToMaster(request);
}

executeWithoutPassword(request, response);
GroupId groupId = checkAndGetGroupId(request);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,8 @@ public Object execute(@PathVariable(value = DB_KEY) final String dbName,
}

executeCheckPassword(request, response);
Object redirectView = redirectToMaster(request, response);
if (redirectView != null) {
return redirectView;
if (!Env.getCurrentEnv().isMaster()) {
return forwardToMaster(request);
}

if (Strings.isNullOrEmpty(dbName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ public Object execute(
if (Strings.isNullOrEmpty(info.label)) {
return new RestBaseResult("No label selected");
}

Object redirectView = redirectToMaster(request, response);
if (redirectView != null) {
return redirectView;
if (!Env.getServingEnv().isMaster()) {
return forwardToMaster(request);
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ public Object execute(@PathVariable(value = DB_KEY) final String dbName,
HttpServletRequest request, HttpServletResponse response) {
executeCheckPassword(request, response);

Object redirectView = redirectToMaster(request, response);
if (redirectView != null) {
return redirectView;
if (!Env.getCurrentEnv().isMaster()) {
return forwardToMaster(request);
}

String label = request.getParameter(LABEL_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,8 @@ private Object executeWithoutPassword(HttpServletRequest request,
if (!isStreamLoad && !Strings.isNullOrEmpty(request.getParameter(SUB_LABEL_NAME_PARAM))) {
// only multi mini load need to redirect to Master, because only Master has the info of table to
// the Backend which the file exists.
Object redirectView = redirectToMaster(request, response);
if (redirectView != null) {
return redirectView;
if (!Env.getCurrentEnv().isMaster()) {
return forwardToMaster(request);
}
try {
redirectAddr = execEnv.getMultiLoadMgr().redirectAddr(fullDbName, label);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.doris.httpv2.rest;

import org.apache.doris.analysis.LoadStmt;
import org.apache.doris.catalog.Env;
import org.apache.doris.common.DdlException;
import org.apache.doris.httpv2.entity.RestBaseResult;
import org.apache.doris.mysql.privilege.PrivPredicate;
Expand Down Expand Up @@ -66,9 +67,8 @@ public Object multi_desc(
checkDbAuth(ConnectContext.get().getCurrentUserIdentity(), fullDbName, PrivPredicate.LOAD);

// only Master has these load info
Object redirectView = redirectToMaster(request, response);
if (redirectView != null) {
return redirectView;
if (!Env.getCurrentEnv().isMaster()) {
return forwardToMaster(request);
}

final List<String> labels = Lists.newArrayList();
Expand All @@ -95,9 +95,8 @@ public Object multi_list(
checkDbAuth(ConnectContext.get().getCurrentUserIdentity(), fullDbName, PrivPredicate.LOAD);

// only Master has these load info
Object redirectView = redirectToMaster(request, response);
if (redirectView != null) {
return redirectView;
if (!Env.getCurrentEnv().isMaster()) {
return forwardToMaster(request);
}

final List<String> labels = Lists.newArrayList();
Expand Down Expand Up @@ -129,10 +128,8 @@ public Object multi_start(

// Multi start request must redirect to master, because all following sub requests will be handled
// on Master

Object redirectView = redirectToMaster(request, response);
if (redirectView != null) {
return redirectView;
if (!Env.getCurrentEnv().isMaster()) {
return forwardToMaster(request);
}

Map<String, String> properties = Maps.newHashMap();
Expand Down Expand Up @@ -180,9 +177,8 @@ public Object multi_unload(
String fullDbName = getFullDbName(dbName);
checkDbAuth(ConnectContext.get().getCurrentUserIdentity(), fullDbName, PrivPredicate.LOAD);

Object redirectView = redirectToMaster(request, response);
if (redirectView != null) {
return redirectView;
if (!Env.getCurrentEnv().isMaster()) {
return forwardToMaster(request);
}

ExecuteEnv.getInstance().getMultiLoadMgr().unload(fullDbName, label, subLabel);
Expand Down Expand Up @@ -213,11 +209,10 @@ public Object multi_commit(
checkDbAuth(ConnectContext.get().getCurrentUserIdentity(), fullDbName, PrivPredicate.LOAD);

// only Master has these load info

Object redirectView = redirectToMaster(request, response);
if (redirectView != null) {
return redirectView;
if (!Env.getCurrentEnv().isMaster()) {
return forwardToMaster(request);
}

try {
ExecuteEnv.getInstance().getMultiLoadMgr().commit(fullDbName, label);
} catch (Exception e) {
Expand Down Expand Up @@ -250,9 +245,8 @@ public Object multi_abort(
checkDbAuth(ConnectContext.get().getCurrentUserIdentity(), fullDbName, PrivPredicate.LOAD);

// only Master has these load info
Object redirectView = redirectToMaster(request, response);
if (redirectView != null) {
return redirectView;
if (!Env.getCurrentEnv().isMaster()) {
return forwardToMaster(request);
}

ExecuteEnv.getInstance().getMultiLoadMgr().abort(fullDbName, label);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,28 @@

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import jline.internal.Nullable;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.view.RedirectView;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Collections;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

Expand Down Expand Up @@ -73,6 +82,13 @@ public ActionAuthorizationInfo executeCheckPassword(HttpServletRequest request,
}

public RedirectView redirectTo(HttpServletRequest request, TNetworkAddress addr) {
RedirectView redirectView = new RedirectView(getRedirectUrL(request, addr));
redirectView.setContentType("text/html;charset=utf-8");
redirectView.setStatusCode(org.springframework.http.HttpStatus.TEMPORARY_REDIRECT);
return redirectView;
}

public String getRedirectUrL(HttpServletRequest request, TNetworkAddress addr) {
URI urlObj = null;
URI resultUriObj = null;
String urlStr = request.getRequestURI();
Expand All @@ -84,7 +100,7 @@ public RedirectView redirectTo(HttpServletRequest request, TNetworkAddress addr)
}
try {
urlObj = new URI(urlStr);
resultUriObj = new URI("http", userInfo, addr.getHostname(),
resultUriObj = new URI(request.getScheme(), userInfo, addr.getHostname(),
addr.getPort(), urlObj.getPath(), "", null);
} catch (Exception e) {
throw new RuntimeException(e);
Expand All @@ -93,12 +109,9 @@ public RedirectView redirectTo(HttpServletRequest request, TNetworkAddress addr)
if (!Strings.isNullOrEmpty(request.getQueryString())) {
redirectUrl += request.getQueryString();
}
LOG.info("Redirect url: {}", "http://" + addr.getHostname() + ":"
+ addr.getPort() + urlObj.getPath());
RedirectView redirectView = new RedirectView(redirectUrl);
redirectView.setContentType("text/html;charset=utf-8");
redirectView.setStatusCode(org.springframework.http.HttpStatus.TEMPORARY_REDIRECT);
return redirectView;
LOG.info("Redirect url: {}", request.getScheme() + "://" + addr.getHostname() + ":"
+ addr.getPort() + urlObj.getPath());
return redirectUrl;
}

public RedirectView redirectToObj(String sign) throws URISyntaxException {
Expand Down Expand Up @@ -197,4 +210,59 @@ public Object redirectToHttps(HttpServletRequest request) {
redirectView.setStatusCode(HttpStatus.TEMPORARY_REDIRECT);
return redirectView;
}

public Object forwardToMaster(HttpServletRequest request) {
try {
return forwardToMaster(request, (Object) getRequestBody(request));
} catch (Exception e) {
LOG.warn(e);
return ResponseEntityBuilder.okWithCommonError(e.getMessage());
}
}

private String getRequestBody(HttpServletRequest request) throws IOException {
BufferedReader reader = request.getReader();
return reader.lines().collect(Collectors.joining(System.lineSeparator()));
}

public Object forwardToMaster(HttpServletRequest request, @Nullable Object body) {
try {
Env env = Env.getCurrentEnv();
String redirectUrl = getRedirectUrL(request,
new TNetworkAddress(env.getMasterHost(), env.getMasterHttpPort()));
String method = request.getMethod();

HttpHeaders headers = new HttpHeaders();
for (String headerName : Collections.list(request.getHeaderNames())) {
headers.add(headerName, request.getHeader(headerName));
}

HttpEntity<Object> entity = new HttpEntity<>(body, headers);

RestTemplate restTemplate = new RestTemplate();

ResponseEntity<Object> responseEntity;
switch (method) {
case "GET":
responseEntity = restTemplate.exchange(redirectUrl, HttpMethod.GET, entity, Object.class);
break;
case "POST":
responseEntity = restTemplate.exchange(redirectUrl, HttpMethod.POST, entity, Object.class);
break;
case "PUT":
responseEntity = restTemplate.exchange(redirectUrl, HttpMethod.PUT, entity, Object.class);
break;
case "DELETE":
responseEntity = restTemplate.exchange(redirectUrl, HttpMethod.DELETE, entity, Object.class);
break;
default:
throw new UnsupportedOperationException("Unsupported HTTP method: " + method);
}

return responseEntity.getBody();
} catch (Exception e) {
LOG.warn(e);
return ResponseEntityBuilder.okWithCommonError(e.getMessage());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import org.apache.doris.persist.Storage;
import org.apache.doris.qe.ConnectContext;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import org.apache.commons.lang3.StringUtils;
Expand All @@ -46,7 +45,6 @@
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.view.RedirectView;

import java.io.IOException;
import java.net.InetSocketAddress;
Expand Down Expand Up @@ -118,13 +116,7 @@ public Object show_proc(HttpServletRequest request, HttpServletResponse response

// forward to master if necessary
if (!Env.getCurrentEnv().isMaster() && isForward) {
try {
RedirectView redirectView = redirectToMasterOrException(request, response);
Preconditions.checkNotNull(redirectView);
return redirectView;
} catch (Exception e) {
return ResponseEntityBuilder.okWithCommonError(e.getMessage());
}
return forwardToMaster(request);
} else {
ProcNodeInterface procNode = null;
ProcService instance = ProcService.getInstance();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ private Object fetchNodeInfo(HttpServletRequest request, HttpServletResponse res
throws Exception {
try {
if (!Env.getCurrentEnv().isMaster()) {
return redirectToMasterOrException(request, response);
return forwardToMaster(request, null);
}

ProcResult procResult = ProcService.getInstance().open(procPath).fetchResult();
Expand Down Expand Up @@ -605,7 +605,7 @@ public Object operateBackend(HttpServletRequest request, HttpServletResponse res
@RequestBody BackendReqInfo reqInfo) {
try {
if (!Env.getCurrentEnv().isMaster()) {
return redirectToMasterOrException(request, response);
return forwardToMaster(request, reqInfo);
}

List<String> hostPorts = reqInfo.getHostPorts();
Expand Down Expand Up @@ -648,7 +648,7 @@ public Object operateFrontends(HttpServletRequest request, HttpServletResponse r
@PathVariable String action, @RequestBody FrontendReqInfo reqInfo) {
try {
if (!Env.getCurrentEnv().isMaster()) {
return redirectToMasterOrException(request, response);
return forwardToMaster(request, reqInfo);
}

String role = reqInfo.getRole();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Object clusterOverview(@RequestBody StoragePolicyVo body,

try {
if (!Env.getCurrentEnv().isMaster()) {
return redirectToMasterOrException(request, response);
return forwardToMaster(request, body);
}
} catch (Exception e) {
return ResponseEntityBuilder.okWithCommonError(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,8 @@ private Object handleRequest(HttpServletRequest request, HttpServletResponse res
executeCheckPassword(request, response);
}

try {
if (!Env.getCurrentEnv().isMaster()) {
return redirectToMasterOrException(request, response);
}
} catch (Exception e) {
return ResponseEntityBuilder.okWithCommonError(e.getMessage());
if (!Env.getCurrentEnv().isMaster()) {
return forwardToMaster(request);
}

Map<String, Object> resultMap = Maps.newHashMap();
Expand Down
Loading

0 comments on commit 9d2a2fb

Please sign in to comment.