@Test
  @TestLogging(value = "cluster.service:TRACE")
  public void testDeleteCreateInOneBulk() throws Exception {
    internalCluster()
        .startNodesAsync(
            2, Settings.builder().put(DiscoveryModule.DISCOVERY_TYPE_KEY, "zen").build())
        .get();
    assertFalse(client().admin().cluster().prepareHealth().setWaitForNodes("2").get().isTimedOut());
    prepareCreate("test")
        .setSettings(IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS, true)
        .addMapping("type")
        .get();
    ensureGreen("test");

    // now that the cluster is stable, remove publishing timeout
    assertAcked(
        client()
            .admin()
            .cluster()
            .prepareUpdateSettings()
            .setTransientSettings(Settings.builder().put(DiscoverySettings.PUBLISH_TIMEOUT, "0")));

    Set<String> nodes = new HashSet<>(Arrays.asList(internalCluster().getNodeNames()));
    nodes.remove(internalCluster().getMasterName());

    // block none master node.
    BlockClusterStateProcessing disruption =
        new BlockClusterStateProcessing(nodes.iterator().next(), getRandom());
    internalCluster().setDisruptionScheme(disruption);
    logger.info("--> indexing a doc");
    index("test", "type", "1");
    refresh();
    disruption.startDisrupting();
    logger.info("--> delete index and recreate it");
    assertFalse(
        client()
            .admin()
            .indices()
            .prepareDelete("test")
            .setTimeout("200ms")
            .get()
            .isAcknowledged());
    assertFalse(
        prepareCreate("test")
            .setTimeout("200ms")
            .setSettings(IndexMetaData.SETTING_AUTO_EXPAND_REPLICAS, true)
            .get()
            .isAcknowledged());
    logger.info("--> letting cluster proceed");
    disruption.stopDisrupting();
    ensureGreen(TimeValue.timeValueMinutes(30), "test");
    assertHitCount(client().prepareSearch("test").get(), 0);
  }
예제 #2
0
  @Test
  public void testClusterRerouteNoAcknowledgementDryRun() throws InterruptedException {
    client()
        .admin()
        .indices()
        .prepareCreate("test")
        .setSettings(
            settingsBuilder()
                .put(
                    SETTING_NUMBER_OF_SHARDS,
                    between(cluster().numDataNodes(), DEFAULT_MAX_NUM_SHARDS))
                .put(SETTING_NUMBER_OF_REPLICAS, 0))
        .get();
    ensureGreen();

    MoveAllocationCommand moveAllocationCommand = getAllocationCommand();

    ClusterRerouteResponse clusterRerouteResponse =
        client()
            .admin()
            .cluster()
            .prepareReroute()
            .setTimeout("0s")
            .setDryRun(true)
            .add(moveAllocationCommand)
            .get();
    // acknowledged anyway as no changes were made
    assertThat(clusterRerouteResponse.isAcknowledged(), equalTo(true));
  }
예제 #3
0
 public void testCloseAndReopenOrDeleteWithActiveScroll() throws IOException {
   createIndex("test");
   for (int i = 0; i < 100; i++) {
     client()
         .prepareIndex("test", "type1", Integer.toString(i))
         .setSource(jsonBuilder().startObject().field("field", i).endObject())
         .execute()
         .actionGet();
   }
   refresh();
   SearchResponse searchResponse =
       client()
           .prepareSearch()
           .setQuery(matchAllQuery())
           .setSize(35)
           .setScroll(TimeValue.timeValueMinutes(2))
           .addSort("field", SortOrder.ASC)
           .execute()
           .actionGet();
   long counter = 0;
   assertThat(searchResponse.getHits().getTotalHits(), equalTo(100l));
   assertThat(searchResponse.getHits().hits().length, equalTo(35));
   for (SearchHit hit : searchResponse.getHits()) {
     assertThat(((Number) hit.sortValues()[0]).longValue(), equalTo(counter++));
   }
   if (randomBoolean()) {
     client().admin().indices().prepareClose("test").get();
     client().admin().indices().prepareOpen("test").get();
     ensureGreen("test");
   } else {
     client().admin().indices().prepareDelete("test").get();
   }
 }
  @Test
  public void testPutMappingsWithBlocks() throws Exception {
    createIndex("test");
    ensureGreen();

    for (String block : Arrays.asList(SETTING_BLOCKS_READ, SETTING_BLOCKS_WRITE)) {
      try {
        enableIndexBlock("test", block);
        assertAcked(
            client()
                .admin()
                .indices()
                .preparePutMapping("test")
                .setType("doc")
                .setSource("{\"properties\":{\"date\":{\"type\":\"integer\"}}}"));
      } finally {
        disableIndexBlock("test", block);
      }
    }

    for (String block : Arrays.asList(SETTING_READ_ONLY, SETTING_BLOCKS_METADATA)) {
      try {
        enableIndexBlock("test", block);
        assertBlocked(
            client()
                .admin()
                .indices()
                .preparePutMapping("test")
                .setType("doc")
                .setSource("{\"properties\":{\"date\":{\"type\":\"integer\"}}}"));
      } finally {
        disableIndexBlock("test", block);
      }
    }
  }
