Skip to content

Commit

Permalink
remove jackson
Browse files Browse the repository at this point in the history
  • Loading branch information
wangweicugw committed Aug 10, 2023
1 parent 35d5f6f commit b08e23c
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 91 deletions.
13 changes: 7 additions & 6 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<hierynomus.version>0.9.0</hierynomus.version>
<protobuf-java.version>3.11.0</protobuf-java.version>
<jetcd-core.version>0.5.11</jetcd-core.version>
<jackson.version>2.8.11</jackson.version>
<jackson.version>2.10.0</jackson.version>
<HikariCP.version>4.0.3</HikariCP.version>
<snakeyaml.version>1.23</snakeyaml.version>
<mysql-connector-j.version>8.0.22</mysql-connector-j.version>
Expand Down Expand Up @@ -100,11 +100,6 @@
<artifactId>simpleclient_httpserver</artifactId>
<version>${prometheus.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
Expand Down Expand Up @@ -239,6 +234,12 @@
<version>${grpc.mockserver.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
33 changes: 0 additions & 33 deletions src/main/java/com/jd/jdbc/api/VtApi.java

This file was deleted.

46 changes: 46 additions & 0 deletions src/main/java/com/jd/jdbc/api/VtApiStatusResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
Copyright 2021 JD Project Authors.
Licensed 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 com.jd.jdbc.api;

import lombok.AllArgsConstructor;

@AllArgsConstructor
public class VtApiStatusResponse {

private final String status;

private final String statusUrl;

private final String refreshVschemaUrl;

private final String keyspaces;

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"status\":\"")
.append(status).append('\"');
sb.append(",\"statusUrl\":\"")
.append(statusUrl).append('\"');
sb.append(",\"refreshVschemaUrl\":\"")
.append(refreshVschemaUrl).append('\"');
sb.append(",\"keyspaces\":\"")
.append(keyspaces).append('\"');
sb.append('}');
return sb.toString();
}
}
11 changes: 4 additions & 7 deletions src/main/java/com/jd/jdbc/api/VtHttpHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,14 @@
import java.util.HashMap;
import java.util.Map;

import static com.jd.jdbc.api.VtApiServerResponse.FAILURE;
import static com.jd.jdbc.api.VtApiServerResponse.SUCCESS;

