Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Enhanced partitioned example
Browse files Browse the repository at this point in the history
  • Loading branch information
David Turanski committed Oct 31, 2013
1 parent 67012a6 commit 552696f
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 138 deletions.
11 changes: 5 additions & 6 deletions basic/partitioned/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,10 @@ When the Client runs, it will create 100 Orders. These will be evenly distribute

# Custom Partitioning

The example also includes a custom PartitionResolver (CountryPartitionResolver) that allows the application to control how entries are co-located. In this example, the order shipping address is either in the US or the UK. In fact, all the odd number orders go to UK, and the evens go to US. The Spring configuration file server/cache-config is configured with 3 Spring profiles, "default", "UK", and "US". In the "US" profile, the region is configured with a fixed partition named "US" and likewise the "UK" profile contains the "UK" partition. Both profiles are configured with the CountryPartitionResolver which routes orders to corresponding partition. So try this:
The example also demonstrates the use of a custom PartitionResolver that allows the application to control how entries are co-located. In this example, the order shipping address is either in the US or the UK. To enable partitioning by country, an alternate implementation of the key class (OrderKey) that implements PartitionResolver replaces the normal key class.

In the server command windows, run the servers again, but each with a different active profile:
Run the the client again, adding an argument:

./gradlew -q run-partitioned -Pmain=Server -Pargs=US
./gradlew -q run-partitioned -Pmain=Server -Pargs=UK

Run the client again. This time, you will observe that all the odd numbered orders go to "UK" and the evens go to "US"
./gradlew -q run-partitioned -Pmain=Client -Pargs=partitionByCountry
This time, observe that the server console log indicates orders are partitioned by country.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.Date;
import java.util.Random;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.gemfire.examples.domain.Address;
Expand All @@ -27,22 +29,34 @@
import com.gemstone.gemfire.cache.Region;

public class Client {


private static Log log = LogFactory.getLog(Client.class);
private static boolean partitionByCountry;
@SuppressWarnings("unchecked")

public static void main(String args[]) throws IOException {

if (args.length >= 1 && args[0].equalsIgnoreCase("partitionByCountry")) {
log.debug("partitioning by country");
partitionByCountry = true;
}

@SuppressWarnings("resource")
ApplicationContext context = new ClassPathXmlApplicationContext("client/cache-config.xml");
Region<Long,Order> region = context.getBean(Region.class);
Region<OrderKey,Order> region = context.getBean(Region.class);

//Create some orders
Random rand = new Random(new Date().getTime());
for (long orderId = 1; orderId <= 100; orderId++) {
Address shipTo = new Address("Some Street","Some City",(orderId%2 == 0)?"US":"UK");
Address shipTo = new Address("Some Street","Some City",(orderId%3 == 0)?"UK":"US");
Order order = new Order(orderId, (new Long(rand.nextInt(100)+1)),shipTo);
region.put(orderId, order, order);
OrderKey orderKey = getOrderKey(orderId,shipTo.getCountry());
region.put(orderKey, order);
}

}

private static OrderKey getOrderKey(Long id, String countryCode) {
if (partitionByCountry) {
return new PartitionedOrderKey(id, countryCode);
}
return new OrderKey(id, countryCode);
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed 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.springframework.data.gemfire.examples;

import java.io.Serializable;

/**
* @author David Turanski
*
*/
@SuppressWarnings("serial")
public class OrderKey implements Serializable {
final String countryCode;
final Long id;

public OrderKey(Long id, String countryCode) {
this.id = id;
this.countryCode = countryCode;
}

public int hashCode() {
return id.hashCode();
}

public boolean equals(Object other) {
if (!(other instanceof OrderKey)) {
return false;
}
return ((OrderKey)other).id.equals(this.id);
}

public String toString() {
return id + ":" + countryCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2002-2013 the original author or authors.
*
* Licensed 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.springframework.data.gemfire.examples;

import com.gemstone.gemfire.cache.EntryOperation;
import com.gemstone.gemfire.cache.PartitionResolver;

/**
* @author David Turanski
*
*/
@SuppressWarnings({ "rawtypes", "serial" })
public class PartitionedOrderKey extends OrderKey implements PartitionResolver {


public PartitionedOrderKey(Long id, String countryCode) {
super(id,countryCode);
}
@Override
public void close() {
}

/* (non-Javadoc)
* @see com.gemstone.gemfire.cache.PartitionResolver#getName()
*/
@Override
public String getName() {
return this.getClass().getName();
}

/* (non-Javadoc)
* @see com.gemstone.gemfire.cache.PartitionResolver#getRoutingObject(com.gemstone.gemfire.cache.EntryOperation)
*/
@Override
public Object getRoutingObject(EntryOperation op) {
return this.countryCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,7 @@
public class Server {
private static Log log = LogFactory.getLog(Server.class);
public static void main(String args[]) throws IOException {
if (args.length >= 1) {
System.setProperty("spring.profiles.active", args[0]);
log.debug("setting Spring profile to " + args[0]);
}


ApplicationContext context = new ClassPathXmlApplicationContext("server/cache-config.xml");
CacheServer server = context.getBean(CacheServer.class);
System.out.println("server running on port " + server.getPort());
Expand Down
44 changes: 2 additions & 42 deletions basic/partitioned/src/main/resources/server/cache-config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,54 +19,14 @@

<bean id="portGenerator"
class="org.springframework.data.gemfire.examples.util.ServerPortGenerator" />
</beans>

<!-- Here we are using Spring environment profiles to provide alternate region configurations. -->

<beans profile="default">
<gfe:partitioned-region id="Order">
<gfe:cache-listener>
<bean
class="org.springframework.data.gemfire.examples.util.LoggingCacheListener" />
</gfe:cache-listener>
</gfe:partitioned-region>
</beans>

<!-- A partitioned region with fixed partition named 'UK'. -->
<beans profile="UK">
<gfe:partitioned-region id="Order">

<gfe:cache-listener>
<bean
class="org.springframework.data.gemfire.examples.util.LoggingCacheListener" />
</gfe:cache-listener>

<gfe:partition-resolver>
<bean
class="org.springframework.data.gemfire.examples.CountryPartitionResolver" />
</gfe:partition-resolver>

<gfe:fixed-partition partition-name="UK"/>

</gfe:partitioned-region>
</beans>

<!-- A partitioned region with fixed partition named 'US'. -->
<beans profile="US">
<gfe:partitioned-region id="Order">
<gfe:partitioned-region id="Order">

<gfe:cache-listener>
<bean
class="org.springframework.data.gemfire.examples.util.LoggingCacheListener" />
</gfe:cache-listener>

<gfe:partition-resolver>
<bean
class="org.springframework.data.gemfire.examples.CountryPartitionResolver" />
</gfe:partition-resolver>

<gfe:fixed-partition partition-name="US"/>


</gfe:partitioned-region>
</beans>
</beans>

0 comments on commit 552696f

Please sign in to comment.