Oozie-Service-CoordMaterializeTriggerService

功能:
定期去掃描元數(shù)據(jù)表CoordinatorJobBean,將滿(mǎn)足條件的Job實(shí)體化;

// default is 300sec (5min)
int schedulingInterval = Services.get().getConf().getInt(CONF_SCHEDULING_INTERVAL, lookupInterval);
Runnable lookupTriggerJobsRunnable = new CoordMaterializeTriggerRunnable(materializationWindow, lookupInterval);
services.get(SchedulerService.class).schedule(lookupTriggerJobsRunnable, 10, schedulingInterval,SchedulerService.Unit.SEC);

CoordMaterializeTriggerRunnable:

oozie里面的觸發(fā)機(jī)制:

 1、啟動(dòng)一個(gè)定時(shí)執(zhí)行器,每隔 schedulingInterval 時(shí)間運(yùn)行一次 CoordMaterializeTriggerRunnable;
 2、查詢(xún)CoordinatorJobBean根據(jù)時(shí)間和條數(shù)限制獲取本次需要實(shí)例化的CoordinatorJobBean;
    技巧: 在CoordinatorJobBean中有個(gè)字段 nextMaterializedTimestamp 表明,在這個(gè)時(shí)間點(diǎn)之前的實(shí)例已經(jīng)存在,為了盡可能的讓任務(wù)的實(shí)例盡可能在觸發(fā)之前
    實(shí)例就產(chǎn)生,而不是事后產(chǎn)生,在查詢(xún)CoordinatorJobBean使用的時(shí)候不是使用當(dāng)前時(shí)間,而是使用未來(lái)的一個(gè)時(shí)間: new Date().getTime() + lookupInterval * 1000
 3、異步去實(shí)例化 CoordinatorJobBean 下的 action;
 4、在實(shí)例化action的時(shí)候,生成的是窗口期內(nèi)的一批實(shí)例,在類(lèi) CoordMaterializeTransitionXCommand中實(shí)現(xiàn);
 5、loadState():加載 CoordinatorJobBean 信息,計(jì)算這次要實(shí)例化的時(shí)間窗口: startMatdTime 和 endMatdTime;
 6、materialize():根據(jù)不同的計(jì)算方式(cron、自定義)來(lái)在這個(gè)時(shí)間窗口內(nèi)循環(huán)的產(chǎn)生action的實(shí)例;
 7、更新  CoordinatorJobBean 狀態(tài),記錄 endMatdTime、lastActionNumber(時(shí)間累加)、狀態(tài)置為running狀態(tài)、如果jobEndTime< endMatdTime
    說(shuō)明這個(gè) CoordinatorJobBean 實(shí)例化工作已經(jīng)結(jié)束,標(biāo)記 job.setDoneMaterialization();
 8、performWrites():更新數(shù)據(jù)庫(kù)
 9、notifyParent():通知上層結(jié)構(gòu) bundle;
