Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.clustermate.service.cleanup;
  
  
  import org.slf4j.Logger;
 
Helper class that handles details of background cleanup processing, related to data storage and expiration.
 
 public class CleanerUpper<K extends EntryKey, E extends StoredEntry<K>>
     implements RunnableStartAndStoppable
 {
     private final Logger LOG = LoggerFactory.getLogger(getClass());
 
     /*
     /**********************************************************************
     /* Configuration
     /**********************************************************************
      */
     
     protected final TimeSpan _delayBetweenCleanups;
     
     /*
     /**********************************************************************
     /* Related helper objects
     /**********************************************************************
      */
 
     protected final TimeMaster _timeMaster;
    
    
Object that keeps track of observed cluster state.
 
     protected ClusterViewByServer _cluster;
 
     /*
     /**********************************************************************
     /* Other state
     /**********************************************************************
      */

    
Flag used for communicating need to shut down processing.
 
     protected final AtomicBoolean _shutdown = new AtomicBoolean(false);

    
Actual clean up thread we use
 
     protected Thread _thread;
    
    
Timestamp of next time that cleanup tasks get triggered.
 
     protected AtomicLong _nextStartTime = new AtomicLong(0L);
    
    
Reference to currently active cleanup component; used for producing status descriptions.
 
     protected AtomicReference<CleanupTask<?>> _currentTask = new AtomicReference<CleanupTask<?>>();
 
     protected final CleanupTask<?>[] _tasks;
     
     /*
     /**********************************************************************
     /* Life-cycle
     /**********************************************************************
      */
     
     public CleanerUpper(SharedServiceStuff stuffStores<K,E> stores,
             ClusterViewByServer cluster)
     {
          = cluster;
          = stuff.getTimeMaster();
         // Important: start with LocalEntryCleaner (to try to avoid dangling files),
         // then do FileCleaner
          = new CleanupTask[] {
                 new LocalEntryCleaner<K,E>(stuffstores),
                 new FileCleaner(stuff)
         };
     }
 
     @Override
     public void start()
    {
        // let's wait 50% of minimum delay; typically 30 minutes
//        long delayMsecs = (_delayBetweenCleanups.toMilliseconds() / 2);
        long delayMsecs = 5000L;
        
        .set(.currentTimeMillis() + delayMsecs);
         = new Thread(this);
        .start();
    }
    @Override
    public void stop()
    {
        .set(true);
        if ( != null) {
            .interrupt();
        }
    }
    /*
    /**********************************************************************
    /* Main loop
    /**********************************************************************
     */
    @Override
    public void run()
    {
        while (!.get()) {
            long delayMsecs = .get() - .currentTimeMillis();
            if (delayMsecs > 0L) {
                .info("Waiting up to {} seconds until running cleanup tasks...",
                        (delayMsecs / 1000L));
                try {
                    .sleep(delayMsecs);
                } catch (InterruptedException e) {
                    continue;
                }
            }
            final long startTime = .currentTimeMillis();
            // ok, run...
            .info("Starting cleanup tasks ({})".);
            .set(startTime + .getMillis());
            for (CleanupTask<?> task : ) {
                .set(task);
                try {
                    Object result = task.cleanUp();
                    long took = .currentTimeMillis() - startTime;
                    .info("Clean up task {} complete in {}, result: {}",
                            task.getClass().getName(), TimeMaster.timeDesc(took), result);
                } catch (Exception e) {
                    .warn("Problems running clean up task of type "+task.getClass().getName()+": "+e.getMessage(), e);
                }
            }
            .set(null);
            long tookAll = .currentTimeMillis() - startTime;
            .info("Completing clean up tasks in {}", TimeMaster.timeDesc(tookAll));
        }
    }
    /*
    /**********************************************************************
    /* Other methods
    /**********************************************************************
     */

    
Method is overridden to provide somewhat readable description of current state, to be served externally.
    @Override
    public String toString()
    {
        CleanupTask<?> task = .get();
        if (task != null) {
            return "Current task: "+task.toString();
        }
        long msecs = .get() - .currentTimeMillis();
        if (msecs < 0L) {
            msecs = 0L;
        }
        return "Waiting for "+TimeMaster.timeDesc(msecs)+" until next cleanup round";
    }
New to GrepCode? Check out our FAQ X