#!/usr/bin/env perl

# Copyright (C) 2005-2008  Joshua Hoblitt
#
# $Id: neb-admin,v 1.14 2008-10-16 22:24:39 jhoblitt Exp $

use strict;
use warnings FATAL => qw( all );

use vars qw( $VERSION );
$VERSION = '0.02';

use DBI;
use Net::Server::Daemonize qw( check_pid_file create_pid_file unlink_pid_file );
use URI;

use Getopt::Long qw( GetOptions :config auto_help auto_version );
use Pod::Usage qw( pod2usage );

my (
    $db,
    $dbhost,
    $dbpass,
    $dbuser,
    $so_id_start,
    $so_id_range,
    $limit,
    $offset,
    $pending,
    $balance,
    $balance_N_sources,
    $balance_M_destinations,
    $removal,
    $verbose,
);

$db     = $ENV{'NEB_DB'};
$dbhost = $ENV{'NEB_DBHOST'} || 'localhost';
$dbuser = $ENV{'NEB_USER'};
$dbpass = $ENV{'NEB_PASS'};

$offset = 0;

GetOptions(
    'db|d=s'                => \$db,
    'host=s'                => \$dbhost,
    'user|u=s'              => \$dbuser,
    'pass|p=s'              => \$dbpass,
    'pendingreplicate|r'    => \$pending,
    'pendingbalance|b'      => \$balance,
    'balancesources|N=s'      => \$balance_N_sources,
    'balancedestinations|M=s' => \$balance_M_destinations,
#    'pendingremoval'        => \$removal,
    'so_id_start=i'         => \$so_id_start,
    'so_id_range=i'         => \$so_id_range,
    'limit|l=i'             => \$limit,
    'offset=i'              => \$offset,
    'verbose|v'             => \$verbose,
) || pod2usage( 2 );

pod2usage( -msg => "Unknown option: @ARGV", -exitval => 2 ) if @ARGV;
pod2usage( -msg => "Required options: --db --user --pass", -exitval => 2 )
    unless $db && $dbuser && $dbpass;
pod2usage( -msg => "--limit is meaningless without --pendingreplicate",
        -exitval => 2 )
    if defined $limit and not (defined $pending or defined $balance);
pod2usage( -msg => "no operation specified", -exitval => 2 )
    unless defined $pending or defined $balance;
pod2usage( -msg => "--pendingbalance needs --balancesources and --balancedestinations options",
	   -exitval => 2)
    if defined $balance and not (defined $balance_N_sources or defined $balance_M_destinations);
# set default limit to 5
$limit ||= 5;

# check to make sure that only one instance of neb-admin is running
# XXX this implies we should move pending replicate elsewhere
my $pidfile;
if (defined $pending) {
    $pidfile = '/var/tmp/neb-admin.replicate';
}
elsif (defined $balance) {
    $pidfile = '/var/tmp/neb-admin.balance';
}
else {
    $pidfile = '/var/tmp/neb-admin';
}
# abort if an instance is already running
END { unlink_pid_file($pidfile) };
check_pid_file($pidfile);
create_pid_file($pidfile);

my $dbh = DBI->connect(
    "DBI:mysql:database=$db:host=$dbhost",
    $dbuser,
    $dbpass,
    {
        RaiseError => 1,
        PrintError => 0,
        AutoCommit => 1,
    },
);

if ($pending) {
    pending();
} elsif ($removal) {
    removal();
} elsif ($balance) {
    balance();
} else {
    die "THIS SHOULD NOT HAPPEN";
}

