Search in sources :

Example 41 with UncheckedIOException

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();
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) Settings(org.elasticsearch.common.settings.Settings) URL(java.net.URL) URLConnection(java.net.URLConnection) SuppressForbidden(org.elasticsearch.common.SuppressForbidden)

Example 42 with UncheckedIOException

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");
}
Also used : NamedObjectParser(org.elasticsearch.common.xcontent.ObjectParser.NamedObjectParser) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) URI(java.net.URI) ParseField(org.elasticsearch.common.ParseField)

Example 43 with UncheckedIOException

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);
}
Also used : UncheckedIOException(java.io.UncheckedIOException) XContentFactory(org.elasticsearch.common.xcontent.XContentFactory) ESSingleNodeTestCase(org.elasticsearch.test.ESSingleNodeTestCase) Settings(org.elasticsearch.common.settings.Settings) Dynamic(org.elasticsearch.index.mapper.ObjectMapper.Dynamic) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) Matchers.equalTo(org.hamcrest.Matchers.equalTo) Matchers.nullValue(org.hamcrest.Matchers.nullValue) MergeReason(org.elasticsearch.index.mapper.MapperService.MergeReason) IOException(java.io.IOException) Function(java.util.function.Function) Matchers.containsString(org.hamcrest.Matchers.containsString) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) Matchers.containsString(org.hamcrest.Matchers.containsString) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException)

Example 44 with UncheckedIOException

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()));
}
Also used : IOError(java.io.IOError) MergePolicy(org.apache.lucene.index.MergePolicy) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException)

Example 45 with UncheckedIOException

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));
}
Also used : Path(java.nio.file.Path) UncheckedIOException(java.io.UncheckedIOException) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException)

Aggregations

UncheckedIOException (java.io.UncheckedIOException)76 IOException (java.io.IOException)72 Path (java.nio.file.Path)13 InputStream (java.io.InputStream)7 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)7 File (java.io.File)6 Arrays (java.util.Arrays)6 HashMap (java.util.HashMap)6 Map (java.util.Map)6 List (java.util.List)5 Collectors (java.util.stream.Collectors)5 BufferedReader (java.io.BufferedReader)4 URL (java.net.URL)4 IntStream (java.util.stream.IntStream)4 Protein (de.bioforscher.jstructure.model.structure.Protein)3 InterruptedIOException (java.io.InterruptedIOException)3 Random (java.util.Random)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3