Skip to content

Commit 9ee9a8f

Browse files
ptupitsynisapego
andcommitted
IGNITE-14293 .NET: Fix AffinityKey metadata in QueryEntity.KeyType
`AffinityKey` system type, when used as a `QueryEntiry` key, got overwritten by `UnmanagedCallbacks.BinaryTypeGet` call, which broke the mapping to a corresponding Java type. * Add missing check for existing registered type (actual fix in Marshaller.cs) * Add check for system type overwrite * Add test for `AffinityKey` + `QueryEntity` Co-authored-by: Igor Sapego <isapego@apache.org> (cherry picked from commit 8cc19b1)
1 parent 4db0a37 commit 9ee9a8f

2 files changed

Lines changed: 61 additions & 0 deletions

File tree

modules/platforms/dotnet/Apache.Ignite.Core.Tests/Cache/Affinity/AffinityTest.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,46 @@ public void TestAffinityKeyMappedWithQueryEntitySpringXml()
121121
}
122122
}
123123

124+
/// <summary>
125+
/// Tests that <see cref="AffinityKey"/> works when used as <see cref="QueryEntity.KeyType"/>.
126+
/// </summary>
127+
[Test]
128+
public void TestAffinityKeyWithQueryEntity()
129+
{
130+
var cacheCfg = new CacheConfiguration(TestUtils.TestName)
131+
{
132+
QueryEntities = new List<QueryEntity>
133+
{
134+
new QueryEntity(typeof(AffinityKey), typeof(QueryEntityValue))
135+
}
136+
};
137+
138+
var ignite = Ignition.GetIgnite("grid-0");
139+
var cache = ignite.GetOrCreateCache<AffinityKey, QueryEntityValue>(cacheCfg);
140+
var aff = ignite.GetAffinity(cache.Name);
141+
142+
var ignite2 = Ignition.GetIgnite("grid-1");
143+
var cache2 = ignite2.GetOrCreateCache<AffinityKey, QueryEntityValue>(cacheCfg);
144+
var aff2 = ignite2.GetAffinity(cache2.Name);
145+
146+
// Check mapping.
147+
for (var i = 0; i < 100; i++)
148+
{
149+
Assert.AreEqual(aff.GetPartition(i), aff.GetPartition(new AffinityKey("foo" + i, i)));
150+
Assert.AreEqual(aff2.GetPartition(i), aff2.GetPartition(new AffinityKey("bar" + i, i)));
151+
Assert.AreEqual(aff.GetPartition(i), aff2.GetPartition(i));
152+
}
153+
154+
// Check put/get.
155+
var key = new AffinityKey("x", 123);
156+
var expected = new QueryEntityValue {Name = "y", AffKey = 321};
157+
cache[key] = expected;
158+
159+
var val = cache2[key];
160+
Assert.AreEqual(expected.Name, val.Name);
161+
Assert.AreEqual(expected.AffKey, val.AffKey);
162+
}
163+
124164
/// <summary>
125165
/// Tests that <see cref="AffinityKeyMappedAttribute"/> works when used on a property of a type that is
126166
/// specified as <see cref="QueryEntity.KeyType"/> or <see cref="QueryEntity.ValueType"/>.

modules/platforms/dotnet/Apache.Ignite.Core/Impl/Binary/Marshaller.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace Apache.Ignite.Core.Impl.Binary
2020
using System;
2121
using System.Collections.Generic;
2222
using System.Diagnostics;
23+
using System.Diagnostics.CodeAnalysis;
2324
using System.Linq;
2425
using System.Runtime.Serialization;
2526
using System.Threading;
@@ -565,6 +566,11 @@ public IBinaryTypeDescriptor GetDescriptor(bool userType, int typeId, bool requi
565566

566567
if (type != null)
567568
{
569+
if (_typeToDesc.TryGetValue(type, out desc))
570+
{
571+
return desc;
572+
}
573+
568574
return AddUserType(type, typeId, GetTypeName(type), true, desc);
569575
}
570576
}
@@ -665,11 +671,25 @@ private BinaryFullTypeDescriptor AddUserType(Type type, int typeId, string typeN
665671
ThrowConflictingTypeError(type, desc0.Type, typeId);
666672
}
667673

674+
ValidateRegistration(type);
668675
_typeToDesc.Set(type, desc);
669676

670677
return desc;
671678
}
672679

680+
/// <summary>
681+
/// Validates type registration.
682+
/// </summary>
683+
[ExcludeFromCodeCoverage]
684+
private void ValidateRegistration(Type type)
685+
{
686+
BinaryFullTypeDescriptor desc;
687+
if (_typeToDesc.TryGetValue(type, out desc) && !desc.UserType)
688+
{
689+
throw new BinaryObjectException("Invalid attempt to overwrite system type registration: " + type);
690+
}
691+
}
692+
673693
/// <summary>
674694
/// Throws the conflicting type error.
675695
/// </summary>
@@ -805,6 +825,7 @@ private BinaryFullTypeDescriptor AddType(Type type, int typeId, string typeName,
805825

806826
if (type != null)
807827
{
828+
ValidateRegistration(type);
808829
_typeToDesc.Set(type, descriptor);
809830
}
810831

0 commit comments

Comments
 (0)