sub pending
{
# so_id, ext_id, instances, available_instances, need_recovery, recoverable
# XXX don't remove the temp table code -- testing w/o it
#    $dbh->do("CREATE TEMPORARY TABLE mymountedvol LIKE mountedvol");
#    $dbh->do("INSERT INTO mymountedvol SELECT * FROM mountedvol");

    if (not defined $so_id_start) { $so_id_start = 0; }
    if (not defined $so_id_range) { $so_id_range = 100000; }
    my $so_id_end = $so_id_start + $so_id_range;

    # XXX check if so_id_start is beyond MAX(so_id): if so, exit with exit status 10
    { 
        my $query = $dbh->prepare("SELECT MAX(so_id) from storage_object");
        $query->execute;
        my $answer = $query->fetchrow_arrayref;
        my $max_so_id = $$answer[0];
        $query->finish;

        if ($so_id_start > $max_so_id) { 
            print STDERR "at end of so_id range, reset please\n";
            exit 10;
        }
    }
        
    my $query = $dbh->prepare(
    "
        SELECT
            so.so_id,
            so.ext_id,
            count(ins_id) as instances,
            mv.name as volume_name,
            mv.host as volume_host,
            count(mv.vol_id) as available_instances,
            count(mv.vol_id) > 0 as recoverable,
            xattr.value as copies
        FROM storage_object AS so
        LEFT JOIN storage_object_xattr AS xattr
            ON so.so_id = xattr.so_id
            AND xattr.name = 'user.copies'
        JOIN instance AS i
            ON so.so_id = i.so_id
        JOIN mountedvol AS mv
            USING(vol_id)
        WHERE
            mv.available = 1
            AND so.so_id >= $so_id_start
            AND so.so_id <  $so_id_end
        GROUP BY so_id
        HAVING available_instances != instances OR instances != copies
        LIMIT $limit"
    );

    $query->execute;

    my @rows;
    while (my $row = $query->fetchrow_hashref) {
        push @rows, $row;        
    }
    $query->finish;
        
    exit unless scalar @rows;
    
    # compare number of responses to limit below
    my $Npending = @rows;

    print "replicatePending MULTI\n\n";

    foreach my $obj (@rows) {
        if (defined $verbose) {
            require Data::Dumper;
            print Data::Dumper::Dumper($obj);
        }

        my $so_id = $obj->{so_id};
        my $key = $obj->{ext_id};
        my $has = $obj->{instances};
        my $vol_name = $obj->{volume_name};
        my $vol_host = $obj->{volume_host};
	my $available = $obj->{available_instances} || 0;
        my $need_recovery = $obj->{need_recovery};
        my $recoverable = $obj->{recoverable};
        # if the copies xattr is unset and the object has 2 or more instances, we
        # will assume a target of 2 copies
        
        # XXX change this: there should be no default value or we will
        # have a race condition.  user.copies should never get set by
        # any client until AFTER a file is no longer in use.
        my $copies = $obj->{copies} || 2;

        # objects with only a single instance that are offline are unrecoverable so
        # we don't need to handle that special case
#    next unless $need_recovery;
        unless ($recoverable) {
            warn "so_id: $so_id key: \'$key\' ",
                "can not be recovered - no available instances\n";
            next;
        }

        my $need = $copies - $available;
	my $cmd = 'neb-stat';
#        $need = 0 if $need < 0;
        next unless $need;

	if ($need > 0) {
	    $cmd = 'neb-replicate';
	}
	elsif ($need < 0) {
	    $cmd = 'neb-cull';
	}

        my %md = (
            key         => $key,
	    command     => $cmd,
            need_copies => $need,
            volume_name => $vol_name,
            volume_host => $vol_host,

#	    has         => $has,
	    available   => $available,
	    xattr_copies      => $copies,
#	    recoverable => $recoverable,
#	    need_recovery => $need_recovery,
	    
        );
#	print "$key $cmd $need $vol_name $vol_host\n";
        print_metadata("replicatePending", \%md);
        print "\n";
    }

    # use a different exit status if we hit the limit (likely more files pending)
    # This is a better way to do this.
    if ($Npending == 0) {
	exit 0;
    }
    else {
	exit 1;
    }

#     if ($Npending == $limit) {
#         exit 1;
#     } 
#     exit 0;
}

