Skip to content

Commit

Permalink
[Rename] client/sniffer (#243)
Browse files Browse the repository at this point in the history
Refactor client/sniffer module as part of the Elasticsearch to OpenSearch renaming effort.

Signed-off-by: Rabi Panda <adnapibar@gmail.com>
  • Loading branch information
adnapibar authored and nknize committed Mar 22, 2021
1 parent 1203aa7 commit 223ce87
Show file tree
Hide file tree
Showing 22 changed files with 86 additions and 86 deletions.
16 changes: 8 additions & 8 deletions client/sniffer/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
* specific language governing permissions and limitations
* under the License.
*/
apply plugin: 'elasticsearch.build'
apply plugin: 'elasticsearch.publish'
apply plugin: 'opensearch.build'
apply plugin: 'opensearch.publish'

targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_1_8

group = 'org.elasticsearch.client'
archivesBaseName = 'elasticsearch-rest-client-sniffer'
group = 'org.opensearch.client'
archivesBaseName = 'opensearch-rest-client-sniffer'

dependencies {
api project(":client:rest")
Expand All @@ -36,8 +36,8 @@ dependencies {
testImplementation project(":client:test")
testImplementation "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}"
testImplementation "junit:junit:${versions.junit}"
testImplementation "org.elasticsearch:securemock:${versions.securemock}"
testImplementation "org.elasticsearch:mocksocket:${versions.mocksocket}"
testImplementation "org.opensearch:securemock:${versions.securemock}"
testImplementation "org.opensearch:mocksocket:${versions.mocksocket}"
}

tasks.named('forbiddenApisMain').configure {
Expand All @@ -59,14 +59,14 @@ tasks.named("dependencyLicenses").configure {
}

// JarHell is part of es server, which we don't want to pull in
// TODO: Not anymore. Now in :libs:elasticsearch-core
// TODO: Not anymore. Now in :libs:opensearch-core
jarHell.enabled = false

testingConventions {
naming.clear()
naming {
Tests {
baseClass 'org.elasticsearch.client.RestClientTestCase'
baseClass 'org.opensearch.client.RestClientTestCase'
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package org.elasticsearch.client.sniff;
package org.opensearch.client.sniff;

import org.elasticsearch.client.Node;

Expand All @@ -29,7 +29,7 @@
*/
public interface NodesSniffer {
/**
* Returns the sniffed Elasticsearch nodes.
* Returns the sniffed OpenSearch nodes.
*/
List<Node> sniff() throws IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package org.elasticsearch.client.sniff;
package org.opensearch.client.sniff;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
Expand Down Expand Up @@ -50,12 +50,12 @@
import static java.util.Collections.unmodifiableMap;

/**
* Class responsible for sniffing the http hosts from elasticsearch through the nodes info api and returning them back.
* Class responsible for sniffing the http hosts from opensearch through the nodes info api and returning them back.
* Compatible with elasticsearch 2.x+.
*/
public final class ElasticsearchNodesSniffer implements NodesSniffer {
public final class OpenSearchNodesSniffer implements NodesSniffer {

private static final Log logger = LogFactory.getLog(ElasticsearchNodesSniffer.class);
private static final Log logger = LogFactory.getLog(OpenSearchNodesSniffer.class);

public static final long DEFAULT_SNIFF_REQUEST_TIMEOUT = TimeUnit.SECONDS.toMillis(1);

Expand All @@ -65,29 +65,29 @@ public final class ElasticsearchNodesSniffer implements NodesSniffer {
private final JsonFactory jsonFactory = new JsonFactory();

/**
* Creates a new instance of the Elasticsearch sniffer. It will use the provided {@link RestClient} to fetch the hosts,
* Creates a new instance of the OpenSearch sniffer. It will use the provided {@link RestClient} to fetch the hosts,
* through the nodes info api, the default sniff request timeout value {@link #DEFAULT_SNIFF_REQUEST_TIMEOUT} and http
* as the scheme for all the hosts.
* @param restClient client used to fetch the hosts from elasticsearch through nodes info api. Usually the same instance
* @param restClient client used to fetch the hosts from opensearch through nodes info api. Usually the same instance
* that is also provided to {@link Sniffer#builder(RestClient)}, so that the hosts are set to the same
* client that was used to fetch them.
*/
public ElasticsearchNodesSniffer(RestClient restClient) {
this(restClient, DEFAULT_SNIFF_REQUEST_TIMEOUT, ElasticsearchNodesSniffer.Scheme.HTTP);
public OpenSearchNodesSniffer(RestClient restClient) {
this(restClient, DEFAULT_SNIFF_REQUEST_TIMEOUT, OpenSearchNodesSniffer.Scheme.HTTP);
}

/**
* Creates a new instance of the Elasticsearch sniffer. It will use the provided {@link RestClient} to fetch the hosts
* Creates a new instance of the OpenSearch sniffer. It will use the provided {@link RestClient} to fetch the hosts
* through the nodes info api, the provided sniff request timeout value and scheme.
* @param restClient client used to fetch the hosts from elasticsearch through nodes info api. Usually the same instance
* @param restClient client used to fetch the hosts from opensearch through nodes info api. Usually the same instance
* that is also provided to {@link Sniffer#builder(RestClient)}, so that the hosts are set to the same
* client that was used to sniff them.
* @param sniffRequestTimeoutMillis the sniff request timeout (in milliseconds) to be passed in as a query string parameter
* to elasticsearch. Allows to halt the request without any failure, as only the nodes
* to opensearch. Allows to halt the request without any failure, as only the nodes
* that have responded within this timeout will be returned.
* @param scheme the scheme to associate sniffed nodes with (as it is not returned by elasticsearch)
* @param scheme the scheme to associate sniffed nodes with (as it is not returned by opensearch)
*/
public ElasticsearchNodesSniffer(RestClient restClient, long sniffRequestTimeoutMillis, Scheme scheme) {
public OpenSearchNodesSniffer(RestClient restClient, long sniffRequestTimeoutMillis, Scheme scheme) {
this.restClient = Objects.requireNonNull(restClient, "restClient cannot be null");
if (sniffRequestTimeoutMillis < 0) {
throw new IllegalArgumentException("sniffRequestTimeoutMillis must be greater than 0");
Expand All @@ -98,7 +98,7 @@ public ElasticsearchNodesSniffer(RestClient restClient, long sniffRequestTimeout
}

/**
* Calls the elasticsearch nodes info api, parses the response and returns all the found http hosts
* Calls the opensearch nodes info api, parses the response and returns all the found http hosts
*/
@Override
public List<Node> sniff() throws IOException {
Expand Down Expand Up @@ -138,7 +138,7 @@ private static Node readNode(String nodeId, JsonParser parser, Scheme scheme) th
HttpHost publishedHost = null;
/*
* We sniff the bound hosts so we can look up the node based on any
* address on which it is listening. This is useful in Elasticsearch's
* address on which it is listening. This is useful in OpenSearch's
* test framework where we sometimes publish ipv6 addresses but the
* tests contact the node on ipv4.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package org.elasticsearch.client.sniff;
package org.opensearch.client.sniff;

import org.elasticsearch.client.Node;
import org.elasticsearch.client.RestClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package org.elasticsearch.client.sniff;
package org.opensearch.client.sniff;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
Expand All @@ -42,7 +42,7 @@
import java.util.concurrent.atomic.AtomicReference;

/**
* Class responsible for sniffing nodes from some source (default is elasticsearch itself) and setting them to a provided instance of
* Class responsible for sniffing nodes from some source (default is opensearch itself) and setting them to a provided instance of
* {@link RestClient}. Must be created via {@link SnifferBuilder}, which allows to set all of the different options or rely on defaults.
* A background task fetches the nodes through the {@link NodesSniffer} and sets them to the {@link RestClient} instance.
* It is possible to perform sniffing on failure by creating a {@link SniffOnFailureListener} and providing it as an argument to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package org.elasticsearch.client.sniff;
package org.opensearch.client.sniff;

import org.elasticsearch.client.RestClient;

Expand All @@ -37,7 +37,7 @@ public final class SnifferBuilder {
private NodesSniffer nodesSniffer;

/**
* Creates a new builder instance by providing the {@link RestClient} that will be used to communicate with elasticsearch
* Creates a new builder instance by providing the {@link RestClient} that will be used to communicate with opensearch
*/
SnifferBuilder(RestClient restClient) {
Objects.requireNonNull(restClient, "restClient cannot be null");
Expand Down Expand Up @@ -69,8 +69,8 @@ public SnifferBuilder setSniffAfterFailureDelayMillis(int sniffAfterFailureDelay
}

/**
* Sets the {@link NodesSniffer} to be used to read hosts. A default instance of {@link ElasticsearchNodesSniffer}
* is created when not provided. This method can be used to change the configuration of the {@link ElasticsearchNodesSniffer},
* Sets the {@link NodesSniffer} to be used to read hosts. A default instance of {@link OpenSearchNodesSniffer}
* is created when not provided. This method can be used to change the configuration of the {@link OpenSearchNodesSniffer},
* or to provide a different implementation (e.g. in case hosts need to taken from a different source).
*/
public SnifferBuilder setNodesSniffer(NodesSniffer nodesSniffer) {
Expand All @@ -84,7 +84,7 @@ public SnifferBuilder setNodesSniffer(NodesSniffer nodesSniffer) {
*/
public Sniffer build() {
if (nodesSniffer == null) {
this.nodesSniffer = new ElasticsearchNodesSniffer(restClient);
this.nodesSniffer = new OpenSearchNodesSniffer(restClient);
}
return new Sniffer(restClient, nodesSniffer, sniffIntervalMillis, sniffAfterFailureDelayMillis);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package org.elasticsearch.client.sniff;
package org.opensearch.client.sniff;

import org.apache.http.HttpHost;
import org.elasticsearch.client.Node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package org.elasticsearch.client.sniff;
package org.opensearch.client.sniff;

import com.fasterxml.jackson.core.JsonFactory;
import org.apache.http.HttpEntity;
Expand All @@ -27,7 +27,7 @@
import org.elasticsearch.client.Node;
import org.elasticsearch.client.Node.Roles;
import org.elasticsearch.client.RestClientTestCase;
import org.elasticsearch.client.sniff.ElasticsearchNodesSniffer.Scheme;
import org.opensearch.client.sniff.OpenSearchNodesSniffer.Scheme;

import java.io.IOException;
import java.io.InputStream;
Expand All @@ -46,9 +46,9 @@

/**
* Test parsing the response from the {@code /_nodes/http} API from fixed
* versions of Elasticsearch.
* versions of OpenSearch.
*/
public class ElasticsearchNodesSnifferParseTests extends RestClientTestCase {
public class OpenSearchNodesSnifferParseTests extends RestClientTestCase {

private void checkFile(String file, Node... expected) throws IOException {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(file);
Expand All @@ -57,7 +57,7 @@ private void checkFile(String file, Node... expected) throws IOException {
}
try {
HttpEntity entity = new InputStreamEntity(in, ContentType.APPLICATION_JSON);
List<Node> nodes = ElasticsearchNodesSniffer.readHosts(entity, Scheme.HTTP, new JsonFactory());
List<Node> nodes = OpenSearchNodesSniffer.readHosts(entity, Scheme.HTTP, new JsonFactory());
/*
* Use these assertions because the error messages are nicer
* than hasItems and we know the results are in order because
Expand Down Expand Up @@ -124,7 +124,7 @@ public void testParsingPublishAddressWithPreES7Format() throws IOException {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("es6_nodes_publication_address_format.json");

HttpEntity entity = new InputStreamEntity(in, ContentType.APPLICATION_JSON);
List<Node> nodes = ElasticsearchNodesSniffer.readHosts(entity, Scheme.HTTP, new JsonFactory());
List<Node> nodes = OpenSearchNodesSniffer.readHosts(entity, Scheme.HTTP, new JsonFactory());

assertEquals("127.0.0.1", nodes.get(0).getHost().getHostName());
assertEquals(9200, nodes.get(0).getHost().getPort());
Expand All @@ -135,7 +135,7 @@ public void testParsingPublishAddressWithES7Format() throws IOException {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("es7_nodes_publication_address_format.json");

HttpEntity entity = new InputStreamEntity(in, ContentType.APPLICATION_JSON);
List<Node> nodes = ElasticsearchNodesSniffer.readHosts(entity, Scheme.HTTP, new JsonFactory());
List<Node> nodes = OpenSearchNodesSniffer.readHosts(entity, Scheme.HTTP, new JsonFactory());

assertEquals("elastic.test", nodes.get(0).getHost().getHostName());
assertEquals(9200, nodes.get(0).getHost().getPort());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package org.elasticsearch.client.sniff;
package org.opensearch.client.sniff;

import com.carrotsearch.randomizedtesting.generators.RandomNumbers;
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
Expand Down Expand Up @@ -61,17 +61,17 @@
import static org.junit.Assert.assertThat;
import static org.junit.Assert.fail;

public class ElasticsearchNodesSnifferTests extends RestClientTestCase {
public class OpenSearchNodesSnifferTests extends RestClientTestCase {

private int sniffRequestTimeout;
private ElasticsearchNodesSniffer.Scheme scheme;
private OpenSearchNodesSniffer.Scheme scheme;
private SniffResponse sniffResponse;
private HttpServer httpServer;

@Before
public void startHttpServer() throws IOException {
this.sniffRequestTimeout = RandomNumbers.randomIntBetween(getRandom(), 1000, 10000);
this.scheme = RandomPicks.randomFrom(getRandom(), ElasticsearchNodesSniffer.Scheme.values());
this.scheme = RandomPicks.randomFrom(getRandom(), OpenSearchNodesSniffer.Scheme.values());
if (rarely()) {
this.sniffResponse = SniffResponse.buildFailure();
} else {
Expand All @@ -88,22 +88,22 @@ public void stopHttpServer() throws IOException {

public void testConstructorValidation() throws IOException {
try {
new ElasticsearchNodesSniffer(null, 1, ElasticsearchNodesSniffer.Scheme.HTTP);
new OpenSearchNodesSniffer(null, 1, OpenSearchNodesSniffer.Scheme.HTTP);
fail("should have failed");
} catch(NullPointerException e) {
assertEquals("restClient cannot be null", e.getMessage());
}
HttpHost httpHost = new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort());
try (RestClient restClient = RestClient.builder(httpHost).build()) {
try {
new ElasticsearchNodesSniffer(restClient, 1, null);
new OpenSearchNodesSniffer(restClient, 1, null);
fail("should have failed");
} catch (NullPointerException e) {
assertEquals(e.getMessage(), "scheme cannot be null");
}
try {
new ElasticsearchNodesSniffer(restClient, RandomNumbers.randomIntBetween(getRandom(), Integer.MIN_VALUE, 0),
ElasticsearchNodesSniffer.Scheme.HTTP);
new OpenSearchNodesSniffer(restClient, RandomNumbers.randomIntBetween(getRandom(), Integer.MIN_VALUE, 0),
OpenSearchNodesSniffer.Scheme.HTTP);
fail("should have failed");
} catch (IllegalArgumentException e) {
assertEquals(e.getMessage(), "sniffRequestTimeoutMillis must be greater than 0");
Expand All @@ -114,7 +114,7 @@ public void testConstructorValidation() throws IOException {
public void testSniffNodes() throws IOException {
HttpHost httpHost = new HttpHost(httpServer.getAddress().getHostString(), httpServer.getAddress().getPort());
try (RestClient restClient = RestClient.builder(httpHost).build()) {
ElasticsearchNodesSniffer sniffer = new ElasticsearchNodesSniffer(restClient, sniffRequestTimeout, scheme);
OpenSearchNodesSniffer sniffer = new OpenSearchNodesSniffer(restClient, sniffRequestTimeout, scheme);
try {
List<Node> sniffedNodes = sniffer.sniff();
if (sniffResponse.isFailure) {
Expand Down Expand Up @@ -171,15 +171,15 @@ public void handle(HttpExchange httpExchange) throws IOException {
}
}

private static SniffResponse buildSniffResponse(ElasticsearchNodesSniffer.Scheme scheme) throws IOException {
private static SniffResponse buildSniffResponse(OpenSearchNodesSniffer.Scheme scheme) throws IOException {
int numNodes = RandomNumbers.randomIntBetween(getRandom(), 1, 5);
List<Node> nodes = new ArrayList<>(numNodes);
JsonFactory jsonFactory = new JsonFactory();
StringWriter writer = new StringWriter();
JsonGenerator generator = jsonFactory.createGenerator(writer);
generator.writeStartObject();
if (getRandom().nextBoolean()) {
generator.writeStringField("cluster_name", "elasticsearch");
generator.writeStringField("cluster_name", "opensearch");
}
if (getRandom().nextBoolean()) {
generator.writeObjectFieldStart("bogus_object");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package org.elasticsearch.client.sniff;
package org.opensearch.client.sniff;

import org.apache.http.HttpHost;
import org.elasticsearch.client.Node;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

package org.elasticsearch.client.sniff;
package org.opensearch.client.sniff;

import com.carrotsearch.randomizedtesting.generators.RandomNumbers;
import org.apache.http.HttpHost;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
* under the License.
*/

package org.elasticsearch.client.sniff;
package org.opensearch.client.sniff;

import org.apache.http.HttpHost;
import org.elasticsearch.client.Node;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientTestCase;
import org.elasticsearch.client.sniff.Sniffer.DefaultScheduler;
import org.elasticsearch.client.sniff.Sniffer.Scheduler;
import org.opensearch.client.sniff.Sniffer.DefaultScheduler;
import org.opensearch.client.sniff.Sniffer.Scheduler;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

Expand Down
Loading

0 comments on commit 223ce87

Please sign in to comment.