Copilot commented on code in PR #7467:
URL: https://github.com/apache/ignite-3/pull/7467#discussion_r2720669643
##########
modules/platforms/dotnet/Apache.Ignite.Tests/Table/PartitionManagerTests.cs:
##########
@@ -34,14 +33,15 @@ namespace Apache.Ignite.Tests.Table;
/// <summary>
/// Tests for <see cref="IPartitionManager"/>.
/// </summary>
+[Obsolete("Obsolete. Replaced by PartitionDistributionTests.")]
public class PartitionManagerTests : IgniteTestsBase
{
[Test]
public async Task TestGetPrimaryReplicas()
{
var replicas = await Table.PartitionManager.GetPrimaryReplicasAsync();
var replicasNodes = replicas.Values.Distinct().OrderBy(x =>
((IPEndPoint)x.Address).Port).ToList();
- var replicasPartitions = replicas.Keys.Select(x =>
((HashPartition)x).PartitionId).OrderBy(x => x).ToList();
+ var replicasPartitions = replicas.Keys.Select(x =>
((HashPartition)x).Id).OrderBy(x => x).ToList();
Review Comment:
`replicasPartitions` is now a `List<long>` because `HashPartition.Id` is
`long`. The later `CollectionAssert.AreEqual(Enumerable.Range(...),
replicasPartitions, ...)` in this test will fail because it compares boxed
`int` vs boxed `long` values. Convert the expected range to `long` (or convert
`replicasPartitions` to `int` if long is not required here).
##########
modules/platforms/dotnet/Apache.Ignite.Tests/Table/PartitionDistributionTests.cs:
##########
@@ -0,0 +1,221 @@
+/*
+ * 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.Net;
+using System.Threading.Tasks;
+using Common.Compute;
+using Common.Table;
+using Ignite.Compute;
+using Ignite.Table;
+using Internal.Table;
+using Network;
+using NUnit.Framework;
+using static Common.Table.TestTables;
+
+/// <summary>
+/// Tests for <see cref="IPartitionDistribution"/>.
+/// </summary>
+public class PartitionDistributionTests : IgniteTestsBase
+{
+ [Test]
+ public async Task TestGetPrimaryReplicas()
+ {
+ var replicas = await
Table.PartitionDistribution.GetPrimaryReplicasAsync();
+ var replicasNodes = replicas.Values.Distinct().OrderBy(x =>
((IPEndPoint)x.Address).Port).ToList();
+ var replicasPartitions = replicas.Keys.Select(x =>
((HashPartition)x).Id).OrderBy(x => x).ToList();
+
+ var expectedNodes = (await Client.GetClusterNodesAsync())
+ .OrderBy(x => ((IPEndPoint)x.Address).Port)
+ .ToList();
+
+ CollectionAssert.AreEqual(expectedNodes, replicasNodes, "Primary
replicas should be distributed among all nodes");
+
+ CollectionAssert.AreEqual(
+ Enumerable.Range(0, TablePartitionCount),
Review Comment:
`replicasPartitions` is a `List<long>` (partition ids are `long`), but the
expected sequence is `Enumerable.Range(...)` which yields `int`.
`CollectionAssert.AreEqual` will fail because boxed `int` and boxed `long` are
not equal even when values match. Convert the expected sequence to `long` (or
convert `replicasPartitions` to `int` if appropriate).
```suggestion
Enumerable.Range(0, TablePartitionCount).Select(x => (long)x),
```
##########
modules/platforms/dotnet/Apache.Ignite/Table/ITable.cs:
##########
@@ -50,7 +51,13 @@ public interface ITable
/// <summary>
/// Gets the partition manager.
/// </summary>
- public IPartitionManager PartitionManager { get; }
+ [Obsolete("Replaced by PartitionDistribution property.")]
+ public IPartitionManager PartitionManager =>
(IPartitionManager)PartitionDistribution;
Review Comment:
`PartitionManager` now has a default implementation that casts
`PartitionDistribution` to `IPartitionManager`. Nothing enforces that
`PartitionDistribution` implementations also implement `IPartitionManager`, so
this can throw `InvalidCastException` at runtime for custom `ITable`
implementations. To make the obsolete API reliably usable, consider expressing
the relationship in the type system (e.g., `IPartitionDistribution` inheriting
from `IPartitionManager`, or making `PartitionManager` abstract again and
keeping both properties implemented by concrete tables).
```suggestion
public IPartitionManager PartitionManager { get; }
```
##########
modules/platforms/dotnet/Apache.Ignite/Internal/Table/PartitionManager.cs:
##########
@@ -76,20 +79,20 @@ public async ValueTask<IClusterNode>
GetPrimaryReplicaAsync(IPartition partition
throw new ArgumentException("Unsupported partition type: " +
partition.GetType());
}
- if (hashPartition.PartitionId < 0)
+ if (hashPartition.Id < 0)
{
throw new ArgumentException("Partition id can't be negative: " +
partition);
}
var replicas = await
GetPrimaryReplicasInternalAsync().ConfigureAwait(false);
var nodes = replicas.Nodes;
- if (hashPartition.PartitionId >= nodes.Length)
+ if (hashPartition.Id >= nodes.Length)
{
throw new ArgumentException($"Partition id can't be greater than
{nodes.Length - 1}: {partition}");
}
- return nodes[hashPartition.PartitionId];
+ return nodes[hashPartition.Id];
Review Comment:
`hashPartition.Id` is a `long`, but `nodes` is an array indexed by `int`.
`return nodes[hashPartition.Id];` will not compile (and even if casted
implicitly, it would be unsafe). After validating the range, cast to `int`
(prefer `checked`) and index with the `int` value (or change the partition id
type back to `int` if long is not required).
```suggestion
var index = checked((int)hashPartition.Id);
return nodes[index];
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]