You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by up...@apache.org on 2017/10/19 21:52:04 UTC

[geode-examples] branch develop updated: GEODE-3650: Add an example of a persistent region. (#18)

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

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


The following commit(s) were added to refs/heads/develop by this push:
     new e42940a  GEODE-3650: Add an example of a persistent region. (#18)
e42940a is described below

commit e42940a8ee65f810fcb2fc36f017236cfabf176b
Author: Michael "Sarge" Dodge <md...@pivotal.io>
AuthorDate: Thu Oct 19 14:52:01 2017 -0700

    GEODE-3650: Add an example of a persistent region. (#18)
    
    Adding example of recovering persistent data from disk.
---
 README.md                                          |  2 +-
 persistence/README.md                              | 61 +++++++++++++++++++
 persistence/scripts/restart.gfsh                   | 23 ++++++++
 persistence/scripts/start.gfsh                     | 24 ++++++++
 persistence/scripts/stop.gfsh                      | 20 +++++++
 .../apache/geode/examples/persistence/Example.java | 68 ++++++++++++++++++++++
 .../geode/examples/persistence/ExampleTest.java    | 41 +++++++++++++
 settings.gradle                                    |  1 +
 8 files changed, 239 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index c10b2e5..2b031d5 100644
--- a/README.md
+++ b/README.md
@@ -59,7 +59,7 @@ tutorial.
 *  [Partitioned Region](partitioned/README.md)
 *  [Put Multiple Values at Once](putall/README.md)
 *  [Functions](functions/README.md)
-*  Persistence
+*  [Persistence](persistence/README.md)
 *  OQL (Querying)
 
 ### Intermediate
diff --git a/persistence/README.md b/persistence/README.md
new file mode 100644
index 0000000..3b02ad3
--- /dev/null
+++ b/persistence/README.md
@@ -0,0 +1,61 @@
+<!--
+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.
+-->
+
+# Geode Persistence Example
+
+This is a simple example that demonstrates persistent regions.
+
+The data for a region resides in memory in the JVM for the server. When a region is persistent, the data for that region is also preserved in a disk store. The disk store uses a directory in a file system to save the operations on regions. Unlike non-persistent regions, the data for a persistent region is available even after a period where no servers for that region are running.
+
+This example assumes you have installed Java and Geode.
+
+## Steps
+
+1. From the `geode-examples/persistence` directory, build the example, and
+   run unit tests.
+
+        $ ../gradlew build
+
+2. Next start a locator, start a server, create a disk store, and create a persistent region.
+
+        $ gfsh run --file=scripts/start.gfsh
+
+3. Run the example to increment an entry the region.
+
+        $ ../gradlew run
+
+4. Observe that the first time an initial value is used.
+
+        Initialized counter to 0
+        Incremented counter to 1
+
+5. Restart down the server.
+
+        $ gfsh run --file=scripts/restart.gfsh
+
+6. Run the example to increment an entry the region.
+
+        $ ../gradlew run
+
+7. Observe that the second time the previous value is used.
+
+        Retrieved counter of 1
+        Incremented counter to 2
+
+8. Shut down the system.
+
+        $ gfsh run --file=scripts/stop.gfsh
diff --git a/persistence/scripts/restart.gfsh b/persistence/scripts/restart.gfsh
new file mode 100644
index 0000000..c5abb47
--- /dev/null
+++ b/persistence/scripts/restart.gfsh
@@ -0,0 +1,23 @@
+#
+# 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.
+#
+
+connect --locator=127.0.0.1[10334]
+
+stop server --name=server1
+
+start server --name=server1 --locators=127.0.0.1[10334] --server-port=0
+list members
diff --git a/persistence/scripts/start.gfsh b/persistence/scripts/start.gfsh
new file mode 100644
index 0000000..5be50fc
--- /dev/null
+++ b/persistence/scripts/start.gfsh
@@ -0,0 +1,24 @@
+#
+# 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.
+#
+
+start locator --name=locator --bind-address=127.0.0.1
+
+start server --name=server1 --locators=127.0.0.1[10334] --server-port=0
+list members
+
+create region --name=example-region --type=REPLICATE_PERSISTENT
+describe region --name=example-region
diff --git a/persistence/scripts/stop.gfsh b/persistence/scripts/stop.gfsh
new file mode 100644
index 0000000..528cb5e
--- /dev/null
+++ b/persistence/scripts/stop.gfsh
@@ -0,0 +1,20 @@
+#
+# 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.
+#
+
+connect --locator=127.0.0.1[10334]
+
+shutdown --include-locators=true
diff --git a/persistence/src/main/java/org/apache/geode/examples/persistence/Example.java b/persistence/src/main/java/org/apache/geode/examples/persistence/Example.java
new file mode 100644
index 0000000..a44cf60
--- /dev/null
+++ b/persistence/src/main/java/org/apache/geode/examples/persistence/Example.java
@@ -0,0 +1,68 @@
+/*
+ * 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.
+ */
+package org.apache.geode.examples.persistence;
+
+import java.util.function.Consumer;
+
+import org.apache.geode.cache.DiskStore;
+import org.apache.geode.cache.Region;
+import org.apache.geode.cache.client.ClientCache;
+import org.apache.geode.cache.client.ClientCacheFactory;
+import org.apache.geode.cache.client.ClientRegionShortcut;
+
+public class Example implements Consumer<Region<String, Integer>> {
+  static final String KEY = "counter";
+
+  int counter;
+
+  public static void main(String[] args) {
+    // connect to the locator using default port 10334
+    ClientCache cache = new ClientCacheFactory().addPoolLocator("127.0.0.1", 10334)
+        .set("log-level", "WARN").create();
+
+    // create a local region that matches the server region
+    Region<String, Integer> region =
+        cache.<String, Integer>createClientRegionFactory(ClientRegionShortcut.PROXY)
+            .create("example-region");
+
+    new Example().accept(region);
+    cache.close();
+  }
+
+  public Example() {
+    counter = -1;
+  }
+
+  public int getCounter() {
+    return counter;
+  }
+
+  @Override
+  public void accept(Region<String, Integer> region) {
+    if (region.containsKeyOnServer(KEY)) {
+      counter = region.get(KEY);
+      System.out.println("Retrieved counter of " + getCounter());
+    } else {
+      counter = 0;
+      System.out.println("Initialized counter to " + getCounter());
+    }
+
+    ++counter;
+    region.put(KEY, counter);
+
+    counter = region.get(KEY);
+    System.out.println("Incremented counter to " + getCounter());
+  }
+}
diff --git a/persistence/src/test/java/org/apache/geode/examples/persistence/ExampleTest.java b/persistence/src/test/java/org/apache/geode/examples/persistence/ExampleTest.java
new file mode 100644
index 0000000..d240f44
--- /dev/null
+++ b/persistence/src/test/java/org/apache/geode/examples/persistence/ExampleTest.java
@@ -0,0 +1,41 @@
+/*
+ * 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.
+ */
+package org.apache.geode.examples.persistence;
+
+import static org.junit.Assert.assertTrue;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+
+import org.apache.geode.cache.Region;
+import org.apache.geode.cache.execute.Execution;
+
+import org.geode.examples.util.Mocks;
+import org.junit.Test;
+
+public class ExampleTest {
+
+  @Test
+  public void testExample() throws Exception {
+    Example example = new Example();
+    final int previous = example.getCounter();
+
+    Region<String, Integer> region = Mocks.region("example-region");
+
+    example.accept(region);
+    assertTrue(previous < example.getCounter());
+  }
+}
diff --git a/settings.gradle b/settings.gradle
index 83314bd..9c78100 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -23,3 +23,4 @@ include 'loader'
 include 'putall'
 include 'clientSecurity'
 include 'functions'
+include 'persistence'

-- 
To stop receiving notification emails like this one, please contact
['"commits@geode.apache.org" <co...@geode.apache.org>'].