/** * This runnable class will run in every "interval" to queue CoordMaterializeTransitionXCommand. 
*/
static class CoordMaterializeTriggerRunnable implements Runnable {
    private int materializationWindow;
    private int lookupInterval;
    private long delay = 0;
    private List<XCallable<Void>> callables;
    private List<XCallable<Void>> delayedCallables;
    private XLog LOG = XLog.getLog(getClass());
    public CoordMaterializeTriggerRunnable(int materializationWindow, int lookupInterval) {
        this.materializationWindow = materializationWindow;
        this.lookupInterval = lookupInterval;
    }
    @Override
    public void run() {
        LockToken lock = null;
        // first check if there is some other running instance from the same service;
        try {
            lock = Services.get().get(MemoryLocksService.class)
                    .getWriteLock(CoordMaterializeTriggerService.class.getName(), lockTimeout);
            if (lock != null) {
                runCoordJobMatLookup();
                if (null != callables) {
                    boolean ret = Services.get().get(CallableQueueService.class).queueSerial(callables);
                    if (ret == false) {
                        XLog.getLog(getClass()).warn(
                                "Unable to queue the callables commands for CoordMaterializeTriggerRunnable. "
                                        + "Most possibly command queue is full. Queue size is :"
                                        + Services.get().get(CallableQueueService.class).queueSize());
                    }
                    callables = null;
                }
                if (null != delayedCallables) {
                    boolean ret = Services.get().get(CallableQueueService.class)
                            .queueSerial(delayedCallables, this.delay);
                    if (ret == false) {
                        XLog.getLog(getClass()).warn(
                                "Unable to queue the delayedCallables commands for CoordMaterializeTriggerRunnable. "
                                        + "Most possibly Callable queue is full. Queue size is :"
                                        + Services.get().get(CallableQueueService.class).queueSize());
                    }
                    delayedCallables = null;
                    this.delay = 0;
                }
            }
            else {
                LOG.debug("Can't obtain lock, skipping");
            }
        }
        catch (Exception e) {
            LOG.error("Exception", e);
        }
        finally {
            if (lock != null) {
                lock.release();
                LOG.info("Released lock for [{0}]", CoordMaterializeTriggerService.class.getName());
            }
        }
    }
    /**
     * Recover coordinator jobs that should be materialized
     * @throws JPAExecutorException
     */
    private void runCoordJobMatLookup() throws JPAExecutorException {
        List<UpdateEntry> updateList = new ArrayList<UpdateEntry>();        XLog.Info.get().clear();
        XLog LOG = XLog.getLog(getClass());
        try {
            // get current date
            Date currDate = new Date(new Date().getTime() + lookupInterval * 1000);
            // get list of all jobs that have actions that should be materialized.
            int materializationLimit = ConfigurationService.getInt(CONF_MATERIALIZATION_SYSTEM_LIMIT);
            materializeCoordJobs(currDate, materializationLimit, LOG, updateList);
        }
        catch (Exception ex) {
            LOG.error("Exception while attempting to materialize coordinator jobs, {0}", ex.getMessage(), ex);
        }
        finally {
            BatchQueryExecutor.getInstance().executeBatchInsertUpdateDelete(null, updateList, null);
        }
    }
    private void materializeCoordJobs(Date currDate, int limit, XLog LOG, List<UpdateEntry> updateList)
            throws JPAExecutorException {
        try {
            List<CoordinatorJobBean> materializeJobs = CoordJobQueryExecutor.getInstance().getList(
                    CoordJobQuery.GET_COORD_JOBS_OLDER_FOR_MATERIALIZATION, currDate, limit);
            LOG.info("CoordMaterializeTriggerService - Curr Date= " + DateUtils.formatDateOozieTZ(currDate)
                    + ", Num jobs to materialize = " + materializeJobs.size());
            for (CoordinatorJobBean coordJob : materializeJobs) {
                Services.get().get(InstrumentationService.class).get()
                        .incr(INSTRUMENTATION_GROUP, INSTR_MAT_JOBS_COUNTER, 1);
                queueCallable(new CoordMaterializeTransitionXCommand(coordJob.getId(), materializationWindow));
                coordJob.setLastModifiedTime(new Date());
                updateList.add(new UpdateEntry<CoordJobQuery>(CoordJobQuery.UPDATE_COORD_JOB_LAST_MODIFIED_TIME,
                        coordJob));
            }
        }
        catch (JPAExecutorException jex) {
            LOG.warn("JPAExecutorException while attempting to materialize coordinator jobs", jex);
        }
    }
    /**
     * Adds callables to a list. If the number of callables in the list reaches {@link
     * CoordMaterializeTriggerService#CONF_CALLABLE_BATCH_SIZE}, the entire batch is queued and the callables list
     * is reset.
     *
     * @param callable the callable to queue. 
    */
    private void queueCallable(XCallable<Void> callable) {
        if (callables == null) {
            callables = new ArrayList<XCallable<Void>>();
        }
        callables.add(callable);
        if (callables.size() == ConfigurationService.getInt(CONF_CALLABLE_BATCH_SIZE)) {            boolean ret = Services.get().get(CallableQueueService.class).queueSerial(callables);
            if (ret == false) {
                XLog.getLog(getClass()).warn(
                        "Unable to queue the callables commands for CoordMaterializeTriggerRunnable. "
                                + "Most possibly command queue is full. Queue size is :"
                                + Services.get().get(CallableQueueService.class).queueSize());
            }
            callables = new ArrayList<XCallable<Void>>();
        }
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開(kāi)發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見(jiàn)模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,628評(píng)論 19 139
  • 飄飄落落進(jìn)入塵土 誰(shuí)能知道它的苦 失去庇護(hù)了的孩子 如同宇宙一粒珠 在那無(wú)限大的疆土 只有靠自己去征途 無(wú)論無(wú)何 ...
    希雨啾閱讀 147評(píng)論 0 1
  • 起因 由于強(qiáng)迫癥的緣故,所以我電腦dns都是用dnsspeeder來(lái)緩存dnsdnsspeeder大概放了10個(gè)d...
    莊msia閱讀 7,582評(píng)論 0 0
  • 早安。 早起吃完早飯,室友還在睡,窗簾拉著,宿舍漆黑安靜。 在空間看到她的說(shuō)說(shuō),一張合照,上面的男生卻不是你。我...
    薄暮涼夏閱讀 167評(píng)論 0 0
  • 空空法師,年有幾何,沒(méi)人知道。他反正沒(méi)有頭發(fā),也就沒(méi)有了白發(fā)。偏偏他又長(zhǎng)著一個(gè)沒(méi)有年齡的臉。真是讓人生氣。 他最喜...
    草小孟閱讀 999評(píng)論 0 0

友情鏈接更多精彩內(nèi)容