Search in sources :

Example 1 with TestLsmIndexFileManager

use of org.apache.hyracks.storage.am.lsm.common.component.TestLsmIndexFileManager in project asterixdb by apache.

the class LSMIndexFileManagerTest method cleanInvalidFilesTest.

public void cleanInvalidFilesTest(IOManager ioManager) throws InterruptedException, IOException {
    String dirPath = ioManager.getIODevices().get(DEFAULT_IO_DEVICE_ID).getMount() + sep + "lsm_tree" + simpleDateFormat.format(new Date()) + sep;
    File f = new File(dirPath);
    f.mkdirs();
    FileReference file = ioManager.resolveAbsolutePath(f.getAbsolutePath());
    ILSMIndexFileManager fileManager = new TestLsmIndexFileManager(ioManager, fileMapProvider, file);
    fileManager.createDirs();
    List<FileReference> flushFiles = new ArrayList<>();
    List<FileReference> allFiles = new ArrayList<>();
    int numFileNames = 100;
    long sleepTime = 5;
    // Generate a bunch of flush files.
    for (int i = 0; i < numFileNames; i++) {
        LSMComponentFileReferences relFlushFileRefs = fileManager.getRelFlushFileReference();
        flushFiles.add(relFlushFileRefs.getInsertIndexFileReference());
        Thread.sleep(sleepTime);
    }
    allFiles.addAll(flushFiles);
    // Simulate merging some of the flush files.
    // Merge range 0 to 4.
    FileReference mergeFile1 = simulateMerge(fileManager, flushFiles.get(0), flushFiles.get(4));
    allFiles.add(mergeFile1);
    // Merge range 5 to 9.
    FileReference mergeFile2 = simulateMerge(fileManager, flushFiles.get(5), flushFiles.get(9));
    allFiles.add(mergeFile2);
    // Merge range 10 to 19.
    FileReference mergeFile3 = simulateMerge(fileManager, flushFiles.get(10), flushFiles.get(19));
    allFiles.add(mergeFile3);
    // Merge range 20 to 29.
    FileReference mergeFile4 = simulateMerge(fileManager, flushFiles.get(20), flushFiles.get(29));
    allFiles.add(mergeFile4);
    // Merge range 50 to 79.
    FileReference mergeFile5 = simulateMerge(fileManager, flushFiles.get(50), flushFiles.get(79));
    allFiles.add(mergeFile5);
    // Simulate merging of merge files.
    FileReference mergeFile6 = simulateMerge(fileManager, mergeFile1, mergeFile2);
    allFiles.add(mergeFile6);
    FileReference mergeFile7 = simulateMerge(fileManager, mergeFile3, mergeFile4);
    allFiles.add(mergeFile7);
    // Create all files and set delete on exit for all files.
    for (FileReference fileRef : allFiles) {
        fileRef.getFile().createNewFile();
        fileRef.getFile().deleteOnExit();
    }
    // Populate expected valid flush files.
    List<String> expectedValidFiles = new ArrayList<>();
    for (int i = 30; i < 50; i++) {
        expectedValidFiles.add(flushFiles.get(i).getFile().getName());
    }
    for (int i = 80; i < 100; i++) {
        expectedValidFiles.add(flushFiles.get(i).getFile().getName());
    }
    // Populate expected valid merge files.
    expectedValidFiles.add(mergeFile5.getFile().getName());
    expectedValidFiles.add(mergeFile6.getFile().getName());
    expectedValidFiles.add(mergeFile7.getFile().getName());
    // Sort expected files.
    Collections.sort(expectedValidFiles, fileManager.getFileNameComparator());
    // Pass null and a dummy component finalizer. We don't test for physical consistency in this test.
    List<LSMComponentFileReferences> lsmComonentFileReference = fileManager.cleanupAndGetValidFiles();
    // Check actual files against expected files.
    assertEquals(expectedValidFiles.size(), lsmComonentFileReference.size());
    for (int i = 0; i < expectedValidFiles.size(); i++) {
        assertEquals(expectedValidFiles.get(i), lsmComonentFileReference.get(i).getInsertIndexFileReference().getFile().getName());
    }
    // Make sure invalid files were removed from the IODevices.
    ArrayList<String> remainingFiles = new ArrayList<>();
    File dir = new File(dirPath);
    FilenameFilter filter = new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            return !name.startsWith(".");
        }
    };
    String[] files = dir.list(filter);
    for (String aFilePath : files) {
        File aFile = new File(aFilePath);
        remainingFiles.add(aFile.getName());
    }
    Collections.sort(remainingFiles, fileManager.getFileNameComparator());
    // Check actual files in directory against expected files.
    assertEquals(expectedValidFiles.size(), remainingFiles.size());
    for (int i = 0; i < expectedValidFiles.size(); i++) {
        assertEquals(expectedValidFiles.get(i), remainingFiles.get(i));
    }
}
Also used : ILSMIndexFileManager(org.apache.hyracks.storage.am.lsm.common.api.ILSMIndexFileManager) ArrayList(java.util.ArrayList) TestLsmIndexFileManager(org.apache.hyracks.storage.am.lsm.common.component.TestLsmIndexFileManager) Date(java.util.Date) FilenameFilter(java.io.FilenameFilter) FileReference(org.apache.hyracks.api.io.FileReference) File(java.io.File) LSMComponentFileReferences(org.apache.hyracks.storage.am.lsm.common.impls.LSMComponentFileReferences)

