Skip to content

Latest commit

 

History

History
104 lines (59 loc) · 5.04 KB

File metadata and controls

104 lines (59 loc) · 5.04 KB

Writing Server Side Functions

Introduction

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.

Function Execution

Concepts you will gain experience with:
  1. Developing a GemFire function

  2. Execute the function on multiple servers

  3. Invoke the function from a client application

Estimated completion time: 30 minutes

Start up the Back End

  1. (TODO-01) Open the cluster/cluster.xml file and add the necessary configuration to enable PDX Serialization; also, be sure to set read-serialized=true.

  2. (TODO-02) Start a locator and two servers in a manner similar to what we’ve done in past labs, by running gfsh run --file=start.gf from within the cluster folder.

  3. From your IDE, execute the OrderLoader class (in package io.pivotal.bookshop.buslogic). This will place some data into the the Order Region.

  4. Via the gfsh shell, run a query to inspect the contents of the BookOrder 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.

Develop the Function

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.

  1. Open the GenericSumFunction class in the io.pivotal.bookshop.buslogic package. Notice that this class implements the Function interface, as well as the Declarable interface.

  2. Implement the execute() method as follows:

    1. (TODO-03) Enforce that the FunctionContext is an instance of RegionFunctionContext. It’s important that this be done as this function is only designed to be run via the onRegion(..) method calls. You might throw a FunctionException if this prerequisite isn’t met. Assuming the FunctionContext argument is an instance of RegionFunctionContext, cast it to an instance of RegionFunctionContext.

    2. (TODO-04) Use the FunctionContext to get the parameter passed from the client. This will represent the name of the field on the PdxInstance for which the summing operation will be performed.

    3. (TODO-05) Use the PartitionRegionHelper 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 of BigDecimal as this will be used to hold the sum that will be returned by the function.

    4. (TODO-06) Iterate over the local data, which should be a collection of PdxInstance objects. We’ll later add configuration to enable PDX Serialization and to enforce that data read on the server side remains serialized.

    5. (TODO-07) Use the argument provided to de-serialize (extract) the field that will be used for summing. Initially, extract an Object. In a successive step, make sure the field is an instance of Float (we are making that assumption). If it is an instance of Float, add this value to the current sum.

    6. (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.

Deploy the Function

We have two options for deploying the function:

  1. We can either restart the servers with the function’s class file in the classpath, or

  2. we can use the gfsh deploy command to hot-deploy the code to all our servers

Let’s use the latter option.

  1. Build and package the code:

    cd server-functions
    gradle assemble

    This action should produce a jar file in the build/libs folder named server-functions.jar.

  2. cd into build/libs, and start gfsh, and invoke:

    gfsh> connect
    gfsh> list functions
  3. 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.

Call the Function

You will test the function using the JUnit test SummingFunctionTest (found in src/test/java).

  1. 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.

  2. (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 the OrderLoader.

  3. Stop the servers and locator.

Congratulations!! You have completed this lab.