use of com.creditease.uav.cache.api.CacheManager in project uavstack by uavorg.
the class MonitorDataAdapter method prepareInsertObj.
@SuppressWarnings({ "unchecked", "rawtypes" })
@Override
public Object prepareInsertObj(DataStoreMsg msg, DataStoreConnection connection) {
boolean isDataStoreOK = (this.dataSource.getAvalibleAddressEntry() == null) ? false : true;
// OpenTSDB points list
List<Map<String, ?>> points = new ArrayList<Map<String, ?>>();
CacheManager cm = (CacheManager) ConfigurationManager.getInstance().getComponent("healthmanager", "HMCacheManager");
int cacheTime = DataConvertHelper.toInt(ConfigurationManager.getInstance().getFeatureConfiguration("healthmanager", "MT_Monitor.ds.cachetime"), 1);
/**
* 消息message传输过来的MDF是一个列表包含多个MDF的json字符串
*/
// parse MDF list
String monitorDataFramesStr = (String) msg.get(MonitorDataFrame.MessageType.Monitor.toString());
List<String> monitorDataFrames = JSONHelper.toObjectArray(monitorDataFramesStr, String.class);
cm.beginBatch();
for (String mdfStr : monitorDataFrames) {
// 反序列化为MonitorDataFrame
MonitorDataFrame mdf = new MonitorDataFrame(mdfStr);
// get all frames
Map<String, List<Map>> frames = mdf.getDatas();
for (String monitorId : frames.keySet()) {
List<Map> monitorDatas = frames.get(monitorId);
for (Map monitorData : monitorDatas) {
String meId = (String) monitorData.get("MEId");
List<Map> instances = (List<Map>) monitorData.get("Instances");
for (Map instance : instances) {
// get instance id
String instanceId = (String) instance.get("id");
// get instance fields
Map<String, Object> fields = (Map<String, Object>) instance.get("values");
/**
* Step 1: put the inst's metric data into cache
*/
String encodedInstId = DataStoreHelper.encodeForOpenTSDB(instanceId);
String mdCacheKey = getMDCacheKey(mdf, encodedInstId, meId);
Map<String, Object> fieldValues = new HashMap<String, Object>();
fieldValues.put("data", fields);
fieldValues.put("time", mdf.getTimeFlag());
cm.rpush(HealthManagerConstants.STORE_REGION_UAV, mdCacheKey, JSONHelper.toString(fieldValues));
cm.expire(HealthManagerConstants.STORE_REGION_UAV, mdCacheKey, cacheTime, TimeUnit.MINUTES);
/**
* FAST Failure: if there is no available address, we may not go to DataStore part
*/
if (isDataStoreOK == false) {
if (log.isDebugEnable() == true) {
log.debug(this, "Skip MonitorData INSERT as no available address");
}
continue;
}
/**
* Step 2: prepare the inst's metic data for opentsdb
*/
for (String key : fields.keySet()) {
Map<String, Object> point = new LinkedHashMap<String, Object>();
String mkey = key;
/**
* NOTE: RC=return code, they are dynamic tags, so make the metric key as "XX.rc" add a new
* tag "ptag" as the last tag
*/
String plusTag = null;
if (key.indexOf("RC") == 0) {
mkey = "RC";
plusTag = key;
} else if (key.indexOf("AC") == 0) {
mkey = "AC";
plusTag = key;
} else if (key.indexOf("EXT") == 0) {
mkey = "EXT";
plusTag = key;
}
/**
* WRITE DATA Schema Sample:
*
* {
*
* metric: urlResp.RC,
*
* timestamp: 1463816026510,
*
* value: 200,
*
* tags:[
*
* ip: 127.0.0.1,
*
* pgid:
* E:/UAVIDE/tomcat/apache-tomcat-7.0.65---E:/UAVIDE/defaultworkspace/.metadata/.plugins/org
* .eclipse.wst.server.core/tmp0,
*
* instid: http://127.0.0.1:8080/com.creditease.uav.console/rs/godeye/profile/q/cache,
*
* ptag(optional): RC400
*
* ]
*
* }
*/
point.put("metric", meId + "." + mkey);
point.put("timestamp", mdf.getTimeFlag());
point.put("value", fields.get(key));
Map<String, String> tags = new LinkedHashMap<>();
// IP
tags.put("ip", DataStoreHelper.encodeForOpenTSDB(mdf.getIP()));
// Program Id: Tomcat Server Profile Or Other Service Program Profile
tags.put("pgid", DataStoreHelper.encodeForOpenTSDB(mdf.getServerId()));
// Instance Id: May be A Tomcat Server, A Tomcat App, A Tomcat App URL or Process Id
tags.put("instid", encodedInstId);
if (plusTag != null) {
tags.put("ptag", DataStoreHelper.encodeForOpenTSDB(plusTag));
}
/**
* note: not store host as no use for tags
*/
// tags.put("host", mdf.getHost());
point.put("tags", tags);
points.add(point);
}
}
}
}
}
cm.submitBatch();
return points;
}
use of com.creditease.uav.cache.api.CacheManager in project uavstack by uavorg.
the class HealthManagerProfileDataLifeKeeper method run.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void run() {
CacheManager cm = cacheManager;
/**
* step 1: get the life keeper lock & check if can operate the nodeinfo
*/
if (!lock.getLock()) {
return;
}
List<String> nodeIds = cm.getHashKeys(HealthManagerConstants.STORE_REGION_UAV, HealthManagerConstants.STORE_KEY_PROFILEINFO);
if (nodeIds.size() == 0) {
// this lock
if (lock.isLockInHand()) {
lock.releaseLock();
}
return;
}
/**
* step 3: update the data
*/
Boolean isLockInHand = null;
for (String nodeId : nodeIds) {
Map<String, String> nodeInfoMap = cm.getHash(HealthManagerConstants.STORE_REGION_UAV, HealthManagerConstants.STORE_KEY_PROFILEINFO, nodeId);
String nodeInfoString = nodeInfoMap.get(nodeId);
Map appInstProfile = JSONHelper.toObject(nodeInfoString, Map.class);
long nodeClientTimestamp = (Long) appInstProfile.get("time");
long checkTime = System.currentTimeMillis();
long timeout = checkTime - nodeClientTimestamp;
if (timeout < 0) {
log.warn(this, "[PROFILE] There is sth wrong with timeout check of NODE[" + nodeId + "]: cur=" + checkTime + ",node=" + nodeClientTimestamp);
continue;
}
// recheck if the lock is still in hand
if (null == isLockInHand) {
// isLockStillInHand(cm, myLock);
isLockInHand = lock.isLockInHand();
}
if (!isLockInHand) {
return;
}
// node is in dying state
if (timeout >= this.nodeDyingTimeout && timeout < this.nodeDyingTimeout * 2) {
appInstProfile.put("state", "0");
nodeInfoMap.put(nodeId, JSONHelper.toString(appInstProfile));
cm.putHash(HealthManagerConstants.STORE_REGION_UAV, HealthManagerConstants.STORE_KEY_PROFILEINFO, nodeInfoMap);
} else // node is in dead state
if (timeout >= this.nodeDyingTimeout * 2 && timeout < this.nodeDyingTimeout * 3) {
appInstProfile.put("state", "-1");
nodeInfoMap.put(nodeId, JSONHelper.toString(appInstProfile));
cm.putHash(HealthManagerConstants.STORE_REGION_UAV, HealthManagerConstants.STORE_KEY_PROFILEINFO, nodeInfoMap);
} else // clean this node info
if (timeout >= this.nodeDyingTimeout * 3) {
// delete profile data
cm.delHash(HealthManagerConstants.STORE_REGION_UAV, HealthManagerConstants.STORE_KEY_PROFILEINFO, nodeId);
for (String key : profileKeyList) {
cm.delHash(HealthManagerConstants.STORE_REGION_UAV, key, nodeId);
}
// delete client profile data
cm.delHash(HealthManagerConstants.STORE_REGION_UAV, HealthManagerConstants.STORE_KEY_PROFILEINFO_APPCLIENT, "C@" + nodeId);
// delete ip link profile data
cm.del(HealthManagerConstants.STORE_REGION_UAV, "LNK@" + nodeId);
}
/**
* step 2: maintain app ip link profile data
*/
checkTime = System.currentTimeMillis();
boolean isAnyChange = false;
long iplinkTimeOut = 3600 * 1000;
Map<String, String> iplinkMap = cm.getHashAll(HealthManagerConstants.STORE_REGION_UAV, "LNK@" + nodeId);
List<String> delKeys = new ArrayList<String>();
for (String key : iplinkMap.keySet()) {
if (key.endsWith("-ts") == false) {
continue;
}
String tkey = key.substring(0, key.length() - 3);
long iplink_ts = DataConvertHelper.toLong(iplinkMap.get(key), -1);
if (iplink_ts == -1) {
continue;
}
long iplink_timeout = checkTime - iplink_ts;
/**
* iplink over 1 hour, then remove it, at least we can see the ip route in 1 hour from first happen
*/
if (iplink_timeout >= iplinkTimeOut) {
isAnyChange = true;
delKeys.add(tkey);
delKeys.add(key);
}
}
if (isAnyChange == true) {
String[] fields = new String[delKeys.size()];
cm.delHash(HealthManagerConstants.STORE_REGION_UAV, "LNK@" + nodeId, delKeys.toArray(fields));
}
}
/**
* step 4: release the lock
*/
lock.releaseLock();
}
use of com.creditease.uav.cache.api.CacheManager in project uavstack by uavorg.
the class HeartBeatServerAgent method stop.
@Override
public void stop() {
// stop HeartBeatEventServerWorker
if (null != hbServerListenWorker) {
hbServerListenWorker.stop();
if (log.isTraceEnable()) {
log.info(this, "HeartBeatServerListenWorker stopped");
}
}
// stop HeartBeatQueryListenWorker
if (null != this.hbqueryListenWorker) {
hbqueryListenWorker.stop();
if (log.isTraceEnable()) {
log.info(this, "HeartBeatQueryListenWorker stopped");
}
}
// stop HeartBeatServerLifeKeeper
this.getTimerWorkManager().cancel("HeartBeatServerLifeKeeper");
// shutdown CacheManager
CacheManager HBCacheManager = (CacheManager) this.getConfigManager().getComponent(this.feature, "HBCacheManager");
HBCacheManager.shutdown();
this.getConfigManager().unregisterComponent(this.feature, "HBCacheManager");
if (log.isTraceEnable()) {
log.info(this, "HeartBeatServerLifeKeeper stopped");
}
super.stop();
}
use of com.creditease.uav.cache.api.CacheManager in project uavstack by uavorg.
the class HeartBeatServerLifeKeeper method run.
@Override
public void run() {
CacheManager cm = cacheManager;
/**
* step 1: get the life keeper lock & check if can operate the nodeinfo
*/
if (!lock.getLock()) {
return;
}
List<String> nodeIds = cm.getHashKeys(HeartBeatProtocol.STORE_REGION_UAV, HeartBeatProtocol.STORE_KEY_NODEINFO);
if (nodeIds.size() == 0) {
// this lock
if (lock.isLockInHand()) {
lock.releaseLock();
}
return;
}
/**
* step 3: update the data
*/
Boolean isLockInHand = null;
for (String nodeId : nodeIds) {
Map<String, String> nodeInfoMap = cm.getHash(HeartBeatProtocol.STORE_REGION_UAV, HeartBeatProtocol.STORE_KEY_NODEINFO, nodeId);
String nodeInfoString = nodeInfoMap.get(nodeId);
NodeInfo nodeInfo = NodeInfo.toNodeInfo(nodeInfoString);
long nodeClientTimestamp = nodeInfo.getClientTimestamp();
long checkTime = System.currentTimeMillis();
long timeout = checkTime - nodeClientTimestamp;
if (timeout < 0) {
log.warn(this, "[HEARTBEAT] There is sth wrong with timeout check of NODE[" + nodeInfo.getIp() + "." + nodeInfo.getName() + "]: cur=" + checkTime + ",node=" + nodeClientTimestamp);
continue;
}
// recheck if the lock is still in hand
if (null == isLockInHand) {
isLockInHand = lock.isLockInHand();
}
if (!isLockInHand) {
return;
}
// node is in dying state
if (timeout >= this.nodeDyingTimeout && timeout < this.nodeDyingTimeout * 2) {
nodeInfo.putInfo(InfoType.Node, "state", "0");
nodeInfoMap.put(nodeId, nodeInfo.toJSONString());
cm.putHash(HeartBeatProtocol.STORE_REGION_UAV, HeartBeatProtocol.STORE_KEY_NODEINFO, nodeInfoMap);
} else // node is in dead state
if (timeout >= this.nodeDyingTimeout * 2 && timeout < this.nodeDyingTimeout * 3) {
nodeInfo.putInfo(InfoType.Node, "state", "-1");
nodeInfoMap.put(nodeId, nodeInfo.toJSONString());
cm.putHash(HeartBeatProtocol.STORE_REGION_UAV, HeartBeatProtocol.STORE_KEY_NODEINFO, nodeInfoMap);
} else // clean this node info
if (timeout >= this.nodeDyingTimeout * 3) {
cm.delHash(HeartBeatProtocol.STORE_REGION_UAV, HeartBeatProtocol.STORE_KEY_NODEINFO, nodeId);
}
}
/**
* step 4: release the lock
*/
lock.releaseLock();
}
use of com.creditease.uav.cache.api.CacheManager in project uavstack by uavorg.
the class DoTestLoadNodeInfo method main.
public static void main(String[] args) {
SystemLogger.init("DEBUG", true, 5);
CacheManager.build("localhost:6379", 5, 5, 5);
CacheManager cm = CacheManager.instance();
// testDelNodeInfo(cm);
testLoadAllNodeInfo(cm);
}
Aggregations