예제 #5
0
  @Test
  public void testUpsertFields() throws Exception {
    createIndex();
    ensureGreen();

    UpdateResponse updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setUpsert(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject())
            .setScript("ctx._source.extra = \"foo\"", ScriptService.ScriptType.INLINE)
            .setFields("_source")
            .execute()
            .actionGet();

    assertThat(updateResponse.getGetResult(), notNullValue());
    assertThat(updateResponse.getGetResult().sourceAsMap().get("bar").toString(), equalTo("baz"));
    assertThat(updateResponse.getGetResult().sourceAsMap().get("extra"), nullValue());

    updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setUpsert(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject())
            .setScript("ctx._source.extra = \"foo\"", ScriptService.ScriptType.INLINE)
            .setFields("_source")
            .execute()
            .actionGet();

    assertThat(updateResponse.getGetResult(), notNullValue());
    assertThat(updateResponse.getGetResult().sourceAsMap().get("bar").toString(), equalTo("baz"));
    assertThat(updateResponse.getGetResult().sourceAsMap().get("extra").toString(), equalTo("foo"));
  }
  @Test
  public void testNullShape() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type1")
            .startObject("properties")
            .startObject("location")
            .field("type", "geo_shape")
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();
    prepareCreate("test").addMapping("type1", mapping).execute().actionGet();
    ensureGreen();

    client()
        .prepareIndex("test", "type1", "aNullshape")
        .setSource("{\"location\": null}")
        .execute()
        .actionGet();
    GetResponse result = client().prepareGet("test", "type1", "aNullshape").execute().actionGet();
    assertThat(result.getField("location"), nullValue());
  }
예제 #7
0
  @Test
  public void testUpsert() throws Exception {
    createIndex();
    ensureGreen();

    UpdateResponse updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject())
            .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
            .execute()
            .actionGet();
    assertTrue(updateResponse.isCreated());

    for (int i = 0; i < 5; i++) {
      GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
      assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("1"));
    }

    updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setUpsert(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject())
            .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
            .execute()
            .actionGet();
    assertFalse(updateResponse.isCreated());

    for (int i = 0; i < 5; i++) {
      GetResponse getResponse = client().prepareGet("test", "type1", "1").execute().actionGet();
      assertThat(getResponse.getSourceAsMap().get("field").toString(), equalTo("2"));
    }
  }
예제 #8
0
  @Test
  public void testCloseIndexNoAcknowledgement() {
    createIndex("test");
    ensureGreen();

    CloseIndexResponse closeIndexResponse =
        client().admin().indices().prepareClose("test").setTimeout("0s").get();
    assertThat(closeIndexResponse.isAcknowledged(), equalTo(false));
  }
  @Test
  public void testMasterAwareExecution() throws Exception {
    Settings settings = settingsBuilder().put("discovery.type", "local").build();

    ListenableFuture<String> master = internalCluster().startNodeAsync(settings);
    ListenableFuture<String> nonMaster =
        internalCluster()
            .startNodeAsync(settingsBuilder().put(settings).put("node.master", false).build());
    master.get();
    ensureGreen(); // make sure we have a cluster

    ClusterService clusterService =
        internalCluster().getInstance(ClusterService.class, nonMaster.get());

    final boolean[] taskFailed = {false};
    final CountDownLatch latch1 = new CountDownLatch(1);
    clusterService.submitStateUpdateTask(
        "test",
        new ClusterStateUpdateTask() {
          @Override
          public ClusterState execute(ClusterState currentState) throws Exception {
            latch1.countDown();
            return currentState;
          }

          @Override
          public void onFailure(String source, Throwable t) {
            taskFailed[0] = true;
            latch1.countDown();
          }
        });

    latch1.await();
    assertTrue("cluster state update task was executed on a non-master", taskFailed[0]);

    taskFailed[0] = true;
    final CountDownLatch latch2 = new CountDownLatch(1);
    clusterService.submitStateUpdateTask(
        "test",
        new ClusterStateNonMasterUpdateTask() {
          @Override
          public ClusterState execute(ClusterState currentState) throws Exception {
            taskFailed[0] = false;
            latch2.countDown();
            return currentState;
          }

          @Override
          public void onFailure(String source, Throwable t) {
            taskFailed[0] = true;
            latch2.countDown();
          }
        });
    latch2.await();
    assertFalse("non-master cluster state update task was not executed", taskFailed[0]);
  }
