0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019 package org.apache.hive.service;
0020
0021 import org.apache.hadoop.hive.conf.HiveConf;
0022 import org.apache.hive.service.Service.STATE;
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034 public class BreakableService extends AbstractService {
0035 private boolean failOnInit;
0036 private boolean failOnStart;
0037 private boolean failOnStop;
0038 private final int[] counts = new int[4];
0039
0040 public BreakableService() {
0041 this(false, false, false);
0042 }
0043
0044 public BreakableService(boolean failOnInit,
0045 boolean failOnStart,
0046 boolean failOnStop) {
0047 super("BreakableService");
0048 this.failOnInit = failOnInit;
0049 this.failOnStart = failOnStart;
0050 this.failOnStop = failOnStop;
0051 inc(STATE.NOTINITED);
0052 }
0053
0054 private int convert(STATE state) {
0055 switch (state) {
0056 case NOTINITED: return 0;
0057 case INITED: return 1;
0058 case STARTED: return 2;
0059 case STOPPED: return 3;
0060 default: return 0;
0061 }
0062 }
0063
0064 private void inc(STATE state) {
0065 int index = convert(state);
0066 counts[index] ++;
0067 }
0068
0069 public int getCount(STATE state) {
0070 return counts[convert(state)];
0071 }
0072
0073 private void maybeFail(boolean fail, String action) {
0074 if (fail) {
0075 throw new BrokenLifecycleEvent(action);
0076 }
0077 }
0078
0079 @Override
0080 public void init(HiveConf conf) {
0081 inc(STATE.INITED);
0082 maybeFail(failOnInit, "init");
0083 super.init(conf);
0084 }
0085
0086 @Override
0087 public void start() {
0088 inc(STATE.STARTED);
0089 maybeFail(failOnStart, "start");
0090 super.start();
0091 }
0092
0093 @Override
0094 public void stop() {
0095 inc(STATE.STOPPED);
0096 maybeFail(failOnStop, "stop");
0097 super.stop();
0098 }
0099
0100 public void setFailOnInit(boolean failOnInit) {
0101 this.failOnInit = failOnInit;
0102 }
0103
0104 public void setFailOnStart(boolean failOnStart) {
0105 this.failOnStart = failOnStart;
0106 }
0107
0108 public void setFailOnStop(boolean failOnStop) {
0109 this.failOnStop = failOnStop;
0110 }
0111
0112
0113
0114
0115 public static class BrokenLifecycleEvent extends RuntimeException {
0116 BrokenLifecycleEvent(String action) {
0117 super("Lifecycle Failure during " + action);
0118 }
0119 }
0120
0121 }