use of org.apache.hyracks.api.io.FileReference in project asterixdb by apache.
the class PersistentLocalResourceRepository method get.
@Override
public LocalResource get(String relativePath) throws HyracksDataException {
LocalResource resource = resourceCache.getIfPresent(relativePath);
if (resource == null) {
FileReference resourceFile = getLocalResourceFileByName(ioManager, relativePath);
if (resourceFile.getFile().exists()) {
resource = readLocalResource(resourceFile.getFile());
resourceCache.put(relativePath, resource);
}
}
return resource;
}
use of org.apache.hyracks.api.io.FileReference in project asterixdb by apache.
the class PersistentLocalResourceRepository method deleteStorageData.
/**
* Deletes physical files of all data verses.
*
* @param deleteStorageMetadata
* @throws IOException
*/
public void deleteStorageData(boolean deleteStorageMetadata) throws IOException {
for (int i = 0; i < mountPoints.length; i++) {
File storageDir = getStorageRootDirectoryIfExists(ioManager, nodeId, i);
if (storageDir != null && storageDir.isDirectory()) {
FileUtils.deleteDirectory(storageDir);
}
if (deleteStorageMetadata) {
//delete the metadata root directory
FileReference storageMetadataFile = getStorageMetadataFile(ioManager, nodeId, i);
File storageMetadataDir = storageMetadataFile.getFile().getParentFile().getParentFile();
if (storageMetadataDir.exists() && storageMetadataDir.isDirectory()) {
FileUtils.deleteDirectory(storageMetadataDir);
}
}
}
}
use of org.apache.hyracks.api.io.FileReference in project asterixdb by apache.
the class PersistentLocalResourceRepository method initializeNewUniverse.
public void initializeNewUniverse(String storageRoot) throws HyracksDataException {
if (LOGGER.isLoggable(Level.INFO)) {
LOGGER.info("Initializing local resource repository ... ");
}
/*
* create storage metadata file
* (This file is used to locate the root storage directory after instance restarts).
* TODO with the existing cluster configuration file being static and distributed on all NCs
* we can find out the storage root directory without looking at this file.
* This file could potentially store more information, otherwise no need to keep it.
*/
String storageRootDirName = storageRoot;
while (storageRootDirName.startsWith(File.separator)) {
storageRootDirName = storageRootDirName.substring(File.separator.length());
}
for (int i = 0; i < mountPoints.length; i++) {
FileReference storageMetadataFile = getStorageMetadataFile(ioManager, nodeId, i);
File storageMetadataDir = storageMetadataFile.getFile().getParentFile();
if (storageMetadataDir.exists()) {
throw HyracksDataException.create(ErrorCode.ROOT_LOCAL_RESOURCE_EXISTS, getClass().getSimpleName(), storageMetadataDir.getAbsolutePath());
}
//make dirs for the storage metadata file
boolean success = storageMetadataDir.mkdirs();
if (!success) {
throw HyracksDataException.create(ErrorCode.ROOT_LOCAL_RESOURCE_COULD_NOT_BE_CREATED, getClass().getSimpleName(), storageMetadataDir.getAbsolutePath());
}
LOGGER.log(Level.INFO, "created the root-metadata-file's directory: " + storageMetadataDir.getAbsolutePath());
try (FileOutputStream fos = new FileOutputStream(storageMetadataFile.getFile())) {
fos.write(storageRootDirName.getBytes(StandardCharsets.UTF_8));
} catch (IOException e) {
throw HyracksDataException.create(e);
}
LOGGER.log(Level.INFO, "created the root-metadata-file: " + storageMetadataFile.getAbsolutePath());
}
LOGGER.log(Level.INFO, "Completed the initialization of the local resource repository");
}
use of org.apache.hyracks.api.io.FileReference in project asterixdb by apache.
the class PersistentLocalResourceRepository method getStorageRootDirectoryIfExists.
/**
* @param mountPoint
* @param nodeId
* @param ioDeviceId
* @return A file reference to the storage root directory if exists, otherwise null.
* @throws HyracksDataException
*/
public static File getStorageRootDirectoryIfExists(IIOManager ioManager, String nodeId, int ioDeviceId) throws HyracksDataException {
try {
FileReference storageMetadataFile = getStorageMetadataFile(ioManager, nodeId, ioDeviceId);
LOGGER.log(Level.INFO, "Storage metadata file is " + storageMetadataFile.getAbsolutePath());
if (storageMetadataFile.getFile().exists()) {
String storageRootDirPath = new String(Files.readAllBytes(storageMetadataFile.getFile().toPath()), StandardCharsets.UTF_8);
LOGGER.log(Level.INFO, "Storage metadata file found and root dir is " + storageRootDirPath);
FileReference storageRootFileRef = new FileReference(ioManager.getIODevices().get(ioDeviceId), storageRootDirPath);
if (storageRootFileRef.getFile().exists()) {
return storageRootFileRef.getFile();
} else {
LOGGER.log(Level.INFO, "Storage root doesn't exist");
}
} else {
LOGGER.log(Level.INFO, "Storage metadata file doesn't exist");
}
return null;
} catch (IOException ioe) {
throw HyracksDataException.create(ioe);
}
}
use of org.apache.hyracks.api.io.FileReference in project asterixdb by apache.
the class BufferCache method openFile.
@Override
public void openFile(int fileId) throws HyracksDataException {
if (LOGGER.isLoggable(fileOpsLevel)) {
LOGGER.log(fileOpsLevel, "Opening file: " + fileId + " in cache: " + this);
}
synchronized (fileInfoMap) {
BufferedFileHandle fInfo;
fInfo = fileInfoMap.get(fileId);
if (fInfo == null) {
// map is full, make room by cleaning up unreferenced files
boolean unreferencedFileFound = true;
while (fileInfoMap.size() >= maxOpenFiles && unreferencedFileFound) {
unreferencedFileFound = false;
for (Map.Entry<Integer, BufferedFileHandle> entry : fileInfoMap.entrySet()) {
if (entry.getValue().getReferenceCount() <= 0) {
int entryFileId = entry.getKey();
boolean fileHasBeenDeleted = entry.getValue().fileHasBeenDeleted();
sweepAndFlush(entryFileId, !fileHasBeenDeleted);
if (!fileHasBeenDeleted) {
ioManager.close(entry.getValue().getFileHandle());
}
fileInfoMap.remove(entryFileId);
unreferencedFileFound = true;
// fileInfoMap
break;
}
}
}
if (fileInfoMap.size() >= maxOpenFiles) {
throw new HyracksDataException("Could not open fileId " + fileId + ". Max number of files " + maxOpenFiles + " already opened and referenced.");
}
// create, open, and map new file reference
FileReference fileRef = fileMapManager.lookupFileName(fileId);
IFileHandle fh = ioManager.open(fileRef, IIOManager.FileReadWriteMode.READ_WRITE, IIOManager.FileSyncMode.METADATA_ASYNC_DATA_ASYNC);
fInfo = new BufferedFileHandle(fileId, fh);
fileInfoMap.put(fileId, fInfo);
}
fInfo.incReferenceCount();
}
}
Aggregations