예제 #10
0
  @Test
  public void testCreateIndexNoAcknowledgement() {
    CreateIndexResponse createIndexResponse =
        client().admin().indices().prepareCreate("test").setTimeout("0s").get();
    assertThat(createIndexResponse.isAcknowledged(), equalTo(false));

    // let's wait for green, otherwise there can be issues with after test checks (mock directory
    // wrapper etc.)
    ensureGreen();
  }
예제 #11
0
  @Test
  public void testRequiredRoutingWithPathMappingBulk() throws Exception {
    client()
        .admin()
        .indices()
        .prepareCreate("test")
        .addAlias(new Alias("alias"))
        .addMapping(
            "type1",
            XContentFactory.jsonBuilder()
                .startObject()
                .startObject("type1")
                .startObject("_routing")
                .field("required", true)
                .field("path", "routing_field")
                .endObject()
                .endObject()
                .endObject())
        .execute()
        .actionGet();
    ensureGreen();

    logger.info("--> indexing with id [1], and routing [0]");
    client()
        .prepareBulk()
        .add(
            client()
                .prepareIndex(indexOrAlias(), "type1", "1")
                .setSource("field", "value1", "routing_field", "0"))
        .execute()
        .actionGet();
    client().admin().indices().prepareRefresh().execute().actionGet();

    logger.info("--> verifying get with no routing, should fail");
    for (int i = 0; i < 5; i++) {
      try {
        client().prepareGet(indexOrAlias(), "type1", "1").execute().actionGet().isExists();
        fail();
      } catch (RoutingMissingException e) {
        assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST));
        assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
      }
    }
    logger.info("--> verifying get with routing, should find");
    for (int i = 0; i < 5; i++) {
      assertThat(
          client()
              .prepareGet(indexOrAlias(), "type1", "1")
              .setRouting("0")
              .execute()
              .actionGet()
              .isExists(),
          equalTo(true));
    }
  }
예제 #12
0
  @Test
  public void testClusterRerouteAcknowledgementDryRun() throws InterruptedException {
    client()
        .admin()
        .indices()
        .prepareCreate("test")
        .setSettings(
            settingsBuilder()
                .put(
                    SETTING_NUMBER_OF_SHARDS,
                    between(cluster().numDataNodes(), DEFAULT_MAX_NUM_SHARDS))
                .put(SETTING_NUMBER_OF_REPLICAS, 0))
        .get();
    ensureGreen();

    MoveAllocationCommand moveAllocationCommand = getAllocationCommand();

    assertAcked(
        client().admin().cluster().prepareReroute().setDryRun(true).add(moveAllocationCommand));

    // testing only on master with the latest cluster state as we didn't make any change thus we
    // cannot guarantee that
    // all nodes hold the same cluster state version. We only know there was no need to change
    // anything, thus no need for ack on this update.
    ClusterStateResponse clusterStateResponse = client().admin().cluster().prepareState().get();
    boolean found = false;
    for (ShardRouting shardRouting :
        clusterStateResponse
            .getState()
            .getRoutingNodes()
            .routingNodeIter(moveAllocationCommand.fromNode())) {
      // the shard that we wanted to move is still on the same node, as we had dryRun flag
      if (shardRouting.shardId().equals(moveAllocationCommand.shardId())) {
        assertThat(shardRouting.started(), equalTo(true));
        found = true;
        break;
      }
    }
    assertThat(found, equalTo(true));

    for (ShardRouting shardRouting :
        clusterStateResponse
            .getState()
            .getRoutingNodes()
            .routingNodeIter(moveAllocationCommand.toNode())) {
      if (shardRouting.shardId().equals(moveAllocationCommand.shardId())) {
        fail(
            "shard ["
                + shardRouting
                + "] shouldn't be on node ["
                + moveAllocationCommand.toString()
                + "]");
      }
    }
  }
