Data Affinity
Data affinity ensures that related entries exist on the same member. If related data is on the same member, operations can be executed without the cost of extra network calls and extra wire data. This feature is provided by using the same partition keys for related data.
PartitionAware
Co-location of related data and computation
Hazelcast has a standard way of finding out which member owns/manages each key object.
The following operations are routed to the same member, since all them are operating based on the same key "key1"
.
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
Map mapA = hazelcastInstance.getMap( "mapA" );
Map mapB = hazelcastInstance.getMap( "mapB" );
Map mapC = hazelcastInstance.getMap( "mapC" );
// since map names are different, operation will be manipulating
// different entries, but the operation will take place on the
// same member since the keys ("key1") are the same
mapA.put( "key1", value );
mapB.get( "key1" );
mapC.remove( "key1" );
// lock operation will still execute on the same member
// of the cluster since the key ("key1") is same
hazelcastInstance.getLock( "key1" ).lock();
// distributed execution will execute the 'runnable' on the
// same member since "key1" is passed as the key.
hazelcastInstance.getExecutorService().executeOnKeyOwner( runnable, "key1" );
When the keys are the same, entries are stored on the same member.
But we sometimes want to have related entries stored on the same member, such as a customer and his/her order entries.
We would have a customers map with customerId as the key and an orders map with orderId as the key.
Since customerId and orderId are different keys, a customer and
his/her orders may fall into different members in your cluster. So how can we have them stored on the same member?
We create an affinity between customer and orders. If we make them part of the same partition then
these entries will be co-located. We achieve this by making orderKey
s PartitionAware
.
final class OrderKey implements PartitionAware, Serializable {
private final long orderId;
private final long customerId;
OrderKey(long orderId, long customerId) {
this.orderId = orderId;
this.customerId = customerId;
}
@Override
public Object getPartitionKey() {
return customerId;
}
@Override
public String toString() {
return "OrderKey{"
+ "orderId=" + orderId
+ ", customerId=" + customerId
+ '}';
Notice that OrderKey implements PartitionAware
and that getPartitionKey()
returns the customerId
.
These make sure that the Customer
entry and its Order
s are stored on the same member.
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
Map mapCustomers = hazelcastInstance.getMap( "customers" );
Map mapOrders = hazelcastInstance.getMap( "orders" );
// create the customer entry with customer id = 1
mapCustomers.put( 1, customer );
// now create the orders for this customer
mapOrders.put( new OrderKey( 21, 1 ), order );
mapOrders.put( new OrderKey( 22, 1 ), order );
mapOrders.put( new OrderKey( 23, 1 ), order );
Assume that you have a customers map where customerId
is the key and the customer object is the value.
You want to remove one of the customer orders and return the number of remaining orders.
Here is how you would normally do it.
public static int removeOrder( long customerId, long orderId ) throws Exception {
IMap<Long, Customer> mapCustomers = instance.getMap( "customers" );
IMap mapOrders = hazelcastInstance.getMap( "orders" );
mapCustomers.lock( customerId );
mapOrders.remove( new OrderKey(orderId, customerId) );
Set orders = orderMap.keySet(Predicates.equal( "customerId", customerId ));
mapCustomers.unlock( customerId );
return orders.size();
}
There are couple of things you should consider.
-
There are four distributed operations there: lock, remove, keySet, unlock. Can you reduce the number of distributed operations?
-
The customer object may not be that big, but can you not have to pass that object through the wire? Think about a scenario where you set order count to the customer object for fast access, so you should do a get and a put, and as a result, the customer object is passed through the wire twice.
Instead, why not move the computation over to the member (JVM) where your customer data resides. Here is how you can do this with distributed executor service.
-
Send a
PartitionAware
Callable
task. -
Callable
does the deletion of the order right there and returns with the remaining order count. -
Upon completion of the
Callable
task, return the result (remaining order count). You do not have to wait until the task is completed; since distributed executions are asynchronous, you can do other things in the meantime.
Here is an example code.
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
public int removeOrder(long customerId, long orderId) throws Exception {
IExecutorService executorService = hazelcastInstance.getExecutorService("ExecutorService");
OrderDeletionTask task = new OrderDeletionTask(customerId, orderId);
Future<Integer> future = executorService.submit(task);
int remainingOrders = future.get();
return remainingOrders;
}
public static class OrderDeletionTask
implements Callable<Integer>, PartitionAware, Serializable, HazelcastInstanceAware {
private long orderId;
private long customerId;
private HazelcastInstance hazelcastInstance;
public OrderDeletionTask() {
}
public OrderDeletionTask(long customerId, long orderId) {
this.customerId = customerId;
this.orderId = orderId;
}
@Override
public Integer call() {
IMap<Long, Customer> customerMap = hazelcastInstance.getMap("customers");
IMap<OrderKey, Order> orderMap = hazelcastInstance.getMap("orders");
customerMap.lock(customerId);
Predicate predicate = Predicates.equal("customerId", customerId);
Set<OrderKey> orderKeys = orderMap.localKeySet(predicate);
int orderCount = orderKeys.size();
for (OrderKey key : orderKeys) {
if (key.orderId == orderId) {
orderCount--;
orderMap.delete(key);
}
}
customerMap.unlock(customerId);
return orderCount;
}
@Override
public Object getPartitionKey() {
return customerId;
}
@Override
public void setHazelcastInstance(HazelcastInstance hazelcastInstance) {
this.hazelcastInstance = hazelcastInstance;
}
}
The following are the benefits of doing the same operation with distributed ExecutorService
based on the key:
-
only one distributed execution (
executorService.submit(task)
), instead of four -
less data is sent over the wire
-
less lock duration, i.e., higher concurrency, for the
Customer
entry since lock/update/unlock cycle is done locally (local to the customer data)
Partitioning Strategies
Another way of storing the related data on the same location is using/implementing
the class PartitioningStrategy
. Normally (if no partitioning strategy is defined),
Hazelcast finds the partition of a key first by converting the object to binary and then by hashing this binary.
If a partitioning strategy is defined, Hazelcast injects the key to the strategy and
the strategy returns an object out of which the partition is calculated by hashing it.
Hazelcast offers the following out-of-the-box partitioning strategies:
-
DefaultPartitioningStrategy
: Default strategy. It checks whether the key implementsPartitionAware
. If it implements, the object is converted to binary and then hashed, to find the partition of the key. -
StringPartitioningStrategy
: Works only for string keys. It uses the string after@
character as the partition ID. For example, if you have two keysordergroup1@region1
andcustomergroup1@region1
, bothordergroup1
andcustomergroup1
fall into the partition whereregion1
is located. -
StringAndPartitionAwarePartitioningStrategy
: Works as the combination of the above two strategies. If the key implementsPartitionAware
, it works like theDefaultPartitioningStrategy
. If it is a string key, it works like theStringPartitioningStrategy
.
You can configure the partitioning strategies:
-
for each map, or
-
globally (applied to all of the data structures in your cluster).
The per map and global partitioning strategies are supported on the member side. Hazelcast Java clients only support the global strategy. |
Per Map Partitioning Strategy Configuration
The following shows example configurations. For the declarative configurations (XML, YAML), you use the partition-strategy
element.
For the programmatic approach, you use the setPartitioningStrategyClass()
method.
<hazelcast>
...
<map name="myMap">
<partition-strategy>
com.hazelcast.partition.strategy.StringAndPartitionAwarePartitioningStrategy
///OR
///YourCustomPartitioningStrategyClass (1)
</partition-strategy>
</map>
...
</hazelcast>
hazelcast:
map:
myMap:
partition-strategy: com.hazelcast.partition.strategy.StringAndPartitionAwarePartitioningStrategy
# OR
# partition-strategy: YourCustomPartitioningStrategyClass (1)
Config config = new Config();
MapConfig mapConfig = config.getMapConfig("myMap");
PartitioningStrategyConfig psConfig = mapConfig.getPartitioningStrategyConfig();
psConfig.setPartitioningStrategyClass( "StringAndPartitionAwarePartitioningStrategy" );
// OR
psConfig.setPartitioningStrategy(YourCustomPartitioningStrategy); (1)
...
1 | You can define your own partition strategy by implementing the class PartitioningStrategy . To enable your implementation, add the full class name to your Hazelcast configuration using either
the declarative or programmatic approach, as exemplified above. |
All the cluster members must have the same partitioning strategy configurations. |
Global Partitioning Strategy Configuration
You can also set a global strategy which is applied to all the data structures in your cluster.
This can be done by setting the hazelcast.partitioning.strategy.class
property.
The following shows example configurations.
<hazelcast>
...
<properties>
<property name="hazelcast.partitioning.strategy.class">
com.hazelcast.partition.strategy.StringAndPartitionAwarePartitioningStrategy
</property>
</properties>
...
</hazelcast>
hazelcast:
properties:
hazelcast.partitioning.strategy.class: com.hazelcast.partition.strategy.StringAndPartitionAwarePartitioningStrategy
Config config = new Config();
config.setProperty( "hazelcast.partitioning.strategy.class", "com.hazelcast.partition.strategy.StringAndPartitionAwarePartitioningStrategy" );
You can specify the aforementioned out-of-the-box strategies or your custom partitioning strategy.
You can also use other system property configuring options as explained in the Configuring with System Properties section.
Attribute Based Partitioning Strategy
Attribute based partitioning (AttributePartitioningStrategy
) is a strategy such that the map entries having the same partition keys live in the same cluster member.
It can be configured per map only, and cannot be used globally.
To configure this strategy for a map, you can provide the partitioning-attributes
configuration property under the map
element. If this property is provided, it will automatically supersede partitioning-strategy
(if any one of them is configured as explained in the previous sections) and the strategy will be set to AttributePartitioningStrategy
. Note that, when provided, partitioning-attributes
should contain at least one attribute name.
Dynamic configuration is supported, which means that you can add or change the configuration of this strategy without restarting the cluster.
It is important to note that AttributePartitioningStrategy
is ONLY supported on the member-side.
Overall, AttributePartitioningStrategy
is a powerful tool for partitioning data across a cluster in Hazelcast. By following the guidelines above, you can easily configure it for your specific use case.
Example Usage
Here is an example flow that shows how attribute based partitioning strategy works.
This example assumes that you have a map named myMap
of type IMap<Person, Long>
where Person
has { public Long id; public String name; public Long orgId; }
.
-
Start a cluster by providing a map configuration that includes the attributes
id
andname
(or you can add this configuration while a cluster is running). -
When data is added to the map using the
put
method, the strategy configured for this map is used to determine the partition ID of the new entry. Specifically, theAttributePartitioningStrategy
extracts theid
andname
attributes from the given object, and creates anObject[]
array out of them. This partition key is then used by the member logic to calculate the partition ID of the new entry.To give an example, assume that you have a key/value pair
Person(1, "John", 3), 1
, whereid
=1
,name
=John
, andorgId
=3
.Add this map entry using
myMap.put(new Person(1, "John", 3), 1);
. The key of this entry (new Person(1, "John", 3)
) will be passed to the configured strategy. -
The
AttributePartitioningStrategy
that is configured for this map takes the key and only extracts theid
andname
attributes from it (1
andJohn
), and creates the {1, "John"} partition key. -
The member logic then calculates the partition ID out of the
{1, "John"}
partition key, and puts the entry to the cluster member which has the calculated partition ID. -
A new entry having the same partition key will be put to the same cluster member.
When you want to query for an entry with the key new Person(1, "John", 3)
, the exact same turn of events described above occurs, except the operation will be get
instead of put
.