The cacheRunner C++ example is an interactive program for modifying and viewing GemFire Enterprise cache contents as a C++ native client interacts with a GemFire cache server. The cacheRunner program joins the distributed system, creates a cache, and then accepts command-line input for inspecting, modifying, and remotely querying the cache while working with a cache server. XML files are provided to configure cacheRunner for different functional operations or behavior.
You can review the C++ source code for this example by opening the files in the
cacheRunnerdirectory that have.cppand.hppfile extensions.
This example is divided into three parts, with each part requiring different sets of configuration files for both cacheRunner and the cache server.
The cacheRunner example can also be configured using a local gfcpp.properties file.
cacheRunner to look at GemFire regions, entries, directories, and files to get a feel for how the product works.The cacheRunner C++ example is located in the GemFire Enterprise Native Client installation under
GemFireInstallDirectory/native_client/examples/cacheRunner.
The instructions in this example apply to the Windows operating system. Alter them as needed for your particular operating system.
The cacheRunner example uses a GemFire Java cache server configuration file, cacherunner.xml. When the Java cache server starts, cacherunner.xml creates a root region and two regions on the server named listenerWriterLoader and sub1. This is a description of each cache server region and how each region is configured:
entry3 that has a value of 3.0 listenerWriterLoader A region with distributed scope, populated with an entry named entry1 that has a value of 1.0sub1 A region with local scopeThe cacheRunner application comes with a set of XML configuration files that configure its local cache and demonstrate various operations with the cache server. The following XML files are used with this example. The application must be run separately for each of these XML files:
tcr_cache.xml
Establishes local and distributed cache operations. tcr_cacheless.xml Data is retrieved from the cache server but not retained in the local cache.csQueryPortfolios.xml Used with the querying examples; creates a subregion named Portfolios, which is a region of stock portfolios whose keys are the portfolio ID.
These procedures introduce a few of the cacheRunner
commands. For information on the others, enter
help
or
?
at the session prompt. In the procedures, the lines you type are shown in a boldface fixed font. System output is shown in a regular fixed font.
Examples that interact with a Java cache server require specific environment configurations so the Java cache server will run properly. Follow the configuration steps listed below that apply to your operating system:
bin/setenv script for your system. Refer to the system configuration information in the GemFire Enterprise Developer's
Guide if you need help with this step.JAVA_HOME and GF_JAVA_HOME environment variables to your installed Java JRE or JDK. See the installation information in the GemFire Enterprise System Administrator's Guide for the versions of Java that are compatible with GemFire Enterprise. The JAVA_HOME setting is for your applications, and GF_JAVA_HOME is for the GemFire scripts. Some downloads of GemFire include a JRE in the jre directory of the installation. If you have a $GEMFIRE/jre directory, the GemFire scripts will automatically use it and you could also use it for your JAVA_HOME setting. If you have no $GEMFIRE/jre directory (such as the .zip file download of the product), then you must have a compatible Java JRE or JDK installed and you must set JAVA_HOME and GF_JAVA_HOME to point to it.$JAVA_HOME/bin to the start of your PATH.The following is a list of the environment configuration commands for the cacheRunner example. Choose the set of commands that are appropriate for your operating system. The text that you type is shown in bold.
These configurations only need to be performed for the sessions that invoke the Java cache server.
Bourne and Korn shells (sh, ksh, bash)
% cd GemFireInstallDirectory
% . bin/setenv.sh
% JAVA_HOME=$GEMFIRE/jre;export JAVA_HOME
% GF_JAVA_HOME=$GEMFIRE/jre;export GF_JAVA_HOME
% PATH=$JAVA_HOME/bin:$PATH;export PATH
C shells (csh, tcsh)
% cd GemFireInstallDirectory
% source bin/setenv.csh
% setenv JAVA_HOME $GEMFIRE/jre
% setenv GF_JAVA_HOME $GEMFIRE/jre
% setenv PATH $JAVA_HOME/bin;${PATH}
Windows
> cd GemFireInstallDirectory
> bin\setenv.bat
> set JAVA_HOME=%GEMFIRE%\jre
> set GF_JAVA_HOME=%GEMFIRE%\jre
> set PATH=%JAVA_HOME%\bin;%PATH%
To start the cache server, create a session and complete the following steps:
Configure the session environment according to the steps listed in Configuring the Environment.
cacheRunner
directory, then start the cache server:
cd native_client/examples/cacheRunner
cacheserver start cache-xml-file=cacherunner.xml
The cache server is initialized using the region settings in the cacherunner.xml file in the cacheRunner directory. A message similar to the following appears, indicating that the cache server is running:
Cacheserver pid: 2120 status: running
Follow these steps to start the cacheRunner example using the tcr_cache.xml initialization file:
Create two cacheRunner sessions.
Go to the cacheRunner directory in both sessions:
cd native_client/examples/cacheRunner
cacheRunner
application in both sessions, specifying tcr_cache.xml:
cacherunner.exe tcr_cache.xml
The cacheRunner client is initialized using the region settings in the tcr_cache.xml file in the cacheRunner directory. The application creates a root region and subregions, then the /root region prompt appears. As you move from region to region, the prompt includes a path to tell you where you are, such as
/root/listenerWriterLoader>.
Set one of the two cacheRunner sessions aside. You only need one client session for the following two Producing Data exercises.
cacheRunner client, check the contents of the root
region:
ls
Region Entries:
Subregions:
listenerWriterLoader <Endpoint:localhost:50505, Client Notification:true>
Portfolios (Endpoint:localhost:50505, Client Notification:true)
/root>
The endpoint and client notification status displays for the listenerWriterLoader and Portfolios regions. To communicate with a cache server, the endpoint value for the client region must be the same as the port number defined for the cache server.
In this first exercise you produce data in the root region of the cacheRunner local client cache.
In the cacherunner client:
root region:
put entry1 ball str
This line creates an entry whose key is entry1
and whose value is the string ball.
ls
Region Entries:
entry1 -> String: ball
inv -l entry1
ls
Region Entries:
entry1 -> No value in cache.
The value for the entry has been removed from the local cache.
get entry1
entry1 -> No value in cache.
The entry cannot be retrieved from the cache server because the client root cache does not have an endpoint established with the cache server, so it did not propagate the entry to the server.
In this exercise you produce data in a local region that is distributed to the cache server.
In the cacherunner client:
listenerWriterLoader region, then check the contents:
chrgn listenerWriterLoader
ls
Region Entries:
Subregions:
There are no entries in the local client cache for the listenerWriterLoader region. However, remember that the listenerWriterLoader region on the cache server was initialized with an entry named entry1 that has a value of 1.0.
entry1 from the cache server:
get entry1
entry1 -> String: 1.0
put entry2 bat str
ls
Region Entries:
entry2 -> String: bat
entry1 -> String: 1.0
entry2 in the local cache and then check the contents:
inv -l entry2
ls
Region Entries:
entry2 -> No value in cache.
entry1 -> String: 1.0
entry2 from the cache server and then check the contents:
get entry2
ls
Region Entries:
entry2 -> String: bat
entry1 -> String: 1.0
The entry in the local cache is updated with the value stored in the cache server.
cacheRunner application and the cache server running for the next exercise.In this exercise you learn how client regions can register interest in cache server entry keys through the use of regular expressions. By registering interest, a client region receives update notifications from the cache server for the keys of interest whose strings match the expression. Regular expressions only work with string keys.
In the first cacherunner client:
cacheRunner client session you used in the previous exercises is still set to the listenerWriterLoader region. If it isn't, then set it to that region:
chrgn listenerWriterLoader
put key-1 val1
put key-2 val2
put key-3 val3
put key-4 val4
In the second cacherunner client:
cacheRunner client session that you created at the start of this example, then go to its listenerWriterLoader region:
chrgn listenerWriterLoader
put in the first client:
get key-1
key-1 -> Bytes: val1
get key-2
key-2 -> Bytes: val2
get key-3
key-3 -> Bytes: val3
get key-4
key-4 -> Bytes: val4
regex key-[2-3]
This registers interest in key-2 and key-3. Remember that registering interest with regular expressions only works with string keys.
In the first client:
Update
key-2andkey-3:
put key-2 newVal2
put key-3 newVal3
In the second client:
ls
Region Entries:
key-3 -> Bytes: newVal3
key-1 -> Bytes: val1
key-4 -> Bytes: val4
key-2 -> Bytes: newVal2
The updated values for key-2 and key-3 are listed.
key-2 and key-3 regular expressions:
unregex key-[2-3]
In the first client:
Update
key-2andkey-3:
put key-2 123
put key-3 456
In the second client:
Check the contents of the region:
ls
Region Entries:
key-3 -> Bytes: newVal3
key-1 -> Bytes: val1
key-4 -> Bytes: val4
key-2 -> Bytes: newVal2The old values for
key-2andkey-3are listed because no interest is registered for them, so no update notifications were received from the cache server.
Keep both cacheRunner applications and the cache server running for the next exercise.
In this exercise you add a cache listener to an existing region, then observe how the cache listener reports events that occur in the cache. A cache listener reports entry and region events both before and after they occur. Entry events are create, update, invalidate, and destroy. Region events are clear, create, invalidate, and destroy. A cache listener only receives callbacks for events occurring in the local cache. The local cache listener is not invoked when an entry is destroyed in a remote cache. However, if that destroy is distributed then the subsequent destroy in the local cache causes the cache listener's afterDestroy method to be called.
Some region attributes are immutable after they have been established, while others, such as a cache listener, can be modified after the region has been created. To see how a cache listener works, complete these steps:
In both cacherunner clients:
Make sure that the two
cacheRunnerclient sessions you used in the previous exercise are still set to thelistenerWriterLoaderregion. If they aren't, then set both of them to that region:
chrgn listenerWriterLoader
In the first cacherunner client:
Put some entries in the region:
put key-1 val1
put key-2 val2
put key-3 val3
put key-4 val4
In the second cacherunner client:
put in the first client:
get key-1
key-1 -> Bytes: val1
get key-2
key-2 -> Bytes: newVal2
get key-3
key-3 -> Bytes: newVal3
get key-4
key-4 -> Bytes: val4
reg key-1 key-2
set listener
put key-5 val-5
The cache listener reports the events that occur:
TestCacheListener.afterCreate :
Print Event:
Cache Event Origin Local
Region is : /root/listenerWriterLoader
Key is : key-5
Old value is : NULL
New value is : val-5
In the first client:
Update
key-1andkey-2:
put key-1 newVal1
put key-2 newVal2
Look at the second client. The cache listener reports the events that occur for the updates:
TestCacheListener.afterUpdate :
Print Event:
Cache Event Origin Local
Region is : /root/listenerWriterLoader
Key is : key-1
Old value is : NULL
New value is : newVal1
TestCacheListener.afterUpdate :
Print Event:
Cache Event Origin Local
Region is : /root/listenerWriterLoader
Key is : key-2
Old value is : NULL
New value is : newVal2
In the second client:
null:
set listener null
put key-6 val-6
No event reporting occurs because the cache listener was removed from the region.
In the first client:
Update
key-2andkey-3:
put key-2 123
put key-3 456
Again, no event reporting occurs because the cache listener was removed.
Ending the client sessions:
You need to keep one of the
cacheRunnersessions open for the next exercise, but the second session can be closed. Follow these steps:
cacheRunner application, but leave the session open:
exit
exit
exit
In this exercise you learn about applications that do not perform local caching. An application can receive all events on a region without storing the data that comes with the events. Consequently, the application can pass data through to other receivers without the overhead of caching. This configuration lets you separate event processing from data management.
To see how a cacheless region works, complete these steps:
cacheRunner session, start the cacheRunner
application and specify tcr_cacheless.xml as its initialization file:
cacherunner.exe tcr_cacheless.xml
listenerWriterLoader region, then check its cache contents:
chrgn listenerWriterLoader
ls
Region Entries:
Subregions:
There are no entries in the local client cache for the listenerWriterLoader region. However, remember that the listenerWriterLoader region on the cache server contains an entry named entry1 that has a value of 1.0.
entry1 from the cache server:
get entry1
entry1 -> String: 1.0
The local cache acknowledges that the entry was received.
ls
Region Entries:
Subregions:
The entry is not retained in the local client cache.
Close the cacheRunner application:
exit
Close the cacheRunner session:
exit
Stop the cache server:
cacheserver stop
exit
In this example, cacheRunner accepts query strings in input and runs them against the cached data stored on a Java cache server. The cacheRunner example uses a GemFire Java cache server configuration file, csQueryPortfolios.xml. When the cache server starts, csQueryPortfolios.xml creates a root region on the server. It also creates a subregion named Portfolios, which is a region of stock portfolios whose keys are the portfolio ID.
These procedures introduce a few of the querying commands. For information on others, enter
help
or
?
at the cacheRunner client session prompt.
In the procedures, the lines you type are shown in a boldface fixed font. System output is shown in a regular fixed font.
To start the cache server, create a session and complete the following steps:
Configure the session environment according to the steps listed in Configuring the Environment.
Add the classes for javaobject.jar to your CLASSPATH by entering the following in a single line:
set CLASSPATH=%GEMFIRE%\native_client\examples\cacheRunner\javaobject.jar;%CLASSPATH%
The file javaobject.jar is required for registering the portfolio and position object on the Java cache server.
cacheRunner
directory, then start the cache server:
cd native_client/examples/cacheRunner
cacheserver start cache-xml-file=csQueryPortfolios.xml
The csQueryPortfolios.xml file configures the server with a root region and a subregion named Portfolios with five portfolio objects. A message similar to the following appears, indicating that the cache server is running:
Cacheserver pid: 2120 status: running
Follow these steps to start the cacheRunner client using the tcr_cacheless.xml initialization file:
From the GemFire product installation directory, create a cacheRunner session.
Go to the cacheRunner directory in the session:
cd native_client/examples/cacheRunner
cacheRunner
application, specifying tcr_cacheless.xml:
cacherunner.exe tcr_cacheless.xml
The cacheRunner client is initialized using the region settings in the tcr_cacheless.xml file in the cacheRunner directory. The application creates a root region and two subregions, then the /root region prompt appears. As you move from region to region, the prompt includes a path to tell you where you are, such as
/root/Portfolios>.
cacheRunner client, go to the Portfolios
region:
chrgn Portfolios
In these exercises you invoke the execute method to submit several queries that are run on the cache server, then the results are returned to the local client cache.
In the cacherunner client:
root/Portfolios region on the cache server. Enter the following command:
exec select distinct ID, status from /root/Portfolios
This is the query output:
Query results : Found 5 row
Struct with 2 fields
ID : 4
status : inactive
Struct with 2 fields
ID : 5
status : active
Struct with 2 fields
ID : 2
status : inactive
Struct with 2 fields
ID : 3
status : active
Struct with 2 fields
ID : 1
status : active
exec select distinct ID from /root/Portfolios
Query results : Found 5 row
2
4
1
3
5
root/Portfolios region on the cache server:
exec select distinct ID, pkid, getType from /root/Portfolios where 1=1
Query results : Found 5 row
Struct with 3 fields
ID : 2
pkid : 2
getType : type2
Struct with 3 fields
ID : 1
pkid : 1
getType : type1
Struct with 3 fields
ID : 3
pkid : 3
getType : type3
Struct with 3 fields
ID : 5
pkid : 5
getType : type2
Struct with 3 fields
ID : 4
pkid : 4
getType : type1
In these exercises you use region query shortcut methods to submit queries to the cache server, then the results are returned to the local client cache. Query shortcuts take the query "predicate" as a parameter (the part after the WHERE clause), or they can take a normal full query. The three query shortcut methods are described below:
query Executes a query and returns the results. existsValue Executes a query and returns TRUE or FALSE based on whether any results (rows) are obtained. selectValue Executes a query and returns only a single result (row). If no results are obtained then it returns NULL. If more than one result (row) is obtained then a QueryException occurs. In the cacherunner client:
query shortcut, which returns the Portfolios objects with an ID of 1:
query ID=1
Query result:
query predicate is ID=1
Query results : Found 1 row
PortfolioObject: [ ID=1 status=active type=type1 pkid=1
P1: NULL P2: NULL ]
The query shortcut takes the predicate string ID=1, constructs the full query statement, then executes the query.
existsValue shortcut.
The query returns either TRUE or FALSE, depending on whether the result is obtained.
existsvalue ID=1
Query result:
query predicate is ID=1
Query result is TRUE
selectValue shortcut. The query returns the object types for the root/Portfolios region on the cache server:
selectValue ID=2
Query result:
query predicate is ID=2
PortfolioObject: [ ID=2 status=inactive type=type2 pkid=2
P1: NULL P2: NULL ]
cacheRunner application:
exit
cacheRunner session:
exit
cacheserver stop
exit
In this example, cacheRunner demonstrates server failover to highly available client queue backups by failing over to a secondary cache server when the primary server becomes unavailable. The secondary server continues to provide the latest entry updates to the client when the primary server fails.
In the procedures, the lines you type are shown in a boldface fixed font. System output is shown in a regular fixed font.
To start the primary and secondary cache servers, create a session and complete the following steps:
Configure the session environment according to the steps listed in Configuring the Environment.
cacheRunner
directory:
cd native_client/examples/cacheRunner
cacheserver start cache-xml-file=cacherunner.xml -dir=gfecs1
The gfecs1 directory contains a copy of cacherunner.xml, which specifies 50505 for the BridgeServer port for the primary cache server.
cacheserver start cache-xml-file=cacherunner2.xml -dir=gfecs2
The gfecs2 directory contains a copy of cacherunner2.xml, which specifies 50506 for the BridgeServer port for the secondary cache server.
To start the cacheRunner client, complete the following steps:
From the GemFire product installation directory, create a session.
Go to the cacheRunner directory in the session:
cd native_client/examples/cacheRunner
cacheRunner
client, specifying tcr_hacache.xml:
cacherunner.exe tcr_hacache.xml
The cacheRunner client is initialized using the settings in tcr_hacache.xml in the cacheRunner directory. The XML specifies two cache-level server endpoints that the client connects to (50505 for the primary, and 50506 for the secondary). The redundancy-level=1 attribute specifies the number of redundant, or secondary, servers to maintain in addition to the primary server.
In these steps you produce data in the cacheRunner local client cache. The primary and secondary cache servers receive the updated values.
cacheRunner client, go to the listenerWriterLoader region:
chrgn listenerWriterLoader
put entry1 ball str
This line creates an entry whose key is entry1
and whose value is the string ball.
ls
Region Entries:
entry1 -> String: ball
inv -l entry1
ls
Region Entries:
entry1 -> No value in cache.
The value for the entry has been removed from the local cache.
get entry1
entry1 -> String: ball
In this procedure you stop and start the primary and secondary servers to initiate a server failover condition.
cacheserver stop -dir=gfecs1
The following message displays in the server session:
CacheServer stopped.
A redundancy level warning continuously displays in the client session as a result of the stopped server:
Requested redundancy level 1 is not satisfiable with 1 servers available
get entry1
entry1 -> String: ball
The updated entry is retrieved from the server.
put entry2 bat str
get entry2
entry2 -> String: bat
cacheserver start cache-xml-file=cacherunner.xml -dir=gfecs1
cacheserver stop-dir=gfecs2
get entry2
entry2 -> String: bat
Both the primary and secondary servers maintain client cache updates in case a server failover occurs.
cacheRunner application:
exit
cacheRunner session:
exit
cacheserver stop-dir=gfecs1
exit
This product ships configured to run with default system properties. If you need to run in a non-default configuration, GemFire also takes a system-level configuration file. Copy the gfcpp.properties file into your cacheRunner directory from the defaultSystem directory and edit it as needed. For example, to change the name of the cache.xml file, uncomment this line and change the file name:
#cache-xml-file=cache.xml
Copyright © 2005-2007 by GemStone Systems, Inc. All rights reserved. GemStone®, GemFire™, and the GemStone logo are trademarks or registered trademarks of GemStone Systems, Inc. All other trade names or trademarks are the property of their respective owners. Information in this document is subject to change without notice.