Search in sources :

Example 1 with StrategyJudgement

use of com.creditease.uav.feature.runtimenotify.StrategyJudgement in project uavstack by uavorg.

the class JudgeNotifyTask method run.

@Override
public void run() {
    NotifyStrategy stra = null;
    try {
        /**
         * Step 1: seek strategy
         */
        RuntimeNotifyStrategyMgr strategyMgr = (RuntimeNotifyStrategyMgr) getConfigManager().getComponent(this.feature, "RuntimeNotifyStrategyMgr");
        stra = strategyMgr.seekStrategy(curSlice.getKey());
        if (stra == null) {
            return;
        }
        /**
         * Step 1.5: underclocking
         */
        long range = stra.getMaxRange();
        if (range > 0) {
            CacheManager cm = (CacheManager) getConfigManager().getComponent(this.feature, STORAGE_CACHE_MANAGER_NAME);
            String judgedKey = genJudgedKey(curSlice);
            if (cm.exists(UAV_CACHE_REGION, judgedKey)) {
                return;
            } else {
                cm.put(UAV_CACHE_REGION, judgedKey, String.valueOf(curSlice.getTime()));
                cm.expire(UAV_CACHE_REGION, judgedKey, range, TimeUnit.MILLISECONDS);
            }
        }
        /**
         * Step 2: dump range slices
         */
        List<Slice> rangeSlices = null;
        if (range > 0) {
            RuntimeNotifySliceMgr sliceMgr = (RuntimeNotifySliceMgr) getConfigManager().getComponent(this.feature, "RuntimeNotifySliceMgr");
            rangeSlices = sliceMgr.getSlices(curSlice, range);
            if (rangeSlices.isEmpty()) {
                if (log.isDebugEnable()) {
                    log.debug(this, "RuntimeNotify judge dump invalid.");
                }
                return;
            }
        } else {
            rangeSlices = new ArrayList<>(1);
            rangeSlices.add(curSlice);
        }
        /**
         * Step 3: judge the strategy
         */
        StrategyJudgement judgement = (StrategyJudgement) getConfigManager().getComponent(feature, "StrategyJudgement");
        Map<String, String> result = judgement.judge(curSlice, stra, rangeSlices);
        // ?? maybe no effective
        if (rangeSlices != null) {
            rangeSlices.clear();
            rangeSlices = null;
        }
        /**
         * Step 5: if fire the event, build notification event
         */
        if (result != null && !result.isEmpty()) {
            NotificationEvent event = this.newNotificationEvent(result, stra);
            // 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, "JudgeNotifyTask RUN FAIL." + " StrategyDesc=" + stra.getDesc() + ", StrategyName=" + stra.getName() + "\n", e);
    }
    if (log.isDebugEnable()) {
        long cost = System.currentTimeMillis() - taskStart;
        String detail = cost < 10 ? "" : " detail: key=" + curSlice.getKey() + ", strategy=" + JSONHelper.toString(stra);
        log.debug(this, "whole task lifecycle COST: (" + cost + ")ms" + detail);
    }
}
Also used : RuntimeNotifyStrategyMgr(com.creditease.uav.feature.runtimenotify.scheduler.RuntimeNotifyStrategyMgr) StrategyJudgement(com.creditease.uav.feature.runtimenotify.StrategyJudgement) NotifyStrategy(com.creditease.uav.feature.runtimenotify.NotifyStrategy) NotificationEvent(com.creditease.agent.monitor.api.NotificationEvent) Slice(com.creditease.uav.feature.runtimenotify.Slice) CacheManager(com.creditease.uav.cache.api.CacheManager) RuntimeNotifySliceMgr(com.creditease.uav.feature.runtimenotify.RuntimeNotifySliceMgr)

Example 2 with StrategyJudgement

use of com.creditease.uav.feature.runtimenotify.StrategyJudgement 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 3 with StrategyJudgement

use of com.creditease.uav.feature.runtimenotify.StrategyJudgement 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)

Example 4 with StrategyJudgement

use of com.creditease.uav.feature.runtimenotify.StrategyJudgement in project uavstack by uavorg.

the class RuntimeNotifyCatcher method start.

