ml.shifu.guagua.master.HookMasterComputable Maven / Gradle / Ivy
/*
* Copyright [2013-2015] PayPal Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package ml.shifu.guagua.master;
import ml.shifu.guagua.io.Bytable;
import ml.shifu.guagua.master.MasterContext.MasterCompletionCallBack;
/**
* Common {@link MasterComputable} abstraction to define {@link #setup(MasterContext)} and
* {@link #cleanup(MasterContext)} hooks.
*
*
* For classes who extends {@link HookMasterComputable}, user can provide its own {@link #setup(MasterContext)} and
* {@link #cleanup(MasterContext)}. Guagua will make sure both will be called only once at the beginning and the end of
* whole process.
*
* @author Zhang David ([email protected])
*/
public abstract class HookMasterComputable implements
MasterComputable {
/**
* Setup method for master computation. This method will be called once before all iterations.
*
* @param context
* the worker context instance.
*/
protected void setup(MasterContext context) {
}
/*
* (non-Javadoc)
*
* @see ml.shifu.guagua.master.MasterComputable#compute(ml.shifu.guagua.master.MasterContext)
*/
@Override
public MASTER_RESULT compute(MasterContext context) {
if(context.isFirstIteration()) {
this.setup(context);
context.addCompletionCallBack(new MasterCompletionCallBack() {
@Override
public void callback(MasterContext context) {
cleanup(context);
}
});
}
return doCompute(context);
}
/**
* Real computation logic excluding some setup and cleanup hooks.
*
* @param context
* the worker context instance.
* @return the master result in that iteration.
*/
public abstract MASTER_RESULT doCompute(MasterContext context);
/**
* Cleanup method for master computation. This method will be called once after all iterations.
*
* @param context
* the worker context instance.
* @see GuaguaMasterService#run(ml.shifu.guagua.util.Progressable) for cleanup hook
*/
protected void cleanup(MasterContext context) {
}
}