Back to home page

OSCL-LXR

 
 

    


0001 /**
0002  * Licensed to the Apache Software Foundation (ASF) under one
0003  * or more contributor license agreements.  See the NOTICE file
0004  * distributed with this work for additional information
0005  * regarding copyright ownership.  The ASF licenses this file
0006  * to you under the Apache License, Version 2.0 (the
0007  * "License"); you may not use this file except in compliance
0008  * with the License.  You may obtain a copy of the License at
0009  *
0010  *     http://www.apache.org/licenses/LICENSE-2.0
0011  *
0012  * Unless required by applicable law or agreed to in writing, software
0013  * distributed under the License is distributed on an "AS IS" BASIS,
0014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0015  * See the License for the specific language governing permissions and
0016  * limitations under the License.
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  * This is a service that can be configured to break on any of the lifecycle
0026  * events, so test the failure handling of other parts of the service
0027  * infrastructure.
0028  *
0029  * It retains a counter to the number of times each entry point is called -
0030  * these counters are incremented before the exceptions are raised and
0031  * before the superclass state methods are invoked.
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    * The exception explicitly raised on a failure
0114    */
0115   public static class BrokenLifecycleEvent extends RuntimeException {
0116     BrokenLifecycleEvent(String action) {
0117       super("Lifecycle Failure during " + action);
0118     }
0119   }
0120 
0121 }