LVS
lvs-users
Google
 
Web LinuxVirtualServer.org

LDIRECTORD: Use goto for error handling in check_sql

To: lvs-users@xxxxxxxxxxxxxxxxxxxxxx
Subject: LDIRECTORD: Use goto for error handling in check_sql
Cc: Roberto Nibali <rnibali@xxxxxxxxx>
Cc: J.Libak@xxxxxxxxxx
Cc: Roberto Nibali <ratz@xxxxxxxxxxxx>
From: Simon Horman <horms@xxxxxxxxxxxx>
Date: Tue, 24 Apr 2007 19:02:03 +0900
I think this simplifies things somewhat.

Cc: Roberto Nibali <ratz@xxxxxxxxxxxx>
Signed-off-by: Simon Horman <horms@xxxxxxxxxxxx>
Index: heartbeat/ldirectord/ldirectord.in
===================================================================
--- heartbeat.orig/ldirectord/ldirectord.in     2007-04-24 18:17:00.000000000 
+0900
+++ heartbeat/ldirectord/ldirectord.in  2007-04-24 18:17:05.000000000 +0900
@@ -2490,17 +2490,16 @@ sub check_sql
        require DBI;
        my ($v, $r, $dbd, $dbname) = @_;
        my $port=(defined $$v{checkport}?$$v{checkport}:$$r{port});
-       my ($dbh, $sth, $query, $rows, $result);   # Local variables
+       my $dbh, $sth, $query, $rows, $result = 1;
+
        $query = $$r{request};
        $query =~ s#^/##;
        unless ($$v{login} && $query) {
-               service_set($v, $r, "down");
                &ld_log("Error: Must specify a login and request string " .
                        "for MySQL and PostgreSQL checks. " .
                        "Not adding $$r{server}.\n");
-               return 1;
+               goto err_down;
        }
-       $result=2;   # Set result flag.  Only ok if ends up at zero.
        &ld_debug(2, "Checking $$v{server} server=$$r{server} port=$port\n");
        $dbh = DBI->connect("dbi:$dbd:$dbname=$$v{database};" .
                            "host=$$r{server};port=$port", $$v{login},
@@ -2508,17 +2507,13 @@ sub check_sql
        unless ($dbh) {
                &ld_debug(4, "Failed to bind to $$r{server} with " .
                          $dbh->errstr);
-               service_set($v, $r, "down");
-               return 1;
+               goto err_down;
        }
-       $result--;
        $sth = $dbh->prepare($query);
        unless ($sth) {
                service_set($v, $r, "down");
                &ld_log("Error preparing statement:" . $dbh->errstr() .  "\n");
-               $dbh->disconnect();
-               service_set($v, $r, "down");
-               return 1;
+               goto err_disconnect;
        }
        $rows = $sth->execute;
        ld_debug(4, "Database search returned $rows rows");
@@ -2526,20 +2521,20 @@ sub check_sql
        # * Disallows query of an empty table.
        # * Only works with select and update statements, however most
        #   people will want to use select statements for health checks.
-       if ($rows gt 0) {
-               $result--;
-       } else {
+       if ($rows == 0) {
                &ld_log("Prior executing statement returned $rows rows: " .
                        "not adding $$r{server}.\n");
-               service_set($v, $r, "down");
-               $sth->finish();
-               $dbh->disconnect();
-               return 1;
+               goto err_finish;
        }
-       service_set($v, $r, "up");
+
+       $result = 0;
+err_finish:
        $sth->finish();
+err_disconnect:
        $dbh->disconnect();
-       return 0;
+err_down:
+       service_set($v, $r, $result == 0 ? "up" : "down");
+       return $result;
 }
 
 

-- 

-- 
Horms
  H: http://www.vergenet.net/~horms/
  W: http://www.valinux.co.jp/en/


<Prev in Thread] Current Thread [Next in Thread>