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

[type:fix] Fix client register uri #5768

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,17 @@ public String registerApiDoc(final ApiDocRegisterDTO apiDocRegisterDTO) {
* @param selectorName the selector name
* @param uriList the uri list
* @param namespaceId the namespace id
* @return the string
*/
@Override
public String doRegisterURI(final String selectorName, final List<URIRegisterDTO> uriList, final String namespaceId) {
public void doRegisterURI(final String selectorName, final List<URIRegisterDTO> uriList, final String namespaceId) {
if (CollectionUtils.isEmpty(uriList)) {
return "";
return;
}
String pluginName = PluginNameAdapter.rpcTypeAdapter(rpcType());
SelectorDO selectorDO = selectorService.findByNameAndPluginNameAndNamespaceId(selectorName, pluginName, namespaceId);
if (Objects.isNull(selectorDO)) {
throw new ShenyuException("doRegister Failed to execute, wait to retry.");
LOG.warn("doRegister Failed to execute, wait to retry.");
return;
}
this.checkNamespacePluginRel(namespaceId, pluginName);
// fetch UPSTREAM_MAP data from db
Expand All @@ -212,7 +212,6 @@ public String doRegisterURI(final String selectorName, final List<URIRegisterDTO
// publish change event.
doDiscoveryLocal(selectorDO, pluginName, validUriList);
}
return ShenyuResultMessage.SUCCESS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,22 +55,18 @@ protected FallbackShenyuClientRegisterService() {
*
* @param selectorName the selector name
* @param uriList the uri list
* @return the string
*/
@Override
public String registerURI(final String selectorName, final List<URIRegisterDTO> uriList, final String namespaceId) {
String result;
public void registerURI(final String selectorName, final List<URIRegisterDTO> uriList, final String namespaceId) {
String key = key(selectorName);
try {
this.removeFallBack(key);
result = this.doRegisterURI(selectorName, uriList, namespaceId);
this.doRegisterURI(selectorName, uriList, namespaceId);
LOG.info("Register success: {},{},{}", namespaceId, selectorName, uriList);
} catch (Exception ex) {
LOG.error("Register exception: cause: {}", ex.getMessage());
result = "";
LOG.error("Register exception: ", ex);
this.addFallback(key, new FallbackHolder(selectorName, uriList, namespaceId));
}
return result;
}

@Override
Expand Down Expand Up @@ -114,7 +110,7 @@ private String key(final String selectorName) {
* @param uriList the uri list
* @return the string
*/
abstract String doRegisterURI(String selectorName, List<URIRegisterDTO> uriList, String namespaceId);
abstract void doRegisterURI(String selectorName, List<URIRegisterDTO> uriList, String namespaceId);

/**
* Heartbeat.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,8 @@ public interface ShenyuClientRegisterService {
* @param selectorName the selector name
* @param uriList the uri list
* @param namespaceId the namespace id
* @return the string
*/
default String registerURI(final String selectorName, final List<URIRegisterDTO> uriList, final String namespaceId) {
return Constants.SUCCESS;
default void registerURI(final String selectorName, final List<URIRegisterDTO> uriList, final String namespaceId) {
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

package org.apache.shenyu.admin.service.register;

import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.admin.mapper.NamespacePluginRelMapper;
import org.apache.shenyu.admin.mapper.PluginMapper;
import org.apache.shenyu.admin.model.dto.RuleDTO;
Expand All @@ -38,7 +37,6 @@
import org.apache.shenyu.common.dto.SelectorData;
import org.apache.shenyu.common.dto.convert.selector.CommonUpstream;
import org.apache.shenyu.common.enums.PluginEnum;
import org.apache.shenyu.common.exception.ShenyuException;
import org.apache.shenyu.register.common.dto.ApiDocRegisterDTO;
import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
import org.apache.shenyu.register.common.dto.URIRegisterDTO;
Expand All @@ -58,8 +56,9 @@

import static org.apache.shenyu.common.constant.Constants.SYS_DEFAULT_NAMESPACE_ID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -169,12 +168,14 @@ public void testDoRegisterURI() {
namespacePluginVO.setPluginId("1");
namespacePluginVO.setNamespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID);
when(namespacePluginRelMapper.selectByPluginIdAndNamespaceId(pluginDO.getId(), Constants.SYS_DEFAULT_NAMESPACE_ID)).thenReturn(namespacePluginVO);
assertEquals(StringUtils.EMPTY, abstractShenyuClientRegisterService.doRegisterURI("Selector_Name", new ArrayList<>(), SYS_DEFAULT_NAMESPACE_ID));
abstractShenyuClientRegisterService.doRegisterURI("Selector_Name", new ArrayList<>(), SYS_DEFAULT_NAMESPACE_ID);
assertNull(selectorService.findByNameAndPluginNameAndNamespaceId("Selector_Name", PluginEnum.GRPC.getName(), SYS_DEFAULT_NAMESPACE_ID));

URIRegisterDTO uriRegisterDTO = URIRegisterDTO.builder().build();
uriRegisterDTO.setPort(8080);
uriRegisterDTO.setHost("Host");
assertThrows(ShenyuException.class, () -> abstractShenyuClientRegisterService.doRegisterURI("Selector_Name", Collections.singletonList(uriRegisterDTO), SYS_DEFAULT_NAMESPACE_ID));
abstractShenyuClientRegisterService.doRegisterURI("Selector_Name", Collections.singletonList(uriRegisterDTO), SYS_DEFAULT_NAMESPACE_ID);
assertNull(selectorService.findByNameAndPluginNameAndNamespaceId("Selector_Name", PluginEnum.GRPC.getName(), SYS_DEFAULT_NAMESPACE_ID));

SelectorDO selectorDO = mock(SelectorDO.class);
when(selectorService.findByNameAndPluginNameAndNamespaceId(any(), any(), any())).thenReturn(selectorDO);
Expand All @@ -188,7 +189,8 @@ public void testDoRegisterURI() {
namespacePlugin.setPluginId("1");
namespacePlugin.setNamespaceId(Constants.SYS_DEFAULT_NAMESPACE_ID);
when(namespacePluginRelMapper.selectByPluginIdAndNamespaceId(pluginDO.getId(), Constants.SYS_DEFAULT_NAMESPACE_ID)).thenReturn(namespacePlugin);
assertEquals(ShenyuResultMessage.SUCCESS, abstractShenyuClientRegisterService.doRegisterURI("Selector_Name", Collections.singletonList(uriRegisterDTO), SYS_DEFAULT_NAMESPACE_ID));
abstractShenyuClientRegisterService.doRegisterURI("Selector_Name", Collections.singletonList(uriRegisterDTO), SYS_DEFAULT_NAMESPACE_ID);
assertNotNull(selectorService.findByNameAndPluginNameAndNamespaceId("Selector_Name", PluginEnum.GRPC.getName(), SYS_DEFAULT_NAMESPACE_ID));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,41 @@

package org.apache.shenyu.admin.service.register;

import org.apache.commons.lang3.StringUtils;
import org.apache.shenyu.common.exception.ShenyuException;
import org.apache.shenyu.register.common.dto.ApiDocRegisterDTO;
import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
import org.apache.shenyu.register.common.dto.URIRegisterDTO;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import java.util.ArrayList;
import java.util.List;

import static org.apache.shenyu.common.constant.Constants.SYS_DEFAULT_NAMESPACE_ID;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;

/**
* Test cases for FallbackShenyuClientRegisterService.
*/
class FallbackShenyuClientRegisterServiceTest {



@Test
public void testRegisterURI() {
MockFallbackShenyuClientRegisterService mockFallbackShenyuClientRegisterService = new MockFallbackShenyuClientRegisterService();
assertEquals("doRegisterURI", mockFallbackShenyuClientRegisterService.registerURI("Selector_Name", new ArrayList<>(), SYS_DEFAULT_NAMESPACE_ID));
MockFallbackShenyuClientRegisterService mockFallbackShenyuClientRegisterService = mock(MockFallbackShenyuClientRegisterService.class);
Mockito.doThrow(ShenyuException.class).when(mockFallbackShenyuClientRegisterService).doRegisterURI("Selector_Name", new ArrayList<>(), SYS_DEFAULT_NAMESPACE_ID);

MockFallbackShenyuClientRegisterServiceException mockFallbackShenyuClientRegisterServiceException = mock(MockFallbackShenyuClientRegisterServiceException.class);
Mockito.doThrow(ShenyuException.class).when(mockFallbackShenyuClientRegisterServiceException).registerURI("Selector_Name", new ArrayList<>(), SYS_DEFAULT_NAMESPACE_ID);

MockFallbackShenyuClientRegisterServiceException mockFallbackShenyuClientRegisterServiceException = new MockFallbackShenyuClientRegisterServiceException();
assertEquals(StringUtils.EMPTY, mockFallbackShenyuClientRegisterServiceException.registerURI("Selector_Name", new ArrayList<>(), SYS_DEFAULT_NAMESPACE_ID));
}

static class MockFallbackShenyuClientRegisterService extends FallbackShenyuClientRegisterService {

@Override
String doRegisterURI(final String selectorName, final List<URIRegisterDTO> uriList, final String namespaceId) {
return "doRegisterURI";
void doRegisterURI(final String selectorName, final List<URIRegisterDTO> uriList, final String namespaceId) {
throw new ShenyuException("Exception");
}

@Override
Expand Down Expand Up @@ -80,7 +83,7 @@ public void checkNamespacePluginRel(final String namespaceId, final String plugi
static class MockFallbackShenyuClientRegisterServiceException extends FallbackShenyuClientRegisterService {

@Override
String doRegisterURI(final String selectorName, final List<URIRegisterDTO> uriList, final String namespaceId) {
void doRegisterURI(final String selectorName, final List<URIRegisterDTO> uriList, final String namespaceId) {
throw new ShenyuException("Exception");
}

Expand Down
Loading