Search This Blog

Friday 18 January 2013

Viewing Data Distribution on GemFire Members

The following code can be used to show which member is hosting which primary piece of data in a partitioned region within GemFire. This was done with gemFire 7 but should work with GemFire 6.6 as well.

1. The server side cache.xml is as follows which pre populates some data into the region to save having to write a client to add data.
  
<?xml version="1.0"?>
<!DOCTYPE cache PUBLIC
    "-//GemStone Systems, Inc.//GemFire Declarative Caching 7.0//EN"
    "http://www.gemstone.com/dtd/cache7_0.dtd">

<cache>
    <cache-server port="40001" notify-by-subscription="true"/>
    <region name="exampleRegion">
      <region-attributes refid="PARTITION_REDUNDANT"/>
      <entry><key><string>1</string></key><value><string>MyValue1</string></value></entry>
      <entry><key><string>2</string></key><value><string>MyValue2</string></value></entry>
      <entry><key><string>3</string></key><value><string>MyValue3</string></value></entry>
      <entry><key><string>4</string></key><value><string>MyValue4</string></value></entry>
      <entry><key><string>5</string></key><value><string>MyValue5</string></value></entry>
      <entry><key><string>6</string></key><value><string>MyValue6</string></value></entry>
      <entry><key><string>7</string></key><value><string>MyValue7</string></value></entry>
      <entry><key><string>8</string></key><value><string>MyValue8</string></value></entry>
      <entry><key><string>9</string></key><value><string>MyValue9</string></value></entry>
      <entry><key><string>10</string></key><value><string>MyValue10</string></value></entry>
      <entry><key><string>11</string></key><value><string>MyValue11</string></value></entry>
      <entry><key><string>12</string></key><value><string>MyValue12</string></value></entry>
      <entry><key><string>13</string></key><value><string>MyValue13</string></value></entry>
      <entry><key><string>14</string></key><value><string>MyValue14</string></value></entry>
      <entry><key><string>15</string></key><value><string>MyValue15</string></value></entry>
      <entry><key><string>16</string></key><value><string>MyValue16</string></value></entry>
      <entry><key><string>17</string></key><value><string>MyValue17</string></value></entry>
      <entry><key><string>18</string></key><value><string>MyValue18</string></value></entry>
      <entry><key><string>19</string></key><value><string>MyValue19</string></value></entry>
      <entry><key><string>20</string></key><value><string>MyValue20</string></value></entry>
      <entry><key><string>21</string></key><value><string>MyValue21</string></value></entry>
      <entry><key><string>22</string></key><value><string>MyValue22</string></value></entry>
      <entry><key><string>23</string></key><value><string>MyValue23</string></value></entry>
      <entry><key><string>24</string></key><value><string>MyValue24</string></value></entry>
      <entry><key><string>25</string></key><value><string>MyValue25</string></value></entry>
      <entry><key><string>26</string></key><value><string>MyValue26</string></value></entry>
      <entry><key><string>27</string></key><value><string>MyValue27</string></value></entry>
      <entry><key><string>28</string></key><value><string>MyValue28</string></value></entry>
      <entry><key><string>29</string></key><value><string>MyValue29</string></value></entry>
      <entry><key><string>30</string></key><value><string>MyValue30</string></value></entry>
   </region>
</cache> 

2. When listing members we have one locator and 2 cache servers.
  
gfsh>list members;
  Name   | Id
-------- | -------------------------------------------------------
server2  | Pas-Apicellas-MacBook-Pro(server2:78035)<v2>:4641
server1  | Pas-Apicellas-MacBook-Pro(server1:78002)<v1>:13379
locator1 | Pas-Apicellas-MacBook-Pro(locator1:77992:locator):23631

3. Get a query to ensure we indeed have 30 entries in the region.
  
gfsh>query --query="select count(*) from /exampleRegion";

Result     : true
startCount : 0
endCount   : 20
Rows       : 1

Result
------
30

NEXT_STEP_NAME : END

