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); } }
@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"); }
/** * 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); }
public static String generateNodeId(Settings settings) { Random random = Randomness.get(settings, NODE_ID_SEED_SETTING); return UUIDs.randomBase64UUID(random); }