예제 #13
0
  @Test
  public void testCopyFromFileWithPartition() throws Exception {
    execute("create table quotes (id int, " + "quote string) partitioned by (id)");
    ensureGreen();
    String filePath = Joiner.on(File.separator).join(copyFilePath, "test_copy_from.json");
    execute("copy quotes partition (id = 1) from ? with (shared=true)", new Object[] {filePath});
    refresh();

    execute("select * from quotes");
    assertEquals(3L, response.rowCount());
  }
예제 #14
0
 @Before
 public void setup() {
   int numIndices = iterations(1, 5);
   for (int i = 0; i < numIndices; i++) {
     indices.add("test" + i);
   }
   for (String index : indices) {
     assertAcked(prepareCreate(index).addAlias(new Alias(index + "-alias")));
   }
   ensureGreen();
 }
예제 #15
0
  public void testCloseIndexAcknowledgement() {
    createIndex("test");
    ensureGreen();

    assertAcked(client().admin().indices().prepareClose("test"));

    for (Client client : clients()) {
      IndexMetaData indexMetaData = getLocalClusterState(client).metaData().indices().get("test");
      assertThat(indexMetaData.getState(), equalTo(State.CLOSE));
    }
  }
예제 #16
0
  @Test
  public void testCreateIndexAcknowledgement() {
    createIndex("test");

    for (Client client : clients()) {
      assertThat(
          getLocalClusterState(client).metaData().indices().containsKey("test"), equalTo(true));
    }

    // let's wait for green, otherwise there can be issues with after test checks (mock directory
    // wrapper etc.)
    // but we do want to check that the new index is on all nodes cluster state even before green
    ensureGreen();
  }
예제 #17
0
  @Test
  // See: https://github.com/elasticsearch/elasticsearch/issues/3265
  public void testNotUpsertDoc() throws Exception {
    createIndex();
    ensureGreen();

    assertThrows(
        client()
            .prepareUpdate("test", "type1", "1")
            .setDoc(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject())
            .setDocAsUpsert(false)
            .setFields("_source")
            .execute(),
        DocumentMissingException.class);
  }
  @Test
  public void testMultipleAliasesPrecedence() throws Exception {
    client()
        .admin()
        .indices()
        .preparePutTemplate("template1")
        .setTemplate("*")
        .setOrder(0)
        .addAlias(new Alias("alias1"))
        .addAlias(new Alias("{index}-alias"))
        .addAlias(new Alias("alias3").filter(FilterBuilders.missingFilter("test")))
        .addAlias(new Alias("alias4"))
        .get();

    client()
        .admin()
        .indices()
        .preparePutTemplate("template2")
        .setTemplate("te*")
        .setOrder(1)
        .addAlias(new Alias("alias1").routing("test"))
        .addAlias(new Alias("alias3"))
        .get();

    assertAcked(
        prepareCreate("test").addAlias(new Alias("test-alias").searchRouting("test-routing")));

    ensureGreen();

    GetAliasesResponse getAliasesResponse =
        client().admin().indices().prepareGetAliases().addIndices("test").get();
    assertThat(getAliasesResponse.getAliases().get("test").size(), equalTo(4));

    for (AliasMetaData aliasMetaData : getAliasesResponse.getAliases().get("test")) {
      assertThat(
          aliasMetaData.alias(),
          anyOf(equalTo("alias1"), equalTo("test-alias"), equalTo("alias3"), equalTo("alias4")));
      if ("alias1".equals(aliasMetaData.alias())) {
        assertThat(aliasMetaData.indexRouting(), equalTo("test"));
        assertThat(aliasMetaData.searchRouting(), equalTo("test"));
      } else if ("alias3".equals(aliasMetaData.alias())) {
        assertThat(aliasMetaData.filter(), nullValue());
      } else if ("test-alias".equals(aliasMetaData.alias())) {
        assertThat(aliasMetaData.indexRouting(), nullValue());
        assertThat(aliasMetaData.searchRouting(), equalTo("test-routing"));
      }
    }
  }
