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

dbarnes 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 8e4b415  GEODE-2256: Colocation example (#88)
8e4b415 is described below

commit 8e4b41559bdcc15addacd91244c0885b8654e2bb
Author: Alberto Bustamante Reyes <[email protected]>
AuthorDate: Tue Oct 8 17:23:08 2019 +0200

    GEODE-2256: Colocation example (#88)
---
 README.md                                          |  3 +-
 colocation/README.md                               | 53 +++++++++++++++++
 colocation/scripts/start.gfsh                      | 30 ++++++++++
 colocation/scripts/stop.gfsh                       | 19 ++++++
 .../apache/geode_examples/colocation/Customer.java | 57 ++++++++++++++++++
 .../apache/geode_examples/colocation/Example.java  | 67 ++++++++++++++++++++++
 .../apache/geode_examples/colocation/Order.java    | 42 ++++++++++++++
 .../apache/geode_examples/colocation/OrderKey.java | 67 ++++++++++++++++++++++
 .../colocation/OrderPartitionResolver.java         | 32 +++++++++++
 settings.gradle                                    |  1 +
 10 files changed, 370 insertions(+), 1 deletion(-)

diff --git a/README.md b/README.md
index 502666c..00fade2 100644
--- a/README.md
+++ b/README.md
@@ -80,7 +80,8 @@ tutorial.
 *  [Eviction](eviction/README.md)
 *  [Expiration](expiration/README.md)
 *  [Overflow](overflow/README.md)
-*  Security
+*  [Security & SSL](clientSecurity/README.md)
+*  [Colocation](colocation/README.md)
 *  Off-heap
 
 ### Advanced
diff --git a/colocation/README.md b/colocation/README.md
new file mode 100644
index 0000000..f2be2c8
--- /dev/null
+++ b/colocation/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 colocation example
+
+This is a simple example that illustrates how to colocate the data of two 
regions.
+
+By default, Geode selects the data locations for a partitioned region 
independent of the data locations for any other partitioned region using a 
hashing policy on the key. This example shows how to modify this policy in 
order to store related data from two regions (`order`and `customer`) in the 
same member.
+
+`customer` region stores `Customer` objects using an `int` as key:
+ 
+ ```
+Region<Integer, Customer> customerRegion
+```
+
+`order` region stores `Order` object using an `OrderKey` object as key:
+
+```
+Region<OrderKey, Order> accountRegion
+```
+In order to store `Order` objects in the same member than their related 
customer info, a custom partition-resolved is needed: `OrderPartitionResolved`. 
When this partition resolver receives an `OrderKey` object, it returns the same 
key (the customer id) that was used to store the related customer. In this way, 
Geode applies the hashing policy over the same key for `Order`and `Customer` 
related objects and as a consequence, they are stored in the same member. 
+
+## Steps
+
+1. From the `geode-examples/colocation` directory, build the example.
+
+        $ ../gradlew build
+
+2. Next start a locator, start two servers, create `customer` region, and 
create `order`region colocated with `customer` region.
+
+        $ ../gradlew start
+
+3. Run the example to put entries into both regions.
+
+        $ ../gradlew run
+
+4. Shut down the system.
+
+        $ ../gradlew stop
diff --git a/colocation/scripts/start.gfsh b/colocation/scripts/start.gfsh
new file mode 100644
index 0000000..679432a
--- /dev/null
+++ b/colocation/scripts/start.gfsh
@@ -0,0 +1,30 @@
+#
+# 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/java/main
+start server --name=server2 --locators=127.0.0.1[10334] --server-port=0 
--classpath=../build/classes/java/main
+
+create region --name=customer --type=PARTITION
+create region --name=order --type=PARTITION --colocated-with=/customer \
+ 
--partition-resolver=org.apache.geode_examples.colocation.OrderPartitionResolver
+
+list members
+describe region --name=customer
+describe region --name=order
+
diff --git a/colocation/scripts/stop.gfsh b/colocation/scripts/stop.gfsh
new file mode 100644
index 0000000..15cd93c
--- /dev/null
+++ b/colocation/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/colocation/src/main/java/org/apache/geode_examples/colocation/Customer.java 
b/colocation/src/main/java/org/apache/geode_examples/colocation/Customer.java
new file mode 100644
index 0000000..2cb223d
--- /dev/null
+++ 
b/colocation/src/main/java/org/apache/geode_examples/colocation/Customer.java
@@ -0,0 +1,57 @@
+/*
+ * 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.colocation;
+
+import java.io.Serializable;
+
+public class Customer implements Serializable {
+
+  private static final long serialVersionUID = 95541179L;
+
+  private int id;
+  private String firstName;
+  private String lastName;
+  private String email;
+
+  public Customer() {}
+
+  public Customer(int id, String firstName, String lastName, String email) {
+    this.id = id;
+    this.firstName = firstName;
+    this.lastName = lastName;
+    this.email = email;
+  }
+
+  public int getId() {
+    return id;
+  }
+
+  public String getFirstName() {
+    return firstName;
+  }
+
+  public String getLastName() {
+    return lastName;
+  }
+
+  public String getEmail() {
+    return email;
+  }
+
+  public String toString() {
+    return "Customer [id=" + id + "firstName=" + firstName + ", lastName=" + 
lastName + ", email="
+        + email + "]";
+  }
+}
diff --git 
a/colocation/src/main/java/org/apache/geode_examples/colocation/Example.java 
b/colocation/src/main/java/org/apache/geode_examples/colocation/Example.java
new file mode 100644
index 0000000..8b2f83d
--- /dev/null
+++ b/colocation/src/main/java/org/apache/geode_examples/colocation/Example.java
@@ -0,0 +1,67 @@
+/*
+ * 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.colocation;
+
+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;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public class Example {
+  private int maximum;
+
+  public static void main(String[] args) {
+    ClientCache cache = new ClientCacheFactory().addPoolLocator("127.0.0.1", 
10334)
+        .set("log-level", "WARN").create();
+
+    Region<Integer, Customer> customerRegion =
+        cache.<Integer, 
Customer>createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
+            .create("customer");
+
+    Region<OrderKey, Order> orderRegion =
+        cache.<OrderKey, 
Order>createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
+            .create("order");
+
+    Map<Integer, Customer> customers = generateCustomers();
+
+    for (int i : customers.keySet()) {
+      Customer customer = customers.get(i);
+      Order order = new Order(i * 10, customer.getId());
+      customerRegion.put(customer.getId(), customer);
+      orderRegion.put(order.getKey(), order);
+    }
+    cache.close();
+  }
+
+  public static Map<Integer, Customer> generateCustomers() {
+    String firstNames[] =
+        {"Albert", "Bob", "Charles", "Daniel", "Ethan", "Frank", "Gregory", 
"Henrik"};
+    String lastNames[] =
+        {"Anthony", "Barkley", "Chen", "Dalembert", "English", "French", 
"Gobert", "Hakanson"};
+    String emails[] = new String[firstNames.length];
+    for (int i = 0; i < firstNames.length; i++) {
+      emails[i] = firstNames[i].toLowerCase() + "." + 
lastNames[i].toLowerCase() + "@example.com";
+    }
+    Map<Integer, Customer> customers = new HashMap<Integer, Customer>();
+
+    for (int i = 0; i < firstNames.length; i++) {
+      customers.put(i + 1, new Customer(i + 1, firstNames[i], lastNames[i], 
emails[i]));
+    }
+    return customers;
+  }
+}
diff --git 
a/colocation/src/main/java/org/apache/geode_examples/colocation/Order.java 
b/colocation/src/main/java/org/apache/geode_examples/colocation/Order.java
new file mode 100644
index 0000000..f462b28
--- /dev/null
+++ b/colocation/src/main/java/org/apache/geode_examples/colocation/Order.java
@@ -0,0 +1,42 @@
+/*
+ * 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.colocation;
+
+import java.io.Serializable;
+
+public class Order implements Serializable {
+
+  private static final long serialVersionUID = 41372560L;
+
+  private OrderKey key;
+
+  public Order() {}
+
+  public Order(int orderId, int customerId) {
+    this.key = new OrderKey(orderId, customerId);
+  }
+
+  public OrderKey getKey() {
+    return key;
+  }
+
+  public int getOrderId() {
+    return this.getKey().getOrderId();
+  }
+
+  public int getCustomerId() {
+    return this.getKey().getCustomerId();
+  }
+}
diff --git 
a/colocation/src/main/java/org/apache/geode_examples/colocation/OrderKey.java 
b/colocation/src/main/java/org/apache/geode_examples/colocation/OrderKey.java
new file mode 100644
index 0000000..6da77e6
--- /dev/null
+++ 
b/colocation/src/main/java/org/apache/geode_examples/colocation/OrderKey.java
@@ -0,0 +1,67 @@
+/*
+ * 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.colocation;
+
+import java.io.Serializable;
+
+public class OrderKey implements Serializable {
+  private static final long serialVersionUID = 60372860L;
+
+  private Integer customerId;
+  private Integer orderId;
+
+  public OrderKey() {}
+
+  public OrderKey(Integer orderId, Integer customerId) {
+    this.orderId = orderId;
+    this.customerId = customerId;
+  }
+
+  public Integer getCustomerId() {
+    return customerId;
+  }
+
+  public Integer getOrderId() {
+    return orderId;
+  }
+
+  @Override
+  public int hashCode() {
+    int result = orderId.hashCode();
+    result = 31 * result + customerId;
+    return result;
+  }
+
+  @Override
+  public boolean equals(Object obj) {
+    if (this == obj)
+      return true;
+    if (obj == null)
+      return false;
+    if (getClass() != obj.getClass())
+      return false;
+    OrderKey other = (OrderKey) obj;
+    if (!orderId.equals(other.getOrderId()))
+      return false;
+    if (!customerId.equals(other.getCustomerId()))
+      return false;
+    return true;
+  }
+
+  @Override
+  public String toString() {
+    return "OrderKey [orderId=" + orderId + ", customerId=" + customerId + "]";
+  }
+}
diff --git 
a/colocation/src/main/java/org/apache/geode_examples/colocation/OrderPartitionResolver.java
 
b/colocation/src/main/java/org/apache/geode_examples/colocation/OrderPartitionResolver.java
new file mode 100644
index 0000000..1e3f592
--- /dev/null
+++ 
b/colocation/src/main/java/org/apache/geode_examples/colocation/OrderPartitionResolver.java
@@ -0,0 +1,32 @@
+/*
+ * 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.colocation;
+
+import org.apache.geode.cache.EntryOperation;
+import org.apache.geode.cache.PartitionResolver;
+
+public class OrderPartitionResolver implements PartitionResolver {
+
+  @Override
+  public Object getRoutingObject(EntryOperation opDetails) {
+    OrderKey key = (OrderKey) opDetails.getKey();
+    return key.getCustomerId();
+  }
+
+  @Override
+  public String getName() {
+    return getClass().getName();
+  }
+}
diff --git a/settings.gradle b/settings.gradle
index d3f35d0..c9408ec 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -39,3 +39,4 @@ include 'transaction'
 include 'wan'
 include 'jdbc'
 include 'sessionState'
+include 'colocation'

Reply via email to