Skip to content

Commit

Permalink
Unified status configuration in HealthCheck. (#114)
Browse files Browse the repository at this point in the history
* Unified status configuration in HealthCheck.

* Correcting a spelling mistake
  • Loading branch information
wangweicugw authored Aug 9, 2023
1 parent 8006b5e commit 35d5f6f
Show file tree
Hide file tree
Showing 11 changed files with 286 additions and 117 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ VtDriver在开发中借鉴了许多开源的程序、框架的方法和意见,
* [mysql-connector](https://github.com/mysql/mysql-connector-j)
* [prometheus](https://prometheus.io/)
* [HikariCp](https://github.com/brettwooldridge/HikariCP)
* [ShardingSphsere](https://shardingsphere.apache.org/)
* [ShardingSphere](https://shardingsphere.apache.org/)

Note: 以上特别感谢Vitess,Vitess给我们节省大量研发成本,感谢[PlanetScale CTO Sugu Sougoumarane](https://github.com/sougou) 对我们团队的支持。

29 changes: 19 additions & 10 deletions src/main/java/com/jd/jdbc/discovery/HealthCheck.java
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,12 @@ public Map<String, TabletHealthCheck> getHealthByAliasCopy() {
}

public Map<String, List<TabletHealthCheck>> getHealthyCopy() {
return new TreeMap<>(healthy);
Map<String, List<TabletHealthCheck>> treeMap = new TreeMap<>();
for (Map.Entry<String, List<TabletHealthCheck>> entry : healthy.entrySet()) {
List<TabletHealthCheck> tabletHealthChecks = entry.getValue();
treeMap.put(entry.getKey(), new ArrayList<>(tabletHealthChecks));
}
return treeMap;
}

public IQueryService tabletConnection(Topodata.TabletAlias alias) {
Expand Down Expand Up @@ -416,7 +421,6 @@ public void updateHealth(final TabletHealthCheck th, final Query.Target preTarge
this.lock.unlock();
if (targetChanged) {
Topodata.Tablet tablet = th.getTablet().toBuilder().setType(th.getTarget().getTabletType()).setKeyspace(th.getTarget().getKeyspace()).setShard(th.getTarget().getShard()).build();

th.getQueryService().setTablet(tablet);
th.closeNativeQueryService();
}
Expand Down Expand Up @@ -455,11 +459,11 @@ public void run() {
private void doWatchTabletHealthCheckStream(final long currentTimeMillis) {
healthByAlias.forEach((alias, thc) -> {
if (thc.healthCheckCtxIsCanceled()) {
thc.finalizeConn();
thc.shutdown();
return;
}

if (thc.getLastResponseTimestamp() > 0 && currentTimeMillis - thc.getLastResponseTimestamp() > (thc.getHealthCheckTimeout() * 1000)) {
if (thc.getLastResponseTimestamp() > 0 && currentTimeMillis - thc.getLastResponseTimestamp() > thc.getHealthCheckTimeout()) {
thc.getServing().set(false);
thc.getRetrying().set(true);
thc.getLastError().set("health check timed out latest " + thc.getLastResponseTimestamp());
Expand All @@ -468,24 +472,29 @@ private void doWatchTabletHealthCheckStream(final long currentTimeMillis) {
}

TabletHealthCheck.TabletStreamHealthDetailStatus tabletStreamHealthDetailStatus = thc.getTabletStreamHealthDetailStatus().get();
if (tabletStreamHealthDetailStatus.status == TabletHealthCheck.TabletStreamHealthStatus.TABLET_STREAM_HEALTH_STATUS_RESPONSE) {
if (tabletStreamHealthDetailStatus.getStatus() == TabletHealthCheck.TabletStreamHealthStatus.TABLET_STREAM_HEALTH_STATUS_RESPONSE) {
return;
}

if (tabletStreamHealthDetailStatus.status == TabletHealthCheck.TabletStreamHealthStatus.TABLET_STREAM_HEALTH_STATUS_ERROR_PACKET) {
if (tabletStreamHealthDetailStatus.getStatus() == TabletHealthCheck.TabletStreamHealthStatus.TABLET_STREAM_HEALTH_STATUS_ERROR_PACKET) {
return;
}

if (tabletStreamHealthDetailStatus.status == TabletHealthCheck.TabletStreamHealthStatus.TABLET_STREAM_HEALTH_STATUS_ERROR) {
if (tabletStreamHealthDetailStatus.getStatus() == TabletHealthCheck.TabletStreamHealthStatus.TABLET_STREAM_HEALTH_STATUS_ERROR) {
thc.getServing().set(false);
thc.getRetrying().set(true);
thc.getLastError().set("health check error :" + tabletStreamHealthDetailStatus.message);
thc.getLastError().set("health check error :" + tabletStreamHealthDetailStatus.getMessage());
thc.startHealthCheckStream();
return;
}

if (tabletStreamHealthDetailStatus.status == TabletHealthCheck.TabletStreamHealthStatus.TABLET_STREAM_HEALTH_STATUS_NONE) {
// tabletHealthCheck is added to healthByAlias just recently, no healthCheck signal has been received.
if (tabletStreamHealthDetailStatus.getStatus() == TabletHealthCheck.TabletStreamHealthStatus.TABLET_STREAM_HEALTH_STATUS_NONE) {
return;
}
if (tabletStreamHealthDetailStatus.getStatus() == TabletHealthCheck.TabletStreamHealthStatus.TABLET_STREAM_HEALTH_STATUS_MISMATCH) {
thc.getServing().set(false);
thc.getLastError().set("health check error :" + tabletStreamHealthDetailStatus.getMessage());
this.removeTablet(thc.getTablet());
return;
}

Expand Down
Loading

0 comments on commit 35d5f6f

Please sign in to comment.