Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IGNITE-22150 .NET: Add PartitionManager API #4062

Merged
merged 36 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
6ec7729
IGNITE-22150 .NET: Add partition API
ptupitsyn Jul 8, 2024
5fbedad
IPartitionManager done
ptupitsyn Jul 8, 2024
2a0fe73
wip PartitionManager
ptupitsyn Jul 8, 2024
e5fda4c
wip ClientOp
ptupitsyn Jul 8, 2024
105df10
GetPrimaryReplicasAsync
ptupitsyn Jul 8, 2024
b40e73b
wip Cached partition objects.
ptupitsyn Jul 8, 2024
4a87b50
wip Cached partition objects.
ptupitsyn Jul 8, 2024
92f45f7
wip GetPartitionAsync
ptupitsyn Jul 8, 2024
9bb1d89
wip GetPartitionInternal
ptupitsyn Jul 8, 2024
f3b071f
wip PartitionManagerTests
ptupitsyn Jul 8, 2024
2c4e9e6
fix ClusterNode.Read
ptupitsyn Jul 8, 2024
908fafb
TestGetPrimaryReplicas done
ptupitsyn Jul 8, 2024
4b6b255
tests
ptupitsyn Jul 9, 2024
feac042
TestGetPrimaryReplica
ptupitsyn Jul 9, 2024
83db897
Add PlatformTestNodeRunner.PartitionJob
ptupitsyn Jul 9, 2024
355fff3
TestGetPartitionForTupleKey done
ptupitsyn Jul 9, 2024
b582c4d
tests done
ptupitsyn Jul 9, 2024
479731c
adding cached behavior tests
ptupitsyn Jul 9, 2024
b311e7c
Make GetPartitionArray static
ptupitsyn Jul 9, 2024
a3b9e1c
Implement replica caching
ptupitsyn Jul 9, 2024
e72f1a6
Implement replica caching
ptupitsyn Jul 9, 2024
815aeb2
Implement replica caching
ptupitsyn Jul 9, 2024
a93257f
Implement replica caching
ptupitsyn Jul 9, 2024
f352920
Add TestGetPrimaryReplicaPartitionIdOutOfRangeThrows
ptupitsyn Jul 9, 2024
2a5b456
cleanup
ptupitsyn Jul 9, 2024
951da36
wip TestPrimaryReplicasCacheInvalidation
ptupitsyn Jul 9, 2024
56aa3af
wip TestPrimaryReplicasCacheInvalidation
ptupitsyn Jul 9, 2024
98cdd7d
TestPrimaryReplicaCacheInvalidation done
ptupitsyn Jul 9, 2024
06bd518
Fix RetryReadPolicy
ptupitsyn Jul 9, 2024
017c37b
Fix ToString tests
ptupitsyn Jul 9, 2024
320331e
ClusterNode band-aid
ptupitsyn Jul 9, 2024
16ae896
cleanup
ptupitsyn Jul 9, 2024
c08e5f4
Fix checkstyle
ptupitsyn Jul 9, 2024
75814da
Merge branch 'refs/heads/main' into ignite-22150
ptupitsyn Jul 10, 2024
94407e4
Add IEquatable to IPartition
ptupitsyn Jul 10, 2024
48bd294
Add TestPartitionEquality
ptupitsyn Jul 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

