Skip to content

Commit

Permalink
Required and Preferred match filtering unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rkargMsft committed Dec 20, 2024
1 parent 5109fb6 commit 5779205
Show file tree
Hide file tree
Showing 10 changed files with 385 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public static ISiloBuilder UseSiloMetadata(this ISiloBuilder builder, Dictionary
services.AddSingleton<ISiloMetadataClient, SiloMetadataClient>();
// Placement filters
services.AddPlacementFilter<PreferredMatchSiloMetadataPlacementFilterStrategy, PreferredMatchSiloMetadataPlacementFilterDirector>(ServiceLifetime.Transient);
services.AddPlacementFilter<RequiredMatchSiloMetadataPlacementFilterStrategy, RequiredMatchSiloMetadataFilterDirector>(ServiceLifetime.Transient);
services.AddPlacementFilter<RequiredMatchSiloMetadataPlacementFilterStrategy, RequiredMatchSiloMetadataPlacementFilterDirector>(ServiceLifetime.Transient);
});
return builder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Orleans.Runtime.Placement.Filtering;

internal class RequiredMatchSiloMetadataFilterDirector(ILocalSiloDetails localSiloDetails, ISiloMetadataCache siloMetadataCache)
internal class RequiredMatchSiloMetadataPlacementFilterDirector(ILocalSiloDetails localSiloDetails, ISiloMetadataCache siloMetadataCache)
: IPlacementFilterDirector
{
public IEnumerable<SiloAddress> Filter(PlacementFilterStrategy filterStrategy, PlacementTarget target, IEnumerable<SiloAddress> silos)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
using TestExtensions;
using Xunit;

namespace UnitTests.General;
namespace UnitTests.PlacementFilterTests;

[TestCategory("Placement"), TestCategory("Filters")]
public class GrainPlacementFilterTests : TestClusterPerTest
Expand All @@ -29,8 +29,8 @@ public void Configure(ISiloBuilder hostBuilder)
[Fact, TestCategory("Functional")]
public async Task PlacementFilter_GrainWithoutFilterCanBeCalled()
{
await this.HostedCluster.WaitForLivenessToStabilizeAsync();
var managementGrain = this.Client.GetGrain<IManagementGrain>(0);
await HostedCluster.WaitForLivenessToStabilizeAsync();
var managementGrain = Client.GetGrain<IManagementGrain>(0);
var silos = await managementGrain.GetHosts(true);
Assert.NotNull(silos);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
using System.Net;
using Orleans.Metadata;
using Orleans.Runtime.MembershipService.SiloMetadata;
using Orleans.Runtime.Placement;
using Orleans.Runtime.Placement.Filtering;
using Xunit;

namespace UnitTests.PlacementFilterTests;

[TestCategory("Placement"), TestCategory("Filters"), TestCategory("SiloMetadata")]
public class PreferredMatchSiloMetadataPlacementFilterDirectorTests
{
[Fact, TestCategory("Functional")]
public void CanBeCreated()
{
var testLocalSiloAddress = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1000, 1);
var director = new PreferredMatchSiloMetadataPlacementFilterDirector(
new TestLocalSiloDetails("name", "clusterId", "dnsHostName",
testLocalSiloAddress,
testLocalSiloAddress),
new TestSiloMetadataCache(new Dictionary<SiloAddress, SiloMetadata>()
{
{ testLocalSiloAddress, SiloMetadata.Empty }
}));
Assert.NotNull(director);
}

[Fact, TestCategory("Functional")]
public void CanBeCalled()
{
var testLocalSiloAddress = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1000, 1);
var director = new PreferredMatchSiloMetadataPlacementFilterDirector(
new TestLocalSiloDetails("name", "clusterId", "dnsHostName",
testLocalSiloAddress,
testLocalSiloAddress),
new TestSiloMetadataCache(new Dictionary<SiloAddress, SiloMetadata>()
{
{testLocalSiloAddress, SiloMetadata.Empty}
}));
var result = director.Filter(new PreferredMatchSiloMetadataPlacementFilterStrategy(), new PlacementTarget(),
new List<SiloAddress>() { testLocalSiloAddress }
).ToList();
Assert.NotNull(result);
Assert.NotEmpty(result);
}

[Fact, TestCategory("Functional")]
public void FiltersToAllWhenNoEntry()
{
var testLocalSiloAddress = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1000, 1);
var testOtherSiloAddress = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1001, 1);
var siloMetadata = new SiloMetadata();
siloMetadata.AddMetadata("metadata.key", "something");
var director = new PreferredMatchSiloMetadataPlacementFilterDirector(
new TestLocalSiloDetails("name", "clusterId", "dnsHostName",
testLocalSiloAddress,
testLocalSiloAddress),
new TestSiloMetadataCache(new Dictionary<SiloAddress, SiloMetadata>()
{
{testOtherSiloAddress, SiloMetadata.Empty},
{testLocalSiloAddress, siloMetadata},
}));
var result = director.Filter(new PreferredMatchSiloMetadataPlacementFilterStrategy(["metadata.key"], 1), new PlacementTarget(),
new List<SiloAddress>() { testOtherSiloAddress }).ToList();
Assert.NotEmpty(result);
}


