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 2018/01/11 17:28:23 UTC

[geode-examples] branch develop updated: GEODE-4119: Add an example of eviction.

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 58eb042  GEODE-4119: Add an example of eviction.
58eb042 is described below

commit 58eb042941fe8624bf4f70e794ff727a9cfcda4b
Author: Sarge <md...@pivotal.io>
AuthorDate: Tue Dec 19 10:50:08 2017 -0800

    GEODE-4119: Add an example of eviction.
---
 README.md                                          |  2 +-
 eviction/README.md                                 | 53 ++++++++++++++++
 eviction/scripts/start.gfsh                        | 25 ++++++++
 eviction/scripts/stop.gfsh                         | 19 ++++++
 .../apache/geode_examples/eviction/Example.java    | 72 ++++++++++++++++++++++
 settings.gradle                                    |  1 +
 6 files changed, 171 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index e77194b..5be09cf 100644
--- a/README.md
+++ b/README.md
@@ -74,7 +74,7 @@ tutorial.
 *  [Async Event Queues & Async Event Listeners](async/README.md)
 *  Continuous Querying
 *  Transactions
-*  Eviction
+*  [Eviction](eviction/README.md)
 *  Expiration
 *  Overflow
 *  Security
diff --git a/eviction/README.md b/eviction/README.md
new file mode 100644
index 0000000..239a944
--- /dev/null
+++ b/eviction/README.md
@@ -0,0 +1,53 @@
+<!--
+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 Eviction Example
+
+This is a simple example that demonstrates eviction of entries from a region. This allows control
+over the system resources consumed by any given region.
+
+A region is a collection of entries which are tuples of key and value. Each entry requires memory
+for the key object, the value object, and some overhead. Regions that contain a large number of
+entries, entries of a large size, or both can consume enough system resources to impact overall
+system performance, even for other regions.
+
+A region can have eviction enabled to enforce an upper limit on either the total number of entries
+_or_ the total amount of memory consumed by the entries. The region will then enforce the specified
+limits on its in-memory resource consumption. When an operation would exceed those limits, the
+region will take an action to assure that the limits will not be exceeded after the operation
+completes. The region can either destroy one or more entries or overflow one or more entries to disk.
+
+This example assumes you have installed Java and Geode.
+
+## Steps
+
+1. From the `geode-examples/eviction` directory, build the example and
+   run unit tests.
+
+        $ ../gradlew build
+
+2. Next start a locator, start a server, and create a region.
+
+        $ gfsh run --file=scripts/start.gfsh
+
+3. Run the example to demonstrate eviction.
+
+        $ ../gradlew run
+
+4. Shut down the system.
+
+        $ gfsh run --file=scripts/stop.gfsh
diff --git a/eviction/scripts/start.gfsh b/eviction/scripts/start.gfsh
new file mode 100755
index 0000000..163aae9
--- /dev/null
+++ b/eviction/scripts/start.gfsh
@@ -0,0 +1,25 @@
+#
+# 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 --classpath=../build/classes/main
+start server --name=server2 --locators=127.0.0.1[10334] --server-port=0 --classpath=../build/classes/main
+list members
+
+create region --name=example-region --type=REPLICATE --skip-if-exists=true \
+    --eviction-entry-count=10 --eviction-action=local-destroy
+describe region --name=example-region
diff --git a/eviction/scripts/stop.gfsh b/eviction/scripts/stop.gfsh
new file mode 100755
index 0000000..15cd93c
--- /dev/null
+++ b/eviction/scripts/stop.gfsh
@@ -0,0 +1,19 @@
+#
+# 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/eviction/src/main/java/org/apache/geode_examples/eviction/Example.java b/eviction/src/main/java/org/apache/geode_examples/eviction/Example.java
new file mode 100644
index 0000000..b83dfbb
--- /dev/null
+++ b/eviction/src/main/java/org/apache/geode_examples/eviction/Example.java
@@ -0,0 +1,72 @@
+/*
+ * 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.eviction;
+
+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.ClientRegionFactory;
+import org.apache.geode.cache.client.ClientRegionShortcut;
+
+import java.util.Collection;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.Random;
+import java.util.Set;
+import java.util.stream.IntStream;
+
+public class Example {
+  public static final int ITERATIONS = 20;
+
+  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();
+
+    Example example = new Example();
+
+    // create a local region that matches the server region
+    ClientRegionFactory<Integer, String> clientRegionFactory =
+        cache.createClientRegionFactory(ClientRegionShortcut.PROXY);
+    Region<Integer, String> region = clientRegionFactory.create("example-region");
+
+    example.putEntries(region);
+    cache.close();
+  }
+
+  private Collection<Integer> generateIntegers() {
+    IntStream stream = new Random().ints(0, ITERATIONS);
+    Iterator<Integer> iterator = stream.iterator();
+    Set<Integer> integers = new LinkedHashSet<>();
+    while (iterator.hasNext() && integers.size() < ITERATIONS) {
+      integers.add(iterator.next());
+    }
+    return integers;
+  }
+
+  public void putEntries(Region<Integer, String> region) {
+    Collection<Integer> integers = generateIntegers();
+    Iterator<Integer> iterator = integers.iterator();
+
+    int created = 0;
+    while (iterator.hasNext()) {
+      Integer integer = iterator.next();
+      region.put(integer, integer.toString());
+      System.out.println("Added value for " + integer + "; the region now has "
+          + region.sizeOnServer() + " entries.");
+      ++created;
+    }
+  }
+}
diff --git a/settings.gradle b/settings.gradle
index e43f7f8..d0760e3 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -29,3 +29,4 @@ include 'writer'
 include 'listener'
 include 'async'
 include 'luceneSpatial'
+include 'eviction'

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