Apache Storm es asombroso. Por eso (y cómo) deberías usarlo.

Los flujos de datos continuos son omnipresentes y lo son aún más con el creciente número de dispositivos de IoT que se utilizan. Por supuesto, esto significa que se almacenan, procesan y analizan grandes volúmenes de datos para proporcionar resultados predictivos y procesables.

Pero los petabytes de datos tardan mucho en analizarse, incluso con herramientas como Hadoop (por muy bueno que sea MapReduce) o Spark (un remedio a las limitaciones de MapReduce).

A menudo, no necesitamos deducir patrones durante largos períodos de tiempo. De los petabytes de datos entrantes recopilados durante meses, en un momento dado, es posible que no necesitemos tenerlos en cuenta todos, solo una instantánea en tiempo real. Tal vez no necesitemos conocer el hashtag de tendencia más largo en cinco años, sino solo el actual.

Para esto está creado Apache Storm, para aceptar toneladas de datos que llegan extremadamente rápido, posiblemente de varias fuentes, analizarlos y publicar actualizaciones en tiempo real en una interfaz de usuario o en algún otro lugar ... sin almacenar ningún dato real .

Este artículo no es la guía definitiva para Apache Storm, ni pretende serlo. Storm es bastante grande, y una sola lectura larga probablemente no pueda hacerle justicia de todos modos. Por supuesto, cualquier adición, retroalimentación o crítica constructiva será muy apreciada.

Bien, ahora que eso está fuera del camino, veamos qué cubriremos:

  • La necesidad de Storm, el 'por qué', qué es y qué no es
  • Una vista de pájaro de cómo funciona.
  • Cómo se ve aproximadamente una topología de Storm en código (Java)
  • Configurar y jugar con un clúster Storm digno de producción en Docker.
  • Algunas palabras sobre la confiabilidad del procesamiento de mensajes.

También supongo que está al menos algo familiarizado con Docker y la contenedorización.

Cómo funciona

La arquitectura de Apache Storm se puede comparar con una red de carreteras que conectan un conjunto de puntos de control. El tráfico comienza en un cierto punto de control (llamado canalón ) y pasa por otros puntos de control (llamados cerrojos ).

El tráfico es, por supuesto, el flujo de datos que recupera el canalón (de una fuente de datos, una API pública, por ejemplo) y se enruta a varios pernos donde los datos se filtran, desinfectan, agregan, analizan y envían a una interfaz de usuario para personas para ver (o para cualquier otro objetivo).

La red de picos y pernos se llama topología y los datos fluyen en forma de tuplas (lista de valores que pueden tener diferentes tipos).

Una cosa importante de la que hablar es la dirección del tráfico de datos.

Convencionalmente, tendríamos uno o varios canales leyendo los datos de una API, un sistema de cola, etc. Los datos luego fluirían unidireccionalmente a uno o varios pernos que pueden reenviarlos a otros pernos y así sucesivamente. Los pernos pueden publicar los datos analizados en una interfaz de usuario o en otro perno.

Pero el tráfico es casi siempre unidireccional, como un gráfico acíclico dirigido (DAG). Aunque ciertamente es posible hacer ciclos, es poco probable que necesitemos una topología tan complicada.

La instalación de una versión de Storm implica una serie de pasos, que puede seguir en su máquina. Pero más adelante usaré contenedores Docker para una implementación de clúster de Storm, y las imágenes se encargarán de configurar todo lo que necesitemos.

Algún código

Aunque Storm ofrece soporte para otros lenguajes, la mayoría de las topologías están escritas en Java, ya que es la opción más eficiente que tenemos.

Un pico muy básico, que solo emite dígitos aleatorios, puede verse así:

Y un simple perno que toma el flujo de dígitos aleatorios y emite solo los pares:

Otro perno simple que recibirá el flujo filtrado de EvenDigitBolt, y simplemente multiplicará cada dígito par por 10 y lo emitirá hacia adelante:

Poniéndolos juntos para formar nuestra topología:

Paralelismo en topologías de tormenta

Comprender completamente el paralelismo en Storm puede ser abrumador, al menos en mi experiencia. Una topología requiere al menos un proceso para operar. Dentro de este proceso, podemos paralelizar la ejecución de nuestros picos y pernos mediante hilos.

