forked from apache/incubator-seata
-
Notifications
You must be signed in to change notification settings - Fork 0
CR: Gsoc routing support #30
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
Closed
Closed
Changes from 26 commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
ecaf257
feat: add check box
YoWuwuuuw 6d033b7
Merge remote-tracking branch 'origin/2.x' into 2.x
YoWuwuuuw 4446570
feat: Data format for supporting metadata
YoWuwuuuw 731b6bd
test: add test
YoWuwuuuw 49c54db
feat: support server-metadata access to RegistryService
YoWuwuuuw e1ad806
opt: solve conflicts and opt discovery-namingserver code
YoWuwuuuw 66702a9
fix: fix some bug and improve code readability
YoWuwuuuw 3953c32
test: fix test for discovery-namingserver
YoWuwuuuw 81465da
feat: enhance discovery-loadbalance to support metadata
YoWuwuuuw 8d015b5
feat: enhance discovery-loadbalance to support metadata
YoWuwuuuw bab788c
feat: support WeightedRandomLoadBalance
YoWuwuuuw a3d50dc
test: fix test
YoWuwuuuw d1c4f12
test: fix test
YoWuwuuuw 775c7b1
Merge branch '2.x' into gsoc-metadata-support
YoWuwuuuw 9f4439d
doc: improve Java doc and improve readability
YoWuwuuuw 8ae37ae
opt: apply spotless
YoWuwuuuw 27d1572
opt: rabbit review
YoWuwuuuw 24ba004
test: fix test
YoWuwuuuw e562774
add changes
YoWuwuuuw 61b95e1
test: add test for ServiceInstance
YoWuwuuuw 1c9385d
test: fix ci
YoWuwuuuw 1b29d94
test: add test for discovery-namingserver
YoWuwuuuw 7cfd97c
test: fix ci
YoWuwuuuw 58806b8
test:refactor test
YoWuwuuuw 7af2622
feat: add routing support
YoWuwuuuw 90eb1cb
Merge branch '2.x' into gsoc-routing-support-mock
YoWuwuuuw 3b3116d
opt: fix ci, ai cr
YoWuwuuuw d05e99a
Merge remote-tracking branch 'origin/gsoc-routing-support-mock' into …
YoWuwuuuw 6772afa
fix: pmd check
YoWuwuuuw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
135 changes: 135 additions & 0 deletions
135
common/src/main/java/org/apache/seata/common/metadata/ServiceInstance.java
This file contains hidden or 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,135 @@ | ||
| /* | ||
| * 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.seata.common.metadata; | ||
|
|
||
| import org.apache.seata.common.util.NetUtil; | ||
|
|
||
| import java.net.InetSocketAddress; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
| import java.util.Set; | ||
|
|
||
| /** | ||
| * entity for packaging inetSocketAddress and metadata for loadBalance | ||
| */ | ||
| public class ServiceInstance { | ||
| private InetSocketAddress address; | ||
| private Map<String, Object> metadata; | ||
|
|
||
| public ServiceInstance(InetSocketAddress address, Map<String, Object> metadata) { | ||
| this.address = address; | ||
| this.metadata = metadata; | ||
| } | ||
|
|
||
| public ServiceInstance(Instance instance) { | ||
| this.address = new InetSocketAddress( | ||
| instance.getTransaction().getHost(), instance.getTransaction().getPort()); | ||
| this.metadata = instance.getMetadata(); | ||
| } | ||
YoWuwuuuw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| public ServiceInstance(InetSocketAddress address) { | ||
| this.address = address; | ||
| } | ||
|
|
||
| public InetSocketAddress getAddress() { | ||
| return address; | ||
| } | ||
|
|
||
| public void setAddress(InetSocketAddress address) { | ||
| this.address = address; | ||
| } | ||
|
|
||
| public Map<String, Object> getMetadata() { | ||
| return metadata; | ||
| } | ||
|
|
||
| public void setMetadata(Map<String, Object> metadata) { | ||
| this.metadata = metadata; | ||
| } | ||
YoWuwuuuw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Converts a list of InetSocketAddress to a list of ServiceInstance. | ||
| * @param addresses list of InetSocketAddress | ||
| * @return list of ServiceInstance | ||
| */ | ||
| public static List<ServiceInstance> convertToServiceInstanceSet(List<InetSocketAddress> addresses) { | ||
| List<ServiceInstance> serviceInstances = new ArrayList<>(); | ||
| if (addresses != null && !addresses.isEmpty()) { | ||
| for (InetSocketAddress address : addresses) { | ||
| NetUtil.validAddress(address); | ||
| serviceInstances.add(new ServiceInstance(address, null)); | ||
| } | ||
| } | ||
| return serviceInstances; | ||
| } | ||
|
|
||
| /** | ||
| * Converts a set of InetSocketAddress to a set of ServiceInstance in RedisRegistryServiceImpl. | ||
| * @param addresses set of InetSocketAddress | ||
| * @return set of ServiceInstance | ||
| */ | ||
| public static Set<ServiceInstance> convertToServiceInstanceSet(Set<InetSocketAddress> addresses) { | ||
| Set<ServiceInstance> serviceInstances = new HashSet<>(); | ||
| if (addresses != null && !addresses.isEmpty()) { | ||
| for (InetSocketAddress address : addresses) { | ||
| NetUtil.validAddress(address); | ||
| serviceInstances.add(new ServiceInstance(address, null)); | ||
| } | ||
| } | ||
| return serviceInstances; | ||
| } | ||
YoWuwuuuw marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| /** | ||
| * Creates a ServiceInstance from an InetSocketAddress and a Map<String, String> of metadata. | ||
| * @param address the InetSocketAddress | ||
| * @param stringMap the map of string metadata | ||
| * @return a new ServiceInstance | ||
| */ | ||
| public static ServiceInstance fromStringMap(InetSocketAddress address, Map<String, String> stringMap) { | ||
| Map<String, Object> metadata = new HashMap<>(); | ||
| if (stringMap != null) { | ||
| metadata.putAll(stringMap); | ||
| } | ||
| return new ServiceInstance(address, metadata); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { | ||
| if (this == o) { | ||
| return true; | ||
| } | ||
| if (o == null || getClass() != o.getClass()) { | ||
| return false; | ||
| } | ||
| ServiceInstance that = (ServiceInstance) o; | ||
| return Objects.equals(address, that.address) && Objects.equals(metadata, that.metadata); | ||
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { | ||
| return Objects.hash(address, metadata); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "ServiceInstance{" + "address=" + address + ", metadata=" + metadata + '}'; | ||
| } | ||
| } | ||
123 changes: 123 additions & 0 deletions
123
common/src/test/java/org/apache/seata/common/metadata/ServiceInstanceTest.java
This file contains hidden or 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,123 @@ | ||
| /* | ||
| * 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.seata.common.metadata; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import java.net.InetSocketAddress; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.HashSet; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertFalse; | ||
| import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
|
||
| public class ServiceInstanceTest { | ||
|
|
||
| private final InetSocketAddress address1 = new InetSocketAddress("127.0.0.1", 8091); | ||
| private final InetSocketAddress address2 = new InetSocketAddress("127.0.0.1", 8092); | ||
|
|
||
| @Test | ||
| public void testConstructorAndGetters() { | ||
| Map<String, Object> metadata = new HashMap<>(); | ||
| metadata.put("key1", "value1"); | ||
|
|
||
| ServiceInstance instance1 = new ServiceInstance(address1, metadata); | ||
|
|
||
| assertEquals(address1, instance1.getAddress()); | ||
| assertEquals(metadata, instance1.getMetadata()); | ||
|
|
||
| Instance instance = Instance.getInstance(); | ||
| instance.setTransaction(new Node.Endpoint("127.0.0.1", 8093)); | ||
|
|
||
| ServiceInstance instance2 = new ServiceInstance(Instance.getInstance()); | ||
|
|
||
| assertEquals( | ||
| Instance.getInstance().getTransaction().getHost(), | ||
| instance2.getAddress().getAddress().getHostAddress()); | ||
| assertEquals( | ||
| Instance.getInstance().getTransaction().getPort(), | ||
| instance2.getAddress().getPort()); | ||
|
|
||
| instance.setTransaction(null); // clean up after test | ||
YoWuwuuuw marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| @Test | ||
| public void testConvertToServiceInstanceList() { | ||
| List<InetSocketAddress> addresses = new ArrayList<>(); | ||
| addresses.add(address1); | ||
| addresses.add(address2); | ||
|
|
||
| List<ServiceInstance> serviceInstances = ServiceInstance.convertToServiceInstanceSet(addresses); | ||
|
|
||
| assertEquals(2, serviceInstances.size()); | ||
| assertEquals(address1, serviceInstances.get(0).getAddress()); | ||
| assertEquals(address2, serviceInstances.get(1).getAddress()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testConvertToServiceInstanceSet() { | ||
| Set<InetSocketAddress> addresses = new HashSet<>(); | ||
| addresses.add(address1); | ||
| addresses.add(address2); | ||
|
|
||
| Set<ServiceInstance> serviceInstances = ServiceInstance.convertToServiceInstanceSet(addresses); | ||
|
|
||
| assertEquals(2, serviceInstances.size()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testSetAddressAndSetMetadata() { | ||
| ServiceInstance instance = new ServiceInstance(address1); | ||
| instance.setAddress(address2); | ||
| instance.setMetadata(new HashMap<>()); | ||
|
|
||
| assertEquals(address2, instance.getAddress()); | ||
| assertNotNull(instance.getMetadata()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFromStringMap() { | ||
| Map<String, String> stringMap = new HashMap<>(); | ||
| stringMap.put("stringKey", "stringValue"); | ||
|
|
||
| ServiceInstance instance = ServiceInstance.fromStringMap(address1, stringMap); | ||
|
|
||
| assertEquals(address1, instance.getAddress()); | ||
| assertNotNull(instance.getMetadata()); | ||
| assertEquals("stringValue", instance.getMetadata().get("stringKey")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEqualsAndToString() { | ||
| ServiceInstance instance1 = new ServiceInstance(address1); | ||
| ServiceInstance instance2 = new ServiceInstance(address1); | ||
| ServiceInstance instance3 = new ServiceInstance(address2); | ||
|
|
||
| assertTrue(instance1.equals(instance2)); | ||
| assertTrue(instance1.equals(instance1)); | ||
| assertFalse(instance1.equals(instance3)); | ||
| assertFalse(instance1.equals("string")); | ||
|
|
||
| assertTrue(instance1.toString().contains("8091")); | ||
| } | ||
| } | ||
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.