Skip to content

Commit

Permalink
[alibaba#5350] In version 1.4.1, modify the Server is Down prompt to …
Browse files Browse the repository at this point in the history
…the specific reason for the machine failure and send it to the user
  • Loading branch information
realJackSun committed Apr 13, 2021
1 parent e689c1a commit a9ca62c
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.Optional;

/**
* Detect and control the working status of local server.
Expand Down Expand Up @@ -68,6 +69,10 @@ public ServerStatus getServerStatus() {
return serverStatus;
}

public Optional<String> getErrorMsg() {
return consistencyService.getErrorMsg();
}

public class ServerStatusUpdater implements Runnable {

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.naming.pojo.Record;

import java.util.Optional;

/**
* Consistence service for all implementations to derive.
*
Expand Down Expand Up @@ -77,6 +79,13 @@ public interface ConsistencyService {
*/
void unListen(String key, RecordListener listener) throws NacosException;

/**
* Get the error message of the consistency protocol.
*
* @return the consistency protocol error message.
*/
Optional<String> getErrorMsg();

/**
* Tell the status of this consistency service.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import org.springframework.context.annotation.DependsOn;
import org.springframework.stereotype.Service;

import java.util.Optional;

/**
* Consistency delegate.
*
Expand Down Expand Up @@ -81,6 +83,25 @@ public boolean isAvailable() {
return ephemeralConsistencyService.isAvailable() && persistentConsistencyService.isAvailable();
}

@Override
public Optional<String> getErrorMsg() {
String errorMsg;
if (ephemeralConsistencyService.getErrorMsg().isPresent()
&& persistentConsistencyService.getErrorMsg().isPresent()) {
errorMsg = "'" + ephemeralConsistencyService.getErrorMsg().get() + "' in Distro protocol and '"
+ persistentConsistencyService.getErrorMsg().get() + "' in jRaft protocol";
} else if (ephemeralConsistencyService.getErrorMsg().isPresent()
&& !persistentConsistencyService.getErrorMsg().isPresent()) {
errorMsg = ephemeralConsistencyService.getErrorMsg().get();
} else if (!ephemeralConsistencyService.getErrorMsg().isPresent()
&& persistentConsistencyService.getErrorMsg().isPresent()) {
errorMsg = persistentConsistencyService.getErrorMsg().get();
} else {
errorMsg = null;
}
return Optional.ofNullable(errorMsg);
}

private ConsistencyService mapConsistencyService(String key) {
return KeyBuilder.matchEphemeralKey(key) ? ephemeralConsistencyService : persistentConsistencyService;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
Expand Down Expand Up @@ -350,6 +351,17 @@ public boolean isAvailable() {
return isInitialized() || ServerStatus.UP.name().equals(switchDomain.getOverriddenServerStatus());
}

@Override
public Optional<String> getErrorMsg() {
String errorMsg;
if (!isInitialized() && !ServerStatus.UP.name().equals(switchDomain.getOverriddenServerStatus())) {
errorMsg = "Distro protocol is not initialized";
} else {
errorMsg = null;
}
return Optional.ofNullable(errorMsg);
}

public boolean isInitialized() {
return distroProtocol.isInitialized() || !globalConfig.isDataWarmup();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import com.alibaba.nacos.sys.env.EnvUtil;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
* Persistent consistency service delegate.
*
Expand Down Expand Up @@ -89,6 +91,11 @@ public boolean isAvailable() {
return switchOne().isAvailable();
}

@Override
public Optional<String> getErrorMsg() {
return switchOne().getErrorMsg();
}

private PersistentConsistencyService switchOne() {
return switchNewPersistentService ? newPersistentConsistencyService : oldPersistentConsistencyService;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,8 @@ enum Op {
*/
protected volatile boolean hasError = false;

protected volatile String jRaftErrorMsg;

/**
* If use old raft, should not notify listener even new listener add.
*/
Expand Down Expand Up @@ -211,6 +213,7 @@ public List<SnapshotOperation> loadSnapshotOperate() {
public void onError(Throwable error) {
super.onError(error);
hasError = true;
jRaftErrorMsg = error.getMessage();
}

protected Type getDatumTypeFromKey(String key) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.TimeUnit;

/**
Expand Down Expand Up @@ -158,4 +159,19 @@ public void unListen(String key, RecordListener listener) throws NacosException
public boolean isAvailable() {
return hasLeader && !hasError;
}

@Override
public Optional<String> getErrorMsg() {
String errorMsg;
if (hasLeader && hasError) {
errorMsg = "The raft peer is in error: " + jRaftErrorMsg;
} else if (hasLeader && !hasError) {
errorMsg = null;
} else if (!hasLeader && hasError) {
errorMsg = "Could not find leader! And the raft peer is in error: " + jRaftErrorMsg;
} else {
errorMsg = "Could not find leader!";
}
return Optional.ofNullable(errorMsg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
* Persistent service manipulation layer in stand-alone mode.
Expand Down Expand Up @@ -107,4 +108,15 @@ public void unListen(String key, RecordListener listener) throws NacosException
public boolean isAvailable() {
return !hasError;
}

@Override
public Optional<String> getErrorMsg() {
String errorMsg;
if (hasError) {
errorMsg = "The raft peer is in error: " + jRaftErrorMsg;
} else {
errorMsg = null;
}
return Optional.ofNullable(errorMsg);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import java.util.Optional;

/**
* Use simplified Raft protocol to maintain the consistency status of Nacos cluster.
Expand All @@ -54,6 +55,8 @@ public class RaftConsistencyServiceImpl implements PersistentConsistencyService

private volatile boolean stopWork = false;

private String errorMsg;

public RaftConsistencyServiceImpl(ClusterVersionJudgement versionJudgement, RaftCore raftCore,
SwitchDomain switchDomain) {
this.raftCore = raftCore;
Expand Down Expand Up @@ -127,6 +130,17 @@ public boolean isAvailable() {
return raftCore.isInitialized() || ServerStatus.UP.name().equals(switchDomain.getOverriddenServerStatus());
}

@Override
public Optional<String> getErrorMsg() {
String errorMsg;
if (!raftCore.isInitialized() && !ServerStatus.UP.name().equals(switchDomain.getOverriddenServerStatus())) {
errorMsg = "The old raft protocol node is not initialized";
} else {
errorMsg = null;
}
return Optional.ofNullable(errorMsg);
}

/**
* Put a new datum from other server.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,12 @@ public void doFilter(ServletRequest request, ServletResponse response, FilterCha
return;
}

resp.getWriter()
.write("server is " + serverStatusManager.getServerStatus().name() + " now, please try again later!");
final String statusMsg = "server is " + serverStatusManager.getServerStatus().name() + "now";
if (serverStatusManager.getErrorMsg().isPresent()) {
resp.getWriter().write(statusMsg + ", detailed error message: " + serverStatusManager.getErrorMsg());
} else {
resp.getWriter().write(statusMsg + ", please try again later!");
}
resp.setStatus(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
}
}

0 comments on commit a9ca62c

Please sign in to comment.