Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BugFix]check if BE node is active #94 #104

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions src/main/java/com/starrocks/connector/spark/rest/RestService.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@

import java.io.IOException;
import java.io.Serializable;
import java.net.InetAddress;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -354,6 +356,32 @@ static QueryPlan getQueryPlan(String response, Logger logger) throws StarrocksEx
return queryPlan;
}

/**
* check if BE node is active
* @param candidate BE node, format: <be_address>:<thrift_port>
* @return BE node status, active is true
*/
@VisibleForTesting
static boolean isCandidateActive(String candidate) {
String[] split = candidate.split(":");
String host = split[0];
int port = Integer.parseInt(split[1]);
Socket socket = null;
try {
socket = new Socket(InetAddress.getByName(host), port);
return true;
} catch (Exception e) {
return false;
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
}
}
}
}

/**
* select which StarRocks BE to get tablet data.
*
Expand All @@ -364,6 +392,7 @@ static QueryPlan getQueryPlan(String response, Logger logger) throws StarrocksEx
*/
@VisibleForTesting
static Map<String, List<Long>> selectBeForTablet(QueryPlan queryPlan, Logger logger) throws StarrocksException {
Map<String, Boolean> candidatesStatus = new HashMap<>();
Map<String, List<Long>> be2Tablets = new HashMap<>();
for (Map.Entry<String, Tablet> part : queryPlan.getPartitions().entrySet()) {
logger.debug("Parse tablet info: '{}'.", part);
Expand All @@ -378,6 +407,14 @@ static Map<String, List<Long>> selectBeForTablet(QueryPlan queryPlan, Logger log
String target = null;
int tabletCount = Integer.MAX_VALUE;
for (String candidate : part.getValue().getRoutings()) {
// check if BE node is active and save the status
if (!candidatesStatus.containsKey(candidate)) {
candidatesStatus.put(candidate, isCandidateActive(candidate));
}
// check if BE node is active, if not, continue
if (Boolean.FALSE.equals(candidatesStatus.get(candidate))) {
continue;
}
logger.trace("Evaluate StarRocks BE '{}' to tablet '{}'.", candidate, tabletId);
if (!be2Tablets.containsKey(candidate)) {
logger.debug("Choice a new StarRocks BE '{}' for tablet '{}'.", candidate, tabletId);
Expand Down