From b8f9250b954b61e4bb41e11bd11ad55486556ead Mon Sep 17 00:00:00 2001 From: han Date: Fri, 27 Oct 2023 11:00:17 +0800 Subject: [PATCH 01/10] complete the reconstruction of the DeadlineCommandExecutor --- .../service/DeadlineCommandExecutor.java | 233 ++++++++++++--- src/main/resources/application.yml | 20 +- .../service/DeadlineCommandExecutorTest.java | 266 ++++++++++++++++++ 3 files changed, 476 insertions(+), 43 deletions(-) create mode 100644 src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java diff --git a/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java b/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java index 984dd01..ef97a16 100644 --- a/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java +++ b/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java @@ -1,63 +1,230 @@ package com.gearbox.platform.deadline.service; -import com.gearbox.core.model.shell.ShellResult; -import com.gearbox.core.util.CmdExecuteResultAssertion; -import com.gearbox.core.util.ShellExecutor; +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.gearbox.core.task.CacheRefreshTask; +import okhttp3.*; +import org.apache.logging.log4j.util.Strings; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.util.CollectionUtils; +import java.io.IOException; +import java.util.Collections; +import java.util.HashMap; import java.util.List; -import java.util.Locale; +import java.util.concurrent.TimeUnit; public class DeadlineCommandExecutor { + + private static OkHttpClient client; + private static ObjectMapper objectMapper; + private static final Logger LOG = LoggerFactory.getLogger(CacheRefreshTask.class); + private static final HashMap statMap = new HashMap() { + { + put(0, "Unknown"); + put(1, "Rendering"); + put(2, "Idle"); + put(3, "Offline"); + put(4, "Stalled"); + put(8, "StartingJob"); + } + }; + + static { + client = new OkHttpClient().newBuilder().connectionPool(new ConnectionPool(5, 10, TimeUnit.MILLISECONDS)) + .readTimeout(60, TimeUnit.SECONDS).writeTimeout(60, TimeUnit.SECONDS).build(); + objectMapper = new ObjectMapper(); + } + public static List getSlaveNames() { - ShellResult result = ShellExecutor.runCommand(String.format(Locale.ROOT, "deadlinecommand -GetSlaveNames")); - CmdExecuteResultAssertion.assertError(result); - return result.getReturnValues(); + List slaveNamesRestAPI = Collections.emptyList(); + try { + slaveNamesRestAPI = getSlaveNamesRestAPI(); + } catch (IOException e) { + LOG.error("get slaves erro, detail info{}", e.getMessage()); + } + return slaveNamesRestAPI; + } public static String getSlaveStatusByName(String slaveName) { - ShellResult result = ShellExecutor.runCommand( - String.format(Locale.ROOT, "deadlinecommand -GetSlaveInfo %s slavestatus", slaveName)); - CmdExecuteResultAssertion.assertError(result); - return result.getReturnValues().get(0); + String slaveStatuByNameRestAPI = null; + try { + slaveStatuByNameRestAPI = getSlaveStatusByNameRestAPI(slaveName); + } catch (IOException e) { + LOG.error("getSlaveStatusByName fail,detail info {}", e.getMessage()); + + } + return slaveStatuByNameRestAPI; } public static boolean isSlaveEnabled(String slaveName) { - ShellResult result = ShellExecutor.runCommand( - String.format(Locale.ROOT, "deadlinecommand -GetSlaveSetting %s SlaveEnabled", slaveName)); - CmdExecuteResultAssertion.assertError(result); - return Boolean.parseBoolean(result.getReturnValues().get(0)); + boolean enAble = false; + try { + enAble = isSlaveEnabledRestAPI(slaveName); + } catch (IOException e) { + LOG.error("isSlaveEnabled fail,detail info {}", enAble); + } + return enAble; } public static void deleteSlave(String slaveName) { - String cmd = String.format(Locale.ROOT, "deadlinecommand -DeleteSlave %s", slaveName); - ShellResult result = ShellExecutor.runCommand(cmd); - CmdExecuteResultAssertion.assertError(result); + String s = "fail"; + try { + s = deleteSlaveRestAPI(slaveName); + LOG.info("deleteSlave result {}", s); + } catch (IOException e) { + LOG.error("deleteSlave fail, detail info {}", e.getMessage()); + + } + } public static void disableSlave(String slaveName) { - String cmd = String.format(Locale.ROOT, "deadlinecommand -setslavesetting %s slaveenabled false", slaveName); - ShellResult result = ShellExecutor.runCommand(cmd); - CmdExecuteResultAssertion.assertError(result); + String flag = "fail"; + try { + flag = disableSlaveRestAPI(slaveName); + LOG.info("disableSlave result {}", flag); + } catch (IOException e) { + LOG.error("disableSlave fail, detail info {}", e.getMessage()); + } } public static List listWaitingJobIds() { - String getWaitingJobCmd = "deadlinecommand -GetJobIdsFilter status=queued status=rendering"; - ShellResult result = ShellExecutor.runCommand(getWaitingJobCmd); - CmdExecuteResultAssertion.assertError(result); - return result.getReturnValues(); + List list = Collections.EMPTY_LIST; + try { + list = listWaitingJobIdsRestAPI(); + } catch (IOException e) { + LOG.error("listWaitingJobIds fail,detail info {}", e.getMessage()); + } + return list; } public static String getJobUserName(String jobId) { - String cmd = String.format(Locale.ROOT, "deadlinecommand -GetJobSetting %s UserName", jobId); - ShellResult result = ShellExecutor.runCommand(cmd); - CmdExecuteResultAssertion.assertError(result); - return result.getReturnValues().get(0); + String username = Strings.EMPTY; + try { + username = getJobUserNameRestAPI(jobId); + } catch (IOException e) { + LOG.error("getJobUserName fail, detail info {}", e.getMessage()); + } + return username; } public static int getJobQueuedChunks(String jobId) { - String cmd = String.format(Locale.ROOT, "deadlinecommand -GetJobSetting %s QueuedChunks", jobId); - ShellResult result = ShellExecutor.runCommand(cmd); - CmdExecuteResultAssertion.assertError(result); - return Integer.parseInt(result.getReturnValues().get(0)); + int numChunks = 0; + try { + numChunks = getJobQueueChunksRestAPI(jobId); + } catch (IOException e) { + LOG.error("getJobQueuedChunks fail, detail info {}", numChunks); + } + return numChunks; + } + + + private static List getSlaveNamesRestAPI() throws IOException { + Request request = new Request.Builder().url("http://localhost:8081/api/slaves?NamesOnly=true").build(); + Response resp = client.newCall(request).execute(); + String bodyStr = resp.body().string(); + List list = objectMapper.readValue(bodyStr, new TypeReference>() { + }); + return list; + } + + private static String getSlaveStatusByNameRestAPI(String name) throws IOException { + String url = "http://localhost:8081/api/slaves?Data=info&Name=" + name; + Request request = new Request.Builder().url(url).build(); + Response resp = client.newCall(request).execute(); + String bodyStr = resp.body().string(); + List> list = objectMapper.readValue(bodyStr, new TypeReference>>() { + }); + if (!CollectionUtils.isEmpty(list)) { + Integer stat = (Integer) list.get(0).get("Stat"); + return statMap.get(stat); + } + return statMap.get(0); + } + + private static boolean isSlaveEnabledRestAPI(String name) throws IOException { + String url = "http://localhost:8081/api/slaves?Data=settings&Name=" + name; + Request request = new Request.Builder().url(url).build(); + Response resp = client.newCall(request).execute(); + String bodyStr = resp.body().string(); + List> list = objectMapper.readValue(bodyStr, new TypeReference>>() { + }); + boolean enable = false; + if (!CollectionUtils.isEmpty(list)) { + enable = (boolean) list.get(0).get("Enable"); + } + return enable; + } + + private static String disableSlaveRestAPI(String name) throws IOException { + String url = "http://localhost:8081/api/slaves"; + HashMap reqMap = new HashMap<>(); + reqMap.put("Command", "savesettings"); + HashMap settingsMap = new HashMap<>(); + settingsMap.put("Name", name); + settingsMap.put("Enable", false); + reqMap.put("SlaveSettings", settingsMap); + String json = objectMapper.writeValueAsString(reqMap); + MediaType JSON = MediaType.parse("application/json; charset=utf-8"); + RequestBody body = RequestBody.create(json, JSON); + Request request = new Request.Builder().url(url).put(body).build(); + String res = "fail"; + try (Response response = client.newCall(request).execute()) { + res = response.body().string(); + } + return res; + } + + private static String deleteSlaveRestAPI(String name) throws IOException { + String url = "http://localhost:8081/api/slaves?Name=" + name; + Request request = new Request.Builder().url(url).delete().build(); + Response execute = client.newCall(request).execute(); + String res = execute.body().string(); + return res; + } + + private static List listWaitingJobIdsRestAPI() throws IOException { + String url = "http://localhost:8081/api/jobs?IdOnly=true&States=Active"; + Request request = new Request.Builder().url(url).get().build(); + Response execute = client.newCall(request).execute(); + String json = execute.body().string(); + List list = objectMapper.readValue(json, new TypeReference>() { + }); + return list; + } + + private static String getJobUserNameRestAPI(String jobId) throws IOException { + HashMap job = getJobByJobId(jobId); + String res = null; + if (!CollectionUtils.isEmpty(job)) { + HashMap props = (HashMap) job.get("Props"); + res = (String) props.get("User"); + } + return res; + } + + private static int getJobQueueChunksRestAPI(String jobId) throws IOException { + HashMap job = getJobByJobId(jobId); + Integer num = 0; + if (!CollectionUtils.isEmpty(job)) { + num = (Integer) job.get("QueuedChunks"); + } + return num; + } + + private static HashMap getJobByJobId(String jobId) throws IOException { + String url = "http://localhost:8081/api/jobs?JobID=" + jobId; + Request request = new Request.Builder().url(url).get().build(); + Response execute = client.newCall(request).execute(); + String json = execute.body().string(); + List> list = objectMapper.readValue(json, new TypeReference>>() { + }); + if (!CollectionUtils.isEmpty(list)) { + return list.get(0); + } + return new HashMap<>(); } } diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index af8b825..0d9d0ad 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,10 +1,10 @@ user: # console账号的AK - ak: + ak: EWKMBYKCVYHEAAKHTKWO # console账号的SK - sk: + sk: A5Z9C4z50Q71ZdSZ6PxjVgRg9NW4UKTP9jNcPSR7 # 待使用局点的租户ID - project-id: 0d61b5ce7680f3e62fc3c005a9e8bdd7 + project-id: 9e99d31defd646e28b1b4168d5c72bf1 # 代理地址,端口,用户名密码等,无需代理可不配置 proxy-address: proxy-port: @@ -14,7 +14,7 @@ as: # AS服务在待使用局点的终端节点域名 endpoint: as.cn-north-4.myhuaweicloud.com # 预置伸缩组资源的伸缩组ID - group: f12d57a3-ed8a-4a16-8c3f-4817ab7171f2 + group: b3447b28-786c-40b1-a295-605c146c4c29 # 查询伸缩实例时单页最大返回数量,默认100可不修改 list-instance-limit: 100 # 删除伸缩实例时最大数量限制,AS服务最大支持50,可不修改 @@ -30,7 +30,7 @@ metric: # 自定义指标维度名,可不修改 dimension-name: autoscaling_group # 自定义指标维度ID,可配置成伸缩组的ID,该值不影响功能 - dimension-id: f12d57a3-ed8a-4a16-8c3f-4817ab7171f2 + dimension-id: b3447b28-786c-40b1-a295-605c146c4c29 # 指标上报的TTL参数,可不修改 report-ttl: 172800 # CES服务在待使用局点的终端节点域名 @@ -39,7 +39,7 @@ task: # 节点状态检查周期,单位 秒 health-audit-period: 30 # 自定义指标上报周期,单位 秒 - metric-report-period: 60 + metric-report-period: 5 # 检测是否需要缩容的周期,单位 秒 scale-in-period: 5 # 自动删除待缩容节点周期,单位 秒 @@ -47,9 +47,9 @@ task: # 新扩容节点自发现周期 discover-instance-period: 20 # 对比伸缩组与slurm节点数量周期,单位 秒 - diff-instance-and-node-period: 60 + diff-instance-and-node-period: 30 # 内部缓存刷新周期,单位 秒 - refresh-cache-period: 100 + refresh-cache-period: 15 # 配置检查任务运行周期,单位 秒 configuration-check-period: 300 system: @@ -62,9 +62,9 @@ system: #不稳定节点所在的分区 variable-partition: dyn1 # 空闲时间阈值,空间时间超过该值的节点将被缩容,单位 秒 - scale-in-time: 1 + scale-in-time: 10 # 判断job是否属于排队状态的时间限制条件,排队时间超过该值的job被认为是在排队并参与指标计算,建议为0 - job-wait-time: 1 + job-wait-time: 10 # 新节点注册的超时时间,超过该时间依然未注册成功将被AS删除,单位:分钟,建议为10 register-timeout-minutes: 10 # 弹性节点使用的cpu核数 diff --git a/src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java b/src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java new file mode 100644 index 0000000..c4916e7 --- /dev/null +++ b/src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java @@ -0,0 +1,266 @@ +package com.gearbox.platform.deadline.service; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import okhttp3.*; +import org.junit.jupiter.api.Test; +import org.springframework.util.CollectionUtils; + +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.concurrent.TimeUnit; + + +public class DeadlineCommandExecutorTest { + OkHttpClient client = new OkHttpClient().newBuilder().connectionPool(new ConnectionPool(5, 10, TimeUnit.MILLISECONDS)) + .readTimeout(60, TimeUnit.SECONDS).writeTimeout(60, TimeUnit.SECONDS).build(); + ObjectMapper objectMapper = new ObjectMapper(); + + @Test + public void test1() { + long start = System.currentTimeMillis(); + List slaveNames = DeadlineCommandExecutor.getSlaveNames(); + for (String slaveName : slaveNames) { + System.out.println(slaveName); + } + long end = System.currentTimeMillis(); + System.out.println(end - start); + } + + @Test + public void test2() { + long start = System.currentTimeMillis(); + String status = DeadlineCommandExecutor.getSlaveStatusByName("worker-192-168-1-226"); + System.out.println(status); + long end = System.currentTimeMillis(); + System.out.println(end - start); + } + + @Test + public void test3() { + long start = System.currentTimeMillis(); + boolean slaveEnabled = DeadlineCommandExecutor.isSlaveEnabled("worker-192-168-1-226"); + System.out.println(slaveEnabled); + long end = System.currentTimeMillis(); + System.out.println(end - start); + } + + @Test + public void test4() { + long start = System.currentTimeMillis(); + DeadlineCommandExecutor.disableSlave("worker-192-168-1-226"); + long end = System.currentTimeMillis(); + System.out.println(end - start); + } + + + @Test + public void test5() { + long start = System.currentTimeMillis(); + DeadlineCommandExecutor.deleteSlave("worker-192-168-1-226"); + long end = System.currentTimeMillis(); + System.out.println(end - start); + } + + @Test + public void test6() { + long start = System.currentTimeMillis(); + List strings = DeadlineCommandExecutor.listWaitingJobIds(); + System.out.println(strings); + long end = System.currentTimeMillis(); + System.out.println(end - start); + } + + @Test + public void test7() { + long start = System.currentTimeMillis(); + String jobId = "653a12ef2674c79d9c329a32"; + String jobUserName = DeadlineCommandExecutor.getJobUserName(jobId); + System.out.println(jobUserName); + long end = System.currentTimeMillis(); + System.out.println(end - start); + } + + @Test + public void test8() { + long start = System.currentTimeMillis(); + String jobId = "653a12ef2674c79d9c329a32"; + int jobQueuedChunks = DeadlineCommandExecutor.getJobQueuedChunks(jobId); + System.out.println(jobQueuedChunks); + long end = System.currentTimeMillis(); + System.out.println(end - start); + } + + @Test + public void getJobQueuedChunksTest() throws IOException { + long start = System.currentTimeMillis(); + String jobId = "653a12ef2674c79d9c329a32"; + String url = "http://localhost:8081/api/jobs?JobID=" + jobId; + Request request = new Request.Builder().url(url).get().build(); + Response execute = client.newCall(request).execute(); + String json = execute.body().string(); + List> list = objectMapper.readValue(json, new TypeReference>>() { + }); + for (HashMap map : list) { + System.out.println(map.get("QueuedChunks")); + } + + long end = System.currentTimeMillis(); + System.out.println(end - start); + + } + + + @Test + public void getJobUserNameTest() throws IOException { + long start = System.currentTimeMillis(); + String jobId = "653a12ef2674c79d9c329a32"; + String url = "http://localhost:8081/api/jobs?JobID=" + jobId; + Request request = new Request.Builder().url(url).get().build(); + Response execute = client.newCall(request).execute(); + String json = execute.body().string(); + List> list = objectMapper.readValue(json, new TypeReference>>() { + }); + for (HashMap map : list) { + System.out.println(map.get("Props")); + } + + if (!CollectionUtils.isEmpty(list)) { + HashMap props = (HashMap) list.get(0).get("Props"); + System.out.println(props.get("User")); + } + long end = System.currentTimeMillis(); + System.out.println(end - start); + } + + @Test + public void listWaitingJobIdsTest() throws IOException { + long start = System.currentTimeMillis(); + String url = "http://localhost:8081/api/jobs?IdOnly=true&States=Active"; + Request request = new Request.Builder().url(url).get().build(); + Response execute = client.newCall(request).execute(); + String json = execute.body().string(); + List list = objectMapper.readValue(json, new TypeReference>() { + }); + + System.out.println(list); + long end = System.currentTimeMillis(); + System.out.println(end - start); + } + + + @Test + public void deletSlavaTest() throws IOException { + long start = System.currentTimeMillis(); + String name = "worker-192-168-1-226"; + String url = "http://localhost:8081/api/slaves?Name=" + name; + Request request = new Request.Builder().url(url).delete().build(); + Response execute = client.newCall(request).execute(); + String string = execute.body().string(); + System.out.println(string); + long end = System.currentTimeMillis(); + System.out.println(end - start); + } + + @Test + public void disableSlaveTest() throws IOException { + long start = System.currentTimeMillis(); + String url = "http://localhost:8081/api/slaves"; + HashMap reqMap = new HashMap<>(); + reqMap.put("Command", "savesettings"); + HashMap settingsMap = new HashMap<>(); + String name = "worker-192-168-1-226"; + settingsMap.put("Name", name); + settingsMap.put("Enable", false); + reqMap.put("SlaveSettings", settingsMap); + String json = objectMapper.writeValueAsString(reqMap); + System.out.println(json); + MediaType JSON = MediaType.parse("application/json; charset=utf-8"); + RequestBody body = RequestBody.create(json, JSON); + Request request = new Request.Builder().url(url).put(body).build(); + try (Response response = client.newCall(request).execute()) { + System.out.println(response.body().string()); + } + long end = System.currentTimeMillis(); + System.out.println(end - start); + + } + + @Test + public void getSlavesTest() throws IOException { + long start = System.currentTimeMillis(); + Request request = new Request.Builder().url("http://localhost:8081/api/slaves?").build(); + Response resp = client.newCall(request).execute(); + String bodyStr = resp.body().string(); + List>> list = objectMapper.readValue(bodyStr, new TypeReference>>>() { + }); + for (HashMap> map : list) { + System.out.println(map); + } + long end = System.currentTimeMillis(); + System.out.println(end - start); + } + + @Test + public void getSlavesNamesTest() throws IOException { + long start = System.currentTimeMillis(); + Request request = new Request.Builder().url("http://localhost:8081/api/slaves?NamesOnly=true").build(); + Response resp = client.newCall(request).execute(); + String bodyStr = resp.body().string(); + List list = objectMapper.readValue(bodyStr, new TypeReference>() { + }); + System.out.println(list); + long end = System.currentTimeMillis(); + System.out.println(end - start); + } + + @Test + public void getSlaveStatusByName() throws IOException { + long start = System.currentTimeMillis(); + String name = "worker-192-168-1-226"; + String url = "http://localhost:8081/api/slaves?Data=info&Name=" + name; + Request request = new Request.Builder().url(url).build(); + Response resp = client.newCall(request).execute(); + String bodyStr = resp.body().string(); + List> list = objectMapper.readValue(bodyStr, new TypeReference>>() { + }); + System.out.println(list.get(0).get("Stat")); + long end = System.currentTimeMillis(); + System.out.println(end - start); + } + + @Test + public void isSlaveEnabledTest() throws IOException { + long start = System.currentTimeMillis(); + String name = "worker-192-168-1-226"; + String url = "http://localhost:8081/api/slaves?Data=settings&Name=" + name; + Request request = new Request.Builder().url(url).build(); + Response resp = client.newCall(request).execute(); + String bodyStr = resp.body().string(); + List> list = objectMapper.readValue(bodyStr, new TypeReference>>() { + }); + if (!CollectionUtils.isEmpty(list)) { + boolean enable = (boolean) list.get(0).get("Enable"); + System.out.println(enable); + } + long end = System.currentTimeMillis(); + System.out.println(end - start); + } + + @Test + public void getJobsTest() throws IOException { + long start = System.currentTimeMillis(); + Request request = new Request.Builder().url("http://localhost:8081/api/jobs").build(); + Response resp = client.newCall(request).execute(); + String bodyStr = resp.body().string(); + List> result = objectMapper.readValue(bodyStr, new TypeReference>>() { + }); + for (HashMap map : result) { + System.out.println(map); + } + long end = System.currentTimeMillis(); + System.out.println(end - start); + } + +} -- Gitee From e3a4035ac5b40bd29c930169b071c6056dd613a2 Mon Sep 17 00:00:00 2001 From: han Date: Fri, 27 Oct 2023 15:03:09 +0800 Subject: [PATCH 02/10] eliminate sensitive information of application.yml --- src/main/resources/application.yml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index 0d9d0ad..d8c52dc 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -1,10 +1,10 @@ user: # console账号的AK - ak: EWKMBYKCVYHEAAKHTKWO + ak: # console账号的SK - sk: A5Z9C4z50Q71ZdSZ6PxjVgRg9NW4UKTP9jNcPSR7 + sk: # 待使用局点的租户ID - project-id: 9e99d31defd646e28b1b4168d5c72bf1 + project-id: 1 # 代理地址,端口,用户名密码等,无需代理可不配置 proxy-address: proxy-port: @@ -14,7 +14,7 @@ as: # AS服务在待使用局点的终端节点域名 endpoint: as.cn-north-4.myhuaweicloud.com # 预置伸缩组资源的伸缩组ID - group: b3447b28-786c-40b1-a295-605c146c4c29 + group: # 查询伸缩实例时单页最大返回数量,默认100可不修改 list-instance-limit: 100 # 删除伸缩实例时最大数量限制,AS服务最大支持50,可不修改 @@ -30,7 +30,7 @@ metric: # 自定义指标维度名,可不修改 dimension-name: autoscaling_group # 自定义指标维度ID,可配置成伸缩组的ID,该值不影响功能 - dimension-id: b3447b28-786c-40b1-a295-605c146c4c29 + dimension-id: # 指标上报的TTL参数,可不修改 report-ttl: 172800 # CES服务在待使用局点的终端节点域名 -- Gitee From 49e4ec7dd8eb53d049a053f94d115bed7d30eb64 Mon Sep 17 00:00:00 2001 From: han Date: Tue, 2 Jan 2024 18:02:23 +0800 Subject: [PATCH 03/10] format request url --- .../service/DeadlineCommandExecutor.java | 15 ++- .../deadline/service/OldCommandExecutor.java | 103 ++++++++++++++++++ .../service/DeadlineCommandExecutorTest.java | 13 +++ 3 files changed, 125 insertions(+), 6 deletions(-) create mode 100644 src/main/java/com/gearbox/platform/deadline/service/OldCommandExecutor.java diff --git a/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java b/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java index ef97a16..c57c03c 100644 --- a/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java +++ b/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java @@ -13,6 +13,7 @@ import java.io.IOException; import java.util.Collections; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.concurrent.TimeUnit; public class DeadlineCommandExecutor { @@ -42,7 +43,7 @@ public class DeadlineCommandExecutor { try { slaveNamesRestAPI = getSlaveNamesRestAPI(); } catch (IOException e) { - LOG.error("get slaves erro, detail info{}", e.getMessage()); + LOG.error("get slaves error, detail info{}", e.getMessage()); } return slaveNamesRestAPI; @@ -123,7 +124,8 @@ public class DeadlineCommandExecutor { private static List getSlaveNamesRestAPI() throws IOException { - Request request = new Request.Builder().url("http://localhost:8081/api/slaves?NamesOnly=true").build(); + String url = "http://localhost:8081/api/slaves?NamesOnly=true"; + Request request = new Request.Builder().url(url).build(); Response resp = client.newCall(request).execute(); String bodyStr = resp.body().string(); List list = objectMapper.readValue(bodyStr, new TypeReference>() { @@ -132,7 +134,7 @@ public class DeadlineCommandExecutor { } private static String getSlaveStatusByNameRestAPI(String name) throws IOException { - String url = "http://localhost:8081/api/slaves?Data=info&Name=" + name; + String url = String.format(Locale.ROOT, "http://localhost:8081/api/slaves?Data=info&Name=%s", name); Request request = new Request.Builder().url(url).build(); Response resp = client.newCall(request).execute(); String bodyStr = resp.body().string(); @@ -146,7 +148,7 @@ public class DeadlineCommandExecutor { } private static boolean isSlaveEnabledRestAPI(String name) throws IOException { - String url = "http://localhost:8081/api/slaves?Data=settings&Name=" + name; + String url = String.format(Locale.ROOT, "http://localhost:8081/api/slaves?Data=settings&Name=%s", name); Request request = new Request.Builder().url(url).build(); Response resp = client.newCall(request).execute(); String bodyStr = resp.body().string(); @@ -179,7 +181,7 @@ public class DeadlineCommandExecutor { } private static String deleteSlaveRestAPI(String name) throws IOException { - String url = "http://localhost:8081/api/slaves?Name=" + name; + String url = String.format(Locale.ROOT, "http://localhost:8081/api/slaves?Name=%s", name); Request request = new Request.Builder().url(url).delete().build(); Response execute = client.newCall(request).execute(); String res = execute.body().string(); @@ -216,7 +218,7 @@ public class DeadlineCommandExecutor { } private static HashMap getJobByJobId(String jobId) throws IOException { - String url = "http://localhost:8081/api/jobs?JobID=" + jobId; + String url = String.format(Locale.ROOT, "http://localhost:8081/api/jobs?JobID=%s", jobId); Request request = new Request.Builder().url(url).get().build(); Response execute = client.newCall(request).execute(); String json = execute.body().string(); @@ -227,4 +229,5 @@ public class DeadlineCommandExecutor { } return new HashMap<>(); } + } diff --git a/src/main/java/com/gearbox/platform/deadline/service/OldCommandExecutor.java b/src/main/java/com/gearbox/platform/deadline/service/OldCommandExecutor.java new file mode 100644 index 0000000..2c9370f --- /dev/null +++ b/src/main/java/com/gearbox/platform/deadline/service/OldCommandExecutor.java @@ -0,0 +1,103 @@ +package com.gearbox.platform.deadline.service; + +import com.gearbox.core.constant.DeadlineInstanceStatusEnum; +import com.gearbox.core.constant.NodeStatusEnum; +import com.gearbox.core.model.Node; +import com.gearbox.core.model.shell.ShellResult; +import com.gearbox.core.util.CmdExecuteResultAssertion; +import com.gearbox.core.util.ShellExecutor; + +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; + +public class OldCommandExecutor { + private static final String EQUAL_SIGN = "="; + + public static List getSlaveNames() { + ShellResult result = ShellExecutor.runCommand(String.format(Locale.ROOT, "deadlinecommand -GetSlaveNames")); + CmdExecuteResultAssertion.assertError(result); + return result.getReturnValues(); + } + + public static String getSlaveStatusByName(String slaveName) { + ShellResult result = ShellExecutor.runCommand( + String.format(Locale.ROOT, "deadlinecommand -GetSlaveInfo %s slavestatus", slaveName)); + CmdExecuteResultAssertion.assertError(result); + return result.getReturnValues().get(0); + } + + public static List getSlaveInfos() { + ShellResult result = ShellExecutor.runCommand("deadlinecommand -GetSlaves"); + CmdExecuteResultAssertion.assertError(result); + List nodeList = new ArrayList<>(); + String nodeNameTemp = ""; + String slaveStatusTemp = ""; + Boolean slaveEnabledTemp = false; + Iterator iterator = result.getReturnValues().iterator(); + while (iterator.hasNext()) { + String str = iterator.next(); + if (str.toLowerCase(Locale.ROOT).startsWith("slavename")) { + nodeNameTemp = str.split(EQUAL_SIGN)[1]; + } + if (str.toLowerCase(Locale.ROOT).startsWith("slavestatus")) { + slaveStatusTemp = str.split(EQUAL_SIGN)[1]; + } + if (str.toLowerCase(Locale.ROOT).startsWith("slaveenabled")) { + slaveEnabledTemp = Boolean.parseBoolean(str.split(EQUAL_SIGN)[1]); + } + // 节点之间依赖空行分割 + if (str.equals("")) { + if (nodeNameTemp.equals("") || slaveStatusTemp.equals("")) { + continue; + } + nodeList.add(new Node(nodeNameTemp, NodeStatusEnum.toNodeStatus(DeadlineInstanceStatusEnum.value(slaveStatusTemp)), slaveEnabledTemp)); + nodeNameTemp = ""; + slaveStatusTemp = ""; + slaveEnabledTemp = false; + } + } + return nodeList; + } + + public static boolean isSlaveEnabled(String slaveName) { + ShellResult result = ShellExecutor.runCommand( + String.format(Locale.ROOT, "deadlinecommand -GetSlaveSetting %s SlaveEnabled", slaveName)); + CmdExecuteResultAssertion.assertError(result); + return Boolean.parseBoolean(result.getReturnValues().get(0)); + } + + public static void deleteSlave(String slaveName) { + String cmd = String.format(Locale.ROOT, "deadlinecommand -DeleteSlave %s", slaveName); + ShellResult result = ShellExecutor.runCommand(cmd); + CmdExecuteResultAssertion.assertError(result); + } + + public static void disableSlave(String slaveName) { + String cmd = String.format(Locale.ROOT, "deadlinecommand -setslavesetting %s slaveenabled false", slaveName); + ShellResult result = ShellExecutor.runCommand(cmd); + CmdExecuteResultAssertion.assertError(result); + } + + public static List listWaitingJobIds() { + String getWaitingJobCmd = "deadlinecommand -GetJobIdsFilter status=queued status=rendering"; + ShellResult result = ShellExecutor.runCommand(getWaitingJobCmd); + CmdExecuteResultAssertion.assertError(result); + return result.getReturnValues(); + } + + public static String getJobUserName(String jobId) { + String cmd = String.format(Locale.ROOT, "deadlinecommand -GetJobSetting %s UserName", jobId); + ShellResult result = ShellExecutor.runCommand(cmd); + CmdExecuteResultAssertion.assertError(result); + return result.getReturnValues().get(0); + } + + public static int getJobQueuedChunks(String jobId) { + String cmd = String.format(Locale.ROOT, "deadlinecommand -GetJobSetting %s QueuedChunks", jobId); + ShellResult result = ShellExecutor.runCommand(cmd); + CmdExecuteResultAssertion.assertError(result); + return Integer.parseInt(result.getReturnValues().get(0)); + } +} diff --git a/src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java b/src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java index c4916e7..3353f24 100644 --- a/src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java +++ b/src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java @@ -3,6 +3,7 @@ package com.gearbox.platform.deadline.service; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import okhttp3.*; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.springframework.util.CollectionUtils; @@ -263,4 +264,16 @@ public class DeadlineCommandExecutorTest { System.out.println(end - start); } + @Test + public void checkDeadlineWebServiceIsRunning() throws IOException { + boolean flag = true; + Request request = new Request.Builder().url("http://localhost:8081").build(); + try { + Response resp = client.newCall(request).execute(); + } catch (Exception e) { + flag = false; + } + Assertions.assertEquals(flag,true); + } + } -- Gitee From 9e97c72419cb41056fef882fbd2e9868f8394317 Mon Sep 17 00:00:00 2001 From: han Date: Tue, 2 Jan 2024 19:57:06 +0800 Subject: [PATCH 04/10] modify DeadlineCommandExecutor --- .../service/DeadlineCommandExecutor.java | 153 ++++++++++++++---- .../deadline/service/OldCommandExecutor.java | 103 ------------ .../service/DeadlineCommandExecutorTest.java | 12 +- 3 files changed, 131 insertions(+), 137 deletions(-) delete mode 100644 src/main/java/com/gearbox/platform/deadline/service/OldCommandExecutor.java diff --git a/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java b/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java index c57c03c..5a6fbf6 100644 --- a/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java +++ b/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java @@ -2,22 +2,27 @@ package com.gearbox.platform.deadline.service; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; +import com.gearbox.core.constant.DeadlineInstanceStatusEnum; +import com.gearbox.core.constant.NodeStatusEnum; +import com.gearbox.core.model.Node; +import com.gearbox.core.model.shell.ShellResult; import com.gearbox.core.task.CacheRefreshTask; +import com.gearbox.core.util.CmdExecuteResultAssertion; +import com.gearbox.core.util.ShellExecutor; import okhttp3.*; +import org.apache.juli.logging.Log; import org.apache.logging.log4j.util.Strings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.CollectionUtils; import java.io.IOException; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Locale; +import java.util.*; import java.util.concurrent.TimeUnit; public class DeadlineCommandExecutor { + private static final String EQUAL_SIGN = "="; private static OkHttpClient client; private static ObjectMapper objectMapper; private static final Logger LOG = LoggerFactory.getLogger(CacheRefreshTask.class); @@ -38,26 +43,28 @@ public class DeadlineCommandExecutor { objectMapper = new ObjectMapper(); } + public static List getSlaveNames() { - List slaveNamesRestAPI = Collections.emptyList(); + List slaveNames = Collections.emptyList(); try { - slaveNamesRestAPI = getSlaveNamesRestAPI(); + slaveNames = getSlaveNamesRestAPI(); } catch (IOException e) { - LOG.error("get slaves error, detail info{}", e.getMessage()); + slaveNames = getSlaveNamesCommand(); + LOG.warn("use deadline command to getSlaveNames"); } - return slaveNamesRestAPI; + return slaveNames; } public static String getSlaveStatusByName(String slaveName) { - String slaveStatuByNameRestAPI = null; + String slaveStatuByName = null; try { - slaveStatuByNameRestAPI = getSlaveStatusByNameRestAPI(slaveName); + slaveStatuByName = getSlaveStatusByNameRestAPI(slaveName); } catch (IOException e) { - LOG.error("getSlaveStatusByName fail,detail info {}", e.getMessage()); - + slaveStatuByName = getSlaveStatusByNameCommand(slaveName); + LOG.warn("use deadline command to getSlaveStatusByName"); } - return slaveStatuByNameRestAPI; + return slaveStatuByName; } public static boolean isSlaveEnabled(String slaveName) { @@ -65,21 +72,21 @@ public class DeadlineCommandExecutor { try { enAble = isSlaveEnabledRestAPI(slaveName); } catch (IOException e) { - LOG.error("isSlaveEnabled fail,detail info {}", enAble); + enAble = isSlaveEnabledCommand(slaveName); + LOG.warn("use deadline command to check slave is enabled"); } return enAble; } public static void deleteSlave(String slaveName) { - String s = "fail"; + String flag = "fail"; try { - s = deleteSlaveRestAPI(slaveName); - LOG.info("deleteSlave result {}", s); + flag = deleteSlaveRestAPI(slaveName); + LOG.info("deleteSlave result {}", flag); } catch (IOException e) { - LOG.error("deleteSlave fail, detail info {}", e.getMessage()); - + deleteSlaveCommand(slaveName); + LOG.warn("use deadline command to deleteSlave"); } - } public static void disableSlave(String slaveName) { @@ -88,7 +95,8 @@ public class DeadlineCommandExecutor { flag = disableSlaveRestAPI(slaveName); LOG.info("disableSlave result {}", flag); } catch (IOException e) { - LOG.error("disableSlave fail, detail info {}", e.getMessage()); + disableSlaveCommand(slaveName); + LOG.warn("use deadline command to disableSlave"); } } @@ -97,7 +105,8 @@ public class DeadlineCommandExecutor { try { list = listWaitingJobIdsRestAPI(); } catch (IOException e) { - LOG.error("listWaitingJobIds fail,detail info {}", e.getMessage()); + list = listWaitingJobIdsCommand(); + } return list; } @@ -107,7 +116,8 @@ public class DeadlineCommandExecutor { try { username = getJobUserNameRestAPI(jobId); } catch (IOException e) { - LOG.error("getJobUserName fail, detail info {}", e.getMessage()); + username = getJobUserNameCommand(jobId); + LOG.warn("use deadline command to getJobUserName"); } return username; } @@ -117,13 +127,14 @@ public class DeadlineCommandExecutor { try { numChunks = getJobQueueChunksRestAPI(jobId); } catch (IOException e) { - LOG.error("getJobQueuedChunks fail, detail info {}", numChunks); + numChunks = getJobQueuedChunksCommand(jobId); + LOG.warn("use deadline command to getJobQueuedChunks"); } return numChunks; } - private static List getSlaveNamesRestAPI() throws IOException { + public static List getSlaveNamesRestAPI() throws IOException { String url = "http://localhost:8081/api/slaves?NamesOnly=true"; Request request = new Request.Builder().url(url).build(); Response resp = client.newCall(request).execute(); @@ -199,7 +210,7 @@ public class DeadlineCommandExecutor { } private static String getJobUserNameRestAPI(String jobId) throws IOException { - HashMap job = getJobByJobId(jobId); + HashMap job = getJobByJobIdRestAPI(jobId); String res = null; if (!CollectionUtils.isEmpty(job)) { HashMap props = (HashMap) job.get("Props"); @@ -209,7 +220,7 @@ public class DeadlineCommandExecutor { } private static int getJobQueueChunksRestAPI(String jobId) throws IOException { - HashMap job = getJobByJobId(jobId); + HashMap job = getJobByJobIdRestAPI(jobId); Integer num = 0; if (!CollectionUtils.isEmpty(job)) { num = (Integer) job.get("QueuedChunks"); @@ -217,7 +228,7 @@ public class DeadlineCommandExecutor { return num; } - private static HashMap getJobByJobId(String jobId) throws IOException { + private static HashMap getJobByJobIdRestAPI(String jobId) throws IOException { String url = String.format(Locale.ROOT, "http://localhost:8081/api/jobs?JobID=%s", jobId); Request request = new Request.Builder().url(url).get().build(); Response execute = client.newCall(request).execute(); @@ -230,4 +241,90 @@ public class DeadlineCommandExecutor { return new HashMap<>(); } + public static List getSlaveNamesCommand() { + ShellResult result = ShellExecutor.runCommand(String.format(Locale.ROOT, "deadlinecommand -GetSlaveNames")); + CmdExecuteResultAssertion.assertError(result); + return result.getReturnValues(); + } + + public static String getSlaveStatusByNameCommand(String slaveName) { + ShellResult result = ShellExecutor.runCommand( + String.format(Locale.ROOT, "deadlinecommand -GetSlaveInfo %s slavestatus", slaveName)); + CmdExecuteResultAssertion.assertError(result); + return result.getReturnValues().get(0); + } + + public static List getSlaveInfos() { + ShellResult result = ShellExecutor.runCommand("deadlinecommand -GetSlaves"); + CmdExecuteResultAssertion.assertError(result); + List nodeList = new ArrayList<>(); + String nodeNameTemp = ""; + String slaveStatusTemp = ""; + Boolean slaveEnabledTemp = false; + Iterator iterator = result.getReturnValues().iterator(); + while (iterator.hasNext()) { + String str = iterator.next(); + if (str.toLowerCase(Locale.ROOT).startsWith("slavename")) { + nodeNameTemp = str.split(EQUAL_SIGN)[1]; + } + if (str.toLowerCase(Locale.ROOT).startsWith("slavestatus")) { + slaveStatusTemp = str.split(EQUAL_SIGN)[1]; + } + if (str.toLowerCase(Locale.ROOT).startsWith("slaveenabled")) { + slaveEnabledTemp = Boolean.parseBoolean(str.split(EQUAL_SIGN)[1]); + } + // 节点之间依赖空行分割 + if (str.equals("")) { + if (nodeNameTemp.equals("") || slaveStatusTemp.equals("")) { + continue; + } + nodeList.add(new Node(nodeNameTemp, NodeStatusEnum.toNodeStatus(DeadlineInstanceStatusEnum.value(slaveStatusTemp)), slaveEnabledTemp)); + nodeNameTemp = ""; + slaveStatusTemp = ""; + slaveEnabledTemp = false; + } + } + return nodeList; + } + + public static boolean isSlaveEnabledCommand(String slaveName) { + ShellResult result = ShellExecutor.runCommand( + String.format(Locale.ROOT, "deadlinecommand -GetSlaveSetting %s SlaveEnabled", slaveName)); + CmdExecuteResultAssertion.assertError(result); + return Boolean.parseBoolean(result.getReturnValues().get(0)); + } + + public static void deleteSlaveCommand(String slaveName) { + String cmd = String.format(Locale.ROOT, "deadlinecommand -DeleteSlave %s", slaveName); + ShellResult result = ShellExecutor.runCommand(cmd); + CmdExecuteResultAssertion.assertError(result); + } + + public static void disableSlaveCommand(String slaveName) { + String cmd = String.format(Locale.ROOT, "deadlinecommand -setslavesetting %s slaveenabled false", slaveName); + ShellResult result = ShellExecutor.runCommand(cmd); + CmdExecuteResultAssertion.assertError(result); + } + + public static List listWaitingJobIdsCommand() { + String getWaitingJobCmd = "deadlinecommand -GetJobIdsFilter status=queued status=rendering"; + ShellResult result = ShellExecutor.runCommand(getWaitingJobCmd); + CmdExecuteResultAssertion.assertError(result); + return result.getReturnValues(); + } + + public static String getJobUserNameCommand(String jobId) { + String cmd = String.format(Locale.ROOT, "deadlinecommand -GetJobSetting %s UserName", jobId); + ShellResult result = ShellExecutor.runCommand(cmd); + CmdExecuteResultAssertion.assertError(result); + return result.getReturnValues().get(0); + } + + public static int getJobQueuedChunksCommand(String jobId) { + String cmd = String.format(Locale.ROOT, "deadlinecommand -GetJobSetting %s QueuedChunks", jobId); + ShellResult result = ShellExecutor.runCommand(cmd); + CmdExecuteResultAssertion.assertError(result); + return Integer.parseInt(result.getReturnValues().get(0)); + } + } diff --git a/src/main/java/com/gearbox/platform/deadline/service/OldCommandExecutor.java b/src/main/java/com/gearbox/platform/deadline/service/OldCommandExecutor.java deleted file mode 100644 index 2c9370f..0000000 --- a/src/main/java/com/gearbox/platform/deadline/service/OldCommandExecutor.java +++ /dev/null @@ -1,103 +0,0 @@ -package com.gearbox.platform.deadline.service; - -import com.gearbox.core.constant.DeadlineInstanceStatusEnum; -import com.gearbox.core.constant.NodeStatusEnum; -import com.gearbox.core.model.Node; -import com.gearbox.core.model.shell.ShellResult; -import com.gearbox.core.util.CmdExecuteResultAssertion; -import com.gearbox.core.util.ShellExecutor; - -import java.util.ArrayList; -import java.util.Iterator; -import java.util.List; -import java.util.Locale; - -public class OldCommandExecutor { - private static final String EQUAL_SIGN = "="; - - public static List getSlaveNames() { - ShellResult result = ShellExecutor.runCommand(String.format(Locale.ROOT, "deadlinecommand -GetSlaveNames")); - CmdExecuteResultAssertion.assertError(result); - return result.getReturnValues(); - } - - public static String getSlaveStatusByName(String slaveName) { - ShellResult result = ShellExecutor.runCommand( - String.format(Locale.ROOT, "deadlinecommand -GetSlaveInfo %s slavestatus", slaveName)); - CmdExecuteResultAssertion.assertError(result); - return result.getReturnValues().get(0); - } - - public static List getSlaveInfos() { - ShellResult result = ShellExecutor.runCommand("deadlinecommand -GetSlaves"); - CmdExecuteResultAssertion.assertError(result); - List nodeList = new ArrayList<>(); - String nodeNameTemp = ""; - String slaveStatusTemp = ""; - Boolean slaveEnabledTemp = false; - Iterator iterator = result.getReturnValues().iterator(); - while (iterator.hasNext()) { - String str = iterator.next(); - if (str.toLowerCase(Locale.ROOT).startsWith("slavename")) { - nodeNameTemp = str.split(EQUAL_SIGN)[1]; - } - if (str.toLowerCase(Locale.ROOT).startsWith("slavestatus")) { - slaveStatusTemp = str.split(EQUAL_SIGN)[1]; - } - if (str.toLowerCase(Locale.ROOT).startsWith("slaveenabled")) { - slaveEnabledTemp = Boolean.parseBoolean(str.split(EQUAL_SIGN)[1]); - } - // 节点之间依赖空行分割 - if (str.equals("")) { - if (nodeNameTemp.equals("") || slaveStatusTemp.equals("")) { - continue; - } - nodeList.add(new Node(nodeNameTemp, NodeStatusEnum.toNodeStatus(DeadlineInstanceStatusEnum.value(slaveStatusTemp)), slaveEnabledTemp)); - nodeNameTemp = ""; - slaveStatusTemp = ""; - slaveEnabledTemp = false; - } - } - return nodeList; - } - - public static boolean isSlaveEnabled(String slaveName) { - ShellResult result = ShellExecutor.runCommand( - String.format(Locale.ROOT, "deadlinecommand -GetSlaveSetting %s SlaveEnabled", slaveName)); - CmdExecuteResultAssertion.assertError(result); - return Boolean.parseBoolean(result.getReturnValues().get(0)); - } - - public static void deleteSlave(String slaveName) { - String cmd = String.format(Locale.ROOT, "deadlinecommand -DeleteSlave %s", slaveName); - ShellResult result = ShellExecutor.runCommand(cmd); - CmdExecuteResultAssertion.assertError(result); - } - - public static void disableSlave(String slaveName) { - String cmd = String.format(Locale.ROOT, "deadlinecommand -setslavesetting %s slaveenabled false", slaveName); - ShellResult result = ShellExecutor.runCommand(cmd); - CmdExecuteResultAssertion.assertError(result); - } - - public static List listWaitingJobIds() { - String getWaitingJobCmd = "deadlinecommand -GetJobIdsFilter status=queued status=rendering"; - ShellResult result = ShellExecutor.runCommand(getWaitingJobCmd); - CmdExecuteResultAssertion.assertError(result); - return result.getReturnValues(); - } - - public static String getJobUserName(String jobId) { - String cmd = String.format(Locale.ROOT, "deadlinecommand -GetJobSetting %s UserName", jobId); - ShellResult result = ShellExecutor.runCommand(cmd); - CmdExecuteResultAssertion.assertError(result); - return result.getReturnValues().get(0); - } - - public static int getJobQueuedChunks(String jobId) { - String cmd = String.format(Locale.ROOT, "deadlinecommand -GetJobSetting %s QueuedChunks", jobId); - ShellResult result = ShellExecutor.runCommand(cmd); - CmdExecuteResultAssertion.assertError(result); - return Integer.parseInt(result.getReturnValues().get(0)); - } -} diff --git a/src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java b/src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java index 3353f24..3dbde47 100644 --- a/src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java +++ b/src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java @@ -19,9 +19,9 @@ public class DeadlineCommandExecutorTest { ObjectMapper objectMapper = new ObjectMapper(); @Test - public void test1() { + public void test1() throws IOException { long start = System.currentTimeMillis(); - List slaveNames = DeadlineCommandExecutor.getSlaveNames(); + List slaveNames = DeadlineCommandExecutor.getSlaveNamesCommand(); for (String slaveName : slaveNames) { System.out.println(slaveName); } @@ -32,7 +32,7 @@ public class DeadlineCommandExecutorTest { @Test public void test2() { long start = System.currentTimeMillis(); - String status = DeadlineCommandExecutor.getSlaveStatusByName("worker-192-168-1-226"); + String status = DeadlineCommandExecutor.getSlaveStatusByNameCommand("worker-192"); System.out.println(status); long end = System.currentTimeMillis(); System.out.println(end - start); @@ -41,7 +41,7 @@ public class DeadlineCommandExecutorTest { @Test public void test3() { long start = System.currentTimeMillis(); - boolean slaveEnabled = DeadlineCommandExecutor.isSlaveEnabled("worker-192-168-1-226"); + boolean slaveEnabled = DeadlineCommandExecutor.isSlaveEnabled("worker-192"); System.out.println(slaveEnabled); long end = System.currentTimeMillis(); System.out.println(end - start); @@ -50,7 +50,7 @@ public class DeadlineCommandExecutorTest { @Test public void test4() { long start = System.currentTimeMillis(); - DeadlineCommandExecutor.disableSlave("worker-192-168-1-226"); + DeadlineCommandExecutor.disableSlave("worker-192"); long end = System.currentTimeMillis(); System.out.println(end - start); } @@ -59,7 +59,7 @@ public class DeadlineCommandExecutorTest { @Test public void test5() { long start = System.currentTimeMillis(); - DeadlineCommandExecutor.deleteSlave("worker-192-168-1-226"); + DeadlineCommandExecutor.deleteSlave("worker-192"); long end = System.currentTimeMillis(); System.out.println(end - start); } -- Gitee From aecfeb854491d40bcad8a6ca4c30ccc8e0fac3cd Mon Sep 17 00:00:00 2001 From: han Date: Thu, 4 Jan 2024 18:00:44 +0800 Subject: [PATCH 05/10] optimized DeadlineCommandExecutor RestAPI --- .../service/DeadlineCommandExecutor.java | 349 ++++++++---------- .../service/DeadlineCommandExecutorTest.java | 265 +------------ 2 files changed, 165 insertions(+), 449 deletions(-) diff --git a/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java b/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java index a94cd2f..e61cf9f 100644 --- a/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java +++ b/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java @@ -1,5 +1,6 @@ package com.gearbox.platform.deadline.service; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.gearbox.core.constant.DeadlineInstanceStatusEnum; @@ -10,7 +11,6 @@ import com.gearbox.core.task.CacheRefreshTask; import com.gearbox.core.util.CmdExecuteResultAssertion; import com.gearbox.core.util.ShellExecutor; import okhttp3.*; -import org.apache.logging.log4j.util.Strings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.util.CollectionUtils; @@ -43,112 +43,121 @@ public class DeadlineCommandExecutor { } public static List getSlaveNames() { - List slaveNames = Collections.emptyList(); - try { - slaveNames = getSlaveNamesRestAPI(); - } catch (IOException e) { - slaveNames = getSlaveNamesCommand(); - LOG.warn("use deadline command to getSlaveNames"); - } - return slaveNames; - + ShellResult result = ShellExecutor.runCommand(String.format(Locale.ROOT, "deadlinecommand -GetSlaveNames")); + CmdExecuteResultAssertion.assertError(result); + return result.getReturnValues(); } public static String getSlaveStatusByName(String slaveName) { - String slaveStatuByName = null; - try { - slaveStatuByName = getSlaveStatusByNameRestAPI(slaveName); - } catch (IOException e) { - slaveStatuByName = getSlaveStatusByNameCommand(slaveName); - LOG.warn("use deadline command to getSlaveStatusByName"); + ShellResult result = ShellExecutor.runCommand( + String.format(Locale.ROOT, "deadlinecommand -GetSlaveInfo %s slavestatus", slaveName)); + CmdExecuteResultAssertion.assertError(result); + return result.getReturnValues().get(0); + } + + public static List getSlaveInfos() { + ShellResult result = ShellExecutor.runCommand("deadlinecommand -GetSlaves"); + CmdExecuteResultAssertion.assertError(result); + List nodeList = new ArrayList<>(); + String nodeNameTemp = ""; + String slaveStatusTemp = ""; + Boolean slaveEnabledTemp = false; + Iterator iterator = result.getReturnValues().iterator(); + while (iterator.hasNext()) { + String str = iterator.next(); + if (str.toLowerCase(Locale.ROOT).startsWith("slavename")) { + nodeNameTemp = str.split(EQUAL_SIGN)[1]; + } + if (str.toLowerCase(Locale.ROOT).startsWith("slavestatus")) { + slaveStatusTemp = str.split(EQUAL_SIGN)[1]; + } + if (str.toLowerCase(Locale.ROOT).startsWith("slaveenabled")) { + slaveEnabledTemp = Boolean.parseBoolean(str.split(EQUAL_SIGN)[1]); + } + // 节点之间依赖空行分割 + if (str.equals("")) { + if (nodeNameTemp.equals("") || slaveStatusTemp.equals("")) { + continue; + } + nodeList.add(new Node(nodeNameTemp, NodeStatusEnum.toNodeStatus(DeadlineInstanceStatusEnum.value(slaveStatusTemp)), slaveEnabledTemp)); + nodeNameTemp = ""; + slaveStatusTemp = ""; + slaveEnabledTemp = false; + } } - return slaveStatuByName; + return nodeList; } public static boolean isSlaveEnabled(String slaveName) { - boolean enAble = false; - try { - enAble = isSlaveEnabledRestAPI(slaveName); - } catch (IOException e) { - enAble = isSlaveEnabledCommand(slaveName); - LOG.warn("use deadline command to check slave is enabled"); - } - return enAble; + ShellResult result = ShellExecutor.runCommand( + String.format(Locale.ROOT, "deadlinecommand -GetSlaveSetting %s SlaveEnabled", slaveName)); + CmdExecuteResultAssertion.assertError(result); + return Boolean.parseBoolean(result.getReturnValues().get(0)); } public static void deleteSlave(String slaveName) { - String flag = "fail"; - try { - flag = deleteSlaveRestAPI(slaveName); - LOG.info("deleteSlave result {}", flag); - } catch (IOException e) { - deleteSlaveCommand(slaveName); - LOG.warn("use deadline command to deleteSlave"); - } + String cmd = String.format(Locale.ROOT, "deadlinecommand -DeleteSlave %s", slaveName); + ShellResult result = ShellExecutor.runCommand(cmd); + CmdExecuteResultAssertion.assertError(result); } public static void disableSlave(String slaveName) { - String flag = "fail"; - try { - flag = disableSlaveRestAPI(slaveName); - LOG.info("disableSlave result {}", flag); - } catch (IOException e) { - disableSlaveCommand(slaveName); - LOG.warn("use deadline command to disableSlave"); - } + String cmd = String.format(Locale.ROOT, "deadlinecommand -setslavesetting %s slaveenabled false", slaveName); + ShellResult result = ShellExecutor.runCommand(cmd); + CmdExecuteResultAssertion.assertError(result); } public static List listWaitingJobIds() { - List list = Collections.EMPTY_LIST; - try { - list = listWaitingJobIdsRestAPI(); - } catch (IOException e) { - list = listWaitingJobIdsCommand(); - - } - return list; + String getWaitingJobCmd = "deadlinecommand -GetJobIdsFilter status=queued status=rendering"; + ShellResult result = ShellExecutor.runCommand(getWaitingJobCmd); + CmdExecuteResultAssertion.assertError(result); + return result.getReturnValues(); } public static String getJobUserName(String jobId) { - String username = Strings.EMPTY; - try { - username = getJobUserNameRestAPI(jobId); - } catch (IOException e) { - username = getJobUserNameCommand(jobId); - LOG.warn("use deadline command to getJobUserName"); - } - return username; + String cmd = String.format(Locale.ROOT, "deadlinecommand -GetJobSetting %s UserName", jobId); + ShellResult result = ShellExecutor.runCommand(cmd); + CmdExecuteResultAssertion.assertError(result); + return result.getReturnValues().get(0); } public static int getJobQueuedChunks(String jobId) { - int numChunks = 0; - try { - numChunks = getJobQueueChunksRestAPI(jobId); - } catch (IOException e) { - numChunks = getJobQueuedChunksCommand(jobId); - LOG.warn("use deadline command to getJobQueuedChunks"); - } - return numChunks; + String cmd = String.format(Locale.ROOT, "deadlinecommand -GetJobSetting %s QueuedChunks", jobId); + ShellResult result = ShellExecutor.runCommand(cmd); + CmdExecuteResultAssertion.assertError(result); + return Integer.parseInt(result.getReturnValues().get(0)); } - - public static List getSlaveNamesRestAPI() throws IOException { + public static List getSlaveNamesRestAPI() { String url = "http://localhost:8081/api/slaves?NamesOnly=true"; Request request = new Request.Builder().url(url).build(); - Response resp = client.newCall(request).execute(); - String bodyStr = resp.body().string(); - List list = objectMapper.readValue(bodyStr, new TypeReference>() { - }); - return list; + List list = null; + try { + Response resp = client.newCall(request).execute(); + String bodyStr = resp.body().string(); + list = objectMapper.readValue(bodyStr, new TypeReference>() { + }); + } catch (IOException e) { + LOG.error("use RestAPI get getSlaveNames failed"); + } + if (!CollectionUtils.isEmpty(list)) { + return list; + } + return Collections.emptyList(); } - private static String getSlaveStatusByNameRestAPI(String name) throws IOException { + private static String getSlaveStatusByNameRestAPI(String name) { String url = String.format(Locale.ROOT, "http://localhost:8081/api/slaves?Data=info&Name=%s", name); Request request = new Request.Builder().url(url).build(); - Response resp = client.newCall(request).execute(); - String bodyStr = resp.body().string(); - List> list = objectMapper.readValue(bodyStr, new TypeReference>>() { - }); + List> list = null; + try { + Response resp = client.newCall(request).execute(); + String bodyStr = resp.body().string(); + list = objectMapper.readValue(bodyStr, new TypeReference>>() { + }); + } catch (IOException e) { + LOG.error("use RestAPI get getSlaveStatusByName failed"); + } if (!CollectionUtils.isEmpty(list)) { Integer stat = (Integer) list.get(0).get("Stat"); return statMap.get(stat); @@ -156,21 +165,51 @@ public class DeadlineCommandExecutor { return statMap.get(0); } - private static boolean isSlaveEnabledRestAPI(String name) throws IOException { + public static List getSlaveInfosRestAPI() { + List nodeList = new ArrayList<>(); + String url = "http://localhost:8081/api/slaves"; + Request request = new Request.Builder().url(url).build(); + try { + Response resp = client.newCall(request).execute(); + String bodyStr = resp.body().string(); + List> list = objectMapper.readValue(bodyStr, new TypeReference>>() { + }); + for (HashMap map : list) { + LinkedHashMap infoMap = (LinkedHashMap) map.get("Info"); + String nodeName = (String) infoMap.get("Name"); + Integer stat = (Integer) infoMap.get("Stat"); + String slaveStatus = statMap.get(stat); + HashMap settingsMap = (HashMap) map.get("Settings"); + boolean slaveEnabled = (boolean) settingsMap.get("Enable"); + nodeList.add(new Node(nodeName, NodeStatusEnum.toNodeStatus(DeadlineInstanceStatusEnum.value(slaveStatus)), slaveEnabled)); + } + } catch (IOException e) { + LOG.error("use RestAPI get getSlaveInfos failed"); + } + return nodeList; + } + + private static boolean isSlaveEnabledRestAPI(String name) { String url = String.format(Locale.ROOT, "http://localhost:8081/api/slaves?Data=settings&Name=%s", name); Request request = new Request.Builder().url(url).build(); - Response resp = client.newCall(request).execute(); - String bodyStr = resp.body().string(); - List> list = objectMapper.readValue(bodyStr, new TypeReference>>() { - }); boolean enable = false; + List> list = null; + try { + Response resp = client.newCall(request).execute(); + String bodyStr = resp.body().string(); + list = objectMapper.readValue(bodyStr, new TypeReference>>() { + }); + + } catch (IOException e) { + LOG.error("use RestAPI get SlaveEnabled failed"); + } if (!CollectionUtils.isEmpty(list)) { enable = (boolean) list.get(0).get("Enable"); } return enable; } - private static String disableSlaveRestAPI(String name) throws IOException { + private static String disableSlaveRestAPI(String name) { String url = "http://localhost:8081/api/slaves"; HashMap reqMap = new HashMap<>(); reqMap.put("Command", "savesettings"); @@ -178,36 +217,49 @@ public class DeadlineCommandExecutor { settingsMap.put("Name", name); settingsMap.put("Enable", false); reqMap.put("SlaveSettings", settingsMap); - String json = objectMapper.writeValueAsString(reqMap); - MediaType JSON = MediaType.parse("application/json; charset=utf-8"); - RequestBody body = RequestBody.create(json, JSON); - Request request = new Request.Builder().url(url).put(body).build(); String res = "fail"; - try (Response response = client.newCall(request).execute()) { + try { + String json = objectMapper.writeValueAsString(reqMap); + MediaType JSON = MediaType.parse("application/json; charset=utf-8"); + RequestBody body = RequestBody.create(json, JSON); + Request request = new Request.Builder().url(url).put(body).build(); + Response response = client.newCall(request).execute(); res = response.body().string(); + } catch (IOException e) { + LOG.error("use RestAPI get disableSlave failed"); } return res; } - private static String deleteSlaveRestAPI(String name) throws IOException { + private static String deleteSlaveRestAPI(String name) { String url = String.format(Locale.ROOT, "http://localhost:8081/api/slaves?Name=%s", name); Request request = new Request.Builder().url(url).delete().build(); - Response execute = client.newCall(request).execute(); - String res = execute.body().string(); + String res = null; + try { + Response execute = client.newCall(request).execute(); + res = execute.body().string(); + } catch (IOException e) { + LOG.error("use RestAPI get deleteSlave failed"); + } return res; } - private static List listWaitingJobIdsRestAPI() throws IOException { + private static List listWaitingJobIdsRestAPI() { String url = "http://localhost:8081/api/jobs?IdOnly=true&States=Active"; Request request = new Request.Builder().url(url).get().build(); - Response execute = client.newCall(request).execute(); - String json = execute.body().string(); - List list = objectMapper.readValue(json, new TypeReference>() { - }); + List list = null; + try { + Response execute = client.newCall(request).execute(); + String json = execute.body().string(); + list = objectMapper.readValue(json, new TypeReference>() { + }); + } catch (IOException e) { + LOG.error("use RestAPI get listWaitingJobIds failed"); + } return list; } - private static String getJobUserNameRestAPI(String jobId) throws IOException { + private static String getJobUserNameRestAPI(String jobId) { HashMap job = getJobByJobIdRestAPI(jobId); String res = null; if (!CollectionUtils.isEmpty(job)) { @@ -217,7 +269,7 @@ public class DeadlineCommandExecutor { return res; } - private static int getJobQueueChunksRestAPI(String jobId) throws IOException { + private static int getJobQueueChunksRestAPI(String jobId) { HashMap job = getJobByJobIdRestAPI(jobId); Integer num = 0; if (!CollectionUtils.isEmpty(job)) { @@ -226,103 +278,22 @@ public class DeadlineCommandExecutor { return num; } - private static HashMap getJobByJobIdRestAPI(String jobId) throws IOException { + private static HashMap getJobByJobIdRestAPI(String jobId) { String url = String.format(Locale.ROOT, "http://localhost:8081/api/jobs?JobID=%s", jobId); Request request = new Request.Builder().url(url).get().build(); - Response execute = client.newCall(request).execute(); - String json = execute.body().string(); - List> list = objectMapper.readValue(json, new TypeReference>>() { - }); + List> list = null; + try { + Response execute = client.newCall(request).execute(); + String json = execute.body().string(); + list = objectMapper.readValue(json, new TypeReference>>() { + }); + } catch (IOException e) { + LOG.error("use RestAPI get getJobByJobId failed"); + } if (!CollectionUtils.isEmpty(list)) { return list.get(0); } return new HashMap<>(); } - public static List getSlaveNamesCommand() { - ShellResult result = ShellExecutor.runCommand(String.format(Locale.ROOT, "deadlinecommand -GetSlaveNames")); - CmdExecuteResultAssertion.assertError(result); - return result.getReturnValues(); - } - - public static String getSlaveStatusByNameCommand(String slaveName) { - ShellResult result = ShellExecutor.runCommand( - String.format(Locale.ROOT, "deadlinecommand -GetSlaveInfo %s slavestatus", slaveName)); - CmdExecuteResultAssertion.assertError(result); - return result.getReturnValues().get(0); - } - - public static List getSlaveInfos() { - ShellResult result = ShellExecutor.runCommand("deadlinecommand -GetSlaves"); - CmdExecuteResultAssertion.assertError(result); - List nodeList = new ArrayList<>(); - String nodeNameTemp = ""; - String slaveStatusTemp = ""; - Boolean slaveEnabledTemp = false; - Iterator iterator = result.getReturnValues().iterator(); - while (iterator.hasNext()) { - String str = iterator.next(); - if (str.toLowerCase(Locale.ROOT).startsWith("slavename")) { - nodeNameTemp = str.split(EQUAL_SIGN)[1]; - } - if (str.toLowerCase(Locale.ROOT).startsWith("slavestatus")) { - slaveStatusTemp = str.split(EQUAL_SIGN)[1]; - } - if (str.toLowerCase(Locale.ROOT).startsWith("slaveenabled")) { - slaveEnabledTemp = Boolean.parseBoolean(str.split(EQUAL_SIGN)[1]); - } - // 节点之间依赖空行分割 - if (str.equals("")) { - if (nodeNameTemp.equals("") || slaveStatusTemp.equals("")) { - continue; - } - nodeList.add(new Node(nodeNameTemp, NodeStatusEnum.toNodeStatus(DeadlineInstanceStatusEnum.value(slaveStatusTemp)), slaveEnabledTemp)); - nodeNameTemp = ""; - slaveStatusTemp = ""; - slaveEnabledTemp = false; - } - } - return nodeList; - } - - public static boolean isSlaveEnabledCommand(String slaveName) { - ShellResult result = ShellExecutor.runCommand( - String.format(Locale.ROOT, "deadlinecommand -GetSlaveSetting %s SlaveEnabled", slaveName)); - CmdExecuteResultAssertion.assertError(result); - return Boolean.parseBoolean(result.getReturnValues().get(0)); - } - - public static void deleteSlaveCommand(String slaveName) { - String cmd = String.format(Locale.ROOT, "deadlinecommand -DeleteSlave %s", slaveName); - ShellResult result = ShellExecutor.runCommand(cmd); - CmdExecuteResultAssertion.assertError(result); - } - - public static void disableSlaveCommand(String slaveName) { - String cmd = String.format(Locale.ROOT, "deadlinecommand -setslavesetting %s slaveenabled false", slaveName); - ShellResult result = ShellExecutor.runCommand(cmd); - CmdExecuteResultAssertion.assertError(result); - } - - public static List listWaitingJobIdsCommand() { - String getWaitingJobCmd = "deadlinecommand -GetJobIdsFilter status=queued status=rendering"; - ShellResult result = ShellExecutor.runCommand(getWaitingJobCmd); - CmdExecuteResultAssertion.assertError(result); - return result.getReturnValues(); - } - - public static String getJobUserNameCommand(String jobId) { - String cmd = String.format(Locale.ROOT, "deadlinecommand -GetJobSetting %s UserName", jobId); - ShellResult result = ShellExecutor.runCommand(cmd); - CmdExecuteResultAssertion.assertError(result); - return result.getReturnValues().get(0); - } - - public static int getJobQueuedChunksCommand(String jobId) { - String cmd = String.format(Locale.ROOT, "deadlinecommand -GetJobSetting %s QueuedChunks", jobId); - ShellResult result = ShellExecutor.runCommand(cmd); - CmdExecuteResultAssertion.assertError(result); - return Integer.parseInt(result.getReturnValues().get(0)); - } - } diff --git a/src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java b/src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java index 3dbde47..da66590 100644 --- a/src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java +++ b/src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java @@ -1,14 +1,12 @@ package com.gearbox.platform.deadline.service; -import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import okhttp3.*; -import org.junit.jupiter.api.Assertions; +import com.gearbox.core.model.Node; +import okhttp3.ConnectionPool; +import okhttp3.OkHttpClient; import org.junit.jupiter.api.Test; -import org.springframework.util.CollectionUtils; import java.io.IOException; -import java.util.HashMap; import java.util.List; import java.util.concurrent.TimeUnit; @@ -20,260 +18,7 @@ public class DeadlineCommandExecutorTest { @Test public void test1() throws IOException { - long start = System.currentTimeMillis(); - List slaveNames = DeadlineCommandExecutor.getSlaveNamesCommand(); - for (String slaveName : slaveNames) { - System.out.println(slaveName); - } - long end = System.currentTimeMillis(); - System.out.println(end - start); + List slaveInfosRestAPI = DeadlineCommandExecutor.getSlaveInfosRestAPI(); + System.out.println(slaveInfosRestAPI); } - - @Test - public void test2() { - long start = System.currentTimeMillis(); - String status = DeadlineCommandExecutor.getSlaveStatusByNameCommand("worker-192"); - System.out.println(status); - long end = System.currentTimeMillis(); - System.out.println(end - start); - } - - @Test - public void test3() { - long start = System.currentTimeMillis(); - boolean slaveEnabled = DeadlineCommandExecutor.isSlaveEnabled("worker-192"); - System.out.println(slaveEnabled); - long end = System.currentTimeMillis(); - System.out.println(end - start); - } - - @Test - public void test4() { - long start = System.currentTimeMillis(); - DeadlineCommandExecutor.disableSlave("worker-192"); - long end = System.currentTimeMillis(); - System.out.println(end - start); - } - - - @Test - public void test5() { - long start = System.currentTimeMillis(); - DeadlineCommandExecutor.deleteSlave("worker-192"); - long end = System.currentTimeMillis(); - System.out.println(end - start); - } - - @Test - public void test6() { - long start = System.currentTimeMillis(); - List strings = DeadlineCommandExecutor.listWaitingJobIds(); - System.out.println(strings); - long end = System.currentTimeMillis(); - System.out.println(end - start); - } - - @Test - public void test7() { - long start = System.currentTimeMillis(); - String jobId = "653a12ef2674c79d9c329a32"; - String jobUserName = DeadlineCommandExecutor.getJobUserName(jobId); - System.out.println(jobUserName); - long end = System.currentTimeMillis(); - System.out.println(end - start); - } - - @Test - public void test8() { - long start = System.currentTimeMillis(); - String jobId = "653a12ef2674c79d9c329a32"; - int jobQueuedChunks = DeadlineCommandExecutor.getJobQueuedChunks(jobId); - System.out.println(jobQueuedChunks); - long end = System.currentTimeMillis(); - System.out.println(end - start); - } - - @Test - public void getJobQueuedChunksTest() throws IOException { - long start = System.currentTimeMillis(); - String jobId = "653a12ef2674c79d9c329a32"; - String url = "http://localhost:8081/api/jobs?JobID=" + jobId; - Request request = new Request.Builder().url(url).get().build(); - Response execute = client.newCall(request).execute(); - String json = execute.body().string(); - List> list = objectMapper.readValue(json, new TypeReference>>() { - }); - for (HashMap map : list) { - System.out.println(map.get("QueuedChunks")); - } - - long end = System.currentTimeMillis(); - System.out.println(end - start); - - } - - - @Test - public void getJobUserNameTest() throws IOException { - long start = System.currentTimeMillis(); - String jobId = "653a12ef2674c79d9c329a32"; - String url = "http://localhost:8081/api/jobs?JobID=" + jobId; - Request request = new Request.Builder().url(url).get().build(); - Response execute = client.newCall(request).execute(); - String json = execute.body().string(); - List> list = objectMapper.readValue(json, new TypeReference>>() { - }); - for (HashMap map : list) { - System.out.println(map.get("Props")); - } - - if (!CollectionUtils.isEmpty(list)) { - HashMap props = (HashMap) list.get(0).get("Props"); - System.out.println(props.get("User")); - } - long end = System.currentTimeMillis(); - System.out.println(end - start); - } - - @Test - public void listWaitingJobIdsTest() throws IOException { - long start = System.currentTimeMillis(); - String url = "http://localhost:8081/api/jobs?IdOnly=true&States=Active"; - Request request = new Request.Builder().url(url).get().build(); - Response execute = client.newCall(request).execute(); - String json = execute.body().string(); - List list = objectMapper.readValue(json, new TypeReference>() { - }); - - System.out.println(list); - long end = System.currentTimeMillis(); - System.out.println(end - start); - } - - - @Test - public void deletSlavaTest() throws IOException { - long start = System.currentTimeMillis(); - String name = "worker-192-168-1-226"; - String url = "http://localhost:8081/api/slaves?Name=" + name; - Request request = new Request.Builder().url(url).delete().build(); - Response execute = client.newCall(request).execute(); - String string = execute.body().string(); - System.out.println(string); - long end = System.currentTimeMillis(); - System.out.println(end - start); - } - - @Test - public void disableSlaveTest() throws IOException { - long start = System.currentTimeMillis(); - String url = "http://localhost:8081/api/slaves"; - HashMap reqMap = new HashMap<>(); - reqMap.put("Command", "savesettings"); - HashMap settingsMap = new HashMap<>(); - String name = "worker-192-168-1-226"; - settingsMap.put("Name", name); - settingsMap.put("Enable", false); - reqMap.put("SlaveSettings", settingsMap); - String json = objectMapper.writeValueAsString(reqMap); - System.out.println(json); - MediaType JSON = MediaType.parse("application/json; charset=utf-8"); - RequestBody body = RequestBody.create(json, JSON); - Request request = new Request.Builder().url(url).put(body).build(); - try (Response response = client.newCall(request).execute()) { - System.out.println(response.body().string()); - } - long end = System.currentTimeMillis(); - System.out.println(end - start); - - } - - @Test - public void getSlavesTest() throws IOException { - long start = System.currentTimeMillis(); - Request request = new Request.Builder().url("http://localhost:8081/api/slaves?").build(); - Response resp = client.newCall(request).execute(); - String bodyStr = resp.body().string(); - List>> list = objectMapper.readValue(bodyStr, new TypeReference>>>() { - }); - for (HashMap> map : list) { - System.out.println(map); - } - long end = System.currentTimeMillis(); - System.out.println(end - start); - } - - @Test - public void getSlavesNamesTest() throws IOException { - long start = System.currentTimeMillis(); - Request request = new Request.Builder().url("http://localhost:8081/api/slaves?NamesOnly=true").build(); - Response resp = client.newCall(request).execute(); - String bodyStr = resp.body().string(); - List list = objectMapper.readValue(bodyStr, new TypeReference>() { - }); - System.out.println(list); - long end = System.currentTimeMillis(); - System.out.println(end - start); - } - - @Test - public void getSlaveStatusByName() throws IOException { - long start = System.currentTimeMillis(); - String name = "worker-192-168-1-226"; - String url = "http://localhost:8081/api/slaves?Data=info&Name=" + name; - Request request = new Request.Builder().url(url).build(); - Response resp = client.newCall(request).execute(); - String bodyStr = resp.body().string(); - List> list = objectMapper.readValue(bodyStr, new TypeReference>>() { - }); - System.out.println(list.get(0).get("Stat")); - long end = System.currentTimeMillis(); - System.out.println(end - start); - } - - @Test - public void isSlaveEnabledTest() throws IOException { - long start = System.currentTimeMillis(); - String name = "worker-192-168-1-226"; - String url = "http://localhost:8081/api/slaves?Data=settings&Name=" + name; - Request request = new Request.Builder().url(url).build(); - Response resp = client.newCall(request).execute(); - String bodyStr = resp.body().string(); - List> list = objectMapper.readValue(bodyStr, new TypeReference>>() { - }); - if (!CollectionUtils.isEmpty(list)) { - boolean enable = (boolean) list.get(0).get("Enable"); - System.out.println(enable); - } - long end = System.currentTimeMillis(); - System.out.println(end - start); - } - - @Test - public void getJobsTest() throws IOException { - long start = System.currentTimeMillis(); - Request request = new Request.Builder().url("http://localhost:8081/api/jobs").build(); - Response resp = client.newCall(request).execute(); - String bodyStr = resp.body().string(); - List> result = objectMapper.readValue(bodyStr, new TypeReference>>() { - }); - for (HashMap map : result) { - System.out.println(map); - } - long end = System.currentTimeMillis(); - System.out.println(end - start); - } - - @Test - public void checkDeadlineWebServiceIsRunning() throws IOException { - boolean flag = true; - Request request = new Request.Builder().url("http://localhost:8081").build(); - try { - Response resp = client.newCall(request).execute(); - } catch (Exception e) { - flag = false; - } - Assertions.assertEquals(flag,true); - } - } -- Gitee From 865f320e8bad5536ce988a6e4711054210cb1f48 Mon Sep 17 00:00:00 2001 From: han Date: Thu, 4 Jan 2024 19:32:37 +0800 Subject: [PATCH 06/10] compete test DeadlineCommandExecutor restAPI --- .../service/DeadlineCommandExecutor.java | 16 +++--- .../service/DeadlineCommandExecutorTest.java | 55 ++++++++++++++++++- 2 files changed, 61 insertions(+), 10 deletions(-) diff --git a/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java b/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java index e61cf9f..62ccae5 100644 --- a/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java +++ b/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java @@ -146,7 +146,7 @@ public class DeadlineCommandExecutor { return Collections.emptyList(); } - private static String getSlaveStatusByNameRestAPI(String name) { + public static String getSlaveStatusByNameRestAPI(String name) { String url = String.format(Locale.ROOT, "http://localhost:8081/api/slaves?Data=info&Name=%s", name); Request request = new Request.Builder().url(url).build(); List> list = null; @@ -189,7 +189,7 @@ public class DeadlineCommandExecutor { return nodeList; } - private static boolean isSlaveEnabledRestAPI(String name) { + public static boolean isSlaveEnabledRestAPI(String name) { String url = String.format(Locale.ROOT, "http://localhost:8081/api/slaves?Data=settings&Name=%s", name); Request request = new Request.Builder().url(url).build(); boolean enable = false; @@ -209,7 +209,7 @@ public class DeadlineCommandExecutor { return enable; } - private static String disableSlaveRestAPI(String name) { + public static String disableSlaveRestAPI(String name) { String url = "http://localhost:8081/api/slaves"; HashMap reqMap = new HashMap<>(); reqMap.put("Command", "savesettings"); @@ -231,7 +231,7 @@ public class DeadlineCommandExecutor { return res; } - private static String deleteSlaveRestAPI(String name) { + public static String deleteSlaveRestAPI(String name) { String url = String.format(Locale.ROOT, "http://localhost:8081/api/slaves?Name=%s", name); Request request = new Request.Builder().url(url).delete().build(); String res = null; @@ -244,7 +244,7 @@ public class DeadlineCommandExecutor { return res; } - private static List listWaitingJobIdsRestAPI() { + public static List listWaitingJobIdsRestAPI() { String url = "http://localhost:8081/api/jobs?IdOnly=true&States=Active"; Request request = new Request.Builder().url(url).get().build(); List list = null; @@ -259,7 +259,7 @@ public class DeadlineCommandExecutor { return list; } - private static String getJobUserNameRestAPI(String jobId) { + public static String getJobUserNameRestAPI(String jobId) { HashMap job = getJobByJobIdRestAPI(jobId); String res = null; if (!CollectionUtils.isEmpty(job)) { @@ -269,7 +269,7 @@ public class DeadlineCommandExecutor { return res; } - private static int getJobQueueChunksRestAPI(String jobId) { + public static int getJobQueueChunksRestAPI(String jobId) { HashMap job = getJobByJobIdRestAPI(jobId); Integer num = 0; if (!CollectionUtils.isEmpty(job)) { @@ -278,7 +278,7 @@ public class DeadlineCommandExecutor { return num; } - private static HashMap getJobByJobIdRestAPI(String jobId) { + public static HashMap getJobByJobIdRestAPI(String jobId) { String url = String.format(Locale.ROOT, "http://localhost:8081/api/jobs?JobID=%s", jobId); Request request = new Request.Builder().url(url).get().build(); List> list = null; diff --git a/src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java b/src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java index da66590..7cf06d3 100644 --- a/src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java +++ b/src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java @@ -4,7 +4,9 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.gearbox.core.model.Node; import okhttp3.ConnectionPool; import okhttp3.OkHttpClient; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import org.junit.platform.commons.annotation.Testable; import java.io.IOException; import java.util.List; @@ -17,8 +19,57 @@ public class DeadlineCommandExecutorTest { ObjectMapper objectMapper = new ObjectMapper(); @Test - public void test1() throws IOException { + public void testGetSlaveInfosRestAPI() { List slaveInfosRestAPI = DeadlineCommandExecutor.getSlaveInfosRestAPI(); - System.out.println(slaveInfosRestAPI); + Assertions.assertEquals(slaveInfosRestAPI.size(), 1); + } + + @Test + public void testGetSlaveNamesRestAPI() { + List slaveNamesRestAPI = DeadlineCommandExecutor.getSlaveNamesRestAPI(); + Assertions.assertEquals(slaveNamesRestAPI.get(0), "worker-192"); + } + + @Test + public void testGetSlaveStatusByNameRestAPI() { + String slaveStatusByNameRestAPI = DeadlineCommandExecutor.getSlaveStatusByNameRestAPI("worker-192"); + Assertions.assertEquals(slaveStatusByNameRestAPI, "Idle"); + } + + @Test + public void testIsSlaveEnabled() { + boolean slaveEnabledRestAPI = DeadlineCommandExecutor.isSlaveEnabledRestAPI("worker-192"); + Assertions.assertEquals(slaveEnabledRestAPI, true); + } + + @Test + public void testDisableSlave() { + String s = DeadlineCommandExecutor.disableSlaveRestAPI("worker-192"); + Assertions.assertEquals(s, "Success"); + } + + @Test + public void testDeleteSlaveRestAPI() { + String s = DeadlineCommandExecutor.deleteSlaveRestAPI("worker-192"); + Assertions.assertEquals(s,"Success"); + } + + @Test + public void testListWaitingJobIdsRestAPI() { + List list = DeadlineCommandExecutor.listWaitingJobIdsRestAPI(); + Assertions.assertEquals(list.size(),12); + } + + @Test + public void testGetJobUserNameRestAPI(){ + String jobUserNameRestAPI = DeadlineCommandExecutor.getJobUserNameRestAPI("6596960866f66719ec4e5619"); + Assertions.assertEquals(jobUserNameRestAPI,"administrator"); + } + + + @Test + public void testGetJobQueueChunksRestAPI(){ + int jobQueueChunksRestAPI = DeadlineCommandExecutor.getJobQueueChunksRestAPI("6596960866f66719ec4e5619"); + Assertions.assertEquals(jobQueueChunksRestAPI,23); } } -- Gitee From 541ff8a8ae3797fe7b05e4195d91c13fc0821f8d Mon Sep 17 00:00:00 2001 From: han Date: Thu, 4 Jan 2024 19:44:31 +0800 Subject: [PATCH 07/10] create DeadlineRestAPIExecutor --- .../service/DeadlineCommandExecutor.java | 204 +---------------- .../service/DeadlineRestAPIExecutor.java | 206 ++++++++++++++++++ ....java => DeadlineRestAPIExecutorTest.java} | 22 +- 3 files changed, 220 insertions(+), 212 deletions(-) create mode 100644 src/main/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutor.java rename src/test/java/com/gearbox/platform/deadline/service/{DeadlineCommandExecutorTest.java => DeadlineRestAPIExecutorTest.java} (73%) diff --git a/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java b/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java index 62ccae5..6d65adb 100644 --- a/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java +++ b/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java @@ -1,46 +1,19 @@ package com.gearbox.platform.deadline.service; -import com.fasterxml.jackson.core.JsonProcessingException; -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; import com.gearbox.core.constant.DeadlineInstanceStatusEnum; import com.gearbox.core.constant.NodeStatusEnum; import com.gearbox.core.model.Node; import com.gearbox.core.model.shell.ShellResult; -import com.gearbox.core.task.CacheRefreshTask; import com.gearbox.core.util.CmdExecuteResultAssertion; import com.gearbox.core.util.ShellExecutor; -import okhttp3.*; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.util.CollectionUtils; -import java.io.IOException; -import java.util.*; -import java.util.concurrent.TimeUnit; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; +import java.util.Locale; public class DeadlineCommandExecutor { - private static final String EQUAL_SIGN = "="; - private static OkHttpClient client; - private static ObjectMapper objectMapper; - private static final Logger LOG = LoggerFactory.getLogger(CacheRefreshTask.class); - private static final HashMap statMap = new HashMap() { - { - put(0, "Unknown"); - put(1, "Rendering"); - put(2, "Idle"); - put(3, "Offline"); - put(4, "Stalled"); - put(8, "StartingJob"); - } - }; - - static { - client = new OkHttpClient().newBuilder().connectionPool(new ConnectionPool(5, 10, TimeUnit.MILLISECONDS)) - .readTimeout(60, TimeUnit.SECONDS).writeTimeout(60, TimeUnit.SECONDS).build(); - objectMapper = new ObjectMapper(); - } public static List getSlaveNames() { ShellResult result = ShellExecutor.runCommand(String.format(Locale.ROOT, "deadlinecommand -GetSlaveNames")); @@ -127,173 +100,4 @@ public class DeadlineCommandExecutor { CmdExecuteResultAssertion.assertError(result); return Integer.parseInt(result.getReturnValues().get(0)); } - - public static List getSlaveNamesRestAPI() { - String url = "http://localhost:8081/api/slaves?NamesOnly=true"; - Request request = new Request.Builder().url(url).build(); - List list = null; - try { - Response resp = client.newCall(request).execute(); - String bodyStr = resp.body().string(); - list = objectMapper.readValue(bodyStr, new TypeReference>() { - }); - } catch (IOException e) { - LOG.error("use RestAPI get getSlaveNames failed"); - } - if (!CollectionUtils.isEmpty(list)) { - return list; - } - return Collections.emptyList(); - } - - public static String getSlaveStatusByNameRestAPI(String name) { - String url = String.format(Locale.ROOT, "http://localhost:8081/api/slaves?Data=info&Name=%s", name); - Request request = new Request.Builder().url(url).build(); - List> list = null; - try { - Response resp = client.newCall(request).execute(); - String bodyStr = resp.body().string(); - list = objectMapper.readValue(bodyStr, new TypeReference>>() { - }); - } catch (IOException e) { - LOG.error("use RestAPI get getSlaveStatusByName failed"); - } - if (!CollectionUtils.isEmpty(list)) { - Integer stat = (Integer) list.get(0).get("Stat"); - return statMap.get(stat); - } - return statMap.get(0); - } - - public static List getSlaveInfosRestAPI() { - List nodeList = new ArrayList<>(); - String url = "http://localhost:8081/api/slaves"; - Request request = new Request.Builder().url(url).build(); - try { - Response resp = client.newCall(request).execute(); - String bodyStr = resp.body().string(); - List> list = objectMapper.readValue(bodyStr, new TypeReference>>() { - }); - for (HashMap map : list) { - LinkedHashMap infoMap = (LinkedHashMap) map.get("Info"); - String nodeName = (String) infoMap.get("Name"); - Integer stat = (Integer) infoMap.get("Stat"); - String slaveStatus = statMap.get(stat); - HashMap settingsMap = (HashMap) map.get("Settings"); - boolean slaveEnabled = (boolean) settingsMap.get("Enable"); - nodeList.add(new Node(nodeName, NodeStatusEnum.toNodeStatus(DeadlineInstanceStatusEnum.value(slaveStatus)), slaveEnabled)); - } - } catch (IOException e) { - LOG.error("use RestAPI get getSlaveInfos failed"); - } - return nodeList; - } - - public static boolean isSlaveEnabledRestAPI(String name) { - String url = String.format(Locale.ROOT, "http://localhost:8081/api/slaves?Data=settings&Name=%s", name); - Request request = new Request.Builder().url(url).build(); - boolean enable = false; - List> list = null; - try { - Response resp = client.newCall(request).execute(); - String bodyStr = resp.body().string(); - list = objectMapper.readValue(bodyStr, new TypeReference>>() { - }); - - } catch (IOException e) { - LOG.error("use RestAPI get SlaveEnabled failed"); - } - if (!CollectionUtils.isEmpty(list)) { - enable = (boolean) list.get(0).get("Enable"); - } - return enable; - } - - public static String disableSlaveRestAPI(String name) { - String url = "http://localhost:8081/api/slaves"; - HashMap reqMap = new HashMap<>(); - reqMap.put("Command", "savesettings"); - HashMap settingsMap = new HashMap<>(); - settingsMap.put("Name", name); - settingsMap.put("Enable", false); - reqMap.put("SlaveSettings", settingsMap); - String res = "fail"; - try { - String json = objectMapper.writeValueAsString(reqMap); - MediaType JSON = MediaType.parse("application/json; charset=utf-8"); - RequestBody body = RequestBody.create(json, JSON); - Request request = new Request.Builder().url(url).put(body).build(); - Response response = client.newCall(request).execute(); - res = response.body().string(); - } catch (IOException e) { - LOG.error("use RestAPI get disableSlave failed"); - } - return res; - } - - public static String deleteSlaveRestAPI(String name) { - String url = String.format(Locale.ROOT, "http://localhost:8081/api/slaves?Name=%s", name); - Request request = new Request.Builder().url(url).delete().build(); - String res = null; - try { - Response execute = client.newCall(request).execute(); - res = execute.body().string(); - } catch (IOException e) { - LOG.error("use RestAPI get deleteSlave failed"); - } - return res; - } - - public static List listWaitingJobIdsRestAPI() { - String url = "http://localhost:8081/api/jobs?IdOnly=true&States=Active"; - Request request = new Request.Builder().url(url).get().build(); - List list = null; - try { - Response execute = client.newCall(request).execute(); - String json = execute.body().string(); - list = objectMapper.readValue(json, new TypeReference>() { - }); - } catch (IOException e) { - LOG.error("use RestAPI get listWaitingJobIds failed"); - } - return list; - } - - public static String getJobUserNameRestAPI(String jobId) { - HashMap job = getJobByJobIdRestAPI(jobId); - String res = null; - if (!CollectionUtils.isEmpty(job)) { - HashMap props = (HashMap) job.get("Props"); - res = (String) props.get("User"); - } - return res; - } - - public static int getJobQueueChunksRestAPI(String jobId) { - HashMap job = getJobByJobIdRestAPI(jobId); - Integer num = 0; - if (!CollectionUtils.isEmpty(job)) { - num = (Integer) job.get("QueuedChunks"); - } - return num; - } - - public static HashMap getJobByJobIdRestAPI(String jobId) { - String url = String.format(Locale.ROOT, "http://localhost:8081/api/jobs?JobID=%s", jobId); - Request request = new Request.Builder().url(url).get().build(); - List> list = null; - try { - Response execute = client.newCall(request).execute(); - String json = execute.body().string(); - list = objectMapper.readValue(json, new TypeReference>>() { - }); - } catch (IOException e) { - LOG.error("use RestAPI get getJobByJobId failed"); - } - if (!CollectionUtils.isEmpty(list)) { - return list.get(0); - } - return new HashMap<>(); - } - } diff --git a/src/main/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutor.java b/src/main/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutor.java new file mode 100644 index 0000000..5e3b39e --- /dev/null +++ b/src/main/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutor.java @@ -0,0 +1,206 @@ +package com.gearbox.platform.deadline.service; + +import com.fasterxml.jackson.core.type.TypeReference; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.gearbox.core.constant.DeadlineInstanceStatusEnum; +import com.gearbox.core.constant.NodeStatusEnum; +import com.gearbox.core.model.Node; +import com.gearbox.core.task.CacheRefreshTask; +import okhttp3.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.util.CollectionUtils; + +import java.io.IOException; +import java.util.*; +import java.util.concurrent.TimeUnit; + +public class DeadlineRestAPIExecutor { + private static OkHttpClient client; + private static ObjectMapper objectMapper; + private static final Logger LOG = LoggerFactory.getLogger(CacheRefreshTask.class); + private static final HashMap statMap = new HashMap() { + { + put(0, "Unknown"); + put(1, "Rendering"); + put(2, "Idle"); + put(3, "Offline"); + put(4, "Stalled"); + put(8, "StartingJob"); + } + }; + + static { + client = new OkHttpClient().newBuilder().connectionPool(new ConnectionPool(5, 10, TimeUnit.MILLISECONDS)) + .readTimeout(60, TimeUnit.SECONDS).writeTimeout(60, TimeUnit.SECONDS).build(); + objectMapper = new ObjectMapper(); + } + + public static List getSlaveNamesRestAPI() { + String url = "http://localhost:8081/api/slaves?NamesOnly=true"; + Request request = new Request.Builder().url(url).build(); + List list = null; + try { + Response resp = client.newCall(request).execute(); + String bodyStr = resp.body().string(); + list = objectMapper.readValue(bodyStr, new TypeReference>() { + }); + } catch (IOException e) { + LOG.error("use RestAPI get getSlaveNames failed"); + } + if (!CollectionUtils.isEmpty(list)) { + return list; + } + return Collections.emptyList(); + } + + public static String getSlaveStatusByNameRestAPI(String name) { + String url = String.format(Locale.ROOT, "http://localhost:8081/api/slaves?Data=info&Name=%s", name); + Request request = new Request.Builder().url(url).build(); + List> list = null; + try { + Response resp = client.newCall(request).execute(); + String bodyStr = resp.body().string(); + list = objectMapper.readValue(bodyStr, new TypeReference>>() { + }); + } catch (IOException e) { + LOG.error("use RestAPI get getSlaveStatusByName failed"); + } + if (!CollectionUtils.isEmpty(list)) { + Integer stat = (Integer) list.get(0).get("Stat"); + return statMap.get(stat); + } + return statMap.get(0); + } + + public static List getSlaveInfosRestAPI() { + List nodeList = new ArrayList<>(); + String url = "http://localhost:8081/api/slaves"; + Request request = new Request.Builder().url(url).build(); + try { + Response resp = client.newCall(request).execute(); + String bodyStr = resp.body().string(); + List> list = objectMapper.readValue(bodyStr, new TypeReference>>() { + }); + for (HashMap map : list) { + LinkedHashMap infoMap = (LinkedHashMap) map.get("Info"); + String nodeName = (String) infoMap.get("Name"); + Integer stat = (Integer) infoMap.get("Stat"); + String slaveStatus = statMap.get(stat); + HashMap settingsMap = (HashMap) map.get("Settings"); + boolean slaveEnabled = (boolean) settingsMap.get("Enable"); + nodeList.add(new Node(nodeName, NodeStatusEnum.toNodeStatus(DeadlineInstanceStatusEnum.value(slaveStatus)), slaveEnabled)); + } + } catch (IOException e) { + LOG.error("use RestAPI get getSlaveInfos failed"); + } + return nodeList; + } + + public static boolean isSlaveEnabledRestAPI(String name) { + String url = String.format(Locale.ROOT, "http://localhost:8081/api/slaves?Data=settings&Name=%s", name); + Request request = new Request.Builder().url(url).build(); + boolean enable = false; + List> list = null; + try { + Response resp = client.newCall(request).execute(); + String bodyStr = resp.body().string(); + list = objectMapper.readValue(bodyStr, new TypeReference>>() { + }); + + } catch (IOException e) { + LOG.error("use RestAPI get SlaveEnabled failed"); + } + if (!CollectionUtils.isEmpty(list)) { + enable = (boolean) list.get(0).get("Enable"); + } + return enable; + } + + public static String disableSlaveRestAPI(String name) { + String url = "http://localhost:8081/api/slaves"; + HashMap reqMap = new HashMap<>(); + reqMap.put("Command", "savesettings"); + HashMap settingsMap = new HashMap<>(); + settingsMap.put("Name", name); + settingsMap.put("Enable", false); + reqMap.put("SlaveSettings", settingsMap); + String res = "fail"; + try { + String json = objectMapper.writeValueAsString(reqMap); + MediaType JSON = MediaType.parse("application/json; charset=utf-8"); + RequestBody body = RequestBody.create(json, JSON); + Request request = new Request.Builder().url(url).put(body).build(); + Response response = client.newCall(request).execute(); + res = response.body().string(); + } catch (IOException e) { + LOG.error("use RestAPI get disableSlave failed"); + } + return res; + } + + public static String deleteSlaveRestAPI(String name) { + String url = String.format(Locale.ROOT, "http://localhost:8081/api/slaves?Name=%s", name); + Request request = new Request.Builder().url(url).delete().build(); + String res = null; + try { + Response execute = client.newCall(request).execute(); + res = execute.body().string(); + } catch (IOException e) { + LOG.error("use RestAPI get deleteSlave failed"); + } + return res; + } + + public static List listWaitingJobIdsRestAPI() { + String url = "http://localhost:8081/api/jobs?IdOnly=true&States=Active"; + Request request = new Request.Builder().url(url).get().build(); + List list = null; + try { + Response execute = client.newCall(request).execute(); + String json = execute.body().string(); + list = objectMapper.readValue(json, new TypeReference>() { + }); + } catch (IOException e) { + LOG.error("use RestAPI get listWaitingJobIds failed"); + } + return list; + } + + public static String getJobUserNameRestAPI(String jobId) { + HashMap job = getJobByJobIdRestAPI(jobId); + String res = null; + if (!CollectionUtils.isEmpty(job)) { + HashMap props = (HashMap) job.get("Props"); + res = (String) props.get("User"); + } + return res; + } + + public static int getJobQueueChunksRestAPI(String jobId) { + HashMap job = getJobByJobIdRestAPI(jobId); + Integer num = 0; + if (!CollectionUtils.isEmpty(job)) { + num = (Integer) job.get("QueuedChunks"); + } + return num; + } + + public static HashMap getJobByJobIdRestAPI(String jobId) { + String url = String.format(Locale.ROOT, "http://localhost:8081/api/jobs?JobID=%s", jobId); + Request request = new Request.Builder().url(url).get().build(); + List> list = null; + try { + Response execute = client.newCall(request).execute(); + String json = execute.body().string(); + list = objectMapper.readValue(json, new TypeReference>>() { + }); + } catch (IOException e) { + LOG.error("use RestAPI get getJobByJobId failed"); + } + if (!CollectionUtils.isEmpty(list)) { + return list.get(0); + } + return new HashMap<>(); + } +} diff --git a/src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java b/src/test/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutorTest.java similarity index 73% rename from src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java rename to src/test/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutorTest.java index 7cf06d3..36100c4 100644 --- a/src/test/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutorTest.java +++ b/src/test/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutorTest.java @@ -6,70 +6,68 @@ import okhttp3.ConnectionPool; import okhttp3.OkHttpClient; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import org.junit.platform.commons.annotation.Testable; -import java.io.IOException; import java.util.List; import java.util.concurrent.TimeUnit; -public class DeadlineCommandExecutorTest { +public class DeadlineRestAPIExecutorTest { OkHttpClient client = new OkHttpClient().newBuilder().connectionPool(new ConnectionPool(5, 10, TimeUnit.MILLISECONDS)) .readTimeout(60, TimeUnit.SECONDS).writeTimeout(60, TimeUnit.SECONDS).build(); ObjectMapper objectMapper = new ObjectMapper(); @Test public void testGetSlaveInfosRestAPI() { - List slaveInfosRestAPI = DeadlineCommandExecutor.getSlaveInfosRestAPI(); + List slaveInfosRestAPI = DeadlineRestAPIExecutor.getSlaveInfosRestAPI(); Assertions.assertEquals(slaveInfosRestAPI.size(), 1); } @Test public void testGetSlaveNamesRestAPI() { - List slaveNamesRestAPI = DeadlineCommandExecutor.getSlaveNamesRestAPI(); + List slaveNamesRestAPI = DeadlineRestAPIExecutor.getSlaveNamesRestAPI(); Assertions.assertEquals(slaveNamesRestAPI.get(0), "worker-192"); } @Test public void testGetSlaveStatusByNameRestAPI() { - String slaveStatusByNameRestAPI = DeadlineCommandExecutor.getSlaveStatusByNameRestAPI("worker-192"); + String slaveStatusByNameRestAPI = DeadlineRestAPIExecutor.getSlaveStatusByNameRestAPI("worker-192"); Assertions.assertEquals(slaveStatusByNameRestAPI, "Idle"); } @Test public void testIsSlaveEnabled() { - boolean slaveEnabledRestAPI = DeadlineCommandExecutor.isSlaveEnabledRestAPI("worker-192"); + boolean slaveEnabledRestAPI = DeadlineRestAPIExecutor.isSlaveEnabledRestAPI("worker-192"); Assertions.assertEquals(slaveEnabledRestAPI, true); } @Test public void testDisableSlave() { - String s = DeadlineCommandExecutor.disableSlaveRestAPI("worker-192"); + String s = DeadlineRestAPIExecutor.disableSlaveRestAPI("worker-192"); Assertions.assertEquals(s, "Success"); } @Test public void testDeleteSlaveRestAPI() { - String s = DeadlineCommandExecutor.deleteSlaveRestAPI("worker-192"); + String s = DeadlineRestAPIExecutor.deleteSlaveRestAPI("worker-192"); Assertions.assertEquals(s,"Success"); } @Test public void testListWaitingJobIdsRestAPI() { - List list = DeadlineCommandExecutor.listWaitingJobIdsRestAPI(); + List list = DeadlineRestAPIExecutor.listWaitingJobIdsRestAPI(); Assertions.assertEquals(list.size(),12); } @Test public void testGetJobUserNameRestAPI(){ - String jobUserNameRestAPI = DeadlineCommandExecutor.getJobUserNameRestAPI("6596960866f66719ec4e5619"); + String jobUserNameRestAPI = DeadlineRestAPIExecutor.getJobUserNameRestAPI("6596960866f66719ec4e5619"); Assertions.assertEquals(jobUserNameRestAPI,"administrator"); } @Test public void testGetJobQueueChunksRestAPI(){ - int jobQueueChunksRestAPI = DeadlineCommandExecutor.getJobQueueChunksRestAPI("6596960866f66719ec4e5619"); + int jobQueueChunksRestAPI = DeadlineRestAPIExecutor.getJobQueueChunksRestAPI("6596960866f66719ec4e5619"); Assertions.assertEquals(jobQueueChunksRestAPI,23); } } -- Gitee From 8f05fb9db7f0e3293ac013f82829d05e2e2f02f7 Mon Sep 17 00:00:00 2001 From: han Date: Thu, 4 Jan 2024 20:41:18 +0800 Subject: [PATCH 08/10] add DeadlineRestAPIExecutor to DeadlineCommandExecutor --- .../service/DeadlineCommandExecutor.java | 27 ++++++++++++ .../service/DeadlineRestAPIExecutor.java | 42 ++++++++++++------- .../service/DeadlineRestAPIExecutorTest.java | 22 +++++++--- 3 files changed, 71 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java b/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java index 6d65adb..dc67cec 100644 --- a/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java +++ b/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java @@ -16,12 +16,18 @@ public class DeadlineCommandExecutor { private static final String EQUAL_SIGN = "="; public static List getSlaveNames() { + if (DeadlineRestAPIExecutor.isUseRestAPI) { + return DeadlineRestAPIExecutor.getSlaveNamesRestAPI(); + } ShellResult result = ShellExecutor.runCommand(String.format(Locale.ROOT, "deadlinecommand -GetSlaveNames")); CmdExecuteResultAssertion.assertError(result); return result.getReturnValues(); } public static String getSlaveStatusByName(String slaveName) { + if (DeadlineRestAPIExecutor.isUseRestAPI) { + return DeadlineRestAPIExecutor.getSlaveStatusByNameRestAPI(slaveName); + } ShellResult result = ShellExecutor.runCommand( String.format(Locale.ROOT, "deadlinecommand -GetSlaveInfo %s slavestatus", slaveName)); CmdExecuteResultAssertion.assertError(result); @@ -29,6 +35,9 @@ public class DeadlineCommandExecutor { } public static List getSlaveInfos() { + if (DeadlineRestAPIExecutor.isUseRestAPI) { + return DeadlineRestAPIExecutor.getSlaveInfosRestAPI(); + } ShellResult result = ShellExecutor.runCommand("deadlinecommand -GetSlaves"); CmdExecuteResultAssertion.assertError(result); List nodeList = new ArrayList<>(); @@ -62,6 +71,9 @@ public class DeadlineCommandExecutor { } public static boolean isSlaveEnabled(String slaveName) { + if (DeadlineRestAPIExecutor.isUseRestAPI) { + return DeadlineRestAPIExecutor.isSlaveEnabledRestAPI(slaveName); + } ShellResult result = ShellExecutor.runCommand( String.format(Locale.ROOT, "deadlinecommand -GetSlaveSetting %s SlaveEnabled", slaveName)); CmdExecuteResultAssertion.assertError(result); @@ -69,18 +81,27 @@ public class DeadlineCommandExecutor { } public static void deleteSlave(String slaveName) { + if (DeadlineRestAPIExecutor.isUseRestAPI) { + DeadlineRestAPIExecutor.deleteSlaveRestAPI(slaveName); + } String cmd = String.format(Locale.ROOT, "deadlinecommand -DeleteSlave %s", slaveName); ShellResult result = ShellExecutor.runCommand(cmd); CmdExecuteResultAssertion.assertError(result); } public static void disableSlave(String slaveName) { + if (DeadlineRestAPIExecutor.isUseRestAPI) { + DeadlineRestAPIExecutor.disableSlaveRestAPI(slaveName); + } String cmd = String.format(Locale.ROOT, "deadlinecommand -setslavesetting %s slaveenabled false", slaveName); ShellResult result = ShellExecutor.runCommand(cmd); CmdExecuteResultAssertion.assertError(result); } public static List listWaitingJobIds() { + if (DeadlineRestAPIExecutor.isUseRestAPI) { + return DeadlineRestAPIExecutor.listWaitingJobIdsRestAPI(); + } String getWaitingJobCmd = "deadlinecommand -GetJobIdsFilter status=queued status=rendering"; ShellResult result = ShellExecutor.runCommand(getWaitingJobCmd); CmdExecuteResultAssertion.assertError(result); @@ -88,6 +109,9 @@ public class DeadlineCommandExecutor { } public static String getJobUserName(String jobId) { + if (DeadlineRestAPIExecutor.isUseRestAPI) { + return DeadlineRestAPIExecutor.getJobUserNameRestAPI(jobId); + } String cmd = String.format(Locale.ROOT, "deadlinecommand -GetJobSetting %s UserName", jobId); ShellResult result = ShellExecutor.runCommand(cmd); CmdExecuteResultAssertion.assertError(result); @@ -95,6 +119,9 @@ public class DeadlineCommandExecutor { } public static int getJobQueuedChunks(String jobId) { + if (DeadlineRestAPIExecutor.isUseRestAPI) { + return DeadlineRestAPIExecutor.getJobQueueChunksRestAPI(jobId); + } String cmd = String.format(Locale.ROOT, "deadlinecommand -GetJobSetting %s QueuedChunks", jobId); ShellResult result = ShellExecutor.runCommand(cmd); CmdExecuteResultAssertion.assertError(result); diff --git a/src/main/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutor.java b/src/main/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutor.java index 5e3b39e..0599e3e 100644 --- a/src/main/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutor.java +++ b/src/main/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutor.java @@ -12,6 +12,7 @@ import org.slf4j.LoggerFactory; import org.springframework.util.CollectionUtils; import java.io.IOException; +import java.net.ServerSocket; import java.util.*; import java.util.concurrent.TimeUnit; @@ -19,6 +20,7 @@ public class DeadlineRestAPIExecutor { private static OkHttpClient client; private static ObjectMapper objectMapper; private static final Logger LOG = LoggerFactory.getLogger(CacheRefreshTask.class); + public static boolean isUseRestAPI = false; private static final HashMap statMap = new HashMap() { { put(0, "Unknown"); @@ -34,6 +36,7 @@ public class DeadlineRestAPIExecutor { client = new OkHttpClient().newBuilder().connectionPool(new ConnectionPool(5, 10, TimeUnit.MILLISECONDS)) .readTimeout(60, TimeUnit.SECONDS).writeTimeout(60, TimeUnit.SECONDS).build(); objectMapper = new ObjectMapper(); + isUseRestAPI = checkWebServiceIsRunning(); } public static List getSlaveNamesRestAPI() { @@ -41,8 +44,8 @@ public class DeadlineRestAPIExecutor { Request request = new Request.Builder().url(url).build(); List list = null; try { - Response resp = client.newCall(request).execute(); - String bodyStr = resp.body().string(); + Response response = client.newCall(request).execute(); + String bodyStr = response.body().string(); list = objectMapper.readValue(bodyStr, new TypeReference>() { }); } catch (IOException e) { @@ -59,8 +62,8 @@ public class DeadlineRestAPIExecutor { Request request = new Request.Builder().url(url).build(); List> list = null; try { - Response resp = client.newCall(request).execute(); - String bodyStr = resp.body().string(); + Response response = client.newCall(request).execute(); + String bodyStr = response.body().string(); list = objectMapper.readValue(bodyStr, new TypeReference>>() { }); } catch (IOException e) { @@ -78,8 +81,8 @@ public class DeadlineRestAPIExecutor { String url = "http://localhost:8081/api/slaves"; Request request = new Request.Builder().url(url).build(); try { - Response resp = client.newCall(request).execute(); - String bodyStr = resp.body().string(); + Response response = client.newCall(request).execute(); + String bodyStr = response.body().string(); List> list = objectMapper.readValue(bodyStr, new TypeReference>>() { }); for (HashMap map : list) { @@ -103,8 +106,8 @@ public class DeadlineRestAPIExecutor { boolean enable = false; List> list = null; try { - Response resp = client.newCall(request).execute(); - String bodyStr = resp.body().string(); + Response response = client.newCall(request).execute(); + String bodyStr = response.body().string(); list = objectMapper.readValue(bodyStr, new TypeReference>>() { }); @@ -144,8 +147,8 @@ public class DeadlineRestAPIExecutor { Request request = new Request.Builder().url(url).delete().build(); String res = null; try { - Response execute = client.newCall(request).execute(); - res = execute.body().string(); + Response response = client.newCall(request).execute(); + res = response.body().string(); } catch (IOException e) { LOG.error("use RestAPI get deleteSlave failed"); } @@ -157,8 +160,8 @@ public class DeadlineRestAPIExecutor { Request request = new Request.Builder().url(url).get().build(); List list = null; try { - Response execute = client.newCall(request).execute(); - String json = execute.body().string(); + Response response = client.newCall(request).execute(); + String json = response.body().string(); list = objectMapper.readValue(json, new TypeReference>() { }); } catch (IOException e) { @@ -191,8 +194,8 @@ public class DeadlineRestAPIExecutor { Request request = new Request.Builder().url(url).get().build(); List> list = null; try { - Response execute = client.newCall(request).execute(); - String json = execute.body().string(); + Response response = client.newCall(request).execute(); + String json = response.body().string(); list = objectMapper.readValue(json, new TypeReference>>() { }); } catch (IOException e) { @@ -203,4 +206,15 @@ public class DeadlineRestAPIExecutor { } return new HashMap<>(); } + + //通过检测webservice端口号8081是否被占用来判断webservice服务是否运行。若端口被占用,则程序会捕获异常,返回true表示webservice服务正在运行。 + public static boolean checkWebServiceIsRunning() { + try { + ServerSocket serverSocket = new ServerSocket(8081); + serverSocket.close(); + return false; + } catch (IOException e) { + return true; + } + } } diff --git a/src/test/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutorTest.java b/src/test/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutorTest.java index 36100c4..6732a35 100644 --- a/src/test/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutorTest.java +++ b/src/test/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutorTest.java @@ -1,12 +1,16 @@ package com.gearbox.platform.deadline.service; +import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.gearbox.core.model.Node; import okhttp3.ConnectionPool; import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.io.IOException; import java.util.List; import java.util.concurrent.TimeUnit; @@ -49,25 +53,31 @@ public class DeadlineRestAPIExecutorTest { @Test public void testDeleteSlaveRestAPI() { String s = DeadlineRestAPIExecutor.deleteSlaveRestAPI("worker-192"); - Assertions.assertEquals(s,"Success"); + Assertions.assertEquals(s, "Success"); } @Test public void testListWaitingJobIdsRestAPI() { List list = DeadlineRestAPIExecutor.listWaitingJobIdsRestAPI(); - Assertions.assertEquals(list.size(),12); + Assertions.assertEquals(list.size(), 12); } @Test - public void testGetJobUserNameRestAPI(){ + public void testGetJobUserNameRestAPI() { String jobUserNameRestAPI = DeadlineRestAPIExecutor.getJobUserNameRestAPI("6596960866f66719ec4e5619"); - Assertions.assertEquals(jobUserNameRestAPI,"administrator"); + Assertions.assertEquals(jobUserNameRestAPI, "administrator"); } @Test - public void testGetJobQueueChunksRestAPI(){ + public void testGetJobQueueChunksRestAPI() { int jobQueueChunksRestAPI = DeadlineRestAPIExecutor.getJobQueueChunksRestAPI("6596960866f66719ec4e5619"); - Assertions.assertEquals(jobQueueChunksRestAPI,23); + Assertions.assertEquals(jobQueueChunksRestAPI, 23); + } + + @Test + public void testIsUseRestAPI() { + boolean b = DeadlineRestAPIExecutor.checkWebServiceIsRunning(); + Assertions.assertEquals(b, true); } } -- Gitee From c72c7365236e9ae4f3f2ce7905cdc0ace086b7a9 Mon Sep 17 00:00:00 2001 From: han Date: Fri, 5 Jan 2024 09:26:48 +0800 Subject: [PATCH 09/10] =?UTF-8?q?=E4=BC=98=E5=8C=96DeadlineRestAPIExecutor?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service/DeadlineCommandExecutor.java | 18 ++++++------- .../service/DeadlineRestAPIExecutor.java | 25 +++++++++--------- .../service/DeadlineRestAPIExecutorTest.java | 26 ++++++++----------- 3 files changed, 32 insertions(+), 37 deletions(-) diff --git a/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java b/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java index dc67cec..9654dac 100644 --- a/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java +++ b/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java @@ -17,7 +17,7 @@ public class DeadlineCommandExecutor { public static List getSlaveNames() { if (DeadlineRestAPIExecutor.isUseRestAPI) { - return DeadlineRestAPIExecutor.getSlaveNamesRestAPI(); + return DeadlineRestAPIExecutor.getSlaveNames(); } ShellResult result = ShellExecutor.runCommand(String.format(Locale.ROOT, "deadlinecommand -GetSlaveNames")); CmdExecuteResultAssertion.assertError(result); @@ -26,7 +26,7 @@ public class DeadlineCommandExecutor { public static String getSlaveStatusByName(String slaveName) { if (DeadlineRestAPIExecutor.isUseRestAPI) { - return DeadlineRestAPIExecutor.getSlaveStatusByNameRestAPI(slaveName); + return DeadlineRestAPIExecutor.getSlaveStatusByName(slaveName); } ShellResult result = ShellExecutor.runCommand( String.format(Locale.ROOT, "deadlinecommand -GetSlaveInfo %s slavestatus", slaveName)); @@ -36,7 +36,7 @@ public class DeadlineCommandExecutor { public static List getSlaveInfos() { if (DeadlineRestAPIExecutor.isUseRestAPI) { - return DeadlineRestAPIExecutor.getSlaveInfosRestAPI(); + return DeadlineRestAPIExecutor.getSlaveInfos(); } ShellResult result = ShellExecutor.runCommand("deadlinecommand -GetSlaves"); CmdExecuteResultAssertion.assertError(result); @@ -72,7 +72,7 @@ public class DeadlineCommandExecutor { public static boolean isSlaveEnabled(String slaveName) { if (DeadlineRestAPIExecutor.isUseRestAPI) { - return DeadlineRestAPIExecutor.isSlaveEnabledRestAPI(slaveName); + return DeadlineRestAPIExecutor.isSlaveEnabled(slaveName); } ShellResult result = ShellExecutor.runCommand( String.format(Locale.ROOT, "deadlinecommand -GetSlaveSetting %s SlaveEnabled", slaveName)); @@ -82,7 +82,7 @@ public class DeadlineCommandExecutor { public static void deleteSlave(String slaveName) { if (DeadlineRestAPIExecutor.isUseRestAPI) { - DeadlineRestAPIExecutor.deleteSlaveRestAPI(slaveName); + DeadlineRestAPIExecutor.deleteSlave(slaveName); } String cmd = String.format(Locale.ROOT, "deadlinecommand -DeleteSlave %s", slaveName); ShellResult result = ShellExecutor.runCommand(cmd); @@ -91,7 +91,7 @@ public class DeadlineCommandExecutor { public static void disableSlave(String slaveName) { if (DeadlineRestAPIExecutor.isUseRestAPI) { - DeadlineRestAPIExecutor.disableSlaveRestAPI(slaveName); + DeadlineRestAPIExecutor.disableSlave(slaveName); } String cmd = String.format(Locale.ROOT, "deadlinecommand -setslavesetting %s slaveenabled false", slaveName); ShellResult result = ShellExecutor.runCommand(cmd); @@ -100,7 +100,7 @@ public class DeadlineCommandExecutor { public static List listWaitingJobIds() { if (DeadlineRestAPIExecutor.isUseRestAPI) { - return DeadlineRestAPIExecutor.listWaitingJobIdsRestAPI(); + return DeadlineRestAPIExecutor.listWaitingJobIds(); } String getWaitingJobCmd = "deadlinecommand -GetJobIdsFilter status=queued status=rendering"; ShellResult result = ShellExecutor.runCommand(getWaitingJobCmd); @@ -110,7 +110,7 @@ public class DeadlineCommandExecutor { public static String getJobUserName(String jobId) { if (DeadlineRestAPIExecutor.isUseRestAPI) { - return DeadlineRestAPIExecutor.getJobUserNameRestAPI(jobId); + return DeadlineRestAPIExecutor.getJobUserName(jobId); } String cmd = String.format(Locale.ROOT, "deadlinecommand -GetJobSetting %s UserName", jobId); ShellResult result = ShellExecutor.runCommand(cmd); @@ -120,7 +120,7 @@ public class DeadlineCommandExecutor { public static int getJobQueuedChunks(String jobId) { if (DeadlineRestAPIExecutor.isUseRestAPI) { - return DeadlineRestAPIExecutor.getJobQueueChunksRestAPI(jobId); + return DeadlineRestAPIExecutor.getJobQueueChunks(jobId); } String cmd = String.format(Locale.ROOT, "deadlinecommand -GetJobSetting %s QueuedChunks", jobId); ShellResult result = ShellExecutor.runCommand(cmd); diff --git a/src/main/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutor.java b/src/main/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutor.java index 0599e3e..cc99829 100644 --- a/src/main/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutor.java +++ b/src/main/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutor.java @@ -39,7 +39,7 @@ public class DeadlineRestAPIExecutor { isUseRestAPI = checkWebServiceIsRunning(); } - public static List getSlaveNamesRestAPI() { + public static List getSlaveNames() { String url = "http://localhost:8081/api/slaves?NamesOnly=true"; Request request = new Request.Builder().url(url).build(); List list = null; @@ -57,7 +57,7 @@ public class DeadlineRestAPIExecutor { return Collections.emptyList(); } - public static String getSlaveStatusByNameRestAPI(String name) { + public static String getSlaveStatusByName(String name) { String url = String.format(Locale.ROOT, "http://localhost:8081/api/slaves?Data=info&Name=%s", name); Request request = new Request.Builder().url(url).build(); List> list = null; @@ -76,7 +76,7 @@ public class DeadlineRestAPIExecutor { return statMap.get(0); } - public static List getSlaveInfosRestAPI() { + public static List getSlaveInfos() { List nodeList = new ArrayList<>(); String url = "http://localhost:8081/api/slaves"; Request request = new Request.Builder().url(url).build(); @@ -100,7 +100,7 @@ public class DeadlineRestAPIExecutor { return nodeList; } - public static boolean isSlaveEnabledRestAPI(String name) { + public static boolean isSlaveEnabled(String name) { String url = String.format(Locale.ROOT, "http://localhost:8081/api/slaves?Data=settings&Name=%s", name); Request request = new Request.Builder().url(url).build(); boolean enable = false; @@ -110,7 +110,6 @@ public class DeadlineRestAPIExecutor { String bodyStr = response.body().string(); list = objectMapper.readValue(bodyStr, new TypeReference>>() { }); - } catch (IOException e) { LOG.error("use RestAPI get SlaveEnabled failed"); } @@ -120,7 +119,7 @@ public class DeadlineRestAPIExecutor { return enable; } - public static String disableSlaveRestAPI(String name) { + public static String disableSlave(String name) { String url = "http://localhost:8081/api/slaves"; HashMap reqMap = new HashMap<>(); reqMap.put("Command", "savesettings"); @@ -142,7 +141,7 @@ public class DeadlineRestAPIExecutor { return res; } - public static String deleteSlaveRestAPI(String name) { + public static String deleteSlave(String name) { String url = String.format(Locale.ROOT, "http://localhost:8081/api/slaves?Name=%s", name); Request request = new Request.Builder().url(url).delete().build(); String res = null; @@ -155,7 +154,7 @@ public class DeadlineRestAPIExecutor { return res; } - public static List listWaitingJobIdsRestAPI() { + public static List listWaitingJobIds() { String url = "http://localhost:8081/api/jobs?IdOnly=true&States=Active"; Request request = new Request.Builder().url(url).get().build(); List list = null; @@ -170,8 +169,8 @@ public class DeadlineRestAPIExecutor { return list; } - public static String getJobUserNameRestAPI(String jobId) { - HashMap job = getJobByJobIdRestAPI(jobId); + public static String getJobUserName(String jobId) { + HashMap job = getJobByJobId(jobId); String res = null; if (!CollectionUtils.isEmpty(job)) { HashMap props = (HashMap) job.get("Props"); @@ -180,8 +179,8 @@ public class DeadlineRestAPIExecutor { return res; } - public static int getJobQueueChunksRestAPI(String jobId) { - HashMap job = getJobByJobIdRestAPI(jobId); + public static int getJobQueueChunks(String jobId) { + HashMap job = getJobByJobId(jobId); Integer num = 0; if (!CollectionUtils.isEmpty(job)) { num = (Integer) job.get("QueuedChunks"); @@ -189,7 +188,7 @@ public class DeadlineRestAPIExecutor { return num; } - public static HashMap getJobByJobIdRestAPI(String jobId) { + public static HashMap getJobByJobId(String jobId) { String url = String.format(Locale.ROOT, "http://localhost:8081/api/jobs?JobID=%s", jobId); Request request = new Request.Builder().url(url).get().build(); List> list = null; diff --git a/src/test/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutorTest.java b/src/test/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutorTest.java index 6732a35..99f758e 100644 --- a/src/test/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutorTest.java +++ b/src/test/java/com/gearbox/platform/deadline/service/DeadlineRestAPIExecutorTest.java @@ -1,16 +1,12 @@ package com.gearbox.platform.deadline.service; -import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.gearbox.core.model.Node; import okhttp3.ConnectionPool; import okhttp3.OkHttpClient; -import okhttp3.Request; -import okhttp3.Response; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -import java.io.IOException; import java.util.List; import java.util.concurrent.TimeUnit; @@ -22,56 +18,56 @@ public class DeadlineRestAPIExecutorTest { @Test public void testGetSlaveInfosRestAPI() { - List slaveInfosRestAPI = DeadlineRestAPIExecutor.getSlaveInfosRestAPI(); + List slaveInfosRestAPI = DeadlineRestAPIExecutor.getSlaveInfos(); Assertions.assertEquals(slaveInfosRestAPI.size(), 1); } @Test public void testGetSlaveNamesRestAPI() { - List slaveNamesRestAPI = DeadlineRestAPIExecutor.getSlaveNamesRestAPI(); + List slaveNamesRestAPI = DeadlineRestAPIExecutor.getSlaveNames(); Assertions.assertEquals(slaveNamesRestAPI.get(0), "worker-192"); } @Test public void testGetSlaveStatusByNameRestAPI() { - String slaveStatusByNameRestAPI = DeadlineRestAPIExecutor.getSlaveStatusByNameRestAPI("worker-192"); + String slaveStatusByNameRestAPI = DeadlineRestAPIExecutor.getSlaveStatusByName("worker-192"); Assertions.assertEquals(slaveStatusByNameRestAPI, "Idle"); } @Test - public void testIsSlaveEnabled() { - boolean slaveEnabledRestAPI = DeadlineRestAPIExecutor.isSlaveEnabledRestAPI("worker-192"); + public void testIsSlaveEnabledRestAPI() { + boolean slaveEnabledRestAPI = DeadlineRestAPIExecutor.isSlaveEnabled("worker-192"); Assertions.assertEquals(slaveEnabledRestAPI, true); } @Test - public void testDisableSlave() { - String s = DeadlineRestAPIExecutor.disableSlaveRestAPI("worker-192"); + public void testDisableSlaveRestAPI() { + String s = DeadlineRestAPIExecutor.disableSlave("worker-192"); Assertions.assertEquals(s, "Success"); } @Test public void testDeleteSlaveRestAPI() { - String s = DeadlineRestAPIExecutor.deleteSlaveRestAPI("worker-192"); + String s = DeadlineRestAPIExecutor.deleteSlave("worker-192"); Assertions.assertEquals(s, "Success"); } @Test public void testListWaitingJobIdsRestAPI() { - List list = DeadlineRestAPIExecutor.listWaitingJobIdsRestAPI(); + List list = DeadlineRestAPIExecutor.listWaitingJobIds(); Assertions.assertEquals(list.size(), 12); } @Test public void testGetJobUserNameRestAPI() { - String jobUserNameRestAPI = DeadlineRestAPIExecutor.getJobUserNameRestAPI("6596960866f66719ec4e5619"); + String jobUserNameRestAPI = DeadlineRestAPIExecutor.getJobUserName("6596960866f66719ec4e5619"); Assertions.assertEquals(jobUserNameRestAPI, "administrator"); } @Test public void testGetJobQueueChunksRestAPI() { - int jobQueueChunksRestAPI = DeadlineRestAPIExecutor.getJobQueueChunksRestAPI("6596960866f66719ec4e5619"); + int jobQueueChunksRestAPI = DeadlineRestAPIExecutor.getJobQueueChunks("6596960866f66719ec4e5619"); Assertions.assertEquals(jobQueueChunksRestAPI, 23); } -- Gitee From fb45103773f60020359219b42e5198e7688cd0dc Mon Sep 17 00:00:00 2001 From: han Date: Fri, 5 Jan 2024 14:17:30 +0800 Subject: [PATCH 10/10] fix bugs in DeadlineCommandExecutor --- .../platform/deadline/service/DeadlineCommandExecutor.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java b/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java index 9654dac..b9d74cf 100644 --- a/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java +++ b/src/main/java/com/gearbox/platform/deadline/service/DeadlineCommandExecutor.java @@ -83,6 +83,7 @@ public class DeadlineCommandExecutor { public static void deleteSlave(String slaveName) { if (DeadlineRestAPIExecutor.isUseRestAPI) { DeadlineRestAPIExecutor.deleteSlave(slaveName); + return; } String cmd = String.format(Locale.ROOT, "deadlinecommand -DeleteSlave %s", slaveName); ShellResult result = ShellExecutor.runCommand(cmd); @@ -92,6 +93,7 @@ public class DeadlineCommandExecutor { public static void disableSlave(String slaveName) { if (DeadlineRestAPIExecutor.isUseRestAPI) { DeadlineRestAPIExecutor.disableSlave(slaveName); + return; } String cmd = String.format(Locale.ROOT, "deadlinecommand -setslavesetting %s slaveenabled false", slaveName); ShellResult result = ShellExecutor.runCommand(cmd); -- Gitee