[Theory, TestCategory("Functional")]
[InlineData(1, 3, "no.match")]
[InlineData(2, 3, "no.match")]
[InlineData( 1, 1, "one.match")]
[InlineData( 2, 3, "one.match")]
[InlineData( 1, 2, "two.match")]
[InlineData( 2, 2, "two.match")]
[InlineData( 3, 3, "two.match")]
[InlineData( 1, 3, "all.match")]
[InlineData( 2, 3, "all.match")]

public void FiltersOnSingleMetadata(int minCandidates, int expectedCount, string key)
{
var testLocalSiloAddress = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1000, 1);
var testOtherSiloAddress1 = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1001, 1);
var testOtherSiloAddress2 = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1002, 1);
var testOtherSiloAddress3 = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1003, 1);
var localSiloMetadata = new SiloMetadata();
localSiloMetadata.AddMetadata("all.match", "match");
localSiloMetadata.AddMetadata("one.match", "match");
localSiloMetadata.AddMetadata("two.match", "match");
localSiloMetadata.AddMetadata("no.match", "match");
var otherSiloMetadata1 = new SiloMetadata();
otherSiloMetadata1.AddMetadata("all.match", "match");
otherSiloMetadata1.AddMetadata("one.match", "match");
otherSiloMetadata1.AddMetadata("two.match", "match");
otherSiloMetadata1.AddMetadata("no.match", "nomatch");
var otherSiloMetadata2 = new SiloMetadata();
otherSiloMetadata2.AddMetadata("all.match", "match");
otherSiloMetadata2.AddMetadata("one.match", "nomatch");
otherSiloMetadata2.AddMetadata("two.match", "match");
otherSiloMetadata2.AddMetadata("no.match", "nomatch");
var otherSiloMetadata3 = new SiloMetadata();
otherSiloMetadata3.AddMetadata("all.match", "match");
otherSiloMetadata3.AddMetadata("one.match", "nomatch");
otherSiloMetadata3.AddMetadata("two.match", "nomatch");
otherSiloMetadata3.AddMetadata("no.match", "nomatch");
var director = new PreferredMatchSiloMetadataPlacementFilterDirector(
new TestLocalSiloDetails("name", "clusterId", "dnsHostName",
testLocalSiloAddress,
testLocalSiloAddress),
new TestSiloMetadataCache(new Dictionary<SiloAddress, SiloMetadata>()
{
{testOtherSiloAddress1, otherSiloMetadata1},
{testOtherSiloAddress2, otherSiloMetadata2},
{testOtherSiloAddress3, otherSiloMetadata3},
{testLocalSiloAddress, localSiloMetadata},
}));
var result = director.Filter(new PreferredMatchSiloMetadataPlacementFilterStrategy([key], minCandidates), new PlacementTarget(),
new List<SiloAddress>() { testOtherSiloAddress1, testOtherSiloAddress2, testOtherSiloAddress3 }).ToList();
Assert.NotEmpty(result);
Assert.Equal(expectedCount, result.Count);
}

[Theory, TestCategory("Functional")]
[InlineData(1, 3, "no.match", "all.match")]
[InlineData(1, 1, "no.match", "one.match", "two.match")]
[InlineData(2, 2, "no.match", "one.match", "two.match")]
[InlineData(3, 3, "no.match", "one.match", "two.match")]
[InlineData(1, 3, "all.match", "no.match")]
[InlineData(1, 1, "one.match", "all.match")]
[InlineData(2, 3, "one.match", "all.match")]
[InlineData(1, 1, "no.match", "one.match", "all.match")]
[InlineData(2, 3, "no.match", "one.match", "all.match")]

