From 0c36ea7c4b2eddae2995f69b890e11d5d82c9576 Mon Sep 17 00:00:00 2001 From: scottf Date: Mon, 17 Jun 2024 14:06:44 -0400 Subject: [PATCH 1/3] Object Store use over leaf-hub domain --- .../io/nats/client/impl/NatsObjectStore.java | 3 +- .../io/nats/client/impl/ObjectStoreTests.java | 57 ++++++++++++++++++- .../java/io/nats/client/utils/TestBase.java | 20 ++++--- 3 files changed, 70 insertions(+), 10 deletions(-) diff --git a/src/main/java/io/nats/client/impl/NatsObjectStore.java b/src/main/java/io/nats/client/impl/NatsObjectStore.java index b056796b6..db3c9ed03 100644 --- a/src/main/java/io/nats/client/impl/NatsObjectStore.java +++ b/src/main/java/io/nats/client/impl/NatsObjectStore.java @@ -232,8 +232,7 @@ public ObjectInfo get(String objectName, OutputStream out) throws IOException, J out.write(data); } else { - - JetStreamSubscription sub = js.subscribe(pubSubChunkSubject(oi.getNuid()), + JetStreamSubscription sub = js.subscribe(rawChunkSubject(oi.getNuid()), PushSubscribeOptions.builder().stream(streamName).ordered(true).build()); Message m = sub.nextMessage(Duration.ofSeconds(1)); diff --git a/src/test/java/io/nats/client/impl/ObjectStoreTests.java b/src/test/java/io/nats/client/impl/ObjectStoreTests.java index 014183c66..25f83e119 100644 --- a/src/test/java/io/nats/client/impl/ObjectStoreTests.java +++ b/src/test/java/io/nats/client/impl/ObjectStoreTests.java @@ -225,7 +225,7 @@ private ObjectInfo validateObjectInfo(ObjectInfo oi, String bucket, String objec } @SuppressWarnings("SameParameterValue") - private static Object[] getInput(int size) throws IOException { + private static Object[] getInput(int size) { File found = null; long foundLen = Long.MAX_VALUE; final String classPath = System.getProperty("java.class.path", "."); @@ -618,4 +618,59 @@ private void validateWatcher(Object[] expecteds, TestObjectStoreWatcher watcher) } } } + + @Test + public void testObjectStoreDomains() throws Exception { + runInJsHubLeaf((hubNc, leafNc) -> { + ObjectStoreManagement hubOsm = hubNc.objectStoreManagement(); + + // Create main OS on HUB + String hubBucket = "bckt"; // bucket(); + ObjectStoreStatus hubStatus = hubOsm.create(ObjectStoreConfiguration.builder() + .name(hubBucket) + .storageType(StorageType.Memory) + .replicas(1) + .build()); + assertEquals(0, hubStatus.getSize()); + assertEquals(1, hubStatus.getReplicas()); + + ObjectStore hubOs = hubNc.objectStore(hubBucket); + ObjectStore leafOs = leafNc.objectStore(hubBucket, ObjectStoreOptions.builder().jsDomain(HUB_DOMAIN).build()); + + String objectName = name(); + ObjectMeta meta = ObjectMeta.builder(objectName) + .chunkSize(8 * 1024) + .build(); + + Object[] input = getInput(4 * 8 * 1024); + File file = (File)input[1]; + InputStream in = Files.newInputStream(file.toPath()); + hubOs.put(meta, in); + + hubStatus = hubOs.getStatus(); + assertTrue(hubStatus.getSize() > 0); + + ObjectStoreStatus leafStatus = leafOs.getStatus(); + + assertEquals(hubStatus.getBucketName(), leafStatus.getBucketName()); + assertEquals(hubStatus.getSize(), leafStatus.getSize()); + + ObjectInfo hubInfo = hubOs.getInfo(objectName); + ObjectInfo leafInfo = leafOs.getInfo(objectName); + + assertEquals(hubInfo.getNuid(), leafInfo.getNuid()); + assertEquals(hubInfo.getSize(), leafInfo.getSize()); + assertEquals(hubInfo.getObjectMeta().getObjectName(), leafInfo.getObjectMeta().getObjectName()); + + ByteArrayOutputStream hubOut = new ByteArrayOutputStream(); + ByteArrayOutputStream leafOut = new ByteArrayOutputStream(); + hubOs.get(objectName, hubOut); + leafOs.get(objectName, leafOut); + + byte[] hubBytes = hubOut.toByteArray(); + byte[] leafBytes = leafOut.toByteArray(); + + assertArrayEquals(hubBytes, leafBytes); + }); + } } diff --git a/src/test/java/io/nats/client/utils/TestBase.java b/src/test/java/io/nats/client/utils/TestBase.java index 930773cb3..15040e723 100644 --- a/src/test/java/io/nats/client/utils/TestBase.java +++ b/src/test/java/io/nats/client/utils/TestBase.java @@ -290,9 +290,10 @@ public static void runInJsHubLeaf(TwoServerTest twoServerTest) throws Exception int leafPort = NatsTestServer.nextPort(); String[] hubInserts = new String[] { - "server_name: HUB", + "server_name: " + HUB_DOMAIN, "jetstream {", - " domain: HUB", + " store_dir: " + tempJsStoreDir(), + " domain: " + HUB_DOMAIN, "}", "leafnodes {", " listen = 127.0.0.1:" + hubLeafPort, @@ -300,9 +301,10 @@ public static void runInJsHubLeaf(TwoServerTest twoServerTest) throws Exception }; String[] leafInserts = new String[] { - "server_name: LEAF", + "server_name: " + LEAF_DOMAIN, "jetstream {", - " domain: LEAF", + " store_dir: " + tempJsStoreDir(), + " domain: " + LEAF_DOMAIN, "}", "leafnodes {", " remotes = [ { url: \"leaf://127.0.0.1:" + hubLeafPort + "\" } ]", @@ -331,9 +333,9 @@ public static void runInJsCluster(ThreeServerTest threeServerTest) throws Except int listen1 = NatsTestServer.nextPort(); int listen2 = NatsTestServer.nextPort(); int listen3 = NatsTestServer.nextPort(); - String path1 = Files.createTempDirectory(variant()).toString().replace("\\", "\\\\"); - String path2 = Files.createTempDirectory(variant()).toString().replace("\\", "\\\\"); - String path3 = Files.createTempDirectory(variant()).toString().replace("\\", "\\\\"); + String path1 = tempJsStoreDir(); + String path2 = tempJsStoreDir(); + String path3 = tempJsStoreDir(); String cluster = variant(); String serverPrefix = variant(); @@ -398,6 +400,10 @@ public static void runInJsCluster(ThreeServerTest threeServerTest) throws Except } } + private static String tempJsStoreDir() throws IOException { + return Files.createTempDirectory(variant()).toString().replace("\\", "\\\\"); + } + private static void cleanupJs(Connection c) { try { From 6fd11c4c327562e4bb2e52f5b9c8f49f8b300653 Mon Sep 17 00:00:00 2001 From: scottf Date: Mon, 17 Jun 2024 15:28:54 -0400 Subject: [PATCH 2/3] fixed test to not use fixed name --- src/test/java/io/nats/client/impl/ObjectStoreTests.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/io/nats/client/impl/ObjectStoreTests.java b/src/test/java/io/nats/client/impl/ObjectStoreTests.java index 25f83e119..c19030c82 100644 --- a/src/test/java/io/nats/client/impl/ObjectStoreTests.java +++ b/src/test/java/io/nats/client/impl/ObjectStoreTests.java @@ -625,7 +625,7 @@ public void testObjectStoreDomains() throws Exception { ObjectStoreManagement hubOsm = hubNc.objectStoreManagement(); // Create main OS on HUB - String hubBucket = "bckt"; // bucket(); + String hubBucket = bucket(); ObjectStoreStatus hubStatus = hubOsm.create(ObjectStoreConfiguration.builder() .name(hubBucket) .storageType(StorageType.Memory) From b5c5c5597c50ababe96a4339de8a0387b912f9ec Mon Sep 17 00:00:00 2001 From: scottf Date: Mon, 17 Jun 2024 15:35:10 -0400 Subject: [PATCH 3/3] added comment --- src/test/java/io/nats/client/utils/TestBase.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/io/nats/client/utils/TestBase.java b/src/test/java/io/nats/client/utils/TestBase.java index 15040e723..d42ef2e1f 100644 --- a/src/test/java/io/nats/client/utils/TestBase.java +++ b/src/test/java/io/nats/client/utils/TestBase.java @@ -401,7 +401,7 @@ public static void runInJsCluster(ThreeServerTest threeServerTest) throws Except } private static String tempJsStoreDir() throws IOException { - return Files.createTempDirectory(variant()).toString().replace("\\", "\\\\"); + return Files.createTempDirectory(variant()).toString().replace("\\", "\\\\"); // when on windows this is necessary. unix doesn't have backslash } private static void cleanupJs(Connection c)