Skip to content

Commit

Permalink
Merge branch '2.x' into pb-dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
slievrly authored Sep 2, 2024
2 parents 7ab92ca + 91eb505 commit 08f6f36
Show file tree
Hide file tree
Showing 19 changed files with 711 additions and 132 deletions.
4 changes: 4 additions & 0 deletions changes/en-us/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#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
- [[#6766](https://github.com/apache/incubator-seata/pull/6766)] add TCC three-phase hooks

### bugfix:
- [[#6592](https://github.com/apache/incubator-seata/pull/6592)] fix @Async annotation not working in ClusterWatcherManager
Expand Down Expand Up @@ -60,6 +61,7 @@ Add changes here for all PR submitted to the 2.x branch.
- [[#6743](https://github.com/apache/incubator-seata/pull/6743)] upgrade npmjs version in saga
- [[#6746](https://github.com/apache/incubator-seata/pull/6746)] optimize compatible dependencies
- [[#6745](https://github.com/apache/incubator-seata/pull/6745)] fix node-gyp build error on arm64 and macos
- [[#6749](https://github.com/apache/incubator-seata/pull/6749)] optimize WebSecurityConfig csrf
- [[#6748](https://github.com/apache/incubator-seata/pull/6748)] optimize ConsistentHashLoadBalance Algorithm
- [[#6747](https://github.com/apache/incubator-seata/pull/6747)] optimize fastjson deserialization
- [[#6755](https://github.com/apache/incubator-seata/pull/6755)] optimize namingserver code logic
Expand All @@ -72,8 +74,10 @@ 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
- [[#6794](https://github.com/apache/incubator-seata/pull/6794)] optimize NacosMockTest UT case


### refactor:

### security:
Expand Down
4 changes: 4 additions & 0 deletions changes/zh-cn/2.x.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- [[#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
- [[#6766](https://github.com/apache/incubator-seata/pull/6766)] 添加tcc三阶段钩子函数,方便用户拓展业务逻辑(比如多数据源情况下可以用于切换数据源)

### bugfix:
- [[#6592](https://github.com/apache/incubator-seata/pull/6592)] fix @Async注解ClusterWatcherManager中不生效的问题
Expand Down Expand Up @@ -61,6 +62,7 @@
- [[#6743](https://github.com/apache/incubator-seata/pull/6743)] 升级saga模块npmjs版本
- [[#6746](https://github.com/apache/incubator-seata/pull/6746)] 优化 compatible 模块依赖
- [[#6745](https://github.com/apache/incubator-seata/pull/6745)] 修复 node-gyp 在 arm64 和 macos 构建失败问题
- [[#6749](https://github.com/apache/incubator-seata/pull/6749)] 优化 WebSecurityConfig csrf 处理
- [[#6748](https://github.com/apache/incubator-seata/pull/6748)] 优化 ConsistentHashLoadBalance 算法
- [[#6747](https://github.com/apache/incubator-seata/pull/6747)] 优化 fastjson 反序列化
- [[#6755](https://github.com/apache/incubator-seata/pull/6755)] 优化namingserver代码逻辑
Expand All @@ -74,6 +76,8 @@
- [[#6783](https://github.com/apache/incubator-seata/pull/6783)] 将server事务分组修改接口改为/vgroup/v1
- [[#6793](https://github.com/apache/incubator-seata/pull/6793)] 修复 npmjs 依赖冲突问题
- [[#6794](https://github.com/apache/incubator-seata/pull/6794)] 优化 NacosMockTest 单测问题
- [[#6793](https://github.com/apache/incubator-seata/pull/6795)] 独立server的meta信息初始化逻辑


### refactor:

Expand Down
29 changes: 8 additions & 21 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 @@ -121,28 +124,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++;
public String toJsonString(ObjectMapper objectMapper) {
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 @@ -42,7 +42,6 @@ public class Instance {
private ClusterRole role = ClusterRole.MEMBER;
private Map<String, Object> metadata = new HashMap<>();


private Instance() {
}

Expand Down Expand Up @@ -162,9 +161,7 @@ public boolean equals(Object o) {
}


// Recursively convert metadata to JSON
public String toJsonString() {
ObjectMapper objectMapper = new ObjectMapper();
public String toJsonString(ObjectMapper objectMapper) {
try {
return objectMapper.writeValueAsString(this);
} catch (JsonProcessingException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
*/
package org.apache.seata.common.metadata.namingserver;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.seata.common.metadata.Node;

import java.util.Objects;
Expand Down Expand Up @@ -85,16 +83,6 @@ public boolean isChanged(Object obj) {
return otherNode.term > term;
}

// convert to String
public String toJsonString() {
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.writeValueAsString(this);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}

public void setWeight(double weight) {
this.weight = weight;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import static org.junit.jupiter.api.Assertions.*;

class InstanceTest {
private final ObjectMapper objectMapper = new ObjectMapper();

@Test
void toJsonString() throws JsonProcessingException {
Expand All @@ -39,7 +40,6 @@ void toJsonString() throws JsonProcessingException {
instance.setMetadata(map);
instance.setControl(new Node.Endpoint("1.1.1.1",888));
instance.setTransaction(new Node.Endpoint("2.2.2.2",999));
System.out.println(instance.toJsonString());
assertEquals(instance.toJsonString(),objectMapper.writeValueAsString(instance));
assertEquals(instance.toJsonString(objectMapper),objectMapper.writeValueAsString(instance));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import static org.junit.jupiter.api.Assertions.*;

class NamingServerNodeTest {
private ObjectMapper objectMapper = new ObjectMapper();

@Test
void toJsonString() throws JsonProcessingException {
Expand All @@ -39,8 +40,7 @@ void toJsonString() throws JsonProcessingException {
node.setGroup("group");
node.setControl(new Node.Endpoint("1.1.1.1",888));
node.setTransaction(new Node.Endpoint("2.2.2.2",999));
System.out.println(node.toJsonString());
assertEquals(node.toJsonString(),objectMapper.writeValueAsString(node));
assertEquals(node.toJsonString(objectMapper),objectMapper.writeValueAsString(node));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,8 @@ protected void configure(HttpSecurity http) throws Exception {
// custom token authorize exception handler
.exceptionHandling().authenticationEntryPoint(unauthorizedHandler).and()
// since we use jwt, session is not necessary
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
// since we use jwt, csrf is not necessary
.csrf().disable();
.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).disable();
// don't disable csrf, jwt may be implemented based on cookies
http.addFilterBefore(new JwtAuthenticationTokenFilter(tokenProvider),
UsernamePasswordAuthenticationFilter.class);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public void doRegister(Instance instance, List<String> urlList) {
String namespace = instance.getNamespace();
String clusterName = instance.getClusterName();
String unit = instance.getUnit();
String jsonBody = instance.toJsonString();
String jsonBody = instance.toJsonString(OBJECT_MAPPER);
String params = "namespace=" + namespace + "&clusterName=" + clusterName + "&unit=" + unit;
url += params;
Map<String, String> header = new HashMap<>();
Expand Down Expand Up @@ -206,7 +206,7 @@ public void unregister(InetSocketAddress address) {
for (String urlSuffix : getNamingAddrs()) {
String url = HTTP_PREFIX + urlSuffix + "/naming/v1/unregister?";
String unit = instance.getUnit();
String jsonBody = instance.toJsonString();
String jsonBody = instance.toJsonString(OBJECT_MAPPER);
String params = "unit=" + unit;
params = params + "&clusterName=" + instance.getClusterName();
params = params + "&namespace=" + instance.getNamespace();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* 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.integration.tx.api.fence.hook;


import org.apache.seata.rm.tcc.api.BusinessActionContext;

public interface TccHook {

/**
* before tcc prepare
*/
void beforeTccPrepare(String xid, Long branchId, String actionName, BusinessActionContext context);

/**
* after tcc prepare
*/
void afterTccPrepare(String xid, Long branchId, String actionName, BusinessActionContext context);

/**
* before tcc commit
*/
void beforeTccCommit(String xid, Long branchId, String actionName, BusinessActionContext context);

/**
* after tcc commit
*/
void afterTccCommit(String xid, Long branchId, String actionName, BusinessActionContext context);

/**
* before tcc rollback
*/
void beforeTccRollback(String xid, Long branchId, String actionName, BusinessActionContext context);

/**
* after tcc rollback
*/
void afterTccRollback(String xid, Long branchId, String actionName, BusinessActionContext context);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* 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.integration.tx.api.fence.hook;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public final class TccHookManager {
private static final Logger LOGGER = LoggerFactory.getLogger(TccHookManager.class);

private TccHookManager() {

}

private static final List<TccHook> TCC_HOOKS = new CopyOnWriteArrayList<>();
// Cache unmodifiable lists
private volatile static List<TccHook> CACHED_UNMODIFIABLE_HOOKS = null;

/**
* get the hooks
* @return tccHook list
*/
public static List<TccHook> getHooks() {
if (CACHED_UNMODIFIABLE_HOOKS == null) {
synchronized (TccHookManager.class) {
if (CACHED_UNMODIFIABLE_HOOKS == null) {
CACHED_UNMODIFIABLE_HOOKS = Collections.unmodifiableList(TCC_HOOKS);
}
}
}
return CACHED_UNMODIFIABLE_HOOKS;
}

/**
* add new hook
* @param tccHook tccHook
*/
public static void registerHook(TccHook tccHook) {
if (tccHook == null) {
throw new NullPointerException("tccHook must not be null");
}
TCC_HOOKS.add(tccHook);
CACHED_UNMODIFIABLE_HOOKS = null;
LOGGER.info("TccHook registered succeeded! TccHooks size: {}", TCC_HOOKS.size());
}

/**
* clear hooks
*/
public static void clear() {
TCC_HOOKS.clear();
CACHED_UNMODIFIABLE_HOOKS = null;
LOGGER.info("All TccHooks have been cleared.");
}
}
Loading

0 comments on commit 08f6f36

Please sign in to comment.