En nuestro ejemplo, RandomDigitSpoutse lanzará solo un hilo, y los datos arrojados desde ese hilo se distribuirán entre dos hilos del EvenDigitBolt.

Pero la forma en que se produce esta distribución, denominada agrupación de flujos , puede ser importante. Por ejemplo, puede tener un flujo de registros de temperatura de dos ciudades, donde las tuplas emitidas por el pico se ven así:

// City name, temperature, time of recording
(“Atlanta”, 94, “2018–05–11 23:14”)(“New York City”, 75, “2018–05–11 23:15”)(“New York City”, 76, “2018–05–11 23:16”)(“Atlanta”, 96, “2018–05–11 23:15”)(“New York City”, 77, “2018–05–11 23:17”)(“Atlanta”, 95, “2018–05–11 23:16”)(“New York City”, 76, “2018–05–11 23:18”)

Suponga que colocamos un solo perno cuyo trabajo es calcular la temperatura promedio cambiante de cada ciudad.

Si podemos esperar razonablemente que en un intervalo de tiempo dado obtendremos aproximadamente el mismo número de tuplas de ambas ciudades, tendría sentido dedicar dos hilos a nuestro perno. Podemos enviar los datos de Atlanta a uno de ellos y de Nueva York al otro.

Una agrupación de campos serviría para nuestro propósito, que divide los datos entre los subprocesos por el valor del campo especificado en la agrupación:

// The tuples with the same city name will go to the same thread.builder.setBolt(“avg-temp-bolt”, new AvgTempBolt(), 2) .fieldsGrouping(“temp-spout”, new Fields(“city_name”));

Y, por supuesto, también existen otros tipos de agrupaciones. Sin embargo, en la mayoría de los casos, la agrupación probablemente no importe mucho. Puede simplemente mezclar los datos y lanzarlos entre los subprocesos de pernos al azar ( agrupación aleatoria ).

Ahora hay otro componente importante en esto: la cantidad de procesos de trabajo en los que se ejecutará nuestra topología.

El número total de subprocesos que especificamos se dividirá por igual entre los procesos de trabajo. Entonces, en nuestro ejemplo de topología de dígitos aleatorios, teníamos una rosca de pico, dos roscas de perno de dígitos pares y cuatro roscas de perno multiplicadas por diez (dando siete en total).

Cada uno de los dos procesos de trabajo sería responsable de ejecutar dos roscas de perno multiplicadas por diez, un perno de dígito par y uno de los procesos ejecutará la rosca de un pico.

Por supuesto, los dos procesos de trabajo tendrán sus hilos principales, que a su vez lanzarán los hilos del pico y del perno. Entonces, en total, tendremos nueve hilos. Estos se denominan colectivamente ejecutores .

Es importante darse cuenta de que si establece la sugerencia de paralelismo de un canalón para que sea mayor que uno (varios ejecutores), puede terminar emitiendo los mismos datos varias veces.

Say the spout reads from the public Twitter stream API and uses two executors. That means that the bolts receiving the data from the spout will get the same tweet twice. It is only after the spout emits the tuples that data parallelism comes into play. In other words, the tuples get divided among the bolts according to the specified stream grouping.

Running multiple workers on a single node would be fairly pointless. Later, however, we’ll use a proper, distributed, multi-node cluster and see how workers are divided on different nodes.

Building our topology

Here’s the directory structure I suggest:

yourproject/ pom.xml src/ jvm/ packagename/ RandomDigitSpout.java EvenDigitBolt.java MultiplyByTenBolt.java OurSimpleTopology.java

Maven is commonly used for building Storm topologies, and it requires a pom.xml file (The POM) that defines various configuration details, project dependencies, and so on. Getting into the nitty-gritty of the POM will probably be overkill here.

  • First, we’ll run mvn clean inside yourproject to clear any compiled files we may have, making sure to compile each module from scratch.
  • And then mvn package to compile our code and package it in an executable JAR file, inside a newly created target folder. This might take quite a few minutes the first time, especially if your topology has many dependencies.
  • To submit our topology: storm jar target/packagename-{version number}.jar packagename.OurSimpleTopology

Hopefully by now the gap between concept and code in Storm has been somewhat bridged. However, no serious Storm deployment will be a single topology instance running on one server.

What a Storm cluster looks like

