-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathstep-8.html
69 lines (67 loc) · 3.41 KB
/
step-8.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
---
layout: tutorial-extended
title: The Weather App
---
{% assign tutorial_github_url = "https://github.com/infinispan/infinispan-embedded-tutorial" %}
{% assign @tutorial_steps = "The Weather Application, Initializing the CacheManager, Putting stuff in the cache, Making entries expire, Configuring the default cache, Clustering the application, Listen to changes in the cluster, Listen to changes in the cache, Grouping entries together, Temperature averages with streams, Declarative configuration" | split: ", " %}
{% assign tutorial_step = 8 %}
<div class="grid-wrapper">
<div class="width-3-12 width-12-12-m">
{% include embedded-tutorial-menu.html tutorial_steps=tutorial_steps tutorial_step=tutorial_step %}
</div>
<div class="width-9-12 width-12-12-m">
<h4>{{page.title}}</h4>
<p>
In Infinispan the distribution of entries in the cluster is done in accordance with a consistent hashing algorithm. This algorithm uses the entry's key to compute a hash, and uses that hash to determine which node will be the primary owner, and which other nodes will act as backups. It is possible to control the distribution. In particular it might be useful, for performance reasons, to co-locate related entries on the same node using some grouping algorithm. In our weather application, for example, we may want to group weather locations per-country. For this we need to create a <i>Grouper</i> class which can compute a group name based on a key. Here's our example:
</p>
<pre>
<code class="java">
public static class LocationGrouper implements Grouper<String> {
@Override
public String computeGroup(String key, String group) {
return key.split(",")[1].trim();
}
@Override
public Class<String> getKeyType() {
return String.class;
}
}
</code>
</pre>
<p>
The above code is quite simple: it merely splits the key using the comma as a delimiter and uses the second half (the "country") as the group. The hashing algorithm will be using the group, instead of the key, to compute the entry's hash. We need to specify the grouper we want to use in the configuration:
</p>
<pre>
<code class="java">
config.clustering().hash().groups().enabled().addGrouper(new LocationWeather.LocationGrouper());
</code>
</pre>
<p>
It's time to run the application:
</p>
<pre>
<code class="nohighlight">
git checkout -f step-8
mvn clean package exec:exec # on terminal 1
mvn exec:exec # on terminal 2
</code>
</pre>
<p>
If you examine the output of each node on this run, you will see that the event logs will be "paired" for all entries belonging to the same country. In my case, the "Romania" group got "hashed" to the second node:
</p>
<details>
<summary>Coordinator Node Output</summary>
<div>
<pre>
<code class="nohighlight">
-- Entry for Bucharest, Romania modified by another node in the cluster
-- Entry for Cluj-Napoca, Romania modified by another node in the cluster
</code>
</pre>
</div>
</details>
<p>
In the next step we will see how to control the serialized form of the entries as they are transferred between nodes.
</p>
</div>
</div>