Search in sources :

Example 46 with ElasticsearchException

use of org.elasticsearch.ElasticsearchException in project elasticsearch by elastic.

the class Netty4HttpServerTransportTests method testBadRequest.

public void testBadRequest() throws InterruptedException {
    final AtomicReference<Throwable> causeReference = new AtomicReference<>();
    final HttpServerTransport.Dispatcher dispatcher = new HttpServerTransport.Dispatcher() {

        @Override
        public void dispatchRequest(final RestRequest request, final RestChannel channel, final ThreadContext threadContext) {
            throw new AssertionError();
        }

        @Override
        public void dispatchBadRequest(final RestRequest request, final RestChannel channel, final ThreadContext threadContext, final Throwable cause) {
            causeReference.set(cause);
            try {
                final ElasticsearchException e = new ElasticsearchException("you sent a bad request and you should feel bad");
                channel.sendResponse(new BytesRestResponse(channel, BAD_REQUEST, e));
            } catch (final IOException e) {
                throw new AssertionError(e);
            }
        }
    };
    final Settings settings;
    final int maxInitialLineLength;
    final Setting<ByteSizeValue> httpMaxInitialLineLengthSetting = HttpTransportSettings.SETTING_HTTP_MAX_INITIAL_LINE_LENGTH;
    if (randomBoolean()) {
        maxInitialLineLength = httpMaxInitialLineLengthSetting.getDefault(Settings.EMPTY).bytesAsInt();
        settings = Settings.EMPTY;
    } else {
        maxInitialLineLength = randomIntBetween(1, 8192);
        settings = Settings.builder().put(httpMaxInitialLineLengthSetting.getKey(), maxInitialLineLength + "b").build();
    }
    try (Netty4HttpServerTransport transport = new Netty4HttpServerTransport(settings, networkService, bigArrays, threadPool, xContentRegistry(), dispatcher)) {
        transport.start();
        final TransportAddress remoteAddress = randomFrom(transport.boundAddress.boundAddresses());
        try (Netty4HttpClient client = new Netty4HttpClient()) {
            final String url = "/" + new String(new byte[maxInitialLineLength], Charset.forName("UTF-8"));
            final FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, url);
            final FullHttpResponse response = client.post(remoteAddress.address(), request);
            assertThat(response.status(), equalTo(HttpResponseStatus.BAD_REQUEST));
            assertThat(new String(response.content().array(), Charset.forName("UTF-8")), containsString("you sent a bad request and you should feel bad"));
        }
    }
    assertNotNull(causeReference.get());
    assertThat(causeReference.get(), instanceOf(TooLongFrameException.class));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) TransportAddress(org.elasticsearch.common.transport.TransportAddress) ByteSizeValue(org.elasticsearch.common.unit.ByteSizeValue) ElasticsearchException(org.elasticsearch.ElasticsearchException) Matchers.containsString(org.hamcrest.Matchers.containsString) Strings.collectionToDelimitedString(org.elasticsearch.common.Strings.collectionToDelimitedString) NullDispatcher(org.elasticsearch.http.NullDispatcher) HttpServerTransport(org.elasticsearch.http.HttpServerTransport) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) Settings(org.elasticsearch.common.settings.Settings) HttpTransportSettings(org.elasticsearch.http.HttpTransportSettings) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) ThreadContext(org.elasticsearch.common.util.concurrent.ThreadContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) RestChannel(org.elasticsearch.rest.RestChannel) IOException(java.io.IOException) RestRequest(org.elasticsearch.rest.RestRequest)

Example 47 with ElasticsearchException

use of org.elasticsearch.ElasticsearchException in project elasticsearch by elastic.

the class TestAmazonS3 method hashCode.

private int hashCode(String path) {
    try {
        MessageDigest digest = MessageDigest.getInstance("MD5");
        byte[] bytes = digest.digest(path.getBytes("UTF-8"));
        int i = 0;
        return ((bytes[i++] & 0xFF) << 24) | ((bytes[i++] & 0xFF) << 16) | ((bytes[i++] & 0xFF) << 8) | (bytes[i++] & 0xFF);
    } catch (UnsupportedEncodingException ex) {
        throw new ElasticsearchException("cannot calculate hashcode", ex);
    } catch (NoSuchAlgorithmException ex) {
        throw new ElasticsearchException("cannot calculate hashcode", ex);
    }
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) ElasticsearchException(org.elasticsearch.ElasticsearchException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) MessageDigest(java.security.MessageDigest)

Example 48 with ElasticsearchException

use of org.elasticsearch.ElasticsearchException in project elasticsearch by elastic.

the class TestAmazonS3 method putObject.

@Override
public PutObjectResult putObject(String bucketName, String key, InputStream input, ObjectMetadata metadata) throws AmazonClientException, AmazonServiceException {
    if (shouldFail(bucketName, key, writeFailureRate)) {
        long length = metadata.getContentLength();
        long partToRead = (long) (length * randomDouble());
        byte[] buffer = new byte[1024];
        for (long cur = 0; cur < partToRead; cur += buffer.length) {
            try {
                input.read(buffer, 0, (int) (partToRead - cur > buffer.length ? buffer.length : partToRead - cur));
            } catch (IOException ex) {
                throw new ElasticsearchException("cannot read input stream", ex);
            }
        }
        logger.info("--> random write failure on putObject method: throwing an exception for [bucket={}, key={}]", bucketName, key);
        AmazonS3Exception ex = new AmazonS3Exception("Random S3 exception");
        ex.setStatusCode(400);
        ex.setErrorCode("RequestTimeout");
        throw ex;
    } else {
        return super.putObject(bucketName, key, input, metadata);
    }
}
Also used : IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) AmazonS3Exception(com.amazonaws.services.s3.model.AmazonS3Exception)

