Skip to content

Commit 1171548

Browse files
authored
feature: init namingserver client (apache#6536)
1 parent fff45ce commit 1171548

File tree

28 files changed

+1413
-96
lines changed

28 files changed

+1413
-96
lines changed

all/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@
146146
<artifactId>seata-discovery-etcd3</artifactId>
147147
<version>${project.version}</version>
148148
</dependency>
149+
<dependency>
150+
<groupId>org.apache.seata</groupId>
151+
<artifactId>seata-discovery-namingserver</artifactId>
152+
<version>${project.version}</version>
153+
</dependency>
149154
<dependency>
150155
<groupId>org.apache.seata</groupId>
151156
<artifactId>seata-brpc</artifactId>

bom/pom.xml

+5
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@
126126
<artifactId>seata-discovery-zk</artifactId>
127127
<version>${project.version}</version>
128128
</dependency>
129+
<dependency>
130+
<groupId>org.apache.seata</groupId>
131+
<artifactId>seata-discovery-namingserver</artifactId>
132+
<version>${project.version}</version>
133+
</dependency>
129134
<dependency>
130135
<groupId>org.apache.seata</groupId>
131136
<artifactId>seata-discovery-redis</artifactId>

changes/en-us/2.x.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ Add changes here for all PR submitted to the 2.x branch.
33
<!-- Please add the `changes` to the following location(feature/bugfix/optimize/test) based on the type of PR -->
44

55
### feature:
6+
- [[#6536](https://github.com/apache/incubator-seata/pull/6536)] support naming server client
67
- [[#6226](https://github.com/apache/incubator-seata/pull/6226)] multi-version seata protocol support
78
- [[#6537](https://github.com/apache/incubator-seata/pull/6537)] support Namingserver
89
- [[#6538](https://github.com/apache/incubator-seata/pull/6538)] Integration of naming server on the Seata server side

changes/zh-cn/2.x.md

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<!-- 请根据PR的类型添加 `变更记录` 到以下对应位置(feature/bugfix/optimize/test) 下 -->
44

55
### feature:
6+
- [[#6536](https://github.com/apache/incubator-seata/pull/6536)] 支持 naming server客户端
67
- [[#6226](https://github.com/apache/incubator-seata/pull/6226)] 支持seata私有协议多版本兼容
78
- [[#6537](https://github.com/apache/incubator-seata/pull/6537)] 支持 Namingserver
89
- [[#6538](https://github.com/apache/incubator-seata/pull/6538)] seata server端集成naming server

common/src/main/java/org/apache/seata/common/XID.java

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static org.apache.seata.common.Constants.IP_PORT_SPLIT_CHAR;
2020

21+
2122
/**
2223
* The type Xid.
2324
*

common/src/main/java/org/apache/seata/common/metadata/Node.java

+67
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.HashMap;
2020
import java.util.Map;
21+
import java.util.Objects;
2122

2223

2324
public class Node {
@@ -28,6 +29,10 @@ public class Node {
2829

2930
private Endpoint internal;
3031

32+
private double weight = 1.0;
33+
private boolean healthy = true;
34+
private long timeStamp;
35+
3136
private String group;
3237
private ClusterRole role = ClusterRole.MEMBER;
3338

@@ -97,6 +102,49 @@ public void setInternal(Endpoint internal) {
97102
this.internal = internal;
98103
}
99104

105+
@Override
106+
public int hashCode() {
107+
return Objects.hash(control, transaction);
108+
}
109+
110+
@Override
111+
public boolean equals(Object o) {
112+
if (this == o) {
113+
return true;
114+
}
115+
if (o == null || getClass() != o.getClass()) {
116+
return false;
117+
}
118+
Node node = (Node) o;
119+
return Objects.equals(control, node.control) && Objects.equals(transaction, node.transaction);
120+
}
121+
122+
123+
// convert to String
124+
public String toJsonString() {
125+
StringBuilder sb = new StringBuilder();
126+
sb.append("{");
127+
sb.append("\"controlEndpoint\": ").append(control.toString()).append(", ");
128+
sb.append("\"transactionEndpoint\": ").append(transaction.toString()).append(", ");
129+
sb.append("\"weight\": ").append(weight).append(", ");
130+
sb.append("\"healthy\": ").append(healthy).append(", ");
131+
sb.append("\"timeStamp\": ").append(timeStamp).append(", ");
132+
sb.append("\"metadata\": {");
133+
134+
// handle metadata k-v map
135+
int i = 0;
136+
for (Map.Entry<String, Object> entry : metadata.entrySet()) {
137+
if (i > 0) {
138+
sb.append(", ");
139+
}
140+
sb.append("\"").append(entry.getKey()).append("\": \"").append(entry.getValue()).append("\"");
141+
i++;
142+
}
143+
144+
sb.append("}}");
145+
return sb.toString();
146+
}
147+
100148
public static class Endpoint {
101149

102150
private String host;
@@ -136,6 +184,25 @@ public String createAddress() {
136184
return host + ":" + port;
137185
}
138186

187+
@Override
188+
public int hashCode() {
189+
return Objects.hash(host,port,protocol);
190+
}
191+
192+
@Override
193+
public boolean equals(Object o) {
194+
if (this == o) {
195+
return true;
196+
}
197+
if (o == null || getClass() != o.getClass()) {
198+
return false;
199+
}
200+
Endpoint endpoint = (Endpoint) o;
201+
return Objects.equals(endpoint.host,this.host)
202+
&& Objects.equals(endpoint.port,this.port)
203+
&& Objects.equals(endpoint.protocol,this.protocol);
204+
}
205+
139206
@Override
140207
public String toString() {
141208
return "Endpoint{" + "host='" + host + '\'' + ", port=" + port + '}';

common/src/main/java/org/apache/seata/common/metadata/namingserver/Instance.java

+24-14
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ public class Instance {
3434
private String namespace;
3535
private String clusterName;
3636
private String unit;
37-
private Node.Endpoint controlEndpoint = new Node.Endpoint();
38-
private Node.Endpoint transactionEndpoint = new Node.Endpoint();
37+
private Node.Endpoint control = new Node.Endpoint();
38+
private Node.Endpoint transaction = new Node.Endpoint();
3939
private double weight = 1.0;
4040
private boolean healthy = true;
4141
private long term;
42+
private long timestamp;
4243
private ClusterRole role = ClusterRole.MEMBER;
4344
private Map<String, Object> metadata = new HashMap<>();
4445

@@ -83,20 +84,20 @@ public void setRole(ClusterRole role) {
8384
this.role = role;
8485
}
8586

86-
public Node.Endpoint getControlEndpoint() {
87-
return controlEndpoint;
87+
public Node.Endpoint getControl() {
88+
return control;
8889
}
8990

90-
public void setControlEndpoint(Node.Endpoint controlEndpoint) {
91-
this.controlEndpoint = controlEndpoint;
91+
public void setControl(Node.Endpoint control) {
92+
this.control = control;
9293
}
9394

94-
public Node.Endpoint getTransactionEndpoint() {
95-
return transactionEndpoint;
95+
public Node.Endpoint getTransaction() {
96+
return transaction;
9697
}
9798

98-
public void setTransactionEndpoint(Node.Endpoint transactionEndpoint) {
99-
this.transactionEndpoint = transactionEndpoint;
99+
public void setTransaction(Node.Endpoint transaction) {
100+
this.transaction = transaction;
100101
}
101102

102103
public double getWeight() {
@@ -124,6 +125,14 @@ public void setTerm(long term) {
124125
this.term = term;
125126
}
126127

128+
public long getTimestamp() {
129+
return timestamp;
130+
}
131+
132+
public void setTimestamp(long timestamp) {
133+
this.timestamp = timestamp;
134+
}
135+
127136
public void addMetadata(String key, Object value) {
128137
this.metadata.put(key, value);
129138
}
@@ -134,7 +143,7 @@ public void setMetadata(Map<String, Object> metadata) {
134143

135144
@Override
136145
public int hashCode() {
137-
return Objects.hash(controlEndpoint, transactionEndpoint);
146+
return Objects.hash(control, transaction);
138147
}
139148

140149
@Override
@@ -146,7 +155,7 @@ public boolean equals(Object o) {
146155
return false;
147156
}
148157
Instance instance = (Instance) o;
149-
return Objects.equals(controlEndpoint, instance.controlEndpoint) && Objects.equals(transactionEndpoint, instance.transactionEndpoint);
158+
return Objects.equals(control, instance.control) && Objects.equals(transaction, instance.transaction);
150159
}
151160

152161

@@ -168,11 +177,12 @@ public Map<String, String> toMap() {
168177
resultMap.put("namespace", namespace);
169178
resultMap.put("clusterName", clusterName);
170179
resultMap.put("unit", unit);
171-
resultMap.put("controlEndpoint", controlEndpoint.toString());
172-
resultMap.put("transactionEndpoint", transactionEndpoint.toString());
180+
resultMap.put("control", control.toString());
181+
resultMap.put("transaction", transaction.toString());
173182
resultMap.put("weight", String.valueOf(weight));
174183
resultMap.put("healthy", String.valueOf(healthy));
175184
resultMap.put("term", String.valueOf(term));
185+
resultMap.put("timestamp",String.valueOf(timestamp));
176186
resultMap.put("metadata", mapToJsonString(metadata));
177187

178188
return resultMap;

common/src/main/java/org/apache/seata/common/metadata/namingserver/NamingServerNode.java

+24-14
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,26 @@ public class NamingServerNode extends Node {
2828
private boolean healthy = true;
2929
private long term;
3030

31+
public double getWeight() {
32+
return weight;
33+
}
34+
35+
public boolean isHealthy() {
36+
return healthy;
37+
}
38+
39+
public void setHealthy(boolean healthy) {
40+
this.healthy = healthy;
41+
}
42+
43+
public long getTerm() {
44+
return term;
45+
}
46+
47+
public void setTerm(long term) {
48+
this.term = term;
49+
}
50+
3151
@Override
3252
public int hashCode() {
3353
return Objects.hash(getControl(), getTransaction());
@@ -45,25 +65,15 @@ public boolean equals(Object o) {
4565
return Objects.equals(getControl(), node.getControl()) && Objects.equals(getTransaction(), node.getTransaction());
4666
}
4767

48-
public boolean isTotalEqual(Object obj) {
49-
if (this == obj) {
50-
return true;
51-
}
5268

53-
if (obj == null || getClass() != obj.getClass()) {
69+
public boolean isChanged(Object obj) {
70+
if (Objects.isNull(obj)) {
5471
return false;
5572
}
56-
5773
NamingServerNode otherNode = (NamingServerNode) obj;
5874

59-
// check each member variable
60-
return Objects.equals(getControl(), otherNode.getControl()) &&
61-
Objects.equals(getTransaction(), otherNode.getTransaction()) &&
62-
Double.compare(otherNode.weight, weight) == 0 &&
63-
healthy == otherNode.healthy &&
64-
Objects.equals(getRole(), otherNode.getRole()) &&
65-
term == otherNode.term &&
66-
Objects.equals(getMetadata(), otherNode.getMetadata());
75+
// other node is newer than me
76+
return otherNode.term > term;
6777
}
6878

6979
// convert to String

common/src/main/java/org/apache/seata/common/metadata/namingserver/Unit.java

+9-8
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class Unit {
2424

2525
private String unitName;
2626

27-
private List<Node> nodeList;
27+
private List<NamingServerNode> nodeList;
2828

2929
public String getUnitName() {
3030
return unitName;
@@ -34,11 +34,11 @@ public void setUnitName(String unitName) {
3434
this.unitName = unitName;
3535
}
3636

37-
public List<Node> getNamingInstanceList() {
37+
public List<NamingServerNode> getNamingInstanceList() {
3838
return nodeList;
3939
}
4040

41-
public void setNamingInstanceList(List<Node> nodeList) {
41+
public void setNamingInstanceList(List<NamingServerNode> nodeList) {
4242
this.nodeList = nodeList;
4343
}
4444

@@ -51,16 +51,17 @@ public void removeInstance(Node node) {
5151
/**
5252
* @param node node
5353
*/
54-
public void addInstance(NamingServerNode node) {
54+
public boolean addInstance(NamingServerNode node) {
5555
if (nodeList.contains(node)) {
56-
Node node1 = nodeList.get(nodeList.indexOf(node));
57-
if (node.isTotalEqual(node1)) {
58-
return;
59-
} else {
56+
NamingServerNode node1 = nodeList.get(nodeList.indexOf(node));
57+
if (node1.isChanged(node)) {
6058
nodeList.remove(node1);
59+
} else {
60+
return false;
6161
}
6262
}
6363
nodeList.add(node);
64+
return true;
6465

6566
}
6667

0 commit comments

Comments
 (0)