Riak2を複数ノードで動かしてみる

みんなでやるRiak Advent Calendar 3日目。今のところは僕ひとり。
でもRiakは一人ぼっちじゃない。複数台で連携して初めて真価を発揮する。
そんな話。

## 複数のRiakをインストールする
開発環境では、一台のコンピュータにRiakノードを複数インストールし、連携を試すことが可能だ。
makeだとrel以下にバイナリが生成されるが、make devrelだとdev以下に5台分のバイナリが生成される。

```shell
[vagrant@localhost riak]$ pwd
/home/vagrant/riak

[vagrant@localhost riak]$ make devrel
（省略）

[vagrant@localhost riak]$ ls dev/
dev1  dev2  dev3  dev4  dev5
```
これらのノードを起動し、クラスタに所属させる。

```shell
[vagrant@localhost riak]$ find dev/dev*/bin/riak -exec {} start \;

[vagrant@localhost riak]$ find dev/dev*/bin/riak -exec {} ping \;
pong
pong
pong
pong
pong

[vagrant@localhost riak]$ find dev/dev[2-5]/bin/riak-admin -exec {} cluster join dev1@127.0.0.1 \;
Success: staged join request for 'dev2@127.0.0.1' to 'dev1@127.0.0.1'
Success: staged join request for 'dev3@127.0.0.1' to 'dev1@127.0.0.1'
Success: staged join request for 'dev4@127.0.0.1' to 'dev1@127.0.0.1'
Success: staged join request for 'dev5@127.0.0.1' to 'dev1@127.0.0.1'
```
クラスタにjoinさせた後、planとcommitを行うことで稼働が始まる。

```shell
[vagrant@localhost riak]$ find dev/dev[2-5]/bin/riak-admin -exec {} cluster join dev1@127.0.0.1 \;
Success: staged join request for 'dev2@127.0.0.1' to 'dev1@127.0.0.1'
Success: staged join request for 'dev3@127.0.0.1' to 'dev1@127.0.0.1'
Success: staged join request for 'dev4@127.0.0.1' to 'dev1@127.0.0.1'
Success: staged join request for 'dev5@127.0.0.1' to 'dev1@127.0.0.1'

[vagrant@localhost riak]$ dev/dev1/bin/riak-admin cluster plan
=============================== Staged Changes ================================
Action         Details(s)
-------------------------------------------------------------------------------
join           'dev2@127.0.0.1'
join           'dev3@127.0.0.1'
join           'dev4@127.0.0.1'
join           'dev5@127.0.0.1'
-------------------------------------------------------------------------------


NOTE: Applying these changes will result in 1 cluster transition

###############################################################################
                         After cluster transition 1/1
###############################################################################

================================= Membership ==================================
Status     Ring    Pending    Node
-------------------------------------------------------------------------------
valid     100.0%     20.3%    'dev1@127.0.0.1'
valid       0.0%     20.3%    'dev2@127.0.0.1'
valid       0.0%     20.3%    'dev3@127.0.0.1'
valid       0.0%     20.3%    'dev4@127.0.0.1'
valid       0.0%     18.8%    'dev5@127.0.0.1'
-------------------------------------------------------------------------------
Valid:5 / Leaving:0 / Exiting:0 / Joining:0 / Down:0

Transfers resulting from cluster changes: 51
  12 transfers from 'dev1@127.0.0.1' to 'dev5@127.0.0.1'
  13 transfers from 'dev1@127.0.0.1' to 'dev4@127.0.0.1'
  13 transfers from 'dev1@127.0.0.1' to 'dev3@127.0.0.1'
  13 transfers from 'dev1@127.0.0.1' to 'dev2@127.0.0.1'

[vagrant@localhost riak]$ dev/dev1/bin/riak-admin member-status
================================= Membership ==================================
Status     Ring    Pending    Node
-------------------------------------------------------------------------------
joining     0.0%      --      'dev2@127.0.0.1'
joining     0.0%      --      'dev3@127.0.0.1'
joining     0.0%      --      'dev4@127.0.0.1'
joining     0.0%      --      'dev5@127.0.0.1'
valid     100.0%      --      'dev1@127.0.0.1'
-------------------------------------------------------------------------------
Valid:1 / Leaving:0 / Exiting:0 / Joining:4 / Down:0

[vagrant@localhost riak]$ dev/dev1/bin/riak-admin cluster commit
Cluster changes committed

[vagrant@localhost riak]$ dev/dev1/bin/riak-admin member-status
================================= Membership ==================================
Status     Ring    Pending    Node
-------------------------------------------------------------------------------
valid      87.5%     20.3%    'dev1@127.0.0.1'
valid       3.1%     20.3%    'dev2@127.0.0.1'
valid       3.1%     20.3%    'dev3@127.0.0.1'
valid       3.1%     20.3%    'dev4@127.0.0.1'
valid       3.1%     18.8%    'dev5@127.0.0.1'
-------------------------------------------------------------------------------
Valid:5 / Leaving:0 / Exiting:0 / Joining:0 / Down:0
```
ここから分散が始まり、Ringの値が時間とともに他ノードに移ることがわかる。

