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

optimize: optimize the initialization logic for server meta #6795

Merged
merged 44 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from 38 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
50c77dc
add signature
ggbocoder Jan 2, 2024
b36f5b2
Merge remote-tracking branch 'upstream/2.x' into 2.x
ggbocoder Jan 20, 2024
e15537d
Merge remote-tracking branch 'upstream/2.x' into 2.x
ggbocoder Jan 25, 2024
85398ec
Merge remote-tracking branch 'upstream/2.x' into 2.x
ggbocoder Jan 31, 2024
e049125
Merge remote-tracking branch 'upstream/2.x' into 2.x
ggbocoder Mar 7, 2024
2939f59
Merge remote-tracking branch 'upstream/2.x' into 2.x
ggbocoder May 12, 2024
ebeda3a
Merge remote-tracking branch 'upstream/2.x' into 2.x
ggbocoder Jul 5, 2024
f2f9509
Merge branch '2.x' of https://github.com/seata/seata into 2.x
ggbocoder Jul 7, 2024
eed8785
Merge branch '2.x' of https://github.com/seata/seata into 2.x
ggbocoder Jul 15, 2024
5147f84
Merge branch '2.x' of https://github.com/seata/seata into 2.x
ggbocoder Jul 28, 2024
f6f4293
Merge branch '2.x' of https://github.com/seata/seata into 2.x
ggbocoder Aug 3, 2024
858aabd
Merge branch '2.x' of https://github.com/seata/seata into 2.x
ggbocoder Aug 14, 2024
f90f421
Merge branch '2.x' of https://github.com/seata/seata into 2.x
ggbocoder Aug 17, 2024
cf97436
fix
ggbocoder Aug 19, 2024
888aaab
Merge remote-tracking branch 'upstream/2.x' into optimize/optimize_na…
ggbocoder Aug 21, 2024
5368f94
fix
ggbocoder Aug 21, 2024
fcdb316
fix
ggbocoder Aug 21, 2024
a5d156b
fix code style
ggbocoder Aug 23, 2024
5cf5d78
fix
ggbocoder Aug 23, 2024
8b13293
fix UT
ggbocoder Aug 23, 2024
dcba549
fix caffeine version
ggbocoder Aug 23, 2024
af275d9
fix
ggbocoder Aug 23, 2024
68a90f5
fix
ggbocoder Aug 23, 2024
f180ed0
fix
ggbocoder Aug 23, 2024
e0402bf
Merge remote-tracking branch 'upstream/2.x' into optimize/optimize_na…
ggbocoder Aug 23, 2024
cca510e
fix
ggbocoder Aug 23, 2024
a81ac44
fix
ggbocoder Aug 23, 2024
65152ed
fix
ggbocoder Aug 23, 2024
5fd36b4
fix
ggbocoder Aug 24, 2024
135858f
Merge remote-tracking branch 'upstream/2.x' into optimize/optimize_na…
ggbocoder Aug 24, 2024
3e556e9
fix
ggbocoder Aug 24, 2024
6fdd35d
fix
ggbocoder Aug 24, 2024
a4a741f
Update script/server/db/mysql.sql
funky-eyes Aug 24, 2024
45dd32f
Merge remote-tracking branch 'upstream/2.x' into optimize/optimize_na…
ggbocoder Aug 28, 2024
6274c82
optimize some problems
ggbocoder Aug 29, 2024
9535b77
Merge remote-tracking branch 'origin/optimize/optimize_namingserver' …
ggbocoder Aug 29, 2024
a31467c
add signature
ggbocoder Aug 29, 2024
48b4b92
fix
ggbocoder Aug 29, 2024
e9a575b
opt
ggbocoder Aug 29, 2024
f5a4fd2
fix style
ggbocoder Aug 30, 2024
65af129
fix
ggbocoder Aug 31, 2024
3569531
Merge remote-tracking branch 'upstream/2.x' into optimize/optimize_na…
ggbocoder Aug 31, 2024
ef6e6a7
Merge branch '2.x' into optimize/optimize_namingserver
funky-eyes Sep 2, 2024
9a09fd7
Merge branch '2.x' into optimize/optimize_namingserver
funky-eyes Sep 2, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6787](https://github.com/apache/incubator-seata/pull/6787)] upgrade elliptic to 6.5.7
- [[#6783](https://github.com/apache/incubator-seata/pull/6783)] rename the server naming/v1 api to vgroup/v1
- [[#6793](https://github.com/apache/incubator-seata/pull/6793)] fix npmjs conflicts

- [[#6793](https://github.com/apache/incubator-seata/pull/6795)] optimize the initialization logic for server meta
### refactor:

### security:
Expand Down
2 changes: 1 addition & 1 deletion changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
- [[#6787](https://github.com/apache/incubator-seata/pull/6787)] 升级 elliptic 至 6.5.7 版本
- [[#6783](https://github.com/apache/incubator-seata/pull/6783)] 将server事务分组修改接口改为/vgroup/v1
- [[#6793](https://github.com/apache/incubator-seata/pull/6793)] 修复 npmjs 依赖冲突问题

- [[#6793](https://github.com/apache/incubator-seata/pull/6795)] 独立server的meta信息初始化逻辑

### refactor:

Expand Down
28 changes: 8 additions & 20 deletions common/src/main/java/org/apache/seata/common/metadata/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
*/
package org.apache.seata.common.metadata;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -122,27 +125,12 @@ public boolean equals(Object o) {

// 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++;
ObjectMapper objectMapper = new ObjectMapper();
funky-eyes marked this conversation as resolved.
Show resolved Hide resolved
try {
return objectMapper.writeValueAsString(this);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}

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

public static class Endpoint {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,12 +125,6 @@ public Cluster getClusterByUnits(Set<String> unitNames) {
}

public boolean registerInstance(NamingServerNode instance, String unitName) {
// refresh node weight
Object weightValue = instance.getMetadata().get("weight");
if (weightValue != null) {
instance.setWeight(Double.parseDouble(String.valueOf(weightValue)));
instance.getMetadata().remove("weight");
}
Unit currentUnit = unitData.computeIfAbsent(unitName, value -> {
Unit unit = new Unit();
List<NamingServerNode> instances = new CopyOnWriteArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public NamingManager() {
@PostConstruct
public void init() {
this.vGroupMap = Caffeine.newBuilder()
.expireAfterAccess(heartbeatTimeThreshold, TimeUnit.MILLISECONDS) // expired time
.expireAfterAccess(heartbeatTimeThreshold + 1000, TimeUnit.MILLISECONDS) // expired time
.maximumSize(Integer.MAX_VALUE)
.removalListener(new RemovalListener<Object, Object>() {

Expand Down
73 changes: 4 additions & 69 deletions server/src/main/java/org/apache/seata/server/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,13 @@
*/
package org.apache.seata.server;

import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import javax.annotation.Resource;
import org.apache.seata.common.XID;
import org.apache.seata.common.holder.ObjectHolder;
import org.apache.seata.common.metadata.Node;
import org.apache.seata.common.metadata.namingserver.Instance;
import org.apache.seata.common.thread.NamedThreadFactory;
import org.apache.seata.common.util.NetUtil;
import org.apache.seata.common.util.StringUtils;
Expand All @@ -37,28 +31,21 @@
import org.apache.seata.core.rpc.netty.NettyRemotingServer;
import org.apache.seata.core.rpc.netty.NettyServerConfig;
import org.apache.seata.server.coordinator.DefaultCoordinator;
import org.apache.seata.server.instance.ServerInstance;
import org.apache.seata.server.lock.LockerManagerFactory;
import org.apache.seata.server.metrics.MetricsManager;
import org.apache.seata.server.session.SessionHolder;
import org.apache.seata.server.store.StoreConfig;
import org.apache.seata.server.store.VGroupMappingStoreManager;
import org.apache.seata.spring.boot.autoconfigure.properties.registry.RegistryNamingServerProperties;
import org.apache.seata.spring.boot.autoconfigure.properties.registry.RegistryProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.context.ApplicationListener;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.stereotype.Component;
import org.springframework.web.context.support.GenericWebApplicationContext;


import static org.apache.seata.common.ConfigurationKeys.META_PREFIX;
import static org.apache.seata.common.ConfigurationKeys.NAMING_SERVER;
import static org.apache.seata.common.Constants.OBJECT_KEY_SPRING_APPLICATION_CONTEXT;
import static org.apache.seata.common.Constants.OBJECT_KEY_SPRING_CONFIGURABLE_ENVIRONMENT;
import static org.apache.seata.spring.boot.autoconfigure.StarterConstants.REGEX_SPLIT_CHAR;
import static org.apache.seata.spring.boot.autoconfigure.StarterConstants.REGISTRY_PREFERED_NETWORKS;

Expand All @@ -69,66 +56,14 @@
public class Server {
private static final Logger LOGGER = LoggerFactory.getLogger(Server.class);

protected static volatile ScheduledExecutorService EXECUTOR_SERVICE;

@Resource
RegistryNamingServerProperties registryNamingServerProperties;

@Resource
RegistryProperties registryProperties;

public void metadataInit() {
VGroupMappingStoreManager vGroupMappingStoreManager = SessionHolder.getRootVGroupMappingManager();
if (StringUtils.equals(registryProperties.getType(), NAMING_SERVER)) {
EXECUTOR_SERVICE = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory("scheduledExcuter", 1, true));
ConfigurableEnvironment environment = (ConfigurableEnvironment) ObjectHolder.INSTANCE.getObject(OBJECT_KEY_SPRING_CONFIGURABLE_ENVIRONMENT);

// load node properties
Instance instance = Instance.getInstance();
// load namespace
String namespace = registryNamingServerProperties.getNamespace();
instance.setNamespace(namespace);
// load cluster name
String clusterName = registryNamingServerProperties.getCluster();
instance.setClusterName(clusterName);

// load cluster type
String clusterType = String.valueOf(StoreConfig.getSessionMode());
instance.addMetadata("cluster-type", "raft".equals(clusterType) ? clusterType : "default");

// load unit name
instance.setUnit(String.valueOf(UUID.randomUUID()));

instance.setTerm(System.currentTimeMillis());

// load node Endpoint
instance.setControl(new Node.Endpoint(NetUtil.getLocalIp(), Integer.parseInt(Objects.requireNonNull(environment.getProperty("server.port"))), "http"));

// load metadata
for (PropertySource<?> propertySource : environment.getPropertySources()) {
if (propertySource instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) propertySource;
for (String propertyName : enumerablePropertySource.getPropertyNames()) {
if (propertyName.startsWith(META_PREFIX)) {
instance.addMetadata(propertyName.substring(META_PREFIX.length()), enumerablePropertySource.getProperty(propertyName));
}
}
}
}
// load vgroup mapping relationship
instance.addMetadata("vGroup", vGroupMappingStoreManager.loadVGroups());

EXECUTOR_SERVICE.scheduleAtFixedRate(() -> {
try {
vGroupMappingStoreManager.notifyMapping();
} catch (Exception e) {
LOGGER.error("Naming server register Exception", e);
}
}, registryNamingServerProperties.getHeartbeatPeriod(), registryNamingServerProperties.getHeartbeatPeriod(), TimeUnit.MILLISECONDS);
ServerRunner.addDisposable(EXECUTOR_SERVICE::shutdown);
}
}

@Resource
ServerInstance serverInstance;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

registryNamingServerProperties 和 registryProperties在server里就不需要了吧?
registryNamingServerProperties and registryProperties are not needed in the server, right?


/**
* The entry point of application.
Expand Down Expand Up @@ -179,7 +114,7 @@ public void start(String[] args) {
coordinator.init();
nettyRemotingServer.setHandler(coordinator);

metadataInit();
serverInstance.serverInstanceInit();
// let ServerRunner do destroy instead ShutdownHook, see https://github.com/seata/seata/issues/4028
ServerRunner.addDisposable(coordinator);
nettyRemotingServer.init();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* 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.server.instance;

import org.apache.seata.common.holder.ObjectHolder;
import org.apache.seata.common.metadata.Node;
import org.apache.seata.common.metadata.namingserver.Instance;
import org.apache.seata.common.thread.NamedThreadFactory;
import org.apache.seata.common.util.NetUtil;
import org.apache.seata.common.util.StringUtils;
import org.apache.seata.server.Server;
import org.apache.seata.server.ServerRunner;
import org.apache.seata.server.session.SessionHolder;
import org.apache.seata.server.store.StoreConfig;
import org.apache.seata.server.store.VGroupMappingStoreManager;
import org.apache.seata.spring.boot.autoconfigure.properties.registry.RegistryNamingServerProperties;
import org.apache.seata.spring.boot.autoconfigure.properties.registry.RegistryProperties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.EnumerablePropertySource;
import org.springframework.core.env.PropertySource;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import static org.apache.seata.common.ConfigurationKeys.META_PREFIX;
import static org.apache.seata.common.ConfigurationKeys.NAMING_SERVER;
import static org.apache.seata.common.Constants.OBJECT_KEY_SPRING_CONFIGURABLE_ENVIRONMENT;


@Component("serverInstance")
public class ServerInstance {
@Resource
private RegistryProperties registryProperties;

protected static volatile ScheduledExecutorService EXECUTOR_SERVICE;

@Resource
private RegistryNamingServerProperties registryNamingServerProperties;

private static final Logger LOGGER = LoggerFactory.getLogger(Server.class);

public void serverInstanceInit() {
VGroupMappingStoreManager vGroupMappingStoreManager = SessionHolder.getRootVGroupMappingManager();
if (StringUtils.equals(registryProperties.getType(), NAMING_SERVER)) {
EXECUTOR_SERVICE = new ScheduledThreadPoolExecutor(1, new NamedThreadFactory("scheduledExcuter", 1, true));
ConfigurableEnvironment environment = (ConfigurableEnvironment) ObjectHolder.INSTANCE.getObject(OBJECT_KEY_SPRING_CONFIGURABLE_ENVIRONMENT);

// load node properties
Instance instance = Instance.getInstance();
// load namespace
String namespace = registryNamingServerProperties.getNamespace();
instance.setNamespace(namespace);
// load cluster name
String clusterName = registryNamingServerProperties.getCluster();
instance.setClusterName(clusterName);

// load cluster type
String clusterType = String.valueOf(StoreConfig.getSessionMode());
instance.addMetadata("cluster-type", "raft".equals(clusterType) ? clusterType : "default");

// load unit name
instance.setUnit(String.valueOf(UUID.randomUUID()));

instance.setTerm(System.currentTimeMillis());

// load node Endpoint
instance.setControl(new Node.Endpoint(NetUtil.getLocalIp(), Integer.parseInt(Objects.requireNonNull(environment.getProperty("server.port"))), "http"));

// load metadata
for (PropertySource<?> propertySource : environment.getPropertySources()) {
if (propertySource instanceof EnumerablePropertySource) {
EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) propertySource;
for (String propertyName : enumerablePropertySource.getPropertyNames()) {
if (propertyName.startsWith(META_PREFIX)) {
instance.addMetadata(propertyName.substring(META_PREFIX.length()), enumerablePropertySource.getProperty(propertyName));
}
}
}
}
// load vgroup mapping relationship
instance.addMetadata("vGroup", vGroupMappingStoreManager.loadVGroups());

EXECUTOR_SERVICE.scheduleAtFixedRate(() -> {
try {
vGroupMappingStoreManager.notifyMapping();
} catch (Exception e) {
LOGGER.error("Naming server register Exception", e);
}
}, registryNamingServerProperties.getHeartbeatPeriod(), registryNamingServerProperties.getHeartbeatPeriod(), TimeUnit.MILLISECONDS);
ServerRunner.addDisposable(EXECUTOR_SERVICE::shutdown);
}
}
}
Loading