@Test public void treeWrittenOnFinish() { when(future.isDone()).thenReturn(true); when(storageWriter.writeNonDefaultDataObject(eq(indexingTree), anyString())).thenReturn(true); indexingTreeHandler.finish(); verify(storageWriter, times(1)).writeNonDefaultDataObject(eq(indexingTree), anyString()); }
public void finish() { cancelIndexingTreeSavingFuture(); IStorageTreeComponent<DefaultData> currentIndexingTree = null; while (true) { currentIndexingTree = storageIndexingTreeReference.get(); if (storageIndexingTreeReference.compareAndSet(currentIndexingTree, null)) { break; } } if (null != currentIndexingTree) { int sleepCount = 0; while (!writeTasksInProcess.isEmpty()) { log.info("Indexing tree handler still waiting for " + writeTasksInProcess.size() + " task(s) to be finished. Going for sleep " + (sleepCount + 1) + " out of " + FINISH_WAITING_ITERATIONS + "."); if (sleepCount > FINISH_WAITING_ITERATIONS) { log.warn("Indexing tree handler waited " + (sleepCount * WAITING_FOR_TREE_TO_BE_READY) + " milliseconds for all tasks to be finished. There are " + writeTasksInProcess.size() + " tasks still in-progress. Saving of the indexing tree will continue without waiting for these tasks."); break; } try { Thread.sleep(WAITING_FOR_TREE_TO_BE_READY); sleepCount++; } catch (InterruptedException e) { Thread.interrupted(); } } currentIndexingTree.preWriteFinalization(); boolean written = storageWriter.writeNonDefaultDataObject(currentIndexingTree, getRandomFileName() + StorageFileType.INDEX_FILE.getExtension()); if (!written) { log.error("Indexing tree saving failed. Indexing tree might be lost."); } } }
StorageIndexingTreeHandler { public void finish() { cancelIndexingTreeSavingFuture(); IStorageTreeComponent<DefaultData> currentIndexingTree = null; while (true) { currentIndexingTree = storageIndexingTreeReference.get(); if (storageIndexingTreeReference.compareAndSet(currentIndexingTree, null)) { break; } } if (null != currentIndexingTree) { int sleepCount = 0; while (!writeTasksInProcess.isEmpty()) { log.info("Indexing tree handler still waiting for " + writeTasksInProcess.size() + " task(s) to be finished. Going for sleep " + (sleepCount + 1) + " out of " + FINISH_WAITING_ITERATIONS + "."); if (sleepCount > FINISH_WAITING_ITERATIONS) { log.warn("Indexing tree handler waited " + (sleepCount * WAITING_FOR_TREE_TO_BE_READY) + " milliseconds for all tasks to be finished. There are " + writeTasksInProcess.size() + " tasks still in-progress. Saving of the indexing tree will continue without waiting for these tasks."); break; } try { Thread.sleep(WAITING_FOR_TREE_TO_BE_READY); sleepCount++; } catch (InterruptedException e) { Thread.interrupted(); } } currentIndexingTree.preWriteFinalization(); boolean written = storageWriter.writeNonDefaultDataObject(currentIndexingTree, getRandomFileName() + StorageFileType.INDEX_FILE.getExtension()); if (!written) { log.error("Indexing tree saving failed. Indexing tree might be lost."); } } } }
StorageIndexingTreeHandler { public void finish() { cancelIndexingTreeSavingFuture(); IStorageTreeComponent<DefaultData> currentIndexingTree = null; while (true) { currentIndexingTree = storageIndexingTreeReference.get(); if (storageIndexingTreeReference.compareAndSet(currentIndexingTree, null)) { break; } } if (null != currentIndexingTree) { int sleepCount = 0; while (!writeTasksInProcess.isEmpty()) { log.info("Indexing tree handler still waiting for " + writeTasksInProcess.size() + " task(s) to be finished. Going for sleep " + (sleepCount + 1) + " out of " + FINISH_WAITING_ITERATIONS + "."); if (sleepCount > FINISH_WAITING_ITERATIONS) { log.warn("Indexing tree handler waited " + (sleepCount * WAITING_FOR_TREE_TO_BE_READY) + " milliseconds for all tasks to be finished. There are " + writeTasksInProcess.size() + " tasks still in-progress. Saving of the indexing tree will continue without waiting for these tasks."); break; } try { Thread.sleep(WAITING_FOR_TREE_TO_BE_READY); sleepCount++; } catch (InterruptedException e) { Thread.interrupted(); } } currentIndexingTree.preWriteFinalization(); boolean written = storageWriter.writeNonDefaultDataObject(currentIndexingTree, getRandomFileName() + StorageFileType.INDEX_FILE.getExtension()); if (!written) { log.error("Indexing tree saving failed. Indexing tree might be lost."); } } }  }
StorageIndexingTreeHandler { public void finish() { cancelIndexingTreeSavingFuture(); IStorageTreeComponent<DefaultData> currentIndexingTree = null; while (true) { currentIndexingTree = storageIndexingTreeReference.get(); if (storageIndexingTreeReference.compareAndSet(currentIndexingTree, null)) { break; } } if (null != currentIndexingTree) { int sleepCount = 0; while (!writeTasksInProcess.isEmpty()) { log.info("Indexing tree handler still waiting for " + writeTasksInProcess.size() + " task(s) to be finished. Going for sleep " + (sleepCount + 1) + " out of " + FINISH_WAITING_ITERATIONS + "."); if (sleepCount > FINISH_WAITING_ITERATIONS) { log.warn("Indexing tree handler waited " + (sleepCount * WAITING_FOR_TREE_TO_BE_READY) + " milliseconds for all tasks to be finished. There are " + writeTasksInProcess.size() + " tasks still in-progress. Saving of the indexing tree will continue without waiting for these tasks."); break; } try { Thread.sleep(WAITING_FOR_TREE_TO_BE_READY); sleepCount++; } catch (InterruptedException e) { Thread.interrupted(); } } currentIndexingTree.preWriteFinalization(); boolean written = storageWriter.writeNonDefaultDataObject(currentIndexingTree, getRandomFileName() + StorageFileType.INDEX_FILE.getExtension()); if (!written) { log.error("Indexing tree saving failed. Indexing tree might be lost."); } } }  void prepare(); int startWrite(WriteTask writeTask); void writeSuccessful(WriteTask writeTask, long position, long size); void writeFailed(WriteTask writeTask); void cancelIndexingTreeSavingFuture(); void finish(); void registerStorageWriter(StorageWriter storageWriter); }
StorageIndexingTreeHandler { public void finish() { cancelIndexingTreeSavingFuture(); IStorageTreeComponent<DefaultData> currentIndexingTree = null; while (true) { currentIndexingTree = storageIndexingTreeReference.get(); if (storageIndexingTreeReference.compareAndSet(currentIndexingTree, null)) { break; } } if (null != currentIndexingTree) { int sleepCount = 0; while (!writeTasksInProcess.isEmpty()) { log.info("Indexing tree handler still waiting for " + writeTasksInProcess.size() + " task(s) to be finished. Going for sleep " + (sleepCount + 1) + " out of " + FINISH_WAITING_ITERATIONS + "."); if (sleepCount > FINISH_WAITING_ITERATIONS) { log.warn("Indexing tree handler waited " + (sleepCount * WAITING_FOR_TREE_TO_BE_READY) + " milliseconds for all tasks to be finished. There are " + writeTasksInProcess.size() + " tasks still in-progress. Saving of the indexing tree will continue without waiting for these tasks."); break; } try { Thread.sleep(WAITING_FOR_TREE_TO_BE_READY); sleepCount++; } catch (InterruptedException e) { Thread.interrupted(); } } currentIndexingTree.preWriteFinalization(); boolean written = storageWriter.writeNonDefaultDataObject(currentIndexingTree, getRandomFileName() + StorageFileType.INDEX_FILE.getExtension()); if (!written) { log.error("Indexing tree saving failed. Indexing tree might be lost."); } } }  void prepare(); int startWrite(WriteTask writeTask); void writeSuccessful(WriteTask writeTask, long position, long size); void writeFailed(WriteTask writeTask); void cancelIndexingTreeSavingFuture(); void finish(); void registerStorageWriter(StorageWriter storageWriter);  }