blob: 2f70c37cd48e7c4357d9c7ed65dbc879d19a4fe8 (
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
86
87
88
89
90
91
|
// 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 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 group) {
this.key = key;
this.hostname = hostname;
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; }
/** 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);
if ( ! working ) {
activeDocuments.set(0);
}
}
/** 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 */
void setActiveDocuments(long activeDocuments) {
this.activeDocuments.set(activeDocuments);
}
/** Returns the active documents on this node. If unknown, 0 is returned. */
long getActiveDocuments() {
return activeDocuments.get();
}
@Override
public int hashCode() { return Objects.hash(hostname, key, pathIndex, group); }
@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.key, other.key)) return false;
if ( ! Objects.equals(this.pathIndex, other.pathIndex)) return false;
if ( ! Objects.equals(this.group, other.group)) return false;
return true;
}
@Override
public String toString() {
return "search node key = " + key + " hostname = "+ hostname + " path = " + pathIndex + " in group " + group +
" statusIsKnown = " + statusIsKnown.get() + " working = " + working.get() +
" activeDocs = " + activeDocuments.get();
}
}
|