```shell
[vagrant@localhost riak]$ dev/dev1/bin/riak-admin member-status
================================= Membership ==================================
Status     Ring    Pending    Node
-------------------------------------------------------------------------------
valid      75.0%     20.3%    'dev1@127.0.0.1'
valid       6.3%     20.3%    'dev2@127.0.0.1'
valid       6.3%     20.3%    'dev3@127.0.0.1'
valid       6.3%     20.3%    'dev4@127.0.0.1'
valid       6.3%     18.8%    'dev5@127.0.0.1'
-------------------------------------------------------------------------------
Valid:5 / Leaving:0 / Exiting:0 / Joining:0 / Down:0

[vagrant@localhost riak]$ dev/dev1/bin/riak-admin member-status
================================= Membership ==================================
Status     Ring    Pending    Node
-------------------------------------------------------------------------------
valid      50.0%     20.3%    'dev1@127.0.0.1'
valid      12.5%     20.3%    'dev2@127.0.0.1'
valid      12.5%     20.3%    'dev3@127.0.0.1'
valid      12.5%     20.3%    'dev4@127.0.0.1'
valid      12.5%     18.8%    'dev5@127.0.0.1'
-------------------------------------------------------------------------------
Valid:5 / Leaving:0 / Exiting:0 / Joining:0 / Down:0

[vagrant@localhost riak]$ dev/dev1/bin/riak-admin member-status
================================= Membership ==================================
Status     Ring    Pending    Node
-------------------------------------------------------------------------------
valid      20.3%      --      'dev1@127.0.0.1'
valid      20.3%      --      'dev2@127.0.0.1'
valid      20.3%      --      'dev3@127.0.0.1'
valid      20.3%      --      'dev4@127.0.0.1'
valid      18.8%      --      'dev5@127.0.0.1'
-------------------------------------------------------------------------------
Valid:5 / Leaving:0 / Exiting:0 / Joining:0 / Down:0
```
これで完了だ。
dev5だけ18.8%と他ノードと異なる値になっているが、これはデフォルト設定のv-node数が実際のノード数で割り切れないからだ。多分。

## 本当に分散しているのか
```shell
[vagrant@localhost riak]$ curl -XPUT -d '{"myname":"saisa"}'  http://localhost:10018/buckets/mybucket/keys/name -v
* About to connect() to localhost port 10018 (#0)
*   Trying ::1... 接続を拒否されました
*   Trying 127.0.0.1... connected
* Connected to localhost (127.0.0.1) port 10018 (#0)
> PUT /buckets/mybucket/keys/name HTTP/1.1
> User-Agent: curl/7.19.7 (x86_64-redhat-linux-gnu) libcurl/7.19.7 NSS/3.14.0.0 zlib/1.2.3 libidn/1.18 libssh2/1.4.2
> Host: localhost:10018
> Accept: */*
> Content-Length: 18
> Content-Type: application/x-www-form-urlencoded
>
< HTTP/1.1 204 No Content
< Vary: Accept-Encoding
< Server: MochiWeb/1.1 WebMachine/1.10.5 (jokes are better explained)
< Date: Tue, 03 Dec 2013 14:55:49 GMT
< Content-Type: application/x-www-form-urlencoded
< Content-Length: 0
<
* Connection #0 to host localhost left intact
* Closing connection #0

[vagrant@localhost riak]$ curl http://localhost:10018/buckets/mybucket/keys/name
{"myname":"saisa"}
[vagrant@localhost riak]$ curl http://localhost:10028/buckets/mybucket/keys/name
{"myname":"saisa"}
```
と、こんなかんじで、dev1に書き込んだ値を2からも読み取ることができる。

## 障害時の挙動
Angular.JSのイベントに行ってたら時間が無くなったのでまた今度な！！
日付変更まであと2分！