Hazelcast 3.3ã®ãªãªã¼ã¹ãããReplicated Mapã¨ãããã¼ã¿æ§é ã追å ããã¾ããã
What's New in Hazelcast 3.3 - New Features
http://docs.hazelcast.org/docs/3.3/manual/html-single/hazelcast-documentation.html#new-features
Replicated Map - BETA
http://docs.hazelcast.org/docs/3.3/manual/html-single/hazelcast-documentation.html#replicated-map-beta
ã¾ã ãã¼ã¿ççãªæ±ãï¼
Hazelcastã¨ããã°ãMapãListãQueueã¨ãã£ãå¤æ§ãªãã¼ã¿æ§é ãæä¾ãã¦ãã¾ããããã¹ã¦åæ£ï¼Distributedï¼ãªãã®ã§ãåNodeã«åæ£ãã¦ãã¼ã¿ãæã¡ã¾ãããã£ã¦ãããã¯ã¢ããæ°ä»¥ä¸ã®Nodeãåæã«ãã¦ã³ããã¨ããã¼ã¿ã失ããã¾ãã
ã§ãä»åã®Replicated Mapã¯ãã©ã®Nodeãåããã¼ã¿ãæã¤ãããªMapã«ãªãã¾ãããã£ã¦ã1çªã¡ã¢ãªã®å°ãªãNodeã«ä¿æå¯è½ãªãã¼ã¿éãåãããããå½¢ã«ãªãã§ããããã
ãã®Replicated Mapã§ãããä»ã®ã¤ã³ã¡ã¢ãªã»ãã¼ã¿ã°ãªããã®ã¬ããªã±ã¼ã·ã§ã³ãè¡ããã®ã¨æ¯ã¹ã¦ãã¡ãã£ã¨æ§æ ¼ãç°ãªãããã§ãã
Hazelcastã®Replicated Mapã®ç¹å¾´ã¯ã
- å¼±ãä¸è²«æ§
- å ¨Nodeãåããã¼ã¿ãæã¡ãã¡ã¢ãªæ¶è²»éã¯å¤§ãããèªã¿åãã¯é«é
- java.util.Mapã¤ã³ã¿ã¼ãã§ã¼ã¹ãå®è£ ãã¦ããããConcurrentMapã§ã¯ãªãï¼ã¢ãããã¯ãªèªã¿æ¸ãã¯ã§ããªãï¼
- æ¸ãè¾¼ã¿é ã¯ä¿è¨¼ãã¦ããªãã2ã¤ã®Nodeããåæã«æ¸ãè¾¼ã¾ããå ´åã¯ãVector Clockã¢ã«ã´ãªãºã ã«ããã²ã¨ã¤ã®å¤ã使ç¨ããããã«æ±ºå®ãã
- Map#clearã¯éåæã«å®è¡ããã
çµæãHTMLãååãã¼ã¿ã®ãããªããã¾ãå¤ãããªããã¼ã¿ãæ ¼ç´ããããã¨ãæ³å®ãã¦ãããããªã
使ã£ã¦ã¿ã
ã¾ããæ¦è¦ã¯ãããããã«ãã¦ãã¾ãã¯ä½¿ã£ã¦ã¿ã¾ãããã
Mavenä¾åé¢ä¿ã¯ããã®ããã«å®ç¾©ã
<dependency> <groupId>com.hazelcast</groupId> <artifactId>hazelcast</artifactId> <version>3.3.1</version> </dependency>
ããåç´ãªãµã³ãã«ãæ¸ãã¦ã¿ã¾ãã
src/main/java/org/littlewings/hazelcast/replicatedmap/ReplicatedMapExample.java
package org.littlewings.hazelcast.replicatedmap; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.util.List; import java.util.Map; import java.util.stream.Collectors; import java.util.stream.IntStream; import com.hazelcast.config.Config; import com.hazelcast.core.Hazelcast; import com.hazelcast.core.HazelcastInstance; public class ReplicatedMapExample { public static void main(String[] args) { Config config = new Config(); HazelcastInstance hazelcast = Hazelcast.newHazelcastInstance(config); Map<String, String> map = hazelcast.getReplicatedMap("default"); // Map<String, String> map = hazelcast.getMap("default"); int entrySize = 20; IntStream .rangeClosed(1, entrySize) .forEach(i -> map.put("key" + i, "value" + i)); System.out.printf("[%s] Hazelcast Node, startup, putted [%d]entries.%n", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), entrySize); System.console().readLine("Enter, shutdown..."); IntStream .rangeClosed(1, entrySize) .forEach(i -> { String key = "key" + i; String value = map.get(key); System.out.printf("Key = [%s], Value = [%s]%n", key, value); }); hazelcast.getLifecycleService(); Hazelcast.shutdownAll(); } }
HazelcastInstance#getReplicatedMapã§ãReplicated Mapãåå¾ãããã¨ãã§ãã¾ãã
Map<String, String> map = hazelcast.getReplicatedMap("default");
ä»åã¯Mapã¤ã³ã¿ã¼ãã§ã¼ã¹ã§åãã¦ãã¾ãããcom.hazelcast.core.ReplicatedMapã¤ã³ã¿ã¼ãã§ã¼ã¹ã§åãã¦ãOKã§ãã
ãã®ããã°ã©ã ã20åã¨ã³ããªãæ¾ãè¾¼ãã§ãEnterãæãããã¾ã§å¾ æ©ãã¾ãã
int entrySize = 20; IntStream .rangeClosed(1, entrySize) .forEach(i -> map.put("key" + i, "value" + i)); System.out.printf("[%s] Hazelcast Node, startup, putted [%d]entries.%n", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")), entrySize); System.console().readLine("Enter, shutdown...");
ãã®å¾ããã¼ã¿ããã³ããã¦ã·ã£ãããã¦ã³ãã¾ãã
IntStream .rangeClosed(1, entrySize) .forEach(i -> { String key = "key" + i; String value = map.get(key); System.out.printf("Key = [%s], Value = [%s]%n", key, value); }); hazelcast.getLifecycleService(); Hazelcast.shutdownAll();
ããã使ã£ã¦ãåä¸ã®Nodeããã¹ã¦ã®ãã¼ã¿ãæã£ã¦ãããã¨ã確èªãã¦ã¿ã¾ãããã
ãã®ããã°ã©ã ã4ã¤èµ·åãã¦ãHazelcast 4Nodeã®ã¯ã©ã¹ã¿ãæ§æãã¦ã¿ã¾ãã
# Node1 $ mvn compile exec:java -Dexec.mainClass=org.littlewings.hazelcast.replicatedmap.ReplicatedMapExample # Node2 $ mvn compile exec:java -Dexec.mainClass=org.littlewings.hazelcast.replicatedmap.ReplicatedMapExample # Node3 $ mvn compile exec:java -Dexec.mainClass=org.littlewings.hazelcast.replicatedmap.ReplicatedMapExample # Node4 $ mvn compile exec:java -Dexec.mainClass=org.littlewings.hazelcast.replicatedmap.ReplicatedMapExample
èµ·åããã¨ãåNodeããããªã¨ããã§æ¢ã¾ãã¾ãã
[2014-10-12 16:21:35] Hazelcast Node, startup, putted [20]entries. Enter, shutdown...
å ¨Nodeãèµ·åãçµããã¨ãæåã«èµ·åããNodeã®ã³ã³ã½ã¼ã«ã«ã¯ãããªè¡¨ç¤ºãåºã¦ããã¨æãã¾ãã
Members [4] { Member [192.168.129.129]:5701 this Member [192.168.129.129]:5702 Member [192.168.129.129]:5703 Member [192.168.129.129]:5704 }
ãããããåNodeãè½ã¨ãã¦ã²ã¨ã¤ã ãæ®ãã¾ãã
ä½ã£ãããã°ã©ã ã®é¢ä¿ä¸ãEnterãæ¼ãã¦ãã£ã¦ãã·ã£ãããã¦ã³ãã¦ããã®ã§ãããããã ã¨ã¯ã©ã¹ã¿åæ§æã®ç¶äºãã§ããã®ã§ãããã¯ä¸æ°ã«killãã¾ãã
$ kill [Node2ã®PID] [Node3ã®PID] [Node4ã®PID]
Node1ã¯ãããããç¶æ ã«ãªãã¾ãã
Members [1] { Member [192.168.129.129]:5701 this }
ãã¨ã¯ãEnterãæ¼ãã¦ã·ã£ãããã¦ã³ã
Key = [key1], Value = [value1] Key = [key2], Value = [value2] Key = [key3], Value = [value3] Key = [key4], Value = [value4] Key = [key5], Value = [value5] Key = [key6], Value = [value6] Key = [key7], Value = [value7] Key = [key8], Value = [value8] Key = [key9], Value = [value9] Key = [key10], Value = [value10] Key = [key11], Value = [value11] Key = [key12], Value = [value12] Key = [key13], Value = [value13] Key = [key14], Value = [value14] Key = [key15], Value = [value15] Key = [key16], Value = [value16] Key = [key17], Value = [value17] Key = [key18], Value = [value18] Key = [key19], Value = [value19] Key = [key20], Value = [value20]
ãã³ãããå¤ã«ããã¹ã¦ã®ã¨ã³ããªãæ ¼ç´ããã¦ãããã¨ã確èªã§ãã¾ãã
次ã«ãHazelcastInstance#getReplicatedMapãHazelcastInstance#getMapã«æ¸ãæããReplicated MapããDistributed Mapã«æ¿ãã¦åããã¨ããã¦ã¿ã¾ãã
// Map<String, String> map = hazelcast.getReplicatedMap("default"); Map<String, String> map = hazelcast.getMap("default");
åNodeãèµ·åãã¦ãåãããã«killãã¾ãã
$ kill [Node2ã®PID] [Node3ã®PID] [Node4ã®PID]
æ®ã£ãNodeã§ãEnterãæ¼ãã¾ãã
Key = [key1], Value = [null] Key = [key2], Value = [null] Key = [key3], Value = [value3] Key = [key4], Value = [null] Key = [key5], Value = [null] Key = [key6], Value = [value6] Key = [key7], Value = [value7] Key = [key8], Value = [value8] Key = [key9], Value = [value9] Key = [key10], Value = [null] Key = [key11], Value = [value11] Key = [key12], Value = [value12] Key = [key13], Value = [null] Key = [key14], Value = [null] Key = [key15], Value = [null] Key = [key16], Value = [null] Key = [key17], Value = [value17] Key = [key18], Value = [null] Key = [key19], Value = [value19] Key = [key20], Value = [null]
ãã¡ãã§ã¯ãã¨ã³ããªãæ¬ ãã¦ãã¾ããã
Replicated Mapã使ãã¨ãåNodeãå ¨ãã¼ã¿ãæã¤ãã¨ã確èªã§ããã¨æãã¾ãã次ã¯ãè¨å®ãªã©ã«ã¤ãã¦è¦ã¦ãããã¨æãã¾ãã
ç¶ãã¯ããã¡ãã
Hazelcastã®Replicated Mapã試ãã¦ã¿ã - ãã®2
http://d.hatena.ne.jp/Kazuhira/20141012/1413105032
ä½æããã³ã¼ãã¯ããã¡ãã«ç½®ãã¦ãã¾ãã
https://github.com/kazuhira-r/hazelcast-examples/tree/master/hazelcast-replicated-map