From 6dcacc05352b51fd173fffbc6c95329744b97ab1 Mon Sep 17 00:00:00 2001 From: SonglinLyu <111941624+SonglinLyu@users.noreply.github.com> Date: Sat, 21 Oct 2023 23:03:24 +0800 Subject: [PATCH] add heartbeat detection (#4) * add heartbeat detect * add for db ut * fix ut stop shell * fix db ut * add heartbeat count * add test for self increment jobid and getting job status by id * fix ut, fix proto * fix ut * fix ut --- .../tugraph/service/HeartbeatService.java | 5 + .../tugraph/service/HeartbeatServiceImpl.java | 26 ++++ .../service/JobManagementServiceImpl.java | 47 +++++-- src/main/proto/tugraph_db_management.proto | 74 ++++++----- src/main/resources/application-ut.properties | 14 ++ .../TuGraphDBManagementApplicationTests.java | 123 ++++++++++++------ ut_start.sh | 4 + ut_stop.sh | 6 + 8 files changed, 221 insertions(+), 78 deletions(-) create mode 100644 src/main/java/com/antgroup/tugraph/service/HeartbeatService.java create mode 100644 src/main/java/com/antgroup/tugraph/service/HeartbeatServiceImpl.java create mode 100644 src/main/resources/application-ut.properties create mode 100644 ut_start.sh create mode 100644 ut_stop.sh diff --git a/src/main/java/com/antgroup/tugraph/service/HeartbeatService.java b/src/main/java/com/antgroup/tugraph/service/HeartbeatService.java new file mode 100644 index 0000000..5eb9878 --- /dev/null +++ b/src/main/java/com/antgroup/tugraph/service/HeartbeatService.java @@ -0,0 +1,5 @@ +package com.antgroup.tugraph; + +public interface HeartbeatService { + TuGraphDBManagement.HeartbeatResponse detectHeartbeat(TuGraphDBManagement.HeartbeatRequest request); +} diff --git a/src/main/java/com/antgroup/tugraph/service/HeartbeatServiceImpl.java b/src/main/java/com/antgroup/tugraph/service/HeartbeatServiceImpl.java new file mode 100644 index 0000000..2de30b0 --- /dev/null +++ b/src/main/java/com/antgroup/tugraph/service/HeartbeatServiceImpl.java @@ -0,0 +1,26 @@ +package com.antgroup.tugraph; + +import com.baidu.cloud.starlight.springcloud.server.annotation.RpcService; +import lombok.extern.slf4j.Slf4j; + +@RpcService +@Slf4j +public class HeartbeatServiceImpl implements HeartbeatService { + + @Override + public TuGraphDBManagement.HeartbeatResponse detectHeartbeat(TuGraphDBManagement.HeartbeatRequest request) { + log.info("get heartbeat request"); + String reqMsg = request.getRequestMsg(); + log.info(reqMsg); + String respMsg = "This is a heartbeat response message."; + int heartbeatCount = request.getHeartbeatCount(); + TuGraphDBManagement.HeartbeatResponse.Builder respBuilder = + TuGraphDBManagement.HeartbeatResponse + .newBuilder(); + respBuilder + .setResponseMsg(respMsg) + .setHeartbeatCount(heartbeatCount + 1); + return respBuilder.build(); + } + +} diff --git a/src/main/java/com/antgroup/tugraph/service/JobManagementServiceImpl.java b/src/main/java/com/antgroup/tugraph/service/JobManagementServiceImpl.java index d9c6c19..11718bc 100644 --- a/src/main/java/com/antgroup/tugraph/service/JobManagementServiceImpl.java +++ b/src/main/java/com/antgroup/tugraph/service/JobManagementServiceImpl.java @@ -132,9 +132,9 @@ public TuGraphDBManagement.ReadJobResponse handleReadJobRequest(TuGraphDBManagem TuGraphDBManagement.ReadJobResponse .newBuilder(); TuGraphDBManagement.ReadJobResponse resp; - try { - List tempJobList = jobService.listStatus(); - for (Job tempJob: tempJobList) { + if (request.hasJobId()) { + try { + Job tempJob = jobService.getStatusById(request.getJobId()); TuGraphDBManagement.Job Job = TuGraphDBManagement.Job.newBuilder() .setDbId(tempJob.getDbId()) @@ -149,14 +149,41 @@ public TuGraphDBManagement.ReadJobResponse handleReadJobRequest(TuGraphDBManagem .setCreateTime(tempJob.getCreateTime()) .build(); respBuilder.addJob(Job); + resp = respBuilder + .setResponseCode(TuGraphDBManagement.ResponseCode.SUCCESS) + .build(); + } catch (Exception e) { + resp = respBuilder + .setResponseCode(TuGraphDBManagement.ResponseCode.FAILED) + .build(); + } + } else { + try { + List tempJobList = jobService.listStatus(); + for (Job tempJob: tempJobList) { + TuGraphDBManagement.Job Job = + TuGraphDBManagement.Job.newBuilder() + .setDbId(tempJob.getDbId()) + .setJobId(tempJob.getJobId()) + .setStartTime(tempJob.getStartTime()) + .setPeriod(tempJob.getPeriod()) + .setProcedureName(tempJob.getProcedureName()) + .setProcedureType(tempJob.getProcedureType()) + .setStatus(tempJob.getStatus()) + .setRuntime(tempJob.getRuntime()) + .setUser(tempJob.getUser()) + .setCreateTime(tempJob.getCreateTime()) + .build(); + respBuilder.addJob(Job); + } + resp = respBuilder + .setResponseCode(TuGraphDBManagement.ResponseCode.SUCCESS) + .build(); + } catch (Exception e) { + resp = respBuilder + .setResponseCode(TuGraphDBManagement.ResponseCode.FAILED) + .build(); } - resp = respBuilder - .setResponseCode(TuGraphDBManagement.ResponseCode.SUCCESS) - .build(); - } catch (Exception e) { - resp = respBuilder - .setResponseCode(TuGraphDBManagement.ResponseCode.FAILED) - .build(); } return resp; diff --git a/src/main/proto/tugraph_db_management.proto b/src/main/proto/tugraph_db_management.proto index c571fc1..0b4e5ac 100644 --- a/src/main/proto/tugraph_db_management.proto +++ b/src/main/proto/tugraph_db_management.proto @@ -29,37 +29,37 @@ enum ResponseCode { message Job { required string db_id = 1; required int32 job_id = 2; - required int64 start_time = 4; - required string period = 5; - required string procedure_name = 6; - required string procedure_type = 7; - required string status = 8; - required int64 runtime = 3; + required int64 start_time = 3; + required string period = 4; + required string procedure_name = 5; + required string procedure_type = 6; + required string status = 7; + required int64 runtime = 8; required string user = 9; required int64 create_time = 10; } message JobResult { - required int32 job_id = 2; - required string result = 1; + required int32 job_id = 1; + required string result = 2; } message CreateJobRequest { - required int64 start_time = 3; - required string period = 4; - required string procedure_name = 5; - required string procedure_type = 6; - required string user = 7; - required int64 create_time = 8; + required int64 start_time = 1; + required string period = 2; + required string procedure_name = 3; + required string procedure_type = 4; + required string user = 5; + required int64 create_time = 6; } message CreateJobResponse { - required int32 job_id = 2; - required ResponseCode response_code = 1; + required int32 job_id = 1; + required ResponseCode response_code = 2; } message ReadJobRequest { - + optional int32 job_id = 1; } message ReadJobResponse { @@ -96,28 +96,42 @@ message DeleteJobResponse { } message JobManagementRequest { - required string db_host = 5; - required string db_port = 6; + required string db_host = 1; + required string db_port = 2; oneof Req { - CreateJobRequest create_job_request = 1; - ReadJobRequest read_job_request = 2; - ReadJobResultRequest read_job_result_request = 7; - UpdateJobRequest update_job_request = 3; - DeleteJobRequest delete_job_request = 4; + CreateJobRequest create_job_request = 3; + ReadJobRequest read_job_request = 4; + ReadJobResultRequest read_job_result_request = 5; + UpdateJobRequest update_job_request = 6; + DeleteJobRequest delete_job_request = 7; }; }; message JobManagementResponse { - required ResponseCode response_code = 5; + required ResponseCode response_code = 1; oneof Resp { - CreateJobResponse create_job_response = 1; - ReadJobResponse read_job_response = 2; - ReadJobResultResponse read_job_result_response = 6; - UpdateJobResponse update_job_response = 3; - DeleteJobResponse delete_job_response = 4; + CreateJobResponse create_job_response = 2; + ReadJobResponse read_job_response = 3; + ReadJobResultResponse read_job_result_response = 4; + UpdateJobResponse update_job_response = 5; + DeleteJobResponse delete_job_response = 6; }; }; +message HeartbeatRequest { + required string request_msg = 1; + required int32 heartbeat_count = 2; +}; + +message HeartbeatResponse { + required string response_msg = 1; + required int32 heartbeat_count = 2; +}; + service JobManagementService { rpc handleRequest(JobManagementRequest) returns (JobManagementResponse); }; + +service HeartbeatService { + rpc detectHeartbeat(HeartbeatRequest) returns (HeartbeatResponse); +} diff --git a/src/main/resources/application-ut.properties b/src/main/resources/application-ut.properties new file mode 100644 index 0000000..e7023c7 --- /dev/null +++ b/src/main/resources/application-ut.properties @@ -0,0 +1,14 @@ +# Application Name +spring.application.name=TuGraphDB Management +spring.profiles.active=ut +server.port=5071 +# logging path +logging.path=./logs + +starlight.server.enable = true +starlight.server.port = 5091 + +spring.datasource.driver-class-name=org.sqlite.JDBC +spring.datasource.url=jdbc:sqlite:tugraph_db_management_ut.db +spring.main.allow-bean-definition-overriding=true + diff --git a/src/test/java/com/antgroup/tugraph/TuGraphDBManagementApplicationTests.java b/src/test/java/com/antgroup/tugraph/TuGraphDBManagementApplicationTests.java index 9be8c9f..93bdd1d 100644 --- a/src/test/java/com/antgroup/tugraph/TuGraphDBManagementApplicationTests.java +++ b/src/test/java/com/antgroup/tugraph/TuGraphDBManagementApplicationTests.java @@ -23,6 +23,9 @@ class TuGraphDBManagementApplicationTests { @Autowired private JobManagementServiceImpl jobManagementService; + @Autowired + private HeartbeatServiceImpl heartbeatService; + @Autowired private JobService jobService; @@ -40,7 +43,8 @@ void testJobManagementService() { Long startTime = System.currentTimeMillis(); String period = "IMMEDIATE"; String procedureName = "Unit Test Procedure"; - String procedureType = "Khop"; + String procedureType1 = "C++"; + String procedureType2 = "Python"; String status = "SUCCESS"; Long runtime = 144L; String user = "tester"; @@ -52,15 +56,15 @@ void testJobManagementService() { Date dStart = new Date(startTime); log.info("当前时间为: " + ft.format(dStart)); - // test creat job - // set up creat job request + // test create job + // set up create job request TuGraphDBManagement.CreateJobRequest utCreateJobRequest = TuGraphDBManagement.CreateJobRequest .newBuilder() .setStartTime(startTime) .setPeriod(period) .setProcedureName(procedureName) - .setProcedureType(procedureType) + .setProcedureType(procedureType1) .setUser(user) .setCreateTime(createTime) .build(); @@ -69,7 +73,22 @@ void testJobManagementService() { jobManagementService.handleCreateJobRequest(utCreateJobRequest, dbId); Integer jobId = createJobResp.getJobId(); assertTrue(jobId == 1); - log.info("job id is: " + Integer.toString(jobId)); + // creat another job to test self increament jobid + TuGraphDBManagement.CreateJobRequest utCreateJobRequest2 = + TuGraphDBManagement.CreateJobRequest + .newBuilder() + .setStartTime(startTime) + .setPeriod(period) + .setProcedureName(procedureName) + .setProcedureType(procedureType2) + .setUser(user) + .setCreateTime(createTime) + .build(); + TuGraphDBManagement.CreateJobResponse createJobResp2 = + jobManagementService.handleCreateJobRequest(utCreateJobRequest2, dbId); + Integer jobId2 = createJobResp2.getJobId(); + assertTrue(jobId2 == 2); + // test read job // set up read job request @@ -80,27 +99,27 @@ void testJobManagementService() { // call handleReadJobRequest method, get all job status TuGraphDBManagement.ReadJobResponse readJobResp = jobManagementService.handleReadJobRequest(utReadJobRequest, dbId); - List JobList = readJobResp.getJobList(); - assertEquals(1, JobList.size()); - TuGraphDBManagement.Job Job = JobList.get(0); + List jobList = readJobResp.getJobList(); + assertEquals(2, jobList.size()); + TuGraphDBManagement.Job job = jobList.get(0); // assert if the job info is correct - assertEquals(Job.getDbId(), dbId); - assertEquals(Job.getJobId(), jobId); - assertEquals(Job.getStartTime(), startTime); - assertEquals(Job.getPeriod(), period); - assertEquals(Job.getProcedureName(), procedureName); - assertEquals(Job.getProcedureType(), procedureType); - assertEquals(Job.getStatus(), "pending"); - assertEquals(Job.getRuntime(), -1L); - assertEquals(Job.getUser(), user); - assertEquals(Job.getCreateTime(), createTime); + assertEquals(job.getDbId(), dbId); + assertEquals(job.getJobId(), jobId); + assertEquals(job.getStartTime(), startTime); + assertEquals(job.getPeriod(), period); + assertEquals(job.getProcedureName(), procedureName); + assertEquals(job.getProcedureType(), procedureType1); + assertEquals(job.getStatus(), "pending"); + assertEquals(job.getRuntime(), -1L); + assertEquals(job.getUser(), user); + assertEquals(job.getCreateTime(), createTime); // test update job // set up update job request TuGraphDBManagement.UpdateJobRequest utUpdateJobRequest = TuGraphDBManagement.UpdateJobRequest .newBuilder() - .setJobId(jobId) + .setJobId(jobId2) .setStatus(status) .setRuntime(runtime) .setResult(result) @@ -110,33 +129,39 @@ void testJobManagementService() { jobManagementService.handleUpdateJobRequest(utUpdateJobRequest, dbId); // assert if updated job info and job result are correct // assert if job info is correct - readJobResp = - jobManagementService.handleReadJobRequest(utReadJobRequest, dbId); - JobList = readJobResp.getJobList(); - assertEquals(1, JobList.size()); - Job = JobList.get(JobList.size() - 1); - assertEquals(Job.getDbId(), dbId); - assertEquals(Job.getJobId(), jobId); - assertEquals(Job.getStartTime(), startTime); - assertEquals(Job.getPeriod(), period); - assertEquals(Job.getProcedureName(), procedureName); - assertEquals(Job.getProcedureType(), procedureType); - assertEquals(Job.getStatus(), status); - assertEquals(Job.getRuntime(), runtime); - assertEquals(Job.getUser(), user); - assertEquals(Job.getCreateTime(), createTime); + // test read job status by job id + TuGraphDBManagement.ReadJobRequest utReadJobByIdRequest = + TuGraphDBManagement.ReadJobRequest + .newBuilder() + .setJobId(jobId2) + .build(); + TuGraphDBManagement.ReadJobResponse readJobByIdResp = + jobManagementService.handleReadJobRequest(utReadJobByIdRequest, dbId); + jobList = readJobByIdResp.getJobList(); + assertEquals(1, jobList.size()); + TuGraphDBManagement.Job job2 = jobList.get(0); + assertEquals(job2.getDbId(), dbId); + assertEquals(job2.getJobId(), jobId2); + assertEquals(job2.getStartTime(), startTime); + assertEquals(job2.getPeriod(), period); + assertEquals(job2.getProcedureName(), procedureName); + assertEquals(job2.getProcedureType(), procedureType2); + assertEquals(job2.getStatus(), status); + assertEquals(job2.getRuntime(), runtime); + assertEquals(job2.getUser(), user); + assertEquals(job2.getCreateTime(), createTime); // test read job result // assert if job result is correct TuGraphDBManagement.ReadJobResultRequest utReadJobResultRequest = TuGraphDBManagement.ReadJobResultRequest .newBuilder() - .setJobId(jobId) + .setJobId(jobId2) .build(); TuGraphDBManagement.ReadJobResultResponse readJobResultResp = jobManagementService.handleReadJobResultRequest(utReadJobResultRequest, dbId); TuGraphDBManagement.JobResult jobResult = readJobResultResp.getJobResult(); - assertEquals(jobResult.getJobId(), jobId); + assertEquals(jobResult.getJobId(), jobId2); assertEquals(jobResult.getResult(), result); // test read job result error response @@ -163,8 +188,30 @@ void testJobManagementService() { // assert if the job has been deleted readJobResp = jobManagementService.handleReadJobRequest(utReadJobRequest, dbId); - JobList = readJobResp.getJobList(); - assertEquals(0, JobList.size()); + jobList = readJobResp.getJobList(); + assertEquals(1, jobList.size()); + assertEquals(jobId2, jobList.get(0).getJobId()); + } + + @Test + @Transactional + @Order(1) + void testHeartbeatService() { + log.info("start testing heartbeat service."); + + String reqMsg = "This is a heartbeat request message."; + int heartbeatCount = 0; + + TuGraphDBManagement.HeartbeatRequest heartbeatRequest = + TuGraphDBManagement.HeartbeatRequest + .newBuilder() + .setRequestMsg(reqMsg) + .setHeartbeatCount(heartbeatCount) + .build(); + TuGraphDBManagement.HeartbeatResponse heartbeatResponse = + heartbeatService.detectHeartbeat(heartbeatRequest); + + assertEquals(heartbeatCount + 1, heartbeatResponse.getHeartbeatCount()); } } diff --git a/ut_start.sh b/ut_start.sh new file mode 100644 index 0000000..4061643 --- /dev/null +++ b/ut_start.sh @@ -0,0 +1,4 @@ +#!/bin/bash +nohup mvn spring-boot:run -Dspring-boot.run.arguments="--spring.profiles.active=ut" > log.txt 2>&1 & echo $! > pidfile.txt +touch tugraph_db_management_ut.db + diff --git a/ut_stop.sh b/ut_stop.sh new file mode 100644 index 0000000..bfe4d70 --- /dev/null +++ b/ut_stop.sh @@ -0,0 +1,6 @@ +#!/bin/bash +kill -9 `cat pidfile.txt` +kill -9 `ps -ef | grep spring.profiles.active=ut | grep -v "grep" | awk '{print $2}'` +rm -f pidfile.txt +rm -f log.txt +rm -f tugraph_db_management_ut.db