Tuesday, October 30, 2018

Functional Java: Hate the player not the game

We've all heard it over and over, Java 8 is not really functional programming. I admit that features like tail call optimization, closure over the context, the hideous boxed() method call when mapping a primitive type to a boxed type might make functional programming in java a little verbose, and maybe even bitter. But that doesn't prevent Java 8 from providing a set of tools and constructs that produce very elegant solutions. In what follows, I have compiled a (very short) set of very elegant solutions that I've encountered, and I couldn't help but admire their beauty.

Recursive traversal of a tree: by Fahd Shariff

Assuming a tree made of a root node, where each node has associated children. Please admire this one liner:
public Stream <TreeNode<E>> stream() {
         return Stream.concat(Stream.of(this), children.stream().flatMap(TreeNode::stream));

Count the occurrence of specific words in a larger passage: by Ken Kousen

public Map<String,Integer> countWords(String passage, String... strings) {
            Map<String, Integer> wordCounts = new HashMap<>();
            Arrays.stream(strings).forEach(s -> wordCounts.put(s, 0));
            Arrays.stream(passage.split(" ")).forEach(
                word -> wordCounts.computeIfPresent(word, (key, val) -> val + 1));
            return wordCounts;
}

Fibonacci Sequence with memoization: by Ken Kousen

private Map<Long, BigInteger> cache = new HashMap<>();
        public BigInteger fib(long i) {
        if (i == 0) return BigInteger.ZERO;
        if (i == 1) return BigInteger.ONE;
       return cache.computeIfAbsent(i, n -> fib(n - 2).add(fib(n - 1)));
}

Monday, October 22, 2018

Asynchronous Messaging: RabbitMQ Introduction

RabbitMQ is considered today as a stable open source message broker implementation. It is considered by many as the natural evolution of JMS. What is brings to the table is the interoperability of disparate and heterogeneous parties. Indeed, a client in .Net for example can seamlessly exchange messages with a consumer on Java with minimal changes to any of these. It is worth noting that RabbitMQ is build with Erlang, the leader language in telecommunications systems within builtin support for fault tolerance.

RabbitMQ promotes the usage of AMQP (Advanced messaging queuing protocol) as the wire level protocol or network protocol for exchanging messages. It is a binary protocol that deals with the low level details of encoding and marshaling of message contents.

Architecturally, RabbitMQ provides the following advantages:

  • Reliability: Aside from being built with Erlan, RabbitMQ can be configured to persist messages, so that in case of a server crash, all messages can be restored. Additionally, producers and consumers can acknowledge proper reception/delivery of messages.
  • Customized routing: RabbitMQ supports different mechanisms for routing through the use of exchanges, it can for example provide point to point communication through direct routing, selective message delivery, similar to JMS message selectors, so that only events carrying a certain "routing key" get delivered to a queue.
  • Built in support for clustering and high availability: Many instances of RabbitMQ can be logically grouped under a single cluster in order to provide redundancy and ultimately high availability in the case of crashes.
  • Scripting and administration: RabbitMQ provides both a web based console for the purpose of monitoring and administration. In addition, it provides a command line interface to automate its administration through scripts.
  • Versatility: there is a plethora of clients for different platforms/technologies. 


Terminology

Since a picture is worth a thousand words, let's start with a high level schematic from RabbitMQ Documentation


  • Publisher: The party that is at the origin of the message to be sent.
  • Consumer: The destination party that expresses its interest in one or more messages.
  • Message Broker: The messaging solution in this case RabbitMQ. It is made of:
    • Exchanges: The abstraction that describes an intermediate endpoint/stage on the message broker where all messages are delivered first.
    • Queues: The intermediate endpoint where messages are sent from the exchange.
    • Route: Provides a routing strategy to define how and when messages on the exchange should be relayed to the queue. This usually takes the form of a routing key, and follows a binding definition.

In the next installment, we'll have a look at the different types of exchanges and various patterns for exchanging information over RabbitMQ.

Sunday, October 21, 2018

Microservices: Why Asynchronous communications?

The shift towards Microservices entails embracing and assessing different approaches to each and every aspect. Today, we'll dwell on asynchronous communication between microservices or between client and service at large.

Why asynchronous?

Or why stick to synchronous communications should we ask? It is true that synchronous communications has come to be regarded as the De facto pattern to exchange information between any two endpoints. Think of TCP, HTTP, FTP ... . It does indeed exhibit the following advantages:
  1. Simpler reasoning and tracing: Everything starts when an outbound request in made from the client, and we can step through the whole process all the way until the request is processed by the server and a response is sent accordingly.
  2. "Instantaneous" feedback:  When a client breaks the contract, or the server deems the current request as invalid, feedback is sent right away to the client. This can take the form of response codes, redirects, or event stack traces.
  3. Natural: In the sense that we like to think about thing A happens before thing B, or thing C happens as a consequence of thing B.
  4. Translates directly to models: We're all used to sequence diagrams on UML, and very few people can tell from the back of their head how asynchronous calls are modeled. Instead, we like to think of them as a succession of synchronous calls.
I assume the list might be longer, but it fails to address the following concerns:
  1. How should a long running process on the server be presented to the client? If the server is experiencing a high peak in demand, or if the resources are scarce, chances are the client will be affected too. Should it just wait? Should it timeout? How should the client interpret this situation?
  2. When an upstream dependency (server A calls server B) is unavailable, should the client care? Why should the client be sensitive to our ecosystem. From its standpoint, it reached the server, the rest is all internals.
  3. Guaranteed response time: what if I told you that my server will respond in O(1) no matter what. How is that for usability?
  4. What if the client does not need a response right away? Assume that I place my order in the context of an ecommerce application. I know what I ordered, I know how much I paid and I know when and where I will be delivered. Why should I hang in there until the server does all its housekeeping (updating product stocks, notifying the warehouse, ....)
  5. Better yet, you don't know whom you're talking to but you're sure that your request will be honored. I always feel this way when I need an administrative paper. It seems that I always need to find the right person, and more importantly, at the right time. I always wished I could just submit my request to a "mailbox" and just head home and be sure that it will be fulfilled.
  6. My request is more urgent that yours. Now you're in a nuclear power plant, and maintenance staff keeps track of the major events there by posting these events to the monitoring application, business as usual, cooling check... Suddenly, Mr Hero realizes that a major event that jeopardizes the safety of the whole neighborhood is about to take place, and posts an emergency stop payload, now I wouldn't like to be the one who processes a ventilation check while making the emergency stop wait.   
These are some of the pain points that asynchronous communications relieve us from. Long running tasks, unavailable servers, unresponsive servers, server transparency, request reordering and prioritizing... In the next post, we'll see how this can be done.

Monday, October 15, 2018

Specification Pattern in Java

This blog post is mainly inspired by the works of Martin Fowler and Eric Evans in their seminal paper Specifications

I find this pattern geniune as it remedies years of head bashing and hair pulling for finding ways of isolating the evolution of a domain model from the mechanisms involved in inspecting it and querying it while maintaining a high level of encapsulation. I like to think about it as "reflection" on the domain model. Hopefully, things will become clearer as we move proceed.

Let us suppose that we have an ecommerce application with a very exotic product catalog whereby products are represented by a type hierarchy, for example

public class Product{
    String gTIN;
    double price;
    int units;
}

public class Television extends Product{
   ResolutionEnum resolution;
   float screenSize; //in inches
}



Suppose we'd like to search for products matching the following criteria:

  1. The maximum price is 3000 Dh (that is the Moroccan currency dirhams, MAD)
  2. The screen size is more than 12.1''


A naive approach would simply have it done this way

public class SearchProductService{

    ProductRepository inMemoryRepository;
   
    public Product findBy(double maxPrice, float minScreenSize){
 
         List<Product> allProducts = inMemoryRepository.findAll(p-> {p.price < price});
        allProduct.stream().filter(p -> p instanceof Television).map( p-> (Television ) p)
         .filter(p-> p.resolution > minScreenSize).collect(toList());
    }
}

Now suppose we also want to be able to search by resolution and available units. It won't get any better

public class SearchProductService{

    ProductRepository inMemoryRepository;
   
    public Product findBy(double minPrice, float maxScreenSize ){
 
    }

     public Product findBy(ResolutionEnum resolution, int availableUnits ){
 
    }

     public Product findBy(ResolutionEnum resolution, int availableUnits ,double maxPrice, float minScreenSize){
 
    }

    //handle all combinations?
}

Clearly, this solution does not scale, plus it forces us to expose some properties of the product, maybe some that we'd rather keep private. What is even worse is the logic that builds up upon these properties far away from where the properties are located. By all design standards, this solution is a time bomb and a maintenance nightmare. Can we do better?

Of course, we can - we wouldn't be here otherwise :-). The solution has been formalized under the name: specification pattern. Basically, we create our search criteria and  let the product tell you if it meets them or not. Quoting the master Martin Fowler "Tell, don't ask".


class Product{

   public boolean satisfies(SearchCriteria criteria){
       //Open up for extension
       return criteria.isSatisifiedBy(this);
   }

}

class Television extends Product{
//No change here }

/***************    Define Criteria ***********************/
public interface SearchCriteria{
     boolean isSatisfiedBy(Product product);
}

/**************** Composite **************************/
public class Criteria implements SearchCriteria{
     private List<SearchCriteria> criteria ;
 
 
    public Criteria(List<SearchCriteron> criteria){
      this.criteria = criteria;
   }
 
//We could also add the operators add, not, or for combining criteria, here it is AND
   public isSatisfiedBy(Product product)(){
       Iterator<Criteria> iterator = criteria.iterator();
      while(iterator.hasNext()){
          if(!iterator.next().isSatisfiedBy(product))
              return false;
      }
      return true:
     }
}

/****************  Price Criterion  **************************/
public class PriceCriterion implements SearchCritera{
 
    public PriceCriterion(Operator operator, double target){
    //
    }
 
     public boolean isSatisfiedBy (Product product){
                 //Put logic here
    }

}

/***************  Criteria builder ********************/
public class SearchCriteriaBuilder{

     protected List<SearchCriteron> criteria = new ArrayList<>();

 
    private PriceCriteriaBuilder priceCriteriaBuilder;
 
   
    public PriceCriteriaBuilder withPrice(){
        if(priceCriteriaBuilder == null)
            priceCriteriaBuilder = new PriceCriteriaBuilder();
         return priceCriteriaBuilder;
     }

    public PriceCriteriaBuilder and(){
       return this;
   }

     public SearchCritera build(){
           return new Criteria(criteria);
      }

 
    public PriceCriteriaBuilder{
        Operator operator;
          double targetPrice;
         public enum Operator{
                lessThan,
                equal,
                largetThan
               ....
         }
   
         public PriceCriteriaBuilder being(Operator operator) {
                   this.operator = operator;
                   return this;
         }
     
          public PriceCriteriaBuilder value(double targetPrice) {
                   this.targetPrice = targetPrice;
                    PriceCriteriaBuilder.this.criteria.add(new PriceCriterion(operator,targetPrice));
                   return  PriceCriteriaBuilder.this ;
         }
 
    }

}

Phew!!! With all of this behind, let's look how the client code would look like
Criteria criteria = new SearchCriteriaBuilder().
            withPrice()
                       .being(lessThan).value(3000)
            .and()
          .withScreenSize()//could be impletement in the same manner
                        .being(largetThan).value(12.1)
           .build();
Television television = ProductRepository.getTelevisions(); television.satisfies(criteria);


Please admire the conciseness and expressivity of the solution. It is true that we had to put in a lot of code behind, but that's the price if you're willing to use the porcelain instead of the plumbing. Please note that this implementation of the specification pattern is a mashup of several design patters:


  • Composite Pattern: product deals only with the criteria class and it does not change if there is one criterion or many.
  • Visitor Pattern: Each Concerte criteria class has to deal with the specifics of the product, and the interaction with the product is kept simple thanks to one method satisfies(SearchCriteria criteria)
  • Command Pattern: We build the list of criteria and execute them once at the end with a single call satisfies(criteria).
  • Builder Pattern: Allowed the creation of a fluid, progressive API.


I hope that was an interesting read.

Thank you

Wednesday, October 10, 2018

Automate the repetitive stuff: Scoop


Scoop: Command line installer for windows

All right, so you've just finished reading up on a stack, language, tool and want to practice, but don't really wanna take the time to dive into the details of setting up the new environment tools. Fear not, Scoop is here to the rescue.

Scoop is written as a bunch of powershell commands that shield you from the details and locations of the different packages and versions you want to try out.


Installation


All you gotta do is install it by running the following command on Powershell

//Set the perimission rights
Set-ExecutionPolicy RemoteSigned -scope CurrentUser

//install it
iex (new-object net.webclient).downloadstring('https://get.scoop.sh')

//Optional, if you do not have git already
scoop install git

//install all buckets (we'll get to that in a moment)
1scoop bucket known | % { scoop bucket add $_ }


You end up with the following directory in your home directory





type in scoop in your console





Please note that you have to run scoop under powershell, otherwise you might run into


The term 'C:\Users\lhechma\scoop\apps\scoop\current\bin\scoop.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.


Architecture

Layout

Scoop adds itself to the home directory as a powershell scipt under ~/scoop/shims

All its commands are themselves scripts distributes accross three main subfolders






  • bin: The main entrypoint, mainly through scoop.ps1 script
  • lib: Its internal supporting librairies, for example installing shortcuts on the start menu, or parsing json
  • libexec: wich almost mirror the commands you get on the console.

How about buckets?

Scoop uses the term bucket in its literal meaning. It is the isolation and addressing mechanism for addressing the different meta-repositories, which contain configuration details for all the software packages within it. This means that is necessary to find buckets for your desired package if it is not bundled with the default list of buckets that are preconfigured with Scoop. In the example below, I wanted to try out Clojure (it is about time I guess). Since it is not there by default on any bucket.


So I had to add a new bucket with clojure in it

scoop bucket add wangzq https://github.com/wangzq/scoop-bucket

scoop install clojure


This is what I ended up with





Go your console and type clojure


I finally got clojure working, with a maximum of 5 commands and without leaving my console. What a scoop!


Minimal intrusion/Transparency:


Scoop does its magic while doing its best not to pollute your PATH environment with the newly installed packages (sometimes it has to do, like for Java JDK). The trick is to create a new cmd command with the name of the package under ~/scoop/shims. Only this path is added to your user path.

Conceptual integrity:


Scoop manages itself in the same way it manages its own packages. DRY at its best.

Simplicity:


Version management has always been a headache. Think back at ANT, Maven with its optional dependencies,...  Scoop manages package versioning with .... folder shorctus. Basically, a new shim (executable if you will) will point to a version folder, that points to the proper version, so you can have multiple versions of the same package without any issues. The old moto: add one level of inderction does wonders here.

Additionally, scoop is based upon well established and understood technologies: GIT, JSON. Your learning curve is almost inexistant if you've been doing any software development in the last years.

Criticism


Just because I love to have the rebel attitude, I'm going to mention that I would have loved to have less verbositiy at the level of the commands used internally to extract and run installers (like for pre_install and post_install hooks). But again, I guess that interoperability is not required here, since scoop is tailored to Windows, a DSL would do more harm than good.

Comparison

I can't do better than the creator. So I'll let check how Scoop compares to Chocolatey here


See you on the next installent for more production tools.
Thanks for reading





Functional Java: Hate the player not the game

We've all heard it over and over, Java 8 is not really functional programming. I admit that features like tail call optimization, closu...