The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


Set::Cluster - Distribute items across nodes in a load balanced way and show takeovers in failure scenarios


  # Hash of items, with relative weighting
  $h = { 'Oranges' => 17, 'Apples' => 3, 'Lemons' => 10, 'Pears' => 12 };

  $c = Set::Cluster->new;
  $c->setup( nodes => [qw(A B C)], items => $h );
  $c->calculate(2);             # Go 2 levels deep

  # Functions to parse $c->results
  @takeover = $c->takeover( node => "A", fail => "C" ); # Items taken over due to a failure of C
  @items = $c->items( node => "A", fail => "B" );       # All items for A, when B has failed
  $node = $c->hash_by_item( fail => "" );               # Returns a hash where $node->{$item} returns the node name

  $results = $c->results;       # Returns a hash with how items are split across the nodes
                                # This structure may change in future


This is an attempt at abstracting clustering. The idea is that you can define a list of items with relative weightings, and a list of nodes that the items should be spread across.

The plugin then calculates where items will be distributed to balance the weightings. If you calculate more than 1 level, it will show what happens if there is a failure. When a node fails, its items are distributed amongst the remaining nodes.


The algorithm used is simple: sort the items by the largest weight, then add to the node with the lowest total weight so far.

There is a limitation in that nodes that have not failed should not redistribute their items - this is because of my main reason for creating this module (see HISTORY).

This is not the best algorithm in the world, so if you can implement a better one, I'd love to hear.


setup( nodes => [list of node names], items => {item1 => 10, item2 => 15, ...} )

Sets the list of nodes and all the items.

setup( nodes => [objects], items => [objects] )

Sets the list of nodes and items. The only restriction for the item objects is that it must support a weight method, in order to get the weight value of each object.


Works out all the possible failure scenarios. A level of 0 means just distribute, a level of 1 works out a single point of failure scenario, etc.

takeover( node => $name, fail => $scenario )

Returns an unordered list of items that were added to the specified node at the failure scenario specified.

items( node => $name, fail => $scenario )

Returns an unordered list of items that the specified node has at the time of the failure scenario.

hash_by_item( fail => "scenario" )

Returns a hash ref where the index is item with value of node.


Returns the hash ref holding all the results. The structure is:

  '' => Set::Cluster::Result=HASH(0x84e01c4)
      'A' => ARRAY(0x84eb370)
         0  'Oranges'
         1  'Lemons'
      'B' => ARRAY(0x84e0050)
         0  'Strawberries'
         1  'Melons'
         2  'Apples'
      'C' => ARRAY(0x84dffb4)
         0  'Pears'
         1  'Bananas'
         2  'Kiwis'
  'A' => Set::Cluster::Result=HASH(0x84eb328)
      'B' => ARRAY(0x84ebaa8)
         0  'Strawberries'
         1  'Melons'
         2  'Apples'
         3  'Oranges'
      'C' => ARRAY(0x84ebad8)
         0  'Pears'
         1  'Bananas'
         2  'Kiwis'
         3  'Lemons'

The first section shows a distribution with no failures. The second section is what happens when node A fails. If there is more than one failure, the key will be "failure1,failure2".

This is for curious minds. The preferred way to access this data is via the other methods, as this structure may change in future.


When using objects as the node or the items, the stringification of the object must be unique, otherwise funny things will happen!


This plugin was originally designed for generating Nagios ( configurations for clustered slave monitoring servers. The items are hosts, with the weighting as the number of services monitored on each host.

As this module can predict what would happen in failure scenarios, an event handler can be setup to start monitoring particular hosts in a takeover situation.

If you have a different use for this module, I would be interested to hear.


Only methods listed in this documentation are public.

These modules are experimental and so the interfaces may change up until Set::Cluster hits version 1.0, but every attempt will be made to make backwards compatible.


Ton Voon, <>


Copyright (C) 2006 by Altinity Limited

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.4 or, at your option, any later version of Perl 5 you may have available.