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

[ISSUES #11499] fix address server health check error #11508

Merged
merged 2 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.paramcheck.ConfigDefaultHttpParamExtractor;
import com.alibaba.nacos.core.cluster.MemberLookup;
import com.alibaba.nacos.core.paramcheck.ExtractorManager;
import com.alibaba.nacos.persistence.datasource.DataSourceService;
import com.alibaba.nacos.persistence.datasource.DynamicDataSource;
Expand Down Expand Up @@ -89,9 +90,27 @@ public String getHealth() {
}

private boolean isAddressServerHealthy() {
Map<String, Object> info = memberManager.getLookup().info();
return info != null && info.get("addressServerHealth") != null && Boolean
.parseBoolean(info.get("addressServerHealth").toString());
final MemberLookup lookup = memberManager.getLookup();
if (lookup == null) {
return false;
}

final boolean useAddressServer = lookup.useAddressServer();
if (!useAddressServer) {
return true;
}

final Map<String, Object> info = lookup.info();
if (info == null) {
return false;
}

final Object addressServerHealth = info.get("addressServerHealth");
if (addressServerHealth == null) {
return false;
}

return Boolean.parseBoolean(addressServerHealth.toString());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,90 @@ public void testGetHealth() throws Exception {
Assert.assertEquals("UP", actualValue);

}

@Test
public void testGetHealthWhenTheLookUpIsNull() throws Exception {
when(dataSourceService.getHealth()).thenReturn("UP");
when(memberManager.getLookup()).thenReturn(null);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH);
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
Assert.assertEquals("DOWN:address server down. ", actualValue);
}

@Test
public void testGetHealthWhenTheLoopUpNotUseAddressServer() throws Exception {
when(dataSourceService.getHealth()).thenReturn("UP");
when(memberManager.getLookup()).thenReturn(memberLookup);
when(memberLookup.useAddressServer()).thenReturn(false);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH);
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
Assert.assertEquals("UP", actualValue);
}

@Test
public void testGetHealthWhenTheLoopUpInfoIsNull() throws Exception {
when(dataSourceService.getHealth()).thenReturn("UP");
when(memberManager.getLookup()).thenReturn(memberLookup);
when(memberLookup.useAddressServer()).thenReturn(true);
when(memberLookup.info()).thenReturn(null);
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH);
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
Assert.assertEquals("DOWN:address server down. ", actualValue);
}

@Test
public void testGetHealthWhenTheLoopUpInfoIsEmpty() throws Exception {
when(dataSourceService.getHealth()).thenReturn("UP");
when(memberManager.getLookup()).thenReturn(memberLookup);
when(memberLookup.useAddressServer()).thenReturn(true);
when(memberLookup.info()).thenReturn(new HashMap<>());
MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH);
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
Assert.assertEquals("DOWN:address server down. ", actualValue);
}

@Test
public void testGetHealthWhenTheLoopUpInfoIsDown() throws Exception {
when(dataSourceService.getHealth()).thenReturn("UP");
when(memberManager.getLookup()).thenReturn(memberLookup);
when(memberLookup.useAddressServer()).thenReturn(true);

final HashMap<String, Object> info = new HashMap<>();
info.put("addressServerHealth", "false");
when(memberLookup.info()).thenReturn(info);

MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH);
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
Assert.assertEquals("DOWN:address server down. ", actualValue);
}

@Test
public void testGetHealthWhenTheLoopUpInfoIsUP() throws Exception {
when(dataSourceService.getHealth()).thenReturn("UP");
when(memberManager.getLookup()).thenReturn(memberLookup);
when(memberLookup.useAddressServer()).thenReturn(true);

final HashMap<String, Object> info = new HashMap<>();
info.put("addressServerHealth", "true");
when(memberLookup.info()).thenReturn(info);

MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH);
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
Assert.assertEquals("UP", actualValue);
}

@Test
public void testGetHealthWhenTheLoopUpInfoParseError() throws Exception {
when(dataSourceService.getHealth()).thenReturn("UP");
when(memberManager.getLookup()).thenReturn(memberLookup);
when(memberLookup.useAddressServer()).thenReturn(true);

final HashMap<String, Object> info = new HashMap<>();
info.put("addressServerHealth", "not boolean value");
when(memberLookup.info()).thenReturn(info);

MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get(Constants.HEALTH_CONTROLLER_PATH);
String actualValue = mockmvc.perform(builder).andReturn().getResponse().getContentAsString();
Assert.assertEquals("DOWN:address server down. ", actualValue);
}
}
Loading