use of com.yahoo.vdslib.state.Node in project vespa by vespa-engine.
the class DatabaseHandler method loadWantedStates.
public boolean loadWantedStates(Context context) throws InterruptedException {
log.log(LogLevel.DEBUG, "Fleetcontroller " + nodeIndex + ": Retrieving node wanted states.");
synchronized (databaseMonitor) {
if (database != null && !database.isClosed()) {
currentlyStored.wantedStates = database.retrieveWantedStates();
}
}
Map<Node, NodeState> wantedStates = currentlyStored.wantedStates;
if (wantedStates == null) {
if (usingZooKeeper()) {
log.log(LogLevel.WARNING, "Fleetcontroller " + nodeIndex + ": Failed to retrieve wanted states from ZooKeeper. Assuming UP for all nodes.");
}
wantedStates = new TreeMap<>();
}
boolean altered = false;
for (Node node : wantedStates.keySet()) {
NodeInfo nodeInfo = context.getCluster().getNodeInfo(node);
// ignore wanted state of nodes which doesn't exist
if (nodeInfo == null)
continue;
NodeState wantedState = wantedStates.get(node);
if (!nodeInfo.getUserWantedState().equals(wantedState)) {
nodeInfo.setWantedState(wantedState);
context.getNodeStateUpdateListener().handleNewWantedNodeState(nodeInfo, wantedState);
altered = true;
}
log.log(LogLevel.DEBUG, "Fleetcontroller " + nodeIndex + ": Node " + node + " has wanted state " + wantedState);
}
// Remove wanted state from any node having a wanted state set that is no longer valid
for (NodeInfo info : context.getCluster().getNodeInfo()) {
NodeState wantedState = wantedStates.get(info.getNode());
if (wantedState == null && !info.getUserWantedState().equals(new NodeState(info.getNode().getType(), State.UP))) {
info.setWantedState(null);
context.getNodeStateUpdateListener().handleNewWantedNodeState(info, info.getWantedState().clone());
altered = true;
}
}
return altered;
}
use of com.yahoo.vdslib.state.Node in project vespa by vespa-engine.
the class DatabaseHandler method saveWantedStates.
public void saveWantedStates(Context context) throws InterruptedException {
log.log(LogLevel.DEBUG, "Fleetcontroller " + nodeIndex + ": Checking whether wanted states have changed compared to zookeeper version.");
Map<Node, NodeState> wantedStates = new TreeMap<>();
for (NodeInfo info : context.getCluster().getNodeInfo()) {
if (!info.getUserWantedState().equals(new NodeState(info.getNode().getType(), State.UP))) {
wantedStates.put(info.getNode(), info.getUserWantedState());
}
}
// - The value is different from the value we know is stored.
if (pendingStore.wantedStates != null || currentlyStored.wantedStates == null || !currentlyStored.wantedStates.equals(wantedStates)) {
log.log(LogLevel.DEBUG, "Fleetcontroller " + nodeIndex + ": Scheduling new wanted states to be stored into zookeeper.");
pendingStore.wantedStates = wantedStates;
doNextZooKeeperTask(context);
}
}
use of com.yahoo.vdslib.state.Node in project vespa by vespa-engine.
the class ZooKeeperDatabase method retrieveStartTimestamps.
@Override
public Map<Node, Long> retrieveStartTimestamps() throws InterruptedException {
try {
log.log(LogLevel.DEBUG, "Fleetcontroller " + nodeIndex + ": Fetching start timestamps at '" + zooKeeperRoot + "starttimestamps'");
Stat stat = new Stat();
byte[] data = session.getData(zooKeeperRoot + "starttimestamps", false, stat);
Map<Node, Long> wanted = new TreeMap<Node, Long>();
if (data != null && data.length > 0) {
StringTokenizer st = new StringTokenizer(new String(data, utf8), "\n", false);
while (st.hasMoreTokens()) {
String token = st.nextToken();
int colon = token.indexOf(':');
try {
if (colon < 0)
throw new Exception();
Node n = new Node(token.substring(0, colon));
Long timestamp = Long.valueOf(token.substring(colon + 1));
wanted.put(n, timestamp);
} catch (Exception e) {
log.log(LogLevel.WARNING, "Fleetcontroller " + nodeIndex + ": Ignoring invalid starttimestamp line in zookeeper '" + token + "'.");
}
}
}
return wanted;
} catch (InterruptedException e) {
throw (InterruptedException) new InterruptedException("Interrupted").initCause(e);
} catch (Exception e) {
if (sessionOpen && reportErrors) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
log.log(LogLevel.WARNING, "Fleetcontroller " + nodeIndex + ": Failed to retrieve start timestamps from zookeeper: " + e.getMessage() + "\n" + sw);
}
return null;
}
}
use of com.yahoo.vdslib.state.Node in project vespa by vespa-engine.
the class ZooKeeperDatabase method retrieveWantedStates.
public Map<Node, NodeState> retrieveWantedStates() throws InterruptedException {
try {
log.log(LogLevel.DEBUG, "Fleetcontroller " + nodeIndex + ": Fetching wanted states at '" + zooKeeperRoot + "wantedstates'");
Stat stat = new Stat();
byte[] data = session.getData(zooKeeperRoot + "wantedstates", false, stat);
Map<Node, NodeState> wanted = new TreeMap<>();
if (data != null && data.length > 0) {
StringTokenizer st = new StringTokenizer(new String(data, utf8), "\n", false);
while (st.hasMoreTokens()) {
String token = st.nextToken();
int colon = token.indexOf(':');
try {
if (colon < 0)
throw new Exception();
Node node = new Node(token.substring(0, colon));
NodeState nodeState = NodeState.deserialize(node.getType(), token.substring(colon + 1));
wanted.put(node, nodeState);
} catch (Exception e) {
log.log(LogLevel.WARNING, "Fleetcontroller " + nodeIndex + ": Ignoring invalid wantedstate line in zookeeper '" + token + "'.");
}
}
}
return wanted;
} catch (InterruptedException e) {
throw (InterruptedException) new InterruptedException("Interrupted").initCause(e);
} catch (Exception e) {
if (sessionOpen && reportErrors) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
log.log(LogLevel.WARNING, "Fleetcontroller " + nodeIndex + ": Failed to retrieve wanted states from zookeeper: " + e.getMessage() + "\n" + sw);
}
return null;
}
}
use of com.yahoo.vdslib.state.Node in project vespa by vespa-engine.
the class ZooKeeperDatabase method storeWantedStates.
public boolean storeWantedStates(Map<Node, NodeState> states) throws InterruptedException {
if (states == null)
states = new TreeMap<>();
StringBuilder sb = new StringBuilder();
for (Node node : states.keySet()) {
NodeState nodeState = states.get(node);
if (!nodeState.equals(new NodeState(node.getType(), State.UP))) {
NodeState toStore = new NodeState(node.getType(), nodeState.getState());
toStore.setDescription(nodeState.getDescription());
if (!toStore.equals(nodeState)) {
log.warning("Attempted to store wanted state with more than just a main state. Extra data stripped. Original data '" + nodeState.serialize(true));
}
sb.append(node.toString()).append(':').append(toStore.serialize(true)).append('\n');
}
}
byte[] val = sb.toString().getBytes(utf8);
try {
log.log(LogLevel.DEBUG, "Fleetcontroller " + nodeIndex + ": Storing wanted states at '" + zooKeeperRoot + "wantedstates'");
session.setData(zooKeeperRoot + "wantedstates", val, -1);
return true;
} catch (InterruptedException e) {
throw (InterruptedException) new InterruptedException("Interrupted").initCause(e);
} catch (Exception e) {
if (sessionOpen && reportErrors) {
StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw));
log.log(LogLevel.WARNING, "Fleetcontroller " + nodeIndex + ": Failed to store wanted states in zookeeper: " + e.getMessage() + "\n" + sw);
}
return false;
}
}
Aggregations