public void run() { // TODO get rid of current and use the marker file instead? directoryProviderLock.lock(); try { long start = System.nanoTime(); // keep time after lock is acquired for correct measure int oldIndex = current; int index = oldIndex == 1 ? 2 : 1; File destinationFile = new File(destination, Integer.valueOf(index).toString()); try { log.tracef("Copying %s into %s", source, destinationFile); FileHelper.synchronize(source, destinationFile, true, copyChunkSize); current = index; } catch (IOException e) { // don't change current log.unableToSynchronizeSource(indexName, e); return; } if (!new File(destination, CURRENT_DIR_NAME[oldIndex]).delete()) { log.unableToRemovePreviousMarket(indexName); } try { new File(destination, CURRENT_DIR_NAME[index]).createNewFile(); } catch (IOException e) { log.unableToCreateCurrentMarker(indexName, e); } log.tracef( "Copy for %s took %d ms", indexName, TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - start)); } finally { directoryProviderLock.unlock(); inProgress.set(false); } }
public void start() { int currentLocal = 0; this.directoryProviderLock = this.context.getDirectoryProviderLock(this); this.context = null; try { // copy to source if (new File(sourceDir, CURRENT1).exists()) { currentLocal = 2; } else if (new File(sourceDir, CURRENT2).exists()) { currentLocal = 1; } else { log.debugf("Source directory for '%s' will be initialized", indexName); currentLocal = 1; } String currentString = Integer.valueOf(currentLocal).toString(); File subDir = new File(sourceDir, currentString); FileHelper.synchronize(indexDir, subDir, true, copyChunkSize); new File(sourceDir, CURRENT1).delete(); new File(sourceDir, CURRENT2).delete(); // TODO small hole, no file can be found here new File(sourceDir, CURRENT_DIR_NAME[currentLocal]).createNewFile(); log.debugf("Current directory: %d", currentLocal); } catch (IOException e) { throw new SearchException("Unable to initialize index: " + directoryProviderName, e); } task = new FSMasterDirectoryProvider.TriggerTask(indexDir, sourceDir); long period = DirectoryProviderHelper.getRefreshPeriod(properties, directoryProviderName); timer.scheduleAtFixedRate(task, period, period); this.current = currentLocal; // write to volatile to publish all state }
@Test public void testMkdirsDetermineIndex() throws Exception { String root = "./testDir/dir1/dir2"; String relative = "dir3"; Properties properties = new Properties(); properties.put("indexBase", root); properties.put("indexName", relative); DirectoryHelper.getVerifiedIndexDir("name", properties, true); assertTrue(new File(root).exists()); FileHelper.delete(new File("./testDir")); }