[Bps-public-commit] app-aws-cloudwatch-monitor branch 0.03/update-meta-data-caching-writes updated. 5083cec0c7bfdfc6b65671aa32c5756e1cf95b67

BPS Git Server git at git.bestpractical.com
Fri May 20 21:26:02 UTC 2022


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "app-aws-cloudwatch-monitor".

The branch, 0.03/update-meta-data-caching-writes has been updated
       via  5083cec0c7bfdfc6b65671aa32c5756e1cf95b67 (commit)
      from  61b92ae956223ae5eb6aef66c22dcf17ff3f02a6 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
commit 5083cec0c7bfdfc6b65671aa32c5756e1cf95b67
Author: Blaine Motsinger <blaine at bestpractical.com>
Date:   Fri May 20 16:25:38 2022 -0500

    WIP - refactor get_meta_data to fallback to cache

diff --git a/lib/App/AWS/CloudWatch/Monitor/CloudWatchClient.pm b/lib/App/AWS/CloudWatch/Monitor/CloudWatchClient.pm
index fdfebe7..4d7259d 100644
--- a/lib/App/AWS/CloudWatch/Monitor/CloudWatchClient.pm
+++ b/lib/App/AWS/CloudWatch/Monitor/CloudWatchClient.pm
@@ -122,33 +122,46 @@ Queries meta data for the current EC2 instance.
 sub get_meta_data {
     my $resource  = shift;
     my $use_cache = shift;
-    my $meta_data = read_meta_data( $resource, $meta_data_short_ttl );
+    my ( $ttl_expired, $cache_data ) = read_meta_data( $resource, $meta_data_short_ttl );
 
-    # if we didn't get meta data from the cache (it might not exist, or the cache TTL expired),
-    # get the meta data from the meta-data mount.
-    unless ($meta_data) {
+    if ( $ttl_expired || !$cache_data ) {
         my $base_uri   = 'http://169.254.169.254/latest/meta-data';
-        my $data_value = get $base_uri . $resource;
+        my $mount_data = get $base_uri . $resource;
 
-        if ($data_value) {
-            $meta_data = $data_value;
+        if ($mount_data) {
+            $cache_data = $mount_data;
 
             if ($use_cache) {
-                write_meta_data( $resource, $data_value );
+                write_meta_data( $resource, $mount_data );
             }
         }
+        # although the likelihood is low, there may be circumstances where the TTL of the cache
+        # has expired and the meta-data mount returned no data.  in that case, use the expired
+        # cache data, but warn that expired data is being used.  once the meta-data mount starts
+        # to return data again, the cache will be updated and TTL countdown started over.
         else {
-            warn "meta-data resource $resource returned empty\n";
+            if ( $ttl_expired && $cache_data ) {
+                warn "meta-data resource $resource cache TTL is expired and the meta-data mount failed to return data.\n" .
+                     "expired data from the cache will continue to be used and will persist in the cache until the meta-data mount starts returning data again.\n";
+            }
         }
     }
 
-    return $meta_data;
+    unless ($cache_data) {
+        warn "meta-data resource $resource returned empty\n";
+    }
+
+    return $cache_data;
 }
 
 =item read_meta_data
 
 Reads meta-data from the local filesystem.
 
+Returns a list containing a truthy value indicating the ttl has expired, and the cache data value.
+
+ my ( $ttl_expired, $cache_data ) = read_meta_data( $resource, $meta_data_short_ttl );
+
 =cut
 
 sub read_meta_data {
@@ -163,27 +176,33 @@ sub read_meta_data {
     $meta_data_ttl = $default_ttl if ( !defined($meta_data_ttl) );
 
     my $data_value;
+    my $ttl_expired = 0;
     if ($location) {
         my $filename = $location . $resource;
         if ( -d $filename ) {
             $data_value = `/bin/ls $filename`;
         }
         elsif ( -e $filename ) {
+            my $ret = open( my $file_fh, '<', $filename );
+            unless ($ret) {
+                warn "open: unable to read meta data from filesystem: $!\n";
+                return;
+            }
+            while ( my $line = <$file_fh> )
+                $data_value .= $line;
+            }
+            close $file_fh;
+
             my $updated  = ( stat($filename) )[9];
             my $file_age = time() - $updated;
-            if ( $file_age < $meta_data_ttl ) {
-                open( my $file_fh, '<', $filename )
-                    or warn "open: unable to read meta data from filesystem: $!\n";
-                while ( my $line = <$file_fh> ) {
-                    $data_value .= $line;
-                }
-                close $file_fh;
+            if ( $file_age >= $meta_data_ttl ) {
+                $ttl_expired = 1;
             }
         }
     }
 
     chomp($data_value) if $data_value;
-    return $data_value;
+    return ( $ttl_expired, $data_value );
 }
 
 =item write_meta_data
-----------------------------------------------------------------------

Summary of changes:
 lib/App/AWS/CloudWatch/Monitor/CloudWatchClient.pm | 55 +++++++++++++++-------
 1 file changed, 37 insertions(+), 18 deletions(-)


hooks/post-receive
-- 
app-aws-cloudwatch-monitor


More information about the Bps-public-commit mailing list