예제 #19
0
  @Test
  public void testPutMappingNoAcknowledgement() {
    createIndex("test");
    ensureGreen();

    PutMappingResponse putMappingResponse =
        client()
            .admin()
            .indices()
            .preparePutMapping("test")
            .setType("test")
            .setSource("field", "type=string,index=not_analyzed")
            .setTimeout("0s")
            .get();
    assertThat(putMappingResponse.isAcknowledged(), equalTo(false));
  }
예제 #20
0
  @Test
  public void testUpsertDoc() throws Exception {
    createIndex();
    ensureGreen();

    UpdateResponse updateResponse =
        client()
            .prepareUpdate("test", "type1", "1")
            .setDoc(XContentFactory.jsonBuilder().startObject().field("bar", "baz").endObject())
            .setDocAsUpsert(true)
            .setFields("_source")
            .execute()
            .actionGet();
    assertThat(updateResponse.getGetResult(), notNullValue());
    assertThat(updateResponse.getGetResult().sourceAsMap().get("bar").toString(), equalTo("baz"));
  }
  @Test
  public void testPointsOnly() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type1")
            .startObject("properties")
            .startObject("location")
            .field("type", "geo_shape")
            .field("tree", randomBoolean() ? "quadtree" : "geohash")
            .field("tree_levels", "6")
            .field("distance_error_pct", "0.01")
            .field("points_only", true)
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();

    assertAcked(prepareCreate("geo_points_only").addMapping("type1", mapping));
    ensureGreen();

    ShapeBuilder shape = RandomShapeGenerator.createShape(random());
    try {
      index(
          "geo_points_only",
          "type1",
          "1",
          jsonBuilder().startObject().field("location", shape).endObject());
    } catch (MapperParsingException e) {
      // RandomShapeGenerator created something other than a POINT type, verify the correct
      // exception is thrown
      assertThat(e.getCause().getMessage(), containsString("is configured for points only"));
      return;
    }

    refresh();
    // test that point was inserted
    SearchResponse response =
        client()
            .prepareSearch()
            .setQuery(geoIntersectionQuery("location", shape))
            .execute()
            .actionGet();

    assertEquals(1, response.getHits().getTotalHits());
  }
예제 #22
0
  @Test
  public void testUpdateRequestWithBothScriptAndDoc() throws Exception {
    createIndex();
    ensureGreen();

    try {
      client()
          .prepareUpdate("test", "type1", "1")
          .setDoc(XContentFactory.jsonBuilder().startObject().field("field", 1).endObject())
          .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
          .execute()
          .actionGet();
      fail("Should have thrown ActionRequestValidationException");
    } catch (ActionRequestValidationException e) {
      assertThat(e.validationErrors().size(), equalTo(1));
      assertThat(e.validationErrors().get(0), containsString("can't provide both script and doc"));
      assertThat(e.getMessage(), containsString("can't provide both script and doc"));
    }
  }
예제 #23
0
  @Test
  public void testPutMappingAcknowledgement() {
    createIndex("test");
    ensureGreen();

    assertAcked(
        client()
            .admin()
            .indices()
            .preparePutMapping("test")
            .setType("test")
            .setSource("field", "type=string,index=not_analyzed"));

    for (Client client : clients()) {
      assertThat(
          getLocalClusterState(client).metaData().indices().get("test").mapping("test"),
          notNullValue());
    }
  }
  @Test
  public void testIndexTemplateWithAliasesSource() {
    client()
        .admin()
        .indices()
        .preparePutTemplate("template_1")
        .setTemplate("te*")
        .setAliases(
            "    {\n"
                + "        \"alias1\" : {},\n"
                + "        \"alias2\" : {\n"
                + "            \"filter\" : {\n"
                + "                \"type\" : {\n"
                + "                    \"value\" : \"type2\"\n"
                + "                }\n"
                + "            }\n"
                + "         },\n"
                + "        \"alias3\" : { \"routing\" : \"1\" }"
                + "    }\n")
        .get();

    assertAcked(prepareCreate("test_index").addMapping("type1").addMapping("type2"));
    ensureGreen();

    GetAliasesResponse getAliasesResponse =
        client().admin().indices().prepareGetAliases().setIndices("test_index").get();
    assertThat(getAliasesResponse.getAliases().size(), equalTo(1));
    assertThat(getAliasesResponse.getAliases().get("test_index").size(), equalTo(3));

    client().prepareIndex("test_index", "type1", "1").setSource("field", "value1").get();
    client().prepareIndex("test_index", "type2", "2").setSource("field", "value2").get();
    refresh();

    SearchResponse searchResponse = client().prepareSearch("test_index").get();
    assertHitCount(searchResponse, 2l);

    searchResponse = client().prepareSearch("alias1").get();
    assertHitCount(searchResponse, 2l);

    searchResponse = client().prepareSearch("alias2").get();
    assertHitCount(searchResponse, 1l);
    assertThat(searchResponse.getHits().getAt(0).type(), equalTo("type2"));
  }
