[ISSUE #5078] set default pageSize to 10000 for getting all the subscribers in other server node. (#5424)

* hardcode to get all the subscribers by set the pageSize to 10000

* change the default pageSize from 10000 to 1000

* change the param of getSubscriber to support the pageNo and pageSize
This commit is contained in:
brothelul 2021-04-23 09:52:04 +08:00 committed by GitHub
parent b1e78b6f6d
commit 6c270cb3ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 23 additions and 10 deletions

View File

@ -43,7 +43,6 @@ import com.alibaba.nacos.naming.web.NamingResourceParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -449,8 +448,8 @@ public class ServiceController {
@Secured(parser = NamingResourceParser.class, action = ActionTypes.READ)
public ObjectNode subscribers(HttpServletRequest request) {
int pageNo = NumberUtils.toInt(WebUtils.required(request, "pageNo"));
int pageSize = NumberUtils.toInt(WebUtils.required(request, "pageSize"));
int pageNo = NumberUtils.toInt(WebUtils.optional(request, "pageNo", "1"));
int pageSize = NumberUtils.toInt(WebUtils.optional(request, "pageSize", "1000"));
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID, Constants.DEFAULT_NAMESPACE_ID);
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
@ -460,7 +459,7 @@ public class ServiceController {
ObjectNode result = JacksonUtils.createEmptyJsonNode();
try {
List<Subscriber> subscribers = subscribeManager.getSubscribers(serviceName, namespaceId, aggregation);
List<Subscriber> subscribers = subscribeManager.getSubscribers(serviceName, namespaceId, aggregation, pageNo, pageSize);
int start = (pageNo - 1) * pageSize;
if (start < 0) {

View File

@ -21,13 +21,14 @@ import com.alibaba.nacos.common.model.RestResult;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.core.cluster.Member;
import com.alibaba.nacos.core.cluster.ServerMemberManager;
import com.alibaba.nacos.sys.env.EnvUtil;
import com.alibaba.nacos.naming.misc.HttpClient;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.NetUtils;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.alibaba.nacos.naming.pojo.Subscriber;
import com.alibaba.nacos.naming.pojo.Subscribers;
import com.alibaba.nacos.naming.push.PushService;
import com.alibaba.nacos.sys.env.EnvUtil;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@ -73,11 +74,13 @@ public class SubscribeManager {
* @param serviceName service name
* @param namespaceId namespace id
* @param aggregation aggregation
* @param pageNo 页码
* @param pageSize 页大小
* @return list of subscriber
* @throws InterruptedException interrupted exception
*/
public List<Subscriber> getSubscribers(String serviceName, String namespaceId, boolean aggregation)
throws InterruptedException {
public List<Subscriber> getSubscribers(String serviceName, String namespaceId, boolean aggregation,
int pageNo, int pageSize) throws InterruptedException {
if (aggregation) {
// size = 1 means only myself in the list, we need at least one another server alive:
if (memberManager.getServerList().size() <= 1) {
@ -92,6 +95,8 @@ public class SubscribeManager {
paramValues.put(CommonParams.SERVICE_NAME, serviceName);
paramValues.put(CommonParams.NAMESPACE_ID, namespaceId);
paramValues.put("aggregation", String.valueOf(Boolean.FALSE));
paramValues.put("pageNo", String.valueOf(pageNo));
paramValues.put("pageSize", String.valueOf(pageSize));
if (NetUtils.localServer().equals(server.getAddress())) {
subscriberList.addAll(getSubscribersFuzzy(serviceName, namespaceId));
continue;
@ -105,6 +110,9 @@ public class SubscribeManager {
if (result.ok()) {
Subscribers subscribers = JacksonUtils.toObj(result.getData(), Subscribers.class);
subscriberList.addAll(subscribers.getSubscribers());
} else {
Loggers.SRV_LOG.warn("[SubscriberManager] get subscriber from other member error, server:{}, resultCode:{}, resultMsg:{}",
server.getAddress(), result.getCode(), result.getMessage());
}
}
return CollectionUtils.isNotEmpty(subscriberList) ? subscriberList.stream()

View File

@ -62,13 +62,15 @@ public class SubscribeManagerTest extends BaseTest {
String serviceName = "test";
String namespaceId = "public";
boolean aggregation = Boolean.FALSE;
int pageNo = 1;
int pageSize = 10;
try {
List<Subscriber> clients = new ArrayList<Subscriber>();
Subscriber subscriber = new Subscriber("127.0.0.1:8080", "test", "app", "127.0.0.1", namespaceId,
serviceName);
clients.add(subscriber);
Mockito.when(pushService.getClients(Mockito.anyString(), Mockito.anyString())).thenReturn(clients);
List<Subscriber> list = subscribeManager.getSubscribers(serviceName, namespaceId, aggregation);
List<Subscriber> list = subscribeManager.getSubscribers(serviceName, namespaceId, aggregation, pageNo, pageSize);
Assert.assertNotNull(list);
Assert.assertEquals(1, list.size());
Assert.assertEquals("public", list.get(0).getNamespaceId());
@ -82,13 +84,15 @@ public class SubscribeManagerTest extends BaseTest {
String serviceName = "test";
String namespaceId = "public";
boolean aggregation = Boolean.TRUE;
int pageNo = 1;
int pageSize = 10;
try {
List<Subscriber> clients = new ArrayList<Subscriber>();
Subscriber subscriber = new Subscriber("127.0.0.1:8080", "test", "app", "127.0.0.1", namespaceId,
"testGroupName@@test_subscriber");
clients.add(subscriber);
Mockito.when(pushService.getClientsFuzzy(Mockito.anyString(), Mockito.anyString())).thenReturn(clients);
List<Subscriber> list = subscribeManager.getSubscribers(serviceName, namespaceId, aggregation);
List<Subscriber> list = subscribeManager.getSubscribers(serviceName, namespaceId, aggregation, pageNo, pageSize);
Assert.assertNotNull(list);
Assert.assertEquals(1, list.size());
Assert.assertEquals("testGroupName@@test_subscriber", list.get(0).getServiceName());
@ -102,6 +106,8 @@ public class SubscribeManagerTest extends BaseTest {
String serviceName = "test";
String namespaceId = "public";
boolean aggregation = Boolean.TRUE;
int pageNo = 1;
int pageSize = 10;
try {
List<Subscriber> clients = new ArrayList<Subscriber>();
Subscriber subscriber = new Subscriber("127.0.0.1:8080", "test", "app", "127.0.0.1", namespaceId,
@ -124,7 +130,7 @@ public class SubscribeManagerTest extends BaseTest {
Mockito.when(memberManager.allMembers()).thenReturn(healthyServers);
//Mockito.doReturn(3).when(serverListManager.getHealthyServers().size());
List<Subscriber> list = subscribeManager.getSubscribers(serviceName, namespaceId, aggregation);
List<Subscriber> list = subscribeManager.getSubscribers(serviceName, namespaceId, aggregation, pageNo, pageSize);
Assert.assertNotNull(list);
Assert.assertEquals(2, list.size());
Assert.assertEquals("public", list.get(0).getNamespaceId());