2011-02-23 20:48:06 +00:00
#!/usr/bin/perl -w
# (c) 2010 Thomas Schoebel-Theuer / 1&1 Internet AG
use strict;
use English;
use warnings;
2012-12-25 21:41:38 +00:00
umask 0077;
##################################################################
# messaging
2013-05-03 21:11:15 +00:00
my $error_count = 0;
2012-12-25 21:41:38 +00:00
my $notify = "";
sub lprint {
my ($text) = @_;
2013-05-03 21:11:15 +00:00
print $text;
2012-12-25 21:41:38 +00:00
if ($notify) {
2013-05-03 21:11:15 +00:00
system("/usr/bin/logger -t marsadm \"$notify $text\"");
2012-12-25 21:41:38 +00:00
}
}
2014-01-20 12:31:21 +00:00
sub lprint_stderr {
my ($text) = @_;
print STDERR $text;
if ($notify) {
system("/usr/bin/logger -t marsadm \"$notify $text\"");
}
}
2012-12-25 21:41:38 +00:00
sub ldie {
my ($text) = @_;
2013-05-03 21:11:15 +00:00
$error_count++;
2014-01-20 12:31:21 +00:00
lprint_stderr "DYING: $text";
2013-05-03 21:11:15 +00:00
die "\n";
2012-12-25 21:41:38 +00:00
}
sub lwarn {
my ($text) = @_;
2014-01-20 12:31:21 +00:00
lprint_stderr "WARNING: $text";
2012-12-25 21:41:38 +00:00
}
##################################################################
2014-03-12 11:44:52 +00:00
# global variables
my $Id = '$Id$ ';
my $user_version = 0.1;
my $mars = "/mars";
my $host = `uname -n` or ldie "cannot determine my network node name\n";
chomp $host;
my $real_host = $host;
my $force = 0;
my $timeout = -1;
my $ip = "";
my $kernel_version = 0;
##################################################################
2014-02-12 11:41:18 +00:00
# path correction
sub correct_path {
my ($path) = @_;
# actual switches
$path =~ s:(/is-attach)[a-z]*$:$1ed:;
$path =~ s:(/is-)(fetch)[a-z]*$:$1copy:;
$path =~ s:(/is-)(apply)[a-z]*$:$1replay:;
$path =~ s:(/is-(copy|replay|sync))[a-z]*$:$1ing:;
# todo switches
$path =~ s:(/fetch)[a-z]*$:/connect:;
$path =~ s:(/apply)[a-z]*$:/allow-replay:;
$path =~ s:(/replay)[a-z]*$:/allow-replay:;
return $path;
}
##################################################################
2013-04-19 08:52:54 +00:00
# low-level infrastructure
2013-04-28 20:52:58 +00:00
my @link_list = ();
my %link_hash;
my $verbose = 0;
2014-01-27 10:48:09 +00:00
my $dry_run = 0;
2013-04-28 20:52:58 +00:00
2013-04-19 08:52:54 +00:00
sub get_link {
my ($path, $unchecked) = @_;
my $result = readlink($path);
if (!defined($result)) {
ldie "cannot read symlink '$path'\n" unless $unchecked;
lwarn "cannot read symlink '$path'\n" if $unchecked == 1;
}
return $result;
}
2013-12-25 16:08:29 +00:00
sub is_link_recent {
my ($path) = @_;
my @stat = lstat($path);
return 0 if (!@stat);
return 1 if $stat[9] + 15 >= mars_time();
return 0;
}
2013-04-28 20:52:58 +00:00
sub to_tmp {
my $path = shift;
$path =~ s:^(.*)/:$1/.tmp.:;
return $path;
}
sub from_tmp {
my $path = shift;
$path =~ s:^(.*)/\.tmp\.:$1/:;
return $path;
}
sub set_link {
my ($src, $dst) = @_;
my $dst_tmp = to_tmp($dst);
unlink($dst_tmp);
symlink($src, $dst_tmp) or ldie "cannot create symlink '$dst' -> '$src'\n";
# the _order_ is important! remove existing intermediate element before re-appanding
if (exists($link_hash{$dst})) {
my @copy = @link_list;
@link_list = ();
foreach my $elem (@copy) {
next if $elem eq $dst;
push @link_list, $elem;
}
}
$link_hash{$dst} = $src;
push @link_list, $dst;
}
sub finish_links {
return unless @link_list;
2013-12-25 18:18:22 +00:00
my $timestamp = mars_time();
lprint "using lamport timestamp $timestamp\n" if $verbose;
2013-04-28 20:52:58 +00:00
while (my $link = shift @link_list) {
my $link_tmp = to_tmp($link);
2013-12-25 18:18:22 +00:00
my $target = readlink($link_tmp);
my $this_timestamp = $timestamp;
# allow overriding of secondaries in partitioned clusters by use of small timestamps
if ($target eq "(none)") {
my @stat = lstat($link);
$this_timestamp = $stat[9] + 1 if @stat;
}
system("touch -h -d \"\@$this_timestamp\" $link_tmp") == 0 or ldie "cannot set mtime on symlink '$link_tmp'\n";
2014-01-27 10:48:09 +00:00
if ($dry_run) {
lprint "DRY_RUN: would create symlink '$link' -> '$target'\n";
unlink($link_tmp);
next;
}
2013-04-28 20:52:58 +00:00
rename($link_tmp, $link) or ldie "cannot finalize symlink '$link'\n";
if ($verbose) {
lprint "created symlink '$link' -> '$target'\n";
}
}
_trigger();
}
2013-04-19 08:52:54 +00:00
##################################################################
2014-03-12 11:44:52 +00:00
# global checks
2011-02-27 14:17:58 +00:00
2014-02-05 09:02:43 +00:00
unless (defined($ARGV[0]) && $ARGV[0] =~ m/cluster|cat/) {
2013-05-07 12:28:18 +00:00
$kernel_version = get_link("$mars/tree-$host", 1);
if ($kernel_version && $user_version != $kernel_version) {
lwarn "kernel_version=$kernel_version user_version=$user_version\n";
if ($user_version < $kernel_version) {
ldie "Sorry, your MARS kernel module uses version $kernel_version, but my $0 userspace version is only $user_version. That cannot work. Please upgrade your userspace scripts!\n";
} elsif (int($user_version) > int($kernel_version) + 1) {
ldie "MAJOR VERSION mismatch : only one major upgrade step is supported.\n";
}
lwarn "using different minor versions is possible, but you should upgrade your kernel module ASAP\n";
2013-04-17 09:03:13 +00:00
}
}
2013-05-11 08:21:25 +00:00
sub get_alive_links {
my $res = shift || "all";
my $alive = shift || "alive";
my $glob = "$mars/$alive-*";
if ($res ne "all") {
$glob = "$mars/$alive-{";
my $count = 0;
2014-01-15 11:22:50 +00:00
foreach my $peer (glob("$mars/resource-$res/data-*")) {
$peer =~ m:/data-(.+):;
2013-05-11 08:21:25 +00:00
$glob .= "," if $count++;
$glob .= $1;
}
$glob .= "}";
}
my %links;
foreach my $path (glob($glob)) {
$path =~ m:/$alive-(.+):;
my $peer = $1;
$links{$peer} = get_link($path);
}
return %links;
}
2011-02-23 20:48:06 +00:00
##################################################################
2013-02-13 10:50:44 +00:00
# timeout handling
2013-04-10 16:53:07 +00:00
#
# return the lamport clock time in nanosecond resolution
# fallback to system time()
#
sub mars_time {
my $lamport_time;
2014-01-22 07:45:02 +00:00
if (open(my $lamport_clock, "<", "/proc/sys/mars/lamport_clock")) {
while (<$lamport_clock>) {
$lamport_time = $1 if /^lamport_now=(.*)/;
}
close($lamport_clock);
2013-04-10 16:53:07 +00:00
}
2014-01-21 15:46:30 +00:00
return $lamport_time || time() . "." . '0' x 9;
2013-04-10 16:53:07 +00:00
}
2013-02-13 10:50:44 +00:00
sub sleep_timeout {
2013-04-16 14:29:50 +00:00
my $sleeptime = shift || 5;
2013-02-13 10:50:44 +00:00
if ($timeout < 0) {
2013-04-16 14:29:50 +00:00
sleep($sleeptime);
2013-02-13 10:50:44 +00:00
return;
}
ldie "Timeout reached. You may retry with --timeout=-1 to ensure waiting until progress is possible.\n" if !$timeout;
my $rest = $timeout;
2013-04-16 14:29:50 +00:00
$rest = $sleeptime if $rest > $sleeptime;
2013-02-13 10:50:44 +00:00
sleep($rest);
$timeout -= $rest;
}
2013-05-14 13:05:31 +00:00
# wait for some condition
sub wait_cond {
my ($cmd, $res, $specific) = @_;
2013-07-03 07:32:42 +00:00
my $is_actual = ($specific =~ s/^(is|has)-//);
2013-05-14 13:05:31 +00:00
my $is_on = !($specific =~ s/-off$//);
$specific =~ s/-on$//;
if ($is_actual) {
2013-07-03 07:32:42 +00:00
if ($specific eq "device") {
check_mars_device($cmd, $res, 1, !$is_on);
return;
}
2013-05-14 13:05:31 +00:00
my %table =
(
"attach" => "is-attached",
2013-07-03 07:32:42 +00:00
"attached" => "is-attached",
2013-07-15 11:44:08 +00:00
"replay" => "is-replaying",
"replaying"=> "is-replaying",
2014-02-12 11:41:18 +00:00
"fetch" => "is-copying",
"fetching" => "is-copying",
2013-07-15 11:44:08 +00:00
"copy" => "is-copying",
"copying" => "is-copying",
"sync" => "is-syncing",
"syncing" => "is-syncing",
2013-05-14 13:05:31 +00:00
"primary" => "is-primary",
);
my $name = $table{$specific};
ldie "actual indicator '$specific' does not exist\n" unless exists($table{$specific});
check_status($cmd, $res, $name, $is_on ? 1 : 0, 1);
} else {
my %table =
(
"attach" => "attach",
2013-07-03 07:32:42 +00:00
"attached" => "attach",
2014-02-12 11:41:18 +00:00
"fetch" => "connect",
2013-05-14 13:05:31 +00:00
"connect" => "connect",
2014-02-12 11:41:18 +00:00
"replay" => "replay",
2013-05-14 13:05:31 +00:00
"sync" => "sync",
);
my $name = $table{$specific};
ldie "button '$specific' does not exist\n" unless exists($table{$specific});
check_todo($cmd, $res, $name, $is_on ? 1 : 0, 1);
}
}
2013-07-03 07:32:42 +00:00
2013-05-11 08:21:25 +00:00
# wait until some communication has occurred
sub wait_cluster {
2013-05-14 13:05:31 +00:00
return wait_cond(@_) if int(@_) >= 3;
2013-05-11 08:21:25 +00:00
my $cmd = shift;
my $res = shift || "all";
my $start_time = mars_time();
_trigger();
my $delta = $timeout > 0 ? $timeout : 30;
while (1) {
my $dead_count = 0;
my $alive_count = 0;
my $unknown_count = 0;
my %status = get_alive_links($res, "time");
my $now = mars_time();
foreach my $peer (keys(%status)) {
next if $peer eq $host;
if ($status{$peer} >= $start_time) {
$alive_count++;
} elsif ($status{$peer} + $delta < $now) {
$dead_count++;
} else {
$unknown_count++;
}
}
if (!$dead_count && !$unknown_count) {
lprint "all $alive_count peer(s) seem to be alive\n";
last;
}
if (!$unknown_count) {
lwarn "$alive_count peer(s) seem to be alive, and $dead_count peer(s) seem to be dead / not reachable\n";
ldie "--force not given\n" unless $force;
last;
}
sleep(1);
}
}
2013-02-13 10:50:44 +00:00
##################################################################
2011-02-23 20:48:06 +00:00
# syntactic checks
sub check_id {
my $str = shift;
2012-12-25 21:41:38 +00:00
ldie "identifier '$str' has disallowed characters" unless $str =~ m/^[A-Za-z_][-A-Za-z0-9_]*$/;
ldie "identifier '$str' is too long (only 16 chars allowed)" if length($str) > 16;
2011-02-23 20:48:06 +00:00
}
2013-11-11 11:24:53 +00:00
sub check_id_list {
my $str = shift;
ldie "comma-separated list '$str' has disallowed characters" unless $str =~ m/^[A-Za-z_][-A-Za-z0-9_,]*$/;
}
2011-02-23 20:48:06 +00:00
##################################################################
# semantic checks
sub check_res {
my $res = shift;
2013-04-03 07:22:32 +00:00
if (not -d "$mars/resource-$res") {
2011-06-10 13:57:52 +00:00
# DO WHAT I MEAN: try to substitute a device name for a badly given resource name if it is unique
my $count = 0;
my $found;
my @tests = glob("$mars/resource-*/device-$host");
foreach my $test (@tests) {
2013-04-19 08:52:54 +00:00
my $target = get_link($test, 2);
2013-04-03 07:22:32 +00:00
if ($target eq $res) {
2011-06-10 13:57:52 +00:00
$found = $test;
$count++;
}
}
2013-04-03 07:22:32 +00:00
if (!$count) {
2011-06-10 13:57:52 +00:00
@tests = glob("$mars/resource-*/_direct-*-$host");
foreach my $test (@tests) {
2013-04-19 08:52:54 +00:00
my $target = get_link($test, 2);
2011-06-10 13:57:52 +00:00
$target =~ s/^.*,//;
2013-04-03 07:22:32 +00:00
if ($target eq $res) {
2011-06-10 13:57:52 +00:00
$found = $test;
$count++;
}
}
}
2012-12-25 21:41:38 +00:00
ldie "resource '$res' does not exist ($count replacements found)\n" unless $count == 1 and $found;
2011-06-10 13:57:52 +00:00
$found =~ s:^.*/resource-(.*)/.*$:$1:;
2012-12-25 21:41:38 +00:00
lwarn "substituting bad resource name '$res' by uniquely matching resource name '$found'\n";
2011-06-10 13:57:52 +00:00
$res = $found;
}
return $res;
2011-02-23 20:48:06 +00:00
}
2013-05-07 14:41:52 +00:00
sub check_sizes {
2014-02-21 06:13:27 +00:00
my ($res, $peer) = @_;
2013-05-07 14:41:52 +00:00
my $logical_size = get_link("$mars/resource-$res/size");
2014-02-21 06:13:27 +00:00
my $physical_size = get_link("$mars/resource-$res/actsize-$peer", 1) || return;
2013-05-07 14:41:52 +00:00
if (defined($physical_size) && $physical_size < $logical_size) {
2014-02-21 06:13:27 +00:00
lwarn "Physical device on host '$peer' has size $physical_size, which is smaller than the logical resource size $logical_size\n";
ldie "This is too dangerous. It cannot work. Fix it!\n" unless $force;
2013-05-07 14:41:52 +00:00
}
}
2011-02-27 14:17:58 +00:00
sub check_res_member {
my $res = shift;
2012-12-25 21:41:38 +00:00
ldie "sorry, I have not yet joined to resource '$res'\n" unless -e "$mars/resource-$res/data-$host";
2013-05-07 14:41:52 +00:00
check_sizes($res, $host);
2011-02-27 14:17:58 +00:00
}
2011-06-17 11:32:38 +00:00
sub check_sync_finished {
2014-02-21 06:13:27 +00:00
my ($res, $peer) = @_;
2013-05-07 14:41:52 +00:00
check_sizes(@_);
2014-02-21 06:13:27 +00:00
my $lnk = "$mars/resource-$res/syncstatus-$peer";
2013-04-03 07:22:32 +00:00
if (lstat($lnk)) {
2013-04-19 08:52:54 +00:00
my $syncstatus = get_link($lnk, 1);
my $size = get_link("$mars/resource-$res/size");
2014-02-21 06:13:27 +00:00
unless ($syncstatus >= $size) {
lwarn "Sync has not yet finished on host '$peer', only $syncstatus / $size bytes are transferred\n";
if ($peer eq $host) {
lwarn "Don't try to make inconsistent host '$host' the new primary!\n";
lwarn "Please wait until sync has finished and all logfile have been replayed.\n";
} else {
lwarn "Changing the primary role during sync is dangerous for data consistency on host '$peer'!\n";
}
ldie "First stop the sync before trying to switch primary!\n" unless $force;
}
2011-06-17 11:32:38 +00:00
}
2014-02-21 06:13:27 +00:00
lprint "OK, it seems that sync has finished on host '$peer'.\n";
2011-06-17 11:32:38 +00:00
}
2011-11-03 11:17:59 +00:00
sub check_primary {
my ($cmd, $res) = @_;
2013-04-03 14:00:50 +00:00
my $lnk = "$mars/resource-$res/actual-$host/is-primary";
2013-04-19 08:52:54 +00:00
my $is_primary = get_link($lnk);
2013-05-10 23:41:53 +00:00
if (!$is_primary) { # give it a second chance
2014-01-30 19:57:46 +00:00
my $name = get_link("$mars/resource-$res/device-$host", 1);
2013-05-10 23:41:53 +00:00
my $dev = "/dev/mars/$name";
$is_primary = 1 if -b $dev;
}
2013-04-03 14:00:50 +00:00
ldie "for operation '$cmd' I need to be primary\n" unless $is_primary;
2013-04-22 09:36:40 +00:00
my $primary = _get_designated_primary($res);
2013-04-19 08:30:34 +00:00
ldie "for operation '$cmd', I also must be the designated primary\n" unless $primary eq $host;
2011-11-03 11:17:59 +00:00
}
sub check_not_primary {
my ($cmd, $res) = @_;
2013-04-03 14:00:50 +00:00
my $lnk = "$mars/resource-$res/actual-$host/is-primary";
2014-02-06 07:53:10 +00:00
my $is_primary = get_link($lnk, 1);
2013-04-03 14:00:50 +00:00
if ($is_primary) {
2014-02-06 07:53:10 +00:00
ldie "operation '$cmd' cannot be executed on primary\n" unless $force;
lwarn "operation '$cmd' is forced on actual primary '$host', THIS IS RISKY\n";
2013-04-03 14:00:50 +00:00
}
2013-04-19 08:30:34 +00:00
# also check whether we intend to become primary
2013-04-22 09:36:40 +00:00
my $primary = _get_designated_primary($res);
2013-04-19 08:30:34 +00:00
ldie "operation '$cmd' cannot be executed on designated primary\n" if $primary eq $host;
2011-11-03 11:17:59 +00:00
}
2011-06-17 11:32:38 +00:00
sub check_primary_gone {
my ($res) = @_;
2013-04-03 07:22:32 +00:00
for (;;) {
2013-04-10 18:36:17 +00:00
my $pri = _get_actual_primary($res);
last if !$pri;
2013-05-04 19:54:12 +00:00
last if $pri eq $host;
2013-04-10 18:36:17 +00:00
lprint "waiting for other primary host ($pri) to disappear....\n";
2013-02-13 10:50:44 +00:00
sleep_timeout();
2011-06-17 11:32:38 +00:00
}
}
2011-10-20 12:27:17 +00:00
sub check_todo {
2013-05-07 08:11:20 +00:00
my ($cmd, $res, $key, $val, $wait, $unchecked, $inv) = @_;
my $path = "$mars/resource-$res/todo-$host/$key";
2014-02-12 11:41:18 +00:00
$path = correct_path($path);
2013-05-07 08:11:20 +00:00
my $link;
2013-04-03 07:22:32 +00:00
for (;;) {
2013-05-07 08:11:20 +00:00
$link = get_link($path, $unchecked);
return unless defined($link);
if (defined($inv) && $inv) {
last if $link != $val;
ldie "cannot execute $cmd: switch '$key' must not have value '$val'\n" if !$wait;
lprint "waiting until switch '$key' leaves the value '$val'....\n";
} else {
last if $link == $val;
ldie "cannot execute $cmd: switch '$key' must have value '$val', but actually has value '$link'\n" if !$wait;
lprint "waiting until switch '$key' reaches the value '$val'....\n";
}
2013-02-13 10:50:44 +00:00
sleep_timeout();
2011-10-20 12:27:17 +00:00
}
2013-05-07 08:11:20 +00:00
lprint "OK, '$path' has acceptable value '$link'\n";
2011-10-20 12:27:17 +00:00
}
2011-08-05 09:26:24 +00:00
sub check_status {
2013-05-07 08:11:20 +00:00
my ($cmd, $res, $key, $val, $wait, $unchecked, $inv) = @_;
my $path = "$mars/resource-$res/actual-$host/$key";
my $link;
2013-04-03 07:22:32 +00:00
for (;;) {
2013-05-07 08:11:20 +00:00
$link = get_link($path, $unchecked);
return unless defined($link);
if (defined($inv) && $inv) {
last if $link != $val;
ldie "cannot execute $cmd: '$path' must not have value '$val'\n" if !$wait;
lprint "waiting until '$key' leaves the value '$val'...\n";
} else {
last if $link == $val;
ldie "cannot execute $cmd: '$path' must have value '$val'\n" if !$wait;
lprint "waiting until '$key' reaches the value '$val'...\n";
}
2013-02-13 10:50:44 +00:00
sleep_timeout();
2011-08-05 09:26:24 +00:00
}
2013-05-07 08:11:20 +00:00
lprint "OK, '$path' has acceptable value '$link'\n";
2011-08-05 09:26:24 +00:00
}
2013-07-03 07:32:42 +00:00
sub check_mars_device {
my ($cmd, $res, $wait, $inv) = @_;
2014-01-30 19:57:46 +00:00
my $name = get_link("$mars/resource-$res/device-$host", $inv);
2013-07-03 07:32:42 +00:00
my $dev = "/dev/mars/$name";
my $backoff = 1;
my $round = 0;
if ($inv) {
while (-b $dev) {
ldie "cannot execute $cmd: device '$dev' has not yet disappeared\n" if !$wait;
lwarn "device '$dev' has not yet disappeared\n";
sleep_timeout($backoff);
# very slowly increasing backoff
if ($backoff < 10 && $round++ > 5) {
$round = 0;
$backoff++;
}
}
lprint "device '$dev' is no longer present\n" unless -b $dev;
return;
}
# !$inv
2014-01-15 10:19:58 +00:00
my $primary = _get_designated_primary($res);
ldie "for operation '$cmd', I should be the designated primary\n" unless $primary eq $host;
2013-07-03 07:32:42 +00:00
while (! -e $dev) {
my $text = get_error_text($cmd, $res);
lprint $text if $text;
ldie "aborting due to errors\n" if $text =~ m/error/mi;
ldie "cannot execute $cmd: device '$dev' not yet present\n" if !$wait;
lprint "device '$dev' not yet present\n";
sleep_timeout($backoff);
# very slowly increasing backoff
if ($backoff < 10 && $round++ > 5) {
$round = 0;
$backoff++;
}
}
lprint "device '$dev' is present\n" if -b $dev;
}
2013-05-14 09:32:17 +00:00
sub check_userspace {
my ($dst) = @_;
if ($dst !~ m:/userspace/:) {
ldie "your path '$dst' must be inside $mars/userspace/ or $mars/resource-*/userspace/\n" unless $force;
lwarn "your path '$dst' is outside $mars/userspace/ or $mars/resource-*/userspace/ and you gave --force, hopefully YOU KNOW WHAT YOU ARE DOING\n";
}
}
2013-05-13 06:40:27 +00:00
##################################################################
2012-01-24 14:12:55 +00:00
2013-05-13 06:40:27 +00:00
# state inspection routines
2012-01-24 14:12:55 +00:00
2012-02-01 14:08:49 +00:00
sub _get_minmax {
my ($res, $glob, $take_symlink) = @_;
my $min = -1;
my $max = -1;
2012-12-25 21:41:38 +00:00
my @paths = glob($glob) or ldie "cannot find '$glob'\n";
2012-02-01 14:08:49 +00:00
foreach my $path (@paths) {
my $nr = $path;
2013-04-03 07:22:32 +00:00
if ($take_symlink) {
2013-04-19 08:52:54 +00:00
$nr = get_link($path);
2012-02-01 14:08:49 +00:00
}
$nr =~ s:^.*[a-z]+-([0-9]+)(-[^/]*)?$:$1:;
$min = $nr if ($nr < $min || $min < 0);
$max = $nr if ($nr > $max || $max < 0);
}
return ($min, $max);
}
sub get_minmax_logfiles {
my ($res) = @_;
return _get_minmax($res, "$mars/resource-$res/log-*", 0);
}
sub get_minmax_versions {
my ($res) = @_;
return _get_minmax($res, "$mars/resource-$res/version-*", 0);
}
sub get_minmax_any {
my ($res) = @_;
return _get_minmax($res, "$mars/resource-$res/{log,version}-*", 0);
}
sub get_minmax_replays {
my ($res) = @_;
return _get_minmax($res, "$mars/resource-$res/replay-*", 1);
}
2014-01-27 11:04:15 +00:00
##################################################################
# versionlink path handling routines
my %visited_pos;
2014-03-01 21:25:47 +00:00
sub _visit {
my ($nr, $peer) = @_;
$nr =~ s:^0*::;
my $visit = "$nr,$peer";
$visited_pos{$visit} = 1;
}
sub _is_visited {
my ($nr, $peer) = @_;
$nr =~ s:^0*::;
my $visit = "$nr,$peer";
return $visited_pos{$visit};
}
2014-01-19 08:17:35 +00:00
sub _parse_pos {
2014-01-27 11:04:15 +00:00
my ($pos, $do_remember) = @_;
2014-03-01 21:25:47 +00:00
$pos =~ m/((?:log|version)-([0-9]+)-([^,]+)(?:,([0-9]+))?)/ or lwarn "cannot parse position info '$pos'\n";
_visit($2, $3) if $do_remember;
2014-01-27 11:04:15 +00:00
return ($1, int($2), $3, defined($4) ? int($4) : -1);
2014-01-19 08:17:35 +00:00
}
sub _get_prev_pos {
2014-03-01 21:25:47 +00:00
my ($basedir, $nr, $peer, $do_remember) = @_;
my $path = sprintf("$basedir/version-%09d-$peer", $nr);
my $vers = get_link($path, 2);
_parse_pos($path, 1) if $do_remember && defined($vers) && $vers;
2014-01-19 08:17:35 +00:00
$vers =~ s/^.*://;
return $vers;
}
sub _get_common_ancestor {
2014-03-06 20:12:26 +00:00
for (;;) {
my ($basedir, $pos1, $host1, $dep1, $pos2, $host2, $dep2) = @_;
my ($p1, $nr1, $from1, $len1) = _parse_pos($pos1, 0);
my ($p2, $nr2, $from2, $len2) = _parse_pos($pos2, 0);
if ($p1 eq $p2) {
# usually no split brain here (only if both path depths are non-zero)
my $split = ($dep1 && $dep2);
if (!$split) { # additionally check the corresponding version links
my $path1 = sprintf("$basedir/version-%09d-$from1", $nr1);
my $path2 = sprintf("$basedir/version-%09d-$from2", $nr2);
if (my $vers1 = get_link($path1) and my $vers2 = get_link($path2)) {
$split = 1 if $vers1 ne $vers2;
}
2014-01-23 09:51:09 +00:00
}
2014-03-06 20:12:26 +00:00
return ($p1, $split);
} elsif ($nr1 > $nr2) {
# just flip arguments
@_ = ($basedir, $pos2, $host2, $dep2, $pos1, $host1, $dep1);
next;
} elsif ($nr1 < $nr2) {
# recursively advance path depth
my $vers2 = _get_prev_pos($basedir, $nr2, $host2);
return ("", -1) if !$vers2;
@_ = ($basedir, $pos1, $host1, $dep1, $vers2, $host2, $dep2 + 1);
next;
} elsif ($from1 ne $from2) {
# split brain is sure now, but continue computing the common split point
my $vers1 = _get_prev_pos($basedir, $nr1, $host1);
return ("", 1) if !$vers1;
my $vers2 = _get_prev_pos($basedir, $nr2, $host2);
return ("", 1) if !$vers2;
my ($res, $split) = _get_common_ancestor($basedir, $vers1, $host1, $dep1 + 1, $vers2, $host2, $dep2 + 1);
return ($res, 1);
} elsif ($len1 < $len2) {
# there may be no split brain (just incomplete replay) depending on path depth
return ($p1, $dep1);
} elsif ($len2 < $len1) {
# dto symmetric
return ($p2, $dep2);
2014-01-23 09:51:09 +00:00
}
2014-03-06 20:12:26 +00:00
lwarn "error in algorithm: $p1, $nr1, $from1, $len1 : $p2, $nr2, $from2, $len2\n";
return ("", -1);
2014-01-19 08:17:35 +00:00
}
}
sub get_common_ancestor {
my ($basedir, $host1, $host2) = @_;
my $repl1 = get_link("$basedir/replay-$host1", 1);
my $repl2 = get_link("$basedir/replay-$host2", 1);
return _get_common_ancestor($basedir, $repl1, $host1, 0, $repl2, $host2, 0);
}
2014-02-26 09:55:06 +00:00
my %detected_splits = ();
2013-12-25 19:49:36 +00:00
sub detect_splitbrain {
my ($res, $do_report) = @_;
2014-02-26 09:55:06 +00:00
# dynamic programming
return $detected_splits{$res} if defined($detected_splits{$res});
2014-01-19 08:17:35 +00:00
my $basedir = "$mars/resource-$res";
2013-12-25 19:49:36 +00:00
my $ok = 1;
2014-01-19 08:17:35 +00:00
my @list = glob("$mars/resource-$res/replay-*");
my @hosts = map { $_ =~ s:.*/replay-::; $_ } @list;
foreach my $host1 (@hosts) {
foreach my $host2 (@hosts) {
next if $host1 ge $host2;
my ($res, $split) = get_common_ancestor($basedir, $host1, $host2);
if ($split) {
$ok = 0;
if ($do_report) {
lwarn "SPLIT BRAIN at '$res' detected: hostA = '$host1', hostB = '$host2'\n";
} else {
return $ok;
}
}
2013-12-25 19:49:36 +00:00
}
}
2014-03-06 07:52:15 +00:00
if ($ok) { # check for duplicate logfiles
my @logs = glob("$mars/resource-$res/log-*");
my $oldnr = -1;
foreach my $path (sort(@logs)) {
$path =~ m:/log-([0-9]+):;
my $nr = $1;
if ($nr == $oldnr) {
$ok = 0;
lwarn "SPLIT BRAIN at '$res' detected: duplicate logfile number $nr\n";
lwarn "hint: first resolve split brain by 'leave-resource' or 'invalidate'\n";
lwarn "hint: if this does not help, try cleanup via 'log-purge-all'\n";
lwarn "hint: if this does not help, try 'log-purge-all --force'\n";
last;
}
$oldnr = $nr;
}
}
2014-02-26 09:55:06 +00:00
$detected_splits{$res} = $ok;
2013-12-25 19:49:36 +00:00
return $ok;
}
2014-01-27 11:04:15 +00:00
sub _mark_path_backward {
2014-03-01 21:25:47 +00:00
my ($basedir, $pos, $peer, $skip_last) = @_;
my $sum = 0;
2014-01-27 11:04:15 +00:00
for (;;) {
my ($p, $nr, $from, $len) = _parse_pos($pos, 1);
2014-03-01 21:25:47 +00:00
_visit($nr, $peer);
$pos = _get_prev_pos($basedir, $nr, $peer, 1);
2014-01-27 11:04:15 +00:00
last if !$pos;
2014-03-01 21:25:47 +00:00
# optionally don't count the last versionlink, pointing into nirvana
if (defined($skip_last) && $skip_last && $nr > 1) {
my ($p, $nr, $from, $len) = _parse_pos($pos, 0);
last if !$p;
my $next = _get_prev_pos($basedir, $nr, $peer, 1);
last if !$next;
}
$sum += $len;
2014-01-27 11:04:15 +00:00
}
2014-03-01 21:25:47 +00:00
return $sum;
2014-01-27 11:04:15 +00:00
}
sub _mark_path_forward {
2014-03-01 21:25:47 +00:00
my ($basedir, $pos, $peer) = @_;
2014-01-27 11:04:15 +00:00
my @list = ($pos);
while (@list) {
my %next_list;
foreach $pos (@list) {
my ($p, $nr, $from, $len) = _parse_pos($pos, 1);
2014-03-01 21:25:47 +00:00
my $cand = sprintf("$basedir/version-%09d-$peer", $nr + 1);
my $vers = get_link($cand, 2);
next unless defined($vers) && $vers ne "";
$vers =~ s/^.*://;
my ($cp, $cnr, $cfrom, $clen) = _parse_pos($vers, 0);
if (int($cnr) == int($nr) && $cfrom eq $from && $clen == $len) {
$next_list{$cand} = 1;
2014-01-27 11:04:15 +00:00
}
}
@list = keys(%next_list);
}
}
sub _mark_path_transitive {
_mark_path_forward(@_);
_mark_path_backward(@_);
}
sub log_purge_res {
my ($cmd, $res) = @_;
2014-03-01 21:25:47 +00:00
lwarn "DANGEROUS OPERATION: $cmd on resource '$res'\n";
my %start_logs;
my $start_count = 0;
2014-01-27 11:04:15 +00:00
my $basedir = "$mars/resource-$res";
2014-03-01 21:25:47 +00:00
foreach my $data (glob("$basedir/{data,replay}-*")) {
$data =~ m:/(data|replay)-(.+):;
my $peer = $2;
my $replay = "$basedir/replay-$peer";
2014-01-27 11:04:15 +00:00
my $target = get_link($replay, 1);
2014-03-01 21:25:47 +00:00
lprint "found replay link '$replay' -> '$target'\n";
$target =~ s/,.*//;
$start_logs{$target}++;
$start_count++;
_mark_path_transitive($basedir, $target, $peer);
}
if (!$start_count) {
ldie "Resource contains no valid information - refusing to delete everything for safety reasons\n";
}
my %logs;
foreach my $file (glob("$basedir/version-*")) {
$file =~ m:/(version-([0-9]+)-([^,]+)): or ldie "bad path '$file'\n";
my $cand = $1;
my $nr = $2;
my $from = $3;
lprint "checking '$cand'\n";
my $vers = get_link($file, 1);
$vers =~ m/(log-[0-9]+-[^,:]+)/;
my $log = $1;
lprint " corresponding logfile is '$log'\n";
$logs{$log}++;
if (_is_visited($nr, $from)) {
lprint " ok '$cand'\n";
next;
}
if (!$force && $from ne $host) {
lprint " skipping foreign object '$cand'\n";
next;
}
lwarn "deleting foreign object from peer '$from' because you said --force\n" if $from ne $host;
2014-01-27 11:04:15 +00:00
_create_delete($file);
}
2014-03-01 21:25:47 +00:00
foreach my $file (glob("$basedir/log-*")) {
$file =~ m:/(log-[0-9]+-(.*)): or ldie "bad path '$file'\n";
my $log = $1;
my $from = $2;
lprint "checking '$log'\n";
if ($logs{$log}) {
lprint " ok '$log'\n";
$logs{$log} = -1;
next;
}
if ($start_logs{$log}) {
lprint " ok start '$log'\n";
$logs{$log} = -1;
next;
}
if (!$force && $from ne $host) {
lprint " skipping foreign object '$log'\n";
next;
}
lwarn "deleting foreign object from peer '$from' because you said --force\n" if $from ne $host;
_create_delete($file);
}
my $count = 0;
foreach my $log (sort(keys(%logs))) {
my $nr = $logs{$log};
next if $nr < 0 || -e "$basedir/$log";
lprint_stderr "info: logfile '$log' is referenced ($nr), but not present.\n";
$count++;
}
if ($count) {
lprint_stderr " Unreferenced logfiles are not necessarily bad.\n";
lprint_stderr " They can regularly appear after 'leave-resource',\n";
lprint_stderr " or 'invalidate', or after emergency mode,\n";
lprint_stderr " or after similar operations.\n";
}
2014-01-27 11:04:15 +00:00
finish_links();
_wait_delete();
}
2013-05-04 19:54:12 +00:00
sub try_to_avoid_splitbrain {
2014-01-21 12:43:43 +00:00
my ($cmd, $res) = @_;
2014-02-26 09:55:06 +00:00
if (!detect_splitbrain($res, 0)) {
lwarn "ATTENTION: you are starting a non-forced primary switchover in a split brain situation.\n";
lwarn "ATTENTION: that's no good idea.\n";
lwarn "ATTENTION: I will continue to do what you want.\n";
lwarn "ATTENTION: But you are responsible for the consequences.\n";
return;
}
# now try to prevent producing a _new_ split brain situation....
2014-01-21 12:43:43 +00:00
my ($min, $max) = get_minmax_versions($res);
my @host_list = glob("$mars/resource-$res/replay-*");
return if scalar(@host_list) < 2;
my $vers_glob = sprintf("$mars/resource-$res/version-%09d-*", $max);
for (;;) {
my $ok = 1;
my @versions = glob($vers_glob);
my $first = get_link(shift @versions);
while (@versions) {
my $next = get_link(shift @versions);
if ($next ne $first) {
$ok = 0;
}
}
last if $ok;
lprint "trying to avoid split brain: logfile update not yet completed.\n";
sleep_timeout();
}
2011-06-17 11:32:38 +00:00
}
2011-02-27 14:17:58 +00:00
sub get_size {
2013-04-25 20:32:37 +00:00
my $arg = shift || "";
if (!($arg =~ m/^([0-9]+(?:\.[0-9]*)?)([kmgtp]?)$/i)) {
2013-04-24 13:38:53 +00:00
ldie "size argument '$arg' must be a number, optionally followed by suffix k or m or g or t or p\n";
}
2013-04-25 20:32:37 +00:00
my $mod = $2 || "";
$arg = $1;
2011-02-23 20:48:06 +00:00
$_ = $mod;
SWITCH: {
/k/i and $arg *= 1024, last SWITCH;
/m/i and $arg *= 1024 * 1024, last SWITCH;
/g/i and $arg *= 1024 * 1024 * 1024, last SWITCH;
/t/i and $arg *= 1024 * 1024 * 1024 * 1024, last SWITCH;
/p/i and $arg *= 1024 * 1024 * 1024 * 1024 * 1024, last SWITCH;
}
2013-04-25 20:32:37 +00:00
ldie "size '$arg' is not a multiple of 4k\n" if ($arg % 4096) != 0;
2011-02-23 20:48:06 +00:00
return $arg;
}
2013-04-10 18:36:17 +00:00
#
# Get actual primary node from links below actual-*/ subdirs
#
sub _get_actual_primary {
my ($res) = @_;
my @primary_links = glob("$mars/resource-$res/actual-*/is-primary");
my $primary;
foreach my $link (@primary_links) {
2013-04-19 08:52:54 +00:00
if (my $val = get_link($link)) {
2013-04-10 18:36:17 +00:00
$primary = ($link =~ qr%.*actual-([^/]+)/is-primary%)[0];
last;
# Note: if there are more than one 'is-primary' links (an insane state anyway),
# the first 'is-primary' link is selected. Other links are ignored.
}
}
return $primary;
}
2013-04-22 09:36:40 +00:00
sub _get_designated_primary {
my ($res) = @_;
return get_link("$mars/resource-$res/primary");
}
2012-02-01 14:08:49 +00:00
sub get_peers {
my ($res) = @_;
2014-01-15 11:22:50 +00:00
my @list = glob("$mars/resource-$res/data-*");
return map { $_ =~ s:$mars/resource-$res/data-::; $_ } @list;
2012-02-01 14:08:49 +00:00
}
2013-05-13 07:05:26 +00:00
sub __conv_tv {
my ($tv_sec, $tv_nsec) = @_;
2014-03-19 07:28:14 +00:00
if (defined($tv_nsec)) {
$tv_nsec = ".$tv_nsec";
} else {
$tv_nsec = "";
}
2014-01-17 07:53:59 +00:00
my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(int($tv_sec));
2014-03-19 07:28:14 +00:00
return "$tv_sec$tv_nsec" unless defined($sec);
return sprintf("%04d-%02d-%02d %02d:%02d:%02d%s", $year+1900, $mon + 1, $mday, $hour, $min, $sec, $tv_nsec);
2013-05-13 07:05:26 +00:00
}
sub _replace_timestamps {
2014-03-19 07:28:14 +00:00
my ($txt, $omit_nsec) = @_;
if (defined($omit_nsec) && $omit_nsec) {
$txt =~ s:([0-9]{9,99})\.([0-9]{9}):__conv_tv($1):ge;
} else {
$txt =~ s:([0-9]{9,99})\.([0-9]{9}):__conv_tv($1,$2):ge;
}
2013-05-13 07:05:26 +00:00
return $txt;
}
sub _get_text {
my ($path, $regex, $do_print) = @_;
open(IN, "<", $path) or return "";
my $text = "";
my $count = 0;
while (my $line = <IN>) {
# use regex e.g. for fetching only errors and warnings
if (!$regex || $line =~ $regex) {
$line = _replace_timestamps($line);
$count++;
if ($do_print) {
print $line;
} else {
$text .= $line;
}
}
}
close(IN);
return $count if $do_print;
return $text;
}
sub get_error_text {
my ($cmd, $res) = @_;
my $text = _get_text("$mars/resource-$res/logstatus-$host.status", "m/^(err|warn)/i", 0);
return $text;
}
2011-02-23 20:48:06 +00:00
##################################################################
2011-02-27 14:17:58 +00:00
# helpers
2011-03-03 09:02:10 +00:00
sub _trigger {
2013-05-11 08:21:25 +00:00
system("(echo 2 > /proc/sys/mars/trigger) >/dev/null 2>&1");
2011-03-03 09:02:10 +00:00
}
2011-02-27 14:17:58 +00:00
sub _switch {
2011-06-10 13:57:52 +00:00
my ($cmd, $res, $path, $on) = @_;
my $src = $on ? "1" : "0";
2014-02-12 11:41:18 +00:00
$path = correct_path($path);
2013-04-19 08:52:54 +00:00
my $old = get_link($path);
2013-04-03 07:22:32 +00:00
if ($old && $old eq $src) {
2012-12-25 21:41:38 +00:00
lprint "${cmd} on resource $res is already activated\n" if $cmd;
2011-06-10 13:57:52 +00:00
return;
}
2013-04-28 20:52:58 +00:00
set_link($src, $path);
2012-12-25 21:41:38 +00:00
lprint "successfully started ${cmd} on resource $res\n" if $cmd;
2011-02-27 14:17:58 +00:00
}
sub _writable {
my ($path, $on) = @_;
my $oldmode = (lstat $path)[2] & 0700;
my $newmode = $on ? $oldmode | 0200 : $oldmode & ~0200;
2012-12-25 21:41:38 +00:00
lprint "chmod '$path' $oldmode $newmode";
chmod($newmode, $path) == 1 or ldie "cannot chmod '$path'\n";
2011-02-27 14:17:58 +00:00
}
2012-02-15 18:37:17 +00:00
sub _get_ip {
2014-02-05 09:02:43 +00:00
my $ip_path = "$mars/ips/ip-$host";
if (my $from_link = get_link($ip_path, 2)) {
lprint_stderr "Using IP '$from_link' from '$ip_path'\n";
return $from_link;
}
chomp (my @info = `/sbin/ip addr show`);
my $interface = "";
foreach my $line (@info) {
$interface = $1 if $line =~ m#^[0-9]+:\s([a-zA-Z_0-9]+):#;
next if $interface eq "lo";
if ($line =~ m#\sinet\s(\d+\.\d+\.\d+\.\d+)#) {
my $from_if = $1;
lprint_stderr "Using IP '$from_if' from interface '$interface'\n";
return $from_if;
}
2012-02-15 18:37:17 +00:00
}
return undef;
}
2012-09-07 10:32:49 +00:00
sub _fake_versionlink {
2013-04-18 08:06:10 +00:00
my ($basedir, $log_nr, $primary) = @_;
2014-03-05 15:10:35 +00:00
my $make_count = 0;
for (my $rounds = 2; $rounds > 0 && $log_nr > 0; $rounds--) {
2014-01-27 14:17:22 +00:00
my $new_version = sprintf("$basedir/version-%09d-$host", $log_nr);
my $pri_version = sprintf("$basedir/version-%09d-$primary", $log_nr);
if ($primary eq $host) {
ldie "Cannot fake my own version link '$new_version'\n";
}
my $pri_link = get_link($pri_version, 1);
if (!$pri_link) {
$log_nr++;
next;
2012-09-12 08:01:34 +00:00
}
2013-04-18 08:06:10 +00:00
lprint "creating new version symlink '$new_version' -> '$pri_link'\n";
2013-04-28 20:52:58 +00:00
set_link($pri_link, $new_version);
2014-03-05 15:10:35 +00:00
$make_count++;
$log_nr--;
2013-04-18 08:06:10 +00:00
}
2014-03-05 15:10:35 +00:00
lwarn "cannot create faked versionlink\n" if !$make_count;
2012-09-07 10:32:49 +00:00
}
sub _set_replaylink {
2014-01-16 10:20:35 +00:00
my ($basedir, $log_nr, $primary, $msg) = @_;
$msg = " -- THIS IS EXTREMELY RISKY -- any inconsistencies are on your own!" unless defined($msg);
2014-01-16 10:13:18 +00:00
ldie "no designated primary defined\n" unless ($primary && $primary ne "(none)");
2013-04-18 08:06:10 +00:00
my $rep_path = "$basedir/replay-$host";
my $rep_val = sprintf("log-%09d-$primary,0,0", $log_nr);
lprint "creating new replaylink '$rep_path' -> '$rep_val'\n";
2013-04-28 20:52:58 +00:00
set_link($rep_val, $rep_path);
2013-04-18 08:06:10 +00:00
if ($log_nr > 1) {
2014-01-16 06:01:06 +00:00
_fake_versionlink($basedir, $log_nr - 1, $primary);
2013-05-13 13:17:20 +00:00
} else {
2013-06-03 11:51:56 +00:00
my $initial;
for (;;) {
$initial = get_link("$basedir/version-000000001-$primary", 1);
last if $initial;
sleep_timeout();
}
2013-05-13 13:17:20 +00:00
set_link($initial, "$basedir/version-000000001-$host");
2012-09-07 10:32:49 +00:00
}
2014-01-16 10:20:35 +00:00
set_link("$log_nr$msg", "$basedir/skip-check-$host");
2012-09-07 10:32:49 +00:00
}
2011-02-27 14:17:58 +00:00
##################################################################
2011-02-23 20:48:06 +00:00
# commands
sub ignore_cmd {
my ($cmd, $res) = @_;
2012-12-25 21:41:38 +00:00
lprint "ignoring command '$cmd' on resource '$res'\n";
2011-02-23 20:48:06 +00:00
}
sub senseless_cmd {
my ($cmd, $res) = @_;
2013-05-03 21:11:15 +00:00
lprint "command '$cmd' makes no sense with MARS (ignoring on resource '$res')\n";
2011-02-23 20:48:06 +00:00
}
sub forbidden_cmd {
my ($cmd, $res) = @_;
2013-05-03 21:11:15 +00:00
ldie "command '$cmd' on resource '$res' cannot be used with MARS (migth affect too many hosts, lead to undesired consequences)\n";
2011-02-23 20:48:06 +00:00
}
sub nyi_cmd {
my ($cmd, $res) = @_;
2013-05-03 21:11:15 +00:00
ldie "command '$cmd' on resource '$res' is not yet implemented\n";
2011-02-23 20:48:06 +00:00
}
2012-08-14 13:42:26 +00:00
sub is_module_loaded {
open TEST, "lsmod | grep mars |";
my $res = <TEST>;
close TEST;
return $res;
}
2013-11-11 11:24:53 +00:00
sub set_connect_pref_list {
my ($cmd, $res, $list) = @_;
check_res_member($res);
my $dst = "$mars/resource-$res/connect-$host";
if ($cmd =~ m/^get-/) {
my $value = get_link($dst);
lprint "$value\n";
return;
}
check_id_list($list);
set_link($list, $dst);
}
2014-01-28 10:58:30 +00:00
sub emergency_limit_res {
my ($cmd, $res, $value) = @_;
my $dst = "$mars/resource-$res/todo-$host/emergency-limit";
if ($cmd =~ m/^get-/) {
my $value = get_link($dst);
lprint "$value\n";
return;
}
ldie "percent argument '$value' isn't numeric\n" unless $value =~ m/^[0-9]+$/;
ldie "percent argument '$value' isn't between 0 and 100%\n" unless ($value >= 0 && $value <= 100);
set_link($value, $dst);
}
2013-05-29 12:01:21 +00:00
sub set_link_cmd {
my $cmd = shift;
for (;;) {
my $src = shift || last;
my $dst = shift || ldie "you did not supply a symlink destination for source '$src'\n";
ldie "symlink target '$dst' is not an absolute path\n" unless $dst =~ m:^/:;
check_userspace($dst);
my $dir = `dirname "$dst"` or ldie "path '$dst' has no dirname\n";
chomp $dir;
ldie "directory '$dir' does not exist\n" unless -d $dir;
set_link($src, $dst);
}
}
2014-02-15 16:48:54 +00:00
sub set_sync_pref_list {
my ($cmd, $list) = @_;
my $todo_dir = "$mars/defaults-$host";
ldie "directory '$todo_dir' does not exist\n" unless -d $todo_dir;
my $dst = "$todo_dir/sync-pref-list";
if ($cmd =~ m/^get-/) {
my $value = get_link($dst);
lprint "$value\n";
return;
}
set_link($list, $dst);
}
sub set_sync_limit_value {
my ($cmd, $value) = @_;
my $todo_dir = "$mars/defaults-$host";
ldie "directory '$todo_dir' does not exist\n" unless -d $todo_dir;
my $dst = "$todo_dir/sync-limit";
if ($cmd =~ m/^get-/) {
my $value = get_link($dst);
lprint "$value\n";
return;
}
set_link($value, $dst);
}
2011-07-05 07:07:06 +00:00
sub _create_cluster {
my ($cmd) = @_;
2011-03-23 08:09:00 +00:00
system("mkdir $mars") unless -d $mars;
2013-05-13 07:45:35 +00:00
my $old_uuid = get_link("$mars/uuid", 2);
if ($cmd eq "create-cluster") {
ldie "cluster was already created with uuid='$old_uuid'\n" if $old_uuid && !$force;
my $uuid = `echo -n \$(hostname) \$(date)`;
set_link($uuid, "$mars/uuid");
finish_links(); # opportunity for errors => don't continue
} elsif (!$old_uuid && !$force) {
if ($user_version == 0.1) {
my $uuid = `echo -n \$(hostname) \$(date)`;
set_link($uuid, "$mars/uuid");
}
ldie "cluster has no uuid\n" if $user_version > 0.1;
}
2011-03-23 08:09:00 +00:00
system("mkdir $mars/ips") unless -d "$mars/ips";
2012-03-06 14:01:54 +00:00
system("mkdir $mars/userspace") unless -d "$mars/userspace";
2011-03-23 08:09:00 +00:00
system("mkdir $mars/defaults") unless -d "$mars/defaults";
system("mkdir $mars/defaults-$host") unless -d "$mars/defaults-$host";
2014-02-15 16:48:54 +00:00
set_link("0", "$mars/defaults-$host/sync-limit");
set_link("(none)", "$mars/defaults-$host/sync-pref-list");
2011-10-24 10:54:15 +00:00
system("mkdir $mars/todo-global") unless -d "$mars/todo-global";
2014-02-06 14:03:46 +00:00
mkdir("$mars/actual-$host") unless -d "$mars/actual-$host";
2013-04-28 20:52:58 +00:00
set_link($ip, "$mars/ips/ip-$host");
set_link("1", "$mars/todo-global/deleted-$host");
2011-03-23 08:09:00 +00:00
}
2011-07-05 07:07:06 +00:00
sub create_cluster {
2012-01-25 15:47:58 +00:00
my ($cmd, $peer) = @_;
2012-12-25 21:41:38 +00:00
ldie "cluster is already created\n" if !$force && -d "$mars/ips";
ldie "mars module is loaded, please unload first\n" if is_module_loaded();
2011-07-05 07:07:06 +00:00
_create_cluster(@_);
2011-03-23 08:09:00 +00:00
}
2011-07-05 07:07:06 +00:00
sub join_cluster {
2012-01-25 15:47:58 +00:00
my ($cmd, $peer) = @_;
2013-04-03 07:22:32 +00:00
if (glob("$mars/resource-*") or glob("$mars/ips/*")) {
2012-12-25 21:41:38 +00:00
ldie "Sorry, some resources already exist!\nThis is dangerous!\nIf you are sure that no resource clash is possible, re-invoke this command with '--force' option\n" unless $force;
2011-02-23 20:48:06 +00:00
}
2012-12-25 21:41:38 +00:00
ldie "mars module is loaded, please unload first\n" if is_module_loaded();
lprint "joining cluster via rsync (peer='$peer')\n";
2012-02-01 14:08:49 +00:00
# check connection
2012-12-25 21:41:38 +00:00
system("ssh $peer uname -a") == 0 or ldie "oops, no connection to $peer ...\n";
2013-05-13 09:58:55 +00:00
mkdir($mars) unless -d $mars;
2014-02-04 08:07:23 +00:00
unless ($dry_run) {
system("rsync --recursive --links --max-size=1 -v $peer:$mars/ $mars/") == 0 or ldie "cannot get remote symlink tree via rsync\n";
}
2011-07-05 07:07:06 +00:00
_create_cluster(@_);
2013-04-28 20:52:58 +00:00
finish_links();
2014-02-04 08:07:23 +00:00
unless ($dry_run) {
system("rsync --recursive --links -v $mars/ips/ $peer:$mars/ips/") == 0 or ldie "oops\n";
}
2011-02-23 20:48:06 +00:00
}
2012-08-08 15:05:16 +00:00
sub leave_cluster {
my ($cmd) = @_;
my $check = "/mars/resource-*/*-$host";
2012-12-25 21:41:38 +00:00
ldie "I am member of some resources\n" if glob($check) && !$force;
2012-08-08 15:05:16 +00:00
_create_delete("$mars/ips/$host");
}
2011-02-23 20:48:06 +00:00
sub create_res {
2013-05-13 10:25:51 +00:00
my ($cmd, $res, $dev, $appear, $size_arg) = @_;
2011-02-23 20:48:06 +00:00
my $create = ($cmd eq "create-resource");
2012-12-25 21:41:38 +00:00
ldie "undefined device or size argument\n" unless $dev;
2011-06-30 13:15:52 +00:00
$appear = $res if !$appear;
2011-02-23 20:48:06 +00:00
check_id($appear) if $appear;
2013-04-22 13:05:41 +00:00
my $resdir = "$mars/resource-$res";
2013-04-03 07:22:32 +00:00
if ($create) {
2014-01-30 06:09:03 +00:00
if (-d $resdir) {
lwarn "resource directory '$res' already exists\n";
my @host_list = glob("$resdir/replay-*");
if (@host_list) {
my $h_list = join(',', map({ $_ =~ s:.*/replay-::;} (@host_list)));
lwarn "DANGER: hosts '$h_list' are already member of resource '$res'.\n";
ldie "REFUSING to trash your resource!\n" unless $force;
}
}
2012-12-25 21:41:38 +00:00
lprint "creating new resource '$res'\n";
2011-02-23 20:48:06 +00:00
} else {
2014-01-15 11:22:50 +00:00
if ( -e "$resdir/data-$host") {
2013-04-22 13:05:41 +00:00
lwarn "resource '$res' has been already joined -- this is dangerous!\n";
ldie "refusing dangerous operation\n" unless $force;
} else {
lprint "joining to existing resource '$res'\n";
}
2011-02-23 20:48:06 +00:00
}
2013-04-22 13:05:41 +00:00
my $size = 0;
if (-b $dev) {
2012-12-25 21:41:38 +00:00
ldie "block device '$dev' must be an absolute path starting with '/'\n" unless $dev =~ m/^\//;
2013-07-18 12:48:47 +00:00
use Fcntl 'SEEK_END', 'O_RDONLY', 'O_RDWR', 'O_EXCL';
my $flags = O_RDWR | O_EXCL;
2013-07-25 07:12:46 +00:00
if ($force) {
2013-07-18 12:48:47 +00:00
$flags = O_RDONLY;
}
sysopen(TEST, $dev, $flags) or ldie "cannot open device '$dev' for exclusive rw access\n";
2011-02-27 14:17:58 +00:00
$size = sysseek(TEST, 0, SEEK_END);
close(TEST);
2013-04-22 13:05:41 +00:00
lprint "block device '$dev': determined size = $size bytes\n";
2013-05-13 10:25:51 +00:00
if ($size_arg) {
my $new_size = get_size($size_arg);
ldie "size argument '$size_arg' is smaller than device size '$size'\n" unless $new_size <= $size;
lprint "reducing size from $size to $new_size\n";
$size = $new_size;
}
2013-04-22 13:05:41 +00:00
} else {
$size = get_size($dev);
if ($size > 0) {
$dev = "";
} else {
ldie "bad parameter '$dev': must be either a block device name, or size followed by k or m or g or t\n";
}
2011-02-23 20:48:06 +00:00
}
2013-04-22 13:05:41 +00:00
ldie "implausible size $size" unless $size > 4096 * 16; # smaller floppies should not exist ;)
2011-02-23 20:48:06 +00:00
my $primary;
2013-04-18 08:06:10 +00:00
my $replay_nr = -1;
2013-04-03 07:22:32 +00:00
if ($create) {
2013-04-22 13:05:41 +00:00
mkdir($resdir);
ldie "could not create resource '$res'\n" unless -d $resdir;
set_link($size, "$resdir/size");
} else { # join
ldie "resource '$res' does not exist\n" unless -d $resdir;
my $res_size = get_link("$mars/resource-$res/size");
if ($size < $res_size) {
lwarn "size of new device is only $size, but should be $res_size\n";
ldie "refusing to join due to bad size\n" unless $force;
} elsif ($size > $res_size) {
lprint "Your physical device has size $size, which is larger than the logical resource size $res_size.\n";
lprint "This does no harm, but you are wasting some space.\n";
}
2013-04-22 09:36:40 +00:00
$primary = _get_designated_primary($res);
2014-01-15 16:12:13 +00:00
ldie "Sorry, joining is only possible if a designated primary exists.\n" if $primary eq "(none)";
2013-04-22 13:05:41 +00:00
ldie "implausible state: I ($host) am already designated primary of resource '$res' which I just wanted to join\n" if $primary eq $host;
2012-12-25 21:41:38 +00:00
ldie "my ip '$ip' is not registered -- please run 'join-cluster' first\n" unless -l "$mars/ips/ip-$host";
2013-04-22 13:05:41 +00:00
my $replay = get_link("$resdir/replay-$primary");
2013-04-18 08:06:10 +00:00
if ($replay =~ m/^log-([0-9]+)-/) {
$replay_nr = $1;
} else {
ldie "cannot determine current logfile number.\n";
}
2011-02-23 20:48:06 +00:00
}
2013-04-22 13:05:41 +00:00
# check for uniqeness of $appear
if ($appear) {
foreach my $old_dev (glob("$mars/resource-*/device-$host")) {
$old_dev =~ m:/resource-([^/]+)/:;
next unless defined($1);
my $old_res = $1;
next if $old_res eq $res;
my $old_name = get_link($old_dev);
if ($old_name eq $appear) {
if ( -e "$mars/resource-$old_res/data-$host") {
ldie "device '/dev/mars/$old_name' is already present in joined resource '$old_res'\n";
} else {
lwarn "device '/dev/mars/$old_name' is already present in another unjoined resource '$old_res' -- this does no harm, but may be confusing.\n";
}
}
}
# warn if devices are named differently throughout the cluster
foreach my $old_dev (glob("$resdir/device-*")) {
my $old_name = get_link($old_dev);
if ($old_name ne $appear) {
$old_dev =~ m:/device-(.+)$:;
my $old_host = $1;
lwarn "your name '/dev/mars/$appear' differs from '/dev/mars/$old_name' on host '$old_host'.";
lwarn "this does no harm, but may be confusing.";
}
}
}
my $file = "$resdir/data-$host";
2013-04-03 07:22:32 +00:00
if (!$dev) {
2013-04-22 13:05:41 +00:00
lwarn "file '$file' already exists - reusing\n" if -e $file;
lprint "setup sparse file '$file' with size $size\n";
open(OUT, ">>", $file) or ldie "could not open '$file'\n";
truncate(OUT, $size) or ldie "truncate to size $size failed\n";
2011-02-23 20:48:06 +00:00
close OUT;
} else {
2012-12-25 21:41:38 +00:00
lprint "using existing device '$dev'\n";
2013-04-28 20:52:58 +00:00
set_link($dev, $file);
2011-02-23 20:48:06 +00:00
}
2013-04-22 13:05:41 +00:00
2013-04-03 07:22:32 +00:00
if ($appear) {
2012-12-25 21:41:38 +00:00
lprint "resource '$res' will appear as local device '/dev/mars/$appear'\n";
2013-04-22 13:05:41 +00:00
set_link($appear, "$resdir/device-$host");
2011-02-23 20:48:06 +00:00
}
2011-03-23 08:09:00 +00:00
2013-04-22 13:05:41 +00:00
mkdir("$resdir/userspace") unless -d "$resdir/userspace";
mkdir("$resdir/defaults") unless -d "$resdir/defaults";
mkdir("$resdir/defaults-$host");
mkdir("$resdir/local-$host");
mkdir("$resdir/actual-$host");
my $todo = "$resdir/todo-$host";
2011-09-21 11:30:11 +00:00
mkdir($todo);
2013-04-28 20:52:58 +00:00
set_link("1", "$todo/attach");
set_link("1", "$todo/connect");
set_link("1", "$todo/sync");
set_link("1", "$todo/allow-replay");
2013-04-22 13:05:41 +00:00
unlink("$resdir/syncstatus-$host");
2011-03-23 08:09:00 +00:00
2013-04-03 07:22:32 +00:00
if ($create) {
2013-04-22 13:05:41 +00:00
set_link($host, "$resdir/primary");
set_link($size, "$resdir/syncstatus-$host");
2014-01-30 06:29:20 +00:00
my $startnr = get_link("$resdir/maxnr", 2);
2014-03-01 19:37:41 +00:00
if (defined($startnr) && $startnr ne "" && $startnr > 0) {
2014-01-30 06:29:20 +00:00
$startnr += 1000;
} else {
$startnr = 1;
}
my $fmt = sprintf("%09d", $startnr);
set_link("log-$fmt-$host,0,0", "$resdir/replay-$host");
2014-02-04 08:07:23 +00:00
system("touch $resdir/log-$fmt-$host") unless $dry_run;
2014-01-30 06:29:20 +00:00
set_link("00000000000000000000000000000000,log-$fmt-$host,0:", "$resdir/version-$fmt-$host");
set_link("$startnr", "$resdir/maxnr");
2013-04-28 20:52:58 +00:00
finish_links();
2012-12-25 21:41:38 +00:00
lprint "successfully created resource '$res'\n";
2011-02-23 20:48:06 +00:00
} else {
2014-01-16 10:20:35 +00:00
_set_replaylink($resdir, $replay_nr, $primary, "");
2013-04-22 13:05:41 +00:00
set_link("0", "$resdir/syncstatus-$host");
2013-04-28 20:52:58 +00:00
finish_links();
2012-12-25 21:41:38 +00:00
lprint "successfully joined resource '$res'\n";
2011-02-23 20:48:06 +00:00
}
}
2013-05-13 09:28:34 +00:00
sub leave_res_phase0 {
2012-08-08 15:05:16 +00:00
my ($cmd, $res) = @_;
2014-02-27 08:44:00 +00:00
check_not_primary(@_) unless $force;
2014-02-06 07:53:10 +00:00
my $errors = 0;
2012-08-08 15:05:16 +00:00
foreach my $tmp (glob("$mars/resource-$res/todo-$host/*")) {
2013-06-27 10:05:25 +00:00
my $status = get_link($tmp, 2);
2014-02-06 07:53:10 +00:00
if ($status) {
lwarn "switch '$tmp' is not off\n";
$errors++;
}
2012-08-08 15:05:16 +00:00
}
2014-02-27 08:44:00 +00:00
foreach my $tmp (glob("$mars/resource-$res/actual-$host/{is-,logfile-}*")) {
my $status = get_link($tmp);
if ($status) {
lwarn "running status '$tmp' is not off\n";
$errors++;
2013-05-13 09:28:34 +00:00
}
2014-02-27 08:44:00 +00:00
}
if (!$force) {
2014-02-06 07:53:10 +00:00
check_status($cmd, $res, "is-attached", 0, 0, 1);
ldie "there were $errors errors.\n" if $errors;
2012-08-08 15:05:16 +00:00
}
2013-05-13 09:28:34 +00:00
}
sub leave_res_phase1 {
my ($cmd, $res) = @_;
_create_delete("$mars/resource-$res/replay-$host");
_create_delete("$mars/resource-$res/data-$host");
_create_delete("$mars/resource-$res/syncstatus-$host");
2014-02-27 08:44:00 +00:00
my $syncpos = "$mars/resource-$res/syncpos-$host";
_create_delete($syncpos) if -e $syncpos;
2013-05-13 09:28:34 +00:00
_create_delete("$mars/resource-$res/device-$host");
2013-06-27 10:05:25 +00:00
_create_delete("$mars/resource-$res/actsize-$host");
foreach my $dir (glob("$mars/resource-$res/*-$host/")) {
foreach my $tmp (glob("${dir}*")) {
_create_delete($tmp);
}
_create_delete($dir);
}
2014-01-14 16:23:21 +00:00
finish_links();
}
# wait for deletions (avoid races with following commands)
sub leave_res_phase2 {
my ($cmd, $res) = @_;
_wait_delete();
2014-02-27 08:44:00 +00:00
$force = 0; # this would be too dangerous
2014-01-15 18:15:14 +00:00
log_purge_res(@_);
2014-02-27 08:44:00 +00:00
finish_links();
_wait_delete();
2012-08-08 15:05:16 +00:00
}
2014-01-28 13:42:35 +00:00
sub delete_res {
my ($cmd, $res) = @_;
my $basedir = "$mars/resource-$res";
# preconditions
if (! -d $basedir) {
2014-01-30 06:09:03 +00:00
lprint "resource directory '$basedir' does no longer exist.\n";
2014-01-28 13:42:35 +00:00
return;
}
my @host_list = glob("$basedir/replay-*");
my $cnt = scalar(@host_list);
if ($cnt > 0) {
my $h_list = join(',', map({ $_ =~ s:.*/replay-::;} (@host_list)));
ldie "resource '$res' is not empty: first remove the hosts '$h_list' via leave-resource\n" unless $force;
lwarn "BRUTE FORCE resource destruction: '$res' has $cnt members ($h_list) THESE ARE FINALLY TRASHED right now -- you are RESPONSIBLE for any subsequent problems.\n";
}
2014-01-30 06:29:20 +00:00
for my $path (`find $basedir/* | sort -r`) {
2014-01-28 13:42:35 +00:00
chomp $path;
2014-02-11 12:47:08 +00:00
next if $path =~ m:/(maxnr$|\.deleted-):;
2014-01-28 13:42:35 +00:00
_create_delete($path);
}
finish_links();
_wait_delete();
}
2011-07-08 07:02:14 +00:00
sub logrotate_res {
my ($cmd, $res) = @_;
2011-11-03 11:17:59 +00:00
check_primary(@_);
2012-12-25 21:41:38 +00:00
my @paths = glob("$mars/resource-$res/log-*-$host") or ldie "cannot find any logfiles\n";
2011-07-08 07:02:14 +00:00
@paths = sort(@paths);
my $last = pop(@paths);
2013-04-03 07:22:32 +00:00
if (-z $last) {
2012-12-25 21:41:38 +00:00
lprint "an empty logfile '$last' already exists, nothing to do.\n";
2011-07-08 07:02:14 +00:00
return;
}
my $nr = $last;
$nr =~ s/^.*log-([0-9]+)-.+$/$1/;
my $next = sprintf("$mars/resource-$res/log-%09d-$host", $nr + 1);
2012-12-25 21:41:38 +00:00
ldie "logfile '$next' already exists\n" if -e $next;
2014-02-04 08:07:23 +00:00
system("touch $next") unless $dry_run;
2014-01-30 06:29:20 +00:00
my $startnr = get_link("$mars/resource-$res/maxnr", 1);
$startnr = $nr + 1 if ($nr >= $startnr);
set_link("$startnr", "$mars/resource-$res/maxnr");
2011-07-08 07:02:14 +00:00
}
2013-05-12 10:40:49 +00:00
sub _get_deletable_logfiles {
2011-10-24 10:54:15 +00:00
my ($cmd, $res) = @_;
2011-10-24 11:30:51 +00:00
my $min = -1;
2013-05-12 10:40:49 +00:00
my $max = -1;
my @log_paths = glob("$mars/resource-$res/log-*") or ldie "cannot find any logfiles\n";
foreach my $path (@log_paths) {
$path =~ m/\/log-([0-9]+)-/;
my $nr = $1;
$min = $nr if ($nr < $min || $min < 0);
$max = $nr if ($nr > $max || $max < 0);
}
2012-12-25 21:41:38 +00:00
my @paths = glob("$mars/resource-$res/replay-*") or ldie "cannot find any replay symlinks\n";
2011-10-24 10:54:15 +00:00
foreach my $path (@paths) {
2013-04-19 08:52:54 +00:00
my $target = get_link($path);
2013-05-12 10:40:49 +00:00
$target =~ m/^log-([0-9]+)/;
my $nr = $1;
$max = $nr if ($nr < $max || $max < 0);
2011-10-24 10:54:15 +00:00
}
2013-05-12 10:40:49 +00:00
lprint "min deletable logfile number: $min\n";
2013-10-02 15:39:26 +00:00
lprint "min non-deletable logfile number: $max\n";
2013-05-12 10:40:49 +00:00
return ($min, $max);
2011-10-24 10:54:15 +00:00
}
2013-04-28 20:52:58 +00:00
my $delete_nr = -1;
2011-10-24 10:54:15 +00:00
sub _create_delete {
my ($target) = @_;
2013-05-14 09:32:17 +00:00
ldie "cannot delete: '$target' is no absolute path\n" unless $target =~ m:^/:;
2013-04-28 20:52:58 +00:00
if ($delete_nr < 0) { # compute only upon first call
my @paths = glob("$mars/todo-global/delete-*");
foreach my $path (@paths) {
$path =~ m/-([0-9]+)/;
if (defined($1) && $1 > $delete_nr) {
$delete_nr = $1;
}
2013-05-06 06:12:00 +00:00
}
2013-04-28 20:52:58 +00:00
my @paths2 = glob("$mars/todo-global/deleted-*");
foreach my $path (@paths2) {
my $link = get_link($path, 1);
$link =~ m/([0-9]+)/;
if (defined($1) && $1 > $delete_nr) {
$delete_nr = $1;
}
2013-05-06 06:12:00 +00:00
}
2011-10-24 10:54:15 +00:00
}
2014-01-14 14:52:40 +00:00
my $new = sprintf("$mars/todo-global/delete-%09d-$host", ++$delete_nr);
2012-12-25 21:41:38 +00:00
lprint "create symlink $new -> $target\n";
2013-04-28 20:52:58 +00:00
set_link($target, $new);
2011-10-24 10:54:15 +00:00
}
2014-01-14 16:23:21 +00:00
sub _wait_delete {
2014-01-27 10:48:09 +00:00
return if $dry_run;
2014-01-14 16:23:21 +00:00
for (;;) {
2014-02-06 07:53:10 +00:00
my $deleted = get_link("$mars/todo-global/deleted-$real_host");
2014-01-14 16:23:21 +00:00
last if $deleted >= $delete_nr;
lprint "waiting for deletions to apply locally....\n";
sleep_timeout();
}
}
2013-05-14 09:32:17 +00:00
sub delete_file_cmd {
my $cmd = shift;
2014-01-23 07:02:05 +00:00
my $res = shift; # ignore this
2013-05-14 09:32:17 +00:00
foreach my $path (@_) {
check_userspace($path);
_create_delete($path);
}
}
2011-10-24 10:54:15 +00:00
sub logdelete_res {
my ($cmd, $res) = @_;
2012-12-25 21:41:38 +00:00
my @paths = glob("$mars/resource-$res/log-*") or ldie "cannot find any logfiles\n";
2011-10-24 10:54:15 +00:00
@paths = sort(@paths);
2013-05-12 10:40:49 +00:00
my ($min_deletable, $max_deletable) = _get_deletable_logfiles(@_);
if ($min_deletable >= $max_deletable) {
lprint "no logfiles are deletable.\n";
return;
}
if ($cmd ne "log-delete-all") {
$max_deletable = $min_deletable + 1; # delete only the first one
}
2012-02-01 14:54:53 +00:00
2012-09-03 09:29:41 +00:00
my $nr = 0;
for (;;) {
2012-02-01 14:54:53 +00:00
my $first = shift(@paths);
2012-09-03 09:03:01 +00:00
last unless $first;
2012-02-01 14:54:53 +00:00
$nr = $first;
$nr =~ s/^.*log-([0-9]+)-.+$/$1/;
2013-05-12 10:40:49 +00:00
next unless $nr < $max_deletable;
2012-09-03 09:29:41 +00:00
2012-12-25 21:41:38 +00:00
lprint "chosen '$first' for deletion\n";
2012-02-01 14:54:53 +00:00
_create_delete($first);
2012-09-03 09:29:41 +00:00
}
2012-12-25 21:41:38 +00:00
lprint "removing left-over version symlinks...\n";
2012-09-03 09:29:41 +00:00
foreach my $versionlink (glob("$mars/resource-$res/version-*")) {
my $nrv = $versionlink;
$nrv =~ s/^.*\/version-([0-9]+)-.+$/$1/;
2013-04-03 13:36:20 +00:00
# we need at least one more version link than logfiles for consistency checks
2013-05-12 10:40:49 +00:00
next unless $nrv < $max_deletable - 1;
2012-09-03 09:29:41 +00:00
_create_delete($versionlink);
}
2011-10-24 10:54:15 +00:00
}
2013-05-12 13:37:30 +00:00
sub attach_res_phase0 {
my ($cmd, $res) = @_;
return if $force;
my $detach = ($cmd eq "detach");
if ($detach) {
my $device_in_use = get_link("$mars/resource-$res/actual-$host/open-count", 1);
if ($device_in_use) {
my $name = get_link("$mars/resource-$res/device-$host");
my $dev = "/dev/mars/$name";
ldie "device '$dev' is in use\n";
}
}
}
sub attach_res_phase1 {
2011-02-23 20:48:06 +00:00
my ($cmd, $res) = @_;
my $detach = ($cmd eq "detach");
2011-09-21 11:30:11 +00:00
my $path = "$mars/resource-$res/todo-$host/attach";
2011-06-10 13:57:52 +00:00
_switch($cmd, $res, $path, !$detach);
2011-02-23 20:48:06 +00:00
}
2013-05-12 13:37:30 +00:00
sub attach_res_phase2 {
my ($cmd, $res) = @_;
my $detach = ($cmd eq "detach");
check_status($cmd, $res, "is-attached", $detach ? 0 : 1, 1);
if ($detach) {
2013-07-03 07:32:42 +00:00
check_mars_device($cmd, $res, 1, 1);
2014-01-27 09:51:53 +00:00
check_status($cmd, $res, "is-replaying", 0, 1);
check_status($cmd, $res, "is-syncing", 0, 1);
2013-05-12 13:37:30 +00:00
}
}
marsadm: change defaults for *{,-local,-global}
By default, {dis}connect and {pause,resume}-{replay,sync} should
only switch the _local_ buttons. Otherwise, unexpected side-effects
could result at bigger clusters (#nodes >> 2) from a human point of view.
The new behaviour is different from DRBD, but DRBD was (until recently)
only working on _pairs_, so global spreadout was impossible.
Global switching may be requested at any time by appending suffix
"-global", which is just no longer the default in MARS.
If anyone has objections, it is straightforward to change the
defaults again.
2013-05-14 10:01:15 +00:00
sub connect_global_res {
2011-02-23 20:48:06 +00:00
my ($cmd, $res) = @_;
marsadm: change defaults for *{,-local,-global}
By default, {dis}connect and {pause,resume}-{replay,sync} should
only switch the _local_ buttons. Otherwise, unexpected side-effects
could result at bigger clusters (#nodes >> 2) from a human point of view.
The new behaviour is different from DRBD, but DRBD was (until recently)
only working on _pairs_, so global spreadout was impossible.
Global switching may be requested at any time by appending suffix
"-global", which is just no longer the default in MARS.
If anyone has objections, it is straightforward to change the
defaults again.
2013-05-14 10:01:15 +00:00
my $disconnect = ($cmd =~ m/disconnect/);
2011-10-21 12:50:43 +00:00
my @paths = glob("$mars/resource-$res/todo-*/");
2011-07-05 07:07:06 +00:00
for my $path (@paths) {
2011-10-21 12:50:43 +00:00
_switch($cmd, $res, "$path/connect", !$disconnect);
2011-07-05 07:07:06 +00:00
}
}
sub connect_local_res {
my ($cmd, $res) = @_;
marsadm: change defaults for *{,-local,-global}
By default, {dis}connect and {pause,resume}-{replay,sync} should
only switch the _local_ buttons. Otherwise, unexpected side-effects
could result at bigger clusters (#nodes >> 2) from a human point of view.
The new behaviour is different from DRBD, but DRBD was (until recently)
only working on _pairs_, so global spreadout was impossible.
Global switching may be requested at any time by appending suffix
"-global", which is just no longer the default in MARS.
If anyone has objections, it is straightforward to change the
defaults again.
2013-05-14 10:01:15 +00:00
my $disconnect = ($cmd =~ m/disconnect/);
2011-09-21 11:30:11 +00:00
my $path = "$mars/resource-$res/todo-$host/connect";
2011-06-10 13:57:52 +00:00
_switch($cmd, $res, $path, !$disconnect);
2011-02-27 14:17:58 +00:00
}
marsadm: change defaults for *{,-local,-global}
By default, {dis}connect and {pause,resume}-{replay,sync} should
only switch the _local_ buttons. Otherwise, unexpected side-effects
could result at bigger clusters (#nodes >> 2) from a human point of view.
The new behaviour is different from DRBD, but DRBD was (until recently)
only working on _pairs_, so global spreadout was impossible.
Global switching may be requested at any time by appending suffix
"-global", which is just no longer the default in MARS.
If anyone has objections, it is straightforward to change the
defaults again.
2013-05-14 10:01:15 +00:00
sub pause_sync_global_res {
2011-02-27 14:17:58 +00:00
my ($cmd, $res) = @_;
marsadm: change defaults for *{,-local,-global}
By default, {dis}connect and {pause,resume}-{replay,sync} should
only switch the _local_ buttons. Otherwise, unexpected side-effects
could result at bigger clusters (#nodes >> 2) from a human point of view.
The new behaviour is different from DRBD, but DRBD was (until recently)
only working on _pairs_, so global spreadout was impossible.
Global switching may be requested at any time by appending suffix
"-global", which is just no longer the default in MARS.
If anyone has objections, it is straightforward to change the
defaults again.
2013-05-14 10:01:15 +00:00
my $pause = ($cmd =~ m/pause/);
2011-10-21 12:50:43 +00:00
my @paths = glob("$mars/resource-$res/todo-*/");
2011-07-05 07:07:06 +00:00
for my $path (@paths) {
2011-10-21 12:50:43 +00:00
_switch($cmd, $res, "$path/sync", !$pause);
2011-07-05 07:07:06 +00:00
}
}
2011-10-21 12:50:43 +00:00
sub pause_sync_local_res {
2011-07-05 07:07:06 +00:00
my ($cmd, $res) = @_;
marsadm: change defaults for *{,-local,-global}
By default, {dis}connect and {pause,resume}-{replay,sync} should
only switch the _local_ buttons. Otherwise, unexpected side-effects
could result at bigger clusters (#nodes >> 2) from a human point of view.
The new behaviour is different from DRBD, but DRBD was (until recently)
only working on _pairs_, so global spreadout was impossible.
Global switching may be requested at any time by appending suffix
"-global", which is just no longer the default in MARS.
If anyone has objections, it is straightforward to change the
defaults again.
2013-05-14 10:01:15 +00:00
my $pause = ($cmd =~ m/pause/);
2011-09-21 11:30:11 +00:00
my $path = "$mars/resource-$res/todo-$host/sync";
2011-06-10 13:57:52 +00:00
_switch($cmd, $res, $path, !$pause);
2011-02-23 20:48:06 +00:00
}
marsadm: change defaults for *{,-local,-global}
By default, {dis}connect and {pause,resume}-{replay,sync} should
only switch the _local_ buttons. Otherwise, unexpected side-effects
could result at bigger clusters (#nodes >> 2) from a human point of view.
The new behaviour is different from DRBD, but DRBD was (until recently)
only working on _pairs_, so global spreadout was impossible.
Global switching may be requested at any time by appending suffix
"-global", which is just no longer the default in MARS.
If anyone has objections, it is straightforward to change the
defaults again.
2013-05-14 10:01:15 +00:00
sub pause_replay_global_res {
2011-10-21 12:50:43 +00:00
my ($cmd, $res) = @_;
marsadm: change defaults for *{,-local,-global}
By default, {dis}connect and {pause,resume}-{replay,sync} should
only switch the _local_ buttons. Otherwise, unexpected side-effects
could result at bigger clusters (#nodes >> 2) from a human point of view.
The new behaviour is different from DRBD, but DRBD was (until recently)
only working on _pairs_, so global spreadout was impossible.
Global switching may be requested at any time by appending suffix
"-global", which is just no longer the default in MARS.
If anyone has objections, it is straightforward to change the
defaults again.
2013-05-14 10:01:15 +00:00
my $pause = ($cmd =~ m/pause/);
2011-10-21 12:50:43 +00:00
my @paths = glob("$mars/resource-$res/todo-*/");
for my $path (@paths) {
2014-02-12 11:41:18 +00:00
_switch($cmd, $res, "$path/replay", !$pause);
2011-10-21 12:50:43 +00:00
}
}
sub pause_replay_local_res {
my ($cmd, $res) = @_;
marsadm: change defaults for *{,-local,-global}
By default, {dis}connect and {pause,resume}-{replay,sync} should
only switch the _local_ buttons. Otherwise, unexpected side-effects
could result at bigger clusters (#nodes >> 2) from a human point of view.
The new behaviour is different from DRBD, but DRBD was (until recently)
only working on _pairs_, so global spreadout was impossible.
Global switching may be requested at any time by appending suffix
"-global", which is just no longer the default in MARS.
If anyone has objections, it is straightforward to change the
defaults again.
2013-05-14 10:01:15 +00:00
my $pause = ($cmd =~ m/pause/);
2014-02-12 11:41:18 +00:00
my $path = "$mars/resource-$res/todo-$host/replay";
2011-10-21 12:50:43 +00:00
_switch($cmd, $res, $path, !$pause);
}
2013-05-12 13:37:30 +00:00
sub up_res_phase0 {
2011-02-23 20:48:06 +00:00
my ($cmd, $res) = @_;
my $down = ($cmd eq "down");
2013-04-03 07:22:32 +00:00
if ($down) {
2013-05-12 13:37:30 +00:00
attach_res_phase0("detach", $res);
2011-02-23 20:48:06 +00:00
} else {
2013-05-12 13:37:30 +00:00
attach_res_phase0("attach", $res);
}
}
sub up_res_phase1 {
my ($cmd, $res) = @_;
my $down = ($cmd eq "down");
if ($down) {
pause_replay_local_res("pause-replay-local", $res);
pause_sync_local_res("pause-sync-local", $res);
2013-07-29 14:30:10 +00:00
connect_local_res("disconnect", $res);
2013-05-12 13:37:30 +00:00
attach_res_phase1("detach", $res);
} else {
attach_res_phase1("attach", $res);
marsadm: change defaults for *{,-local,-global}
By default, {dis}connect and {pause,resume}-{replay,sync} should
only switch the _local_ buttons. Otherwise, unexpected side-effects
could result at bigger clusters (#nodes >> 2) from a human point of view.
The new behaviour is different from DRBD, but DRBD was (until recently)
only working on _pairs_, so global spreadout was impossible.
Global switching may be requested at any time by appending suffix
"-global", which is just no longer the default in MARS.
If anyone has objections, it is straightforward to change the
defaults again.
2013-05-14 10:01:15 +00:00
connect_local_res("connect", $res);
2013-05-12 13:37:30 +00:00
pause_sync_local_res("resume-sync-local", $res);
pause_replay_local_res("resume-replay-local", $res);
}
}
sub up_res_phase2 {
my ($cmd, $res) = @_;
my $down = ($cmd eq "down");
if ($down) {
attach_res_phase2("detach", $res);
} else {
attach_res_phase2("attach", $res);
2011-02-23 20:48:06 +00:00
}
}
2013-04-18 08:37:14 +00:00
sub set_replay_res {
my ($cmd, $res, $new_nr) = @_;
if (!$new_nr || $new_nr <= 0) {
ldie "you must supply a numeric logfile number as third argument.\n";
}
check_not_primary(@_);
2014-02-12 11:41:18 +00:00
check_todo($cmd, $res, "replay", 0, 0);
2013-04-18 08:37:14 +00:00
my $replaylink = "$mars/resource-$res/replay-$host";
my $old_val = get_link($replaylink);
my $old_nr = $old_val;
$old_nr =~ s/log-([0-9]+)-.*/$1/;
ldie "old log number '$old_nr' is wrong\n" unless $old_nr > 0;
if ($new_nr > $old_nr) {
lwarn "you try to skip logfile numbers from $old_nr to $new_nr, are you sure?\n";
ldie "you would need --force if you really know what you are doing.\n" unless $force;
}
2014-01-16 10:13:18 +00:00
my $primary = _get_designated_primary($res);
_set_replaylink("$mars/resource-$res", $new_nr, $primary);
2013-04-18 08:37:14 +00:00
}
2011-08-05 09:26:24 +00:00
sub fake_local_res {
my ($cmd, $res) = @_;
2011-09-21 11:30:11 +00:00
my $path = "$mars/resource-$res/todo-$host/sync";
2011-08-05 09:26:24 +00:00
_switch($cmd, $res, $path, 0);
#check_status($res, "copy-syncstatus-$host", 0);
2013-04-19 08:52:54 +00:00
my $size = get_link("$mars/resource-$res/size");
2011-08-05 09:26:24 +00:00
my $target = "$mars/resource-$res/syncstatus-$host";
2013-04-28 20:52:58 +00:00
set_link($size, $target);
2011-08-05 09:26:24 +00:00
}
2011-06-17 11:32:38 +00:00
sub _primary_res {
2013-04-16 14:29:50 +00:00
my ($res, $new, $old) = @_;
2013-04-10 18:36:17 +00:00
my $pri = "$mars/resource-$res/primary";
2013-04-28 20:52:58 +00:00
set_link($new, $pri);
2013-04-22 09:36:40 +00:00
lprint "designated primary changed from '$old' to '$new'\n";
2011-06-17 11:32:38 +00:00
}
2013-05-04 19:54:12 +00:00
# check whether primary/secondary switching is possible at all
sub primary_phase0 {
2011-02-23 20:48:06 +00:00
my ($cmd, $res) = @_;
2013-05-04 19:54:12 +00:00
ldie "cannot switch primary: mars kernel module is not loaded\n" unless ($cmd eq "secondary" || -d "/proc/sys/mars");
2013-12-25 16:08:29 +00:00
if ($force) {
lwarn "You can do a '$cmd --force' only in DISCONNECTED state.\n";
check_todo($cmd, $res, "connect", 0, 0);
}
2014-02-21 06:13:27 +00:00
my $old = _get_designated_primary($res);
2013-12-25 16:08:29 +00:00
if ($cmd eq "primary") {
2013-05-04 19:54:12 +00:00
check_sync_finished($res, $host);
2014-02-21 06:13:27 +00:00
# also check that other secondaries won't loose their sync primary
foreach my $peer (glob("$mars/resource-$res/data-*")) {
$peer =~ m:/data-(.+):;
next if ($peer eq $old || $peer eq $host);
check_sync_finished($res, $peer);
}
2013-05-04 19:54:12 +00:00
check_todo($cmd, $res, "attach", 1, 0);
2013-12-25 16:08:29 +00:00
check_todo($cmd, $res, "connect", 1, 0) if !$force;
2014-02-12 11:41:18 +00:00
check_todo($cmd, $res, "replay", 1, 0);
2011-02-23 20:48:06 +00:00
}
2013-05-04 19:54:12 +00:00
return if ($old eq $host and $cmd eq "primary");
return if $old eq "(none)";
2013-12-25 16:08:29 +00:00
my $open_count_path = "$mars/resource-$res/actual-$old/open-count";
my $device_in_use = get_link($open_count_path, 1);
2013-05-04 19:54:12 +00:00
if ($device_in_use) {
my $name = get_link("$mars/resource-$res/device-$old", 1) || "unknown";
lwarn "device '/dev/mars/$name' for resource '$res' is $device_in_use times in use on primary host '$old'\n";
ldie "first you must umount/close the device (on host '$old')\n" unless $force;
2013-12-25 16:08:29 +00:00
lwarn "First you SHOULD umount/close the device (on host '$old'), but you ignore this recommendation by giving the --force option.\n";
if (is_link_recent($open_count_path)) {
lwarn "You are forcing a SPLIT BRAIN via --force right now. Do you know that this is an ERRONEOUS state? Do you really know what you are doing?\n";
} else {
lwarn "You may produce a SPLIT BRAIN via --force because the peer host '$old' is currently not reachable.\n";
}
2013-05-04 19:54:12 +00:00
}
lprint "all preconditions OK for resource '$res'\n";
}
# when necessary, switch to secondary (intermediately)
sub primary_phase1 {
my ($cmd, $res) = @_;
return if ($force and $cmd eq "primary");
my $old = _get_designated_primary($res);
return if ($old eq $host and $cmd eq "primary");
my $new = "(none)";
return if $old eq $new;
2013-04-16 14:29:50 +00:00
_primary_res($res, $new, $old);
2013-05-04 19:54:12 +00:00
}
# when necessary, wait
sub primary_phase2 {
my ($cmd, $res) = @_;
return if $force;
return unless $cmd eq "primary";
check_primary_gone($res);
2014-01-21 12:43:43 +00:00
my $ok = detect_splitbrain($res);
try_to_avoid_splitbrain(@_) if $ok;
2013-05-04 19:54:12 +00:00
}
# when necessary, switch to primary
sub primary_phase3 {
my ($cmd, $res) = @_;
return unless $cmd eq "primary";
my $old = _get_designated_primary($res);
my $new = $host;
_primary_res($res, $new, $old);
}
2013-06-06 13:51:48 +00:00
# wait for device to appear / disappear
2013-05-04 19:54:12 +00:00
sub primary_phase4 {
my ($cmd, $res) = @_;
2013-06-06 13:51:48 +00:00
if($cmd eq "secondary") {
2013-07-03 07:32:42 +00:00
check_mars_device($cmd, $res, 1, 1);
2013-06-06 13:51:48 +00:00
return;
}
2014-01-17 11:43:02 +00:00
my $ok = detect_splitbrain($res, 1);
if (!$ok) {
2014-02-21 06:13:27 +00:00
lwarn "\n";
2014-01-17 11:43:02 +00:00
lwarn "Sorry, in split brain situations I can only set the _designated_\n";
lwarn "primary, but I cannot _guarantee_ that becoming the\n";
2014-02-21 06:13:27 +00:00
lwarn "_actual_ primary is always possible.\n";
2014-01-17 11:43:02 +00:00
lwarn "You SHOULD resolve the split brain ASAP (e.g. by leave-resource\n";
lwarn "or invalidate etc).\n";
2014-02-21 06:13:27 +00:00
lwarn "\n";
lwarn "If you already tried to resolve the split brain manually, but\n";
lwarn "this message does not disappear, the reason could be some\n";
lwarn "hindering left-overs/remains from the former split brain.\n";
lwarn "ONLY in such a case, try log-purge-all --force.\n";
lwarn "\n";
2014-01-17 11:43:02 +00:00
return;
}
2013-07-03 07:32:42 +00:00
check_mars_device($cmd, $res, 1, 0);
2011-02-23 20:48:06 +00:00
}
2013-05-10 22:20:53 +00:00
sub wait_umount_res {
my ($cmd, $res) = @_;
while (1) {
my $sum = 0;
foreach my $path (glob("$mars/resource-$res/actual-*/open-count")) {
$sum += get_link($path);
}
last if !$sum;
lprint "device for resource '$res' is $sum times in use somewhere\n";
sleep_timeout(3);
}
lprint "OK, device for resource '$res' is not in use.\n";
}
2013-05-13 12:28:53 +00:00
sub invalidate_res_phase0 {
2011-02-27 14:17:58 +00:00
my ($cmd, $res) = @_;
2011-11-03 11:17:59 +00:00
check_not_primary(@_);
2014-01-14 16:43:56 +00:00
my $primary = _get_designated_primary($res);
ldie "for operation '$cmd', some other designated primary must exist (currently there is none)\n" if $primary eq "(none)";
2013-07-15 11:44:08 +00:00
}
sub invalidate_res_phase1 {
my ($cmd, $res) = @_;
_switch($cmd, $res, "$mars/resource-$res/todo-$host/sync", 0);
2014-02-12 11:41:18 +00:00
_switch($cmd, $res, "$mars/resource-$res/todo-$host/replay", 0);
2013-07-15 11:44:08 +00:00
}
sub invalidate_res_phase2 {
my ($cmd, $res) = @_;
2013-05-13 12:28:53 +00:00
if (!$force) {
2013-07-15 11:44:08 +00:00
check_status($cmd, $res, "is-syncing", 0, 1);
check_status($cmd, $res, "is-replaying", 0, 1);
2012-09-07 10:32:49 +00:00
}
2013-05-13 12:28:53 +00:00
}
2013-07-15 11:44:08 +00:00
sub invalidate_res_phase3 {
2013-05-13 12:28:53 +00:00
my ($cmd, $res) = @_;
2011-02-27 14:17:58 +00:00
my $dst = "$mars/resource-$res/syncstatus-$host";
2013-04-22 09:36:40 +00:00
my $primary = _get_designated_primary($res);
2013-12-25 18:52:32 +00:00
ldie "Cannot execute 'invalidate' because noone is designated as primary.\n" if (!$primary || $primary eq "(none)");
2013-04-19 08:52:54 +00:00
my $replay = get_link("$mars/resource-$res/replay-$primary");
2013-04-18 08:06:10 +00:00
$replay =~ m/^log-([0-9]+)-/ or ldie "replay link '$replay' is not parsable\n";
my $replay_nr = $1;
2013-05-13 12:28:53 +00:00
set_link("0", $dst);
finish_links(); # opportunity for errors => don't continue
2014-01-16 10:20:35 +00:00
_set_replaylink("$mars/resource-$res", $replay_nr, $primary, "");
2014-02-12 11:41:18 +00:00
_switch($cmd, $res, "$mars/resource-$res/todo-$host/replay", 1);
2013-07-15 11:44:08 +00:00
_switch($cmd, $res, "$mars/resource-$res/todo-$host/sync", 1);
2011-02-27 14:17:58 +00:00
}
2012-08-08 13:50:41 +00:00
sub resize_res {
2013-04-24 13:38:53 +00:00
my ($cmd, $res, $size_arg) = @_;
my $new_size = 0;
if ($size_arg) {
$new_size = get_size($size_arg);
ldie "optional size argument '$new_size' must be numeric and positive\n" unless scalar($new_size) > 0;
lprint "new size: $new_size bytes\n";
}
2012-08-16 15:10:19 +00:00
check_primary(@_);
2012-08-08 13:50:41 +00:00
my @actsizes = glob("$mars/resource-$res/actsize-*");
2012-12-25 21:41:38 +00:00
ldie "resource $res has no actsize-* symlinks\n" unless @actsizes;
2012-08-08 13:50:41 +00:00
my $lnk = "$mars/resource-$res/size";
2013-04-19 08:52:54 +00:00
my $old_size = get_link($lnk);
2012-08-08 13:50:41 +00:00
my $min_size = 0;
foreach my $actsize (@actsizes) {
2013-04-19 08:52:54 +00:00
my $this_size = get_link($actsize);
2012-08-16 12:34:49 +00:00
if (!$min_size || $this_size < $min_size) {
2012-08-08 13:50:41 +00:00
$min_size = $this_size;
}
}
2012-12-25 21:41:38 +00:00
lprint "old_size=$old_size\n";
2013-04-24 13:38:53 +00:00
lprint "min_size=$min_size\n";
$new_size = $min_size if !$new_size;
lprint "new_size=$new_size\n";
ldie "new size $new_size is higher than the minimum size of all volumes $min_size" if $new_size > $min_size; # no override with --force possible
2013-05-07 14:41:52 +00:00
# for now, disallow decreasing until some bugs are fixed
ldie "only increases of the size are possible!\n" if $new_size < $old_size;
2013-04-24 13:38:53 +00:00
ldie "only increases of the size are possible without --force\n" if $new_size <= $old_size && !$force;
2012-08-16 14:41:23 +00:00
foreach my $switch (glob("$mars/resource-$res/todo-*/sync")) {
2013-04-19 08:52:54 +00:00
my $this_switch = get_link($switch);
2012-12-25 21:41:38 +00:00
ldie "sync on '$switch' is switched on -- use marsadm pause-sync to stop\n" unless !$this_switch;
2012-08-16 14:41:23 +00:00
}
2012-08-16 15:10:19 +00:00
my @syncsizes = glob("$mars/resource-$res/syncstatus-$host");
2012-08-16 14:41:23 +00:00
foreach my $syncsize (@syncsizes) {
2013-04-19 08:52:54 +00:00
my $this_size = get_link($syncsize);
2012-12-25 21:41:38 +00:00
ldie "sync on $syncsize has not yet finished: $this_size != $old_size (DANGEROUS FIX: if you know what you are doing, marsadm fake-sync can 'fix' it -- but this may need a full-sync afterwards)\n" unless $this_size == $old_size;
2012-08-16 14:41:23 +00:00
}
2013-06-27 06:14:40 +00:00
foreach my $syncsize (@syncsizes) {
my $this_size = get_link($syncsize);
set_link($new_size, $syncsize);
2012-08-16 14:41:23 +00:00
}
2013-04-28 20:52:58 +00:00
set_link($new_size, $lnk);
2012-08-08 13:50:41 +00:00
}
2011-02-23 20:48:06 +00:00
sub role_cmd {
my ($cmd, $res) = @_;
2013-04-10 18:36:17 +00:00
my $primary = _get_actual_primary($res) || '(none)';
2013-04-22 09:36:40 +00:00
my $todo_primary = _get_designated_primary($res);
2013-04-10 18:36:17 +00:00
my $msg = "I am actually ";
$msg .= ($primary eq $host) ? "primary" : "secondary";
if ($primary eq $todo_primary) {
$msg .= " and $primary is primary" if ($primary ne $host);
}
elsif ($primary ne $todo_primary) {
$todo_primary = "I" if ($todo_primary eq $host);
$msg .= " and $todo_primary should be primary";
2011-02-23 20:48:06 +00:00
}
2013-04-10 18:36:17 +00:00
lprint $msg . "\n";
2011-02-23 20:48:06 +00:00
}
2013-02-12 14:39:49 +00:00
sub mars_state_cmd {
my ($cmd, $res) = @_;
2013-04-10 18:36:17 +00:00
my $primary = _get_actual_primary($res) || '(none)';
2013-04-22 09:36:40 +00:00
my $todo_primary = _get_designated_primary($res);
2013-04-10 18:36:17 +00:00
2013-02-12 14:39:49 +00:00
if ($primary eq $host) {
2013-04-10 18:36:17 +00:00
lprint "is_primary\n";
return;
}
elsif ($todo_primary eq $host) {
lprint "becoming_primary\n";
2013-02-12 14:39:49 +00:00
return;
}
2013-04-10 18:36:17 +00:00
# secondary without ambitions to become primary
2013-04-19 08:52:54 +00:00
my $size = get_link("$mars/resource-$res/size");
my $syncstatus = get_link("$mars/resource-$res/syncstatus-$host");
2013-02-12 14:39:49 +00:00
if ($syncstatus != $size) {
lprint "secondary inconsistent ($syncstatus bytes of $size)\n";
return;
}
2013-04-10 18:36:17 +00:00
2013-02-12 14:39:49 +00:00
if ($primary eq "(none)") {
my $min = 0;
foreach my $path (glob("$mars/resource-$res/log-*")) {
my $nr = $path;
$nr =~ s:^.*[a-z]+-([0-9]+)(-[^/]*)?$:$1:;
if ($nr > $min) {
$primary = $path;
$primary =~ s:^.*/[a-z]+-[0-9]+-([^/]*)$:$1:;
$min = $nr;
}
}
}
2013-04-19 08:52:54 +00:00
my $primary_replay = get_link("$mars/resource-$res/replay-$primary");
my $host_replay = get_link("$mars/resource-$res/replay-$host");
2013-02-12 14:39:49 +00:00
if ($primary_replay eq $host_replay) {
lprint "secondary uptodate\n";
return;
}
lprint "secondary outdated ($host_replay instead of $primary_replay)\n";
}
2013-05-13 07:05:26 +00:00
sub cat_cmd {
my $cmd = shift;
foreach my $path (@_) {
_get_text($path, undef, 1);
}
}
2013-04-08 07:47:44 +00:00
sub mars_info_cmd {
my ($cmd, $res) = @_;
my $info = "$mars/resource-$res/logstatus-$host.status";
2013-05-13 07:05:26 +00:00
cat_cmd($cmd, $info);
2013-04-08 07:47:44 +00:00
}
2013-04-03 07:58:35 +00:00
sub show_cmd {
my ($cmd, $res) = @_;
$res = "*" if !$res || $res eq "all";
2013-04-05 11:04:55 +00:00
my $glob = "$mars/{ips/ip-$host,alive-$host,emergency-$host,rest-space-$host,resource-$res/{device,primary,size,actsize-$host,syncstatus-$host,replay-$host,actual-$host/*,todo-$host/*}}";
2013-04-03 07:58:35 +00:00
foreach my $link (glob($glob)) {
next unless -l $link;
2013-04-19 08:52:54 +00:00
my $res = get_link($link);
2013-04-03 07:58:35 +00:00
my $short = $link;
$short =~ s:^$mars/::;
lprint "$short=$res\n";
}
}
2013-05-13 07:05:26 +00:00
sub show_errors_cmd {
my ($cmd, $res) = @_;
my $text = get_error_text(@_);
if ($text) {
lprint $text;
ldie "resource $res has some (old) problems.\n";
} else {
lprint "no errors/warnings are reported.\n";
}
}
2012-04-10 16:52:25 +00:00
sub helplist {
my $temp;
$temp = shift;
2012-12-25 21:41:38 +00:00
lprint "ERROR: $temp" if ($temp);
lprint "
2013-02-13 10:57:01 +00:00
marsadm [<global_option>] <command> [<resource>] [<option>]
<global_option> =
--force : skip safety checks
use only when you know what you are doing!
--timeout=<seconds> : leave safety checks after timeout with an error
instead of waiting forever
2012-04-10 16:52:25 +00:00
2012-08-08 15:05:16 +00:00
<command> =
cluster : create-cluster join-cluster leave-cluster
resource : create-resource join-resource leave-resource
2012-08-15 14:24:43 +00:00
primary secondary invalidate
2012-08-08 15:05:16 +00:00
up down
replication: connect disconnect connect-local disconnect-local
replay : pause-replay resume-replay pause-replay-local resume-replay-local
sync : pause-sync resume-sync pause-sync-local resume-sync-local fake-sync
logfile : log-rotate log-delete log-delete-all
device : attach detach
internal : help version
2012-04-10 16:52:25 +00:00
2012-08-08 15:05:16 +00:00
<resource> = name of resource or \"all\" for all resources
2012-04-10 16:52:25 +00:00
2012-08-08 15:05:16 +00:00
<option> = special by command
2012-04-10 16:52:25 +00:00
\n";
exit 0;
}
sub version {
2012-12-25 21:41:38 +00:00
lprint "$0 $Id\n";
#lprint "my IP is $ip\n";
2012-04-10 16:52:25 +00:00
exit 0;
}
2012-02-20 14:06:58 +00:00
2011-02-23 20:48:06 +00:00
##################################################################
my %cmd_table =
(
# new keywords
2011-07-05 07:07:06 +00:00
"create-cluster" => \&create_cluster,
"join-cluster" => \&join_cluster,
2012-08-08 15:05:16 +00:00
"leave-cluster" => \&leave_cluster,
2011-02-27 14:17:58 +00:00
"create-resource" => \&create_res,
"join-resource" => \&create_res,
2013-05-13 09:28:34 +00:00
"leave-resource" => [
"check preconditions", \&leave_res_phase0,
"switch state", \&leave_res_phase1,
2014-01-14 16:23:21 +00:00
"wait for deletions", \&leave_res_phase2,
2013-05-13 09:28:34 +00:00
],
2014-01-28 13:42:35 +00:00
"delete-resource" => \&delete_res,
2013-11-11 11:24:53 +00:00
"set-connect-pref-list"=> \&set_connect_pref_list,
"get-connect-pref-list"=> \&set_connect_pref_list,
2011-10-24 10:54:15 +00:00
"log-rotate" => \&logrotate_res,
"log-delete" => \&logdelete_res,
2012-02-01 14:54:53 +00:00
"log-delete-all" => \&logdelete_res,
2014-01-27 11:04:15 +00:00
"log-purge-all" => \&log_purge_res,
2011-08-05 09:26:24 +00:00
"fake-sync" => \&fake_local_res,
2013-05-29 12:01:21 +00:00
"set-link" => \&set_link_cmd,
2014-02-15 16:48:54 +00:00
"set-sync-pref-list"=> \&set_sync_pref_list,
"get-sync-pref-list"=> \&set_sync_pref_list,
"set-sync-limit-value"=> \&set_sync_limit_value,
"get-sync-limit-value"=> \&set_sync_limit_value,
2013-05-14 09:32:17 +00:00
"delete-file" => \&delete_file_cmd,
2014-01-28 10:58:30 +00:00
"set-emergency-limit" => \&emergency_limit_res,
"get-emergency-limit" => \&emergency_limit_res,
"emergency-limit" => \&emergency_limit_res,
2013-05-13 07:05:26 +00:00
"cat" => \&cat_cmd,
2013-04-17 09:33:57 +00:00
"show" => \&show_cmd,
2013-05-13 07:05:26 +00:00
"show-errors" => \&show_errors_cmd,
"show-state" => \&mars_state_cmd,
"mars-state" => \&mars_state_cmd, # deprecated
"show-info" => \&mars_info_cmd,
"mars-info" => \&mars_info_cmd, # deprecated
2013-04-17 09:33:57 +00:00
"pause-replay-local" => \&pause_replay_local_res,
marsadm: change defaults for *{,-local,-global}
By default, {dis}connect and {pause,resume}-{replay,sync} should
only switch the _local_ buttons. Otherwise, unexpected side-effects
could result at bigger clusters (#nodes >> 2) from a human point of view.
The new behaviour is different from DRBD, but DRBD was (until recently)
only working on _pairs_, so global spreadout was impossible.
Global switching may be requested at any time by appending suffix
"-global", which is just no longer the default in MARS.
If anyone has objections, it is straightforward to change the
defaults again.
2013-05-14 10:01:15 +00:00
"pause-replay-global" => \&pause_replay_global_res,
"pause-replay" => \&pause_replay_local_res,
2013-04-17 09:33:57 +00:00
"resume-replay-local" => \&pause_replay_local_res,
marsadm: change defaults for *{,-local,-global}
By default, {dis}connect and {pause,resume}-{replay,sync} should
only switch the _local_ buttons. Otherwise, unexpected side-effects
could result at bigger clusters (#nodes >> 2) from a human point of view.
The new behaviour is different from DRBD, but DRBD was (until recently)
only working on _pairs_, so global spreadout was impossible.
Global switching may be requested at any time by appending suffix
"-global", which is just no longer the default in MARS.
If anyone has objections, it is straightforward to change the
defaults again.
2013-05-14 10:01:15 +00:00
"resume-replay-global"=> \&pause_replay_global_res,
"resume-replay" => \&pause_replay_local_res,
2013-04-18 08:37:14 +00:00
"set-replay" => \&set_replay_res,
2013-05-10 22:20:53 +00:00
"wait-umount" => \&wait_umount_res,
2013-05-11 08:21:25 +00:00
"wait-cluster" => \&wait_cluster,
"wait-resource" => \&wait_cluster,
2011-02-23 20:48:06 +00:00
2013-04-17 09:33:57 +00:00
# compatible keywords (or their derivatives)
2013-05-12 13:37:30 +00:00
"attach" => [
"check preconditions", \&attach_res_phase0,
"switch state", \&attach_res_phase1,
"wait for effect", \&attach_res_phase2,
],
"detach" => [
"check preconditions", \&attach_res_phase0,
"switch state", \&attach_res_phase1,
"wait for effect", \&attach_res_phase2,
],
2011-07-05 07:07:06 +00:00
"connect-local" => \&connect_local_res,
marsadm: change defaults for *{,-local,-global}
By default, {dis}connect and {pause,resume}-{replay,sync} should
only switch the _local_ buttons. Otherwise, unexpected side-effects
could result at bigger clusters (#nodes >> 2) from a human point of view.
The new behaviour is different from DRBD, but DRBD was (until recently)
only working on _pairs_, so global spreadout was impossible.
Global switching may be requested at any time by appending suffix
"-global", which is just no longer the default in MARS.
If anyone has objections, it is straightforward to change the
defaults again.
2013-05-14 10:01:15 +00:00
"connect-global" => \&connect_global_res,
"connect" => \&connect_local_res,
2011-07-05 07:07:06 +00:00
"disconnect-local" => \&connect_local_res,
marsadm: change defaults for *{,-local,-global}
By default, {dis}connect and {pause,resume}-{replay,sync} should
only switch the _local_ buttons. Otherwise, unexpected side-effects
could result at bigger clusters (#nodes >> 2) from a human point of view.
The new behaviour is different from DRBD, but DRBD was (until recently)
only working on _pairs_, so global spreadout was impossible.
Global switching may be requested at any time by appending suffix
"-global", which is just no longer the default in MARS.
If anyone has objections, it is straightforward to change the
defaults again.
2013-05-14 10:01:15 +00:00
"disconnect-global" => \&connect_global_res,
"disconnect" => \&connect_local_res,
2011-02-27 14:17:58 +00:00
"syncer" => \&ignore_cmd,
2013-05-12 13:37:30 +00:00
"up" => [
"check preconditions", \&up_res_phase0,
"switch state", \&up_res_phase1,
"wait for effect", \&up_res_phase2,
],
"down" => [
"check preconditions", \&up_res_phase0,
"switch state", \&up_res_phase1,
"wait for effect", \&up_res_phase2,
],
2013-05-04 19:54:12 +00:00
"primary" => [
"check preconditions", \&primary_phase0,
"leave primary state", \&primary_phase1,
"wait when necessary", \&primary_phase2,
"switch to primary", \&primary_phase3,
"wait for device", \&primary_phase4,
],
"secondary" => [
"check preconditions", \&primary_phase0,
"leave primary state", \&primary_phase1,
2013-06-23 21:39:36 +00:00
"wait for effect", \&primary_phase4,
2013-05-04 19:54:12 +00:00
],
2013-05-13 12:28:53 +00:00
"invalidate" => [
"check preconditions", \&invalidate_res_phase0,
2013-07-15 11:44:08 +00:00
"stop old replay", \&invalidate_res_phase1,
"wait for replay off", \&invalidate_res_phase2,
"force symlinks", \&invalidate_res_phase3,
2013-05-13 12:28:53 +00:00
],
2011-02-23 20:48:06 +00:00
"invalidate-remote" => \&forbidden_cmd,
2012-08-08 13:50:41 +00:00
"resize" => \&resize_res,
2011-02-27 14:17:58 +00:00
"create-md" => \&senseless_cmd,
"get-gi" => \&ignore_cmd,
"show-gi" => \&ignore_cmd,
"dump-md" => \&senseless_cmd,
"outdate" => \&ignore_cmd,
"adjust" => \&ignore_cmd,
2013-06-29 19:08:40 +00:00
"wait-connect" => \&wait_cluster,
2011-02-27 14:17:58 +00:00
"role" => \&role_cmd,
"state" => \&role_cmd,
"cstate" => \&nyi_cmd,
2013-02-12 14:39:49 +00:00
"dstate" => \&nyi_cmd,
2011-02-27 14:17:58 +00:00
"status" => \&nyi_cmd,
"dump" => \&senseless_cmd,
2013-06-29 19:02:46 +00:00
"verify" => \&forbidden_cmd,
2011-10-21 12:50:43 +00:00
"pause-sync-local" => \&pause_sync_local_res,
marsadm: change defaults for *{,-local,-global}
By default, {dis}connect and {pause,resume}-{replay,sync} should
only switch the _local_ buttons. Otherwise, unexpected side-effects
could result at bigger clusters (#nodes >> 2) from a human point of view.
The new behaviour is different from DRBD, but DRBD was (until recently)
only working on _pairs_, so global spreadout was impossible.
Global switching may be requested at any time by appending suffix
"-global", which is just no longer the default in MARS.
If anyone has objections, it is straightforward to change the
defaults again.
2013-05-14 10:01:15 +00:00
"pause-sync-global" => \&pause_sync_global_res,
"pause-sync" => \&pause_sync_local_res,
2011-10-21 12:50:43 +00:00
"resume-sync-local" => \&pause_sync_local_res,
marsadm: change defaults for *{,-local,-global}
By default, {dis}connect and {pause,resume}-{replay,sync} should
only switch the _local_ buttons. Otherwise, unexpected side-effects
could result at bigger clusters (#nodes >> 2) from a human point of view.
The new behaviour is different from DRBD, but DRBD was (until recently)
only working on _pairs_, so global spreadout was impossible.
Global switching may be requested at any time by appending suffix
"-global", which is just no longer the default in MARS.
If anyone has objections, it is straightforward to change the
defaults again.
2013-05-14 10:01:15 +00:00
"resume-sync-global"=> \&pause_sync_global_res,
"resume-sync" => \&pause_sync_local_res,
2011-02-27 14:17:58 +00:00
"new-current-uuid" => \&senseless_cmd,
"hidden-commands" => \&ignore_cmd,
2011-02-23 20:48:06 +00:00
);
2012-01-25 15:47:58 +00:00
my @args;
foreach my $arg (@ARGV) {
2013-04-28 20:52:58 +00:00
if ($arg eq "--force" || $arg eq "-f") {
2012-01-25 15:47:58 +00:00
$force++;
next;
2014-01-27 10:48:09 +00:00
} elsif ($arg eq "--dry-run" || $arg eq "-d") {
$dry_run++;
next;
2013-04-28 20:52:58 +00:00
} elsif ($arg eq "--verbose" || $arg eq "-v") {
$verbose++;
next;
2013-02-13 10:50:44 +00:00
} elsif ($arg =~ s/--timeout\s*=\s*([0-9]+)/$1/) {
$timeout = $arg;
next;
2013-05-14 12:11:29 +00:00
} elsif ($arg =~ s/--host\s*=\s*([-_A-Za-z0-9]+)/$1/) {
ldie "host '$arg' does not exist in /mars/ips/ip-*\n" unless -l "/mars/ips/ip-$arg";
if ($arg ne $host) {
lprint "ATTENTION: acting as if I were host '$arg'\n";
lwarn "some commands require local knowledge not available here.\n";
lwarn "thus something may fail or go wrong - use this at your risk!\n";
$host = $arg;
}
next;
2014-02-05 09:02:43 +00:00
} elsif ($arg =~ s/--ip\s*=\s*([0-9.:\[\]]+)/$1/) {
$ip = $arg;
lprint_stderr "Using IP '$ip' from command line.\n";
next;
2012-01-25 15:47:58 +00:00
}
2013-04-03 07:22:32 +00:00
if ($arg =~ s/^force-//) {
2012-01-25 15:47:58 +00:00
$force++;
}
push @args, $arg;
}
2012-04-10 16:52:25 +00:00
my $cmd = shift @args || helplist "command argument is missing\n";
2012-02-20 14:06:58 +00:00
2014-02-05 09:02:43 +00:00
if (!$ip) {
$ip = _get_ip() or ldie "cannot determine my IP address\n";
}
2012-12-25 21:41:38 +00:00
$notify = "(cmd: $cmd)" unless $cmd eq "version";
2013-04-03 07:22:32 +00:00
if ($cmd =~ m/^help$/ || $cmd =~ m/^h$/) {
2012-04-10 16:52:25 +00:00
helplist;
}
2012-02-20 14:06:58 +00:00
2013-04-03 07:22:32 +00:00
if ($cmd =~ m/^version$/ || $cmd =~ m/^v$/) {
2012-04-10 16:52:25 +00:00
version;
}
2012-02-20 14:06:58 +00:00
2013-05-13 07:05:26 +00:00
ldie "only root may use this tool\n" if $< != 0 && $cmd !~ m/^cat$/; # getpid() seems to be missing in perlfunc
2012-04-10 16:52:25 +00:00
helplist "unknown command $cmd\n" if !exists $cmd_table{$cmd};
2013-05-14 06:36:32 +00:00
if (!(-d $mars) && $cmd !~ m/(create|join)-cluster|cat/) {
ldie "The $mars directory does not exist.\n";
}
2011-07-08 07:02:14 +00:00
my $res = "";
2014-02-15 16:48:54 +00:00
if ($cmd =~ "show") {
2013-04-03 07:58:35 +00:00
$res = shift @args;
2014-02-15 16:48:54 +00:00
} elsif ($cmd =~ m/^set-.*-list$/) {
$res = shift @args || helplist "comma-separated list argument is missing\n";
} elsif ($cmd =~ m/^set-.*-value$/) {
$res = shift @args || helplist "numeric argument is missing\n";
ldie "argument '$res' isn't numeric\n" unless $res =~ m/^[0-9.]+$/;
2014-01-28 10:58:30 +00:00
} elsif (!($cmd =~ m/^(create|leave|wait)-cluster|cat|[a-z]+-file|^[sg]et-/)) {
2012-04-10 16:52:25 +00:00
$res = shift @args || helplist "resource argument is missing\n";
2011-07-08 07:02:14 +00:00
check_id($res);
}
2011-02-23 20:48:06 +00:00
2012-02-20 14:06:58 +00:00
2014-01-21 13:03:08 +00:00
lwarn "Using FORCE option -- hopefully you know what you are doing!\n" if $force;
2012-01-25 15:47:58 +00:00
2014-01-15 09:51:37 +00:00
my %checked_res;
2013-05-03 21:11:15 +00:00
sub do_one_res {
my $func = shift;
my ($cmd, $res) = @_;
2014-02-15 16:48:54 +00:00
if ($cmd =~ m/^cat|-file$|-list$|-link$|-value$/) { # no resource argument
} elsif (!$checked_res{"$cmd$res"}) {
$res = check_res($res) unless (!$res || $cmd =~ m/^(join|create|leave|wait)-cluster|create-resource|show/);
check_res_member($res) unless (!$res || $cmd =~ m/^(join|create|delete)-(cluster|resource)|(leave|wait)-cluster|show/);
2014-01-19 08:17:35 +00:00
detect_splitbrain($res, 1);
2014-01-15 09:51:37 +00:00
$checked_res{"$cmd$res"} = 1;
}
2013-05-03 21:11:15 +00:00
&{$func}(@_);
}
my %skip_res;
sub do_all_res {
my $func = shift;
my $cmd = shift;
my $res = shift || "all";
2014-01-23 07:02:05 +00:00
if ($res eq "all" && $cmd !~ m/show|cat|cluster|set-link|delete-file/) {
2013-05-03 21:11:15 +00:00
ldie "For safety reasons, --force is only allowed on explicitly named resources. Combination of 'all' with --force is disallowed!\n" if $force;
ldie "Cannot combine command '$cmd' with 'all' existing resources - you must explicitly name a single new resource\n" if $cmd =~ m/create|join/;
my $any_success = 0;
my $any_member = 0;
foreach $res (glob("$mars/resource-*")) {
next unless -e "$res/data-$host";
$any_member++;
$res =~ s/^.*\/resource-(.*)$/$1/;
next if defined($skip_res{$res});
lprint "--------- resource $res\n";
eval {
do_one_res($func, $cmd, $res, @_);
1;
} and $any_success = 1 or $skip_res{$res} = 1;
}
if (!$any_success) {
if (!$any_member) {
lprint "I am not member of any resource\n";
return 1;
}
ldie "all resources have errors\n";
}
return !$any_success;
} else {
return do_one_res($func, $cmd, $res, @_);
}
2011-03-02 16:20:36 +00:00
}
2013-05-03 21:11:15 +00:00
my $func = $cmd_table{$cmd};
ldie "unknown command '$cmd'\n" unless $func;
if (ref($func) eq "ARRAY") {
my @list = @$func;
while (@list) {
my $headline = shift @list;
my $memb_func = shift @list;
lprint "---------------------------- $headline:\n";
my $status = do_all_res($memb_func, $cmd, $res, @args);
last if (defined($status) && $status);
finish_links();
2011-03-02 16:20:36 +00:00
}
2013-05-03 21:11:15 +00:00
} elsif (ref($func) eq "CODE") {
do_all_res($func, $cmd, $res, @args);
2011-03-02 16:20:36 +00:00
} else {
2013-05-03 21:11:15 +00:00
ldie "internal error: command table is wrong for '$cmd'";
2011-03-02 16:20:36 +00:00
}
2013-04-28 20:52:58 +00:00
finish_links();
2013-05-03 21:11:15 +00:00
exit($error_count);