summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/dispatch/searchcluster/Node.java
blob: b47f2fefa5bde31e9282ad7f4a12bd280a552c6e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.dispatch.searchcluster;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicLong;

/**
 * A node in a search cluster. This class is multithread safe.
 *
 * @author bratseth
 * @author ollivir
 */
public class Node {

    private final int key;
    private int pathIndex;
    private final String hostname;
    private final int fs4port;
    final int group;

    private final AtomicBoolean statusIsKnown = new AtomicBoolean(false);
    private final AtomicBoolean working = new AtomicBoolean(true);
    private final AtomicLong activeDocuments = new AtomicLong(0);

    public Node(int key, String hostname, int fs4port, int group) {
        this.key = key;
        this.hostname = hostname;
        this.fs4port = fs4port;
        this.group = group;
    }

    /** Returns the unique and stable distribution key of this node */
    public int key() { return key; }

    public int pathIndex() { return pathIndex; }

    void setPathIndex(int index) {
        pathIndex = index;
    }

    public String hostname() { return hostname; }

    public int fs4port() { return fs4port; }

    /** Returns the id of this group this node belongs to */
    public int group() { return group; }

    public void setWorking(boolean working) {
        this.statusIsKnown.lazySet(true);
        this.working.lazySet(working);
    }

    /** Returns whether this node is currently responding to requests, or null if status is not known */
    public Boolean isWorking() {
        return statusIsKnown.get() ? working.get() : null;
    }

    /** Updates the active documents on this node */
    public void setActiveDocuments(long activeDocuments) {
        this.activeDocuments.set(activeDocuments);
    }

    /** Returns the active documents on this node. If unknown, 0 is returned. */
    public long getActiveDocuments() {
        return this.activeDocuments.get();
    }

    @Override
    public int hashCode() { return Objects.hash(hostname, fs4port); }

    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if ( ! (o instanceof Node)) return false;
        Node other = (Node)o;
        if ( ! Objects.equals(this.hostname, other.hostname)) return false;
        if ( ! Objects.equals(this.fs4port, other.fs4port)) return false;
        return true;
    }

    @Override
    public String toString() { return "search node " + hostname + ":" + fs4port + " in group " + group; }

}