Skip to content

Commit

Permalink
2.6 aaa
Browse files Browse the repository at this point in the history
  • Loading branch information
Lambert-Rao committed Feb 6, 2024
1 parent a38a7bd commit cb4e76b
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,6 @@ public class HealthCheckObjectConfig {

//mysql
private String database;

private Long requestTimeoutMillis;
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,20 @@ public void execute(AbstractHealthCheckService service) {
//The callback interface is used to pass the processing methods for checking success and failure.
service.doCheck(new HealthCheckCallback() {
@Override
public void success() {
public void onSuccess() {
//when the health check is successful, the result is updated to the memory cache.
Long latency = System.currentTimeMillis() - startTime;
//todo config this
if (latency > 3000){

}
memoryCache.update(service.getConfig().getHealthCheckResourceType(), service.getConfig().getInstanceId(),
HealthCheckStatus.PASSED, "health check success",
System.currentTimeMillis() - startTime);
);
}

@Override
public void fail(Exception e) {
public void onFail(Exception e) {
//when the health check fails, the result is updated to the memory cache, passing in the exception message.
log.error("Health check failed for reason: {}. Service config is {}", e, service.getConfig());
memoryCache.update(service.getConfig().getHealthCheckResourceType(), service.getConfig().getInstanceId(),
Expand All @@ -88,6 +93,8 @@ public void startExecute() {
memoryCache.getCacheMap().forEach((type, subMap) -> {
subMap.forEach((instanceId, result) -> {
if (result.getStatus() == HealthCheckStatus.CHECKING) {
//todo
result.getLatencyMilliSeconds();
result.setStatus(HealthCheckStatus.TIMEOUT);
}
addToResultList(result, resultList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @see HealthExecutor
*/
public interface HealthCheckCallback {
public void success();
public void onSuccess();

public void fail(Exception e);
public void onFail(Exception e);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,20 @@ public StorageRedisCheck(HealthCheckObjectConfig healthCheckObjectConfig) {
@Override
public void doCheck(HealthCheckCallback callback) {
try {
//TODO timeout
RedisAsyncCommands<String, String> commands = redisClient.connect().async();
commands.ping().thenAccept(result -> {
callback.success();
callback.onSuccess();
}).exceptionally(e -> {
if (e instanceof Exception) {
callback.fail((Exception) e);
callback.onFail((Exception) e);
} else {
callback.fail(new RuntimeException("Internal server error"));
callback.onFail(new RuntimeException("RedisCheck failed."));
}
return null;
});
} catch (Exception e) {
callback.fail(e);
callback.onFail(e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ class HealthExecutorTest {
public void initMock() {
Mockito.lenient().doAnswer((Answer<Void>) invocation -> {
HealthCheckCallback callback = invocation.getArgument(0);
callback.success();
callback.onSuccess();
return null;
}).when(successHealthCheckService).doCheck(any(HealthCheckCallback.class));
Mockito.lenient().doAnswer((Answer<Void>) invocation -> {
HealthCheckCallback callback = invocation.getArgument(0);
callback.fail(new RuntimeException("TestRuntimeException"));
callback.onFail(new RuntimeException("TestRuntimeException"));
return null;
}).when(failHealthCheckService).doCheck(any(HealthCheckCallback.class));
Mockito.lenient().doAnswer((Answer<Void>) invocation -> {
Expand All @@ -85,7 +85,7 @@ public void initMock() {
} catch (InterruptedException e) {
return;
}
callback.fail(new RuntimeException("TestRuntimeException"));
callback.onFail(new RuntimeException("TestRuntimeException"));
});
return null;
}).when(timeoutHealthCheckService).doCheck(any(HealthCheckCallback.class));
Expand Down Expand Up @@ -161,11 +161,12 @@ public void testTimeout() {
}

@Test
public void fullTest() {
public void testFull() throws InterruptedException {
healthExecutor.startExecute();
healthExecutor.execute(successHealthCheckService);
healthExecutor.execute(failHealthCheckService);
healthExecutor.endExecute();
Thread.sleep(1000);
healthExecutor.startExecute();
healthExecutor.execute(successHealthCheckService);
healthExecutor.execute(failHealthCheckService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public TestHealthCheckService(HealthCheckObjectConfig healthCheckObjectConfig) {

@Override
public void doCheck(HealthCheckCallback callback) {
callback.success();
callback.onSuccess();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public void init() {
public void testDoCheck() {
storageRedisCheck.doCheck(new HealthCheckCallback() {
@Override
public void success() {
public void onSuccess() {
System.out.println("success");
}

@Override
public void fail(Exception e) {
public void onFail(Exception e) {
System.out.println("fail");
}
});
Expand Down

0 comments on commit cb4e76b

Please sign in to comment.