Partitioned Data Caching Example

Error formatting macro: composition-setup: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil

Partitioning a region splits your data storage across a number of members. If a data set is too large for one member to hold, you can store it in more than one member and work with it as if it were a single entity. You set up a partitioned region by creating region instances with the same name in all members that will hold the data, and enable partitioning during region creation. GemFire handles the physical location of data in the members that participate in a partitioned region.

You can configure your partitioned region to store redundant copies of each member's data in other members where the region is defined. This way, every data entry is hosted by multiple members, giving the region seamless high availability in the case of an application failure. Read more about partitioned data caching.

Running the Example

In this example, two members have the same partitioned region configured, with one extra copy of data entries. The example creates three entries in the partitioned region and reads them, then destroys an entry, invalidates an entry, and reads them all again.

To run this example, you must have terminal sessions configured for the QuickStart examples, as described in Setting Up the Environment.
  1. In one session, start the first member:
    $ java quickstart.PartitionedRegionVM1
    
  2. When the first VM tells you to do so, in the other session, start the second member:
    $ java quickstart.PartitionedRegionVM2
    

    This example deliberately compromises redundancy by closing PartitionedRegionVM2. Because of this, you will see this warning from PartitionedRegionVM1:

    [warning 2008/09/08 11:34:41.587 PDT PartitionedRegion /PartitionedRegion Message Processor1> tid=0x2b]
    Redundancy has dropped below 1 configured copy to 0 actual copies for /PartitionedRegion
    

Example Listings

Program and cache configuration file listings for the partitioned region shared by VMs 1 and 2, which use the same cache XML configuration file.

Error formatting macro: deck: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cache PUBLIC
    "-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
    "http://www.gemstone.com/dtd/cache6_5.dtd">
    
<!--
  | PartitionedRegion.xml
  |
  | Configures PartitionedRegion.
 -->
<cache>
	<region name="PartitionedRegion" refid="PARTITION_REDUNDANT" />
</cache>
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
package quickstart;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.Region;

/**
 * This example shows basic Region operations on a partitioned region, which has 
 * data partitions on two VMs. The region is configured to create one extra copy 
 * of each data entry. The copies are placed on different VMs, so when one VM shuts 
 * down, no data is lost. Operations proceed normally on the other VM.  Please 
 * refer to the quickstart guide for instructions on how to run this example.
 * 
 * @author GemStone Systems, Inc.
 */

public class PartitionedRegionVM1 {
    // Represents region name for the partitioned region
	public static final String  regionName = "PartitionedRegion";
	
  public static void main(String[] args) throws Exception {
	System.out.println("\nThis example shows the operation of a partitioned region on two VMs.");

	System.out.println("\nConnecting to the distributed system and creating the cache.");	
	// Create the cache which causes the cache-xml-file to be parsed
	Cache cache = new CacheFactory()
          .set("name", "PartitionedRegion")
          .set("cache-xml-file","xml/PartitionedRegion.xml")
          .create();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
	System.out.println("\nExample region, " + regionName + ", created in cache.");

        System.out.println("Please start VM2.");
        bufferedReader.readLine();

	// Get the partitioned region from cache
	Region<String,String> pr =  cache.getRegion(regionName);

	// Create three entries in the partitioned region
        final int endCount = 3;
	System.out.println("Putting " + endCount + " entries into " + regionName + ". . .");
	for (int count = 0; count < endCount; count++) {
	  String key = "key" + count;
	  String value = "value" + count;
	  System.out.println("\n     Putting entry: " + key + ", " + value);
	  pr.put(key, value);
	}

	System.out.println("\nPlease press Enter in VM2.");
        bufferedReader.readLine();
        // destroy and invalidate keys which are present in the partitioned region
	System.out.println("Destroying and invalidating entries in " + regionName + ". . .");
        final String destroyKey = "key0";
        System.out.println("\n     Destroying " + destroyKey);
	pr.destroy(destroyKey);
        final String invalidateKey = "key1";
        System.out.println("\n     Invalidating " + invalidateKey); 		  
	pr.invalidate(invalidateKey);

	System.out.println("\nPlease press Enter in VM2 again.");
	bufferedReader.readLine();
	System.out.println("Getting " + endCount + " entries from " + regionName + " after VM2 is closed. . .");
	for (int count = 0; count < endCount; count++) {
	  String key = "key" + count;
	  System.out.println("\n     Getting key " + key + ": " + pr.get(key));
	}
	
	System.out.println("\nClosing the cache and disconnecting.");
	cache.close();
   }
}
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
package quickstart;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.Region;

/**
 * This example shows basic Region operations on a partitioned region, which has 
 * data partitions on two VMs. The region is configured to create one extra copy 
 * of each data entry. The copies are placed on different VMs, so when one VM shuts 
 * down, no data is lost. Operations proceed normally on the other VM.  Please 
 * refer to the quickstart guide for instructions on how to run this example.
 * 
 * @author GemStone Systems, Inc.
 */

public class PartitionedRegionVM2
{

  public static final String regionName = "PartitionedRegion";

  public static void main(String[] args) throws Exception
  {

    System.out
        .println("\nConnecting to the distributed system and creating the cache.");

    // Create the cache which causes the cache-xml-file to be parsed
    Cache cache = new CacheFactory()
      .set("name", "PartitionedRegion")
      .set("cache-xml-file", "xml/PartitionedRegion.xml")
      .create();
    System.out.println("\nExample region, " + regionName
        + ", created in cache.");

    Region<String,String> pr = cache.getRegion(regionName);
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

    System.out.println("Please press Enter in VM1.");
    bufferedReader.readLine();

    // Get three entries from the partitioned region
    final int endCount = 3;
    System.out.println("Getting " + endCount + " entries from " + regionName  + ". . .");
    for (int count = 0; count < endCount; count++) {
      String key = "key" + count;
      System.out.println("\n     Getting key " + key + ": " + pr.get(key));
    }

    System.out.println("\nPlease press Enter in VM1 again.");
    bufferedReader.readLine();

    // Get three entries from the partitioned region after destroy and invalidate
    System.out.println("Getting the same entries from " + regionName
        + " after destroy and invalidate. . .");
    for (int count = 0; count < endCount; count++) {
      String key = "key" + count;
      System.out.println("\n     Getting key " + key + ": " + pr.get(key));
    }

    System.out.println("\nAfter VM2 is closed, please press Enter in VM1 to read the values.");
    // Close the cache and disconnect from GemFire distributed system
    System.out.println("\nClosing the cache and disconnecting.");
    cache.close();
  }
}

Related Javadocs

CacheFactory, Region, PartitionAttributes, PartitionAttributesFactory, PARTITION_REDUNDANT in RegionShortcut, and PartitionAttributes in RegionFactory

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.