use of java.io.UncheckedIOException in project elasticsearch by elastic.
the class Ec2DiscoveryPlugin method getAvailabilityZoneNodeAttributes.
// pkg private for testing
@SuppressForbidden(reason = "We call getInputStream in doPrivileged and provide SocketPermission")
static Settings getAvailabilityZoneNodeAttributes(Settings settings, String azMetadataUrl) {
if (AwsEc2Service.AUTO_ATTRIBUTE_SETTING.get(settings) == false) {
return Settings.EMPTY;
}
Settings.Builder attrs = Settings.builder();
final URL url;
final URLConnection urlConnection;
try {
url = new URL(azMetadataUrl);
logger.debug("obtaining ec2 [placement/availability-zone] from ec2 meta-data url {}", url);
urlConnection = SocketAccess.doPrivilegedIOException(url::openConnection);
urlConnection.setConnectTimeout(2000);
} catch (IOException e) {
// should not happen, we know the url is not malformed, and openConnection does not actually hit network
throw new UncheckedIOException(e);
}
try (InputStream in = SocketAccess.doPrivilegedIOException(urlConnection::getInputStream);
BufferedReader urlReader = new BufferedReader(new InputStreamReader(in, StandardCharsets.UTF_8))) {
String metadataResult = urlReader.readLine();
if (metadataResult == null || metadataResult.length() == 0) {
throw new IllegalStateException("no ec2 metadata returned from " + url);
} else {
attrs.put(Node.NODE_ATTRIBUTES.getKey() + "aws_availability_zone", metadataResult);
}
} catch (IOException e) {
// this is lenient so the plugin does not fail when installed outside of ec2
logger.error("failed to get metadata for [placement/availability-zone]", e);
}
return attrs.build();
}
use of java.io.UncheckedIOException in project elasticsearch by elastic.
the class ObjectParserTests method testUseClassicPullParsingSubParser.
/**
* This test ensures we can use a classic pull-parsing parser
* together with the object parser
*/
public void testUseClassicPullParsingSubParser() throws IOException {
class ClassicParser {
URI parseURI(XContentParser parser) throws IOException {
String fieldName = null;
String host = "";
int port = 0;
XContentParser.Token token;
while ((token = parser.currentToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
fieldName = parser.currentName();
} else if (token == XContentParser.Token.VALUE_STRING) {
if (fieldName.equals("host")) {
host = parser.text();
} else {
throw new IllegalStateException("boom");
}
} else if (token == XContentParser.Token.VALUE_NUMBER) {
if (fieldName.equals("port")) {
port = parser.intValue();
} else {
throw new IllegalStateException("boom");
}
}
parser.nextToken();
}
return URI.create(host + ":" + port);
}
}
class Foo {
public String name;
public URI uri;
public void setName(String name) {
this.name = name;
}
public void setURI(URI uri) {
this.uri = uri;
}
}
class CustomParseContext {
public final ClassicParser parser;
CustomParseContext(ClassicParser parser) {
this.parser = parser;
}
public URI parseURI(XContentParser parser) {
try {
return this.parser.parseURI(parser);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
XContentParser parser = createParser(JsonXContent.jsonXContent, "{\"url\" : { \"host\": \"http://foobar\", \"port\" : 80}, \"name\" : \"foobarbaz\"}");
ObjectParser<Foo, CustomParseContext> objectParser = new ObjectParser<>("foo");
objectParser.declareString(Foo::setName, new ParseField("name"));
objectParser.declareObjectOrDefault(Foo::setURI, (p, s) -> s.parseURI(p), () -> null, new ParseField("url"));
Foo s = objectParser.parse(parser, new Foo(), new CustomParseContext(new ClassicParser()));
assertEquals(s.uri.getHost(), "foobar");
assertEquals(s.uri.getPort(), 80);
assertEquals(s.name, "foobarbaz");
}
use of java.io.UncheckedIOException in project elasticsearch by elastic.
the class NestedObjectMapperTests method testLimitOfNestedFieldsPerIndex.
public void testLimitOfNestedFieldsPerIndex() throws Exception {
Function<String, String> mapping = type -> {
try {
return XContentFactory.jsonBuilder().startObject().startObject(type).startObject("properties").startObject("nested1").field("type", "nested").startObject("properties").startObject("nested2").field("type", "nested").endObject().endObject().endObject().endObject().endObject().endObject().string();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
};
// default limit allows at least two nested fields
createIndex("test1").mapperService().merge("type", new CompressedXContent(mapping.apply("type")), MergeReason.MAPPING_UPDATE, false);
// explicitly setting limit to 0 prevents nested fields
Exception e = expectThrows(IllegalArgumentException.class, () -> createIndex("test2", Settings.builder().put(MapperService.INDEX_MAPPING_NESTED_FIELDS_LIMIT_SETTING.getKey(), 0).build()).mapperService().merge("type", new CompressedXContent(mapping.apply("type")), MergeReason.MAPPING_UPDATE, false));
assertThat(e.getMessage(), containsString("Limit of nested fields [0] in index [test2] has been exceeded"));
// setting limit to 1 with 2 nested fields fails
e = expectThrows(IllegalArgumentException.class, () -> createIndex("test3", Settings.builder().put(MapperService.INDEX_MAPPING_NESTED_FIELDS_LIMIT_SETTING.getKey(), 1).build()).mapperService().merge("type", new CompressedXContent(mapping.apply("type")), MergeReason.MAPPING_UPDATE, false));
assertThat(e.getMessage(), containsString("Limit of nested fields [1] in index [test3] has been exceeded"));
MapperService mapperService = createIndex("test4", Settings.builder().put(MapperService.INDEX_MAPPING_NESTED_FIELDS_LIMIT_SETTING.getKey(), 2).build()).mapperService();
mapperService.merge("type1", new CompressedXContent(mapping.apply("type1")), MergeReason.MAPPING_UPDATE, false);
// merging same fields, but different type is ok
mapperService.merge("type2", new CompressedXContent(mapping.apply("type2")), MergeReason.MAPPING_UPDATE, false);
// adding new fields from different type is not ok
String mapping2 = XContentFactory.jsonBuilder().startObject().startObject("type3").startObject("properties").startObject("nested3").field("type", "nested").startObject("properties").endObject().endObject().endObject().endObject().endObject().string();
e = expectThrows(IllegalArgumentException.class, () -> mapperService.merge("type3", new CompressedXContent(mapping2), MergeReason.MAPPING_UPDATE, false));
assertThat(e.getMessage(), containsString("Limit of nested fields [2] in index [test4] has been exceeded"));
// do not check nested fields limit if mapping is not updated
createIndex("test5", Settings.builder().put(MapperService.INDEX_MAPPING_NESTED_FIELDS_LIMIT_SETTING.getKey(), 0).build()).mapperService().merge("type", new CompressedXContent(mapping.apply("type")), MergeReason.MAPPING_RECOVERY, false);
}
use of java.io.UncheckedIOException in project elasticsearch by elastic.
the class ElasticsearchUncaughtExceptionHandlerTests method testIsFatalCause.
public void testIsFatalCause() {
assertFatal(new MergePolicy.MergeException(new OutOfMemoryError(), null));
assertFatal(new OutOfMemoryError());
assertFatal(new StackOverflowError());
assertFatal(new InternalError());
assertFatal(new UnknownError());
assertFatal(new IOError(new IOException()));
assertNonFatal(new RuntimeException());
assertNonFatal(new UncheckedIOException(new IOException()));
}
use of java.io.UncheckedIOException in project elasticsearch by elastic.
the class ESTestCase method assertPathHasBeenCleared.
/**
* Asserts that there are no files in the specified path
*/
public void assertPathHasBeenCleared(Path path) {
logger.info("--> checking that [{}] has been cleared", path);
int count = 0;
StringBuilder sb = new StringBuilder();
sb.append("[");
if (Files.exists(path)) {
try (DirectoryStream<Path> stream = Files.newDirectoryStream(path)) {
for (Path file : stream) {
// Skip files added by Lucene's ExtraFS
if (file.getFileName().toString().startsWith("extra")) {
continue;
}
logger.info("--> found file: [{}]", file.toAbsolutePath().toString());
if (Files.isDirectory(file)) {
assertPathHasBeenCleared(file);
} else if (Files.isRegularFile(file)) {
count++;
sb.append(file.toAbsolutePath().toString());
sb.append("\n");
}
}
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
sb.append("]");
assertThat(count + " files exist that should have been cleaned:\n" + sb.toString(), count, equalTo(0));
}
Aggregations