To take full advantage of Storm’s scalability and fault-tolerance, any production-grade topology would be submitted to a cluster of machines.

Storm distributions are installed on the primary node (Nimbus) and all the replica nodes (Supervisors).

The primary node runs the Storm Nimbus daemon and the Storm UI. The replica nodes run the Storm Supervisor daemons. A Zookeeper daemon on a separate node is used for coordination among the primary node and the replica nodes.

Zookeeper, by the way, is only used for cluster management and never any kind of message passing. It’s not like spouts and bolts are sending data to each other through it or anything like that. The Nimbus daemon finds available Supervisors via ZooKeeper, to which the Supervisor daemons register themselves. It also carries out other managerial tasks, some of which will become clear shortly.

The Storm UI is a web interface used to manage the state of our cluster. We’ll get to this later.

Our topology is submitted to the Nimbus daemon on the primary node, and then distributed among the worker processes running on the replica/supervisor nodes. Thanks to Zookeeper, it doesn’t matter how many replica/supervisor nodes you run initially, as you can always seamlessly add more. Storm will automatically integrate them into the cluster.

Whenever we start a Supervisor, it allocates a certain number of worker processes (that we can configure). These can then be used by the submitted topology. So in the image above, there are a total of five allocated workers.

Remember this line: conf.setNumWorkers(5)

This means that the topology will try to use a total of five workers. And since our two Supervisor nodes have a total of five allocated workers, each of the 5 allocated worker processes will run one instance of the topology.

If we had run conf.setNumWorkers(4) then one worker process would have remained idle/unused. If the number of specified workers was six and the total allocated workers were five, then because of the limitation only five actual topology workers would’ve been functional.

Before we set this all up using Docker, there are a few important things to keep in mind regarding fault-tolerance:

  • If any worker on any replica node dies, the Supervisor daemon will have it restarted. If restarting repeatedly fails, the worker will be reassigned to another machine.
  • If an entire replica node dies, its share of the work will be given to another supervisor/replica node.
  • If the Nimbus goes down, the workers will remain unaffected. However, until the Nimbus is restored, workers won’t be reassigned to other replica nodes if, say, their node crashes.
  • The Nimbus and Supervisors are themselves stateless. But with Zookeeper, some state information is stored so that things can begin where they were left off if a node crashes or a daemon dies unexpectedly.
  • Nimbus, Supervisor and Zookeeper daemons are all fail-fast. This means that they themselves are not very tolerant to unexpected errors, and will shut down if they encounter one. For this reason they have to be run under supervision using a watchdog program that monitors them constantly and restarts them automatically if they ever crash. Supervisord is probably the most popular option for this (not to be confused with the Storm Supervisor daemon).

Note: In most Storm clusters, the Nimbus itself is never deployed as a single instance but as a cluster. If this fault-tolerance is not incorporated and our sole Nimbus goes down, we’ll lose the ability to submit new topologies, gracefully kill running topologies, reassign work to other Supervisor nodes if one crashes, and so on.

For simplicity, our illustrative cluster will use a single instance. Similarly, the Zookeeper is very often deployed as a cluster but we’ll use just one.

Dockerizing The Cluster

Launching individual containers and all that goes along with them can be cumbersome, so I prefer to use Docker Compose.

We’ll be going with one Zookeeper node, one Nimbus node, and one Supervisor node initially. They’ll be defined as Compose services, all corresponding to one container each at the beginning.

Later on, I’ll use Compose scaling to add another Supervisor node (container). Here’s the entire code and the project structure:

zookeeper/ Dockerfilestorm-nimbus/ Dockerfile storm.yaml code/ pom.xml src/ jvm/ coincident_hashtags/ ExclamationTopology.java storm-supervisor/ Dockerfile storm.yamldocker-compose.yml

And our docker-compose.yml:

Feel free to explore the Dockerfiles. They basically just install the dependencies (Java 8, Storm, Maven, Zookeeper) on the relevant containers.

The storm.yaml files override certain default configurations for the Storm installations. The line ADD storm.yaml /conf inside the Nimbus and Supervisor Dockerfiles puts them inside the containers where Storm can read them.

storm-nimbus/storm.yaml:

storm-supervisor/storm.yaml:

These options are adequate for our cluster. If you are curious, you can check out all the default configurations here.

