ZooKeeper ã使ã£ã¦ã¿ã(1)
Hadoopã®ãµãããã¸ã§ã¯ãã®ä¸ã¤ã«ZooKeeperã¨ããã®ããããåæ£ã·ã¹ãã ãä½ãããã®ãã«ãã£ã³ã°ãããã¯ã¨ãã¦é常ã«éè¦ã«ãªã£ã¦ããããªã®ã§ãã¡ãã£ã¨èª¿ã¹ã¦è¦ããã¨æããã¹ããªã¼ã ã³ã³ãã¥ã¼ãã£ã³ã°ã·ã¹ãã ã®S4ã§ã使ããã¦ããããã ãã©ã使ããã¦ããã®ãã¯ç¥ããªãã
ZooKeeperã¨ã¯
ZooKeeperã¯ãGoogleã®Chubbyã模ããã·ã¹ãã ãHadoopãGoogleã®GFSã¨MapReduceã模ããã®ã¨åãæ§å³ã ã
ããããChubbyã¯ãªããªã®ããã¨ããã¨ãããä¸è¨ã§èª¬æããã®ã¯é£ãããé常ã«å°è¦æ¨¡ãªãã¡ã¤ã«ã«ç¹åããåæ£ãã¡ã¤ã«ã·ã¹ãã ãã¨ãããã¨ã«ãªãã®ã ããããå®ã«ãã¾ãã¾ãªç®çã«ä½¿ããã¦ããããããDNS代ãããããã¯ãµã¼ãã¹ããµã¼ãã¹ã®æ»æ´»å¤å®ãªã©ãªã©ãZooKeeperã¯ä¸éããããã®ç®çã«ä½¿ããããã«ã§ãã¦ããããã ããã©ã®ãããæ©è½çã«å¿ å®ãªã³ãã¼ã«ãªã£ã¦ããã®ãã¯ãGoogleã®ä¸ã®äººã«ããããããªãã
Chubby ã¯èé害æ§ã®ããã«Paxosãç¨ããè¤è£½ãæ¡ã£ã¦ãããã¨ã§ãç¥ããã¦ããã5ã¤ã®ãµã¼ãã§ãã»ã«ããæ§æãããã®ãã¡ã®3å°ãçãã¦ããã°æ£å¸¸ã«å¦çãç¶è¡ã§ãããZooKeeperãåãæ§é ãæã¤ã
ãã¥ã¼ããªã¢ã«ã«ã¯ZooKeeperã使ã£ã¦è¤æ°ã®ããã»ã¹éã®ããªã¢åæããããã»ã¹éã§ã®çç£è æ¶è²»è ãã¥ã¼ã®ä½ãæ¹ãè¼ã£ã¦ããAmazonã®SQSã¿ãããªãã®ã¯ç°¡åã«ä½ããã®ããç¥ããªãã
ã¹ã¿ã³ãã¢ãã¼ã³ã¢ã¼ãã¨ãCLIã¯ã©ã¤ã¢ã³ã
ã¾ãã¯ãã¹ã¿ã³ãã¢ãã³ã¢ã¼ãã§åããã¦ã¿ãã1å°ã®ã¿ã§åããã¢ã¼ãã ãè¤è£½ã¯ã¨ãããªãã®ã§ãèé害æ§ã¯ãªãããã¢ããªã±ã¼ã·ã§ã³ã®éçºã«ã¯ãæ軽ã ã
ã¾ãã³ã³ãã£ã°ãã¡ã¤ã«ãã¤ããã
tickTime=2000 dataDir=/tmp/zookeeper clientPort=2181
conf/zoo.cfg ã¨ããååã«ãã¦ããã¨bin/zkServer.sh ããã®ã¾ã¾ä½¿ãã¦ä¾¿å©ã2181ã¯ããã©ã«ãã®portããã¡ãã
> mkdir /tmp/zookeeper
ã¨ãã¦ãããã¨ã
> bin/zkServer.sh start
ã¨ããã¨ãããããã°ãåºã¦èµ·åããã
ããã§ãCLIã®ã¯ã©ã¤ã¢ã³ããèµ·åããã
> bin/zkCli.sh
ããã©ã«ãã§ãã¼ã«ã«ãã¹ãã®2181ã«æ¥ç¶ã«è¡ãã®ã§å¼æ°ã¯ä¸è¦ãæ¥ç¶ã«æåããã¨ãããã³ãããåºãã
ãã㧠helpã¨æã¤ã¨ã³ãã³ãä¸è¦§ãåºããåºæ¬çã«ã¯é層çãªåå空éã«å¤ãç½®ãã¦ããã¨ãããã¨ãªã®ã§ãæä½ã¯ã·ã§ã«ã«ä¼¼ã¦ããã
lsãæåã¯ä½ãå ¥ã£ã¦ããªãã
[zk: localhost:2181(CONNECTED) 2] ls / [zookeeper]
/tmp ã¨ãããã¹ãä½ãå¤ããcontentãã¨è¨å®ãls ãã¦ã§ãã¦ãããã¨ã確èªã
[zk: localhost:2181(CONNECTED) 3] create /tmp content Created /tmp [zk: localhost:2181(CONNECTED) 4] ls / [tmp, zookeeper]
/tmpãgetãå¤ã ãã§ãªãä½ææ¥æãACLãªã©ã表示ããã¦ããã
[zk: localhost:2181(CONNECTED) 5] get /tmp content cZxid = 0x7 ctime = Thu Mar 03 01:28:17 JST 2011 mZxid = 0x7 mtime = Thu Mar 03 01:28:17 JST 2011 pZxid = 0x7 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 7 numChildren = 0
Java ã§ãã¼ããä½ã£ã¦ã¿ãã
Javaãã使ã£ã¦ã¿ãããæå®ããããã¼ããå¤ãä½æããããã°ã©ã ãZooKeeperã¯ã©ã¹ã®createã¡ã½ããã使ããACLã®è¨å®ãã¡ãã£ã¨ããã©ãã ããåºæ¬çã«ã¯ã¹ãã¬ã¼ããã©ã¯ã¼ãã ã
import java.util.ArrayList; import java.util.List; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.data.ACL; import static org.apache.zookeeper.ZooDefs.Ids.*; import static org.apache.zookeeper.ZooDefs.Perms.*; public class AddNode{ public void run() throws KeeperException, InterruptedException { } public static void main(String[] args) throws Exception{ if (args.length != 3) { System.err .println("USAGE: AddNode hostPort znode val"); System.exit(2); } ZooKeeper zk = new ZooKeeper(args[0], 3000, null); List<ACL> acls = new ArrayList<ACL>(); acls.add(new ACL(ALL, ANYONE_ID_UNSAFE)); zk.create(args[1], args[2].getBytes(), acls, CreateMode.PERSISTENT); } }
java addNode localhost:2181 /tmp/tmp3 hoge
ã¨ãããã
ã¡ãªã¿ã«ãã¡ãã®ããã°ã©ã ã¯çµäºããªããZooKeeper ã¨ã®ã³ãã¯ã·ã§ã³ã«ã¹ã¬ãããå¼µãä»ãã¦ããã®ã ããã
ã¾ã¨ã
ã¹ã¿ã³ãã¢ãã¼ã³ã¢ã¼ãã§ã®å®è¡ã¯ã¨ããããç°¡åãJavaã®ã¯ã©ã¤ã¢ã³ããç´æçã