Oozie-Service-CoordMaterializeTriggerService

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

// 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ā)機制:

 1、啟動一個定時執(zhí)行器,每隔 schedulingInterval 時間運行一次 CoordMaterializeTriggerRunnable;
 2、查詢CoordinatorJobBean根據(jù)時間和條數(shù)限制獲取本次需要實例化的CoordinatorJobBean;
    技巧: 在CoordinatorJobBean中有個字段 nextMaterializedTimestamp 表明,在這個時間點之前的實例已經(jīng)存在,為了盡可能的讓任務(wù)的實例盡可能在觸發(fā)之前
    實例就產(chǎn)生,而不是事后產(chǎn)生,在查詢CoordinatorJobBean使用的時候不是使用當(dāng)前時間,而是使用未來的一個時間: new Date().getTime() + lookupInterval * 1000
 3、異步去實例化 CoordinatorJobBean 下的 action;
 4、在實例化action的時候,生成的是窗口期內(nèi)的一批實例,在類 CoordMaterializeTransitionXCommand中實現(xiàn);
 5、loadState():加載 CoordinatorJobBean 信息,計算這次要實例化的時間窗口: startMatdTime 和 endMatdTime;
 6、materialize():根據(jù)不同的計算方式(cron、自定義)來在這個時間窗口內(nèi)循環(huán)的產(chǎn)生action的實例;
 7、更新  CoordinatorJobBean 狀態(tài),記錄 endMatdTime、lastActionNumber(時間累加)、狀態(tài)置為running狀態(tài)、如果jobEndTime< endMatdTime
    說明這個 CoordinatorJobBean 實例化工作已經(jīng)結(jié)束,標(biāo)記 job.setDoneMaterialization();
 8、performWrites():更新數(shù)據(jù)庫
 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)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,002評論 6 542
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,400評論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,136評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,714評論 1 317
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 72,452評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,818評論 1 328
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,812評論 3 446
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,997評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,552評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 41,292評論 3 358
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 43,510評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,035評論 5 363
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 44,721評論 3 348
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,121評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,429評論 1 294
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,235評論 3 398
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 48,480評論 2 379

推薦閱讀更多精彩內(nèi)容

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