Run docker-compose up at the project root.

After all the images have been built and all the service started, open a new terminal, type docker ps and you’ll see something like this:

Starting The Nimbus

Let’s SSH into the Nimbus container using its name:

docker exec -it coincidenthashtagswithapachestorm_storm-nimbus_1 bash

And then start the Nimbus daemon: storm nimbus

Starting The Storm UI

Similarly, open another terminal, SSH into the Nimbus again and launch the UI using storm ui:

Go to localhost:8080 on your browser and you’ll see a nice overview of our cluster:

The ‘Free slots’ in the Cluster Summary indicate how many total workers (on all Supervisor nodes) are available and waiting for a topology to consume them.

‘Used slots’ indicates how many of the total are currently busy with a topology. Since we haven’t launched any Supervisors yet, they’re both zero. We’ll get to Executors and Tasks later. Also, as we can see, no topologies have been submitted yet.

Starting A Supervisor Node

SSH into the one Supervisor container and launch the Supervisor daemon:

docker exec -it coincidenthashtagswithapachestorm_storm-supervisor_1 bashstorm supervisor 

Now let’s go refresh our UI:

Note: Any changes in our cluster may take a few seconds to reflect on the UI.

We have a new running Supervisor which comes with four allocated workers. These four workers are the result of specifying four ports in our storm.yaml for the Supervisor node. Of course, they’re all free (four Free slots).

Let’s submit a topology to the Nimbus and put ’em to work.

Submitting A Topology To The Nimbus

SSH into the Nimbus on a new terminal. I’ve written the Dockerfile so that we land on our working (landing) directory /theproject. Inside this is code, where our topology resides.

Our topology is pretty simple. It uses a spout that generates random words and a bolt that just appends three exclamation marks (!!!) to the words. Two of these bolts are added back-to-back, and so at the end of the stream we’ll get words with six exclamation marks. It also specifies that it needs three workers (conf.setNumWorkers(3)).

Run these commands:

1. cd code

2. mvn clean

3. mvn package

4. storm jar target/coincident-hashtags-1.2.1.jar coincident_hashtags.ExclamationTopology

After the topology has been submitted successfully, refresh the UI:

As soon as we submitted the topology, the Zookeeper was notified. The Zookeeper in turn notified the Supervisor to download the code from the Nimbus. We now see our topology along with its three occupied workers, leaving just one free.

And ten word spout threads + three exclaim1 bolt threads + two exclaim bolt threads + the three main threads from the workers = total of 18 executors.

And you might’ve noticed something new: tasks.

What are tasks?

Tasks are another concept in Storm’s parallelism. But don’t sweat it, a task is just an instance of a spout or bolt that an executor uses. They are what actually does the processing.

By default, the number of tasks is equal to the number of executors. In rare cases you might need each executor to instantiate more tasks.

// Each of the two executors (threads) of this bolt will instantiate// two objects of this bolt (total 4 bolt objects/tasks).builder.setBolt(“even-digit-bolt”, new EvenDigitBolt(), 2) .setNumTasks(4) .shuffleGrouping(“random-digit-spout”);

This is a shortcoming on my part, but I can’t think of a good use case where we’d need multiple tasks per executor.

Maybe if we were adding some parallelism ourselves, like spawning a new thread within the bolt to handle a long running task, then the main executor thread won’t block and will be able to continue processing using the other bolt.

However, this can make our topology hard to understand. If anyone knows of scenarios where the performance gain from multiple tasks outweighs the added complexity, please post a comment.

Anyways, returning from that slight detour, let’s see an overview of our topology. Click on the name under Topology Summary and scroll down to Worker Resources:

We can clearly see the division of our executors (threads) among the three workers. And of course all the three workers are on the same, single Supervisor node we’re running.

Now, let’s say scale out!

Add Another Supervisor

From the project root, let’s add another Supervisor node/container:

docker-compose scale storm-supervisor=2

SSH into the new container:

docker exec -it coincidenthashtagswithapachestorm_storm-supervisor_2 bash

And fire up: storm supervisor