@Override
public void start() {
    // reset aredis bootstrap executor pool size
    AsyncRedisConnection.setBootstrapExecutorPoolSize(getCfgInt("cm.bootstrappoolsize", 10));
    // init cache manager
    String cacheServer = getCfg("cm.server");
    int cmMinSize = getCfgInt("cm.minsize", 5);
    int cmMaxSize = getCfgInt("cm.maxsize", 10);
    int cmQueueSize = getCfgInt("cm.qsize", 5);
    String cmPwd = getCfg("cm.pwd");
    CacheManager cm = CacheManagerFactory.build(cacheServer, cmMinSize, cmMaxSize, cmQueueSize, cmPwd);
    getConfigManager().registerComponent(this.feature, CACHE_MANAGER_NAME, cm);
    if (log.isTraceEnable()) {
        log.info(this, String.format("RuntimeNotifyCatcher-CacheManager INIT: server:%s,minsize:%d,maxsize:%d,qsize:%d", cacheServer, cmMinSize, cmMaxSize, cmQueueSize));
    }
    // init slice storage cm
    String storeCMServer = getCfg("storecm.server");
    int storeCMMinSize = getCfgInt("storecm.minsize", 5);
    int storeCMMaxSize = getCfgInt("storecm.maxsize", 10);
    int storeCMQueueSize = getCfgInt("storecm.qsize", 5);
    String storeCMPwd = getCfg("storecm.pwd");
    CacheManager storeCM = CacheManagerFactory.build(storeCMServer, storeCMMinSize, storeCMMaxSize, storeCMQueueSize, storeCMPwd);
    getConfigManager().registerComponent(this.feature, STORAGE_CACHE_MANAGER_NAME, storeCM);
    if (log.isTraceEnable()) {
        log.info(this, String.format("RuntimeNotifyCatcher-StorageCacheManager INIT: server:%s,minsize:%d,maxsize:%d,qsize:%d", storeCMServer, storeCMMinSize, storeCMMaxSize, storeCMQueueSize));
    }
    new StrategyJudgement("StrategyJudgement", this.feature);
    // init strategyMgr
    String strategy = getCfg("strategy.config");
    RuntimeNotifyStrategyMgr rtNotifyStrategyMgr = new RuntimeNotifyStrategyMgr("RuntimeNotifyStrategyMgr", feature, cm);
    /**
     * NOTE: this setting is only for development testing for production env, the strategy is only read from cache
     */
    if (strategy != null) {
        rtNotifyStrategyMgr.loadStrategy(strategy);
    }
    long strategyPeriod = DataConvertHelper.toLong(this.getConfigManager().getFeatureConfiguration(this.feature, "strategy.interval"), 30000);
    this.getTimerWorkManager().scheduleWork("RuntimeNotifyStrategyMgr", rtNotifyStrategyMgr, 0, strategyPeriod);
    if (log.isTraceEnable()) {
        log.info(this, "RuntimeNotifyCatcher-RuntimeNotifyStrategyMgr started: " + strategy);
    }
    // init SliceMgr
    String sliceConfig = getCfg("slice.config");
    RuntimeNotifySliceMgr sliceMgr = new RuntimeNotifySliceMgr("RuntimeNotifySliceMgr", feature, storeCM);
    sliceMgr.loadSliceConfig(sliceConfig);
    if (log.isTraceEnable()) {
        log.info(this, "RuntimeNotifyCatcher-RuntimeNotifySliceMgr started: " + sliceConfig);
    }
    // init 1+N qworker
    int coreSize = getCfgInt("qworker.coresize", 5);
    int maxSize = getCfgInt("qworker.maxsize", 50);
    int bqSize = getCfgInt("qworker.bqsize", 20);
    int timeout = getCfgInt("qworker.keepalivetimeout", 60000);
    I1NQueueWorker runtimeNotifyJudgeWorker = get1NQueueWorkerMgr().newQueueWorker(QWORKER_NAME, feature, coreSize, maxSize, bqSize, timeout);
    qwThread = new Thread(runtimeNotifyJudgeWorker);
    qwThread.setName("RuntimeNotifyCatcher-I1NQueueWorker-MainThread");
    qwThread.start();
    if (log.isTraceEnable()) {
        log.info(this, String.format("RuntimeNotifyCatcher-I1NQueueWorker[" + QWORKER_NAME + "] started: coresize:%d,maxsize:%d,bqsize:%d,keepalivetimeout:%d", coreSize, maxSize, bqSize, timeout));
    }
    // init RuntimeNotifyActionEngine
    engine = this.getActionEngineMgr().newActionEngine("RuntimeNotifyActionEngine", this.feature);
    // register exprAdaptor
    new ClientExprAdaptor(AbstractExprAdaptor.CLIENT_RESP, this.feature, engine);
    new UrlExprAdaptor(AbstractExprAdaptor.URL_RESP, this.feature, engine);
    new AppExprAdaptor(AbstractExprAdaptor.APP_RESP, this.feature, engine);
    new ServerExprAdaptor(AbstractExprAdaptor.SERVER_RESP, this.feature, engine);
    new JvmExprAdaptor(AbstractExprAdaptor.JVM, this.feature, engine);
    new ProcExprAdaptor(AbstractExprAdaptor.PROC_STATE, this.feature, engine);
    new HostExprAdaptor(AbstractExprAdaptor.HOST_STATE, this.feature, engine);
    // start runtime notify data consumer
    StandardMessagingBuilder smb = new StandardMessagingBuilder("RTNTFCommonMsgBuilder", this.feature);
    try {
        smb.init("com.creditease.uav.feature.runtimenotify.messaging.handlers");
    } catch (IOException e) {
        log.err(this, "Read msgtype2topic.properties FAILs, RuntimeNotifyCatcher can not START", e);
        return;
    }
    runtimeDataConsumer = smb.buildConsumer(MonitorDataFrame.MessageType.RuntimeNtf.toString());
    if (runtimeDataConsumer != null) {
        runtimeDataConsumer.start();
        this.getConfigManager().registerComponent(this.feature, COMSUMER_RUNTIME, runtimeDataConsumer);
        if (log.isTraceEnable()) {
            log.info(this, "RuntimeNotifyCacher RuntimeConsumer started");
        }
    }
    // init timer worker
    boolean isEnableInfoTimer = DataConvertHelper.toBoolean(this.getCfg("nodeinfotimer.enable"), false);
    if (isEnableInfoTimer == true) {
        int period = getCfgInt("nodeinfotimer.period", 15000);
        getTimerWorkManager().scheduleWorkInPeriod("NodeInfoWatcher", new NodeInfoWatcher("NodeInfoWatcher", feature), 0, period);
        if (log.isTraceEnable()) {
            log.info(this, "RuntimeNotifyCatcher-NodeInfoWatcher started: period:" + period);
        }
    }
    // init RuntimeNotifyServerWorker
    int port = Integer.parseInt(this.getConfigManager().getFeatureConfiguration(this.feature, "http.port"));
    int backlog = Integer.parseInt(this.getConfigManager().getFeatureConfiguration(this.feature, "http.backlog"));
    int core = Integer.parseInt(this.getConfigManager().getFeatureConfiguration(this.feature, "http.core"));
    int max = Integer.parseInt(this.getConfigManager().getFeatureConfiguration(this.feature, "http.max"));
    int bqsize = Integer.parseInt(this.getConfigManager().getFeatureConfiguration(this.feature, "http.bqsize"));
    runtimeNotifyServerWorker = new RuntimeNotifyServerWorker("RuntimeNotifyServerWorker", feature, "rnswhandlers");
    @SuppressWarnings({ "rawtypes", "unchecked" }) ThreadPoolExecutor exe = new ThreadPoolExecutor(core, max, 30000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue(bqsize));
    runtimeNotifyServerWorker.start(exe, port, backlog);
    if (log.isTraceEnable()) {
        log.info(this, "RuntimeNotifyCatcher-RuntimeNotifyServerWorker started");
    }
    // init RuntimeNotifyTimerWorker
    if (DataConvertHelper.toBoolean(this.getCfg("timernotify.enable"), false)) {
        TimerNotifyWorker timerNotifyWorker = new TimerNotifyWorker("TimerNotifyWorker", feature);
        this.getTimerWorkManager().scheduleWork("TimerNotifyWorker", timerNotifyWorker, 0, 60000);
        if (log.isTraceEnable()) {
            log.info(this, "RuntimeNotifyCatcher-RuntimeNotifyStrategyMgr started: " + strategy);
        }
    }
}
Also used : RuntimeNotifyStrategyMgr(com.creditease.uav.feature.runtimenotify.scheduler.RuntimeNotifyStrategyMgr) HostExprAdaptor(com.creditease.uav.feature.runtimenotify.expradaptors.HostExprAdaptor) AppExprAdaptor(com.creditease.uav.feature.runtimenotify.expradaptors.AppExprAdaptor) RuntimeNotifyServerWorker(com.creditease.uav.feature.runtimenotify.http.RuntimeNotifyServerWorker) ClientExprAdaptor(com.creditease.uav.feature.runtimenotify.expradaptors.ClientExprAdaptor) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) CacheManager(com.creditease.uav.cache.api.CacheManager) UrlExprAdaptor(com.creditease.uav.feature.runtimenotify.expradaptors.UrlExprAdaptor) JvmExprAdaptor(com.creditease.uav.feature.runtimenotify.expradaptors.JvmExprAdaptor) StrategyJudgement(com.creditease.uav.feature.runtimenotify.StrategyJudgement) StandardMessagingBuilder(org.uavstack.resources.common.messaging.StandardMessagingBuilder) ServerExprAdaptor(com.creditease.uav.feature.runtimenotify.expradaptors.ServerExprAdaptor) TimerNotifyWorker(com.creditease.uav.feature.runtimenotify.scheduler.TimerNotifyWorker) NodeInfoWatcher(com.creditease.uav.feature.runtimenotify.scheduler.NodeInfoWatcher) IOException(java.io.IOException) I1NQueueWorker(com.creditease.agent.spi.I1NQueueWorker) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) RuntimeNotifySliceMgr(com.creditease.uav.feature.runtimenotify.RuntimeNotifySliceMgr) ProcExprAdaptor(com.creditease.uav.feature.runtimenotify.expradaptors.ProcExprAdaptor)