public abstract class VtHttpHandler implements HttpHandler {

/**
* @param httpExchange
* @throws IOException
*/
protected void success(HttpExchange httpExchange) throws IOException {
this.success(httpExchange, SUCCESS.getMessage());
this.success(httpExchange, VtApiServerResponse.SUCCESS.getMessage());
}

/**
Expand All @@ -44,7 +41,7 @@ protected void success(HttpExchange httpExchange) throws IOException {
*/
protected void success(HttpExchange httpExchange, String successMessage) throws IOException {
try (OutputStream os = httpExchange.getResponseBody()) {
httpExchange.sendResponseHeaders(SUCCESS.getCode(), successMessage.length());
httpExchange.sendResponseHeaders(VtApiServerResponse.SUCCESS.getCode(), successMessage.length());
os.write(successMessage.getBytes(StandardCharsets.UTF_8));
os.flush();
}
Expand All @@ -55,7 +52,7 @@ protected void success(HttpExchange httpExchange, String successMessage) throws
* @throws IOException
*/
protected void failure(HttpExchange httpExchange) throws IOException {
this.failure(httpExchange, FAILURE.getMessage());
this.failure(httpExchange, VtApiServerResponse.FAILURE.getMessage());
}

/**
Expand All @@ -65,7 +62,7 @@ protected void failure(HttpExchange httpExchange) throws IOException {
*/
protected void failure(HttpExchange httpExchange, String errorMessage) throws IOException {
try (OutputStream os = httpExchange.getResponseBody()) {
httpExchange.sendResponseHeaders(FAILURE.getCode(), errorMessage.length());
httpExchange.sendResponseHeaders(VtApiServerResponse.FAILURE.getCode(), errorMessage.length());
os.write(errorMessage.getBytes(StandardCharsets.UTF_8));
os.flush();
}
Expand Down
25 changes: 4 additions & 21 deletions src/main/java/com/jd/jdbc/api/handler/VtStatusHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,46 +17,29 @@
package com.jd.jdbc.api.handler;

import com.jd.jdbc.VSchemaManager;
import com.jd.jdbc.api.VtApi;
import com.jd.jdbc.api.VtApiServer;
import com.jd.jdbc.api.VtApiStatusResponse;
import com.jd.jdbc.api.VtHttpHandler;
import com.jd.jdbc.util.JsonUtil;
import com.jd.jdbc.util.NetUtil;
import com.sun.net.httpserver.HttpExchange;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class VtStatusHandler extends VtHttpHandler {
private final String rootPrefix;

private final Map<String, VSchemaManager> vSchemaManagerMap;

private final Map<String, Object> responseMap;

public VtStatusHandler(String rootPrefix, Map<String, VSchemaManager> vSchemaManagerMap) {
this.rootPrefix = rootPrefix;
this.vSchemaManagerMap = vSchemaManagerMap;
responseMap = new HashMap<String, Object>(16, 1) {{
put("Status", "OK");
}};
}

@Override
public void handle(HttpExchange httpExchange) throws IOException {
String pathPrefix = "http://" + NetUtil.getLocalAdder() + ":" + VtApiServer.port + rootPrefix;
responseMap.put("Path Prefix", pathPrefix);
responseMap.put("APIs", new ArrayList<VtApi>() {{
add(new VtApi("Status And Info", pathPrefix + "/status"));
add(new VtApi("Refresh VSchema(Specify one with parameter: target=keyspace, use all to refresh all keyspaces)", pathPrefix + "/vschema/refresh"));
}});

ArrayList<String> targets = new ArrayList<>(vSchemaManagerMap.size());
vSchemaManagerMap.forEach((key, value) -> {
targets.add(key);
});
responseMap.put("Registered Target", targets.toString());
super.success(httpExchange, JsonUtil.toJSONString(responseMap, true));
String keyspaces = String.join(", ", vSchemaManagerMap.keySet());
VtApiStatusResponse vtApiStatusResponse = new VtApiStatusResponse("OK", pathPrefix + "/status", pathPrefix + "/vschema/refresh", keyspaces);
super.success(httpExchange, vtApiStatusResponse.toString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,9 @@ public void handle(HttpExchange httpExchange) throws IOException {
if (vSchemaManagerMap.containsKey(ks)) {
vSchemaManagerMap.get(ks).refreshVschema(ks);
refreshFlag = true;
resMessage.append(ks).append(" cuccess,");
resMessage.append(ks).append(" success.");
} else {
resMessage.append(ks).append(" not found,");
resMessage.append(ks).append(" not found.");
}
}
}
Expand Down
25 changes: 23 additions & 2 deletions src/main/java/com/jd/jdbc/monitor/discovery/HeartBeatReport.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@

package com.jd.jdbc.monitor.discovery;

import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Data
@Setter
@Getter
@NoArgsConstructor
public class HeartBeatReport {

private String id;
Expand All @@ -30,4 +34,21 @@ public class HeartBeatReport {
private Integer port;

private String version;

@Override
public String toString() {
final StringBuilder sb = new StringBuilder("{");
sb.append("\"id\":\"")
.append(id).append('\"');
sb.append(",\"name\":\"")
.append(name).append('\"');
sb.append(",\"address\":\"")
.append(address).append('\"');
sb.append(",\"port\":")
.append(port);
sb.append(",\"version\":\"")
.append(version).append('\"');
sb.append('}');
return sb.toString();
}
}
37 changes: 17 additions & 20 deletions src/test/java/com/jd/jdbc/api/VtApiServerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@

package com.jd.jdbc.api;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.jd.jdbc.util.JsonUtil;
import com.jd.jdbc.vitess.VitessConnection;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import lombok.Data;
import org.junit.Assert;
import org.junit.Ignore;
Expand Down Expand Up @@ -59,13 +57,14 @@ public void case01() throws SQLException, InterruptedException {
try (Connection conn = getConnection(Driver.of(TestSuiteShardSpec.TWO_SHARDS))) {
Assert.assertNotNull(conn);
Thread.sleep(2000);
String response = execCurl(new String[] {"curl", "-XGET", "http://127.0.0.1:15002"
+ VtApiServer.rootPrefix + "/status"});
String response = execCurl(new String[] {"curl", "-XGET", "http://127.0.0.1:15002" + VtApiServer.rootPrefix + "/status"});
StatusResponse statusResponse = JsonUtil.parseObject(response, StatusResponse.class);

Assert.assertNotNull(statusResponse);
Assert.assertEquals(statusResponse.status, "OK");
Assert.assertFalse(statusResponse.apis.isEmpty());
Assert.assertEquals("OK", statusResponse.status);
Assert.assertFalse(statusResponse.statusUrl.isEmpty());
Assert.assertFalse(statusResponse.refreshVschemaUrl.isEmpty());
Assert.assertEquals(conn.getCatalog(), statusResponse.keyspaces);
}
}

Expand All @@ -74,8 +73,7 @@ public void case02() throws SQLException, InterruptedException {
try (Connection conn = getConnection(Driver.of(TestSuiteShardSpec.TWO_SHARDS))) {
Assert.assertNotNull(conn);
Thread.sleep(2000);
String response = execCurl(new String[] {"curl", "-XGET", "http://127.0.0.1:15002"
+ VtApiServer.rootPrefix + "/vschema/?target=" + ((VitessConnection) conn).getDefaultKeyspace()});
String response = execCurl(new String[] {"curl", "-XGET", "http://127.0.0.1:15002" + VtApiServer.rootPrefix + "/vschema/?target=" + ((VitessConnection) conn).getDefaultKeyspace()});
Assert.assertNotNull(response);
Assert.assertEquals(VtApiServerResponse.SUCCESS.getMessage(), response.trim());
}
Expand All @@ -88,8 +86,7 @@ public void case03() throws SQLException, InterruptedException {
Assert.assertNotNull(conn2);
Assert.assertNotNull(conn16);
Thread.sleep(2000);
String response = execCurl(new String[] {"curl", "-XGET", "http://127.0.0.1:15002"
+ VtApiServer.rootPrefix + "/vschema/?target=" + ((VitessConnection) conn2).getDefaultKeyspace()});
String response = execCurl(new String[] {"curl", "-XGET", "http://127.0.0.1:15002" + VtApiServer.rootPrefix + "/vschema/?target=" + ((VitessConnection) conn2).getDefaultKeyspace()});
Assert.assertNotNull(response);
Assert.assertEquals(VtApiServerResponse.SUCCESS.getMessage(), response.trim());

Expand All @@ -98,8 +95,7 @@ public void case03() throws SQLException, InterruptedException {
Assert.assertNotNull(response);
Assert.assertEquals(VtApiServerResponse.SUCCESS.getMessage(), response.trim());

response = execCurl(new String[] {"curl", "-XGET", "http://127.0.0.1:15002"
+ VtApiServer.rootPrefix + "/vschema/?target=all"});
response = execCurl(new String[] {"curl", "-XGET", "http://127.0.0.1:15002" + VtApiServer.rootPrefix + "/vschema/?target=all"});
Assert.assertNotNull(response);
Assert.assertEquals(VtApiServerResponse.SUCCESS.getMessage(), response.trim());
}
Expand All @@ -116,20 +112,21 @@ public void case04() throws SQLException, InterruptedException {
String response = execCurl(new String[] {"curl", "-XGET", "http://127.0.0.1:" + 15006 + VtApiServer.rootPrefix + "/status"});
StatusResponse statusResponse = JsonUtil.parseObject(response, StatusResponse.class);
Assert.assertNotNull(statusResponse);
Assert.assertEquals(statusResponse.status, "OK");
Assert.assertFalse(statusResponse.apis.isEmpty());
Assert.assertEquals("OK", statusResponse.status);
Assert.assertFalse(statusResponse.statusUrl.isEmpty());
Assert.assertFalse(statusResponse.refreshVschemaUrl.isEmpty());
Assert.assertEquals(conn.getCatalog(), statusResponse.keyspaces);
}
}

@Data
protected static class StatusResponse {
@JsonProperty("Status")
protected String status;
private String status;

private String statusUrl;

@JsonProperty("Path Prefix")
protected String pathPrefix;
private String refreshVschemaUrl;

@JsonProperty("APIs")
protected List<VtApi> apis;
private String keyspaces;
}
}

0 comments on commit b08e23c

Please sign in to comment.