Show
Ignore:
Timestamp:
09/04/09 12:41:37 (5 years ago)
Author:
kazuho
Message:

reinstall incline triggers

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • lang/perl/DBIx-ShardManager-Pacific/trunk/pacific_divide

    r35189 r35192  
    66use DBI; 
    77use Getopt::Long; 
     8use File::Temp qw(tempfile); 
    89use IO::File::AtomicChange; 
    910use JSON; 
     11use Storable qw(dclone); 
    1012 
    1113our $VERSION = 0.01; 
     
    2022my ( 
    2123    $opt_rdbms, $opt_database, $opt_user, $opt_password, $opt_shard_def, 
    22     $opt_new_host, $opt_new_port, $opt_from_id, $opt_drop_old_user_after, 
    23     $opt_help, $opt_version, 
     24    $opt_new_host, $opt_new_port, $opt_from_id, $opt_incline_source, 
     25    $opt_drop_old_user_after, $opt_help, $opt_version, 
    2426); 
    2527 
     
    3739  --new-port=port     (optional) 
    3840  --from=lowest_id    lowest id of the data to be moved to the new node 
     41  --incline-source=file 
     42                      if set, re-installs incline triggers (default: empty) 
    3943  --drop-old-user-after=seconds 
    4044                      drops read privileges granted to old user after given 
     
    8993    'new-port=i'            => \$opt_new_port, 
    9094    'from-id=i'             => \$opt_from_id, 
     95    'incline-source=s'      => \$opt_incline_source, 
    9196    'drop-old-user-after=i' => \$opt_drop_old_user_after, 
    9297    help                    => \$opt_help, 
     
    207212); 
    208213 
     214my $new_username = "pac" . time; 
     215 
     216sub updated_shard_def { 
     217    my $r = dclone($shard_def); 
     218    $orig_node->[0]->{username} = $new_username; 
     219    $r->{map}->{$opt_from_id} = [ 
     220        { 
     221            %{$orig_node->[0]}, 
     222            host => $opt_new_host, 
     223            ($opt_new_port ? (port => $opt_new_port) : ()), 
     224        }, 
     225    ]; 
     226    return $r; 
     227} 
     228 
     229# reinstall incline triggers 
     230if ($opt_incline_source) { 
     231    print "INFO: reinstalling incline triggers\n"; 
     232    my ($tmp_fh, $tmp_def) = tempfile(); 
     233    print $tmp_fh to_json(updated_shard_def(), { utf8 => 1, pretty => 1 }); 
     234    $tmp_fh->flush; 
     235    my @incline_cmd = ( 
     236        q(incline), 
     237        '--mode=shard', 
     238        "--source=$opt_incline_source", 
     239        "--shard-source=$tmp_def", 
     240        "--rdbms=$opt_rdbms", 
     241        "--database=$opt_database", 
     242        ($opt_user ? "--user=$opt_user" : ()), 
     243        ($opt_password ? "--password=$opt_password" : ()), 
     244    ); 
     245    if ($orig_from_id != $opt_from_id) { 
     246        my @opts = ( 
     247            "--host=$orig_node->[0]->{host}", 
     248            "--port=" . ($orig_node->[0]->{port} || $DEFAULT_PORT{$opt_rdbms}), 
     249        ); 
     250        print "INFO:   ", join(' ', @incline_cmd, @opts), " drop-trigger\n"; 
     251        system(@incline_cmd, @opts, 'drop-trigger') == 0 
     252            or die "failed to drop incline triggers from the original node:$?"; 
     253        print "INFO:   ", join(' ', @incline_cmd, @opts), " create-trigger\n"; 
     254        system(@incline_cmd, @opts, 'create-trigger') == 0 
     255            or die "failed to install incline triggers to the original node:$?"; 
     256    } 
     257    my @opts = ( 
     258        "--host=$opt_new_host", 
     259        "--port=" . ($opt_new_port || $DEFAULT_PORT{$opt_rdbms}), 
     260    ); 
     261    print "INFO:   ", join(' ', @incline_cmd, @opts), " drop-trigger\n"; 
     262    system(@incline_cmd, @opts, 'drop-trigger') == 0 
     263        or die "failed to drop incline triggers from the new node:$?"; 
     264    print "INFO:   ", join(' ', @incline_cmd, @opts), " create-trigger\n"; 
     265    system(@incline_cmd, @opts, 'create-trigger') == 0 
     266        or die "failed to install incline triggers to the new node:$?"; 
     267} 
     268 
    209269# create new user on both nodes 
    210 my $new_username = "pac" . time; 
    211270print "INFO: creating user '$new_username' on the original node\n"; 
    212271$orig_dbh->do( 
     
    221280 
    222281# update shard definition 
    223 $orig_node->[0]->{username} = $new_username; 
    224 $shard_def->{map}->{$opt_from_id} = [ 
    225     { 
    226         %{$orig_node->[0]}, 
    227         host => $opt_new_host, 
    228         ($opt_new_port ? (port => $opt_new_port) : ()), 
    229     }, 
    230 ]; 
     282$shard_def = updated_shard_def(); 
    231283 
    232284{ # link current shard def as a backup 
     
    242294    my $fh = IO::File::AtomicChange->new($opt_shard_def, 'w') 
    243295        or die "failed to open file:$opt_shard_def:$!"; 
    244     $fh->print(to_json($shard_def, { utf8 => 1 })); 
     296    $fh->print(to_json($shard_def, { utf8 => 1, pretty => 1 })); 
    245297    $fh->close; 
    246298}