4. Create an XML cache.xml file which will enable us to connect as a cache server to the Distributed System with storage disabled.
  
<?xml version="1.0"?>
<!DOCTYPE cache PUBLIC
    "-//GemStone Systems, Inc.//GemFire Declarative Caching 7.0//EN"
    "http://www.gemstone.com/dtd/cache7_0.dtd">

<cache>
  <cache-server port="0" />

  <region name="exampleRegion" >
       <region-attributes data-policy="partition" >
           <partition-attributes local-max-memory="0" redundant-copies="1"/>
           <subscription-attributes interest-policy="all"/>            
       </region-attributes>
  </region>
</cache> 

5. Write a java client with code as follows
  
package vmware.au.gemfire.demos.deptemp;

import java.util.Map;
import java.util.Set;

import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.partition.PartitionRegionHelper;
import com.gemstone.gemfire.distributed.DistributedMember;

public class VerifyDataLocations 
{
 private Cache cache = null;
   
 public VerifyDataLocations() 
 {
     CacheFactory cf = new CacheFactory();
     cf.set("cache-xml-file", "xml/datalocations-cache-no-storage.xml");
     cf.set("locators", "localhost[10334]");
     cache = cf.create();
 }

 public void run() throws InterruptedException
 {
  Region<String,String> exampleRegion = cache.getRegion("exampleRegion");
  System.out.println("exampleRegion size = " + exampleRegion.size());

     Set<Map.Entry<String, String>> entries = exampleRegion.entrySet();

     for (Map.Entry entry: entries) 
     {

       DistributedMember member = 
         PartitionRegionHelper.getPrimaryMemberForKey(exampleRegion, (String) entry.getKey());
       System.out.println
         (String.format("\"Primary Member [Host=%s, Id=%s - Key=%s, Value=%s]", 
          member.getHost(), member.getId(), entry.getKey(), (String) entry.getValue())); 
        
     }
     
     System.out.println("Sleeping for 20 seconds..");
     
        cache.close();
     
 }
 
 /**
  * @param args
  * @throws InterruptedException 
  */
 public static void main(String[] args) throws InterruptedException 
 {
  // TODO Auto-generated method stub
  VerifyDataLocations test = new VerifyDataLocations();
  test.run();
 }

}

6. Output as follows.

exampleRegion size = 30
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035):4641 - Key=20, Value=MyValue20]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035):4641 - Key=21, Value=MyValue21]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035):4641 - Key=22, Value=MyValue22]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035):4641 - Key=23, Value=MyValue23]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035):4641 - Key=24, Value=MyValue24]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035):4641 - Key=25, Value=MyValue25]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035):4641 - Key=26, Value=MyValue26]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035):4641 - Key=27, Value=MyValue27]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035):4641 - Key=28, Value=MyValue28]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035):4641 - Key=29, Value=MyValue29]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035):4641 - Key=30, Value=MyValue30]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035):4641 - Key=1, Value=MyValue1]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035):4641 - Key=2, Value=MyValue2]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035):4641 - Key=3, Value=MyValue3]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server2:78035):4641 - Key=4, Value=MyValue4]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002):13379 - Key=5, Value=MyValue5]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002):13379 - Key=6, Value=MyValue6]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002):13379 - Key=7, Value=MyValue7]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002):13379 - Key=8, Value=MyValue8]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002):13379 - Key=9, Value=MyValue9]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002):13379 - Key=10, Value=MyValue10]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002):13379 - Key=11, Value=MyValue11]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002):13379 - Key=12, Value=MyValue12]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002):13379 - Key=13, Value=MyValue13]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002):13379 - Key=14, Value=MyValue14]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002):13379 - Key=15, Value=MyValue15]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002):13379 - Key=16, Value=MyValue16]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002):13379 - Key=17, Value=MyValue17]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002):13379 - Key=18, Value=MyValue18]
"Primary Member [Host=Pas-Apicellas-MacBook-Pro.local, Id=Pas-Apicellas-MacBook-Pro(server1:78002):13379 - Key=19, Value=MyValue19]




No comments: