diff --git a/src/main/java/org/apache/cassandra/gms/ApplicationState.java b/src/main/java/org/apache/cassandra/gms/ApplicationState.java new file mode 100644 index 0000000..31958cf --- /dev/null +++ b/src/main/java/org/apache/cassandra/gms/ApplicationState.java @@ -0,0 +1,54 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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. + */ +/* + * Copyright (C) 2015 ScyllaDB + */ +/* + * Moddified by ScyllaDB + */ + +package org.apache.cassandra.gms; + +public enum ApplicationState +{ + STATUS, + LOAD, + SCHEMA, + DC, + RACK, + RELEASE_VERSION, + REMOVAL_COORDINATOR, + INTERNAL_IP, + RPC_ADDRESS, + X_11_PADDING, // padding specifically for 1.1 + SEVERITY, + NET_VERSION, + HOST_ID, + TOKENS, + // pad to allow adding new states to existing cluster + X1, + X2, + X3, + X4, + X5, + X6, + X7, + X8, + X9, + X10, +} diff --git a/src/main/java/org/apache/cassandra/gms/EndpointState.java b/src/main/java/org/apache/cassandra/gms/EndpointState.java new file mode 100644 index 0000000..c786d94 --- /dev/null +++ b/src/main/java/org/apache/cassandra/gms/EndpointState.java @@ -0,0 +1,103 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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. + */ +/* + * Copyright (C) 2015 ScyllaDB + */ +/* + * Moddified by ScyllaDB + */ +package org.apache.cassandra.gms; + +import java.util.HashMap; +import java.util.Map; + +/** + * This abstraction represents both the HeartBeatState and the ApplicationState + * in an EndpointState instance. Any state for a given endpoint can be retrieved + * from this instance. + */ + +public class EndpointState { + private volatile HeartBeatState hbState; + + final Map applicationState = new HashMap(); + + private volatile long updateTimestamp; + private volatile boolean isAlive; + ApplicationState[] applicationValues; + + EndpointState(HeartBeatState initialHbState) { + applicationValues = ApplicationState.values(); + hbState = initialHbState; + updateTimestamp = System.nanoTime(); + isAlive = true; + } + + HeartBeatState getHeartBeatState() { + return hbState; + } + + void setHeartBeatState(HeartBeatState newHbState) { + hbState = newHbState; + } + + public String getApplicationState(ApplicationState key) { + return applicationState.get(key); + } + + /** + * TODO replace this with operations that don't expose private state + */ + @Deprecated + public Map getApplicationStateMap() { + return applicationState; + } + + void addApplicationState(ApplicationState key, String value) { + applicationState.put(key, value); + } + + void addApplicationState(int key, String value) { + addApplicationState(applicationValues[key], value); + } + + /* getters and setters */ + /** + * @return System.nanoTime() when state was updated last time. + */ + public long getUpdateTimestamp() { + return updateTimestamp; + } + + public void setUpdateTimestamp(long ts) { + updateTimestamp = ts; + } + + public boolean isAlive() { + return isAlive; + } + + public void setAliave(boolean alive) { + isAlive = alive; + } + + public String toString() { + return "EndpointState: HeartBeatState = " + hbState + ", AppStateMap = " + + applicationState; + } +} diff --git a/src/main/java/org/apache/cassandra/gms/FailureDetector.java b/src/main/java/org/apache/cassandra/gms/FailureDetector.java index c08c2d2..2f1194b 100644 --- a/src/main/java/org/apache/cassandra/gms/FailureDetector.java +++ b/src/main/java/org/apache/cassandra/gms/FailureDetector.java @@ -27,6 +27,9 @@ package org.apache.cassandra.gms; import java.lang.management.ManagementFactory; import java.net.UnknownHostException; import java.util.*; + +import javax.json.JsonArray; +import javax.json.JsonObject; import javax.management.MBeanServer; import javax.management.ObjectName; @@ -64,7 +67,7 @@ public class FailureDetector implements FailureDetectorMBean { } public void setPhiConvictThreshold(double phi) { - log(" setPhiConvictThreshold(double phi)"); + log(" setPhiConvictThreshold(double phi)"); } public double getPhiConvictThreshold() { @@ -74,7 +77,44 @@ public class FailureDetector implements FailureDetectorMBean { public String getAllEndpointStates() { log(" getAllEndpointStates()"); - return c.getStringValue("/failure_detector/endpoints"); + + StringBuilder sb = new StringBuilder(); + for (Map.Entry entry : getEndpointStateMap().entrySet()) + { + sb.append(entry.getKey()).append("\n"); + appendEndpointState(sb, entry.getValue()); + } + return sb.toString(); + } + + private void appendEndpointState(StringBuilder sb, EndpointState endpointState) + { + sb.append(" generation:").append(endpointState.getHeartBeatState().getGeneration()).append("\n"); + sb.append(" heartbeat:").append(endpointState.getHeartBeatState().getHeartBeatVersion()).append("\n"); + for (Map.Entry state : endpointState.applicationState.entrySet()) + { + if (state.getKey() == ApplicationState.TOKENS) + continue; + sb.append(" ").append(state.getKey()).append(":").append(state.getValue()).append("\n"); + } + } + + public Map getEndpointStateMap() { + Map res = new HashMap(); + JsonArray arr = c.getJsonArray("/failure_detector/endpoints"); + for (int i = 0; i < arr.size(); i++) { + JsonObject obj = arr.getJsonObject(i); + EndpointState ep = new EndpointState(new HeartBeatState(obj.getInt("generation"), obj.getInt("version"))); + ep.setAliave(obj.getBoolean("is_alive")); + ep.setUpdateTimestamp(obj.getJsonNumber("update_time").longValue()); + JsonArray states = obj.getJsonArray("application_state"); + for (int j = 0; j < states.size(); j++) { + JsonObject state = states.getJsonObject(j); + ep.addApplicationState(state.getInt("application_state"), state.getString("value")); + } + res.put(obj.getString("addrs"), ep); + } + return res; } public String getEndpointState(String address) throws UnknownHostException { diff --git a/src/main/java/org/apache/cassandra/gms/HeartBeatState.java b/src/main/java/org/apache/cassandra/gms/HeartBeatState.java new file mode 100644 index 0000000..0af0ef6 --- /dev/null +++ b/src/main/java/org/apache/cassandra/gms/HeartBeatState.java @@ -0,0 +1,65 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF 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. + */ +/* + * Copyright (C) 2015 ScyllaDB + */ +/* + * Moddified by ScyllaDB + */ +package org.apache.cassandra.gms; + +/** + * HeartBeat State associated with any given endpoint. + */ +class HeartBeatState { + private int generation; + private int version; + + HeartBeatState(int gen) { + this(gen, 0); + } + + HeartBeatState(int gen, int ver) { + generation = gen; + version = ver; + } + + int getGeneration() { + return generation; + } + + void updateHeartBeat() { + } + + int getHeartBeatVersion() { + return version; + } + + void forceNewerGenerationUnsafe() { + generation += 1; + } + + void forceHighestPossibleVersionUnsafe() { + version = Integer.MAX_VALUE; + } + + public String toString() { + return String.format("HeartBeat: generation = %d, version = %d", + generation, version); + } +}