[feature](CANCEL-ALTER-SYSTEM)decommission backend by ids (#25441)

Issue Number: close #23636
This commit is contained in:
Guangdong Liu
2023-10-25 19:49:38 +08:00
committed by GitHub
parent f31c1d858a
commit 3e21e4bdc2
5 changed files with 117 additions and 38 deletions

View File

@ -107,8 +107,8 @@ public class SystemHandler extends AlterHandler {
@Override
// add synchronized to avoid process 2 or more stmts at same time
public synchronized void process(String rawSql, List<AlterClause> alterClauses, String clusterName,
Database dummyDb,
OlapTable dummyTbl) throws UserException {
Database dummyDb,
OlapTable dummyTbl) throws UserException {
Preconditions.checkArgument(alterClauses.size() == 1);
AlterClause alterClause = alterClauses.get(0);
@ -263,31 +263,48 @@ public class SystemHandler extends AlterHandler {
CancelAlterSystemStmt cancelAlterSystemStmt = (CancelAlterSystemStmt) stmt;
SystemInfoService infoService = Env.getCurrentSystemInfo();
// check if backends is under decommission
List<Backend> backends = Lists.newArrayList();
List<HostInfo> hostInfos = cancelAlterSystemStmt.getHostInfos();
for (HostInfo hostInfo : hostInfos) {
// check if exist
Backend backend = infoService.getBackendWithHeartbeatPort(hostInfo.getHost(),
hostInfo.getPort());
if (backend == null) {
throw new DdlException("Backend does not exist["
+ NetUtils.getHostPortInAccessibleFormat(hostInfo.getHost(), hostInfo.getPort()) + "]");
if (hostInfos.isEmpty()) {
List<String> ids = cancelAlterSystemStmt.getIds();
for (String id : ids) {
Backend backend = infoService.getBackend(Long.parseLong(id));
if (backend == null) {
throw new DdlException("Backend does not exist["
+ id + "]");
}
if (!backend.isDecommissioned()) {
// it's ok. just log
LOG.info("backend is not decommissioned[{}]", backend.getId());
continue;
}
if (backend.setDecommissioned(false)) {
Env.getCurrentEnv().getEditLog().logBackendStateChange(backend);
} else {
LOG.info("backend is not decommissioned[{}]", backend.getHost());
}
}
if (!backend.isDecommissioned()) {
// it's ok. just log
LOG.info("backend is not decommissioned[{}]", backend.getId());
continue;
}
} else {
for (HostInfo hostInfo : hostInfos) {
// check if exist
Backend backend = infoService.getBackendWithHeartbeatPort(hostInfo.getHost(),
hostInfo.getPort());
if (backend == null) {
throw new DdlException("Backend does not exist["
+ NetUtils.getHostPortInAccessibleFormat(hostInfo.getHost(), hostInfo.getPort()) + "]");
}
backends.add(backend);
}
if (!backend.isDecommissioned()) {
// it's ok. just log
LOG.info("backend is not decommissioned[{}]", backend.getId());
continue;
}
for (Backend backend : backends) {
if (backend.setDecommissioned(false)) {
Env.getCurrentEnv().getEditLog().logBackendStateChange(backend);
} else {
LOG.info("backend is not decommissioned[{}]", backend.getHost());
if (backend.setDecommissioned(false)) {
Env.getCurrentEnv().getEditLog().logBackendStateChange(backend);
} else {
LOG.info("backend is not decommissioned[{}]", backend.getHost());
}
}
}
}

View File

@ -23,43 +23,62 @@ import org.apache.doris.system.SystemInfoService.HostInfo;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import lombok.Getter;
import java.util.List;
public class CancelAlterSystemStmt extends CancelStmt {
protected List<String> hostPorts;
private List<HostInfo> hostInfos;
protected List<String> params;
@Getter
private final List<HostInfo> hostInfos;
public CancelAlterSystemStmt(List<String> hostPorts) {
this.hostPorts = hostPorts;
@Getter
private final List<String> ids;
public CancelAlterSystemStmt(List<String> params) {
this.params = params;
this.hostInfos = Lists.newArrayList();
}
public List<HostInfo> getHostInfos() {
return hostInfos;
this.ids = Lists.newArrayList();
}
@Override
public void analyze(Analyzer analyzer) throws AnalysisException {
for (String hostPort : hostPorts) {
HostInfo hostInfo = SystemInfoService.getHostAndPort(hostPort);
this.hostInfos.add(hostInfo);
for (String param : params) {
if (!param.contains(":")) {
ids.add(param);
} else {
HostInfo hostInfo = SystemInfoService.getHostAndPort(param);
this.hostInfos.add(hostInfo);
}
}
Preconditions.checkState(!this.hostInfos.isEmpty());
Preconditions.checkState(!this.hostInfos.isEmpty() || !this.ids.isEmpty(),
"hostInfos or ids can not be empty");
}
@Override
public String toSql() {
StringBuilder sb = new StringBuilder();
sb.append("CANCEL DECOMMISSION BACKEND ");
for (int i = 0; i < hostPorts.size(); i++) {
sb.append("\"").append(hostPorts.get(i)).append("\"");
if (i != hostPorts.size() - 1) {
sb.append(", ");
if (!ids.isEmpty()) {
for (int i = 0; i < hostInfos.size(); i++) {
sb.append("\"").append(hostInfos.get(i)).append("\"");
if (i != hostInfos.size() - 1) {
sb.append(", ");
}
}
} else {
for (int i = 0; i < params.size(); i++) {
sb.append("\"").append(params.get(i)).append("\"");
if (i != params.size() - 1) {
sb.append(", ");
}
}
}
return sb.toString();
}
}