Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/jbossmx/cluster/watchdog/mbean/WatchdogCorrectiveAction.java


1   /**
2    * JBoss, the OpenSource EJB server
3    *
4    * Distributable under LGPL license Version 2.1, February 1999.
5    * See terms of license at gnu.org.
6    */
7   
8   package org.jbossmx.cluster.watchdog.mbean;
9   
10  import org.jbossmx.cluster.watchdog.HermesException;
11  
12  /**
13   * This class encapsulates the sequences of corrective actions the Watchdog should take
14   * when the StartableMBean fails.
15   *
16   * @author Stacy Curl
17   */
18  public class WatchdogCorrectiveAction
19  {
20      /** Constant identifying an Invoke Restart Method Corrective Action */
21      public static final int INVOKE_RESTART_METHOD = 0;
22      /** Constant identifying a Reregister MBean Corrective Action */
23      public static final int REREGISTER_MBEAN = 1;
24      /** Constant identifying a Restart Agent Corrective Action */
25      public static final int RESTART_AGENT = 2;
26      /** Constant identifying a Restart Machine Corrective Action */
27      public static final int RESTART_MACHINE = 3;
28  
29  //    public static final int MAX_INVOKE_START_METHOD = 2;
30  //    public static final int MAX_REREGISTER_MBEAN = 2;
31  //    public static final int MAX_RESTART_AGENT = 2;
32  //    public static final int MAX_RESTART_MACHINE = 1;
33  
34      /**
35       * Constructor for WatchdogCorrectiveAction
36       *
37       * @param    watchdogInterface the Watchdog that is watching the thing this object will correct. mmm
38       * @throws HermesException if INVOKE_RESTART_METHOD is an invalid Corrective Action
39       * @see validateCorrectiveAction
40       */
41      public WatchdogCorrectiveAction(WatchdogMBean watchdogInterface) throws HermesException
42      {
43          this(watchdogInterface, INVOKE_RESTART_METHOD);
44      }
45  
46      /**
47       * Constructor for WatchdogCorrectiveAction
48       * @param    watchdogInterface the Watchdog that is watching the thing this object will correct. mmm
49       * @param    initialCorrectiveAction the initial Corrective Action to take
50       * @throws HermesException if <code>initialCorrectiveAction</code> is an invalid Corrective
51       * Action
52       * @see validateCorrectiveAction
53       */
54      public WatchdogCorrectiveAction(WatchdogMBean watchdogInterface, int initialCorrectiveAction)
55          throws HermesException
56      {
57          setWatchdogInterface(watchdogInterface);
58          setInitialCorrectiveAction(getNextAvailableCorrectiveAction(initialCorrectiveAction));
59          setCurrentCorrectiveAction(getNextAvailableCorrectiveAction(initialCorrectiveAction));
60          setWorstCorrectiveAction(getCurrentCorrectiveAction());
61      }
62  
63      /**
64       * Gets the current Corrective Action
65       *
66       * @return the current Corrective Action
67       */
68      public int getCurrentCorrectiveAction()
69      {
70          return m_currentCorrectiveAction;
71      }
72  
73      /**
74       * Gets the worst current CorrectiveAction so far
75       *
76       * @return the worst current CorrectiveAction so far
77       */
78      public int getWorstCorrectiveAction()
79      {
80          return m_worstCorrectiveAction;
81      }
82  
83      /**
84       * Set whether the current Corrective Action succeeded
85       *
86       * @param    succeeded whether the current Corrective Action succeeded
87       * @throws HermesException if the current Corrective Action is updated and the new Corrective
88       * Action is invalid
89       * @see validateCorrectiveAction
90       */
91      public void setCorrectiveActionSucceeded(final boolean succeeded) throws HermesException
92      {
93          if(succeeded)
94          {
95              updateCorrectiveAction(getInitialCorrectiveAction());
96          }
97          else if(hasCountReachedMax())
98          {
99              increaseCorrectiveActionSeverity();
100         }
101     }
102 
103     /**
104      * Update the current Corrective Action, also updates the worst current Corrective Action.
105      *
106      * @param    correctiveAction the new current CorrectiveAction
107      * @throws HermesException if the current Corrective Action is updated and the new Corrective
108      * Action is invalid
109      * @see validateCorrectiveAction
110      */
111     private void updateCorrectiveAction(final int correctiveAction) throws HermesException
112     {
113         if(getCurrentCorrectiveAction() != correctiveAction)
114         {
115             setCount(0);
116         }
117 
118         setCurrentCorrectiveAction(correctiveAction);
119         updateWorstCorrectiveAction();
120 
121         setCount(getCount() + 1);
122     }
123 
124     /**
125      * Update the current Corrective Action to point to the next Corrective Action that is
126      * applicable.
127      *
128      * @throws HermesException if the the new Corrective Action is invalid
129      * @see validateCorrectiveAction
130      */
131     private void increaseCorrectiveActionSeverity() throws HermesException
132     {
133         setCurrentCorrectiveAction(getNextAvailableCorrectiveAction(getCurrentCorrectiveAction()
134             + 1));
135 
136 //        final int nextAvailableCorrectiveAction = getNextAvailableCorrectiveAction(
137 //            getCurrentCorrectiveAction() + 1);
138 //
139 //        if(nextAvailableCorrectiveAction != -1)
140 //        {
141 //            setCurrentCorrectiveAction(nextAvailableCorrectiveAction);
142 //        }
143 //        else
144 //        {
145 //            throw new HermesException(
146 //                "WatchdogCorrectiveAction.increaseCorrectiveActionSeverity - no more corrective actions available",
147 //                null);
148 //        }
149     }
150 
151     /**
152      * Gets the next available Corrective Action that is applicable, returns -1 if there are no
153      * Corrective Actions >= <code>startingCorrectiveAction</code>
154      *
155      * @param    startingCorrectiveAction the Corrective Action to start at
156      *
157      * @return the next available Corrective Action that is applicable
158      */
159     private int getNextAvailableCorrectiveAction(final int startingCorrectiveAction)
160     {
161         int nextAvailableCorrectiveAction = startingCorrectiveAction;
162 
163         while((getNumTimesToAttemptCorrectiveAction(nextAvailableCorrectiveAction) == 0)
164             && (nextAvailableCorrectiveAction <= RESTART_MACHINE))
165         {
166             ++nextAvailableCorrectiveAction;
167         }
168 
169         if(nextAvailableCorrectiveAction > RESTART_MACHINE)
170         {
171             nextAvailableCorrectiveAction = -1;
172         }
173 
174         return nextAvailableCorrectiveAction;
175     }
176 
177 //    /**
178 //     * I don't understand this code, the <code>getNextAvailableCorrectiveAction</code> should be
179 //     * enough, this method should be deleted.
180 //     *
181 //     * @param    initialCorrectiveAction
182 //     *
183 //     * @return
184 //     */
185 //    private int getFirstApplicableCorrectiveAction(int initialCorrectiveAction)
186 //    {
187 //        int currentFirstApplicableCorrectiveAction = initialCorrectiveAction;
188 //
189 //        while((getNumTimesToAttemptCorrectiveAction(currentFirstApplicableCorrectiveAction) == 0)
190 //            && (currentFirstApplicableCorrectiveAction != RESTART_MACHINE))
191 //        {
192 //            ++currentFirstApplicableCorrectiveAction;
193 //        }
194 //
195 //        return currentFirstApplicableCorrectiveAction;
196 //    }
197 
198     /**
199      * Gets the number of times a Corrective Action can be attempted
200      *
201      * @param    correctiveAction the Corrective Action
202      *
203      * @return the number of times a Corrective Action can be attempted
204      */
205     private int getNumTimesToAttemptCorrectiveAction(final int correctiveAction)
206     {
207         switch(correctiveAction)
208         {
209             case INVOKE_RESTART_METHOD:
210                 return getWatchdogInterface().getNumTimesToAttemptMBeanRestart();
211 
212             case REREGISTER_MBEAN:
213                 return getWatchdogInterface().getNumTimesToAttemptMBeanReregister();
214 
215             case RESTART_AGENT:
216                 return getWatchdogInterface().getNumTimesToAttemptAgentRestart();
217 
218             default:    //RESTART_MACHINE:
219                 return getWatchdogInterface().getNumTimesToAttemptMachineRestart();
220         }
221     }
222 
223     /**
224      * Updates the worst corrective action so that it is at least equal to the current corrective
225      * action.
226      * @throws HermesException if the the new worst Corrective Action is invalid
227      * @see validateCorrectiveAction
228      */
229     private void updateWorstCorrectiveAction() throws HermesException
230     {
231         if(getCurrentCorrectiveAction() > getWorstCorrectiveAction())
232         {
233             setWorstCorrectiveAction(getCurrentCorrectiveAction());
234         }
235     }
236 
237     /**
238      * Gets whether the current corrective action has reached the maximum number of times it can
239      * be attempted.
240      *
241      * @return whether the current corrective action can be attempted again.
242      */
243     private boolean hasCountReachedMax()
244     {
245         return (getCount() >= getNumTimesToAttemptCorrectiveAction(getCurrentCorrectiveAction()));
246     }
247 
248     /**
249      * Gets the WatchdogMBean which is watching the MBean that this Corrective Action applied to.
250      *
251      * @return the WatchdogMBean which is watching the MBean that this Corrective Action applied to.
252      */
253     private WatchdogMBean getWatchdogInterface()
254     {
255         return m_watchdogInterface;
256     }
257 
258     /**
259      * Sets the WatchdogMBean which is watching the MBean that this Corrective Action applied to.
260      *
261      * @param    watchdogInterface the WatchdogMBean which is watching the MBean that this
262      * Corrective Action applied to.
263      */
264     private void setWatchdogInterface(WatchdogMBean watchdogInterface)
265     {
266         m_watchdogInterface = watchdogInterface;
267     }
268 
269     /**
270      * Gets the initial Corrective Action
271      *
272      * @return the initial Corrective Action
273      */
274     private int getInitialCorrectiveAction()
275     {
276         return m_initialCorrectiveAction;
277     }
278 
279     /**
280      * Sets the initial Corrective Action
281      *
282      * @param    initialCorrectiveAction the initial Corrective Action
283      * @throws HermesException if <code>initialCorrectiveAction</code> is an invalid Corrective Action
284      * @see validateCorrectiveAction
285      */
286     private void setInitialCorrectiveAction(final int initialCorrectiveAction)
287         throws HermesException
288     {
289         validateCorrectiveAction(initialCorrectiveAction);
290 
291         m_initialCorrectiveAction = initialCorrectiveAction;
292 //        m_initialCorrectiveAction = getFirstApplicableCorrectiveAction(initialCorrectiveAction);
293     }
294 
295     /**
296      * @param    currentCorrectiveAction
297      * @throws HermesException if <code>currentCorrectiveAction</code> is an invalid Corrective
298      * Action
299      * @see validateCorrectiveAction
300      */
301     private void setCurrentCorrectiveAction(int currentCorrectiveAction) throws HermesException
302     {
303         validateCorrectiveAction(currentCorrectiveAction);
304 
305         m_currentCorrectiveAction = currentCorrectiveAction;
306 //        m_currentCorrectiveAction = getFirstApplicableCorrectiveAction(currentCorrectiveAction);
307     }
308 
309     /**
310      * @param    worstCorrectiveAction
311      * @throws HermesException if <code>worstCorrectiveAction</code> is an invalid Corrective Action
312      * @see validateCorrectiveAction
313      */
314     private void setWorstCorrectiveAction(int worstCorrectiveAction) throws HermesException
315     {
316         validateCorrectiveAction(worstCorrectiveAction);
317 
318         m_worstCorrectiveAction = worstCorrectiveAction;
319     }
320 
321     /**
322      * Validates a Corrective Action. A Corrective Action is valid if it is >= INVOKE_RESTART_METHOD
323      * and <= RESTART_MACHINE.
324      *
325      * @param    correctiveAction the Corrective Action to validate
326      * @throws HermesException if <code>correctiveAction</code> is invalid.
327      */
328     private void validateCorrectiveAction(final int correctiveAction) throws HermesException
329     {
330         if((correctiveAction < INVOKE_RESTART_METHOD) || (correctiveAction > RESTART_MACHINE))
331         {
332             throw new HermesException("Invalid Corrective Action", null);
333         }
334     }
335 
336     /**
337      * Sets the number of times that the current Corrective Action has been applied
338      *
339      * @param    count the number of times that the current Corrective Action has been applied
340      */
341     private void setCount(int count)
342     {
343         m_count = count;
344     }
345 
346     /**
347      * Gets the number of times that the current Corrective Action has been applied
348      *
349      * @return the number of times that the current Corrective Action has been applied
350      */
351     private int getCount()
352     {
353         return m_count;
354     }
355 
356     /** The WatchdogMBean which is watching the MBean that this Corrective Action applied to */
357     private WatchdogMBean m_watchdogInterface;
358     /** The initial Corrective Action */
359     private int m_initialCorrectiveAction;
360     /** The current Corrective Action */
361     private int m_currentCorrectiveAction;
362     /** The number of times that the current Corrective Action has been applied */
363     private int m_count;
364     /** The worst Corrective Action */
365     private int m_worstCorrectiveAction;
366 }