Search in sources :

Example 1 with CacheLock

use of com.creditease.uav.cache.api.CacheManager.CacheLock in project uavstack by uavorg.

the class JudgeNotifyTaskForTimer method run.

@Override
public void run() {
    CacheLock lock = null;
    try {
        lock = cm.newCacheLock(LOCK_REGION, stra.getName(), LOCK_TIMEOUT);
        if (!lock.getLock()) {
            return;
        }
        // get the type of source instance,like 'hostState'.For convert it to another type's instance
        String instType = stra.getName().substring(stra.getName().indexOf('@') + 1, stra.getName().lastIndexOf('@'));
        long judge_time_minute = judge_time - judge_time % 60000;
        /**
         * Step 1:find out instance
         */
        for (String instance : stra.getInstances()) {
            /**
             * Step 2: judge the strategy
             */
            StrategyJudgement judgement = (StrategyJudgement) getConfigManager().getComponent(feature, "StrategyJudgement");
            /**
             *  we add "_" to the end of each instance in hostState strategy for stream judge, we should remove it in timer judge.
             */
            if (instance.endsWith("_")) {
                instance = instance.substring(0, instance.length() - 1);
            }
            Slice slice = new Slice(instance, judge_time_minute);
            Map<String, Object> args = new HashMap<String, Object>();
            // 标识该slice由TimerTask创建,为同环比创建,非流式计算创建
            args.put("creater", "timer");
            args.put("instType", instType);
            slice.setArgs(args);
            Map<String, String> result = judgement.judge(slice, stra, null);
            /**
             * Step 3: if fire the event, build notification event
             */
            if (result != null && !result.isEmpty()) {
                NotificationEvent event = this.newNotificationEvent(instance, result, stra.getConvergences());
                // get context
                putContext(slice, event);
                // get action
                putNotifyAction(event, stra);
                // get msg tempalte
                putNotifyMsg(event, stra);
                if (this.log.isTraceEnable()) {
                    this.log.info(this, "RuntimeNotify Notification Event Happen: event=" + event.toJSONString());
                }
                this.putNotificationEvent(event);
            }
        }
    } catch (Exception e) {
        log.err(this, "JudgeNotifyTimerTask RUN FAIL." + " StrategyDesc=" + stra.getDesc() + ", StrategyName=" + stra.getName() + "\n", e);
    } finally {
        if (lock != null && lock.isLockInHand()) {
            lock.releaseLock();
        }
    }
    if (log.isDebugEnable()) {
        long cost = System.currentTimeMillis() - taskStart;
        String detail = cost < 10 ? "" : " detail:strategy=" + JSONHelper.toString(stra);
        log.debug(this, "whole task lifecycle COST: (" + cost + ")ms" + detail);
    }
}
Also used : CacheLock(com.creditease.uav.cache.api.CacheManager.CacheLock) StrategyJudgement(com.creditease.uav.feature.runtimenotify.StrategyJudgement) HashMap(java.util.HashMap) Slice(com.creditease.uav.feature.runtimenotify.Slice) NotificationEvent(com.creditease.agent.monitor.api.NotificationEvent)

Example 2 with CacheLock

use of com.creditease.uav.cache.api.CacheManager.CacheLock in project uavstack by uavorg.

the class JudgeNotifyTimerTask method run.

@Override
public void run() {
    CacheLock lock = null;
    try {
        lock = cm.newCacheLock(LOCK_REGION, stra.getName(), LOCK_TIMEOUT);
        if (!lock.getLock()) {
            return;
        }
        /**
         * Step 1:find out instance
         */
        for (String instance : stra.getInstances()) {
            /**
             * Step 2: judge the strategy
             */
            StrategyJudgement judgement = (StrategyJudgement) getConfigManager().getComponent(feature, "StrategyJudgement");
            Map<String, String> result = judgement.judge(new Slice(instance, judge_time), stra, null);
            /**
             * Step 3: if fire the event, build notification event
             */
            if (result != null && !result.isEmpty()) {
                NotificationEvent event = this.newNotificationEvent(instance, result, stra.getConvergences());
                // get context
                putContext(event);
                // get action
                putNotifyAction(event, stra);
                // get msg tempalte
                putNotifyMsg(event, stra);
                if (this.log.isTraceEnable()) {
                    this.log.info(this, "RuntimeNotify Notification Event Happen: event=" + event.toJSONString());
                }
                this.putNotificationEvent(event);
            }
        }
    } catch (Exception e) {
        log.err(this, "JudgeNotifyTimerTask" + stra.getName() + " RUN FAIL.", e);
    } finally {
        if (lock != null && lock.isLockInHand()) {
            lock.releaseLock();
        }
    }
    if (log.isDebugEnable()) {
        long cost = System.currentTimeMillis() - taskStart;
        String detail = cost < 10 ? "" : " detail:strategy=" + JSONHelper.toString(stra);
        log.debug(this, "whole task lifecycle COST: (" + cost + ")ms" + detail);
    }
}
Also used : CacheLock(com.creditease.uav.cache.api.CacheManager.CacheLock) StrategyJudgement(com.creditease.uav.feature.runtimenotify.StrategyJudgement) Slice(com.creditease.uav.feature.runtimenotify.Slice) NotificationEvent(com.creditease.agent.monitor.api.NotificationEvent)

Aggregations

NotificationEvent (com.creditease.agent.monitor.api.NotificationEvent)2 CacheLock (com.creditease.uav.cache.api.CacheManager.CacheLock)2 Slice (com.creditease.uav.feature.runtimenotify.Slice)2 StrategyJudgement (com.creditease.uav.feature.runtimenotify.StrategyJudgement)2 HashMap (java.util.HashMap)1