Aggregations

StrategyJudgement (com.creditease.uav.feature.runtimenotify.StrategyJudgement)4 NotificationEvent (com.creditease.agent.monitor.api.NotificationEvent)3 Slice (com.creditease.uav.feature.runtimenotify.Slice)3 CacheManager (com.creditease.uav.cache.api.CacheManager)2 CacheLock (com.creditease.uav.cache.api.CacheManager.CacheLock)2 RuntimeNotifySliceMgr (com.creditease.uav.feature.runtimenotify.RuntimeNotifySliceMgr)2 RuntimeNotifyStrategyMgr (com.creditease.uav.feature.runtimenotify.scheduler.RuntimeNotifyStrategyMgr)2 I1NQueueWorker (com.creditease.agent.spi.I1NQueueWorker)1 NotifyStrategy (com.creditease.uav.feature.runtimenotify.NotifyStrategy)1 AppExprAdaptor (com.creditease.uav.feature.runtimenotify.expradaptors.AppExprAdaptor)1 ClientExprAdaptor (com.creditease.uav.feature.runtimenotify.expradaptors.ClientExprAdaptor)1 HostExprAdaptor (com.creditease.uav.feature.runtimenotify.expradaptors.HostExprAdaptor)1 JvmExprAdaptor (com.creditease.uav.feature.runtimenotify.expradaptors.JvmExprAdaptor)1 ProcExprAdaptor (com.creditease.uav.feature.runtimenotify.expradaptors.ProcExprAdaptor)1 ServerExprAdaptor (com.creditease.uav.feature.runtimenotify.expradaptors.ServerExprAdaptor)1 UrlExprAdaptor (com.creditease.uav.feature.runtimenotify.expradaptors.UrlExprAdaptor)1 RuntimeNotifyServerWorker (com.creditease.uav.feature.runtimenotify.http.RuntimeNotifyServerWorker)1 NodeInfoWatcher (com.creditease.uav.feature.runtimenotify.scheduler.NodeInfoWatcher)1 TimerNotifyWorker (com.creditease.uav.feature.runtimenotify.scheduler.TimerNotifyWorker)1 IOException (java.io.IOException)1