Example 49 with ElasticsearchException

use of org.elasticsearch.ElasticsearchException in project elasticsearch by elastic.

the class AzureRepositoryF method main.

public static void main(String[] args) throws Throwable {
    Settings.Builder settings = Settings.builder();
    settings.put("http.cors.enabled", "true");
    settings.put("http.cors.allow-origin", "*");
    settings.put("cluster.name", AzureRepositoryF.class.getSimpleName());
    // Example for azure repo settings
    // settings.put("cloud.azure.storage.my_account1.account", "account_name");
    // settings.put("cloud.azure.storage.my_account1.key", "account_key");
    // settings.put("cloud.azure.storage.my_account1.default", true);
    // settings.put("cloud.azure.storage.my_account2.account", "account_name");
    // settings.put("cloud.azure.storage.my_account2.key", "account_key_secondary");
    final CountDownLatch latch = new CountDownLatch(1);
    final Node node = new MockNode(settings.build(), Collections.singletonList(AzureRepositoryPlugin.class));
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            try {
                IOUtils.close(node);
            } catch (IOException e) {
                throw new ElasticsearchException(e);
            } finally {
                latch.countDown();
            }
        }
    });
    node.start();
    latch.await();
}
Also used : MockNode(org.elasticsearch.node.MockNode) Node(org.elasticsearch.node.Node) AzureRepositoryPlugin(org.elasticsearch.plugin.repository.azure.AzureRepositoryPlugin) MockNode(org.elasticsearch.node.MockNode) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) CountDownLatch(java.util.concurrent.CountDownLatch) Settings(org.elasticsearch.common.settings.Settings)

Example 50 with ElasticsearchException

use of org.elasticsearch.ElasticsearchException in project elasticsearch by elastic.

the class PendingClusterStatesQueueTests method testFailedStateCleansSupersededStatesOnly.

public void testFailedStateCleansSupersededStatesOnly() {
    List<ClusterState> states = randomStates(scaledRandomIntBetween(10, 50), "master1", "master2", "master3", "master4");
    PendingClusterStatesQueue queue = createQueueWithStates(states);
    List<ClusterStateContext> committedContexts = randomCommitStates(queue);
    ClusterState toFail = randomFrom(committedContexts).state;
    queue.markAsFailed(toFail, new ElasticsearchException("boo!"));
    final Map<String, ClusterStateContext> committedContextsById = new HashMap<>();
    for (ClusterStateContext context : committedContexts) {
        committedContextsById.put(context.stateUUID(), context);
    }
    // now check that queue doesn't contain superseded states
    for (ClusterStateContext context : queue.pendingStates) {
        if (context.committed()) {
            assertFalse("found a committed cluster state, which is superseded by a failed state.\nFound:" + context.state + "\nfailed:" + toFail, toFail.supersedes(context.state));
        }
    }
    // check no state has been erroneously removed
    for (ClusterState state : states) {
        ClusterStateContext pendingContext = queue.findState(state.stateUUID());
        if (pendingContext != null) {
            continue;
        }
        if (state.equals(toFail)) {
            continue;
        }
        assertThat("non-committed states should never be removed", committedContextsById, hasKey(state.stateUUID()));
        final ClusterStateContext context = committedContextsById.get(state.stateUUID());
        assertThat("removed state is not superseded by failed state. \nRemoved state:" + context + "\nfailed: " + toFail, toFail.supersedes(context.state), equalTo(true));
        assertThat("removed state was failed with wrong exception", ((MockListener) context.listener).failure, notNullValue());
        assertThat("removed state was failed with wrong exception", ((MockListener) context.listener).failure.getMessage(), containsString("boo"));
    }
}
Also used : ClusterState(org.elasticsearch.cluster.ClusterState) PendingClusterStatesQueue(org.elasticsearch.discovery.zen.PendingClusterStatesQueue) HashMap(java.util.HashMap) ElasticsearchException(org.elasticsearch.ElasticsearchException) Matchers.containsString(org.hamcrest.Matchers.containsString) ClusterStateContext(org.elasticsearch.discovery.zen.PendingClusterStatesQueue.ClusterStateContext)

Aggregations

ElasticsearchException (org.elasticsearch.ElasticsearchException)309 IOException (java.io.IOException)127 Settings (org.elasticsearch.common.settings.Settings)32 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)30 HashMap (java.util.HashMap)29 ClusterState (org.elasticsearch.cluster.ClusterState)29 ArrayList (java.util.ArrayList)28 Matchers.containsString (org.hamcrest.Matchers.containsString)25 List (java.util.List)20 Map (java.util.Map)20 AtomicReference (java.util.concurrent.atomic.AtomicReference)20 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)18 XContentParser (org.elasticsearch.common.xcontent.XContentParser)17 Path (java.nio.file.Path)16 Test (org.junit.Test)16 ActionListener (org.elasticsearch.action.ActionListener)15 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 Version (org.elasticsearch.Version)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13 ResourceNotFoundException (org.elasticsearch.ResourceNotFoundException)13