Skip to content

Use snapshot archive if last snapshot is corrupt #91

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

Conversation

jeanconn
Copy link
Contributor

@jeanconn jeanconn commented Feb 6, 2025

Description

Use snapshot archive if last snapshot is corrupt.

In simulated local testing this fixes the issue recently experienced where the last snapshot at /data/mta4/www/Snapshot/chandra.snapshot is truncated and doesn't have an obsid.

Interface impacts

The code uses the archived snapshots in reverse time order if the last file snapshot is corrupt. Practically this is not an interface inpact but the snapshot time could be even older while the replan central page is still up-to-date.

Functional testing

For local testing, I just deleted the obsid value in the parsed snapshot right in the code and confirmed that the code went into the archive loop and got a new snapshot.

Practically, I do not have a way to test this in the real situation of the snapshot being broken as I'm not really sure what will be found in the snapshot archive at that time. But the change seems benign.

@jeanconn
Copy link
Contributor Author

jeanconn commented Feb 6, 2025

github seems to think this Snap.pm file is binary even if I change the test that uses "^@".

--- a/Snap.pm
+++ b/Snap.pm
@@ -25,7 +25,7 @@ sub get_snap {
     my @snap_files = @{ shift() };
     my $snap;                  # Latest snapshot as a string
     my @snarc;                 # Recent snapshots (array of hashes in reverse time order)
-    my %snap;                  # Final snapshot parsed into a hash
+    my %snap_data;             # Reference to final snapshot parsed into a hash
 
     local $_;
 
@@ -36,7 +36,7 @@ sub get_snap {
   SNAPFILE: foreach my $file (@snap_files) {
        if (-r $file) {
            $snap < io($file);
-            if ($snap eq "" or $snap =~ "^@") {
+            if ($snap eq "" or $snap =~ "^@"){
                 sleep 6;
                 $snap < io($file);
             }
@@ -50,25 +50,39 @@ sub get_snap {
 
     # Parse the latest snapshot.  In general this does not have correct SCS state info
     # (which is only available in EPS subformat)
-    %snap = parse_snap($snap);
+    %snap_data = parse_snap($snap);
 
-    # Get all the snapshots from the last few days in reverse time order. 
+    # Get all the snapshots from the last few days in reverse time order.
     # If get_snap_archive fails then just use the single most recent snap
-    my $snarc_ref = get_snap_archive($snarc_dir) || [ { %snap } ];
-    
-    # Use get_scs_states to determine the correct SCS state values 
+    my $snarc_ref = get_snap_archive($snarc_dir) || [ { %snap_data } ];
+
+    # Use get_scs_states to determine the correct SCS state values
     my %scs_state = get_scs_states($snarc_ref);
 
     # Copy SCS state values into the final snapshot hash
-    $snap{$_}{value} = $scs_state{$_} foreach qw(scs107 scs128 scs129 scs130 scs131 scs132 scs133 scs_obt);
+    $snap_data{$_}{value} = $scs_state{$_} foreach qw(scs107 scs128 scs129 scs130 scs131 scs132 scs133 scs_obt);
+
+    # If obsid is not defined, the snap is broken, so try one from the archive
+    # until we get one with obsid defined.  If $snarc_ref is already just the link
+    # to the current snap - this just won't do anything.
+    if (not defined $snap_data{obsid}{value}) {
+        print STDERR "Warning - obsid not defined in current snapshot, trying archive\n";
+         foreach my $snap_arc (@{$snarc_ref}) {
+             if (defined $snap_arc->{obsid}{value}) {
+                # Override the current snap with the one from the archive
+                 %snap_data = %{$snap_arc};
+                 last;
+             }
+         }
+    }

-    unless (defined $snap{obsid}{value}) {
+    unless (defined $snap_data{obsid}{value}) {
-    return \@warn, %snap;
+    return \@warn, %snap_data;
 }
 
 ####################################################################################
@@ -108,12 +122,12 @@ sub get_snap_archive {
        print STDERR "$msg\n";
        return;
     }
-       
+
     $snap_archive =~ s/<.*?>//g;       # Do the stupidest possible HTML tag removal.
                                # since it's fast and works for snarc files
-    # See HTML::FormatText for the "right" way to do it,  except that the damn 
+    # See HTML::FormatText for the "right" way to do it,  except that the damn
     # thing doesn't work for <pre> with HTML formatting tags!
- 
+
     # Split on the UTC key, but then put it back into each snapshot
     my @snap_archive = map { "UTC $_" } split /^UTC/m, $snap_archive;
     # But then skip the first because it was made from extra splitting
@@ -150,7 +164,7 @@ SNAP: for my $snap (@{$snarc_ref}) {
                # Check that the SCS state values are the same (guarding against invalid telem in snap)
                foreach (qw(scs107 scs128 scs129 scs130 scs131 scs132 scs133)) {
                    last SNAP if ($scs_state{$_} ne $curr_scs_state{$_});
-               } 
+               }
            }
            %scs_state = %curr_scs_state;
        } elsif (defined $scs_state{scs107}) {
@@ -163,7 +177,7 @@ SNAP: for my $snap (@{$snarc_ref}) {
        @scs_state{qw(scs107 scs128 scs129 scs130 scs131 scs132 scs133)} = qw(??? ??? ??? ??? ??? ??? ???);
        @scs_state{qw(obt utc)} = ($CurrentTime, $CurrentTime);
     }
-         
+

@jeanconn
Copy link
Contributor Author

In our meeting I heard that the preference is to close this as not needed and come back to it if this comes up again.

@jeanconn jeanconn closed this Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant