Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migrated to Confluence 5.3

...

Say, for example, you want to colocate all Trades by Symbol.The key is implemented by TradeKey class which also implements the PartitionResolver interface

...

Code Block
languagejava
public class TradeKey implements PartitionResolver {

...


private String tradeID;

...


private String symbol;

...


public TradingKey(String id, Trade someTradeObj){
 tradeID = id;
 // Get the symbol from the Trade instance
 symbol = someTradeObj.getSymbol();

...


}

...


public Serializable getRoutingObject(EntryOperation opDetails){
 return this.symbol;

...


} 

Essentially, when data colocation is required, all entry keys returning the same 'routing object' (symbol in this case) are guaranteed to be collocated on the same partition. Geode hashes the returned symbol to a bucket which is mapped to a partition node.

Applications can also introduce a partition resolver for a partitioned data region non-intrusively by specifying the PartitionResolver class to invoke when data is published.

...

Code Block
languagexml
 <cache>
  <region name="myPrDataRegion">
    <region-attributes>
      <partition-attributes>
         <partition-resolver>
              <class-name>com.gemstone.gemfire.cache.MyPartitionResolver</class-name>

...


          </partition-resolver>
       <partition-attributes>
     </region-attributes>
  </region>

...


<cache>
Code Block
languagejava
 

...

//Create a new PartitionResolver 
PartitionResolver resolver = new MyPartitionResolver();

...



//Set the PartitionResolver to partition attributes

...


PartitionAttributes attrs = new PartitionAttributesFactory().setPartitionResolver(resolver).create();

...


//Create a partition data region

...


Region region = new RegionFactory().setPartitionAttributes(attrs).create("myPrDataRegion");

...

Colocating related entries across multiple partitioned regions 

So, for instance, in a Orders partitioned region, all order entries that return the same CustomerID will be guaranteed to reside on the same node.

Code Block
languagexml
<cache>
  <region name="Customers">
    <region-attributes>
      <partition-attributes>
  

...

       <partition-resolver>
              <class-name>com.gemstone.gemfire.cache.CustomerPartitionResolver</class-name>
          </partition-resolver>
       <partition-attributes>
     </region-attributes>
  </region>
  <region name="Orders">
    <region-attributes>
      <partition-attributes colocated-with="Customers"> // COLOCATION ATTRIBUTE
  

...

 

...

 

...

     <partition-resolver> // Name
              <class-name>com.gemstone.gemfire.cache.CustomerPartitionResolver</class-name>
          </partition-resolver>
       <partition-attributes>
     </region-attributes>
  </region>
  <region name="Shipments">
    <region-attributes>
      <partition-attributes colocated-with="Customers"> // COLOCATION ATTRIBUTE
 

...

 

...

       <partition-resolver> // Name
              <class-name>com.gemstone.gemfire.cache.CustomerPartitionResolver</class-name>
          </partition-resolver>
       <partition-attributes>
     </region-attributes>
  </region>

...


</

...

cache> 

...

Code Block
languagejava
//Create a new PartitionResolver based on CustomerID, so that all orders from same CustomerId will yield to a single bucket

...


PartitionResolver resolver = new CustomerPartitionResolver();

...




//Set Partition resolver to partition attributes

...


PartitionAttributes attrs = new PartitionAttributesFactory().setPartitionResolver(resolver).create();

...


//Create a Customers Partition Region

...


Region customers = new RegionFactory().setPartitionAttributes(attrs).create("Customers");

...


// Entry ops allowed before creation of associated partitioned regions.
 
 
// Create PartitionAttributes for Orders which should colocated with Customers

...


attrs = new PartitionAttributesFactory().setPartitionResolver(resolver).setColocatedWith(customers.getFullPath()).create();

...


//Create a Orders partition region

...


Region orders = new RegionFactory().setPartitionAttributes(attrs ).create("Orders");

...


// Even orders partioned region is now ready for operations

...



// Create PartitionAttributes for Shipments which should colocated with Customers

...


attrs = new PartitionAttributesFactory().setPartitionResolver(resolver).setColocatedWith(customers.getFullPath()).create(); 
//Create a Shipments partition region

...


Region shipments = new RegionFactory().setPartitionAttributes(attrs ).create("Shipments");

...

Following rules apply while defining colocation :

...

Applications can declare and register the functions using declarative means (cache.xml) or through the Geode API. All registered functions have an identifier. Identifying functions allows the administrator to monitor function activity and cancel them on demand.

Code Block
languagexml
<cache>
 ...

...


<function-service>
  <function>
    <class-name> com.bigFatCompany.tradeService.cache.func.TradeCalc1</class-name> <!--implementsFunction and Declarable interfaces --> 
  </function>
  <function>
    <class-name> com.bigFatCompany.tradeService.cache.func.TradeCalc2</class-name> <!--implementsFunction and Declarable interfaces --> 
  </function>  
</function-service>
 ...

...


</cache>

...

Code Block
languagejava
Registering functions in programmatic way :
Function function1 = new TradeCalc1();//TradeCalc1 implements Function interface
Function function2 =new TradeCalc2();//TradeCalc2 implements Function interface
FunctionService.registerFunction(function1);
FunctionService.registerFunction(function2);

...

 

+Functions that need to be executed across remote members should be registered in each member before invoking. +Applications may create inline functions which need not be registered. +Id (returned from Function.getFunctionId()) can be any arbitrary string. +Modifying function instance after registration has no effect on function execution.

...