<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>Alvaro Videla</title>
 <link href="http://videlalvaro.github.com/atom.xml" rel="self"/>
 <link href="http://videlalvaro.github.com/"/>
 <updated>2021-04-17T14:14:21+00:00</updated>
 <id>http://videlalvaro.github.com/</id>
 <author>
   <name>Alvaro Videla</name>
   <email>videlalvaro@gmail.com</email>
 </author>

 
 <entry>
   <title>Reference vs. Interpretation</title>
   <link href="http://videlalvaro.github.com/2021/04/reference-vs-interpretation.html"/>
   <updated>2021-04-17T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2021/04/reference-vs-interpretation</id>
   <content type="html">&lt;meta name=&quot;twitter:card&quot; content=&quot;summary_large_image&quot; /&gt;

&lt;meta name=&quot;twitter:site&quot; content=&quot;@old_sound&quot; /&gt;

&lt;meta name=&quot;twitter:creator&quot; content=&quot;@old_sound&quot; /&gt;

&lt;meta name=&quot;twitter:title&quot; content=&quot;Reference vs Interpretation&quot; /&gt;

&lt;meta name=&quot;twitter:description&quot; content=&quot;Intentio Auctoris vs Intentio Lectoris&quot; /&gt;

&lt;h1 id=&quot;reference-vs-interpretation&quot;&gt;Reference vs. Interpretation&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;April 17 2021&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;In Brains in a vat Putnam argues that if an ant walks around and the path it traces happens to “draw” 
or “resemble” Churchill that doesn’t imply the image left in the sand &lt;em&gt;represents&lt;/em&gt; Churchill, because 
the ant had no way of knowing about Churchill. The same about a description of trees found in a text 
that happens to be written by monkeys randomly typing on a typewriter. The words there do not &lt;em&gt;refer&lt;/em&gt; 
to anything, says Putnam. So from a &lt;em&gt;reference&lt;/em&gt; perspective &lt;em&gt;intentio auctoris&lt;/em&gt; matters. 
(see Reason, Truth and History by Putnam, Brains in a Vat).&lt;/p&gt;

&lt;p&gt;Now about interpretation. Let’s say there’s a culture for which a volcano is a god that communicates to the 
villagers about its mood, when the volcano is angry, there’s an eruption and so on. 
So the villagers &lt;em&gt;interpret&lt;/em&gt; different things about the world and react accordingly, 
based on how the volcano behaves, they provide sacrifices and so on. 
Clearly as per Putnam’s argument, the volcano cannot be referring to anger or any other 
kinds of moods, or cannot be demanding sacrifices, but from a semiotic perspective 
that is completely different, almost irrelevant. Semiotics is interested in the &lt;em&gt;code&lt;/em&gt; created by the villagers 
that assign meaning to the various states of the volcano, and what kind of interpretation the 
villagers do based on that code. For semiotics it is irrelevant if the interpretations are “true”, 
or can be “scientifically proved”, for semiotics what matters is that there’s a
signification system where those signs are interpreted as such by a culture. Here &lt;em&gt;intentio lectoris&lt;/em&gt; matters 
more than &lt;em&gt;intentio auctoris&lt;/em&gt;. (see The Limits of Interpretation by Eco, Intentio Lectoris: The State of the Art).&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Programming Languages are not Languages</title>
   <link href="http://videlalvaro.github.com/2018/09/programming-languages-are-not-languages.html"/>
   <updated>2018-09-01T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2018/09/programming-languages-are-not-languages</id>
   <content type="html">&lt;meta name=&quot;twitter:card&quot; content=&quot;summary_large_image&quot; /&gt;

&lt;meta name=&quot;twitter:site&quot; content=&quot;@old_sound&quot; /&gt;

&lt;meta name=&quot;twitter:creator&quot; content=&quot;@old_sound&quot; /&gt;

&lt;meta name=&quot;twitter:title&quot; content=&quot;Programming Languages are not Languages&quot; /&gt;

&lt;meta name=&quot;twitter:description&quot; content=&quot;Does the language metaphor applies to programming languages?&quot; /&gt;

&lt;meta name=&quot;twitter:image&quot; content=&quot;http://alvaro-videla.com/images/programming_languages.png&quot; /&gt;

&lt;h1 id=&quot;programming-languages-are-not-languages&quot;&gt;Programming Languages are not Languages&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;September 01 2018&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;In my &lt;a href=&quot;http://alvaro-videla.com/2018/08/the-power-of-generative-metaphor.html&quot;&gt;previous article&lt;/a&gt; I talked about how the metaphors used to present a problem, set the stage for how we are going to talk about the problem. They provide a framework for our thinking, on one side helping us solve it, and on another side they constraining the solutions we find.&lt;/p&gt;

&lt;p&gt;I ended the article by saying that we are so used to call Programming Languages “languages” that we don’t even stop for a second to think about the implications that are carried over (&lt;em&gt;metapherein&lt;/em&gt;) when we liken them to natural languages.&lt;/p&gt;

&lt;p&gt;Of course treating programming languages like natural languages, is very helpful. This allows us to reason about their syntax and their grammar rules, the semantics of certain expressions, and so on.&lt;/p&gt;

&lt;p&gt;Recently I found some cases where from my point of view the PLT community was getting too carried away by the language metaphor that they abscribed some characteristics from natural languages to programming languages which I think don’t apply 100% to them. As always happens with metaphors, one need to be careful when using them, because by definition they are not a 1 to 1 map from source domain to target domain.&lt;/p&gt;

&lt;p&gt;In the article &lt;em&gt;&lt;a href=&quot;https://cacm.acm.org/magazines/2018/3/225475-a-programmable-programming-language/fulltext&quot;&gt;A Programmable Programming Language&lt;/a&gt;&lt;/em&gt; by Felleisen et al[4]. the authors said something that caught my attention with regards to how they talk about programming languages. Here’s the section where they present what are the principles that guide Language Oriented Programming (the topic of their article).&lt;/p&gt;

&lt;p&gt;In a very butchered way, we can say that despite the new name, LOP is like programming with embedded DSLs, but in this case they propose a language (&lt;a href=&quot;https://racket-lang.org/&quot;&gt;Racket&lt;/a&gt;) whose goal is to remove the friction between the eDSL and the host language (see their article for a full definition of LOP). I think LOP is a very interesting idea and thanks to this article I definitely want to try Racket.&lt;/p&gt;

&lt;p&gt;They define one of the guidelines for LOP as: &lt;em&gt;&lt;strong&gt;Turn extra-linguistic mechanisms into linguistic constructs&lt;/strong&gt;&lt;/em&gt;, and they expand:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;A LOP programmer who resorts to extra-linguistic mechanisms effectively acknowledges that the chosen language lacks expressive power. The numerous external languages required to deal with Java projects - a configuration language, a project description language, and a makefile language-represent symptoms of this problem. We treat such gaps as challenges later in the article.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This is a very interesting point that brings to mind all these a-ha! moments that designers talk about, when they come up with a new idea that changes people’s lives, where we think “this has always been this way, but I had no idea it could be better”. After reading that paragraph I do wonder why a Java project (to stay in their example), is built with a variety of languages. Do we really need all those languages to build a software project?&lt;/p&gt;

&lt;p&gt;On the other hand, after seeing what happened with Scala and &lt;a href=&quot;https://racket-lang.org/&quot;&gt;SBT&lt;/a&gt;, where sometimes the project build description ends up written directly in Scala, I’m not sure having one language that encompasses all is something I’d want. Erlang suffered the same problem. RabbitMQ - as many other tools built in Erlang - exposed their configuration in Erlang. That was the source of many problems for people that tried to use RabbitMQ but that were foreign to Erlang. Is it worth to expose users to an alien configuration format, just so the developers of the project can program in just one language?&lt;/p&gt;

&lt;p&gt;Also when they refer to “extra-linguistic mechanisms” do they mean different &lt;a href=&quot;https://en.wikipedia.org/wiki/Code_%28semiotics%29&quot;&gt;codes&lt;/a&gt; as in semiotics? In a &lt;a href=&quot;http://alvaro-videla.com/2018/08/a-veritable-system-of-intercodical-relations.html&quot;&gt;previous article&lt;/a&gt; I talked how different codes are used in film. Think for example a movie that shows a sad protagonist, with rain in the background and how that blends with the background music and sometimes even the lyrics. There the different codes work together to augment the meaning conveyed by that scene. Also having different codes, can allow one community (music) to evolve independently from another one (film), so when they are used in communion later on, we get an even richer experience. The same could be said about build tools vs. programming tools, &lt;a href=&quot;https://increment.com/documentation/notes-on-the-synthesis-of-labyrinths/&quot;&gt;but I digress&lt;/a&gt;…&lt;/p&gt;

&lt;p&gt;Back to the topic of this article, I wanted to see when the language metaphor doesn’t hold. Let me bring back a sentence from the paragraph I cited above:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;A LOP programmer who resorts to extra-linguistic mechanisms effectively acknowledges that the chosen language lacks expressive power.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When they say expressive power the authors attach a footnote that reads:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Like many programming-language researchers, we subscribe to a weak form of the Sapir-Whorf hypothesis&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So what’s the Sapir-Whorf hypothesis. This hypothesis is pretty popular in linguistics, because it became kind of an urban legend. Have you ever heard the semi-myth that &lt;a href=&quot;https://medium.com/r/?url=https%3A%2F%2Fen.wikipedia.org%2Fwiki%2FEskimo_words_for_snow&quot;&gt;Eskimo languages have a large amount of words for the concept of snow&lt;/a&gt;? This is related to the Sapir-Whorf hypothesis or the &lt;a href=&quot;https://en.wikipedia.org/wiki/Linguistic_relativity&quot;&gt;Linguistic Relativity&lt;/a&gt; hypothesis. As you can see from the text cited above, the authors subscribe to a weak form of the hypothesis. So what are these two forms? Quoting Wikipedia:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;The strong version says that language determines thought and that linguistic categories limit and determine cognitive categories.&lt;/li&gt;
  &lt;li&gt;The weak version says that linguistic categories and usage only influence thought and decisions.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The strong version is generally agreed to be false, while there’s several experiments that show that our mother languages do shape the way we see the world, that is, the way we can categorize it. If you want to learn more about this hypothesis, I’d recommend reading the chapter My Language Limits My Thoughts, from the book &lt;em&gt;&lt;a href=&quot;https://www.cambridge.org/core/books/women-talk-more-than-men/8532B2D22C70E5D3AEE4F9176AD29E7D&quot;&gt;Women Talk More than Men… and Other Myths about Language Explained&lt;/a&gt;&lt;/em&gt;[6].&lt;/p&gt;

&lt;p&gt;So while I don’t know what do the authors mean when they say “we subscribe to a weak form of the Sapir-Whorf hypothesis”, I’m not sure this hypothesis applies to programming languages. Or in other words, this is stretching the language metaphor. Let’s see why.&lt;/p&gt;

&lt;p&gt;While the world is a &lt;em&gt;continuum&lt;/em&gt;, we slice into tiny pieces, we &lt;em&gt;discretize&lt;/em&gt; it with words. We categorize the world and this categorization is expressed in the words from our language:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Language makes experience orderly by creating discrete and stable objects out of a relatively undifferentiated sensory flux; these objects are internalized as concepts.[2]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And here’s why I don’t think the Sapir-Whorf hypothesis fully applies to programming languages. When we program we categorize the world in English (or Spanish, or any other natural language), and then we encode that into a program. So the programming language I’m using, is not limiting the way in which I see the world. As Umberto Eco says in &lt;em&gt;The Search for The Perfect Language&lt;/em&gt;[3]:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Computer languages like BASIC or Pascal, are, in fact, a priori languages. They are not full languages because their syntax, though rigorous, is simplified and limited, and they remain parasitic on the natural languages which attach meaning to their empty symbols […]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Is there another metaphor we could use to think about programming languages, besides languages. I think there is: tools. This was proposed by Kenneth Iverson in his 1979 Turing Award lecture: &lt;em&gt;&lt;strong&gt;Notation as a Tool of Thought&lt;/strong&gt;&lt;/em&gt;[5].&lt;/p&gt;

&lt;p&gt;If we think of programming languages as tools, then it becomes evident how some programming languages lend themselves naturally to problems that in other languages might be - while not impossible - hard to solve. See for example Erlang, a language that’s tailored for Concurrency Oriented and Message Passing programming, or Racket itself, a language tailored for LOP.&lt;/p&gt;

&lt;h2 id=&quot;relative-linguistic-relativity&quot;&gt;Relative Linguistic Relativity&lt;/h2&gt;

&lt;p&gt;Above I said “I don’t think the Sapir-Whorf hypothesis fully applies”. Let’s explain why I wrote “&lt;em&gt;fully applies&lt;/em&gt;”. Words help us categorize the world around us. They help us define concepts. The etymology of the word &lt;a href=&quot;https://www.etymonline.com/word/define#etymonline_v_914&quot;&gt;define&lt;/a&gt; is very interesting, because it brings forth the idea of “to bound, limit” (the word term also has a similar &lt;a href=&quot;https://www.etymonline.com/word/term#etymonline_v_10648&quot;&gt;etymology&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;So if a language influences the way we define things, I can see how this manifest in how programmers with backgrounds in different language paradigms define the units in their code. To give an example: A &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for loop&lt;/code&gt;, something so common in imperative languages like Java or C, is usually abstracted away in functional programming languages with constructs like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fold&lt;/code&gt;. A functional programmer looking at Java code, might see immediately that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for loop&lt;/code&gt; must be extracted into its own function. A Java programmer might wonder why a bunch of functions that are scattered across a file and seem to operate on the same data, aren’t abstracted away into an class.&lt;/p&gt;

&lt;p&gt;It would be interesting to see if this is in fact true: that different language paradigms influence what people determine as units in their code, for instance, when they decide to apply the &lt;a href=&quot;https://refactoring.com/catalog/extractMethod.html&quot;&gt;extract-method&lt;/a&gt; refactoring, to name one example. It’s interesting to note that Forth calls these units “words”, and the process of refactoring, simply &lt;a href=&quot;http://thinking-forth.sourceforge.net/&quot;&gt;factoring&lt;/a&gt;[1].&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Because metaphors are so commonplace in our day to day speaking, we lose track of when we are using them, which results in us framing problems in what perhaps could be a wrong setting, as explained in my previous &lt;a href=&quot;http://alvaro-videla.com/2018/08/the-power-of-generative-metaphor.html&quot;&gt;article&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If we want to think about what kind of problems a programming language makes easy to solve, and which ones they make more difficult, I’d say it’s better to use the tool metaphor, and see where that framing could take us.&lt;/p&gt;

&lt;p&gt;With regards to the Sapir-Whorf hypothesis, I think it might apply to programming languages if we think about what each language community most generally defines as a unit of code, i.e.: something that could be wrapped inside a function/method and thus be named, &lt;a href=&quot;https://en.wikipedia.org/wiki/Ludwig_Wittgenstein&quot;&gt;and not passed over in silence&lt;/a&gt;.&lt;/p&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;references&quot;&gt;References:&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Brodie, Leo. Thinking Forth. 2004. http://thinking-forth.sourceforge.net&lt;/li&gt;
  &lt;li&gt;Chandler, Daniel. Semiotics: The Basics, Third Edition. Routledge, 2017.&lt;/li&gt;
  &lt;li&gt;Eco, Umberto. The Search for the Perfect Language. Blackwell, 2006.&lt;/li&gt;
  &lt;li&gt;Felleisen, Matthias et al. A Programmable Programming Language. Communications of the ACM, March 2018, Vol. 61 №3, Pages 62–71.&lt;/li&gt;
  &lt;li&gt;Iverson, Kenneth. Notation as a Tool of Thought. Communications of the ACM, August 1980, Volume 23 Issue 8, Pages 444–465.&lt;/li&gt;
  &lt;li&gt;Kaplan, Abby. Women Talk More than Men… and Other Myths about Language Explained. Cambridge University Press, 2016&lt;/li&gt;
&lt;/ol&gt;

&lt;hr /&gt;

&lt;h2 id=&quot;credits&quot;&gt;Credits:&lt;/h2&gt;

&lt;p&gt;Image from the British Library Online Archive, from &lt;a href=&quot;http://www.bl.uk/manuscripts/Viewer.aspx?ref=burney_ms_108_f077r&quot;&gt;Heron of Alexandria, Pneumatica, De automatis&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>The Power of Generative Metaphor</title>
   <link href="http://videlalvaro.github.com/2018/08/the-power-of-generative-metaphor.html"/>
   <updated>2018-08-31T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2018/08/the-power-of-generative-metaphor</id>
   <content type="html">&lt;meta name=&quot;twitter:card&quot; content=&quot;summary_large_image&quot; /&gt;

&lt;meta name=&quot;twitter:site&quot; content=&quot;@old_sound&quot; /&gt;

&lt;meta name=&quot;twitter:creator&quot; content=&quot;@old_sound&quot; /&gt;

&lt;meta name=&quot;twitter:title&quot; content=&quot;The Power of Generative Metaphor&quot; /&gt;

&lt;meta name=&quot;twitter:description&quot; content=&quot;How metaphors condition problem setting.&quot; /&gt;

&lt;meta name=&quot;twitter:image&quot; content=&quot;http://alvaro-videla.com/images/generative_metaphor.jpeg&quot; /&gt;

&lt;h1 id=&quot;the-power-of-generative-metaphor&quot;&gt;The Power of Generative Metaphor&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;August 31 2018&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/generative_metaphor.jpeg&quot; alt=&quot;alt text&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In &lt;em&gt;Generative Metaphor: A Perspective on Problem-Setting in Social Policy&lt;/em&gt; Donald A. Schön presents the idea of how metaphors are central to the task of setting the frame in which we are going to make sense of a problem[1].&lt;/p&gt;

&lt;p&gt;According to Schön, the metaphor used to present a problem, sets the stage for how we are going to talk about the problem, how we are going to think about it, and therefore solve it. Let’s see an example.&lt;/p&gt;

&lt;h2 id=&quot;gossip-vs-epidemics&quot;&gt;Gossip vs Epidemics&lt;/h2&gt;

&lt;p&gt;Back in the ’80s researchers at the Xerox Palo Alto Research Center were trying to find a new way to &lt;a href=&quot;https://www.cis.upenn.edu/~bcpierce/courses/dd/papers/demers-epidemic.pdf&quot;&gt;replicate data for database maintenance&lt;/a&gt;[2]. They framed this problem under the idea of gossip. In this sense, computers spread rumors to other computers, in a similar fashion as how people do that, say, in an office setting. With this metaphor, we can easily explain how the algorithm disseminates data, since while we are not in the business of spreading rumors, we have experience of seeing others do it ;-)&lt;/p&gt;

&lt;p&gt;So here, gossip works as a generative metaphor that helps frame the problem, and reason about it. But there is a problem with it. While gossip makes it really easy to talk about the problem, it makes it hard to formally reason about it. These researcher found a new way to frame the problem: epidemics.&lt;/p&gt;

&lt;p&gt;Thanks to epidemics they found that there was a &lt;a href=&quot;https://www.amazon.com/Mathematical-Theory-Epidemics-Norman-Bailey/dp/0852641133&quot;&gt;Mathematical Theory of Epidemics&lt;/a&gt;, which they could use to formally reason about their algorithms. Also it provided a framework that was also useful for explaining their algorithms.&lt;/p&gt;

&lt;p&gt;So there we have two generative metaphors that helped frame a problem, guided their solutions, and in one case, even brought for free a mathematical theory to help reason about the problem.&lt;/p&gt;

&lt;p&gt;Are there other interesting instances of Generative Metaphors?&lt;/p&gt;

&lt;h2 id=&quot;the-guesthost-relationship&quot;&gt;The Guest/Host Relationship&lt;/h2&gt;

&lt;p&gt;In Human Centered Design there’s the idea of the &lt;a href=&quot;http://www.eamesoffice.com/the-work/the-guest-host-relationship/&quot;&gt;Guest/Host&lt;/a&gt; relationship[3]. This generative metaphor induces you to think of a design problem, be it a new product, or an software API, around the question of &lt;em&gt;how do you treat people when you invite them to your home&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;This idea was presented by the designer Charles Eames during an interview:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;[…] a very good, thoughtful host, all of whose energy goes into trying to anticipate the needs of his guests. […] We decided that this was an essential ingredient in the design of a building or a useful object.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This generative metaphor allowed them to frame their problems from that point of view. Now try to image what would happen if we use this mindset when we build the APIs that conform our software. How welcoming are them? How much do they try to anticipate the needs of our future users? Would our users need to find workarounds to these APIs so they can get things done? Think about the word work-around. It immediately sounds like something we wouldn’t want to deal with when we use someone else’s API.&lt;/p&gt;

&lt;h2 id=&quot;the-idea-of-the-labyrinth&quot;&gt;The Idea of the Labyrinth&lt;/h2&gt;

&lt;p&gt;Recently I &lt;a href=&quot;https://increment.com/documentation/notes-on-the-synthesis-of-labyrinths/&quot;&gt;wrote&lt;/a&gt; about how we can frame the process of writing software as that of traversing a labyrinth[4]. We explore many avenues, try many ideas, but there’s just one path that’s materialized in the code. All the other paths get discarded along the way. This causes that we lose the reasoning behind some decisions that are implicit in the code. Later on when we want to understand that code, we end up scratching our heads, because what led to those decisions is not entirely clear by reading the code alone.&lt;/p&gt;

&lt;p&gt;On the other hand if we understand this process as labyrinthine, then we can see how it is necessary to explain to our colleagues the paths that we traversed, but that didn’t make it to the code, to the final documentation, or the report.&lt;/p&gt;

&lt;h2 id=&quot;language-vs-tool&quot;&gt;Language vs. Tool&lt;/h2&gt;

&lt;p&gt;Finally I want to set the stage for a next article I’d like to write. About what are the implications behind thinking of Programming Languages as Languages vs. seeing them as &lt;em&gt;Tools&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;We are so used to call them Programming &lt;em&gt;Languages&lt;/em&gt; that we don’t stop to reflect what are the implications of treating them as languages, and where the language metaphor breaks off. But again, I just wanted to get the ball rolling, the match will be played in a future article.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;Here I presented just a few examples that show how metaphors let us talk and think about problems under the limits set by the framework that’s implicit in the metaphor. It would be an interesting exercise to try to find what are the metaphors that guide our day to day programming. Just to give one last example, what are the implications of calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git-blame&lt;/code&gt;, &lt;a href=&quot;https://twitter.com/Una/status/911767625864286208&quot;&gt;git blame&lt;/a&gt;?&lt;/p&gt;

&lt;p&gt;If you would like to read more about Generative Metaphor, I’d recommend the paper by Schön cited above.&lt;/p&gt;

&lt;h2 id=&quot;references&quot;&gt;References:&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Schön, D. 1979. Generative Metaphor: A Perspective on Problem-Setting in Social Policy. Metaphor and Thought. Cambridge University Press.&lt;/li&gt;
  &lt;li&gt;Demers, A., Greene, D., Hauser, C., Irish, W., Larson, J. 1987. Epidemic algorithms for replicated database maintenance. Proceedings of the Sixth Annual ACM Symposium on Principles of Distributed Computing: 1–12.&lt;/li&gt;
  &lt;li&gt;Eames, C. 1972. The Guest/Host Relationship. &lt;a href=&quot;http://www.eamesoffice.com/the-work/the-guest-host-relationship/&quot;&gt;http://www.eamesoffice.com/the-work/the-guest-host-relationship/&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;Videla. A. 2018. Notes on the synthesis of labyrinths. Increment Magazine.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;credits&quot;&gt;Credits:&lt;/h2&gt;

&lt;p&gt;Photo by &lt;a href=&quot;https://www.flickr.com/photos/flacoporta/&quot;&gt;Daniel Porta&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>A Veritable System of Intercodical Relations</title>
   <link href="http://videlalvaro.github.com/2018/08/a-veritable-system-of-intercodical-relations.html"/>
   <updated>2018-08-24T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2018/08/a-veritable-system-of-intercodical-relations</id>
   <content type="html">&lt;meta name=&quot;twitter:card&quot; content=&quot;summary_large_image&quot; /&gt;

&lt;meta name=&quot;twitter:site&quot; content=&quot;@old_sound&quot; /&gt;

&lt;meta name=&quot;twitter:creator&quot; content=&quot;@old_sound&quot; /&gt;

&lt;meta name=&quot;twitter:title&quot; content=&quot;A Veritable System of Intercodical Relations&quot; /&gt;

&lt;meta name=&quot;twitter:description&quot; content=&quot;How humans interpret the interplay between different codes to create meaning.&quot; /&gt;

&lt;meta name=&quot;twitter:image&quot; content=&quot;http://alvaro-videla.com/images/kimmy/01.png&quot; /&gt;

&lt;h1 id=&quot;a-veritable-system-of-intercodical-relations&quot;&gt;A Veritable System of Intercodical Relations&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;August 24 2018&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;In the episode &lt;a href=&quot;https://en.wikipedia.org/wiki/List_of_Unbreakable_Kimmy_Schmidt_episodes#Season_2_%282016%29&quot;&gt;“Kimmy Goes on a Playdate!”&lt;/a&gt; from Netflix’s TV Series &lt;em&gt;&lt;strong&gt;Unbreakable Kimmy Schmidt&lt;/strong&gt;&lt;/em&gt;, there’s a couple of interesting bits that can lead us to reflect on how we humans learn about interpreting codes, and thus we can extract meaning from text in different contexts.&lt;/p&gt;

&lt;p&gt;Understanding when context changes the meaning of a text is a task at which we humans seem to be pretty good at, but I wonder if “Machine Learning”, would be able to learn contextualization of text in a similar fashion (the definition of ML is left as an exercise for the reader), but this is a question for a different article.&lt;/p&gt;

&lt;h2 id=&quot;breaking-kimmy-schmidt-apart&quot;&gt;Breaking Kimmy Schmidt Apart&lt;/h2&gt;

&lt;p&gt;If you are unfamiliar with the show, here’s all you need to know to follow along: &lt;strong&gt;Kimmy Schmidt&lt;/strong&gt; lives in New York after escaping a doomsday cult from &lt;em&gt;&lt;strong&gt;Reverend Richard Wayne Gary Wayne&lt;/strong&gt;&lt;/em&gt;. She works for &lt;em&gt;&lt;strong&gt;Jacqueline Voorhees&lt;/strong&gt;&lt;/em&gt; where among many things, she takes care of Jacqueline’s step daughter &lt;em&gt;&lt;strong&gt;Xanthippe&lt;/strong&gt;&lt;/em&gt;. This is as much context as you need to understand what follows.&lt;/p&gt;

&lt;h2 id=&quot;codes-in-action&quot;&gt;Codes in Action&lt;/h2&gt;

&lt;p&gt;In episode two of the second season, Kimmy is about to destroy Xanthippe’s lifestyle by making her go back to her previous life with her real mother. Xanthippe gets angry and tell’s Kimmy how bad she is, which makes Kimmy reflect and realize that she’s behaving exactly like the reverend by ruining someone else’s life.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/kimmy/01.png&quot; alt=&quot;alt text&quot; title=&quot;Kimmy realizes she’s like the reverend — Unbreakable Kimmy Schmidt — Season 2, Episode 2.&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In denial, Kimmy then thinks that she’s not like the reverend:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/kimmy/02.png&quot; alt=&quot;alt text&quot; title=&quot;Kimmy is in denial — Unbreakable Kimmy Schmidt — Season 2, Episode 2.&quot; /&gt;&lt;/p&gt;

&lt;p&gt;But here is where the interesting thing happens: when the frame changes as the camera follows Kimmy we see a wall picture with the text “YES YOU ARE”.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/kimmy/03.png&quot; alt=&quot;alt text&quot; title=&quot;'Yes You Are' wall picture — Unbreakable Kimmy Schmidt — Season 2, Episode 2.&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Despite nothing being explicitly uttered at this point in the episode, what mechanisms are in place that let us understand that the text in that wall picture is relevant to what’s happening in the scene? (Besides the obvious question: are non English speakers watching this show able to understand the implications of that wall picture?)&lt;/p&gt;

&lt;p&gt;Later in the episode the same wall picture appears, but this time even though it has the same text written on it, the text is completely irrelevant. So how do we know that in this instance we must ignore the text in the wall picture?&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/kimmy/04.png&quot; alt=&quot;alt text&quot; title=&quot;Wall picture in the background — Unbreakable Kimmy Schmidt — Season 2, Episode 2.&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In his introduction to Literary Theory, Terry Eagleton asks the following question: &lt;em&gt;What is involved in the act of reading&lt;/em&gt;?&lt;/p&gt;

&lt;p&gt;While this seems a rather simple question, think about a sign at the London Tube saying: “&lt;em&gt;Dogs must be carried on escalator&lt;/em&gt;”. How do we understand that this particular sign doesn’t many any of what follows:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;You must carry a dog in the escalator&lt;/li&gt;
  &lt;li&gt;You are going to be banned from the escalator unless you find a stray dog to carry.&lt;/li&gt;
  &lt;li&gt;“Carried” is to be taken metaphorically and help dogs get through life&lt;/li&gt;
  &lt;li&gt;How do I know this is not a decoration.&lt;/li&gt;
  &lt;li&gt;I need to understand that the sign has been placed there by some authority Conventions: I understand that “escalator” means this escalator and not some escalator in Paraguay.&lt;/li&gt;
  &lt;li&gt;“Must be” means must be now.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Eagleton writes:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;To read at all, we need to be familiar with the literary techniques and conventions which a particular work deploys; we must have some grasp of its ‘codes’, by which is meant the rules which systematically govern the way it produces its meanings.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What are these codes? In his book about Semiotics, Daniel Chandler says:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Codes organize signs into meaningful systems […] Since the (intended) meaning of a sign depends on the code which it is situated, codes provide a framework within which signs make sense.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Think about a traffic light. If we are not familiar with their code, we cannot understand that a red light means ‘Stop!’, explains Chandler.&lt;/p&gt;

&lt;p&gt;When we watch a TV Show we are familiar with the various codes that play together so we understand the film at several levels. Think of the typical movie scene when a person is missing the love of their life: we might have rain falling over the protagonist, sad music sounding in the background. In this case visual codes, like the rain, are mixed with music, which involve a total different code. We are able to place them together and augment the meaning we extract from the scene.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;We read meaning into texts, drawing upon our existing knowledge and experience of signs, referents, and codes in order to make explicit what is only implicit.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;what-about-programming&quot;&gt;What about programming?&lt;/h2&gt;

&lt;p&gt;Is any of this semiotic babble somehow related to programming? We can ask ourselves what mechanisms do we put in place when we understand code? Consider this Java class:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;package com.example.store.user;
/**
 * Implements a user for our store.
 */
public class User {
    private String name;
    
    public User(String name) {
        this.name = name;
    }
    
    public String getName() {
        return this.name;
    }
    
    @Override
    public boolean equals(Object obj) {
        if (obj == null) return false;
        if (!(obj instanceof User))
            return false;
        if (obj == this)
            return true;
        return this.getName() == ((User) obj).getName();
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This program is a bit under 30 lines of code. There we use the word user as a package name, a class name, it’s part of a comment in English, and it’s used for type casting inside the equals method.&lt;/p&gt;

&lt;p&gt;To understand this program we need an understanding of the codes implicit in Java, to see that despite the word being the same in all those instances, its meaning is different. Also we mix codes, in this case when we have English comments with it’s own rules and syntax, mixed with those of Java.&lt;/p&gt;

&lt;p&gt;A mixture of codes happens in a more evident way in frameworks like React with its language JSX that mixes XML style templating with Javascript. While on a first impression React might seem to be mixing concerns by not separating presentation code from its logic, what is happening is that these intercodical relations between XML and Javascript cooperate to augment the information provided by a component created with React.&lt;/p&gt;

&lt;p&gt;When we understand a piece of programming code, we activate a series of mechanisms that help us remove ambiguity so we know what each word is trying to convey. In a program, we want to limit the possibles interpretation of the text, because we need to reason about the correctness of the program. Limiting the possible interpretation of a text requires the help of many devices that would guide its reading.&lt;/p&gt;

&lt;p&gt;In Java the keyword &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;class&lt;/code&gt; tells that what comes is the name of the class, while something like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;instanceof&lt;/code&gt; will tell us that we want to use the class name for type checking. A package name provide more clues about the purpose of the class, and so do comments.&lt;/p&gt;

&lt;p&gt;As Eagleton says:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;[…] adopting a reading convention which eradicates ambiguity, itself depends upon a whole network of social knowledge.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Understanding the mechanisms that we humans use for reading texts, can help us recognize areas where we could improve our programs helping others adopt the right reading conventions for the code we wrote.&lt;/p&gt;

&lt;h2 id=&quot;references&quot;&gt;References:&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Chandler, Daniel. Semiotics: The Basics, Third Edition. Routledge, 2017.&lt;/li&gt;
  &lt;li&gt;Eagleton, Terry. Literary Theory: an Introduction. Blackwell Publishing, 2015.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2 id=&quot;credits&quot;&gt;Credits:&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;The title of this blogpost is a reference to a quote from film theorist &lt;a href=&quot;https://en.wikipedia.org/wiki/Christian_Metz_%28critic%29&quot;&gt;Christian Metz&lt;/a&gt;. See Chandler’s book for more details.&lt;/li&gt;
  &lt;li&gt;All screenshots were taken from Netflix’s TV Show Unbreakable Kimmy Schmidt&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Lector in Codigo</title>
   <link href="http://videlalvaro.github.com/2018/05/lector-in-codigo.html"/>
   <updated>2018-05-07T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2018/05/lector-in-codigo</id>
   <content type="html">&lt;h1 id=&quot;lector-in-codigo&quot;&gt;Lector in Codigo&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;May 07 2018&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;You can download a PDF of the paper pre-print &lt;a href=&quot;http://alvaro-videla.com/pdfs/lector_in_codigo.pdf&quot;&gt;here&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In this article I want to explore the relation between the process of
writing computer programs with that of writing literary works of
fiction. I want to see what parallels can we trace from the ideas
presented by Umberto Eco in &lt;em&gt;Lector in Fabula&lt;/em&gt; and &lt;em&gt;Six Walks in the
Fictional Woods&lt;/em&gt;, with the way we write programs today.&lt;/p&gt;

&lt;p&gt;The goal of this article is to ask–and try to answer–the following
questions: what can we learn as programmers from literary theory? What
ideas can we incorporate from that discipline into our day to day
programming activities, so we write code that’s easier to understand for
other humans (or our future selves)?&lt;/p&gt;

&lt;h2 id=&quot;programming-as-a-humans-first-activity&quot;&gt;Programming as a Humans-First Activity&lt;/h2&gt;

&lt;p&gt;The need for computer programs that are easier to understand for humans
is not a new one. Since the early ’50s as programmers started to
distance themselves from machine code and began to program in higher
level languages, we began to see a need to write code that appeals not
only to machines but to humans as well. When David
Wheeler [25] introduced the idea of the subroutine to the
programming world, he said that:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;sub-routines are very useful–although not absolutely necessary–and
that the prime objectives to be born in mind when constructing them
are simplicity of use, correctness of codes and accuracy of
description.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So we see that &lt;em&gt;subroutines&lt;/em&gt; are not necessary for a program to work,
but they are a device that can help with program understanding among
other things.&lt;/p&gt;

&lt;p&gt;Almost a decade later an anonymous article was published in the
Datamation magazine called &lt;em&gt;What A Programmer Does&lt;/em&gt; [3], which
is surprising for how relatable those ideas are today. Its author is
also concerned with the way humans communicate knowledge to each other,
in the forms of programs. The money quote is this one:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;A programmer does not primarily write code; rather, he primarily
writes to another programmer about his problem solution.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Later they reaffirm their point by saying:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Both the value and quality of a programmer’s work improve directly
with the importance he places on communicating his program to a human,
rather than merely to the machine.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We could say that this sentiment is finally echoed in its most popular
form in Structure and Interpretation of Computer Programs
(SICP) [2] when their authors say:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Programs must be written for people to read, and only incidentally for
machines to execute.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is a strong statement, elevating humans as the main recipients of
computer programs. Computers becoming mere tools that are able to run
those programs at a speed that’s useful for our practical purposes.&lt;/p&gt;

&lt;p&gt;These ideas, presented by programmers for programmers, contrast with the
ways programs are being seen by the industry and by the public in
general. That is to say, we program because we expect results from the
programs we produce. Whether it’s accounting, or finding the route to
hour hotel in a foreign city, most consumers of programs use them
because of the results they produce. In fact, we could argue that a user
will be quite displeased if they find themselves lost after following
the routes offered by their GPS, even if customer support arguments that
the code for their GPS software looks really pretty. We could say that
this practicality has placed more emphasis in the results produced by
programs than in how well they read for other people. When the goal is
to meet a client’s deadline, then the results produced by a program,
it’s &lt;em&gt;visible output&lt;/em&gt;, is what matters (unless in cases where we are
selling the software itself, where the end consumers will be other
programmers).&lt;/p&gt;

&lt;p&gt;This last point shows us that if we want to advocate for writing
programs that other programmers can understand, then we need to
understand that we need to accommodate these ideas in the software
industry, an industry where the results produced by the programs is in
constant tension with the quality in which those programs are produced,
usually due to budget requirements. If we want to advocate for programs
that are easier to understand, we need not forget that while programmers
might want that as well, there are other forces at play that may compel
programmers to declare their programs to be ready for delivery as soon
as they produce the expected outputs, rather than investing time in
tasks like refactoring. I said &lt;em&gt;expected outputs&lt;/em&gt; because talking about
&lt;em&gt;correct outputs&lt;/em&gt; would be the topic of a complete different discussion.
Having said that, the rest of this article assumes that we share the
preoccupation of the authors of &lt;em&gt;SICP&lt;/em&gt;, of the anonymous author of &lt;em&gt;What
A Programmer Does&lt;/em&gt;, and that of David Wheeler, among others. Who else
was interested in these ideas? Donald Knuth.&lt;/p&gt;

&lt;h2 id=&quot;literate-programming&quot;&gt;Literate Programming&lt;/h2&gt;

&lt;p&gt;In Literate Programming Donald Knuth [18] wrote about the
attitude we should have when we write programs: “instead of imagining
that our main task is to instruct a computer what to do, let us
concentrate rather on explaining to human beings what we want a computer
to do”. He goes on in his paper explaining the WEB system for writing
software documentation, which we could say has been partially adopted by
the industry in tools like Java Docs and the like.&lt;/p&gt;

&lt;p&gt;So while we have systems that can help us write documentation, we still
have the problem of how to effectively transfer our tacit knowledge
about the system to other programmers. Literate Programming is a good
step towards placing emphasis on documentation and communication, but
still doesn’t explain how we could accomplish that goal. It’s
interesting at least that he points at literature as a place where we
could learn techniques for writing programs that are easier to
understand for other humans.&lt;/p&gt;

&lt;p&gt;Here we need to address a difference between writing (as in literature),
and programming that’s been raised by papers like &lt;em&gt;Programming is
Writing is Programming&lt;/em&gt; by Herman and Aldewereld [16]. There
they say:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;a difference between writing and programming, [is that] in
programming, the programmer gets feedback very early on whether the
program text is executable, during compiling. Furthermore, they get
feedback on whether the program is working as intended.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One could argue that while this is true, knowing that a program is
working as intended doesn’t indicate that we know what the program
actually means, that is, what process from the real world is trying to
represent, what problem is trying to solve, neither is telling us how
said problem has been solved.&lt;/p&gt;

&lt;p&gt;We could compare this with playing music on a guitar by reading the
notes as they appear on a staff. In the guitar an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;E5&lt;/code&gt; could be played
on the 12th fret on the 6th string, on the 7th fret on the 5th string
and on the 2nd fret on the 4th string. Since all these finger positions
produce the same sound, we could say the music was executed as written
in the staff, but this is not sufficient to consider it correct playing.
As Carlevaro explains in his &lt;em&gt;Serie Didáctica para
Guitarra&lt;/em&gt; [6]:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Correct guitar playing is unconceivable without correct fingering.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So it’s not only is important the result produced by a function, or the
musical note emitted by an instrument, but also it’s important how these
results were produced.&lt;/p&gt;

&lt;p&gt;As Knuth writes in The Art of Computer Programming [19], is a
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;random&lt;/code&gt; function that returns the number &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt; as result, a valid
implementation? Equally we could ask: is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;square&lt;/code&gt; function that
returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;25&lt;/code&gt; a correct implementation? As long as the input values for
the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;square&lt;/code&gt; function happen to be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;5&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-5&lt;/code&gt;, then the implementation
appears to be correct.&lt;/p&gt;

&lt;p&gt;While this example might sound like a stretch, TDD [4] advocates
to start writing stub functions that behave exactly like this, providing
the minimum implementation that could yield a correct result for a
particular test. This kinds of testing presents the problem of needing
to submit a program to every kind of possible input to show that it
works as expected. In the words of Dijkstra [9]:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Program testing can be used to show the presence of bugs, but never to
show their absence!.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Tools like QuickCheck [7] try to alleviate this problem by
generating random input for tests, while at the same time presenting
techniques like &lt;em&gt;Property Based Testing&lt;/em&gt;. So while we are creating
better tools to prove the correctness of programs, we are still left to
our own means when it comes to understanding what a program is doing.&lt;/p&gt;

&lt;p&gt;Does this means we should consider every program as a &lt;em&gt;work of
literature&lt;/em&gt; like Knuth says in his paper? In Cybertext: Perspectives on
Ergodic Literature, Espen J. Aarseth presents a different point of
view [1]:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;[...] a search for literary value in texts that are neither
intended nor structured as literature will only obscure the unique
aspects of these texts and transform a formal investigation into an
apologetic crusade.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I concur with Aarseth. Trying to find literary value in programs seems
to be a task destined to fail, because programs aren’t written with a
literary goal. That doesn’t mean there isn’t any value in thinking of
programs as literature. I think there is, but in a different way. We
should look at literature and learn from the techniques authors use when
writing fictional works. This means we should go to the Humanities side
of the building and learn from Literature Theory, Linguistics and
Semiotics and incorporate some of their techniques into our day to day
activities as programmers.&lt;/p&gt;

&lt;h2 id=&quot;knowledge-sharing&quot;&gt;Knowledge sharing&lt;/h2&gt;

&lt;p&gt;Before we delve into the various ideas from Literary Theory that could
help us improve the way we write our programs to we share knowledge
between programmers, we need to ask ourselves &lt;em&gt;what&lt;/em&gt; kind of knowledge
are we trying to share. What is this &lt;em&gt;meaning&lt;/em&gt; that programs should
convey and that me as a programmer reading a certain piece of code
should be able to understand.&lt;/p&gt;

&lt;p&gt;In &lt;em&gt;Programming as Theory Building&lt;/em&gt; Peter Naur [20] draws our
attention towards the idea of a &lt;em&gt;Theory&lt;/em&gt; based on Gilbert Ryle’s &lt;em&gt;The
Concept of Mind&lt;/em&gt; [21]. Naur explains the knowledge a software
engineer builds about a system in the following terms:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;[...] a person who has or possesses a theory in this sense knows
how to do certain things and in addition can support the actual doing
with explanations, justifications, and answers to queries, about the
activity of concern.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;and he continues:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;[...]what has to be built by the programmer is a theory of how
certain affairs of the world will be handled by, or supported by, a
computer program.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So from the moment we do requirements gathering, to how we implement
those requirements in code, we are building a theory that tacitly
includes the different tradeoffs we took along the way. For example: why
are we using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Long&lt;/code&gt; instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Integer&lt;/code&gt; for the numbers we use in
certain class; why the time resolutions is in nanoseconds and not
milliseconds; why we added a caching layer to avoid network roundtrips;
why we decided to implement our own library for problem X instead of
using what was available on the market; why our key value data structure
is backed by a tree instead of an array; why we decided to not add a
layer of abstraction between the database and its query language; and so
on. Of course the list is not exhaustive. Some answers to those
questions might be clearly obtained by reading the code, like for
example: let’s say there’s just one SQL query in the whole project. So
we could argue there was no need for a library that abstracted the
database away.&lt;/p&gt;

&lt;p&gt;In this article we are interested about those instances where knowledge
cannot be immediately obtained from the source code, its tests and
documentation, or any of the other artifacts produced by the code. There
is where we want to go and look for help in Literary Theory,
Linguistics, and related disciplines, so we see how we could improve the
way we write software.&lt;/p&gt;

&lt;p&gt;When we try to read code written by other programmers or by a past
version of ourselves, we are presented with the task of reverse
engineering that code. We start by trying to understand what the
original programmers tried to solve with that program. It doesn’t matter
if this is legacy code or if we just landed on a team and we are handed
with the next issue on the bug tracker. We will be faced with some code
and we will have to read it and understand it so we can proceed with the
confidence that we are modifying it in the right way, or adding the new
feature in the right place, so we solve the problem at hand.&lt;/p&gt;

&lt;p&gt;The problem with this approach is that it places the burden of
understanding the code in the reader. The reader has to be able to
decipher the code, but the difference with cryptography is that we are
not adversaries, but members of the same team, so understanding code
shouldn’t be a challenge, but a matter of collaboration.&lt;/p&gt;

&lt;p&gt;Imagine if every time we tried to read a book, we had to play code
breakers? Unless we were reading the Finnegans Wake, I’m sure that
wouldn’t be a enjoyable reading experience. In the case of programming,
unless we have are in the business of reverse engineering code, then we
shouldn’t have the need to play Finnegans Wake every time we are faced
for the first time with a program.&lt;/p&gt;

&lt;p&gt;So what can we do as programmers to help others understand our code?&lt;/p&gt;

&lt;h2 id=&quot;the-encyclopedia&quot;&gt;The Encyclopedia&lt;/h2&gt;

&lt;p&gt;In &lt;em&gt;Lector in Fabula&lt;/em&gt; [13] and &lt;em&gt;Six Walks in the Fictional
Woods&lt;/em&gt; [11] Eco introduces the idea of the &lt;em&gt;Encyclopedia&lt;/em&gt;, which
comprises all the knowledge in the world. It’s safe to assume that no
human posses that knowledge. We have our own limited &lt;em&gt;encyclopedias&lt;/em&gt; (in
lowercase). Whenever we try to interpret a text we bring our own
encyclopedia into the game and we rebuild its contents, we actualize it
according to our own competence. Consider this very short story:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;I sat in front of my computer and started coding.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When we read a text like in the previous example, we make a lot of
choices when it comes to actualize that text. If we imagine the
programmer typing their program on a laptop or a on a keyboard connected
to a computer, it doesn’t matter, unless the device used is
consequential to the rest of the story. I’m used to write programs on a
laptop, so most probably I will image that scene with a person typing
their program on that kind of computer. What’s the programmer’s gender?
Are their blond or have dark hair? For some readers that doesn’t matter,
so they don’t even think about that, while other people might render
them according to their own preferences or biases. The lesson to take
home from this, is that &lt;em&gt;we fill in the blanks on a story using what’s
available on our own encyclopedia&lt;/em&gt;. This opens a very interesting
question of how we as authors manage to transfer our ideas from our
minds to our communication recipients or &lt;em&gt;destinataries&lt;/em&gt;. If we fail at
that, the other person might end up building a different interpretation
of what the code does.&lt;/p&gt;

&lt;p&gt;A key idea from &lt;em&gt;Lector in Fabula&lt;/em&gt; is Eco’s criticism of communication
theory, he says:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;[...] the competence of the recipient is not necessarily that of
the sender.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;and later he adds:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Therefore, in order to “decode” a verbal message, in addition to
linguistic competence, a differently circumstantial competence, an
ability to trigger presuppositions, to repress idiosyncrasies, etc.,
and so on.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So how do we go about understanding the &lt;em&gt;target audience&lt;/em&gt; for our
programs? Eco has an answer for that as well, the &lt;em&gt;Model Reader&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;the-model-reader&quot;&gt;The Model Reader&lt;/h2&gt;

&lt;p&gt;The &lt;em&gt;Model Reader&lt;/em&gt; is not the empirical reader (not you, or me). It’s a
reader that lives in the mind of the author, which the author builds as
they write their story. This model reader will help the author decide
how much detail is required in their work, so the empirical readers are
able to understand it. Let’s go back to our one sentence story:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;I sat in front of my computer and started coding.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In that text I’ve made some choices about how much info I wanted to
convey based on my Model Reader; in this case I assumed that I don’t
need to explain that I’m using a keyboard to type the program, because
in most cases, that’s how it’s done. In that one sentence story, my
Model Reader is a person that’s familiar with how computers work, and
with how we type programs in a computer today.&lt;/p&gt;

&lt;p&gt;To show how important the model reader is, let’s imagine we see a sign
at the London Underground system that says: &lt;em&gt;dogs must be carried on
escalator&lt;/em&gt;. In &lt;em&gt;Literary Theory: an Introduction&lt;/em&gt; [10], Terry
Eagleton, asks the following question about that simple sentence:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;Does it mean that you must carry a dog in the escalator?&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Are you going to be banned from the escalator unless you find a
stray dog to carry?&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;“Carried” is to be taken metaphorically and help dogs get through
life?&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;How do I know this is not a decoration?&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;I need to understand that the sign has been placed there by some
authority Conventions: I understand that “escalator” means this
escalator and not some escalator in Paraguay.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;“Must be” means must be now.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So we see that such a simple sentence like “dogs must be carried on
escalator”, implies a Model Reader that understands that if they are
using the London Underground system, and they bring a dog, the must
carry that dog if they have to use an escalator. All that information,
while not provided in the text, was implied when the designers of the
sign chose their Model Reader.&lt;/p&gt;

&lt;p&gt;So one aspect of the idea of the Model Reader revolves around how much
information or context do we provide in the text, so the message we are
trying to convey is understandable. Another aspect is that, as the story
progresses the author also builds the Model Reader.&lt;/p&gt;

&lt;p&gt;Now let’s imagine my previous text went as follows:”I sat in front of my
computer and started coding. The clouds outside my window had cleared,
revealing the Mars landscape.” Once the word &lt;em&gt;Mars&lt;/em&gt; appears in the text,
we can easily tell that what seemed to be a personal recollection (about
me typing into a computer), turned out to be a science fiction story
about space exploration. With that small clue (Mars) the author built a
Model Reader that must accept that the story is fictional, since so far
we don’t have humans living in Mars.&lt;/p&gt;

&lt;p&gt;In &lt;em&gt;Lector in Fabula&lt;/em&gt;, Eco goes deeper into the idea of the Model Reader
(and the Model Author), to present the concept of textual cooperation.
There he says:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;A text is a lazy (or economic) mechanism that lives on the surplus
value of meaning introduced by the recipient [...]&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Reading is essentially a work of cooperation between the author and the
reader.&lt;/p&gt;

&lt;p&gt;To summarize, the author uses a Model Reader in order to decide how to
tell the story, both in how much detail they need to provide, and on how
to give clues to tell the reader this is a noir novel, or this is epic
fantasy, or everything that was said so far &lt;em&gt;was part of a dream so be
careful if you keep reading, since what comes next might still be part
of a dream&lt;/em&gt;. The Model Reader is someone that’s willing to cooperate
with the author. The Model Reader actualizes the text.&lt;/p&gt;

&lt;p&gt;The question is how does the concept of the Model Reader can help us
think about how we write programs? Who is our Model Reader? Is it the
computer? Is it another human, or a future me?&lt;/p&gt;

&lt;h2 id=&quot;a-programmers-model-reader&quot;&gt;A Programmer’s Model Reader&lt;/h2&gt;

&lt;p&gt;As I wrote in the introduction to this article, a striking difference
between writing programs and writing literature is that most of the time
programs are written for computers to execute, not for humans to find
the development of plots and storylines like one would expect from a
work of fiction. As we already argued in this article, when we are
writing software we are trying to satisfy some very practical needs,
trying to address some human problem. So how can we integrate these two
seemingly opposing points of view?&lt;/p&gt;

&lt;p&gt;We can try to answer that question by pondering who is the Model Reader
when we write programs? Who do we keep in mind while we type our code?
The first answer that comes to mind is the compiler. We write code
trying to follow all the syntactical rules of a specific programming
language, while providing all the necessary clues for it to find the
definitions of the functions and types used in the program. We can say
that when we program “we play computer” in our heads. Since today almost
nobody writes code for a specific computer architecture, we can say that
we build a model computer in our minds, and then we try to second guess
how it will run our code based on our assumptions about that ideal
computer. So our Model Reader fluctuates between trying to satisfy a
compiler and an imaginary computer.&lt;/p&gt;

&lt;h2 id=&quot;a-cooperation-game&quot;&gt;A Cooperation Game&lt;/h2&gt;

&lt;p&gt;The idea of playing computer in our heads is interesting because it
relates to Eco’s idea of producing text as a game of strategy. To win in
a game of strategy we build a model opponent and use that model to try
to anticipate their moves, so we can put in place a strategy that help
us win the game. Eco says that in a way at Waterloo, Wellington built a
more accurate model of Napoleon.&lt;/p&gt;

&lt;p&gt;Let’s forget about fighting and let’s think in terms of cooperation,
let’s think on how we build strategies that can help the recipients of
the text actualize it. In the words of Eco:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;A text wants someone to help it work.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;How can we help a computer/compiler to actualize a text, so it’s able to
bring the code to life, in the same way we do when we read fiction?
Borrowing from literature we can say we do world building when we
declare types, or when we define interfaces in header files. Later we
tell the compiler where to find the headers that define the interfaces
used in our programs. Whenever we use names, like variables, we usually
have to declare them first. In strong-typed languages, we even assign
types to those variables, which help us reason about those programs, but
also give clues to the compiler about what to do whenever they find
those datatypes further down in the program. So on one hand a variable
name, or its type, appeals to the reader to access their encyclopedia to
try to understand what that variable represents, on the other hand, when
we define those variables, or specify their types, we are building said
reader as the text is built.&lt;/p&gt;

&lt;p&gt;Still, we keep seeing this duplicity of writing for the computer, and
writing for a human reader. Again, Espen Aarseth raises an interesting
point in his book about cybertext:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Programs are normally written with two kinds of receivers in mind: the
machines and other programmers. This gives rise to a double standard
of aesthetics, often in conflict: efficiency and clarity.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This poses an interesting question, which can be related to Eco’s idea
of the different levels of readers.&lt;/p&gt;

&lt;h2 id=&quot;different-levels-of-readers&quot;&gt;Different Levels of Readers&lt;/h2&gt;

&lt;p&gt;In the essay &lt;em&gt;Intertextual Irony and Levels of Reading&lt;/em&gt; [12],
Umberto Eco writes that texts that have an aesthetic aim tend to
construct several levels of Model Readers. In the first level, the
reader just wishes to finish the story, to know what happens, to know
how it ends. The second level reader is a semiotic or aesthetic reader,
who wants to know how what happens has been narrated. To become a second
level reader, one has to read a story many times.&lt;/p&gt;

&lt;p&gt;I want to posit that we humans are the second level readers of our
programs. We are the ones that want to know how what happens in the code
has been narrated.&lt;/p&gt;

&lt;p&gt;The question is then how do we manage to interpret a text, and add
meaning to it, understanding its intended goal? And how we as
authors/programmers can help others understand our code? Let’s talk
about metaphors.&lt;/p&gt;

&lt;h2 id=&quot;metaphors&quot;&gt;Metaphors&lt;/h2&gt;

&lt;p&gt;In &lt;em&gt;Metaphors We Compute By&lt;/em&gt; [24] I wrote that code is a
metaphor for a solution we found. A program is a translation into code
of that problem solution. The explanatory power of that code, how well
the solution is transmitted, is the measure of its elegance.&lt;/p&gt;

&lt;p&gt;I will not repeat the whole argument presented in that article, but the
gist of it is that if we have a collection of elements that need to be
unique, a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set&lt;/code&gt; data structure has more explanatory power than an
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt;, even tho an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Array&lt;/code&gt; would fit the job just fine, because the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Set&lt;/code&gt; implies element uniqueness. A second point is the power metaphors
have for obscuring and augmenting our knowledge. In &lt;em&gt;Epidemic algorithms
for replicated database maintenance&lt;/em&gt; Demers et al. [8] explain
how &lt;em&gt;gossip&lt;/em&gt; was a good metaphor for understanding the replication
technique they were proposing, but it wasn’t until they come to the
realization that it mapped quite well to the idea of &lt;em&gt;epidemics&lt;/em&gt; and how
they spread in a population. This realization helped them wield all the
mathematical theory behind the prevention of epidemics’ spread.&lt;/p&gt;

&lt;p&gt;As Peter Gärdenfors says in &lt;em&gt;Geometry of Meaning: Semantics Based on
Conceptual Spaces&lt;/em&gt; [15]:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Metaphorical mappings preserve the the cognitive topology of the
source domain in a way consistent with the inherent structure of the
target domain. Metaphors transfer information from one conceptual
domain to another. What is transferred is a pattern rather than domain
specific information. A metaphor can thus be used to identify a
structure in a domain that would not have been discovered otherwise.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So metaphors help with understanding, which means we could leverage
their power when choosing the abstractions we use in our code. The
understanding of who the Model Readers of our code are, will serve as
guide when needing to choose the right metaphor. For example if we
implement the exponentiation algorithm presented in section 1.2.4 of
Structure and Interpretation of Computer Programs [2], should
we do it by allowing our function to just accept &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;integers&lt;/code&gt;, or should
we go for an abstraction like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Monoids&lt;/code&gt; so the code would work for other
data types that provide an associative binary operation and identity,
like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Strings&lt;/code&gt; and concatenation, or a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Matrix&lt;/code&gt; and exponentiation? (See
&lt;em&gt;Elements of Programming&lt;/em&gt; by [23] for a discussion on how to
evolve the exponentiation algorithm for from Integers to Monoids).&lt;/p&gt;

&lt;p&gt;What other tool could we bring from literature that could help us write
programs that are easier to understand? Enter paratexts.&lt;/p&gt;

&lt;h2 id=&quot;paratexts&quot;&gt;Paratexts&lt;/h2&gt;

&lt;p&gt;In literature there’s the idea of the &lt;em&gt;Paratext&lt;/em&gt;, which Eco quoting
Gérard Genette defines as:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;[...] the “paratext” consists of the whole series of messages that
accompany and help explain a given text–messages such as
advertisements, jacket copy, title, subtitles, introduction, reviews,
and so on.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Genette adds the following to his definition of paratext[14]:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;[a paratext is] a privileged place of a pragmatics and a strategy,
of an influence on the public, an influence that–whether well or
poorly understood and achieved–is at the service of a better
reception for the text and a more pertinent reading of it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;An obvious instance of paratexts in literature could be found in old
books, like Don Quixote, where chapter titles are followed by text like
this:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Chapter I. Which treats of the character and pursuits of the famous
gentleman Don Quixote of La Mancha.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Do we have these kinds of paratext in code? Yes, as said earlier, we not
only specify imports, we arrange the code in modules, packages and
libraries. We specify flags for the compiler that can change the way the
software is built (for example changing a library’s location). In
languages like Haskell we could add pragmas to the source code that
could change the meaning of the program or even its efficiency. This
means that as we write code, we are not only targeting our model
computer, we are also targeting a specific compiler. We are trying to
provide it with information so it’s able to understand what it should do
when it finds code stating that the variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;isActive&lt;/code&gt; is of the type
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AtomicBoolean&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We also need to consider how paratexts helps us humans understand
programs. When we read &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;utils&lt;/code&gt; as part of a package name we build a
different set of expectations about the contents of that package from
when we read &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;network&lt;/code&gt;, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;persistence&lt;/code&gt;. A class name like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FileSystem&lt;/code&gt;
is telling us a lot of things about what to expect in its API. Finding a
public method called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stringCompare&lt;/code&gt; inside it will feel very strange.
Code comments are probably one of the most important paratexts that can
help other programmers understand our code. Keeping them in sync with
the code is a whole different problem. Not even Cervantes escaped this
fate: in Don Quixote, the original description for Chapter X doesn’t
match the contents of the chapter!&lt;/p&gt;

&lt;p&gt;To understand how important paratexts are, consider what Genette says
about them:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;To indicate what is at stake, we can ask one simple question as an
example: limited to the text alone and without a guiding set of
directions, how would we read Joyce’s Ulysses if it were not entitled
Ulysses?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s try to visualize this with a simple code example. Consider the
following class representing a user:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;class User {
    String username;
    String password;
    String role;
    
    User(String username, String password, String role) {
        this.username = username;
        this.password = password;
        this.role     = role;
    }
    
    public String getUsername() {return username;}
    public String getPassword() {return password;}
    public String getRole()     {return role;}
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The code of this class doesn’t offer enough information to tell us what
is the class purpose. Now let’s consider the following test case:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;User user = new User('alice', 'secret', 'admin');
assertEquals(user.getUsername(), 'alice');
assertEquals(user.getPassword(), 'secret');
assertEquals(user.getRole(), 'admin');
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The previous test can give us feedback about the code working as
expected, but we are still in the dark about what is this class purpose,
that is, what concept of the real world this class is trying to
represent.&lt;/p&gt;

&lt;p&gt;Now let’s see the following class:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;package database;

class User {
    String username;
    String password;
    String role;
    
    User(String username, String password, String role) {
        this.username = username;
        this.password = password;
        this.role     = role;
    }
    
    public String getUsername() {return username;}
    public String getPassword() {return password;}
    public String getRole()     {return role;}
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The only difference with the previous code is the line saying
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;package database&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The mere fact that we include a paratext telling us which package this
class belongs to, tells us immediately that this class serves a
different purpose from another &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;User&lt;/code&gt; class that could appear inside the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;model&lt;/code&gt; package. The latter could represent a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;User&lt;/code&gt; in the system, say
our social network website, which has different roles, like &lt;em&gt;paying
member&lt;/em&gt;, while the database &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;User&lt;/code&gt; class could offer roles like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;admin&lt;/code&gt;,
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app_read&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;app_write&lt;/code&gt;, for example. In a language without packages,
the same information could be conveyed via the project’s folder
structure.&lt;/p&gt;

&lt;p&gt;On a funnier note, what would Magritte think of the following code?&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;// This is not a person
class Person {
    String name;
    String age;
    
    User(String name, String age) {
        this.name = name;
        this.age  = age;
    }
    
    public String getName() {return name;}
    public String getAge()  {return age;}
}
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;So while it’s dubious that someone will think the previous class
encompasses the whole definition of a person, how many of us have spent
hours debugging a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FileSystem&lt;/code&gt; class that doesn’t live up to its API’s
promises, because ultimately we didn’t realize that this class &lt;em&gt;is not&lt;/em&gt;
the File System? So that comment about the class not being a person,
even if funny, can lead us to interesting semiotical questions about the
realities represented by our code.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;The last point brings us back to metaphors, maps, and ultimately how we
see the world. Espen Aarseth writes:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;[...] paradigms such as object orientation [inspire] practical
philosophies and provides hermeneutic models for organizing and
understanding the world, both directly (through programed systems) and
indirectly (through the worldviews of computer engineers).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Earlier I said that a program is a metaphor for a solution we found. A
map between reality and the world of data structures and algorithms.
Code is a kind of visualization for a particular problem. Visualizations
are made with certain goals and certain audiences in mind. This is what
Noah Iliinsky has to say about maps in the book &lt;em&gt;Beautiful
Visualization&lt;/em&gt; [22], when he describes the style of the London
Tube’s Map, a map that detached itself from a mere geographical
representation of the underground lines:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;That freed the map of any attachment to accurate representation of
geography and led to an abstracted visual style that more simply
reflected the realities of subway travel: once you’re in the system,
what matters most is your logical relationship to the rest of the
subway system.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then Iliinsky writes about what makes for an effective visualization:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The first area to consider is what knowledge you’re trying to convey,
what question you’re trying to answer, or what story you’re trying to
tell.&lt;/p&gt;

  &lt;p&gt;[...] the next consideration is how the visualization is going to
be used. The readers and their needs, jargon, and biases must all be
considered.&lt;/p&gt;

  &lt;p&gt;The readers’ specific knowledge needs may not be well understood
initially, but this is still a critical factor to bear in mind during
the design process.&lt;/p&gt;

  &lt;p&gt;If you cannot, eventually, express your goal concisely in terms of
your readers and their needs, you don’t have a target to aim for and
have no way to gauge your success.&lt;/p&gt;

  &lt;p&gt;“Our goal is to provide a view of the London subway system that allows
riders to easily determine routes between stations,”&lt;/p&gt;

  &lt;p&gt;Understanding the goals of the visualization will allow you to
effectively select which facets of the data to include and which are
not useful or, worse, are distracting.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;His points resonate with the ideas of choosing a Model Reader, where we
ask ourselves what elements from our encyclopedia we need to share, and
which ones could be readily actualized by the recipients of our texts.
We also must write code with certain goals in mind, not only thinking
about what our final users will be able to do with the product, but also
thinking about what we will allow other programs to perform with the
APIs we expose. How good is the &lt;em&gt;theory&lt;/em&gt; we build about the problem we
are solving, will determine the usability our maps–our code–will end
up having. Because in the end, with abstractions–whether they are
types, classes or interfaces–we are designing maps.&lt;/p&gt;

&lt;p&gt;As William Kent says in &lt;em&gt;Data and Reality: a Timeless Perspective on
Perceiving and Managing Information in Our Imprecise World&lt;/em&gt; [17]:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;After a while it dawned on me that these are all just maps, being poor
artificial approximations of some real underlying terrain.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;and he adds:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;What is the territory really like? How can I describe it to you? Any
description I give you is just another map.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;em&gt;theory&lt;/em&gt; we built about our software, with its implicit decisions
and tradeoffs, will be effectively shared by choosing the right
&lt;em&gt;metaphors&lt;/em&gt; that will resonate with our reader’s &lt;em&gt;encyclopedias&lt;/em&gt;. These
metaphors will work like maps that guide understanding. We will also
count with the help of &lt;em&gt;paratexts&lt;/em&gt;. These paratexts will work like sign
posts on a country road, so when the map is not good enough, our users
will still be able to orient themselves. Because ultimately we need to
understand what Borges so clearly illustrates in his short story &lt;em&gt;On
Exactitude in Science&lt;/em&gt; [5], and that is: &lt;strong&gt;the map is not the
territory&lt;/strong&gt;.&lt;/p&gt;

&lt;h2 id=&quot;bibliography&quot;&gt;Bibliography&lt;/h2&gt;

&lt;ol&gt;
  &lt;li&gt;Espen J. Aarseth, Cybertext: Perspectives on ergodic literature, Johns Hopkins University Press, 1997.&lt;/li&gt;
  &lt;li&gt;Harold Abelson and Gerald Jay Sussman with Julie Sussman, Structure and interpretation of computer programs, MIT Press, Massachusetts, 1985.&lt;/li&gt;
  &lt;li&gt;Anonymous, What a programmer does, Datamation (1967).&lt;/li&gt;
  &lt;li&gt;Kent Beck, Test-driven development: by example, Addison-Wesley, 2006.&lt;/li&gt;
  &lt;li&gt;Jorge Luis Borges and Andrew Hurley, Collected fictions, Penguin Books, 1999.&lt;/li&gt;
  &lt;li&gt;Abel Carlevaro, Serie didactica: Para guitarra, Barry, 1966.&lt;/li&gt;
  &lt;li&gt;Koen Claessen and John Hughes, Quickcheck, ACM SIGPLAN Notices 35 (2000), no. 9, 268–279.&lt;/li&gt;
  &lt;li&gt;A. Demers, D. Greene, C. Hauser, W. Irish, and J. Larson, Epidemic algorithms for replicated database maintenance, Proceedings of the Sixth Annual ACM Symposium on Principles of Distributed Computing (1987), 1–12.&lt;/li&gt;
  &lt;li&gt;Edsger W. Dijkstra, Notes on structured programming, 1970.&lt;/li&gt;
  &lt;li&gt;Terry Eagleton, Literary theory: an introduction, Blackwell Publishing, 2015.&lt;/li&gt;
  &lt;li&gt;Umberto Eco, Six walks in the fictional woods, Harvard Univ. Press, 2004.&lt;/li&gt;
  &lt;li&gt;, On literature, Secker Warburg, 2005.&lt;/li&gt;
  &lt;li&gt;, Lector in fabula: La cooperazione interpretativa nei testi narrativi, Bompiani, 2016.&lt;/li&gt;
  &lt;li&gt;Gérard Genette, Paratexts: Thresholds of interpretation, Cambridge Univ. Press, 2001.&lt;/li&gt;
  &lt;li&gt;Peter Gaerdenfors, Geometry of meaning: Semantics based on conceptual spaces, The MIT Press, 2017.&lt;/li&gt;
  &lt;li&gt;Felienne Hermans and Marlies Aldewereld, Programming is writing is programming, Proceed- ings of the International Conference on the Art, Science, and Engineering of Programming (2017).&lt;/li&gt;
  &lt;li&gt;William Kent and Steve Hoberman, Data and reality: a timeless perspective on perceiving and managing information in our imprecise world, Technics Publications, 2012.&lt;/li&gt;
  &lt;li&gt;Donald E. Knuth, Literate programming, The Computer Journal (1983).&lt;/li&gt;
  &lt;li&gt;, The art of computer programming, vol. 2, Addison-Wesley, 2011.&lt;/li&gt;
  &lt;li&gt;Peter Naur, Programming as theory building, Microprocessing and Microprogramming 15 (1985), no. 5, 253–261.&lt;/li&gt;
  &lt;li&gt;Gilbert Ryle, The concept of mind, Penguin, Harmondsworth, England, 1963, first published 1949.&lt;/li&gt;
  &lt;li&gt;Julie Steele and Noah P. N. Iliinsky, Beautiful visualization, O’Reilly, 2010.&lt;/li&gt;
  &lt;li&gt;Alexander A. Stepanov and Paul McJones, Elements of programming, 1st ed., Addison-Wesley, June 2009.&lt;/li&gt;
  &lt;li&gt;Alvaro Videla, Metaphors we compute by, Communications of the ACM 60 (2017), no. 10, 42–45.&lt;/li&gt;
  &lt;li&gt;David J. Wheeler, The use of sub-routines in programmes, ACM ’52 Proceedings of the 1952 ACM national meeting (Pittsburgh) (1952), 235–236.&lt;/li&gt;
&lt;/ol&gt;
</content>
 </entry>
 
 <entry>
   <title>Metaphors We Compute By</title>
   <link href="http://videlalvaro.github.com/2017/01/metaphors-we-code-by.html"/>
   <updated>2017-01-17T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2017/01/metaphors-we-code-by</id>
   <content type="html">&lt;meta name=&quot;twitter:card&quot; content=&quot;summary_large_image&quot; /&gt;

&lt;meta name=&quot;twitter:site&quot; content=&quot;@old_sound&quot; /&gt;

&lt;meta name=&quot;twitter:creator&quot; content=&quot;@old_sound&quot; /&gt;

&lt;meta name=&quot;twitter:title&quot; content=&quot;Metaphors We Compute By&quot; /&gt;

&lt;meta name=&quot;twitter:description&quot; content=&quot;How metaphors affect our code.&quot; /&gt;

&lt;meta name=&quot;twitter:image&quot; content=&quot;http://alvaro-videla.com/images/computer_chip.jpg&quot; /&gt;

&lt;h1 id=&quot;metaphors-we-compute-by&quot;&gt;Metaphors We Compute By&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;January 17 2017&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;In the book Metaphors We Live By, George Lakoff and Mark Johnson set
out to show the linguistic and philosophical worlds that metaphor
wasn’t just a matter of poetry and rhetorical flourish. They showed
that metaphor permeates all areas of our lives, and in particular that
metaphor dictates how we understand the world, how we act in it, how
we live it. They showed our conceptual system is based on metaphors
too, but since we are not normally aware of our own conceptual system,
they had to study it via a proxy: language.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/computer_chip.jpg&quot; alt=&quot;computer chip&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;in-the-beginning-there-was-the-word&quot;&gt;In the beginning there was the Word&lt;/h2&gt;

&lt;p&gt;By studying language they tried to understand how metaphors work by
imposing meaning in our lives. The basic example they present is the
conceptual metaphor of “Argument is War”. We understand the act of
arguing with another person in the same way we understand wars. This
means we come up with the following expressions in our daily language:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Your claims are &lt;em&gt;indefensible&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;He &lt;em&gt;attacked every weak point&lt;/em&gt; in my argument.&lt;/li&gt;
  &lt;li&gt;I &lt;em&gt;demolished&lt;/em&gt; his argument.&lt;/li&gt;
  &lt;li&gt;I never &lt;em&gt;won&lt;/em&gt; an argument with him.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The problem with these sentences that seem innocuous is that we act
and feel based on them. We end up seeing the person we are arguing
with as our &lt;em&gt;opponent&lt;/em&gt;. Someone that’s attacking our positions. This
mean we structure argument in a similar way as if we were at war with
the other person. This means the metaphor is not just language
flourish, we live it. Lakoff and Johnson propose the exercise of
imagining a culture where arguments are not viewed in terms of war, of
winners and losers, but in which language is a dance, where we have to
cooperate with our partner in order to achieve our desired goal,
reaching conclusions as a team. The book keeps going analyzing the
different aspects of language and metaphors and how they affect our
concepts and view of the world. The authors give many examples to
defend the thesis that our understanding of the world is based on
metaphors and that those metaphors are the foundation of our
behaviors.&lt;/p&gt;

&lt;p&gt;A big takeaway from the book is that metaphors enable certain ways of
thinking while it restrict others, as the example above shows. In this
article we are interested in understanding how metaphors shape the way
we understand computing and its related problems. We want to see what
kind of problems are enabled by the metaphors in use, and for which
ones we lack the right metaphors still. And no, monads are not like
burritos!&lt;/p&gt;

&lt;p&gt;In what follows we are not going to discuss the book thesis any
further, on the other hand, we will accept it and bring it into
Computer Science. First we will see how metaphor help us understand
the world of computers, a world that didn’t exist a mere 80 years
ago. Then we will try to see how metaphor works in the way we
structure code, or design algorithms and data structures. We will see
that we even solve problems in our discipline based on what metaphors
are part of our arsenal.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;“Sometimes our tools do what we tell them to. Other times, we adapt
ourselves to our tools’ requirements.” – The Shallows. Metaphors are
the tools of comprehension.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;a-metaphorical-understanding&quot;&gt;A metaphorical understanding&lt;/h2&gt;

&lt;p&gt;We understand new concepts by way of relating them to things we
already know. Back in the late ’40s and early ’50s when today’s
computers came to life, there was no word to name such invention, but
we understood them as automatic brains. What’s more interesting is the
fact that the word &lt;em&gt;computer&lt;/em&gt; existed already, and it was the term
used to refer to the person doing calculations for engineers. Think of
an engineer having to know what would be the trajectory of a
projectile, or how the wind would affect an airplane’s wing shape, so
they would throw a couple of formulas and numbers to their “human
computers” in order to get the answer they wanted. Now they had these
new machines that worked automatically, so they called them Electronic
Computers, dropping the electronic part as timed passed by. So our
very own discipline was named after a metaphor.&lt;/p&gt;

&lt;p&gt;But metaphors also obscure possibilities if we don’t understand their
limitations. A common problem with new metaphors is that we use the
original meaning of the word at face value. We don’t pay attention to
how the word we are using to explain a new concept limits our
understanding of the very concept we are trying to explain. James
Gleick on his book The Information gives a fascinating account of the
invention of the telegraph. The word “tele-graph” means
far-writing. Lo and behold early telegraphs were strange machines that
tried to actually write at a distance, using one-to-one mapping of
letters of the alphabet into wires which sounds terribly
impractical. Around that time, thanks to Braille we understood that
language could be coded in a form different from how it sounds (or how
it is written). Morse Code was the next step in improving our
telegraphs, and to understand that we don’t have to
write-at-a-distance to have actual long distance
communications. Thanks to Claude Shannon and the many smart people
before him, we managed to escape from the problems of the &lt;em&gt;telegraph&lt;/em&gt;
direct metaphor and build the whole discipline of Information Theory. We
know that information must be encoded first, then sent into a channel,
to be decoded at the other end of the channel so the destination
receives it.&lt;/p&gt;

&lt;p&gt;Peter Gärdenfors explains how metaphors create new knowledge in his
book “The Geometry of Meaning – Semantics Based on Conceptual Spaces”:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;[…] a metaphor transfer information from one conceptual domain to
another; what is transferred is a pattern rather than domain specific
information. A metaphor can thus be used to identify a structure in a
domain that would not have been discovered otherwise. This is how
metaphors create new knowledge.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Lakoff (quoted by Gärdenfors) says:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Metaphorical mappings preserve the cognitive topology of the
source domain in a way consistent with the inherent structure of the
target domain.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;metaphors-and-code&quot;&gt;Metaphors and Code&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://history.computer.org/pioneers/baker.html&quot;&gt;An Unknown author&lt;/a&gt; in a
letter to Donald Knuth said that &lt;em&gt;to program is to write to another
programmer about our solution to a problem&lt;/em&gt;. A program is our
explanation of how a problem might be solved, it’s a metaphor that
stands in place of our understanding, but for metaphors to be
effective they need to convey meaning using concepts already known to
us. We could use the explanatory power of a particular program as
measure of its own elegance.&lt;/p&gt;

&lt;p&gt;Let’s think about the following example. We could program a computer
to command other computers to perform tasks respecting their arrival
order. This description is already hard to understand. On the other
hand we could describe the solution by talking of a &lt;em&gt;Queue Server&lt;/em&gt;
that assigns &lt;em&gt;Jobs&lt;/em&gt; to &lt;em&gt;Workers&lt;/em&gt; based on &lt;em&gt;a First Come First Served&lt;/em&gt;
queue discipline. Queues are a familiar concept from our daily lives,
we see them at the supermarket or the bank. We wait in them at
airports or train stations. We understand how they work, so for
someone reading our code it might be easier to talk about queues,
workers and jobs, than to go all over the verbosity required to
explain this setting without using the &lt;em&gt;Queue&lt;/em&gt; metaphor. We see that
by choosing the right metaphor our program reaches a level of
abstraction that requires the least effort for someone foreign to the
problem to understand our solution. Also by solving our problem with
queues, we get a whole mathematical theory for free. Mathematics is
itself a field where often problems are only tackled once an
appropriately expressive language is available to approach them. We
are no longer punching blind in the dark. Now we can analyze and
understand our problem with all the tools provided by Queueing Theory.&lt;/p&gt;

&lt;h2 id=&quot;data-structures-as-metaphors&quot;&gt;Data Structures as Metaphors&lt;/h2&gt;

&lt;p&gt;We analyze data structures to see which one would be a better fit for
the performance characteristics of our particular problem, but we
often forget that a data structure also contains explanatory power.&lt;/p&gt;

&lt;p&gt;The choice of data structures helps us convey the meaning. We could
store a bunch of elements in an array, but what if we actually need to
ensure that the elements are unique? Isn’t that what &lt;em&gt;Sets&lt;/em&gt; are for?
The underlying representation of our set can still be backed by a
plain array, but now our program express its intentions in a clearer
way. Whenever another programmer reads it, they will understand that
the elements in our collection must be unique. A very important
realization is to see that for a computer our programs will be just
another succession of bits that it needs to process. It is us that
give meaning to those bits, so we have to use the right metaphor on
top of them to make our programs clearer. As it is said:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;no one has seen a program which the machine could not comprehend but
which humans did. –– Unknown author
See
&lt;a href=&quot;http://alvaro-videla.com/2014/09/a-programmers-role.html&quot;&gt;here&lt;/a&gt; for
the paper with the original quote.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We must strive to make our programs as easy to understand to other
programers as possible. Ease of comprehension should be the tally with
which we measure our programs. We should keep in mind that we can
arrange our code in many different ways to solve a computing problem,
but not all those arrangements will favor human communication and
understanding. We must ask ourselves: is someone reading my code going
to understand how I solved this particular problem?&lt;/p&gt;

&lt;p&gt;The same way a metaphor enables certain ways of understanding and
limits others, so do data structures. We already saw the problem
with using direct metaphors like the original telegraph. When it comes
to data structures we can see that a Set will tell us that its
elements are unique, and it would allow us to test if an element is a
member of the set. But a Linked List gives us the idea of traversing
its elements on after the other, without being able to skip them. An
Array gives us the idea that we can address its elements by index. The
same can be seen with Queues and Stacks, two of the most fundamental
data structures taught in any algorithms course. Both can be
implemented using an array, the difference is that one returns the
elements in FIFO order, while the other does in LIFO order
respectively. Even if this looks like an everyday thing for most
programmers, the moment we choose to use a Stack instead of a Queue,
we are deciding how we want to explain our program. The Stack is a
very good metaphor for the collection of items that our program works
with, because it tells a future reader of our program in which order
we expect to process the items. We don’t even need to read how the
stack is implemented, since we can assume we will get the items in
LIFO order. This is why types are so important in computer
science. Not types as in static type checking of programs, but the
types as the &lt;em&gt;concepts&lt;/em&gt; we use to describe our programs: Persons,
Users, Stacks, Trees, Nodes, you name it. Types are the characters
that tell the story of our programs, without types, we just have
operations on streams of bytes.&lt;/p&gt;

&lt;h2 id=&quot;cognitive-leaps&quot;&gt;Cognitive Leaps&lt;/h2&gt;

&lt;p&gt;A situation we will face once and again is finding the right metaphor
that describes and explains our problem. As we saw earlier with our
queueing theory example, we had to make a cognitive leap to go from
tasks that we have to process in certain order to understanding this
is a queueing problem. Once we managed the cognitive leap, all the
mathematical tools from queueing theory were at our disposition. Graph
Theory is filled with examples of mundane tasks that once converted to
a graph problem, have well known solutions. Whenever you ask Google
Maps to get you to your destination, Google is translating your
problem to a graph representation and suggests one or more &lt;em&gt;paths&lt;/em&gt; in
the graph. Graphs are the right metaphor, understood by mathematicians
and computers alike. Do we have more instances of problems that seem
difficult but that we crack once we find the right metaphor? The
Distributed Systems literature has a very interesting one.&lt;/p&gt;

&lt;p&gt;In the late ’80s Alan Demers and his colleagues from Xerox tried to
find a solution to database replication in unreliable networks. They
classified their algorithms as “randomized”, explaining them by using
the &lt;em&gt;Rumor Mongering&lt;/em&gt; metaphor: a computer would tell two other
computers about and update, then each in turn will tell two other
computers about the update, and so on, until the information got
replicated. This metaphor gave way to a new area of study called
around so called &lt;em&gt;Gossip Algorithms&lt;/em&gt;. The &lt;em&gt;gossip&lt;/em&gt; metaphor makes the
idea easy to explain, but we still lack the mathematical tools that
would help us analyze the effectiveness of the algorithms. During
their research they discovered another metaphor related to
Epidemics. They understood that their algorithms replicated data like
when an epidemic disseminated across a population. By using this new
metaphor they got immediate access to all the knowledge
in
&lt;a href=&quot;https://www.amazon.com/Mathematical-Theory-Epidemics-Norman-Bailey/dp/0852641133&quot;&gt;The Mathematical Theory of Epidemics&lt;/a&gt;,
which fitted their work like a glove. Not only they named their paper
“Epidemic Algorithms for Replicated Database Maintenance” but they
also took the nomenclature of the discipline to explain their
algorithms. It was a matter of finding the right metaphor to get
access to a new world of explanatory power.&lt;/p&gt;

&lt;h2 id=&quot;metaphors-everywhere&quot;&gt;Metaphors Everywhere&lt;/h2&gt;

&lt;p&gt;We might still be skeptical and ask ourselves if we do use that many
metaphors in programming. Let’s take a look at the Distributed Systems
literature in general (metaphors are in &lt;em&gt;italics&lt;/em&gt;):&lt;/p&gt;

&lt;p&gt;Whenever nodes need to &lt;em&gt;agree&lt;/em&gt; on a common value, we start a
&lt;em&gt;consensus&lt;/em&gt; algorithm to decide on a value. There’s usually a &lt;em&gt;leader&lt;/em&gt;
process that takes care of making the final decision based on the
&lt;em&gt;votes&lt;/em&gt; it has received from its &lt;em&gt;peers&lt;/em&gt;. Nodes communicate sending
&lt;em&gt;messages&lt;/em&gt; over a &lt;em&gt;channel&lt;/em&gt;, which might get &lt;em&gt;congested due to too
much traffic&lt;/em&gt;. This could create an information &lt;em&gt;bottleneck&lt;/em&gt;, with
&lt;em&gt;queues&lt;/em&gt; at each end of the &lt;em&gt;channels&lt;/em&gt; backing up. These bottlenecks
might render one or more nodes unresponsive, causing &lt;em&gt;network
partitions&lt;/em&gt;. Is the process that’s taking long to &lt;em&gt;respond dead&lt;/em&gt;? Why
didn’t it acknowledge the &lt;em&gt;heart beat&lt;/em&gt; and a timeout &lt;em&gt;triggered&lt;/em&gt;?.”. I
could go on, but I guess you get the point.&lt;/p&gt;

&lt;h2 id=&quot;a-story-in-code&quot;&gt;A Story in Code&lt;/h2&gt;

&lt;p&gt;As programmers we must be able to tell a story with our code
explaining how you solved a particular problem. Like a writer we must
know our metaphors. Many metaphors will be able to explain a concept,
but we must have enough skill to choose the right one that’s able to
convey our ideas to those future programmers that will read our
code. Thus we cannot use every metaphor under our belt. We must master
the art of metaphor selection, of meaning amplification. We must know
when to add and when to subtract. We will learn to revise and rewrite
our code like a writer does. Once there’s nothing else to add or
remove we have finished our work. The problem we started with is now
the solution in front of our eyes. Is that the meaning we intended to
convey in the first place?&lt;/p&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;Lakoff, George, and Mark Johnson. “Metaphors We Live By”&lt;/li&gt;
  &lt;li&gt;Gleick, James. “The Information: A History, a Theory, a Flood”&lt;/li&gt;
  &lt;li&gt;Shannon, Claude Elwood, and Warren Weaver. “The Mathematical Theory of Communication”&lt;/li&gt;
  &lt;li&gt;Bailey, Norman T. J. “The Mathematical Theory of Epidemics”&lt;/li&gt;
  &lt;li&gt;Demers, Alan, Dan Greene, Carl Hauser, Wes Irish, and John Larson. “Epidemic Algorithms for Replicated Database Maintenance”&lt;/li&gt;
  &lt;li&gt;Gärdenfors, Peter. “The Geometry of Meaning – Semantics Based on Conceptual Spaces”&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;credits&quot;&gt;Credits&lt;/h2&gt;

&lt;p&gt;Image: &lt;a href=&quot;https://flic.kr/p/6GRuVy&quot;&gt;https://flic.kr/p/6GRuVy&lt;/a&gt; License &lt;a href=&quot;https://creativecommons.org/licenses/by-nc/2.0/&quot;&gt;CC BY-NC 2.0&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;slides&quot;&gt;Slides&lt;/h2&gt;

&lt;p&gt;I had the honor to present this blogpost as an eighteen minute talk at this year’s &lt;a href=&quot;https://monkigras.com&quot;&gt;Monki Gras Conference in London&lt;/a&gt;. Here are the slides:&lt;/p&gt;

&lt;p&gt;
&lt;script async=&quot;&quot; class=&quot;speakerdeck-embed&quot; data-id=&quot;6315a8a09fb74058bb86fa84ee74330c&quot; data-ratio=&quot;1.7&quot; src=&quot;//speakerdeck.com/assets/embed.js&quot;&gt;&lt;/script&gt;
&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Java ThreadLocalRandom Explained</title>
   <link href="http://videlalvaro.github.com/2016/10/inside-java-s-threadlocalrandom.html"/>
   <updated>2016-10-29T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2016/10/inside-java-s-threadlocalrandom</id>
   <content type="html">&lt;meta name=&quot;twitter:card&quot; content=&quot;summary_large_image&quot; /&gt;

&lt;meta name=&quot;twitter:site&quot; content=&quot;@old_sound&quot; /&gt;

&lt;meta name=&quot;twitter:creator&quot; content=&quot;@old_sound&quot; /&gt;

&lt;meta name=&quot;twitter:title&quot; content=&quot;Java ThreadLocalRandom Explained&quot; /&gt;

&lt;meta name=&quot;twitter:description&quot; content=&quot;In this blogpost we'll see the details behind ThreadLocalRandom's algorithm.&quot; /&gt;

&lt;meta name=&quot;twitter:image&quot; content=&quot;http://videlalvaro.github.io/images/fork-join-tree.png&quot; /&gt;

&lt;h1 id=&quot;java-threadlocalrandom-explained&quot;&gt;Java ThreadLocalRandom Explained&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;October 26 2016&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;In this blogpost I’d like to present some things I’ve found while
trying to understand the implementation of the
jdk8
&lt;a href=&quot;https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/ThreadLocalRandom.html&quot;&gt;ThreadLocalRandom&lt;/a&gt; class. What
was my motivation for doing something like that? Over the weekend I
was reading the Second Volume from Knuth’s TAOCP “Seminumerical
Algorithms”, where he devotes the whole of Chapter 3 describing
techniques for generating random numbers with a computer, and then
explaining how to test those random number generators (of couse we are
talking about pseudo random number generators or PRNGs). Near the end
of the chapter Knuth proposes the following exercise:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Look at the subroutine library of each computer installation in your
organization, and replace the random number generators by good
ones. Try to avoid being too shocked at what you find.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That seemed like a good exercise, but while I didn’t want to replace
the PRNGs on my organisation, I decided to take a look at the
implementation and see for myself. I grabbed the code
from &lt;a href=&quot;http://hg.openjdk.java.net/jdk8/jdk8&quot;&gt;OpenJDK&lt;/a&gt; and went straight
to check the implementation
of
&lt;a href=&quot;http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/concurrent/ThreadLocalRandom.java&quot;&gt;ThreadLocalRandom&lt;/a&gt;. The
class ThreadLocalRandom (TLR from now on), is an implementation of the
interface offered by
the
&lt;a href=&quot;http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/classes/java/util/Random.java&quot;&gt;Random&lt;/a&gt; class. Random
is an implementation of
the
&lt;a href=&quot;https://en.wikipedia.org/wiki/Linear_congruential_generator&quot;&gt;Linear Congruence Method&lt;/a&gt; presented
by Knuth on TAOCP. A quick foray into TLR revealed something totally
different.&lt;/p&gt;

&lt;p&gt;In trying to understand TLR, I downloaded the source code for the
class, and got it into IntelliJ. I tried to just ignore the comments,
and go and read the code as is. What an interesting experience! The
code is full of hardcoded constants like the following:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;mix32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;33&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xff51afd7ed558ccd&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)(((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;33&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xc4ceb9fe1a85ec53&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;What is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xff51afd7ed558ccdL&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xc4ceb9fe1a85ec53L&lt;/code&gt;? How do they come up with those constants? And there’s even a couple more:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;mh&quot;&gt;0x9e3779b97f4a7c15&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;L&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0x9e3779b9&lt;/span&gt;
&lt;span class=&quot;mh&quot;&gt;0xbb67ae8584caa73b&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;L&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;A little googling revealed that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x9e3779b97f4a7c15L&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x9e3779b9&lt;/code&gt;
come from The R5 Encryption Algorithm by Rivest (the same person that
gave the R to RSA). In the paper we can see that Rivest took
the &lt;a href=&quot;https://en.wikipedia.org/wiki/Golden_ratio&quot;&gt;Golden Ratio&lt;/a&gt; number
and used it in the following formula to produce either
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x9e3779b97f4a7c15L&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x9e3779b9&lt;/code&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;no&quot;&gt;Q&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Odd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;φ&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Where Odd is a function that returns the nearest integer to the input
parameter. Then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;w&lt;/code&gt; is the wordsize we want to use, like 32 or 64. As
you might have guessed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x9e3779b97f4a7c15L&lt;/code&gt; is the 64bit version of
the constant and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x9e3779b9&lt;/code&gt; is the 32bit version. Note that
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x9e3779b9&lt;/code&gt; is also used in
the
&lt;a href=&quot;https://en.wikipedia.org/wiki/Tiny_Encryption_Algorithm&quot;&gt;Tiny Encryption Algorithm&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The constant &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x9e3779b97f4a7c15L&lt;/code&gt; is called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GAMMA&lt;/code&gt; in the TLR source
code and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x9e3779b9&lt;/code&gt; is called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PROBE_INCREMENT&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then we have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xbb67ae8584caa73bL&lt;/code&gt; which in TLR is called
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SEEDER_INCREMENT&lt;/code&gt;. That one is easier, since it’s just the fractional
part of the square root of 3 obtained like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;n&quot;&gt;frac&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sqrt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;64&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;It happens that that constant is used in
the
&lt;a href=&quot;http://www.iwar.org.uk/comsec/resources/cipher/sha256-384-512.pdf&quot;&gt;SHA512&lt;/a&gt; algorithm,
which does a similar procedure to the first 8 prime numbers to extract
8 constants that are used in the algorithm as well.&lt;/p&gt;

&lt;p&gt;Then we have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xff51afd7ed558ccdL&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xc4ceb9fe1a85ec53L&lt;/code&gt; which are
used in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mix32&lt;/code&gt; method shown above. Google reveals that these
constants come from the finaliser step of
the &lt;a href=&quot;https://en.wikipedia.org/wiki/MurmurHash&quot;&gt;MurmurHash3&lt;/a&gt; algorithm.&lt;/p&gt;

&lt;p&gt;So far so good, but what are all these numbers used for. To understand
this, we need to understand what’s the purpose of
ThreadLocalRandom. As the name suggest, the idea is to have a source
of random numbers, per thread, so we can obtain random numbers
concurrently. This means that every time a thread initialises an
instance of TLR, the code needs to initialise the random seed for that
particular thread. The seed is initialised on a &lt;em&gt;mixed&lt;/em&gt; version of
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SEEDER_INCREMENT&lt;/code&gt;; at the same time, a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PROBE&lt;/code&gt; value is initialised
for that particular thread by adding the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PROBE_INCREMENT&lt;/code&gt; to the
current &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;probeGenerator&lt;/code&gt; value. What’s that &lt;em&gt;probe&lt;/em&gt; value used for? It
is used by classes like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ConcurrentHashMap&lt;/code&gt; to calculate hashes for
the map keys.&lt;/p&gt;

&lt;p&gt;So we have constants taken from RC5 and TEA, SHA512 and
MurmurHash3. This is starting to make no sense at all, but let’s check
how it all works when put together to see if we can make sense out of
this.&lt;/p&gt;

&lt;h2 id=&quot;obtaining-random-numbers&quot;&gt;Obtaining Random Numbers&lt;/h2&gt;

&lt;p&gt;To get a random number from ThreadLocalRandom we can call the
method &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nextInt()&lt;/code&gt; which is part of the interface exposed by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Random&lt;/code&gt;
as well. Let’s check the implementation of that method:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;nextInt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;mix32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nextSeed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;So we compute the next seed and that’s &lt;em&gt;mixed&lt;/em&gt; and returned to the
user. Let’s see what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nextSeed()&lt;/code&gt; is doing:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;nextSeed&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;Thread&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// read and update per-thread seed&lt;/span&gt;
    &lt;span class=&quot;no&quot;&gt;UNSAFE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;putLong&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Thread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;currentThread&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;SEED&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;
                   &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;UNSAFE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getLong&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;SEED&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;GAMMA&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;That method obtains the current value of the seed and just adds to it
the value of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GAMMA&lt;/code&gt; constant presented above. This doesn’t look
random at all, for some defintion of the word random anyway. If we go
back to Knuth, the Linear Congruence Method proposed in his book
involves a calculation like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;n&quot;&gt;nextseed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;oldseed&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;multiplier&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addend&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mask&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;So we have a multiplier, and addend, and then we apply a mask to that
value.&lt;/p&gt;

&lt;p&gt;The method used by TLR lacks the multiplier part but in TAOCP Knuth is
very clear that the lack of multiplier has the effect of producing a
sequence that’s “everything but random”. The TLR case is like setting
the multiplier to 1 in the Linear Congruence Method.&lt;/p&gt;

&lt;p&gt;So there’s either something very wrong with ThreadLocalRandom or I am
missing something. Considering that ThreadLocalRandom is part of the
JDK used by millions of developers, I’m pretty sure that I am at
fault, and I’m for sure missing something. Time to read those
comments.&lt;/p&gt;

&lt;p&gt;Right at the top of the class we have this comment:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Even though this class subclasses java.util.Random, it uses the same
basic algorithm as java.util.SplittableRandom.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So let’s
check
&lt;a href=&quot;http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/SplittableRandom.java&quot;&gt;SplittableRandom&lt;/a&gt; and
see what we find out over there, but this time let’s read the
comments there as well.&lt;/p&gt;

&lt;h2 id=&quot;the-theory-behind-threadlocalrandom&quot;&gt;The Theory Behind ThreadLocalRandom&lt;/h2&gt;

&lt;p&gt;When we open SplittableRandom’s sourcecode we find this illustrative
comment:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;cm&quot;&gt;/*
 * This algorithm was inspired by the &quot;DotMix&quot; algorithm by
 * Leiserson, Schardl, and Sukha &quot;Deterministic Parallel
 * Random-Number Generation for Dynamic-Multithreading Platforms&quot;,
 * PPoPP 2012, as well as those in &quot;Parallel random numbers: as
 * easy as 1, 2, 3&quot; by Salmon, Morae, Dror, and Shaw, SC 2011.  It
 * differs mainly in simplifying and cheapening operations.
 */&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Here’s the
paper
&lt;a href=&quot;http://supertech.csail.mit.edu/papers/dprng.pdf&quot;&gt;Deterministic Parallel Random-Number Generation for Dynamic-Multithreading Platforms&lt;/a&gt; and
here we
have
&lt;a href=&quot;http://www.thesalmons.org/john/random123/papers/random123sc11.pdf&quot;&gt;Parallel random numbers: as easy as 1, 2, 3&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The goal of the first paper by Leiserson, Schardl, and Sukha (from now
on LSS) is to see how to create an efficient &lt;em&gt;Deterministic parallel
random-number generators&lt;/em&gt; for dthreading platforms (as opposed to
POSIX’s pthreading). Dthreading is an implementation of
the &lt;a href=&quot;https://en.wikipedia.org/wiki/Fork%E2%80%93join_model&quot;&gt;Fork-Join&lt;/a&gt;
parallel computation model. The problem is that traditional DPRNGs
don’t scale to hundreds of thousands of &lt;em&gt;strands&lt;/em&gt; (read
green-threads), since they were made with the pthread model in
mind. In that paper they present an algorithm for DPRNGs called
DotMix, which uses a &lt;strong&gt;dot&lt;/strong&gt; product of a &lt;em&gt;pedigree&lt;/em&gt; and then
&lt;strong&gt;mix&lt;/strong&gt;es the result using
the &lt;a href=&quot;https://en.wikipedia.org/wiki/RC6&quot;&gt;RC6&lt;/a&gt; algorithm. They claim
DotMix has a statistic quality which rivals the one from
the &lt;a href=&quot;https://en.wikipedia.org/wiki/Mersenne_Twister&quot;&gt;Mersenne Twister&lt;/a&gt;
algorithm and should work for hundreds of thousands of &lt;em&gt;strands&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;What they mean by saying those other algorithms don’t scale? The
problem of parallel streams of random numbers is that there has to be
some way to prevent two streams from producing the same sequence of
random numbers. We could keep state around and synchronise using locks
among threads, but that will be slow. They are trying to find a way
for each thread to be able to have a seed for their random numbers
that doesn’t depend on shared state.&lt;/p&gt;

&lt;h3 id=&quot;whats-a-pedigree&quot;&gt;What’s a Pedigree?&lt;/h3&gt;

&lt;p&gt;They use the following definition to explain a pedigree which if we
don’t read the original paper, it won’t make much sense:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;A pedigree scheme uniquely identifies each strand of a dthreaded
program in a scheduler-independent manner.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Let’s try to understand that definition in the context of a fork-join
parallel computing model. Each thread can fork multiple threads by
calling fork (&lt;em&gt;spawn&lt;/em&gt; in the LSS paper), or could generate a value. At
the same time the spawned threads can also do the same: either spawn a
new child thread or generate more values. Now let’s represent that
model using the following tree:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/fork-join-tree.png&quot; alt=&quot;fork-join-tree&quot; /&gt;&lt;/p&gt;

&lt;p&gt;We have that the root task &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; generated the value &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;6A&lt;/code&gt;, forked the
thread &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; and then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C&lt;/code&gt;, and generated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;81&lt;/code&gt;. Then the task &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt;
generated three values: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;12&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;74&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C7&lt;/code&gt;. Then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C&lt;/code&gt; forked &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;D&lt;/code&gt; and
generated &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B9&lt;/code&gt;; and so on.&lt;/p&gt;

&lt;p&gt;An oversimplification of what LSS claims is that the pedigree of each
value, taken as the vector of labels in the tree from the generated
value leaf to the root, is unique, independently of the scheduling of
the tasks. For example the value &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;74&lt;/code&gt; has the following pedigree: &amp;lt;2,
2&amp;gt;.&lt;/p&gt;

&lt;p&gt;We have a unique vector per thread, but how do we generate Random
Numbers from there? The authors of DotMix talk about the idea of
&lt;em&gt;compressing&lt;/em&gt; the values of the vector to a single machine
word. Here’s were the &lt;em&gt;Dot&lt;/em&gt; part of DotMix comes into play. They
calculate the dot product of the pedigree vector with another vector
of integers “chosen uniformly at random” (see they paper for the exact
details).&lt;/p&gt;

&lt;p&gt;This integer hash that’s produced from the dot product has a small
probability of collision with the ones produced by other threads. The
problem is that now two similar pedigrees can produce similar hashes,
but the authors of DotMix wanted the produced value to be
&lt;em&gt;statistically dissimilar&lt;/em&gt; from other hashes. To solve that problem
they introduced the mixing part to DotMix. So, what’s mixing?&lt;/p&gt;

&lt;h3 id=&quot;whats-mixing&quot;&gt;What’s Mixing?&lt;/h3&gt;

&lt;p&gt;The mixing step of the algorithm applies a function to the hash value
obtained from the pedigree in order to reduce the statistical
correlation of two hash values, so it becomes hard to predict their
sequence. In the case of DotMix, the mix function swaps the high
and low order bits of the hash value, that is, a function that in a
64bit hash for example, swaps the first 32bit part with the second
one. For DotMix they use a mixing function based on
the &lt;a href=&quot;https://en.wikipedia.org/wiki/RC6&quot;&gt;RC6 Encryption Algorithm&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;counter-based-prngs&quot;&gt;Counter Based PRNGs&lt;/h2&gt;

&lt;p&gt;From the description of DotMix we can see that ThreadLocalRandom is
using a mixing step that’s applied to the seed obtained from
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nextSeed&lt;/code&gt;, but as we saw &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nextSeed&lt;/code&gt; just increases the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;currentSeed&lt;/code&gt;
by the value of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GAMMA&lt;/code&gt; constant, which means this has nothing to
do with pedigrees. Here’s where the other paper mentioned in the
comments comes into play.&lt;/p&gt;

&lt;p&gt;The paper “Parallel random numbers: as easy as 1, 2, 3” by Salmon,
Morae, Dror, and Shaw presents the idea of Counter Based PRNGs. In
their paper they are trying to solve the problem of having “massively
parallel high-performance computations”, for which they say
traditional PRNGs don’t scale well.&lt;/p&gt;

&lt;p&gt;The problem with traditional PRNGs like the method from TAOCP is that
they are serial. So to calculate random number N+1 we need to have
calculated the Nth random number before. If we want to produce several
streams of random numbers, then that approach won’t scale if our goal
is to be sure the streams are different, since initialising each
stream with its own seed becomes complicated.&lt;/p&gt;

&lt;p&gt;To counter that problem they propose a simple function to produce a
sequence of numbers:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;mod&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;^&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;That function is just a simple counter that increases the input value
by one and then applies mod to some power of two. Since it’s just a
counter, this method is called Counter Based PRNGs.&lt;/p&gt;

&lt;p&gt;At this point we might start hurting our own eye muscles from so much
eye-rolling but stay with me. The counter function could use just 1 as
the increment as in that example or use a number a bit more
complicated like the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GAMMA&lt;/code&gt; value used in ThreadLocalRandom. Still,
we don’t have random numbers yet.&lt;/p&gt;

&lt;p&gt;What the authors of that paper propose is that we apply a
cryptographically secure function to the values produced by the
counter function. In particular they propose using parts of AES or
Threefish to the value produced by the counter. In their paper instead
of incrementing the counter by 1 they propose a couple of constants
that are also used in ThreadLocalRandom: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xbb67ae8584caa73b&lt;/code&gt; and
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0x9e3779b97f4a7c15&lt;/code&gt; which are our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SEEDER_INCREMENT&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GAMMA&lt;/code&gt;
values mentioned before. They say that by using these constants and
some variants of AES or Threefish they managed to pass
the &lt;a href=&quot;https://en.wikipedia.org/wiki/TestU01&quot;&gt;TestU01’s BigCrush tests&lt;/a&gt;
for PRNGs and their PRNGs produce periods of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2^128&lt;/code&gt; numbers.&lt;/p&gt;

&lt;h2 id=&quot;getting-the-puzzle-together&quot;&gt;Getting the Puzzle Together&lt;/h2&gt;

&lt;p&gt;So now we are managing to put together the puzzle of
ThreadLocalRandom. From DotMix we have the mixing function and from
Salmon et al. we get the idea of Counter-Based PRNGs. There’s still a
missing piece though: why is ThreadLocalRandom using what seems to be
a custom mixing function?&lt;/p&gt;

&lt;p&gt;It happens that ThreadLocalRandom &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mix32&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mix64&lt;/code&gt; are not custom
at all. They are in fact based on MurmurHash3 finaliser function. The
idea behind that function is to produce
an &lt;a href=&quot;https://en.wikipedia.org/wiki/Avalanche_effect&quot;&gt;Avalanche Effect&lt;/a&gt;
on the bits of the value passed to the mix function. The Avalanche
Effect is a technique that by flipping a single bit, manages to change
enough bits (the avalanche part) so that the resulting number is quite
different from the input. So if we pass two values to the function
that are quite similar, the mixing function will make sure they end up
quite different. If we look at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mix32&lt;/code&gt; again we will see a couple of
constants there, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xff51afd7ed558ccdL&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0xc4ceb9fe1a85ec53L&lt;/code&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;mix32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;33&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xff51afd7ed558ccd&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)(((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;33&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xc4ceb9fe1a85ec53&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;32&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;According to the creator of MurmurHash3 these constants were chosen
because they produce an Avalanche Effect with a probability near to
0.5, but the story does not end here. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SplittableRandom&lt;/code&gt; does not use
the same constants!&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// SplittableRandom implementation&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;mix64&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0xbf58476d1ce4e5b9&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;27&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mh&quot;&gt;0x94d049bb133111eb&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;^&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;z&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The constants used here were suggested by David Stafford in
his
&lt;a href=&quot;http://zimbry.blogspot.ch/2011/09/better-bit-mixing-improving-on.html&quot;&gt;blog&lt;/a&gt; who
found them after running some experiments. For some reason
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SplittableRandom&lt;/code&gt; has the &lt;em&gt;better&lt;/em&gt; constants while ThreadLocalRandom
does not.&lt;/p&gt;

&lt;h2 id=&quot;threadlocalrandom-a-random-algorithm&quot;&gt;ThreadLocalRandom a Random Algorithm?&lt;/h2&gt;

&lt;p&gt;One thing that Knuth tries to make clear in his book is that we should
not use a random algorithm to produce a PRNGs. That is, the steps of
the algorithm shouldn’t be chosen at random, like grab a piece from
here, another from there, put them together, shake it a bit, and TAH
DAH! we got a PRNG. So far this seems to be the case with
ThreadLocalRandom. What are we missing?&lt;/p&gt;

&lt;p&gt;There’s yet another paper
called
&lt;a href=&quot;http://dl.acm.org/citation.cfm?id=2660195&quot;&gt;Fast Splittable Pseudorandom Number Generators&lt;/a&gt;. Its
authors are Guy Steele, Doug Lea and Christine H. Flood, which in case
you don’t know, they are all people involved with the development of
the JDK. What is that paper about? It explains the algorithm behind
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SplittableRandom&lt;/code&gt;, which is the one used for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ThreadLocalRandom&lt;/code&gt; as
well (with some small diferences as explained above).&lt;/p&gt;

&lt;p&gt;In that paper they explain that they took DotMix and implemented it in
Scala because the language would permit them a clean implementation
which they could analyse for further improvements which then would be
translated to Java. That’s a pretty interesting use case for Scala.&lt;/p&gt;

&lt;p&gt;Once they had DotMix implemented they tried to refine it, focusing on
simplifying it’s steps, trying to increase the performance of the
algorithm, while at the same time keeping it secure enough to pass the
TestU01 battery of tests. So a pedigree based PRNG became a counter
based one; and cryptographically secure for mixing (but arguably slow)
functions for mixing became the finaliser function from
MurmurHash3. Of course they put their new PRNG algorithm under the
battery of tests offered by TestU01 which is the industry standard for
testing PRNGs.&lt;/p&gt;

&lt;p&gt;There’s a historical note from that paper that it’s worth
noticing. The paper is from October 2014. On December that year
a
&lt;a href=&quot;https://hal.archives-ouvertes.fr/hal-01098598/document&quot;&gt;paper from INRIA&lt;/a&gt; got
submitted for publication which discussed the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MRG32k3a&lt;/code&gt; algorithm as
a way to replace ThreadLocalRandom implementation. If we read Steele’s
et al paper we see that they reviewed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MRG32k3a&lt;/code&gt; but they saw it
didn’t fit their selection criteria, because it wouldn’t allow to
split the stream beyond 192 sub-threads. I would assume that the
authors of the INRIA paper didn’t know about Steele’s paper at the
time of publication.&lt;/p&gt;

&lt;p&gt;Another interesting note from that paper is their commentary on
Haskell’s System.Random implementation:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The API is beautiful; the implementation turns out to have a severe
flaw.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;On this interesting journey through ThreadLocalRandom we saw that
while we found quite a few things along the way that seemed to have no
logical explanation, there was in fact a reason for them to be
there. In this case the authors of ThreadLocalRandom took two
algorithms for producing PRNGs and refined their implementation until
they reached to what SplittableRandom is (and subsequently
ThreadLocalRandom). Even if that method seems sound, a PRNG needs
testing of its statistical properties, and Steele et al tell us that
their SplitMix passes the TestU01 battery of tests.&lt;/p&gt;

&lt;h2 id=&quot;credits&quot;&gt;Credits&lt;/h2&gt;

&lt;p&gt;The fork-join tree image and the explanation comes from the paper by
Guy Steele et al mentioned above.&lt;/p&gt;

&lt;h2 id=&quot;papers-and-links&quot;&gt;Papers and Links&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/concurrent/ThreadLocalRandom.java&quot;&gt;ThreadLocalRandom&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/tip/src/share/classes/java/util/Random.java&quot;&gt;Random&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/SplittableRandom.java&quot;&gt;SplittableRandom&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://en.wikipedia.org/wiki/Linear_congruential_generator&quot;&gt;Linear Congruence Method&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://supertech.csail.mit.edu/papers/dprng.pdf&quot;&gt;Deterministic Parallel Random-Number Generation for Dynamic-Multithreading Platforms&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.thesalmons.org/john/random123/papers/random123sc11.pdf&quot;&gt;Parallel random numbers: as easy as 1, 2, 3&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://dl.acm.org/citation.cfm?id=2660195&quot;&gt;Fast Splittable Pseudorandom Number Generators&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://hal.archives-ouvertes.fr/hal-01098598/document&quot;&gt;ThreadLocalMRG32k3a: A Statistically Sound Substitute to Pseudorandom Number Generation in Parallel Java Applications&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Donald Knuth Was The First Erlang Programmer</title>
   <link href="http://videlalvaro.github.com/2016/10/knuth-first-erlang-programmer.html"/>
   <updated>2016-10-19T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2016/10/knuth-first-erlang-programmer</id>
   <content type="html">&lt;meta name=&quot;twitter:card&quot; content=&quot;summary_large_image&quot; /&gt;

&lt;meta name=&quot;twitter:site&quot; content=&quot;@old_sound&quot; /&gt;

&lt;meta name=&quot;twitter:creator&quot; content=&quot;@old_sound&quot; /&gt;

&lt;meta name=&quot;twitter:title&quot; content=&quot;Donald Knuth Was The First Erlang Programmer&quot; /&gt;

&lt;meta name=&quot;twitter:description&quot; content=&quot;In this blogpost we'll see how Erlang's Syntax already appeared on TAOCP years before the language was even created.&quot; /&gt;

&lt;meta name=&quot;twitter:image&quot; content=&quot;http://videlalvaro.github.io/images/algorithm_e.jpg&quot; /&gt;

&lt;h1 id=&quot;donald-knuth-was-the-first-erlang-programmer&quot;&gt;Donald Knuth Was The First Erlang Programmer&lt;/h1&gt;

&lt;p&gt;Combing through The Art Of Computer Programming (TAOCP) by Donald
Knuth, I’ve found something interesting. Right at the beginning of the
book, where he’s stating the basis of his presentation of algorithms,
he proposes a different notation for expressing algorithms, that seems
to be used only there on section 1.1, and then forgotten for the rest
of the book.&lt;/p&gt;

&lt;p&gt;Here’s the notation I’m talking about, where Knuth presents the
Greatest Common Divisor algorithm.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/algorithm_e.jpg&quot; alt=&quot;Algorithm E&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Which to me looks too similar to Erlang’s Syntax. Uses semi-colons &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;;&lt;/code&gt;
to separate function clauses. Function bodies to be executed are
selected by pattern matching its arguments. The last function clause
ends with a period &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.&lt;/code&gt;. It has a &lt;em&gt;guards&lt;/em&gt; in the form of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if r=0&lt;/code&gt;
execute &lt;em&gt;this&lt;/em&gt;, otherwise &lt;em&gt;that&lt;/em&gt;; and so on.&lt;/p&gt;

&lt;p&gt;Let’s translate it to erlang:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;p&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;ni&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;algorithm_e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;ni&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]).&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;M&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;rem&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;when&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;R&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=:=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;R&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;P&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}).&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;What we did was to write the variable names in uppercase, since that’s
required in Erlang; we delimited tuples using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{}&lt;/code&gt; instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;()&lt;/code&gt; as
expected in erlang; and finally we wrote guards using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;when&lt;/code&gt;
keyword. Also instead of assigning function bodies with the equal sign
(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt;) we used the arrow: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;-&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Interesting curiosity, which proves that the Erlang Syntax is some
sort of Platonic Language Ideal that predates all programming
languages. Now it’s time for &lt;a href=&quot;https://twitter.com/joeerl&quot;&gt;Joe&lt;/a&gt;
and &lt;a href=&quot;https://twitter.com/rvirding&quot;&gt;Robert&lt;/a&gt; to confess where they got
their inspiration from.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Testing Java Libraries from ScalaCheck</title>
   <link href="http://videlalvaro.github.com/2016/07/testing-java-libraries-with-scalacheck.html"/>
   <updated>2016-07-26T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2016/07/testing-java-libraries-with-scalacheck</id>
   <content type="html">&lt;h1 id=&quot;testing-java-libraries-from-scalacheck&quot;&gt;Testing Java Libraries from ScalaCheck&lt;/h1&gt;

&lt;p&gt;In this article I would like to show you how to integrate ScalaCheck
into a Maven project in order to test your Java classes. I will use as
an example the PhoneNumber class as seen on Item 9 on the book
Effective Java. The idea will be to test that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;equals&lt;/code&gt; method
implementation on that class conforms to the &lt;em&gt;equals contract&lt;/em&gt;
according to the Java Specification (more on that later). Also we will
be testing a couple of Netty handlers that could be used to
Encode/Decode a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PhoneBook&lt;/code&gt; object in order to send it on the network.&lt;/p&gt;

&lt;p&gt;The finished project could be found on this Github repo:
&lt;a href=&quot;https://github.com/videlalvaro/phone-guide&quot;&gt;https://github.com/videlalvaro/phone-guide&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;the-phonenumber-class&quot;&gt;The PhoneNumber Class&lt;/h2&gt;

&lt;p&gt;Here’s the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PhoneNumber&lt;/code&gt; class based on Item 9 from the book Effective
Java. This class is special because it overrides the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;equals&lt;/code&gt; method,
so we want to use ScalaCheck to verify that our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;equals&lt;/code&gt;
implementation follows the contract specified in the Java
Specification.&lt;/p&gt;

&lt;p&gt;Here’s the full sourcecode for this class:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kn&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.videlalvaro.phoneguide&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.apache.commons.lang.builder.HashCodeBuilder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/**
 * As seen in Effective Java Item 9
 */&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PhoneNumber&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;areaCode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lineNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;areaCode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lineNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;rangeCheck&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;areaCode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;    &lt;span class=&quot;mi&quot;&gt;999&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;area code&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;rangeCheck&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;      &lt;span class=&quot;mi&quot;&gt;999&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;prefix&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;rangeCheck&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lineNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9999&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;line number&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;areaCode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;areaCode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;prefix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;lineNumber&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lineNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PhoneNumber&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;fromPhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;PhoneNumber&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getAreaCode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getPrefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getLineNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getAreaCode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;areaCode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getPrefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;short&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;getLineNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lineNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;rangeCheck&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;max&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;IllegalArgumentException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;:&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;boolean&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Object&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(!(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;o&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;instanceof&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nc&quot;&gt;PhoneNumber&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;lineNumber&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lineNumber&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;prefix&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prefix&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pn&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;areaCode&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;areaCode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;hashCode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;HashCodeBuilder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;17&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;31&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;areaCode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;prefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lineNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toHashCode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This class goes into the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;src/main/java/org/videlalvaro/phoneguide/PhoneNumber.java &lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;As you can see in the code we also override &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hashCode&lt;/code&gt; since whenever
we override &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;equals&lt;/code&gt; we should override &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hashCode&lt;/code&gt; as well. Here I
took the liberty of using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HashCodeBuilder&lt;/code&gt; class from
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apache-commons&lt;/code&gt;, since it provides helper methods that let you build
a correct implementation of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;hashCode&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now that we have the class we want to test, let’s see how to setup the
build environment.&lt;/p&gt;

&lt;h2 id=&quot;maven-setup&quot;&gt;Maven Setup&lt;/h2&gt;

&lt;p&gt;In this project I’ve decided to use Maven, since it’s one of the most
popular tools for building Java projects. In order to use ScalaCheck
we need to add a couple of libraries as dependencies. Here’s an
excerpt from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pom.xml&lt;/code&gt; file&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;dependencies&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;io.netty&lt;span class=&quot;nt&quot;&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;netty-all&lt;span class=&quot;nt&quot;&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;version&amp;gt;&lt;/span&gt;4.0.36.Final&lt;span class=&quot;nt&quot;&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;scope&amp;gt;&lt;/span&gt;compile&lt;span class=&quot;nt&quot;&gt;&amp;lt;/scope&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;commons-lang&lt;span class=&quot;nt&quot;&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;commons-lang&lt;span class=&quot;nt&quot;&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;version&amp;gt;&lt;/span&gt;2.6&lt;span class=&quot;nt&quot;&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.scala-lang&lt;span class=&quot;nt&quot;&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;scala-library&lt;span class=&quot;nt&quot;&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;version&amp;gt;&lt;/span&gt;${scala.version}&lt;span class=&quot;nt&quot;&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;scope&amp;gt;&lt;/span&gt;test&lt;span class=&quot;nt&quot;&gt;&amp;lt;/scope&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.scalatest&lt;span class=&quot;nt&quot;&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;scalatest_2.10&lt;span class=&quot;nt&quot;&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;version&amp;gt;&lt;/span&gt;2.2.5&lt;span class=&quot;nt&quot;&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;scope&amp;gt;&lt;/span&gt;test&lt;span class=&quot;nt&quot;&gt;&amp;lt;/scope&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.scalatest&lt;span class=&quot;nt&quot;&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;scalatest-maven-plugin&lt;span class=&quot;nt&quot;&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;version&amp;gt;&lt;/span&gt;1.0&lt;span class=&quot;nt&quot;&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;scope&amp;gt;&lt;/span&gt;test&lt;span class=&quot;nt&quot;&gt;&amp;lt;/scope&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;dependency&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.scalacheck&lt;span class=&quot;nt&quot;&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;scalacheck_2.10&lt;span class=&quot;nt&quot;&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;version&amp;gt;&lt;/span&gt;1.12.5&lt;span class=&quot;nt&quot;&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;scope&amp;gt;&lt;/span&gt;test&lt;span class=&quot;nt&quot;&gt;&amp;lt;/scope&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;/dependency&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/dependencies&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;There we specify the dependencies on Scala the language, ScalaTest
which we will use for writing our test suites and running the tests,
and finally there’s the ScalaCheck dependency which we will use to
write our property based tests. We also added the dependencies for
Netty, which we will use later in the project, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;commons-lang&lt;/code&gt;
which gives use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HashCodeBuilder&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;Then we need to configure the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scala-maven-plugin&lt;/code&gt; and the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;scalatest-maven-plugin&lt;/code&gt; which we will use to compile and run our
tests. Here’s what we need to add to our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;pom.xml&lt;/code&gt; file:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;build&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;plugins&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;net.alchim31.maven&lt;span class=&quot;nt&quot;&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;scala-maven-plugin&lt;span class=&quot;nt&quot;&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;version&amp;gt;&lt;/span&gt;3.2.2&lt;span class=&quot;nt&quot;&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;
				&lt;span class=&quot;nt&quot;&gt;&amp;lt;recompileMode&amp;gt;&lt;/span&gt;incremental&lt;span class=&quot;nt&quot;&gt;&amp;lt;/recompileMode&amp;gt;&lt;/span&gt;
				&lt;span class=&quot;nt&quot;&gt;&amp;lt;useZincServer&amp;gt;&lt;/span&gt;true&lt;span class=&quot;nt&quot;&gt;&amp;lt;/useZincServer&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;executions&amp;gt;&lt;/span&gt;
				&lt;span class=&quot;nt&quot;&gt;&amp;lt;execution&amp;gt;&lt;/span&gt;
					&lt;span class=&quot;nt&quot;&gt;&amp;lt;id&amp;gt;&lt;/span&gt;compile&lt;span class=&quot;nt&quot;&gt;&amp;lt;/id&amp;gt;&lt;/span&gt;
					&lt;span class=&quot;nt&quot;&gt;&amp;lt;goals&amp;gt;&lt;/span&gt;
						&lt;span class=&quot;nt&quot;&gt;&amp;lt;goal&amp;gt;&lt;/span&gt;compile&lt;span class=&quot;nt&quot;&gt;&amp;lt;/goal&amp;gt;&lt;/span&gt;
						&lt;span class=&quot;nt&quot;&gt;&amp;lt;goal&amp;gt;&lt;/span&gt;testCompile&lt;span class=&quot;nt&quot;&gt;&amp;lt;/goal&amp;gt;&lt;/span&gt;
					&lt;span class=&quot;nt&quot;&gt;&amp;lt;/goals&amp;gt;&lt;/span&gt;
				&lt;span class=&quot;nt&quot;&gt;&amp;lt;/execution&amp;gt;&lt;/span&gt;
				&lt;span class=&quot;nt&quot;&gt;&amp;lt;execution&amp;gt;&lt;/span&gt;
					&lt;span class=&quot;nt&quot;&gt;&amp;lt;id&amp;gt;&lt;/span&gt;attach-javadocs&lt;span class=&quot;nt&quot;&gt;&amp;lt;/id&amp;gt;&lt;/span&gt;
					&lt;span class=&quot;nt&quot;&gt;&amp;lt;goals&amp;gt;&lt;/span&gt;
						&lt;span class=&quot;nt&quot;&gt;&amp;lt;goal&amp;gt;&lt;/span&gt;doc&lt;span class=&quot;nt&quot;&gt;&amp;lt;/goal&amp;gt;&lt;/span&gt;
						&lt;span class=&quot;nt&quot;&gt;&amp;lt;goal&amp;gt;&lt;/span&gt;doc-jar&lt;span class=&quot;nt&quot;&gt;&amp;lt;/goal&amp;gt;&lt;/span&gt;
					&lt;span class=&quot;nt&quot;&gt;&amp;lt;/goals&amp;gt;&lt;/span&gt;
				&lt;span class=&quot;nt&quot;&gt;&amp;lt;/execution&amp;gt;&lt;/span&gt;
				&lt;span class=&quot;nt&quot;&gt;&amp;lt;execution&amp;gt;&lt;/span&gt;
					&lt;span class=&quot;nt&quot;&gt;&amp;lt;goals&amp;gt;&lt;/span&gt;
						&lt;span class=&quot;nt&quot;&gt;&amp;lt;goal&amp;gt;&lt;/span&gt;doc&lt;span class=&quot;nt&quot;&gt;&amp;lt;/goal&amp;gt;&lt;/span&gt;
					&lt;span class=&quot;nt&quot;&gt;&amp;lt;/goals&amp;gt;&lt;/span&gt;
					&lt;span class=&quot;nt&quot;&gt;&amp;lt;phase&amp;gt;&lt;/span&gt;site&lt;span class=&quot;nt&quot;&gt;&amp;lt;/phase&amp;gt;&lt;/span&gt;
				&lt;span class=&quot;nt&quot;&gt;&amp;lt;/execution&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;/executions&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;c&quot;&gt;&amp;lt;!-- disable surefire --&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.apache.maven.plugins&lt;span class=&quot;nt&quot;&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;maven-surefire-plugin&lt;span class=&quot;nt&quot;&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;version&amp;gt;&lt;/span&gt;2.7&lt;span class=&quot;nt&quot;&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;
				&lt;span class=&quot;nt&quot;&gt;&amp;lt;skipTests&amp;gt;&lt;/span&gt;true&lt;span class=&quot;nt&quot;&gt;&amp;lt;/skipTests&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;c&quot;&gt;&amp;lt;!--enable scalatest--&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;plugin&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;groupId&amp;gt;&lt;/span&gt;org.scalatest&lt;span class=&quot;nt&quot;&gt;&amp;lt;/groupId&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;artifactId&amp;gt;&lt;/span&gt;scalatest-maven-plugin&lt;span class=&quot;nt&quot;&gt;&amp;lt;/artifactId&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;version&amp;gt;&lt;/span&gt;1.0&lt;span class=&quot;nt&quot;&gt;&amp;lt;/version&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;configuration&amp;gt;&lt;/span&gt;
				&lt;span class=&quot;nt&quot;&gt;&amp;lt;reportsDirectory&amp;gt;&lt;/span&gt;${project.build.directory}/surefire-reports&lt;span class=&quot;nt&quot;&gt;&amp;lt;/reportsDirectory&amp;gt;&lt;/span&gt;
				&lt;span class=&quot;nt&quot;&gt;&amp;lt;junitxml&amp;gt;&lt;/span&gt;.&lt;span class=&quot;nt&quot;&gt;&amp;lt;/junitxml&amp;gt;&lt;/span&gt;
				&lt;span class=&quot;nt&quot;&gt;&amp;lt;filereports&amp;gt;&lt;/span&gt;WDF TestSuite.txt&lt;span class=&quot;nt&quot;&gt;&amp;lt;/filereports&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;/configuration&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;executions&amp;gt;&lt;/span&gt;
				&lt;span class=&quot;nt&quot;&gt;&amp;lt;execution&amp;gt;&lt;/span&gt;
					&lt;span class=&quot;nt&quot;&gt;&amp;lt;id&amp;gt;&lt;/span&gt;test&lt;span class=&quot;nt&quot;&gt;&amp;lt;/id&amp;gt;&lt;/span&gt;
					&lt;span class=&quot;nt&quot;&gt;&amp;lt;goals&amp;gt;&lt;/span&gt;
						&lt;span class=&quot;nt&quot;&gt;&amp;lt;goal&amp;gt;&lt;/span&gt;test&lt;span class=&quot;nt&quot;&gt;&amp;lt;/goal&amp;gt;&lt;/span&gt;
					&lt;span class=&quot;nt&quot;&gt;&amp;lt;/goals&amp;gt;&lt;/span&gt;
				&lt;span class=&quot;nt&quot;&gt;&amp;lt;/execution&amp;gt;&lt;/span&gt;
			&lt;span class=&quot;nt&quot;&gt;&amp;lt;/executions&amp;gt;&lt;/span&gt;
		&lt;span class=&quot;nt&quot;&gt;&amp;lt;/plugin&amp;gt;&lt;/span&gt;
	&lt;span class=&quot;nt&quot;&gt;&amp;lt;/plugins&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/build&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;As you can see we are telling maven how to build our Scala test
sources and then how to execute them. For that we first disable
maven’s Surefire to enable the test runner provided by ScalaTest.&lt;/p&gt;

&lt;p&gt;If at this point we run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mvn compile&lt;/code&gt; it should build our project. Now
let’s add some tests.&lt;/p&gt;

&lt;h2 id=&quot;testing-our-equals-implementation&quot;&gt;Testing our equals implementation&lt;/h2&gt;

&lt;p&gt;According to Effective Java, a conforming implementation of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;equals&lt;/code&gt;
should have the following properties:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Reflexive: For any non-null reference value &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x.equals(x)&lt;/code&gt; must return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Symmetric: For any non-null reference values &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x.equals(y)&lt;/code&gt; must return true if and only if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y.equals(x)&lt;/code&gt; returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Transitive: For any non-null reference values &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;z&lt;/code&gt;, if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x.equals(y)&lt;/code&gt; returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y.equals(z)&lt;/code&gt; returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt; then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x.equals(z)&lt;/code&gt; returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt;.&lt;/li&gt;
  &lt;li&gt;Consistent: For any non-null reference values &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt;, multiple invocations of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x.equals(y)&lt;/code&gt; consistently return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt; or consistently return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;, provided no information used in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;equals&lt;/code&gt; comparisons on the objects is modified.&lt;/li&gt;
  &lt;li&gt;For any non-null reference value &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x.equals(null)&lt;/code&gt; must return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s try to use ScalaCheck to test these properties. If you want to
learn more about ScalaCheck, take a look at their &lt;a href=&quot;https://github.com/rickynils/scalacheck/blob/master/doc/UserGuide.md&quot;&gt;user
guide&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In order to test our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PhoneBook&lt;/code&gt; class first we need to tell
ScalaCheck how to generate arbitrary instances of our class. Let’s add
the following code inside the file
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;src/test/scala/org/videlalvaro/phoneguide/generators/Generators.scala&lt;/code&gt;&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;k&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.videlalvaro.phoneguide.generators&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.scalacheck.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Arbitrary&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.videlalvaro.phoneguide.PhoneNumber&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;object&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Generators&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;implicit&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;arbPhoneNumber&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Arbitrary&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Arbitrary&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;genPhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;genPhoneNumber&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;areaCode&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;choose&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;999&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;prefix&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;choose&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;999&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;lineNumber&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Gen&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;choose&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;9999&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;areaCode&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;prefix&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lineNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;There we have a function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;genPhoneNumber&lt;/code&gt; which lets ScalaCheck
generate random values for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;areaCode&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prefix&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lineNumber&lt;/code&gt;
fields, and based on those we &lt;em&gt;yield&lt;/em&gt; new instances of the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PhoneNumber&lt;/code&gt; class.&lt;/p&gt;

&lt;p&gt;Then for ScalaCheck to be able to use our generator we need to provide
an implicit conversion from our generator into ScalaCheck’s
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Arbitrary&lt;/code&gt; class, which is what the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;implicit val arbPhoneNumber:
Arbitrary[PhoneNumber] = Arbitrary(genPhoneNumber)&lt;/code&gt; line is doing.&lt;/p&gt;

&lt;p&gt;With that code in place then we can write code like:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;n&quot;&gt;forAll&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nv&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;should&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Provided that the generator and the arbitrary definitions are in
scope.&lt;/p&gt;

&lt;p&gt;Now let’s see how the test code looks like:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;k&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.videlalvaro.phoneguide&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.scalatest.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Matchers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FunSuite&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.scalatest.prop.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;GeneratorDrivenPropertyChecks&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Checkers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.videlalvaro.phoneguide.generators.Generators._&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PhoneNumberTest&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FunSuite&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;GeneratorDrivenPropertyChecks&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Matchers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;

  &lt;span class=&quot;nf&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;equals should be reflexive&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;forAll&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;should&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;nf&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;equals should be symmetric&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;forAll&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;phoneNumber2&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;fromPhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;should&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;phoneNumber2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;should&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;nf&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;equals should be transitive&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;forAll&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;phoneNumber2&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;fromPhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;phoneNumber3&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;fromPhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;should&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;should&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;phoneNumber2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;should&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;phoneNumber2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;should&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;phoneNumber3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;should&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;phoneNumber3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;should&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;nf&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;equals should be consistent&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;forAll&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;phoneNumber2&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;fromPhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;should&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;should&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;phoneNumber2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;should&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;phoneNumber2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;should&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;nf&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;equals should respect non-nullity&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;forAll&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;should&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;In our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PhoneNumberTest&lt;/code&gt; class we extend the &lt;a href=&quot;http://www.scalatest.org/getting_started_with_fun_suite&quot;&gt;FunSuite&lt;/a&gt; class provided by ScalaCheck that will allow us to write descriptive test functions like the ones in our example: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;test(&quot;equals should be consistent&quot;) {...}&lt;/code&gt;. We also mixed in the &lt;a href=&quot;http://www.scalatest.org/user_guide/generator_driven_property_checks&quot;&gt;GeneratorDrivenPropertyChecks&lt;/a&gt; trait which gives us the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forAll&lt;/code&gt; method required to write our properties. Finally, and as a matter of taste, we mixed the &lt;a href=&quot;http://www.scalatest.org/user_guide/using_matchers&quot;&gt;Matchers&lt;/a&gt; trait that let us write assertions using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;should&lt;/code&gt; and similar &lt;em&gt;matchers&lt;/em&gt;: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;phoneNumber.equals(phoneNumber2) should be (true)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;In our tests we are saying that for all PhoneNumber instances: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forAll { (phoneNumber: PhoneNumber) =&amp;gt;&lt;/code&gt; what follows in the method body should be true, like in our first test, where and object should be equal to itself: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;phoneNumber.equals(phoneNumber) should be (true)&lt;/code&gt;. In a similar way we test the other properties of a correct &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;equals&lt;/code&gt; implementation.&lt;/p&gt;

&lt;p&gt;Let’s execute the tests by running &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mvn test&lt;/code&gt;. If all went well you
should see output like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;Discovery starting.
Discovery completed &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;178 milliseconds.
Run starting. Expected &lt;span class=&quot;nb&quot;&gt;test &lt;/span&gt;count is: 5
PhoneNumberTest:
- equals should be reflexive
- equals should be symmetric
- equals should be transitive
- equals should be consistent
- equals should respect non-nullity
Run completed &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;516 milliseconds.
Total number of tests run: 5
Suites: completed 2, aborted 0
Tests: succeeded 5, failed 0, canceled 0, ignored 0, pending 0
All tests passed.&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;To finish the article, let’s add a couple of Netty encoders and
decoders and see how to test them using ScalaCheck.&lt;/p&gt;

&lt;h2 id=&quot;testing-netty-handlers-using-scalacheck&quot;&gt;Testing Netty Handlers Using ScalaCheck&lt;/h2&gt;

&lt;p&gt;Let’s say that our little phone-guide got distributed and now we need
to send our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PhoneNumber&lt;/code&gt; objects over the network. In Java there’s a
pretty cool library to implement network servers called
&lt;a href=&quot;http://netty.io/&quot;&gt;Netty&lt;/a&gt;. In order to write our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PhoneNumber&lt;/code&gt; objects
to the network we need to write an encoder class, and to be able to
read them back from the network we have to write a decoder class. Then
we provide those classes to Netty and it will know how to call them in
order to send and receive &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PhoneNumber&lt;/code&gt; objects.&lt;/p&gt;

&lt;p&gt;Here’s the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MessageEncoder&lt;/code&gt; implementation that you should store in
the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;src/main/java/org/videlalvaro/phoneguide/netty/MessageEncoder.java &lt;/code&gt;
file:&lt;/p&gt;

&lt;p&gt;And here is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MessageDecoder&lt;/code&gt; class that should go in the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;src/main/java/org/videlalvaro/phoneguide/netty/MessageDecoder.java&lt;/code&gt;
file:&lt;/p&gt;

&lt;p&gt;The implementation of these encoder/decoder classes is quite
simple. We just tell netty how to write each class field to the
network and how to read it back. For more details check the Netty
&lt;a href=&quot;http://netty.io/wiki/index.html&quot;&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In an usual Netty application we would create a channel that will
write or read data from the network, but for a test we don’t want to
setup a whole network stack. Luckily Netty provides an
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;EmbeddedChannel&lt;/code&gt; class that let use simulate writing and reading from
a channel without having to create network connections. Let’s create
the test for our encoder/decoder classes.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-scala&quot; data-lang=&quot;scala&quot;&gt;&lt;span class=&quot;k&quot;&gt;package&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.videlalvaro.phoneguide.netty&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;io.netty.channel.embedded.EmbeddedChannel&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.scalatest.prop.GeneratorDrivenPropertyChecks&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.scalatest.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;FunSuite&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Matchers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.videlalvaro.phoneguide.PhoneNumber&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;org.videlalvaro.phoneguide.generators.Generators._&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;EncoderDecoderTest&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FunSuite&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;GeneratorDrivenPropertyChecks&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Matchers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;test&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;netty encode/decode message&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;forAll&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&amp;gt;&lt;/span&gt;
      &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;channel&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;EmbeddedChannel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MessageEncoder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MessageDecoder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt;

      &lt;span class=&quot;nv&quot;&gt;channel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;writeOutbound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;channel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;writeInbound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;channel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;readOutbound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt;

      &lt;span class=&quot;k&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;readPhoneNumber&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;channel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;readInbound&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;readPhoneNumber&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;should&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;readPhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;should&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;nv&quot;&gt;phoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;py&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;readPhoneNumber&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;should&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;be&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;There we want to test that for all instances of our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PhoneNumber&lt;/code&gt;
class, if we encode it using Netty, we write it to the channel, and
then we read it back and decode it, we get the right &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PhoneNumber&lt;/code&gt;
instance, that is, it should be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;equal&lt;/code&gt; to the one we started from.&lt;/p&gt;

&lt;p&gt;If we now run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mvn test&lt;/code&gt; we should see something like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;Discovery starting.
Discovery completed &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;170 milliseconds.
Run starting. Expected &lt;span class=&quot;nb&quot;&gt;test &lt;/span&gt;count is: 6
EncoderDecoderTest:
- netty encode/decode message
PhoneNumberTest:
- equals should be reflexive
- equals should be symmetric
- equals should be transitive
- equals should be consistent
- equals should respect non-nullity
Run completed &lt;span class=&quot;k&quot;&gt;in &lt;/span&gt;564 milliseconds.
Total number of tests run: 6
Suites: completed 3, aborted 0
Tests: succeeded 6, failed 0, canceled 0, ignored 0, pending 0
All tests passed.&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;As you can see instead of us providing very specific sample instances
of our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PhoneNumber&lt;/code&gt; class to unit tests, we are letting ScalaCheck
generate them so we can verify properties on them. I think this is
quite powerful.&lt;/p&gt;

&lt;h2 id=&quot;wrapping-up&quot;&gt;Wrapping Up&lt;/h2&gt;

&lt;p&gt;In this article we learned how to setup a Java project that will be
tested from ScalaTest using ScalaCheck. Once we had the build system
set up, we implemented our Java class and then went straight to
writing tests for it, seeing how to extend ScalaTest &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FunSuite&lt;/code&gt; so we
could write &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ScalaCheck&lt;/code&gt; style properties in our tests. I hope the
article would be useful for you and let’s you get started with
ScalaCheck even if you are implementing a Java project.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>What We Talk About When We Talk About Distributed Systems</title>
   <link href="http://videlalvaro.github.com/2015/12/learning-about-distributed-systems.html"/>
   <updated>2015-12-04T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2015/12/learning-about-distributed-systems</id>
   <content type="html">&lt;h1 id=&quot;what-we-talk-about-when-we-talk-about-distributed-systems&quot;&gt;What We Talk About When We Talk About Distributed Systems&lt;/h1&gt;

&lt;p&gt;For quite some time now I’ve been trying to learn about distributed
systems, and it’s appropriate to say that once you start digging,
there seems to be no end to it, the rabbit hole goes on and on. The
literature in distributed systems is quite extensive, with lots of
papers coming from different universities, plus quite a few books to
choose from. For a total beginner like me, it proved hard to decide
what paper to read, or what book to buy.&lt;/p&gt;

&lt;p&gt;At the same time, I’ve found that several bloggers recommend this or
that paper that one must know in order to become a &lt;em&gt;distributed
systems engineer&lt;/em&gt; (whatever that means). So the list of things to read
grows:
&lt;a href=&quot;http://cs-www.cs.yale.edu/homes/arvind/cs425/doc/fischer.pdf&quot;&gt;FLP&lt;/a&gt;,
&lt;a href=&quot;http://web.stanford.edu/class/cs347/reading/zab.pdf&quot;&gt;Zab&lt;/a&gt;,
&lt;a href=&quot;http://research.microsoft.com/en-us/um/people/lamport/pubs/time-clocks.pdf&quot;&gt;Time, Clocks and the Ordering of Events in a Distributed Systems&lt;/a&gt;,
&lt;a href=&quot;http://pmg.csail.mit.edu/papers/vr.pdf&quot;&gt;Viewstamped Replication&lt;/a&gt;,
&lt;a href=&quot;http://research.microsoft.com/en-us/um/people/lamport/pubs/lamport-paxos.pdf&quot;&gt;Paxos&lt;/a&gt;,
&lt;a href=&quot;http://static.googleusercontent.com/media/research.google.com/en//archive/chubby-osdi06.pdf&quot;&gt;Chubby&lt;/a&gt;
and on and on. My problem is that many times I don’t see a
justification of &lt;em&gt;why&lt;/em&gt; I should read this or that paper. I love the
idea of learning just for knowledge’s sake, to satisfy curiosity, but
at the same time one needs to prioritise what to read, since the day
only has 24hs.&lt;/p&gt;

&lt;p&gt;Apart from the abundance of papers and research material, as mentioned
above, there are also lots books. Having bought quite a few of them
and read chapters here and there, I started to see that a book that
had a promising title was quite unrelated to what I was looking for,
or that the content didn’t directly target the problems I would have
liked to solve. Therefore, I would like to go over what I think are
the main concepts of distributed systems, citing papers, books or
resources where one can learn about them.&lt;/p&gt;

&lt;p&gt;As I’m continuously learning while writing these words, please have
some patience, expect some mistakes, and be aware that I will try to
expand whatever I end up writing here.&lt;/p&gt;

&lt;p&gt;Before we start, I must tell you that I have presented this blog post
at various conferences, so here are the slides if you are interested:&lt;/p&gt;

&lt;p&gt;
&lt;script async=&quot;&quot; class=&quot;speakerdeck-embed&quot; data-id=&quot;5f16b66144bb4d0ba5aac87488efecf6&quot; data-ratio=&quot;1.7&quot; src=&quot;//speakerdeck.com/assets/embed.js&quot;&gt;&lt;/script&gt;
&lt;/p&gt;

&lt;p&gt;And here’s a video of when I presented this talk at the Erlang User
Conference in Stockholm:&lt;/p&gt;

&lt;p&gt;
&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https://www.youtube.com/embed/yC6b0709HCw&quot; frameborder=&quot;0&quot; allowfullscreen=&quot;&quot;&gt;&lt;/iframe&gt;
&lt;/p&gt;

&lt;p&gt;Let’s start with the article:&lt;/p&gt;

&lt;h2 id=&quot;main-concepts&quot;&gt;Main Concepts&lt;/h2&gt;

&lt;p&gt;Distributed systems algorithms can be classified according to
different kinds of attributes. Some classifications are: the timing
model; the kind of interprocess communication used; the failure model
assumed for the algorithm; and many others as we will see.&lt;/p&gt;

&lt;p&gt;Here are the main concepts we will see:&lt;/p&gt;

&lt;p&gt;
&lt;li&gt;&lt;a href=&quot;http://videlalvaro.github.io/2015/12/learning-about-distributed-systems.html#timing-model&quot;&gt;Timing Model&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://videlalvaro.github.io/2015/12/learning-about-distributed-systems.html#interprocess-communication&quot;&gt;Interprocess Communication&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://videlalvaro.github.io/2015/12/learning-about-distributed-systems.html#failure-modes&quot;&gt;Failure Modes&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://videlalvaro.github.io/2015/12/learning-about-distributed-systems.html#failure-detectors&quot;&gt;Failure Detectors&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://videlalvaro.github.io/2015/12/learning-about-distributed-systems.html#leader-election&quot;&gt;Leader Election&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://videlalvaro.github.io/2015/12/learning-about-distributed-systems.html#consensus&quot;&gt;Consensus&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://videlalvaro.github.io/2015/12/learning-about-distributed-systems.html#quorums&quot;&gt;Quorums&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://videlalvaro.github.io/2015/12/learning-about-distributed-systems.html#time-in-distributed-systems&quot;&gt;Time In Distributed Systems&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://videlalvaro.github.io/2015/12/learning-about-distributed-systems.html#a-quick-look-at-flp&quot;&gt;A Quick Look At FLP&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://videlalvaro.github.io/2015/12/learning-about-distributed-systems.html#conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;http://videlalvaro.github.io/2015/12/learning-about-distributed-systems.html#references&quot;&gt;References&lt;/a&gt;&lt;/li&gt;
&lt;/p&gt;

&lt;h3 id=&quot;timing-model&quot;&gt;Timing Model&lt;/h3&gt;

&lt;p&gt;Here we have the &lt;em&gt;synchronous model&lt;/em&gt;, the &lt;em&gt;asynchronous model&lt;/em&gt; and the
&lt;em&gt;partially synchronous model&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;synchronous model&lt;/strong&gt; is the simplest one to use; here components
take steps simultaneously, in what are called &lt;em&gt;synchronous
rounds&lt;/em&gt;. The time for a message to be delivered is usually known, and
we also can assume the speed of each process, ie.: how long it takes
for a process to execute one step of the algorithm. The problem with
this model is that it doesn’t reflect reality so well, even less in a
distributed system context, where one can send a message to another
process, and wish that the stars are aligned for us, so the message
arrives to said process. The good thing is that using this model it is
possible to achieve theoretical results that later can be translated
to other models. For example, due to the guarantees of this model
about time, we could see that if a problem cannot be solved under
these timing guarantees, then it would probably be impossible to solve
once we relax them (think of a Perfect Failure Detector for instance).&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;asynchronous model&lt;/strong&gt; gets a bit more complex. Here components
can take steps in whatever order they choose and they don’t offer any
guarantee about the speed in which they will take such steps. One
problem with this model is that while it can be simple to describe and
closer to reality, it still doesn’t reflect it properly. For example,
a process might take infinitely long to respond to a request, but in a
real project, we would probably impose a timeout on said request, and
once the timeout expires we will abort the request. A difficulty that
comes with this model is how to assure the liveness condition of a
process. One of the most famous &lt;em&gt;impossibility results&lt;/em&gt;, the
&lt;a href=&quot;http://cs-www.cs.yale.edu/homes/arvind/cs425/doc/fischer.pdf&quot;&gt;“Impossibility of Consensus with one Faulty Process”&lt;/a&gt;
belongs to this timing model, where it is not possible to detect if a
process has crashed, or if the process is just taking an infinitely long
time to reply to a message.&lt;/p&gt;

&lt;p&gt;In the &lt;strong&gt;partially synchronous model&lt;/strong&gt;, components have some
information about timing, having access to almost synchronised clocks,
or they might have approximations of how long messages take to be
delivered, or how long it takes a processes to execute a step.&lt;/p&gt;

&lt;p&gt;The book
&lt;a href=&quot;http://www.amazon.com/Distributed-Algorithms-Kaufmann-Management-Systems/dp/1558603484&quot;&gt;Distributed Algorithms&lt;/a&gt;
by Nancy Lynch is actually organised in sections based on these timing
models.&lt;/p&gt;

&lt;h3 id=&quot;interprocess-communication&quot;&gt;Interprocess Communication&lt;/h3&gt;

&lt;p&gt;Here we need to think about &lt;em&gt;how&lt;/em&gt; processes in the system exchange
information. They can do it by sending messages to each other, in the
&lt;strong&gt;message passing&lt;/strong&gt; model, or by using the &lt;strong&gt;shared memory&lt;/strong&gt; model,
where they share data by accessing shared variables.&lt;/p&gt;

&lt;p&gt;One thing to keep in mind is that we can use a message passing
algorithm to build a distributed shared memory object. A common example
in books is the implementation of a read/write register. We also have
queues and stacks, which are used by some authors to describe
consistency properties, like
&lt;a href=&quot;https://cs.brown.edu/~mph/HerlihyW90/p463-herlihy.pdf&quot;&gt;linearizabilty&lt;/a&gt;. We
should not confuse &lt;em&gt;shared memory&lt;/em&gt; as a way to share data between
processes by accessing a shared variable with &lt;em&gt;shared memory
abstractions&lt;/em&gt; built on top of message passing, like the ones just
mentioned.&lt;/p&gt;

&lt;p&gt;Back to the message passing model, we have another abstraction to
consider when trying to understand algorithms: the kind of link used
between processes (think of channels used to send messages back and
forth between processes). These links will offer certain guarantees to
the algorithm using them. For example there’s the &lt;em&gt;Perfect Links&lt;/em&gt;
abstraction that has reliable delivery and sends no duplicates; this
abstraction also assures exactly once delivery. We can easily see that
this abstraction doesn’t reflect the real world either, therefore
there are other kinds of link abstractions used by algorithm designers
when they try to design models that are closer to real systems. Keep
in mind that even if the &lt;em&gt;Perfect Links&lt;/em&gt; abstraction is not so real,
it can still be useful, for example if we can prove a problem is not
possible to solve even assuming perfect links, then we know of a whole
bunch of related problems which might not be solvable as well. On the
topic of links authors usually consider or assume FIFO message
ordering, like in
&lt;a href=&quot;http://web.stanford.edu/class/cs347/reading/zab.pdf&quot;&gt;Zab&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;failure-modes&quot;&gt;Failure Modes&lt;/h3&gt;

&lt;p&gt;I already wrote an article about
&lt;a href=&quot;http://videlalvaro.github.io/2013/12/failure-modes-in-distributed-systems.html&quot;&gt;failure modes in distributed systems&lt;/a&gt;
but it is worth reiterating here. A property of a distributed system
model is what kind of process failures are assumed. On the
&lt;em&gt;crash-stop&lt;/em&gt; failure mode, a process is assumed to be correct until it
crashes. Once it crashes, it never recovers. There’s also the
&lt;em&gt;crash-recovery model&lt;/em&gt;, where processes can recover after a fault, in
this case, some algorithms also include a way for the process to
recover the state it had before before crashing. This can be done
either by reading from persistent storage or by communicating with
other processes in a group. It’s worth noting that for some group
membership algorithms, a process that crashes and then recovers could
not be considered as the same process that was alive before. This
usually depends if we have dynamic groups or fixed groups.&lt;/p&gt;

&lt;p&gt;There are also failure modes where processes fail to receive or send
messages, these are called &lt;em&gt;omission failure modes&lt;/em&gt;. There are
different kind of omissions as well, a process can fail to receive a
message, or to send a message. Why does this matter? Imagine the
scenario where a group of processes implement a distributed cache. If
a process is failing to reply to requests from other processes on the
group, even though it is able to receive requests from them, that process
will still have its state up to date, which means it can reply to read
requests from clients.&lt;/p&gt;

&lt;p&gt;A more complex failure mode is the one called Byzantine or &lt;em&gt;arbitrary
failures&lt;/em&gt; mode, where processes can send wrong information to their
peers; they can impersonate processes; reply to other process with the
correct data, but garble its local database contents, and more.&lt;/p&gt;

&lt;p&gt;When thinking about the design of a system, we should consider which
kind of process failures we want to cope with. Birman (see
&lt;a href=&quot;http://www.amazon.com/gp/product/1447124154&quot;&gt;Guide to Reliable Distributed Systems&lt;/a&gt;)
argues that usually we don’t need to cope with Byzantine failures. He
cites work done at Yahoo! where they concluded that crash failures are
way more common than Byzantine failures.&lt;/p&gt;

&lt;h3 id=&quot;failure-detectors&quot;&gt;Failure Detectors&lt;/h3&gt;

&lt;p&gt;Depending on the process failure mode and timing assumptions we can
construct abstractions that take care of reporting to the system if a
process has crashed, or if it is suspected to have crashed. There are
&lt;em&gt;Perfect Failure&lt;/em&gt; detectors that never give a false positive. Having a
crash-stop failure mode plus a synchronous system, we can implement
this algorithm by just using timeouts. If we ask processes to
periodically ping back to the Failure Detector Process, we know
exactly when a ping should arrive to the failure detector (due to the
synchronous model guarantees). If the ping doesn’t arrive after a
certain configurable timeout, then we can assume the other node has
crashed.&lt;/p&gt;

&lt;p&gt;On a more realistic system, it might not be possible to always assume
the time needed for a message to reach its destination, or how long
it will take for a process to execute a step. In this case we can have
a failure detector &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt; that would report a process &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q&lt;/code&gt; as &lt;em&gt;suspected&lt;/em&gt;
if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q&lt;/code&gt; doesn’t reply after a timeout of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N&lt;/code&gt; milliseconds. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q&lt;/code&gt; later
replies, then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt; will remove &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q&lt;/code&gt; from the list of suspected
processes, and it will increase &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N&lt;/code&gt;, since it doesn’t know what the
actual network delay between itself and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q&lt;/code&gt; is, but it wants to stop
suspecting &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q&lt;/code&gt; of having crashed, since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q&lt;/code&gt; was alive, as it took
longer than &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N&lt;/code&gt; to ping back. If at some point &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q&lt;/code&gt; crashes,
then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt; will first suspect it has crashed, and it will never revise
its judgement (since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q&lt;/code&gt; will never ping back). A better description
of this algorithm can be found in
&lt;a href=&quot;http://www.amazon.com/Introduction-Reliable-Secure-Distributed-Programming/dp/3642152597/&quot;&gt;Introduction to Reliable and Secure Distributed Programming&lt;/a&gt;
under the name “Eventually Perfect Failure Detector”.&lt;/p&gt;

&lt;p&gt;Failure Detectors usually offer two properties: completeness and
accuracy. For the &lt;em&gt;Eventually Perfect Failure Detector&lt;/em&gt; type, we have
the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Strong Completeness&lt;/strong&gt;: Eventually, every process that crashes is
permanently suspected by every correct process.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Eventual Strong Accuracy&lt;/strong&gt;: Eventually, no correct process is
suspected by any correct process.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Failure detectors have been crucial in solving consensus in the
asynchronous model. There’s a quite famous impossibility result
presented in the
&lt;a href=&quot;http://cs-www.cs.yale.edu/homes/arvind/cs425/doc/fischer.pdf&quot;&gt;FLP&lt;/a&gt;
paper mentioned above. This paper talks about the impossibility of
consensus in asynchronous distributed systems where one process might
fail. One way to go around this impossibility result is to introduce a
failure detector that can
&lt;a href=&quot;http://www.cs.utexas.edu/~lorenzo/corsi/cs380d/papers/p225-chandra.pdf&quot;&gt;circumvent the problem&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;leader-election&quot;&gt;Leader Election&lt;/h3&gt;

&lt;p&gt;Related to the problem of failure detectors is that of actually doing
the opposite, to determine which process hasn’t crashed and is
therefore working properly. This process will then be trusted by other
peers in the network and it will be considered as the leader that can
coordinate some distributed actions. This is the case of protocols
like &lt;a href=&quot;https://raft.github.io&quot;&gt;Raft&lt;/a&gt; or
&lt;a href=&quot;https://web.stanford.edu/class/cs347/reading/zab.pdf&quot;&gt;Zab&lt;/a&gt; that
depend on a leader to coordinate actions.&lt;/p&gt;

&lt;p&gt;Having a leader in a protocol introduces asymmetry between nodes,
since non-leader nodes will then be followers. This will have the
consequence that the leader node will end up being a bottleneck for
many operations, so depending on the problem we are trying to solve,
using a protocol that requires leader election might not be what we
want. Note that most protocols that achieve consistency via some sort
of consensus use a leader process and a set of followers. See
&lt;a href=&quot;http://research.microsoft.com/en-us/um/people/lamport/pubs/lamport-paxos.pdf&quot;&gt;Paxos&lt;/a&gt;,
&lt;a href=&quot;https://web.stanford.edu/class/cs347/reading/zab.pdf&quot;&gt;Zab&lt;/a&gt; or
&lt;a href=&quot;https://raft.github.io&quot;&gt;Raft&lt;/a&gt; for some examples.&lt;/p&gt;

&lt;h3 id=&quot;consensus&quot;&gt;Consensus&lt;/h3&gt;

&lt;p&gt;The consensus or agreement problem was first introduced in the paper
&lt;a href=&quot;http://research.microsoft.com/en-us/um/people/lamport/pubs/reaching.pdf&quot;&gt;Reaching Agreement in the Presence of Faults&lt;/a&gt;
by Pease, Shostak and Lamport. There they introduced the problem like
this:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Fault-tolerant systems often require a means by which independent
 processors or processes can arrive at an exact mutual agreement of
 some kind. It may be necessary, for example, for the processors of a
 redundant system to synchronise their internal clocks periodically. Or
 they may have to settle upon a value of a time-varying input sensor
 that gives each of them a slightly different reading.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So consensus is a problem of reaching agreement among independent
processes. These processes will propose some values for a certain
problem, like what’s the current reading of their sensor, and then
agree on a common action based on the proposed values. For example, a
car might have various sensors providing it with information about the
breaks temperature levels. These readings might have some variations
depending on the precision of each sensor and so on, but the ABS
computer needs to agree on how much pressure it should apply on the
breaks. That’s a consensus problem being solved in our everyday lives.
The book
&lt;a href=&quot;http://www.springer.com/us/book/9780792396574&quot;&gt;Fault-Tolerant Real-Time Systems&lt;/a&gt;
explains consensus and other problems in distributed systems in the
context of the automotive industry.&lt;/p&gt;

&lt;p&gt;A process that implements some form of consensus works via exposing an
API with &lt;em&gt;propose&lt;/em&gt; and &lt;em&gt;decide&lt;/em&gt; functions. A process will propose
a certain value when consensus starts and then it will have to decide on
a value, based on the values that were proposed in the system. These
algorithms must satisfy certain properties, which are: Termination,
Validity, Integrity and Agreement. For example for &lt;em&gt;Regular Consensus&lt;/em&gt;
we have:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Termination&lt;/strong&gt;: Every correct process eventually decides some value.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Validity&lt;/strong&gt;: If a process decides &lt;em&gt;v&lt;/em&gt;, then &lt;em&gt;v&lt;/em&gt; was proposed by some
process.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Integrity&lt;/strong&gt;: No process decides twice.&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Agreement&lt;/strong&gt;: No two correct processes decide differently.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For more details on consensus please consult the original paper
mentioned above. Also the following books are a great reference:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Introduction-Reliable-Secure-Distributed-Programming/dp/3642152597/&quot;&gt;Introduction to Reliable and Secure Distributed Programming&lt;/a&gt;, Chapter 5.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Fault-tolerant-Agreement-Synchronous-Message-passing-Distributed/dp/1608455254/&quot;&gt;Fault-tolerant Agreement in Synchronous Message-passing Systems&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Communication-Abstractions-Fault-tolerant-Asynchronous-Distributed/dp/160845293X/&quot;&gt;Communication and Agreement Abstractions for Fault-tolerant Asynchronous Distributed Systems&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;quorums&quot;&gt;Quorums&lt;/h3&gt;

&lt;p&gt;Quorums are a tool used for designing fault-tolerant distributed
systems. Quorums refer to intersecting sets of processes that can be
used to understand the characteristic of a sytem when some processes
might fail.&lt;/p&gt;

&lt;p&gt;For example if we have an algorithm where N processes have crash-failure
modes, we have a quorum of processes whenever we have a majority of
processes applying certain operation to the system, for example a
write to the database. If a minority of process might crash, that is
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N/2 - 1&lt;/code&gt; process crashes, we still have a majority of processes that
know about the last operation applied to the system. For example
Raft uses majorities when committing logs to the system. The leader
will apply an entry to its state machine as soon as half the servers
in the cluster have replied to its request of log replication. The
leader plus half the servers constitute a majority. This has the
advantage that Raft doesn’t need to wait for the whole cluster to reply
to a log-replication RPC request.&lt;/p&gt;

&lt;p&gt;Another example would be: let’s say we want to limit the access to a
shared resource by one process at a time. This resource is guarded by
a set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt; of processes. Whenever process &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt; wants to access the
resource it needs to first ask permission to a majority of the
processes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt; guarding the resource. A majority of processes in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;
grant access to the resource to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt;. Now process &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q&lt;/code&gt; arrives into the
system and tries to access the shared resource. No matter which
processes it contacts in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;S&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;q&lt;/code&gt; will never arrive to a majority of
processes that will grant it access to the shared resource until the
resource is freed by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt;. See
&lt;a href=&quot;http://www.cs.utexas.edu/~lorenzo/corsi/cs395t/04S/notes/naor98load.pdf&quot;&gt;The Load, Capacity, and Availability of Quorum Systems&lt;/a&gt;
for more details.&lt;/p&gt;

&lt;p&gt;Quorums don’t always refer to a majority of processes. Sometimes they
even need more processes to form a quorum for an operation to succeed,
like in the case of a group &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N&lt;/code&gt; processes that can suffer Byzantine
failures. In this case if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;f&lt;/code&gt; is the number of tolerated process
failures, a quorum will be a set of more than &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(N + f) / 2&lt;/code&gt; processes
See
&lt;a href=&quot;http://www.amazon.com/Introduction-Reliable-Secure-Distributed-Programming/dp/3642152597/&quot;&gt;Introduction to Reliable and Secure Distributed Programming&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you are interested in this topic, there’s a whole book dedicated to
quorums in distributed systems:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/Quorum-Systems-Applications-Consensus-Distributed/dp/1608456838/&quot;&gt;Quorum Systems: With Applications to Storage and Consensus&lt;/a&gt;&lt;/p&gt;

&lt;h3 id=&quot;time-in-distributed-systems&quot;&gt;Time in Distributed Systems&lt;/h3&gt;

&lt;p&gt;Understanding time and its consequences is one of the biggest problems
in distributed systems. We are used to the concept of events in our
life happening one after the other, with a perfectly defined
&lt;a href=&quot;https://en.wikipedia.org/wiki/Happened-before&quot;&gt;happened before&lt;/a&gt;
order, but when we have a series of distributed processes, exchanging
messages, accessing resources concurrently, and so on, how can we tell
which process event happened before? To be able to answer these kinds
of questions, processes would need to share a synchronised clock, and
know exactly how long it takes for electrons to move around the
network; for CPUs to schedule tasks, and so on. Clearly this is not
quite possible on a real-world system.&lt;/p&gt;

&lt;p&gt;The seminal paper that discusses these issues is called
&lt;a href=&quot;http://research.microsoft.com/en-us/um/people/lamport/pubs/time-clocks.pdf&quot;&gt;Time, Clocks, and the Ordering of Events in a Distributed System&lt;/a&gt;
which introduced the concept of logical clocks. Logical Clocks are a
way of assigning a number to an event in the system; said numbers are
not related to the actual passage of time, but to the processing of
events by a node in a distributed system.&lt;/p&gt;

&lt;p&gt;There are many kinds of logical clocks algorithms, like
&lt;a href=&quot;http://zoo.cs.yale.edu/classes/cs426/2012/lab/bib/fidge88timestamps.pdf&quot;&gt;Vector Clocks&lt;/a&gt;
or
&lt;a href=&quot;http://gsd.di.uminho.pt/members/cbm/ps/itc2008.pdf&quot;&gt;Interval Tree Clocks&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;For a very interesting discussion on time in distributed systems I
recommend reading the article
&lt;a href=&quot;https://queue.acm.org/detail.cfm?id=2745385&quot;&gt;There Is No Now&lt;/a&gt; by
Justin Sheehy.&lt;/p&gt;

&lt;p&gt;I would claim that time and its problems in distributed systems is
one of the crucial concepts to understand. &lt;strong&gt;The idea of simultaneity
is something we have to let go&lt;/strong&gt;. This is related with the old belief
of “Absolute Knowledge”, where we used to think that such a thing as
absolute knowledge was attainable. The laws of physics show us that
even light requires some time in order to get from one place to
another, so whenever it reaches our eyes, and it’s processed by our
brains, whatever the light is communicating, it’s an old view of the
world. This idea is discussed by Umberto Eco in the book
&lt;a href=&quot;http://www.amazon.com/Inventing-Enemy-Essays-Umberto-Eco/dp/0544104684&quot;&gt;Inventing the Enemy&lt;/a&gt;,
chapter “Absolute and Relative”.&lt;/p&gt;

&lt;h2 id=&quot;a-quick-look-at-flp&quot;&gt;A Quick look at FLP&lt;/h2&gt;

&lt;p&gt;To finalize this article, let’s take a quick look at the
&lt;strong&gt;Impossibility of Distributed Consensus with One Faulty Process&lt;/strong&gt;
paper to try to relate the concepts we have just learned about
distributed systems.&lt;/p&gt;

&lt;p&gt;The abstract starts like this:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The consensus problem involves an asynchronous system of processes,
 some of which may be unreliable.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So we have an &lt;em&gt;asynchronous system&lt;/em&gt;, where no timing assumptions are
made, either on processing speed or the time required for messages to
reach other processes. We also know that some of these process may
crash.&lt;/p&gt;

&lt;p&gt;The issue here is that in usual technical jargon,
&lt;a href=&quot;https://en.wikipedia.org/wiki/Asynchronous_I/O&quot;&gt;asynchronous&lt;/a&gt; might
refer to a way of processing requests, like RPC for example, where a
process &lt;em&gt;p&lt;/em&gt; sends an asynchronous request to process &lt;em&gt;q&lt;/em&gt;, and while
&lt;em&gt;q&lt;/em&gt; is processing the request, &lt;em&gt;p&lt;/em&gt; keeps doing other things, that is:
&lt;em&gt;p&lt;/em&gt; doesn’t block waiting for a reply. We can see that this definition
is completely different from the one used in the distributed systems
literature, so without having this knowledge, it’s quite hard to fully
understand the meaning of just the first sentence of the FLP paper.&lt;/p&gt;

&lt;p&gt;Later in the paper they say:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;In this paper, we show the surprising result that no completely
 asynchronous consensus protocol can tolerate even a single
 unannounced process death. We do not consider Byzantine failures, and
 we assume that the message system is reliable—it delivers all
 messages correctly and exactly once.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So the paper only considers the &lt;em&gt;crash-stop&lt;/em&gt; failure mode discussed
above (sometimes called &lt;em&gt;fail-stop&lt;/em&gt;). We can also see that there are no omission failures, since the
message system is reliable.&lt;/p&gt;

&lt;p&gt;And finally they also add this constraint:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Finally, we do not postulate the ability to detect the death of a
 process, so it is impossible for one process to tell whether another
 has died (stopped entirely) or is just running very slowly.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So we can’t use failure detectors either.&lt;/p&gt;

&lt;p&gt;To recap, this means that the FLP impossibility applies for asynchronous
systems with fail-stop processors, with access to a reliable message
system, and where detecting the death of a process is not
possible. Without knowing the theory related to the different models
of distributed systems, it might be possible that we miss many of
these details, or we just interpret them in a totally different way
from what the authors meant.&lt;/p&gt;

&lt;p&gt;For a more detailed overview of FLP please take a look at this blog
post:
&lt;a href=&quot;http://the-paper-trail.org/blog/a-brief-tour-of-flp-impossibility/&quot;&gt;A Brief Tour of FLP Impossibility&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Also, it is interesting to read the paper
&lt;a href=&quot;http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.174.8238&amp;amp;rep=rep1&amp;amp;type=pdf&quot;&gt;Stumbling over Consensus Research: Misunderstandings and Issues&lt;/a&gt;
by Marcos Aguilera, which has a discussion about what it means for FLP
to be an &lt;em&gt;impossibility result&lt;/em&gt; for distributed systems (spoiler
alert: is not the same level of &lt;em&gt;impossibility&lt;/em&gt; as the &lt;a href=&quot;https://en.wikipedia.org/wiki/Halting_problem&quot;&gt;halting
problem&lt;/a&gt;).&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;As you can see, learning about distributed systems takes time. It’s a
very vast topic, with tons of research on each of its sub-areas. At
the same time implementing and verifying distributed systems is also
quite complex. There are many subtle places where to commit mistakes
can make our implementations totally broken under unexpected
circumstances.&lt;/p&gt;

&lt;p&gt;What if we choose the wrong quorum and then our new fancy replication
algorithm loses critical data? Or we choose a very conservative quorum
that slows down our application without need, making us break SLAs
with customers? What if the problem we are trying to solve doesn’t
need consensus at all and we can live with eventual consistency?
Perhaps our system has the wrong timing assumptions? Or it uses a
failure detector unfit for the underlying system properties? What if
we decide to optimise an algorithm like Raft, by avoiding a small step
here or there and we end up breaking its safety guarantees? All these
things and many more can happen if we don’t understand the underlying
theory of distributed systems.&lt;/p&gt;

&lt;p&gt;OK, I get it, I won’t reinvent the distributed systems wheel, but with
such vast literature and set of problems, where to start, then? As
stated at the top of this article, I think randomly reading papers will
get you nowhere, as shown with the FLP paper, where understanding the
first sentence requires you to know about the various timing
models. Therefore I recommend the following books in order to get
started:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/gp/product/1558603484&quot;&gt;Distributed Algorithms&lt;/a&gt;
by Nancy Lynch. This book is kinda the bible of distributed
systems. It covers the various models cited above, having sections
with algorithms for each of them.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.amazon.com/Introduction-Reliable-Secure-Distributed-Programming/dp/3642152597/&quot;&gt;Introduction to Reliable and Secure Distributed Programming&lt;/a&gt;
by Christian Cachin et al. Besides from being a very good
introduction, it covers many kinds of consensus algorithms. The book
is full of pseudo-code explaining the algorithms which is a good thing
to have.&lt;/p&gt;

&lt;p&gt;Of course there are many more books, but I think these two are a good
start. If you feel you need to diver deeper, here’s the list of
resources used in this article:&lt;/p&gt;

&lt;h2 id=&quot;references&quot;&gt;References&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.174.8238&amp;amp;rep=rep1&amp;amp;type=pdf&quot;&gt;Marcos K. Aguilera. 2010. Stumbling over consensus research: misunderstandings and issues. In Replication, Bernadette Charron-Bost, Fernando Pedone, and André Schiper (Eds.). Springer-Verlag, Berlin, Heidelberg 59-72.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://gsd.di.uminho.pt/members/cbm/ps/itc2008.pdf&quot;&gt;Paulo Sérgio Almeida, Carlos Baquero, and Victor Fonte. 2008. Interval Tree Clocks. In Proceedings of the 12th International Conference on Principles of Distributed Systems (OPODIS ‘08), Theodore P. Baker, Alain Bui, and Sébastien Tixeuil (Eds.). Springer-Verlag, Berlin, Heidelberg, 259-274.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/gp/product/1447124154&quot;&gt;Kenneth P. Birman. 2012. Guide to Reliable Distributed Systems: Building High-Assurance Applications and Cloud-Hosted Services. Springer Publishing Company, Incorporated.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://static.googleusercontent.com/media/research.google.com/en//archive/chubby-osdi06.pdf&quot;&gt;Mike Burrows. 2006. The Chubby lock service for loosely-coupled distributed systems. In Proceedings of the 7th symposium on Operating systems design and implementation (OSDI ‘06). USENIX Association, Berkeley, CA, USA, 335-350.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Introduction-Reliable-Secure-Distributed-Programming/dp/3642152597/&quot;&gt;Christian Cachin, Rachid Guerraoui, and Luis Rodrigues. 2014. Introduction to Reliable and Secure Distributed Programming (2nd ed.). Springer Publishing Company, Incorporated.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.cs.utexas.edu/~lorenzo/corsi/cs380d/papers/p225-chandra.pdf&quot;&gt;Tushar Deepak Chandra and Sam Toueg. 1996. Unreliable failure detectors for reliable distributed systems. J. ACM 43, 2 (March 1996), 225-267.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Inventing-Enemy-Essays-Umberto-Eco/dp/0544104684&quot;&gt;Umberto Eco. 2013. Inventing the Enemy: Essays. Mariner Books.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://zoo.cs.yale.edu/classes/cs426/2012/lab/bib/fidge88timestamps.pdf&quot;&gt;Colin J. Fidge. 1988. Timestamps in message-passing systems that preserve the partial ordering. Proceedings of the 11th Australian Computer Science Conference 10 (1) , 56–66.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://cs-www.cs.yale.edu/homes/arvind/cs425/doc/fischer.pdf&quot;&gt;Michael J. Fischer, Nancy A. Lynch, and Michael S. Paterson. 1983. Impossibility of distributed consensus with one faulty process. In Proceedings of the 2nd ACM SIGACT-SIGMOD symposium on Principles of database systems (PODS ‘83). ACM, New York, NY, USA, 1-7.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://cs.brown.edu/~mph/HerlihyW90/p463-herlihy.pdf&quot;&gt;Maurice P. Herlihy and Jeannette M. Wing. 1990. Linearizability: a correctness condition for concurrent objects. ACM Trans. Program. Lang. Syst. 12, 3 (July 1990), 463-492.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://research.microsoft.com/en-us/um/people/lamport/pubs/time-clocks.pdf&quot;&gt;Leslie Lamport. 1978. Time, clocks, and the ordering of events in a distributed system. Commun. ACM 21, 7 (July 1978), 558-565.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://research.microsoft.com/en-us/um/people/lamport/pubs/lamport-paxos.pdf&quot;&gt;Leslie Lamport. 1998. The part-time parliament. ACM Trans. Comput. Syst. 16, 2 (May 1998), 133-169.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Distributed-Algorithms-Kaufmann-Management-Systems/dp/1558603484&quot;&gt;Nancy A. Lynch. 1996. Distributed Algorithms. Morgan Kaufmann Publishers Inc., San Francisco, CA, USA.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.cs.utexas.edu/~lorenzo/corsi/cs395t/04S/notes/naor98load.pdf&quot;&gt;Moni Naor and Avishai Wool. 1998. The Load, Capacity, and Availability of Quorum Systems. SIAM J. Comput. 27, 2 (April 1998), 423-447.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://pmg.csail.mit.edu/papers/vr.pdf&quot;&gt;Brian M. Oki and Barbara H. Liskov. 1988. Viewstamped Replication: A New Primary Copy Method to Support Highly-Available Distributed Systems. In Proceedings of the seventh annual ACM Symposium on Principles of distributed computing (PODC ‘88). ACM, New York, NY, USA, 8-17.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://ramcloud.stanford.edu/raft.pdf&quot;&gt;Diego Ongaro and John Ousterhout. 2014. In search of an understandable consensus algorithm. In Proceedings of the 2014 USENIX conference on USENIX Annual Technical Conference (USENIX ATC’14), Garth Gibson and Nickolai Zeldovich (Eds.). USENIX Association, Berkeley, CA, USA, 305-320.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://research.microsoft.com/en-us/um/people/lamport/pubs/reaching.pdf&quot;&gt;M. Pease, R. Shostak, and L. Lamport. 1980. Reaching Agreement in the Presence of Faults. J. ACM 27, 2 (April 1980), 228-234.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Fault-Tolerant-Real-Time-Systems-Determinism-International/dp/079239657X/&quot;&gt;Stefan Poledna. 1996. Fault-Tolerant Real-Time Systems: The Problem of Replica Determinism. Kluwer Academic Publishers, Norwell, MA, USA.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Communication-Abstractions-Fault-tolerant-Asynchronous-Distributed/dp/160845293X/&quot;&gt;Michel Raynal. 2010. Communication and Agreement Abstractions for Fault-Tolerant Asynchronous Distributed Systems (1st ed.). Morgan and Claypool Publishers.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Fault-tolerant-Agreement-Synchronous-Message-passing-Distributed/dp/1608455254/&quot;&gt;Michel Raynal. 2010. Fault-tolerant Agreement in Synchronous Message-passing Systems (1st ed.). Morgan and Claypool Publishers.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://web.stanford.edu/class/cs347/reading/zab.pdf&quot;&gt;Benjamin Reed and Flavio P. Junqueira. 2008. A simple totally ordered broadcast protocol. In Proceedings of the 2nd Workshop on Large-Scale Distributed Systems and Middleware (LADIS ‘08). ACM, New York, NY, USA, , Article 2 , 6 pages.&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://queue.acm.org/detail.cfm?id=2745385&quot;&gt;Justin Sheehy. 2015. There Is No Now. ACM Queue&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Quorum-Systems-Applications-Consensus-Distributed/dp/1608456838/&quot;&gt;Marko Vukolic. 2012. Quorum Systems: With Applications to Storage and Consensus. Morgan and Claypool Publishers.&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Gossip Protocols, Where to Start</title>
   <link href="http://videlalvaro.github.com/2015/12/gossip-protocols.html"/>
   <updated>2015-12-02T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2015/12/gossip-protocols</id>
   <content type="html">&lt;h1 id=&quot;gossip-protocols-where-to-start&quot;&gt;Gossip Protocols, Where to Start&lt;/h1&gt;

&lt;p&gt;This week I started reading about Gossip Protocols, or &lt;em&gt;Epidemic
Protocols&lt;/em&gt; as they are sometimes called as well. It’s quite an
interesting topic in distributed systems and as you might guess, once
you start investigating, the rabbit hole goes on and on.&lt;/p&gt;

&lt;p&gt;Gossip protocols were initially used as a way to maintain consistency
on databases that were replicated at several hundreds sites. From then
on it was seen that the gossiping could be used to solve other
problems, like calculating averages across a network of nodes; or as a
way to build an overlay of nodes in a network. Maintaining node
membership is another problem that’s been tackled with gossiping.&lt;/p&gt;

&lt;p&gt;These protocols usually work like this:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A node in the network randomly selects a peer with which it will
exchange some information.&lt;/li&gt;
  &lt;li&gt;A data exchange process happens between these peers.&lt;/li&gt;
  &lt;li&gt;Each node processes the data it received.&lt;/li&gt;
  &lt;li&gt;These steps are periodically repeated by the nodes in the network as
a way to disseminate information.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These protocols work in a similar fashion as how a disease
disseminates in a population. Nodes are classified as being
&lt;em&gt;infective&lt;/em&gt;, &lt;em&gt;susceptible&lt;/em&gt; and &lt;em&gt;removed&lt;/em&gt;. An infective node will try
to spread some information by periodically selecting a random peer
from the network. If the peer is &lt;em&gt;susceptible&lt;/em&gt;, that is, it doesn’t
know about said information, then it will become infected, and thus
will also start to try to spread this particular information to other
nodes. A &lt;em&gt;removed&lt;/em&gt; node is one that knows about the new piece of
information that circulating around, but it’s not spreading it (for
example if all the neighboring nodes already know the information,
there’s no need to keep spreading it).&lt;/p&gt;

&lt;p&gt;This can also be seen in the light of how rumour is spread. One person
&lt;em&gt;A&lt;/em&gt; first hears a rumour, then calls over the phone someone person &lt;em&gt;B&lt;/em&gt;
in order to share the rumour. Once they hung the phone, &lt;em&gt;B&lt;/em&gt; calls a
third one, let’s say &lt;em&gt;C&lt;/em&gt;, while at the same time &lt;em&gt;A&lt;/em&gt; contacts &lt;em&gt;D&lt;/em&gt; to
share the rumour as well. The process continues on and on until
everyone learns about the rumour. As you can see, this technique can
be used to spread data pretty fast among a network of processes.&lt;/p&gt;

&lt;p&gt;The analysis of these algorithms focuses on designing strategies on
how to best select the peer to share information with. For this
problem, there are mathematical models that prove that using this or
that peer selection technique, the state of the system will converge
in this or that direction after M rounds. In these systems, information
is usually spread in O(log(N)) steps, where N is the number of peers
in the network.&lt;/p&gt;

&lt;p&gt;If you want to get started with this topic, here I recommend some
papers that are quite fundamental when it comes to understanding how
gossiping works:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.cis.upenn.edu/~bcpierce/courses/dd/papers/demers-epidemic.pdf&quot;&gt;Epidemic Algorithms For Replicated Database Maintenance&lt;/a&gt;:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This paper by Alan Demers et al. is considered to be the seminal paper
that introduced gossiping/epidemic algorithms in our industry. It’s a
very interesting read that also shows the problems they were trying to
solve in distributed systems back in the ’80s.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.distributed-systems.net/papers/2007.osr.pdf&quot;&gt;Gossiping in Distributed Systems&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This paper by Anne-Marie Kermarrec and Maarten van Steen tries to give
an overview and build a framework that would help us better understand
and analyse gossiping protocols. Highly recommended as a kind of
“foundations” reading.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://dl.acm.org/citation.cfm?id=1317382&quot;&gt;The promise, and limitations, of gossip protocols&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before drinking the Gossiping Kool Aid, it’s recommended to read this
paper by Ken Birman, in which he describes some of the limitations of
gossip protocols.&lt;/p&gt;

&lt;p&gt;I hope you find this topic interesting and that these papers server
you as a base to get you started.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>This Erlanger Used The Process Dictionary, Look What He Found</title>
   <link href="http://videlalvaro.github.com/2015/05/erlang-process-dict.html"/>
   <updated>2015-05-06T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2015/05/erlang-process-dict</id>
   <content type="html">&lt;h1 id=&quot;this-erlanger-used-the-process-dictionary-look-what-he-found&quot;&gt;This Erlanger Used The Process Dictionary, Look What He Found&lt;/h1&gt;

&lt;p&gt;&lt;img src=&quot;/images/l4grp.jpg&quot; alt=&quot;Erlang Process Dictionary Considered&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Erlang has something called
&lt;a href=&quot;http://www.erlang.org/course/advanced.html#dict&quot;&gt;Process Dictionary&lt;/a&gt;
which is a sort of Key/Value store that belongs to each Erlang
process. We can use it to store values in a simple manner inside Erlang
programs while avoiding the limitations of Pure Functional
Programming. The anti-flames suit is sold separately tho.&lt;/p&gt;

&lt;p&gt;There are
&lt;a href=&quot;http://prog21.dadgum.com/53.html&quot;&gt;some&lt;/a&gt;
&lt;a href=&quot;http://ferd.ca/on-the-use-of-the-process-dictionary-in-erlang.html&quot;&gt;articles&lt;/a&gt;
out there describing the good and bad uses of the process dictionary,
so if you want to get the big picture, go and read there. I won’t be
talking about that.&lt;/p&gt;

&lt;p&gt;What I do want to talk about is something I’ve found while reading
Erlang’s process dictionary implementation. You can find
the source code on Github:
&lt;a href=&quot;https://github.com/erlang/otp/blob/maint/erts/emulator/beam/erl_process_dict.c&quot;&gt;erl_process_dict.c&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;What caught my attention there was the function called
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;erts_dictionary_copy&lt;/code&gt; defined
&lt;a href=&quot;https://github.com/erlang/otp/blob/maint/erts/emulator/beam/erl_process_dict.c#L220&quot;&gt;here&lt;/a&gt;. From
that function name is clear that Erlang
&lt;a href=&quot;https://github.com/erlang/otp/blob/maint/erts/emulator/beam/erl_process_dict.c#L251&quot;&gt;copies&lt;/a&gt;
the process dictionary, but why/when? According to the comments to
that function plus a quick search for places where it is called, we
can see that it is there mostly for reporting purposes. Whenever we
ask Erlang for a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;process_info/1&lt;/code&gt;, that particular process’ dict will
be copied. In the case of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;process_info/2&lt;/code&gt; the dict copying will only
happen if we specifically request for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dictionary&lt;/code&gt; info key.&lt;/p&gt;

&lt;p&gt;Now, if we store lots of things in process dictionaries, (note: we
shouldn’t), then our Erlang app could see a RAM usage spike, or
perhaps OOM on us, who knows. Something similar could happen if we
have to inspect several thousands of processes while accessing their
dicts via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;process_info/1,2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;While using the process dictionary can be a source of discussion about
proper Erlang code etiquette, we can tell for sure that it &lt;em&gt;could&lt;/em&gt; be
problematic on a production system, for the reasons stated above.&lt;/p&gt;

&lt;p&gt;TL;DR: use the process dictionary with care, call
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;erlang:process_info/1,2&lt;/code&gt; with extra care.&lt;/p&gt;

&lt;h2 id=&quot;experiment-time&quot;&gt;Experiment Time&lt;/h2&gt;

&lt;p&gt;If you are brave enough and want an unrealistic experiment, just try
the following:&lt;/p&gt;

&lt;p&gt;Create a module with the following code. The only purpose of this
module is to start a process and store some values on that process
dictionary.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;p&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;ni&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;memory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;ni&quot;&gt;export&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]).&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;spawn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;receive&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nb&quot;&gt;put&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
            &lt;span class=&quot;nf&quot;&gt;loop&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Then start a repl like this: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;erl +K true&lt;/code&gt; and run the following
commands. Note, you’ll need a 3MB file or similar, for science.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;c&quot;&gt;%% compile the module
&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;memory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;%% Load a file, size is arbitrary but a file too big will make the VM
%% crawl with what follows
&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Bin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;read_file&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;my_at_least_3MB_file.something&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;L&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;binary_to_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Bin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Pid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;memory&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;%% keep N low, a value of 100 makes my VM unresponsive in my 16GB of
%% RAM Retina Mac.
&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;N&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;6&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Pid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;set&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Key&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;L&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Key&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;lists&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;seq&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)].&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;%% use Activity Monitor or similar to check beam.smp memory usage.
%% next line will double memory usage since the dictionary will be
%% duplicated
&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;7&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;erlang&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;process_info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;dictionary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;If everything ran as expected, you should have seen the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;beam.smp&lt;/code&gt; RAM
usage spiking to about the double of what was before you called
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;process_info/2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;That was a rather artificial experiment to prove a point, but if
academia taught me anything, is that I should run artificial and
carefully selected experiments to prove my points.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Folding Erlang Records</title>
   <link href="http://videlalvaro.github.com/2015/04/folding-erlang-records.html"/>
   <updated>2015-04-27T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2015/04/folding-erlang-records</id>
   <content type="html">&lt;meta name=&quot;twitter:card&quot; content=&quot;summary_large_image&quot; /&gt;

&lt;meta name=&quot;twitter:site&quot; content=&quot;@old_sound&quot; /&gt;

&lt;meta name=&quot;twitter:creator&quot; content=&quot;@old_sound&quot; /&gt;

&lt;meta name=&quot;twitter:title&quot; content=&quot;Folding Erlang Records&quot; /&gt;

&lt;meta name=&quot;twitter:description&quot; content=&quot;Applying Functional Programming Patterns to Erlang problems.&quot; /&gt;

&lt;meta name=&quot;twitter:image&quot; content=&quot;http://videlalvaro.github.io/images/2283737150_4b7a1775a9_z.jpg&quot; /&gt;

&lt;h1 id=&quot;folding-erlang-records&quot;&gt;Folding Erlang Records&lt;/h1&gt;

&lt;p&gt;&lt;img src=&quot;/images/2283737150_4b7a1775a9_z.jpg&quot; alt=&quot;Fold Your Hands Child&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;April 27 2015&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;When developing the new
&lt;a href=&quot;https://github.com/rabbitmq/rabbitmq-delayed-message-exchange&quot;&gt;RabbitMQ Delayed Exchange&lt;/a&gt;
plugin I had the problem of extracting values from deeply nested
records like the one created for message deliveries in RabbitMQ.&lt;/p&gt;

&lt;p&gt;Imagine you have the following record from which you need to access
the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;headers&lt;/code&gt; filed of the innermost &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#'P_basic'&lt;/code&gt; record.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;nl&quot;&gt;#delivery&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;#basic_message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;#content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;properties&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;#'P_basic'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;H&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}}}}.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Of course we could have pattern matching on a function header to
extract the innermost value, like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;nf&quot;&gt;headers1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;#delivery&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;#basic_message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;#content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;properties&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;#'P_basic'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;H&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}}}})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;H&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Besides from the point that doing this kind of destructuring gets a
bit hairy, what would happen if at some point we want to just access
all the content properties? Or just the basic_message?&lt;/p&gt;

&lt;p&gt;To solve this I decided to have a function to access each specific
property from each record, ending up with these functions:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;nf&quot;&gt;get_msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;#delivery&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;Msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;get_content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;#basic_message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;Content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;get_props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;#content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;properties&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;Props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;get_headers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;#'P_basic'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;H&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;H&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;To use these functions we could call each one in succession and then
return the headers:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;nf&quot;&gt;headers2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Delivery&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;Msg&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Delivery&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;Content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;Props&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;Headers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_headers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;Headers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now in the above situation, what would happen if the field we are
trying to access from the record is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undefined&lt;/code&gt;? One solution would be
to add an extra clause to each of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get_*&lt;/code&gt; functions where we
handle the undefined case:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;nf&quot;&gt;get_msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;get_msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;#delivery&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;Msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;get_content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;get_content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;#basic_message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;content&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;Content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;get_props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;get_props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;#content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;properties&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;Props&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;get_headers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;get_headers&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;#'P_basic'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;headers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;H&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;H&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;While that would work, it seems to me there should be a better
solution. Enter &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foldl&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With lists:foldl/3 we can do the following:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;nf&quot;&gt;headers3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Deliver&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nn&quot;&gt;lists&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;foldl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Acc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Acc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;nv&quot;&gt;Delivery&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_msg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_content&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_props&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_headers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]).&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;When calling this fold using the current record as the accumulator and
a list of functions as the things to apply on each iteration of the
fold. Each successive call will be able to go deeper in the record
structure until it finds the headers we are looking for.&lt;/p&gt;

&lt;p&gt;We still have the problem of what happens when a record property we
try to access returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undefined&lt;/code&gt;. Thanks to Erlang’s pattern matching
on function headers, we can easily address that:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;nf&quot;&gt;headers3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Delivery&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nn&quot;&gt;lists&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;foldl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(_,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
                    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Acc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Acc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;nv&quot;&gt;Delivery&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_msg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_content&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                 &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_props&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_headers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]).&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;With this last version of the function, now we can handle the
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undefined&lt;/code&gt; case but we still have the problem that the fold doesn’t
short circuit the first time it finds and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undefined&lt;/code&gt; value. Let’s add
that:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;nf&quot;&gt;headers4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Delivery&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;lists&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;foldl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(_,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;throw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
                          &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Acc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Acc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                      &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                      &lt;span class=&quot;nv&quot;&gt;Delivery&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                      &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_msg&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_content&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                       &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_props&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;get_headers&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]).&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Here we added a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;throw()&lt;/code&gt; to short-circuit the fold and a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;catch&lt;/code&gt; to
handle that. In the successful case, catch would return the header we
want to access, for all other cases &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;undefined&lt;/code&gt; will be returned as
soon as a record field is undefined.&lt;/p&gt;

&lt;p&gt;By using fold and a list of funs, we can decide how deep in the
record we want to go just by providing the right list of functions.&lt;/p&gt;

&lt;p&gt;I hope this idea of &lt;em&gt;folding Erlang records&lt;/em&gt; is useful to you, which
BTW, looks an awful lot like composition in the Maybe Monad.&lt;/p&gt;

&lt;p&gt;Here’s a gist with the code examples:
&lt;a href=&quot;https://gist.github.com/videlalvaro/e38b990126b0abdae2d5&quot;&gt;fold_example.erl&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;credits&quot;&gt;Credits&lt;/h2&gt;

&lt;p&gt;Photo Credit &lt;a href=&quot;https://flic.kr/p/4tNKus&quot;&gt;Ran Yanviv Hartstein&lt;/a&gt;. License CC BY 2.0.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Harmful GOTOs, Premature Optimizations, and Programming Myths are the Root of all Evil</title>
   <link href="http://videlalvaro.github.com/2015/02/programming-myths.html"/>
   <updated>2015-02-13T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2015/02/programming-myths</id>
   <content type="html">&lt;meta name=&quot;twitter:card&quot; content=&quot;summary_large_image&quot; /&gt;

&lt;meta name=&quot;twitter:site&quot; content=&quot;@old_sound&quot; /&gt;

&lt;meta name=&quot;twitter:creator&quot; content=&quot;@old_sound&quot; /&gt;

&lt;meta name=&quot;twitter:title&quot; content=&quot;Programming Mythds Considered Harmful&quot; /&gt;

&lt;meta name=&quot;twitter:description&quot; content=&quot;Let's revisit some programming myths and see how harmful they have been to our industry.&quot; /&gt;

&lt;meta name=&quot;twitter:image&quot; content=&quot;http://videlalvaro.github.io/images/myths/flamewar.gif&quot; /&gt;

&lt;h1 id=&quot;harmful-gotos-premature-optimizations-and-programming-myths-are-the-root-of-all-evil&quot;&gt;Harmful GOTOs, Premature Optimizations, and Programming Myths are the Root of all Evil&lt;/h1&gt;

&lt;p&gt;What follows is the presentation I gave in the inaugural meeting of
Papers We Love Madrid. The subject of the talk was the paper
Structured Programming with Go To Statements by Donald Knuth.&lt;/p&gt;

&lt;script async=&quot;&quot; class=&quot;speakerdeck-embed&quot; data-id=&quot;3ab49362d8434ad29937726e29a1533d&quot; data-ratio=&quot;1.77777777777778&quot; src=&quot;//speakerdeck.com/assets/embed.js&quot;&gt;&lt;/script&gt;

&lt;blockquote&gt;
  &lt;p&gt;The biggest problem we have as human beings is that we confuse our
beliefs with reality” - Alan Kay.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;The question is whether we should ban it [GOTO], or educate against
 it. –– Donald Knuth&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this article I would like to talk about myths. Google defines
&lt;em&gt;myth&lt;/em&gt; as “a widely held but false belief or idea”, while
dictionary.com tell us: “an unproved or false collective belief that
is used to justify a social institution”. The question is, do we, XXI
century people believe in myths?&lt;/p&gt;

&lt;p&gt;Umberto Eco on The Book of Legendary Lands proposes a simple exercise
to show us how widespread some myths are in our societies, so far in
fact that sometimes we don’t even realize we are talking about things
that are not true. He proposed to ask a person what it was that
Christopher Columbus tried to demonstrate when he wanted to reach the
Indies by traveling west, and what was the common held belief at the
time by the doctors of Salamanca. Many people will answer that
Columbus wanted to prove that the Earth was round, opposing the common
held belief that
&lt;a href=&quot;http://en.wikipedia.org/wiki/Myth_of_the_Flat_Earth&quot;&gt;the Earth was flat&lt;/a&gt;. The
thing is, as Eco demonstrates in his book, people in the Middle Ages
knew all too well that the Earth wasn’t flat. There were even
calculations about the actual diameter of the Earth to see how long
Columbus would have to travel to reach the Indies. The real reason the
establishment opposed Columbus’ plans was in fact based on this
knowledge: they were concerned it would demand too many resources
because of the distance.&lt;/p&gt;

&lt;p&gt;How did such a myth become so widespread? There’s a book called
&lt;em&gt;&lt;a href=&quot;https://archive.org/details/ahistorylifeand08irvigoog&quot;&gt;A History of the Life and Voyages of Christopher Columbus&lt;/a&gt;&lt;/em&gt;
by Washington Irving, who, while trying to document Columbus’ life,
also added some &lt;em&gt;spice&lt;/em&gt; to the story in order improve the entertaining
qualities of his work. At the same time as Irving’s book was making
the rounds, science was trying to bring forth the Theory of Evolution
as opposed to Creationism. If the Church Fathers were wrong about the
shape of the Earth, perhaps these secular thinkers from the XIX
century could show as well how the Church was wrong about
Creationism. They unearthed the theories of a certain Lactantius and
also Cosmas Indicopleustes who claimed back in the 3rd century that
the Earth was flat. So we have on one side a writer trying to improve
the story on his book by adding some &lt;em&gt;facts&lt;/em&gt; here and there, and some
secular thinkers trying to bring forth the Theory of Evolution, giving
birth, or adding fuel, to the myth of the flat Earth.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/A_History_of_the_Life_and_Voyages_of_Christopher_Columbus&quot;&gt;&lt;img src=&quot;/images/myths/Life_and_Voyages_of_Columbus.jpg&quot; alt=&quot;A History of the Life and Voyages of Christopher Columbus&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We have a case of a widespread myth that despite facts to the
contrary, still survives. A myth propagated by people whose intentions
were to bring &lt;em&gt;reason&lt;/em&gt; forward. It looks like a case of the &lt;em&gt;end
justifies the means&lt;/em&gt;. It’s interesting to see how some pseudo-fact get
published on a book, and then scholars just repeat that fact without
checking its veracity, until it snowballs into the present. It is most
probable that the &lt;em&gt;Myth of the Flat Earth&lt;/em&gt; doesn’t affect us at all
today, after more than four hundred years have passed since Columbus
voyages, but the question is, what about our field, computer
science. Do we have similar cases in Software Engineering? Myths that
persist in present days, but for which we don’t even know the origin
or the context in which they were initially created, and whose truth
we actually ignore? I think we do, but more in the shape of certain
&lt;em&gt;maxims&lt;/em&gt; that we all should follow no matter what. Which brings us to
the paper I would like to discuss today,
&lt;a href=&quot;http://dl.acm.org/citation.cfm?id=356640&amp;amp;dl=ACM&amp;amp;coll=DL&quot;&gt;Structured Programming with Go To Statements&lt;/a&gt;
by Donald Knuth.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/myths/structured_programming_with_goto_statements.png&quot; alt=&quot;Structured Programming with Go To Statements&quot; /&gt;&lt;/p&gt;

&lt;p&gt;How did I get interested in this paper? It all started with a simple
question posted as a
&lt;a href=&quot;https://github.com/igorw/retry/issues/3&quot;&gt;Github issue&lt;/a&gt; for a PHP
library. The question is as follows:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;May I ask if there is a reason why you prefer using goto instead of
 function recursion?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The user was referring to the goto statement that appears on line 17
of the following program:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;namespace&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;igorw&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FailingTooHardException&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;extends&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;retry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$retries&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;callable&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;beginning&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;try&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$fn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;\&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$retries&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FailingTooHardException&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;''&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$retries&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;--&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;goto&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;beginning&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The author of the library proceeded to give a
&lt;a href=&quot;https://github.com/igorw/retry/issues/3#issuecomment-56448334&quot;&gt;deep explanation of how the PHP interpreter works&lt;/a&gt;,
giving the reasons why he favored the use of GOTO instead of recursion
as suggested by the user. Of course, this being the internet, a
flamewar was just about to start. One user not only was against the
use of a GOTO statement, but he also linked to the famous paper by
Dijkstra
&lt;a href=&quot;https://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF&quot;&gt;Go To Statement Considered Harmful&lt;/a&gt;. Seeing
so many different opinions on the issue, I wanted to know more.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/myths/flamewar.gif&quot; alt=&quot;flamewar dot gif&quot; /&gt;&lt;/p&gt;

&lt;p&gt;A simple Google search lead me to the Stack Overflow discussion called
&lt;a href=&quot;http://stackoverflow.com/questions/46586/goto-still-considered-harmful&quot;&gt;GOTO still considered harmful?&lt;/a&gt;
which in turn contrasts Dijkstra’s paper with the one by Donald Knuth,
which I have already mentioned, and which is the main subject of this
talk.&lt;/p&gt;

&lt;p&gt;Dijkstra’s paper advocates for the removal of GOTO statements, since
they make understanding programs that use them difficult. It’s
interesting to note some of the story around that paper. Dijkstra had
submitted it as
&lt;a href=&quot;http://www.cs.utexas.edu/~EWD/transcriptions/EWD13xx/EWD1308.html&quot;&gt;A case against the goto statement&lt;/a&gt;,
but in order to speed up its publication, the editor published it
under “letters to the editor” with the new, &lt;em&gt;more interesting&lt;/em&gt;,
title. The editor was Niklaus Wirth, creator of Pascal.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/myths/a_case_against_goto.png&quot; alt=&quot;A Case against the Go To Statement&quot; /&gt;&lt;/p&gt;

&lt;p&gt;It seems the paper created a lot of agitation at the time, not only
against people that were misusing GOTO but also against people that
were writing well structured programs, but which happened to use
GOTOs. These people were offended because now they were also treated
as bad programmers.&lt;/p&gt;

&lt;p&gt;Before finding Knuth’s &lt;em&gt;Structured Programming with Go To Statements&lt;/em&gt;
paper, I had the idea that Columbus was trying to prove that the Earth
was flat, which is to say, that Dijkstra was against all uses of the
GOTO statement. A few paragraphs into Knuth’s paper and we start
finding some gems from private communications between Knuth and
Dijkstra. According to Knuth, Dijkstra wrote:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Please don’t fall into the trap of believing that I am terribly
 dogmatical about [the go to statement]. I have the uncomfortable
 feeling that others are making a religion out of it, as if the
 conceptual problems of programming could be solved by a single trick,
 by a simple form of coding discipline!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Interesting choice of words, of which I would like to underline
&lt;em&gt;religion&lt;/em&gt; and &lt;em&gt;single trick&lt;/em&gt;. For some reason we developers are
always in search of Silver Bullets, looking for Holy Grails that will
solve all our problems. For example, more often than not we see blog
posts by people claiming to have
&lt;a href=&quot;http://ferd.ca/beating-the-cap-theorem-checklist.html&quot;&gt;beaten the CAP Theorem&lt;/a&gt;. A
mathematical theorem… beaten; but in software development silver
bullets don’t exist, as Frederick Brooks Jr. discusses on his paper
called
&lt;a href=&quot;http://worrydream.com/refs/Brooks-NoSilverBullet.pdf&quot;&gt;No Silver Bullet&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;There is no single development, in either technology or management
 technique, which by itself promises even one order of magnitude
 [tenfold] improvement within a decade in productivity, in
 reliability, in simplicity.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Despite all evidence, we still are in the look for the magical formula
that will save us from ourselves.&lt;/p&gt;

&lt;p&gt;Back to the paper. After the intro to his paper, Knuth starts to do an
exhaustive historical revision of who has opposed GOTOs in the history
of programming, mentioning that this goes back as afar as 1959.&lt;/p&gt;

&lt;p&gt;Then the paper starts the first of its main sections: “Elimination of
go to statements”. Here Knuth presents a variety of algorithms that
involve either for loops, or while loops that use GOTO statements here
and there and then Knuth proceed to discuss how to remove them. What’s
interesting here besides the programs themselves, is that they are made
only of the most basic program constructs, like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;while&lt;/code&gt;,
there are no procedures for example, let alone classes. So I started
to wonder, in what era of programming are we in while we read this
paper?&lt;/p&gt;

&lt;p&gt;The paper was published in 1974. Here’s the second code snippet,
called &lt;em&gt;Example 1a&lt;/em&gt;:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/myths/example_1a_knuth.png&quot; alt=&quot;Example 1a&quot; /&gt;&lt;/p&gt;

&lt;p&gt;What’s interesting about this is not the algorithm per se, but what
follows:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The &lt;strong&gt;and&lt;/strong&gt; operation used here stands for McCarthy’s sequential
 conjunction operation.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So in this paper Knuth has to stop and explain what is the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AND&lt;/code&gt;
&lt;a href=&quot;http://en.wikipedia.org/wiki/Short-circuit_evaluation&quot;&gt;short-circuit operator&lt;/a&gt;
that today we take for granted in most programming languages. Let’s
take a diversion for a moment and see what was going on in the
computing circles of the time this paper was published.&lt;/p&gt;

&lt;p&gt;I’ve mention already that the examples in the paper are about basic
programming constructs, like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;for&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;while&lt;/code&gt;. This is 1974 and
Barbara Liskov just published her seminal paper on
&lt;a href=&quot;http://dl.acm.org/citation.cfm?id=807045&amp;amp;dl=ACM&amp;amp;coll=DL&quot;&gt;Programming with Abstract Data Types&lt;/a&gt;. In
a recent talk at
&lt;a href=&quot;http://www.infoq.com/presentations/programming-abstraction-liskov&quot;&gt;InfoQ&lt;/a&gt;,
Liskov said that procedures were the way of doing abstraction, people
didn’t even know what modules were.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/myths/abstract_data_types_liskov.png&quot; alt=&quot;Programming with Abstract Data Types&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Logic Programming is just starting to be a
&lt;em&gt;thing&lt;/em&gt;. &lt;a href=&quot;http://dspace.mit.edu/handle/1721.1/6171&quot;&gt;PLANNER&lt;/a&gt; was
designed in 1969 and in Europe we get
&lt;a href=&quot;http://dl.acm.org/citation.cfm?doid=155360.155362&quot;&gt;Prolog&lt;/a&gt; in 1972.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/myths/the_birth_of_prolog.png&quot; alt=&quot;The Birth of Prolog&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Back in 1964
&lt;a href=&quot;http://web.archive.org/web/20120716185629/http://www.bitsavers.org/pdf/dartmouth/BASIC_Oct64.pdf&quot;&gt;BASIC&lt;/a&gt;
was created, and in 1968 we get ALGOL, the language that introduced
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;begin&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;end&lt;/code&gt; as block delimiters.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/myths/basic.png&quot; alt=&quot;BASIC&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In 1973, UNIX is released to the public, the same year
&lt;a href=&quot;http://www.computerhistory.org/timeline/?year=1973&quot;&gt;Ethernet&lt;/a&gt; was
created. Also the
&lt;a href=&quot;http://worrydream.com/refs/Hewitt-ActorModel.pdf&quot;&gt;ACTOR model&lt;/a&gt; was
developed by Carl Hewitt.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/myths/ethernet.png&quot; alt=&quot;Ethernet&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Another characteristic of Knuth’s paper is that it has the old way of
calculating the complexity of an algorithm by counting its
operations. It will take until 1976 for Knuth to introduce
&lt;a href=&quot;http://www.phil.uu.nl/datastructuren/10-11/knuth_big_omicron.pdf&quot;&gt;Big O notation&lt;/a&gt;
into the programming circles as a way of analyzing algorithm
complexity, a concept that was part of mathematics since 1894. To
bring some more context, 1976 is the same year the Apple I was
released.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/myths/big_omicron_knuth.png&quot; alt=&quot;Big Omicron Knuth&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://archive.org/stream/dieanalytischeza00bachuoft#page/400/mode/2up&quot;&gt;&lt;img src=&quot;/images/myths/big_o_notation.png&quot; alt=&quot;Big Oh Notation in Die analytische Zahlentheorie&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the security side we have that in 1977 the
&lt;a href=&quot;http://www.google.com/patents/US4405829&quot;&gt;RSA algorithm&lt;/a&gt; was presented
to the public.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/myths/rsa_patent_scheme.png&quot; alt=&quot;RSA Original Scheme&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Next year, in 1978 Robin Milner publishes
&lt;a href=&quot;https://courses.engr.illinois.edu/cs421/sp2013/project/milner-polymorphism.pdf&quot;&gt;A Theory of Type Polymorphism in Programming&lt;/a&gt;. In
the same year Leslie Lamport publishes the seminal paper on
distributed systems called
&lt;a href=&quot;http://web.stanford.edu/class/cs240/readings/lamport.pdf&quot;&gt;Time, Clocks, and the Ordering of Events in a Distributed System&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/myths/type_polymorphism_milner.png&quot; alt=&quot;RSA Original Scheme&quot; /&gt;&lt;/p&gt;

&lt;p&gt;So we have a time where personal computers were far from becoming
common place, where public-key encryption as we know it today was
about to appear, and where programming techniques in common use today,
like the use of Abstract Data Types were just starting to be discussed
in academia. Having that in mind we can see why the examples in
Knuth’s paper are all about how to improve loops that use for
statements or GOTOs; or why Knuth has to explain what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AND&lt;/code&gt; means
inside an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt; clause.&lt;/p&gt;

&lt;p&gt;In this context of scarce computer resources we find why Knuth is
interested about extracting the maximum performance out of his
programs. He writes that “most of the running time in non-IO-bound
programs is concentrated in about 3% of the source text”, that there’s
always an “inner-loop” which if we speed it up by 10% then everything
gets the same gains.&lt;/p&gt;

&lt;p&gt;The problem for Knuth is that programmers spend time trying to speed
up the wrong parts of the program, as he said in his turing award
lecture,
&lt;a href=&quot;http://dl.acm.org/citation.cfm?id=361612&quot;&gt;Computer Programming as an Art&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Experience indicates that nearly everybody has the wrong idea about
 the real bottlenecks in his programs&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So for him, “we should forget about small efficiencies, say about 97%
of the time” and continues with the famous quote:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;premature optimization is the root of all evil&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That last sentence has been quoted most of the time out of context,
creating a myth on its own, a complete religion on what to do and what
not to do with regards to program optimization. Is like the
&lt;a href=&quot;http://en.wikipedia.org/wiki/Godwin's_law&quot;&gt;Godwin Law&lt;/a&gt; of
optimization discussions. The problem is we usually omit what comes
next in the paper:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Yet we should not pass up our opportunities in that critical 3%. A
 good programmer will not be lulled into complacency by such
 reasoning, he will be wise to look carefully at the critical code;
 but only after that code has been identified&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The key words on that sentence being “only after that code
[the critical one] has been identified”; and then Knuth reminds us:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;the universal experience of programmers who have been using
 measurement tools has been that their intuitive guesses fail.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It’s clear that Knuth is not forbidding us from performing
optimizations, we shall do them, but only when we know they are going
to help, and of course the only way of knowing is measuring, running
benchmarks on our programs.&lt;/p&gt;

&lt;p&gt;The problem with the &lt;em&gt;root of all evil&lt;/em&gt; quote being cited out of
context is that it seems to be used to forbid all kind of
optimizations, like a sword of Damocles preventing us from even trying
to improve some code.&lt;/p&gt;

&lt;p&gt;As the paper progresses Knuth keeps showing us examples of program
where GOTOs can be removed, but all the while the resulting program
keeps having the same or very similar performance characteristics as
the original one. At the same time, readability and the
understandability of the program are key factors for Knuth when
deciding to refactor the program one way or another. One might wonder
why Knuth is so concerned with the efficiency of his programs. Of
course he has the answer:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;My books emphasize efficiency because they deal with algorithms that
 are used repeatedly as building blocks in a large variety of
 applications&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This can help us put into context the information we are receiving
from this paper. If we think about it, perhaps most of the basic
algorithms in use today have been influenced one way or another by
what Knuth wrote on his TAOCP book.&lt;/p&gt;

&lt;p&gt;Later in the paper Knuth writes:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;We shouldn’t merely remove go to statements because it’s the
 fashionable thing to do; the presence or absence of go to statements
 is not really the issue.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;“The biggest problem we have as human beings is that we confuse our
beliefs with reality” - Alan
Kay. &lt;a href=&quot;http://www.tele-task.de/archive/video/html5/14029/&quot;&gt;Programming and Scaling&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I’ve presented only two cases of sentences or even paper titles that
have out grown their surroundings to become greater than the papers
where they were originally published, but I think they are enough as
examples of
&lt;a href=&quot;http://en.wikipedia.org/wiki/Cargo_cult_programming&quot;&gt;myths that we carry with us&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;On an &lt;a href=&quot;https://queue.acm.org/detail.cfm?id=1039523&quot;&gt;interview&lt;/a&gt;, Alan
Kay discussed why he think some languages with arguably more technical
merits than others go unnoticed, while some other programming
languages become more popular. He understand that we have a problem
with education, with our own education as programmers:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;[in the 70s] computing spread out much, much faster than educating
 unsophisticated people can happen&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;He said that this created a sort of pop-culture of programming where
we started ignoring the fundamentals of our discipline. He adds:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;So I think the lack of a real computer science today, and the lack of
 real software engineering today, is partly due to this pop culture.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And this brings us to two phenomena of today’s programming culture:
Twitter and Hacker News. Discussions that happen in those websites
would lead us to believe that people is forming opinions based in
&lt;a href=&quot;https://news.ycombinator.com/item?id=5473558&quot;&gt;just reading the story titles&lt;/a&gt;. We
live in such &lt;a href=&quot;http://en.wikipedia.org/wiki/Clickbait&quot;&gt;clickbait&lt;/a&gt;
times, than there are even
&lt;a href=&quot;http://www.vice.com/read/this-guy-hates-click-baity-headlines-heres-why&quot;&gt;Twitter accounts&lt;/a&gt;
whose only purpose is to dismantle those baits.&lt;/p&gt;

&lt;p&gt;Once I joked that if a “thought doesn’t fit in 140 characters, then
it’s not a thought worth having”, to which a colleague responded: did
you know there’s a conference for computer science research whose
papers fit in 140 characters or less? Yep, here it is:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://tinytocs.org/&quot;&gt;&lt;img src=&quot;/images/myths/tiny_tocs.png&quot; alt=&quot;Tiny TOCS&quot; /&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I think this trend is taking us away from computer science. We no
longer know what happened in the past. We reinvent methodologies and
give them new names: &lt;a href=&quot;http://thinking-forth.sourceforge.net&quot;&gt;Agile&lt;/a&gt;,
&lt;a href=&quot;https://arialdomartini.wordpress.com/2012/07/20/you-wont-believe-how-old-tdd-is/&quot;&gt;TDD&lt;/a&gt;,
&lt;a href=&quot;http://www.isi.edu/~johnh/RESEARCH/ucla/#ficus_replication&quot;&gt;offline-first&lt;/a&gt;,
to name a few.&lt;/p&gt;

&lt;p&gt;Dijkstra
&lt;a href=&quot;https://www.cs.utexas.edu/users/EWD/transcriptions/EWD13xx/EWD1308.html&quot;&gt;knew&lt;/a&gt;
all too well about this problem:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;In 1968, the Communications of the ACM published a text of mine under
 the title “The goto statement considered harmful”, which in later
 years would be most frequently referenced, regrettably, however,
 often by authors who had seen no more of it than its title&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;His paper more than anything has been “godwin-lawed” at innumerable
discussions, often by just naming its title, ignoring completely the
context, or the remarks made afterwards about it by Dijkstra himself.&lt;/p&gt;

&lt;p&gt;This treatment of knowledge reminds me of a paragraph from Fahrenheit
451, when the chief fireman explains to Mr. Montag what was the reason
why they banned books:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Classics cut to fit fifteen-minute radio shows, then cut again to
 fill a two-minute book column, winding up at last as a ten- or
 twelve-line dictionary resume.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Why is it that we prefer to spread only these &lt;em&gt;digests&lt;/em&gt; of great
papers and ideas from the past, to form these myths? I believe there’s
a simple answer and a more complex, related one. The simple answer has
to do with laziness and the need to appear smart. On an agitated
discussions is better to drop a paper title than to be beaten by other
people’s arguments. Of course I’m being sarcastic.&lt;/p&gt;

&lt;p&gt;I believe the more complex answer is related to memes as explained by
Richard Dawkins on his book
&lt;a href=&quot;http://en.wikipedia.org/wiki/The_Selfish_Gene&quot;&gt;The Selfish Gene&lt;/a&gt;. All
these one-sentence programming maxims are easy to repeat and to
follow, than to actually learning the whole methodology. As Knuth says
on his paper:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;[…] there has been far too much emphasis on go to elimination instead
 of on the really important issues; people have a natural tendency to
 set up all easily understood quantitative goal like the abolition of
 jumps, instead of working directly for a qualitative goal like good
 program structure&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It’s easier to count lines of code produced by an programmer that the
actual quality of the work they produce.&lt;/p&gt;

&lt;p&gt;At the end of the day we should focus on what matters, we should
internalize that we are always making choices between different kind
of tradeoffs, but in our case, programs that are easier to understand
should triumph over complicated ones:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;we should strive most of all for a program that is easy to understand
 and almost sure to work&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Finally with regards to our own discipline, let’s close by reminding
us of Dijkstra’s words:&lt;/p&gt;

&lt;p&gt;“we had better see to it that the computer industry does not kill
computing science” from Dijkstra on
&lt;a href=&quot;https://www.cs.utexas.edu/users/EWD/transcriptions/EWD12xx/EWD1284.html&quot;&gt;Computing Science: Achievements and Challenges&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;acknowledgments&quot;&gt;Acknowledgments:&lt;/h2&gt;

&lt;p&gt;I would like to thank &lt;a href=&quot;https://twitter.com/miguelinlas3&quot;&gt;Miguel Pastor&lt;/a&gt; and &lt;a href=&quot;https://twitter.com/flopezluis&quot;&gt;Félix López&lt;/a&gt; for inviting me to
present at
&lt;a href=&quot;http://www.meetup.com/Papers-We-Love-Madrid/&quot;&gt;Papers We Love Madrid&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;At the same time I would like to thank &lt;a href=&quot;https://twitter.com/leifwalsh&quot;&gt;Leif Walsh&lt;/a&gt; for helping me by
proof reading an early draft of this article. I have to add that
whatever errors or typos that remain in this paper are entirely my
fault.&lt;/p&gt;

&lt;h2 id=&quot;resources&quot;&gt;Resources:&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Myth_of_the_Flat_Earth&quot;&gt;Myth of the Flat Earth&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://archive.org/details/ahistorylifeand08irvigoog&quot;&gt;A History of the Life and Voyages of Christopher Columbus&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://dl.acm.org/citation.cfm?id=356640&quot;&gt;Structured Programming with Go To Statements&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/igorw/retry/issues/3&quot;&gt;Retry library, GOTO Usage&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://github.com/igorw/retry/issues/3#issuecomment-56448334&quot;&gt;Explanation of how the PHP compiler works&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.cs.utexas.edu/users/EWD/ewd02xx/EWD215.PDF&quot;&gt;Go To Statement Considered Harmful&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://stackoverflow.com/questions/46586/goto-still-considered-harmful&quot;&gt;GOTO still considered harmful?&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.cs.utexas.edu/~EWD/transcriptions/EWD13xx/EWD1308.html&quot;&gt;A Case against the Go To Statement&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://ferd.ca/beating-the-cap-theorem-checklist.html&quot;&gt;Beating the CAP Theorem&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://worrydream.com/refs/Brooks-NoSilverBullet.pdf&quot;&gt;No Silver Bullet&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Short-circuit_evaluation&quot;&gt;Short-Circuit Operator&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://dl.acm.org/citation.cfm?id=807045&amp;amp;dl=ACM&amp;amp;coll=DL&quot;&gt;Programming with Abstract Data Types&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.infoq.com/presentations/programming-abstraction-liskov&quot;&gt;The Power of Abstraction, B. Liskov InfoQ Keynote&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://dspace.mit.edu/handle/1721.1/6171&quot;&gt;PLANNER: A Language for Manipulating Models and Proving Theorems in a Robot&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://dl.acm.org/citation.cfm?doid=155360.155362&quot;&gt;The birth of Prolog&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://web.archive.org/web/20120716185629/http://www.bitsavers.org/pdf/dartmouth/BASIC_Oct64.pdf&quot;&gt;BASIC&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://research.microsoft.com/en-us/um/people/pcosta/cn_slides/metcalfe76ethernet.pdf&quot;&gt;Ethernet: Distributed Packet Switching for Local Computer Networks&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://worrydream.com/refs/Hewitt-ActorModel.pdf&quot;&gt;A Universal Modular ACTOR Formalism for Artificial Intelligence&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.phil.uu.nl/datastructuren/10-11/knuth_big_omicron.pdf&quot;&gt;BIG OMICRON AND BIG OMEGA AND BIG THETA&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://archive.org/stream/dieanalytischeza00bachuoft#page/400/mode/2up&quot;&gt;Die analytische Zahlentheorie&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.google.com/patents/US4405829&quot;&gt;RSA Algorithm Patent&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://courses.engr.illinois.edu/cs421/sp2013/project/milner-polymorphism.pdf&quot;&gt;A Theory of Type Polymorphism in Programming&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://web.stanford.edu/class/cs240/readings/lamport.pdf&quot;&gt;Time, Clocks, and the Ordering of Events in a Distributed System&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://dl.acm.org/citation.cfm?id=361612&quot;&gt;Computer Programming as an Art&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Godwin's_law&quot;&gt;Godwin Law&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.tele-task.de/archive/video/html5/14029/&quot;&gt;Programming and Scaling, Talk by Alan Kay&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Cargo_cult_programming&quot;&gt;Cargo Cult Programming&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://queue.acm.org/detail.cfm?id=1039523&quot;&gt;A Conversation with Alan Kay&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://news.ycombinator.com/item?id=5473558&quot;&gt;HN just reading the story titles&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/Clickbait&quot;&gt;Clickbait&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.vice.com/read/this-guy-hates-click-baity-headlines-heres-why&quot;&gt;Saved You A Click&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://tinytocs.org/&quot;&gt;Tiny ToCS&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://thinking-forth.sourceforge.net&quot;&gt;Thinking Forth&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://arialdomartini.wordpress.com/2012/07/20/you-wont-believe-how-old-tdd-is/&quot;&gt;You Won’t Believe How Old TDD Is&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.isi.edu/~johnh/RESEARCH/ucla/#ficus_replication&quot;&gt;Ficus Replication&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.cs.utexas.edu/users/EWD/transcriptions/EWD13xx/EWD1308.html&quot;&gt;What led to “Notes on Structured Programming”&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://en.wikipedia.org/wiki/The_Selfish_Gene&quot;&gt;The Selfish Gene&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.cs.utexas.edu/users/EWD/transcriptions/EWD12xx/EWD1284.html&quot;&gt;Computing Science: Achievements and Challenges&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>A Programmer's Role</title>
   <link href="http://videlalvaro.github.com/2014/09/a-programmers-role.html"/>
   <updated>2014-09-02T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2014/09/a-programmers-role</id>
   <content type="html">&lt;h1 id=&quot;a-programmers-role&quot;&gt;A Programmer’s Role&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;September 02 2014&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The other day I found an article from 1967 that could easily be called
“Clean Code, 1967 edition”. The article explains what separates bad
programmers from good ones, and bad programs from good ones.&lt;/p&gt;

&lt;p&gt;One of the main themes of the article is how to make our programs so
that they survive the passage of time, &lt;em&gt;entropy&lt;/em&gt; as they called it
back then, or &lt;em&gt;code rot&lt;/em&gt; as textbooks call it today. So one of the
main features a program can have is its ability to adapt:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;A program is rarely killed by a failure of static stability. The
death usually occurs because of a failure in the program’s mechanisms
for maintaining stability, in its abilities to respond to
environmental change.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One of the main ways to achieve that is to work on making our programs
easier to understand for other programers, and for our future selves:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Thus a programmer develops his program so that a human can comprehend
it. Initially this is merely self-protective, as he must understand
the program enough to get into production. It would appear that all
good programmers attempt to do this, whether they recognize it or
not.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The following quote is the typical one where I think, wow, why it
didn’t occur to me to express this idea like that:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Furthermore, no one has seen a program which the machine could not
comprehend but which humans did.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The usual programmer doing &lt;em&gt;smart&lt;/em&gt; things that nobody else can
understand later comes immediately to mind. Programs should communicate
our intent, tell others about the problem we are trying to solve,
something that we could argue that is better done with languages that
encourage a more declarative style of programming.&lt;/p&gt;

&lt;h2 id=&quot;what-a-programmer-does&quot;&gt;What a programmer does&lt;/h2&gt;

&lt;p&gt;For me and probably the people reading this blogpost, this all might
seem rather obvious, on the other hand, almost 50 years have passed
and the problem of building maintainable software still
exists. Countless languages have appeared, programming methodologies
and what not, but we still somehow fail. I think the author of this
article has a clear lead on how to address this problem:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;A programmer does not primarily write code; rather, he primarily
writes to another programmer about his problem solution. The
understanding of this fact is the final step in his maturation as
technician.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We should program for other humans and forget about computers. If our
code is legally correct, the computer will for sure understand it.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Both the value and quality of a programmer’s work improve directly
with the importance he places on communicating his program to a
human, rather than merely to the machine.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Here’s the link to the full article:
&lt;a href=&quot;http://archive.computerhistory.org/resources/text/Knuth_Don_X4100/PDF_index/k-9-pdf/k-9-u2769-1-Baker-What-Programmer-Does.pdf&quot;&gt;What a Programmer Does&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;NOTE: It’s interesting to see that in those days, &lt;em&gt;the programmer&lt;/em&gt;
was usually mentioned as &lt;em&gt;“he”&lt;/em&gt;, even in papers/books written by women,
like Jean Sammet. Since I was quoting this article, I left the use of
the gendered pronoun as it was used those days.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>On Contributing to Open Source</title>
   <link href="http://videlalvaro.github.com/2014/08/on-contributing-to-opensource.html"/>
   <updated>2014-08-29T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2014/08/on-contributing-to-opensource</id>
   <content type="html">&lt;h1 id=&quot;on-contributing-to-open-source&quot;&gt;On Contributing to Open Source&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;August 29 2014&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The other day I was reading
&lt;a href=&quot;http://symfony.es/noticias/2014/07/24/los-errores-de-la-pagina-28/&quot;&gt;a blog post in Spanish&lt;/a&gt;
where it was discussed how people and companies contribute to open
source. Many people shared their views in the comments section, on why
to collaborate, why not to collaborate, about why some certifications
should be cheaper, about rewards to contributors and what not. The
whole discussion prompted me to write this blog post, sharing what I
think one could do in order to contribute back to the open source
community, both as an individual or as a company.&lt;/p&gt;

&lt;p&gt;In my particular case, I have benefited greatly from open source. In
fact, I think I wouldn’t be where I am today in my career if it weren’t
for open source. I remember back in the days starting with PHP and
Apache to create my first web page. Open source allowed me to find
source code written by others, read it, and learn how to do my
things. My first iteration over a database result-set was probably some
copy pasted code that I found online somewhere on the internet. But
that’s not all. Later in my career, once I had got a job as a
programmer, I found myself getting interested in Symfony, being
probably one of the first users in Uruguay developing a project for a
company using the framework. The involvement with Symfony led me to
find my job in Shanghai, where I moved with my wife, over 6 years ago.&lt;/p&gt;

&lt;p&gt;Open source then led me to learn many other things, the most salient
of those was probably Erlang and in turn RabbitMQ. My involvement with
that community led me to end up writing a book about it, presenting
at numerous conferences around the world explaining what RabbitMQ is,
and why it was great using it. Once the book got published, I even got
an offer from VMware to work for them as part of the Cloud Foundry and
the RabbitMQ teams. So I can say that open source has really helped in
my career. With that out of the way, let’s see how I think we can
contribute to open source.&lt;/p&gt;

&lt;p&gt;The most obvious way to contribute to open source is by contributing
code. &lt;strong&gt;you_dont_say.gif&lt;/strong&gt;. This of course requires that we know
about the internals of the project where we are trying to contribute,
so perhaps fixing a little bug can turn into a big challenge. At the
same time this presents a great opportunity for us for learning about
technology X in more detail, so there are two sides to the
story. Another problem this might have are projects that have very high
requirements in order to send some code back. For example, Erlang has
a very
&lt;a href=&quot;https://github.com/erlang/otp/wiki/submitting-patches&quot;&gt;strict set of requirements&lt;/a&gt;
that at the beginning might look like a burden. &lt;em&gt;I just wanted to fix
issue X&lt;/em&gt;. On the other hand, if we follow them, we might actually
learn a lot. For me fixing a little bug for Erlang was quite the
experience, trying to follow their guidelines, and even learning about
how to use git in order to send my PR. On the other hand, I must
confess that I was really happy when my name appeared in the release
notes.&lt;/p&gt;

&lt;p&gt;Another problem is that there are projects that have the source out
there, but they aren’t really quite &lt;em&gt;open source&lt;/em&gt;, in the sense that
their authors don’t accept contributions, or they use a very arcane
VCS that doesn’t work in our platform, or for which we don’t have any
interest in learning. I remember having some things I wanted to
contribute back to &lt;a href=&quot;http://graphite.wikidot.com&quot;&gt;Graphite&lt;/a&gt;, but their
use of &lt;a href=&quot;http://bazaar.canonical.com/en/&quot;&gt;Bazaar&lt;/a&gt; put me down.&lt;/p&gt;

&lt;p&gt;Then there’s time, some say it’s our only resource. Another reason why
we might not be able to contribute is just because we don’t have the
time to do it. Probably sharing time with our family/friends seems
more rewarding than trying to send a patch back to a project, and who
is going to blame us for that?&lt;/p&gt;

&lt;p&gt;Related to the time issue, there are companies that don’t let their
programmers spend time on external projects. I think this is not
good. Those companies are usually taking a big advantage from the code
written by others, but then they don’t let their developers to
contribute back. I’m quite happy to say that the companies where I had
worked didn’t have that problem, on the contrary, they motivated
employees to contribute back to open source.&lt;/p&gt;

&lt;p&gt;Something to take into account when sending code contributions is, as
someone said on Twitter, that we are giving the project maintainer some
more code to be responsible for. Sometimes we send a 20 lines of code
pull request, it gets merged, and we forget about the issue. That’s
not the case for the project maintainer. They have to live with our
changes for ever.&lt;/p&gt;

&lt;p&gt;Also if we don’t want to send PRs to other projects, maybe we can
create a satellite project that helps users of project X. For example,
while using Symfony at &lt;a href=&quot;http://www.thenetcircle.com&quot;&gt;The NetCircle&lt;/a&gt; we
created a couple of libraries to facilitate using Propel, Memcached
and some other things with Symfony. In my case I started maintaining
libraries to access
&lt;a href=&quot;https://github.com/videlalvaro/php-amqplib&quot;&gt;RabbitMQ from PHP&lt;/a&gt;, just
to name an example.&lt;/p&gt;

&lt;h2 id=&quot;beyond-the-code&quot;&gt;Beyond the Code&lt;/h2&gt;

&lt;p&gt;So let’s say contributing code is out of the question. What else could
we do if we wanted to give back to the open source community?&lt;/p&gt;

&lt;p&gt;We can write documentation, all kinds of it. Whether it is actually
improving the project guides or tutorials, if any, or writing your own
blog posts. You spent a day trying to figure out how to run RabbitMQ
on EC2? Why not write about it? Why not share it back with the
community? You don’t have a blog of your own? Well, why not ask the
project maintainers to publish your guide in their blog. I remember
sending articles to Fabien Potencier about Symfony that he kindly
published in the framework’s official blog. At the same time, some
people might engage with our blog posts, even correcting our mistakes,
or showing us better ways of doing things, and we will all benefit
from it.&lt;/p&gt;

&lt;p&gt;Related to writing docs, is actually taking part in discussions inside
the community, like being part of a mailing list or IRC channels. Many
people resort to mailing lists in order to paste some code and wait
for an answer to their problems. We could position ourselves on the
other end of the food chain and be the person actually answering
questions. We don’t need to answer every question, we could
participate from time to time and that will be fine too. Even if our
contributions are small, think that one answer we give on a mailing
list, or on stack overflow could potentially save a wasted day from
other’s people time. Here I come back to companies that don’t want to
contribute to the community. Don’t they realize that some volunteer on
a mailing list, just saved one of their developers a day of work?&lt;/p&gt;

&lt;p&gt;What about conferences? There are tons of conferences and user groups
around the world. We can contribute to project X by just submitting a
talk on a project we have experience and know about, and then help
others understanding how to use it. Depending on the conference, you
could even be invited to travel to another country, including plane
tickets and paid hotel. Not to mention that you will make friends and
meet other like minded people. If you think about your career, you
will be putting your name out there, perhaps at some point even
getting an invitation for a new job opportunity.&lt;/p&gt;

&lt;p&gt;We don’t need to speak at conferences if we are shy or simply don’t
want to do it. We could also organize our own, from a local meetup to
a full conference. I know a lot of friends that organize conferences
as a way to contribute back to the community they are in.&lt;/p&gt;

&lt;h2 id=&quot;gimme-the-money&quot;&gt;Gimme the money&lt;/h2&gt;

&lt;p&gt;Another obvious way is to actually contribute to the project with
money. Developers working in open source usually have to eat as
well. Some companies decide to donate to the actual project, or pay to
prioritize certain features. Then there are companies that pay for the
whole development of a project, by employing the core team of a
project for example.&lt;/p&gt;

&lt;p&gt;Also from the financial side, what some companies do is to sponsor a
workshop for the community. Last year
&lt;a href=&quot;http://shuttlecloud.com&quot;&gt;Shuttle Cloud&lt;/a&gt; sponsored a RabbitMQ workshop
in Madrid, by covering my hotel and flight costs so I could deliver
the workshop. They also provided food for the attendees and took care
of the logistics for getting a room, projector and so on. Why did they
do that? Their team was working hard on their product, so they didn’t
have much time for contributing to open source, but on the other hand
they felt they were getting a lot from open source, so they decided
that hosting the workshop was their way of giving back. BTW, if you
want to host a RabbitMQ workshop in your city, get in touch with me.&lt;/p&gt;

&lt;h2 id=&quot;whats-rewarding-about-contributing-to-open-source&quot;&gt;What’s rewarding about contributing to open source?&lt;/h2&gt;

&lt;p&gt;I listed some of the ways we could contribute to open source, whether
as a company or as individuals. Now I want to share one of the reasons
why I think it’s great doing it. From my point of view (probably a
very hippie one) at the end of the day what matters is that behind
many people working in the industry, there’s a father or a mother that
needs to bring food to their table. For example, one day in China, at
a conference, a group of developers approached me and asked me to take
a group picture with them. The reason? One of my open source libraries
helped them release their product and their boss was really happy with
them and their performance. For me, seeing them so happy there, made
my day, and certainly made me happy about having contributed to that
library. Many times I find people that tell me that my book helped
them do their jobs. For me that’s what matters, to know that at least
for a second, someone’s life got a little bit easier (end of hippie
rant).&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Replication Techniques: some papers and an interesting book</title>
   <link href="http://videlalvaro.github.com/2014/08/some-papers-and-an-interesting-book.html"/>
   <updated>2014-08-27T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2014/08/some-papers-and-an-interesting-book</id>
   <content type="html">&lt;h1 id=&quot;replication-techniques-some-papers-and-an-interesting-book&quot;&gt;Replication Techniques: some papers and an interesting book&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;August 27 2014&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;In this post I would like to share some papers I’ve found this week,
along with a book on database replication techniques.&lt;/p&gt;

&lt;p&gt;This week I started doing some research about replication in
distributed systems. My first stop was a book called
&lt;a href=&quot;http://www.amazon.com/Replication-Techniques-Distributed-Advances-Database/dp/0792398009/&quot;&gt;Replication Techniques in Distributed Systems&lt;/a&gt;
which has quite a few interesting quotes about what are distributed
systems, specifically, how we should always expect failures when
dealing with these kind of systems, for example:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;A distributed computing system, being composed of a large number of
computers and communication links, must almost always function with
some part of it broken.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And continues:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Over time, only the identity and number of the failed components
change.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And that’s literally just the first paragraph of the book’s
introduction.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;https://twitter.com/old_sound/status/502916201224622080&quot;&gt;I joked on Twitter&lt;/a&gt;
that this book is a manual of &lt;em&gt;“how to drop the mic while discussing
distributed systems”&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;If we know the system will have failures, we need to think about how
to achieve fault tolerance and there’s where the discussion of the
book lead us to by presenting replication techniques.&lt;/p&gt;

&lt;p&gt;It’s interesting to see how the authors mention that from the point of
view of applications, the source of the failures, whether they are
bugs or mechanical failures, they just don’t matter. The application
(the user of the distributed system) simply cares about what are the
reliability and availability properties of the system.&lt;/p&gt;

&lt;h2 id=&quot;availability-vs-reliability&quot;&gt;Availability vs Reliability&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;A reliable system is not necessarily highly available.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The book explains the difference between availability and
reliability. A system is “highly available” if its fraction of
downtime is very small; on the other hand, a system is reliable if it
manages to stay up for the amount of time required to perform
computations in order to serve requests.&lt;/p&gt;

&lt;p&gt;For example, a car can be &lt;em&gt;highly available&lt;/em&gt;, that is, it always goes
on when you turn the ignition, but it might have a failure in the
radiator, (it overheats), so is not able to function long enough to
take you were you wanted to go, that is, is not &lt;em&gt;reliable&lt;/em&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;In short, a system is reliable to the extent that it is able
successfully to complete a service request, once it accepts it. To
the end user therefore, a system is perceived reliable if
interruptions of on-going service is rare.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Another pearl from the book:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;As the number of sites involved in a computation increases, the
likelihood decreases that the distributed system will deliver its
functionality with acceptable availability and reliability&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The book then goes on to describe various protocols used for data
replication on distributed systems, putting an emphasis that
&lt;a href=&quot;http://aphyr.com/posts/288-the-network-is-reliable&quot;&gt;network partitions do happen&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;related-papers&quot;&gt;Related Papers&lt;/h2&gt;

&lt;p&gt;While reading the first chapters from the book I had the &lt;em&gt;Depth First
Search&lt;/em&gt; problem that this XKCD comic clearly demonstrates:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://imgs.xkcd.com/comics/dfs.png&quot; alt=&quot;DFS&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I basically wanted to read every paper mentioned in that book,
specifically, since they seemed to present some context on how network
partitions have been treated historically in computer science.&lt;/p&gt;

&lt;p&gt;So I found this 1984 paper by Susan Davidson and Hector Garcia-Molina:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://repository.upenn.edu/cgi/viewcontent.cgi?article=1669&amp;amp;context=cis_reports&quot;&gt;Consistency in a Partitioned Network: A Survey&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This paper has the classical insights on network partitions, like:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;The design of a replicated data management algorithm tolerating
partition failures is a notoriously hard problem.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;Typically, the cause or extent of a partition failure can not be
discerned by the processors themselves.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;In addition, slow responses from certain processors can cause the
network to appear partitioned even when it is not&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then the paper presents some curious use cases from back in the
’80s. One of them mentions that a system had &lt;em&gt;“170 transactions per
second at peak time”&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Let’s look at this quote:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Since it is clearly impossible to satisfy both goals simultaneously,
one or both must be relaxed to some extent depending on the
application requirements.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the context of this quote &lt;em&gt;both goals&lt;/em&gt; means &lt;em&gt;correctness&lt;/em&gt; and
&lt;em&gt;availability&lt;/em&gt;. So we could stretch this (and maybe close one eye
while we are at it) and we can smell a hint to what today we call the
CAP theorem. Pick any two.&lt;/p&gt;

&lt;p&gt;So, it was all good and shinny until I read;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;As far back as 1977, partitioned operation was identified as one of
the important and challenging open issues in distributed data
management&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, erm, there’s another paper I should be reading? I go and search
for that paper until I find it:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.few.vu.nl/~kgr700/sdd1.pdf&quot;&gt;Introduction to a System for Distributed Databases SDD-1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Lo, and behold! It was the paper introducing SDD-1, or in other words
&lt;em&gt;“the first general-purpose distributed DBMS ever developed.”&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;At this point, I skim over that paper and try to get back to the
book. It was a bit late, so I decided to gloss over the the book to
see what was there. One of appendices has a reading list on the
various topics presented in the book. One of such lists is called
&lt;em&gt;“Replication in Heterogeneous, Mobile, and Large-Scale Systems”.&lt;/em&gt; The
first paper there sounded a bit, familiar.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://ftp.isi.edu/~johnh/PAPERS/Heidemann92a.pdf&quot;&gt;Primarily Disconnected Operation: Experiences with Ficus&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This paper basically presents what we today call “Offline First” but
with the terminology used back in the ’90s, 1992 to be precise. For
example, let’s see this use case of their solution:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Home Use: Imagine having a machine at home which replicates most of
the environment you use in the office. When you commute, your machine
at home dials into the office, pulling down any changes made that
day. By the time you get home, the machine there is up to date and you
can work in the evening. When you are done for the night, your machine
dials in to the office and reconciles changes again, uploading all
your changes. While primarily disconnected the machine at home is
functioning almost as if it were constantly connected.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So there it is, the past showing us the future. Smart individuals way
before us faced some of the same problems that appear &lt;em&gt;novel&lt;/em&gt; today.
The buzzwords have changed, but the problems remained the same.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;nihil sub sole novum&lt;/em&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>A Not So New Software Morality</title>
   <link href="http://videlalvaro.github.com/2014/05/a-not-so-new-software-morality.html"/>
   <updated>2014-05-07T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2014/05/a-not-so-new-software-morality</id>
   <content type="html">&lt;h1 id=&quot;a-not-so-new-software-morality&quot;&gt;A Not So New Software Morality&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;May 07 2014&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Now that we all know TDD is dead, is time to perform some archeology,
to understand from where it came from. A good place to do that is the
book
&lt;a href=&quot;http://www.amazon.com/Software-Testing-Techniques-2nd-Edition/dp/1850328803&quot;&gt;Software Testing Techniques&lt;/a&gt;
by Boris Beizer. Originally written in 1983, there’s a “new” edition
from 1990 that can be purchased form Amazon. The book covers Unit
Testing as well as Integration Testing, among other techniques. What
caught my attention today is some section near the end of the book
where he presents &lt;strong&gt;A New Software Morality&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;What is interesting is how similar this is to what today we consider
as part of &lt;em&gt;agile&lt;/em&gt; or &lt;em&gt;xp&lt;/em&gt; or &lt;em&gt;methodology in vogue&lt;/em&gt;. Here it is in
full:&lt;/p&gt;

&lt;h2 id=&quot;a-new-software-morality&quot;&gt;A New Software Morality&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;Good software works, meets requirements, is robust, and is easy to
understand, easy to integrate, and easy to test. Furthermore, it
keeps these qualities over a long period of time because it is a
permanent product rather than a transitory one. Software quality has
almost nothing to do with algorithmic elegance, compactness, or
speed—in fact, those attributes do more harm to quality than
good. Good algorithms, compactness, and speed do matter for a small
part of software—such a small part of software that I can safely
discount these attributes as having anything to do with quality. Most
programmers will never find themselves in a situation where they
matter. Unfortunately, most programmers have been led to believe,
because of their early training, hacker folklore, and hero myths that
led them into programming, their early experiences with toy programs,
that “good” means elegant, compact, and fast. Here’s a software
morality update.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It’s quite interesting how he contrasts the &lt;em&gt;hacker&lt;/em&gt; mentality vs. the
&lt;em&gt;software-engineer-you-are-part-of-a-bigger-project&lt;/em&gt; mentality.&lt;/p&gt;

&lt;p&gt;Then he goes on and gives a list of six points on what makes this new 
“morality”:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;Test —If you can’t test it, don’t build it. If you don’t test it,
rip it out.&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;I can almost read this previous paragraph with the voice of my friend
&lt;a href=&quot;https://twitter.com/grmpyprogrammer&quot;&gt;Chris Hartjes&lt;/a&gt;, Mr. Grumpy
Testing.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;Put Testing Up Front —You can’t achieve testability if it’s an
after-thought that follows design and coding. Tests are as important
as code because code is garbage unless it is tested.&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;TDD anyone?&lt;/p&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;Don’t Be a Functional Overachiever —If it isn’t called for, don’t
build it. There’s no requirement, therefore no specification,
therefore no test criteria, therefore you can’t test it. And if you
do “test” it, who’ll understand?&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;For the young people in the audience, you might have heard about this
previous concept being referred as YAGNI.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;Don’t Put in Private Hooks —Hooks for future enhancements and
functionality are architectural issues. Your hooks won’t be
consistent with someone else’s hooks so they won’t be used in the
future unless you happen to be the project leader. You can’t test the
private hooks because there’s no supporting data structures or
corequisite components. See rule 1.&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;Decouple —Don’t take advantage of hardware peculiarities,
operating system quirks, the compiler, residues left by other
components, physical anything, data structure details, environment,
processing done by other components. Do what you can to reduce the
dependency of your component’s proper behavior on the proper behavior
or the invariant behavior of other components. Always ask yourself
what will happen if the other component is changed. And if coupling
is essential, document the potential vulnerability of your component
to the continued good graces of the other component.&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;This last one particularly must be printed and sticked to every Scrum
board out there:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;ol&gt;
    &lt;li&gt;Don’t Squeeze —Don’t squeeze space, time, or schedule. Don’t
allow shortsighted bosses concerned with next week’s progress report
or the quarterly stockholders’ report to squeeze them for you. If you
can’t fight them, document your objections and the expected
consequences and/or vote with your feet.&lt;/li&gt;
  &lt;/ol&gt;
&lt;/blockquote&gt;

&lt;p&gt;At this point I’d like to imagine that
&lt;a href=&quot;http://en.wikipedia.org/wiki/Boris_Beizer&quot;&gt;Boris Beizer&lt;/a&gt; dropped the
mic and walked out of stage.&lt;/p&gt;

&lt;h3 id=&quot;sources&quot;&gt;Sources&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;Beizer, Boris. 1990. SOFTWARE TESTING TECHNIQUES. New York: Van Nostrand.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>On the speed of Algorithms and Faster Hardware</title>
   <link href="http://videlalvaro.github.com/2014/04/on-the-speed-of-algorithms.html"/>
   <updated>2014-04-08T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2014/04/on-the-speed-of-algorithms</id>
   <content type="html">&lt;h1 id=&quot;on-the-speed-of-algorithms-and-faster-hardware&quot;&gt;On the speed of Algorithms and Faster Hardware&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;April 08 2014&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Today I’ve read on Twitter a half-joke saying that basically we can
just implement our code and then wait for faster computers to improve
the speed of our programs. As many might know, that’s not actually the
case, but instead of trying to explain this, I’d prefer to let the
authors of
&lt;a href=&quot;http://www.amazon.com/The-Design-Analysis-Computer-Algorithms/dp/0201000296/&quot;&gt;The Design and Analysis of Computer Algorithms&lt;/a&gt;
explain that for me.&lt;/p&gt;

&lt;p&gt;On section 1.1 of that book the authors are analyzing the speed of
algorithms based on their problem size and their “asymptotic
behavior”. They say:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;One might suspect that the tremendous increase in speed of
calculations brought about by the advent of the present generation of
digital computers would decrease the importance of efficient
algorithms. However, just the opposite is true.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then they propose the following set of algorithms with their time
complexities:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Algorithm&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Time Complexity&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;A1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;n&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;A2&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;n log n&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;A3&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;n^2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;A4&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;n^3&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;A5&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;2^n&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Where time complexity is the number of steps required to process some
input of size &lt;em&gt;n&lt;/em&gt;, and one unit of time equals one millisecond.&lt;/p&gt;

&lt;p&gt;Then they present the maximum problem size for each algorithm. For
example &lt;strong&gt;A1&lt;/strong&gt; would be able to process a problem size of 1000 in one
second, or 3.6 x 10^6 in one hour. Compare that with &lt;strong&gt;A5&lt;/strong&gt;, which can
process 9 elements per second, or a maximum of 21 in one hour.&lt;/p&gt;

&lt;p&gt;So then we get a computer than is 10 time faster of what he have right
now. Here’s the speedup comparison table the authors provide:&lt;/p&gt;

&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Algorithm&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Time Complexity&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Max. Problem Size Before Speed-up&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;Max. Problem Size After Speed-up&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;A1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;n&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;S1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;10 * S1&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;A2&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;n log n&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;S2&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;Approx 10 * S2 for large S2&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;A3&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;n^2&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;S3&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;3.16 * S3&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;A4&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;n^3&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;S4&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;2.15 * S4&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;A5&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;2^n&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;S5&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;S5 + 3.3&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;As we can see, algorithm &lt;strong&gt;A5&lt;/strong&gt; could only increase the size of the
problem by &lt;strong&gt;3&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So does the speed of a computer affects the speed of an algorithms,
yes; is it a significant improvement, usually not, unless of course we
are talking o changes in word sizes, which could benefit numerical
algorithms; or bigger CPU caches and so on.&lt;/p&gt;

&lt;p&gt;Still, in general, it’s always better to design a proper algorithm, or
even better, use an algorithm designed by the experts.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Bibliography&lt;/em&gt;:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Aho, Alfred V., John E. Hopcroft, and Jeffrey D. Ullman. 1974. The design and analysis of computer algorithms. Reading, Mass: Addison-Wesley Pub. Co.&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>The Power Algorithm</title>
   <link href="http://videlalvaro.github.com/2014/03/the-power-algorithm.html"/>
   <updated>2014-03-19T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2014/03/the-power-algorithm</id>
   <content type="html">&lt;h1 id=&quot;the-power-algorithm&quot;&gt;The Power Algorithm&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;March 19 2014&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;In this blog post I would like to show how a very basic idea like
raising a number to a certain power could lead us to discover
abstractions like Semigroups and Monoids.&lt;/p&gt;

&lt;p&gt;There’s a very well known algorithm for calculation powers, that is &lt;em&gt;x
to the power of n&lt;/em&gt; or simply: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x^n&lt;/code&gt;. Donald Knuth presents the
algorithm in section 4.6.3 &lt;em&gt;Evaluation of Powers&lt;/em&gt; of TAOCP.&lt;/p&gt;

&lt;p&gt;The naïve way to implement this algorithm would be to multiply &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; by
itself &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; times, but of course the idea is to provide an algorithm
that’s faster than this. The algorithm in question is usually called
the &lt;em&gt;binary method&lt;/em&gt;, the &lt;em&gt;powering ladder&lt;/em&gt; or the &lt;em&gt;repeated-squaring&lt;/em&gt;
algorithm.&lt;/p&gt;

&lt;p&gt;Say we want to calculate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2^23&lt;/code&gt;, where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x = 2&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n = 23&lt;/code&gt; this
algorithm takes into account the binary representation of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;23&lt;/code&gt;
(&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10111&lt;/code&gt;), scans it and depending if it finds a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; or a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt; it will
either square &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; or multiply by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The problem with this method is that it scans the binary representation
of the number from left to right, but for computers it’s usually
easier to do it the other way around, so Knuth proposes a alternative
algorithm.&lt;/p&gt;

&lt;p&gt;A simple implementation of &lt;em&gt;Algorithm A&lt;/em&gt; from section &lt;em&gt;4.6.3&lt;/em&gt; of TAOCP
is as follow:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;power1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;floor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nv&quot;&gt;$y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This function takes two integers, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$x&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$n&lt;/code&gt; and returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$x&lt;/code&gt; to
the power of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$n&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;First it sets an auxiliary variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$y&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt; which is the identity
of the multiplication.&lt;/p&gt;

&lt;p&gt;Then the function scans the binary representation of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$n&lt;/code&gt; on each
iteration of the loop. If it finds a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt; then it multiplies by
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$x&lt;/code&gt;. On every step of the loop it squares &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$x&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Finding a 1&lt;/em&gt; means that the current value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$n&lt;/code&gt; is not divisible by
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt;, or in other words, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$n % 2 == 1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Also on every iteration of the loop &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$n&lt;/code&gt; gets halved and then we apply
&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;floor&lt;/code&gt; to the result. When &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$n&lt;/code&gt; equals &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;, we terminate the loop and
return the value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$y&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;power&lt;/code&gt; can be called like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;mi&quot;&gt;1024&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;power1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;At this point I imagine you being like the guy on this gif:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/csb.gif&quot; alt=&quot;CSB&quot; /&gt;&lt;/p&gt;

&lt;p&gt;While the algorithm really looks like a CSB, it is actually quite
effective when it comes to computing with very big numbers. For
example there are many primality testing algorithms that depend on
variations of this algorithm.&lt;/p&gt;

&lt;h2 id=&quot;adding-some-abstraction&quot;&gt;Adding some abstraction&lt;/h2&gt;

&lt;p&gt;So far nothing fancy, but if we notice that raising a number to a
power is actually the same as multiplying the number by itself that
many times, we could also see that multiplication is the same as
adding a number by itself that many times. So for example &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2 * 5&lt;/code&gt;
could be computed by doing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2 + 2 + 2 + 2 + 2&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Can we convert our algorithm into a more generic form that would work
for multiplication and addition? Of course we can. We just need to
change a couple of things.&lt;/p&gt;

&lt;p&gt;On the current implementation we set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$y&lt;/code&gt; to the identity element of
multiplication, namely &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt;. If we want to use the algorithm for
addition, then we need to set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$y&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;. So we could just pass the
value of our identity element to the function.&lt;/p&gt;

&lt;p&gt;The second step would be to provide a function to our algorithm that
would do either multiplication or addition. To do that we will pass a
function that will act as a binary operation, i.e.: a function that
takes two arguments. This function needs to follow some rules as well,
that is, it has to be associative: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a⁡• (b • c) = (a • b) •&lt;/code&gt;. Also the
type of the result value must be the same type as both input
parameters.&lt;/p&gt;

&lt;p&gt;Luckily for us addition and multiplication are associative operations,
so we can just wrap them in a function and pass it to our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;power&lt;/code&gt;
algorithm.&lt;/p&gt;

&lt;p&gt;Here’s the new implementation of the algorithm:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;power2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$id&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;%&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;floor&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nv&quot;&gt;$y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;break&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$x&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$y&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And we can call it like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;mi&quot;&gt;1024&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;power2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;As you can see there we passed &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt; as our identity element, and we
provided a function that takes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2&lt;/code&gt; arguments and returns the result of
their multiplication. Nothing fancy.&lt;/p&gt;

&lt;p&gt;But now say we want to calculate &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;2 * 10&lt;/code&gt; using our algorithm. We
could wrap the addition operator inside a function and then pass &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt; as
the identity element like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;power2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Keep in mind that the operation that we pass to our algorithm must be
associative, for example, subtraction can’t be used here since for
example &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10 - (5 - 3) = 8&lt;/code&gt; while &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(10 - 5) - 3 = 2&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;appending-more-abstraction&quot;&gt;Appending more abstraction&lt;/h2&gt;

&lt;p&gt;Mathematically speaking this algorithm works with any algebraic
structure that has an associative operation (like multiplication and
addition in the case of integers), that is, it works on Semigroups. To quote a book on Group
Theory:&lt;/p&gt;

&lt;p&gt;A &lt;em&gt;semigroup&lt;/em&gt; set S equipped with an associative binary operation •;
that is, x • (y • z) = (x • y) • z for all x, y, z ∈ S.&lt;/p&gt;

&lt;p&gt;Also, that set must have an identity element, which makes it a Monoid:&lt;/p&gt;

&lt;p&gt;A &lt;em&gt;monoid&lt;/em&gt; is a set M equipped with an associative binary operation •;
together with an identity element e ∈ M that satisfies e • x = x • e =
x for all x ∈ M.&lt;/p&gt;

&lt;p&gt;What other structures that we use every day in programming can work
under the previous conditions? If you are a web developer you don’t
need to dig so much to find strings. Strings, using &lt;em&gt;string append&lt;/em&gt; as
the binary operation and the &lt;em&gt;empty string&lt;/em&gt; as the identity element
would give use similar results as above. If we wanted to &lt;em&gt;repeat&lt;/em&gt; a
string &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; times, we could create the following function:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;repeat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;power2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$s&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
               &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
           &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And to test it:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;s2&quot;&gt;&quot;aaaaaaaaaa&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;repeat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now think about arrays (or lists in other languages). We could want to
build an array with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; copies of the same element. Here the &lt;em&gt;empty
array&lt;/em&gt; is the identity element, and in the case of PHP &lt;em&gt;array_merge&lt;/em&gt;
would be our binary operation.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;repeat_el&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$el&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;power2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$el&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;array_merge&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
           &lt;span class=&quot;p&quot;&gt;});&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And here are our results:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$arr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;repeat_el&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;a&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$arr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;kc&quot;&gt;true&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;As you can see here, something that seemed so simple as raising a
number to a certain power gave us a neat algorithm that can be used
for several things, like repeating strings or elements inside an
array.&lt;/p&gt;

&lt;h2 id=&quot;further-reading&quot;&gt;Further Reading&lt;/h2&gt;

&lt;p&gt;The power algorithm as implemented here is based on
&lt;a href=&quot;http://www-cs-faculty.stanford.edu/~uno/taocp.html&quot;&gt;TAOCP&lt;/a&gt;, Vol II,
section 4.6.3.&lt;/p&gt;

&lt;p&gt;The whole explanation of how this works can be found in TAOCP or in
the book
&lt;a href=&quot;http://shoup.net/ntb/&quot;&gt;A Computational Introduction to Number Theory and Algebra&lt;/a&gt;. The
PDF of the book is freely available on the author’s website. Look into
section “Computing with large integers - The repeated squaring
algorithm”.&lt;/p&gt;

&lt;p&gt;If you would like to learn about several uses for this algorithm and
some more theory behind it, then consult the book called
&lt;a href=&quot;http://www.amazon.com/Elements-Programming-Alexander-Stepanov/dp/032163537X&quot;&gt;Elements of Programming&lt;/a&gt;. This
book is great in the way it defines the different types of functions,
and how it uses the type system to be sure that the function is
actually associative, binary, and so on. The author is the designer of
C++ STL, so while the concerns of the book might seem very
theoretical, then can be directly applied in OOP programs.&lt;/p&gt;

&lt;p&gt;The quotes about Semigroups and Monoids are from the book
&lt;a href=&quot;http://www.amazon.com/Handbook-Computational-Discrete-Mathematics-Applications/dp/1584883723/&quot;&gt;Handbook of Computational Group Theory&lt;/a&gt;. A
very interesting book if you are into CGT.&lt;/p&gt;

&lt;p&gt;If you want to learn more about Monoids and their implementations this
chapter from &lt;em&gt;Learn You a Haskell&lt;/em&gt; has a very nice introduction to
&lt;a href=&quot;http://learnyouahaskell.com/functors-applicative-functors-and-monoids#monoids&quot;&gt;Functors, Applicative Functors and Monoids&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It could be an interesting exercise to implement these concepts using
say PHP and OOP, or for the PHP non lovers, in some other imperative
language of your choice.&lt;/p&gt;

&lt;h2 id=&quot;did-you-just-say-haskell&quot;&gt;Did you just say Haskell?&lt;/h2&gt;

&lt;p&gt;Since I’ve mentioned a Haskell book, here’s an implementation of the
power algorithm in Haskell, using a recursive algorithm based on the book
&lt;a href=&quot;http://www.amazon.com/Prime-Numbers-Computational-Richard-Crandall/dp/0387252827/&quot;&gt;Prime Numbers: A Computational Perspective&lt;/a&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;n&quot;&gt;power&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Eq&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Integral&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;power&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;even&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;square&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;`&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;otherwise&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;square&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;`&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;div&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;`&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;kr&quot;&gt;where&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;square&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a'&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n'&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;power&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a'&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;power&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a'&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And here are the results of several invocations of the function:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;load&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pow&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hs&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Compiling&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Main&lt;/span&gt;             &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;pow&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;interpreted&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;Ok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;modules&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;loaded&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;power&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;1024&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;power&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Main&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;power&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;a&quot;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;s&quot;&gt;&quot;aaaaaaaaaa&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;As you can see there, the function takes a function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(a -&amp;gt; a -&amp;gt; a)&lt;/code&gt;
which could be either &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;+&lt;/code&gt; for integers, or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;++&lt;/code&gt; for lists, for
example.&lt;/p&gt;

&lt;p&gt;I hope you found this blog post interesting, and that I have whetted
your appetite for learning maths in relation to programming, since I
think the more we grasp these ideas, the better the abstractions we
would be able to use.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>You don't roll your own crypto</title>
   <link href="http://videlalvaro.github.com/2014/03/you-dont-roll-your-own-crypto.html"/>
   <updated>2014-03-02T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2014/03/you-dont-roll-your-own-crypto</id>
   <content type="html">&lt;h1 id=&quot;you-dont-roll-your-own-crypto&quot;&gt;You don’t roll your own crypto&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;March 2 2014&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Imagine you enter a contest organized by the &lt;a href=&quot;http://en.wikipedia.org/wiki/National_Institute_of_Standards_and_Technology&quot;&gt;National Institute for Standards and Technology&lt;/a&gt; (NIST),
where the goal is to design a new encryption standard to replace &lt;a href=&quot;http://en.wikipedia.org/wiki/Data_Encryption_Standard&quot;&gt;DES&lt;/a&gt; and &lt;a href=&quot;http://en.wikipedia.org/wiki/Triple_DES&quot;&gt;Triple-DES&lt;/a&gt;.
Some of the contestants? The RSA, IBM, Deutsche Telekom and Counterpane (a team lead by Bruce Schneier). Would you think you stand a chance of winning? I know I wouldn’t, but a Belgian team
composed by Joan Daemen and Vincent Rijmen beat the other teams with their submission: Rijndael, the system that was later to be come the new Advanced Encryption Standard (AES).&lt;/p&gt;

&lt;p&gt;The book &lt;a href=&quot;http://www.amazon.com/The-Design-RijndaeL-Information-Cryptography/dp/3540425802&quot;&gt;The Design of Rijndael&lt;/a&gt; has a very fascinating first chapter where the authors describe the selection
process proposed by the NIST to chose the new AES, from which I would like to share here some facts that are quite interesting. 
They prove, that &lt;a href=&quot;http://security.stackexchange.com/a/18198&quot;&gt;you don’t roll your own crypto&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Note that I don’t claim to know the stuff I’m presenting here, or be an expert in cryptography, I’m just learning about this topic and I thought it would be interesting to share this story. I’ve also
added a list of books at the bottom, that are the ones I’ve been using to learn about it.&lt;/p&gt;

&lt;h2 id=&quot;the-selection-process&quot;&gt;The selection process&lt;/h2&gt;

&lt;p&gt;The selection process for AES started in 1997 and it was open, anyone could submit and entry as long as the entry met the requirements:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Entries should be for symmetric block ciphers, for block lengths of 128 bits, and key lengths of 128, 192 and 256 bits.&lt;/li&gt;
  &lt;li&gt;Entries should be available in a worldwide royalty-free basis.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The designers should provide:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A complete written specification as an algorithm&lt;/li&gt;
  &lt;li&gt;A reference implementation in ANSI C, and mathematically optimized versions in ANSI C and Java.&lt;/li&gt;
  &lt;li&gt;Implementation of &lt;a href=&quot;http://csrc.nist.gov/groups/STM/cavp/&quot;&gt;known-answer and Monte Carlo tests&lt;/a&gt; for the algorithms, and the expected outputs for a correct implementation of their block cipher.&lt;/li&gt;
  &lt;li&gt;Statements about the estimated computational requirements both for hardware and software.&lt;/li&gt;
  &lt;li&gt;Statements about the expected strength against cryptanalytic attacks&lt;/li&gt;
  &lt;li&gt;Statements about the advantages and limitations of the cipher in various applications.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The security and evaluation of the submissions was not going to be made by NIST, but by the cryptology community. Anyone could have commented on the entries and send their papers back to 
the NIST.&lt;/p&gt;

&lt;p&gt;As you can see from the requirements, this wasn’t a contest for hobbyists, it required serious effort and knowledge. This meant that many proposal got filtered out simply by these requirements.&lt;/p&gt;

&lt;p&gt;From the start something quite cool happened, a team called Cryptix decided to provide Java implementations for all ciphers and the KAT and MCT tests. Here we can see the community working together
to achieve a common goal.&lt;/p&gt;

&lt;h2 id=&quot;round-one&quot;&gt;Round One&lt;/h2&gt;

&lt;p&gt;Another interesting fact from the selection process was that it happened in the open, together with conferences about security that gathered most of the researchers interested in the topic.&lt;/p&gt;

&lt;p&gt;The cryptology community mounted different kinds of attacks and tried to analyze the different candidates. Then people will send submissions to be presented at the conference. The main topics of
analysis were:&lt;/p&gt;

&lt;h3 id=&quot;security&quot;&gt;Security&lt;/h3&gt;

&lt;p&gt;This was the most important category, no news about it. Some candidates already showed some theoretical design flaws at this stage. Later the book develops the math behind AES and one can see that
one doesn’t design an encryption algorithm over some beers. This might seem obvious, but still people keep designing their own encryption.&lt;/p&gt;

&lt;h3 id=&quot;costs&quot;&gt;Costs&lt;/h3&gt;

&lt;p&gt;Here the evaluation was two fold. First, algorithms should be free of royalties as mentioned above. Also contestants were not allowed to exercise patents over other people’s ideas. The second part
was the analysis of computational costs, program size, memory usage, and so on.&lt;/p&gt;

&lt;h3 id=&quot;algorithm-difficulty&quot;&gt;Algorithm Difficulty&lt;/h3&gt;

&lt;p&gt;Here the algorithms were analyzed on how easy was to implement them for different platforms. From 8-bit micro-controllers to dedicated hardware, like encryption/decryption at one gigabit-per-second rates.&lt;/p&gt;

&lt;p&gt;They were also interested in &lt;em&gt;key-agility&lt;/em&gt;, that is, how easy it is to setup new keys for the algorithm.&lt;/p&gt;

&lt;p&gt;Finally, simplicity played an important role: how easy it is to implement the algorithm and make sure that the implementation is correct.&lt;/p&gt;

&lt;h2 id=&quot;selecting-the-five-finalists&quot;&gt;Selecting the Five Finalists&lt;/h2&gt;

&lt;p&gt;Fast forward to 1999, where the second AES conference was held in Rome. Here papers presented crypto-attacks, cipher cross-analysis and &lt;em&gt;algorithm observations&lt;/em&gt;. Were you ever afraid of code reviews? Well,
imagine the whole cryptology community reviewing your code. If your code survives the process, then there’s must be something good in it. At this point it was shown that Magenta, the entry by Deutsche Telekom
didn’t satisfy the security requirements imposed by NIST. Another algorithm called DEAL was know to not satisfy the security requirements, and another paper demonstrated that an algorithm called HPC had weaknesses.&lt;/p&gt;

&lt;p&gt;An example that shows how important peer review is in this kind of situations was a &lt;a href=&quot;http://csrc.nist.gov/archive/aes/round1/conf2/papers/gladman.pdf&quot;&gt;paper&lt;/a&gt; presented by a researcher that wasn’t member of any 
of the contestant teams. This person analyzed the performance of the algorithms on Pentium Processors, showing that Rijndael was among the five fastest algorithms.&lt;/p&gt;

&lt;p&gt;Another person presented a &lt;a href=&quot;http://csrc.nist.gov/archive/aes/round1/conf2/papers/biham2.pdf&quot;&gt;paper&lt;/a&gt; comparing the security strength of the presented algorithms, showing that there was a big margin between them,
prompting for this detail to be taken into account when making a choosing and algorithm. There was an algorithm called SERPENT that was slow, but it had a high margin of security.&lt;/p&gt;

&lt;p&gt;Other papers focused on how the algorithm performed in 8-bit and 32-bit processors. Some algorithms didn’t even fit into the smaller architectures. Again, Rijndael was one of the best algorithms in this category.&lt;/p&gt;

&lt;p&gt;At the end of the conference there was a workshop where the five finalists were announced:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;MARS, by IBM&lt;/li&gt;
  &lt;li&gt;RC6, by RSA&lt;/li&gt;
  &lt;li&gt;Rijndael, by Daemen and Rijmen, researchers&lt;/li&gt;
  &lt;li&gt;Serpent, by Anderson, Bihman, Knudsen, researchers&lt;/li&gt;
  &lt;li&gt;Twofish, by Counterpane, Bruce Schneier’s team&lt;/li&gt;
&lt;/ul&gt;

&lt;h2 id=&quot;round-two&quot;&gt;Round Two&lt;/h2&gt;

&lt;p&gt;At this stage NIST made another call for papers discussing the finalist algorithms. In this case even the NSA took part, sending several performance simulations that were done for the finalists. On the area
of cryptographic attacks there were no breakthroughs, since none of the finalist showed any weaknesses that would disqualify them from the competition. On the area of hardware implementation, Rijndael performed
well again.&lt;/p&gt;

&lt;p&gt;The aforementioned results were presented at a conference in New York City, in April 2000, ending with a questionnaire to the attendants to vote for their favorite algorithm. 
Rijndael was voted the public’s favorite.&lt;/p&gt;

&lt;h2 id=&quot;the-selection&quot;&gt;The Selection&lt;/h2&gt;

&lt;p&gt;On October of the same year, the NIST announced that Rijndael would become the new AES without any modifications. The NIST published a &lt;a href=&quot;http://csrc.nist.gov/archive/aes/round2/r2report.pdf&quot;&gt;report&lt;/a&gt; were it made
the announcement, saying about Rijndael:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Rijndael appears to be consistently a very good performer in both hardware and software across a wide range of computing environments regardless of its use in feedback or non- feedback modes. 
Its key setup time is excellent, and its key agility is good. Rijndael’s very low memory requirements make it very well suited for restricted-space environments, in which it also demonstrates excellent performance. 
Rijndael’s operations are among the easiest to defend against power and timing attacks. Additionally, it appears that some defense can be provided against such attacks without significantly impacting Rijndael’s 
performance. Rijndael is designed with some flexibility in terms of block and key sizes, and the algorithm can accommodate alterations in the number of rounds, although these features would require further study and are not being considered at this time.&lt;/p&gt;

  &lt;p&gt;Finally, Rijndael’s internal round structure appears to have good potential to benefit from instruction-level parallelism.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;I hope you found this story as interesting as I found it when I read it for the first time. For me the take away from it is that while it might be useful to create our own cipher algorithm for education purposes, 
we should never attempt to create one to be used in production. Also, please don’t publish them. Somebody will use them. I’ve seen how people have tried to use in production complete jokes such as 
&lt;a href=&quot;http://stackoverflow.com/questions/13593420/countdown-timer-image-gif-in-email&quot;&gt;gifsockets&lt;/a&gt;. We could see that teams like the people from Deutsche Telekom had created algorithms that weren’t’ 
secure enough according to the selection criteria. Also, the eyes and scrutiny of world’s cryptology experts are a better safety net than what we might think is &lt;em&gt;safe&lt;/em&gt;. 
As they say on TV: don’t try this at home, kids.&lt;/p&gt;

&lt;p&gt;As I said before, this is based on Chapter 1 from the book &lt;a href=&quot;http://www.amazon.com/The-Design-RijndaeL-Information-Cryptography/dp/3540425802&quot;&gt;The Design of Rijndael&lt;/a&gt;. It’s a quite small book that describes
the mathematical background behind AES, its design and implementation.&lt;/p&gt;

&lt;h2 id=&quot;books&quot;&gt;Books&lt;/h2&gt;

&lt;p&gt;In case you want to learn more about the subject, here are some books I’ve been studying:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/The-Design-RijndaeL-Information-Cryptography/dp/3540425802&quot;&gt;The Design of Rijndael&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Course-Number-Cryptography-Graduate-Mathematics/dp/0387942939/&quot;&gt;A Course in Number Theory and Cryptography&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Primes-Programming-Peter-J-Giblin/dp/0521409888/&quot;&gt;Primes and Programming&lt;/a&gt;. Examples for this book are in Pascal&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Computational-Introduction-Number-Theory-Algebra/dp/0521516447/&quot;&gt;A Computational Introduction to Number Theory and Algebra&lt;/a&gt;. This book has a very clear introduction to the mathematical concepts&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://www.amazon.com/Prime-Numbers-Computational-Richard-Crandall/dp/0387252827/&quot;&gt;Prime Numbers: A Computational Perspective&lt;/a&gt;. Apart from the great content, this book is written by Richard Crandall, who was Apple’s Chief Cryptographer. The other author is Carl Pomerance, an authority in Number Theory&lt;/li&gt;
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Why you should learn Erlang</title>
   <link href="http://videlalvaro.github.com/2014/02/why-you-should-learn-erlang.html"/>
   <updated>2014-02-24T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2014/02/why-you-should-learn-erlang</id>
   <content type="html">&lt;h1 id=&quot;why-you-should-learn-erlang&quot;&gt;Why you should learn Erlang&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;February 24 2014&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;With all the rage about &lt;a href=&quot;http://www.forbes.com/sites/briansolomon/2014/02/19/stunner-facebook-to-buy-whatsapp-for-16-billion-in-cash-stock/&quot;&gt;WhatsApp acquisition by Facebook&lt;/a&gt;,
people is going crazy over Erlang. Even &lt;a href=&quot;https://twitter.com/susanpotter/status/437940423776927745&quot;&gt;recruiters&lt;/a&gt; are using it as a weapon to lure
engineers in. So I said, what don’t I write that blogpost about why I consider people should learn Erlang?&lt;/p&gt;

&lt;p&gt;Keep in mind this is not a blog post about &lt;em&gt;how to program&lt;/em&gt; in Erlang, nor is it a fanboy attempt to say Erlang is better than language X. So please refrain from language flamewars.
Also, I don’t think I will provide many code examples, since I think they don’t matter for this particular kind of discussion.&lt;/p&gt;

&lt;h2 id=&quot;the-reasons&quot;&gt;The reasons&lt;/h2&gt;

&lt;p&gt;I’m writing this blog post from this question: I might not need to code in Erlang from my day job, still, is there anything I could learn from Erlang that will make me a better programmer?
So without much further ado, here are the reasons why you should learn Erlang:&lt;/p&gt;

&lt;h3 id=&quot;single-assignment-variables&quot;&gt;Single Assignment Variables&lt;/h3&gt;

&lt;p&gt;The first thing that was remarkable for me from Erlang was that variables don’t vary. In Erlang, once you &lt;em&gt;assign&lt;/em&gt; a value to a variable, then it can’t be changed. If you come from the
imperative programming world (Java, PHP, .Net, Python, etc.), then learning to live with that it’s a lot, because in our programs we are making assignments and mutating variables all
the time.&lt;/p&gt;

&lt;p&gt;Now, what is so cool about this “feature” of Erlang, that actually prevents you from doing things? Remember that time you had to track the lifecycle of one variable to try to debug why at
in production, when it was 3:03 PM sharp after the 20th request has been served, the variable will become &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt; instead of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;, as it should have been? Did you enjoy wasting time
reproducing that bug to be able to track it down? No? Me neither.&lt;/p&gt;

&lt;p&gt;With Erlang it becomes quite easy to avoid such problems because you simply can’t change a variable once it’s been assigned a value. Little by little your brain starts to learn to live with
that, to see that and algorithms can be implemented without mutable variables, that problems can be solved that way. You start to learn that you actually don’t need that many variables, that
less is more.&lt;/p&gt;

&lt;p&gt;One you go back to your day to day job, and you start coding in Imperative Language (IL) you will start seeing that perhaps this variable that we change here and there is not actually needed.
That modifying that object at that particular place makes the code hard to reason about. That all the encapsulation that you brag about got completely destroyed when you passed an object to a
method, and the method mutated the object in ways that shouldn’t have happened. Well, having some experience with Erlang will train your coder instincts to detect those places where modifying
an object or variable state is dangerous. For me this has been the best thing I’ve learnt from single assignment variables. Please not that I’m not arguing against those algorithms implemented
in imperative languages that due to performance reason have to be implemented with mutation, I’m just saying that coding in Erlang will let you easily detect those parts of your code that might
present a problem later.&lt;/p&gt;

&lt;h3 id=&quot;pattern-matching&quot;&gt;Pattern Matching&lt;/h3&gt;

&lt;p&gt;Pattern matching is the part of Erlang that sets it apart from most other languages. For example, Erlang doesn’t have for loops, but you can implement a similar behaviour like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;nf&quot;&gt;do_n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;do_n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]).&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;do_n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(_&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Acc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;Acc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;do_n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Acc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;do_n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Count&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;N&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;F&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Count&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)|&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Acc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]).&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The first function definition &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;do_n(F, N)&lt;/code&gt; is just a more usable interface to our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;do_n/4&lt;/code&gt; function that takes four arguments. We pass an anonymous function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;F&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;do_n&lt;/code&gt; and a number &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;N&lt;/code&gt;
indicating how many times we want to call that function. Then this function would call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;do_n/4&lt;/code&gt; like this for example: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;do_n(F, 0, 10, [])&lt;/code&gt;, where it’s telling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;do_n&lt;/code&gt; to start counting at &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;
and to stop when the counter reaches &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;On the second part we have a recursive function with two headers, that is: two function definitions that would be called depending on the value of its arguments. Every time the function is
called, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Count&lt;/code&gt; variable will be increased, and the second part of the function will be called again. Now, we don’t see any &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if&lt;/code&gt; statement there checking if the counter ever becomes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The first header of the function is written like this: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;do_n(_F, N, N, Acc)&lt;/code&gt;, that is: only execute this body of the function if the 2nd and the 3rd argument are both the same. So when the
function is called, The first argument will be whatever is passed to it, and the second one will be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10&lt;/code&gt; in our example, so unless the counter becomes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10&lt;/code&gt; at some point, then this body of
the function will never be called.&lt;/p&gt;

&lt;p&gt;For me the simplicity of this construct alone is what makes learning to program in Erlang so valuable.
(See more about this feature in real world example from RabbitMQ &lt;a href=&quot;http://videlalvaro.github.io/2013/09/rabbitmq-validating-user-ids-with-erlang-pattern-matching.html&quot;&gt;here&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Apart from that, Erlang’s pattern matching comes with “destructuring”. Another cool feature that will drive you mad next time you need to access a deeply nested data structure in a non Erlang
language. Once you learn to you use it, you will probably open an online petition so your day to day programing language gets this feature soon.&lt;/p&gt;

&lt;h3 id=&quot;the-actor-model&quot;&gt;The Actor Model&lt;/h3&gt;

&lt;p&gt;The whole concept of having millions of tiny processes composing your system, working in harmony to reach a solution, sounds cool enough to start learning Erlang already. This mode of
computation is called the Actor Model, introduced by Carl Hewitt in his famous paper: &lt;a href=&quot;http://worrydream.com/refs/Hewitt-ActorModel.pdf&quot;&gt;A Universal Modular ACTOR Formalism for Artificial Intelligence&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The concept of processes supervising each others; processes that will automatically shut down if a neighbor process has died; process that are isolated from each other and that can’t
corrupt each others states; and more, are quite interesting reasons to dive into Erlang to see what is this all about, and learn that the way we do OOP perhaps should be the way Erlang is
doing it, with completely isolated objects that communicate with each others by means of message passing. Learning the actor model will let you improve the ways you understand OOP.&lt;/p&gt;

&lt;h3 id=&quot;the-syntax&quot;&gt;The Syntax&lt;/h3&gt;

&lt;p&gt;There’s a lot of complaining about Erlang Syntax. Well, yes, if you come from the ALGOL family of languages, then of course it’s weird, I grant you that, but if you want to learn something
new, I think Erlang syntax is there to remind you “Hey! I don’t think that thing means what you think it means”, all the time. For a language that comes with a complete new paradigm like
concurrent programming, I think a rather different syntax is also interesting.&lt;/p&gt;

&lt;h2 id=&quot;books&quot;&gt;Books&lt;/h2&gt;

&lt;p&gt;Some resources for Learning Erlang:&lt;/p&gt;

&lt;p&gt;I’ve learnt Erlang from this book: &lt;a href=&quot;http://pragprog.com/book/jaerlang/programming-erlang&quot;&gt;Programming Erlang: Software for a Concurrent World&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;From that book I went directly to this guide from the official docs: &lt;a href=&quot;http://www.erlang.org/doc/design_principles/des_princ.html&quot;&gt;OTP Design Principles&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I’ve learnt about Mnesia (the Erlang database) from here: &lt;a href=&quot;http://www.erlang.org/doc/apps/mnesia/Mnesia_chap1.html&quot;&gt;Mnesia User’s Guide&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Then the book &lt;a href=&quot;http://www.manning.com/logan/&quot;&gt;Erlang and OTP in Action&lt;/a&gt; will teach you a lot about building reliable apps with Erlang.&lt;/p&gt;

&lt;p&gt;As a project to learn Erlang, I tried to create a Redis clone in Erlang. I’ve never published that project, but I’ve learnt a lot from it.&lt;/p&gt;

&lt;p&gt;Finally I’ve got hooked with RabbitMQ so from time to time I would read the source code just to learn more about Erlang. You can read some of those adventures &lt;a href=&quot;http://videlalvaro.github.io/internals.html&quot;&gt;here&lt;/a&gt;
and &lt;a href=&quot;https://github.com/videlalvaro/rabbit-internals&quot;&gt;here&lt;/a&gt;, and some slides &lt;a href=&quot;http://www.slideshare.net/old_sound/dissecting-the-rabbit&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;To sum it up, why you should learn Erlang? It will make you more aware of all the places were your code is modifying state, and perhaps there’s a cleaner implementation that doesn’t need that.
Keep in mind that the more you write &lt;em&gt;pure functions&lt;/em&gt;, function that always return the same value for the same arguments, the easier it will become to prove that code correct, for example by
writing unit tests for it. Erlang has the notion of pattern matching. This will literally change the way you see code, and will make you wonder why your language of choice doesn’t support this feature.
The actor model will give you a new perspective on the words &lt;em&gt;encapsulation&lt;/em&gt; and probably make you think that the OOP we’ve been doing so far has been just putting namespaces around collections
of related functions. Finally the syntax will help you navigate through the moody waters of new concepts, reminding you that what looks like &lt;em&gt;this&lt;/em&gt; is actually &lt;em&gt;that&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;I hope this has been helpful in making you decide why to learn Erlang. Again, I didn’t want to write a tutorial about Erlang, but just to highlight some of its salient features that will make
worth your time learning a new language like Erlang. I’m sure new perspectives on programming will benefit our day to day jobs in the long run.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Shift-And Visualization</title>
   <link href="http://videlalvaro.github.com/2014/01/shift-and-visualization.html"/>
   <updated>2014-01-05T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2014/01/shift-and-visualization</id>
   <content type="html">&lt;h1 id=&quot;shift-and-visualization&quot;&gt;Shift-And Visualization&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;January 05 2014&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;In this article I would like to describe the Shift-And algorithm for pattern matching, based on the book &lt;a href=&quot;http://www.amazon.com/Flexible-Pattern-Matching-Strings-Algorithms/dp/0521039932&quot;&gt;Flexible Pattern Matching in Strings&lt;/a&gt;. Apart from the actual description, you can scroll down for a demonstration of how the algorithm works.&lt;/p&gt;

&lt;p&gt;Shift-And is categorized as a prefix-based algorithm, similar to &lt;abbr title=&quot;Knuth-Morris-Pratt&quot;&gt;KMP&lt;/abbr&gt;, but what Shift-And does is to keep a set of all prefixes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;p&lt;/code&gt; that matches a suffix of the text read so far. Keep in mind that is the suffix of the text read &lt;em&gt;so far&lt;/em&gt;, not a suffix of the whole text.&lt;/p&gt;

&lt;p&gt;The set of prefixes is updated using &lt;strong&gt;bit-parallelism&lt;/strong&gt;, that is, the set of prefixes is kept in a bit mask called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;D&lt;/code&gt;. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;D&lt;/code&gt; is defined as: d&lt;sub&gt;m&lt;/sub&gt; … d&lt;sub&gt;1&lt;/sub&gt;, where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;m&lt;/code&gt; is the canonical way of referring to the pattern length. For example, if we have a six character pattern, and so far our first character matches against the text, then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;D&lt;/code&gt; would be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;000001&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With every character read from the text, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;D&lt;/code&gt; will be updated, if the bit d&lt;sub&gt;m&lt;/sub&gt; is active, then we have a match. Is it possible to update this set in constant time? Yes, using &lt;strong&gt;bit-parallel&lt;/strong&gt; operations.&lt;/p&gt;

&lt;p&gt;This algorithm will first build a table &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt;, holding a bit mask b&lt;sub&gt;m&lt;/sub&gt; … b&lt;sub&gt;1&lt;/sub&gt; for every character in the pattern. So, if we are searching for the word &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;announce&lt;/code&gt;, the entry for the character &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; will look like this: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;00100110&lt;/code&gt;, that is, the word &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;announce&lt;/code&gt; has an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;n&lt;/code&gt; in the 2nd, 3rd and 6th positions, which are marked in the bit mask from right to left. So to recap: the algorithm will scan the pattern we are searching, and then it will build this table, updating the bit mask as it goes. Here’s the code in javascript:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;c1&quot;&gt;// init the table&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;c1&quot;&gt;// build bit mask table;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;So the bit mask table for the word &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;announce&lt;/code&gt; will end up looking like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;B&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;00000001&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;00100110&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;o&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;00001000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;u&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;00010000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;c&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;01000000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10000000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
  &lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;00000000&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Of course we will have the actual numbers, not those bit masks, with the padding and so on. We also added an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;*&lt;/code&gt; entry in the table. That’s because whenever a read character from the text doesn’t match, then the bit mask used will be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once we have the table built, we start searching for the pattern in the text. First we set &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;D&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;, and then we start reading from the text, character by character. Let’s say we read an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt;, so we search for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;a&lt;/code&gt; bit mask in our table, it will return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;00000001&lt;/code&gt;. To calculate the new &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;D&lt;/code&gt; we first shift it &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt; to the left, and we &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;OR&lt;/code&gt; a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt; to it : &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(D &amp;lt;&amp;lt; 1) | 1&lt;/code&gt;. That value gets &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AND'ed&lt;/code&gt; to the mask we retrieved from the table, so this would be the complete line: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;D &amp;lt;- ((D &amp;lt;&amp;lt; 1) | 1) &amp;amp; B[current-char]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We keep iterating over the text until we find a match. How do we find a match? We need to check if the bit d&lt;sub&gt;m&lt;/sub&gt; is active. To do that we have to run the following operation: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;D &amp;amp; (1 &amp;lt;&amp;lt; m-1)&lt;/code&gt;. We create the following bit mask &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;10000000&lt;/code&gt; and we &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AND&lt;/code&gt; it to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;D&lt;/code&gt;. If this is not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;0&lt;/code&gt;, then we have a match.&lt;/p&gt;

&lt;p&gt;Here’s the complete algorithm in a vanilla implementation using Javascript:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;cm&quot;&gt;/**
  * string p: pattern to search (needle);
  * string text: haystack
**/&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;shift_and&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{};&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;tl&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;length&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
 
    &lt;span class=&quot;c1&quot;&gt;// init the table&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;//build bit mask table;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
 
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;matchMask&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;l&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;tl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;++&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nx&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;text&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;charAt&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;matched&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;d&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;matchMask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;matched&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;l&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And now to the interesting part, the visual simulation:&lt;/p&gt;

&lt;h2 id=&quot;shift-and-visual-simulation&quot;&gt;Shift-And visual simulation&lt;/h2&gt;

&lt;p&gt;Let me briefly explain what’s going on here. First we have a form, where you can enter the pattern to search, and the text where to search it for. That’s easy.&lt;/p&gt;

&lt;p&gt;Once hit &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;start&lt;/code&gt;, the visualization will display the text and bellow it the pattern you are searching for. Beneath them, there will be the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;b table&lt;/code&gt; and next to it, the results for each step will be reported.&lt;/p&gt;

&lt;p&gt;For example, we will see the initial value of D, then it will be &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(D &amp;lt;&amp;lt; 1) | 1&lt;/code&gt;. Then we read a character from the text, and we get the value from the table, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AND'ing&lt;/code&gt; it to the value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;D&lt;/code&gt; and getting the result below, and so on.&lt;/p&gt;

&lt;p&gt;I hope the colors are pretty self explanatory (I’m colorblind so don’t trust me on this one).&lt;/p&gt;

&lt;p&gt;So, click &lt;strong&gt;start&lt;/strong&gt; and enjoy the show.&lt;/p&gt;

&lt;link rel=&quot;stylesheet&quot; href=&quot;/css/sand.css&quot; type=&quot;text/css&quot; media=&quot;screen&quot; title=&quot;no title&quot; charset=&quot;utf-8&quot; /&gt;

&lt;div class=&quot;shiftand&quot;&gt;
&lt;h4&gt;Shift-And&lt;/h4&gt;
&lt;form id=&quot;shiftand&quot;&gt;
    &lt;label for=&quot;pattern&quot;&gt;Pattern:&lt;/label&gt;
    &lt;input id=&quot;pattern&quot; type=&quot;text&quot; name=&quot;pattern&quot; value=&quot;announce&quot; size=&quot;70&quot; /&gt;
    &lt;label for=&quot;text&quot;&gt;Text:&lt;/label&gt;
    &lt;textarea id=&quot;text&quot; name=&quot;text&quot; cols=&quot;50&quot; rows=&quot;3&quot;&gt;annual_announce&lt;/textarea&gt;
    &lt;input type=&quot;submit&quot; value=&quot;start&quot; /&gt;
&lt;/form&gt;
&lt;div id=&quot;vis_container&quot; class=&quot;hidden&quot;&gt;
  &lt;div id=&quot;text_blocks&quot;&gt;
    &lt;h5&gt;Visualization&lt;/h5&gt;
    &lt;div id=&quot;the-haystack&quot;&gt;
      
    &lt;/div&gt;
    &lt;div id=&quot;the-needle&quot;&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  &lt;div id=&quot;bitrun&quot;&gt;
    &lt;div id=&quot;bitmask&quot;&gt;
        &lt;h5&gt;Bit-mask Table&lt;/h5&gt;
        &lt;ul id=&quot;bitmask-table&quot; class=&quot;table&quot;&gt;&lt;/ul&gt;
    &lt;/div&gt;
    &lt;div id=&quot;match-run&quot;&gt;
        &lt;h5&gt;Run&lt;/h5&gt;
        &lt;pre&gt;&lt;/pre&gt;
     &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;
&lt;script type=&quot;text/javascript&quot; charset=&quot;utf-8&quot; src=&quot;/javascripts/jquery-1.9.1.js&quot;&gt;
  
&lt;/script&gt;

&lt;script type=&quot;text/javascript&quot; charset=&quot;utf-8&quot; src=&quot;/javascripts/jquery-ui-1.10.3.custom.js&quot;&gt;
  
&lt;/script&gt;

&lt;script type=&quot;text/javascript&quot; charset=&quot;utf-8&quot; src=&quot;/javascripts/forms.js&quot;&gt;
  
&lt;/script&gt;

&lt;script type=&quot;text/javascript&quot; charset=&quot;utf-8&quot; src=&quot;/javascripts/shift_and.js&quot;&gt;
  
&lt;/script&gt;

&lt;p&gt;I hope this post has made justice to what I think is one of the most elegant algorithms that I know of. If there was a book called &lt;em&gt;“Algorithms from the Book”&lt;/em&gt; similar to &lt;a href=&quot;http://www.amazon.com/Proofs-THE-BOOK-Martin-Aigner/dp/3642008550&quot;&gt;this&lt;/a&gt;, I’m sure this algorithm would make it.&lt;/p&gt;

&lt;p&gt;If you are interested in learning more about Pattern Matching in Strings, then I would recommend you get a copy of the book &lt;a href=&quot;http://www.amazon.com/Flexible-Pattern-Matching-Strings-Algorithms/dp/0521039932&quot;&gt;Flexible Pattern Matching in Strings&lt;/a&gt; since it’s really worth every penny.&lt;/p&gt;

&lt;p&gt;I would like to mention also Mr. &lt;a href=&quot;https://twitter.com/sicher&quot;&gt;@sicher&lt;/a&gt; who basically single handedly fixed the mess of HTML and CSS that I had created. So if you enjoyed the visual styles, then send him some props on twitter.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Things I learnt last year</title>
   <link href="http://videlalvaro.github.com/2014/01/things-I-learnt-last-year.html"/>
   <updated>2014-01-01T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2014/01/things-I-learnt-last-year</id>
   <content type="html">&lt;h1 id=&quot;things-i-learnt-last-year&quot;&gt;Things I learnt last year&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;January 01 2014&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;In this blog post I’ve plan to share some of the topics that caught my attention last year, like programming languages I used, algorithms I’ve learnt during the year, and even books I’ve read. I’m sharing this with the hope that something might be interesting to you. Let’s start with a programming language/tool that was really interesting to play with, Processing.&lt;/p&gt;

&lt;h2 id=&quot;processing&quot;&gt;Processing&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;http://www.processing.org/&quot;&gt;Processing&lt;/a&gt; is a programming language and set of tools to produce animations and graphics. It started as a language on top of the JVM and today even runs on the browser via &lt;a href=&quot;http://processingjs.org/&quot;&gt;Processing.js&lt;/a&gt;, a port of Processing to Java Script by John Resig (of jQuery fame). What I like about processing is that it uses a syntax similar to Java, so I can invest time learning about animation in this case, and not worrying too much about the language syntax.&lt;/p&gt;

&lt;p&gt;I used Processing to create the &lt;a href=&quot;http://vimeo.com/56986242&quot;&gt;RabbitMQ Simulator&lt;/a&gt; in order to improve the way I was explaining RabbitMQ at my talks. Before having the simulator I used to explain RabbitMQ routing via static images and a bit of hand waving. Today I can just start the simulator and show people how it can get messages from A to B passing through exchanges and queues. Every time I presented AMQP concepts via the Simulator I saw the faces of people in the audience showing the “A Ha!” expression.&lt;/p&gt;

&lt;p&gt;My takeaway from this experience is that images and good visuals triumph over wordy and hand-waving explanations. So whenever I want to explain something technical I think for myself in true internet spirit: “Picture or it didn’t happen”. Considering the tools we have today, if your architectural description of some software doesn’t include graphics, and if possible a simulation, then your explanation “didn’t happen”.&lt;/p&gt;

&lt;p&gt;The main inspiration for me were the many talks by &lt;a href=&quot;http://vimeo.com/worrydream&quot;&gt;Bret Victor&lt;/a&gt;, specifically the concepts introduced here: &lt;a href=&quot;http://worrydream.com/ExplorableExplanations/&quot;&gt;Explorable Explanations&lt;/a&gt;. What would you prefer, a 1000 words text explaining how RabbitMQ &lt;em&gt;basic.qos&lt;/em&gt; settings work, or a live simulation where you can tweak parameters and see the effects right away? I would prefer the later, backed with the minimum amount of theory to explain me what’s going on.&lt;/p&gt;

&lt;p&gt;Also a talk by Lea Verou where she used the tool &lt;a href=&quot;http://leaverou.github.io/CSSS/#intro&quot;&gt;CSSS&lt;/a&gt; to do her whole presentation was a great inspiration, since me, a design impaired person, could understand the wonders of the new CSS3.&lt;/p&gt;

&lt;h2 id=&quot;generative-art&quot;&gt;Generative Art&lt;/h2&gt;

&lt;p&gt;Processing got me interested into Generative Art. While I didn’t do any interesting experiment in the topic, I used books about the topic to learn more about Processing and what could be possible with the platform. Two interesting books are: &lt;a href=&quot;http://www.amazon.com/Generative-Art-Matt-Pearson/dp/1935182625&quot;&gt;Generative Art&lt;/a&gt; and &lt;a href=&quot;http://www.amazon.com/Generative-Design-Visualize-Program-Processing/dp/1616890770/&quot;&gt;Generative Design: Visualize, Program, and Create with Processing&lt;/a&gt;. The later book is simply amazing, from the cover design, to the binding, the paper quality, the graphics inside the book, everything! It’s an amazing book to have just by the looks! What is interesting is how they present each concept in one or two pages, using a couple of graphics to see the result, plus code examples that go straight to the point.&lt;/p&gt;

&lt;h2 id=&quot;rabbitmq-internals&quot;&gt;RabbitMQ Internals&lt;/h2&gt;

&lt;p&gt;In 2013 I retook my &lt;a href=&quot;https://github.com/videlalvaro/rabbit-internals&quot;&gt;old quest&lt;/a&gt; of diving into RabbitMQ’s internals. I’ve learnt a lot about what’s going on inside the server. From basic things on using pattern matching to &lt;a href=&quot;http://videlalvaro.github.io/2013/09/rabbitmq-validating-user-ids-with-erlang-pattern-matching.html&quot;&gt;validate user ids&lt;/a&gt; to more advanced concepts like &lt;a href=&quot;http://videlalvaro.github.io/2013/09/rabbitmq-internals-credit-flow-for-erlang-processes.html&quot;&gt;Credit Flow&lt;/a&gt;. The whole series of blog posts can be found &lt;a href=&quot;http://videlalvaro.github.io/internals.html&quot;&gt;here&lt;/a&gt;. I plan to expand it in the future.&lt;/p&gt;

&lt;p&gt;I’ve gave a talk about it at the &lt;a href=&quot;http://www.highload.ru/&quot;&gt;High Load++&lt;/a&gt; conference in Russia: &lt;a href=&quot;http://www.slideshare.net/old_sound/dissecting-the-rabbit&quot;&gt;Dissecting the Rabbit - RabbitMQ Internal Architecture&lt;/a&gt;. Also, I took part of one of the &lt;a href=&quot;http://vivecodigo.org&quot;&gt;Vive Codigo&lt;/a&gt; podcast episodes, where I explain Erlang concepts using RabbitMQ as the running example. The video is &lt;a href=&quot;http://vivecodigo.org/2013/10/02/episodio-4-de-la-temporada-1-erlang-con-alvaro-videla-old_sound/&quot;&gt;here&lt;/a&gt; (in Spanish).&lt;/p&gt;

&lt;h2 id=&quot;pattern-matching-tries-and-more&quot;&gt;Pattern Matching, Tries and more&lt;/h2&gt;

&lt;p&gt;RabbitMQ internals took me to pattern matching in strings. How? I wanted to write a plugin that provided a new type of exchange called the &lt;a href=&quot;https://github.com/videlalvaro/rabbitmq-rtopic-exchange&quot;&gt;reverse topic exchange&lt;/a&gt;. You can read more about the exchange in its README.&lt;/p&gt;

&lt;p&gt;The main problem I had is that one of the most efficient data structures for this kind of problem are &lt;a href=&quot;http://en.wikipedia.org/wiki/Trie&quot;&gt;Tries&lt;/a&gt;. While tries are very efficient for prefix based searches, they are very inefficient for suffix based searches, unless you build a reverse trie of the keys you’d want to search for later.&lt;/p&gt;

&lt;p&gt;Assuming you can build a reverse trie, the next issue my matching problem had was &lt;em&gt;extended matching&lt;/em&gt;, where you can use classes of characters, or bounded gaps in the patterns and so on. Trying to solve this problem I went to the classic books by &lt;a href=&quot;http://www.amazon.com/Algorithms-Parts-1-5-Bundle-Fundamentals/dp/0201756080/&quot;&gt;Sedgewick&lt;/a&gt;, &lt;a href=&quot;http://www.amazon.com/Computer-Programming-Volumes-1-4A-Boxed/dp/0321751043/&quot;&gt;TAOCP&lt;/a&gt; by Knuth and probably some other book from my shelf. Sadly I couldn’t find a proper algorithm that was efficient for all cases of the &lt;a href=&quot;https://github.com/videlalvaro/rabbitmq-rtopic-exchange#performance&quot;&gt;problem&lt;/a&gt;. That is, searches by suffix, by prefix, searches with gaps, or &lt;em&gt;key contains pattern&lt;/em&gt; kind of searches. Of course I could transform the routing key with patterns into a regex, and then run that against every binding key, but that was not the point of the algorithm.&lt;/p&gt;

&lt;p&gt;That led me to this marvelous book: &lt;a href=&quot;http://www.amazon.com/Flexible-Pattern-Matching-Strings-Algorithms/dp/0521039932&quot;&gt;Flexible Pattern Matching in Strings: Practical On-Line Search Algorithms for Texts and Biological Sequences&lt;/a&gt;. The book not only does an amazing job explaining the algorithms, it’s also straight to the point, well organized and small. A very good book to have in hand when attacking these kind of problems.&lt;/p&gt;

&lt;h3 id=&quot;bit-parallelism&quot;&gt;Bit Parallelism&lt;/h3&gt;

&lt;p&gt;For me one of the best things I’ve got from the &lt;em&gt;Flexible Pattern Matching in Strings&lt;/em&gt; book was the concept of &lt;strong&gt;bit parallel&lt;/strong&gt; algorithms, that is, algorithms that use bit-masks to perform many calculations at once. One example of these kind of algorithms is the &lt;em&gt;Shift And&lt;/em&gt; algorithm, which I can easily say that it’s a work of Art. If you can’t access that book, there’s a good explanation of the algorithm here: &lt;a href=&quot;http://www.mi.fu-berlin.de/wiki/pub/ABI/AdvancedAlgorithms11_Searching/script-03-ShiftOrUkkonenBitVecMyers.pdf&quot;&gt;Bit-parallel string matching&lt;/a&gt;. I’ve plan to do a post on the topic soon, since it’s really fascinating.&lt;/p&gt;

&lt;h3 id=&quot;the-burrows-wheeler-transform&quot;&gt;The Burrows-Wheeler Transform&lt;/h3&gt;

&lt;p&gt;The book also led to me to learning about the &lt;a href=&quot;http://en.wikipedia.org/wiki/Burrows%E2%80%93Wheeler_transform&quot;&gt;BWT&lt;/a&gt;, which similar to Shift-And is another piece of art. How did Mr. Wheeler came up with the idea of the BWT is unbeknownst to me. Then I started reading more about the topic and it happened that &lt;a href=&quot;http://www.thocp.net/biographies/wheeler_david.htm&quot;&gt;Mr. David Wheeler&lt;/a&gt; basically invented subroutines, or wrote the &lt;a href=&quot;http://www.amazon.com/preparation-programs-electronic-digital-computer/dp/093822803X&quot;&gt;first book about programming digital computers&lt;/a&gt;. If there was a website about &lt;a href=&quot;http://www.badassoftheweek.com/&quot;&gt;computer-scientist-badass of the week&lt;/a&gt; he would be there, along the likes of Grace Murray Hopper, Donald E. Knuth and John McCarthy among others.&lt;/p&gt;

&lt;p&gt;I’m still reading about the topic. I was lucky enough to get hold of a copy of this book: &lt;a href=&quot;http://www.amazon.com/The-Burrows-Wheeler-Transform-Compression-Matching/dp/0387789081/&quot;&gt;The Burrows-Wheeler Transform: Data Compression, Suffix Arrays, and Pattern Matching&lt;/a&gt;, and I’ve plan to finish it during this year.&lt;/p&gt;

&lt;h2 id=&quot;bit-bytes-and-knuths&quot;&gt;Bit, Bytes and Knuths&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Shift And&lt;/strong&gt; algorithm showed me that there are many interesting things that could be done if we become familiar with how bits and bytes work. This might sound obvious, after all we are working with computers. The problem is that usually we are working at higher levels, whether it’s OOP concepts as in the traditional Java sense, processes in the Erlang world, or even pure functions and types in Haskell, we don’t tend to use bits and bit masks every day (or maybe it’s just me).&lt;/p&gt;

&lt;p&gt;In any case the interests in bits took me again to TAOCP, in this case the chapter about &lt;em&gt;Combinatorial Searching&lt;/em&gt; from &lt;em&gt;Volume 4A&lt;/em&gt;. While I can’t claim that I understand everything that’s presented there, there’s a good deal of fascinating techniques about bits, bytes, bit masks and more.&lt;/p&gt;

&lt;p&gt;For example, I had no idea that we could use only eight 64-bit numbers to tell if a number less than 1024 is prime; or that a solution for the &lt;a href=&quot;http://chinesepuzzles.org/nine-linked-rings/&quot;&gt;Chinese Ring Puzzle&lt;/a&gt; was related with binary numbers. I grant you that these are not your every day computer science problems. My point here is that, there are many techniques we ignore about using bit and bytes that might help us solve problems that otherwise would have very complicated solutions (pre building arrays, looping and so on).&lt;/p&gt;

&lt;p&gt;Also it’s worth mentioning that for some reason that first chapter from Volume 4A has a whole section on Graph Theory, which being this a book by Knuth, it’s full of math’s about graphs. For me that was actually and advantage, since the concepts of graphs were clearly defined. I went back to &lt;a href=&quot;http://www.amazon.com/Algorithms-Parts-1-5-Bundle-Fundamentals/dp/0201756080/&quot;&gt;Sedgewick&lt;/a&gt; to compare and I kinda felt lost in all the definitions based in &lt;em&gt;plain english&lt;/em&gt; vs math. The more I read Knuth, the more I like his approach to teaching Computer Science.&lt;/p&gt;

&lt;p&gt;Apart from bit related things, I peaked over many topics from TAOCP, specifically some combinatoric problems like &lt;em&gt;generating all n-tuples&lt;/em&gt;, and Volume Two section on &lt;em&gt;Positional Number Systems&lt;/em&gt; and different radixes, like binary and so on.&lt;/p&gt;

&lt;p&gt;My take on this is that, while daunting, TAOCP is not scary at all. I could read it and understand parts from it, you can do it too, don’t be afraid of this almost mythological book. There’s a lot to learn from it.&lt;/p&gt;

&lt;h2 id=&quot;distributed-systems&quot;&gt;Distributed Systems&lt;/h2&gt;

&lt;p&gt;Last year I also decided to get some more formal education with regards to distributed systems. For some reason I bumped into this book &lt;a href=&quot;http://www.amazon.com/Fault-Tolerant-Real-Time-Systems-Determinism-International/dp/1475770286/&quot;&gt;Fault-Tolerant Real-Time Systems: The Problem of Replica Determinism&lt;/a&gt; and got hooked. The book has a very methodic explanation of the different concepts and problems from distributed systems. It cites many papers as well, so it’s a nice read to get into context and understand what someone might mean when they say “Byzantine failure”, “Consistency”, “Replica Determinism”, “Atomic Broadcast”, or things like &lt;a href=&quot;http://www.cs.cornell.edu/home/halpern/papers/common_knowledge.pdf&quot;&gt;states of knowledge&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I started a &lt;a href=&quot;http://videlalvaro.github.io/2013/12/failure-modes-in-distributed-systems.html&quot;&gt;series of blog posts&lt;/a&gt; on the topic, mostly commenting the Yellow Book, which I plan to expand along the year.&lt;/p&gt;

&lt;p&gt;Why am I learning this? I want to be able to reason and document RabbitMQ’s distributed components, specifically the &lt;a href=&quot;http://hg.rabbitmq.com/rabbitmq-server/file/bb9b95480101/src/gm.erl&quot;&gt;Guaranteed Multicast&lt;/a&gt; module and related components.&lt;/p&gt;

&lt;p&gt;I’ve also got this book: &lt;a href=&quot;http://www.amazon.com/Distributed-Algorithms-Message-Passing-Systems-Michel/dp/3642381227&quot;&gt;Distributed Algorithms for Message-Passing Systems&lt;/a&gt; which I’ve reading as well –yes, I buy a lot of books and I read a lot of books in parallel.&lt;/p&gt;

&lt;p&gt;I liked how that book explains the concept of time in distributed systems, and why a concept like vector clocks is needed.&lt;/p&gt;

&lt;h2 id=&quot;queueing-theory&quot;&gt;Queueing Theory&lt;/h2&gt;

&lt;p&gt;Is there anything to learn from queueing and scheduling theory that could help me better utilize RabbitMQ? Sure there is, and guess what, there are tons of books about it. Again, I’ve bought yet another book, this time called &lt;a href=&quot;http://www.amazon.com/Performance-Modeling-Design-Computer-Systems/dp/1107027500/&quot;&gt;Performance Modeling and Design of Computer Systems: Queueing Theory in Action&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This book is really great. Instead of the boring here’s more math, that explains the math in the previous page, that explained the math from the chapter title, you get a very easy going &lt;em&gt;questions &amp;amp; answers&lt;/em&gt; kind of book. Yes, there is math, but thanks to the questions posed by the author, you get to understand why you’ll need said math.&lt;/p&gt;

&lt;p&gt;Apart from that, this book helped me see how usually when we do performance optimization we are really just &lt;em&gt;shooting in the dark&lt;/em&gt;. Yes, I know that we usually collect metrics to see what are the most visited pages on the website to optimize those first; or we check the Nginx logs to find out what are the slowest ones, and so on. Then a profiler could pinpoint what functions are slow, and we go and optimize those. Well, I’m not talking about that particular kind of optimization.&lt;/p&gt;

&lt;p&gt;In this case queueing theory can let you answer questions like: do I need two big servers for this website, or many small ones? Shall I add more consumers to my queues or improve the job scheduling strategy?&lt;/p&gt;

&lt;p&gt;Also I see companies bragging about how they handle the extra load during &lt;em&gt;holiday x&lt;/em&gt; that brought &lt;em&gt;Y&lt;/em&gt; more users to their websites. People claim things like: “thanks to cloud vendor X, we could just fire 1000 servers and that was that, problem solved”. While this is cool, did you really need 1000 servers? Where did the 1000 number came from anyway? This book could help you answer that and many more questions about performance and capacity planning. I wrote about one of the many concepts from that book here: &lt;a href=&quot;http://www.rabbitmq.com/blog/2013/12/16/using-consumer-priorities-with-rabbitmq/&quot;&gt;Using Consumer Priorities with RabbitMQ&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;First, sorry for the long blog post, but I wanted to write this down somewhere. I think the books, links and papers that I shared above are useful to you, and of course, to a future me. All in all my conclusion is: someone before us had the same problem we are trying to solve, and that person probably documented it somewhere, or published their solution as open source (hopefully).&lt;/p&gt;

&lt;p&gt;There is no need to repeat ourselves, or others. If we have problem &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt;, there’s probably tons of literature on how to solve &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;X&lt;/code&gt;. &lt;em&gt;There’s an algorithm for that&lt;/em&gt;. We just need to be humble enough, put on the student hat, and grab a book. They really don’t bite and they can save us hours of experimenting and head scratching. I’ve prefer to scratch my head by reading TAOCP than by trying naively to solve some &lt;a href=&quot;http://en.wikipedia.org/wiki/Boolean_satisfiability_problem&quot;&gt;boolean SAT&lt;/a&gt; without even knowing that I’m getting into that particular kind of problem.&lt;/p&gt;

&lt;p&gt;I leave you with an anecdote:&lt;/p&gt;

&lt;p&gt;The other day somebody asked on the RabbitMQ about how the get data as fast as possible from China to the US, the problem being that upload bandwidth between China and the US is very low. At that time I happened to be reading about &lt;em&gt;Network Flows&lt;/em&gt; and the &lt;a href=&quot;http://en.wikipedia.org/wiki/Ford–Fulkerson_algorithm&quot;&gt;Ford-Fulkerson algorithm&lt;/a&gt; which gave me the following idea. This company had many data centers in Asia, namely Japan and Korea, so for example instead of sending the data directly to the US, assuming that a direct path is better, one could try to send the data from China to Japan and from there to the US. Using the same concept, data could travel from China to Korea, then to Japan and the US, or to the US directly from Korea. Of course this had to be experimented first in order to validate the idea. The point is, that this wouldn’t occurred to me if I hadn’t read about the Ford-Fulkerson algorithm.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Failure modes in distributed systems</title>
   <link href="http://videlalvaro.github.com/2013/12/failure-modes-in-distributed-systems.html"/>
   <updated>2013-12-20T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2013/12/failure-modes-in-distributed-systems</id>
   <content type="html">&lt;h1 id=&quot;failure-modes-in-distributed-systems&quot;&gt;Failure modes in distributed systems&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;December 20 2013&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;As I said in my previous blog post, I’ve been reading the book &lt;a href=&quot;http://www.amazon.com/Fault-Tolerant-Real-Time-Systems-Determinism-International/dp/1475770286/&quot;&gt;Fault-Tolerant Real-Time Systems: The Problem of Replica Determinism&lt;/a&gt;, which now I call &lt;em&gt;“The Yellow Book”&lt;/em&gt;. Since the book is small and self-contained, I’ve found it very good to get an introduction to Distributed Systems. It has a very good section with a list of papers that the book cites, so one can go deeper if interested.&lt;/p&gt;

&lt;p&gt;The book has a section that presents the different &lt;strong&gt;failure modes for distributed systems&lt;/strong&gt; as perceived for the user of those systems. For me it was quite interesting to see a summary of them and the relation between them, i.e.: how more severe failure modes cover less severe failure modes. Yep, it sounds pretty obvious but it wasn’t for me when I was reading the book.&lt;/p&gt;

&lt;p&gt;The modes are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Byzantine or arbitrary failures&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this case a server can send a message stating that fact &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;φ&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt; to some servers, but to others it may reply that fact &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;φ&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;. Apart from that, the server might be able to forge messages from other servers, i.e.: saying that according to server S1 fact &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;φ&lt;/code&gt; is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt; when it’s actually &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt; according to S1.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Authentification detectable byzantine failures&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this case a server may show byzantine failures but it cannot lie about facts sent by other servers.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Performance failures&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is one is pretty simple to understand. While the server is delivering the correct values, they arrive at the wrong time, either early or late.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Omission failures&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is a special case of the previous one. The server is replying “infinitely late”.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Crash failures&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When a server suffers from an &lt;em&gt;omission failure&lt;/em&gt; and then stops responding.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Fail-stop failures&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this type of failure, the server only exhibits &lt;em&gt;crash failures&lt;/em&gt;, but at the same time, we can assume that any correct server in the system can detect that this particular server has failed.&lt;/p&gt;

&lt;h2 id=&quot;relations-between-failure-modes&quot;&gt;Relations between failure modes&lt;/h2&gt;

&lt;p&gt;These failure modes, having byzantine failures as the more severe and fail-stop failures as the less severe, show what kind of assumptions we can make about a server. With byzantine failures we can’t make no assumptions at all. Which server failed, which server is correct? Who knows? With &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;fail-stop failures&lt;/code&gt; we know that server S has failed. With that in mind we can say that severe failures &lt;em&gt;contain&lt;/em&gt; or &lt;em&gt;cover&lt;/em&gt; less severe failures. So if we know a set of servers might show &lt;em&gt;performance failures&lt;/em&gt;, then we can assume the servers will also show &lt;em&gt;omission failures&lt;/em&gt;. More formally: Byzantine failures ⊃ authentification detectable byzantine failures ⊃ performance failures ⊃ omission failures ⊃ crash failures ⊃ fail-stop failures.&lt;/p&gt;

&lt;p&gt;The aforementioned failure modes can be divided in &lt;em&gt;Value failures&lt;/em&gt; and &lt;em&gt;Timing failures&lt;/em&gt;. The byzantine failure modes are value failures, while the others are timing failures.&lt;/p&gt;

&lt;p&gt;Also they can be seen as &lt;em&gt;Consistent failures&lt;/em&gt; or &lt;em&gt;Inconsistent failures&lt;/em&gt;. That is, how service users see the failure modes. In the first case, all users perceive the same failures. All the performance failures modes are in this category. Then under inconsistent failures we have the byzantine ones, where different users of the service might get different perceptions of the failure. Keep in mind that users of the systems might be other servers in our architecture, not necessarily “human users”.&lt;/p&gt;

&lt;h2 id=&quot;failure-semantics&quot;&gt;Failure semantics&lt;/h2&gt;

&lt;p&gt;The previous distinctions between failure modes bring us to something that for me is quite interesting to reason about: failure semantics. By quoting the book:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;Failure semantic&lt;/strong&gt;: a server exhibits a given failure semantic if the probability of failure modes which are not covered by the failure semantic is sufficiently low.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So if a server S1 is said to have “crash failure semantics”, then the possibility of having, say, byzantine failure should be very low. With this in mind, a service user of the previous server S1 only needs to consider that S1 will only fail with “crash failure semantics”.&lt;/p&gt;

&lt;p&gt;For me the interesting part of thinking about failure semantics are in say, a system like RabbitMQ that internally is made of many Erlang processes that talk to each other. What are the failure semantics of those subsystems? How do other processes using those subsystems behaving on the presence of failures? What’s the role for the Erlang supervision tree here? All these are interesting questions that I’ve plan to answer soon.&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;More articles on the &lt;em&gt;Yellow Book&lt;/em&gt; series:&lt;/p&gt;
&lt;ul class=&quot;homelist posts&quot;&gt;
    
      
    
      
          &lt;li&gt;&lt;span&gt;14 Dec 2013&lt;/span&gt; &lt;a href=&quot;/2013/12/tell-me-more-about-realtime.html&quot;&gt;Tell me more about your real-time systems&lt;/a&gt;&lt;/li&gt;
      
    
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Tell me more about your real-time systems</title>
   <link href="http://videlalvaro.github.com/2013/12/tell-me-more-about-realtime.html"/>
   <updated>2013-12-14T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2013/12/tell-me-more-about-realtime</id>
   <content type="html">&lt;h1 id=&quot;tell-me-more-about-your-real-time-systems&quot;&gt;Tell me more about your real-time systems&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;December 14 2013&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;I’ve been reading the book &lt;a href=&quot;http://www.amazon.com/Fault-Tolerant-Real-Time-Systems-Determinism-International/dp/1475770286/&quot;&gt;Fault-Tolerant Real-Time Systems: The Problem of Replica Determinism&lt;/a&gt; and I’ve been sincerely blown out by the content. The book is really interesting, and even more for me: a complete n00b with regards to real-time systems.&lt;/p&gt;

&lt;p&gt;The second chapter gives an introduction about automotive electronics, since the “distributed systems” explained in the book are in fact cars internal electronics. In this short blog post I’d like to share a paragraph from that chapter:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;High activation frequencies: In automotive electronics there are functions which require high service activation frequencies. Some control loops require sampling frequencies of up to 4 kHz. In engine control control applications even higher service activation frequencies are required to monitor the actual position of the crankshaft or camshaft. Some systems resolve one revolution of the engine with an accuracy of 6 degrees. By considering a maximum engine speed of 7000 rpm the time interval between the passage of 6 degree crank  shaft is as little as 142µs. This results in a service activation frequency of 7 kHz for the service which monitors the crank angle position. For these reasons cumulated service activation frequencies of up to 10kHz are assumed. To achieve these high activation frequencies the context switch between different tasks has to be very efficient.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;By reading descriptions like this I kind of understand some people dislike of the use of the term &lt;em&gt;“real-time”&lt;/em&gt; for systems that are in fact not real-time at all. I think the current evolution of technical language around web developers has made real-time mean: &lt;em&gt;“consume information as soon as it is available”&lt;/em&gt; and not &lt;em&gt;“react to the information in a timely manner or this car will crash”&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;More articles on the &lt;em&gt;Yellow Book&lt;/em&gt; series:&lt;/p&gt;
&lt;ul class=&quot;homelist posts&quot;&gt;
    
      
          &lt;li&gt;&lt;span&gt;20 Dec 2013&lt;/span&gt; &lt;a href=&quot;/2013/12/failure-modes-in-distributed-systems.html&quot;&gt;Failure modes in distributed systems&lt;/a&gt;&lt;/li&gt;
      
    
      
    
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>RabbitMQ Internals - Credit Flow for Erlang Processes</title>
   <link href="http://videlalvaro.github.com/2013/09/rabbitmq-internals-credit-flow-for-erlang-processes.html"/>
   <updated>2013-09-09T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2013/09/rabbitmq-internals-credit-flow-for-erlang-processes</id>
   <content type="html">&lt;h1 id=&quot;rabbitmq-internals---credit-flow-for-erlang-processes&quot;&gt;RabbitMQ Internals - Credit Flow for Erlang Processes&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;September 09 2013&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;With a queueing server like RabbitMQ there’s the potential problem of having publishers that out pace consumers, creating the issue of queues that start to back off and get filled with messages. If this problem is not taken into account, RabbitMQ will run out of disk space at some point. While this is more off an issue for the app that uses RabbitMQ than of RabbitMQ itself, the broker still needs to provide a way to prevent the system from going down under such circumstances. To deal with this problem RabbitMQ implements the concept of &lt;strong&gt;credit flow&lt;/strong&gt; for communication between Erlang processes.&lt;/p&gt;

&lt;p&gt;RabbitMQ has a reader process, implemented inside the &lt;a href=&quot;http://hg.rabbitmq.com/rabbitmq-server/file/f3f18609e649/src/rabbit_reader.erl&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_reader.erl&lt;/code&gt;&lt;/a&gt; module that takes care of reading data from the network and convert it to AMQP commands that are dispatched to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_channel.erl&lt;/code&gt; module / process. So for example whenever you publish an AMQP message via &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;basic.publish&lt;/code&gt;, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_reader&lt;/code&gt; will interpret that command and send it over to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_channel&lt;/code&gt; process to handle it properly. To do that an Erlang message is sent between the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_reader&lt;/code&gt; process and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_channel&lt;/code&gt;.  But what happens if your app goes rogue and overwhelms the RabbitMQ server with more messages from what it can process? Could we prevent that? Enter &lt;a href=&quot;http://hg.rabbitmq.com/rabbitmq-server/file/f3f18609e649/src/credit_flow.erl&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credit_flow.erl&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;With &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credit_flow&lt;/code&gt; what happens is that a process that wants to use the flow mechanism has a certain amount of credit for sending messages to a particular process. That credit is diminished with every message sent via the flow mechanism. After &lt;em&gt;X&lt;/em&gt; messages, the receiver process will grant more credit to the sender so it can continue sending messages in case it got blocked at some point. How does it all works?&lt;/p&gt;

&lt;p&gt;First we have what is called a credit specification in the form of a tuple like:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;InitialCredit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;MoreCreditAfter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;For example unless you specify different values when using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credit_flow&lt;/code&gt; API, your process will start with a default credit of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{200, 50}&lt;/code&gt;. That is it can send 200 messages until it runs out of credit. How does process &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; gets more credit either before or after credits runs down? Let’s say that process &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; is sending messages to process &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt;. Every time &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; processes a message sent by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; it will “ack” it back. Once it acks &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MoreCreditAfter&lt;/code&gt; messages from process &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt;, then it will grant more credit to process it. Let’s see how this is implemented taking the interactions between &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_reader&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_channel&lt;/code&gt; as our example.&lt;/p&gt;

&lt;p&gt;Whenever &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_reader&lt;/code&gt; parses a protocol command that has content, like a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;basic.publish&lt;/code&gt;, it will pass it onto the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_channel&lt;/code&gt; like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;nn&quot;&gt;rabbit_channel&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;do_flow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ChPid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And here’s how’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_channel:do_flow/3&lt;/code&gt; is implemented:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;nf&quot;&gt;do_flow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nn&quot;&gt;credit_flow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;nn&quot;&gt;gen_server2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;cast&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;flow&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}).&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;As you can see the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_channel&lt;/code&gt; module takes care of telling the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credit_flow&lt;/code&gt; module to account for the message received from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_reader&lt;/code&gt; by calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credit_flow:send(Pid)&lt;/code&gt; where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Pid&lt;/code&gt; is the &lt;em&gt;process id&lt;/em&gt; of the current channel. While this is a bit confusing at first, what happens is that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_reader&lt;/code&gt;, in this case the message sender, will track how many messages it has sent to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_channel&lt;/code&gt; process.&lt;/p&gt;

&lt;p&gt;Now, where is this information tracked? The credits are being tracked on the &lt;a href=&quot;http://www.erlang.org/course/advanced.html#dict&quot;&gt;process dictionary&lt;/a&gt;. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credit_flow&lt;/code&gt; module will call the &lt;a href=&quot;http://erlang.org/doc/man/erlang.html#get-1&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;get/1&lt;/code&gt;&lt;/a&gt; and &lt;a href=&quot;http://erlang.org/doc/man/erlang.html#put-2&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;put/2&lt;/code&gt;&lt;/a&gt; BIFs to get and update those values.&lt;/p&gt;

&lt;p&gt;Let’s backtrack a bit and see how this works.&lt;/p&gt;

&lt;h2 id=&quot;keeping-track-of-credits&quot;&gt;Keeping track of credits&lt;/h2&gt;

&lt;p&gt;As we can see from the code, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_reader&lt;/code&gt; will call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_channel:do_flow/3&lt;/code&gt; and finally that function will call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credit_flow:send/1&lt;/code&gt; which will decrease the credit for the reader process. The thing to understand here is that while the function call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credit_flow:send/1&lt;/code&gt; happens inside the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_channel&lt;/code&gt; module, we are still inside the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_reader&lt;/code&gt; process. Only after the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gen_server2:cast(Pid, {method, Method, Content, flow})&lt;/code&gt; is handled, we are going to be into the memory space of the channel process. Therefore when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credit_flow:send/1&lt;/code&gt; is called, the credit decreased operation is tracked inside the sender, in this case the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_reader&lt;/code&gt; process dictionary.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/credit_flow_process_dict.png&quot; alt=&quot;Process dictionary&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The key used to store that information is like this &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{credit_from, From}&lt;/code&gt;, where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;From&lt;/code&gt; is the Pid of the message receiver, in this case the channel (The previous image shows all the dictionary keys used by the credit flow mechanism). If the information stored in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{credit_from, From}&lt;/code&gt; key reaches &lt;strong&gt;0&lt;/strong&gt; then the process that owns that dictionary will get blocked, in our example that process would be the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_reader&lt;/code&gt; process. Here’s the implementation of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credit_flow:send&lt;/code&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;nb&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;From&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;InitialCredit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;MoreCreditAfter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;UPDATE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;credit_from&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;From&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;InitialCredit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;C&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;From&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                         &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;true&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;C&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;&lt;img src=&quot;/images/credit_flow_send.png&quot; alt=&quot;credit_flow:send&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Now keep in mind that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_reader&lt;/code&gt; process could have been sending messages to various processes at the same time where is possible to reach a state where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_reader&lt;/code&gt; is blocked by more than one process. Therefore each process keeps a list in its dictionary where it tracks which processes pids are blocking it, if any:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;n&quot;&gt;credit_blocked&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;acknowledging-messages&quot;&gt;Acknowledging messages&lt;/h2&gt;

&lt;p&gt;We said that whenever a process handles a message it will &lt;em&gt;ack&lt;/em&gt; the message via the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credit_flow&lt;/code&gt; system, possible granting more credit:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;nf&quot;&gt;ack&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;To&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{_&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;InitialCredit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;MoreCreditAfter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;UPDATE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;credit_to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;To&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;MoreCreditAfter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;C&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;grant&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;To&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;MoreCreditAfter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                         &lt;span class=&quot;nv&quot;&gt;MoreCreditAfter&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;true&lt;/span&gt;   &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;C&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;In this case the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_channel&lt;/code&gt; process will keep track of how many messages it has &lt;em&gt;ack’ed&lt;/em&gt; from a particular sender (in our case the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_reader&lt;/code&gt;). The key used to keep that message in the dictionary is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{credit_to, To}&lt;/code&gt; where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;To&lt;/code&gt; is the pid of the sender. After &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;MoreCreditAfter&lt;/code&gt; messages has been received it will grant more credit to the sender.&lt;/p&gt;

&lt;h2 id=&quot;granting-more-credit&quot;&gt;Granting more credit&lt;/h2&gt;

&lt;p&gt;To grant more credit a process will send a message of the form &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{bump_credit, {self(), Quantity}}&lt;/code&gt; to the process that should receive the credit, where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;self()&lt;/code&gt; points to the process granting credit. But granting credit is not an easy task tho. Why? Because to grant more credits a process needs to send a message to whichever process it is trying to send credits. While sending a message is not a problem, it could happen that a process has been blocked, and therefore it can’t send messages. What happens here? The blocked message will keep that credit for later inside the dictionary pointed by the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credit_deferred&lt;/code&gt; key where it’ll keep a list of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{Msg, To}&lt;/code&gt; tuples. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Msg&lt;/code&gt; in this case has this signature: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{bump_credit, {self(), Quantity}}&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;handling-bump-credit-messages&quot;&gt;Handling bump credit messages&lt;/h2&gt;

&lt;p&gt;When a process receives a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bump_credit&lt;/code&gt; message it needs handle it by calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credit_flow:handle_bump_msg/1&lt;/code&gt; passing the received message over. Because we control is on the process that’s handling the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bump_credit&lt;/code&gt; message, we can access the dictionary of that particular process. The process will update the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{credit_from, From}&lt;/code&gt; key, where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;From&lt;/code&gt; is the pid of the process that granted credits. So if the credits goes above zero, then the process will be unblocked for that particular receiver. If by calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credit_flow:unblock/1&lt;/code&gt; the list of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credit_blocked&lt;/code&gt; goes empty, then the process can resume sending messages. Also &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credit_flow:unblock/1&lt;/code&gt; will take care of sending all the messages that were saved for later in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credit_deferred&lt;/code&gt; list. This means we could potential have a chain of blocked processes. Here’s the code for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;handle_bump_msg&lt;/code&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;nf&quot;&gt;handle_bump_msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;From&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;MoreCredit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;UPDATE&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;credit_from&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;From&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;C&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;lt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;andalso&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;C&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;MoreCredit&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;unblock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;From&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                                                    &lt;span class=&quot;nv&quot;&gt;C&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;MoreCredit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
               &lt;span class=&quot;n&quot;&gt;true&lt;/span&gt;                              &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;C&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;MoreCredit&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;unblocking-a-process&quot;&gt;Unblocking a process&lt;/h2&gt;

&lt;p&gt;When &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;unblock/1&lt;/code&gt; is called what will actually happen is that the &lt;em&gt;pid&lt;/em&gt; of the process granting credits will be removed from the list of pids that’s being kept in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credit_blocked&lt;/code&gt; key. So if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C&lt;/code&gt; are blocking &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; this is what will happen if &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;B&lt;/code&gt; unblocks &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;c&quot;&gt;%% We are operating on process A dictionary.
&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;credit_blocked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;unblock&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;B&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;credit_blocked&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;C&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;In this particular case &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; will still remain blocked until &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C&lt;/code&gt; grants more credit to it. Once that happens then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; will be free again to send messages. At this point process &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;A&lt;/code&gt; will take care of his &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credit_deferred&lt;/code&gt; list, sending &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;bump_credit&lt;/code&gt; messages to those process that were on the deferred list.&lt;/p&gt;

&lt;h2 id=&quot;peer-down&quot;&gt;Peer down&lt;/h2&gt;

&lt;p&gt;The credit flow mechanism also handles the case of a peer process going down. Keep in mind that a process keeps track of credits in a per receiver pid basis. So if the receiver dies, then the process has to remove the entries for that particular process. Those entries where kept in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{credit_from, Pid}&lt;/code&gt; used by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credit_flow:send&lt;/code&gt;. Also because the process might have received messages from the dead process, then it has to delete the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;{credit_to, Pid}&lt;/code&gt; key.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;When we have systems that have queues of any kind, we need to be aware that if our producers outpace our consumers, then those queues will grow infinitely. In the case of Erlang, each process will have it’s own mailbox and if a process can’t keep up with the amount of messages is receiving, at some point everything will break havoc. By having a system like the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;credit_flow&lt;/code&gt; in place, as used by RabbitMQ, we can prevent those scenarios and react accordingly.&lt;/p&gt;

&lt;p&gt;PS: If you wonder how the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;?UPDATE&lt;/code&gt; macro works, take a look at its implementation &lt;a href=&quot;http://hg.rabbitmq.com/rabbitmq-server/file/f3f18609e649/src/credit_flow.erl#l57&quot;&gt;here&lt;/a&gt;. I leave it as an exercise to the reader to understand how that work and I’ll explain it in a future blog post.&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;More articles on the RabbitMQ internals series:&lt;/p&gt;
&lt;ul class=&quot;homelist posts&quot;&gt;
    
      
    
      
          &lt;li&gt;&lt;span&gt;04 Sep 2013&lt;/span&gt; &lt;a href=&quot;/2013/09/rabbitmq-internals-validating-erlang-behaviours.html&quot;&gt;RabbitMQ Internals - Validating Erlang Behaviours&lt;/a&gt;&lt;/li&gt;
      
    
      
          &lt;li&gt;&lt;span&gt;02 Sep 2013&lt;/span&gt; &lt;a href=&quot;/2013/09/rabbitmq-sanitzing-user-input-in-erlang.html&quot;&gt;RabbitMQ - Sanitizing user input in Erlang&lt;/a&gt;&lt;/li&gt;
      
    
      
          &lt;li&gt;&lt;span&gt;01 Sep 2013&lt;/span&gt; &lt;a href=&quot;/2013/09/rabbitmq-validating-user-ids-with-erlang-pattern-matching.html&quot;&gt;RabbitMQ - Validating User IDs with Erlang Pattern Matching&lt;/a&gt;&lt;/li&gt;
      
    
      
          &lt;li&gt;&lt;span&gt;29 Aug 2013&lt;/span&gt; &lt;a href=&quot;/2013/08/erlang-pattern-matching-protocols-and-rabbitmq.html&quot;&gt;Erlang Pattern Matching, Protocols and RabbitMQ&lt;/a&gt;&lt;/li&gt;
      
    
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>RabbitMQ Internals - Validating Erlang Behaviours</title>
   <link href="http://videlalvaro.github.com/2013/09/rabbitmq-internals-validating-erlang-behaviours.html"/>
   <updated>2013-09-04T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2013/09/rabbitmq-internals-validating-erlang-behaviours</id>
   <content type="html">&lt;h1 id=&quot;rabbitmq-internals---validating-erlang-behaviours&quot;&gt;RabbitMQ Internals - Validating Erlang Behaviours&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;September 04 2013&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;RabbitMQ implements several routing algorithms in the form of exchanges, therefore we say that each exchange has a &lt;em&gt;type&lt;/em&gt;. By default RabbitMQ comes with four exchange types: &lt;em&gt;direct&lt;/em&gt;, &lt;em&gt;fanout&lt;/em&gt;, &lt;em&gt;topic&lt;/em&gt; and &lt;em&gt;headers&lt;/em&gt; and allows the user to add new exchange types via plugins. For example there’s the &lt;a href=&quot;http://hg.rabbitmq.com/rabbitmq-consistent-hash-exchange/file/rabbitmq_v3_1_5/README.md&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbitmq-consistent-hash-exchange&lt;/code&gt;&lt;/a&gt; which adds consistent hash routing to RabbitMQ.&lt;/p&gt;

&lt;p&gt;Now, how does RabbitMQ makes sure that an user provided exchange type is in fact an implementation of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_exchange_type&lt;/code&gt; behaviour? It doesn’t make sense for RabbitMQ to register a new exchange type that’s unable to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;route/2&lt;/code&gt; messages.&lt;/p&gt;

&lt;p&gt;The process of checking that a certain exchange implements the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_exchange_type&lt;/code&gt; behaviour starts when the exchange type is registered. RabbitMQ keeps a registry of exchange types by using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_registry&lt;/code&gt; module. When a new exchange type is defined, it is registered like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;nn&quot;&gt;rabbit_registry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;register&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exchange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;direct&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rabbit_exchange_type_direct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]).&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;What that code does is to register an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exchange&lt;/code&gt; of the type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;&amp;lt;&quot;direct&quot;&amp;gt;&amp;gt;&lt;/code&gt;, which is implemented by the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_exchange_type_direct&lt;/code&gt; module.&lt;/p&gt;

&lt;p&gt;The first part of the puzzle is a map of classes to behaviours that’s part of RabbitMQ’s source code. If you are confused at this point, don’t worry, I am as well.&lt;/p&gt;

&lt;p&gt;RabbitMQ has an architecture that supports plugins that could modify the broker behaviour in several places. Adding new exchange types is one of those places, but there are more, like for example adding new authentication mechanisms. Each of those concepts –exchanges and authentication mechanisms– have their own class. In the following code:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;class_module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exchange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;           &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rabbit_exchange_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;class_module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;auth_mechanism&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;     &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rabbit_auth_mechanism&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;there’s a map definition that says the class &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exchange&lt;/code&gt; is implemented via the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_exchange_type&lt;/code&gt; behaviour and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auth_mechanism&lt;/code&gt; class is implemented via the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_auth_mechanism&lt;/code&gt; behaviour.&lt;/p&gt;

&lt;p&gt;When we call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_registry:register&lt;/code&gt; RabbitMQ will sanity check the parameters we passed to see if our new exchange module actually implements the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_exchange_type&lt;/code&gt; behaviour. That task is taken care by the function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_registry:sanity_check_module/2&lt;/code&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;sanity_check_module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ClassModule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;lists&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;member&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;ClassModule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                            &lt;span class=&quot;nn&quot;&gt;lists&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;flatten&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                              &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Bs&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;||&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Attr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Bs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt;
                                         &lt;span class=&quot;nv&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;module_info&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attributes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
                                     &lt;span class=&quot;nv&quot;&gt;Attr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;behavior&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;orelse&lt;/span&gt;
                                         &lt;span class=&quot;nv&quot;&gt;Attr&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;behaviour&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;of&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;'EXIT'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;undef&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;_}}&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;not_module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;false&lt;/span&gt;                 &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;not_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;ClassModule&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}};&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;true&lt;/span&gt;                  &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This function grabs the attributes of our module by calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Module:module_info(attributes)&lt;/code&gt; and then inspects their declared behaviours to see if they implement the required one for the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exchange&lt;/code&gt; class, in this case &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_exchange_type&lt;/code&gt;. Note also the error handling here. RabbitMQ takes into account the fact that we could provide a non existing module.&lt;/p&gt;

&lt;p&gt;The takeaway here is that if you are going to allow your users to extend your Erlang application, first your application needs to expose behaviours that have to be implemented by the user. From there, we can resort to inspecting the attributes of the module provided by the user to see if the expected behaviours are present. While this technique might seem rather simple, it’s good to remember that we can add our own attributes to our Erlang modules and then later retrieve them to do interesting things with them, see the &lt;a href=&quot;https://github.com/videlalvaro/rabbit-internals/blob/master/rabbit_boot_process.md&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_boot&lt;/code&gt;&lt;/a&gt; system for an advanced example of this technique.&lt;/p&gt;

&lt;p&gt;To see the full source code go here:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_registry:sanity_check_module/2&lt;/code&gt; &lt;a href=&quot;http://hg.rabbitmq.com/rabbitmq-server/file/dd70b629351f/src/rabbit_registry.erl#l117&quot;&gt;source code&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;More articles on the RabbitMQ internals series:&lt;/p&gt;
&lt;ul class=&quot;homelist posts&quot;&gt;
    
      
          &lt;li&gt;&lt;span&gt;09 Sep 2013&lt;/span&gt; &lt;a href=&quot;/2013/09/rabbitmq-internals-credit-flow-for-erlang-processes.html&quot;&gt;RabbitMQ Internals - Credit Flow for Erlang Processes&lt;/a&gt;&lt;/li&gt;
      
    
      
    
      
          &lt;li&gt;&lt;span&gt;02 Sep 2013&lt;/span&gt; &lt;a href=&quot;/2013/09/rabbitmq-sanitzing-user-input-in-erlang.html&quot;&gt;RabbitMQ - Sanitizing user input in Erlang&lt;/a&gt;&lt;/li&gt;
      
    
      
          &lt;li&gt;&lt;span&gt;01 Sep 2013&lt;/span&gt; &lt;a href=&quot;/2013/09/rabbitmq-validating-user-ids-with-erlang-pattern-matching.html&quot;&gt;RabbitMQ - Validating User IDs with Erlang Pattern Matching&lt;/a&gt;&lt;/li&gt;
      
    
      
          &lt;li&gt;&lt;span&gt;29 Aug 2013&lt;/span&gt; &lt;a href=&quot;/2013/08/erlang-pattern-matching-protocols-and-rabbitmq.html&quot;&gt;Erlang Pattern Matching, Protocols and RabbitMQ&lt;/a&gt;&lt;/li&gt;
      
    
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>RabbitMQ - Sanitizing user input in Erlang</title>
   <link href="http://videlalvaro.github.com/2013/09/rabbitmq-sanitzing-user-input-in-erlang.html"/>
   <updated>2013-09-02T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2013/09/rabbitmq-sanitzing-user-input-in-erlang</id>
   <content type="html">&lt;h1 id=&quot;rabbitmq---sanitizing-user-input-in-erlang&quot;&gt;RabbitMQ - Sanitizing user input in Erlang&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;September 02 2013&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;This post presents a rather basic technique for user input sanitization but useful nonetheless. Let’s first introduce the problem at hand.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.rabbitmq.com/&quot;&gt;RabbitMQ&lt;/a&gt; supports various types of routing algorithms that are implemented in the form of exchanges. Therefore each exchange has its own &lt;em&gt;type&lt;/em&gt;. Exchange types are declared as Erlang binaries, like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;&amp;lt;&quot;direct&quot;&amp;gt;&amp;gt;&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;lt;&amp;lt;&quot;fanout&quot;&amp;gt;&amp;gt;&lt;/code&gt; for example. That means during the process of booting up the broker, all the supported exchange types are defined, &lt;em&gt;direct&lt;/em&gt; in this case, and an Erlang module is registered for each particular exchange implementation. Then when a user wants to create an exchange to use in his/her application, it has to provide a type, say, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;direct&quot;&lt;/code&gt;. RabbitMQ then needs to pick up the right exchange module based on the user input, and as we know, we should never trust user provided input. What happens if the user would try to forge that value into something malicious?&lt;/p&gt;

&lt;p&gt;There are at least two problems with mapping user provided input to Erlang module names: 1) the user might try to load a different module. 2) Erlang module names are just Erlang &lt;em&gt;atoms&lt;/em&gt; and the amount of atoms is limited on an Erlang system. If the system runs out of atoms, it will crash (note that the default limit is 2^20, so it’s more than enough).&lt;/p&gt;

&lt;p&gt;The first problem has two sides. First if someone has already been able to upload arbitrary code into our Erlang node and is trying to execute it, then we have a much more serious problem that goes beyond RabbitMQ security (or any other Erlang application for that matter). Supposing that hasn’t happened, on the next blog post we are going to see what RabbitMQ does to ensure that the module called is actually an implementation of an exchange type and not say: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dump_every_user_credential.erl&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For the second problem, this is what RabbitMQ does: whenever we declare an exchange, RabbitMQ will call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_exchange:check_type/1&lt;/code&gt; passing the binary provided over the wire with the exchange type, say «“direct”», or «“fanout”». RabbitMQ does that because it won’t declare an exchange for which there’s no implementation, (keep in mind user can add new exchange algorithms via &lt;a href=&quot;http://www.rabbitmq.com/plugins.html&quot;&gt;plugins&lt;/a&gt;). To assert that the exchange type does exist, RabbitMQ keeps a registry mapping &lt;em&gt;types&lt;/em&gt; to &lt;em&gt;Erlang modules&lt;/em&gt;. For example the &lt;em&gt;direct&lt;/em&gt; exchange logic is implemented inside the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_exchange_type_direct&lt;/code&gt; module. To find the actual module, RabbitMQ has to transform the binary that it has received over the wire into an atom, and here’s where the problem starts.&lt;/p&gt;

&lt;p&gt;To sanitize or validate the input &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_exchange:check_type/1&lt;/code&gt; calls &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_registry:binary_to_type/1&lt;/code&gt; which has a very simple implementation, but one that is very good to know, at least for Erlang beginners who might not know the dangers of running out of atoms –this sound like the perfect plot from some cheap sci-fi book–. Here’s the code:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;binary_to_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;TypeBin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;when&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;is_binary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;TypeBin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;catch&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;list_to_existing_atom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;binary_to_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;TypeBin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;of&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;'EXIT'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;badarg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;_}}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;not_found&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;TypeAtom&lt;/span&gt;              &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;TypeAtom&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;What this code does is very simple to understand, first it converts the supplied binary to a list, and then uses Erlang’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;list_to_existing_atom&lt;/code&gt; to prevent the user from creating new atoms and potentially crashing the system. The error handling logic there is present just in case the supplied binary doesn’t translate into an existing atom. The remaining question now is, where does RabbitMQ creates those exchange type atoms?&lt;/p&gt;

&lt;p&gt;When an exchange type implementation is created, for example in the module &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_exchange_type_direct&lt;/code&gt;, the exchange type has to be registered, During that process, the binary type is converted into an atom using the previous process in reverse, like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;internal_binary_to_type&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;TypeBin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;when&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;is_binary&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;TypeBin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nb&quot;&gt;list_to_atom&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;binary_to_list&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;TypeBin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)).&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;With this simple technique RabbitMQ can translate back and forth from user supplied input into the exchange types that it supports. Note that this is used in several more places in the RabbitMQ source code, for example, RabbitMQ supports plugable authentication backends, where again it has to map from user provided binaries into implementation modules. The same happens when it needs to detect the type of &lt;a href=&quot;http://www.rabbitmq.com/ha.html#genesis&quot;&gt;mirroring mode&lt;/a&gt; chosen for a HA queue.&lt;/p&gt;

&lt;p&gt;In the next blog post we are going to explore how RabbitMQ ensures that a new exchange type provided by a plugin is in fact an implementation of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_exchange_type&lt;/code&gt; behaviour, or that an authentication mechanism exposed by a plugin is an implementation of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_auth_mechanism&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To see the full source code go here:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_registry:binary_to_type/1&lt;/code&gt; &lt;a href=&quot;http://hg.rabbitmq.com/rabbitmq-server/file/fe3f446ab083/src/rabbit_registry.erl#l62&quot;&gt;source code&lt;/a&gt;.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_registry:internal_binary_to_type/1&lt;/code&gt; &lt;a href=&quot;http://hg.rabbitmq.com/rabbitmq-server/file/fe3f446ab083/src/rabbit_registry.erl#l81&quot;&gt;source code&lt;/a&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;More articles on the RabbitMQ internals series:&lt;/p&gt;
&lt;ul class=&quot;homelist posts&quot;&gt;
    
      
          &lt;li&gt;&lt;span&gt;09 Sep 2013&lt;/span&gt; &lt;a href=&quot;/2013/09/rabbitmq-internals-credit-flow-for-erlang-processes.html&quot;&gt;RabbitMQ Internals - Credit Flow for Erlang Processes&lt;/a&gt;&lt;/li&gt;
      
    
      
          &lt;li&gt;&lt;span&gt;04 Sep 2013&lt;/span&gt; &lt;a href=&quot;/2013/09/rabbitmq-internals-validating-erlang-behaviours.html&quot;&gt;RabbitMQ Internals - Validating Erlang Behaviours&lt;/a&gt;&lt;/li&gt;
      
    
      
    
      
          &lt;li&gt;&lt;span&gt;01 Sep 2013&lt;/span&gt; &lt;a href=&quot;/2013/09/rabbitmq-validating-user-ids-with-erlang-pattern-matching.html&quot;&gt;RabbitMQ - Validating User IDs with Erlang Pattern Matching&lt;/a&gt;&lt;/li&gt;
      
    
      
          &lt;li&gt;&lt;span&gt;29 Aug 2013&lt;/span&gt; &lt;a href=&quot;/2013/08/erlang-pattern-matching-protocols-and-rabbitmq.html&quot;&gt;Erlang Pattern Matching, Protocols and RabbitMQ&lt;/a&gt;&lt;/li&gt;
      
    
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>RabbitMQ - Validating User IDs with Erlang Pattern Matching</title>
   <link href="http://videlalvaro.github.com/2013/09/rabbitmq-validating-user-ids-with-erlang-pattern-matching.html"/>
   <updated>2013-09-01T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2013/09/rabbitmq-validating-user-ids-with-erlang-pattern-matching</id>
   <content type="html">&lt;h1 id=&quot;rabbitmq---validating-user-ids-with-erlang-pattern-matching&quot;&gt;RabbitMQ - Validating User IDs with Erlang Pattern Matching&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;September 01 2013&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;In the &lt;a href=&quot;http://videlalvaro.github.io/2013/08/erlang-pattern-matching-protocols-and-rabbitmq.html&quot;&gt;previous blog post&lt;/a&gt; on this series on &lt;a href=&quot;https://www.rabbitmq.com&quot;&gt;RabbitMQ&lt;/a&gt; internals we saw how Erlang’s pattern matching can be used to process protocol calls, in this one we are going to see how RabbitMQ handles the validation of user ids by using pattern matching, one of Erlang most powerful features.&lt;/p&gt;

&lt;p&gt;User IDs can be used in AMQP to tell a consumer that a certain message was published by a certain user. To do that we just set the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; property of a message whenever we publish one. Now, I could login as Bob and set the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; property of my messages as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&quot;Alice&quot;&lt;/code&gt;and nobody would suspect I tainted my messages. How could we prevent that?&lt;/p&gt;

&lt;p&gt;Enter &lt;a href=&quot;http://www.rabbitmq.com/validated-user-id.html&quot;&gt;validated user ids&lt;/a&gt;. What this feature does is that RabbitMQ will make sure the user id set on the message property field is the same one as the user that’s logged-in and publishing messages to RabbitMQ. The way this is implemented uses a feature of Erlang pattern matching that’s more similar to Prolog’s logical variables than to pattern matching in languages like Haskell, F# or Clojure.&lt;/p&gt;

&lt;p&gt;Here’s the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;check_user_id_header&lt;/code&gt; function that we will be analyzing:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;check_user_id_header&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;#'P_basic'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;_)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;check_user_id_header&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;#'P_basic'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
                     &lt;span class=&quot;nl&quot;&gt;#ch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;#user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;username&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;check_user_id_header&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;#'P_basic'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Claimed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
                     &lt;span class=&quot;nl&quot;&gt;#ch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;#user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;username&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Actual&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                      &lt;span class=&quot;n&quot;&gt;tags&lt;/span&gt;     &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Tags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;lists&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;member&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;impersonator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Tags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;of&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;true&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;precondition_failed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                   &lt;span class=&quot;s&quot;&gt;&quot;user_id property set to '&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;~s&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;' but authenticated user was &quot;&lt;/span&gt;
                   &lt;span class=&quot;s&quot;&gt;&quot;'&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;~s&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Claimed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Actual&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The first clause of the function is very simple: if the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; property is undefined, then RabbitMQ returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ok&lt;/code&gt; since there’s no reason to validate it.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;check_user_id_header&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;#'P_basic'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;undefined&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;_)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;On the second case we have this special way that Erlang does pattern matching that’s not present in out-of-the-box Haskell for example, that is, using the same variable name on the function header. What’s going on here? First what happens is that here &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#'P_basic'{user_id = Username}&lt;/code&gt; the variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Username&lt;/code&gt; is initialized to whatever value was on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; field of the message properties record. Say for example that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Username&lt;/code&gt; will equal “«“Bob”»”. Then here &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#ch{user = #user{username = Username}}&lt;/code&gt; we can see that the function goes into the channel state to extract the username of the user that opened the connection and then matches the value of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;username&lt;/code&gt; filed with the variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Username&lt;/code&gt; that was set before on the same header. If the values are the same, the function will return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ok&lt;/code&gt;, but if they don’t match then this function clause won’t be executed. Wait, what?&lt;/p&gt;

&lt;p&gt;In Erlang, when you first use a variable, it is said that the variable is “unbound”, so in our case when the variable is &lt;em&gt;equaled&lt;/em&gt; to the value of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; property from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#P_basic&lt;/code&gt; record what happens is that the variable takes the value stored there. Why? Because in Erlang the equals sign &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; is not used for setting variables, but for pattern matching. Every time we use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;=&lt;/code&gt; in Erlang what we are doing is pattern matching what’s on the &lt;em&gt;right&lt;/em&gt; of the equals sign with what’s on the left. If the values don’t match, then Erlang will produce an error. In this case since the variable is unbound what happens is that it gets set to whatever was on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; field. So far so good.&lt;/p&gt;

&lt;p&gt;The second time the variable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Username&lt;/code&gt; is used on that function header, it is not unbound anymore, it holds a value in it, so when Erlang extracts the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;username&lt;/code&gt; value and pattern-matches it against the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Username&lt;/code&gt; variable then, well, it has to match. If it doesn’t match then the function clause is skipped and Erlang moves onto the third header. Here’s the code:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;check_user_id_header&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;#'P_basic'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
                     &lt;span class=&quot;nl&quot;&gt;#ch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;#user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;username&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;For the sake of completeness let’s comment the third and final &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;check_user_id_header&lt;/code&gt; clause. In this case RabbitMQ takes into account the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;impersonator&lt;/code&gt; tag. This tag can be set on a particular user to allow him to publish messages on behalf of others. Here the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; is kept in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Claimed&lt;/code&gt; variable and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;username&lt;/code&gt; is set in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Actual&lt;/code&gt; variable. In this case &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Claimed&lt;/code&gt; will never be the same as &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Actual&lt;/code&gt;, because if they were, then the previous function header would have matched. Here’s the code:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;check_user_id_header&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;#'P_basic'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user_id&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Claimed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
                     &lt;span class=&quot;nl&quot;&gt;#ch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;#user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;username&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Actual&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                                      &lt;span class=&quot;n&quot;&gt;tags&lt;/span&gt;     &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Tags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;lists&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;member&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;impersonator&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Tags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;of&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;true&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;false&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;precondition_failed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
                   &lt;span class=&quot;s&quot;&gt;&quot;user_id property set to '&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;~s&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;' but authenticated user was &quot;&lt;/span&gt;
                   &lt;span class=&quot;s&quot;&gt;&quot;'&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;~s&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Claimed&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Actual&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;We can argue that this is rather simple code, and for me it’s the simplicity of this use case that makes Erlang so great. When I see that second function header I can easily notice that the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;user_id&lt;/code&gt; passed when publishing the message has to match the one set channel state &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;username&lt;/code&gt; field. As simple as that.&lt;/p&gt;

&lt;p&gt;In case you are interested in reading more, here’s the code from where these examples were extracted: &lt;a href=&quot;http://hg.rabbitmq.com/rabbitmq-server/file/fe3f446ab083/src/rabbit_channel.erl#l491&quot;&gt;rabbit_channel.erl&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;More articles on the RabbitMQ internals series:&lt;/p&gt;
&lt;ul class=&quot;homelist posts&quot;&gt;
    
      
          &lt;li&gt;&lt;span&gt;09 Sep 2013&lt;/span&gt; &lt;a href=&quot;/2013/09/rabbitmq-internals-credit-flow-for-erlang-processes.html&quot;&gt;RabbitMQ Internals - Credit Flow for Erlang Processes&lt;/a&gt;&lt;/li&gt;
      
    
      
          &lt;li&gt;&lt;span&gt;04 Sep 2013&lt;/span&gt; &lt;a href=&quot;/2013/09/rabbitmq-internals-validating-erlang-behaviours.html&quot;&gt;RabbitMQ Internals - Validating Erlang Behaviours&lt;/a&gt;&lt;/li&gt;
      
    
      
          &lt;li&gt;&lt;span&gt;02 Sep 2013&lt;/span&gt; &lt;a href=&quot;/2013/09/rabbitmq-sanitzing-user-input-in-erlang.html&quot;&gt;RabbitMQ - Sanitizing user input in Erlang&lt;/a&gt;&lt;/li&gt;
      
    
      
    
      
          &lt;li&gt;&lt;span&gt;29 Aug 2013&lt;/span&gt; &lt;a href=&quot;/2013/08/erlang-pattern-matching-protocols-and-rabbitmq.html&quot;&gt;Erlang Pattern Matching, Protocols and RabbitMQ&lt;/a&gt;&lt;/li&gt;
      
    
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Erlang Pattern Matching, Protocols and RabbitMQ</title>
   <link href="http://videlalvaro.github.com/2013/08/erlang-pattern-matching-protocols-and-rabbitmq.html"/>
   <updated>2013-08-29T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2013/08/erlang-pattern-matching-protocols-and-rabbitmq</id>
   <content type="html">&lt;h1 id=&quot;erlang-pattern-matching-protocols-and-rabbitmq&quot;&gt;Erlang Pattern Matching, Protocols and RabbitMQ&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;August 29 2013&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Here’s a very simple example from &lt;a href=&quot;https://www.rabbitmq.com&quot;&gt;RabbitMQ&lt;/a&gt;’s source code on how easy it’s to implement a protocol calls with Erlang.&lt;/p&gt;

&lt;p&gt;While RabbitMQ supports many &lt;a href=&quot;http://www.rabbitmq.com/protocols.html&quot;&gt;protocols&lt;/a&gt;, internally mostly everything is handled via AMQP. In AMQP there’s the concept of a &lt;em&gt;Channel&lt;/em&gt;, which is just a way to subdivide connections. Most of the commands send to the server, whether to publish or consume messages, or to create queues, are done over the channel. To be able to use a channel, first we have to open one, by calling &lt;em&gt;&lt;a href=&quot;https://www.rabbitmq.com/amqp-0-9-1-quickref.html#channel.open&quot;&gt;channel.open&lt;/a&gt;&lt;/em&gt;. Let’s see how RabbitMQ handles these calls by taking advantage of Erlang’s pattern matching.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;handle_method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;#'channel.open'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;_,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;State&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;#ch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;starting&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;#'channel.open_ok'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;State&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;#ch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}};&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;handle_method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;#'channel.open'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;_,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;State&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nn&quot;&gt;rabbit_misc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;protocol_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;command_invalid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;second 'channel.open' seen&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]);&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;handle_method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(_&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;_,&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;#ch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;starting&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nn&quot;&gt;rabbit_misc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;protocol_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;channel_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;expected 'channel.open'&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;There we have three different headers for the same Erlang function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;handle_method/3&lt;/code&gt;, where the actual function body to be executed will depend on the parameters passed to the function. The function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;handle_method/3&lt;/code&gt; expects three parameters: the actual AMQP &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Method&lt;/code&gt;, its &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Content&lt;/code&gt; (for example the published message), and the current channel &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;State&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The default &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt; of a channel that has been just instantiated is ‘starting’, so we can see that the first body of the function will be called only on a new channel, and RabbitMQ replies &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;channel.open_ok&lt;/code&gt; to the client:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;handle_method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;#'channel.open'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;_,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;State&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;#ch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;starting&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;reply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;#'channel.open_ok'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;State&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;#ch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;running&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}};&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;As you can see from the return of this function, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt; of the channel is updated to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;running&lt;/code&gt;. We can also learn from here that while we have &lt;em&gt;variables that don’t vary&lt;/em&gt; in Erlang, we can still update the state of our program with ease.&lt;/p&gt;

&lt;p&gt;Then the second function header is where Erlang’s pattern matching starts to play. If the method called is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;channel.open&lt;/code&gt;, but the channel &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;state&lt;/code&gt; is different from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;starting&lt;/code&gt; RabbitMQ can safely return an error to the client: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;second 'channel.open' seen&lt;/code&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;handle_method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;#'channel.open'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;_,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;State&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nn&quot;&gt;rabbit_misc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;protocol_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;command_invalid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;second 'channel.open' seen&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;On the third function header we have that we can receive any method, but the channel has just been created as indicated by the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;starting&lt;/code&gt; state. In this case RabbitMQ covers all those cases where the user tried to send a method over the channel &lt;em&gt;before&lt;/em&gt; having received the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;channel.open_ok&lt;/code&gt;:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;handle_method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(_&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Method&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;_,&lt;/span&gt; &lt;span class=&quot;nl&quot;&gt;#ch&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;state&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;starting&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nn&quot;&gt;rabbit_misc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;protocol_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;channel_error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;expected 'channel.open'&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]);&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;...&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;As you can see on those examples, with a few basic rules expressed via Erlang’s powerful pattern matching we can handle all these tiny detail of protocol negotiation. While these examples might seem trivial, I hope to dive deeper into RabbitMQ’s source code and present more advanced examples.&lt;/p&gt;

&lt;p&gt;In case you are interested in reading more, here’s the code from where these examples were extracted: &lt;a href=&quot;http://hg.rabbitmq.com/rabbitmq-server/file/fe3f446ab083/src/rabbit_channel.erl#l593&quot;&gt;rabbit_channel.erl&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;hr /&gt;

&lt;p&gt;&lt;br /&gt;&lt;/p&gt;
&lt;p&gt;More articles on the RabbitMQ internals series:&lt;/p&gt;
&lt;ul class=&quot;homelist posts&quot;&gt;
    
      
          &lt;li&gt;&lt;span&gt;09 Sep 2013&lt;/span&gt; &lt;a href=&quot;/2013/09/rabbitmq-internals-credit-flow-for-erlang-processes.html&quot;&gt;RabbitMQ Internals - Credit Flow for Erlang Processes&lt;/a&gt;&lt;/li&gt;
      
    
      
          &lt;li&gt;&lt;span&gt;04 Sep 2013&lt;/span&gt; &lt;a href=&quot;/2013/09/rabbitmq-internals-validating-erlang-behaviours.html&quot;&gt;RabbitMQ Internals - Validating Erlang Behaviours&lt;/a&gt;&lt;/li&gt;
      
    
      
          &lt;li&gt;&lt;span&gt;02 Sep 2013&lt;/span&gt; &lt;a href=&quot;/2013/09/rabbitmq-sanitzing-user-input-in-erlang.html&quot;&gt;RabbitMQ - Sanitizing user input in Erlang&lt;/a&gt;&lt;/li&gt;
      
    
      
          &lt;li&gt;&lt;span&gt;01 Sep 2013&lt;/span&gt; &lt;a href=&quot;/2013/09/rabbitmq-validating-user-ids-with-erlang-pattern-matching.html&quot;&gt;RabbitMQ - Validating User IDs with Erlang Pattern Matching&lt;/a&gt;&lt;/li&gt;
      
    
      
    
&lt;/ul&gt;
</content>
 </entry>
 
 <entry>
   <title>Writing RabbitMQ Plugins With Elixir</title>
   <link href="http://videlalvaro.github.com/2013/06/writing-rabbitmq-plugins-with-elixir.html"/>
   <updated>2013-06-03T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2013/06/writing-rabbitmq-plugins-with-elixir</id>
   <content type="html">&lt;h1 id=&quot;writing-rabbitmq-plugins-with-elixir&quot;&gt;Writing RabbitMQ Plugins With Elixir&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;June 3 2013&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;RabbitMQ is a very extensible message broker, allowing its users to extend the server functionality by writing plugins. Even many of the broker features are shipped as plugins that come by default with the broker installation: &lt;a href=&quot;http://www.rabbitmq.com/management.html&quot;&gt;The Management Plugin&lt;/a&gt;, or &lt;a href=&quot;https://www.rabbitmq.com/stomp.html&quot;&gt;STOMP&lt;/a&gt; support, to name just a couple. While that’s pretty cool, the fact that plugins must be written in Erlang is sometimes in itself a challenge. I decided to see if it was possible to write plugins in another language that targeted the Erlang Virtual Machine (EVM) and in this post I’d share my progress.&lt;/p&gt;

&lt;h2 id=&quot;elixir&quot;&gt;Elixir&lt;/h2&gt;

&lt;p&gt;In the last couple of months I’ve been paying attention to a new programming language called &lt;a href=&quot;http://elixir-lang.org&quot;&gt;&lt;em&gt;Elixir&lt;/em&gt;&lt;/a&gt; that targets the EVM and in the last week it became immensely popular inside the Erlang community (and outer circles) since &lt;a href=&quot;https://twitter.com/joeerl&quot;&gt;Joe Armstrong&lt;/a&gt;, the father of Erlang, &lt;a href=&quot;http://joearms.github.io/2013/05/31/a-week-with-elixir.html&quot;&gt;tried the language&lt;/a&gt;, and liked it very much. So I said, OK, lets give it a try.&lt;/p&gt;

&lt;p&gt;I don’t want to spend much time describing that Elixir is, it’s better if you read its website or Joe Armstrong blog post about it. For me it brings ideas from the Ruby Language into the Erlang platform. We can basically write Erlang programs with an arguable better syntax, for some definition of better syntax.&lt;/p&gt;

&lt;p&gt;Compare this Erlang Hello World program:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;p&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;ni&quot;&gt;module&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;module_name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;% you may use some other name
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;ni&quot;&gt;compile&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;export_all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;

&lt;span class=&quot;nf&quot;&gt;hello&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;nn&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;~s~n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hello world!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]).&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;With this one in Elixir:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;  &lt;span class=&quot;c1&quot;&gt;# module_name.ex&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;defmodule&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;ModuleName&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hello&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
      &lt;span class=&quot;no&quot;&gt;IO&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;puts&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Hello World&quot;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;While yes, this example is quite trivial, I think for someone that has never seen Erlang syntax, the Elixir version is easier to read. From my point of view, I always liked Erlang’s syntax, since it makes the semantic of Erlang concepts stand up and be very clear of what’s going on, but again, that’s my own point of view.&lt;/p&gt;

&lt;p&gt;Now considering that people might prefer to write RabbitMQ plugins with Elixir, let’s see how that could be done&lt;/p&gt;

&lt;h2 id=&quot;writing-rabbitmq-plugins&quot;&gt;Writing RabbitMQ Plugins&lt;/h2&gt;

&lt;p&gt;To write plugins for RabbitMQ you will need to setup your development environment to use the tools, Makefiles and libraries provided by the &lt;em&gt;rabbitmq public umbrella&lt;/em&gt;. You can follow the setup instructions from &lt;a href=&quot;http://www.rabbitmq.com/plugin-development.html&quot;&gt;here&lt;/a&gt;. Once you have cloned the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;http://hg.rabbitmq.com/rabbitmq-public-umbrella&lt;/code&gt; project and have all the dependencies installed, we can start writing our own plugin. To do it with Elixir, you first need to install it on your machine, so you can use the Elixir compiler &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mix&lt;/code&gt; and the language libraries.&lt;/p&gt;

&lt;p&gt;When you write RabbitMQ plugins you may want to use some Erlang library with your plugin, to do that you need to &lt;em&gt;wrap it&lt;/em&gt; as a plugin as well, so it can be picked up by the build environment when you declare that library as a dependency of your project. In this case our new plugins will depend on Elixir, so we need to wrap the language libraries as a plugin. I’ve done that already and you can just clone the &lt;a href=&quot;https://github.com/videlalvaro/elixir_wrapper&quot;&gt;elixir_wrapper&lt;/a&gt; from Github and follow the instructions on its README to get it installed.&lt;/p&gt;

&lt;p&gt;Now is time to create our own plugin. As an example I’ve ported to Elixir a plugin called &lt;em&gt;recent-history-exchange&lt;/em&gt; that as the name implies, it adds a new exchange type to RabbitMQ. Exchanges are routing algorithms that RabbitMQ uses to decide where your messages are going to end up. If you want to read more about exchanges please go &lt;a href=&quot;http://www.rabbitmq.com/tutorials/amqp-concepts.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The code for the new exchange can be found on github: &lt;a href=&quot;https://github.com/videlalvaro/rabbitmq-recent-history-exchange-elixir&quot;&gt;RabbitMQ Recent History Exchange&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The code for the new exchange type lives inside the file &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lib/rabbit_exchange_type_recent_history.ex&lt;/code&gt;, where we implement the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbit_exchange_type&lt;/code&gt; behaviour. The overridden methods are: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;route/2&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;delete/3&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;add_binding&lt;/code&gt;. What this exchange does is to cache the last 20 messages while they are routed to queues. Whenever a new queue is bound to the exchange, then we deliver those last 20 messages to it. Finally whenever the exchange is deleted we remove its entries from the database. When is this useful? Say you implement a chat with RabbitMQ and you want that people that join the room get the last messages sent to the room. This is a simple way to accomplish that.&lt;/p&gt;

&lt;p&gt;While you could understand most of the code there if you first follow an &lt;a href=&quot;http://elixir-lang.org/getting_started/1.html&quot;&gt;Elixir Tutorial&lt;/a&gt; there are some points worth noting since for me it was not so clear on how to port them into Elixir. To take a look at what was the original project I was porting from Erlang take look at the project &lt;a href=&quot;https://github.com/videlalvaro/rabbitmq-recent-history-exchange&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&quot;module-attributes&quot;&gt;Module Attributes&lt;/h3&gt;

&lt;p&gt;RabbitMQ uses a concept of &lt;em&gt;boot steps&lt;/em&gt; in order to start the broker. Those boot steps are scanned when the broker starts and from there plugins are automatically picked up by the server. They are declared as module attributes, so my first blocker was how to add a module attribute to Elixir. Assuming we have the following module:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span class=&quot;k&quot;&gt;defmodule&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;RabbitExchangeTypeRecentHistory&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;To add an attribute to it like the ones expected by RabbitMQ we have to do like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span class=&quot;k&quot;&gt;defmodule&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;RabbitExchangeTypeRecentHistory&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;

  &lt;span class=&quot;no&quot;&gt;Module&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;register_attribute&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;__MODULE__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
       &lt;span class=&quot;ss&quot;&gt;:rabbit_boot_step&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
       &lt;span class=&quot;ss&quot;&gt;accumulate:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;persist:&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;true&lt;/span&gt;

  &lt;span class=&quot;nv&quot;&gt;@rabbit_boot_step&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;__MODULE__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                     &lt;span class=&quot;p&quot;&gt;[{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:description&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;exchange type x-recent-history&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
                      &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:mfa&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:rabbit_registry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:register&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                              &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:exchange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;x-recent-history&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;__MODULE__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]}},&lt;/span&gt;
                      &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:requires&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:rabbit_registry&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;
                      &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:enables&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:kernel_ready&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}]&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;First we have to register our attribute by calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Module.register_attribute&lt;/code&gt; and then we can use them in our code as in this example.&lt;/p&gt;

&lt;h3 id=&quot;behaviours&quot;&gt;Behaviours&lt;/h3&gt;

&lt;p&gt;Declaring behaviours in our modules is quite easy. We just need to add a behaviour attribute to our module like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;  &lt;span class=&quot;nv&quot;&gt;@behaviour&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:rabbit_exchange_type&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h3 id=&quot;erlang-records&quot;&gt;Erlang Records&lt;/h3&gt;

&lt;p&gt;When you develop RabbitMQ plugins and probably whenever you interop with Erlang libraries you will need to use the records defined in the library. This isn’t as straightforward as I was expecting. We have to import the record definitions into Elixir, for example, to have the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;#exchange&lt;/code&gt; record from RabbitMQ we have to do like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-elixir&quot; data-lang=&quot;elixir&quot;&gt;&lt;span class=&quot;k&quot;&gt;defmodule&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;RabbitExchangeTypeRecentHistory&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;  
  &lt;span class=&quot;k&quot;&gt;defrecord&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:exchange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Record&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;extract&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:exchange&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;from_lib:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;rabbit_common/include/rabbit.hrl&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Keep in mind that here I’m just placing snippets of the code. You don’t need to define the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RabbitExchangeTypeRecentHistory&lt;/code&gt; module every time.&lt;/p&gt;

&lt;h2 id=&quot;building-the-plugin&quot;&gt;Building the Plugin&lt;/h2&gt;

&lt;p&gt;Once we finished implementing our plugin we might want to build it, after all there’s a reason why we spent all this time on it. To do so we need to add two &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Makefiles&lt;/code&gt; files into our project folder in order to integrate it with RabbitMQ’s build system.&lt;/p&gt;

&lt;p&gt;The first is called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Makefile&lt;/code&gt; and it includes just one line:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-make&quot; data-lang=&quot;make&quot;&gt;&lt;span class=&quot;k&quot;&gt;include&lt;/span&gt;&lt;span class=&quot;sx&quot;&gt; ../umbrella.mk&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The second one is a bit more involved. Here we specify the dependencies of our project and we tell the RabbitMQ build system how to compile our Elixir code.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-make&quot; data-lang=&quot;make&quot;&gt;&lt;span class=&quot;nv&quot;&gt;DEPS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt;rabbitmq-server rabbitmq-erlang-client elixir_wrapper
&lt;span class=&quot;nv&quot;&gt;RETAIN_ORIGINAL_VERSION&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;ORIGINAL_VERSION&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt;0.1
&lt;span class=&quot;nv&quot;&gt;DO_NOT_GENERATE_APP_FILE&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt;

&lt;span class=&quot;nv&quot;&gt;CONSTRUCT_APP_PREREQS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt;mix-compile
&lt;span class=&quot;err&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;construct_app_commands&lt;/span&gt;
	&lt;span class=&quot;err&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;-p&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;$(APP_DIR)/ebin&lt;/span&gt;
	&lt;span class=&quot;err&quot;&gt;cp&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;$(PACKAGE_DIR)/ebin/*&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;$(APP_DIR)/ebin&lt;/span&gt;
&lt;span class=&quot;err&quot;&gt;endef&lt;/span&gt;

&lt;span class=&quot;err&quot;&gt;define&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;package_rules&lt;/span&gt;

&lt;span class=&quot;nl&quot;&gt;$(PACKAGE_DIR)/deps/.done&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;
	&lt;span class=&quot;nb&quot;&gt;rm&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-rf&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;$$&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;@D&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;nb&quot;&gt;mkdir&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-p&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;$$&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;@D&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;p&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;elided] unzip ezs
	&lt;span class=&quot;p&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cd&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;$$&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;@D&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;foreach&lt;/span&gt; EZ,&lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;wildcard&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$(PACKAGE_DIR)&lt;/span&gt;/build/dep-ezs/&lt;span class=&quot;k&quot;&gt;*&lt;/span&gt;.ez&lt;span class=&quot;nf&quot;&gt;)&lt;/span&gt;,unzip &lt;span class=&quot;nt&quot;&gt;-q&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;abspath&lt;/span&gt; &lt;span class=&quot;err&quot;&gt;$&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$(EZ)&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;)&lt;/span&gt; :
	&lt;span class=&quot;nb&quot;&gt;touch&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$$&lt;/span&gt;@

&lt;span class=&quot;nl&quot;&gt;mix-compile&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;$(PACKAGE_DIR)/deps/.done&lt;/span&gt;
	mix clean
	&lt;span class=&quot;nv&quot;&gt;ERL_LIBS&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$(PACKAGE_DIR)&lt;/span&gt;/deps mix compile

&lt;span class=&quot;err&quot;&gt;endef&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;I won’t explain this code line by line, but just the remarkable bits. On the first line we declare our plugin’s dependencies. In this case we depend on the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbitmq server&lt;/code&gt; and the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbitmq-erlang-client&lt;/code&gt; in order to have access to all the behaviours and records required by our plugin. Of course we also depend on the Elixir libraries to be shipped with our plugin.&lt;/p&gt;

&lt;p&gt;Then we define some make targets to compile our plugin and package it inside an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.ez&lt;/code&gt; file. (RabbitMQ plugins are shipped as .ez files). The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mix-compile&lt;/code&gt; target will as the name says, build our Elixir code. As you might have noticed it sets the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ERL_LIBS&lt;/code&gt; variable our plugin &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;./deps&lt;/code&gt; folder. For that to work we first need to have the dependencies unpacked there, therefore the make rule &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$(PACKAGE_DIR)/deps/.done&lt;/code&gt;, which will unpack into that folder the previously built dependencies.&lt;/p&gt;

&lt;p&gt;Finally our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;define construct_app_commands&lt;/code&gt; will copy our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.beam&lt;/code&gt; files into the destination folder so the RabbitMQ build system will be able to find them and ship them inside our plugin &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.ez&lt;/code&gt; file.&lt;/p&gt;

&lt;p&gt;Once we have everything in place it’s time to actually build our plugin. We can do so by simply calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;make&lt;/code&gt; inside the plugin folder.&lt;/p&gt;

&lt;p&gt;Once the build process has finished, we can find the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.ez&lt;/code&gt; files inside the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;dist&lt;/code&gt; folder&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-bash&quot; data-lang=&quot;bash&quot;&gt;&lt;span class=&quot;nb&quot;&gt;ls &lt;/span&gt;dist/
amqp_client-3.3.1.ez                                     elixir-0.9.2.dev-rmq3.3.1-git7c379aa.ez                  rabbit_common-3.3.1.ez                                   rabbitmq_recent_history_exchange_elixir-0.1-rmq3.3.1.ez&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;From those files we only need to distribute &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbitmq_recent_history_exchange_elixir-0.1-rmq3.3.1.ez&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elixir-0.9.2.dev-rmq3.3.1-git7c379aa.ez&lt;/code&gt; as part of our plugin.&lt;/p&gt;

&lt;h2 id=&quot;installing-the-plugin&quot;&gt;Installing the Plugin&lt;/h2&gt;

&lt;p&gt;Copy the files &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbitmq_recent_history_exchange_elixir-0.1-rmq3.3.1.ez&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elixir-0.9.2.dev-rmq3.3.1-git7c379aa.ez&lt;/code&gt; into the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;plugins&lt;/code&gt; folder of your RabbitMQ installation and then activate the plugin by calling: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/sbin/rabbitmq-plugins enable rabbitmq_recent_history_exchange_elixir&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Once we start the broker we can see in the management interface that we can add exchanges of the type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x-recent-history&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/rh_exchange.png&quot; alt=&quot;Recent History Exchange&quot; /&gt;&lt;/p&gt;

&lt;h2 id=&quot;coda&quot;&gt;Coda&lt;/h2&gt;

&lt;p&gt;And that’s that. We can build RabbitMQ plugins using Elixir, most the &lt;em&gt;yak shaving&lt;/em&gt; is done already we just need to use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;elixir_wrapper&lt;/code&gt; and create the proper &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;package.mk&lt;/code&gt; file for our plugin. From there it’s just a matter of invoking &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;make&lt;/code&gt;. Let me know what do you think and if you build a RabbitMQ plugin using Elixir please share it and let me know.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Using RabbitMQ in Unit Tests</title>
   <link href="http://videlalvaro.github.com/2013/04/using-rabbitmq-in-unit-tests.html"/>
   <updated>2013-04-29T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2013/04/using-rabbitmq-in-unit-tests</id>
   <content type="html">&lt;h1 id=&quot;using-rabbitmq-in-unit-tests&quot;&gt;Using RabbitMQ in Unit Tests&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;April 29 2013&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;In this blog post I want to show you a very simple technique for using RabbitMQ in our Unit or Functional Tests. Let’s say you wrote a bunch of tests for your RabbitMQ consumers and then it’s time to run them. To do that you probably need to setup a RabbitMQ server just for tests with their own users and passwords, or you need to set up a whole new virtual host for your tests. While that task is not that hard to accomplish, you might need to install RabbitMQ and Erlang in order to get the broker running.&lt;/p&gt;

&lt;p&gt;With a future release of RabbitMQ that we can already test on the &lt;a href=&quot;http://www.rabbitmq.com/nightlies/rabbitmq-server/current/&quot;&gt;nightlies website&lt;/a&gt;, we can run RabbitMQ without the need to install Erlang. We created a package that ships a stripped down version of Erlang together with the broker bits, so running RabbitMQ now is as easy as downloading a tarball, uncompressing it and starting the server. It’s worth noting that this works only on Mac for now, but we plan to extend it for other platforms soon.&lt;/p&gt;

&lt;p&gt;The tarball is called &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbitmq-server-mac-standalone-3.0.4.XYZ.tar.gz&lt;/code&gt; where XYZ is the current build version. At the time of this writing this is the current tarball &lt;a href=&quot;http://www.rabbitmq.com/nightlies/rabbitmq-server/current/rabbitmq-server-mac-standalone-3.0.4.40426.tar.gz&quot;&gt;http://www.rabbitmq.com/nightlies/rabbitmq-server/current/rabbitmq-server-mac-standalone-3.0.4.40426.tar.gz&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The cool thing about that tarball is that we can use it as a very cheap setup to run our RabbitMQ tests against. We can download the tarball and then keep it in a known location in our machine. Then every time we run our test suite we can uncompress it, start the broker, run the tests and then stop the broker and erase the folder contents. Pretty easy.&lt;/p&gt;

&lt;p&gt;I’ve implemented that using PHPUnit but I’m sure you can implement it with your favorite unit testing framework.&lt;/p&gt;

&lt;p&gt;PHPUnit has a concept of test listeners that you can attach to your test suites. They will be invoked whenever the test suite runs and you can use them to do several stuff, in our case, we are going to start/stop RabbitMQ.&lt;/p&gt;

&lt;p&gt;Here’s how we set up a test listener in our XML configuration file:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-xml&quot; data-lang=&quot;xml&quot;&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;listeners&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;listener&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;RabbitMQListener&quot;&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;file=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;PhpAmqpLib/Tests/RabbitMQListener.php&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;arguments&amp;gt;&lt;/span&gt;
            &lt;span class=&quot;nt&quot;&gt;&amp;lt;string&amp;gt;&lt;/span&gt;/tmp/rabbitmq-server-mac-standalone-3.0.4.40426.tar.gz&lt;span class=&quot;nt&quot;&gt;&amp;lt;/string&amp;gt;&lt;/span&gt;
        &lt;span class=&quot;nt&quot;&gt;&amp;lt;/arguments&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;nt&quot;&gt;&amp;lt;/listener&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/listeners&amp;gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;There we specify our listener class, it’s location in relation to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;phpunit.xml&lt;/code&gt; file and we pass an argument to the listener with the location of the RabbitMQ tarball.&lt;/p&gt;

&lt;p&gt;Our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RabbitMQListener&lt;/code&gt; will take care of finding the tarball and uncompressing it into a temporary folder. Then it will start the broker and once it is started it will let the test suite continue running tests. For each test, it will reset the broker state so there are no queues or exchanges leftovers between tests. Finally, when the test suite is over, it will shut down the broker and will proceed to delete the temporary folder contents.&lt;/p&gt;

&lt;p&gt;Once we add the listener configuration to PHPUnit and we specify the right path to the new RabbitMQ tarball, we can run our tests by simply calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;phpunit&lt;/code&gt; as we used to do.&lt;/p&gt;

&lt;p&gt;Here’s the code for the PHPUnit listener class:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;cp&quot;&gt;&amp;lt;?php&lt;/span&gt;

&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;RabbitMQListener&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;implements&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;PHPUnit_Framework_TestListener&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$tmp_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$rabbit_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$rabbit_tar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$rabbit_pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__construct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rabbit_tar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; 
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rabbit_tar&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$rabbit_tar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp_dir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sys_get_temp_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;();&lt;/span&gt;

        &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;extractRabbitMQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rabbit_tar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;nv&quot;&gt;$rabbit_dir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;getRabbitMQDir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rabbit_tar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;strlen&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rabbit_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rabbit_base&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tmp_dir&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'/'&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$rabbit_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rabbit_pid&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rabbit_base&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'var/lib/rabbitmq/rabbitmq.pid'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;

        &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;createRabbitConf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rabbit_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rabbit_pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;startRabbitMQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rabbit_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;isRabbitMQRunning&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rabbit_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rabbit_pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;k&quot;&gt;exit&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__destruct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;stopRabbitMQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rabbit_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rabbit_pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;clearRabbitMQDir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rabbit_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;maybeStopEpmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rabbit_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;maybeStopEpmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rabbit_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'ps ax | grep epmd | grep -v grep'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;strstr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$rabbit_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'epmd -kill'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;startTestSuite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PHPUnit_Framework_TestSuite&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$suite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;resetRabbitMQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rabbit_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;resetRabbitMQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rabbit_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rabbit_base&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'sbin/rabbitmqctl stop_app'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rabbit_base&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'sbin/rabbitmqctl reset'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rabbit_base&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'sbin/rabbitmqctl start_app'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;extractRabbitMQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rabbit_tar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$tmp_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$tar_extract_cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'tar -xzf '&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$rabbit_tar&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;' -C '&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$tmp_dir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$tar_extract_cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getRabbitMQDir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rabbit_tar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$tar_dir_cmd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'tar --exclude=&quot;*/*/*&quot; -tf '&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$rabbit_tar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$tar_dir_cmd&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$dir_out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;isset&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$dir_out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;?&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$dir_out&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;createRabbitConf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rabbit_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$rabbit_pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$rabbit_conf_dir&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$rabbit_base&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'etc/rabbitmq/'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$rabbit_conf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$rabbit_conf_dir&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'rabbitmq-env.conf'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
        &lt;span class=&quot;nv&quot;&gt;$conf&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sprintf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'RABBITMQ_PID_FILE=&quot;%s&quot;%s'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$rabbit_pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;

        &lt;span class=&quot;nb&quot;&gt;file_put_contents&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rabbit_conf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$conf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;startRabbitMQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rabbit_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rabbit_base&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'sbin/rabbitmq-server &amp;gt; /dev/null 2&amp;gt;&amp;amp;1 &amp;amp;'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;waitForRabbitMQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rabbit_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$rabbit_pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rabbit_base&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'sbin/rabbitmqctl wait '&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$rabbit_pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$output&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$return_val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$return_val&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;isRabbitMQRunning&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rabbit_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$rabbit_pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;waitForRabbitMQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rabbit_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$rabbit_pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;stopRabbitMQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rabbit_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$rabbit_pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rabbit_base&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'sbin/rabbitmqctl stop '&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$rabbit_pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;removePid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rabbit_pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'rm '&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$rabbit_pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;protected&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;clearRabbitMQDir&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$rabbit_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;nb&quot;&gt;exec&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'rm -rf '&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$rabbit_base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endTestSuite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PHPUnit_Framework_TestSuite&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$suite&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PHPUnit_Framework_Test&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addFailure&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PHPUnit_Framework_Test&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;PHPUnit_Framework_AssertionFailedError&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addIncompleteTest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PHPUnit_Framework_Test&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;addSkippedTest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PHPUnit_Framework_Test&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$e&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;startTest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PHPUnit_Framework_Test&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;endTest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;PHPUnit_Framework_Test&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$test&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;For the purposes of this blog post the whole listener code sits in one class, but I think one smart thing to do with that code would be to move the bits that manage RabbitMQ to a separate utils-like class.&lt;/p&gt;

&lt;p&gt;I hope this can be useful to you and let me know about any improvements you’d like to see. Maybe once we can get this into a more generic class we can add it to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;php-amqplib&lt;/code&gt;. One interesting change to add will be the possibility to run the broker in a different port, and also a way to pass our test user credentials to the listener class so RabbitMQ can use those instead of the default &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;guest:guest&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So go here and check out what’s being cooked at the RabbitMQ labs: &lt;a href=&quot;http://www.rabbitmq.com/nightlies/rabbitmq-server/current/&quot;&gt;http://www.rabbitmq.com/nightlies/rabbitmq-server/current/&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;NOTE: If you have Erlang installed you can do the same by using the &lt;a href=&quot;http://www.rabbitmq.com/releases/rabbitmq-server/v3.0.4/rabbitmq-server-generic-unix-3.0.4.tar.gz&quot;&gt;generic-unix tarball&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Conference Speaking Tips</title>
   <link href="http://videlalvaro.github.com/2012/06/conference-speaking-tips.html"/>
   <updated>2012-06-23T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2012/06/conference-speaking-tips</id>
   <content type="html">&lt;h1 id=&quot;conference-speaking-tips&quot;&gt;Conference Speaking Tips&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;June 23 2012&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;After having attended way too many conferences over the past two years both as an atendee and a speaker I would like to share some tips I’ve learnt along the way for those who want to present at conferences. My tips are quite simple and even obvious, but I’ve committed these mistakes many times and I’ve seen others commit them as well quite a few times, so here we go.&lt;/p&gt;

&lt;h2 id=&quot;theres-color-blind-people-in-your-audience&quot;&gt;There’s color blind people in your audience&lt;/h2&gt;

&lt;p&gt;Believe it or not there’s color blind people out there. I’m one of them. So if you present your nice charts and graphs with combinations of Red and Green chances are high that I won’t understand what you are demoing. As simple as that. Which lead us to:&lt;/p&gt;

&lt;h2 id=&quot;high-contrast-colors&quot;&gt;High Contrast Colors&lt;/h2&gt;

&lt;p&gt;Choose colors in your presentation that have high contrast between each other. I don’t care if it’s just black &amp;amp; white, but at least I will understand your slides if they have a white background with black letters.&lt;/p&gt;

&lt;p&gt;Also keep in mind that when you present code, the dark-i-am-a-hacker color scheme of your editor might not be visible at all during your talk since you won’t be presenting using your computer monitor. Projectors vary in quality and luminosity across conferences so you have to be prepared for that. I always choose dark colors for my fonts over a white background for my code examples.&lt;/p&gt;

&lt;h2 id=&quot;output-adapters&quot;&gt;Output Adapters&lt;/h2&gt;

&lt;p&gt;Speaking of projectors, if you present with a Mac keep in mind that some conferences don’t have the fancy Mac to VGA adapter, so it’s a good idea to buy your own. Tag it with your name and always keep it in your bag; you’ll thank me later.&lt;/p&gt;

&lt;h2 id=&quot;screen-resolution&quot;&gt;Screen Resolution&lt;/h2&gt;

&lt;p&gt;You just bought the latest Macbook Pro with the super cool retina display and that’s fine, but I have news for you, most projectors out there won’t support such high resolutions. While that’s pretty obvious keep it in mind when designing your slides. What’s even more important, if you are going to perform a live demo keep in mind that the window size at which you are used to see your desktop, terminal window and text editor will change. That means that the code example that used to fit in one screen won’t fit anymore. You will have to scroll back and forth across your code example in order to demo it to your audience. Most probably you won’t have rehearsed for that, so be ready for it as well.&lt;/p&gt;

&lt;h2 id=&quot;font-size&quot;&gt;Font Size&lt;/h2&gt;

&lt;p&gt;This one is so obvious that it hurts, but yes. Most of the time we speakers have font-sizes that are not readable from the back row. The solution is easier: use bigger font sizes. –Thanks Captain Obvious.&lt;/p&gt;

&lt;h2 id=&quot;looking-at-the-whole-audience&quot;&gt;Looking at the whole audience&lt;/h2&gt;

&lt;p&gt;Always try to look at the whole audience, don’t give the talk to that one guy that for some reason catches your attention. Try to explain your ideas to the whole crowd. This will help to keep them involved.&lt;/p&gt;

&lt;h2 id=&quot;dont-depend-on-internet-connection&quot;&gt;Don’t depend on Internet Connection&lt;/h2&gt;

&lt;p&gt;That’s pretty obvious but keep in mind that internet connection won’t be reliable at the event. It might be reliable depending on the conference, but don’t bet on that.&lt;/p&gt;

&lt;h2 id=&quot;learn-to-fail&quot;&gt;Learn to fail&lt;/h2&gt;

&lt;p&gt;If you are performing a live demo and then for some reason the app you are demoing fails, don’t keep trying and trying again and again to see if this time it works. Learn to move on and have a plan B. As simple as that. If you keep trying you’re showing that you don’t understand how computers work. Running the same command that is failing should keep failing. If it doesn’t fail again under the same conditions then your program has a bug.&lt;/p&gt;

&lt;h2 id=&quot;dont-count-with-your-own-laptop&quot;&gt;Don’t count with your own laptop&lt;/h2&gt;

&lt;p&gt;Losing your laptop could mean a tragedy for your future talk. Now let’s assume that doesn’t happen, that your laptop is still OK but for some reason you can’t use it during your talk. You have to be ready for that. In the last conference I attended we had to use the event computers because they were already set up to use the session recording system, so we had to accept to use their own PCs which were running a different operating system from my machine.&lt;/p&gt;

&lt;h2 id=&quot;incorporate-content-from-other-speakers-into-your-talk&quot;&gt;Incorporate content from other speakers into your talk&lt;/h2&gt;

&lt;p&gt;In the same way a teacher links the current lesson with the previous one you could do the same in your talks. If you are not the first one talking I’m sure you can incorporate and quote other speakers from the event into your presentation. By doing so you link your talk with ideas from others showing attendees how everything is related, helping them feel more familiar with what you are presenting. You can easily say “what we see here is related to what John Doe said yesterday when he talked about XYZ” and expand a bit on that. This is not a requirement for a good talk of course, but I like it when it can be done.&lt;/p&gt;

&lt;h2 id=&quot;having-a-conclusion&quot;&gt;Having a Conclusion&lt;/h2&gt;

&lt;p&gt;Many times I’ve ended my talks and then I had a bad feeling, like there was something missing. It took me a while to realize that many of my talks lacked a Conclusion, a final wrap up of ideas for the audience. Keep in mind that you were driving the audience through slides and ideas for over 40 minutes, at the end is time to tell them &lt;em&gt;OK here is where I wanted to arrive&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;finito&quot;&gt;Finito&lt;/h2&gt;

&lt;p&gt;This is all for now, as you can see they are quite simple tips but those simple details can go a long way in improving our talks.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>The Future of php-amqplib</title>
   <link href="http://videlalvaro.github.com/2011/11/php-amqplib-future.html"/>
   <updated>2011-11-16T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2011/11/php-amqplib-future</id>
   <content type="html">&lt;h1 id=&quot;the-future-of-php-amqplib&quot;&gt;The Future of php-amqplib&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;November 16 2011&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Yesterday I moved the development of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;php-amqplib&lt;/code&gt; from here &lt;a href=&quot;https://github.com/tnc/php-amqplib&quot;&gt;https://github.com/tnc/php-amqplib&lt;/a&gt; to here &lt;a href=&quot;https://github.com/videlalvaro/php-amqplib&quot;&gt;https://github.com/videlalvaro/php-amqplib&lt;/a&gt;. This library started as a fork from the repository hosted at &lt;a href=&quot;http://code.google.com/p/php-amqplib/&quot;&gt;Google Code&lt;/a&gt; since we wanted to use the library with PHP 5.3 and back then it was raising a lot of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;E_STRICT&lt;/code&gt; errors.&lt;/p&gt;

&lt;p&gt;The code hosted at the &lt;strong&gt;tnc&lt;/strong&gt; account was used by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;The NetCircle&lt;/code&gt; in production so we decided to publish it back in Github. Since March I don’t work there anymore so I kind of stopped contributing to that library except from merging the occasional pull request with bug fixes. Now I wanted to start again working in that library, adding tests and refactoring the code structure to a more modern style: separating classes in one file per class, getting rid of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.inc&lt;/code&gt; files and possibly introducing namespaces plus other improvements. Besides the code restructure I also integrated the library with &lt;a href=&quot;http://travis-ci.org/#!/videlalvaro/php-amqplib&quot;&gt;travis-ci.org&lt;/a&gt; so it can be continuously integrated in that platform. To be able to do that I needed to be admin of that repository but since I don’t work anymore at TNC I thought the easiest way was to just fork the project &lt;em&gt;yet again&lt;/em&gt; and continue from there. This time since the forking happened in Github it’s easier to track changes.&lt;/p&gt;

&lt;h2 id=&quot;are-you-going-to-break-everything-then&quot;&gt;Are you going to break everything then&lt;/h2&gt;

&lt;p&gt;Yes and No. I tagged the current stable version of master so you can still get a safe copy from here if you want: &lt;a href=&quot;https://github.com/videlalvaro/php-amqplib/zipball/v1.0&quot;&gt;v1.0 tag&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;new-benchmarks&quot;&gt;New Benchmarks&lt;/h2&gt;

&lt;p&gt;Since people kept asking me at conferences or over &lt;em&gt;the internet™&lt;/em&gt; about the speed of the library I wrote some small benchmarks that you can try by yourself on your machine. They are very simple but they can give you a baseline to see what to expect: &lt;a href=&quot;https://github.com/videlalvaro/php-amqplib/tree/master/benchmark&quot;&gt;https://github.com/videlalvaro/php-amqplib/tree/master/benchmark&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In those benchmarks I could send/receive 1000 1kb msgs/sec on my 2010 Macbook Pro using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;RabbitMQ 2.7.0&lt;/code&gt;. Please share your results in the comments.&lt;/p&gt;

&lt;h2 id=&quot;future&quot;&gt;Future&lt;/h2&gt;

&lt;p&gt;My plans for the future are:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Improving the Class Structure: one class per file.&lt;/li&gt;
  &lt;li&gt;Start using Namespaces.&lt;/li&gt;
  &lt;li&gt;Add tests for the protocol related functions.&lt;/li&gt;
  &lt;li&gt;Improve the code readability.&lt;/li&gt;
  &lt;li&gt;Add better configuration for the AMQPConnection object.&lt;/li&gt;
  &lt;li&gt;Improve performance when possible&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Again please share your comments on what you want to see on the next versions of the library.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Scrum is just a tool</title>
   <link href="http://videlalvaro.github.com/2011/11/scrum-is-just-a-tool.html"/>
   <updated>2011-11-14T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2011/11/scrum-is-just-a-tool</id>
   <content type="html">&lt;h1 id=&quot;scrum-is-just-a-tool&quot;&gt;Scrum is just a tool&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;November 14 2011&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Today I read an article by Martin Fowler called &lt;a href=&quot;http://martinfowler.com/bliki/FlaccidScrum.html&quot;&gt;Flaccid Scrum&lt;/a&gt; that gave me a lot to think about the way I’ve seen Scrum used. He talks about a common problem in software development where the speed of a team starts to go down and down. The development process stagnates. It gets harder and harder to add new features and technical debt starts to accumulate more and more.  He then says that kind of problem is quite common with teams doing Scrum. See this quote from that article:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;For many people, this situation is exacerbated by Scrum because Scrum is process that’s centered on project management techniques and deliberately omits any technical practices, in contrast to (for example) Extreme Programming.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That single sentence shed a lot of light to help me understand why Scrum teams seems to accumulate so much technical debt. Let’s try to understand why that happens. The problem as I see it is because people put a lot on emphasis on getting Scrum right. They go repeating the Scrum procedures here and there, telling people what to do when situation XYZ arises. I hear things like:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Morning stand-ups shouldn’t take longer than 15 minutes.&lt;/li&gt;
  &lt;li&gt;You should mention what you did yesterday, what are you going to do today and what are your impediments.&lt;/li&gt;
  &lt;li&gt;You can’t add that task now since the sprint started already.&lt;/li&gt;
  &lt;li&gt;The team should pick the tasks in a shopping card manner in order to commit for the next sprint.&lt;/li&gt;
  &lt;li&gt;And so on…&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also I’ve heard Scrum maxims repeated over and over, again and again: “In scrum we do XYZ”; “According to scrum this and that”; etc., etc. That’s alright and I’m happy with Scrum and all, but there’s a problem with this kind of attitude. Scrum is a tool to manage/organize software development but, as Martin Fowler points out, there’s nothing in there that tells you how to do proper software development. And there’s where the problem starts.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;There’s so much effort put by managers on getting scrum right that they forget to put some effort too on getting software development right.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We have to change that. We have to understand that we are developers that happen to organize their activities using Scrum and not the Scrum Practitioners organizing developers. Let’s improve our coding craft while we practice Scrum or whatever methodology is in Vogue.&lt;/p&gt;

&lt;p&gt;In my case when I search for a job, I’m looking to work as a Software Developer, not as a Scrum Practitioner. I’m sorry if that doesn’t sound nice, but what I like to do is to develop software, to solve problems and to overcome programming challenges. Scrum just happens to be the tool used to organize our activities.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Command Line Editing Tips</title>
   <link href="http://videlalvaro.github.com/2011/08/command-line-editing-tips.html"/>
   <updated>2011-08-16T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2011/08/command-line-editing-tips</id>
   <content type="html">&lt;h1 id=&quot;command-line-editing-tips&quot;&gt;Command Line Editing Tips&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;August 16 2011&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Back when I was in Shanghai I saw one of our chinese colleagues to be able to work really fast on the command line. By really fast I don’t mean fast typing but fast edition of commands. He could jump around his cursor over the line and move words here and there with ease. Perhaps you are an advanced CLI user but in my case I’ve got really impressed by that. Of course I asked what was his &lt;em&gt;secret&lt;/em&gt;. &lt;strong&gt;Emacs&lt;/strong&gt; he said, plain and old Emacs.&lt;/p&gt;

&lt;p&gt;So what does Emacs has to do with all this speed? The &lt;em&gt;key&lt;/em&gt; is in the key bindings that you have to use while editing with Emacs. After I learned some of them I also found out that most applications also support them. Yes even your browser supports the Emacs key bindings. Not that I’m discovering powder but yes, for me most of this was new.&lt;/p&gt;

&lt;p&gt;In this post I want to show you how to edit the text that you entered on the CLI, how to move text around, copy paste it and so on all by using default keyboard shortcuts. Some of this keyboard shortcuts should work on your Terminal and in some of your other applications like Mail or Safari as well. Keep in mind that I’ve only tested this on my Mac using the default shell.&lt;/p&gt;

&lt;h2 id=&quot;fast-command-edition&quot;&gt;Fast command edition&lt;/h2&gt;

&lt;p&gt;If you are working on the mac Terminal the first thing you should do is set it up properly so it can understand the &lt;em&gt;Meta Key&lt;/em&gt; or &lt;em&gt;Alt&lt;/em&gt;. As you know the &lt;em&gt;Alt&lt;/em&gt; key is used in the Mac to enter special characters. In the Terminal that’s not so useful so you can convert it to be the &lt;em&gt;Meta&lt;/em&gt; key. Press &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;command+,&lt;/code&gt; or simply go to &lt;em&gt;Preferences&lt;/em&gt; and check the &lt;em&gt;Use option as meta key&lt;/em&gt; checkbox at the bottom of the &lt;em&gt;Keyboard&lt;/em&gt; tab. Once you got that you are ready to go.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/meta_key.png&quot; alt=&quot;RPC Over RabbitMQ&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As a convention the &lt;em&gt;ctrl&lt;/em&gt; key is always abbreviated &lt;em&gt;C&lt;/em&gt; and the &lt;em&gt;alt&lt;/em&gt; or &lt;em&gt;Meta&lt;/em&gt; key is always abbreviated &lt;em&gt;M&lt;/em&gt; so the combination of pressing ctrl and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;e&lt;/code&gt; together will be: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-e&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;I think the most well know Emacs shortcuts probably are those to go to the beginning or the end of the line. So say you typed the following command:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;  &lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;mv&lt;/span&gt; ./path/to/soem/file.txt ./path/to/soem/file_1.txt&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And you want to move to the begging of the line. The you can do so by pressing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-a&lt;/code&gt;. If you want to return to the end of the line just press &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-e&lt;/code&gt;. Pretty easy.&lt;/p&gt;

&lt;p&gt;Now, there’s a typo there, the folder shouldn’t be &lt;em&gt;soem&lt;/em&gt; but &lt;em&gt;some&lt;/em&gt;. How can you quickly jump to the &lt;em&gt;soem&lt;/em&gt; and edit that word? Press &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-a&lt;/code&gt; to go to the begging of the line. Then press &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M-f&lt;/code&gt; to jump &lt;strong&gt;f&lt;/strong&gt;orward from word to word. You can also do &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M-b&lt;/code&gt; to jump &lt;strong&gt;b&lt;/strong&gt;ackwards. If instead of doing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M-f&lt;/code&gt; you try &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-f&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-b&lt;/code&gt; then the cursor will move character by character but in my case I’m used to just using the arrow keys for that.&lt;/p&gt;

&lt;p&gt;If you are at the beginning of the word &lt;em&gt;soem&lt;/em&gt; you can press &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M-d&lt;/code&gt; to &lt;strong&gt;d&lt;/strong&gt;elete it and then you can type the corrected version.&lt;/p&gt;

&lt;p&gt;After you edited the line in both places you have something like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;  &lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;mv&lt;/span&gt; ./path/to/some/file.txt ./path/to/some/file_1.txt&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now we realize that we want to swap source an destination since we made a mistake there. How can we do that without typing everything again? First place the cursor at the beginning of the second argument and type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-k&lt;/code&gt; to &lt;em&gt;kill&lt;/em&gt; everything till the end of the line. You will end with something like:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;  &lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;mv&lt;/span&gt; ./path/to/soem/file.txt&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Then press &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-a&lt;/code&gt; to go to the beginning of the line followed by &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M-f&lt;/code&gt; to move forward from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mv&lt;/code&gt; command and after pressing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SPACE&lt;/code&gt; type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-y&lt;/code&gt; to &lt;strong&gt;y&lt;/strong&gt;ank (paste) the text that you killed before. You should end with something like:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;  &lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;mv&lt;/span&gt; ./path/to/some/file_1.txt ./path/to/some/file.txt&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Let’s open the file to see its contents:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;  &lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;open ./path/to/some/file.txt&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Once we see the content is irrelevant and that we can delete the file we can do so by going to the previous command in the history using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-p&lt;/code&gt; for example so we see again:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;  &lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;open ./path/to/some/file.txt&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M-d&lt;/code&gt; to delete the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;open&lt;/code&gt; command, and type &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rm&lt;/code&gt; and that’s it. Pretty basic but there we go another way of using these key bindings so we don’t have to type all over again.&lt;/p&gt;

&lt;p&gt;An interesting feature is the &lt;em&gt;kill ring&lt;/em&gt; where all (well almost all) the text that you have killed before in the session is available. So after you &lt;em&gt;yank&lt;/em&gt; some text you can then press &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M-y&lt;/code&gt; to cycle through that text.&lt;/p&gt;

&lt;p&gt;What’s cool about these keyboard bindings is that if you use Emacs you already know them because they are some sort of standard in *nix software. Start for example a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mysql&lt;/code&gt; console and enter the following query:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sql&quot; data-lang=&quot;sql&quot;&gt;&lt;span class=&quot;k&quot;&gt;SELECT&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;c&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FORM&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;the_table&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;WHERE&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Try using the keyboard shortcuts to fix the mistyped &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FROM&lt;/code&gt;: It just works! It will also work with your Erlang REPL and Haskell GHCi.&lt;/p&gt;

&lt;p&gt;I know this has been working like this since ages but somehow I felt the need to document it. You can also try some of these key bindings on most native text boxes on the Mac.&lt;/p&gt;

&lt;h2 id=&quot;summary&quot;&gt;Summary&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-a&lt;/code&gt;: jumps to the beginning of the line.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-e&lt;/code&gt;: jumps to the end of the line.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M-f&lt;/code&gt;: moves forward word by word.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M-b&lt;/code&gt;: moves backwards word by word.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-k&lt;/code&gt;: kills text till the end of the line.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;C-y&lt;/code&gt;: yanks previously killed text.&lt;/li&gt;
  &lt;li&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;M-y&lt;/code&gt;: cycles through the &lt;em&gt;kill ring&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I hope this results in improved command line editing for your future CLI usage.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>RabbitMQ Chat Post Mortem</title>
   <link href="http://videlalvaro.github.com/2011/05/rabbitmq-chat-post-mortem.html"/>
   <updated>2011-05-12T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2011/05/rabbitmq-chat-post-mortem</id>
   <content type="html">&lt;h1 id=&quot;rabbitmq-chat-post-mortem&quot;&gt;RabbitMQ Chat Post Mortem&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;May 12 2011&lt;/span&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;NOTE 1: I’m preparing a post update to clarify some questions that I’m getting here and on Reddit. Also I’m creating some test scripts to try to easily reproduce the messaging bits so other people can test this scenario. So far I’m failing to kill RabbitMQ on my machine with 1000 consumers, 1 queue and 1 channel per consumer. I’ve got a loop publishing 50 bytes messages. I hope to publish this later in the afternoon.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;blockquote&gt;
  &lt;p&gt;NOTE 2: Since this blog is just a git repository on Github, you can track the changes transparently &lt;a href=&quot;https://github.com/videlalvaro/videlalvaro.github.com&quot;&gt;here&lt;/a&gt;:&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;background&quot;&gt;Background&lt;/h2&gt;

&lt;p&gt;Last week I started preparing a chapter for the &lt;a href=&quot;http://bit.ly/rabbitmq+&quot;&gt;book&lt;/a&gt; I’m writing so I had to come up with an example on how to extend &lt;a href=&quot;http://www.rabbitmq.com/&quot;&gt;RabbitMQ&lt;/a&gt;. I was looking for something fairly simple to implement that at the same time proved of value for the reader. Since recently &lt;a href=&quot;https://github.com/jbrisbin/riak-exchange&quot;&gt;some&lt;/a&gt; &lt;a href=&quot;https://github.com/squaremo/rabbitmq-lvc-plugin&quot;&gt;custom&lt;/a&gt; &lt;a href=&quot;https://github.com/jbrisbin/random-exchange&quot;&gt;exchanges&lt;/a&gt; had appeared on the mailing list, I thought that creating a custom exchange could be a nice example to implement, so I went for it.&lt;/p&gt;

&lt;p&gt;In case you don’t know what &lt;a href=&quot;http://www.rabbitmq.com/&quot;&gt;RabbitMQ&lt;/a&gt; is, I’ll explain it briefly here. RabbitMQ is a messaging server that as such you use it to pass messages around in your applications. You can send event notifications through it; enqueue tasks for background processing; collect logs from many sources and filter them; and so on. Taking all that into account, creating a Chat where RabbitMQ is used as the message router doesn’t sound so &lt;em&gt;crazy&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;For that purpose I created the &lt;a href=&quot;https://github.com/videlalvaro/rabbitmq-recent-history-exchange&quot;&gt;Recent History Exchange&lt;/a&gt;. The purpose of having such custom exchange was to provide the user that joined the chat room with a minimal context of the last 20 messages that passed through the chat room.&lt;/p&gt;

&lt;p&gt;So I sat down and started hacking, in about 4 hours I’ve got the &lt;a href=&quot;https://github.com/videlalvaro/rabbitmq-chat&quot;&gt;Chat Repository&lt;/a&gt; ready and up on Github. It was very very basic. The frontend part was based on &lt;a href=&quot;https://github.com/seancribbs/yakriak&quot;&gt;YakRiak&lt;/a&gt;, a web chat with a &lt;a href=&quot;https://github.com/basho/riak_kv&quot;&gt;Riak&lt;/a&gt; backend.&lt;/p&gt;

&lt;h2 id=&quot;the-architecture&quot;&gt;The Architecture&lt;/h2&gt;

&lt;p&gt;OK, let’s assume there’s an &lt;em&gt;architecture&lt;/em&gt; as such behind this little book example.&lt;/p&gt;

&lt;p&gt;First as some one said, I was &lt;a href=&quot;http://www.reddit.com/r/programming/comments/h6aai/hey_reddit_i_wrote_a_chat_app_using_rabbitmq_and/c1swq7w&quot;&gt;cheating&lt;/a&gt;, I wrote all this in Erlang.&lt;/p&gt;

&lt;p&gt;The HTTP bits of the chat: the HTML page, the CSS and Javascript was all served by &lt;a href=&quot;https://github.com/ostinelli/misultin&quot;&gt;Misultin&lt;/a&gt;, an Erlang web server. Also I used Misultin as a Websockets server to send the messages to the connected clients.&lt;/p&gt;

&lt;p&gt;What happened when a user connects to the chat is this:&lt;/p&gt;

&lt;p&gt;1) The server starts a Websockets connection to the user browser.&lt;/p&gt;

&lt;p&gt;2) The Websockets &lt;a href=&quot;https://github.com/videlalvaro/rabbitmq-chat/blob/master/src/rabbitmq_chat_rest.erl#L69&quot;&gt;loop&lt;/a&gt; starts a RabbitMQ consumer. This consumer takes care of creating a &lt;em&gt;private auto delete queue on the server&lt;/em&gt;. This means &lt;strong&gt;each user has its own queue&lt;/strong&gt; on the server. By making the queues &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;auto delete&lt;/code&gt;, I’ve made sure that if the users closed his chat window, then his queue will be automatically deleted by RabbitMQ.&lt;/p&gt;

&lt;p&gt;3) All queues where bound to the custom Recent History Exchange. As I said before, every user that connected to the chat got the last 20 messages thanks to this exchange. This exchange acted like a single chat room where every user was logged in.&lt;/p&gt;

&lt;p&gt;4) Every time a user sent a message to the server, that message got &lt;strong&gt;fanout’ed to every queue bound to the exchange&lt;/strong&gt;. Depending on the amount of queues created on the server, this pattern of message distribution can get very very slow. When the chat got &lt;strong&gt;500~ users online&lt;/strong&gt; and &lt;strong&gt;10~&lt;/strong&gt; messages published per second, that meant the server was routing each of those ten messages to each of the &lt;strong&gt;500~ queues&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;5) Once the broker delivered the message to the private queue, the consumer took care of sending it back to the Websocket process that started it, and from there it was sent to the browser. The messages where transient, kept in memory by the broker while they were getting delivered to the consumers.&lt;/p&gt;

&lt;p&gt;And that was basically it.&lt;/p&gt;

&lt;p&gt;Some people raised valid complains about the chat interface having some CSS problems. Also I was too lazy to implement a rooster kind of feature. The goal of the chat was to test the proof of concept that was the Recent History Exchange. Just that, nothing more. It wasn’t meant to become a full blown chat. Perhaps in the future I could try to build a more interesting chat, but not in this case.&lt;/p&gt;

&lt;p&gt;Also keep in mind that this &lt;strong&gt;was not made to compete with other solutions&lt;/strong&gt; like the &lt;a href=&quot;http://www.ape-project.org/wiki/index.php/Setup&quot;&gt;APE server&lt;/a&gt; or &lt;a href=&quot;http://nodejs.org/&quot;&gt;Node JS&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;the-setup&quot;&gt;The Setup&lt;/h2&gt;

&lt;p&gt;To deploy the server I’ve got a &lt;strong&gt;vps200&lt;/strong&gt; from &lt;a href=&quot;http://www.servergrove.com/vps&quot;&gt;Servergrove&lt;/a&gt;: 1 GB of RAM and 2 cores. I asked for a bare bones installation of Ubuntu 10.04 on it. The account set up was really easy and their admin panel is pretty slick.&lt;/p&gt;

&lt;p&gt;Once I’ve got the login details I ssh’ed into the server and compiled Erlang from source. From there it was a matter of &lt;a href=&quot;http://vimeo.com/10254034&quot;&gt;20 seconds to get RabbitMQ up and running&lt;/a&gt;. I used the latest &lt;a href=&quot;http://www.rabbitmq.com/releases/rabbitmq-server/v2.4.1/rabbitmq-server-generic-unix-2.4.1.tar.gz&quot;&gt;Generic Unix Distribution&lt;/a&gt; to get it installed.&lt;/p&gt;

&lt;p&gt;After I’ve got the server installed, I’ve added the Recent History Exchange and the &lt;a href=&quot;http://www.rabbitmq.com/plugins.html#rabbitmq-management&quot;&gt;Management Plugin&lt;/a&gt;. The latter so I could easily monitor the status of the server directly from the web.&lt;/p&gt;

&lt;h2 id=&quot;deployment-method&quot;&gt;Deployment Method&lt;/h2&gt;

&lt;p&gt;To deploy the code I used Github to get the source code for the application. Whenever there was a bug, or a new feature I wanted to add, like logging, what I did was to develop in my machine, pushed to Github, pulled on the server, compiled and restarted the Chat.&lt;/p&gt;

&lt;h2 id=&quot;going-live&quot;&gt;Going Live&lt;/h2&gt;

&lt;p&gt;To get the Chat up and running I didn’t do anything special. Erlang compiled with default options. RabbitMQ started with default options. Nothing fancy. There was no clustering set up or any kind of failover. As you see, this was a pretty minimalistic setup. No load balancers, no heartbeats to check system health, etc.&lt;/p&gt;

&lt;p&gt;Once I’ve got the chat up and running. I’ve showed it to some friends on Twitter to be sure it wasn’t broken. The next day I decided to ask &lt;a href=&quot;http://www.reddit.com/r/programming/comments/h6aai/hey_reddit_i_wrote_a_chat_app_using_rabbitmq_and/&quot;&gt;Reddit&lt;/a&gt; to try to take down the chat. There the fun started.&lt;/p&gt;

&lt;p&gt;Then Reddit users started to do some nasty things against the poor ol’ little server. Some guy ran Apache Benchmark against it. Here are the &lt;a href=&quot;http://pastebin.com/ecVX7xtv&quot;&gt;results&lt;/a&gt;: 100000 requests in 43~ seconds. I would say that’s not bad for such a simple and &lt;em&gt;smallish&lt;/em&gt; setup.&lt;/p&gt;

&lt;p&gt;Not happy with that another developer –&lt;a href=&quot;http://twitter.com/#!/danopia&quot;&gt;@danopia&lt;/a&gt;– created some Ruby &lt;a href=&quot;https://gist.github.com/5c4769f21486a3c34d6a/442139759a20ed79ec2f9d805de156f2d2216cec&quot;&gt;gists&lt;/a&gt; to act as bots that were constantly posting messages to chat.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://twitter.com/#!/michaelklishin&quot;&gt;@michaelklishin&lt;/a&gt; further developed the concept and created some bots that opened hundreds of idle websockets connections to the server. They weren’t sending messages, but they triggered the creation of their own queues on the server which added more workload to RabbitMQ. All these scripts now have their own &lt;a href=&quot;https://github.com/michaelklishin/rmq-chat-load-testing-scripts&quot;&gt;Github repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Besides from all the obscenity that you can get when you leave the internet without moderation, some funny messages passed around, like:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;whats rabbitmq anyway, some weird language bindings to zmq?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It took more than &lt;strong&gt;350&lt;/strong&gt; users and &lt;strong&gt;2000&lt;/strong&gt;+ messages/second to exhaust my the little VPS memory to crash the server. This can be prevented of course, as we will discuss later. In this case there was no flow control… no throttle, no nothing… just the internet free to take down a poor web chat baby. So what happened?&lt;/p&gt;

&lt;h2 id=&quot;hardening-your-crash-proof-bunny&quot;&gt;Hardening your Crash Proof Bunny&lt;/h2&gt;

&lt;p&gt;On detail that we have to keep into account when running RabbitMQ is that it is actually running on top of the Erlang Virtual Machine. This means that the issues that can make Erlang crash, can also crash RabbitMQ, or for that matter any system that you develop with Erlang.&lt;/p&gt;

&lt;p&gt;In this particular case, after I took a look at the Erlang crash dump, I could find that the server somehow ran out of memory. What the Erlang Virtual Machine does when it runs out of memory is to exit the process. As simple as that.&lt;/p&gt;

&lt;p&gt;A particular setting for RabbitMQ that can help in this case is the &lt;strong&gt;memory_high_watermark&lt;/strong&gt; parameter that you can specify in your RabbitMQ configuration file. &lt;a href=&quot;http://twitter.com/#!/michaelklishin&quot;&gt;@michaelklishin&lt;/a&gt; suggested me to change it. The server was using &lt;strong&gt;400MB&lt;/strong&gt; top, that is about a 40% of the available memory that Erlang can detect from the machine. I changed that value to 80% so the new settings allowed the server to go up to &lt;strong&gt;800MB&lt;/strong&gt; if needed.&lt;/p&gt;

&lt;p&gt;As I said before, the chat server didn’t implemented any kind of throttling. So as long as the websockets connection could get opened, then a new AMPQ Channel and a new Queue would be declared for that user. For the purposes of a proof of concept project or a prototype like this web chat it might be OK to run a system like in this way but if you do some serious production deployment of a RabbitMQ system, you will look at ways of preventing the server from getting flowed by connections, messages and hyper active producers.&lt;/p&gt;

&lt;p&gt;There is much more you can do here to optimize your RabbitMQ server, like configure the number of Erlang Processes the server can spawn for example. You can see the description of the arguments accepted by the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;erl&lt;/code&gt; command &lt;a href=&quot;http://erlang.org/doc/man/erl.html&quot;&gt;here&lt;/a&gt; and the RabbitMQ configuration details &lt;a href=&quot;http://www.rabbitmq.com/configure.html&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;running-smoothly&quot;&gt;Running Smoothly&lt;/h2&gt;

&lt;p&gt;From that point it kept running pretty smoothly and I went to sleep when there were &lt;strong&gt;530~&lt;/strong&gt; users online. How did I knew that… pretty simple. I used a simple shell command to count the number of private queues on the broker:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;k&quot;&gt;while &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; ./rabbitmqctl list_queues | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;amq. | &lt;span class=&quot;nb&quot;&gt;wc&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;uptime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;sleep &lt;/span&gt;5s&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;done&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;It uses the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;rabbitmqctl&lt;/code&gt; script to list the queues, it &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;greps&lt;/code&gt; the queues whose name starts with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;amq.&lt;/code&gt; and pipes that to the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;wc&lt;/code&gt; command to get the number of lines. As simple as it can get. Poor man stats.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/5000_msgs.png&quot; alt=&quot;RabbitMQ routing 5000~ msgs/sec&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As you can see there, the server was routing more than &lt;strong&gt;5000 messages&lt;/strong&gt; per second using &lt;strong&gt;86MB of RAM&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;As the time passed I started to get tired since it was around 4:00AM, Sunday morning. I took some screenshots of the current broker status, made a small screen cast out of what was happening and went to bed. &lt;a href=&quot;http://vimeo.com/23424752&quot;&gt;This is the video showcasing the server&lt;/a&gt; &lt;em&gt;load average&lt;/em&gt;, number of routed messages, channels opened to the broker and queues.&lt;/p&gt;

&lt;p&gt;The load average was pretty low. The same can be said of the memory used by the broker. It seems I was really cheating by using Erlang :D&lt;/p&gt;

&lt;p&gt;After a day I took a look at google analytics, the response from Reddit was quite impressive:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/analytics.png&quot; alt=&quot;More than 7000~ visits in one day&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The chat got more than 7000 visits in one day. You can see that nearly a thousand of visits happened between 0:00 and 1:00 AM.&lt;/p&gt;

&lt;p&gt;On Monday I decided to take the chat offline since the test was finished and people started to abuse the Gravatars, impersonating other persons. The chat didn’t have any kind of authentication, it didn’t store any messages nor user information. So it was not possible to check that the people using Gravatar was legitimate.&lt;/p&gt;

&lt;h2 id=&quot;what-ive-learned&quot;&gt;What I’ve learned&lt;/h2&gt;

&lt;p&gt;Apart that I’ve used RabbitMQ in production before, this scenario showed again that is a &lt;strong&gt;pretty robust software&lt;/strong&gt;. Used with the default settings it crashed only because the memory allocated to it was reduced. It’s worth noting here that is the Erlang Virtual Machine what crashed when it runs out of memory.&lt;/p&gt;

&lt;p&gt;Another point is that Erlang has some serious tools to keep a system alive. Apart from all the cool OTP behaviors like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;supervisors&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;gen_servers&lt;/code&gt;, you get for free tools like:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Standard report logger&lt;/li&gt;
  &lt;li&gt;Report browser, with filters by date, regex. etc. That makes it simple to browse through logs.&lt;/li&gt;
  &lt;li&gt;Etop. Is like the unix &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;top&lt;/code&gt; command but for Erlang Virtual Machines.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Also it has things like and embedded database: &lt;a href=&quot;http://www.erlang.org/doc/apps/mnesia/index.html&quot;&gt;Mnesia&lt;/a&gt;. Having a database that understand the language you are building your applications with simplifies the process a lot.&lt;/p&gt;

&lt;p&gt;Then, thanks to the Erlang Distribution System, is pretty easy to hook into a running Erlang Virtual Machine and send messages to the running processes there. It’s also possible to perform RPC invocations. That simplifies the process of restarting some parts of the application while keeping others alive.&lt;/p&gt;

&lt;p&gt;Finally a big thanks to the reddit/programming folks for joining the &lt;em&gt;destroy the chat&lt;/em&gt; party last Saturday.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>An Army of Zombie Minions for RabbitMQ</title>
   <link href="http://videlalvaro.github.com/2011/04/an-army-of-minions-for-rabbitmq.html"/>
   <updated>2011-04-30T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2011/04/an-army-of-minions-for-rabbitmq</id>
   <content type="html">&lt;h1 id=&quot;an-army-of-zombie-minions-for-rabbitmq&quot;&gt;An Army of Zombie Minions for RabbitMQ&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;April 30 2011&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Imagine that you spawn a bunch of RabbitMQ consumers and let them in a zombie state waiting for orders to start performing some sort of work, like counting words occurrences in text files. Then from the secret chamber where you orchestrate things you will send a message to all those zombies and the will start working on what you tell them to. The sequence will be something like this:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Launch 10 consumers.&lt;/li&gt;
  &lt;li&gt;They do nothing, just wait for orders.&lt;/li&gt;
  &lt;li&gt;Send an AMQP message: “Become a count words server”.&lt;/li&gt;
  &lt;li&gt;The 10 consumers will start consuming from a queue that will feed them text via AMQP messages.&lt;/li&gt;
  &lt;li&gt;Send AMQP message: “Become a reverse words server”.&lt;/li&gt;
  &lt;li&gt;The 10 consumers will start consuming from a queue that will feed them text to reverse via AMQP messages.&lt;/li&gt;
  &lt;li&gt;Laugh like a mad scientist (rainy night with thunderstorm required).&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;To achieve such scenario using RabbitMQ we will need the following:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;A topic exchange called &lt;em&gt;control&lt;/em&gt;.&lt;/li&gt;
  &lt;li&gt;A routing key to identify a group of consumers/zombies.&lt;/li&gt;
  &lt;li&gt;An exchange where we can send the &lt;em&gt;word count&lt;/em&gt; or &lt;em&gt;word reverse&lt;/em&gt;  messages.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We will be using the &lt;a href=&quot;https://github.com/videlalvaro/rmq_patterns&quot;&gt;rmq_patterns&lt;/a&gt; library for the examples, so you can go and get the code while we continue.&lt;/p&gt;

&lt;p&gt;By using that library we can fire up the Erlang REPL and start getting the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;exchanges&lt;/code&gt; we need declared on the server:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;sc&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rmq_patterns&lt;/span&gt;
&lt;span class=&quot;sc&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sh&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;misc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;declare_exchanges&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([{&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;control&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;topic&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;my_exchange&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;direct&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}]).&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;There we declare two exchanges, one called &lt;em&gt;control&lt;/em&gt; that we will use as part of our &lt;a href=&quot;http://www.eaipatterns.com/ControlBus.html&quot;&gt;control bus&lt;/a&gt;. The other will be used to send messages to our zombies once they are &lt;em&gt;awake&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The line that says &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;./start-dev.sh 1&lt;/code&gt; will just get us an Erlang REPL up and running. The number &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;1&lt;/code&gt; there as an argument can be anything that can &lt;em&gt;unique-identify&lt;/em&gt; our Erlang node in a cluster of Erlang nodes.&lt;/p&gt;

&lt;p&gt;The next step is to start our zombie consumer.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;amqp_consumer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start_demo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;control&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;word.zombies&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;What we do there is to start our consumer and tell it that the it will receive orders from the &lt;strong&gt;control&lt;/strong&gt; exchange and it will be addressed as part of the &lt;strong&gt;word.zombies&lt;/strong&gt; group. This all sounds to magical? Ok here’s how this work: We will have an exchange called &lt;strong&gt;control&lt;/strong&gt; where we will send our &lt;strong&gt;control&lt;/strong&gt; messages. Messages like “Start consuming from queue xyz using callback zyx”, or “Stop consuming messages”, and so on. The group identifier for consumers is just an AMQP &lt;strong&gt;routing key&lt;/strong&gt; that we will use to address a group of consumers. So when we start our consumers using routing key like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;word.zombies&lt;/code&gt; what we are saying is “this consumer is part of the group &lt;em&gt;word.zombies&lt;/em&gt;. As a final note &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;amqp_consumer:start_demo/2&lt;/code&gt; is just a helper function to start the consumer using some defaults. You can peek inside it and adapt it to your own needs, like different connection parameters and so on.&lt;/p&gt;

&lt;p&gt;Now that we got our consumer in zombie mode, let’s start our &lt;strong&gt;control&lt;/strong&gt; interface in another Terminal window.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;sc&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cd&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;path&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rmq_patterns&lt;/span&gt;
&lt;span class=&quot;sc&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;start&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dev&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sh&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;
&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;amqp_control_bus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start_demo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;Server&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;started&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;with&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;53&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;53&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;As we said before, we will use the &lt;strong&gt;control&lt;/strong&gt; process to send messages to a group of zombies and then make them start working for us. What we want to do is to send messages containing sentences to an exchange called &lt;strong&gt;my_exchange&lt;/strong&gt; and see how many words they had. Something pretty simple for the purposes of this demo. So let’s send a message with an English sentence to see how many words it has. Let’s do this in the same console where we are running our &lt;strong&gt;control&lt;/strong&gt; process.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;amqp_utils&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;send_msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;my_exchange&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;I can't explain myself, I'm afraid, Sir, because I'm not myself you see.&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;consumer.key&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Oops! nothing happened… And that’s correct, we haven’t told our zombies that they have to start listening on exchange &lt;strong&gt;my_exchange&lt;/strong&gt; to start getting messages. So let’s get that fixed and raise our army!.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;amqp_control_bus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;consumer_msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;word.zombies&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;my_exchange&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;consumer.key&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;misc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word_count_callback&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}).&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now let’s try to send a message to the zombie consumer again&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;amqp_utils&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;send_msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;my_exchange&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;I can't explain myself, I'm afraid, Sir, because I'm not myself you see.&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;consumer.key&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;If everything went well, on the REPL where we were running the Zombie consumer we should see this output: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Count: 13&lt;/code&gt;, that is, the sentence we sent over RabbitMQ had 13 words on it –counting them as separated by single spaces–. Try with different sentences to see what happens.&lt;/p&gt;

&lt;p&gt;Let’s explain what we just did. First we sent a message to our group of Zombies telling them to start listening to messages published on the &lt;strong&gt;my_exchange&lt;/strong&gt; exchange using the &lt;strong&gt;consumer.key&lt;/strong&gt; routing key (You can specify whatever key you want as long as you publish your messages using that routing key). The last argument element of our tuple is the Erlang callback that we want to be executed by our Zombies. In this case it will be the function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;misc:word_count_callback/2&lt;/code&gt;. The first argument that this function takes is a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Channel&lt;/code&gt; connected to RabbitMQ. The second one is the AMQP &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Message&lt;/code&gt; that we got from the server.&lt;/p&gt;

&lt;p&gt;Now, say we got bored of counting words. Let’s reverse them. To do that we will tell our zombies to start using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;misc:word_reverse_callback/2&lt;/code&gt; function. Let’s do it. Switch to the window with the control REPL and type:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;amqp_control_bus&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;consumer_msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;Pid&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;word.zombies&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;my_exchange&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;consumer.key&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;misc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;word_reverse_callback&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;/&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}).&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And then let’s send a message to see if this worked:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;amqp_utils&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;send_msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;my_exchange&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;I can't explain myself, I'm afraid, Sir, because I'm not myself you see.&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;consumer.key&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;ok&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Check the Terminal window running your Zombie consumer, you should see some output like:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-erlang&quot; data-lang=&quot;erlang&quot;&gt;&lt;span class=&quot;nv&quot;&gt;Reversed&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;see.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;you&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;myself&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;not&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;I'm&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;because&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Sir,&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;afraid,&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
                 &lt;span class=&quot;s&quot;&gt;&quot;I'm&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;myself,&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;explain&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;can't&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;I&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Isn’t this cool? We just modified the behavior of our consumer without even restarting it!&lt;/p&gt;

&lt;p&gt;Imagine modifying the behavior of your RabbitMQ workers on the fly, without ever stopping them? Or sending them configuration messages like: “Hey the MySQL database on server ABC just went down, so you’d better start performing inserts on server CBA” and so on.&lt;/p&gt;

&lt;p&gt;I dared to call this technique as &lt;strong&gt;“Control Enabled Messaging Endpoints.”&lt;/strong&gt; Normally we create messaging endpoints, AKA consumers/producers, that publish/consume messages sent between them. In this case we augment them by making them listen to the Control Messages that are published on a separate Exchange. If you are interested in messaging, take a look at the &lt;a href=&quot;http://www.eaipatterns.com/&quot;&gt;Enterprise Integration Patterns&lt;/a&gt; book, which explain these patterns in great detail. A nice example of this technique and where I also took inspiration from is Chapter 16 from the book &lt;a href=&quot;http://pragprog.com/titles/jaerlang/programming-erlang&quot;&gt;Programming Erlang&lt;/a&gt; by Joe Armstrong.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Learn You Some Refactoring For Great Good</title>
   <link href="http://videlalvaro.github.com/2011/04/learn-you-some-refactoring-for-great-good.html"/>
   <updated>2011-04-22T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2011/04/learn-you-some-refactoring-for-great-good</id>
   <content type="html">&lt;h1 id=&quot;learn-you-some-refactoring-for-great-good&quot;&gt;Learn You Some Refactoring For Great Good&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;April 22 2011&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;The last few days I had to refactor a class of 3600+ LoC… I don’t want to go into the details of how painful is to accomplish such thing with a language like PHP that lacks a type system ala Haskell. What I would like to talk about are some coding &lt;em&gt;practices&lt;/em&gt; I’ve found that I thought they where part of the past.&lt;/p&gt;

&lt;h2 id=&quot;objects-with-a-purpose&quot;&gt;Objects with a purpose&lt;/h2&gt;

&lt;p&gt;Objects shouldn’t be modeled like swiss army knives. Period. They need to have a specific purpose, and that purpose is what the class name should convey.&lt;/p&gt;

&lt;p&gt;Imagine you have the following class:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Logger&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;c1&quot;&gt;// omitted code&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// omitted code&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sendMail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$from&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// omitted code&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;c1&quot;&gt;// omitted code&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now can you tell me why on earth a class called &lt;em&gt;Logger&lt;/em&gt; is sending emails? Can you? I can’t? The very name &lt;em&gt;Logger&lt;/em&gt; tells you what this class should do and guess what &lt;em&gt;that&lt;/em&gt; is, &lt;em&gt;log&lt;/em&gt; messages. We can argue to see if this class should manage the file handle were to write the logs, or if it should be provided to it in some form of composite. We can argue if this class should implement formatter functions for the logs or if it should allow several formatters injected as composites. Still, I don’t think this class should be sending emails.&lt;/p&gt;

&lt;p&gt;Code examples like this one can be found everywhere. I’ve found myself many times adding methods to classes that had nothing to do with what the method was doing.&lt;/p&gt;

&lt;h2 id=&quot;conditionals-and-state&quot;&gt;Conditionals and State&lt;/h2&gt;

&lt;p&gt;Another piece of code that smells bad are boolean conditions that depend on the object internal state. And I’m not referring here to the object reacting differently to the status of a database handle for example. So you would have code like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;execute&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$sql&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dbHandle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;//do something with the dbHandle and the SQL query&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;I don’t have problem with such code. My problem comes when some method behavior depends entirely on the state of some object flags that can be changed from anywhere in the inheritance chain:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SomeClass&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;someMethod&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$foo&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$bar&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;quux&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;baz&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'key'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;else&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
      &lt;span class=&quot;c1&quot;&gt;//&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Why do I have to be tracking down what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$this-&amp;gt;quux&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$this-&amp;gt;baz['key']&lt;/code&gt; could be at run time to see what this function is going to do? Also imagine that the method &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;someMethod&lt;/code&gt; was our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sendMail&lt;/code&gt; method from the example above and you would like to refactor it out to a new class that you want to use as a composite. You will have to see what &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$this-&amp;gt;quux&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$this-&amp;gt;baz['key']&lt;/code&gt; mean for the entirety of the code that uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;SomeClass&lt;/code&gt; to be able to refactor it. That’s not nice. That’s not easy to do, and is pretty hard to get it right. Even more with a language like PHP that is dynamically typed.&lt;/p&gt;

&lt;p&gt;Take a look at &lt;a href=&quot;http://bit.ly/fuzaUw&quot;&gt;Referential Transparency&lt;/a&gt;. If you want your code to be &lt;em&gt;unit-testable&lt;/em&gt;, then separating your stateful computations from those that don’t depend on state (AKA pure code) will really help you to achieve such goal.&lt;/p&gt;

&lt;p&gt;As it is said many times, you don’t need to be coding in Haskell to use its concepts in your daily language.&lt;/p&gt;

&lt;h2 id=&quot;lack-of-types-doesnt-mean-a-duck-is-a-chicken&quot;&gt;Lack of Types doesn’t mean a duck is a chicken&lt;/h2&gt;

&lt;p&gt;Talking about PHP being dynamically typed it seems that sometime people forget what types are or what they mean semantically. See this code:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;OtherClass&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__construct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nv&quot;&gt;$dbHandle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$logger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dbHandle&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$dbHandle&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;$this&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;logger&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;$logger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;So far nothing fancy, but…&lt;/p&gt;

&lt;p&gt;I saw some code like that being called in this way:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-php&quot; data-lang=&quot;php&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$v&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;OtherClass&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;false&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Logger&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;());&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The problem with such code is that the absence of a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$dbHandle&lt;/code&gt; is being represented with the &lt;em&gt;boolean&lt;/em&gt; &lt;em&gt;false&lt;/em&gt;. Booleans purpose, whether your language is statically typed or not, is to represent values that have some sense of “truthiness” on them i.e.: True &amp;amp; False –ORLY?–.&lt;/p&gt;

&lt;p&gt;So when people go so crazy about semantic HTML, regarding the use of proper tags for the proper markup, I wonder why they also don’t go so crazy about using the proper types for the proper values. In the case of PHP I would have used &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt; to represent the absence of a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;$dbHandle&lt;/code&gt;, but not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt;. SQL has this distinction too with the NULL value and the checks for &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;IS NULL&lt;/code&gt; for example.&lt;/p&gt;

&lt;p&gt;Why does all this matters? Because when I read such code and I see a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;false&lt;/code&gt; as first argument I’m left wondering what happens if I pass &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt; there. After inspecting the code I know what happens… the code will simply blow up. See the example on top where I do &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;if($this-&amp;gt;dbHandle)&lt;/code&gt;. The code will try to execute a query on a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;true&lt;/code&gt; value. It’s a very small detail, but a detail that improves code readability and intent.&lt;/p&gt;

&lt;h2 id=&quot;exceptions-or-null-or-booleans-or&quot;&gt;Exceptions or Null or Booleans or…&lt;/h2&gt;

&lt;p&gt;What happens when a &lt;em&gt;C&lt;/em&gt; style developer learns &lt;em&gt;Java&lt;/em&gt;? You get a function that returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;booleans&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nulls&lt;/code&gt; and also throws exceptions to signal failures. This kind of APIs really drive me crazy. I can never know what I will get back when calling such functions. One of the principles of good encapsulation is that as a user of an API I shouldn’t care about what’s happening inside functions.&lt;/p&gt;

&lt;h2 id=&quot;what-can-we-do&quot;&gt;What can we do?&lt;/h2&gt;

&lt;p&gt;Something that has really helped me to improve the way I code is Functional Programming. Sadly when I talked to peers about it I often see that people think of Functional Programming as some academic exercise even despite so &lt;a href=&quot;http://www.haskell.org/haskellwiki/Haskell_in_industry&quot;&gt;many&lt;/a&gt; &lt;a href=&quot;http://www.ejabberd.im/&quot;&gt;examples&lt;/a&gt; of &lt;a href=&quot;http://www.franz.com/success/customer_apps/animation_graphics/nichimen.lhtml&quot;&gt;successful&lt;/a&gt; &lt;a href=&quot;http://rabbitmq.com/&quot;&gt;projects&lt;/a&gt; &lt;a href=&quot;http://www.basho.com/products_riak_overview.php&quot;&gt;written&lt;/a&gt; using Functional Languages.&lt;/p&gt;

&lt;p&gt;If you practice Functional Programming in a language like Haskell that forces you to separate pure code from the impure one you will get used on writing functions that depend entirely on the parameters they accept. And that’s just the beginning of it.&lt;/p&gt;

&lt;p&gt;So the advice you didn’t ask for is: learn Functional Programming. You can start &lt;a href=&quot;http://learnyouahaskell.com/&quot;&gt;here&lt;/a&gt;&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Reply to "Scala is Easier than PHP"</title>
   <link href="http://videlalvaro.github.com/2010/11/reply-to-scala-is-easier-than-php.html"/>
   <updated>2010-11-20T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2010/11/reply-to-scala-is-easier-than-php</id>
   <content type="html">&lt;h1 id=&quot;reply-to-scala-is-easier-than-php&quot;&gt;Reply to “Scala is Easier than PHP”&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;November 20 2010&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;This week I came across the blog post by Wade Arnold &lt;a href=&quot;http://wadearnold.com/blog/scala/scala-is-easier-than-php&quot;&gt;Scala is Easier than PHP&lt;/a&gt; and while I agree with most of it, there are some things where I don’t. Before going on with the points, let me state something: please avoid flame wars, all the Scala vs. PHP stuff, fanboyism and what not. This post is not about that. Regarding Wade Arnold I have to say that I fully respect him. While I don’t know him personally, I know him for his work on AMFPHP, since it was a platform I used to work with before. So, all things considered lets begin…&lt;/p&gt;

&lt;p&gt;In his blog he explains some of the advantages of using a functional programming language like Scala over PHP. At some point he writes:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;As we continue to add more screens, more protocols, and more data aggregation services, and frankly more data we need to be able use all cores in the server and the cluster. As Guy Steele implied at Strange Loop. The single processor programming model is going the way of the punch card.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;What I understand from that paragraph is that if we want to do multicore programming, then we have to use something else, not PHP, to get the full advantage of today’s CPUs. He then goes an suggests Scala as the solution. I’ve done something similar in the past, suggesting Erlang as the super cool functional programming language that will relieve all your multicore programming pains. Back in June this year I realized how wrong I was.&lt;/p&gt;

&lt;p&gt;I was at the IPC Spring Edition conference in Berlin presenting my talk “Integrating Erlang with PHP”. I started by talking about all the wonders that Erlang promises such as:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Code reload without stoping the server.&lt;/li&gt;
  &lt;li&gt;Being ready for multicore programming.&lt;/li&gt;
  &lt;li&gt;No shared state.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When the audience looked at me like: “hey PHP already does that”, I realized how much Kool Aid I had drunk. Every PHP developer knows that to see the changes in your code reflected on the webpage you just have to hit F5 on your browsers and “ta da!”. Your new shinny whatever will be there –Well, if you use APC you’ll probably have to clear the APC cache, but that’s about it–. So “what are you talking about with this ‘code reload’ thing”.&lt;/p&gt;

&lt;p&gt;Regarding &lt;em&gt;running on all the cores&lt;/em&gt;… well, normally you spawn many PHP threads to process the web requests, so you’ll take advantage of your server many CPUs. We can scream that PHP is ready for multicore! –put down them torches and	 keep reading please ;)–.&lt;/p&gt;

&lt;p&gt;What about &lt;em&gt;shared state&lt;/em&gt;? When you call a PHP script it will load into memory all the configuration and things that make up the process state –extensions for example–, will process the request and then will cleanup and shutdown. Of course there’s shared state in the programming model of PHP, but each PHP script running on the server has no idea that there are many –probably dozens– of other PHP scripts serving requests at the same time.&lt;/p&gt;

&lt;p&gt;This seems that we are comparing apples to some weird asian fruit that I don’t even know the name but I know that it smells bad… and that raises the question of “ok do I need Scala, Erlang or [cool FP lang here]?” I’d venture to say yes, and here I’ll negate all what I said before, bear with me.&lt;/p&gt;

&lt;p&gt;I talked about code reload without stoping the server kind of worked by default with PHP. The thing is PHP is no server, PHP &lt;em&gt;is&lt;/em&gt; served :). So your PHP script code &lt;em&gt;is reloaded&lt;/em&gt; with every execution –unless you use APC– therefore updating the results of its executing right after you modifying the script, so comparing this to Erlang’s way is –you guessed right– cheating. Now say you want to do a long running process with PHP and introduce changes latter, then yes, you’ll have to restart that script. As you may guess, Erlang has an advantage here. Of course the average PHP user that is using the language for creating web pages won’t care.&lt;/p&gt;

&lt;p&gt;If you read what I wrote before it looks like I went nuts, that I know nothing about PHP, less about Erlang and Scala, because PHP is not ready for multicore! “Wait a minute, before you said that it was ready”. Well the way that most people run PHP, spawning many threads, will use several cores,  but each single PHP script will not. That seems pretty obvious and it is. So if you have a PHP script that gets a message from a queue and then executes a callback on that message, it would be very nice to be able to execute the callback in parallel while you keep getting messages from the queue and calling other callbacks. Sorry amigo, it may sound nice, but you can’t do that with PHP’s execution model. You could accomplish that in languages like Erlang for example.&lt;/p&gt;

&lt;p&gt;Now back to &lt;em&gt;no shared state&lt;/em&gt;. On the PHP world, this gives you a big advantage. Say some PHP file is faulty, or some database went down, and then you get exceptions thrown all over the place… Well, the users of your website that are accessing other parts of the site that has no DB access will still see what they want. That’s pretty similar with Erlang’s philosophy when one process dies and then all the others continue working with no problems. The bad thing with PHP is that there’s no &lt;em&gt;easy&lt;/em&gt; way for those processes to talk to each other –of course you can stretch some boundaries and make that communication happen, but by that point you should be using something else–. Erlang on the other side has some pretty impressive interprocess communication ways. In fact all its programming model is based around message passing. Once you got used to it, you’ll do many nasty things to get that in your language.&lt;/p&gt;

&lt;p&gt;In conclusion if you want to do some long running process, like some kind of server, then of course Scala and Erlang both offer many advantages over PHP, and being able to use all your CPUs is an important one. I believe Wade Arnold blog post was referring to such use cases. But if you just want to generate web pages with PHP, then get some PHP-FPM goodness and you’ll get all your CPUs doing the work.&lt;/p&gt;

&lt;p&gt;Another interesting point that I made from his blog post and from my mistakes back in the conference is that if we want to &lt;em&gt;sell&lt;/em&gt; functional languages like Erlang or Scala to the PHP programmer then we have to look for more compelling features that may attract them to look into these languages. What I think are those features –I guess–, should be part of another blog post.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Speaking at Zendcon - RabbitMQ and PHP</title>
   <link href="http://videlalvaro.github.com/2010/10/speaking-at-zendcon.html"/>
   <updated>2010-10-25T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2010/10/speaking-at-zendcon</id>
   <content type="html">&lt;h1 id=&quot;speaking-at-zendcon---rabbitmq-and-php&quot;&gt;Speaking at Zendcon - RabbitMQ and PHP&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;October 25 2010&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Next week I’ll be speaking at Zendcon 2010 in Santa Clara, California about &lt;em&gt;Integrating RabbitMQ With PHP&lt;/em&gt;. This time the talk will be a little bit different from the one &lt;a href=&quot;http://www.slideshare.net/old_sound/integrating-rabbitmq-with-php&quot;&gt;I gave at the IPC Spring Edition&lt;/a&gt; conference in Berlin.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/zsb.jpg&quot; alt=&quot;Speaking at Zendcon&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In this future talk, besides from presenting all the greatness that RabbitMQ is, I will show many messaging/queuing patterns that can be implemented with RabbitMQ. So if you want to learn more about RabbitMQ beyond queuing I invite you to my talk that will be on November 2nd at 10:00 AM.&lt;/p&gt;

&lt;p&gt;Also I will show how easily one can build a library like &lt;a href=&quot;http://github.com/videlalvaro/Thumper&quot;&gt;Thumper&lt;/a&gt; which can reduce most of our interaction with RabbitMQ to four lines of code or less, removing away the hassle of learning the AMQ Protocol.&lt;/p&gt;

&lt;p&gt;So, hope see you there next week!&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>RPC over RabbitMQ</title>
   <link href="http://videlalvaro.github.com/2010/10/rpc-over-rabbitmq.html"/>
   <updated>2010-10-13T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2010/10/rpc-over-rabbitmq</id>
   <content type="html">&lt;h1 id=&quot;rpc-over-rabbitmq&quot;&gt;RPC over RabbitMQ&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;October 13 2010&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;In this post I would like to address a question that I see often in the RabbitMQ mailing list and IRC channel. How to reply to a client request with RabbitMQ? I think this question can be quite tricky for new users when we are not familiar with AMQP. The big deal here is to know about the options that we can use to call &lt;em&gt;exchange.declare&lt;/em&gt; and &lt;em&gt;queue.declare&lt;/em&gt;. So let’s see how to implement RPC using RabbitMQ and AMQP.&lt;/p&gt;

&lt;p&gt;For our purposes we need a server that will get the job requests from a queue. That queue must be bound to an exchange where our client will send the job requests. So far, this is what we are used to do with RabbitMQ. Then we need a &lt;em&gt;reply queue&lt;/em&gt;. A place where the responses from the server will go so they can reach our client. Since we can have many clients sending requests to the server, we can’t know in advance the name of those queues, so we will let RabbitMQ assign those names. We also want that once the client is disconnected the queue is removed to keep the server status clean –we don’t want to end with thousands of idle queues in the server–. To achieve this we can declare an anonymous queue as we did in the &lt;a href=&quot;http://videlalvaro.github.com/2010/09/haskell-and-rabbitmq.html&quot;&gt;previous&lt;/a&gt; blog post. We need to get the queue name that RabbitMQ assigned to our client and then see how to tell the server that we want the replies into that queue.&lt;/p&gt;

&lt;p&gt;This seems like a lot of work… Well, not really if we use the power of AMQP.&lt;/p&gt;

&lt;p&gt;According to the AMQP spec there’s a direct exchange with no public name to which every queue must be bound by default using the queue name as routing key. Our server has to use this exchange to reply to its clients, publishing the messages with said routing key. The next piece in our puzzle is the &lt;em&gt;reply-to&lt;/em&gt; property from AMQP messages. When our client publishes a request, it will send the queue name via the &lt;em&gt;reply-to&lt;/em&gt; property. Once the request is published it will wait and listen into its own anonymous queue. Once the RPC server gets the reply ready, it will send it to the exchange and it will finally arrive to our client. Easy…&lt;/p&gt;

&lt;p&gt;Here’s an image to further illustrate the point:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/images/RPC-OverRMQ.png&quot; alt=&quot;RPC Over RabbitMQ&quot; /&gt;&lt;/p&gt;

&lt;p&gt;So, let’s see the code now, which is written in Haskell as in the previous post. In this case we will have a server that will hold a &lt;em&gt;Map&lt;/em&gt; of users to their surnames. We will query the server for the users &lt;em&gt;surnames&lt;/em&gt;, so our request will be the user &lt;em&gt;name&lt;/em&gt; that we will input as a parameter when we invoke our client.&lt;/p&gt;

&lt;p&gt;Here’s the Code for the client:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;c1&quot;&gt;--RpcClient.hs&lt;/span&gt;

&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;qualified&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Data.ByteString.Lazy.Char8&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BL&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt;          &lt;span class=&quot;nn&quot;&gt;System.Environment&lt;/span&gt;

&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Network.AMQP&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;kr&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;getArgs&lt;/span&gt;
  
    &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;openConnection&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;127.0.0.1&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;guest&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;guest&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;openChannel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;
  
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;replyToQ&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;declareQueue&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;anonQueue&lt;/span&gt;
  
    &lt;span class=&quot;n&quot;&gt;declareExchange&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newExchange&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exchangeName&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;rpc-exchange&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exchangeType&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;direct&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;publishMsg&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;rpc-exchange&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;replyToQ&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;consumeMsgs&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;replyToQ&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Ack&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rpcCallback&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;getLine&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- wait for keypress&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;closeConnection&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;connection closed&quot;&lt;/span&gt;
    
  &lt;span class=&quot;kr&quot;&gt;where&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rk&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newMsg&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msgBody&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;BL&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
                  &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msgReplyTo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;rk&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
    
&lt;span class=&quot;n&quot;&gt;anonQueue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;QueueOpts&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;anonQueue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;QueueOpts&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;False&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;False&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;True&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;True&lt;/span&gt;
  
&lt;span class=&quot;n&quot;&gt;rpcCallback&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Envelope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;rpcCallback&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;User Surname is: &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;BL&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unpack&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msgBody&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ackEnv&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;As we did in the previous blog post, first we declare an anonymous queue. The server will assign it a random name which we will use later as the replyTo property of our message, which we pass to the RPC server when we create the &lt;em&gt;msg&lt;/em&gt; in the &lt;em&gt;where&lt;/em&gt; part. Then the code is nearly the same as in the previous example, where we declare a callback for our consumer.&lt;/p&gt;

&lt;p&gt;Next is the code for the RPC server:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;c1&quot;&gt;--RpcServer.hs&lt;/span&gt;

&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;qualified&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Data.ByteString.Lazy.Char8&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BL&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Data.Maybe&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;fromJust&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Data.Map&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;M&lt;/span&gt;

&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Network.AMQP&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;      &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;openConnection&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;127.0.0.1&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;guest&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;guest&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt;      &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;openChannel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;replyChan&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;openChannel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;--declare queues, exchanges and bindings&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;declareQueue&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newQueue&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;queueName&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;rpc-queue&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;declareExchange&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;newExchange&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;exchangeName&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;rpc-exchange&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;exchangeType&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;direct&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;bindQueue&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;rpc-queue&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;rpc-exchange&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Running RPC Server&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Press any key to close&quot;&lt;/span&gt;

    &lt;span class=&quot;c1&quot;&gt;--subscribe to the queues&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;consumeMsgs&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;rpc-queue&quot;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Ack&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;rpcServer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;replyChan&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;getLine&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- wait for keypress&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;closeConnection&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;connection closed&quot;&lt;/span&gt;
    
&lt;span class=&quot;n&quot;&gt;rpcServer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Channel&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Envelope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;rpcServer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;publishMsg&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;routingKey&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;reply&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;ackEnv&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;

  &lt;span class=&quot;kr&quot;&gt;where&lt;/span&gt;
		&lt;span class=&quot;c1&quot;&gt;-- extract routing key&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;routingKey&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;fromJust&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msgReplyTo&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;
		&lt;span class=&quot;c1&quot;&gt;-- build reply&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;reply&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;newMsg&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msgBody&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;BL&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;pack&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;findSurname&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))})&lt;/span&gt;
		&lt;span class=&quot;c1&quot;&gt;--extract user name&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;BL&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unpack&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msgBody&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;
		&lt;span class=&quot;c1&quot;&gt;-- fake users database&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;users&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;fromList&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Some&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Guy&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Another&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Dude&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;John&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Doe&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt;
		&lt;span class=&quot;c1&quot;&gt;-- lookup user surname in the Map&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;findSurname&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; 
      &lt;span class=&quot;kr&quot;&gt;case&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;M&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lookup&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;users&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;of&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;Just&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;
        &lt;span class=&quot;kt&quot;&gt;Nothing&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;None&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;First we setup the exchange and queue that we will use to receive requests. Then on the &lt;em&gt;rpcServer&lt;/em&gt; callback we consume messages extracting from them the request and the &lt;em&gt;replyTo&lt;/em&gt; property. We create a fake database to keep track of a couple of users and their surnames and we do a lookup based on the provided user name. Our callback will take care of sending the reply back to the client via RabbitMQ. You can see that the message is published to an exchange with no name using the queue name as the routing key, as we explained before.&lt;/p&gt;

&lt;p&gt;Let’s try this in the command line, first start the server and then on type the following on a Terminal window.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;runghc RpcServer.hs
Running RPC Server
Press any key to close&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Once the RPC server has started, fire up the consumer passing as parameter one of the names that we have in the database:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;runghc RpcClient.hs John
User Surname is: Doe&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And that’s it!&lt;/p&gt;

&lt;p&gt;I know this example is pretty simple, but I think is enough to illustrate how easy is to implement several kinds of messaging patterns using RabbitMQ and AMQP.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Haskell and RabbitMQ</title>
   <link href="http://videlalvaro.github.com/2010/09/haskell-and-rabbitmq.html"/>
   <updated>2010-09-09T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2010/09/haskell-and-rabbitmq</id>
   <content type="html">&lt;h1 id=&quot;haskell-and-rabbitmq&quot;&gt;Haskell and RabbitMQ&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;September 09 2010&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Since long time I wanted to try the &lt;a href=&quot;http://hackage.haskell.org/package/amqp&quot;&gt;Haskell AMQP library that’s on hackage&lt;/a&gt;, both to practice some Haskell and to learn more about RabbitMQ’s default exchanges.&lt;/p&gt;

&lt;p&gt;If you go to your &lt;em&gt;rabbitmqctl&lt;/em&gt; and try the following command:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;./rabbitmqctl list_exchanges&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Among the list of exchanges you will see a topic exchange by the name &lt;em&gt;amq.rabbitmq.log&lt;/em&gt;. As you can guess, RabbitMQ will be sending logs to that exchange. Since it’s a topic exchange, I wanted to find out which routing keys was RabbitMQ using. If we check inside &lt;em&gt;rabbit_error_logger.erl&lt;/em&gt; on RabbitMQ’s source code, we’ll find that the server is using three routing keys, depending on the log level:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;error&lt;/li&gt;
  &lt;li&gt;warning&lt;/li&gt;
  &lt;li&gt;info&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;So we could have tree queues as the destination for the log messages.&lt;/p&gt;

&lt;p&gt;In the following example I’ll try to show how to consume messages from three private queues. The idea is that we could “spy” the server events on demand, and when we stop the consumers the queues should be gone. AMQP has a neat feature to allow this kind of use case which is to declare queues with no name –the server will provide a random name–, giving the &lt;em&gt;auto_delete&lt;/em&gt; and &lt;em&gt;exclusive&lt;/em&gt; options as &lt;em&gt;true&lt;/em&gt; and &lt;em&gt;durable&lt;/em&gt; as &lt;em&gt;false&lt;/em&gt;. Then when our consumer get disconnected, the queue is deleted from the server.&lt;/p&gt;

&lt;p&gt;Here’s the code based on the examples from the AMQP library&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;c1&quot;&gt;--RabbitMQLogs.hs&lt;/span&gt;

&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Network.AMQP&lt;/span&gt;

&lt;span class=&quot;kr&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;qualified&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Data.ByteString.Lazy.Char8&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;as&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;BL&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;main&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;openConnection&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;127.0.0.1&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;/&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;guest&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;guest&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;openChannel&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;--declare queues&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;errorQueue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;     &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;declareQueue&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;anonQueue&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;warningQueue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;  &lt;span class=&quot;kr&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;declareQueue&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;anonQueue&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;infoQueue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;_&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;      &lt;span class=&quot;o&quot;&gt;&amp;lt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;declareQueue&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;anonQueue&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Queues Declared: &quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;mapM_&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;putStrLn&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;errorQueue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;warningQueue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;infoQueue&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;-- bind queues&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;bindQueue&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;errorQueue&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;amq.rabbitmq.log&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;error&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;bindQueue&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;warningQueue&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;amq.rabbitmq.log&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;warning&quot;&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;bindQueue&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;infoQueue&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;amq.rabbitmq.log&quot;&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;info&quot;&lt;/span&gt;
    
    &lt;span class=&quot;c1&quot;&gt;--subscribe to the queues&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;consumeMsgs&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;errorQueue&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Ack&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;callbackError&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;consumeMsgs&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;warningQueue&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Ack&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;callbackWarning&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;consumeMsgs&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;infoQueue&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Ack&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;callbackInfo&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;waiting for logs…&quot;&lt;/span&gt;
    
    &lt;span class=&quot;n&quot;&gt;getLine&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;-- wait for keypress&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;closeConnection&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;connection closed&quot;&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;anonQueue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;QueueOpts&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;anonQueue&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;QueueOpts&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;False&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;False&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;True&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;True&lt;/span&gt;
    

&lt;span class=&quot;n&quot;&gt;callbackError&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Envelope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;callbackError&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;error: &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;BL&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unpack&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msgBody&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ackEnv&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;
    
&lt;span class=&quot;n&quot;&gt;callbackWarning&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Envelope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;callbackWarning&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;warning: &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;BL&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unpack&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msgBody&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ackEnv&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;
    
&lt;span class=&quot;n&quot;&gt;callbackInfo&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;::&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;Message&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Envelope&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;callbackInfo&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kr&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;putStrLn&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;info: &quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;++&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;BL&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unpack&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;$&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msgBody&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;ackEnv&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;env&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The code is pretty self explanatory. First we create the connection to RabbitMQ and obtain a channel. The &lt;em&gt;declareQueue&lt;/em&gt; function expects a &lt;em&gt;channel&lt;/em&gt; and the &lt;em&gt;QueueOpts&lt;/em&gt; which we provide by calling the &lt;em&gt;anonQueue&lt;/em&gt; function. This function will return the following record:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;kt&quot;&gt;QueueOpts&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queueName&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queuePassive&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;False&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queueDurable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;False&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queueExclusive&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;True&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;queueAutoDelete&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;True&lt;/span&gt; 
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;By calling &lt;em&gt;declareQueue&lt;/em&gt; we get back the random name assigned by RabbitMQ to our queue. We declare three queues, one for each log level and then we bind them to the &lt;em&gt;amq.rabbitmq.log&lt;/em&gt; exchange using the respective routing key.&lt;/p&gt;

&lt;p&gt;Once the queues are bound we set up our callback consumers like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-haskell&quot; data-lang=&quot;haskell&quot;&gt;&lt;span class=&quot;n&quot;&gt;consumeMsgs&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chan&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;errorQueue&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;Ack&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;callbackError&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;There we are saying that the messages sent to the &lt;em&gt;errorQueue&lt;/em&gt; will be processed by the “callbackError” function.&lt;/p&gt;

&lt;p&gt;Finally on key press, we will close the connection.&lt;/p&gt;

&lt;p&gt;Providing that the code is on the file &lt;em&gt;RabbitMQLogs.hs&lt;/em&gt; we can test it like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;runghc RabbitMQLogs.hs&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Then on another Terminal window you could start/stop any RabbitMQ consumer/producer and you should start seeing messages like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;info: closing TCP connection &amp;lt;0.2393.0&amp;gt; from 127.0.0.1:59145

warning: exception on TCP connection &amp;lt;0.313.0&amp;gt; from 127.0.0.1:58657
connection_closed_abruptly

info: closing TCP connection &amp;lt;0.313.0&amp;gt; from 127.0.0.1:58657

info: accepted TCP connection on 0.0.0.0:5672 from 127.0.0.1:59195&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Of course to get all of this working you will have to install the AMQP library. The following command should suffice:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;cabal &lt;span class=&quot;nb&quot;&gt;install &lt;/span&gt;amqp&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;I really like Haskell as a language, so I’m quite happy that now there’s a library to use it with RabbitMQ which is another nice piece of software.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;UPDATE 2010-09-10&lt;/em&gt;
Holger Reinhard, the author of the AMQP package, kindly updated the library to return a tuple of (String, Int, Int) from the call to &lt;em&gt;declareQueue&lt;/em&gt;. The first &lt;em&gt;Int&lt;/em&gt; is the number of messages and the second one is the number of consumers.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Riak Map/Reduce Queries in Clojure</title>
   <link href="http://videlalvaro.github.com/2010/08/riak-map-reduce-queries-in-clojure.html"/>
   <updated>2010-08-20T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2010/08/riak-map-reduce-queries-in-clojure</id>
   <content type="html">&lt;h1 id=&quot;riak-mapreduce-queries-in-clojure&quot;&gt;Riak Map/Reduce Queries in Clojure&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;August 14 2010&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;Over this week I’ve been working on a proof of concept to see if it’s possible to use Clojure as the map/reduce language for Riak, in the same way now we can use Javascript and Erlang for that purpose. To accomplish that I needed a way to call Clojure code from Erlang. So I set up a very simple server in Clojure that runs as an Erlang node using &lt;a href=&quot;http://github.com/videlalvaro/closerl&quot;&gt;Closerl&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;After startup that server will ping the Riak node to start a connecting and then will wait for incoming map/reduce queries. Then when there’s a m/r request to Riak it will send a a message like this: &lt;em&gt;{slef(), Command, Value, CljFun}&lt;/em&gt;, where Command will be the atom &lt;em&gt;map&lt;/em&gt; or &lt;em&gt;red&lt;/em&gt; –indicating the operation to perform–, Value is the return riak_object:get_value(Object), and CljFun is the function sent in the JSON request.&lt;/p&gt;

&lt;p&gt;When the Clojure server receives the message, it will do some processing and marshaling to the data, and then it will apply the CljFun to the Value. The CljFun will be read using &lt;em&gt;(read-string fval)&lt;/em&gt;. I wrote some helpers that the function can call to do the processing and the assembly of the reply to Riak, which I will document later.&lt;/p&gt;

&lt;p&gt;So, how does a Map/Reduce request looks like in this case? Based on the examples for the Riak Wiki this will be a similar query but for Clojure:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;curl &lt;span class=&quot;nt&quot;&gt;-X&lt;/span&gt; POST &lt;span class=&quot;nt&quot;&gt;-H&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;content-type: application/json&quot;&lt;/span&gt; &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
http://localhost:8098/mapred &lt;span class=&quot;nt&quot;&gt;--data&lt;/span&gt; @-
&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;inputs&quot;&lt;/span&gt;:[[&lt;span class=&quot;s2&quot;&gt;&quot;alice&quot;&lt;/span&gt;,&lt;span class=&quot;s2&quot;&gt;&quot;p1&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;,[&lt;span class=&quot;s2&quot;&gt;&quot;alice&quot;&lt;/span&gt;,&lt;span class=&quot;s2&quot;&gt;&quot;p2&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]&lt;/span&gt;,[&lt;span class=&quot;s2&quot;&gt;&quot;alice&quot;&lt;/span&gt;,&lt;span class=&quot;s2&quot;&gt;&quot;p5&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;]]&lt;/span&gt;,&lt;span class=&quot;s2&quot;&gt;&quot;query&quot;&lt;/span&gt;:
&lt;span class=&quot;o&quot;&gt;[{&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;map&quot;&lt;/span&gt;:&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;language&quot;&lt;/span&gt;:&lt;span class=&quot;s2&quot;&gt;&quot;clojure&quot;&lt;/span&gt;,&lt;span class=&quot;s2&quot;&gt;&quot;source&quot;&lt;/span&gt;:&lt;span class=&quot;s2&quot;&gt;&quot;(fn [data]  (let [words
(re-seq #&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;\\&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;w+&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt; data)] (map (fn [v] (closerl/otp-tuple
(closerl/otp-binary v) (closerl/otp-long 1))) words)))&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;}}&lt;/span&gt;,
&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;reduce&quot;&lt;/span&gt;:&lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;language&quot;&lt;/span&gt;:&lt;span class=&quot;s2&quot;&gt;&quot;clojure&quot;&lt;/span&gt;,&lt;span class=&quot;s2&quot;&gt;&quot;source&quot;&lt;/span&gt;:&lt;span class=&quot;s2&quot;&gt;&quot;(fn [vs] (let [v1
(remove-struct (remove-not-found vs)) v2 (apply concat v1)
v3 (reduce (fn [m v] (assoc m (first v) (+ (get m (first v) 0)
(second v)))) {} v2)] (as-proplist v3 closerl/otp-binary
closerl/otp-long)))&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;}}]}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;From all this mess let’s extract the map function:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-clj&quot; data-lang=&quot;clj&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;re-seq&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;\&quot;\\&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;w+&lt;/span&gt;&lt;span class=&quot;sc&quot;&gt;\&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;closerl/otp-tuple&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;closerl/otp-binary&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;closerl/otp-long&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;words&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;And the reduce function:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-clj&quot; data-lang=&quot;clj&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;let&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;remove-struct&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;remove-not-found&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;vs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v2&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v3&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;fn&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;assoc&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;first&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;second&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)]&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;as-proplist&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;v3&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;closerl/otp-binary&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;closerl/otp-long&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;What we have here are a couple of anonymous functions that have access to some Clojure libraries for processing the data. For example in the reduce example we want to return a data structure like:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;[{&amp;lt;&amp;lt;&quot;word1&quot;&amp;gt;&amp;gt;, Count1}, {&amp;lt;&amp;lt;&quot;word2&quot;&amp;gt;&amp;gt;, Count2}, ...]
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;To do that we call the helper &lt;em&gt;as-proplist&lt;/em&gt;  to accomplish that. It will iterate the map of key value pairs passed to it and then wrap them in the Erlang types passed as second and third parameters.&lt;/p&gt;

&lt;p&gt;At this point you are probably asking yourself about how advanced is this POC, is it stable, can I use it in production tomorrow, etc. I will try to address those points now.&lt;/p&gt;

&lt;p&gt;What’s missing on the Erlang side of things:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Better integration with rebar. Rebar should be able to pull the closerl repo and compile it.&lt;/li&gt;
  &lt;li&gt;The Clojure node name is hardcoded inside &lt;em&gt;riak_kv_clj_vm:call_clojure&lt;/em&gt;&lt;/li&gt;
  &lt;li&gt;&lt;em&gt;riak_kv_clj_vm&lt;/em&gt; needs to be able to launch the Clojure environment. At the moment is launched by hand.&lt;/li&gt;
  &lt;li&gt;When launching the jvm we need to pass it parameters related to Riak’s node name, cookie, etc.&lt;/li&gt;
  &lt;li&gt;Improve error handling when calling Clojure.&lt;/li&gt;
  &lt;li&gt;Write code to relaunch the JVM.&lt;/li&gt;
  &lt;li&gt;Polish the code implementation, suggestions are welcomed, NOT! :)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;What’s missing on the Clojure side of things:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Add error handling for malformed functions.&lt;/li&gt;
  &lt;li&gt;Improve error handling when calling map and reduce functions.&lt;/li&gt;
  &lt;li&gt;Improve error handling when doing data type conversion from/to Erlang.&lt;/li&gt;
  &lt;li&gt;Add more helpers, load more libraries by default.&lt;/li&gt;
  &lt;li&gt;Polish the code implementation, suggestions are welcomed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As you can see there’s a lot to do, but if you check the code, there’s a lot ready to. Also since I’m mostly trying to get this working properly, I haven’t test with complex cases etc. So please take it as it is.&lt;/p&gt;

&lt;p&gt;How can I get to play with it?&lt;/p&gt;

&lt;p&gt;1) Get Riak’s code:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone http://github.com/basho/riak.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;2) Get riak_kv code from my fork:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone http://github.com/videlalvaro/riak_kv.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;3) Get Closerl code:&lt;/p&gt;

&lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;git clone http://github.com/videlalvaro/closerl.git
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;4) Modify Riak’s code:&lt;/p&gt;

&lt;p&gt;In &lt;em&gt;rebar.config&lt;/em&gt; point the dependency on &lt;em&gt;riak_kv&lt;/em&gt; to your copy of the fork that you just’ve cloned:&lt;/p&gt;

&lt;p&gt;{riak_kv, “0.12.0”, {git, “/path/to/riak_kv”, “HEAD”}}&lt;/p&gt;

&lt;p&gt;In &lt;em&gt;rel/files/vm.args change the Erlang parameters from *name&lt;/em&gt; to &lt;em&gt;sname&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In &lt;em&gt;rel/vars.config&lt;/em&gt; change the node name from &lt;em&gt;riak@127.0.0.1&lt;/em&gt; to &lt;em&gt;riak&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Once you did that then cd into the riak folder and run:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;./rebar get-deps
./rebar compile
./rebar generate
rel/riak/bin/riak console&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;You are half done by now. Next step is to cd into the Closerl folder and run the following commands [1]:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;lein deps
lein run run.clj&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;That will start the map-reduce server. You should see some output like:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-clj&quot; data-lang=&quot;clj&quot;&gt;&lt;span class=&quot;o&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;&amp;lt;OtpNode&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;clj2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mrhyde&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;#&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;&amp;lt;OtpMbox&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;com.ericsson.otp.erlang.OtpMbox&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;@&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a504ec1&amp;gt;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;true&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;running&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;m/r&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;server&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The important thing here is that you see that &lt;em&gt;true&lt;/em&gt; there before the “running m/r server” message. That means that Clojure could connect to the Erlang node where Riak is running.&lt;/p&gt;

&lt;p&gt;You could follow the examples posted &lt;a href=&quot;http://github.com/videlalvaro/closerl/blob/master/riak_commands.sh&quot;&gt;here&lt;/a&gt; to see this in action.&lt;/p&gt;

&lt;p&gt;And that’s it! Thanks for reading this long and please post your comments about this topic.&lt;/p&gt;

&lt;p&gt;Notes:&lt;/p&gt;

&lt;p&gt;1 - As you can see I’m expecting that you have &lt;em&gt;leiningen&lt;/em&gt; installed, which is pretty easy, just follow the steps &lt;a href=&quot;http://github.com/technomancy/leiningen&quot;&gt;here&lt;/a&gt;.&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Hello World</title>
   <link href="http://videlalvaro.github.com/2010/08/hello-world.html"/>
   <updated>2010-08-14T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2010/08/hello-world</id>
   <content type="html">&lt;h1 id=&quot;hello-world&quot;&gt;Hello World&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;August 14 2010&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;After some thoughts I decided to move my tech blog over to GitHub. My main reasons are that writing code examples at Blogspot is plain annoying. Also I think that writing the blog posts in a plain text file is much easier for me than doing so at Blogspot’s web interface.&lt;/p&gt;

&lt;p&gt;So, I hope to be writing here soon about the technology I’m working with right now. Expect to read about Erlang, Clojure, Haskell, PHP, RabbitMQ and who knows what else :).&lt;/p&gt;

&lt;p&gt;See you around soon!&lt;/p&gt;

&lt;p&gt;Álvaro&lt;/p&gt;
</content>
 </entry>
 
 <entry>
   <title>Closerl - Clojure Erlang Bridge</title>
   <link href="http://videlalvaro.github.com/2010/08/closer-clojure-erlang-bridge.html"/>
   <updated>2010-08-14T00:00:00+00:00</updated>
   <id>http://videlalvaro.github.com/2010/08/closer-clojure-erlang-bridge</id>
   <content type="html">&lt;h1 id=&quot;closerl---clojure-erlang-bridge&quot;&gt;Closerl - Clojure Erlang Bridge&lt;/h1&gt;

&lt;p&gt;&lt;span class=&quot;meta&quot;&gt;August 15 2010&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;In order to learn to program with Clojure I decided to work on a bridge between Clojure and Erlang. Why? I wanted to start by doing something that I’m familiar with. I’ve worked with erlang_interface before, bridging Erlang with PHP, so I thought that doing the same with Clojure will be a nice exercise. Besides that I’ve go to learn a lot about the JInterface, the OTP library for bridging Erlang with Java.&lt;/p&gt;

&lt;p&gt;The project is called &lt;a href=&quot;http://github.com/videlalvaro/closerl&quot;&gt;Closer&lt;/a&gt; and is a wrapper around the JInterface, with some helper functions for marshaling values from/to Erlang. Consider the project to be in &lt;em&gt;very&lt;/em&gt; alpha state. Feel free to report bugs, fixes, and comments so I can improve my Clojure style.&lt;/p&gt;

&lt;p&gt;Installation:&lt;/p&gt;

&lt;p&gt;First we have to configure the JInterface to work with our mvn local repo. (If there’s a better way of doing this let me know, since I’m a complete Java n00b). We use &lt;em&gt;mvn&lt;/em&gt; to install JInterface. Adapt the following command to suit your specific configuration:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;mvn &lt;span class=&quot;nb&quot;&gt;install&lt;/span&gt;:install-file &lt;span class=&quot;nt&quot;&gt;-DgroupId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;com.ericsson.otp &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-DartifactId&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;erlang &lt;span class=&quot;nt&quot;&gt;-Dversion&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;1.5.3 &lt;span class=&quot;nt&quot;&gt;-Dpackaging&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;jar &lt;span class=&quot;se&quot;&gt;\&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;-Dfile&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;/usr/local/lib/erlang/lib/jinterface-1.5.3/priv/OtpErlang.jar&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Then obtain the &lt;em&gt;closerl&lt;/em&gt; source code:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;git clone http://github.com/videlalvaro/closerl.git
&lt;span class=&quot;nb&quot;&gt;cd &lt;/span&gt;closerl
lein deps&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Once we have closerl set up we can fire up the Clojure repl and start typing some commands:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-clj&quot; data-lang=&quot;clj&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;use&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;'closerl.core&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;otp-self&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;closerl&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;peer&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;otp-peer&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;rabbit@localhost&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;otp-connect&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;peer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Up to here what we did is to connect to the remote node. We have to create our node object and the reference to the remote node. Then we use &lt;em&gt;otp-connect&lt;/em&gt; to obtain a connection. In this example we connect to a RabbitMQ server running in the local machine. What we are going to do next is to send a message to the RabbitMQ server to retrieve the list of exchanges.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-clj&quot; data-lang=&quot;clj&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;otp-rpc-and-receive&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conn&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
  &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;rabbit_exchange&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;list&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;otp-list&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;otp-binary&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The result should be something like this, depending on your RabbitMQ configuration:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-clj&quot; data-lang=&quot;clj&quot;&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;exchange&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;resource&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;exchange&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;amq.direct&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;direct&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;false&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;exchange&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;resource&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;exchange&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;amq.topic&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;topic&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;false&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;exchange&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;resource&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;exchange&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;amq.rabbitmq.log&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;topic&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;false&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;exchange&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;resource&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;exchange&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;amq.fanout&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;fanout&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;false&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;exchange&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;resource&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;exchange&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;amq.headers&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;headers&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;false&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;exchange&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;resource&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;exchange&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;direct&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;false&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;exchange&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; 
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;resource&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;/&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;exchange&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;amq.match&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;headers&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;true&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;false&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;What we did with &lt;em&gt;otp-rpc-and-receive&lt;/em&gt; was to call the equivalent of Erlang’s &lt;em&gt;rpc:call(node, module, function, args)&lt;/em&gt;. Instead of using &lt;em&gt;node&lt;/em&gt; we use our &lt;em&gt;connection&lt;/em&gt; object. Then the second and third parameters are the module and function respectively.&lt;/p&gt;

&lt;p&gt;As arguments we send an Erlang List with only one element, a binary specifiying the &lt;em&gt;vhost&lt;/em&gt; for which we want the exchanges to be listed. The &lt;em&gt;otp-list&lt;/em&gt; returns a list of &lt;em&gt;OtpErlangObject&lt;/em&gt; containing each of the arguments that we passed to the function, which of course should be of the OtpErlangObject type.&lt;/p&gt;

&lt;p&gt;At the moment the library offers the following functions for constructing OtpErlangObject values.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;otp-double&lt;/li&gt;
  &lt;li&gt;otp-long&lt;/li&gt;
  &lt;li&gt;otp-string&lt;/li&gt;
  &lt;li&gt;otp-binary&lt;/li&gt;
  &lt;li&gt;otp-atom&lt;/li&gt;
  &lt;li&gt;otp-tuple&lt;/li&gt;
  &lt;li&gt;otp-list&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That’s it for now. Thanks for reading.&lt;/p&gt;
</content>
 </entry>
 

</feed>
