From 75fc790a7f56c76f303dc8843bfb96a8b3530848 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=87=AA=E7=94=B1=E4=B8=8D=E8=87=AA=E5=9C=A8?= Date: Fri, 18 Jul 2014 21:31:48 +0800 Subject: [PATCH 1/4] first release Do lock/unlock operations on nodes. --- bin/try_paxos_lock | 113 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 bin/try_paxos_lock diff --git a/bin/try_paxos_lock b/bin/try_paxos_lock new file mode 100644 index 0000000..f54b7a1 --- /dev/null +++ b/bin/try_paxos_lock @@ -0,0 +1,113 @@ +#!/usr/bin/env perl + +# Copyright (C) 2011 DeNA Co.,Ltd. +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +use strict; +use warnings FATAL => 'all'; +use Carp qw(croak); +use MHA::NodeConst; +use MHA::NodeUtil; +use Getopt::Long; +use Pod::Usage; +use Fcntl; + +$| = 1; + +GetOptions( + \my %opt, qw/ + help + version + workdir=s + holder=s + unlock + debug + /, +) or pod2usage(1); + +if ( $opt{help} ) { + pod2usage(0); +} +if ( $opt{version} ) { + print "try_paxos_lock version $MHA::NodeConst::VERSION.\n"; + exit 0; +} + +exit &main(); + +sub main() { + my $exit_code = 1; + my $workdir = $opt{workdir}; + unless ($workdir) { + croak "Paxos lock failed. workdir is not specified.\n"; + } + unless (-d $workdir) { + croak "Paxos lock failed. Work directory does not exists(directory: $workdir).\n"; + } + unless ($opt{holder}) { + croak "Paxos lock failed. holder is not specified.\n"; + } + my $lock_file = "$workdir/paxos_lock"; + unless ($opt{unlock}) { + unless (sysopen(LOCK_FILE, $lock_file, O_CREAT | O_EXCL | O_WRONLY)) { + open(LOCK_FILE, $lock_file); + my $exist_holder = ; + close LOCK_FILE; + if ($exist_holder eq $opt{holder}) { + print "Paxos locked again successfully(holder: $exist_holder).\n"; + $exit_code = 0; + return $exit_code; + } + croak "Paxos lock failed. $lock_file exist(holder: $exist_holder).\n"; + } + print LOCK_FILE $opt{holder}; + close LOCK_FILE; + open(LOCK_FILE, $lock_file) or croak "Read after paxos lock failed.\n"; + my $read_holder = ; + close LOCK_FILE; + if ($read_holder eq $opt{holder}) { + print "Paxos locked first time successfully(holder: $read_holder).\n"; + $exit_code = 0; + return $exit_code; + } + croak "Compare after paxos lock failed, It's strange(holder: $read_holder).\n"; + } else { + unless (-f $lock_file) { + print "Paxos unlocked successfully. Lock file not exists.\n"; + $exit_code = 0; + return $exit_code; + } + my $link_filename="/var/tmp/paxos_lock"; + if (-f $link_filename) { + print "Old link file exists,unlink it.\n"; + unlink $link_filename; + } + link $lock_file,$link_filename; + open(READ_FILE, $lock_file); + my $exist_holder = ; + close READ_FILE; + if ($exist_holder eq $opt{holder}) { + unlink $link_filename; + unlink $lock_file; + print "Paxos unlocked successfully.\n"; + $exit_code = 0; + return $exit_code; + } + unlink $link_filename; + croak "Paxos unlock failed, It's strange(holder: $exist_holder).\n"; + } +} From d5935f5f7b4aa111a19dfc5bb25c61c3d0612411 Mon Sep 17 00:00:00 2001 From: huxingyi Date: Mon, 28 Jul 2014 21:06:00 +0800 Subject: [PATCH 2/4] Append --lease options. --- bin/try_paxos_lock | 51 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/bin/try_paxos_lock b/bin/try_paxos_lock index f54b7a1..d38a9a5 100644 --- a/bin/try_paxos_lock +++ b/bin/try_paxos_lock @@ -34,6 +34,7 @@ GetOptions( version workdir=s holder=s + lease=i unlock debug /, @@ -52,6 +53,7 @@ exit &main(); sub main() { my $exit_code = 1; my $workdir = $opt{workdir}; + my $expired_time = time() + 3600; unless ($workdir) { croak "Paxos lock failed. workdir is not specified.\n"; } @@ -61,30 +63,47 @@ sub main() { unless ($opt{holder}) { croak "Paxos lock failed. holder is not specified.\n"; } + if ($opt{lease}) { + $expired_time = time() + $opt{lease}; + } my $lock_file = "$workdir/paxos_lock"; unless ($opt{unlock}) { unless (sysopen(LOCK_FILE, $lock_file, O_CREAT | O_EXCL | O_WRONLY)) { - open(LOCK_FILE, $lock_file); - my $exist_holder = ; - close LOCK_FILE; - if ($exist_holder eq $opt{holder}) { - print "Paxos locked again successfully(holder: $exist_holder).\n"; - $exit_code = 0; - return $exit_code; + if (sysopen(LOCK_FILE, $lock_file, O_EXCL | O_RDWR)) { + my ($exist_holder, $exist_expired_time) = split(/:/, ); + my ($now) = time(); + if (($exist_holder eq $opt{holder}) && + ($exist_expired_time && (($exist_expired_time + 0) > $now))) { + close LOCK_FILE; + print "Paxos locked again successfully(holder: $exist_holder).\n"; + $exit_code = 0; + return $exit_code; + } + if ($exist_expired_time && (($exist_expired_time + 0) < $now)) { + # fallthrough + } else { + close LOCK_FILE; + croak "Paxos lock failed. $lock_file exist and not expired(holder: $exist_holder expired_time: $exist_expired_time now: $now).\n"; + } + } else { + croak "Paxos lock failed. $lock_file exist and open failed.\n"; } - croak "Paxos lock failed. $lock_file exist(holder: $exist_holder).\n"; } - print LOCK_FILE $opt{holder}; + seek LOCK_FILE,0,0; + truncate LOCK_FILE,0; + print LOCK_FILE "$opt{holder}:$expired_time"; close LOCK_FILE; open(LOCK_FILE, $lock_file) or croak "Read after paxos lock failed.\n"; - my $read_holder = ; + my ($read_holder, $read_expired_time) = split(/:/, ); close LOCK_FILE; - if ($read_holder eq $opt{holder}) { + my ($now) = time(); + if (($read_holder eq $opt{holder}) && + ($read_expired_time && (($read_expired_time + 0) > $now))) { print "Paxos locked first time successfully(holder: $read_holder).\n"; $exit_code = 0; return $exit_code; } - croak "Compare after paxos lock failed, It's strange(holder: $read_holder).\n"; + croak "Compare after paxos lock failed, It's strange(holder: $read_holder read_expired_time: $read_expired_time now: $now).\n"; } else { unless (-f $lock_file) { print "Paxos unlocked successfully. Lock file not exists.\n"; @@ -98,9 +117,11 @@ sub main() { } link $lock_file,$link_filename; open(READ_FILE, $lock_file); - my $exist_holder = ; + my ($exist_holder, $exist_expired_time) = split(/:/, ); close READ_FILE; - if ($exist_holder eq $opt{holder}) { + my ($now) = time(); + if (($exist_holder eq $opt{holder}) || + ($exist_expired_time && (($exist_expired_time + 0) < $now))) { unlink $link_filename; unlink $lock_file; print "Paxos unlocked successfully.\n"; @@ -108,6 +129,6 @@ sub main() { return $exit_code; } unlink $link_filename; - croak "Paxos unlock failed, It's strange(holder: $exist_holder).\n"; + croak "Paxos unlock failed, It's strange(holder: $exist_holder expired: $exist_expired_time now: $now).\n"; } } From bdf08fba7ac833a166a9cbbcd05366fc50d45730 Mon Sep 17 00:00:00 2001 From: huxingyi Date: Tue, 12 Aug 2014 10:51:00 +0800 Subject: [PATCH 3/4] change the default lease from 3600secs to 86400secs. --- bin/try_paxos_lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/try_paxos_lock b/bin/try_paxos_lock index d38a9a5..57599b5 100644 --- a/bin/try_paxos_lock +++ b/bin/try_paxos_lock @@ -53,7 +53,7 @@ exit &main(); sub main() { my $exit_code = 1; my $workdir = $opt{workdir}; - my $expired_time = time() + 3600; + my $expired_time = time() + 86400; unless ($workdir) { croak "Paxos lock failed. workdir is not specified.\n"; } From 69b5f941cdbb9cb0797e5cd4c36eba5e4fadc8b0 Mon Sep 17 00:00:00 2001 From: huxingyi Date: Tue, 12 Aug 2014 15:06:47 +0800 Subject: [PATCH 4/4] follow the convention coding style. --- bin/try_paxos_lock | 172 ++++++++++++++++++++++----------------------- 1 file changed, 86 insertions(+), 86 deletions(-) diff --git a/bin/try_paxos_lock b/bin/try_paxos_lock index 57599b5..fd1524a 100644 --- a/bin/try_paxos_lock +++ b/bin/try_paxos_lock @@ -30,105 +30,105 @@ $| = 1; GetOptions( \my %opt, qw/ - help - version - workdir=s - holder=s - lease=i - unlock - debug - /, + help + version + workdir=s + holder=s + lease=i + unlock + debug + /, ) or pod2usage(1); if ( $opt{help} ) { - pod2usage(0); + pod2usage(0); } if ( $opt{version} ) { - print "try_paxos_lock version $MHA::NodeConst::VERSION.\n"; - exit 0; + print "try_paxos_lock version $MHA::NodeConst::VERSION.\n"; + exit 0; } exit &main(); sub main() { - my $exit_code = 1; - my $workdir = $opt{workdir}; - my $expired_time = time() + 86400; - unless ($workdir) { - croak "Paxos lock failed. workdir is not specified.\n"; + my $exit_code = 1; + my $workdir = $opt{workdir}; + my $expired_time = time() + 86400; + unless ($workdir) { + croak "Paxos lock failed. workdir is not specified.\n"; + } + unless (-d $workdir) { + croak "Paxos lock failed. Work directory does not exists(directory: $workdir).\n"; + } + unless ($opt{holder}) { + croak "Paxos lock failed. holder is not specified.\n"; + } + if ($opt{lease}) { + $expired_time = time() + $opt{lease}; + } + my $lock_file = "$workdir/paxos_lock"; + unless ($opt{unlock}) { + unless (sysopen(LOCK_FILE, $lock_file, O_CREAT | O_EXCL | O_WRONLY)) { + if (sysopen(LOCK_FILE, $lock_file, O_EXCL | O_RDWR)) { + my ($exist_holder, $exist_expired_time) = split(/:/, ); + my ($now) = time(); + if (($exist_holder eq $opt{holder}) && + ($exist_expired_time && (($exist_expired_time + 0) > $now))) { + close LOCK_FILE; + print "Paxos locked again successfully(holder: $exist_holder).\n"; + $exit_code = 0; + return $exit_code; + } + if ($exist_expired_time && (($exist_expired_time + 0) < $now)) { + # fallthrough + } else { + close LOCK_FILE; + croak "Paxos lock failed. $lock_file exist and not expired(holder: $exist_holder expired_time: $exist_expired_time now: $now).\n"; + } + } else { + croak "Paxos lock failed. $lock_file exist and open failed.\n"; + } } - unless (-d $workdir) { - croak "Paxos lock failed. Work directory does not exists(directory: $workdir).\n"; + seek LOCK_FILE,0,0; + truncate LOCK_FILE,0; + print LOCK_FILE "$opt{holder}:$expired_time"; + close LOCK_FILE; + open(LOCK_FILE, $lock_file) or croak "Read after paxos lock failed.\n"; + my ($read_holder, $read_expired_time) = split(/:/, ); + close LOCK_FILE; + my ($now) = time(); + if (($read_holder eq $opt{holder}) && + ($read_expired_time && (($read_expired_time + 0) > $now))) { + print "Paxos locked first time successfully(holder: $read_holder).\n"; + $exit_code = 0; + return $exit_code; } - unless ($opt{holder}) { - croak "Paxos lock failed. holder is not specified.\n"; + croak "Compare after paxos lock failed, It's strange(holder: $read_holder read_expired_time: $read_expired_time now: $now).\n"; + } else { + unless (-f $lock_file) { + print "Paxos unlocked successfully. Lock file not exists.\n"; + $exit_code = 0; + return $exit_code; } - if ($opt{lease}) { - $expired_time = time() + $opt{lease}; + my $link_filename="/var/tmp/paxos_lock"; + if (-f $link_filename) { + print "Old link file exists,unlink it.\n"; + unlink $link_filename; } - my $lock_file = "$workdir/paxos_lock"; - unless ($opt{unlock}) { - unless (sysopen(LOCK_FILE, $lock_file, O_CREAT | O_EXCL | O_WRONLY)) { - if (sysopen(LOCK_FILE, $lock_file, O_EXCL | O_RDWR)) { - my ($exist_holder, $exist_expired_time) = split(/:/, ); - my ($now) = time(); - if (($exist_holder eq $opt{holder}) && - ($exist_expired_time && (($exist_expired_time + 0) > $now))) { - close LOCK_FILE; - print "Paxos locked again successfully(holder: $exist_holder).\n"; - $exit_code = 0; - return $exit_code; - } - if ($exist_expired_time && (($exist_expired_time + 0) < $now)) { - # fallthrough - } else { - close LOCK_FILE; - croak "Paxos lock failed. $lock_file exist and not expired(holder: $exist_holder expired_time: $exist_expired_time now: $now).\n"; - } - } else { - croak "Paxos lock failed. $lock_file exist and open failed.\n"; - } - } - seek LOCK_FILE,0,0; - truncate LOCK_FILE,0; - print LOCK_FILE "$opt{holder}:$expired_time"; - close LOCK_FILE; - open(LOCK_FILE, $lock_file) or croak "Read after paxos lock failed.\n"; - my ($read_holder, $read_expired_time) = split(/:/, ); - close LOCK_FILE; - my ($now) = time(); - if (($read_holder eq $opt{holder}) && - ($read_expired_time && (($read_expired_time + 0) > $now))) { - print "Paxos locked first time successfully(holder: $read_holder).\n"; - $exit_code = 0; - return $exit_code; - } - croak "Compare after paxos lock failed, It's strange(holder: $read_holder read_expired_time: $read_expired_time now: $now).\n"; - } else { - unless (-f $lock_file) { - print "Paxos unlocked successfully. Lock file not exists.\n"; - $exit_code = 0; - return $exit_code; - } - my $link_filename="/var/tmp/paxos_lock"; - if (-f $link_filename) { - print "Old link file exists,unlink it.\n"; - unlink $link_filename; - } - link $lock_file,$link_filename; - open(READ_FILE, $lock_file); - my ($exist_holder, $exist_expired_time) = split(/:/, ); - close READ_FILE; - my ($now) = time(); - if (($exist_holder eq $opt{holder}) || - ($exist_expired_time && (($exist_expired_time + 0) < $now))) { - unlink $link_filename; - unlink $lock_file; - print "Paxos unlocked successfully.\n"; - $exit_code = 0; - return $exit_code; - } - unlink $link_filename; - croak "Paxos unlock failed, It's strange(holder: $exist_holder expired: $exist_expired_time now: $now).\n"; + link $lock_file,$link_filename; + open(READ_FILE, $lock_file); + my ($exist_holder, $exist_expired_time) = split(/:/, ); + close READ_FILE; + my ($now) = time(); + if (($exist_holder eq $opt{holder}) || + ($exist_expired_time && (($exist_expired_time + 0) < $now))) { + unlink $link_filename; + unlink $lock_file; + print "Paxos unlocked successfully.\n"; + $exit_code = 0; + return $exit_code; } + unlink $link_filename; + croak "Paxos unlock failed, It's strange(holder: $exist_holder expired: $exist_expired_time now: $now).\n"; + } }