use of java.nio.file.NoSuchFileException in project elasticsearch by elastic.
the class BlobStoreRepository method getRepositoryData.
@Override
public RepositoryData getRepositoryData() {
try {
final long indexGen = latestIndexBlobId();
final String snapshotsIndexBlobName = INDEX_FILE_PREFIX + Long.toString(indexGen);
RepositoryData repositoryData;
try (InputStream blob = snapshotsBlobContainer.readBlob(snapshotsIndexBlobName)) {
BytesStreamOutput out = new BytesStreamOutput();
Streams.copy(blob, out);
// EMPTY is safe here because RepositoryData#fromXContent calls namedObject
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, out.bytes())) {
repositoryData = RepositoryData.snapshotsFromXContent(parser, indexGen);
} catch (NotXContentException e) {
logger.warn("[{}] index blob is not valid x-content [{} bytes]", snapshotsIndexBlobName, out.bytes().length());
throw e;
}
}
// now load the incompatible snapshot ids, if they exist
try (InputStream blob = snapshotsBlobContainer.readBlob(INCOMPATIBLE_SNAPSHOTS_BLOB)) {
BytesStreamOutput out = new BytesStreamOutput();
Streams.copy(blob, out);
try (XContentParser parser = XContentHelper.createParser(NamedXContentRegistry.EMPTY, out.bytes())) {
repositoryData = repositoryData.incompatibleSnapshotsFromXContent(parser);
}
} catch (NoSuchFileException e) {
logger.debug("[{}] Incompatible snapshots blob [{}] does not exist, the likely reason is that " + "there are no incompatible snapshots in the repository", metadata.name(), INCOMPATIBLE_SNAPSHOTS_BLOB);
}
return repositoryData;
} catch (NoSuchFileException ex) {
// repository doesn't have an index blob, its a new blank repo
return RepositoryData.EMPTY;
} catch (IOException ioe) {
throw new RepositoryException(metadata.name(), "could not read repository data from index blob", ioe);
}
}
use of java.nio.file.NoSuchFileException in project elasticsearch by elastic.
the class URLBlobStoreTests method testNoBlobFound.
public void testNoBlobFound() throws IOException {
BlobContainer container = urlBlobStore.blobContainer(BlobPath.cleanPath().add("indices"));
String incorrectBlobName = "incorrect_" + blobName;
try (InputStream ignored = container.readBlob(incorrectBlobName)) {
fail("Should have thrown NoSuchFileException exception");
ignored.read();
} catch (NoSuchFileException e) {
assertEquals(String.format("[%s] blob not found", incorrectBlobName), e.getMessage());
}
}
use of java.nio.file.NoSuchFileException in project elasticsearch by elastic.
the class FileBasedUnicastHostsProvider method buildDynamicNodes.
@Override
public List<DiscoveryNode> buildDynamicNodes() {
List<String> hostsList;
try (Stream<String> lines = Files.lines(unicastHostsFilePath)) {
hostsList = // lines starting with `#` are comments
lines.filter(line -> line.startsWith("#") == false).collect(Collectors.toList());
} catch (FileNotFoundException | NoSuchFileException e) {
logger.warn((Supplier<?>) () -> new ParameterizedMessage("[discovery-file] Failed to find unicast hosts file [{}]", unicastHostsFilePath), e);
hostsList = Collections.emptyList();
} catch (IOException e) {
logger.warn((Supplier<?>) () -> new ParameterizedMessage("[discovery-file] Error reading unicast hosts file [{}]", unicastHostsFilePath), e);
hostsList = Collections.emptyList();
}
final List<DiscoveryNode> discoNodes = new ArrayList<>();
try {
discoNodes.addAll(resolveHostsLists(executorService, logger, hostsList, 1, transportService, UNICAST_HOST_PREFIX, resolveTimeout));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
logger.debug("[discovery-file] Using dynamic discovery nodes {}", discoNodes);
return discoNodes;
}
use of java.nio.file.NoSuchFileException in project elasticsearch by elastic.
the class StreamInput method readException.
public <T extends Exception> T readException() throws IOException {
if (readBoolean()) {
int key = readVInt();
switch(key) {
case 0:
final int ord = readVInt();
return (T) ElasticsearchException.readException(this, ord);
case 1:
String msg1 = readOptionalString();
String resource1 = readOptionalString();
return (T) readStackTrace(new CorruptIndexException(msg1, resource1, readException()), this);
case 2:
String resource2 = readOptionalString();
int version2 = readInt();
int minVersion2 = readInt();
int maxVersion2 = readInt();
return (T) readStackTrace(new IndexFormatTooNewException(resource2, version2, minVersion2, maxVersion2), this);
case 3:
String resource3 = readOptionalString();
if (readBoolean()) {
int version3 = readInt();
int minVersion3 = readInt();
int maxVersion3 = readInt();
return (T) readStackTrace(new IndexFormatTooOldException(resource3, version3, minVersion3, maxVersion3), this);
} else {
String version3 = readOptionalString();
return (T) readStackTrace(new IndexFormatTooOldException(resource3, version3), this);
}
case 4:
return (T) readStackTrace(new NullPointerException(readOptionalString()), this);
case 5:
return (T) readStackTrace(new NumberFormatException(readOptionalString()), this);
case 6:
return (T) readStackTrace(new IllegalArgumentException(readOptionalString(), readException()), this);
case 7:
return (T) readStackTrace(new AlreadyClosedException(readOptionalString(), readException()), this);
case 8:
return (T) readStackTrace(new EOFException(readOptionalString()), this);
case 9:
return (T) readStackTrace(new SecurityException(readOptionalString(), readException()), this);
case 10:
return (T) readStackTrace(new StringIndexOutOfBoundsException(readOptionalString()), this);
case 11:
return (T) readStackTrace(new ArrayIndexOutOfBoundsException(readOptionalString()), this);
case 12:
return (T) readStackTrace(new FileNotFoundException(readOptionalString()), this);
case 13:
final int subclass = readVInt();
final String file = readOptionalString();
final String other = readOptionalString();
final String reason = readOptionalString();
// skip the msg - it's composed from file, other and reason
readOptionalString();
final Exception exception;
switch(subclass) {
case 0:
exception = new NoSuchFileException(file, other, reason);
break;
case 1:
exception = new NotDirectoryException(file);
break;
case 2:
exception = new DirectoryNotEmptyException(file);
break;
case 3:
exception = new AtomicMoveNotSupportedException(file, other, reason);
break;
case 4:
exception = new FileAlreadyExistsException(file, other, reason);
break;
case 5:
exception = new AccessDeniedException(file, other, reason);
break;
case 6:
exception = new FileSystemLoopException(file);
break;
case 7:
exception = new FileSystemException(file, other, reason);
break;
default:
throw new IllegalStateException("unknown FileSystemException with index " + subclass);
}
return (T) readStackTrace(exception, this);
case 14:
return (T) readStackTrace(new IllegalStateException(readOptionalString(), readException()), this);
case 15:
return (T) readStackTrace(new LockObtainFailedException(readOptionalString(), readException()), this);
case 16:
return (T) readStackTrace(new InterruptedException(readOptionalString()), this);
case 17:
return (T) readStackTrace(new IOException(readOptionalString(), readException()), this);
default:
assert false : "no such exception for id: " + key;
}
}
return null;
}
use of java.nio.file.NoSuchFileException in project buck by facebook.
the class MoreFiles method concatenateFiles.
/**
* Concatenates the contents of one or more files.
*
* @param dest The path to which the concatenated files' contents are written.
* @param pathsToConcatenate The paths whose contents are concatenated to {@code dest}.
*
* @return {@code true} if any data was concatenated to {@code dest}, {@code false} otherwise.
*/
public static boolean concatenateFiles(Path dest, Iterable<Path> pathsToConcatenate) throws IOException {
// Concatenate all the logs to a temp file, then atomically rename it to the
// passed-in concatenatedPath if any log data was collected.
String tempFilename = "." + dest.getFileName() + ".tmp." + UUID.randomUUID().toString();
Path tempPath = dest.resolveSibling(tempFilename);
try {
long bytesCollected = 0;
try (OutputStream os = Files.newOutputStream(tempPath, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.WRITE)) {
for (Path path : pathsToConcatenate) {
try (InputStream is = Files.newInputStream(path)) {
bytesCollected += ByteStreams.copy(is, os);
} catch (NoSuchFileException e) {
continue;
}
}
}
if (bytesCollected > 0) {
Files.move(tempPath, dest, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
return true;
} else {
return false;
}
} finally {
Files.deleteIfExists(tempPath);
}
}
Aggregations