<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by GPad on Medium]]></title>
        <description><![CDATA[Stories by GPad on Medium]]></description>
        <link>https://medium.com/@GPad?source=rss-604f30614bde------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*CoohbjCgFNTXTXdM.jpg</url>
            <title>Stories by GPad on Medium</title>
            <link>https://medium.com/@GPad?source=rss-604f30614bde------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 14:21:33 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@GPad/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[From web application to distributed system —Prologue]]></title>
            <link>https://medium.com/@GPad/from-web-application-to-distributed-system-prologue-ee61ce2e992e?source=rss-604f30614bde------2</link>
            <guid isPermaLink="false">https://medium.com/p/ee61ce2e992e</guid>
            <category><![CDATA[riak]]></category>
            <category><![CDATA[erlang]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <dc:creator><![CDATA[GPad]]></dc:creator>
            <pubDate>Mon, 04 Jun 2018 19:54:00 GMT</pubDate>
            <atom:updated>2018-06-04T19:54:00.332Z</atom:updated>
            <content:encoded><![CDATA[<h3>Welcome back</h3><p>It’s passed a long time from my last post. In this time I have been worked in different projects and I would share with you some experiences about how to develop a web application that can <em>“simply”</em> scale to a distributed system.</p><p>The idea in this post come from a different places that you can find in the reference above, but a special mention should go to <a href="https://twitter.com/ino_murko">Ino</a> that show me the first steps in this direction.</p><p>This post was first published on Code Beam Stockholm <a href="https://www.codesync.global/media/from-a-web-application-to-distributed-system/">website</a>. This post is about a presentation that I did the first in time in <a href="https://codesync.global/conferences/code-beam-lite-milan-2018/">Milan</a> and the second time in <a href="https://codesync.global/conferences/code-beam-sto-2018/">Stockholm</a>. The presentations are like software and bananas they rot if we do not take care of it. So this presentation was a little different in Stockholm and also the code-base that refers has been changed during this period and I hope also in the future. My plan is to create a workshop about this topic that can help the new developers to better familiarize with this concepts.</p><h3>Why this talk</h3><p>I have developed software from last millennium and I see a lot of patters and development models rise and fall. I’m a big fan of the contamination because some patterns are common in some communities but didn’t used or completely ignored in others.</p><p>There’s a saying in Italy <em>“paese che vai usanza che trovi”</em> (when in Rome do as the Romans do). In every community there are often patterns that the newbies should learn to better use and understand the platform and the language that their are using.</p><p>The Elixir community is very lucky, because it can take advantages of Erlang and the BEAM virtual machine’s patterns, which are ‘battle hardened’ from over 20 years of history. There is currently a lot of interest in how these problems are solved in the BEAM environment (using Actor model) and how some common patterns like Supervisor or GenServer are used in other languages or frameworks, Akka for example.</p><p>This post is the first of a series that will explain some patterns and some attitudes that are common in BEAM world but that can be unusual also for experienced developer that have never been meet Erlang before.</p><h3>A bit of history</h3><p>In the beginning, web development was very “simple”, there was a web server, a bunch of scripts and a database. Every single http request was translated into SQL queries that loaded or wrote data in a DB, the result was often the creation of a HTML page and nothing more. If we were in a “complex environment” we had also some background tasks powered often by something similar to cron. Our DB was the truth and everything was in a single place, everything had a beginning (the arrival of the http request) and an end (the delivery of the http response). This procedure developed a development attitude where we created all the related objects (we were probably adopting an OOP language) at the beginning of our work and we destroyed everything at the end. We were “ not allowed” to keep data and object in memory because everything started and finished with the life of the http request.</p><p>However, over time, the web became a platform to develop “applications” where the state cannot be kept in a single database and in a single process (you may hear a little voice whispering “microservices” …). Operations now span the length of a single http request and we now need the ability to communicate with a lot of different systems and keep track of their states. It is indeed natural the growing interest in BEAM and in the patterns that the BEAM can give us.</p><p>So we can ask us a question, a little rhetoric:</p><blockquote>How can we create a web application that takes advantage of the characteristics of the BEAM using the resources of a single node and eventually scale to a multi-node application?</blockquote><h3>What we will see</h3><p>In this talk and in the next posts we will try to create a simple e-commerce that will expose some classical problems that we will resolve using some pattern of the BEAM and we will also see how these patterns can help us to move from a single node application to a clustered application using a library like riak-core.</p><p>If you want to start to dig in to the code this is the <a href="http://github.com/gpad/les">repo</a> and this is the <a href="https://www.slideshare.net/gpadovani/from-a-web-application-to-a-distributed-system-100508341/gpadovani/from-a-web-application-to-a-distributed-system-100508341">link</a> of the talk that I did in Milan &amp; Stockholm. If something is not clear or you have any questions, don’t hesitate to leave a comment.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ee61ce2e992e" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[On my branching model]]></title>
            <link>https://medium.com/@GPad/on-my-branching-model-43149d1cbb94?source=rss-604f30614bde------2</link>
            <guid isPermaLink="false">https://medium.com/p/43149d1cbb94</guid>
            <category><![CDATA[continuous-delivery]]></category>
            <category><![CDATA[git]]></category>
            <category><![CDATA[continuous-integration]]></category>
            <dc:creator><![CDATA[GPad]]></dc:creator>
            <pubDate>Wed, 15 Nov 2017 13:55:42 GMT</pubDate>
            <atom:updated>2017-11-15T13:55:42.990Z</atom:updated>
            <content:encoded><![CDATA[<p>Some weeks ago I talked with my friend <a href="https://medium.com/u/7f48cc0a8fa0">Paolo Laurenti</a> about how we use branches in <a href="http://www.coders51.com/">coders51</a> and he suggested me to write about it, so here we are.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CcHVL6cf6zJ2jt7kRmIirg.jpeg" /><figcaption>A typical Italian git repository</figcaption></figure><h4>GitFlow</h4><p>A lot of people probably know the <a href="https://datasift.github.io/gitflow/IntroducingGitFlow.html">GitFlow</a> that suggests to have a situation like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/494/0*R1lk8ftNh-Z9FXiL.png" /></figure><p>As you can see in this way of work you have the <em>develop</em> branch where you start to branch your <em>feature-branch</em> and where you’ll merge back your work when it will be finished. You have also the <em>release branches</em> that are branches created from develop with the idea to become the new release of the product that will then merge back in <em>master</em>, the source of all.</p><p>This way of work could be good if you are developing a product with a scheduled release plan, for example a release at the end of the sprint or after some of them. IMHO it’s in contrast with the idea of <a href="https://en.wikipedia.org/wiki/Continuous_delivery">Continuous Delivery</a> where you should put ASAP your development in production. Using Gitflow you are able to release a single feature but you need to wait that all the release branch is ready for production. If you find a bug and you want to fix it ASAP, you need to create a branch from master instead of development as usual and you need to keep a lot of different branches with different purposes.</p><h4>Trunk based development</h4><p>Another idea, often opposed to git flow, is <a href="https://trunkbaseddevelopment.com/">trunk based development</a> where:</p><blockquote>A source-control branching model, where developers collaborate on code in a single branch called ‘trunk’ *, resist any pressure to create other long-lived development branches by employing documented techniques. They therefore avoid merge hell, do not break the build, and live happily ever after.</blockquote><p>In this is type of work you have only the <em>master</em> and the <em>feature-branch. </em>When you start a new feature, you branch from <em>master</em>, develop your feature and merge it back to master, nothing more. It’s very linear but for example if you need to get a feedback from your client in what is called <em>“stage environment”</em>, you should deploy the master branch in to staging environment and, when the entire branch is ready, you can also deploy it in to “production environment”. Until all the master is ready for production you can’t deploy it. To solve this you can use <a href="https://trunkbaseddevelopment.com/feature-flags/">feature flags</a> or <a href="http://Branch by Abstraction">branch by abstraction</a> but these techniques are not so easy to implement.</p><h4>master-staging flow</h4><p>In coders51 we often works with projects where the branching model is already established and it happens that we have to use one of the branching models above, often with no problem at all. Other times we can decide the branching model to use and in that case we opt for our branching model that we think it provides all the pros of Git Flow and Trunk Based Development minimizing their drawbacks.</p><p>The idea is to have one branch for environment, so you have the <em>master</em> that is for production, we set up a CI &amp; CD to continuously test and deploy any single commit on master.</p><p>Then we have a branch called <em>staging</em> that was branched from master at the beginning of the project and that is configured to CI &amp; CD in stage environment for every single commit.</p><p>Then we have the <em>feature-branch</em> <strong>always</strong> created from master. These branches are created by developers that need to work on the new features. These branches should be live as short as possible. When the developer has finished her work or a piece of work that she wants to share with client, she first merges her branch in staging and test it in an environment very similar to production. Then she can also check her work with client and make fixes and improvements in her feature-branch. Finally she merges again in staging. When the feature is ready for the production the <strong><em>feature-branch </em></strong>(not the staging) is merged in master and automatically deployed to production.</p><p>The pros of this way of work are:</p><ul><li>You can decide to put only some features in production not all the staging.</li><li>You need to maintain only two long living branch (one for every environment that you have).</li><li>You work always in the same way for feature, bug or hot fix. Branch from master, merge in staging and then in master.</li><li>Staging is always a superset of master.</li></ul><p>We find this way of work very proficient in our environment where we work on more features in parallel and we need to get in touch with client before the deployment. In fact, in this way, we can decide to publish not all the features that are in staging but only a subset of them.</p><p>What do you think about? Please leave a comment if you want to try it or spot an error.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=43149d1cbb94" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create a riak_core application in Elixir (Part 5)]]></title>
            <link>https://medium.com/@GPad/create-a-riak-core-application-in-elixir-part-5-86cd9d2c6b92?source=rss-604f30614bde------2</link>
            <guid isPermaLink="false">https://medium.com/p/86cd9d2c6b92</guid>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[erlang]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[riak]]></category>
            <dc:creator><![CDATA[GPad]]></dc:creator>
            <pubDate>Wed, 28 Jun 2017 19:26:10 GMT</pubDate>
            <atom:updated>2017-06-28T19:26:10.809Z</atom:updated>
            <content:encoded><![CDATA[<p>It’s been a long time sine my <a href="https://medium.com/@GPad/create-a-riak-core-application-in-elixir-part-4-728512ece224">last post</a>. I’m sorry about it but a lot of interesting things are happen, stay tuned on next posts and probably you’ll find some interesting things. Ok, quick recap.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/994/0*oW9w8Ygbw_3ZCRhC.jpg" /></figure><h4>Where we are now ..</h4><p>We <a href="https://github.com/gpad/no_slides">have</a> an application that can do a lot of things:</p><ul><li>We can add and remove data in a Key-Value way</li><li>We can get all the keys and all the values stored in our system</li><li>We can add and remove nodes in a very simple way without lose data</li></ul><p>But If you remember my last <a href="https://medium.com/@GPad/create-a-riak-core-application-in-elixir-part-4-728512ece224">post</a> we are not able to manage a crash. This is a very important feature if we want to be <a href="https://en.wikipedia.org/wiki/High_availability">HA</a>. We are creating a distributed system that would be able to survive to a crash of some nodes, we will see later how many failures nodes we can manage. Technically speaking we are creating an <a href="https://codahale.com/you-cant-sacrifice-partition-tolerance/">AP</a> system in the terms of <a href="https://en.wikipedia.org/wiki/CAP_theorem">CAP theorem</a>. We are trying to be available when we lose a node, and <a href="http://www.rgoarchitects.com/Files/fallacies.pdf">we will lose a node</a> at the end.</p><p>If you remember the architecture of the system, we have some nodes that contains a lot of vnodes.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*cOf2TFRSf3hySYh4." /><figcaption>Cluster with 4 physical nodes and 32 vnodes.</figcaption></figure><p>In the previous image we have 4 physical nodes that contains 32 vnodes. So every physical node contains 32 / 4 = 8 vnodes. If we lose a physical node we lose 8 vnodes and its data, ouch.</p><p>Given that we can’t work <a href="https://codahale.com/you-cant-sacrifice-partition-tolerance/">against the fate</a> a possible solution is to duplicate our data when we write and read from multiple source when we want our data back.</p><h4>Redundancy</h4><p>We could send a “write” command to some vnodes wait an ack from these nodes and return an “ok” to the client.</p><p>Similarly, when we want to perform a read we send a “read” request to some vnodes and wait a response from these, when we have collected all this data we can return it to the client.</p><p>As you can probably suppose we have some questions to deliver:</p><ul><li>To whom send the read or write request?</li><li>How many responses do we want to wait before send ok to client?</li><li>How many copies do we want try to read or write for every request?</li></ul><p>But before to continue we have another questions …</p><h4>How many nodes for our clusters?</h4><p>This is a good question. In the previous example we used 3 nodes. There is a interesting <a href="http://basho.com/posts/technical/why-your-riak-cluster-should-have-at-least-five-nodes/">article</a> that suggest to use at least 5 nodes. To make a long story short, you should have at least 3 replica for you data. If you have 3 nodes and one crash you can’t more have 3 replica. With only 4 nodes it’s not guaranteed that the three replica are on different physical nodes. So use 5 nodes. In our examples now we will use 5 nodes and also in the repo I added config for <a href="https://github.com/gpad/no_slides/blob/master/config/gpad_4.exs">node4</a> and <a href="https://github.com/gpad/no_slides/blob/master/config/gpad_5.exs">node5</a>.</p><h4>How many replicas and how many responses</h4><p>Until now we have used this function:</p><pre>:riak_core_apl.get_primary_apl(idx, 1, NoSlides.Service)</pre><p>Where idx is the index of the document created and 1 is how many entries we want in our preference list. Now we can request 3 items and, in my environment, I get this result:</p><pre>iex(gpad_3@127.0.0.1)7&gt; :riak_core_apl.get_primary_apl(idx, 3, NoSlides.Service)<br>[{{91343852333181432387730302044767688728495783936, :&quot;gpad_2@127.0.0.1&quot;}, :primary},<br> {{182687704666362864775460604089535377456991567872, :&quot;gpad_3@127.0.0.1&quot;}, :primary},<br> {{274031556999544297163190906134303066185487351808, :&quot;gpad_5@127.0.0.1&quot;}, :primary}]</pre><p>Ok very well! But, for example, if I kill the node gpad_2 then I get this result:</p><pre>iex(gpad_3@127.0.0.1)8&gt; :riak_core_apl.get_primary_apl(idx, 3, NoSlides.Service)<br>[{{182687704666362864775460604089535377456991567872, :&quot;gpad_3@127.0.0.1&quot;}, :primary},<br></pre><pre>{{274031556999544297163190906134303066185487351808, :&quot;gpad_5@127.0.0.1&quot;}, :primary}]</pre><p>So I request 3 items but I get 2. We can try to use the function:</p><pre>:riak_core_apl.get_apl(idx, <a href="http://twitter.com/n_val">3</a>, NoSlides.Service)</pre><p>This function with all node up and running return a similar result to get_primary_apl:</p><pre>iex(gpad_3@127.0.0.1)9&gt; :riak_core_apl.get_apl(idx, 3, NoSlides.Service)        <br>[{91343852333181432387730302044767688728495783936, :&quot;gpad_2@127.0.0.1&quot;},</pre><pre>{182687704666362864775460604089535377456991567872, :&quot;gpad_3@127.0.0.1&quot;},<br> {274031556999544297163190906134303066185487351808, :&quot;gpad_5@127.0.0.1&quot;}]<br>iex(gpad_3@127.0.0.1)10&gt;</pre><p>But if we kill the node gpad_2 we get this result:</p><pre>iex(gpad_3@127.0.0.1)10&gt; :riak_core_apl.get_apl(idx, 3, NoSlides.Service)<br>[{182687704666362864775460604089535377456991567872, :&quot;gpad_3@127.0.0.1&quot;},<br> {274031556999544297163190906134303066185487351808, :&quot;gpad_5@127.0.0.1&quot;},<br> {91343852333181432387730302044767688728495783936, :&quot;gpad_1@127.0.0.1&quot;}]</pre><p>As you can see the function return always 3 entries, the first two are “primary” entries the last is filled with another entry. In this way we can always send the request of write/read to 3 vnodes and then wait the response. But wait, how many nodes should respond to declare the command completed?</p><p><a href="https://twitter.com/KentBeck/status/596007846887628801">It depends!</a></p><p>If we wait a response from all replicas and one node has some problems, the client could receive error too.</p><p>If we wait a response only from one replica and the other replicas doesn’t receive data for some reason (network problem?!?), we get only one replica with our data and we can lost it if this node crash.</p><p>So a good balance is to send the request to three replicas and wait a response from two of them. Now let see some code …</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/550/0*w2a9APIOOjcGMpB1." /><figcaption>Thanks to <a href="https://www.nasa.gov/feature/margaret-hamilton-apollo-software-engineer-awarded-presidential-medal-of-freedom">Margaret Hamilton</a>.</figcaption></figure><h4>Send a request and wait a response</h4><p>As we already wrote we need to send a command to a group (3) of vnodes and wait the response from a subset of them (2). To do this you need:</p><ul><li>A supervisor with a simple_one_for_one strategy</li><li>A process that implement <a href="http://erlang.org/doc/man/gen_fsm.html">gen_fsm</a> to manage the request/response cycle</li><li>A new function in <a href="https://github.com/gpad/no_slides/blob/master/lib/no_slides/service.ex#L48">NoSlides.Service</a> module to manage this request/response path.</li></ul><h4>Writing in a redundancy way</h4><p>Take for <a href="http://github.com/gpad/no_slides">example</a> the function to write a new key and value pair NoSlides.Service.ft_put/2. This function is very simple, it execute the function NoSlides.WriteFsm.write/2 and get back a req_id, an unique id that identifies the request. This last function spawn a new process implemented in module NoSlides.WriteFSM and supervised by module NoSlides.WriteFsmSupervisor. After that the NoSlides.Service.ft_put/2 waits the real response from the FSM. This is the code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c195891debe793657173ddced1d0645c/href">https://medium.com/media/c195891debe793657173ddced1d0645c/href</a></iframe><p>As you can see a similar pattern is followed for the get function.</p><h4>Send requests and wait responses</h4><p>A lot of work is done in NoSlide.WriteFsm module. This module expose a function called write/2 that accept key and value and spawn a new <a href="http://erlang.org/doc/man/gen_fsm.html">gen_fsm</a> process and dynamically attach it do WriteFsmSupervisor.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b357c053c45d1b2121837e41364636ed/href">https://medium.com/media/b357c053c45d1b2121837e41364636ed/href</a></iframe><p>The process receive these arguments:</p><ul><li>req_id an unique identifier for the request</li><li>self() is the pid of the request that start to wait response</li><li>k, v the key and the value to store</li></ul><p>The implementation of this fsm is where the magic happen. The gen_fsm is similar to a gen_server and respect the pattern start_link/init but in the init you should return the first state of the FSM. This is our code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/39b2e0ff0bda947e49f18cb92aa1782f/href">https://medium.com/media/39b2e0ff0bda947e49f18cb92aa1782f/href</a></iframe><p>The first step to be executed is prepare where we get the preference list</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/911879b1f5d6255f803b58d82a99a860/href">https://medium.com/media/911879b1f5d6255f803b58d82a99a860/href</a></iframe><p>as you can see we request 3 items in preference list. After the prepare we go in execute state.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/31a2a3883b88e9283134fdb225133546/href">https://medium.com/media/31a2a3883b88e9283134fdb225133546/href</a></iframe><p>The execute step execute a function against the vnode, very similar to what we have done until now in the last posts. Now we execute the command on the 3 items stored in state.pref_list, we send the req_idto the vnode because we need to match our request, and we also add the sender of the request {:fsm, :undefined, self()} because we want receive the response from the vnode to consolidate it in the next step. We also set a @timeout of 30 seconds.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6be4b5749c5fb085ea471eaeebbd68e8/href">https://medium.com/media/6be4b5749c5fb085ea471eaeebbd68e8/href</a></iframe><p>As we have seen before we wait two responses (@n_writes 2) from the vnodes otherwise the @timeout expires. If we receive enough responses we send to the requester stored in state.from the :ok and exit normally.</p><h4>Inside the vnode</h4><p>The implementation inside the vnode is very similar to the other command in vnode:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ca2d7d36b4b6974badd5231449ffb2d/href">https://medium.com/media/6ca2d7d36b4b6974badd5231449ffb2d/href</a></iframe><p>the only difference is the response that is not only :ok but {:ok, :req_id}.</p><h4>And the read ???</h4><p>As you have noticed we wrote a new KV on our system and it has been replicated on three or at least two nodes. We can get a very similar behaviour with the read. We get the values from three or at least two nodes and return it. But when you start to replicate the data often this data can’t be in perfect sync.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*HESR0sjHSCfPptyO." /></figure><p>The <a href="http://docs.basho.com/riak/kv/2.2.3/developing/usage/conflict-resolution/">conflict resolution</a> is a wide field and there are a lot of strategies that you can apply. The more widely strategies adopted are <a href="http://basho.com/posts/technical/vector-clocks-revisited/">vector clock</a> (or better vector version) and <a href="http://basho.com/posts/technical/distributed-data-types-riak-2-0/">CRDT</a>. In our example we simple return all different values that we will find. So the consolidate of the NoSlides.GetFsm module is implemented in this way:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5a7519dd8034b0ea6c59cb9d75fa02f4/href">https://medium.com/media/5a7519dd8034b0ea6c59cb9d75fa02f4/href</a></iframe><h4>Run it</h4><p>Now it’s time to run some code. We can try to execute some reads and writes. As I told before I suggest to run five nodes. In the repo now there are a config also for node gpad_4 and gpad_5. Now you can run all the nodes in different consoles in this way:</p><pre>$ MIX_ENV=gpad_1 iex --name gpad_1@127.0.0.1 -S mix run<br>$ MIX_ENV=gpad_2 iex --name gpad_2@127.0.0.1 -S mix run<br>$ MIX_ENV=gpad_3 iex --name gpad_3@127.0.0.1 -S mix run<br>$ MIX_ENV=gpad_4 iex --name gpad_4@127.0.0.1 -S mix run<br>$ MIX_ENV=gpad_5 iex --name gpad_5@127.0.0.1 -S mix run</pre><p>now you can join all the nodes executing this command on all nodes from two to five:</p><pre>iex(gpad_2@127.0.0.1)1&gt; :riak_core.join(:&quot;gpad_1@127.0.0.1&quot;)</pre><p>After a while the cluster became stable and you can get the state of the cluster in this way:</p><pre>iex(gpad_3@127.0.0.1)6&gt; NoSlides.Service.ring_status<br>==================================== Nodes ====================================<br>Node a: 4 ( 25.0%) gpad_1@127.0.0.1<br>Node b: 3 ( 18.8%) gpad_2@127.0.0.1<br>Node c: 3 ( 18.8%) gpad_3@127.0.0.1<br>Node d: 3 ( 18.8%) gpad_4@127.0.0.1<br>Node e: 3 ( 18.8%) gpad_5@127.0.0.1<br>==================================== Ring =====================================<br>abcd|eabc|deab|cdea|<br>:ok</pre><p>Probably your valued could be slightly different from that but you should have 5 nodes in the cluster.</p><p>Now you can write a value replicated on more nodes, to do that try to execute this command:</p><pre>iex(gpad_1@127.0.0.1)8&gt; NoSlides.Service.ft_put(:gpad, 1)</pre><p>If you look the log you should see that this value is written on three nodes, in my environment it’s written on node 2, 3 and 4. Now you can try to read the value in this way:</p><pre>iex(gpad_1@127.0.0.1)8&gt; NoSlides.Service.ft_get(:gpad)<br>17:48:09.370 [debug] Start GetFSM<br>{:ok, [1]}</pre><p>You should get {:ok, [1]} because all the copies are in sync.</p><p>Now we try to kill one of our nodes, for example the 2. Now without the gpad_2 node, if you try to execute the ft_get repeatedly you should get a response like this:</p><pre>iex(gpad_1@127.0.0.1)8&gt; NoSlides.Service.ft_get(:gpad)<br>{:ok, [nil, 1]}</pre><p>Why? Because you sent 3 requests of read to 3 nodes and return response to client when you get 2 of them. Sometimes one of the these two responses came from a node without value, in my environment the node 5.</p><p>Continue in our experiment. Now we write the value 2, in this way:</p><pre>iex(gpad_1@127.0.0.1)8&gt; NoSlides.Service.ft_put(:gpad, 2)</pre><p>It is written on node 3, 4, and 5. Now we restart node 2 and we see that a transfer of data from node 5 and node 2 happen so all node now are synchronized and we get back this value: [2]. As you can imagine this operation can take time and in some situation one of the 3 node can lose the request and remain with the old value. As writeen before there are some strategy to manage this situation also when <a href="http://basho.com/posts/technical/vector-clocks-revisited-part-2-dotted-version-vectors/">simultaneous writes occur</a>.</p><h4>Recap</h4><p>Now our system is able, in a very simple way, to manage a crash from a node, without lose data. As I said before we choose to be AP instead of CP. So we choose to loose some of consistency (we return [nil, 2]) but remain available also when we loose some nodes.</p><p>This is the last post of our journey on how to implement a riak_core application in Elixir. It was a pleasure for me to write it but also a big effort. I hope that you can find this posts useful and interesting.</p><p>I’m enthusiast about distributed system and in the next posts I’ll try to give you some interesting info about this wonderful field of IT.</p><p>Please leave a comment about this post or if you are interested in some particular topic I’ll be happy to consider it for the next posts.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=86cd9d2c6b92" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create a riak_core application in Elixir (Part 4)]]></title>
            <link>https://medium.com/@GPad/create-a-riak-core-application-in-elixir-part-4-728512ece224?source=rss-604f30614bde------2</link>
            <guid isPermaLink="false">https://medium.com/p/728512ece224</guid>
            <category><![CDATA[riak]]></category>
            <category><![CDATA[erlang]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <dc:creator><![CDATA[GPad]]></dc:creator>
            <pubDate>Tue, 18 Apr 2017 07:18:32 GMT</pubDate>
            <atom:updated>2017-04-18T07:18:32.510Z</atom:updated>
            <content:encoded><![CDATA[<p>In the <a href="https://medium.com/@GPad/create-a-riak-core-application-in-elixir-part-3-8bac36632be0#.23nff6tcn">last post</a> I ended with this simple question: <em>“How can we get the list of all the keys?”</em>. In this post I’m going to answer this question.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*z0p4D97GqPGEj1zsm4ymPg.png" /><figcaption>This is a ring drawn by one of my favourite <a href="http://www.openculture.com/2017/04/the-tone-circle-john-coltrane-drew-to-illustrate-the-theory-behind-his-most-famous-compositions-1967.html">musicians</a>.</figcaption></figure><h3>Coverage Command</h3><p>In riak_core slang a command that involves all the nodes of the cluster is called <strong>“coverage command”. </strong>A coverage command is handled by a specific function of <a href="https://github.com/basho/riak_core/blob/develop/src/riak_core_vnode.erl">riak_core_vnode</a> behaviour. To implement a vnode we need to implement this behaviour, and in particular this function:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2aeeb24bb5fc7195eb79826bc0a2e412/href">https://medium.com/media/2aeeb24bb5fc7195eb79826bc0a2e412/href</a></iframe><p>As you can see, from a prospective of vnode, a coverage command is very simple. The tricky part comes when you want to query every vnodes of the cluster and collect all their responses.</p><h3>Starting from Service</h3><p>As we had seen in previous posts the service module is the module used by the client to interact with our cluster, so it’s a good place to start to implement our coverage command. In this module we add a new public function for every feature that we want to implement. In this post we are going to implement two functions:</p><ul><li>the first one is keys and it returns the keys stored in the KV</li><li>the second one is values and it returns the values stored in in the KV</li></ul><p>These functions retrieve all the data from the various vnodes using a new process that implements a behaviour defined by riak_core called <a href="https://github.com/basho/riak_core/blob/develop/src/riak_core_coverage_fsm.erl"><strong><em>riak_core_coverage_fsm</em></strong></a><strong><em>. </em></strong>To obtain the final result from this new process we wait a message that will arrive when all the data will be gathered. To do that we call the function start_fsm/1 that returns an unique id. We can use this req_id with the receive statement to identify our response through a classic pattern matching.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2dfb85eeac50abe304168961d3d5ba4/href">https://medium.com/media/e2dfb85eeac50abe304168961d3d5ba4/href</a></iframe><p>Before going in detail about how to implement a coverage command, we need to agree upon what we mean when we talk about <strong><em>all </em></strong>nodes. Do we mean all the running nodes or all the nodes defined in the cluster? What should happen when we try to execute a command on all the defined nodes of the clusters but one of them is disconnected? This is something that you need to decide at application level. In our example we implement the coverage commands for all the running nodes. If you need to go more into details, please leave a comment and I’ll try to address your requests in the next posts.</p><h3>Implementing riak_core_coverage_fsm</h3><p>As you can think the <a href="https://github.com/basho/riak_core/blob/develop/src/riak_core_coverage_fsm.erl"><strong><em>riak_core_coverage_fsm</em></strong></a> behaviour is based on a classic Erlang<a href="http://erlang.org/doc/man/gen_fsm.html"> gen_fsm</a>. This behaviour take care of all the work necessary to interrogate and retrieve the responses from the vnodes. We need to set up some infos required by this behaviour to execute the work. To start I suggest to create a new module called NoSlides.CoverageFsm and to implement it in this way:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/443c6879812aab2f330f80f9e9f06198/href">https://medium.com/media/443c6879812aab2f330f80f9e9f06198/href</a></iframe><p>As you can see there are some functions to implement; the first one is:</p><h4>start_link(req_id, from, what)</h4><p>This is the function that is called by supervisor to start the gen_fsm. We simply use the :riak_core_coverage_fsm.start_link function to start the FSM passing the required arguments, the module that implement the behaviour, who did the call and the arguments that will be passed to the next functions.</p><h4>init(from, args)</h4><p>This is the first function called by riak_core, you need to return a tuple shaped in this way:</p><ul><li>{what, req_id, from} are the arguments that are passed to handle_coverage_command of the vnode module and are managed by riak_core as an opaque structure</li><li>:allup is an atom that specifies that we need to execute the command on all the running nodes, not on all the nodes in the cluster</li><li>n_val and vnode_coverage are two arguments that specify how to manage the replication in our cluster. For now we set them to one.</li><li>NoSlides.Service and NoSlidesVNode_master are respectively the service used to identify the running nodes and the module that implements the vnode.</li><li>timeout is a time out</li><li>the last is the state of the finite state machine that will be passed to every next function.</li></ul><p>As you can see we decide to execute the command against all the running nodes and we save in the state the pid of the caller and the received arguments.</p><h4>process_result({partition, value}, state)</h4><p>That function is called for every response received from the vnode. In this function we collect all the values that we receive in the res field of the state. It the end we will have, inside the res, an array of array composed by all the values (keys or values) that we’ve received. It the end of the process riak_core will call the function finish/2.</p><h4>finish(:clean, state)</h4><p>This function is called when everything goes fine and, in this implementation, we will send a message to the process that made the request using the pid saved inside the from field of the state.</p><p>If something goes wrong, for example a node goes down during a command execution, the command will fail and it will be called the function finish/2 with a first argument of type {:error, reason}.</p><h4>finish({:error, reason}, state)</h4><p>This function receives the reason for the error (time out for example) and it simply sends a message with {:error, reason} to the caller.</p><h3>vnode implementation</h3><p>As you can see in the gist below the implementation in the vnode is very simple.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5b4f147fab4a08ad51eb004f9f9c28b2/href">https://medium.com/media/5b4f147fab4a08ad51eb004f9f9c28b2/href</a></iframe><p>Through the pattern matching it will be identified the correct function to execute and it will return the data requested getting it from the state.</p><h3>Supervisor</h3><p>To start all the processes the service module calls the function start_fsm/1 defined in the supervisor’s module NoSlides.CoverageFsmSupervisor and passing to it the atom :values or :keys to get the values or the keys of our cluster.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9d762e98df7c7d5a6e97b5388f53bda4/href">https://medium.com/media/9d762e98df7c7d5a6e97b5388f53bda4/href</a></iframe><p>As you can see the supervisor is very simple. Just a note: the strategy that it implements is simple_one_for_one because we want to attach dynamically the children to the supervisor when the function start_fsm/1 is called.</p><h3>Recap</h3><p>In this post we have implemented two commands to get the list of the keys and the values saved in our KV store. Until now we have created a cluster that is able to do a lot of interesting things:</p><ul><li>it can store and retrieve the data in KV way, distributing uniformly in the cluster (remember the <a href="https://en.wikipedia.org/wiki/Consistent_hashing">consistent hashing</a> algorithm).</li><li>It can execute a command on the entire cluster, as example, we have just implemented a command to get all the keys and the values stored on it.</li><li>It can be resized, grown or shrinked, through the addition and removal of nodes using the suitable commands.</li></ul><p>Now we are ready to try to manage a node crash. In the next post we will discuss how to manage a crash, what problems will arise and how to manage them.</p><p>As always please leave a comment if you spot an error or if you find anything not clear. You can find in the usual<a href="https://github.com/gpad/no_slides"> repo</a> the implementation discussed in this post.</p><p>As a side note, on April 7th 2017, I gave a speech at <a href="http://www.erlang-factory.com/rome2017">Rome Erlang Factory Lite</a> about these topics. You can find my slides <a href="https://www.slideshare.net/gpadovani/cook-your-kv">here</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=728512ece224" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create a riak_core application in Elixir (Part 3)]]></title>
            <link>https://medium.com/@GPad/create-a-riak-core-application-in-elixir-part-3-8bac36632be0?source=rss-604f30614bde------2</link>
            <guid isPermaLink="false">https://medium.com/p/8bac36632be0</guid>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[riak]]></category>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[erlang]]></category>
            <dc:creator><![CDATA[GPad]]></dc:creator>
            <pubDate>Mon, 20 Feb 2017 07:26:07 GMT</pubDate>
            <atom:updated>2017-02-20T07:26:07.503Z</atom:updated>
            <content:encoded><![CDATA[<p>Hi, welcome back! This is the third post about how to write an elixir application with riak_core. In may <a href="https://medium.com/@GPad/create-a-riak-core-application-in-elixir-part-2-88bdec73f368">last post</a> I asked some questions, let me copy it here:</p><ul><li>What happens if a node leaves the cluster with :riak_core.leave ?</li><li>What happens if a node crashes?</li><li>How can we get the list of all keys?</li></ul><p>In this post we will try to answer to first question <em>What happens if a node leaves the cluster?</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/640/0*9bU3ClL1A46_SjCe." /><figcaption>Classic riak_core cluster</figcaption></figure><p>Probably, if you are reading this post you had already try it and you had discovered that some data disappears. The same problem arise also if you add a node. Why?</p><h3>Handoff</h3><p>Do you remember how the data are organized in the cluster? You have a fixed number of vnodes that are equally distributed on the various physical nodes. Every vnode manages a partition of the keyspace. When you have one physical node, all the vnodes are on this node, when you add another one, part of these vnodes are “moved” to this second one. In reality, the process is a little bit complex. The vnodes are not migrated, when a single node boot, it starts with all the vnodes, when you start a second node and join the first one, you have two nodes that run all the vnodes simultaneously. Riak_core start a procedure to kill the vnodes that are not necessary on each physical node and move the data from one physical node to another. This procedure is called handoff. You can check it out on <a href="https://github.com/basho/riak_core/wiki/Handoffs">wiki’s project</a>.</p><p>If you remember, in the <a href="https://github.com/basho/riak_core/blob/develop/src/riak_core_vnode.erl#L95">vnode behaviour</a> there are some functions that are dedicated to manage the handoff. This functions are called by riak_core to mange this situation and you can fill it with the <em>correct </em>code. Let starts!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/700/0*nZYEA6HjAS7uHN4r.jpg" /></figure><h4>handoff_starting(dest, state)</h4><p>This function is the first function that is called by riak_core to signal that an handoff is starting. The vnode should return {true, state} to continue the handoff and manage the state of the vnode. If, for any reasons, the vnode can’t manage the handoff immediately it can return false, and postpone it.</p><h4>handoff_cancelled(state)</h4><p>This function is called if, for any reason, the handoff procedure is stopped.</p><h4>is_empty(state)</h4><p>This function is called by riak_core to evaluate if the vnode has some data. If the vnode return {:true, state}then no data is present, the delete function is immediately called, otherwise the <em>real </em>handoff procedure start with handle_handoff_command function.</p><h4>handle_handoff_command(request, sender, state)</h4><p>This function in called during the handoff to manage two main reasons. The first one is for transferring the data from one physical node, the source, to the destination node. The function is called with request argument of type riak_core_fol_req_v2 record. To manage this record we need to import the record definition by the Erlang include file. To to this we need to use <a href="https://hexdocs.pm/elixir/Record.html">Record</a> module and write something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6982894e63401989de546cb0d1abe743/href">https://medium.com/media/6982894e63401989de546cb0d1abe743/href</a></iframe><p>At line 8 we import a record definition from the include file of riak_core. After this import we can use the defined record as a target in the pattern matching of function defined at line 10. This function is called only to move data during an handoff. This record contains two important field. The first foldfun contains a function that should be called for every data that we want to move. If you know the concept of a <a href="https://en.wikipedia.org/wiki/Fold_%28higher-order_function%29">reduce</a> (aka fold) function this process should be familiar. You cycle on every item of the collection accumulating data in a variable, starting from a defined value. In our case the value to start is acc0 extracted by the record that we receive. From line 14 to line 16 we get all the data of the vnode from state.data and pass it to Enum.reduce/3, we pass also acc0as start value. We define an anonymous function that receive the key and value pair and the accumulator and call the foldfun passing the pair and the accumulator. The result of this reduction is the result that we can return from the entire function.</p><p>If during this operation the vnode receive a normal command, getor put for example, riak_core route the request to handle_handoff_command funtion and not to the classic handle_command. In our case, through the pattern matching we recognize this situation and we call directly handle_command. During this procedure, riak_core receives the data to transfer through the foldfun, it calls another function to transform this data in a binary form that can be easily transferred to the physical destination node. This function is encode_handoff_item.</p><h4>encode_handoff_item(k, v)</h4><p>This function is very simple, require that we encode the data in a format that can be then transferred and then decoded on other nodes. We can easily fill this function with:</p><pre>:erlang.term_to_binary({k, v})</pre><h4>handle_handoff_data(bin_data, state)</h4><p>On other nodes we receive the data in this function so we can decode the data with the analogous Erlang function:</p><pre>{k, v} = :erlang.binary_to_term(bin_data)</pre><h4>handoff_finished(dest, state)</h4><p>This is the function that signals the end of the handoff procedure. We should return {:ok, state} if everything is fine. It also precedes the delete function and the last function terminate.</p><h4>delete(state)</h4><p>This function is called before the termination of the vnode and we should delete all the data. In our case, we don’t store anything but we can return an empty map as data in state:</p><pre>{:ok, Map.put(state, :data, %{})}</pre><h4>terminate(reason, state)</h4><p>This is the last function, we can put a log to put out the reason of the termination. It should be :normal in our case.</p><h3>Recap</h3><p>As you can see a lot of functions are called on source vnode and only handle_handoff_data is called on receiving node. The more important function to keep in mind is handle_handoff_command because it receives the data for the handoff procedure that are encapsulated in fold_req_v2 record but, keep in mind that it also receives the normal command.</p><p>Another interesting thing is the function handoff_started that the vnode can optionally exports and that will be called, if present, when the handoff will start. In this case the vnode can stop the handoff returning {:error, <em>reason</em>}.</p><h3>References:</h3><p>First of all you can find the entire implementation of what we have done until now in this <a href="https://github.com/gpad/no_slides">repo</a>. There are also some post made by basho that are interesting about how to manage the handoff:</p><ul><li><a href="http://basho.com/posts/technical/understanding-riak_core-handoff/">http://basho.com/posts/technical/understanding-riak_core-handoff/</a></li><li><a href="http://basho.com/posts/technical/understanding-riak_core-visitfun/">http://basho.com/posts/technical/understanding-riak_core-visitfun/</a></li><li><a href="http://basho.com/posts/technical/understanding-riak_core-building-handoff/">http://basho.com/posts/technical/understanding-riak_core-building-handoff/</a></li></ul><p>In the next post I’ll write about “How can we get the list of all keys?”, we leave the managing of crash nodes as last post.</p><p>Please leave a comment if you have any question or something is not clear. See you next time!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8bac36632be0" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create a riak_core application in Elixir (Part 2)]]></title>
            <link>https://medium.com/@GPad/create-a-riak-core-application-in-elixir-part-2-88bdec73f368?source=rss-604f30614bde------2</link>
            <guid isPermaLink="false">https://medium.com/p/88bdec73f368</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <category><![CDATA[erlang]]></category>
            <category><![CDATA[riak]]></category>
            <dc:creator><![CDATA[GPad]]></dc:creator>
            <pubDate>Wed, 18 Jan 2017 07:53:04 GMT</pubDate>
            <atom:updated>2017-02-02T22:20:56.059Z</atom:updated>
            <content:encoded><![CDATA[<p>First of all, thanks to everyone who read my <a href="https://medium.com/@GPad/create-a-riak-core-application-in-elixir-part-1-41354c1f26c3">previous</a> post. In this second post we continue to use the code that we started to write in the previous post and we will add some features. You can find a final version <a href="https://github.com/gpad/no_slides">here</a>.</p><p>The first feature to add is the classic ping.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*p1k9GZI8FK0vUMyt.jpg" /><figcaption>If you played the original then you are old as I am :-)</figcaption></figure><p>Before we start, a quick recap, we have a simple “empty” application that can be compiled and executed in three different environment using this command:</p><pre># this is node 1<br>MIX_ENV=gpad_1 iex --name gpad_1@127.0.0.1 -S mix run</pre><pre># this is node 2<br>MIX_ENV=gpad_2 iex --name gpad_2@127.0.0.1 -S mix run</pre><pre># this is node 3<br>MIX_ENV=gpad_3 iex --name gpad_3@127.0.0.1 -S mix run</pre><p>What we want to do now is to add a simple ping functionality that can be used from the application console and that can show how a cluster works.</p><h3>Nodes, virtual nodes and <strong>one ring to rule them all</strong></h3><p>One of the main concepts of riak_core is the concept of virtual nodes that is related to what is called <a href="http://docs.basho.com/riak/kv/2.2.0/learn/concepts/clusters/#the-ring">the ring</a>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/0*6LbTAu3NsWJ2l6pb.png" /></figure><p>The idea is simple. You should take all the possible values that you can manage in your application (this is called the <em>keyspace)</em> and partition it. For example, riak_core uses a classic hash function to spot a value from a binary. The code is similar to this:</p><pre>iex(1)&gt; key = &quot;gpad&quot;<br>&quot;gpad&quot;<br>iex(2)&gt; idx = :erlang.term_to_binary(key) |&gt; :crypto.sha       <br>&lt;&lt;211, 108, 199, 240, 242, 57, 27, 91, 139, 82, 154, 145, 27, 215, 191, 24, 107, 77, 162, 202&gt;&gt;<br>iex(3)&gt; &lt;&lt; v::unsigned-big-integer-size(160) &gt;&gt; = idx<br>&lt;&lt;211, 108, 199, 240, 242, 57, 27, 91, 139, 82, 154, 145, 27, 215, 191, 24, 107, 77, 162, 202&gt;&gt;<br>iex(4)&gt; v<br>1207022950459909619005619617169028319269647917770<br>iex(5)&gt;</pre><p>As you can see, we transform the term that we want to use as key to a binary and then we hash the value with :crypt.sha.After that we use the <a href="https://hexdocs.pm/elixir/Kernel.SpecialForms.html#%3C%3C%3E%3E/1">binary pattern matching</a> to show the real value that is between zero and 2¹⁶⁰.</p><p>By default, riak_core divide the keyspace (2¹⁶⁰) in 64 partitions, every single partition manages (<em>claims</em>) a segment of the ring. Every single partition is called <em>v</em>irtual node (<em>vnode</em>) and it is distributed on a real node, in this way we can minimize the data transfer when we add or remove nodes (we will see in more details in the next posts).</p><p>We are in the BEAM world so it is natural to map every <em>vnode</em> to an Erlang process. Riak_core is so nice to provide a <a href="http://erlang.org/doc/design_principles/des_princ.html#id69904">behaviour</a> to help us in the implementation of a vnode process. Adopting a behaviour in Elixir is very simple. I suggest to create a module called NoSlides.VNode in lib/no_slides and declare the riak_core_vnode behaviour like this:</p><pre>defmodule NoSlides.VNode do<br>  @behaviour :riak_core_vnode<br>end</pre><p>If you try to compile the project you should get some warnings that say <strong><em>warning: undefined behaviour function …</em></strong>. This is because we don’t have implemented the behaviour yet. If you come from a OOP mindset then you can think of a behaviour as an interface, a list of functions that a module should implement to be executed in a determined context. This is the list of warnings that we have:</p><pre>warning: undefined behaviour function delete/1 <br>warning: undefined behaviour function encode_handoff_item/2<br>warning: undefined behaviour function handle_command/3 <br>warning: undefined behaviour function handle_coverage/4 <br>warning: undefined behaviour function handle_exit/3<br>warning: undefined behaviour function handle_handoff_command/3 warning: undefined behaviour function handle_handoff_data/2<br>warning: undefined behaviour function handoff_cancelled/1 <br>warning: undefined behaviour function handoff_finished/2<br>warning: undefined behaviour function handoff_starting/2<br>warning: undefined behaviour function init/1<br>warning: undefined behaviour function is_empty/1<br>warning: undefined behaviour function terminate/2</pre><p>These functions should be implemented by your module and are called when certain event are triggered:</p><ul><li><strong><em>init/1</em></strong><em> </em>— It’s the function called when the virtual node is created. It’s very similar to the <em>init</em> function of <a href="https://hexdocs.pm/elixir/GenServer.html#c:init/1">GenServer</a>. It receives an array of one element, the value of the partition that the vnode takes care of. What is returned from this function is the state of the process. The state is always passed as a last argument in every other callbacks.</li><li><strong><em>terminate/2</em></strong><em> —</em> It’s called when the vnode is terminated, it receives the reason (an atom) and the state.</li><li><strong><em>handle_exit/3</em></strong><em> — </em>It’s called when a process <a href="http://erlang.org/doc/reference_manual/processes.html#id87759">linked</a> with the vnode dies. It receives the pid of the dead process, the reason and the state. You should return {:noreply, state} to continue.</li><li><strong><em>delete/1 </em></strong><em>— </em>It’s called when it’s necessary to remove the data related to this vnode. As usual it receives the state and should return {:ok, new_state}.</li><li><strong><em>handle_command/3 </em></strong><em>— </em>It’s called when a command is executed against this vnode. It’s the function that we use to implement our command.</li><li><strong><em>handle_coverage/4 </em></strong><em>— </em>It’s called when we want to create a <a href="https://marianoguerra.github.io/little-riak-core-book/listing-keys-from-a-bucket.html">coverage command</a>. We treat this type of command in the next posts.</li><li>The remaining functions are all related to <a href="https://github.com/basho/riak_core/wiki/Handoffs">handoffs procedure</a>. We’ll discuss handoffs in the next posts.</li></ul><p>There is a last function that we should implement if we want the system to work properly, the start_vnode/1 function. So you could implement your module like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c3a570bfeb9f50ad2d051a8f94728139/href">https://medium.com/media/c3a570bfeb9f50ad2d051a8f94728139/href</a></iframe><p>The function handle_command/3 is what contains the real work, it works with pattern matching (similar to how a GenServer works) so, we can implement handle_command in this way:</p><pre>def handle_command({:ping, v}, _sender, state) do<br>  {:reply, {:pong, v + 1}, state}<br>end</pre><p>Now that we have the ping command implemented, how can we execute it from the console? We need to introduce a new concept that I like to call it <strong><em>service. </em></strong>A service is a module that wraps how to interact with the command exposed by riak_core. This service should be registered in riak_core so riak_core knowns what node exposes that service. Check out the code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8f476376bd3f3bd199cd870ed86e2361/href">https://medium.com/media/8f476376bd3f3bd199cd870ed86e2361/href</a></iframe><p>At line 4 we calculate the <em>id</em> of the value that we want to store, and the id is a value in the <em>keyspace</em>. Using this <strong><em>idx</em></strong>, we can ask riak_core for a <strong><em>preference list </em></strong>with method <strong><em>get_primary_apl </em></strong>(line 5). The preference list is a collection of which nodes take care of which partition. When we call <em>get_primary_apl </em>we ask for a list length one (second parameter) and a node that implement the <strong><em>NoSlides.Service</em></strong> (third parameter). In this example we require one element because we want execute a command on only one node, in the next posts we will discuss about <em>redundancy. </em>From the preference list we extract the <strong><em>index_node</em></strong> that we use to identify the real and virtual node on which to execute the command. This node has the ownership of the data identified by <strong><em>idx.</em></strong></p><p>At line 9 we use <strong><em>index_node</em></strong> to execute the call using the function :riak_core_vnode_master.sync_command. This function is synchronous and it doesn’t return until the work in vnode module is completed. If you check the :riak_core_vnode_master code then you will also find the function :riak_core_vnode_master.command that executes the work in an asynchronous way.</p><p>You can also check out the sync_spawn_command that work like sync_command, if you check the code you read this comment:</p><pre>%% Send a synchronous spawned command to an individual Index/Node <br>%% combination.<br>%% Will not return until the vnode has returned, but the<br>%%% vnode_master will<br>%% continue to handle requests.<br>sync_spawn_command({Index,Node}, Msg, VMaster) -&gt;</pre><p>This is not true, it’s probably an old comment and also an old implementation. Last but not least, the vnode name is postfixed with _master(<strong><em>NoSlides.VNode_master</em></strong>) which is a riak_core convention.</p><p>Now that we have implemented the <strong><em>Service </em></strong>and the <strong><em>VNode</em></strong> we need to put everything together. To do this we need to start from the beginning …</p><h3>Starting a riak_core application</h3><p>We are in a OTP application so we need to start from a module that implements the <a href="https://hexdocs.pm/elixir/Application.html">Application</a> behaviour. If you create a empty project with mix then you probably have a module called NoSlides that use Application, throw away it and replace it with something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/91ce54bbe4431b2e7777a921b5657102/href">https://medium.com/media/91ce54bbe4431b2e7777a921b5657102/href</a></iframe><p>At line 6 we start a supervisor that we’ll implement later, if everything starts smoothly then at line 8 we register the module that implements the vnode and, at line 9, we register the module that implement the service.</p><p>The supervisor should be placed in file lib/no_slides/supervisor.ex and should be something like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6ba3b9e617a185853b1f11cd6c723ad1/href">https://medium.com/media/6ba3b9e617a185853b1f11cd6c723ad1/href</a></iframe><p>This is a classic supervisor but we need to pay attention to some details, the supervisor id should end with _sup (line 6) and the worker id should be postfixed with _master_worker(line 11).</p><p>After that, you can start a node with this command:</p><pre>MIX_ENV=gpad_1 iex --name gpad_1@127.0.0.1 -S mix run</pre><p>inside elixir console, you can execute the service ping:</p><pre>Interactive Elixir (1.3.4) - press Ctrl+C to exit (type h() ENTER for help)<br>iex(gpad_1@127.0.0.1)1&gt; NoSlides.Service.ping<br>{:pong, 2}<br>iex(gpad_1@127.0.0.1)2&gt; NoSlides.Service.ping(42)<br>{:pong, 43}<br>iex(gpad_1@127.0.0.1)3&gt;</pre><p>Now we can join more nodes and execute a <em>distributed ping</em>, as first step we need to start more nodes on different consoles:</p><pre># this is node 1<br>MIX_ENV=gpad_1 iex --name gpad_1@127.0.0.1 -S mix run</pre><pre># this is node 2<br>MIX_ENV=gpad_2 iex --name gpad_2@127.0.0.1 -S mix run</pre><pre># this is node 3<br>MIX_ENV=gpad_3 iex --name gpad_3@127.0.0.1 -S mix run</pre><p>Now you can join all the nodes together, from console of node two you type:</p><pre>iex(gpad_2@127.0.0.1)1&gt; :riak_core.join(&#39;gpad_1@127.0.0.1&#39;)</pre><p>Do the same on console of node 3:</p><pre>iex(gpad_3@127.0.0.1)1&gt; :riak_core.join(&#39;gpad_1@127.0.0.1&#39;)</pre><p>If you check the console of node 1 then you should see some logs like this:</p><pre>12:21:53.168 [info] &#39;gpad_2@127.0.0.1&#39; joined cluster with status &#39;valid&#39;<br>12:22:39.155 [info] &#39;gpad_3@127.0.0.1&#39; joined cluster with status &#39;joining&#39;<br>12:22:39.191 [info] &#39;gpad_3@127.0.0.1&#39; changed from &#39;joining&#39; to &#39;valid&#39;</pre><p>Now you can ask to riak_core to print the status of the <strong><em>ring </em></strong>with this command:</p><pre>{:ok, ring} = :riak_core_ring_manager.get_my_ring<br>:riak_core_ring.pretty_print(ring, [:legend])</pre><p>You should see something like this:</p><pre>============================= Nodes =============================<br>Node a: 22 ( 34.4%) gpad_1@127.0.0.1<br>Node b: 21 ( 32.8%) gpad_2@127.0.0.1<br>Node c: 21 ( 32.8%) gpad_3@127.0.0.1<br>============================= Ring =============================<br>abcc|abcc|abcc|abcc|abcc|abcc|abcc|abcc|abcc|abcc|abca|abba|abba|abba|abba|abba|</pre><p>As you can see, your ring is divided in 64 partitions and the first node receive 22 VNodes, the other two nodes 21 VNodes, you can check it also with <strong><em>observer:</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*a-URVlMl4S2YGLItyAIZGw.png" /></figure><p>Now we can add a log in the VNode implementation so we can see which node respond to ping call (remember to add require Logger at the beginning of the module)</p><pre>def handle_command({:ping, v}, _sender, state) do<br>  Logger.debug(&quot;Receive ping with value: #{v}&quot;)<br>  {:reply, {:pong, v + 1}, state}<br>end</pre><p>from console 1 execute this command:</p><pre>iex(gpad_1@127.0.0.1)1&gt; NoSlides.Service.ping<br>{:pong, 2}</pre><p>On node 2 you can see a log like this:</p><pre>12:43:00.822 [debug] Receive ping with value: 1</pre><p>We have a very simple distributed ping. If you change the value passed to ping, you should see a different node that respond to ping. For example, if you use the value 42 then the node 3 should respond.</p><p>Now that we have a ping we can easily create a in-memory key-value store.</p><h3>Cook your memory KV</h3><p>Now that we have understood how to connect a service and a vnode, we can easily create a KV memory store exposing a get and put function on service module:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1cc8585e1cd4461f5644989d26779590/href">https://medium.com/media/1cc8585e1cd4461f5644989d26779590/href</a></iframe><p>Add the get and put implementation also on VNode:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e0aea724c6018b7c2445d79552974442/href">https://medium.com/media/e0aea724c6018b7c2445d79552974442/href</a></iframe><p>Now you can put and get some values from different nodes.</p><p>Stop and restart every node, It’s not necessary rejoin all the nodes. From console of node 1 execute this command:</p><pre>iex(gpad_1@127.0.0.1)1&gt; NoSlides.Service.put(:k, 42)<br>:ok<br>iex(gpad_1@127.0.0.1)2&gt; NoSlides.Service.get(:k)    <br>42</pre><p>Check logs on node 2:</p><pre>iex(gpad_2@127.0.0.1)1&gt; <br>19:31:30.634 [debug] [put]: k: :k v: 42</pre><pre>19:31:39.242 [debug] [get]: k: :k</pre><p>You can also retrieve this value from node 3:</p><pre>iex(gpad_3@127.0.0.1)1&gt; NoSlides.Service.get(:k)<br>42</pre><p>We have a simple KV store in memory, you can put a lot of different types as value:</p><pre>iex(gpad_1@127.0.0.1)9&gt; NoSlides.Service.put(&quot;gpad&quot;, %{ blogs: [&quot;riak_core I&quot;, &quot;riak_core II&quot;] })<br>:ok<br>iex(gpad_1@127.0.0.1)10&gt; NoSlides.Service.get(&quot;gpad&quot;)<br>%{blogs: [&quot;riak_core I&quot;, &quot;riak_core II&quot;]}</pre><p>And also as key:</p><pre>iex(gpad_1@127.0.0.1)11&gt; NoSlides.Service.put(%{a: 1, b: 2}, &quot;gpad&quot;)<br>:ok<br>iex(gpad_1@127.0.0.1)12&gt; NoSlides.Service.get(%{a: 1, b: 2})        <br>&quot;gpad&quot;</pre><p>This is the beginning of a simple memory key-value store. There are some open questions:</p><ul><li>What happens if a node leaves the cluster with :riak_core.leave ?</li><li>What happens if a node crashes?</li><li>How can we get the list of all keys?</li></ul><p>I’ll try to answer these questions in the following posts. If you have any questions or spot some errors, don’t hesitate to leave a comment.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=88bdec73f368" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Create a riak_core application in Elixir (Part 1)]]></title>
            <link>https://medium.com/@GPad/create-a-riak-core-application-in-elixir-part-1-41354c1f26c3?source=rss-604f30614bde------2</link>
            <guid isPermaLink="false">https://medium.com/p/41354c1f26c3</guid>
            <category><![CDATA[elixir]]></category>
            <category><![CDATA[erlang]]></category>
            <category><![CDATA[riak]]></category>
            <category><![CDATA[distributed-systems]]></category>
            <dc:creator><![CDATA[GPad]]></dc:creator>
            <pubDate>Mon, 12 Dec 2016 07:03:48 GMT</pubDate>
            <atom:updated>2017-01-30T17:21:47.606Z</atom:updated>
            <cc:license>http://creativecommons.org/publicdomain/zero/1.0/</cc:license>
            <content:encoded><![CDATA[<p>The 3rd December I talked at <a href="http://www.noslidesconf.net/#schedule">NoSlidesConf</a> and as example for my application I create <a href="https://github.com/gpad/no_slides">this</a> simple application in Elixir to show some features of <a href="https://github.com/basho/riak_core/">riak_core</a>.</p><p>After that I decided to write a tutorial about the use of riak_core in Elixir. It is true that it’s very simple to use an Erlang library from an Elixir application, this app is an example, but a lot of examples are in Erlang and a lot of alchemists are not so familiar with Erlang syntax, which is a shame :-(.</p><p>In this first post I’ll describe how to create an empty Elixir application that can be started as multiple nodes on the same machine without any functionality. You should wait for the next part to have some useful features :-).</p><h4>How to start</h4><p><a href="https://github.com/basho/riak_core/">Riak_core</a> is a library developed by <a href="http://basho.com/">basho</a>, this library was extracted as a stand-alone library from <a href="https://github.com/basho/riak">riakKV/riakTS</a>. The <a href="https://github.com/basho/riak_core/">repo</a> from basho is not easily usable from Elixir app, but you can check out the code as the “<em>main source</em>”. As an alchemist you are probably familiar with <a href="https://hex.pm/">https://hex.pm/</a> (and if you are not you should :-)), if you search riak_core there you find <a href="https://hex.pm/packages/riak_core_ng">riak_core_ng</a> that is a <a href="https://github.com/project-fifo/riak_core">fork</a> form the original library, maintained by <a href="https://twitter.com/heinz_gies">Heinz N. Gies</a>.</p><p>Before starting, some notes about the version of Erlang and Elixir. In all these posts I used Elixir v1.3.4 and Erlang 18.3. In my test I was not able to use Erlang 19 for riak_core. If you check the basho <a href="https://github.com/basho/riak_core/">repo</a> and riak_core_ng <a href="https://github.com/project-fifo/riak_core">repo</a> you can see some work to port the libraries to Erlang v19 but, until now, the work is not finished. To manage different versions of Erlang I suggest to use <a href="https://github.com/kerl/kerl">kerl</a> or <a href="https://github.com/asdf-vm/asdf">asdf</a> and, if you want use <a href="http://erlang.org/doc/apps/observer/observer_ug.html">observer</a> ( and you want use it :-)), I suggest you to build the version of Erlang OTP with wx enabled. You can check <a href="http://featurebranch.com/howto-getting-wx-to-work-with-erlang-r16b02-on-os-x/">here</a> if you are on OSX and <a href="http://stackoverflow.com/questions/32934641/how-to-get-erlang-to-show-ui-components-debugger-and-observer-on-linux">here</a> if you are on linux; if you are on other OS, try to google it.</p><p>So let’s start, create a new mix application with --sup parameter:</p><pre>$ mix new no_slides --sup</pre><p>In this way we have created an empty app, the first thing to do is to add the riak_core to the dependencies as OTP app to start automatically, so in <em>mix.exs</em> file add this lines:</p><pre>def application do<br>  [applications: [:<strong>riak_core</strong>, :logger],<br>   mod: {NoSlides, []}]<br>end</pre><pre># ...</pre><pre>defp deps do<br>  [<br>   <strong> {:riak_core, &quot;~&gt; 2.2.8&quot;, hex: :riak_core_ng}</strong><br>  ]<br>end</pre><p>Remember to insert :riak_core as the first application in application function otherwise some strange things could occur :-D.</p><p>After that, as usually, get the dependencies:</p><pre>$ mix deps.get</pre><p>and now you can run your first riak_core app, if you do it for example in this way:</p><pre>$ iex -S mix run</pre><p>you will get a lot of errors. First of all you should execute the node using the parameter --name &lt;node_name&gt;, I can run it in this way:</p><pre>$ iex --name gpad@127.0.0.1 -S mix run</pre><p>In this way I’m running the application with a <a href="http://erlang.org/doc/reference_manual/distributed.html">full name</a>, but if you try, you will get another error. You need to set the schema of your node and some other configurations. You can find the schema in deps/riak_core/priv directory: copy it in your priv directory with this command:</p><pre>$ cp -r deps/riak_core/priv .</pre><p>The last thing to do is give to riak_core some info about how manage this node. I suggest to add these lines in file config/config.exs</p><pre>config :riak_core,<br>  ring_state_dir: &#39;ring_data_dir&#39;,<br>  handoff_port: 8099,<br>  handoff_ip: &#39;127.0.0.1&#39;,<br>  schema_dirs: [&#39;priv&#39;]</pre><pre>config :sasl,<br>  errlog_type: :error</pre><p>The config of riak_core is necessary to riak_core to start as single node (we modify a little bit when we will run a multinodes), we tell riak_core where to save all the <a href="https://github.com/basho/riak_core/wiki#ring">ring</a> data (cluster configuration), what is the IP/port to manage the <a href="https://github.com/basho/riak_core/wiki/Handoffs">handoff</a> and where to find the schema of the config. Last but not least, we tell the <a href="http://erlang.org/doc/man/sasl_app.html">sasl</a> application to only print error logs.</p><p>Now you should be able to execute a single node riak_core application:</p><pre>$ iex --name gpad@127.0.0.1 -S mix run</pre><p><strong>Hooray!!!</strong></p><h4>Multinodes environment</h4><p>With riak_core we want create a distributed application, so our application should be executed in some different nodes. When we develop our application it is simpler to run more nodes on the same machine. To do it, it’s necessary to execute every node with some different configuration, and we can achieve this result using the <a href="http://elixir-lang.org/getting-started/mix-otp/introduction-to-mix.html#environments">environments</a> support of mix. We identify every node with a different environment and we add a different config file for every environment. In my example I call every env as gpad_1, gpad_2 and gpad_3. I suggest to add three files in config directory called gpad_1.exs, gpad_2.exs and gpad_3.exs. Modify config.exs adding this line to the end of the file:</p><pre>import_config “#{Mix.env}.exs”</pre><p>In every new file you should set the config of riak_core in a way that can’t conflict with other nodes that run on the same machine, so we have:</p><p><em>gpad_1.exs</em></p><pre>use Mix.Config</pre><pre>config :riak_core,<br>  node: &#39;gpad_1@127.0.0.1&#39;,<br>  web_port: 8198,<br>  handoff_port: 8199,<br>  ring_state_dir: &#39;ring_data_dir_1&#39;,<br>  platform_data_dir: &#39;data_1&#39;</pre><p><em>gpad_2.exs</em></p><pre>use Mix.Config</pre><pre>config :riak_core,<br>  node: &#39;gpad_2@127.0.0.1&#39;,<br>  web_port: 8298,<br>  handoff_port: 8299,<br>  ring_state_dir: &#39;ring_data_dir_2&#39;,<br>  platform_data_dir: &#39;data_2&#39;</pre><p><em>gpad_3.exs</em></p><pre>use Mix.Config</pre><pre>config :riak_core,<br>  node: &#39;gpad_3@127.0.0.1&#39;,<br>  web_port: 8398,<br>  handoff_port: 8399,<br>  ring_state_dir: &#39;ring_data_dir_3&#39;,<br>  platform_data_dir: &#39;data_3&#39;</pre><p>Now you have three different environment and you can start it in three different console, before start remember to use Erlang 18 because we need to recompile the application for the three different environment, so start every line in a different console:</p><pre># this is node 1<br>MIX_ENV=gpad_1 iex --name gpad_1@127.0.0.1 -S mix run</pre><pre># this is node 2<br>MIX_ENV=gpad_2 iex --name gpad_2@127.0.0.1 -S mix run</pre><pre># this is node 3<br>MIX_ENV=gpad_3 iex --name gpad_3@127.0.0.1 -S mix run</pre><p>If you want to continue to run a single node as before using command iex --name gpad@127.0.0.1 -S mix run, you should add a file named dev.exs with inside only this line:</p><pre>use Mix.Config</pre><p>Now you should be able to run three different node on the same machine.</p><p><strong>Good job!!!</strong></p><p>In the next post we try to add a simple <em>ping </em>functionality and join the three nodes together.</p><p>Before concluding I need to say a lot of “thank you” to:</p><ul><li><a href="http://basho.com/">basho</a> for creating the <a href="https://github.com/basho/riak_core/">library</a> and the <a href="http://basho.com/search/?q=riak_core">docs</a>.</li><li><a href="https://twitter.com/heinz_gies">Heinz N. Gies</a> for creating <a href="https://github.com/project-fifo/riak_core">riak_core_ng</a> that can be used in <a href="https://hex.pm/packages/riak_core_ng">Elixir</a>.</li><li><a href="https://github.com/kanatohodets">Ben Tyler</a> for the inspirational talk at <a href="http://www.elixirconf.eu/elixirconf2016/ben-tyler">ElixirConf.EU</a>.</li><li><a href="https://twitter.com/warianoguerra">Mariano Guerra</a> for this amazing <a href="https://marianoguerra.github.io/little-riak-core-book/">book</a> about riak_core.</li><li><a href="https://twitter.com/rzezeski">Ryan Zezeski</a> for his <a href="https://github.com/rzezeski/try-try-try">blog</a> about riak_core.</li><li>You that are reading this post and tying to use riak_core, thank you.</li></ul><p>Stay tuned, see you next time!!!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=41354c1f26c3" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>