Changeset 18
- Timestamp:
- 08/25/09 21:25:23 (4 years ago)
- Files:
-
- trunk/bin/corneactl (modified) (4 diffs)
- trunk/etc/cornea.conf (modified) (1 diff)
- trunk/perl/lib/Cornea/RecallTable.pm (modified) (7 diffs)
- trunk/perl/lib/Cornea/Utils.pm (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/bin/corneactl
r9 r18 1 #!/usr/bin/ perl1 #!/usr/bin/env perl 2 2 use strict; 3 3 … … 11 11 use Carp; 12 12 use Cornea::Config; 13 use Socket; 13 14 use Switch; 14 15 … … 24 25 } 25 26 case 'update-node' { 27 my $ip = Cornea::Utils::my_ip(); 26 28 my $fqdn = shift; 27 29 my $location = shift; 30 if(defined $fqdn) { 31 # Adding a dot forces it to resolve outside search domains 32 my @addr = gethostbyname("$fqdn."); 33 die "fqdn '$fqdn' does not map to $ip\n" 34 unless eval { $ip eq inet_ntoa($addr[4]); }; 35 } 28 36 my $rt = Cornea::RecallTable->new(); 29 37 my ($total, $used) = Cornea::Utils::fsinfo($conf->get('Storage::base')."/."); … … 32 40 if ($min =~ /^(\d+)\s*([GBKMT])/i); 33 41 my $state = (($total - $used) < $min) ? 'closed' : 'open'; 34 $rt->updateNode($fqdn, { state => $state, 35 total_storage => $total, 36 used_storage => $used, 37 location => $location, 38 } ); 42 $rt->updateNode($ip, { fqdn => $fqdn, 43 state => $state, 44 total_storage => $total, 45 used_storage => $used, 46 location => $location, 47 } ); 39 48 } 40 49 else { trunk/etc/cornea.conf
r6 r18 1 DB::dsn = [ d sn1 dsn2 ]1 DB::dsn = [ dbi:Pg:dbname=cornea;host=cornea-db1 dbi:Pg:dbname=cornea;host=cornea-db2 ] 2 2 DB::user = cornea 3 DB::pass = nsdfkjnwr3 DB::pass = cornea 4 4 5 5 Storage::path = /c trunk/perl/lib/Cornea/RecallTable.pm
r9 r18 43 43 my $tried = 0; 44 44 die 'bad parameters' unless UNIVERSAL::ISA($snl, 'Cornea::StorageNodeList'); 45 again: 46 eval { 47 my $sth = $self->{dbh}->prepare("select storeAsset(?,?,?,?)"); 48 $sth->execute($serviceId, $assetId, $repId, $snl); 45 my $snl_arr = '{' . join(',', map { $_->storeagenodeid() } 46 ($snl->items())) . '}'; 47 again: 48 eval { 49 my $sth = $self->{dbh}->prepare("select make_asset(?,?,?,?::int[])"); 50 $sth->execute($serviceId, $assetId, $repId, $snl_arr); 49 51 $sth->finish(); 50 52 }; … … 59 61 my $self = shift; 60 62 my ($serviceId, $assetId, $repId) = @_; 61 my $sth = $self->{dbh}->prepare("select findAsset(?,?,?)");63 my $sth = $self->{dbh}->prepare("select get_asset_location(?,?,?)"); 62 64 my $tried = 0; 63 65 my $C; … … 86 88 eval { 87 89 $snl = Cornea::StorageNodeList->new(); 88 my $sth = $self->{dbh}->prepare("select * from get CorneaNodes(?)");90 my $sth = $self->{dbh}->prepare("select * from get_storage_nodes_by_state(?)"); 89 91 $sth->execute($type); 90 92 while(my $row = $sth->fetchrow_hashref()) { … … 100 102 } 101 103 104 sub _2pc_add_storage { 105 my $ip = shift; 106 my $attr = shift; 107 my $storage_node_id; 108 my $config = Cornea::Config->new(); 109 my $dsns = $config->get_list("DB::dsn"); 110 my @dbh = map { 111 my $dbh = DBI->connect($_, 112 $config->get("DB::user"), 113 $config->get("DB::pass"), 114 { PrintError => 0, RaiseError => 1, AutoCommit => 1 } 115 ); 116 $dbh->begin_work(); 117 $dbh; 118 } @$dsns; 119 eval { 120 foreach (@dbh) { 121 eval { 122 my $sth = $_->prepare("select set_storage_node(?,?,?,?,?,?,?)"); 123 $sth->execute($attr->{state}, 124 $attr->{total_storage}, $attr->{used_storage}, 125 $attr->{location}, $attr->{fqdn}, $ip, $storage_node_id); 126 my ($returned_storade_node_id) = $sth->fetchrow(); 127 $storage_node_id ||= $returned_storade_node_id; 128 $sth->finish(); 129 die "Storage node trickery! (this should never happen).\n" 130 if($storage_node_id != $returned_storade_node_id); 131 }; 132 if ($@) { 133 die "location must be specified for first-time update\n" 134 if $@ =~ /null value in column "location"/; 135 die $@ if $@; 136 } 137 } 138 foreach (@dbh) { $_->do("prepare transaction 'cornea_node'"); } 139 foreach (@dbh) { $_->do("commit prepared 'cornea_node'"); } 140 }; 141 if ($@) { 142 my $real_error = $@; 143 $storage_node_id = undef; 144 eval { foreach (@dbh) { $_->do("rollback prepared 'cornea_node'"); } }; 145 die $real_error; 146 } 147 foreach (@dbh) { $_->disconnect; } 148 die $@ unless($storage_node_id); 149 return $storage_node_id; 150 } 102 151 sub updateNode { 103 152 my $self = shift; 104 my $ fqdn= shift;153 my $ip = shift; 105 154 my $attr = shift; 155 my $config = Cornea::Config->new(); 106 156 die "bad state" 107 157 unless $attr->{state} =~ /^(?:open|closed|offline|decommissioned)$/; … … 112 162 unless !defined($attr->{location}) or 113 163 $attr->{location} =~ /^[^\/]+(?:\/[^\/]+){4}$/; 114 115 my $tried = 0; 116 again: 117 eval { 118 my $sth = $self->{dbh}->prepare("select storeCorneaNode(?,?,?,?,?)"); 119 $sth->execute($attr->{state}, 120 $attr->{total_storage}, $attr->{used_storage}, 121 $attr->{location}, $fqdn); 122 $sth->finish(); 123 }; 124 if ($@) { 125 die "location must be specified for first-time update\n" 126 if $@ =~ /null value in column "location"/; 127 unless ($tried++) { $self->__reconnect(); goto again; } 128 die $@ if $@; 164 die "fqdn must not be blank" 165 unless !defined($attr->{fqdn}) or length($attr->{fqdn}); 166 167 if(defined($attr->{location}) || defined($attr->{fqdn})) { 168 return return _2pc_add_storage($ip, $attr); 169 } 170 my $dsns = $config->get_list("DB::dsn"); 171 foreach (@$dsns) { 172 my $dbh = DBI->connect($_, 173 $config->get("DB::user"), 174 $config->get("DB::pass"), 175 { PrintError => 0, RaiseError => 1, AutoCommit => 1 } 176 ); 177 my $tried = 0; 178 again: 179 eval { 180 my $sth = $self->{dbh}->prepare("select set_storage_node(?,?,?,?,?,?,?)"); 181 $sth->execute($attr->{state}, 182 $attr->{total_storage}, $attr->{used_storage}, 183 $attr->{location}, $attr->{fqdn}, $ip, undef); 184 $sth->finish(); 185 }; 186 if ($@) { 187 unless ($tried++) { $self->__reconnect(); goto again; } 188 print STDERR $@; 189 } 129 190 } 130 191 return 0; … … 138 199 again: 139 200 eval { 140 my $sth = $self->{dbh}->prepare("select * from get RepInfo(?,?)");201 my $sth = $self->{dbh}->prepare("select * from get_representation(?,?)"); 141 202 $sth->execute($serviceId, $repId); 142 203 $row = $sth->fetchrow_hashref(); … … 158 219 eval { 159 220 @deps = (); 160 my $sth = $self->{dbh}->prepare("select * from get RepInfoDependents(?,?)");221 my $sth = $self->{dbh}->prepare("select * from get_representation_dependents(?,?)"); 161 222 $sth->execute($serviceId, $repId); 162 223 while(my $row = $sth->fetchrow_hashref()) { trunk/perl/lib/Cornea/Utils.pm
r9 r18 3 3 use strict; 4 4 use Carp; 5 use Socket; 5 6 use Switch; 7 use POSIX qw/uname/; 6 8 require "sys/syscall.ph"; 9 10 sub my_ip { 11 my @uname = POSIX::uname(); 12 my @addr = gethostbyname($uname[1]); 13 my $name = gethostbyaddr($addr[4], AF_INET); 14 die unless ($name eq $uname[1]); 15 return inet_ntoa($addr[4]); 16 } 7 17 8 18 sub shuffle { … … 56 66 my $bavail = unpack64(substr($buf, 24, 8)); 57 67 my $files = unpack64(substr($buf, 32, 8)); 58 my $ffree = unpack64(substr($buf, 80, 8));68 my $ffree = unpack64(substr($buf, 40, 8)); 59 69 my $bfactor = $bsize / 1024; 70 return int($blocks * $bfactor), int(($blocks - $bavail) * $bfactor); 71 } 72 case 'solaris' { 73 my $buf = '\0' x 4096; 74 syscall(&SYS_statvfs, $path, $buf) == 0 or die "$!"; 75 my $fbasetype = unpack "Z16", substr($buf, 72, 16); 76 die "Unsupported fs '$fbasetype' on solaris\n" 77 unless ($fbasetype eq 'zfs'); 78 my $bsize = unpack64(substr($buf, 0, 8)); 79 my $frsize = unpack64(substr($buf, 8, 8)); 80 my $blocks = unpack64(substr($buf, 16, 8)); 81 my $bfree = unpack64(substr($buf, 24, 8)); 82 my $bavail = unpack64(substr($buf, 32, 8)); 83 my $files = unpack64(substr($buf, 40, 8)); 84 my $ffree = unpack64(substr($buf, 48, 8)); 85 my $favail = unpack64(substr($buf, 56, 8)); 86 my $fsid = unpack64(substr($buf, 64, 8)); 87 my $bfactor = $frsize / 1024; 60 88 return int($blocks * $bfactor), int(($blocks - $bavail) * $bfactor); 61 89 }
