Skip to content

Commit

Permalink
feature: init namingserver client (#6536)
Browse files Browse the repository at this point in the history
  • Loading branch information
ggbocoder authored Aug 13, 2024
1 parent fff45ce commit 1171548
Show file tree
Hide file tree
Showing 28 changed files with 1,413 additions and 96 deletions.
5 changes: 5 additions & 0 deletions all/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@
<artifactId>seata-discovery-etcd3</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.seata</groupId>
<artifactId>seata-discovery-namingserver</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.seata</groupId>
<artifactId>seata-brpc</artifactId>
Expand Down
5 changes: 5 additions & 0 deletions bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@
<artifactId>seata-discovery-zk</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.seata</groupId>
<artifactId>seata-discovery-namingserver</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.seata</groupId>
<artifactId>seata-discovery-redis</artifactId>
Expand Down
1 change: 1 addition & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ Add changes here for all PR submitted to the 2.x branch.
<!-- Please add the `changes` to the following location(feature/bugfix/optimize/test) based on the type of PR -->

### feature:
- [[#6536](https://github.com/apache/incubator-seata/pull/6536)] support naming server client
- [[#6226](https://github.com/apache/incubator-seata/pull/6226)] multi-version seata protocol support
- [[#6537](https://github.com/apache/incubator-seata/pull/6537)] support Namingserver
- [[#6538](https://github.com/apache/incubator-seata/pull/6538)] Integration of naming server on the Seata server side
Expand Down
1 change: 1 addition & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
<!-- 请根据PR的类型添加 `变更记录` 到以下对应位置(feature/bugfix/optimize/test) 下 -->

### feature:
- [[#6536](https://github.com/apache/incubator-seata/pull/6536)] 支持 naming server客户端
- [[#6226](https://github.com/apache/incubator-seata/pull/6226)] 支持seata私有协议多版本兼容
- [[#6537](https://github.com/apache/incubator-seata/pull/6537)] 支持 Namingserver
- [[#6538](https://github.com/apache/incubator-seata/pull/6538)] seata server端集成naming server
Expand Down
1 change: 1 addition & 0 deletions common/src/main/java/org/apache/seata/common/XID.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

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


/**
* The type Xid.
*
Expand Down
67 changes: 67 additions & 0 deletions common/src/main/java/org/apache/seata/common/metadata/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;


public class Node {
Expand All @@ -28,6 +29,10 @@ public class Node {

private Endpoint internal;

private double weight = 1.0;
private boolean healthy = true;
private long timeStamp;

private String group;
private ClusterRole role = ClusterRole.MEMBER;

Expand Down Expand Up @@ -97,6 +102,49 @@ public void setInternal(Endpoint internal) {
this.internal = internal;
}

@Override
public int hashCode() {
return Objects.hash(control, transaction);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Node node = (Node) o;
return Objects.equals(control, node.control) && Objects.equals(transaction, node.transaction);
}


// convert to String
public String toJsonString() {
StringBuilder sb = new StringBuilder();
sb.append("{");
sb.append("\"controlEndpoint\": ").append(control.toString()).append(", ");
sb.append("\"transactionEndpoint\": ").append(transaction.toString()).append(", ");
sb.append("\"weight\": ").append(weight).append(", ");
sb.append("\"healthy\": ").append(healthy).append(", ");
sb.append("\"timeStamp\": ").append(timeStamp).append(", ");
sb.append("\"metadata\": {");

// handle metadata k-v map
int i = 0;
for (Map.Entry<String, Object> entry : metadata.entrySet()) {
if (i > 0) {
sb.append(", ");
}
sb.append("\"").append(entry.getKey()).append("\": \"").append(entry.getValue()).append("\"");
i++;
}

sb.append("}}");
return sb.toString();
}

public static class Endpoint {

private String host;
Expand Down Expand Up @@ -136,6 +184,25 @@ public String createAddress() {
return host + ":" + port;
}

@Override
public int hashCode() {
return Objects.hash(host,port,protocol);
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Endpoint endpoint = (Endpoint) o;
return Objects.equals(endpoint.host,this.host)
&& Objects.equals(endpoint.port,this.port)
&& Objects.equals(endpoint.protocol,this.protocol);
}

@Override
public String toString() {
return "Endpoint{" + "host='" + host + '\'' + ", port=" + port + '}';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,12 @@ public class Instance {
private String namespace;
private String clusterName;
private String unit;
private Node.Endpoint controlEndpoint = new Node.Endpoint();
private Node.Endpoint transactionEndpoint = new Node.Endpoint();
private Node.Endpoint control = new Node.Endpoint();
private Node.Endpoint transaction = new Node.Endpoint();
private double weight = 1.0;
private boolean healthy = true;
private long term;
private long timestamp;
private ClusterRole role = ClusterRole.MEMBER;
private Map<String, Object> metadata = new HashMap<>();

Expand Down Expand Up @@ -83,20 +84,20 @@ public void setRole(ClusterRole role) {
this.role = role;
}

public Node.Endpoint getControlEndpoint() {
return controlEndpoint;
public Node.Endpoint getControl() {
return control;
}

public void setControlEndpoint(Node.Endpoint controlEndpoint) {
this.controlEndpoint = controlEndpoint;
public void setControl(Node.Endpoint control) {
this.control = control;
}

public Node.Endpoint getTransactionEndpoint() {
return transactionEndpoint;
public Node.Endpoint getTransaction() {
return transaction;
}

public void setTransactionEndpoint(Node.Endpoint transactionEndpoint) {
this.transactionEndpoint = transactionEndpoint;
public void setTransaction(Node.Endpoint transaction) {
this.transaction = transaction;
}

public double getWeight() {
Expand Down Expand Up @@ -124,6 +125,14 @@ public void setTerm(long term) {
this.term = term;
}

public long getTimestamp() {
return timestamp;
}

public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}

public void addMetadata(String key, Object value) {
this.metadata.put(key, value);
}
Expand All @@ -134,7 +143,7 @@ public void setMetadata(Map<String, Object> metadata) {

@Override
public int hashCode() {
return Objects.hash(controlEndpoint, transactionEndpoint);
return Objects.hash(control, transaction);
}

@Override
Expand All @@ -146,7 +155,7 @@ public boolean equals(Object o) {
return false;
}
Instance instance = (Instance) o;
return Objects.equals(controlEndpoint, instance.controlEndpoint) && Objects.equals(transactionEndpoint, instance.transactionEndpoint);
return Objects.equals(control, instance.control) && Objects.equals(transaction, instance.transaction);
}


Expand All @@ -168,11 +177,12 @@ public Map<String, String> toMap() {
resultMap.put("namespace", namespace);
resultMap.put("clusterName", clusterName);
resultMap.put("unit", unit);
resultMap.put("controlEndpoint", controlEndpoint.toString());
resultMap.put("transactionEndpoint", transactionEndpoint.toString());
resultMap.put("control", control.toString());
resultMap.put("transaction", transaction.toString());
resultMap.put("weight", String.valueOf(weight));
resultMap.put("healthy", String.valueOf(healthy));
resultMap.put("term", String.valueOf(term));
resultMap.put("timestamp",String.valueOf(timestamp));
resultMap.put("metadata", mapToJsonString(metadata));

return resultMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,26 @@ public class NamingServerNode extends Node {
private boolean healthy = true;
private long term;

public double getWeight() {
return weight;
}

public boolean isHealthy() {
return healthy;
}

public void setHealthy(boolean healthy) {
this.healthy = healthy;
}

public long getTerm() {
return term;
}

public void setTerm(long term) {
this.term = term;
}

@Override
public int hashCode() {
return Objects.hash(getControl(), getTransaction());
Expand All @@ -45,25 +65,15 @@ public boolean equals(Object o) {
return Objects.equals(getControl(), node.getControl()) && Objects.equals(getTransaction(), node.getTransaction());
}

public boolean isTotalEqual(Object obj) {
if (this == obj) {
return true;
}

if (obj == null || getClass() != obj.getClass()) {
public boolean isChanged(Object obj) {
if (Objects.isNull(obj)) {
return false;
}

NamingServerNode otherNode = (NamingServerNode) obj;

// check each member variable
return Objects.equals(getControl(), otherNode.getControl()) &&
Objects.equals(getTransaction(), otherNode.getTransaction()) &&
Double.compare(otherNode.weight, weight) == 0 &&
healthy == otherNode.healthy &&
Objects.equals(getRole(), otherNode.getRole()) &&
term == otherNode.term &&
Objects.equals(getMetadata(), otherNode.getMetadata());
// other node is newer than me
return otherNode.term > term;
}

// convert to String
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public class Unit {

private String unitName;

private List<Node> nodeList;
private List<NamingServerNode> nodeList;

public String getUnitName() {
return unitName;
Expand All @@ -34,11 +34,11 @@ public void setUnitName(String unitName) {
this.unitName = unitName;
}

public List<Node> getNamingInstanceList() {
public List<NamingServerNode> getNamingInstanceList() {
return nodeList;
}

public void setNamingInstanceList(List<Node> nodeList) {
public void setNamingInstanceList(List<NamingServerNode> nodeList) {
this.nodeList = nodeList;
}

Expand All @@ -51,16 +51,17 @@ public void removeInstance(Node node) {
/**
* @param node node
*/
public void addInstance(NamingServerNode node) {
public boolean addInstance(NamingServerNode node) {
if (nodeList.contains(node)) {
Node node1 = nodeList.get(nodeList.indexOf(node));
if (node.isTotalEqual(node1)) {
return;
} else {
NamingServerNode node1 = nodeList.get(nodeList.indexOf(node));
if (node1.isChanged(node)) {
nodeList.remove(node1);
} else {
return false;
}
}
nodeList.add(node);
return true;

}

Expand Down
Loading

0 comments on commit 1171548

Please sign in to comment.