-
Notifications
You must be signed in to change notification settings - Fork 682
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
SOLR-17548: Switch all public Java APIs from File to Path #2907
Changes from 7 commits
1ee430d
ac5f63e
58d52b7
45b2712
f851b1f
e7f9be1
5035793
562263d
1c1a315
4e43fed
b4937aa
02201b4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ | |
*/ | ||
package org.apache.solr.cloud; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.Reader; | ||
import java.lang.invoke.MethodHandles; | ||
|
@@ -51,10 +50,10 @@ public class SolrZkServer { | |
|
||
private Thread zkThread; // the thread running a zookeeper server, only if zkRun is set | ||
|
||
private File dataHome; // o.a.zookeeper.**.QuorumPeerConfig needs a File not a Path | ||
private Path dataHome; // o.a.zookeeper.**.QuorumPeerConfig needs a File not a Path | ||
private String confHome; | ||
|
||
public SolrZkServer(String zkRun, String zkHost, File dataHome, String confHome, int solrPort) { | ||
public SolrZkServer(String zkRun, String zkHost, Path dataHome, String confHome, int solrPort) { | ||
this.zkRun = zkRun; | ||
this.zkHost = zkHost; | ||
this.dataHome = dataHome; | ||
|
@@ -277,8 +276,8 @@ public static boolean hasServers(Properties props) { | |
return false; | ||
} | ||
|
||
public void setDataDir(File dataDir) { | ||
this.dataDir = dataDir; | ||
public void setDataDir(Path dataDir) { | ||
this.dataDir = dataDir.toFile(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. is this an example of where you felt changing things got out of hand? Otherwise, seems like if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah this one is setting |
||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -339,6 +339,8 @@ public String getDataHome(CoreDescriptor cd) throws IOException { | |
|
||
public void cleanupOldIndexDirectories( | ||
final String dataDirPath, final String currentIndexDirPath, boolean afterCoreReload) { | ||
|
||
// TODO SOLR-8282 move to PATH | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your stepwise logic is probably much better than a "lets change it all in one fell swoop!" |
||
File dataDir = new File(dataDirPath); | ||
if (!dataDir.isDirectory()) { | ||
log.debug( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,7 +16,6 @@ | |
*/ | ||
package org.apache.solr.core; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.lang.invoke.MethodHandles; | ||
import java.nio.file.AtomicMoveNotSupportedException; | ||
|
@@ -78,7 +77,7 @@ protected LockFactory createLockFactory(String rawLockType) throws IOException { | |
|
||
@Override | ||
public String normalize(String path) throws IOException { | ||
return super.normalize(new File(path).getCanonicalPath()); | ||
return super.normalize(Path.of(path).toAbsolutePath().toString()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. File.getCanonicalPath is not identical to Path.toAbsolutePath. It requires adding normalize(). |
||
} | ||
|
||
@Override | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -422,7 +422,7 @@ IndexFetchResult fetchLatestIndex(boolean forceReplication, boolean forceCoreRel | |
Directory indexDir = null; | ||
String indexDirPath; | ||
boolean deleteTmpIdxDir = true; | ||
File tmpTlogDir = null; | ||
Path tmpTlogDir = null; | ||
|
||
if (!solrCore.getSolrCoreState().getLastReplicateIndexSuccess()) { | ||
// if the last replication was not a success, we force a full replication | ||
|
@@ -797,7 +797,7 @@ private void cleanup( | |
Directory tmpIndexDir, | ||
Directory indexDir, | ||
boolean deleteTmpIdxDir, | ||
File tmpTlogDir, | ||
Path tmpTlogDir, | ||
boolean successfulInstall) | ||
throws IOException { | ||
try { | ||
|
@@ -1065,6 +1065,7 @@ private void downloadConfFiles( | |
confFilesDownloaded = Collections.synchronizedList(new ArrayList<>()); | ||
Path tmpConfPath = | ||
solrCore.getResourceLoader().getConfigPath().resolve("conf." + getDateAsStr(new Date())); | ||
// TODO SOLR-8282 move to PATH | ||
File tmpconfDir = tmpConfPath.toFile(); | ||
try { | ||
boolean status = tmpconfDir.mkdirs(); | ||
|
@@ -1076,7 +1077,8 @@ private void downloadConfFiles( | |
for (Map<String, Object> file : confFilesToDownload) { | ||
String saveAs = (String) (file.get(ALIAS) == null ? file.get(NAME) : file.get(ALIAS)); | ||
localFileFetcher = | ||
new LocalFsFileFetcher(tmpconfDir, file, saveAs, CONF_FILE_SHORT, latestGeneration); | ||
new LocalFsFileFetcher( | ||
tmpconfDir.toPath(), file, saveAs, CONF_FILE_SHORT, latestGeneration); | ||
currentFile = file; | ||
localFileFetcher.fetchFile(); | ||
confFilesDownloaded.add(new HashMap<>(file)); | ||
|
@@ -1086,7 +1088,7 @@ private void downloadConfFiles( | |
terminateAndWaitFsyncService(); | ||
copyTmpConfFiles2Conf(tmpConfPath); | ||
} finally { | ||
delTree(tmpconfDir); | ||
delTree(tmpconfDir.toPath()); | ||
} | ||
} | ||
|
||
|
@@ -1153,6 +1155,7 @@ private long downloadIndexFiles( | |
alwaysDownload); | ||
} | ||
if (!compareResult.equal || downloadCompleteIndex || alwaysDownload) { | ||
// TODO SOLR-8282 move to PATH | ||
File localFile = new File(indexDirPath, filename); | ||
if (downloadCompleteIndex | ||
&& doDifferentialCopy | ||
|
@@ -1200,15 +1203,15 @@ private long downloadIndexFiles( | |
|
||
private static Long getUsableSpace(String dir) { | ||
try { | ||
File file = new File(dir); | ||
if (!file.exists()) { | ||
file = file.getParentFile(); | ||
Path file = Path.of(dir); | ||
if (Files.notExists(file)) { | ||
file = file.getParent(); | ||
// this is not a disk directory. so just pretend that there is enough space | ||
if (!file.exists()) { | ||
if (Files.notExists(file)) { | ||
return Long.MAX_VALUE; | ||
} | ||
} | ||
FileStore fileStore = Files.getFileStore(file.toPath()); | ||
FileStore fileStore = Files.getFileStore(file); | ||
return fileStore.getUsableSpace(); | ||
} catch (IOException e) { | ||
throw new SolrException(ErrorCode.SERVER_ERROR, "Could not free disk space", e); | ||
|
@@ -1487,7 +1490,7 @@ private void copyTmpConfFiles2Conf(Path tmpconfDir) { | |
ErrorCode.SERVER_ERROR, "Unable to mkdirs: " + oldPath.getParent(), e); | ||
} | ||
if (Files.exists(oldPath)) { | ||
File oldFile = oldPath.toFile(); // TODO drop this | ||
File oldFile = oldPath.toFile(); // TODO SOLR-8282 move to PATH | ||
File backupFile = | ||
new File(oldFile.getPath() + "." + getDateAsStr(new Date(oldFile.lastModified()))); | ||
if (!backupFile.getParentFile().exists()) { | ||
|
@@ -1518,12 +1521,14 @@ private void copyTmpConfFiles2Conf(Path tmpconfDir) { | |
* backup of the old directory is maintained. If the directory move fails, it will try to revert | ||
* back the original tlog directory. | ||
*/ | ||
private boolean copyTmpTlogFiles2Tlog(File tmpTlogDir) { | ||
private boolean copyTmpTlogFiles2Tlog(Path tmpTlogDir) { | ||
Path tlogDir = | ||
FileSystems.getDefault().getPath(solrCore.getUpdateHandler().getUpdateLog().getTlogDir()); | ||
Path backupTlogDir = | ||
FileSystems.getDefault() | ||
.getPath(tlogDir.getParent().toAbsolutePath().toString(), tmpTlogDir.getName()); | ||
.getPath( | ||
tlogDir.getParent().toAbsolutePath().toString(), | ||
tmpTlogDir.getFileName().toString()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks more complicated than it needs to be. It's highly unusual for anyone to need to refer to FileSystems. Look at the implementation of But hey, copyTmpTlogFiles2Tlog isn't used at all so let's just delete it! |
||
|
||
try { | ||
Files.move(tlogDir, backupTlogDir, StandardCopyOption.ATOMIC_MOVE); | ||
|
@@ -1534,7 +1539,8 @@ private boolean copyTmpTlogFiles2Tlog(File tmpTlogDir) { | |
|
||
Path src = | ||
FileSystems.getDefault() | ||
.getPath(backupTlogDir.toAbsolutePath().toString(), tmpTlogDir.getName()); | ||
.getPath( | ||
backupTlogDir.toAbsolutePath().toString(), tmpTlogDir.getFileName().toString()); | ||
try { | ||
Files.move(src, tlogDir, StandardCopyOption.ATOMIC_MOVE); | ||
} catch (IOException e) { | ||
|
@@ -1604,9 +1610,9 @@ private Collection<Map<String, Object>> getModifiedConfFiles( | |
* SecurityException, otherwise returns Throwable preventing deletion (instead of false), for | ||
* additional information. | ||
*/ | ||
static Throwable delete(File file) { | ||
static Throwable delete(Path file) { | ||
try { | ||
Files.delete(file.toPath()); | ||
Files.delete(file); | ||
return null; | ||
} catch (SecurityException e) { | ||
throw e; | ||
|
@@ -1615,9 +1621,9 @@ static Throwable delete(File file) { | |
} | ||
} | ||
|
||
static boolean delTree(File dir) { | ||
static boolean delTree(Path dir) { | ||
try { | ||
org.apache.lucene.util.IOUtils.rm(dir.toPath()); | ||
org.apache.lucene.util.IOUtils.rm(dir); | ||
return true; | ||
} catch (IOException e) { | ||
log.warn("Unable to delete directory : {}", dir, e); | ||
|
@@ -2046,27 +2052,29 @@ protected class DirectoryFileFetcher extends FileFetcher { | |
} | ||
|
||
private static class LocalFsFile implements FileInterface { | ||
private File copy2Dir; | ||
private Path copy2Dir; | ||
|
||
FileChannel fileChannel; | ||
private FileOutputStream fileOutputStream; | ||
File file; | ||
Path file; | ||
|
||
LocalFsFile(File dir, String saveAs) throws IOException { | ||
LocalFsFile(Path dir, String saveAs) throws IOException { | ||
this.copy2Dir = dir; | ||
|
||
this.file = new File(copy2Dir, saveAs); | ||
this.file = Path.of(copy2Dir.toString(), saveAs); | ||
|
||
File parentDir = this.file.getParentFile(); | ||
if (!parentDir.exists()) { | ||
if (!parentDir.mkdirs()) { | ||
Path parentDir = this.file.getParent(); | ||
if (Files.notExists(parentDir)) { | ||
try { | ||
Files.createDirectories(parentDir); | ||
} catch (Exception e) { | ||
throw new SolrException( | ||
SolrException.ErrorCode.SERVER_ERROR, | ||
"Failed to create (sub)directory for file: " + saveAs); | ||
} | ||
} | ||
|
||
this.fileOutputStream = new FileOutputStream(file); | ||
this.fileOutputStream = new FileOutputStream(file.toFile()); | ||
this.fileChannel = this.fileOutputStream.getChannel(); | ||
} | ||
|
||
|
@@ -2088,13 +2096,13 @@ public void close() throws Exception { | |
|
||
@Override | ||
public void delete() throws Exception { | ||
Files.delete(file.toPath()); | ||
Files.delete(file); | ||
} | ||
} | ||
|
||
protected class LocalFsFileFetcher extends FileFetcher { | ||
LocalFsFileFetcher( | ||
File dir, | ||
Path dir, | ||
Map<String, Object> fileDetails, | ||
String saveAs, | ||
String solrParamOutput, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
love seeing the changed version is shorter and simpler than the original one!