ferigis/mykv

语言: Erlang

git: https://github.com/ferigis/mykv

用Erlang / OTP编写的分布式键/值存储
Distributed key/value store written in Erlang/OTP
README.md (中文)

南瓜

作者:Felipe Ripoll。 (Ferigis@gmail.com)。

分布式键/值存储。

注意:mykv仍处于开发阶段。目前,节点数必须是不可移动的。还有更多东西需要添加,如切换,分布式测试,一致哈希,添加法定数据......

关于

Mykv是一个用Erlang / OTP编写的分布式键/值存储。它使用Mnesia作为后端。此应用程序是为学习目的而构建的,不适合在生产应用程序中使用。

建立

Mykv使用Rebar3。

$ git clone https://github.com/ferigis/mykv.git
$ cd mykv
$ make compile

我们想要创建一个2节点集群。首先,我们需要确定我们想要的副本数量。我们将选择R = 2(完全一致!)。要完成,我们必须从config / mykv.config文件更改为2副本值。 然后启动两个Erlang控制台:

$ erl -name node1@127.0.0.1 -pa _build/default/lib/mykv/ebin/ -config config/mykv.config

$ erl -name node2@127.0.0.1 -pa _build/default/lib/mykv/ebin/ -config config/mykv.config

然后在两个节点中启动mykv:

application:start(mykv).
ok

现在我们创建一个集群,调用方法mykv:join(Node)从node1到node2(或反之亦然)。如果我们想要向集群添加新节点,我们必须将该连接方法从新节点调用到集群中的一个。

(node1@127.0.0.1)2> mykv:join('node2@127.0.0.1').
ok

我们可以在两个节点中检查集群中的节点:

(node2@127.0.0.1)2> mykv:get_cluster_nodes().
['node2@127.0.0.1','node1@127.0.0.1']

现在我们已经创建了集群!但是......如果我们有很多节点怎么办?我们要重复N次吗?好吧,实际上我们可以调用aux方法。让我们用5个节点和3个副本(记住config / mykv.conf上的更改副本)。 首先打开5个Erlang控制台(如node1@127.0.0.1到node5@127.0.0.1)。之后调用node1上的setup_cluster方法:

(node1@127.0.0.1)2> mykv:setup_cluster(['node2@127.0.0.1','node3@127.0.0.1','node4@127.0.0.1','node5@127.0.0.1']).
ok

检查node3中的节点(例如):

(node3@127.0.0.1)1> mykv:get_cluster_nodes().
['node1@127.0.0.1','node2@127.0.0.1','node3@127.0.0.1',
 'node4@127.0.0.1','node5@127.0.0.1']

这样很容易,对吧?  现在我们要添加一些数据。让我们选择一个节点,例如node2

(node2@127.0.0.1)6> mykv:set(bucket1, key1, value1).
Key key1 is stored on Nodes ['node4@127.0.0.1','node5@127.0.0.1',
                          'node1@127.0.0.1']
ok

Mykv说桶1中的key1已经存储在节点4,节点5和node1上。让我们尝试在node2和node4上获取数据。

(node2@127.0.0.1)7> mykv:get_locally(bucket1, key1).
bucket_not_found


(node4@127.0.0.1)1> mykv:get_locally(bucket1, key1).
{mykv_record,key1,value1}

那是对的。那么,我们如何在node2上获取key1值?只是调用get mehod:

(node2@127.0.0.1)8> mykv:get(bucket1, key1).        
{mykv_record,key1,value1}

这是正确的价值!

运行测试

$ make test

本文使用googletrans自动翻译,仅供参考, 原文来自github.com

en_README.md

mykv

Authors: Felipe Ripoll. (ferigis@gmail.com).

Distributed Key/Value store.

Note: mykv is still under development. Currently the number of nodes must be immovable. There are more things to add like handoff, distributed tests, consistent hashing, adding quorums...

About

Mykv is a distributed key/value store written in Erlang/OTP. It uses Mnesia as a backend. This app was built for learning purposes, this is not suitable for use in production applications.

Build

Mykv uses Rebar3.

$ git clone https://github.com/ferigis/mykv.git
$ cd mykv
$ make compile

Example

We want to create a 2 nodes cluster. First we need to decide which number of Replicas we want. We are going to choose R=2 (full consistency!). To acomplish that we must change to 2 the replicas value from config/mykv.config file.
Then start two Erlang consoles:

$ erl -name node1@127.0.0.1 -pa _build/default/lib/mykv/ebin/ -config config/mykv.config

and

$ erl -name node2@127.0.0.1 -pa _build/default/lib/mykv/ebin/ -config config/mykv.config

Then start mykv in both nodes:

application:start(mykv).
ok

Now we create the cluster calling the method mykv:join(Node) from node1 to node2 (or viceversa). If we want to add a new node to the cluster we have to call that join method from the new node to one in the cluster.

(node1@127.0.0.1)2> mykv:join('node2@127.0.0.1').
ok

We can check the nodes in the cluster in both nodes:

(node2@127.0.0.1)2> mykv:get_cluster_nodes().
['node2@127.0.0.1','node1@127.0.0.1']

Now we have the cluster created!! but... what if we have many nodes? do we have to repeat this N times?? well, actually we can call an aux method. Lets do it with 5 nodes and 3 Replicas (remember change replicas on config/mykv.conf).
First open 5 Erlang consoles as we did before (from node1@127.0.0.1 to node5@127.0.0.1). After that call the setup_cluster method on node1:

(node1@127.0.0.1)2> mykv:setup_cluster(['node2@127.0.0.1','node3@127.0.0.1','node4@127.0.0.1','node5@127.0.0.1']).
ok

Checking the nodes in node3(for example):

(node3@127.0.0.1)1> mykv:get_cluster_nodes().
['node1@127.0.0.1','node2@127.0.0.1','node3@127.0.0.1',
 'node4@127.0.0.1','node5@127.0.0.1']

Much easy on that way, right?
Now we are going to add some data. Lets pick one node, node2 for example

(node2@127.0.0.1)6> mykv:set(bucket1, key1, value1).
Key key1 is stored on Nodes ['node4@127.0.0.1','node5@127.0.0.1',
                          'node1@127.0.0.1']
ok

Mykv says the key1 from bucket1 has been stored on node4, node5 and node1. Lets try to get the data on node2 and on node4.

(node2@127.0.0.1)7> mykv:get_locally(bucket1, key1).
bucket_not_found


(node4@127.0.0.1)1> mykv:get_locally(bucket1, key1).
{mykv_record,key1,value1}

That is correct. So, how can we get the key1 value on node2? just calling the get mehod:

(node2@127.0.0.1)8> mykv:get(bucket1, key1).        
{mykv_record,key1,value1}

Here it is the correct value!.

Running Tests

$ make test