sub balance
{
# so_id, ext_id, instances, available_instances, need_recovery, recoverable
# XXX don't remove the temp table code -- testing w/o it
#    $dbh->do("CREATE TEMPORARY TABLE mymountedvol LIKE mountedvol");
#    $dbh->do("INSERT INTO mymountedvol SELECT * FROM mountedvol");

    if (not defined $so_id_start) { $so_id_start = 0; }
    if (not defined $so_id_range) { $so_id_range = 100000; }
    my $so_id_end = $so_id_start + $so_id_range;

    # XXX check if so_id_start is beyond MAX(so_id): if so, exit with exit status 10
    { 
        my $query = $dbh->prepare("SELECT MAX(so_id) from storage_object");
        $query->execute;
        my $answer = $query->fetchrow_arrayref;
        my $max_so_id = $$answer[0];
        $query->finish;

        if ($so_id_start > $max_so_id) { 
            print STDERR "at end of so_id range, reset please\n";
            exit 10;
        }
    }

    # Calculate the average disk usage
    my $average = 0;
    my $avg_query = $dbh->prepare(
	"select sum(used)/sum(total) AS average from mountedvol WHERE available = 1 AND allocate = 1"
	);
    $avg_query->execute();
    while (my $row = $avg_query->fetchrow_hashref) {
	$average = $row->{average};
    }
    $avg_query->finish;
    print STDERR "average: $average\n";
    if ($average == 0) {
	exit 1;
    }
    # I truly am sorry for this statement.
    my $query = $dbh->prepare(
	"
SELECT * FROM (
 -- U Randomize the list of all possible source/destination matches,then group by so_id to select one at random
 SELECT T.*,volume.cab_id AS source_cab_id,volume.name AS source_name,volume.host AS source_host,
        destination.vol_id AS destination_vol_id,destination.cab_id AS destination_cab_id,
        destination.name AS destination_name,destination.host AS destination_host FROM (
  -- V Ensure that the copy we found on the source volume is the second copy instance
  SELECT K.vol_id AS here,K.ins_id,K.so_id,ext_id,value AS user_copies,MAXins_id,instance.vol_id AS there FROM (
   -- W Determine what the second copy instance is
   SELECT V.*,MAX(instance.ins_id) AS MAXins_id FROM (
    -- X Determine which hosts have instances of an object
    SELECT vol_id,ins_id,so_id,ext_id,value FROM 
    storage_object JOIN storage_object_xattr USING(so_id) JOIN instance USING(so_id)
    WHERE vol_id IN (
     -- Y Extra select for debugging purposes
     SELECT vol_id FROM (
      -- Z Ran hosts by R (need to be a source) and return top N
      SELECT mountedvol.vol_id,mountedvol.name,mountedvol.available,mountedvol.allocate,total,used,
             (used / total) AS D,total * ((used / total) - $average ) AS R,  -- average
             (used / total) - $average AS delta,
             cab_id FROM
      mountedvol JOIN volume USING(vol_id)
      WHERE mountedvol.available = 1
      ORDER BY delta DESC LIMIT $balance_N_sources                                            -- N
      ) AS ranked_sources
      -- Z End
     )
     -- Y End
    AND name = 'user.copies' AND value >= 2
  AND ext_id LIKE '%ota%fits'       -- This line is the emergency speedup option
    AND so_id >= $so_id_start                                  -- so_id_start
    AND so_id <  $so_id_end                                    -- so_id_end
   ) AS V -- volumes that host a copy
   -- X End
  LEFT OUTER JOIN instance USING(so_id) GROUP BY so_id
  ) AS K -- copies that are the second copy
  -- W End
 JOIN instance ON MAXins_id = instance.ins_id GROUP BY so_id
 ) AS T -- matched copies that I know the second copy is on my source volume
 -- V End
 JOIN volume on T.here = volume.vol_id RIGHT JOIN (
  -- Y2 Chose a random destination from the list
  SELECT vol_id,cab_id,name,host FROM (
   -- Z2 Reverse rank hosts by R and return top M
   SELECT mountedvol.vol_id,mountedvol.name,mountedvol.host,mountedvol.available,mountedvol.allocate,total,used,
          (used / total) AS D,total * ((used / total) - $average ) AS R,  -- average
          (used / total) - $average AS delta,
          cab_id FROM
   mountedvol JOIN volume USING(vol_id)
   WHERE mountedvol.available = 1 AND mountedvol.allocate = 1
   ORDER BY delta ASC LIMIT $balance_M_destinations                                               -- M
  ) AS ranked_destinations
  -- Z2 End
 ORDER BY RAND()
 ) AS destination
 -- Y2 End
 ON destination.cab_id != volume.cab_id
 WHERE here = there
 ORDER BY RAND()
) AS results
-- U End
GROUP BY so_id
    LIMIT $limit                                               -- limit
    OFFSET $offset                                             -- offset

"    );

    $query->execute();
		    
		    

    my @rows;
    while (my $row = $query->fetchrow_hashref) {
        push @rows, $row;        
    }
    $query->finish;

    print STDERR "No rows found\n" unless scalar @rows;
    exit unless scalar @rows;
    
    # compare number of responses to limit below
    my $Npending = @rows;

    print "balancePending MULTI\n\n";

    foreach my $obj (@rows) {
        if (defined $verbose) {
            require Data::Dumper;
            print Data::Dumper::Dumper($obj);
        }
	

	my $here = $obj->{here};
	my $there = $obj->{there};
	my $ins_id = $obj->{ins_id};
	my $max_ins_id = $obj->{MAXins_id};
	
	my $so_id = $obj->{so_id};
	my $key   = $obj->{ext_id};
	my $copies = $obj->{user_copies};

	my $source_vol_id = $obj->{here};
	my $source_cab_id = $obj->{source_cab_id};
	my $source_name   = $obj->{source_name};
	my $source_host   = $obj->{source_host};

	my $destination_vol_id = $obj->{destination_vol_id};
	my $destination_cab_id = $obj->{destination_cab_id};
	my $destination_name   = $obj->{destination_name};
	my $destination_host   = $obj->{destination_host};
	
	unless ($here == $there) { $Npending-- ; next; }
	unless ($ins_id == $max_ins_id) { $Npending-- ; next; }
	unless ($source_cab_id != $destination_cab_id) { $Npending-- ;  next; }
	
        my %md = (
	    key           => $key,
	    copies        => $copies,
	    so_id         => $so_id,
	    source_vol_id => $source_vol_id,
	    source_cab_id => $source_cab_id,
	    source_name   => $source_name,
	    source_host   => $source_host,
	    destination_vol_id => $destination_vol_id,
	    destination_cab_id => $destination_cab_id,
	    destination_name   => $destination_name,
	    destination_host   => $destination_host,
#             key         => $key,
# 	    command     => $cmd,
#             need_copies => $need,
#             volume_name => $vol_name,
#             volume_host => $vol_host,

# #	    has         => $has,
# 	    available   => $available,
# 	    xattr_copies      => $copies,
# #	    recoverable => $recoverable,
# #	    need_recovery => $need_recovery,
	    
        );
#	print "$key $cmd $need $vol_name $vol_host\n";
        print_metadata("balancePending", \%md);
        print "\n";
    }

    # use a different exit status if we hit the limit (likely more files pending)
    print STDERR "Which exit: $Npending $limit\n";
    # This is a better way to do this. This should be even better-er.
    if ($Npending < $limit) {
	exit 0;
    }
    else {
	exit 1;
    }

#     if ($Npending == $limit) {
#         exit 1;
#     } 
#     exit 0;
}

sub removal
{
# so_id, ext_id, instances, available_instances, need_recovery, recoverable
    $dbh->do("CREATE TEMPORARY TABLE mymountedvol LIKE mountedvol");
    $dbh->do("INSERT INTO mymountedvol SELECT * FROM mountedvol");
    $dbh->do("CREATE TEMPORARY TABLE myvolume LIKE volume");
    $dbh->do("INSERT INTO myvolume SELECT * FROM volume");

    my $query = $dbh->prepare(
    );
    $query->execute;

    $dbh->do("DROP TABLE IF EXISTS mymountedvol");
    $dbh->do("DROP TABLE IF EXISTS myvolume");

    my @rows;
    while (my $row = $query->fetchrow_hashref) {
        push @rows, $row;        
    }
    $query->finish;
        
    exit unless scalar @rows;

    print "removalPending MULTI\n\n";

    foreach my $obj (@rows) {
        if (defined $verbose) {
            require Data::Dumper;
            print Data::Dumper::Dumper($obj);
        }

        my $so_id = $obj->{so_id};
        my $key = $obj->{ext_id};
        my $has = $obj->{instances};
        my $vol_name = $obj->{volume_name};
        my $vol_host = $obj->{volume_host};
        my $available = $obj->{available_instances} || 0;
        my $need_recovery = $obj->{need_recovery};
        my $recoverable = $obj->{recoverable};
        # if the copies xattr is unset and the object has 2 or more instances, we
        # will assume a target of 2 copies
        my $copies = $obj->{copies} || 2;

        # objects with only a single instance that are offline are unrecoverable so
        # we don't need to handle that special case
#    next unless $need_recovery;
        unless ($recoverable) {
            warn "so_id: $so_id key: \'$key\' ",
                "can not be recovered - no available instances\n";
            next;
        }

        my $need = $copies - $available;
        $need = 0 if $need < 0;

        next unless $need;

        my %md = (
            key         => $key,
            need_copies => $need,
            volume_name => $vol_name,
            volume_host => $vol_host,
        );

        print_metadata("replicatePending", \%md);
        print "\n";
    }

    return 1;
}


sub print_metadata
{
    my ($name, $pairs) = @_;

    print "$name METADATA\n";

    # format from formatMetadataItem() in pzMetadataConfig.c
    foreach my $key (keys %$pairs) {
        my $value = $pairs->{$key};
        if (looks_like_number($value)) {
            printf("   " . "%-15s  %-8s  %-15s\n", $key, "S32", $value);
        } else {
            printf("   " . "%-15s  %-8s  %-15s\n", $key, "STR", $value);
        }
    }
    
    print "END\n";
}

# looks_like_number() was stolen from Scalar::Util
#
# Copyright (c) 1997-2006 Graham Barr <gbarr@pobox.com>. All rights reserved.
# This program is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.

sub looks_like_number {
  local $_ = shift;

  # checks from perlfaq4
  return 0 if !defined($_) or ref($_);
  return 1 if (/^[+-]?\d+$/); # is a +/- integer
  return 1 if (/^([+-]?)(?=\d|\.\d)\d*(\.\d*)?([Ee]([+-]?\d+))?$/); # a C float
  return 1 if ($] >= 5.008 and /^(Inf(inity)?|NaN)$/i) or ($] >= 5.006001 and /^Inf$/i);

  0;
}