예제 #25
0
  private int indexRandomNumbers(String analyzer, int shards) throws Exception {
    Builder builder =
        ImmutableSettings.settingsBuilder()
            .put(indexSettings())
            .put(SETTING_NUMBER_OF_REPLICAS, between(0, 1));

    if (shards > 0) {
      builder.put(SETTING_NUMBER_OF_SHARDS, shards);
    }

    client()
        .admin()
        .indices()
        .prepareCreate("test")
        .addMapping(
            "type1",
            jsonBuilder()
                .startObject()
                .startObject("type1")
                .startObject("properties")
                .startObject("field1")
                .field("analyzer", analyzer)
                .field("type", "string")
                .endObject()
                .endObject()
                .endObject()
                .endObject())
        .setSettings(builder)
        .get();
    int numDocs = atLeast(100);
    IndexRequestBuilder[] docs = new IndexRequestBuilder[numDocs];
    for (int i = 0; i < numDocs; i++) {
      docs[i] =
          client()
              .prepareIndex("test", "type1", String.valueOf(i))
              .setSource("field1", English.intToEnglish(i));
    }

    indexRandom(true, docs);
    ensureGreen();
    return numDocs;
  }
예제 #26
0
  @Test
  public void testClusterRerouteAcknowledgement() throws InterruptedException {
    assertAcked(
        prepareCreate("test")
            .setSettings(
                Settings.builder()
                    .put(indexSettings())
                    .put(
                        SETTING_NUMBER_OF_SHARDS,
                        between(cluster().numDataNodes(), DEFAULT_MAX_NUM_SHARDS))
                    .put(SETTING_NUMBER_OF_REPLICAS, 0)));
    ensureGreen();

    MoveAllocationCommand moveAllocationCommand = getAllocationCommand();

    assertAcked(client().admin().cluster().prepareReroute().add(moveAllocationCommand));

    for (Client client : clients()) {
      ClusterState clusterState = getLocalClusterState(client);
      for (ShardRouting shardRouting :
          clusterState.getRoutingNodes().routingNodeIter(moveAllocationCommand.fromNode())) {
        // if the shard that we wanted to move is still on the same node, it must be relocating
        if (shardRouting.shardId().equals(moveAllocationCommand.shardId())) {
          assertThat(shardRouting.relocating(), equalTo(true));
        }
      }

      boolean found = false;
      for (ShardRouting shardRouting :
          clusterState.getRoutingNodes().routingNodeIter(moveAllocationCommand.toNode())) {
        if (shardRouting.shardId().equals(moveAllocationCommand.shardId())) {
          assertThat(
              shardRouting.state(),
              anyOf(equalTo(ShardRoutingState.INITIALIZING), equalTo(ShardRoutingState.STARTED)));
          found = true;
          break;
        }
      }
      assertThat(found, equalTo(true));
    }
  }
예제 #27
0
 @Test
 public void testUpdateRequestWithScriptAndShouldUpsertDoc() throws Exception {
   createIndex();
   ensureGreen();
   try {
     client()
         .prepareUpdate("test", "type1", "1")
         .setScript("ctx._source.field += 1", ScriptService.ScriptType.INLINE)
         .setDocAsUpsert(true)
         .execute()
         .actionGet();
     fail("Should have thrown ActionRequestValidationException");
   } catch (ActionRequestValidationException e) {
     assertThat(e.validationErrors().size(), equalTo(1));
     assertThat(
         e.validationErrors().get(0),
         containsString("doc must be specified if doc_as_upsert is enabled"));
     assertThat(
         e.getMessage(), containsString("doc must be specified if doc_as_upsert is enabled"));
   }
 }
