关于Ignite AffinityKey的一些事情

时间:2017-07-29 04:37:51

标签: java apache memcached ignite

我是Apache Ignite的初学者。最近,我试图弄清楚AffinityKey是如何工作的,我遇到了一些问题。以下是我可以从Apache Ignite Example获得的课程:

public class Person implements Serializable {

private static final AtomicLong ID_GEN = new AtomicLong();

public Long id;

public Long orgId;

private transient AffinityKey<Long> key;

    public Person(Long org, String firstName, String lastName, double salary, String resume) {
    // Generate unique ID for this person.
    id = ID_GEN.incrementAndGet();

    orgId = org;

    this.firstName = firstName;
    this.lastName = lastName;
    this.salary = salary;
    this.resume = resume;
}

public Person(Long id, String firstName, String lastName) {
    this.id = id;

    this.firstName = firstName;
    this.lastName = lastName;
}

public AffinityKey<Long> key() {
    if (key == null)
        key = new AffinityKey<>(id, orgId);

    return key;
}

/*Getters and Setters*/
}



public class Organization {

private static final AtomicLong ID_GEN = new AtomicLong();

private Long id;

private String name;

public Organization(long id, String name) {
    this.id = id;
    this.name = name;
}

}

Person 类中,似乎我将 Person Organization 并置,并且它们应该放在同一个节点中。但是,它可能是错的。这是我的一些例子。

        // People.
        Person p1 = new Person((long)1, "John", "Doe", 2000, "John Doe has Master Degree.");
        Person p2 = new Person((long)2, "Jane", "Doe", 1000, "Jane Doe has Bachelor Degree.");
        Person p3 = new Person((long)2, "John", "Smith", 1000, "John Smith has Bachelor Degree.");


        Person p4 = new Person((long)2, "Jane", "Smith", 2000, "Jane Smith has Master Degree.");
        Person p5 = new Person((long)2, "John", "Harden", 1000, "John Harden has Bachelor Degree.");
        Person p6 = new Person(*(long)5*, "Jane", "Harden", 2000, "Jane Harden has Master Degree.");
        Person p7 = new Person(*(long)5*, "John", "Christopher", 1000, "John Christopher has Bachelor Degree.");
        Person p8 = new Person(*(long)5*, "Jane", "Christopher", 2000, "Jane Christopher has Master Degree.");
        Person p9 = new Person((long)6, "John", "Bush", 1000, "John Bush has Bachelor Degree.");
        Person p10 = new Person((long)3, "Jane", "Bush", 2000, "Jane Bush has Master Degree.");
        Person p11 = new Person((long)3, "John", "Gay", 1000, "John Gay has Bachelor Degree.");
        Person p12 = new Person((long)3, "Jane", "Gay", 2000, "Jane Gay has master Degree.");

        personCache.put(p1.key(), p1);
        personCache.put(p2.key(), p2);
        personCache.put(p3.key(), p3);
        personCache.put(p4.key(), p4);
        personCache.put(p5.key(), p5);
        personCache.put(p6.key(), p6);
        personCache.put(p7.key(), p7);
        personCache.put(p8.key(), p8);
        personCache.put(p9.key(), p9);
        personCache.put(p10.key(), p10);
        personCache.put(p11.key(), p11);
        personCache.put(p12.key(), p12);


        //irrelevant cache to interfere AffinityKey
        IgniteCache<Long,String> addCache=ignite.getOrCreateCache("addCache");
        addCache.put((long)1, "1");
        addCache.put((long)2, "2");
        addCache.put((long)3, "3");
        addCache.put((long)4, "4");
        //this pair has the same value with the orgId of person6-person8
        addCache.put((long)5, "5");
        addCache.put((long)6, "6");

开始时,我启动了一个节点,它显示:

local size PERSON with aff  : 12
local size ORG with aff  : 5
local size add with aff  : 6

然后,我启动了另一个节点,它显示:

local size PERSON with aff  : 9
local size ORG with aff  : 5
local size add with aff  : 5

结果显示,person6-person8 已与该对搭配 - (5,&#34; 5&#34;),我从逻辑上不想这样做。

我认为AffinityKey就是这样的:它搜索所有缓存,找出一个与AffinityKey.key()具有相同密钥的对,并将它们并置在一起。

例如:我想将A与B并置,然后编写代码

AffinityKey<Integer> key=new AffinityKey<>(A.id,B.id);

但是C与B具有相同的id,与B根本没有任何关系;

如果恰好有缓存B

IgniteCache<Integer,B>

并缓存C

IgniteCache<Integer,C>

然后我不知道哪一个会与A搭配。

总之,我该怎么做才能避免这个问题呢? AffinityKey如何在地球上运作?我很困惑。

先谢谢你的建议!!!

1 个答案:

答案 0 :(得分:1)

将密钥映射到节点的工作方式如下:

  • 每个密钥都将映射到分区。亲和力函数决定 哪个分区将包含一个密钥。
  • 然后,Affinity Function确定每个分区到节点。

使用Affinity Key时,Ignite不会搜索所有缓存。只需将密钥转换为亲和关系函数,就会传递关联密钥(在您的情况下代替人员ID,组织ID将被传递)。在这种情况下,来自一个组织的所有人都将映射到同一个分区和组织。如果分区相同,那么节点也将是相同的。

在您的情况下,所有缓存都具有相同的亲和功能(我猜它的RendezvousAffinityFunction具有默认设置),密钥具有相同的类型(长),因此来自addCache和personCache的条目(亲和关键字用于亲和关系)功能即组织ID)具有相同密钥的缓存映射在相同的节点上。您可以删除关联密钥,并查看具有相同密钥的组织和人员(例如1L)将映射到同一分区,从而映射到节点。

有关它的更多详细信息,您可以在那里找到https://apacheignite.readme.io/docs/affinity-collocation

相关问题