001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.master.cleaner; 019 020import java.time.Instant; 021import java.time.ZoneOffset; 022import java.time.format.DateTimeFormatter; 023import org.apache.hadoop.conf.Configuration; 024import org.apache.hadoop.fs.FileStatus; 025import org.apache.hadoop.fs.Path; 026import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 027import org.apache.yetus.audience.InterfaceAudience; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031/** 032 * Base class for time to live file cleaner. 033 */ 034@InterfaceAudience.Private 035public abstract class BaseTimeToLiveFileCleaner extends BaseLogCleanerDelegate { 036 037 private static final Logger LOG = 038 LoggerFactory.getLogger(BaseTimeToLiveFileCleaner.class.getName()); 039 040 private static final DateTimeFormatter FORMATTER = 041 DateTimeFormatter.ISO_DATE_TIME.withZone(ZoneOffset.systemDefault()); 042 043 // Configured time a log can be kept after it was closed 044 private long ttlMs; 045 046 private volatile boolean stopped = false; 047 048 @Override 049 public final void setConf(Configuration conf) { 050 super.setConf(conf); 051 this.ttlMs = getTtlMs(conf); 052 } 053 054 @Override 055 public boolean isFileDeletable(FileStatus status) { 056 // Files are validated for the second time here, 057 // if it causes a bottleneck this logic needs refactored 058 if (!valiateFilename(status.getPath())) { 059 return true; 060 } 061 long currentTime = EnvironmentEdgeManager.currentTime(); 062 long time = status.getModificationTime(); 063 long life = currentTime - time; 064 065 if (LOG.isTraceEnabled()) { 066 LOG.trace("File life:{}ms, ttl:{}ms, current:{}, from{}", life, ttlMs, 067 FORMATTER.format(Instant.ofEpochMilli(currentTime)), 068 FORMATTER.format(Instant.ofEpochMilli(time))); 069 } 070 if (life < 0) { 071 LOG.warn("Found a file ({}) newer than current time ({} < {}), probably a clock skew", 072 status.getPath(), FORMATTER.format(Instant.ofEpochMilli(currentTime)), 073 FORMATTER.format(Instant.ofEpochMilli(time))); 074 return false; 075 } 076 return life > ttlMs; 077 } 078 079 @Override 080 public void stop(String why) { 081 this.stopped = true; 082 } 083 084 @Override 085 public boolean isStopped() { 086 return this.stopped; 087 } 088 089 protected abstract long getTtlMs(Configuration conf); 090 091 protected abstract boolean valiateFilename(Path file); 092}