Skip to content

Instantly share code, notes, and snippets.

@pyh
Created May 24, 2012 19:35
Show Gist options
  • Save pyh/2783746 to your computer and use it in GitHub Desktop.
Save pyh/2783746 to your computer and use it in GitHub Desktop.
Deprioritize hosts mogilefs plugin
package MogileFS::Plugin::DeprioritizeHosts;
use strict;
use warnings;
our $VERSION = '0.01';
$VERSION = eval $VERSION;
use MogileFS::Util qw(error);
use Data::Dumper;
our $max_sorted_devs = 20;
sub load {
MogileFS::register_global_hook( 'cmd_create_open_order_devices', sub {
my $devices = shift;
my $sorted_devs = shift;
my ( $primary_devices, $fallback_devices ) = sorted_devices( $devices );
# Plugin has not been configured, or there was nothing to sort (should not happen)
return if !( $primary_devices || $fallback_devices );
# Mimics Worker/Query.pm cmd_create_open
# Sort primary targets
@$sorted_devs = MogileFS::Worker::Query::sort_devs_by_freespace( @$primary_devices );
# Less than 20 devices returned, should be rare (for us), add fallback devices
my $fallback_devices_needed = $max_sorted_devs - scalar( @$sorted_devs );
if( $fallback_devices_needed > 0 ) {
my @fallbacks_sorted = MogileFS::Worker::Query::sort_devs_by_freespace( @$fallback_devices );
push( @$sorted_devs, splice( @fallbacks_sorted, 0, $fallback_devices_needed ));
}
return 1;
} );
MogileFS::register_global_hook( 'cmd_get_paths_order_devices', sub {
my $devices = shift;
my $sorted_devs = shift;
my ( $primary_devices, $fallback_devices ) = sorted_devices( $devices );
# Plugin has not been configured, or there was nothing to sort (should not happen)
return if !( $primary_devices || $fallback_devices );
# Mimics Wordker/Query.pm cmd_get_paths
@$sorted_devs = ( MogileFS::Worker::Query::sort_devs_by_utilization( @$primary_devices ),
MogileFS::Worker::Query::sort_devs_by_utilization( @$fallback_devices ) );
return 1;
} );
return 1;
}
sub sorted_devices {
my $devices = shift;
my $deprioritized_hosts = MogileFS::Config->server_setting_cached( "deprioritized_hosts" );
# Plugin has not been configured, return quickly
return if !$deprioritized_hosts;
my %host_map = map { $_ => 1 } split( /\s*,\s*/, $deprioritized_hosts );
my @fallback_devices;
my @primary_devices;
foreach my $dev ( @$devices ) {
if( $host_map{$dev->host->name} ) {
push( @fallback_devices, $dev );
}else{
push( @primary_devices, $dev );
}
}
return ( \@primary_devices, \@fallback_devices );
}
sub unload {
MogileFS::unregister_global_hook( 'cmd_create_open_order_devices' );
MogileFS::unregister_global_hook( 'cmd_get_paths_order_devices' );
return 1;
}
1;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment