-
Notifications
You must be signed in to change notification settings - Fork 588
HDDS-4346.Ozone specific Trash Policy #1535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
e52ba21
HDDS-4346.Ozone specific Trash Policy
sadanand48 fbad42e
fix failing test
sadanand48 89bea76
Addressed review comment
sadanand48 b27a063
remove initialise method as it uses the parent implementation
sadanand48 a39e1a6
call ozone trash emptier from OM
sadanand48 d7c30ee
trigger new CI check
sadanand48 080eed0
addressed review comments
sadanand48 b2297c2
trigger new CI check
sadanand48 560b644
trigger new CI check
sadanand48 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
234 changes: 234 additions & 0 deletions
234
hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/TrashPolicyOzone.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,234 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.apache.hadoop.ozone.om; | ||
|
|
||
| import java.io.FileNotFoundException; | ||
| import java.io.IOException; | ||
| import java.text.DateFormat; | ||
| import java.text.ParseException; | ||
| import java.text.SimpleDateFormat; | ||
| import java.util.Collection; | ||
| import java.util.Date; | ||
|
|
||
| import org.apache.hadoop.conf.Configuration; | ||
| import org.apache.hadoop.fs.FileStatus; | ||
| import org.apache.hadoop.fs.FileSystem; | ||
| import org.apache.hadoop.fs.Path; | ||
| import org.apache.hadoop.fs.TrashPolicyDefault; | ||
| import org.apache.hadoop.fs.permission.FsAction; | ||
| import org.apache.hadoop.fs.permission.FsPermission; | ||
| import org.apache.hadoop.fs.FileAlreadyExistsException; | ||
| import org.apache.hadoop.util.Time; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| /** TrashPolicy for Ozone Specific Trash Operations.Through this implementation | ||
sadanand48 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| * of TrashPolicy ozone-specific trash optimizations are/will be made such as | ||
| * having a multithreaded TrashEmptier. | ||
| */ | ||
| public class TrashPolicyOzone extends TrashPolicyDefault { | ||
|
|
||
| private static final Logger LOG = | ||
| LoggerFactory.getLogger(TrashPolicyOzone.class); | ||
|
|
||
| private static final Path CURRENT = new Path("Current"); | ||
|
|
||
| private static final FsPermission PERMISSION = | ||
| new FsPermission(FsAction.ALL, FsAction.NONE, FsAction.NONE); | ||
|
|
||
| private static final DateFormat CHECKPOINT = new SimpleDateFormat( | ||
| "yyMMddHHmmss"); | ||
| /** Format of checkpoint directories used prior to Hadoop 0.23. */ | ||
| private static final DateFormat OLD_CHECKPOINT = | ||
| new SimpleDateFormat("yyMMddHHmm"); | ||
| private static final int MSECS_PER_MINUTE = 60*1000; | ||
|
|
||
| private long emptierInterval; | ||
|
|
||
| public TrashPolicyOzone(){ | ||
| } | ||
|
|
||
| private TrashPolicyOzone(FileSystem fs, Configuration conf){ | ||
| super.initialize(conf, fs); | ||
| } | ||
|
|
||
| @Override | ||
| public Runnable getEmptier() throws IOException { | ||
| return new TrashPolicyOzone.Emptier(getConf(), emptierInterval); | ||
| } | ||
|
|
||
| protected class Emptier implements Runnable { | ||
|
|
||
| private Configuration conf; | ||
| // same as checkpoint interval | ||
| private long emptierInterval; | ||
|
|
||
| Emptier(Configuration conf, long emptierInterval) throws IOException { | ||
| this.conf = conf; | ||
| this.emptierInterval = emptierInterval; | ||
| if (emptierInterval > deletionInterval || emptierInterval <= 0) { | ||
| LOG.info("The configured checkpoint interval is " + | ||
| (emptierInterval / MSECS_PER_MINUTE) + " minutes." + | ||
| " Using an interval of " + | ||
| (deletionInterval / MSECS_PER_MINUTE) + | ||
| " minutes that is used for deletion instead"); | ||
| this.emptierInterval = deletionInterval; | ||
| } | ||
| LOG.info("Ozone Manager trash configuration: Deletion interval = " | ||
| + (deletionInterval / MSECS_PER_MINUTE) | ||
| + " minutes, Emptier interval = " | ||
| + (this.emptierInterval / MSECS_PER_MINUTE) + " minutes."); | ||
| } | ||
|
|
||
| @Override | ||
| public void run() { | ||
| if (emptierInterval == 0) { | ||
| return; // trash disabled | ||
| } | ||
| long now, end; | ||
| while (true) { | ||
| now = Time.now(); | ||
| end = ceiling(now, emptierInterval); | ||
| try { // sleep for interval | ||
| Thread.sleep(end - now); | ||
| } catch (InterruptedException e) { | ||
| break; // exit on interrupt | ||
| } | ||
|
|
||
| try { | ||
| now = Time.now(); | ||
| if (now >= end) { | ||
| Collection<FileStatus> trashRoots; | ||
| trashRoots = fs.getTrashRoots(true); // list all trash dirs | ||
|
|
||
| for (FileStatus trashRoot : trashRoots) { // dump each trash | ||
| if (!trashRoot.isDirectory()) { | ||
| continue; | ||
| } | ||
| try { | ||
| TrashPolicyOzone trash = new TrashPolicyOzone(fs, conf); | ||
| trash.deleteCheckpoint(trashRoot.getPath(), false); | ||
| trash.createCheckpoint(trashRoot.getPath(), new Date(now)); | ||
| } catch (IOException e) { | ||
| LOG.warn("Trash caught: "+e+". Skipping " + | ||
| trashRoot.getPath() + "."); | ||
| } | ||
| } | ||
| } | ||
| } catch (Exception e) { | ||
| LOG.warn("RuntimeException during Trash.Emptier.run(): ", e); | ||
| } | ||
| } | ||
| try { | ||
| fs.close(); | ||
| } catch(IOException e) { | ||
| LOG.warn("Trash cannot close FileSystem: ", e); | ||
| } | ||
| } | ||
|
|
||
| private long ceiling(long time, long interval) { | ||
| return floor(time, interval) + interval; | ||
| } | ||
| private long floor(long time, long interval) { | ||
| return (time / interval) * interval; | ||
| } | ||
|
|
||
| } | ||
|
|
||
| private void createCheckpoint(Path trashRoot, Date date) throws IOException { | ||
| if (!fs.exists(new Path(trashRoot, CURRENT))) { | ||
| return; | ||
| } | ||
| Path checkpointBase; | ||
| synchronized (CHECKPOINT) { | ||
| checkpointBase = new Path(trashRoot, CHECKPOINT.format(date)); | ||
| } | ||
| Path checkpoint = checkpointBase; | ||
| Path current = new Path(trashRoot, CURRENT); | ||
|
|
||
| int attempt = 0; | ||
| while (true) { | ||
| try { | ||
| fs.rename(current, checkpoint); | ||
| LOG.info("Created trash checkpoint: " + checkpoint.toUri().getPath()); | ||
| break; | ||
| } catch (FileAlreadyExistsException e) { | ||
| if (++attempt > 1000) { | ||
| throw new IOException("Failed to checkpoint trash: " + checkpoint); | ||
| } | ||
| checkpoint = checkpointBase.suffix("-" + attempt); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void deleteCheckpoint(Path trashRoot, boolean deleteImmediately) | ||
| throws IOException { | ||
| LOG.info("TrashPolicyOzone#deleteCheckpoint for trashRoot: " + trashRoot); | ||
|
|
||
| FileStatus[] dirs = null; | ||
| try { | ||
| dirs = fs.listStatus(trashRoot); // scan trash sub-directories | ||
| } catch (FileNotFoundException fnfe) { | ||
| return; | ||
| } | ||
|
|
||
| long now = Time.now(); | ||
| for (int i = 0; i < dirs.length; i++) { | ||
| Path path = dirs[i].getPath(); | ||
| String dir = path.toUri().getPath(); | ||
| String name = path.getName(); | ||
| if (name.equals(CURRENT.getName())) { // skip current | ||
| continue; | ||
| } | ||
|
|
||
| long time; | ||
| try { | ||
| time = getTimeFromCheckpoint(name); | ||
| } catch (ParseException e) { | ||
| LOG.warn("Unexpected item in trash: "+dir+". Ignoring."); | ||
| continue; | ||
| } | ||
|
|
||
| if (((now - deletionInterval) > time) || deleteImmediately) { | ||
| if (fs.delete(path, true)) { | ||
| LOG.info("Deleted trash checkpoint: "+dir); | ||
| } else { | ||
| LOG.warn("Couldn't delete checkpoint: " + dir + " Ignoring."); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private long getTimeFromCheckpoint(String name) throws ParseException { | ||
| long time; | ||
|
|
||
| try { | ||
| synchronized (CHECKPOINT) { | ||
| time = CHECKPOINT.parse(name).getTime(); | ||
| } | ||
| } catch (ParseException pe) { | ||
| // Check for old-style checkpoint directories left over | ||
| // after an upgrade from Hadoop 1.x | ||
| synchronized (OLD_CHECKPOINT) { | ||
| time = OLD_CHECKPOINT.parse(name).getTime(); | ||
| } | ||
| } | ||
|
|
||
| return time; | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.