You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by bb...@apache.org on 2019/01/04 21:46:04 UTC

[geode-native] branch develop updated: GEODE-6210: Add 'transaction' example for cpp (#430)

This is an automated email from the ASF dual-hosted git repository.

bbender pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode-native.git


The following commit(s) were added to refs/heads/develop by this push:
     new dd86276  GEODE-6210: Add 'transaction' example for cpp (#430)
dd86276 is described below

commit dd86276a6b1cb9684465e0f879b67846f316d2a5
Author: M. Oleske <mo...@users.noreply.github.com>
AuthorDate: Fri Jan 4 13:45:59 2019 -0800

    GEODE-6210: Add 'transaction' example for cpp (#430)
    
    * GEODE-6210: Add 'transaction' example for cpp
    
     - demonstrates commit and rollback of transactions
     - handles exceptions
    - Incorporates retry logic
    
    Co-authored-by: Michael Oleske <mo...@pivotal.io>
---
 examples/cpp/CMakeLists.txt                        |  9 ++-
 examples/cpp/CMakeLists.txt.in                     |  3 +-
 examples/cpp/transaction/README.md                 | 61 +++++++++++++++
 examples/cpp/transaction/main.cpp                  | 89 ++++++++++++++++++++++
 .../startserver.sh}                                | 23 +++---
 .../stopserver.sh}                                 | 23 +++---
 6 files changed, 186 insertions(+), 22 deletions(-)

diff --git a/examples/cpp/CMakeLists.txt b/examples/cpp/CMakeLists.txt
index a6e8b80..b87067d 100644
--- a/examples/cpp/CMakeLists.txt
+++ b/examples/cpp/CMakeLists.txt
@@ -36,6 +36,9 @@ add_example(NAME continuousquery
 add_example(NAME dataserializable
     SOURCE main.cpp Order.cpp Order.hpp)
 
+add_example(NAME function-execution
+        SOURCE main.cpp)
+
 add_example(NAME pdxserializable
     SOURCE main.cpp Order.cpp Order.hpp)
 
@@ -45,12 +48,12 @@ add_example(NAME pdxserializer
 add_example(NAME put-get-remove
     SOURCE main.cpp)
 
-add_example(NAME function-execution
-    SOURCE main.cpp)
-
 add_example(NAME remotequery
     SOURCE main.cpp Order.cpp Order.hpp)
 
+add_example(NAME transaction
+        SOURCE main.cpp)
+
   install(DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}
   DESTINATION examples/
   PATTERN "./*.in" EXCLUDE
diff --git a/examples/cpp/CMakeLists.txt.in b/examples/cpp/CMakeLists.txt.in
index a1854c0..a9852a1 100644
--- a/examples/cpp/CMakeLists.txt.in
+++ b/examples/cpp/CMakeLists.txt.in
@@ -19,8 +19,9 @@ project(@PRODUCT_DLL_NAME@.Cpp.Examples LANGUAGES NONE)
 
 add_subdirectory(continuousquery)
 add_subdirectory(dataserializable)
+add_subdirectory(function-execution)
 add_subdirectory(pdxserializable)
 add_subdirectory(pdxserializer)
 add_subdirectory(put-get-remove)
-add_subdirectory(function-execution)
 add_subdirectory(remotequery)
+add_subdirectory(transaction)
diff --git a/examples/cpp/transaction/README.md b/examples/cpp/transaction/README.md
new file mode 100644
index 0000000..de5cf6e
--- /dev/null
+++ b/examples/cpp/transaction/README.md
@@ -0,0 +1,61 @@
+# Transaction example
+This is a very simple example showing how to use TransactionManager.  This example shows
+how to begin a transaction, commit a transaction, and rollback a transaction while showing
+exception handling.  We commit two keys and rollback adding a third key and destroying an
+existing key while showing how to handle exceptions.
+
+## Prerequisites
+* An installation of Apache Geode.
+* Apache Geode Native, built and installed.
+* Apache Geode Native examples, built and installed.
+* A `GEODE_HOME` environment variable set to the location of the Apache Geode installation.
+* `GEODE_HOME/bin` in the execution path.
+
+## Running
+1. Set the current directory to the `transaction` directory in your example workspace.
+
+  ```
+  $ cd workspace/examples/cpp/transaction
+  ```
+
+1. Run the `startserver.sh` script to start the Geode server, create a region, and populate the region with sample data.
+
+  ```
+  $ sh ./startserver.sh
+  /Users/user/geode/bin/gfsh
+
+  (1) Executing - start locator --name=locator
+  ...
+  (2) Executing - start server --name=server
+  ...
+  (3) Executing - create region --name=exampleRegion --type=PARTITION
+
+  Member | Status
+  ------ | ----------------------------------------------
+  server | Region "/exampleRegion" created on "server"
+  ```
+
+1. Execute `transaction`:
+
+  ```
+  $ build/transaction
+    Created cache
+    Created region 'exampleRegion'
+    Rolled back transaction - retrying(4)
+    Rolled back transaction - retrying(3)
+    Rolled back transaction - retrying(2)
+    Committed transaction - exiting
+  ```
+
+1. Stop the server
+
+  ```
+  $ sh ./stopserver.sh
+  /Users/user/geode/bin/gfsh
+  (1) Executing - connect
+  ...
+  (2) Executing - stop server --name=server
+  ...
+  (3) Executing - stop locator --name=locator
+  ....
+  ```
diff --git a/examples/cpp/transaction/main.cpp b/examples/cpp/transaction/main.cpp
new file mode 100644
index 0000000..9104644
--- /dev/null
+++ b/examples/cpp/transaction/main.cpp
@@ -0,0 +1,89 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <iostream>
+#include <random>
+
+#include <geode/CacheFactory.hpp>
+#include <geode/CacheTransactionManager.hpp>
+#include <geode/PoolManager.hpp>
+#include <geode/RegionFactory.hpp>
+#include <geode/RegionShortcut.hpp>
+
+using apache::geode::client::Cache;
+using apache::geode::client::CacheFactory;
+using apache::geode::client::CacheTransactionManager;
+using apache::geode::client::RegionShortcut;
+
+auto keys = {
+    "Key1",
+    "Key2",
+    "Key3",
+    "Key4",
+    "Key5",
+    "Key6",
+    "Key7",
+    "Key8",
+    "Key9",
+    "Key10"
+};
+
+
+int getValueFromExternalSystem() {
+  static thread_local std::default_random_engine generator(std::random_device{}());
+  auto value = std::uniform_int_distribution<int32_t>{0, 9}(generator);
+
+  if (!value) {
+    throw "failed to get from external system";
+  }
+
+  return value;
+}
+
+int main(int argc, char** argv) {
+  auto cache = CacheFactory().set("log-level", "none").create();
+  auto poolFactory = cache.getPoolManager().createFactory();
+
+  std::cout << "Created cache" << std::endl;
+
+  poolFactory.addLocator("localhost", 10334);
+  auto pool = poolFactory.create("pool");
+  auto regionFactory = cache.createRegionFactory(RegionShortcut::PROXY);
+  auto region = regionFactory.setPoolName("pool").create("exampleRegion");
+
+  std::cout << "Created region 'exampleRegion'" << std::endl;
+
+  auto transactionManager = cache.getCacheTransactionManager();
+
+  auto retries = 5;
+  while (retries--) {
+    try {
+      transactionManager->begin();
+      for (auto& key : keys) {
+        auto value = getValueFromExternalSystem();
+        region->put(key, value);
+      }
+      transactionManager->commit();
+      std::cout << "Committed transaction - exiting" << std::endl;
+      break;
+    } catch ( ... ) {
+      transactionManager->rollback();
+      std::cout << "Rolled back transaction - retrying(" << retries << ")" << std::endl;
+    }
+  }
+}
+
diff --git a/examples/cpp/CMakeLists.txt.in b/examples/cpp/transaction/startserver.sh
old mode 100644
new mode 100755
similarity index 63%
copy from examples/cpp/CMakeLists.txt.in
copy to examples/cpp/transaction/startserver.sh
index a1854c0..fce5f21
--- a/examples/cpp/CMakeLists.txt.in
+++ b/examples/cpp/transaction/startserver.sh
@@ -13,14 +13,19 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-cmake_minimum_required(VERSION 3.10)
+#!/usr/bin/env bash
+GFSH_PATH=""
+which gfsh 2> /dev/null
 
-project(@PRODUCT_DLL_NAME@.Cpp.Examples LANGUAGES NONE)
+if [ $? -eq 0 ]; then
+    GFSH_PATH="gfsh"
+else
+    if [ "$GEODE_HOME" == "" ]; then
+        echo "Could not find gfsh. Please set the GEODE_HOME path."
+        echo "e.g. export GEODE_HOME=<path to Geode>"
+    else
+        GFSH_PATH=$GEODE_HOME/bin/gfsh
+    fi
+fi
 
-add_subdirectory(continuousquery)
-add_subdirectory(dataserializable)
-add_subdirectory(pdxserializable)
-add_subdirectory(pdxserializer)
-add_subdirectory(put-get-remove)
-add_subdirectory(function-execution)
-add_subdirectory(remotequery)
+$GFSH_PATH  -e "start locator --name=locator" -e "start server --name=server"  -e "create region --name=exampleRegion --type=PARTITION"
diff --git a/examples/cpp/CMakeLists.txt.in b/examples/cpp/transaction/stopserver.sh
old mode 100644
new mode 100755
similarity index 65%
copy from examples/cpp/CMakeLists.txt.in
copy to examples/cpp/transaction/stopserver.sh
index a1854c0..67a0f85
--- a/examples/cpp/CMakeLists.txt.in
+++ b/examples/cpp/transaction/stopserver.sh
@@ -13,14 +13,19 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
-cmake_minimum_required(VERSION 3.10)
+#!/usr/bin/env bash
+GFSH_PATH=""
+which gfsh 2> /dev/null
 
-project(@PRODUCT_DLL_NAME@.Cpp.Examples LANGUAGES NONE)
+if [ $? -eq 0 ]; then
+    GFSH_PATH="gfsh"
+else
+    if [ "$GEODE_HOME" == "" ]; then
+        echo "Could not find gfsh. Please set the GEODE_HOME path."
+        echo "e.g. export GEODE_HOME=<path to Geode>"
+    else
+        GFSH_PATH=$GEODE_HOME/bin/gfsh
+    fi
+fi
 
-add_subdirectory(continuousquery)
-add_subdirectory(dataserializable)
-add_subdirectory(pdxserializable)
-add_subdirectory(pdxserializer)
-add_subdirectory(put-get-remove)
-add_subdirectory(function-execution)
-add_subdirectory(remotequery)
+$GFSH_PATH -e "connect" -e "stop server --name=server" -e "stop locator --name=locator"