Package org.elasticsearch.test.integration.consistencylevel

Source Code of org.elasticsearch.test.integration.consistencylevel.WriteConsistencyLevelTests

/*
* Licensed to Elastic Search and Shay Banon under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. Elastic Search licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.test.integration.consistencylevel;

import org.elasticsearch.action.UnavailableShardsException;
import org.elasticsearch.action.WriteConsistencyLevel;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse;
import org.elasticsearch.action.admin.cluster.health.ClusterHealthStatus;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.test.integration.AbstractNodesTests;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.Test;

import static org.elasticsearch.common.unit.TimeValue.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

/**
* @author kimchy (shay.banon)
*/
public class WriteConsistencyLevelTests extends AbstractNodesTests {

    @AfterMethod public void closeNodes() {
        closeAllNodes();
    }

    @Test public void testWriteConsistencyLevelReplication2() throws Exception {
        startNode("node1");
        client("node1").admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("number_of_shards", 1).put("number_of_replicas", 2)).execute().actionGet();

        ClusterHealthResponse clusterHealth = client("node1").admin().cluster().prepareHealth().setWaitForActiveShards(1).setWaitForYellowStatus().execute().actionGet();
        logger.info("Done Cluster Health, status " + clusterHealth.status());
        assertThat(clusterHealth.timedOut(), equalTo(false));
        assertThat(clusterHealth.status(), equalTo(ClusterHealthStatus.YELLOW));

        // indexing, by default, will work (ONE consistency level)
        client("node1").prepareIndex("test", "type1", "1").setSource(source("1", "test")).setConsistencyLevel(WriteConsistencyLevel.ONE).execute().actionGet();
        try {
            client("node1").prepareIndex("test", "type1", "1").setSource(source("1", "test"))
                    .setConsistencyLevel(WriteConsistencyLevel.QUORUM)
                    .setTimeout(timeValueSeconds(1)).execute().actionGet();
            assert false : "can't index, does not match consistency";
        } catch (UnavailableShardsException e) {
            // all is well
        }

        startNode("node2");

        clusterHealth = client("node1").admin().cluster().prepareHealth().setWaitForActiveShards(2).setWaitForYellowStatus().execute().actionGet();
        logger.info("Done Cluster Health, status " + clusterHealth.status());
        assertThat(clusterHealth.timedOut(), equalTo(false));
        assertThat(clusterHealth.status(), equalTo(ClusterHealthStatus.YELLOW));

        // this should work, since we now have
        client("node1").prepareIndex("test", "type1", "1").setSource(source("1", "test"))
                .setConsistencyLevel(WriteConsistencyLevel.QUORUM)
                .setTimeout(timeValueSeconds(1)).execute().actionGet();

        try {
            client("node1").prepareIndex("test", "type1", "1").setSource(source("1", "test"))
                    .setConsistencyLevel(WriteConsistencyLevel.ALL)
                    .setTimeout(timeValueSeconds(1)).execute().actionGet();
            assert false : "can't index, does not match consistency";
        } catch (UnavailableShardsException e) {
            // all is well
        }

        startNode("node3");

        clusterHealth = client("node1").admin().cluster().prepareHealth().setWaitForActiveShards(3).setWaitForGreenStatus().execute().actionGet();
        logger.info("Done Cluster Health, status " + clusterHealth.status());
        assertThat(clusterHealth.timedOut(), equalTo(false));
        assertThat(clusterHealth.status(), equalTo(ClusterHealthStatus.GREEN));

        // this should work, since we now have
        client("node1").prepareIndex("test", "type1", "1").setSource(source("1", "test"))
                .setConsistencyLevel(WriteConsistencyLevel.ALL)
                .setTimeout(timeValueSeconds(1)).execute().actionGet();
    }

    private String source(String id, String nameValue) {
        return "{ type1 : { \"id\" : \"" + id + "\", \"name\" : \"" + nameValue + "\" } }";
    }
}
TOP

Related Classes of org.elasticsearch.test.integration.consistencylevel.WriteConsistencyLevelTests

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.