FilePickUpPlaceHealthCheck.java

  1. package emissary.core;

  2. import emissary.pickup.file.FilePickUpPlace;

  3. import com.codahale.metrics.health.HealthCheck;
  4. import org.apache.commons.io.filefilter.HiddenFileFilter;

  5. import java.io.File;
  6. import java.io.FileFilter;
  7. import java.lang.reflect.Field;

  8. /**
  9.  * A health check that warns if the input data queue is larger than a given threshold or if the aggregate file size is
  10.  * larger than a given threshold. This only applies to FilePickUpPlace.
  11.  */
  12. public class FilePickUpPlaceHealthCheck extends HealthCheck {

  13.     /**
  14.      * How many files to allow in the queue before we deem the situation unhealthy
  15.      */
  16.     protected int maxFileCountBeforeUnhealthy = Integer.MAX_VALUE; // default is unbounded

  17.     /**
  18.      * How large we let the aggregate file size to get before we deem the situation unhealthy
  19.      */
  20.     protected long maxAggregateFileSizeBeforeUnhealthy = Long.MAX_VALUE; // default is unbounded

  21.     public FilePickUpPlaceHealthCheck(final int maxFileCountBeforeUnhealthy, final long maxAggregateFileSizeBeforeUnhealthy) {
  22.         this.maxFileCountBeforeUnhealthy = maxFileCountBeforeUnhealthy;
  23.         this.maxAggregateFileSizeBeforeUnhealthy = maxAggregateFileSizeBeforeUnhealthy;
  24.     }

  25.     @Override
  26.     protected Result check() throws Exception {

  27.         int aggregateFileCount = 0; // how many things we think are in the queue
  28.         long aggregateFileSize = 0; // aggregate file size across all files

  29.         try {
  30.             final FilePickUpPlace pup = (FilePickUpPlace) Namespace.lookup("FilePickUpPlace"); // if no exception, will
  31.                                                                                                // not be
  32.                                                                                                // null

  33.             // Get the inputDirs by reflection, result should be a String[]
  34.             final Field inputDirField = pup.getClass().getDeclaredField("inputDataDirs");
  35.             inputDirField.setAccessible(true); // make it so we can read it

  36.             if (inputDirField.getType().isArray()) {
  37.                 // This better be a String[], but we are not checking for the String part
  38.                 final String[] inputDirs = (String[]) inputDirField.get(pup);
  39.                 if (inputDirs != null) {
  40.                     for (int dirIdx = 0; dirIdx < inputDirs.length; dirIdx++) {
  41.                         final File inputDir = new File(inputDirs[dirIdx]);
  42.                         if ((inputDir != null) && inputDir.isDirectory()) {
  43.                             final File[] files = inputDir.listFiles((FileFilter) HiddenFileFilter.VISIBLE);
  44.                             aggregateFileCount += files.length;
  45.                             if (files != null) {
  46.                                 for (int fileIdx = 0; fileIdx < files.length; fileIdx++) {
  47.                                     aggregateFileSize += files[fileIdx].length();
  48.                                 }
  49.                             }
  50.                         }
  51.                     }
  52.                 }
  53.             }

  54.             // Check either condition
  55.             if (aggregateFileCount > this.maxFileCountBeforeUnhealthy) {
  56.                 return Result.unhealthy("Large number of files backed up for FilePickUpPlace = " + aggregateFileCount);
  57.             } else if (aggregateFileSize > this.maxAggregateFileSizeBeforeUnhealthy) {
  58.                 return Result.unhealthy("Large aggregate file size backed up for FilePickUpPlace = " + aggregateFileSize);
  59.             }

  60.         } catch (NamespaceException ne) {
  61.             // This gets throw if can't find FilePickUpPlace, assume it is not configured
  62.             // and things are healthy
  63.         } catch (NoSuchFieldException nsfe) {
  64.             // Possibly thrown by getDeclaredField, assume FilePickUpPlace implementation has changed
  65.         } catch (SecurityException se) {
  66.             // Possibly thrown by getDeclaredField - would prevent our access to the field
  67.         } catch (NullPointerException npe) {
  68.             // A variety of methods throw NPEs
  69.         } catch (IllegalArgumentException iae) {
  70.             // Thrown by get()
  71.         } catch (IllegalAccessException iae) {
  72.             // Also thrown by get()
  73.         } catch (ExceptionInInitializerError eiie) {
  74.             // Also thrown by get()
  75.         }

  76.         // If we get here, we assume things are OK
  77.         return Result.healthy("FilePickUpPlace appears to be healthy");
  78.     }


  79. }