Search in sources :

Example 1 with NodeInfo

use of com.creditease.agent.feature.hbagent.node.NodeInfo in project uavstack by uavorg.

the class HBServerDefaultHandler method handleServerOut.

@Override
public void handleServerOut(HeartBeatEvent data) {
    if (!data.containEvent(HeartBeatProtocol.EVENT_DEFAULT)) {
        return;
    }
    @SuppressWarnings("unchecked") List<String> nodeInfoStringArray = (List<String>) data.getParam(HeartBeatProtocol.EVENT_DEFAULT, HeartBeatProtocol.EVENT_KEY_NODE_INFO);
    if (null != nodeInfoStringArray && nodeInfoStringArray.size() == 0) {
        return;
    }
    String checkIsMaster = this.getConfigManager().getFeatureConfiguration("hbserveragent", "ismaster");
    // is not master
    if (null == checkIsMaster || !"true".equalsIgnoreCase(checkIsMaster)) {
        AgentFeatureComponent afc = (AgentFeatureComponent) this.getConfigManager().getComponent("hbclientagent", "HeartBeatClientAgent");
        afc.exchange("hbclientagent.nodeinfo.upstream", nodeInfoStringArray);
    } else // is master
    {
        // AgentFeatureComponent ida = (AgentFeatureComponent) this.getConfigManager().getComponent("ida",
        // "IssueDiagnoseAssitAgent");
        // sync node info to storage
        cacheManager.beginBatch();
        for (String nodeInfoString : nodeInfoStringArray) {
            NodeInfo ni = NodeInfo.toNodeInfo(nodeInfoString);
            long curTime = System.currentTimeMillis();
            // set server side timestamp
            ni.setServerTimestamp(curTime);
            // /**
            // * push to IDA feature
            // */
            // if (ida != null) {
            // ida.exchange("ida.put.data", ni);
            // }
            cacheManager.putHash(HeartBeatProtocol.STORE_REGION_UAV, HeartBeatProtocol.STORE_KEY_NODEINFO, ni.getId(), ni.toJSONString());
            /**
             * sync node services to service list
             */
            String servicesStr = ni.getInfo(InfoType.Node, "services");
            @SuppressWarnings("rawtypes") Map services = JSONHelper.toObject(servicesStr, Map.class, true);
            for (Object s : services.keySet()) {
                String sId = (String) s;
                /**
                 * MSCP Services: state =1, means all right, expire time out is 60 seconds
                 */
                cacheManager.putHash(HeartBeatProtocol.STORE_REGION_UAV, HeartBeatProtocol.STORE_KEY_SERVICE_PREFIX + sId, (String) services.get(sId), "1");
                cacheManager.expire(HeartBeatProtocol.STORE_REGION_UAV, HeartBeatProtocol.STORE_KEY_SERVICE_PREFIX + sId, 60, TimeUnit.SECONDS);
            }
        }
        cacheManager.submitBatch();
    }
    // set return code
    data.putParam(HeartBeatProtocol.EVENT_DEFAULT, HeartBeatProtocol.EVENT_KEY_RETCODE, HeartBeatProtocol.RC_I0000);
    if (isEnableNTP == true) {
        data.putParam(HeartBeatProtocol.EVENT_DEFAULT, HeartBeatProtocol.EVENT_KEY_TIME, System.currentTimeMillis());
    }
}
Also used : NodeInfo(com.creditease.agent.feature.hbagent.node.NodeInfo) List(java.util.List) Map(java.util.Map) AgentFeatureComponent(com.creditease.agent.spi.AgentFeatureComponent)

Example 2 with NodeInfo

use of com.creditease.agent.feature.hbagent.node.NodeInfo in project uavstack by uavorg.