public void FiltersOnMultipleMetadata(int minCandidates, int expectedCount, params string[] keys)
{
var testLocalSiloAddress = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1000, 1);
var testOtherSiloAddress1 = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1001, 1);
var testOtherSiloAddress2 = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1002, 1);
var testOtherSiloAddress3 = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1003, 1);
var localSiloMetadata = new SiloMetadata();
localSiloMetadata.AddMetadata("all.match", "match");
localSiloMetadata.AddMetadata("one.match", "match");
localSiloMetadata.AddMetadata("two.match", "match");
localSiloMetadata.AddMetadata("no.match", "match");
var otherSiloMetadata1 = new SiloMetadata();
otherSiloMetadata1.AddMetadata("all.match", "match");
otherSiloMetadata1.AddMetadata("one.match", "match");
otherSiloMetadata1.AddMetadata("two.match", "match");
otherSiloMetadata1.AddMetadata("no.match", "not.match");
var otherSiloMetadata2 = new SiloMetadata();
otherSiloMetadata2.AddMetadata("all.match", "match");
otherSiloMetadata2.AddMetadata("one.match", "nomatch");
otherSiloMetadata2.AddMetadata("two.match", "match");
otherSiloMetadata2.AddMetadata("no.match", "not.match");
var otherSiloMetadata3 = new SiloMetadata();
otherSiloMetadata3.AddMetadata("all.match", "match");
otherSiloMetadata3.AddMetadata("one.match", "not.match");
otherSiloMetadata3.AddMetadata("two.match", "not.match");
otherSiloMetadata3.AddMetadata("no.match", "not.match");
var director = new PreferredMatchSiloMetadataPlacementFilterDirector(
new TestLocalSiloDetails("name", "clusterId", "dnsHostName",
testLocalSiloAddress,
testLocalSiloAddress),
new TestSiloMetadataCache(new Dictionary<SiloAddress, SiloMetadata>()
{
{testOtherSiloAddress1, otherSiloMetadata1},
{testOtherSiloAddress2, otherSiloMetadata2},
{testOtherSiloAddress3, otherSiloMetadata3},
{testLocalSiloAddress, localSiloMetadata},
}));
var result = director.Filter(new PreferredMatchSiloMetadataPlacementFilterStrategy(keys, minCandidates), new PlacementTarget(),
new List<SiloAddress>() { testOtherSiloAddress1, testOtherSiloAddress2, testOtherSiloAddress3 }).ToList();
Assert.NotEmpty(result);
Assert.Equal(expectedCount, result.Count);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
using System.Net;
using Orleans.Runtime.MembershipService.SiloMetadata;
using Orleans.Runtime.Placement;
using Orleans.Runtime.Placement.Filtering;
using Xunit;

namespace UnitTests.PlacementFilterTests;

[TestCategory("Placement"), TestCategory("Filters"), TestCategory("SiloMetadata")]
public class RequiredMatchSiloMetadataPlacementFilterDirectorTests
{
[Fact, TestCategory("Functional")]
public void RequiredMatchSiloMetadataPlacementFilterDirector_CanBeCreated()
{
var testLocalSiloAddress = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1000, 1);
var director = new RequiredMatchSiloMetadataPlacementFilterDirector(
new TestLocalSiloDetails("name", "clusterId", "dnsHostName",
testLocalSiloAddress,
testLocalSiloAddress),
new TestSiloMetadataCache(new Dictionary<SiloAddress, SiloMetadata>()
{
{testLocalSiloAddress, SiloMetadata.Empty}
}));
Assert.NotNull(director);
}

[Fact, TestCategory("Functional")]
public void RequiredMatchSiloMetadataPlacementFilterDirector_CanBeCalled()
{
var testLocalSiloAddress = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1000, 1);
var director = new RequiredMatchSiloMetadataPlacementFilterDirector(
new TestLocalSiloDetails("name", "clusterId", "dnsHostName",
testLocalSiloAddress,
testLocalSiloAddress),
new TestSiloMetadataCache(new Dictionary<SiloAddress, SiloMetadata>()
{
{testLocalSiloAddress, SiloMetadata.Empty}
}));
var result = director.Filter(new RequiredMatchSiloMetadataPlacementFilterStrategy(), new PlacementTarget(),
new List<SiloAddress>() { testLocalSiloAddress }
).ToList();
Assert.NotNull(result);
Assert.NotEmpty(result);
}