If you refresh the UI you’ll see that we’ve successfully added another Supervisor and four more workers (total of eight workers/slots). To really take advantage of the new Supervisor, let’s increase the topology’s workers.

  • First kill the running one: storm kill exclamation-topology
  • Change this line to: conf.setNumWorkers(6)
  • Change the project version number in your pom.xml. Try using a proper scheme, like semantic versioning. I’ll just stick with 1.2.1.
  • Rebuild the topology: mvn package
  • Resubmit it: storm jar target/coincident-hashtags-1.2.1.jar coincident_hashtags.ExclamationTopology

Reload the UI:

You can now see the new Supervisor and the six busy workers out of a total of eight available ones.

Also important to note is that the six busy ones have been equally divided among the two Supervisors. Again, click the topology name and scroll down.

We see two unique Supervisor IDs, both running on different nodes, and all our executors pretty evenly divided among them. This is great.

But Storm comes with another nifty way of doing so while the topology is running — rebalancing.

On the Nimbus we’d run:

storm rebalance exclamation-topology -n 6

Or to change the number of executors for a particular component:

storm rebalance exclamation-topology -e even-digit-bolt=3

Reliable Message Processing

One question we haven’t tackled is about what happens if a bolt fails to process a tuple.

Storm provides us a mechanism by which the originating spout (specifically, the task) can replay the failed tuple. This processing guarantee doesn’t just happen by itself. It’s a conscious design choice, and does add latency.

Spouts send out tuples to bolts, which emit tuples derived from the input tuples to other bolts and so on. That one original tuple spurs an entire tree of tuples.

If any child tuple, so to speak, of the original one fails, then any remedial steps (rollbacks etc) may well have to be taken at multiple bolts. That could get pretty hairy, and so what Storm does is that it allows the original tuple to be emitted again right from the source (the spout).

Consequently, any operations performed by bolts that are a function of the incoming tuples should be idempotent.

A tuple is considered “fully processed” when every tuple in its tree has been processed, and every tuple has to be explicitly acknowledged by the bolts.

However, that’s not all. There’s another thing to be done explicitly: maintain a link between the original tuple and its child tuples. Storm will then be able to trace the origin of the child tuples and thus be able to replay the original tuple. This is called anchoring. And this has been done in our exclamation bolt:

// ExclamationBolt
// ‘tuple’ is the original one received from the test word spout.// It’s been anchored to/with the tuple going out._collector.emit(tuple, new Values(exclamatedWord.toString()));
// Explicitly acknowledge that the tuple has been processed._collector.ack(tuple);

The ack call will result in the ack method on the spout being called, if it has been implemented.

So, say you’re reading the tuple data from some queue and you can only take it off the queue if the tuple has been fully processed. The ack method is where you’d do that.

You can also emit out tuples without anchoring:

_collector.emit(new Values(exclamatedWord.toString())) 

and forgo reliability.

A tuple can fail two ways:

  1. A bolt dies and a tuple times out. Or, it times out for some other reason. The timeout is 30 seconds by default and can be changed using config.put(Config.TOPOLOGY_MESSAGE_TIMEOUT_SECS, 60)
  2. The fail method is explicitly called on the tuple in a bolt: _collector.fail(tuple). You may do this in case of an exception.

In both these cases, the fail method on the spout will be called, if it is implemented. And if we want the tuple to be replayed, it would have to be done explicitly in the fail method by calling emit, just like in nextTuple(). When tracking tuples, every one has to be acked or failed. Otherwise, the topology will eventually run out of memory.

It’s also important to know that you have to do all of this yourself when writing custom spouts and bolts. But the Storm core can help. For example, a bolt implementing BaseBasicBolt does acking automatically. Or built-in spouts for popular data sources like Kafka take care of queuing and replay logic after acknowledgment and failure.

Parting Shots

Designing a Storm topology or cluster is always about tweaking the various knobs we have and settling where the result seems optimal.

There are a few things that’ll help in this process, like using a configuration file to read parallelism hints, number of workers, and so on so you don’t have to edit and recompile your code repeatedly.

Define your bolts logically, one per indivisible task, and keep them light and efficient. Similarly, your spouts’ nextTuple() methods should be optimized.

Use the Storm UI effectively. By default, it doesn’t show us the complete picture, only 5% of the total tuples emitted. To monitor all of them, use config.setStatsSampleRate(1.0d).

Keep an eye on the Acks and Latency values for individual bolts and topologies via the UI. That’s what you want to look at when tuning the parameters.