the class ContainerPerfCatcher method handle.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Override
public void handle(Object data) {
    if (!isEnable) {
        return;
    }
    Object[] da = (Object[]) data;
    /*
         * only handle NodeInfo
         */
    if (!NodeInfo.class.isAssignableFrom(da[0].getClass())) {
        return;
    }
    NodeInfo ni = (NodeInfo) da[0];
    String nodeProcsStr = ni.getInfo().get("node.procs");
    if (null == nodeProcsStr) {
        return;
    }
    Map procs = JSONHelper.toObject(nodeProcsStr, Map.class);
    for (Object pidObj : procs.values()) {
        Map proc = (Map) pidObj;
        Object tagsObj = proc.get("tags");
        if (tagsObj == null) {
            continue;
        }
        /**
         * check if any perf value reach the threshold
         */
        Map<String, String> tags = (Map<String, String>) tagsObj;
        boolean isReachThreshold = false;
        String thresHoldExp = "";
        double reachThresHoldVal = -1;
        for (String key : thresholds.keySet()) {
            if (tags.containsKey(key)) {
                try {
                    double curD = Double.parseDouble(tags.get(key));
                    double d = thresholds.get(key);
                    if (curD >= d) {
                        isReachThreshold = true;
                        reachThresHoldVal = curD;
                        thresHoldExp = key + "=" + d;
                        break;
                    }
                } catch (NumberFormatException e) {
                // ignore
                }
            }
        }
        /**
         * ReachThreshold
         */
        if (isReachThreshold == false) {
            continue;
        }
        String when = DateTimeHelper.toStandardDateFormat(ni.getClientTimestamp());
        String dayFolder = this.outputFolderPath + "/" + DateTimeHelper.toFormat("yyyy-MM-dd", ni.getClientTimestamp());
        IOHelper.createFolder(dayFolder);
        String hourFile = dayFolder + "/" + DateTimeHelper.toFormat("yyyy-MM-dd_HH", ni.getClientTimestamp()) + ".uav.ida.log";
        String log = when + "   " + thresHoldExp + "    " + reachThresHoldVal + "   " + JSONHelper.toString(proc).replace("\r", "").replace("\n", "") + System.lineSeparator();
        try {
            IOHelper.writeTxtFile(hourFile, log, "utf-8", true);
        } catch (IOException e) {
            this.log.err(this, "WRITE IDA LOG FAIL.", e);
        }
    }
}
Also used : NodeInfo(com.creditease.agent.feature.hbagent.node.NodeInfo) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 3 with NodeInfo

use of com.creditease.agent.feature.hbagent.node.NodeInfo in project uavstack by uavorg.

the class HBClientDefaultHandler method handleClientOut.

