use of org.opensearch.client.NodeSelector in project OpenSearch by opensearch-project.
the class DoSection method parseVersionSelector.
private static NodeSelector parseVersionSelector(XContentParser parser) throws IOException {
if (false == parser.currentToken().isValue()) {
throw new XContentParseException(parser.getTokenLocation(), "expected [version] to be a value");
}
List<VersionRange> skipVersionRanges = SkipSection.parseVersionRanges(parser.text());
return new NodeSelector() {
@Override
public void select(Iterable<Node> nodes) {
for (Iterator<Node> itr = nodes.iterator(); itr.hasNext(); ) {
Node node = itr.next();
if (node.getVersion() == null) {
throw new IllegalStateException("expected [version] metadata to be set but got " + node);
}
Version version = Version.fromString(node.getVersion());
boolean skip = skipVersionRanges.stream().anyMatch(v -> v.contains(version));
if (false == skip) {
itr.remove();
}
}
}
@Override
public String toString() {
return "version ranges " + skipVersionRanges;
}
};
}
use of org.opensearch.client.NodeSelector in project OpenSearch by opensearch-project.
the class DoSection method parseAttributeValuesSelector.
private static NodeSelector parseAttributeValuesSelector(XContentParser parser) throws IOException {
if (parser.currentToken() != XContentParser.Token.START_OBJECT) {
throw new XContentParseException(parser.getTokenLocation(), "expected START_OBJECT");
}
String key = null;
XContentParser.Token token;
NodeSelector result = NodeSelector.ANY;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
key = parser.currentName();
} else if (token.isValue()) {
/*
* HasAttributeNodeSelector selects nodes that do not have
* attribute metadata set so it can be used against nodes that
* have not yet been sniffed. In these tests we expect the node
* metadata to be explicitly sniffed if we need it and we'd
* like to hard fail if it is not so we wrap the selector so we
* can assert that the data is sniffed.
*/
NodeSelector delegate = new HasAttributeNodeSelector(key, parser.text());
NodeSelector newSelector = new NodeSelector() {
@Override
public void select(Iterable<Node> nodes) {
for (Node node : nodes) {
if (node.getAttributes() == null) {
throw new IllegalStateException("expected [attributes] metadata to be set but got " + node);
}
}
delegate.select(nodes);
}
@Override
public String toString() {
return delegate.toString();
}
};
result = result == NodeSelector.ANY ? newSelector : new ComposeNodeSelector(result, newSelector);
} else {
throw new XContentParseException(parser.getTokenLocation(), "expected [" + key + "] to be a value");
}
}
return result;
}
use of org.opensearch.client.NodeSelector in project OpenSearch by opensearch-project.
the class DoSection method parse.
public static DoSection parse(XContentParser parser) throws IOException {
String currentFieldName = null;
XContentParser.Token token;
DoSection doSection = new DoSection(parser.getTokenLocation());
ApiCallSection apiCallSection = null;
NodeSelector nodeSelector = NodeSelector.ANY;
Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
List<String> expectedWarnings = new ArrayList<>();
List<String> allowedWarnings = new ArrayList<>();
if (parser.nextToken() != XContentParser.Token.START_OBJECT) {
throw new IllegalArgumentException("expected [" + XContentParser.Token.START_OBJECT + "], " + "found [" + parser.currentToken() + "], the do section is not properly indented");
}
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if ("catch".equals(currentFieldName)) {
doSection.setCatch(parser.text());
} else {
throw new ParsingException(parser.getTokenLocation(), "unsupported field [" + currentFieldName + "]");
}
} else if (token == XContentParser.Token.START_ARRAY) {
if ("warnings".equals(currentFieldName)) {
while ((token = parser.nextToken()) == XContentParser.Token.VALUE_STRING) {
expectedWarnings.add(parser.text());
}
if (token != XContentParser.Token.END_ARRAY) {
throw new ParsingException(parser.getTokenLocation(), "[warnings] must be a string array but saw [" + token + "]");
}
} else if ("allowed_warnings".equals(currentFieldName)) {
while ((token = parser.nextToken()) == XContentParser.Token.VALUE_STRING) {
allowedWarnings.add(parser.text());
}
if (token != XContentParser.Token.END_ARRAY) {
throw new ParsingException(parser.getTokenLocation(), "[allowed_warnings] must be a string array but saw [" + token + "]");
}
} else {
throw new ParsingException(parser.getTokenLocation(), "unknown array [" + currentFieldName + "]");
}
} else if (token == XContentParser.Token.START_OBJECT) {
if ("headers".equals(currentFieldName)) {
String headerName = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
headerName = parser.currentName();
} else if (token.isValue()) {
headers.put(headerName, parser.text());
}
}
} else if ("node_selector".equals(currentFieldName)) {
String selectorName = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
selectorName = parser.currentName();
} else {
NodeSelector newSelector = buildNodeSelector(selectorName, parser);
nodeSelector = nodeSelector == NodeSelector.ANY ? newSelector : new ComposeNodeSelector(nodeSelector, newSelector);
}
}
} else if (currentFieldName != null) {
// must be part of API call then
apiCallSection = new ApiCallSection(currentFieldName);
String paramName = null;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
paramName = parser.currentName();
} else if (token.isValue()) {
if ("body".equals(paramName)) {
String body = parser.text();
XContentParser bodyParser = JsonXContent.jsonXContent.createParser(NamedXContentRegistry.EMPTY, DeprecationHandler.THROW_UNSUPPORTED_OPERATION, body);
// multiple bodies are supported e.g. in case of bulk provided as a whole string
while (bodyParser.nextToken() != null) {
apiCallSection.addBody(bodyParser.mapOrdered());
}
} else {
apiCallSection.addParam(paramName, parser.text());
}
} else if (token == XContentParser.Token.START_OBJECT) {
if ("body".equals(paramName)) {
apiCallSection.addBody(parser.mapOrdered());
}
}
}
}
}
}
try {
if (apiCallSection == null) {
throw new IllegalArgumentException("client call section is mandatory within a do section");
}
for (String w : expectedWarnings) {
if (allowedWarnings.contains(w)) {
throw new IllegalArgumentException("the warning [" + w + "] was both allowed and expected");
}
}
apiCallSection.addHeaders(headers);
apiCallSection.setNodeSelector(nodeSelector);
doSection.setApiCallSection(apiCallSection);
doSection.setExpectedWarningHeaders(unmodifiableList(expectedWarnings));
doSection.setAllowedWarningHeaders(unmodifiableList(allowedWarnings));
} finally {
parser.nextToken();
}
return doSection;
}
use of org.opensearch.client.NodeSelector in project OpenSearch by opensearch-project.
the class RestClientDocumentation method usage.
// end::rest-client-options-singleton
@SuppressWarnings("unused")
public void usage() throws IOException, InterruptedException {
// tag::rest-client-init
RestClient restClient = RestClient.builder(new HttpHost("localhost", 9200, "http"), new HttpHost("localhost", 9201, "http")).build();
// end::rest-client-init
// tag::rest-client-close
restClient.close();
// end::rest-client-close
{
// tag::rest-client-init-default-headers
RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
Header[] defaultHeaders = new Header[] { new BasicHeader("header", "value") };
// <1>
builder.setDefaultHeaders(defaultHeaders);
// end::rest-client-init-default-headers
}
{
// tag::rest-client-init-node-selector
RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
// <1>
builder.setNodeSelector(NodeSelector.SKIP_DEDICATED_MASTERS);
// end::rest-client-init-node-selector
}
{
// tag::rest-client-init-allocation-aware-selector
RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
builder.setNodeSelector(new // <1>
NodeSelector() {
@Override
public void select(Iterable<Node> nodes) {
/*
* Prefer any node that belongs to rack_one. If none is around
* we will go to another rack till it's time to try and revive
* some of the nodes that belong to rack_one.
*/
boolean foundOne = false;
for (Node node : nodes) {
String rackId = node.getAttributes().get("rack_id").get(0);
if ("rack_one".equals(rackId)) {
foundOne = true;
break;
}
}
if (foundOne) {
Iterator<Node> nodesIt = nodes.iterator();
while (nodesIt.hasNext()) {
Node node = nodesIt.next();
String rackId = node.getAttributes().get("rack_id").get(0);
if ("rack_one".equals(rackId) == false) {
nodesIt.remove();
}
}
}
}
});
// end::rest-client-init-allocation-aware-selector
}
{
// tag::rest-client-init-failure-listener
RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
builder.setFailureListener(new RestClient.FailureListener() {
@Override
public void onFailure(Node node) {
// <1>
}
});
// end::rest-client-init-failure-listener
}
{
// tag::rest-client-init-request-config-callback
RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
builder.setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback() {
@Override
public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder requestConfigBuilder) {
// <1>
return requestConfigBuilder.setSocketTimeout(10000);
}
});
// end::rest-client-init-request-config-callback
}
{
// tag::rest-client-init-client-config-callback
RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200, "http"));
builder.setHttpClientConfigCallback(new HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
return httpClientBuilder.setProxy(// <1>
new HttpHost("proxy", 9000, "http"));
}
});
// end::rest-client-init-client-config-callback
}
{
// tag::rest-client-sync
Request request = new Request(// <1>
"GET", // <2>
"/");
Response response = restClient.performRequest(request);
// end::rest-client-sync
}
{
// tag::rest-client-async
Request request = new Request(// <1>
"GET", // <2>
"/");
Cancellable cancellable = restClient.performRequestAsync(request, new ResponseListener() {
@Override
public void onSuccess(Response response) {
// <3>
}
@Override
public void onFailure(Exception exception) {
// <4>
}
});
// end::rest-client-async
}
{
Request request = new Request("GET", "/");
// tag::rest-client-parameters
request.addParameter("pretty", "true");
// end::rest-client-parameters
// tag::rest-client-body
request.setEntity(new NStringEntity("{\"json\":\"text\"}", ContentType.APPLICATION_JSON));
// end::rest-client-body
// tag::rest-client-body-shorter
request.setJsonEntity("{\"json\":\"text\"}");
// end::rest-client-body-shorter
// tag::rest-client-options-set-singleton
request.setOptions(COMMON_OPTIONS);
// end::rest-client-options-set-singleton
{
// tag::rest-client-options-customize-header
RequestOptions.Builder options = COMMON_OPTIONS.toBuilder();
options.addHeader("cats", "knock things off of other things");
request.setOptions(options);
// end::rest-client-options-customize-header
}
}
{
HttpEntity[] documents = new HttpEntity[10];
// tag::rest-client-async-example
final CountDownLatch latch = new CountDownLatch(documents.length);
for (int i = 0; i < documents.length; i++) {
Request request = new Request("PUT", "/posts/doc/" + i);
// let's assume that the documents are stored in an HttpEntity array
request.setEntity(documents[i]);
restClient.performRequestAsync(request, new ResponseListener() {
@Override
public void onSuccess(Response response) {
// <1>
latch.countDown();
}
@Override
public void onFailure(Exception exception) {
// <2>
latch.countDown();
}
});
}
latch.await();
// end::rest-client-async-example
}
{
// tag::rest-client-async-cancel
Request request = new Request("GET", "/posts/_search");
Cancellable cancellable = restClient.performRequestAsync(request, new ResponseListener() {
@Override
public void onSuccess(Response response) {
// <1>
}
@Override
public void onFailure(Exception exception) {
// <2>
}
});
cancellable.cancel();
// end::rest-client-async-cancel
}
{
// tag::rest-client-response2
Response response = restClient.performRequest(new Request("GET", "/"));
// <1>
RequestLine requestLine = response.getRequestLine();
// <2>
HttpHost host = response.getHost();
// <3>
int statusCode = response.getStatusLine().getStatusCode();
// <4>
Header[] headers = response.getHeaders();
// <5>
String responseBody = EntityUtils.toString(response.getEntity());
// end::rest-client-response2
}
}
use of org.opensearch.client.NodeSelector in project OpenSearch by opensearch-project.
the class ClientYamlTestExecutionContextTests method testHeadersSupportStashedValueReplacement.
public void testHeadersSupportStashedValueReplacement() throws IOException {
final AtomicReference<Map<String, String>> headersRef = new AtomicReference<>();
final Version version = VersionUtils.randomVersion(random());
final ClientYamlTestExecutionContext context = new ClientYamlTestExecutionContext(null, randomBoolean()) {
@Override
ClientYamlTestResponse callApiInternal(String apiName, Map<String, String> params, HttpEntity entity, Map<String, String> headers, NodeSelector nodeSelector) {
headersRef.set(headers);
return null;
}
@Override
public Version esVersion() {
return version;
}
};
final Map<String, String> headers = new HashMap<>();
headers.put("foo", "$bar");
headers.put("foo1", "baz ${c}");
context.stash().stashValue("bar", "foo2");
context.stash().stashValue("c", "bar1");
assertNull(headersRef.get());
context.callApi("test", Collections.emptyMap(), Collections.emptyList(), headers);
assertNotNull(headersRef.get());
assertNotEquals(headers, headersRef.get());
assertEquals("foo2", headersRef.get().get("foo"));
assertEquals("baz bar1", headersRef.get().get("foo1"));
}
Aggregations