|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * <p> |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * <p> |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | +package org.apache.hadoop.hbase.regionserver; |
| 19 | + |
| 20 | +import java.io.IOException; |
| 21 | +import java.util.Arrays; |
| 22 | +import java.util.List; |
| 23 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 24 | +import java.util.concurrent.atomic.AtomicLong; |
| 25 | +import org.apache.hadoop.conf.Configuration; |
| 26 | +import org.apache.hadoop.fs.FileStatus; |
| 27 | +import org.apache.hadoop.fs.Path; |
| 28 | +import org.apache.hadoop.hbase.ScheduledChore; |
| 29 | +import org.apache.hadoop.hbase.Stoppable; |
| 30 | +import org.apache.hadoop.hbase.io.HFileLink; |
| 31 | +import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; |
| 32 | +import org.apache.hadoop.ipc.RemoteException; |
| 33 | +import org.apache.yetus.audience.InterfaceAudience; |
| 34 | +import org.slf4j.Logger; |
| 35 | +import org.slf4j.LoggerFactory; |
| 36 | + |
| 37 | +/** |
| 38 | + * This Chore, every time it runs, will clear the unsused HFiles in the data |
| 39 | + * folder. |
| 40 | + */ |
| 41 | +@InterfaceAudience.Private |
| 42 | +public class BrokenStoreFileCleaner extends ScheduledChore { |
| 43 | + private static final Logger LOG = LoggerFactory.getLogger(BrokenStoreFileCleaner.class); |
| 44 | + public static final String BROKEN_STOREFILE_CLEANER_ENABLED = |
| 45 | + "hbase.region.broken.storefilecleaner.enabled"; |
| 46 | + public static final boolean DEFAULT_BROKEN_STOREFILE_CLEANER_ENABLED = false; |
| 47 | + public static final String BROKEN_STOREFILE_CLEANER_TTL = |
| 48 | + "hbase.region.broken.storefilecleaner.ttl"; |
| 49 | + public static final long DEFAULT_BROKEN_STOREFILE_CLEANER_TTL = 1000 * 60 * 60 * 12; //12h |
| 50 | + public static final String BROKEN_STOREFILE_CLEANER_DELAY = |
| 51 | + "hbase.region.broken.storefilecleaner.delay"; |
| 52 | + public static final int DEFAULT_BROKEN_STOREFILE_CLEANER_DELAY = 1000 * 60 * 60 * 2; //2h |
| 53 | + public static final String BROKEN_STOREFILE_CLEANER_DELAY_JITTER = |
| 54 | + "hbase.region.broken.storefilecleaner.delay.jitter"; |
| 55 | + public static final double DEFAULT_BROKEN_STOREFILE_CLEANER_DELAY_JITTER = 0.25D; |
| 56 | + public static final String BROKEN_STOREFILE_CLEANER_PERIOD = |
| 57 | + "hbase.region.broken.storefilecleaner.period"; |
| 58 | + public static final int DEFAULT_BROKEN_STOREFILE_CLEANER_PERIOD = 1000 * 60 * 60 * 6; //6h |
| 59 | + |
| 60 | + private HRegionServer regionServer; |
| 61 | + private final AtomicBoolean enabled = new AtomicBoolean(true); |
| 62 | + private long fileTtl; |
| 63 | + |
| 64 | + public BrokenStoreFileCleaner(final int delay, final int period, final Stoppable stopper, |
| 65 | + Configuration conf, HRegionServer regionServer) { |
| 66 | + super("BrokenStoreFileCleaner", stopper, period, delay); |
| 67 | + this.regionServer = regionServer; |
| 68 | + setEnabled( |
| 69 | + conf.getBoolean(BROKEN_STOREFILE_CLEANER_ENABLED, DEFAULT_BROKEN_STOREFILE_CLEANER_ENABLED)); |
| 70 | + fileTtl = conf.getLong(BROKEN_STOREFILE_CLEANER_TTL, DEFAULT_BROKEN_STOREFILE_CLEANER_TTL); |
| 71 | + } |
| 72 | + |
| 73 | + public boolean setEnabled(final boolean enabled) { |
| 74 | + return this.enabled.getAndSet(enabled); |
| 75 | + } |
| 76 | + |
| 77 | + public boolean getEnabled() { |
| 78 | + return this.enabled.get(); |
| 79 | + } |
| 80 | + |
| 81 | + @Override |
| 82 | + public void chore() { |
| 83 | + if (getEnabled()) { |
| 84 | + long start = EnvironmentEdgeManager.currentTime(); |
| 85 | + AtomicLong deletedFiles = new AtomicLong(0); |
| 86 | + AtomicLong failedDeletes = new AtomicLong(0); |
| 87 | + for (HRegion region : regionServer.getRegions()) { |
| 88 | + for (HStore store : region.getStores()) { |
| 89 | + //only do cleanup in stores not using tmp directories |
| 90 | + if (store.getStoreEngine().requireWritingToTmpDirFirst()) { |
| 91 | + continue; |
| 92 | + } |
| 93 | + Path storePath = |
| 94 | + new Path(region.getRegionFileSystem().getRegionDir(), store.getColumnFamilyName()); |
| 95 | + |
| 96 | + try { |
| 97 | + List<FileStatus> fsStoreFiles = |
| 98 | + Arrays.asList(region.getRegionFileSystem().fs.listStatus(storePath)); |
| 99 | + fsStoreFiles.forEach( |
| 100 | + file -> cleanFileIfNeeded(file, store, deletedFiles, failedDeletes)); |
| 101 | + } catch (IOException e) { |
| 102 | + LOG.warn("Failed to list files in {}, cleanup is skipped there",storePath); |
| 103 | + continue; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + LOG.debug( |
| 108 | + "BrokenStoreFileCleaner on {} run for: {}ms. It deleted {} files and tried but failed " |
| 109 | + + "to delete {}", |
| 110 | + regionServer.getServerName().getServerName(), EnvironmentEdgeManager.currentTime() - start, |
| 111 | + deletedFiles.get(), failedDeletes.get()); |
| 112 | + } else { |
| 113 | + LOG.trace("Broken storefile Cleaner chore disabled! Not cleaning."); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + private void cleanFileIfNeeded(FileStatus file, HStore store, |
| 118 | + AtomicLong deletedFiles, AtomicLong failedDeletes) { |
| 119 | + if(file.isDirectory()){ |
| 120 | + LOG.trace("This is a Directory {}, skip cleanup", file.getPath()); |
| 121 | + return; |
| 122 | + } |
| 123 | + |
| 124 | + if(!validate(file.getPath())){ |
| 125 | + LOG.trace("Invalid file {}, skip cleanup", file.getPath()); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + if(!isOldEnough(file)){ |
| 130 | + LOG.trace("Fresh file {}, skip cleanup", file.getPath()); |
| 131 | + return; |
| 132 | + } |
| 133 | + |
| 134 | + if(isActiveStorefile(file, store)){ |
| 135 | + LOG.trace("Actively used storefile file {}, skip cleanup", file.getPath()); |
| 136 | + return; |
| 137 | + } |
| 138 | + |
| 139 | + // Compacted files can still have readers and are cleaned by a separate chore, so they have to |
| 140 | + // be skipped here |
| 141 | + if(isCompactedFile(file, store)){ |
| 142 | + LOG.trace("Cleanup is done by a different chore for file {}, skip cleanup", file.getPath()); |
| 143 | + return; |
| 144 | + } |
| 145 | + |
| 146 | + if(isCompactionResultFile(file, store)){ |
| 147 | + LOG.trace("The file is the result of an ongoing compaction {}, skip cleanup", file.getPath()); |
| 148 | + return; |
| 149 | + } |
| 150 | + |
| 151 | + deleteFile(file, store, deletedFiles, failedDeletes); |
| 152 | + } |
| 153 | + |
| 154 | + private boolean isCompactionResultFile(FileStatus file, HStore store) { |
| 155 | + return store.getStoreEngine().getCompactor().getCompactionTargets().contains(file.getPath()); |
| 156 | + } |
| 157 | + |
| 158 | + // Compacted files can still have readers and are cleaned by a separate chore, so they have to |
| 159 | + // be skipped here |
| 160 | + private boolean isCompactedFile(FileStatus file, HStore store) { |
| 161 | + return store.getStoreEngine().getStoreFileManager().getCompactedfiles().stream() |
| 162 | + .anyMatch(sf -> sf.getPath().equals(file.getPath())); |
| 163 | + } |
| 164 | + |
| 165 | + private boolean isActiveStorefile(FileStatus file, HStore store) { |
| 166 | + return store.getStoreEngine().getStoreFileManager().getStorefiles().stream() |
| 167 | + .anyMatch(sf -> sf.getPath().equals(file.getPath())); |
| 168 | + } |
| 169 | + |
| 170 | + boolean validate(Path file) { |
| 171 | + if (HFileLink.isBackReferencesDir(file) || HFileLink.isBackReferencesDir(file.getParent())) { |
| 172 | + return true; |
| 173 | + } |
| 174 | + return StoreFileInfo.validateStoreFileName(file.getName()); |
| 175 | + } |
| 176 | + |
| 177 | + boolean isOldEnough(FileStatus file){ |
| 178 | + return file.getModificationTime() + fileTtl < EnvironmentEdgeManager.currentTime(); |
| 179 | + } |
| 180 | + |
| 181 | + private void deleteFile(FileStatus file, HStore store, AtomicLong deletedFiles, |
| 182 | + AtomicLong failedDeletes) { |
| 183 | + Path filePath = file.getPath(); |
| 184 | + LOG.debug("Removing {} from store", filePath); |
| 185 | + try { |
| 186 | + boolean success = store.getFileSystem().delete(filePath, false); |
| 187 | + if (!success) { |
| 188 | + failedDeletes.incrementAndGet(); |
| 189 | + LOG.warn("Attempted to delete:" + filePath |
| 190 | + + ", but couldn't. Attempt to delete on next pass."); |
| 191 | + } |
| 192 | + else{ |
| 193 | + deletedFiles.incrementAndGet(); |
| 194 | + } |
| 195 | + } catch (IOException e) { |
| 196 | + e = e instanceof RemoteException ? |
| 197 | + ((RemoteException)e).unwrapRemoteException() : e; |
| 198 | + LOG.warn("Error while deleting: " + filePath, e); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | +} |
0 commit comments