forked from elastic/elasticsearch
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
REST Client: NodeSelector for node attributes
Add a `NodeSelector` so that users can filter the nodes that receive requests based on node attributes. I believe we'll need this to backport elastic#30523 and we want it anyway. I also added a bash script to help with rebuilding the sniffer parsing test documents.
- Loading branch information
Showing
18 changed files
with
1,081 additions
and
468 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
client/rest/src/main/java/org/elasticsearch/client/HasAttributeNodeSelector.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.client; | ||
|
||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
/** | ||
* A {@link NodeSelector} that selects nodes that have a particular value | ||
* for an attribute. | ||
*/ | ||
public final class HasAttributeNodeSelector implements NodeSelector { | ||
private final String key; | ||
private final String value; | ||
|
||
public HasAttributeNodeSelector(String key, String value) { | ||
this.key = key; | ||
this.value = value; | ||
} | ||
|
||
@Override | ||
public void select(Iterable<Node> nodes) { | ||
Iterator<Node> itr = nodes.iterator(); | ||
while (itr.hasNext()) { | ||
Map<String, List<String>> allAttributes = itr.next().getAttributes(); | ||
if (allAttributes == null) continue; | ||
List<String> values = allAttributes.get(key); | ||
if (values == null || false == values.contains(value)) { | ||
itr.remove(); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return key + "=" + value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
client/rest/src/test/java/org/elasticsearch/client/HasAttributeNodeSelectorTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Licensed to Elasticsearch under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package org.elasticsearch.client; | ||
|
||
import org.apache.http.HttpHost; | ||
import org.elasticsearch.client.Node.Roles; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import static java.util.Collections.singletonList; | ||
import static java.util.Collections.singletonMap; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
public class HasAttributeNodeSelectorTests extends RestClientTestCase { | ||
public void testHasAttribute() { | ||
Node hasAttributeValue = dummyNode(singletonMap("attr", singletonList("val"))); | ||
Node hasAttributeButNotValue = dummyNode(singletonMap("attr", singletonList("notval"))); | ||
Node hasAttributeValueInList = dummyNode(singletonMap("attr", Arrays.asList("val", "notval"))); | ||
Node notHasAttribute = dummyNode(singletonMap("notattr", singletonList("val"))); | ||
List<Node> nodes = new ArrayList<>(); | ||
nodes.add(hasAttributeValue); | ||
nodes.add(hasAttributeButNotValue); | ||
nodes.add(hasAttributeValueInList); | ||
nodes.add(notHasAttribute); | ||
List<Node> expected = new ArrayList<>(); | ||
expected.add(hasAttributeValue); | ||
expected.add(hasAttributeValueInList); | ||
new HasAttributeNodeSelector("attr", "val").select(nodes); | ||
assertEquals(expected, nodes); | ||
} | ||
|
||
private Node dummyNode(Map<String, List<String>> attributes) { | ||
return new Node(new HttpHost("dummy"), Collections.<HttpHost>emptySet(), | ||
randomAsciiAlphanumOfLength(5), randomAsciiAlphanumOfLength(5), | ||
new Roles(randomBoolean(), randomBoolean(), randomBoolean()), | ||
attributes); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.