@Override
public void handleClientOut(HeartBeatEvent data) {
    /**
     * sync node info
     */
    NodeInfo ni = new NodeInfo();
    // node id
    ni.setId(this.getConfigManager().getContext(IConfigurationManager.NODEUUID));
    String[] hosts = NetworkHelper.getHosts();
    // host
    ni.setHost(hosts[0]);
    // ip
    ni.setIp(hosts[1]);
    // node name
    String nodeName = this.getConfigManager().getContext(IConfigurationManager.NODEAPPNAME);
    ni.setName(nodeName);
    // node group
    String nodeGroup = this.getConfigManager().getContext(IConfigurationManager.NODEGROUP);
    ni.setGroup(nodeGroup);
    // mac address
    ni.putInfo(InfoType.OS, "mac", NetworkHelper.getMACAddress());
    // OS type
    ni.putInfo(InfoType.OS, "type", System.getProperty("os.name"));
    // OS arch
    ni.putInfo(InfoType.OS, "arch", System.getProperty("os.arch"));
    // CPU number
    ni.putInfo(InfoType.OS, "cpu.number", String.valueOf(Runtime.getRuntime().availableProcessors()));
    OperatingSystemMXBean osmb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
    // max physical memory
    ni.putInfo(InfoType.OS, "cpu.maxmem", String.valueOf(osmb.getTotalPhysicalMemorySize()));
    // free physical memory
    ni.putInfo(InfoType.OS, "cpu.freemem", String.valueOf(osmb.getFreePhysicalMemorySize()));
    // CPU load
    ni.putInfo(InfoType.OS, "cpu.load", String.valueOf(Math.round(osmb.getSystemCpuLoad() * 100)));
    // CPU avg load
    ni.putInfo(InfoType.OS, "cpu.avgload", String.valueOf(Math.round(osmb.getSystemLoadAverage() * 100)));
    // Current Connections
    ni.putInfo(InfoType.OS, "conn.cur", getCurrentConnectionCount());
    // IO DISK INFO
    String str = "{}";
    try {
        str = getIODiskInfo();
    } catch (Exception e) {
        log.err(this, "error occur during get diskinfo:" + e);
    }
    ni.putInfo(InfoType.OS, "io.disk", str);
    // java version
    ni.putInfo(InfoType.OS, "java.ver", System.getProperty("java.version"));
    // java vm name
    ni.putInfo(InfoType.OS, "java.vm", System.getProperty("java.vm.name"));
    // java home
    ni.putInfo(InfoType.OS, "java.home", System.getProperty("java.home"));
    // node info
    ni.putInfo(InfoType.Node, "root", ConfigurationManager.getInstance().getContext(IConfigurationManager.ROOT));
    ni.putInfo(InfoType.Node, "profile", ConfigurationManager.getInstance().getContext(IConfigurationManager.PROFILENAME));
    ni.putInfo(InfoType.Node, "hbserver", ((HeartBeatClientReqWorker) ConfigurationManager.getInstance().getComponent(this.feature, "HeartBeatClientReqWorker")).getCurrentHBServerURL());
    ni.putInfo(InfoType.Node, "pid", getPID());
    /**
     * node.state>0, means node alive node.state==0, means node is dying node.state<0, means node is dead
     */
    ni.putInfo(InfoType.Node, "state", "1");
    // node feature info
    Map<String, List<String>> featureInfo = getFeatureInfo();
    ni.putInfo(InfoType.Node, "feature", JSONHelper.toString(featureInfo));
    // node services info
    Map<String, String> services = getServiceURLs();
    String servicesStr = JSONHelper.toString(services);
    ni.putInfo(InfoType.Node, "services", servicesStr);
    ni.putInfo(InfoType.Node, "version", getConfigManager().getContext(IConfigurationManager.NODEAPPVERSION));
    // set time stamp
    ni.setClientTimestamp(System.currentTimeMillis());
    // set -1 mean no set by server at client side
    ni.setServerTimestamp(-1);
    // get the hbagent queue if this is non-master hbserver node
    HeartBeatClientAgent hbagent = (HeartBeatClientAgent) this.getConfigManager().getComponent(this.feature, "HeartBeatClientAgent");
    // get node ext info
    if (null != hbagent) {
        Map<String, Object> nodeExtInfo = hbagent.getNodeExtInfo();
        for (String key : nodeExtInfo.keySet()) {
            ni.putInfo(InfoType.Node, key, JSONHelper.toString(nodeExtInfo.get(key)));
        }
    }
    List<String> nodeInfos = new ArrayList<String>();
    // add node info of this node
    String nodeInfoOfThisNode = ni.toJSONString();
    nodeInfos.add(nodeInfoOfThisNode);
    if (null != hbagent) {
        List<List<String>> nodeInfoLists = hbagent.pollNodeInfoQueue();
        if (nodeInfoLists.size() > 0) {
            for (List<String> nodeInfoList : nodeInfoLists) {
                nodeInfos.addAll(nodeInfoList);
            }
        }
    }
    data.putParam(HeartBeatProtocol.EVENT_DEFAULT, HeartBeatProtocol.EVENT_KEY_NODE_INFO, nodeInfos);
    // record hb request start time
    hbStartTime = System.currentTimeMillis();
}
Also used : HeartBeatClientAgent(com.creditease.agent.feature.HeartBeatClientAgent) ArrayList(java.util.ArrayList) NodeInfo(com.creditease.agent.feature.hbagent.node.NodeInfo) ArrayList(java.util.ArrayList) List(java.util.List) OperatingSystemMXBean(com.sun.management.OperatingSystemMXBean)

Example 4 with NodeInfo

use of com.creditease.agent.feature.hbagent.node.NodeInfo 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();
}
Also used : NodeInfo(com.creditease.agent.feature.hbagent.node.NodeInfo) CacheManager(com.creditease.uav.cache.api.CacheManager)

Aggregations

NodeInfo (com.creditease.agent.feature.hbagent.node.NodeInfo)4 List (java.util.List)2 Map (java.util.Map)2 HeartBeatClientAgent (com.creditease.agent.feature.HeartBeatClientAgent)1 AgentFeatureComponent (com.creditease.agent.spi.AgentFeatureComponent)1 CacheManager (com.creditease.uav.cache.api.CacheManager)1 OperatingSystemMXBean (com.sun.management.OperatingSystemMXBean)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1