Skip to content

Commit

Permalink
Object Store use over leaf-hub domain (#900)
Browse files Browse the repository at this point in the history
  • Loading branch information
scottf authored Jun 17, 2024
1 parent 711388d commit 8be62fc
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/NATS.Client/ObjectStore/ObjectStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
66 changes: 64 additions & 2 deletions src/Tests/IntegrationTests/TestObjectStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Text;
Expand All @@ -32,9 +33,9 @@
namespace IntegrationTests
{
[SuppressMessage("ReSharper", "ParameterOnlyUsedForPreconditionCheck.Local")]
public class TestObjectStore : TestSuite<OneServerSuiteContext>
public class TestObjectStore : TestSuite<ObjectStoreSuiteContext>
{
public TestObjectStore(OneServerSuiteContext context) : base(context) { }
public TestObjectStore(ObjectStoreSuiteContext context) : base(context) { }

[Fact]
public void TestWorkFlow()
Expand Down Expand Up @@ -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
Expand Down
22 changes: 15 additions & 7 deletions src/Tests/IntegrationTests/TestSuite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,29 +226,35 @@ public void RunInJsServer(TestServerInfo testServerInfo, string config, Action<I
}
}

public const string HubDomain = "HUB";
public const string LeafDomain = "LEAF";

public void RunInJsHubLeaf(TestServerInfo hubServerInfo,
TestServerInfo hubLeafInfo,
TestServerInfo leafServerInfo, Action<IConnection, IConnection> test)
{
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 + "\" } ]");
Expand Down Expand Up @@ -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;

Expand All @@ -489,7 +497,7 @@ public class KeyValueSuiteContext : SuiteContext
public void RunInJsHubLeaf(Action<IConnection, IConnection> test) =>
base.RunInJsHubLeaf(Server1, Server2, Server3, test);
}

public class OneServerSuiteContext : SuiteContext
{
public readonly TestServerInfo Server1;
Expand Down
5 changes: 5 additions & 0 deletions src/Tests/UnitTests/TestBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit 8be62fc

Please sign in to comment.