This is an automated email from the ASF dual-hosted git repository. pivotalsarge 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 e28850d GEODE-4253: Add an example for expiration. (#44) e28850d is described below commit e28850ddd6cd6d77dfadac0719c8cab90d7900d1 Author: Michael "Sarge" Dodge <mdo...@pivotal.io> AuthorDate: Thu Feb 8 12:07:36 2018 -0800 GEODE-4253: Add an example for expiration. (#44) --- README.md | 2 +- expiration/README.md | 54 +++++++++++++ expiration/scripts/start.gfsh | 26 +++++++ expiration/scripts/stop.gfsh | 19 +++++ .../apache/geode_examples/expiration/Example.java | 88 ++++++++++++++++++++++ settings.gradle | 1 + 6 files changed, 189 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 952dc1f..e2d67f0 100644 --- a/README.md +++ b/README.md @@ -78,7 +78,7 @@ tutorial. * Continuous Querying * Transactions * [Eviction](eviction/README.md) -* Expiration +* [Expiration](expiration/README.md) * Overflow * Security * Off-heap diff --git a/expiration/README.md b/expiration/README.md new file mode 100644 index 0000000..74edb95 --- /dev/null +++ b/expiration/README.md @@ -0,0 +1,54 @@ +<!-- +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 Expiration Example + +This is a simple example that demonstrates expiration of entries from a region. This can be used to +prevent stale entries from lingering in a region. This also 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. When statistics-gathering is +enabled, the region maintains access and modification times for each entry. With entry expiration +configured, the region will enforce time-to-live limits on entries. When the time since access or +modification exceeds the configured duration, the region will take an action to expire the entry. +The region can either destroy expired entries in their entirety or invalidate expired entries by +removing their values. + +This example creates a region where the entries are destroyed after ten seconds without being +updated. The example first puts ten random integers into the region. Then the example loops, +printing the number of entries in the region, until the region is empty. + +This example assumes you have installed Java and Geode. + +## Steps + +1. From the `geode-examples/expiration` 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 expiration. + + $ ../gradlew run + +4. Shut down the system. + + $ gfsh run --file=scripts/stop.gfsh diff --git a/expiration/scripts/start.gfsh b/expiration/scripts/start.gfsh new file mode 100755 index 0000000..e06d1d2 --- /dev/null +++ b/expiration/scripts/start.gfsh @@ -0,0 +1,26 @@ +# +# 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 \ + --enable-statistics=true \ + --entry-time-to-live-expiration=10 --entry-time-to-live-expiration-action=local-destroy +describe region --name=example-region diff --git a/expiration/scripts/stop.gfsh b/expiration/scripts/stop.gfsh new file mode 100755 index 0000000..15cd93c --- /dev/null +++ b/expiration/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/expiration/src/main/java/org/apache/geode_examples/expiration/Example.java b/expiration/src/main/java/org/apache/geode_examples/expiration/Example.java new file mode 100644 index 0000000..c13758b --- /dev/null +++ b/expiration/src/main/java/org/apache/geode_examples/expiration/Example.java @@ -0,0 +1,88 @@ +/* + * 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.expiration; + +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.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Collection; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.LinkedHashSet; +import java.util.Map; +import java.util.Random; +import java.util.Set; +import java.util.stream.IntStream; + +public class Example { + private static final DateFormat ISO_8601_TIMESTAMP_FORMAT = + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS"); + + 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.insertValues(region, example.generateIntegers(10)); + example.monitorEntries(region); + + cache.close(); + } + + private Collection<Integer> generateIntegers(int upperLimit) { + IntStream stream = new Random().ints(0, upperLimit); + Iterator<Integer> iterator = stream.iterator(); + Set<Integer> integers = new LinkedHashSet<>(); + while (iterator.hasNext() && integers.size() < upperLimit) { + integers.add(iterator.next()); + } + return integers; + } + + void insertValues(Region<Integer, String> region, Collection<Integer> integers) { + Map values = new HashMap<Integer, String>(); + for (Integer i : integers) { + values.put(i, i.toString()); + } + region.putAll(values); + System.out.println( + ISO_8601_TIMESTAMP_FORMAT.format(new Date()) + "\tInserted " + values.size() + " values."); + } + + public void monitorEntries(Region<Integer, String> region) { + while (0 < region.sizeOnServer()) { + try { + Thread.sleep(1000); + System.out.println(ISO_8601_TIMESTAMP_FORMAT.format(new Date()) + "\tThe region now has " + + region.sizeOnServer() + " entries."); + } catch (InterruptedException ie) { + // NOP + } + } + } +} diff --git a/settings.gradle b/settings.gradle index 91968ba..dbec67e 100644 --- a/settings.gradle +++ b/settings.gradle @@ -31,3 +31,4 @@ include 'async' include 'luceneSpatial' include 'eviction' include 'serialization' +include 'expiration' -- To stop receiving notification emails like this one, please contact pivotalsa...@apache.org.