@Test
  public void simpleFlagTests() {
    AllocationService allocation =
        new AllocationService(
            settingsBuilder().put("cluster.routing.allocation.concurrent_recoveries", 10).build());

    logger.info("creating an index with 1 shard, no replica");
    MetaData metaData =
        newMetaDataBuilder()
            .put(newIndexMetaDataBuilder("test").numberOfShards(1).numberOfReplicas(0))
            .build();
    RoutingTable routingTable = routingTable().addAsNew(metaData.index("test")).build();
    ClusterState clusterState =
        newClusterStateBuilder().metaData(metaData).routingTable(routingTable).build();
    assertThat(
        clusterState.routingTable().index("test").shard(0).primaryAllocatedPostApi(),
        equalTo(false));

    logger.info("adding two nodes and performing rerouting");
    clusterState =
        newClusterStateBuilder()
            .state(clusterState)
            .nodes(newNodesBuilder().put(newNode("node1")).put(newNode("node2")))
            .build();
    RoutingAllocation.Result rerouteResult = allocation.reroute(clusterState);
    clusterState =
        newClusterStateBuilder()
            .state(clusterState)
            .routingTable(rerouteResult.routingTable())
            .build();
    assertThat(
        clusterState.routingTable().index("test").shard(0).primaryAllocatedPostApi(),
        equalTo(false));

    logger.info("start primary shard");
    rerouteResult =
        allocation.applyStartedShards(
            clusterState, clusterState.routingNodes().shardsWithState(INITIALIZING));
    clusterState =
        newClusterStateBuilder()
            .state(clusterState)
            .routingTable(rerouteResult.routingTable())
            .build();
    assertThat(
        clusterState.routingTable().index("test").shard(0).primaryAllocatedPostApi(),
        equalTo(true));
  }
  public void testClusterStateUpdateTask() {
    AllocationService allocationService =
        new AllocationService(
            Settings.builder().build(),
            new AllocationDeciders(
                Settings.EMPTY,
                Collections.singleton(new MaxRetryAllocationDecider(Settings.EMPTY))),
            NoopGatewayAllocator.INSTANCE,
            new BalancedShardsAllocator(Settings.EMPTY),
            EmptyClusterInfoService.INSTANCE);
    ClusterState clusterState = createInitialClusterState(allocationService);
    ClusterRerouteRequest req = new ClusterRerouteRequest();
    req.dryRun(true);
    AtomicReference<ClusterRerouteResponse> responseRef = new AtomicReference<>();
    ActionListener<ClusterRerouteResponse> responseActionListener =
        new ActionListener<ClusterRerouteResponse>() {
          @Override
          public void onResponse(ClusterRerouteResponse clusterRerouteResponse) {
            responseRef.set(clusterRerouteResponse);
          }

          @Override
          public void onFailure(Exception e) {}
        };
    TransportClusterRerouteAction.ClusterRerouteResponseAckedClusterStateUpdateTask task =
        new TransportClusterRerouteAction.ClusterRerouteResponseAckedClusterStateUpdateTask(
            logger, allocationService, req, responseActionListener);
    ClusterState execute = task.execute(clusterState);
    assertSame(execute, clusterState); // dry-run
    task.onAllNodesAcked(null);
    assertNotSame(responseRef.get().getState(), execute);

    req.dryRun(false); // now we allocate

    final int retries = MaxRetryAllocationDecider.SETTING_ALLOCATION_MAX_RETRY.get(Settings.EMPTY);
    // now fail it N-1 times
    for (int i = 0; i < retries; i++) {
      ClusterState newState = task.execute(clusterState);
      assertNotSame(newState, clusterState); // dry-run=false
      clusterState = newState;
      RoutingTable routingTable = clusterState.routingTable();
      assertEquals(routingTable.index("idx").shards().size(), 1);
      assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), INITIALIZING);
      assertEquals(
          routingTable
              .index("idx")
              .shard(0)
              .shards()
              .get(0)
              .unassignedInfo()
              .getNumFailedAllocations(),
          i);
      List<FailedRerouteAllocation.FailedShard> failedShards =
          Collections.singletonList(
              new FailedRerouteAllocation.FailedShard(
                  routingTable.index("idx").shard(0).shards().get(0),
                  "boom" + i,
                  new UnsupportedOperationException()));
      RoutingAllocation.Result result =
          allocationService.applyFailedShards(clusterState, failedShards);
      assertTrue(result.changed());
      clusterState = ClusterState.builder(clusterState).routingTable(result.routingTable()).build();
      routingTable = clusterState.routingTable();
      assertEquals(routingTable.index("idx").shards().size(), 1);
      if (i == retries - 1) {
        assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), UNASSIGNED);
      } else {
        assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), INITIALIZING);
      }
      assertEquals(
          routingTable
              .index("idx")
              .shard(0)
              .shards()
              .get(0)
              .unassignedInfo()
              .getNumFailedAllocations(),
          i + 1);
    }

    // without retry_failed we won't allocate that shard
    ClusterState newState = task.execute(clusterState);
    assertNotSame(newState, clusterState); // dry-run=false
    task.onAllNodesAcked(null);
    assertSame(responseRef.get().getState(), newState);
    RoutingTable routingTable = clusterState.routingTable();
    assertEquals(routingTable.index("idx").shards().size(), 1);
    assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), UNASSIGNED);
    assertEquals(
        routingTable
            .index("idx")
            .shard(0)
            .shards()
            .get(0)
            .unassignedInfo()
            .getNumFailedAllocations(),
        retries);

    req.setRetryFailed(true); // now we manually retry and get the shard back into initializing
    newState = task.execute(clusterState);
    assertNotSame(newState, clusterState); // dry-run=false
    clusterState = newState;
    routingTable = clusterState.routingTable();
    assertEquals(routingTable.index("idx").shards().size(), 1);
    assertEquals(routingTable.index("idx").shard(0).shards().get(0).state(), INITIALIZING);
    assertEquals(
        routingTable
            .index("idx")
            .shard(0)
            .shards()
            .get(0)
            .unassignedInfo()
            .getNumFailedAllocations(),
        retries);
  }
示例#3
0
 public Builder routingResult(RoutingAllocation.Result routingResult) {
   this.routingTable = routingResult.routingTable();
   this.allocationExplanation = routingResult.explanation();
   return this;
 }