private static String randomNodeName(Path configDir) {
    InputStream input;
    if (configDir != null && Files.exists(configDir.resolve("names.txt"))) {
      Path namesPath = configDir.resolve("names.txt");
      try {
        input = Files.newInputStream(namesPath);
      } catch (IOException e) {
        throw new RuntimeException("Failed to load custom names.txt from " + namesPath, e);
      }
    } else {
      input = InternalSettingsPreparer.class.getResourceAsStream("/config/names.txt");
    }

    try {
      List<String> names = new ArrayList<>();
      try (BufferedReader reader =
          new BufferedReader(new InputStreamReader(input, StandardCharsets.UTF_8))) {
        String name = reader.readLine();
        while (name != null) {
          names.add(name);
          name = reader.readLine();
        }
      }
      int index = Randomness.get().nextInt(names.size());
      return names.get(index);
    } catch (IOException e) {
      throw new RuntimeException("Could not read node names list", e);
    }
  }
  public void testOutOfOrderCommitMessages() throws Throwable {
    MockNode node = createMockNode("node").setAsMaster();
    final CapturingTransportChannel channel = new CapturingTransportChannel();

    List<ClusterState> states = new ArrayList<>();
    final int numOfStates = scaledRandomIntBetween(3, 25);
    for (int i = 1; i <= numOfStates; i++) {
      states.add(
          ClusterState.builder(node.clusterState)
              .version(i)
              .stateUUID(ClusterState.UNKNOWN_UUID)
              .build());
    }

    final ClusterState finalState = states.get(numOfStates - 1);

    logger.info("--> publishing states");
    for (ClusterState state : states) {
      node.action.handleIncomingClusterStateRequest(
          new BytesTransportRequest(
              PublishClusterStateAction.serializeFullClusterState(state, Version.CURRENT),
              Version.CURRENT),
          channel);
      assertThat(
          channel.response.get(), equalTo((TransportResponse) TransportResponse.Empty.INSTANCE));
      assertThat(channel.error.get(), nullValue());
      channel.clear();
    }

    logger.info("--> committing states");

    long largestVersionSeen = Long.MIN_VALUE;
    Randomness.shuffle(states);
    for (ClusterState state : states) {
      node.action.handleCommitRequest(
          new PublishClusterStateAction.CommitClusterStateRequest(state.stateUUID()), channel);
      if (largestVersionSeen < state.getVersion()) {
        assertThat(
            channel.response.get(), equalTo((TransportResponse) TransportResponse.Empty.INSTANCE));
        if (channel.error.get() != null) {
          throw channel.error.get();
        }
        largestVersionSeen = state.getVersion();
      } else {
        // older cluster states will be rejected
        assertNotNull(channel.error.get());
        assertThat(channel.error.get(), instanceOf(IllegalStateException.class));
      }
      channel.clear();
    }

    // now check the last state held
    assertSameState(node.clusterState, finalState);
  }
 @Override
 protected GroupShardsIterator shards(
     ClusterState clusterState, ValidateQueryRequest request, String[] concreteIndices) {
   // Hard-code routing to limit request to a single shard, but still, randomize it...
   Map<String, Set<String>> routingMap =
       indexNameExpressionResolver.resolveSearchRouting(
           clusterState, Integer.toString(Randomness.get().nextInt(1000)), request.indices());
   return clusterService
       .operationRouting()
       .searchShards(clusterState, concreteIndices, routingMap, "_local");
 }
예제 #4
0
 /**
  * simulate a remote error for the given requestId, will be wrapped by a {@link
  * RemoteTransportException}
  *
  * @param requestId the id corresponding to the captured send request
  * @param t the failure to wrap
  */
 public void handleRemoteError(final long requestId, final Throwable t) {
   final RemoteTransportException remoteException;
   if (rarely(Randomness.get())) {
     remoteException = new RemoteTransportException("remote failure, coming from local node", t);
   } else {
     try (BytesStreamOutput output = new BytesStreamOutput()) {
       output.writeException(t);
       remoteException =
           new RemoteTransportException(
               "remote failure", output.bytes().streamInput().readException());
     } catch (IOException ioException) {
       throw new ElasticsearchException(
           "failed to serialize/deserialize supplied exception " + t, ioException);
     }
   }
   this.handleError(requestId, remoteException);
 }
예제 #5
0
 public void shuffle() {
   Randomness.shuffle(unassigned);
 }
 public static String generateNodeId(Settings settings) {
   Random random = Randomness.get(settings, NODE_ID_SEED_SETTING);
   return UUIDs.randomBase64UUID(random);
 }