From 7fc33498dad4c7396d4f383e0c328e41da1309e3 Mon Sep 17 00:00:00 2001 From: Arati Kakadiya Date: Mon, 4 Nov 2024 10:32:52 -0800 Subject: [PATCH 1/5] Added JSON file for k8s-resources. Made code changes to replace k8s-resources.properties file with k8s-resources.json file. --- .../skaha/skaha-config/k8s-resources.json | 88 +++++++++++++++++++ .../org/opencadc/skaha/context/GetAction.java | 23 ++++- .../skaha/context/ResourceContexts.java | 57 +++++++----- 3 files changed, 141 insertions(+), 27 deletions(-) create mode 100644 deployment/helm/skaha/skaha-config/k8s-resources.json diff --git a/deployment/helm/skaha/skaha-config/k8s-resources.json b/deployment/helm/skaha/skaha-config/k8s-resources.json new file mode 100644 index 00000000..82b27e4b --- /dev/null +++ b/deployment/helm/skaha/skaha-config/k8s-resources.json @@ -0,0 +1,88 @@ +{ + "cores-default-request": 1, + "cores-default-limit": 16, + "cores-default": 1, + "cores-default-headless": 1, + "cores-options": [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16 + ], + "mem-gb-default-request": 4, + "mem-gb-default-limit": 192, + "mem-gb-default": 2, + "mem-gb-default-headless": 4, + "mem-gb-options": [ + 1, + 2, + 4, + 6, + 8, + 10, + 12, + 14, + 16, + 20, + 24, + 26, + 28, + 30, + 32, + 36, + 40, + 44, + 48, + 56, + 64, + 80, + 92, + 112, + 128, + 140, + 170, + 192 + ], + "gpus-options": [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + 21, + 22, + 23, + 24, + 25, + 26, + 27, + 28 + ] +} diff --git a/skaha/src/main/java/org/opencadc/skaha/context/GetAction.java b/skaha/src/main/java/org/opencadc/skaha/context/GetAction.java index e67982a5..8f798365 100644 --- a/skaha/src/main/java/org/opencadc/skaha/context/GetAction.java +++ b/skaha/src/main/java/org/opencadc/skaha/context/GetAction.java @@ -72,6 +72,10 @@ import org.opencadc.skaha.SkahaAction; +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; + /** * Output the resource context information. * @@ -87,10 +91,21 @@ public GetAction() { public void doAction() throws Exception { super.initRequest(); - ResourceContexts rc = new ResourceContexts(); - Gson gson = new GsonBuilder().disableHtmlEscaping().setPrettyPrinting().create(); - String json = gson.toJson(rc); + File propertiesFile = ResourceContexts.getResourcesFile("k8s-resources.json"); + byte[] bytes = getBytes(propertiesFile); syncOutput.setHeader("Content-Type", "application/json"); - syncOutput.getOutputStream().write(json.getBytes()); + syncOutput.getOutputStream().write(bytes); + } + + private static byte[] getBytes(File propertiesFile) throws IOException { + byte[] bytes = new byte[(int) propertiesFile.length()]; + + try (FileInputStream fileInputStream = new FileInputStream(propertiesFile)) { + int bytesRead = fileInputStream.read(bytes); + if (bytesRead != bytes.length) { + throw new IOException("Could not read the entire file: " + propertiesFile.getAbsolutePath()); + } + } + return bytes; } } diff --git a/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java b/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java index 5fc6f244..77193c1e 100644 --- a/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java +++ b/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java @@ -69,9 +69,12 @@ import ca.nrc.cadc.util.MultiValuedProperties; import ca.nrc.cadc.util.PropertiesReader; +import java.io.File; +import java.io.FileReader; import java.util.ArrayList; import java.util.List; +import com.google.gson.*; import org.apache.log4j.Logger; import org.opencadc.skaha.SkahaAction; @@ -100,35 +103,43 @@ public class ResourceContexts { public ResourceContexts() { try { - PropertiesReader reader = new PropertiesReader("k8s-resources.properties"); - MultiValuedProperties mvp = reader.getAllProperties(); - defaultRequestCores = Integer.valueOf(mvp.getFirstPropertyValue("cores-default-request")); - defaultLimitCores = Integer.valueOf(mvp.getFirstPropertyValue("cores-default-limit")); - defaultCores = Integer.valueOf(mvp.getFirstPropertyValue("cores-default")); - defaultCoresHeadless = Integer.valueOf(mvp.getFirstPropertyValue("cores-default-headless")); - defaultRequestRAM = Integer.valueOf(mvp.getFirstPropertyValue("mem-gb-default-request")); - defaultLimitRAM = Integer.valueOf(mvp.getFirstPropertyValue("mem-gb-default-limit")); - defaultRAM = Integer.valueOf(mvp.getFirstPropertyValue("mem-gb-default")); - defaultRAMHeadless = Integer.valueOf(mvp.getFirstPropertyValue("mem-gb-default-headless")); - String cOptions = mvp.getFirstPropertyValue("cores-options"); - String rOptions = mvp.getFirstPropertyValue("mem-gb-options"); - String gOptions = mvp.getFirstPropertyValue("gpus-options"); - - for (String c : cOptions.split(" ")) { - availableCores.add(Integer.valueOf(c)); - } - for (String r : rOptions.split(" ")) { - availableRAM.add(Integer.valueOf(r)); - } - for (String g : gOptions.split(" ")) { - availableGPUs.add(Integer.valueOf(g)); - } + FileReader reader = new FileReader(getResourcesFile("k8s-resources.json")); + JsonElement jsonElement = JsonParser.parseReader(reader); + JsonObject jsonObject = jsonElement.getAsJsonObject(); + + // Extract fields into variables + defaultRequestCores= jsonObject.get("cores-default-request").getAsInt(); + defaultLimitCores = jsonObject.get("cores-default-limit").getAsInt(); + defaultCores = jsonObject.get("cores-default").getAsInt(); + defaultCoresHeadless = jsonObject.get("cores-default-headless").getAsInt(); + defaultRequestRAM = jsonObject.get("mem-gb-default-request").getAsInt(); + defaultLimitRAM = jsonObject.get("mem-gb-default-limit").getAsInt(); + defaultRAM = jsonObject.get("mem-gb-default").getAsInt(); + defaultRAMHeadless = jsonObject.get("mem-gb-default-headless").getAsInt(); + + JsonArray coresOptions = jsonObject.getAsJsonArray("cores-options"); + coresOptions.asList().forEach(coreOption->availableCores.add(coreOption.getAsInt())); + + JsonArray ramOptions = jsonObject.getAsJsonArray("mem-gb-options"); + ramOptions.asList().forEach(ramOption -> availableRAM.add(ramOption.getAsInt())); + + JsonArray gpuOptions = jsonObject.getAsJsonArray("gpus-option"); + gpuOptions.asList().forEach(gpuOption -> availableGPUs.add(gpuOption.getAsInt())); } catch (Exception e) { log.error(e); throw new IllegalStateException("failed reading k8s-resources.properties", e); } } + public static File getResourcesFile(String fileName){ + String configDir = System.getProperty("user.home") + "/config"; + String configDirSystemProperty = PropertiesReader.class.getName() + ".dir"; + if (System.getProperty(configDirSystemProperty) != null) { + configDir = System.getProperty(configDirSystemProperty); + } + return new File(new File(configDir), fileName); + } + public Integer getDefaultRequestCores() { return defaultRequestCores; } From 572c662a0e1eae448d22f4db99007fc7cffeb62d Mon Sep 17 00:00:00 2001 From: Arati Kakadiya Date: Mon, 4 Nov 2024 10:34:55 -0800 Subject: [PATCH 2/5] Added JSON file for k8s-resources. Made code changes to replace k8s-resources.properties file with k8s-resources.json file. --- skaha/src/main/java/org/opencadc/skaha/context/GetAction.java | 3 --- .../main/java/org/opencadc/skaha/context/ResourceContexts.java | 1 - 2 files changed, 4 deletions(-) diff --git a/skaha/src/main/java/org/opencadc/skaha/context/GetAction.java b/skaha/src/main/java/org/opencadc/skaha/context/GetAction.java index 8f798365..9a695427 100644 --- a/skaha/src/main/java/org/opencadc/skaha/context/GetAction.java +++ b/skaha/src/main/java/org/opencadc/skaha/context/GetAction.java @@ -67,9 +67,6 @@ package org.opencadc.skaha.context; -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; - import org.opencadc.skaha.SkahaAction; import java.io.File; diff --git a/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java b/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java index 77193c1e..2cf43639 100644 --- a/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java +++ b/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java @@ -66,7 +66,6 @@ */ package org.opencadc.skaha.context; -import ca.nrc.cadc.util.MultiValuedProperties; import ca.nrc.cadc.util.PropertiesReader; import java.io.File; From a5f655a285cee305a20e347217d39282dfde4b91 Mon Sep 17 00:00:00 2001 From: Arati Kakadiya Date: Thu, 7 Nov 2024 12:02:35 -0800 Subject: [PATCH 3/5] Changes in JSON structure. Code changes according to the changes in JSON structure. --- .../skaha/skaha-config/k8s-resources.json | 178 +++++++++--------- .../skaha/context/ResourceContexts.java | 26 +-- 2 files changed, 106 insertions(+), 98 deletions(-) diff --git a/deployment/helm/skaha/skaha-config/k8s-resources.json b/deployment/helm/skaha/skaha-config/k8s-resources.json index 82b27e4b..09cedec3 100644 --- a/deployment/helm/skaha/skaha-config/k8s-resources.json +++ b/deployment/helm/skaha/skaha-config/k8s-resources.json @@ -1,88 +1,94 @@ { - "cores-default-request": 1, - "cores-default-limit": 16, - "cores-default": 1, - "cores-default-headless": 1, - "cores-options": [ - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 9, - 10, - 11, - 12, - 13, - 14, - 15, - 16 - ], - "mem-gb-default-request": 4, - "mem-gb-default-limit": 192, - "mem-gb-default": 2, - "mem-gb-default-headless": 4, - "mem-gb-options": [ - 1, - 2, - 4, - 6, - 8, - 10, - 12, - 14, - 16, - 20, - 24, - 26, - 28, - 30, - 32, - 36, - 40, - 44, - 48, - 56, - 64, - 80, - 92, - 112, - 128, - 140, - 170, - 192 - ], - "gpus-options": [ - 1, - 2, - 3, - 4, - 5, - 6, - 7, - 8, - 9, - 10, - 11, - 12, - 13, - 14, - 15, - 16, - 17, - 18, - 19, - 20, - 21, - 22, - 23, - 24, - 25, - 26, - 27, - 28 - ] + "cores": { + "default": 1, + "default-request": 1, + "default-limit": 16, + "default-headless": 1, + "options": [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16 + ] + }, + "memory": { + "default-request-gb": 4, + "default-limit-gb": 192, + "default-gb": 2, + "default-headless-gb": 4, + "options-gb": [ + 1, + 2, + 4, + 6, + 8, + 10, + 12, + 14, + 16, + 20, + 24, + 26, + 28, + 30, + 32, + 36, + 40, + 44, + 48, + 56, + 64, + 80, + 92, + 112, + 128, + 140, + 170, + 192 + ] + }, + "gpus": { + "options": [ + 1, + 2, + 3, + 4, + 5, + 6, + 7, + 8, + 9, + 10, + 11, + 12, + 13, + 14, + 15, + 16, + 17, + 18, + 19, + 20, + 21, + 22, + 23, + 24, + 25, + 26, + 27, + 28 + ] + } } diff --git a/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java b/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java index 2cf43639..26fd356a 100644 --- a/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java +++ b/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java @@ -107,22 +107,24 @@ public ResourceContexts() { JsonObject jsonObject = jsonElement.getAsJsonObject(); // Extract fields into variables - defaultRequestCores= jsonObject.get("cores-default-request").getAsInt(); - defaultLimitCores = jsonObject.get("cores-default-limit").getAsInt(); - defaultCores = jsonObject.get("cores-default").getAsInt(); - defaultCoresHeadless = jsonObject.get("cores-default-headless").getAsInt(); - defaultRequestRAM = jsonObject.get("mem-gb-default-request").getAsInt(); - defaultLimitRAM = jsonObject.get("mem-gb-default-limit").getAsInt(); - defaultRAM = jsonObject.get("mem-gb-default").getAsInt(); - defaultRAMHeadless = jsonObject.get("mem-gb-default-headless").getAsInt(); - - JsonArray coresOptions = jsonObject.getAsJsonArray("cores-options"); + JsonObject cores = jsonObject.getAsJsonObject("cores"); + defaultRequestCores= cores.get("default-request").getAsInt(); + defaultLimitCores = cores.get("default-limit").getAsInt(); + defaultCores = cores.get("default").getAsInt(); + defaultCoresHeadless = cores.get("default-headless").getAsInt(); + JsonArray coresOptions = cores.getAsJsonArray("options"); coresOptions.asList().forEach(coreOption->availableCores.add(coreOption.getAsInt())); - JsonArray ramOptions = jsonObject.getAsJsonArray("mem-gb-options"); + JsonObject memory = jsonObject.getAsJsonObject("memory"); + defaultRequestRAM = memory.get("default-request-gb").getAsInt(); + defaultLimitRAM = memory.get("default-limit-gb").getAsInt(); + defaultRAM = memory.get("default-gb").getAsInt(); + defaultRAMHeadless = memory.get("default-headless-gb").getAsInt(); + JsonArray ramOptions = memory.getAsJsonArray("options-gb"); ramOptions.asList().forEach(ramOption -> availableRAM.add(ramOption.getAsInt())); - JsonArray gpuOptions = jsonObject.getAsJsonArray("gpus-option"); + JsonObject gpus = jsonObject.getAsJsonObject("gpus"); + JsonArray gpuOptions = gpus.getAsJsonArray("options"); gpuOptions.asList().forEach(gpuOption -> availableGPUs.add(gpuOption.getAsInt())); } catch (Exception e) { log.error(e); From b7823ec680c01fbfc11d0d17afa7e0ea16270eed Mon Sep 17 00:00:00 2001 From: Arati Kakadiya Date: Thu, 7 Nov 2024 12:21:44 -0800 Subject: [PATCH 4/5] Changes in JSON structure. Code changes according to the changes in JSON structure. --- .../helm/skaha/skaha-config/k8s-resources.json | 18 +++++++++--------- .../skaha/context/ResourceContexts.java | 18 +++++++++--------- 2 files changed, 18 insertions(+), 18 deletions(-) diff --git a/deployment/helm/skaha/skaha-config/k8s-resources.json b/deployment/helm/skaha/skaha-config/k8s-resources.json index 09cedec3..cacd7803 100644 --- a/deployment/helm/skaha/skaha-config/k8s-resources.json +++ b/deployment/helm/skaha/skaha-config/k8s-resources.json @@ -1,9 +1,9 @@ { "cores": { "default": 1, - "default-request": 1, - "default-limit": 16, - "default-headless": 1, + "defaultRequest": 1, + "defaultLimit": 16, + "defaultHeadless": 1, "options": [ 1, 2, @@ -23,12 +23,12 @@ 16 ] }, - "memory": { - "default-request-gb": 4, - "default-limit-gb": 192, - "default-gb": 2, - "default-headless-gb": 4, - "options-gb": [ + "memoryGB": { + "default": 2, + "defaultRequest": 4, + "defaultLimit": 192, + "defaultHeadless": 4, + "options": [ 1, 2, 4, diff --git a/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java b/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java index 26fd356a..fa163a14 100644 --- a/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java +++ b/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java @@ -108,19 +108,19 @@ public ResourceContexts() { // Extract fields into variables JsonObject cores = jsonObject.getAsJsonObject("cores"); - defaultRequestCores= cores.get("default-request").getAsInt(); - defaultLimitCores = cores.get("default-limit").getAsInt(); + defaultRequestCores= cores.get("defaultRequest").getAsInt(); + defaultLimitCores = cores.get("defaultLimit").getAsInt(); defaultCores = cores.get("default").getAsInt(); - defaultCoresHeadless = cores.get("default-headless").getAsInt(); + defaultCoresHeadless = cores.get("defaultHeadless").getAsInt(); JsonArray coresOptions = cores.getAsJsonArray("options"); coresOptions.asList().forEach(coreOption->availableCores.add(coreOption.getAsInt())); - JsonObject memory = jsonObject.getAsJsonObject("memory"); - defaultRequestRAM = memory.get("default-request-gb").getAsInt(); - defaultLimitRAM = memory.get("default-limit-gb").getAsInt(); - defaultRAM = memory.get("default-gb").getAsInt(); - defaultRAMHeadless = memory.get("default-headless-gb").getAsInt(); - JsonArray ramOptions = memory.getAsJsonArray("options-gb"); + JsonObject memory = jsonObject.getAsJsonObject("memoryGB"); + defaultRequestRAM = memory.get("defaultRequest").getAsInt(); + defaultLimitRAM = memory.get("defaultLimit").getAsInt(); + defaultRAM = memory.get("default").getAsInt(); + defaultRAMHeadless = memory.get("defaultHeadless").getAsInt(); + JsonArray ramOptions = memory.getAsJsonArray("options"); ramOptions.asList().forEach(ramOption -> availableRAM.add(ramOption.getAsInt())); JsonObject gpus = jsonObject.getAsJsonObject("gpus"); From f4f3bc5fd6b61ccf394410a278d1d916c1dfff7c Mon Sep 17 00:00:00 2001 From: Arati Kakadiya Date: Thu, 7 Nov 2024 12:42:39 -0800 Subject: [PATCH 5/5] Small code change and code formatted. --- .../java/org/opencadc/skaha/context/GetAction.java | 2 +- .../opencadc/skaha/context/ResourceContexts.java | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/skaha/src/main/java/org/opencadc/skaha/context/GetAction.java b/skaha/src/main/java/org/opencadc/skaha/context/GetAction.java index 9a695427..2315b61f 100644 --- a/skaha/src/main/java/org/opencadc/skaha/context/GetAction.java +++ b/skaha/src/main/java/org/opencadc/skaha/context/GetAction.java @@ -87,7 +87,7 @@ public GetAction() { @Override public void doAction() throws Exception { super.initRequest(); - + File propertiesFile = ResourceContexts.getResourcesFile("k8s-resources.json"); byte[] bytes = getBytes(propertiesFile); syncOutput.setHeader("Content-Type", "application/json"); diff --git a/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java b/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java index fa163a14..4bab0057 100644 --- a/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java +++ b/skaha/src/main/java/org/opencadc/skaha/context/ResourceContexts.java @@ -101,19 +101,18 @@ public class ResourceContexts { private final List availableGPUs = new ArrayList<>(); public ResourceContexts() { - try { - FileReader reader = new FileReader(getResourcesFile("k8s-resources.json")); + try (final FileReader reader = new FileReader(getResourcesFile("k8s-resources.json"))) { JsonElement jsonElement = JsonParser.parseReader(reader); JsonObject jsonObject = jsonElement.getAsJsonObject(); // Extract fields into variables JsonObject cores = jsonObject.getAsJsonObject("cores"); - defaultRequestCores= cores.get("defaultRequest").getAsInt(); + defaultRequestCores = cores.get("defaultRequest").getAsInt(); defaultLimitCores = cores.get("defaultLimit").getAsInt(); defaultCores = cores.get("default").getAsInt(); defaultCoresHeadless = cores.get("defaultHeadless").getAsInt(); JsonArray coresOptions = cores.getAsJsonArray("options"); - coresOptions.asList().forEach(coreOption->availableCores.add(coreOption.getAsInt())); + coresOptions.asList().forEach(coreOption -> availableCores.add(coreOption.getAsInt())); JsonObject memory = jsonObject.getAsJsonObject("memoryGB"); defaultRequestRAM = memory.get("defaultRequest").getAsInt(); @@ -128,17 +127,17 @@ public ResourceContexts() { gpuOptions.asList().forEach(gpuOption -> availableGPUs.add(gpuOption.getAsInt())); } catch (Exception e) { log.error(e); - throw new IllegalStateException("failed reading k8s-resources.properties", e); + throw new IllegalStateException("failed reading k8s-resources.json", e); } } - public static File getResourcesFile(String fileName){ + public static File getResourcesFile(String fileName) { String configDir = System.getProperty("user.home") + "/config"; String configDirSystemProperty = PropertiesReader.class.getName() + ".dir"; if (System.getProperty(configDirSystemProperty) != null) { configDir = System.getProperty(configDirSystemProperty); } - return new File(new File(configDir), fileName); + return new File(new File(configDir), fileName); } public Integer getDefaultRequestCores() {