Example #1
0
 @Override
 FlushResponse doProcess() {
   FlushResponse res = esClient.flushTransLog(indexName).actionGet();
   int failedShards = res.getFailedShards();
   // 例外が発生しないがflushのエラーになった場合は、他のリクエストでのflushにまかせるため、ここではエラーとはしていない。
   if (failedShards > 0) {
     String message =
         String.format(
             "ES translog flush failed. index[%s] failedShardsCount[%d]",
             indexName, failedShards);
     log.info(message);
   }
   return res;
 }
  @Test
  @TestLogging(value = "cluster.service:TRACE,action.get:TRACE")
  public void testSimpleRecovery() throws Exception {
    assertAcked(prepareCreate("test", 1).execute().actionGet(5000));

    NumShards numShards = getNumShards("test");

    logger.info("Running Cluster Health");
    ensureYellow();

    client()
        .index(indexRequest("test").type("type1").id("1").source(source("1", "test")))
        .actionGet();
    FlushResponse flushResponse =
        client().admin().indices().flush(flushRequest("test")).actionGet();
    assertThat(flushResponse.getTotalShards(), equalTo(numShards.totalNumShards));
    assertThat(flushResponse.getSuccessfulShards(), equalTo(numShards.numPrimaries));
    assertThat(flushResponse.getFailedShards(), equalTo(0));
    client()
        .index(indexRequest("test").type("type1").id("2").source(source("2", "test")))
        .actionGet();
    RefreshResponse refreshResponse =
        client().admin().indices().refresh(refreshRequest("test")).actionGet();
    assertThat(refreshResponse.getTotalShards(), equalTo(numShards.totalNumShards));
    assertThat(refreshResponse.getSuccessfulShards(), equalTo(numShards.numPrimaries));
    assertThat(refreshResponse.getFailedShards(), equalTo(0));

    allowNodes("test", 2);

    logger.info("Running Cluster Health");
    ensureGreen();

    GetResponse getResult;

    for (int i = 0; i < 5; i++) {
      getResult =
          client()
              .get(getRequest("test").type("type1").id("1").operationThreaded(false))
              .actionGet(1000);
      assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
      getResult =
          client()
              .get(getRequest("test").type("type1").id("1").operationThreaded(false))
              .actionGet(1000);
      assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
      getResult =
          client()
              .get(getRequest("test").type("type1").id("2").operationThreaded(true))
              .actionGet(1000);
      assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
      getResult =
          client()
              .get(getRequest("test").type("type1").id("2").operationThreaded(true))
              .actionGet(1000);
      assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
    }

    // now start another one so we move some primaries
    allowNodes("test", 3);
    Thread.sleep(200);
    logger.info("Running Cluster Health");
    ensureGreen();

    for (int i = 0; i < 5; i++) {
      getResult = client().get(getRequest("test").type("type1").id("1")).actionGet(1000);
      assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
      getResult = client().get(getRequest("test").type("type1").id("1")).actionGet(1000);
      assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
      getResult = client().get(getRequest("test").type("type1").id("1")).actionGet(1000);
      assertThat(getResult.getSourceAsString(), equalTo(source("1", "test")));
      getResult =
          client()
              .get(getRequest("test").type("type1").id("2").operationThreaded(true))
              .actionGet(1000);
      assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
      getResult =
          client()
              .get(getRequest("test").type("type1").id("2").operationThreaded(true))
              .actionGet(1000);
      assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
      getResult =
          client()
              .get(getRequest("test").type("type1").id("2").operationThreaded(true))
              .actionGet(1000);
      assertThat(getResult.getSourceAsString(), equalTo(source("2", "test")));
    }
  }
  @Test
  public void testBroadcastOperations() throws IOException {
    startNode("server1");

    client("server1").admin().indices().prepareCreate("test").execute().actionGet(5000);

    logger.info("Running Cluster Health");
    ClusterHealthResponse clusterHealth =
        client("server1")
            .admin()
            .cluster()
            .health(clusterHealthRequest().waitForYellowStatus())
            .actionGet();
    logger.info("Done Cluster Health, status " + clusterHealth.status());
    assertThat(clusterHealth.timedOut(), equalTo(false));
    assertThat(clusterHealth.status(), equalTo(ClusterHealthStatus.YELLOW));

    client("server1")
        .index(indexRequest("test").type("type1").id("1").source(source("1", "test")))
        .actionGet();
    FlushResponse flushResponse =
        client("server1").admin().indices().flush(flushRequest("test")).actionGet();
    assertThat(flushResponse.totalShards(), equalTo(10));
    assertThat(flushResponse.successfulShards(), equalTo(5));
    assertThat(flushResponse.failedShards(), equalTo(0));
    client("server1")
        .index(indexRequest("test").type("type1").id("2").source(source("2", "test")))
        .actionGet();
    RefreshResponse refreshResponse =
        client("server1").admin().indices().refresh(refreshRequest("test")).actionGet();
    assertThat(refreshResponse.totalShards(), equalTo(10));
    assertThat(refreshResponse.successfulShards(), equalTo(5));
    assertThat(refreshResponse.failedShards(), equalTo(0));

    logger.info("Count");
    // check count
    for (int i = 0; i < 5; i++) {
      // test successful
      CountResponse countResponse =
          client("server1")
              .count(
                  countRequest("test")
                      .query(termQuery("_type", "type1"))
                      .operationThreading(BroadcastOperationThreading.NO_THREADS))
              .actionGet();
      assertThat(countResponse.count(), equalTo(2l));
      assertThat(countResponse.totalShards(), equalTo(5));
      assertThat(countResponse.successfulShards(), equalTo(5));
      assertThat(countResponse.failedShards(), equalTo(0));
    }

    for (int i = 0; i < 5; i++) {
      CountResponse countResponse =
          client("server1")
              .count(
                  countRequest("test")
                      .query(termQuery("_type", "type1"))
                      .operationThreading(BroadcastOperationThreading.SINGLE_THREAD))
              .actionGet();
      assertThat(countResponse.count(), equalTo(2l));
      assertThat(countResponse.totalShards(), equalTo(5));
      assertThat(countResponse.successfulShards(), equalTo(5));
      assertThat(countResponse.failedShards(), equalTo(0));
    }

    for (int i = 0; i < 5; i++) {
      CountResponse countResponse =
          client("server1")
              .count(
                  countRequest("test")
                      .query(termQuery("_type", "type1"))
                      .operationThreading(BroadcastOperationThreading.THREAD_PER_SHARD))
              .actionGet();
      assertThat(countResponse.count(), equalTo(2l));
      assertThat(countResponse.totalShards(), equalTo(5));
      assertThat(countResponse.successfulShards(), equalTo(5));
      assertThat(countResponse.failedShards(), equalTo(0));
    }

    for (int i = 0; i < 5; i++) {
      // test failed (simply query that can't be parsed)
      CountResponse countResponse =
          client("server1")
              .count(
                  countRequest("test")
                      .query(Unicode.fromStringAsBytes("{ term : { _type : \"type1 } }")))
              .actionGet();

      assertThat(countResponse.count(), equalTo(0l));
      assertThat(countResponse.totalShards(), equalTo(5));
      assertThat(countResponse.successfulShards(), equalTo(0));
      assertThat(countResponse.failedShards(), equalTo(5));
      for (ShardOperationFailedException exp : countResponse.shardFailures()) {
        assertThat(exp.reason(), containsString("QueryParsingException"));
      }
    }
  }