__END__

=pod

=head1 NAME

neb-admin - perform misc. Nebulous administrative functions

=head1 SYNOPSIS

    neb-admin [--host <db host>] [--db <db name>] [--user <db username>]
        [--pass <db password>] [--pendingreplicate] [--limit <n>]

=head1 DESCRIPTION

=head1 OPTIONS

=over 4

=item * --pendingreplicate|-r

Print out a list of nebulous keys that need additional replications created.

=item * --limit|-l <n>

Limits the number of items return in response to some request (like
C<--pendingreplicate>). 

Optional.  Defaults to 5.  --limit 0 is treated as asking for the default
value.

=item * --host <database host>

Name of host on which the database resides.

Optional.  Defaults to C<localhost>.

=item * --db|-d <database>

Name of database (C<namespace>) to create tables in.

Optional if the appropriate environment variable is set.

=item * --user|-u <username>

Username to authenticate with.

Optional if the appropriate environment variable is set.

=item * --pass|-p <password>

Password to authenticate with.

Optional if the appropriate environment variable is set.

=back

=head1 ENVIRONMENT

These environment variables may be used in place of the specified command line
options.  All command line option will override the corresponding environment
value.

=over 4

=item * C<NEB_DB>

Equivalent to --db|-d

=item * C<NEB_HOST>

Equivalent to --host

=item * C<NEB_USER>

Equivalent to --user|-u

=item * C<NEB_PASS>

Equivalent to --pass|-p 

=back

=head1 CREDITS

Just me, myself, and I.

=head1 SUPPORT

Please contact the author directly via e-mail.

=head1 AUTHOR

Joshua Hoblitt <jhoblitt@cpan.org>

=head1 COPYRIGHT

Copyright (C) 2005-2008  Joshua Hoblitt.  All rights reserved.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE.  See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place - Suite 330, Boston, MA  02111-1307, USA.

The full text of the license can be found in the L<perlgpl> Pod as supplied
with Perl 5.8.1 and later.

=head1 SEE ALSO

L<Nebulous::Server>, L<neb-addvol>, L<neb-fsck>, L<neb-initdb>, L<nebdiskd>

=cut
