biao li
biao li

Reputation: 121

How can I get information about who is the primary node of the distributed queue and who is the backup node?

I add some data into distributed queue and I am wondering how I can get some information about who is the primary node of the queue and who is the backup node.

Thanks, Bill

Upvotes: 0

Views: 96

Answers (2)

Valentin Kulichenko
Valentin Kulichenko

Reputation: 8390

GridGain cache queue is distributed, i.e. different elements can be stored on different nodes. If your cache has backups then each element will be duplicated on two or more nodes. So there is no way to determine the primary or backup node for non-collocated queue.

If queue is collocated, all its items will be stored on one node (this can be used if you have many small queues instead of one large queue). In this case you can get primary and backup node for this queue passing queue name to affinity, like this:

// Create collocated queue (3rd parameter is true).
GridCacheQueue<String> queue = cache.dataStructures().queue("MyQueue", 100, true, true);

// Get primary and backup nodes using cache affinity.    
Iterator<GridNode> nodes = cache.affinity().mapKeyToPrimaryAndBackups("MyQueue").iterator();

// First element in collection is always the primary node.
GridNode primary = nodes.next();

// Other nodes in collection are backup nodes.
GridNode backup1 = nodes.next();
GridNode backup2 = nodes.next();

You don't see anything during iteration over cache because queue elements are internal entries, so they are accessible only via GridCacheQueue API, but not via GridCache API. Here is an example:

// Create or get a queue.
GridCacheQueue<String> queue = cache.dataStructures().queue("MyQueue", 100, false, true);

for (String item : queue)
    System.out.println(item);

Upvotes: 2

biao li
biao li

Reputation: 121

So far I know that distributed queue is based on GridGain cache. However, I run the following code and I get empty cache.

        GridCache<Object, Object> cache = grid.cache("partitioned_tx");
        GridCacheDataStructures dataStruct = cache.dataStructures();
        GridCacheQueue<String> queue = dataStruct.queue("myQueueName", 0, false, true);

        for (int i = 0; i < 20; i++){
             queue.add("Value-"+i);
        }

        GridCacheAffinity<Object> affinity = cache.affinity();
        int part;
        Collection<GridNode> nodes;

        for(Object key:cache.keySet()){
            System.out.println("key="+key.toString());
            part = affinity.partition(key);
            nodes = affinity.mapPartitionToPrimaryAndBackups(part);

            for(GridNode node:nodes){
                System.out.println("key of "+key.toString()+" is primary: "+affinity.isPrimary(node, key));
                System.out.println("key of "+key.toString()+" is backup: "+affinity.isBackup(node, key));
            }
        }

Upvotes: -1

Related Questions