Skip to content

Commit

Permalink
Merge pull request #2149 from codeimport/develop-fuzzyget-subscriber
Browse files Browse the repository at this point in the history
[ISSUE #2148] subscribers list page support fuzzy query
  • Loading branch information
nkorange authored Dec 19, 2019
2 parents 4029dd3 + 7b31daf commit bf10a32
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ private List<Subscriber> getSubscribers(String serviceName, String namespaceId)
return pushService.getClients(serviceName, namespaceId);
}

private List<Subscriber> getSubscribersFuzzy(String serviceName, String namespaceId) {
return pushService.getClientsFuzzy(serviceName, namespaceId);
}

/**
* @param serviceName
* @param namespaceId
Expand All @@ -68,7 +72,7 @@ public List<Subscriber> getSubscribers(String serviceName, String namespaceId, b
if (aggregation) {
// size = 1 means only myself in the list, we need at least one another server alive:
if (serverListManager.getHealthyServers().size() <= 1) {
return getSubscribers(serviceName, namespaceId);
return getSubscribersFuzzy(serviceName, namespaceId);
}

List<Subscriber> subscriberList = new ArrayList<Subscriber>();
Expand All @@ -80,7 +84,7 @@ public List<Subscriber> getSubscribers(String serviceName, String namespaceId, b
paramValues.put(CommonParams.NAMESPACE_ID, namespaceId);
paramValues.put("aggregation", String.valueOf(Boolean.FALSE));
if (NetUtils.localServer().equals(server.getKey())) {
subscriberList.addAll(getSubscribers(serviceName, namespaceId));
subscriberList.addAll(getSubscribersFuzzy(serviceName, namespaceId));
continue;
}

Expand All @@ -97,7 +101,7 @@ public List<Subscriber> getSubscribers(String serviceName, String namespaceId, b
: Collections.EMPTY_LIST;
} else {
// local server
return getSubscribers(serviceName, namespaceId);
return getSubscribersFuzzy(serviceName, namespaceId);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package com.alibaba.nacos.naming.push;

import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.naming.utils.NamingUtils;
import com.alibaba.nacos.naming.core.Service;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.SwitchDomain;
Expand Down Expand Up @@ -260,6 +262,31 @@ public List<Subscriber> getClients(String serviceName, String namespaceId) {
return clients;
}

/**
* fuzzy search subscriber
* @param serviceName
* @param namespaceId
* @return
*/
public List<Subscriber> getClientsFuzzy(String serviceName, String namespaceId) {
List<Subscriber> clients = new ArrayList<Subscriber>();
clientMap.forEach((outKey, clientConcurrentMap) -> {
//get groupedName from key
String serviceFullName = outKey.split(UtilsAndCommons.NAMESPACE_SERVICE_CONNECTOR)[1];
//get groupName
String groupName = NamingUtils.getGroupName(serviceFullName);
//get serviceName
String name = NamingUtils.getServiceName(serviceFullName);
//fuzzy match
if (outKey.startsWith(namespaceId) && name.indexOf(NamingUtils.getServiceName(serviceName)) >= 0 && groupName.indexOf(NamingUtils.getGroupName(serviceName)) >= 0) {
clientConcurrentMap.forEach((key, client) -> {
clients.add(new Subscriber(client.getAddrStr(), client.getAgent(), client.getApp(), client.getIp(), namespaceId, serviceFullName));
});
}
});
return clients;
}

public static void removeClientIfZombie() {

int size = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.alibaba.nacos.naming.core;

import com.alibaba.nacos.naming.BaseTest;
import com.alibaba.nacos.naming.pojo.Subscriber;
import com.alibaba.nacos.naming.push.PushService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import java.net.InetSocketAddress;
import java.util.List;

/**
* @description:
* @author: codeimport
* @create: 2019/12/6 18:30
**/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = MockServletContext.class)
@WebAppConfiguration
public class PushServiceTest extends BaseTest {
@InjectMocks
private PushService pushService;

@Before
public void before() {
super.before();
}

@Test
public void testGetClientsFuzzy() throws Exception {

String namespaceId = "public";
String clusters = "DEFAULT";
String agent = "Nacos-Java-Client:v1.1.4";
String clientIp = "localhost";
String app = "nacos";
int udpPort = 10000;

String helloServiceName = "helloGroupName@@helloServiceName";
int helloUdpPort = 10000;

String testServiceName = "testGroupName@@testServiceName";
int testUdpPort = 10001;

pushService.addClient(namespaceId
, helloServiceName
, clusters
, agent
, new InetSocketAddress(clientIp, helloUdpPort)
, null
, namespaceId
, app);

pushService.addClient(namespaceId
, testServiceName
, clusters
, agent
, new InetSocketAddress(clientIp, testUdpPort)
, null
, namespaceId
, app);

List<Subscriber> fuzzylist = pushService.getClientsFuzzy("hello@@hello","public");
Assert.assertEquals(fuzzylist.size(),1);
Assert.assertEquals(fuzzylist.get(0).getServiceName(),"helloGroupName@@helloServiceName");

List<Subscriber> list = pushService.getClientsFuzzy("helloGroupName@@helloServiceName","public");
Assert.assertEquals(list.size(),1);
Assert.assertEquals(list.get(0).getServiceName(),"helloGroupName@@helloServiceName");

List<Subscriber> noDataList = pushService.getClientsFuzzy("badGroupName@@badServiceName","public");
Assert.assertEquals(noDataList.size(),0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,25 @@ public void getSubscribersWithFalse() {
}
}

@Test
public void testGetSubscribersFuzzy() {
String serviceName = "test";
String namespaceId = "public";
boolean aggregation = Boolean.TRUE;
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);
Assert.assertNotNull(list);
Assert.assertEquals(1, list.size());
Assert.assertEquals("testGroupName@@test_subscriber", list.get(0).getServiceName());
} catch (Exception e) {

}
}

@Test
public void getSubscribersWithTrue() {
String serviceName = "test";
Expand Down

0 comments on commit bf10a32

Please sign in to comment.