Search in sources :

Example 6 with FileReference

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;
}
Also used : FileReference(org.apache.hyracks.api.io.FileReference) DatasetLocalResource(org.apache.asterix.common.dataflow.DatasetLocalResource) LocalResource(org.apache.hyracks.storage.common.LocalResource)

Example 7 with FileReference

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);
            }
        }
    }
}
Also used : FileReference(org.apache.hyracks.api.io.FileReference) File(java.io.File)

Example 8 with FileReference

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");
}
Also used : FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) FileReference(org.apache.hyracks.api.io.FileReference) File(java.io.File)

Example 9 with FileReference

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);
    }
}
Also used : IOException(java.io.IOException) FileReference(org.apache.hyracks.api.io.FileReference)

Example 10 with FileReference

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();
    }
}
Also used : BufferedFileHandle(org.apache.hyracks.storage.common.file.BufferedFileHandle) IFileHandle(org.apache.hyracks.api.io.IFileHandle) FileReference(org.apache.hyracks.api.io.FileReference) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Aggregations

FileReference (org.apache.hyracks.api.io.FileReference)52 IIOManager (org.apache.hyracks.api.io.IIOManager)16 File (java.io.File)10 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)9 IBufferCache (org.apache.hyracks.storage.common.buffercache.IBufferCache)7 IOException (java.io.IOException)6 LSMComponentFileReferences (org.apache.hyracks.storage.am.lsm.common.impls.LSMComponentFileReferences)6 IFileMapProvider (org.apache.hyracks.storage.common.file.IFileMapProvider)6 IInvertedListBuilder (org.apache.hyracks.storage.am.lsm.invertedindex.api.IInvertedListBuilder)5 Test (org.junit.Test)5 FilenameFilter (java.io.FilenameFilter)4 IVirtualBufferCache (org.apache.hyracks.storage.am.lsm.common.api.IVirtualBufferCache)4 ArrayList (java.util.ArrayList)3 IODeviceHandle (org.apache.hyracks.api.io.IODeviceHandle)3 BTree (org.apache.hyracks.storage.am.btree.impls.BTree)3 LocalResource (org.apache.hyracks.storage.common.LocalResource)3 FileOutputStream (java.io.FileOutputStream)2 HashMap (java.util.HashMap)2 IBinaryComparatorFactory (org.apache.hyracks.api.dataflow.value.IBinaryComparatorFactory)2 ITypeTraits (org.apache.hyracks.api.dataflow.value.ITypeTraits)2