/** * 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. */ package org.apache.cassandra.gms; import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.util.*; import org.apache.cassandra.io.ICompactSerializer; import org.apache.cassandra.service.StorageService; import org.apache.log4j.Logger; /** * 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 static ICompactSerializer<EndPointState> serializer_; static { serializer_ = new EndPointStateSerializer(); } HeartBeatState hbState_; Map<String, ApplicationState> applicationState_ = new Hashtable<String, ApplicationState>(); /* fields below do not get serialized */ long updateTimestamp_; boolean isAlive_; boolean isAGossiper_; // whether this endpoint has token associated with it or not. Initially set false for all // endpoints. After certain time of inactivity, gossiper will examine if this node has a // token or not and will set this true if token is found. If there is no token, this is a // fat client and will be removed automatically from gossip. boolean hasToken_; public static ICompactSerializer<EndPointState> serializer() { return serializer_; } EndPointState(HeartBeatState hbState) { hbState_ = hbState; updateTimestamp_ = System.currentTimeMillis(); isAlive_ = true; isAGossiper_ = false; hasToken_ = false; } HeartBeatState getHeartBeatState() { return hbState_; } synchronized void setHeartBeatState(HeartBeatState hbState) { updateTimestamp(); hbState_ = hbState; } public ApplicationState getApplicationState(String key) { return applicationState_.get(key); } public Map<String, ApplicationState> getApplicationStateMap() { return applicationState_; } void addApplicationState(String key, ApplicationState appState) { applicationState_.put(key, appState); } /* getters and setters */ long getUpdateTimestamp() { return updateTimestamp_; } synchronized void updateTimestamp() { updateTimestamp_ = System.currentTimeMillis(); } public boolean isAlive() { return isAlive_; } synchronized void isAlive(boolean value) { isAlive_ = value; } boolean isAGossiper() { return isAGossiper_; } synchronized void isAGossiper(boolean value) { //isAlive_ = false; isAGossiper_ = value; } public synchronized void setHasToken(boolean value) { hasToken_ = value; } public boolean getHasToken() { return hasToken_; } public List<Map.Entry<String,ApplicationState>> getSortedApplicationStates() { ArrayList<Map.Entry<String, ApplicationState>> entries = new ArrayList<Map.Entry<String, ApplicationState>>(); entries.addAll(applicationState_.entrySet()); Collections.sort(entries, new Comparator<Map.Entry<String, ApplicationState>>() { public int compare(Map.Entry<String, ApplicationState> lhs, Map.Entry<String, ApplicationState> rhs) { return lhs.getValue().compareTo(rhs.getValue()); } }); return entries; } } class EndPointStateSerializer implements ICompactSerializer<EndPointState> { private static Logger logger_ = Logger.getLogger(EndPointStateSerializer.class); public void serialize(EndPointState epState, DataOutputStream dos) throws IOException { /* serialize the HeartBeatState */ HeartBeatState hbState = epState.getHeartBeatState(); HeartBeatState.serializer().serialize(hbState, dos); /* serialize the map of ApplicationState objects */ int size = epState.applicationState_.size(); dos.writeInt(size); for (String key : epState.applicationState_.keySet()) { ApplicationState appState = epState.applicationState_.get(key); if (appState != null) { dos.writeUTF(key); ApplicationState.serializer().serialize(appState, dos); } } } public EndPointState deserialize(DataInputStream dis) throws IOException { HeartBeatState hbState = HeartBeatState.serializer().deserialize(dis); EndPointState epState = new EndPointState(hbState); int appStateSize = dis.readInt(); for ( int i = 0; i < appStateSize; ++i ) { if ( dis.available() == 0 ) { break; } String key = dis.readUTF(); ApplicationState appState = ApplicationState.serializer().deserialize(dis); epState.addApplicationState(key, appState); } return epState; } }