In this lab, you will gain hands-on experience with creating a custom partition resolver.
-
Creating a custom partition scheme in GemFire
-
Implementing a custom partition resolver
Estimated completion time: 30 minutes
Recall that in order to ensure that related data ends up on the same member for partitioned regions, one key step is causing the buckets to be aligned between related regions.
(TODO-01
): Open the cluster/cluster.xml
file and add the appropriate configuration to the BookOrder
region to ensure bucket assignments are aligned with the Customer
region.
To create a Custom Partitioning scheme for GemFire, we need to implement the PartitionResolver
interface. We want to partition on customerId
, which is a common field shared between Customer
objects and BookOrder
objects. We will need to make this an attribute of the key as well as the orderId
. We have been using Integer
representing just the orderId
as the key up until this point, so we will need to wrap both the orderId
and customerId
in a OrderKey
class.
-
Open the
OrderKey
class-
(
TODO-02a
) Notice that it contains the key (orderNumber) and the customerNumber, which will be used for partitioning. -
(
TODO-02b
) Notice that thehashCode()
andequals()
methods are based on theorderNumber
part of the key and not thecustomerNumber
. The addition of thecustomerNumber
is for thePartitionResolver
to use, not to impact the 'equality' of entries.
-
-
Create the
PartitionResolver
-
(
TODO-03
) Open the classCustomerPartitionResolver
and implement thegetRoutingObject()
method. -
Using the
EntryOperation
, return the part of the key that will be used for ensuring thatBookOrder
objects related to a givenCustomer
land in the same bucket. -
Notice the
close()
andgetName()
methods.The close()
method is a callback method required by theCacheCallback
interface. In this case, there’s nothing to do when the cache closes. Similarly, thegetName()
method is specified by the PartitionResolver interface and offers a way to attach a name to the resolver.
-
-
(
TODO-04
) Modify the cluster XML file to register the custom partition resolver-
Open
cluster/cluster.xml
file -
Notice that, for the sake of this lab, we’ve reduced the number of buckets to
5
. That means any entries will land in one of these 5 buckets (0-4) depending on the key value used. -
Add the appropriate attributes to the
BookOrder
Region to haveio.pivotal.bookshop.domain.CustomerPartitionResolver
declared as the Partition Resolver. -
cd
into thecustom-partitioning/cluster/
folder, start gfsh, and start a locator process -
Start server1 and verify the configuration. If the first server fails to start, it’s likely due to incorrectly specifying the partition resolver.
NoteBecause you are registering a custom PartitionResolver
on the server, you’ll need to explicitly include the option:--classpath=../../build/classes/java/main
with the server start commands. -
Once server 1 starts properly, start servers 2 and 3.
-
-
Run the System test
-
(
TODO-05
) Open theDataLoader
class underio.pivotal.bookshop.buslogic
package and observe thepopulateCustomers()
method and thepopulateBookOrders()
method. Notice for example, thatcust1
has customer number5598
, and has a corresponding book order (17600
) that thepopulateBookOrders()
method creates. Notice a similar case forcust3
. -
(
TODO-06
) RunDataLoader
. This will loadCustomer
objects andBookOrder
objects into their respective regions. If your PartitionResolver has been correctly implemented,Customer
objects and relatedBookOrder
objects should end up on the same member because they will each use the same bucket number for storage. -
(
TODO-07
) Use the gfshlocate entry
command to ascertain whether a customer record and its associated book order are hosted in the same server process.Notethe domain jar file will need to be deployed (similar to what we did in the server regions lab) in order for the locate entry
command to workHere’s an example:
gfsh> locate entry --region=BookOrder --key-class=io.pivotal.bookshop.domain.OrderKey --key='{customerNumber: 5598, orderNumber: 17600}'
Note how gfsh supports a JSON representation of the OrderKey class.
Alternatively, you can verify the distribution by using the
PRBFunction
we utilized in the previousserver-regions
lab. To run this function, you’ll have to first install it, in the same manner that we did previously. Next, run the classPRBFunctionExecutor
(under theio.pivotal.training.prb
package) to list the bucket distribution.After execution completes, look at the console output and observe two things:
-
The bucket numbers for the
Customer
region andBookOrder
regions are on the same member. If they are not, go back and double check yourcluster.xml
configuration and ensure that you’ve correctly specified co-location. -
The data appears be aligned, as evidenced by the fact that there is one
Customer
entry in each bucket and that there appears to be oneBookOrder
entry for bucket3
and4
. Refer to the table below to see how the keys align to buckets. If you DON’T see the described alignment, go back and double check yourPartitionResolver
implementation.Table 1. Keys aligning with bucket numbers (assuming total buckets = 5) Key (customerNumber) Bucket Number 5598
3
5542
2
6024
4
-
-
Return to gfsh (re-connect to the locator if necessary) and stop all servers and the locator using the
shutdown
command.
-
Congratulations!! You have completed this lab.