예제 #28
0
  @Test
  public void testCopyFromWithRoutingInPK() throws Exception {
    execute(
        "create table t (i int primary key, c string primary key, a int)"
            + " clustered by (c) with (number_of_replicas=0)");
    ensureGreen();
    execute("insert into t (i, c) values (1, 'clusteredbyvalue'), (2, 'clusteredbyvalue')");
    refresh();

    String uri = Paths.get(folder.getRoot().toURI()).toString();
    SQLResponse response = execute("copy t to directory ? with(shared=true)", new Object[] {uri});
    assertThat(response.rowCount(), is(2L));

    execute("delete from t");
    refresh();

    execute("copy t from ? with (shared=true)", new Object[] {uri + "/t_*"});
    refresh();

    // only one shard should have all imported rows, since we have the same routing for both rows
    response = execute("select count(*) from sys.shards where num_docs>0 and table_name='t'");
    assertThat((long) response.rows()[0][0], is(1L));
  }
  @Test(expected = RepositoryVerificationException.class)
  @Ignore
  public void testWrongPath() {
    Client client = client();
    logger.info("-->  creating hdfs repository with path [{}]", path);

    PutRepositoryResponse putRepositoryResponse =
        client
            .admin()
            .cluster()
            .preparePutRepository("test-repo")
            .setType("hdfs")
            .setSettings(
                Settings.settingsBuilder()
                    .put("uri", "file://./")
                    .put("path", path + "a@b$c#11:22")
                    .put("chunk_size", randomIntBetween(100, 1000) + "k")
                    .put("compress", randomBoolean()))
            .get();
    assertThat(putRepositoryResponse.isAcknowledged(), equalTo(true));

    createIndex("test-idx-1", "test-idx-2", "test-idx-3");
    ensureGreen();
  }
  @Test
  public void testIndexPointsFilterRectangle() throws Exception {
    String mapping =
        XContentFactory.jsonBuilder()
            .startObject()
            .startObject("type1")
            .startObject("properties")
            .startObject("location")
            .field("type", "geo_shape")
            .field("tree", "quadtree")
            .endObject()
            .endObject()
            .endObject()
            .endObject()
            .string();
    prepareCreate("test").addMapping("type1", mapping).execute().actionGet();
    ensureGreen();

    client()
        .prepareIndex("test", "type1", "1")
        .setSource(
            jsonBuilder()
                .startObject()
                .field("name", "Document 1")
                .startObject("location")
                .field("type", "point")
                .startArray("coordinates")
                .value(-30)
                .value(-30)
                .endArray()
                .endObject()
                .endObject())
        .execute()
        .actionGet();

    client()
        .prepareIndex("test", "type1", "2")
        .setSource(
            jsonBuilder()
                .startObject()
                .field("name", "Document 2")
                .startObject("location")
                .field("type", "point")
                .startArray("coordinates")
                .value(-45)
                .value(-50)
                .endArray()
                .endObject()
                .endObject())
        .execute()
        .actionGet();

    refresh();
    client().admin().indices().prepareRefresh().execute().actionGet();

    ShapeBuilder shape = ShapeBuilder.newEnvelope().topLeft(-45, 45).bottomRight(45, -45);

    SearchResponse searchResponse =
        client()
            .prepareSearch()
            .setQuery(filteredQuery(matchAllQuery(), geoIntersectionFilter("location", shape)))
            .execute()
            .actionGet();

    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l));
    assertThat(searchResponse.getHits().hits().length, equalTo(1));
    assertThat(searchResponse.getHits().getAt(0).id(), equalTo("1"));

    searchResponse =
        client().prepareSearch().setQuery(geoShapeQuery("location", shape)).execute().actionGet();

    assertThat(searchResponse.getHits().getTotalHits(), equalTo(1l));
    assertThat(searchResponse.getHits().hits().length, equalTo(1));
    assertThat(searchResponse.getHits().getAt(0).id(), equalTo("1"));
  }