use of java.nio.file.Path in project elasticsearch by elastic.
the class FileWatcherTests method testSimpleDirectoryOperations.
public void testSimpleDirectoryOperations() throws IOException {
Path tempDir = createTempDir();
RecordingChangeListener changes = new RecordingChangeListener(tempDir);
Path testDir = tempDir.resolve("test-dir");
Files.createDirectories(testDir);
touch(testDir.resolve("test.txt"));
touch(testDir.resolve("test0.txt"));
FileWatcher fileWatcher = new FileWatcher(testDir);
fileWatcher.addListener(changes);
fileWatcher.init();
assertThat(changes.notifications(), contains(equalTo("onDirectoryInit: test-dir/"), equalTo("onFileInit: test-dir/test.txt"), equalTo("onFileInit: test-dir/test0.txt")));
changes.notifications().clear();
fileWatcher.checkAndNotify();
assertThat(changes.notifications(), hasSize(0));
for (int i = 0; i < 4; i++) {
touch(testDir.resolve("test" + i + ".txt"));
}
// Make sure that first file is modified
append("Test", testDir.resolve("test0.txt"), Charset.defaultCharset());
fileWatcher.checkAndNotify();
assertThat(changes.notifications(), contains(equalTo("onFileChanged: test-dir/test0.txt"), equalTo("onFileCreated: test-dir/test1.txt"), equalTo("onFileCreated: test-dir/test2.txt"), equalTo("onFileCreated: test-dir/test3.txt")));
changes.notifications().clear();
fileWatcher.checkAndNotify();
assertThat(changes.notifications(), hasSize(0));
Files.delete(testDir.resolve("test1.txt"));
Files.delete(testDir.resolve("test2.txt"));
fileWatcher.checkAndNotify();
assertThat(changes.notifications(), contains(equalTo("onFileDeleted: test-dir/test1.txt"), equalTo("onFileDeleted: test-dir/test2.txt")));
changes.notifications().clear();
fileWatcher.checkAndNotify();
assertThat(changes.notifications(), hasSize(0));
Files.delete(testDir.resolve("test0.txt"));
touch(testDir.resolve("test2.txt"));
touch(testDir.resolve("test4.txt"));
fileWatcher.checkAndNotify();
assertThat(changes.notifications(), contains(equalTo("onFileDeleted: test-dir/test0.txt"), equalTo("onFileCreated: test-dir/test2.txt"), equalTo("onFileCreated: test-dir/test4.txt")));
changes.notifications().clear();
Files.delete(testDir.resolve("test3.txt"));
Files.delete(testDir.resolve("test4.txt"));
fileWatcher.checkAndNotify();
assertThat(changes.notifications(), contains(equalTo("onFileDeleted: test-dir/test3.txt"), equalTo("onFileDeleted: test-dir/test4.txt")));
changes.notifications().clear();
if (Files.exists(testDir)) {
IOUtils.rm(testDir);
}
fileWatcher.checkAndNotify();
assertThat(changes.notifications(), contains(equalTo("onFileDeleted: test-dir/test.txt"), equalTo("onFileDeleted: test-dir/test2.txt"), equalTo("onDirectoryDeleted: test-dir")));
}
use of java.nio.file.Path in project elasticsearch by elastic.
the class FileWatcherTests method testNestedDirectoryOperations.
public void testNestedDirectoryOperations() throws IOException {
Path tempDir = createTempDir();
RecordingChangeListener changes = new RecordingChangeListener(tempDir);
Path testDir = tempDir.resolve("test-dir");
Files.createDirectories(testDir);
touch(testDir.resolve("test.txt"));
Files.createDirectories(testDir.resolve("sub-dir"));
touch(testDir.resolve("sub-dir/test0.txt"));
FileWatcher fileWatcher = new FileWatcher(testDir);
fileWatcher.addListener(changes);
fileWatcher.init();
assertThat(changes.notifications(), contains(equalTo("onDirectoryInit: test-dir/"), equalTo("onDirectoryInit: test-dir/sub-dir/"), equalTo("onFileInit: test-dir/sub-dir/test0.txt"), equalTo("onFileInit: test-dir/test.txt")));
changes.notifications().clear();
fileWatcher.checkAndNotify();
assertThat(changes.notifications(), hasSize(0));
// Create new file in subdirectory
touch(testDir.resolve("sub-dir/test1.txt"));
fileWatcher.checkAndNotify();
assertThat(changes.notifications(), contains(equalTo("onFileCreated: test-dir/sub-dir/test1.txt")));
changes.notifications().clear();
fileWatcher.checkAndNotify();
assertThat(changes.notifications(), hasSize(0));
// Create new subdirectory in subdirectory
Files.createDirectories(testDir.resolve("first-level"));
touch(testDir.resolve("first-level/file1.txt"));
Files.createDirectories(testDir.resolve("first-level/second-level"));
touch(testDir.resolve("first-level/second-level/file2.txt"));
fileWatcher.checkAndNotify();
assertThat(changes.notifications(), contains(equalTo("onDirectoryCreated: test-dir/first-level/"), equalTo("onFileCreated: test-dir/first-level/file1.txt"), equalTo("onDirectoryCreated: test-dir/first-level/second-level/"), equalTo("onFileCreated: test-dir/first-level/second-level/file2.txt")));
changes.notifications().clear();
fileWatcher.checkAndNotify();
assertThat(changes.notifications(), hasSize(0));
// Delete a directory, check notifications for
Path path = testDir.resolve("first-level");
if (Files.exists(path)) {
IOUtils.rm(path);
}
fileWatcher.checkAndNotify();
assertThat(changes.notifications(), contains(equalTo("onFileDeleted: test-dir/first-level/file1.txt"), equalTo("onFileDeleted: test-dir/first-level/second-level/file2.txt"), equalTo("onDirectoryDeleted: test-dir/first-level/second-level"), equalTo("onDirectoryDeleted: test-dir/first-level")));
}
use of java.nio.file.Path in project elasticsearch by elastic.
the class FileWatcherTests method testFileReplacingDirectory.
public void testFileReplacingDirectory() throws IOException {
Path tempDir = createTempDir();
RecordingChangeListener changes = new RecordingChangeListener(tempDir);
Path testDir = tempDir.resolve("test-dir");
Files.createDirectories(testDir);
Path subDir = testDir.resolve("sub-dir");
Files.createDirectories(subDir);
touch(subDir.resolve("test0.txt"));
touch(subDir.resolve("test1.txt"));
FileWatcher fileWatcher = new FileWatcher(testDir);
fileWatcher.addListener(changes);
fileWatcher.init();
assertThat(changes.notifications(), contains(equalTo("onDirectoryInit: test-dir/"), equalTo("onDirectoryInit: test-dir/sub-dir/"), equalTo("onFileInit: test-dir/sub-dir/test0.txt"), equalTo("onFileInit: test-dir/sub-dir/test1.txt")));
changes.notifications().clear();
if (Files.exists(subDir)) {
IOUtils.rm(subDir);
}
touch(subDir);
fileWatcher.checkAndNotify();
assertThat(changes.notifications(), contains(equalTo("onFileDeleted: test-dir/sub-dir/test0.txt"), equalTo("onFileDeleted: test-dir/sub-dir/test1.txt"), equalTo("onDirectoryDeleted: test-dir/sub-dir"), equalTo("onFileCreated: test-dir/sub-dir")));
changes.notifications().clear();
Files.delete(subDir);
Files.createDirectories(subDir);
fileWatcher.checkAndNotify();
assertThat(changes.notifications(), contains(equalTo("onFileDeleted: test-dir/sub-dir/"), equalTo("onDirectoryCreated: test-dir/sub-dir/")));
}
use of java.nio.file.Path in project elasticsearch by elastic.
the class FileWatcherTests method testEmptyDirectory.
public void testEmptyDirectory() throws IOException {
Path tempDir = createTempDir();
RecordingChangeListener changes = new RecordingChangeListener(tempDir);
Path testDir = tempDir.resolve("test-dir");
Files.createDirectories(testDir);
touch(testDir.resolve("test0.txt"));
touch(testDir.resolve("test1.txt"));
FileWatcher fileWatcher = new FileWatcher(testDir);
fileWatcher.addListener(changes);
fileWatcher.init();
changes.notifications().clear();
Files.delete(testDir.resolve("test0.txt"));
Files.delete(testDir.resolve("test1.txt"));
fileWatcher.checkAndNotify();
assertThat(changes.notifications(), contains(equalTo("onFileDeleted: test-dir/test0.txt"), equalTo("onFileDeleted: test-dir/test1.txt")));
}
use of java.nio.file.Path in project elasticsearch by elastic.
the class URLSnapshotRestoreTests method testUrlRepository.
public void testUrlRepository() throws Exception {
Client client = client();
logger.info("--> creating repository");
Path repositoryLocation = randomRepoPath();
assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType(FsRepository.TYPE).setSettings(Settings.builder().put(FsRepository.LOCATION_SETTING.getKey(), repositoryLocation).put(FsRepository.COMPRESS_SETTING.getKey(), randomBoolean()).put(FsRepository.CHUNK_SIZE_SETTING.getKey(), randomIntBetween(100, 1000), ByteSizeUnit.BYTES)));
createIndex("test-idx");
ensureGreen();
logger.info("--> indexing some data");
for (int i = 0; i < 100; i++) {
index("test-idx", "doc", Integer.toString(i), "foo", "bar" + i);
}
refresh();
assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().getTotalHits(), equalTo(100L));
logger.info("--> snapshot");
CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx").get();
assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
int actualTotalShards = createSnapshotResponse.getSnapshotInfo().totalShards();
assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(actualTotalShards));
SnapshotState state = client.admin().cluster().prepareGetSnapshots("test-repo").setSnapshots("test-snap").get().getSnapshots().get(0).state();
assertThat(state, equalTo(SnapshotState.SUCCESS));
logger.info("--> delete index");
cluster().wipeIndices("test-idx");
logger.info("--> create read-only URL repository");
assertAcked(client.admin().cluster().preparePutRepository("url-repo").setType(URLRepository.TYPE).setSettings(Settings.builder().put(URLRepository.URL_SETTING.getKey(), repositoryLocation.toUri().toURL()).put("list_directories", randomBoolean())));
logger.info("--> restore index after deletion");
RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("url-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx").execute().actionGet();
assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
assertThat(client.prepareSearch("test-idx").setSize(0).get().getHits().getTotalHits(), equalTo(100L));
logger.info("--> list available shapshots");
GetSnapshotsResponse getSnapshotsResponse = client.admin().cluster().prepareGetSnapshots("url-repo").get();
assertThat(getSnapshotsResponse.getSnapshots(), notNullValue());
assertThat(getSnapshotsResponse.getSnapshots().size(), equalTo(1));
logger.info("--> delete snapshot");
DeleteSnapshotResponse deleteSnapshotResponse = client.admin().cluster().prepareDeleteSnapshot("test-repo", "test-snap").get();
assertAcked(deleteSnapshotResponse);
logger.info("--> list available shapshot again, no snapshots should be returned");
getSnapshotsResponse = client.admin().cluster().prepareGetSnapshots("url-repo").get();
assertThat(getSnapshotsResponse.getSnapshots(), notNullValue());
assertThat(getSnapshotsResponse.getSnapshots().size(), equalTo(0));
}
Aggregations