private MasterContext<MASTER_RESULT, WORKER_RESULT> buildContext() { if(getContext() != null) { return getContext(); } this.context = new MasterContext<MASTER_RESULT, WORKER_RESULT>(getTotalIteration(), getWorkers(), getProps(), getAppId(), getContainerId(), getMasterResultClassName(), getWorkerResultClassName(), getMinWorkersRatio(), getMinWorkersTimeOut()); return getContext(); }
@Override protected void initialize(Properties props) { super.initialize(props); initIterResults(props); }
@Override public MASTER_RESULT compute(MasterContext<MASTER_RESULT, WORKER_RESULT> context) { if(context.isFirstIteration()) { this.setup(context); context.addCompletionCallBack(new MasterCompletionCallBack<MASTER_RESULT, WORKER_RESULT>() { @Override public void callback(MasterContext<MASTER_RESULT, WORKER_RESULT> context) { cleanup(context); } }); } return doCompute(context); }
@Override public void preIteration(MasterContext<MASTER_RESULT, WORKER_RESULT> context) { this.iterGCStartTime = computeGCTime(); LOG.info("Application {} container {} iteration {} starts master computation.", context.getAppId(), context.getContainerId(), context.getCurrentIteration()); }
@Override public void doExecute() throws KeeperException, InterruptedException { String appId = context.getAppId(); int lastIteration = context.getCurrentIteration(); final String appMasterNode = getCurrentMasterNode(appId, lastIteration).toString(); final String appMasterSplitNode = getCurrentMasterSplitNode(appId, lastIteration).toString(); // actually, this is last master result for fault recovery setMasterResult(context, appMasterNode, appMasterSplitNode); } }.execute();
@Override public void postIteration(MasterContext<MASTER_RESULT, WORKER_RESULT> context) { LOG.debug("post application:{} container:{} iteration:{}, context:{}", context.getAppId(), context.getContainerId(), context.getCurrentIteration(), context); }
protected void setMasterResult(final MasterContext<MASTER_RESULT, WORKER_RESULT> context, final String appMasterNode, final String appMasterSplitNode) throws KeeperException, InterruptedException { if(context.getCurrentIteration() == GuaguaConstants.GUAGUA_INIT_STEP) { return; } byte[] data = getBytesFromZNode(appMasterNode, appMasterSplitNode); if(data != null && data.length > 0) { MASTER_RESULT lastMasterResult = getMasterSerializer().bytesToObject(data, context.getMasterResultClassName()); context.setMasterResult(lastMasterResult); } }
@Override public void preIteration(MasterContext<MASTER_RESULT, WORKER_RESULT> context) { LOG.debug("pre application:{} container:{} iteration:{}, context:{}", context.getAppId(), context.getCurrentIteration(), context); }
@Override public void postApplication(MasterContext<MASTER_RESULT, WORKER_RESULT> context) { LOG.info("Application {} container {} ends with {}ms gc time.", context.getAppId(), context.getContainerId(), TimeUnit.NANOSECONDS.toMillis(computeGCTime() - this.appGCStartTime)); }
/** * Whether is in first iteration, default first iteration is 1. */ public boolean isFirstIteration() { return getCurrentIteration() == GuaguaConstants.GUAGUA_FIRST_ITERATION; }
@Override public void start() { // all services are included in each intecepter. MasterContext<MASTER_RESULT, WORKER_RESULT> context = buildContext(); // iteration 0 is to wait for all workers are available. context.setCurrentIteration(GuaguaConstants.GUAGUA_INIT_STEP); for(MasterInterceptor<MASTER_RESULT, WORKER_RESULT> masterInterceptor: getMasterInterceptors()) { try { masterInterceptor.preApplication(context); } catch (Throwable e) { LOG.error("Error in master interceptors starting.", e); throw new GuaguaRuntimeException(e); } } }
@Override public MASTER_RESULT call() throws Exception { return masterComputable.compute(context); } }).get(duration, unit);
@Override public void callback(MasterContext<MASTER_RESULT, WORKER_RESULT> context) { cleanup(context); } });
@Override public void preApplication(MasterContext<MASTER_RESULT, WORKER_RESULT> context) { this.appGCStartTime = computeGCTime(); }
@Override public MasterInterceptor<MASTER_RESULT, WORKER_RESULT> createMasterCoordinator() { return new SyncMasterCoordinator<MASTER_RESULT, WORKER_RESULT>(); }
@Override public MasterInterceptor<MASTER_RESULT, WORKER_RESULT> createMasterCoordinator() { return new AsyncMasterCoordinator<MASTER_RESULT, WORKER_RESULT>(); }
@Override public WorkerResultWrapper combine(WorkerResultWrapper wrw) { if(isWorkerCombinable()) { @SuppressWarnings("unchecked") Combinable<Bytable> cwr = ((Combinable<Bytable>) workerResult); cwr.combine(wrw.workerResult); } return this; }
@Override public void postIteration(MasterContext<MASTER_RESULT, WORKER_RESULT> context) { LOG.info("Application {} container {} iteration {} ends with {}ms gc time.", context.getAppId(), context.getContainerId(), context.getCurrentIteration(), TimeUnit.NANOSECONDS.toMillis(computeGCTime() - this.iterGCStartTime)); }
/** * Whether is in initiate iteration, default initiate iteration is 1. */ public boolean isInitIteration() { return getCurrentIteration() == GuaguaConstants.GUAGUA_INIT_STEP; }