Permalink
Browse files

r180@miyako: hachi | 2006-04-20 15:16:47 -0700

 Initial support for DBI based locking (mysql cluster in particular)


git-svn-id: http://code.sixapart.com/svn/ddlockd/trunk@30 75bc8fc0-0210-0410-8e5e-a32055405bc5
  • Loading branch information...
1 parent d9bb130 commit a1bc8ab66c94f5225f8080e7195ad6f8c0df9647 hachi committed Apr 20, 2006
Showing with 78 additions and 1 deletion.
  1. +78 −1 ddlockd
View
79 ddlockd
@@ -26,6 +26,8 @@ $DEBUG = 0;
my (
$daemonize,
$nokeepalive,
+ $hostname,
+ $table,
);
my $conf_port = 7002;
my $lock_type = "internal";
@@ -36,20 +38,29 @@ Getopt::Long::GetOptions(
'debug=i' => \$DEBUG,
'n|no-keepalive' => \$nokeepalive,
't|type=s' => \$lock_type,
+ 'h|hostname=s' => \$hostname,
+ 'T|table=s' => \$table,
);
my $client_class;
+my @client_options;
if ($lock_type eq 'internal') {
$client_class = "Client::Internal";
}
elsif($lock_type eq 'dlmfs') {
$client_class = "Client::DLMFS";
}
+elsif($lock_type eq 'dbi') {
+ length( $hostname ) or die( "-h (--hostname) must be included with a hostname in dbi mode\n" );
+ length( $table ) or die( "-T (--table) must be included with a table name in dbi mode\n" );
+ $client_class = "Client::DBI";
+ @client_options = ( $hostname, $table );
+}
else {
die( "Unknown lock type of '$lock_type' specified.\n" );
}
-$client_class->_setup;
+$client_class->_setup( @client_options );
daemonize() if $daemonize;
@@ -395,6 +406,72 @@ sub _get_locks {
# return map { " $_ = " . $holder{$_}->as_string } (sort keys %holder);
}
+package Client::DBI;
+
+# CREATE TABLE
+
+use base 'Client';
+use fields qw(dbh);
+use DBI;
+
+my $hostname;
+my $table;
+my $dbh;
+
+sub _setup {
+ my Client::DBI $self = shift;
+ ($hostname, $table) = @_;
+ $dbh = DBI->connect( 'dbi:mysql:dbname=sixalock', '', '', {AutoCommit => 0} ) or die;
+ #$dbh->{HandleError} =
+}
+
+sub _trylock {
+ my Client::DBI $self = shift;
+ my $lock = shift;
+
+ my $local_locks = $self->{locks};
+ exists( $local_locks->{$lock} ) and return $self->err_line( "local taken" );
+
+ $dbh->do( 'START TRANSACTION' ) or return $self->err_line( "Transaction failed to start" );
+
+ my $sth = $dbh->prepare( "SELECT * FROM $table WHERE name=?" );
+ $sth->execute( $lock )
+ or return $self->err_line( "STH->execute failed" );
+ my $ary = $sth->fetchall_arrayref;
+ ref($ary) eq 'ARRAY'
+ or return $self->err_line( "DBI->selectall_arrayref returned non-arrayref" );
+ scalar @$ary == 0
+ or return $self->err_line( "remote taken" );
+
+ $dbh->do( "INSERT INTO $table (name) VALUES (?)", {}, $lock )
+ or return $self->err_line( "INSERT failed" );
+ $dbh->do( 'COMMIT' )
+ or return $self->err_line( "COMMIT failed" );
+ return $self->ok_line();
+}
+
+sub _release_lock {
+ my Client::DBI $self = shift;
+ my $lock = shift;
+
+ my $locks = $self->{locks};
+ if (exists( $locks->{$lock} )) {
+ delete $locks->{$lock};
+ $dbh->do( "SELECT AND DELETE * FROM $table WHERE name=?", {}, $lock );
+ return 1;
+ }
+ else {
+ return 0;
+ }
+}
+
+sub _get_locks {
+ my Client::DBI $self = shift;
+
+ my $ary = $dbh->selectall_arrayref( "SELECT name FROM $table" );
+ return map { $_->[0] } @$ary;
+}
+
# Local Variables:
# mode: perl
# c-basic-indent: 4

0 comments on commit a1bc8ab

Please sign in to comment.