Search code examples
c#.netignitegridgain

Why can't I add data with an AffinityKey into an Apache Ignite cache?


I am trying to use a GridGain Community on-prem cluster to evaluate the product for use at scale.

I am trying to use it with the .Net thin client. I wanted to use a combination of the Key-Value API and the SQL API to get the best performance.

As documented in the code below, I get a strange exception when trying to insert data when I have an AffinityKey defined. I have tried numerous combinations of attributes and schema definitions and I get unintuitive behaviour in a a lot of cases. Can anyone point out what I am doing wrong?

using Apache.Ignite.Core;
using Apache.Ignite.Core.Cache.Affinity;
using Apache.Ignite.Core.Cache.Configuration;
using Apache.Ignite.Core.Cache.Query;
using Apache.Ignite.Core.Client;
using Apache.Ignite.Core.Client.Cache;

public record PERSONKEY(
    [property:AffinityKeyMapped] [property: QuerySqlField(NotNull = true)] long COMPANYID, 
    [property: QuerySqlField(NotNull = true)] long PERSONID);

public record PERSONVALUE(
    [property: QuerySqlField(NotNull = true)] string FIRSTNAME, 
    [property: QuerySqlField(NotNull = true)] string LASTNAME);

internal class Program
{
    public static void Main(string[] args)
    {
        var cfg = new IgniteClientConfiguration
        {
            Endpoints = new[] {"10.7.116.49:10800"},
        };
        using var client = Ignition.StartClient(cfg);

        var schemaBuilder = client.GetOrCreateCache<int, int>(new CacheClientConfiguration
        {
            Name = "RR", SqlSchema = "PUBLIC"
        });

        schemaBuilder.Query(new SqlFieldsQuery(
            $@"CREATE TABLE IF NOT EXISTS PERSON (
                COMPANYID BIGINT NOT NULL,
                PERSONID BIGINT NOT NULL,
                FIRSTNAME VARCHAR NOT NULL,
                LASTNAME VARCHAR NOT NULL,
                PRIMARY KEY(COMPANYID, PERSONID)
            ) WITH ""TEMPLATE=PARTITIONED,BACKUPS=1,AFFINITY_KEY=COMPANYID,CACHE_NAME=PERSON,
                     KEY_TYPE={typeof(PERSONKEY).FullName},VALUE_TYPE={typeof(PERSONVALUE).FullName}"""
        ) { Schema = "PUBLIC" }).GetAll();
        
        var cache = client.GetCache<PERSONKEY, PERSONVALUE>("PERSON");
        
        var key = new PERSONKEY(1, 2);
        var value = new PERSONVALUE("JOHN", "SMITH");
        
        // Throws an exception
        // Apache.Ignite.Core.Common.IgniteException: Affinity keys are not supported.
        // Object 'PERSONKEY { COMPANYID = 1, PERSONID = 2 }' has an affinity key.
        cache.Put(key, value);
        
        // Does not throw an exception
        cache.PutAll(new[]{KeyValuePair.Create(key, value)});
        
        var people = cache.Query(new SqlFieldsQuery("SELECT * FROM PERSON WHERE COMPANYID = ?", key.COMPANYID)).GetAll();
        // Correctly prints "1 2 JOHN SMITH"
        Console.WriteLine(string.Join("\n", people.Select(p => $"{p[0]} {p[1]} {p[2]} {p[3]}")));
    }
}

Solution

  • It is a bug, I've created a ticket: https://issues.apache.org/jira/browse/IGNITE-19359

    Workaround: disable partition awareness in IgniteClientConfiguration:

    var cfg = new IgniteClientConfiguration
    {
        EnablePartitionAwareness = false,
        ...
    };