Skip to content

Commit

Permalink
[ISSUE #5078] set default pageSize to 10000 for getting all the subsc…
Browse files Browse the repository at this point in the history
…ribers 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
  • Loading branch information
brotherlu-xcq authored Apr 23, 2021
1 parent b1e78b6 commit 6c270cb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
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;
Expand Down Expand Up @@ -449,8 +448,8 @@ public ObjectNode checksum(HttpServletRequest request) throws Exception {
@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);
Expand All @@ -460,7 +459,7 @@ public ObjectNode subscribers(HttpServletRequest request) {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@
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;
Expand Down Expand Up @@ -73,11 +74,13 @@ private List<Subscriber> getSubscribers(String serviceName, String namespaceId)
* @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) {
Expand All @@ -92,6 +95,8 @@ public List<Subscriber> getSubscribers(String serviceName, String namespaceId, b
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;
Expand All @@ -105,6 +110,9 @@ public List<Subscriber> getSubscribers(String serviceName, String namespaceId, b
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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,15 @@ public void getSubscribersWithFalse() {
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());
Expand All @@ -82,13 +84,15 @@ public void testGetSubscribersFuzzy() {
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());
Expand All @@ -102,6 +106,8 @@ public void getSubscribersWithTrue() {
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,
Expand All @@ -124,7 +130,7 @@ public void getSubscribersWithTrue() {

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());
Expand Down

0 comments on commit 6c270cb

Please sign in to comment.