Example 2 with TestLsmIndexFileManager

use of org.apache.hyracks.storage.am.lsm.common.component.TestLsmIndexFileManager in project asterixdb by apache.

the class LSMIndexFileManagerTest method sortOrderTest.

public void sortOrderTest(boolean testFlushFileName) throws InterruptedException, HyracksDataException {
    ILSMIndexFileManager fileManager = new TestLsmIndexFileManager(ioManager, fileMapProvider, file);
    LinkedList<String> fileNames = new LinkedList<>();
    int numFileNames = 100;
    long sleepTime = 5;
    for (int i = 0; i < numFileNames; i++) {
        String flushFileName = fileManager.getRelFlushFileReference().getInsertIndexFileReference().getFile().getName();
        if (testFlushFileName) {
            fileNames.addFirst(flushFileName);
        }
        Thread.sleep(sleepTime);
        if (!testFlushFileName) {
            String secondFlushFileName = fileManager.getRelFlushFileReference().getInsertIndexFileReference().getFile().getName();
            String mergeFileName = getMergeFileName(fileManager, flushFileName, secondFlushFileName);
            fileNames.addFirst(mergeFileName);
            Thread.sleep(sleepTime);
        }
    }
    List<String> sortedFileNames = new ArrayList<>();
    sortedFileNames.addAll(fileNames);
    // Make sure the comparator sorts in the correct order (i.e., the
    // reverse insertion order in this case).
    Comparator<String> cmp = fileManager.getFileNameComparator();
    Collections.sort(sortedFileNames, cmp);
    for (int i = 0; i < numFileNames; i++) {
        assertEquals(fileNames.get(i), sortedFileNames.get(i));
    }
}
Also used : ILSMIndexFileManager(org.apache.hyracks.storage.am.lsm.common.api.ILSMIndexFileManager) ArrayList(java.util.ArrayList) TestLsmIndexFileManager(org.apache.hyracks.storage.am.lsm.common.component.TestLsmIndexFileManager) LinkedList(java.util.LinkedList)

Aggregations

ArrayList (java.util.ArrayList)2 ILSMIndexFileManager (org.apache.hyracks.storage.am.lsm.common.api.ILSMIndexFileManager)2 TestLsmIndexFileManager (org.apache.hyracks.storage.am.lsm.common.component.TestLsmIndexFileManager)2 File (java.io.File)1 FilenameFilter (java.io.FilenameFilter)1 Date (java.util.Date)1 LinkedList (java.util.LinkedList)1 FileReference (org.apache.hyracks.api.io.FileReference)1 LSMComponentFileReferences (org.apache.hyracks.storage.am.lsm.common.impls.LSMComponentFileReferences)1