Apache.Ignite.Net CacheStoreFactory和CacheMode.Replicated不会写入所有节点

时间:2018-04-26 06:29:12

标签: c# apache ignite

在Windows 10上使用C#,Apache Ignite 2.4,我的目标是拥有多个复制节点,每个节点都有一个自定义CacheStoreFactory。我喜欢每当缓存条目改变时调用每个节点的CacheStoreFactory。 下面的代码示例导致输出到我预期的CacheMode.Partitioned的行为。使用CacheMode.Replicated,我希望每个cache.Put操作有两个输出行(因为两个节点正在运行)。我怎样才能做到这一点?

输出

Node count: 2
Started..
Write 0-0 by 63642613
Write 1-1 by 12036987
Write 2-2 by 63642613
Write 3-3 by 12036987
Write 4-4 by 63642613
Write 5-5 by 63642613
Write 6-6 by 12036987
Write 7-7 by 63642613
Write 8-8 by 12036987
Write 9-9 by 12036987
Finished count1:10 count2:10

示例代码

class Program
{
    static void Main(string[] args)
    {
        var config = new IgniteConfiguration()
        {
            IgniteInstanceName = "A",
            CacheConfiguration = new List<CacheConfiguration>
            {
                new CacheConfiguration("mycache",new QueryEntity(typeof(int), typeof(int)))
                {
                    CacheMode = CacheMode.Replicated,
                    WriteThrough = true,
                    CacheStoreFactory = new StoreFactory(),
                },
            },
        };

        var ignite1 = Ignition.Start(config);
        config.IgniteInstanceName = "B";
        var ignite2 = Ignition.Start(config);

        Console.WriteLine("Node count: " + ignite1.GetCluster().GetNodes().Count);
        ignite1.GetCluster().SetActive(true);

        var mycache1 = ignite1.GetCache<int, int>("mycache");
        var mycache2 = ignite2.GetCache<int, int>("mycache");
        Console.WriteLine("Started..");
        for (int i = 0; i < 10; i++)
        {
            mycache1.Put(i, i);
        }
        Console.WriteLine($"Finished count1:{mycache1.Count()} count2:{mycache2.Count()}");
        Console.ReadLine();

        ignite1.Dispose();
        ignite2.Dispose();
    }

    private class StoreFactory : IFactory<ICacheStore>
    {
        public ICacheStore CreateInstance() { return new Store(); }
    }

    private class Store : CacheStoreAdapter<int, int>
    {
        public override void Delete(int key) { }

        public override int Load(int key) { return 1; }

        public override void Write(int key, int val)
        {
            Console.WriteLine($"Write {key}-{val} by {this.GetHashCode()}");
        }
    }
}

1 个答案:

答案 0 :(得分:2)

每个密钥都有自己的主节点,即使是复制缓存,只有这个主节点会将值写入存储。 Ignite期望所有节点都具有相同的底层CacheStore,在这种情况下,只将值写入一次是有意义的,因为不需要再次从备份中写入它。

如果您想拥有分布式底层商店,我建议您尝试使用Ignite Native Persistence:https://apacheignite.readme.io/docs/distributed-persistent-store