Skip to content
Draft
27 changes: 27 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 @@ -16,6 +16,7 @@
*/
package org.apache.seata.common.metadata;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.seata.common.exception.ParseEndpointException;
Expand Down Expand Up @@ -99,6 +100,32 @@ public void setVersion(String version) {
this.version = version;
}

@JsonIgnore
public boolean isHttp2Supported() {
String baseVersion = "2.6.0";
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we use the Version class to compare versions instead?


if (version == null || version.isEmpty()) {
return false;
}

String[] current = version.split("\\.");
String[] base = baseVersion.split("\\.");

int len = Math.max(current.length, base.length);
for (int i = 0; i < len; i++) {
int cur = i < current.length ? Integer.parseInt(current[i]) : 0;
int bas = i < base.length ? Integer.parseInt(base[i]) : 0;

if (cur > bas) {
return true;
}
if (cur < bas) {
return false;
}
}
return true;
}

public Endpoint getInternal() {
return internal;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class Http5ClientUtil {
public class Http2ClientUtil {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we name it HttpClientUtil? It handles more than just HTTP/2 requests.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think a factory class can be provided, for example, called HttpClientFactory, to obtain the specific client instance through the getClient method: For instance, if it is detected that http2 is available in the business system, use Http2ClientUtil; otherwise, use the original HttpClientUtil. However, this requires unifying the current apis of these two tool classes

Copy link
Contributor

Choose a reason for hiding this comment

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

However, recently I have been thinking about one point: whether the http2 function is supported or not not only depends on the business system's reliance on jar packages, but also on whether the TC end supports the http2 protocol. Currently, I'm considering whether the judgment logic for the latter can be directly placed in the getClient method of HttpClientFactory for judgment. If so, such an implementation would be more elegant. This is what I now solve this issue (#7561)

Copy link
Member Author

Choose a reason for hiding this comment

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

At first, what I considered was abstraction through an interface.
I thought it would be good to dynamically select the class at runtime via abstraction, but since the method return values of the http2 and http utility classes are different, that turned out to be impossible.

So for now, I also think a factory class is the best option. However, unifying the APIs of the two utility classes seems difficult at this point due to differences in their internal logic.

It might be a good idea to revisit this after you finish the work on #7561.
In the meantime, I’ll try to work on this PR over the weekend and wrap it up as quickly as possible.


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

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

Expand All @@ -50,7 +50,7 @@ public class Http5ClientUtil {
public static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json");
public static final MediaType MEDIA_TYPE_FORM_URLENCODED = MediaType.parse("application/x-www-form-urlencoded");

public static void doPostHttp(
public static void doPost(
String url, Map<String, String> params, Map<String, String> headers, HttpCallback<Response> callback) {
try {
Headers.Builder headerBuilder = new Headers.Builder();
Expand All @@ -75,8 +75,7 @@ public static void doPostHttp(
}
}

public static void doPostHttp(
String url, String body, Map<String, String> headers, HttpCallback<Response> callback) {
public static void doPost(String url, String body, Map<String, String> headers, HttpCallback<Response> callback) {
Headers.Builder headerBuilder = new Headers.Builder();
if (headers != null) {
headers.forEach(headerBuilder::add);
Expand All @@ -93,7 +92,7 @@ public static void doPostHttp(
executeAsync(HTTP_CLIENT, request, callback);
}

public static void doGetHttp(
public static void doGet(
String url, Map<String, String> headers, final HttpCallback<Response> callback, int timeout) {
OkHttpClient client = new OkHttpClient.Builder()
.connectTimeout(timeout, TimeUnit.SECONDS)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

class Http5ClientUtilTest {
class Http2ClientUtilTest {

@Test
void testDoPostHttp_param_onSuccess() throws Exception {
void testDoPost_param_onSuccess() throws Exception {
CountDownLatch latch = new CountDownLatch(1);

HttpCallback<Response> callback = new HttpCallback<Response>() {
Expand Down Expand Up @@ -62,12 +62,12 @@ public void onCancelled() {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");

Http5ClientUtil.doPostHttp("https://www.apache.org/", params, headers, callback);
Http2ClientUtil.doPost("https://www.apache.org/", params, headers, callback);
assertTrue(latch.await(10, TimeUnit.SECONDS));
}

@Test
void testDoPostHttp_param_onFailure() throws Exception {
void testDoPost_param_onFailure() throws Exception {
CountDownLatch latch = new CountDownLatch(1);

HttpCallback<Response> callback = new HttpCallback<Response>() {
Expand All @@ -94,12 +94,12 @@ public void onCancelled() {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");

Http5ClientUtil.doPostHttp("http://localhost:9999/invalid", params, headers, callback);
Http2ClientUtil.doPost("http://localhost:9999/invalid", params, headers, callback);
assertTrue(latch.await(10, TimeUnit.SECONDS));
}

@Test
void testDoPostHttp_body_onSuccess() throws Exception {
void testDoPost_body_onSuccess() throws Exception {
CountDownLatch latch = new CountDownLatch(1);

HttpCallback<Response> callback = new HttpCallback<Response>() {
Expand All @@ -124,12 +124,12 @@ public void onCancelled() {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");

Http5ClientUtil.doPostHttp("https://www.apache.org/", "{\"key\":\"value\"}", headers, callback);
Http2ClientUtil.doPost("https://www.apache.org/", "{\"key\":\"value\"}", headers, callback);
assertTrue(latch.await(10, TimeUnit.SECONDS));
}

@Test
void testDoPostHttp_body_onFailure() throws Exception {
void testDoPost_body_onFailure() throws Exception {
CountDownLatch latch = new CountDownLatch(1);

HttpCallback<Response> callback = new HttpCallback<Response>() {
Expand All @@ -153,12 +153,12 @@ public void onCancelled() {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");

Http5ClientUtil.doPostHttp("http://localhost:9999/invalid", "{\"key\":\"value\"}", headers, callback);
Http2ClientUtil.doPost("http://localhost:9999/invalid", "{\"key\":\"value\"}", headers, callback);
assertTrue(latch.await(10, TimeUnit.SECONDS));
}

@Test
void testDoPostHttp_param_onSuccess_forceHttp1() throws Exception {
void testDoPostHttp_param_onSuccess_force1() throws Exception {
CountDownLatch latch = new CountDownLatch(1);

HttpCallback<Response> callback = new HttpCallback<Response>() {
Expand Down Expand Up @@ -186,12 +186,12 @@ public void onCancelled() {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");

Http5ClientUtil.doPostHttp("http://httpbin.org/post", params, headers, callback);
Http2ClientUtil.doPost("http://httpbin.org/post", params, headers, callback);
assertTrue(latch.await(10, TimeUnit.SECONDS));
}

@Test
void testDoGetHttp_onSuccess() throws Exception {
void testDoGet_onSuccess() throws Exception {
CountDownLatch latch = new CountDownLatch(1);

HttpCallback<Response> callback = new HttpCallback<Response>() {
Expand All @@ -216,12 +216,12 @@ public void onCancelled() {
Map<String, String> headers = new HashMap<>();
headers.put("Accept", "application/json");

Http5ClientUtil.doGetHttp("https://www.apache.org/", headers, callback, 1);
Http2ClientUtil.doGet("https://www.apache.org/", headers, callback, 1);
assertTrue(latch.await(10, TimeUnit.SECONDS));
}

@Test
void testDoPostHttp_body_onSuccess_forceHttp1() throws Exception {
void testDoPostHttp_body_onSuccess_force1() throws Exception {
CountDownLatch latch = new CountDownLatch(1);

HttpCallback<Response> callback = new HttpCallback<Response>() {
Expand All @@ -246,7 +246,7 @@ public void onCancelled() {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");

Http5ClientUtil.doPostHttp("http://httpbin.org/post", "{\"key\":\"value\"}", headers, callback);
Http2ClientUtil.doPost("http://httpbin.org/post", "{\"key\":\"value\"}", headers, callback);
assertTrue(latch.await(10, TimeUnit.SECONDS));
}
}
Loading
Loading