forked from apache/bookkeeper
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Try to use jdk api to create hardlink when rename file when compactio…
…n. (apache#3876) ### Motivation Current HardLink will create a process to execute mv like command to rename file in compaction logic. maybe we can just use jdk api to do this with lower overhead. see javadoc: https://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html#createLink(java.nio.file.Path,%20java.nio.file.Path) ### Changes test if `Files.createLink` is available in hardlink static code block. if test fails means `Files.createLink` is not available. else will use `Files.createLink` when call createHardlink (cherry picked from commit af419cc)
- Loading branch information
1 parent
e7187f6
commit 7875f15
Showing
3 changed files
with
114 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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
82 changes: 82 additions & 0 deletions
82
bookkeeper-server/src/test/java/org/apache/bookkeeper/util/TestHardLink.java
This file contains 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,82 @@ | ||
/* | ||
* 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.bookkeeper.util; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.util.UUID; | ||
import org.apache.commons.io.FileUtils; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
|
||
public class TestHardLink { | ||
|
||
private File tempDir; | ||
|
||
@Before | ||
public void setup() throws IOException { | ||
// Create at least one file so that target disk will never be empty | ||
tempDir = IOUtils.createTempDir("TestHardLink", "test-hardlink"); | ||
} | ||
|
||
@After | ||
public void tearDown() throws IOException { | ||
FileUtils.deleteDirectory(tempDir); | ||
} | ||
|
||
private void verifyHardLink(File origin, File linkedOrigin) throws IOException { | ||
Assert.assertTrue(origin.exists()); | ||
Assert.assertFalse(linkedOrigin.exists()); | ||
|
||
HardLink.createHardLink(origin, linkedOrigin); | ||
|
||
Assert.assertTrue(origin.exists()); | ||
Assert.assertTrue(linkedOrigin.exists()); | ||
|
||
// when delete origin file it should be success and not exist. | ||
origin.delete(); | ||
Assert.assertFalse(origin.exists()); | ||
Assert.assertTrue(linkedOrigin.exists()); | ||
} | ||
|
||
@Test | ||
public void testHardLink() throws IOException { | ||
String uuidSuffix = UUID.randomUUID().toString(); | ||
|
||
// prepare file | ||
File origin = new File(tempDir, "originFile." + uuidSuffix); | ||
File linkedOrigin = new File(tempDir, "linkedOrigin." + uuidSuffix); | ||
origin.createNewFile(); | ||
|
||
// disable jdk api link first | ||
HardLink.enableJdkLinkApi(false); | ||
verifyHardLink(origin, linkedOrigin); | ||
|
||
// prepare file | ||
File jdkorigin = new File(tempDir, "jdkoriginFile." + uuidSuffix); | ||
File jdklinkedOrigin = new File(tempDir, "jdklinkedOrigin." + uuidSuffix); | ||
jdkorigin.createNewFile(); | ||
|
||
// enable jdk api link | ||
HardLink.enableJdkLinkApi(true); | ||
verifyHardLink(jdkorigin, jdklinkedOrigin); | ||
} | ||
} |