import java.time.Instant;
import java.time.temporal.ChronoUnit;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
Expand Down Expand Up @@ -85,11 +84,6 @@ public CompletableFuture<Map<Partition, ClusterNode>> primaryReplicasAsync() {
w -> w.out().packInt(tbl.tableId()),
r -> {
ClientMessageUnpacker in = r.in();

if (in.tryUnpackNil()) {
return Collections.<Partition, ClusterNode>emptyMap();
}

int size = in.unpackInt();

Map<Partition, ClusterNode> res = new HashMap<>(size);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class ComputeTests : IgniteTestsBase

public static readonly JobDescriptor<string, object> CheckedExceptionJob = new(PlatformTestNodeRunner + "$CheckedExceptionJob");

public static readonly JobDescriptor<long, int> PartitionJob = new(PlatformTestNodeRunner + "$PartitionJob");

[Test]
public async Task TestGetClusterNodes()
{
Expand Down
23 changes: 23 additions & 0 deletions modules/platforms/dotnet/Apache.Ignite.Tests/FakeServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,29 @@ protected override void Handle(Socket handler, CancellationToken cancellationTok
Send(handler, requestId, Array.Empty<byte>());
continue;
}

case ClientOp.PrimaryReplicasGet:
{
using var arrayBufferWriter = new PooledArrayBuffer();
var writer = new MsgPackWriter(arrayBufferWriter);

writer.Write(PartitionAssignment.Length);

for (var index = 0; index < PartitionAssignment.Length; index++)
{
var nodeId = PartitionAssignment[index];

writer.Write(index); // Partition id.
writer.Write(4); // Prop count.
writer.Write(nodeId); // Id.
writer.Write(nodeId); // Name.
writer.Write("localhost"); // Host.
writer.Write(10900 + index); // Port.
}

Send(handler, requestId, arrayBufferWriter);
continue;
}
}

// Fake error message for any other op code.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ namespace Apache.Ignite.Tests
public class IgniteTestsBase
{
protected const string TableName = "TBL1";
protected const int TablePartitionCount = 10;

protected const string TableAllColumnsName = "TBL_ALL_COLUMNS";
protected const string TableAllColumnsNotNullName = "TBL_ALL_COLUMNS_NOT_NULL";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Apache.Ignite.Tests.Table;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Compute;
using Ignite.Compute;
using Ignite.Table;
using Internal.Table;
using NUnit.Framework;

/// <summary>
/// Tests for <see cref="IPartitionManager"/>.
/// </summary>
public class PartitionManagerTests : IgniteTestsBase
{
[Test]
public async Task TestGetPrimaryReplicas()
{
var replicas = await Table.PartitionManager.GetPrimaryReplicasAsync();
var replicasNodes = replicas.Values.Distinct().OrderBy(x => x.Address.Port).ToList();
var replicasPartitions = replicas.Keys.Select(x => ((HashPartition)x).PartitionId).OrderBy(x => x).ToList();

var expectedNodes = (await Client.GetClusterNodesAsync()).OrderBy(x => x.Address.Port).ToList();

CollectionAssert.AreEqual(expectedNodes, replicasNodes, "Primary replicas should be distributed among all nodes");

CollectionAssert.AreEqual(
Enumerable.Range(0, TablePartitionCount),
replicasPartitions,
"Primary replicas map should have all partitions");
}

[Test]
public async Task TestGetPrimaryReplicasReturnsCachedPartitionInstances()
{
var partitions1 = await GetPartitions();
var partitions2 = await GetPartitions();

for (int i = 0; i < partitions1.Count; i++)
{
Assert.AreSame(partitions1[i], partitions2[i]);
}

async Task<List<HashPartition>> GetPartitions()
{
var replicas = await Table.PartitionManager.GetPrimaryReplicasAsync();
return replicas.Keys.Cast<HashPartition>().OrderBy(x => x.PartitionId).ToList();
}
}

[Test]
public async Task TestGetPrimaryReplica()
{
var nodes = await Client.GetClusterNodesAsync();

for (int partId = 0; partId < TablePartitionCount; partId++)
{
var partition = new HashPartition(partId);
var replica = await Table.PartitionManager.GetPrimaryReplicaAsync(partition);

CollectionAssert.Contains(nodes, replica);
}
}

[Test]
public void TestGetPrimaryReplicaNegativePartitionIdThrows()
{
var ex = Assert.ThrowsAsync<ArgumentException>(
async () => await Table.PartitionManager.GetPrimaryReplicaAsync(new HashPartition(-1)));

Assert.AreEqual("Partition id can't be negative: HashPartition { PartitionId = -1 }", ex.Message);
}

[Test]
public void TestGetPrimaryReplicaPartitionIdOutOfRangeThrows()
{
var ex = Assert.ThrowsAsync<ArgumentException>(
async () => await Table.PartitionManager.GetPrimaryReplicaAsync(new HashPartition(10)));

Assert.AreEqual("Partition id can't be greater than 9: HashPartition { PartitionId = 10 }", ex.Message);
}

[Test]
public void TestGetPrimaryReplicaUnknownPartitionClassThrows()
{
var ex = Assert.ThrowsAsync<ArgumentException>(
async () => await Table.PartitionManager.GetPrimaryReplicaAsync(new MyPartition()));

Assert.AreEqual($"Unsupported partition type: {typeof(MyPartition)}", ex.Message);
}

[Test]
public async Task TestGetPartitionForKey([Values(true, false)] bool poco)
{
var jobTarget = JobTarget.AnyNode(await Client.GetClusterNodesAsync());

for (int id = 0; id < 30; id++)
{
var partition = poco
? await Table.PartitionManager.GetPartitionAsync(GetPoco(id))
: await Table.PartitionManager.GetPartitionAsync(GetTuple(id));

var partitionJobExec = await Client.Compute.SubmitAsync(jobTarget, ComputeTests.PartitionJob, id);
var expectedPartition = await partitionJobExec.GetResultAsync();

Assert.AreEqual(expectedPartition, ((HashPartition)partition).PartitionId);
}
}

[Test]
public async Task TestGetPartitionReturnsCachedInstance()
{
var partition1 = await Table.PartitionManager.GetPartitionAsync(GetTuple(1));
var partition2 = await Table.PartitionManager.GetPartitionAsync(GetTuple(1));

Assert.AreSame(partition1, partition2);
}

[Test]
public async Task TestPrimaryReplicaCacheInvalidation()
{
using var server = new FakeServer
{
PartitionAssignmentTimestamp = 123,
PartitionAssignment = new[] { "n1", "n2" }
};

using var client = await server.ConnectClientAsync();
var table = await client.Tables.GetTableAsync(FakeServer.ExistingTableName);
var partition = new HashPartition(0);

var replica1 = await table!.PartitionManager.GetPrimaryReplicaAsync(partition);
Assert.AreEqual("n1", replica1.Name);

server.PartitionAssignmentTimestamp = 124;
server.PartitionAssignment = new[] { "n2", "n1" };

await client.Tables.GetTablesAsync(); // Trigger cache invalidation with any response.

var replica2 = await table.PartitionManager.GetPrimaryReplicaAsync(partition);
Assert.AreEqual("n2", replica2.Name);
}

private class MyPartition : IPartition
{
// No-op.
}
}
5 changes: 5 additions & 0 deletions modules/platforms/dotnet/Apache.Ignite/ClientOperationType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,11 @@ public enum ClientOperationType
/// </summary>
ComputeChangePriority,

/// <summary>
/// Get primary replicas (<see cref="IPartitionManager.GetPrimaryReplicasAsync"/>).
/// </summary>
PrimaryReplicasGet,

/// <summary>
/// Send data streamer batch (<see cref="IDataStreamerTarget{T}"/>).
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,7 @@ IList<IClusterNode> Read()

for (var i = 0; i < count; i++)
{
var fieldCount = r.ReadInt32();
Debug.Assert(fieldCount == 4, "fieldCount == 4");

res.Add(new ClusterNode(
id: r.ReadString(),
name: r.ReadString(),
endpoint: new IPEndPoint(IPAddress.Parse(r.ReadString()), r.ReadInt32())));
res.Add(ClusterNode.Read(ref r));
}

return res;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@
namespace Apache.Ignite.Internal.Network
{
using System;
using System.Diagnostics;
using System.Linq;
using System.Net;
using Ignite.Network;
using Proto.MsgPack;

/// <summary>
/// Cluster node.
Expand Down Expand Up @@ -74,5 +77,30 @@ public bool Equals(ClusterNode? other)

/// <inheritdoc/>
public override int GetHashCode() => HashCode.Combine(Id, Name, Address);

/// <summary>
/// Read node from reader.
/// </summary>
/// <param name="r">Reader.</param>
/// <returns>Cluster node.</returns>
internal static ClusterNode Read(ref MsgPackReader r)
{
var fieldCount = r.ReadInt32();
Debug.Assert(fieldCount == 4, "fieldCount == 4");

var id = r.ReadString();
var name = r.ReadString();
var addr = r.ReadString();
var port = r.ReadInt32();

// TODO IGNITE-22695 .NET: ClusterNode.Address does not support host names
var ipAddress = IPAddress.TryParse(addr, out var ip)
? ip
: Dns.GetHostEntry(addr).AddressList.FirstOrDefault() ?? IPAddress.Loopback;

var endPoint = new IPEndPoint(ipAddress, port);

return new ClusterNode(id, name, endPoint);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ internal enum ClientOp
/** Send streamer batch. */
StreamerBatchSend = 62,

/** Get primary replicas. */
PrimaryReplicasGet = 65,

/** Send streamer batch with receiver. */
StreamerWithReceiverBatchSend = 66
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ internal static class ClientOpExtensions
ClientOp.PartitionAssignmentGet => null,
ClientOp.SqlParamMeta => null,
ClientOp.StreamerBatchSend => ClientOperationType.StreamerBatchSend,
ClientOp.PrimaryReplicasGet => ClientOperationType.PrimaryReplicasGet,
ClientOp.StreamerWithReceiverBatchSend => ClientOperationType.StreamerBatchSend,

// Do not return null from default arm intentionally so we don't forget to update this when new ClientOp values are added.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace Apache.Ignite.Internal.Table;

using Ignite.Table;

/// <summary>
/// Hash partition.
/// </summary>
/// <param name="PartitionId">Partition id.</param>
internal sealed record HashPartition(int PartitionId) : IPartition; // Not a struct to avoid interface boxing.
Loading