-
Notifications
You must be signed in to change notification settings - Fork 750
[GOBBLIN-2168] Add TimeBasedSnapshotCleanupPolicyTest and Fix a bug with props.containsKey() instead of props.contains() in Trash class #4070
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
Open
linweihs
wants to merge
9
commits into
apache:master
Choose a base branch
from
linweihs:welin/trash
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ed01119
Compare with the current time in UTC
linweihs 6bfb281
clean up code
linweihs e864d80
address comments
linweihs 0ab6daa
update unit tests
linweihs 3ead802
address comments
linweihs c5c4892
address comment
linweihs 401fb30
address comments
linweihs f749ab9
Fix bug where azkaban prop.contains(object) is checking value not key…
linweihs 5b6fa8f
Mock the FileStatus object to return the desired path
linweihs 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
98 changes: 98 additions & 0 deletions
98
...est/java/org/apache/gobblin/data/management/trash/TimeBasedSnapshotCleanupPolicyTest.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,98 @@ | ||
| /* | ||
| * 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.gobblin.data.management.trash; | ||
|
|
||
| import org.apache.hadoop.fs.FileStatus; | ||
| import org.apache.hadoop.fs.Path; | ||
| import org.joda.time.DateTime; | ||
| import org.joda.time.DateTimeZone; | ||
| import org.testng.Assert; | ||
| import org.testng.annotations.Test; | ||
| import org.testng.annotations.BeforeMethod; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.Properties; | ||
|
|
||
| public class TimeBasedSnapshotCleanupPolicyTest { | ||
|
|
||
| private MockTimeBasedSnapshotCleanupPolicy cleanupPolicy; | ||
|
|
||
| @BeforeMethod | ||
| public void setUp() { | ||
| // Initialize the cleanup policy with a retention period (e.g., 1 day) | ||
| Properties properties = new Properties(); | ||
| properties.setProperty(MockTimeBasedSnapshotCleanupPolicy.SNAPSHOT_RETENTION_POLICY_MINUTES_KEY, "1440"); // 1440 minutes = 1 day | ||
| properties.setProperty(MockTimeBasedSnapshotCleanupPolicy.RETENTION_SNAPSHOT_TIMEZONE, "UTC"); | ||
| // Mock the cutoff time to be 2024-10-30 01:01:00 UTC | ||
| cleanupPolicy = new MockTimeBasedSnapshotCleanupPolicy(properties, new DateTime(2024, 10, 30, 1, 1)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testShouldDeleteSnapshot() throws IOException { | ||
|
|
||
| // Create a Trash | ||
| TrashTestBase trashTestBase = new TrashTestBase(new Properties()); | ||
| Trash trash = trashTestBase.trash; | ||
|
|
||
| // Create dummy paths | ||
| FileStatus fs1 = new FileStatus(0, true, 0, 0, 0, | ||
| new Path(trash.getTrashLocation(), new DateTime(2024, 10, 29, 1, 0, DateTimeZone.UTC).toString(Trash.TRASH_SNAPSHOT_NAME_FORMATTER))); | ||
| FileStatus fs2 = new FileStatus(0, true, 0, 0, 0, | ||
| new Path(trash.getTrashLocation(), new DateTime(2024, 10, 29, 2, 0, DateTimeZone.UTC).toString(Trash.TRASH_SNAPSHOT_NAME_FORMATTER))); | ||
|
|
||
| // Test old snapshot (should be deleted) | ||
| // 2024-10-29 01:00:00 UTC + 1440 minutes = 2024-10-30 01:00:00 UTC < Cutoff time a.k.a system current_time (2024-10-30 01:01:00 UTC) | ||
| Assert.assertTrue(cleanupPolicy.shouldDeleteSnapshot(fs1, trash), "Old snapshot should be deleted"); | ||
|
|
||
| // Test snapshot (should not be deleted) | ||
| // 2024-10-29 02:00:00 UTC + 1440 minutes = 2024-10-30 02:00:00 UTC > Cutoff time a.k.a system current_time (2024-10-30 01:01:00 UTC) | ||
| Assert.assertFalse(cleanupPolicy.shouldDeleteSnapshot(fs2, trash), "snapshot should not be deleted"); | ||
| } | ||
|
|
||
| /** | ||
| * Mock the TimeBasedSnapshotCleanupPolicy for testing purposes | ||
| * | ||
| * In this class, the current time used in the comparison method isBefore() can be mocked | ||
| * Why? The current time is used to determine if a snapshot is older than the retention period, | ||
| * and given that the current time is always changing, it is difficult to test the method shouldDeleteSnapshot() | ||
| */ | ||
| public class MockTimeBasedSnapshotCleanupPolicy implements SnapshotCleanupPolicy { | ||
|
|
||
| public static final String SNAPSHOT_RETENTION_POLICY_MINUTES_KEY = "gobblin.trash.snapshot.retention.minutes"; | ||
| public static final int SNAPSHOT_RETENTION_POLICY_MINUTES_DEFAULT = 1440; // one day | ||
| public static final String RETENTION_SNAPSHOT_TIMEZONE = "gobblin.trash.snapshot.retention.timezone"; | ||
|
|
||
| private final int retentionMinutes; | ||
| private final DateTime MOCK_CURRENT_TIME; | ||
| private final DateTimeZone retentionSnapshotTimezone; | ||
|
|
||
| public MockTimeBasedSnapshotCleanupPolicy(Properties props, DateTime mockCurrentTime) { | ||
| this.retentionMinutes = Integer.parseInt(props.getProperty(SNAPSHOT_RETENTION_POLICY_MINUTES_KEY, | ||
| Integer.toString(SNAPSHOT_RETENTION_POLICY_MINUTES_DEFAULT))); | ||
| this.MOCK_CURRENT_TIME = mockCurrentTime; | ||
| this.retentionSnapshotTimezone = props.containsKey(RETENTION_SNAPSHOT_TIMEZONE) ? DateTimeZone.forID(props.getProperty(RETENTION_SNAPSHOT_TIMEZONE)) : DateTimeZone.getDefault(); | ||
| } | ||
linweihs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| @Override | ||
| public boolean shouldDeleteSnapshot(FileStatus snapshot, Trash trash) { | ||
| DateTime snapshotTime = Trash.TRASH_SNAPSHOT_NAME_FORMATTER.parseDateTime(snapshot.getPath().getName()); | ||
| return snapshotTime.plusMinutes(this.retentionMinutes).isBefore(this.MOCK_CURRENT_TIME.withZoneRetainFields(this.retentionSnapshotTimezone)); | ||
| } | ||
linweihs marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
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.