From a72eea24e697c9ca5b510e0b7366f38bd1ca1da7 Mon Sep 17 00:00:00 2001 From: scottf Date: Mon, 17 Jun 2024 15:31:36 -0400 Subject: [PATCH 1/2] Object Store use over leaf-hub domain --- src/NATS.Client/ObjectStore/ObjectStore.cs | 2 +- src/Tests/IntegrationTests/TestObjectStore.cs | 68 ++++++++++++++++++- src/Tests/IntegrationTests/TestSuite.cs | 22 ++++-- src/Tests/UnitTests/TestBase.cs | 5 ++ 4 files changed, 86 insertions(+), 11 deletions(-) diff --git a/src/NATS.Client/ObjectStore/ObjectStore.cs b/src/NATS.Client/ObjectStore/ObjectStore.cs index c6b22d84d..090671aa5 100644 --- a/src/NATS.Client/ObjectStore/ObjectStore.cs +++ b/src/NATS.Client/ObjectStore/ObjectStore.cs @@ -204,7 +204,7 @@ public ObjectInfo Get(string objectName, Stream outputStream) } else { IJetStreamPushSyncSubscription sub = js.PushSubscribeSync( - PubSubChunkSubject(oi.Nuid), + RawChunkSubject(oi.Nuid), PushSubscribeOptions.Builder() .WithStream(StreamName) .WithOrdered(true) diff --git a/src/Tests/IntegrationTests/TestObjectStore.cs b/src/Tests/IntegrationTests/TestObjectStore.cs index abf5a78e2..76aab59e1 100644 --- a/src/Tests/IntegrationTests/TestObjectStore.cs +++ b/src/Tests/IntegrationTests/TestObjectStore.cs @@ -13,6 +13,7 @@ using System; using System.Collections.Generic; +using System.ComponentModel.DataAnnotations; using System.Diagnostics.CodeAnalysis; using System.IO; using System.Text; @@ -32,9 +33,9 @@ namespace IntegrationTests { [SuppressMessage("ReSharper", "ParameterOnlyUsedForPreconditionCheck.Local")] - public class TestObjectStore : TestSuite + public class TestObjectStore : TestSuite { - public TestObjectStore(OneServerSuiteContext context) : base(context) { } + public TestObjectStore(ObjectStoreSuiteContext context) : base(context) { } [Fact] public void TestWorkFlow() @@ -597,6 +598,67 @@ private void ValidateWatcher(object[] expecteds, TestObjectStoreWatcher watcher) } } } + + [Fact] + public void TestObjectStoreDomains() { + Context.RunInJsHubLeaf((hubNc, leafNc) => + { + IObjectStoreManagement hubOsm = hubNc.CreateObjectStoreManagementContext(); + + // Create main OS on HUB + String hubBucket = Bucket(); + ObjectStoreStatus hubStatus = hubOsm.Create(ObjectStoreConfiguration.Builder() + .WithName(hubBucket) + .WithStorageType(StorageType.Memory) + .WithReplicas(1) + .Build()); + + Assert.Equal(0U, hubStatus.Size); + Assert.Equal(1, hubStatus.Replicas); + + IObjectStore hubOs = hubNc.CreateObjectStoreContext(hubBucket); + IObjectStore leafOs = leafNc.CreateObjectStoreContext(hubBucket, + ObjectStoreOptions.Builder().WithJsDomain(SuiteContext.HubDomain).Build()); + + String objectName = Name(); + ObjectMeta meta = ObjectMeta.Builder(objectName) + .WithChunkSize(8 * 1024) + .Build(); + + Object[] input = GetInput(4 * 8 * 1024, ".", long.MaxValue, null); + FileInfo fileInfo = (FileInfo)input[1]; + hubOs.Put(meta, fileInfo.OpenRead()); + + hubStatus = hubOs.GetStatus(); + Assert.True(hubStatus.Size > 0); + + ObjectStoreStatus leafStatus = leafOs.GetStatus(); + + Assert.Equal(hubStatus.BucketName, leafStatus.BucketName); + Assert.Equal(hubStatus.Size, leafStatus.Size); + + ObjectInfo hubInfo = hubOs.GetInfo(objectName); + ObjectInfo leafInfo = leafOs.GetInfo(objectName); + + Assert.Equal(hubInfo.Nuid, leafInfo.Nuid); + Assert.Equal(hubInfo.Size, leafInfo.Size); + Assert.Equal(hubInfo.ObjectMeta.ObjectName, leafInfo.ObjectMeta.ObjectName); + + MemoryStream hubMs = new MemoryStream(); + ObjectInfo hubOi = hubOs.Get(objectName, hubMs); + byte[] hubBytes = hubMs.ToArray(); + + MemoryStream leafMs = new MemoryStream(); + ObjectInfo leafOi = leafOs.Get(objectName, leafMs); + byte[] leafBytes = leafMs.ToArray(); + + Assert.Equal(hubBytes.Length, leafBytes.Length); + for (int x = 0; x < hubBytes.Length; x++) + { + Assert.Equal(hubBytes[x], leafBytes[x]); + } + }); + } } class TestObjectStoreWatcher : IObjectStoreWatcher @@ -625,4 +687,4 @@ public void EndOfData() { } } } -} +} \ No newline at end of file diff --git a/src/Tests/IntegrationTests/TestSuite.cs b/src/Tests/IntegrationTests/TestSuite.cs index 9002282a6..3030ff067 100644 --- a/src/Tests/IntegrationTests/TestSuite.cs +++ b/src/Tests/IntegrationTests/TestSuite.cs @@ -226,6 +226,9 @@ public void RunInJsServer(TestServerInfo testServerInfo, string config, Action test) @@ -233,22 +236,25 @@ public void RunInJsHubLeaf(TestServerInfo hubServerInfo, string hubConfFile = TestBase.TempConfFile(); StreamWriter streamWriter = File.CreateText(hubConfFile); streamWriter.WriteLine("port: " + hubServerInfo.Port); - streamWriter.WriteLine("server_name: HUB"); + streamWriter.WriteLine("server_name: " + HubDomain); streamWriter.WriteLine("jetstream {"); - streamWriter.WriteLine(" domain: HUB"); + streamWriter.WriteLine(" store_dir: " + TestBase.TempConfDir()); + streamWriter.WriteLine(" domain: " + HubDomain); streamWriter.WriteLine("}"); streamWriter.WriteLine("leafnodes {"); streamWriter.WriteLine(" listen = 127.0.0.1:" + hubLeafInfo.Port); streamWriter.WriteLine("}"); streamWriter.Flush(); streamWriter.Close(); - + string leafConfFile = TestBase.TempConfFile(); + string leafJsStoreDir = TestBase.TempConfDir(); streamWriter = File.CreateText(leafConfFile); streamWriter.WriteLine("port: " + leafServerInfo.Port); - streamWriter.WriteLine("server_name: LEAF"); + streamWriter.WriteLine("server_name: " + LeafDomain); streamWriter.WriteLine("jetstream {"); - streamWriter.WriteLine(" domain: LEAF"); + streamWriter.WriteLine(" store_dir: " + TestBase.TempConfDir()); + streamWriter.WriteLine(" domain: " + LeafDomain); streamWriter.WriteLine("}"); streamWriter.WriteLine("leafnodes {"); streamWriter.WriteLine(" remotes = [ { url: \"leaf://127.0.0.1:" + hubLeafInfo.Port + "\" } ]"); @@ -473,8 +479,10 @@ public class JetStreamPublishSuiteContext : OneServerSuiteContext {} public class JetStreamPushAsyncSuiteContext : OneServerSuiteContext {} public class JetStreamPushSyncSuiteContext : OneServerSuiteContext {} public class JetStreamPushSyncQueueSuiteContext : OneServerSuiteContext {} + public class KeyValueSuiteContext : HubLeafSuiteContext {} + public class ObjectStoreSuiteContext : HubLeafSuiteContext {} - public class KeyValueSuiteContext : SuiteContext + public class HubLeafSuiteContext : SuiteContext { private const int SeedPort = TestSeedPorts.KvSuite; @@ -489,7 +497,7 @@ public class KeyValueSuiteContext : SuiteContext public void RunInJsHubLeaf(Action test) => base.RunInJsHubLeaf(Server1, Server2, Server3, test); } - + public class OneServerSuiteContext : SuiteContext { public readonly TestServerInfo Server1; diff --git a/src/Tests/UnitTests/TestBase.cs b/src/Tests/UnitTests/TestBase.cs index 671f7915f..03a657c81 100644 --- a/src/Tests/UnitTests/TestBase.cs +++ b/src/Tests/UnitTests/TestBase.cs @@ -114,6 +114,11 @@ public static DateTime AsDateTime(string dtString) return DateTime.Parse(dtString).ToUniversalTime(); } + public static string TempConfDir() + { + return Path.GetTempPath().Replace("\\", "\\\\"); // when on windows this is necessary. unix doesn't have backslash + } + public static string TempConfFile() { return Path.GetTempPath() + "nats_net_test" + Guid.NewGuid() + ".conf"; From 9727c3534dcf2053805a5f0ef6b878547ca16978 Mon Sep 17 00:00:00 2001 From: scottf Date: Mon, 17 Jun 2024 15:37:42 -0400 Subject: [PATCH 2/2] aroc line at eof --- src/Tests/IntegrationTests/TestObjectStore.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Tests/IntegrationTests/TestObjectStore.cs b/src/Tests/IntegrationTests/TestObjectStore.cs index 76aab59e1..b4e8abcea 100644 --- a/src/Tests/IntegrationTests/TestObjectStore.cs +++ b/src/Tests/IntegrationTests/TestObjectStore.cs @@ -687,4 +687,4 @@ public void EndOfData() { } } } -} \ No newline at end of file +}