Problem
Storm assigns spouts/bolts in a topology to supervisors using its default scheduler, with which users can hardly predict where the spout/bolt goes. But you may want to control where they go based on certain metadata (e.g. location, sequence number) in some use cases. In this case, the default scheduler will not work well for you.
Background
Since 0.8.0, Storm allows users to use custom scheduler for task scheduling. It also allows users to specify scheduling-specific metadata in supervisor’s configuration. The new feature favors us in resolving the problem.
Use Case
To showcase the solution, we assume that we submit a topology with spouts/bolts tagged with “group ID” and want them to be assigned to supervisors with the same “group ID” as shown below.

Solution
The solution consists of 3 steps:
- Add metadata, i.e. group ID in this scenario, to supervisors
- Tag spouts/bolts with group IDs
- Implement custom scheduler that reads the metadata and associates the spouts/bolts with supervisors properly
Step 1
Storm preserves a field supervisor.scheduler.meta in each supervisor’s configuration file, $STORM_HOME/conf/storm.yaml, for specifying custom metadata. Any key-value pairs can be put in this field. The metadata won’t be used by Storm’s core. In our scenario, we will put a “group-id: 1” pair for supervisor in Group 1 as below. Remember restart the supervisor daemon after modifying the configuration in order to have the updates applied.
...
supervisor.scheduler.meta:
group-id: 1
...
Step 2
After tagging supervisors with “group-id”, we then tag the spouts/bolts accordingly so that the scheduler can associate the spouts/bolts with supervisors correctly. The tagging is done using addConfiguration(key, value) method when building the topology with TopologyBuilder. The example is shown below.
...
TopologyBuilder builder = new TopologyBuilder();
builder.setSpout("Spout_1", new ExampleSpout(), 1).addConfiguration("group-id", 1);
builder.setBolt("Bolt_1", new ExampleBolt(), 1).shuffleGrouping("Spout_1").addConfiguration("group-id", 1);
builder.setBolt("Bolt_2", new ExampleBolt2(), 1).shuffleGrouping("Spout_1").addConfiguration("group-id", 2);
builder.setBolt("Bolt_3", new ExampleBolt3(), 1).globalGrouping("Bolt_1").globalGrouping("Bolt_2").addConfiguration("group-id", 3);
StormSubmitter.submitTopology("example-topology", builder.createTopology());
...
Step 3
This step is important as it makes the magic happen. The custom scheduler is deployed on Nimbus and should be referenced in the configuration file. Now let’s go through it step by step.
To implement a custom scheduler, we need to implement the IScheduler interface as shown below.
import java.util.Map;
import backtype.storm.scheduler.Cluster;
import backtype.storm.scheduler.IScheduler;
import backtype.storm.scheduler.Topologies;
public class GroupingScheduler implements IScheduler{
@Override
public void prepare(Map conf) {
...
}
@Override
public void schedule(Topologies topologies, Cluster cluster) {
...
}
}
The interface has two methods to be implemented. The prepare(Map conf) method is used for initializing the scheduler. It comes with a system-wide configuration. The custom scheduling policy is implemented in the schedule(Topologies topologies, Cluster cluster) method. It is called by Storm core periodically. The topologies is a wrap-up of all topologies submitted to the Storm cluster. It contains a collection of TopologyDetails instances, each of which contains all the details about a topology, including custom metadata we set before. The collection can be acquired by calling getTopologies() method. The cluster represents the Storm cluster, which has a getSupervisors() method to get a collection of SupervisorDetails instances. A SupervisorDetails instance encapsulates all the information about a supervisor, including the metadata we set for each supervisor before.
Note that the complexity of matching up group IDs on spouts/bolts and supervisors is O(total number of spouts/bolts * total number of supervisors) . Since the number of supervisors is relatively static, we can store the supervisors in a hash map and index them using the metadata, i.e. group ID in this scenario, so that the complexity can be lowered to O(total number of spouts/bolts). The metadata on each supervisor can be derived by calling getSchedulerMeta() method, which returns metadata in key-value pairs. The supervisor map will be frequently updated as the schedule() method is called periodically, so the scheduler will be aware of newly added supervisors. Till now, the code should look like what is shown below.
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import backtype.storm.scheduler.Cluster;
import backtype.storm.scheduler.IScheduler;
import backtype.storm.scheduler.SupervisorDetails;
import backtype.storm.scheduler.Topologies;
import backtype.storm.scheduler.TopologyDetails;
public class GroupingScheduler implements IScheduler{
@Override
public void prepare(Map conf) {
}
@Override
public void schedule(Topologies topologies, Cluster cluster) {
Collection<TopologyDetails> topologyDetails = topologies.getTopologies();
Collection<SupervisorDetails> supervisorDetails = cluster.getSupervisors().values();
Map<Integer, SupervisorDetails> supervisors = new HashMap<Integer, SupervisorDetails>();
// to accelerate supervisor lookup
for(SupervisorDetails s : supervisorDetails){
Map<String, Object> metadata = (Map<String, Object>)s.getSchedulerMeta();
if(metadata.get("group-id") != null){
supervisors.put((Integer)metadata.get("group-id"), s);
}
}
}
}
As we may have a number of submitted topologies, we need to iterate over them to check if they need to be scheduled. Storm cluster offers a method, needsScheduling(TopologyDetails topology), for checking if a topology needs to be scheduled. The spouts/bolts details in a topology are encapsulated in the StormTopology, which can be derived by calling getTopology(). Now the code should look like this.
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import backtype.storm.generated.Bolt;
import backtype.storm.generated.SpoutSpec;
import backtype.storm.generated.StormTopology;
import backtype.storm.scheduler.Cluster;
import backtype.storm.scheduler.IScheduler;
import backtype.storm.scheduler.SupervisorDetails;
import backtype.storm.scheduler.Topologies;
import backtype.storm.scheduler.TopologyDetails;
public class GroupingScheduler implements IScheduler{
@Override
public void prepare(Map conf) {
}
@Override
public void schedule(Topologies topologies, Cluster cluster) {
Collection<TopologyDetails> topologyDetails = topologies.getTopologies();
Collection<SupervisorDetails> supervisorDetails = cluster.getSupervisors().values();
Map<Integer, SupervisorDetails> supervisors = new HashMap<Integer, SupervisorDetails>();
for(SupervisorDetails s : supervisorDetails){
Map<String, Object> metadata = (Map<String, Object>)s.getSchedulerMeta();
if(metadata.get("group-id") != null){
supervisors.put((Integer)metadata.get("group-id"), s);
}
}
for(TopologyDetails t : topologyDetails){
if(cluster.needsScheduling(t)) continue;
StormTopology topology = t.getTopology();
Map<String, Bolt> bolts = topology.get_bolts();
Map<String, SpoutSpec> get_spouts = topology.get_spouts();
}
}
}
The map keys are names of spouts/bolts, which will be referenced later. Bolt and SpoutSpec are similar in structure. Both of them have a get_common() method that returns a ComponentCommon object. The custom metadata is stored as a JSON string returned by get_json_conf(). To parse the JSON string, you need to pick up your preferred JSON API, such as json-simple, Jackson, etc. In this example, we use json-simple.
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import backtype.storm.generated.Bolt;
import backtype.storm.generated.SpoutSpec;
import backtype.storm.generated.StormTopology;
import backtype.storm.scheduler.Cluster;
import backtype.storm.scheduler.IScheduler;
import backtype.storm.scheduler.SupervisorDetails;
import backtype.storm.scheduler.Topologies;
import backtype.storm.scheduler.TopologyDetails;
public class GroupingScheduler implements IScheduler{
@Override
public void prepare(Map conf) {
}
@Override
public void schedule(Topologies topologies, Cluster cluster) {
Collection<TopologyDetails> topologyDetails = topologies.getTopologies();
Collection<SupervisorDetails> supervisorDetails = cluster.getSupervisors().values();
Map<Integer, SupervisorDetails> supervisors = new HashMap<Integer, SupervisorDetails>();
for(SupervisorDetails s : supervisorDetails){
Map<String, Object> metadata = (Map<String, Object>)s.getSchedulerMeta();
if(metadata.get("group-id") != null){
supervisors.put((Integer)metadata.get("group-id"), s);
}
}
for(TopologyDetails t : topologyDetails){
if(cluster.needsScheduling(t)) continue;
StormTopology topology = t.getTopology();
Map<String, Bolt> bolts = topology.get_bolts();
Map<String, SpoutSpec> spouts = topology.get_spouts();
JSONParser parser = new JSONParser();
try{
for(String name : bolts.keySet()){
Bolt bolt = bolts.get(name);
JSONObject conf = (JSONObject)parser.parse(bolt.get_common().get_json_conf());
if(conf.get("group-id") != null && supervisors.get(conf.get("group-id")) != null){
Integer gid = (Integer)conf.get("group-id");
}
}
for(String name : spouts.keySet()){
SpoutSpec spout = spouts.get(name);
JSONObject conf = (JSONObject)parser.parse(spout.get_common().get_json_conf());
if(conf.get("group-id") != null && supervisors.get(conf.get("group-id")) != null){
Integer gid = (Integer)conf.get("group-id");
}
}
}catch(ParseException pe){
pe.printStackTrace();
}
}
}
}
With the topology components’ group IDs in hand, it’s time to assign them to the supervisors correctly. Since we cached supervisors by their group IDs before, we can simply retrieve them back now. In Storm, a supervisor’s system resource is partitioned into a number of worker slots. So we actually assign a component to an available worker slot on a particular supervisor. We use getAvailableSlots(SupervisorDetails supervisor) method to get a list of available worker slots on the supervisor. On the other hand, a topology component may consist of a number of exectutors, which are threads making progress on data processing. Similarly, we also need to get the executors for each component using getNeedsSchedulingComponentToExecutor(TopologyDetails topology). The method returns a map consisting of “<component-name, executors>” pairs, so we can retrieve the right executors with the component name. Before the assignment, make sure that there are available worker slots on the supervisor and there are executors for the component waiting for scheduling. Finally, we use assign(WorkerSlot slot, String topologyId, Collection executors) method to do the assignment. We assume that there is at least one worker slot available on each supervisor and we pick up the first available slot to run the component. The completed scheduler is listed below.
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import backtype.storm.generated.Bolt;
import backtype.storm.generated.SpoutSpec;
import backtype.storm.generated.StormTopology;
import backtype.storm.scheduler.Cluster;
import backtype.storm.scheduler.ExecutorDetails;
import backtype.storm.scheduler.IScheduler;
import backtype.storm.scheduler.SupervisorDetails;
import backtype.storm.scheduler.Topologies;
import backtype.storm.scheduler.TopologyDetails;
import backtype.storm.scheduler.WorkerSlot;
public class GroupingScheduler implements IScheduler{
@Override
public void prepare(Map conf) {
}
@Override
public void schedule(Topologies topologies, Cluster cluster) {
Collection<TopologyDetails> topologyDetails = topologies.getTopologies();
Collection<SupervisorDetails> supervisorDetails = cluster.getSupervisors().values();
Map<Integer, SupervisorDetails> supervisors = new HashMap<Integer, SupervisorDetails>();
for(SupervisorDetails s : supervisorDetails){
Map<String, Object> metadata = (Map<String, Object>)s.getSchedulerMeta();
if(metadata.get("group-id") != null){
supervisors.put((Integer)metadata.get("group-id"), s);
}
}
for(TopologyDetails t : topologyDetails){
if(cluster.needsScheduling(t)) continue;
StormTopology topology = t.getTopology();
Map<String, Bolt> bolts = topology.get_bolts();
Map<String, SpoutSpec> spouts = topology.get_spouts();
JSONParser parser = new JSONParser();
try{
for(String name : bolts.keySet()){
Bolt bolt = bolts.get(name);
JSONObject conf = (JSONObject)parser.parse(bolt.get_common().get_json_conf());
if(conf.get("group-id") != null && supervisors.get(conf.get("group-id")) != null){
Integer gid = (Integer)conf.get("group-id");
SupervisorDetails supervisor = supervisors.get(gid);
List<WorkerSlot> availableSlots = cluster.getAvailableSlots(supervisor);
List<ExecutorDetails> executors = cluster.getNeedsSchedulingComponentToExecutors(t).get(name);
if(!availableSlots.isEmpty() && executors != null){
cluster.assign(availableSlots.get(0), t.getId(), executors);
}
}
}
for(String name : spouts.keySet()){
SpoutSpec spout = spouts.get(name);
JSONObject conf = (JSONObject)parser.parse(spout.get_common().get_json_conf());
if(conf.get("group-id") != null && supervisors.get(conf.get("group-id")) != null){
Integer gid = (Integer)conf.get("group-id");
SupervisorDetails supervisor = supervisors.get(gid);
List<WorkerSlot> availableSlots = cluster.getAvailableSlots(supervisor);
List<ExecutorDetails> executors = cluster.getNeedsSchedulingComponentToExecutors(t).get(name);
if(!availableSlots.isEmpty() && executors != null){
cluster.assign(availableSlots.get(0), t.getId(), executors);
}
}
}
}catch(ParseException pe){
pe.printStackTrace();
}
}
}
}
Installation
The scheduler should be exported as a jar file for installation. It is recommended to use Apache Maven to package it but you can choose you favorite toolkit to do that too. With the jar file ready, you need to copy it to Storm’s library directory on Nimbus as follow.
cp grouping-scheduler.jar $STORM_HOME/lib
Then tell Nimbus to use the scheduler.
...
storm.scheduler: "storm.custom.scheduler.GroupingScheduler"
...
Remember to restart Nimbus daemon after modifying the configuration.
Improvements
The example scheduler is very simple as it is designed for showcasing how to acquire and use user metadata in a scheduler. Designing a production-ready scheduler is a delicate work. We will not discuss how to improve the scheduler line by line here, but you may want to take the following situations into consideration when doing the design:
- How to mix regular and custom scheduling in a single scheduler?
- What if there are multiple supervisors with same metadata tag (e.g. group ID)?
- What if there are no worker slots available on a particular supervisor?
- If multiple scheduling policies are introduced, how to manage priorities?
- Is it possible to distribute executors over multiple worker slots/supervisors for load balancing purpose?
I believe there are more interesting points around this topic. I will try out several of them in the future.
Reference
This post is inspired by Twitter Storm: How to develop a pluggable scheduler.