[Fact, TestCategory("Functional")]
public void RequiredMatchSiloMetadataPlacementFilterDirector_FiltersToNothingWhenNoEntry()
{
var testLocalSiloAddress = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1000, 1);
var testOtherSiloAddress = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1001, 1);
var siloMetadata = new SiloMetadata();
siloMetadata.AddMetadata("metadata.key", "something");
var director = new RequiredMatchSiloMetadataPlacementFilterDirector(
new TestLocalSiloDetails("name", "clusterId", "dnsHostName",
testLocalSiloAddress,
testLocalSiloAddress),
new TestSiloMetadataCache(new Dictionary<SiloAddress, SiloMetadata>()
{
{testOtherSiloAddress, SiloMetadata.Empty},
{testLocalSiloAddress, siloMetadata},
}));
var result = director.Filter(new RequiredMatchSiloMetadataPlacementFilterStrategy(["metadata.key"]), new PlacementTarget(),
new List<SiloAddress>() { testOtherSiloAddress }).ToList();
Assert.Empty(result);
}

[Fact, TestCategory("Functional")]
public void RequiredMatchSiloMetadataPlacementFilterDirector_FiltersToNothingWhenDifferentValue()
{
var testLocalSiloAddress = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1000, 1);
var testOtherSiloAddress = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1001, 1);
var localSiloMetadata = new SiloMetadata();
localSiloMetadata.AddMetadata("metadata.key", "local");
var otherSiloMetadata = new SiloMetadata();
otherSiloMetadata.AddMetadata("metadata.key", "other");
var director = new RequiredMatchSiloMetadataPlacementFilterDirector(
new TestLocalSiloDetails("name", "clusterId", "dnsHostName",
testLocalSiloAddress,
testLocalSiloAddress),
new TestSiloMetadataCache(new Dictionary<SiloAddress, SiloMetadata>()
{
{testOtherSiloAddress, otherSiloMetadata},
{testLocalSiloAddress, localSiloMetadata},
}));
var result = director.Filter(new RequiredMatchSiloMetadataPlacementFilterStrategy(["metadata.key"]), new PlacementTarget(),
new List<SiloAddress>() { testOtherSiloAddress }).ToList();
Assert.Empty(result);
}

[Fact, TestCategory("Functional")]
public void RequiredMatchSiloMetadataPlacementFilterDirector_FiltersToSiloWhenMatching()
{
var testLocalSiloAddress = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1000, 1);
var testOtherSiloAddress = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1001, 1);
var localSiloMetadata = new SiloMetadata();
localSiloMetadata.AddMetadata("metadata.key", "same");
var otherSiloMetadata = new SiloMetadata();
otherSiloMetadata.AddMetadata("metadata.key", "same");
var director = new RequiredMatchSiloMetadataPlacementFilterDirector(
new TestLocalSiloDetails("name", "clusterId", "dnsHostName",
testLocalSiloAddress,
testLocalSiloAddress),
new TestSiloMetadataCache(new Dictionary<SiloAddress, SiloMetadata>()
{
{testOtherSiloAddress, otherSiloMetadata},
{testLocalSiloAddress, localSiloMetadata},
}));
var result = director.Filter(new RequiredMatchSiloMetadataPlacementFilterStrategy(["metadata.key"]), new PlacementTarget(),
new List<SiloAddress>() { testOtherSiloAddress }).ToList();
Assert.NotEmpty(result);
}

[Fact, TestCategory("Functional")]
public void RequiredMatchSiloMetadataPlacementFilterDirector_FiltersToMultipleSilosWhenMatching()
{
var testLocalSiloAddress = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1000, 1);
var testOtherSiloAddress1 = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1001, 1);
var testOtherSiloAddress2 = SiloAddress.New(IPAddress.Parse("1.1.1.1"), 1002, 1);
var localSiloMetadata = new SiloMetadata();
localSiloMetadata.AddMetadata("metadata.key", "same");
var otherSiloMetadata1 = new SiloMetadata();
otherSiloMetadata1.AddMetadata("metadata.key", "same");
var otherSiloMetadata2 = new SiloMetadata();
otherSiloMetadata2.AddMetadata("metadata.key", "same");
var director = new RequiredMatchSiloMetadataPlacementFilterDirector(
new TestLocalSiloDetails("name", "clusterId", "dnsHostName",
testLocalSiloAddress,
testLocalSiloAddress),
new TestSiloMetadataCache(new Dictionary<SiloAddress, SiloMetadata>()
{
{testOtherSiloAddress1, otherSiloMetadata1},
{testOtherSiloAddress2, otherSiloMetadata2},
{testLocalSiloAddress, localSiloMetadata},
}));
var result = director.Filter(new RequiredMatchSiloMetadataPlacementFilterStrategy(["metadata.key"]), new PlacementTarget(),
new List<SiloAddress>() { testOtherSiloAddress1, testOtherSiloAddress2 }).ToList();
Assert.NotEmpty(result);
Assert.Equal(2, result.Count);
}

}
Loading

0 comments on commit 5779205

Please sign in to comment.