DownGradeStateMachine.java 1.97 KB
package com.yoho.search.common.downgrade;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Created by xy on 2016/8/24.
 */
public class DownGradeStateMachine {

	private static final Logger logger = LoggerFactory.getLogger("DOWNGRADE");

	/**
	 * 直接走全降级流程
	 * 
	 * @return
	 */
	public static DownGradeLevelEnum getDirectDownGradeLevel() {
		logger.info("direct change to DOWN_GRADE_LEVEL_ON ");
		return DownGradeLevelEnum.DOWN_GRADE_LEVEL_ON;
	}

	/**
	 *
	 * @param downGradeLevelEnum
	 *            降级state状态
	 * @param downgrade
	 *            true 降级通道;false 恢复通道
	 * @return
	 */
	public static DownGradeLevelEnum changeDownGradeLevel(DownGradeLevelEnum downGradeLevelEnum, boolean downgrade) {
		logger.info("try changeDownGradeLevel , current is [{}], operation is [{}]", downGradeLevelEnum.name(), downgrade ? "downgrade" : "revert");
		switch (downGradeLevelEnum) {
		case DOWN_GRADE_LEVEL_OFF:
			if (downgrade) {
				downGradeLevelEnum = DownGradeLevelEnum.DOWN_GRADE_LEVEL_HALF;
				logger.info("DownGradeLevel change from [{}] to [{}]", DownGradeLevelEnum.DOWN_GRADE_LEVEL_OFF, DownGradeLevelEnum.DOWN_GRADE_LEVEL_HALF);
			}
			break;
		case DOWN_GRADE_LEVEL_HALF:
			if (downgrade) {
				downGradeLevelEnum = DownGradeLevelEnum.DOWN_GRADE_LEVEL_ON;
				logger.info("DownGradeLevel change from [{}] to [{}]", DownGradeLevelEnum.DOWN_GRADE_LEVEL_HALF, DownGradeLevelEnum.DOWN_GRADE_LEVEL_ON);
			} else {
				downGradeLevelEnum = DownGradeLevelEnum.DOWN_GRADE_LEVEL_OFF;
				logger.info("DownGradeLevel change from [{}] to [{}]", DownGradeLevelEnum.DOWN_GRADE_LEVEL_HALF, DownGradeLevelEnum.DOWN_GRADE_LEVEL_OFF);
			}
			break;
		case DOWN_GRADE_LEVEL_ON:
			if (!downgrade) {
				downGradeLevelEnum = DownGradeLevelEnum.DOWN_GRADE_LEVEL_HALF;
				logger.info("DownGradeLevel change from [{}] to [{}]", DownGradeLevelEnum.DOWN_GRADE_LEVEL_ON, DownGradeLevelEnum.DOWN_GRADE_LEVEL_HALF);
			}
			break;
		}
		return downGradeLevelEnum;
	}

}