| 1 |
package Cornea::RecallTable; |
|---|
| 2 |
use strict; |
|---|
| 3 |
use Cornea::Config; |
|---|
| 4 |
use Cornea::Utils; |
|---|
| 5 |
use DBI; |
|---|
| 6 |
|
|---|
| 7 |
sub __connect { |
|---|
| 8 |
my $self = shift; |
|---|
| 9 |
my $config = Cornea::Config->new(); |
|---|
| 10 |
my $dbh; |
|---|
| 11 |
my $failed_err = undef; |
|---|
| 12 |
my $dsns = $config->get_list("DB::dsn"); |
|---|
| 13 |
Cornea::Utils::shuffle($dsns); |
|---|
| 14 |
foreach my $dsn (@$dsns) { |
|---|
| 15 |
eval { |
|---|
| 16 |
$dbh = DBI->connect($dsn, |
|---|
| 17 |
$config->get("DB::user"), |
|---|
| 18 |
$config->get("DB::pass"), |
|---|
| 19 |
{ PrintError => 0, RaiseError => 1 }, |
|---|
| 20 |
); |
|---|
| 21 |
}; |
|---|
| 22 |
die "$dsn: $@\n" if $@; |
|---|
| 23 |
last unless $@; |
|---|
| 24 |
} |
|---|
| 25 |
print STDERR "$failed_err\n" if $failed_err; |
|---|
| 26 |
$self->{dbh} = $dbh; |
|---|
| 27 |
} |
|---|
| 28 |
sub __reconnect { |
|---|
| 29 |
my $self = shift; |
|---|
| 30 |
$self->{dbh} = undef; |
|---|
| 31 |
$self->__connect(); |
|---|
| 32 |
} |
|---|
| 33 |
sub new { |
|---|
| 34 |
my $class = shift; |
|---|
| 35 |
my $self = bless { }, $class; |
|---|
| 36 |
$self->__connect; |
|---|
| 37 |
$self; |
|---|
| 38 |
} |
|---|
| 39 |
|
|---|
| 40 |
sub insert { |
|---|
| 41 |
my $self = shift; |
|---|
| 42 |
my ($serviceId, $assetId, $repId, $snl) = @_; |
|---|
| 43 |
my $tried = 0; |
|---|
| 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); |
|---|
| 49 |
$sth->finish(); |
|---|
| 50 |
}; |
|---|
| 51 |
if ($@) { |
|---|
| 52 |
unless ($tried++) { $self->__reconnect(); goto again; } |
|---|
| 53 |
die $@ if $@; |
|---|
| 54 |
} |
|---|
| 55 |
return 1; |
|---|
| 56 |
} |
|---|
| 57 |
|
|---|
| 58 |
sub find { |
|---|
| 59 |
my $self = shift; |
|---|
| 60 |
my ($serviceId, $assetId, $repId) = @_; |
|---|
| 61 |
my $sth = $self->{dbh}->prepare("select findAsset(?,?,?)"); |
|---|
| 62 |
my $tried = 0; |
|---|
| 63 |
my $C; |
|---|
| 64 |
again: |
|---|
| 65 |
eval { |
|---|
| 66 |
$C = Cornea::StorageNodeList->new(); |
|---|
| 67 |
$sth->execute($serviceId, $assetId, $repId); |
|---|
| 68 |
while(my $node = $sth->fetchrow_hashref()) { |
|---|
| 69 |
$C->add(Cornea::StorageNode->new_from_row($node)); |
|---|
| 70 |
} |
|---|
| 71 |
$sth->finish(); |
|---|
| 72 |
}; |
|---|
| 73 |
if ($@) { |
|---|
| 74 |
unless ($tried++) { $self->__reconnect(); goto again; } |
|---|
| 75 |
die $@ if $@; |
|---|
| 76 |
} |
|---|
| 77 |
return $C; |
|---|
| 78 |
} |
|---|
| 79 |
|
|---|
| 80 |
sub getNodes { |
|---|
| 81 |
my $self = shift; |
|---|
| 82 |
my $type = shift; |
|---|
| 83 |
my $tried = 0; |
|---|
| 84 |
my $snl; |
|---|
| 85 |
again: |
|---|
| 86 |
eval { |
|---|
| 87 |
$snl = Cornea::StorageNodeList->new(); |
|---|
| 88 |
my $sth = $self->{dbh}->prepare("select * from getCorneaNodes(?)"); |
|---|
| 89 |
$sth->execute($type); |
|---|
| 90 |
while(my $row = $sth->fetchrow_hashref()) { |
|---|
| 91 |
$snl->add(Cornea::StorageNode->new_from_row($row)); |
|---|
| 92 |
} |
|---|
| 93 |
$sth->finish(); |
|---|
| 94 |
}; |
|---|
| 95 |
if ($@) { |
|---|
| 96 |
unless ($tried++) { $self->__reconnect(); goto again; } |
|---|
| 97 |
die $@ if $@; |
|---|
| 98 |
} |
|---|
| 99 |
return $snl; |
|---|
| 100 |
} |
|---|
| 101 |
|
|---|
| 102 |
sub updateNode { |
|---|
| 103 |
my $self = shift; |
|---|
| 104 |
my $fqdn = shift; |
|---|
| 105 |
my $attr = shift; |
|---|
| 106 |
die "bad state" |
|---|
| 107 |
unless $attr->{state} =~ /^(?:open|closed|offline|decommissioned)$/; |
|---|
| 108 |
die "storage must be a number" |
|---|
| 109 |
unless $attr->{total_storage} =~ /^[1-9]\d*$/ and |
|---|
| 110 |
$attr->{used_storage} =~ /^[1-9]\d*$/; |
|---|
| 111 |
die "locaion must be dc/cage/row/rack/pdu" |
|---|
| 112 |
unless !defined($attr->{location}) or |
|---|
| 113 |
$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 $@; |
|---|
| 129 |
} |
|---|
| 130 |
return 0; |
|---|
| 131 |
} |
|---|
| 132 |
|
|---|
| 133 |
sub repInfo { |
|---|
| 134 |
my $self = shift; |
|---|
| 135 |
my ($serviceId, $repId) = @_; |
|---|
| 136 |
my $tried = 0; |
|---|
| 137 |
my $row; |
|---|
| 138 |
again: |
|---|
| 139 |
eval { |
|---|
| 140 |
my $sth = $self->{dbh}->prepare("select * from getRepInfo(?,?)"); |
|---|
| 141 |
$sth->execute($serviceId, $repId); |
|---|
| 142 |
$row = $sth->fetchrow_hashref(); |
|---|
| 143 |
$sth->finish(); |
|---|
| 144 |
}; |
|---|
| 145 |
if ($@) { |
|---|
| 146 |
unless ($tried++) { $self->__reconnect(); goto again; } |
|---|
| 147 |
die $@ if $@; |
|---|
| 148 |
} |
|---|
| 149 |
return Cornea::RepresentationInfo->new_from_row($row); |
|---|
| 150 |
} |
|---|
| 151 |
|
|---|
| 152 |
sub repInfoDependents { |
|---|
| 153 |
my $self = shift; |
|---|
| 154 |
my ($serviceId, $repId) = @_; |
|---|
| 155 |
my $tried = 0; |
|---|
| 156 |
my @deps; |
|---|
| 157 |
again: |
|---|
| 158 |
eval { |
|---|
| 159 |
@deps = (); |
|---|
| 160 |
my $sth = $self->{dbh}->prepare("select * from getRepInfoDependents(?,?)"); |
|---|
| 161 |
$sth->execute($serviceId, $repId); |
|---|
| 162 |
while(my $row = $sth->fetchrow_hashref()) { |
|---|
| 163 |
push @deps, Cornea::RepresentationInfo->new_from_row($row); |
|---|
| 164 |
} |
|---|
| 165 |
$sth->finish(); |
|---|
| 166 |
}; |
|---|
| 167 |
if ($@) { |
|---|
| 168 |
unless ($tried++) { $self->__reconnect(); goto again; } |
|---|
| 169 |
die $@ if $@; |
|---|
| 170 |
} |
|---|
| 171 |
return @deps; |
|---|
| 172 |
} |
|---|
| 173 |
|
|---|
| 174 |
1; |
|---|