Elasticsearch RestClientのNodeSelectorについて

7506 ワード

シーケンス
本稿では主にElasticsearch RestClientのNodeSelectorについて検討する
NodeSelector
elasticsearch-7.0.1/client/rest/src/main/java/org/elasticsearch/client/NodeSelector.java
public interface NodeSelector {
    /**
     * Select the {@link Node}s to which to send requests. This is called with
     * a mutable {@link Iterable} of {@linkplain Node}s in the order that the
     * rest client would prefer to use them and implementers should remove
     * nodes from the that should not receive the request. Implementers may
     * iterate the nodes as many times as they need.
     * 

* This may be called twice per request: first for "living" nodes that * have not been blacklisted by previous errors. If the selector removes * all nodes from the list or if there aren't any living nodes then the * {@link RestClient} will call this method with a list of "dead" nodes. *

* Implementers should not rely on the ordering of the nodes. */ void select(Iterable nodes); /* * We were fairly careful with our choice of Iterable here. The caller has * a List but reordering the list is likely to break round robin. Luckily * Iterable doesn't allow any reordering. */ /** * Selector that matches any node. */ NodeSelector ANY = new NodeSelector() { @Override public void select(Iterable nodes) { // Intentionally does nothing } @Override public String toString() { return "ANY"; } }; /** * Selector that matches any node that has metadata and doesn't * have the {@code master} role OR it has the data {@code data} * role. */ NodeSelector SKIP_DEDICATED_MASTERS = new NodeSelector() { @Override public void select(Iterable nodes) { for (Iterator itr = nodes.iterator(); itr.hasNext();) { Node node = itr.next(); if (node.getRoles() == null) continue; if (node.getRoles().isMasterEligible() && false == node.getRoles().isData() && false == node.getRoles().isIngest()) { itr.remove(); } } } @Override public String toString() { return "SKIP_DEDICATED_MASTERS"; } }; }

  • NodeSelectorインタフェースはselectメソッドを定義し、mutableのIterableを受信し、具体的な実装はnodeを選択的に削除する.ANY、SKIP_を定義していますDEDICATED_MASTERSの2つの匿名実装クラスは、ANYのselectメソッドが何もしない.SKIP_DEDICATED_MASTERSのselectメソッドは、dateがfalse、ingestがfalseのmaster nodeを削除します.他にもHasAttributeNodeSelector、P e r f e r H a s AttributeNodeSelectorの2つの実装クラス
  • がある.
    HasAttributeNodeSelector
    elasticsearch-7.0.1/client/rest/src/main/java/org/elasticsearch/client/HasAttributeNodeSelector.java
    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 nodes) {
            Iterator itr = nodes.iterator();
            while (itr.hasNext()) {
                Map> allAttributes = itr.next().getAttributes();
                if (allAttributes == null) continue;
                List values = allAttributes.get(key);
                if (values == null || false == values.contains(value)) {
                    itr.remove();
                }
            }
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            HasAttributeNodeSelector that = (HasAttributeNodeSelector) o;
            return Objects.equals(key, that.key) &&
                    Objects.equals(value, that.value);
        }
    
        @Override
        public int hashCode() {
            return Objects.hash(key, value);
        }
    
        @Override
        public String toString() {
            return key + "=" + value;
        }
    }
  • HasAttributeNodeSelectorはNodeSelectorインタフェースを実現し、そのコンストラクタはkey、valueパラメータを受信し、そのselectメソッドはnodeのattributesがこのkeyまたはそのkeyに対応するvalueがnullのnode
  • であることを削除する.
    PreferHasAttributeNodeSelector
    elasticsearch-7.0.1/client/rest/src/main/java/org/elasticsearch/client/PreferHasAttributeNodeSelector.java
    public final class PreferHasAttributeNodeSelector implements NodeSelector {
        private final String key;
        private final String value;
    
        public PreferHasAttributeNodeSelector(String key, String value) {
            this.key = key;
            this.value = value;
        }
    
        @Override
        public void select(Iterable nodes) {
            boolean foundAtLeastOne = false;
    
            for (Node node : nodes) {
                Map> attributes = node.getAttributes();
    
                if (attributes == null) {
                    continue;
                }
    
                List values = attributes.get(key);
    
                if (values == null) {
                    continue;
                }
    
                if (values.contains(value)) {
                    foundAtLeastOne = true;
                    break;
                }
            }
    
            if (foundAtLeastOne) {
                Iterator nodeIterator = nodes.iterator();
                while (nodeIterator.hasNext()) {
                    Map> attributes = nodeIterator.next().getAttributes();
    
                    if (attributes == null) {
                        continue;
                    }
    
                    List values = attributes.get(key);
    
                    if (values == null || !values.contains(value)) {
                        nodeIterator.remove();
                    }
                }
            }
        }
    
        @Override
        public boolean equals(Object o) {
            if (this == o) {
                return true;
            }
            if (o == null || getClass() != o.getClass()) {
                return false;
            }
            PreferHasAttributeNodeSelector that = (PreferHasAttributeNodeSelector) o;
            return Objects.equals(key, that.key) &&
                Objects.equals(value, that.value);
        }
    
        @Override
        public int hashCode() {
            return Objects.hash(key, value);
        }
    
        @Override
        public String toString() {
            return key + "=" + value;
        }
    }
  • PreferHasAttributeNodeSelectorはNodeSelectorインタフェースを実現し、そのコンストラクタはkey、valueパラメータを受信し、そのselectメソッドはまずnodesを巡ってfoundAtLeastOneを設定し、foundAtLeastOneがtrueである場合、nodeのattributesはそのkeyがないか、またはそのkeyに対応するvalueがnullのnode
  • を削除する.
    小結
  • NodeSelectorインタフェースはselectメソッドを定義し、mutableのIterableを受信し、具体的な実装はnodeを選択的に削除する.ANY、SKIP_を定義していますDEDICATED_MASTERSの2つの匿名実装クラスは、ANYのselectメソッドが何もしない.SKIP_DEDICATED_MASTERSのselectメソッドは、dateがfalse、ingestがfalseのmaster node
  • を削除します.
  • HasAttributeNodeSelectorはNodeSelectorインタフェースを実現し、そのコンストラクタはkey、valueパラメータを受信し、そのselectメソッドはnodeのattributesがこのkeyまたはそのkeyに対応するvalueがnullのnode
  • であることを削除する.
  • PreferHasAttributeNodeSelectorはNodeSelectorインタフェースを実現し、そのコンストラクタはkey、valueパラメータを受信し、そのselectメソッドはまずnodesを巡ってfoundAtLeastOneを設定し、foundAtLeastOneがtrueである場合、nodeのattributesはそのkeyがないか、またはそのkeyに対応するvalueがnullのnode
  • を削除する.
    doc
  • Node selector