In this lab, you will gain hands-on experience with writing a GemFire function. A test client will invoke the function to compute a total across multiple GemFire servers.
-
Developing a GemFire function
-
Execute the function on multiple servers
-
Invoke the function from a client application
Estimated completion time: 30 minutes
-
(
TODO-01
) Open thecluster/cluster.xml
file and add the necessary configuration to enable PDX Serialization; also, be sure to setread-serialized=true
. -
(
TODO-02
) Start a locator and two servers in a manner similar to what we’ve done in past labs, by runninggfsh run --file=start.gf
from within thecluster
folder. -
From your IDE, execute the
OrderLoader
class (in packageio.pivotal.bookshop.buslogic
). This will place some data into the the Order Region. -
Via the
gfsh
shell, run a query to inspect the contents of theBookOrder
region, and verify that we’ve got two orders. Optionally, try to locate each of the entries to determine which primary server they reside on.
Function execution allows you to move function behavior to the application member hosting the data. In this exercise, you develop a function to perform a sum over a specified field for all entries in a given region. This will offer an opportunity to explore one of the use cases for functions in GemFire. For simplicity, this 'generic' summing function assumes that the type used for summing is a java.lang.Float
.
To perform this task, open the server-functions
project in your IDE.
-
Open the
GenericSumFunction
class in theio.pivotal.bookshop.buslogic
package. Notice that this class implements theFunction
interface, as well as theDeclarable
interface. -
Implement the
execute()
method as follows:-
(
TODO-03
) Enforce that theFunctionContext
is an instance ofRegionFunctionContext
. It’s important that this be done as this function is only designed to be run via theonRegion(..)
method calls. You might throw aFunctionException
if this prerequisite isn’t met. Assuming theFunctionContext
argument is an instance ofRegionFunctionContext
, cast it to an instance ofRegionFunctionContext
. -
(
TODO-04
) Use theFunctionContext
to get the parameter passed from the client. This will represent the name of the field on thePdxInstance
for which the summing operation will be performed. -
(
TODO-05
) Use thePartitionRegionHelper
class to get all the local region data. We’ll later add additional configuration to ensure that the local data obtained is strictly primary region data. Also initialize an instance ofBigDecimal
as this will be used to hold the sum that will be returned by the function. -
(
TODO-06
) Iterate over the local data, which should be a collection ofPdxInstance
objects. We’ll later add configuration to enable PDX Serialization and to enforce that data read on the server side remains serialized. -
(
TODO-07
) Use the argument provided to de-serialize (extract) the field that will be used for summing. Initially, extract anObject
. In a successive step, make sure the field is an instance ofFloat
(we are making that assumption). If it is an instance ofFloat
, add this value to the current sum. -
(
TODO-08
) Once all entries have been processed, send the final sum back to the caller. Since you will be only sending one result for this execution, make sure that you signal that this is the last result.
-
We have two options for deploying the function:
-
We can either restart the servers with the function’s class file in the classpath, or
-
we can use the
gfsh deploy
command to hot-deploy the code to all our servers
Let’s use the latter option.
-
Build and package the code:
cd server-functions gradle assemble
This action should produce a jar file in the
build/libs
folder namedserver-functions.jar
. -
cd
intobuild/libs
, and startgfsh
, and invoke:gfsh> connect gfsh> list functions
-
Observe that, for now, no functions are listed in the output. Invoke:
gfsh> deploy --jar=server-functions.jar
Now repeat the
list functions
command. You should see that gfsh has now deployed the function to all servers, without having to restart them.
Ok, with our function deployed, it’s now time to turn our attention to exercising it.
You will test the function using the JUnit test SummingFunctionTest
(found in src/test/java
).
-
In the next lab exercise, you will focus on the writing the code to execute the function and process the results. For now, you will just worry about executing the function and verifying the results.
-
(
TODO-09
) Go ahead and run the program from the IDE. The test should pass. If not, go back and review your implementation, and also make sure that you ran theOrderLoader
. -
Stop the servers and locator.
Congratulations!! You have completed this lab.