forked from apache/shenyu
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[type:feat] divide plugin adapt discovery (apache#5185)
* discover-mode adapt divide plugin * add test --------- Co-authored-by: ‘xcsnx’ <‘[email protected]’>
- Loading branch information
Showing
6 changed files
with
352 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
...vide/src/main/java/org/apache/shenyu/plugin/divide/handler/DivideUpstreamDataHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.shenyu.plugin.divide.handler; | ||
|
||
import org.apache.shenyu.common.dto.DiscoverySyncData; | ||
import org.apache.shenyu.common.dto.DiscoveryUpstreamData; | ||
import org.apache.shenyu.common.enums.PluginEnum; | ||
import org.apache.shenyu.loadbalancer.cache.UpstreamCacheManager; | ||
import org.apache.shenyu.loadbalancer.entity.Upstream; | ||
import org.apache.shenyu.plugin.base.cache.MetaDataCache; | ||
import org.apache.shenyu.plugin.base.handler.DiscoveryUpstreamDataHandler; | ||
import org.springframework.util.ObjectUtils; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* upstreamList data change. | ||
*/ | ||
public class DivideUpstreamDataHandler implements DiscoveryUpstreamDataHandler { | ||
|
||
@Override | ||
public void handlerDiscoveryUpstreamData(final DiscoverySyncData discoverySyncData) { | ||
if (Objects.isNull(discoverySyncData) || Objects.isNull(discoverySyncData.getSelectorId())) { | ||
return; | ||
} | ||
List<DiscoveryUpstreamData> upstreamList = discoverySyncData.getUpstreamDataList(); | ||
UpstreamCacheManager.getInstance().submit(discoverySyncData.getSelectorId(), convertUpstreamList(upstreamList)); | ||
// the update is also need to clean, but there is no way to | ||
// distinguish between crate and update, so it is always clean | ||
MetaDataCache.getInstance().clean(); | ||
} | ||
|
||
@Override | ||
public String pluginName() { | ||
return PluginEnum.DIVIDE.getName(); | ||
} | ||
|
||
private List<Upstream> convertUpstreamList(final List<DiscoveryUpstreamData> upstreamList) { | ||
if (ObjectUtils.isEmpty(upstreamList)) { | ||
return Collections.emptyList(); | ||
} | ||
return upstreamList.stream().map(u -> Upstream.builder() | ||
.protocol(u.getProtocol()) | ||
.url(u.getUrl()) | ||
.weight(u.getWeight()) | ||
.status(0 == u.getStatus()) | ||
.timestamp(u.getDateUpdated().getTime()) | ||
.build()).collect(Collectors.toList()); | ||
} | ||
|
||
} |
100 changes: 100 additions & 0 deletions
100
.../src/test/java/org/apache/shenyu/plugin/divide/handler/DivideUpstreamDataHandlerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.shenyu.plugin.divide.handler; | ||
|
||
import org.apache.shenyu.common.dto.DiscoverySyncData; | ||
import org.apache.shenyu.common.dto.DiscoveryUpstreamData; | ||
import org.apache.shenyu.common.enums.PluginEnum; | ||
import org.apache.shenyu.common.utils.UpstreamCheckUtils; | ||
import org.apache.shenyu.loadbalancer.cache.UpstreamCacheManager; | ||
import org.apache.shenyu.loadbalancer.entity.Upstream; | ||
import org.junit.jupiter.api.AfterEach; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.MockedStatic; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.mockito.junit.jupiter.MockitoSettings; | ||
import org.mockito.quality.Strictness; | ||
|
||
import java.sql.Timestamp; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.Stream; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.mockito.ArgumentMatchers.anyInt; | ||
import static org.mockito.ArgumentMatchers.anyString; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.mockStatic; | ||
import static org.mockito.Mockito.when; | ||
|
||
@ExtendWith(MockitoExtension.class) | ||
@MockitoSettings(strictness = Strictness.LENIENT) | ||
public class DivideUpstreamDataHandlerTest { | ||
|
||
private DiscoverySyncData discoverySyncData; | ||
|
||
private DivideUpstreamDataHandler divideUpstreamDataHandler; | ||
|
||
private MockedStatic<UpstreamCheckUtils> mockCheckUtils; | ||
|
||
@BeforeEach | ||
public void setUp() { | ||
this.divideUpstreamDataHandler = new DivideUpstreamDataHandler(); | ||
List<DiscoveryUpstreamData> divideUpstreamList = Stream.of(3) | ||
.map(weight -> DiscoveryUpstreamData.builder() | ||
.url("mock-" + weight) | ||
.dateUpdated(new Timestamp(System.currentTimeMillis())) | ||
.build()) | ||
.collect(Collectors.toList()); | ||
this.discoverySyncData = mock(DiscoverySyncData.class); | ||
when(discoverySyncData.getSelectorId()).thenReturn("handler"); | ||
when(discoverySyncData.getUpstreamDataList()).thenReturn(divideUpstreamList); | ||
|
||
// mock static | ||
mockCheckUtils = mockStatic(UpstreamCheckUtils.class); | ||
mockCheckUtils.when(() -> UpstreamCheckUtils.checkUrl(anyString(), anyInt())).thenReturn(true); | ||
} | ||
|
||
@AfterEach | ||
public void tearDown() { | ||
mockCheckUtils.close(); | ||
} | ||
|
||
/** | ||
* Handler selector test. | ||
*/ | ||
@Test | ||
public void handlerDiscoveryUpstreamDataTest() { | ||
divideUpstreamDataHandler.handlerDiscoveryUpstreamData(discoverySyncData); | ||
List<Upstream> result = UpstreamCacheManager.getInstance().findUpstreamListBySelectorId("handler"); | ||
assertEquals(discoverySyncData.getUpstreamDataList().get(0).getUrl(), result.get(0).getUrl()); | ||
DiscoverySyncData discoverySyncData = new DiscoverySyncData(); | ||
discoverySyncData.setSelectorId(null); | ||
divideUpstreamDataHandler.handlerDiscoveryUpstreamData(discoverySyncData); | ||
} | ||
|
||
/** | ||
* Plugin named test. | ||
*/ | ||
@Test | ||
public void pluginNamedTest() { | ||
assertEquals(divideUpstreamDataHandler.pluginName(), PluginEnum.DIVIDE.getName()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters