Advertisement
Guest User

Untitled

a guest
Apr 20th, 2019
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1. package com.xiaojukeji.chronos.workers;
  2.  
  3. import com.google.common.base.Charsets;
  4. import com.xiaojukeji.chronos.config.ConfigManager;
  5. import com.xiaojukeji.chronos.config.DeleteConfig;
  6. import com.xiaojukeji.chronos.db.CFManager;
  7. import com.xiaojukeji.chronos.db.RDB;
  8. import com.xiaojukeji.chronos.services.MetaService;
  9. import org.apache.commons.lang3.concurrent.BasicThreadFactory;
  10. import org.slf4j.Logger;
  11. import org.slf4j.LoggerFactory;
  12.  
  13. import java.text.SimpleDateFormat;
  14. import java.util.concurrent.ScheduledExecutorService;
  15. import java.util.concurrent.ScheduledThreadPoolExecutor;
  16. import java.util.concurrent.TimeUnit;
  17.  
  18.  
  19. public class DeleteBgWorker {
  20.     private static final Logger LOGGER = LoggerFactory.getLogger(DeleteBgWorker.class);
  21.  
  22.     private static final DeleteConfig DELETE_CONFIG = ConfigManager.getConfig().getDeleteConfig();
  23.     private static final int SAVE_HOURS_OF_DATA = DELETE_CONFIG.getSaveHours();
  24.     private static final long INITIAL_DELAY_MINUTES = 1; // 1 分钟
  25.     private static final long PERIOD_MINUTES = 10;       // 10 分钟
  26.  
  27.     private static volatile DeleteBgWorker instance = null;
  28.     private static final SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  29.  
  30.     /**
  31.      * 2017/10/13 00:00:00
  32.      */
  33.     private static final long MIN_TIMESTAMP = 1507824000;
  34.  
  35.     private static final ScheduledExecutorService SCHEDULE = new ScheduledThreadPoolExecutor(1,
  36.             new BasicThreadFactory.Builder().namingPattern("delete-bg-worker-schedule-%d").daemon(true).build());
  37.  
  38.     private DeleteBgWorker() {
  39.     }
  40.  
  41.     public void start() {
  42.         SCHEDULE.scheduleAtFixedRate(() -> {
  43.             byte[] beginKey = String.valueOf(MIN_TIMESTAMP).getBytes(Charsets.UTF_8);
  44.  
  45.             final long seekTimestampInSecond = MetaService.getSeekTimestamp();
  46.             byte[] endKey = String.valueOf(seekTimestampInSecond - SAVE_HOURS_OF_DATA * 60 * 60).getBytes(Charsets.UTF_8);
  47.             deleteRange(beginKey, endKey);
  48.         }, INITIAL_DELAY_MINUTES, PERIOD_MINUTES, TimeUnit.MINUTES);
  49.         LOGGER.info("DeleteBgWorker has started, initialDelayInMinutes:{}", INITIAL_DELAY_MINUTES);
  50.     }
  51.  
  52.     private void deleteRange(final byte[] beginKey, final byte[] endKey) {
  53.         LOGGER.info("deleteRange start, beginKey:{}, endKey:{}", new String(beginKey), new String(endKey));
  54.         final long start = System.currentTimeMillis();
  55.         RDB.deleteFilesInRange(CFManager.CFH_DEFAULT, beginKey, endKey);
  56.         LOGGER.info("deleteRange end, beginKey:{}({}), endKey:{}({}), cost:{}ms",
  57.                 new String(beginKey), formatter.format(Long.parseLong(new String(beginKey)) * 1000),
  58.                 new String(endKey), formatter.format(Long.parseLong(new String(endKey)) * 1000),
  59.                 System.currentTimeMillis() - start);
  60.     }
  61.  
  62.     public void stop() {
  63.         SCHEDULE.shutdownNow();
  64.         while (!SCHEDULE.isShutdown()) {
  65.             LOGGER.info("DeleteBgWorker is shutting down!");
  66.             try {
  67.                 TimeUnit.SECONDS.sleep(1);
  68.             } catch (InterruptedException e) {
  69.                 LOGGER.info("DeleteBgWorker was forced to shutdown, err:{}", e.getMessage(), e);
  70.             }
  71.         }
  72.         LOGGER.info("DeleteBgWorker was shutdown!");
  73.     }
  74.  
  75.     public static DeleteBgWorker getInstance() {
  76.         if (instance == null) {
  77.             synchronized (DeleteBgWorker.class) {
  78.                 if (instance == null) {
  79.                     instance = new DeleteBgWorker();
  80.                 }
  81.             }
  82.         }
  83.         return instance;
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement