IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Apr 6, 2023, 6:00:06 PM (3 years ago)
Author:
eugene
Message:

big re-work to allow repeat, iterated fixes

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tools/eam/rawfix.20230221/src/fix_chip_locations.pl

    r42441 r42444  
    1212use Getopt::Long qw( GetOptions :config auto_help auto_version );
    1313
    14 # USAGE: fix_chip_locations.pl --dateobs (night) --chiplist (file) --stage (v0,v1,etc)
     14# USAGE: fix_chip_locations.pl --dateobs (night) --chiplist (file) --stage (0,1,etc)
    1515
    1616# we need to select targets for ITC copies of files currently only at ATRC:
     
    2525
    2626# choose ITC machines on which to place backup copies:
    27 my @volarray = &get_neb_volumes;
     27my @vol_copy = &get_neb_volumes_copy; # array of neb hosts to copy data to at ITC
     28my %vol_good = &get_neb_volumes_good; # hash of valid neb hosts
    2829my $volnum = 0;
    2930
    3031my $Ngood = 0;
     32my $Ndead = 0;
    3133my $Ngone = 0;
    32 my $Ndead = 0;
    3334my $Nsing = 0;
    3435my $Ndups = 0;
     
    4041my $Ncul3 = 0;
    4142
     43# load the lists of good and fail chips
     44my $prev = $stage - 1;
     45my $goodfile = "$topdir/$dateword/goodchips.$prev.txt";
     46my %goodchips;
     47if (-e $goodfile) {
     48    open (GOODFILE,  $goodfile);
     49    my @lines = <GOODFILE>;
     50    close (GOODFILE);
     51   
     52    foreach my $line (@lines) {
     53        chomp $line;
     54        $goodchips{$line} = 1;
     55    }
     56}
     57my $failfile = "$topdir/$dateword/failchips.$prev.txt";
     58my %failchips;
     59if (-e $failfile) {
     60    open (FAILFILE,  $failfile);
     61    my @lines = <FAILFILE>;
     62    close (FAILFILE);
     63   
     64    foreach my $line (@lines) {
     65        chomp $line;
     66        $failchips{$line} = 1;
     67    }
     68}
     69
    4270foreach my $chip_id (@chip_ids) {
    4371
    44     my $urifile = "$topdir/$dateword/$chip_id.uris.txt";
    45     open (FILE,  $urifile);
    46     my @lines = <FILE>;
    47     close (FILE);
     72    # read from chip_id file, update to next stage number
     73    my $urifile = "$topdir/$dateword/$chip_id.uris.$stage.txt";
     74    if (! -e $urifile) { die "missing file $urifile\n"; }
     75    open (URIFILE,  $urifile);
     76    my @lines = <URIFILE>;
     77    close (URIFILE);
    4878
    4979    foreach my $line (@lines) {
     
    6191        my $md5sum = $words[3];
    6292
     93        my $haveMD5 = 0;
     94        my $goodMD5 = 0;
     95        if ($goodchips{$ext_id}) { $haveMD5 = 1; $goodMD5 = 1; }
     96        if ($failchips{$ext_id}) { $haveMD5 = 1; }
     97
     98
     99        # nothing to be done, or need be done for these:
    63100        if ($state eq "GOOD:") { $Ngood++; next; }
    64101        if ($state eq "DEAD:") { $Ndead++; next; }
     
    66103
    67104        # one copy at ITC: neb-replicate will create a copy at ATRC
     105        # does not depend on existence of MD5sum
    68106        if ($state eq "SING:") {
    69107            my $status = &vsystem ("neb-replicate $ext_id");
     
    79117
    80118        # one copy at ATRC: neb-replicate will create a copy at ITC iff we supply the target volume
     119        # does not depend on existence of MD5sum
    81120        if ($state eq "SBCK:") {
    82             my $tgtvol = $volarray[$volnum];
     121            my $tgtvol = $vol_copy[$volnum];
    83122            my $status = &vsystem ("neb-replicate --volume $tgtvol $ext_id");
    84123            if ($status) { print STDERR "FAIL SBCK: neb-replicate : $ext_id on $tgtvol\n"; next; }
     
    92131        }
    93132
    94         # these are done in stage 2:
    95         if ($state eq "DUPS:") { $Ndups++; next; }
    96         if ($state eq "XBCK:") { $Nxbck++; next; }
    97 
    98         # these are done in stage 3:
    99         if ($state eq "CUL1:") { $Ncul1++; next; }
    100         if ($state eq "CUL2:") { $Ncul2++; next; }
    101         if ($state eq "CUL3:") { $Ncul3++; next; }
     133        # multiple copies @ ITC, but none at ATRC. use neb-replicate to make copies
     134        # skip if we do not know the state of the md5sum
     135        if ($state eq "DUPS:") {
     136            if ($haveMD5 && $goodMD5) {
     137                my $status = &vsystem ("neb-replicate $ext_id");
     138                if ($status) { print STDERR "FAIL DUPS: neb-replicate : $ext_id\n"; next; }
     139                my ($newfile, $newhost, $newxattr) = &get_new_instance ($ext_id);
     140                unless ($newxattr == 1) { die "new instance is not at ATRC? $ext_id : $newfile : $newhost : $newxattr\n"; }
     141                open (OUTFILE, ">>$topdir/$dateword/$newhost.inst.$stage.txt");
     142                print OUTFILE "$newfile $datast $md5sum\n";
     143                close (OUTFILE);
     144            }
     145            $Ndups++;
     146            next;
     147        }
     148
     149        # multiple copies @ ATRC, but none @ ITC: neb-replicate will create a copy at ITC iff we supply the target volume
     150        # skip if we do not know the state of the md5sum
     151        if ($state eq "XBCK:") {
     152            if ($haveMD5 && $goodMD5) {
     153                my $tgtvol = $vol_copy[$volnum];
     154                my $status = &vsystem ("neb-replicate --volume $tgtvol $ext_id");
     155                if ($status) { print STDERR "FAIL XBCK: neb-replicate : $ext_id on $tgtvol\n"; next; }
     156                my ($newfile, $newhost, $newxattr) = &get_new_instance ($ext_id);
     157                unless ($newxattr == 0) { die "new instance is not at ITC? $ext_id : $newfile : $newhost : $newxattr\n"; }
     158                open (OUTFILE, ">>$topdir/$dateword/$newhost.inst.$stage.txt");
     159                print OUTFILE "$newfile $datast $md5sum\n";
     160                close (OUTFILE);
     161            }
     162            $Nxbck++;
     163            next;
     164        }
     165
     166        # one copy at ITC: neb-replicate will create a copy at ATRC
     167        # skip if we do not know the state of the md5sum
     168        if (($state eq "CUL1:") || ($state eq "CUL2:") || ($state eq "CUL3:")) {
     169
     170            if ($haveMD5 && $goodMD5) {
     171                # get info for all instances of this chip
     172                # returns a reference to the array of instance data
     173                # also strips neb://hostname.volnum/ from nebname and returns as ext_id
     174                my ($inforows, $ext_id) =  &get_chip_instances ($ext_id);
     175
     176                my ($main_keep, $back_keep, $cull_list_r) = &check_instance_xattr ($inforows, $ext_id);
     177
     178                if ($main_keep eq "") { print STDERR "FAIL $state: missing ITC copy for $ext_id\n";  next; }
     179                if ($back_keep eq "") { print STDERR "FAIL $state: missing ATRC copy for $ext_id\n"; next; }
     180
     181                my $NcullTotal = @$cull_list_r;
     182                foreach my $instance (@$cull_list_r) {
     183                    my ($volname) = $instance =~ m|file:///data/([0-9a-zA-Z]*\.[0-9])/\S*|;
     184                    unless (defined $vol_good{$volname}) { die "ERROR: invalid volume name $volname\n"; }
     185                    # print STDERR "neb-cull $state: $ext_id $volname ($main_keep $back_keep)\n";
     186                    my $status = &vsystem ("neb-cull --volume $volname $ext_id");
     187                    if ($status) { print STDERR "FAIL CULL: neb-replicate : $ext_id\n"; next; }
     188                }
     189                # print STDERR "Ncull Total $NcullTotal\n";
     190            }
     191
     192            # these are done in stage 3:
     193            if ($state eq "CUL1:") { $Ncul1++; next; }
     194            if ($state eq "CUL2:") { $Ncul2++; next; }
     195            if ($state eq "CUL3:") { $Ncul3++; next; }
     196        }
    102197    }
    103198    $volnum ++;
     
    136231}
    137232
    138 # get the list of chip neb paths for one chip
    139 sub get_neb_volumes {
     233sub get_chip_instances {
     234
     235    my $ext_id = $_[0];
     236   
     237    my $sql_chip_instances = "SELECT uri, host, available, xattr, cab_id from instance join storage_object using (so_id) join volume using (vol_id) where ext_id = '$ext_id'";
     238    my $rowref   = $neb_dbh->selectall_arrayref( $sql_chip_instances );
     239   
     240    return ($rowref, $ext_id);
     241}
     242
     243# select 1 instance with xattr = 0, 1 with xattr = 1.
     244# the rest should be culled.
     245
     246sub check_instance_xattr {
     247
     248    my $rowref  = $_[0];
     249    my $ext_id  = $_[1];
     250
     251    my @inforows = @$rowref;
     252    my $Nrows    = @inforows;
     253
     254    if ($DEBUG > 5) { print "found $Nrows instances\n"; }
     255
     256    # get user_copies for this storage_object
     257    my $main_keep = "";
     258    my $back_keep = "";
     259    my @cull_list = ();
     260
     261    # xattr = 0 means instance @ ITC  (main)
     262    # xattr = 1 means instance @ ATRC (back)
     263    for (my $j = 0; $j < $Nrows; $j ++) {
     264        my $valref = $inforows[$j];
     265        my @values = @$valref;
     266       
     267        # the number of returned values per row must match the query above (in get_chip_instances)
     268        my $Nvalue = @values;
     269        if ($Nvalue != 5) {
     270            print STDERR "ERROR : INVALID DB XATTR RESPONSE: $ext_id\n";
     271            next;
     272        }
     273
     274        my $instance = $values[0];
     275        my $hostname = $values[1];
     276        my $xattr    = $values[3];
     277       
     278        if (($xattr == 0) && ($main_keep eq "")) { $main_keep = $instance; next; }
     279        if (($xattr == 1) && ($back_keep eq "")) { $back_keep = $instance; next; }
     280
     281        # if we make it here, we already have an instance at this location
     282        push (@cull_list, $instance);
     283    }
     284    return ($main_keep, $back_keep, \@cull_list);
     285}
     286
     287# get the list of neb hosts to copy data to at ITC
     288sub get_neb_volumes_copy {
    140289
    141290    my @volarray;
     
    164313}
    165314
     315# get the list of valid neb hosts (ITC or ATRC)
     316sub get_neb_volumes_good {
     317
     318    my %volarray;
     319
     320    my $sql_neb_volumes = "select name from mountedvol where ((xattr = 0) or (xattr = 1))";
     321    if ($DEBUG > 4) { print "sql: $sql_neb_volumes\n"; }
     322
     323    my $rowref   = $neb_dbh->selectall_arrayref( $sql_neb_volumes );
     324    my @tmparray = @$rowref;
     325    my $Nrows    = @tmparray;
     326
     327    if ($DEBUG > 4) { print "found $Nrows possible volumes\n"; }
     328
     329    foreach my $valref (@tmparray) {
     330        my @values = @$valref;
     331        # there should only be a single value (only name selected above)
     332        my $Nvalues = @values;
     333        if ($Nvalues == 0) { print "ERROR: missing uri\n"; next; }
     334        if ($Nvalues  > 1) { print "ERROR: too many uri entries?\n"; next; }
     335       
     336        $volarray{$values[0]} = 1;
     337        if ($DEBUG > 4) { print "$values[0]\n"; }
     338    }
     339   
     340    return %volarray;
     341}
     342
    166343# read the list of chip_ids:
    167344sub load_chip_list {
     
    194371    pod2usage( -msg => "Cannot determine target date, use --dateobs YYYY/MM/DD", -exitval => 2) unless defined $dateobs;
    195372    pod2usage( -msg => "Provide a list of chips with --chiplist", -exitval => 2) unless defined $chiplist;
    196     pod2usage( -msg => "Define the stage (v0, v1, etc) with --stage", -exitval => 2) unless defined $stage;
     373    pod2usage( -msg => "Define the stage (0, 1, etc) with --stage", -exitval => 2) unless defined $stage;
    197374    pod2usage( -msg => "Specify the top-level output directory with --topdir", -exitval => 2) unless defined $topdir;
    198375   
     376    # check that stage is an integer
     377    unless (is_integer ($stage)) { die "stage must be a non-negative integer\n"; }
     378    unless ($stage >= 0) { die "stage must be a non-negative integer\n"; }
     379
    199380    # check the date format (require YYYY/MM/DD and use that from 00:00:00 to 23:59:59 or take start and end?)
    200381
     
    248429sub vsystem {
    249430    print STDERR "@_\n";
    250     #return 0;
     431    # return 0;
    251432    my $status = system ("@_");
    252433    return $status;
    253434}
     435
     436sub is_integer {
     437   defined $_[0] && $_[0] =~ /^[+-]?\d+$/;
     438}
     439   
Note: See TracChangeset for help on using the changeset viewer.