1 /*
2 * Copyright (c) 1998-2002 The Jgroup Team.
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU Lesser General Public License for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16 *
17 */
18
19 package jgroup.arm.recovery;
20
21 import jgroup.core.arm.DistributionScheme;
22 import jgroup.core.arm.RecoveryStrategy;
23 import jgroup.relacs.config.AppConfig;
24
25 import org.apache.log4j.Logger;
26
27
28 /**
29 * This implements only the core initialization of a recovery strategy,
30 * including also handling for the RM recoveree replica.
31 *
32 * @author Hein Meling
33 * @since Jgroup 1.2
34 */
35 public abstract class AbstractRecoveryStrategy
36 implements RecoveryStrategy
37 {
38
39 ////////////////////////////////////////////////////////////////////////////////////////////
40 // Logger
41 ////////////////////////////////////////////////////////////////////////////////////////////
42
43 /** Obtain logger for this class */
44 private static final Logger log = Logger.getLogger(AbstractRecoveryStrategy.class);
45
46
47 ////////////////////////////////////////////////////////////////////////////////////////////
48 // Fields
49 ////////////////////////////////////////////////////////////////////////////////////////////
50
51 /** The distribution scheme interface */
52 protected DistributionScheme distScheme;
53
54 /** The application associated with this recovery strategy instance */
55 protected AppConfig app;
56
57 /** The current delta redundancy (the number of missing replicas) */
58 protected int missing;
59
60
61 ////////////////////////////////////////////////////////////////////////////////////////////
62 // Constructors
63 ////////////////////////////////////////////////////////////////////////////////////////////
64
65 /**
66 * All recovery strategy implementations should implement a
67 * no-argument constructor. This pattern cannot be enforced by the
68 * RecoveryStratgy interface, but can be check while parsing.
69 */
70 public AbstractRecoveryStrategy() {}
71
72
73 ////////////////////////////////////////////////////////////////////////////////////////////
74 // Helper methods
75 ////////////////////////////////////////////////////////////////////////////////////////////
76
77 /**
78 * Returns the number of replicas that needs to be recovered to
79 * maintain the minimal redundancy level specified. If no recovery
80 * is needed, zero is returned.
81 */
82 protected int deltaRedundancy(AppConfig app)
83 {
84 int delta = app.getMinimalRedundancy() - app.getCurrentGroupSize();
85 if (log.isDebugEnabled())
86 log.debug("delta=" + delta);
87 return (delta > 0) ? delta : 0;
88 }
89
90
91 ////////////////////////////////////////////////////////////////////////////////////////////
92 // RecoveryStrategy interface methods
93 ////////////////////////////////////////////////////////////////////////////////////////////
94
95
96 /* (non-Javadoc)
97 * @see jgroup.core.arm.RecoveryStrategy#initialize(jgroup.core.arm.DistributionScheme, jgroup.relacs.config.AppConfig)
98 */
99 public void initialize(DistributionScheme distScheme, AppConfig app)
100 {
101 this.distScheme = distScheme;
102 this.app = app;
103 }
104
105 /* (non-Javadoc)
106 * @see jgroup.core.arm.RecoveryStrategy#needsRecovery()
107 */
108 public boolean needsRecovery()
109 {
110 return (deltaRedundancy(app) > 0);
111 }
112
113 /* (non-Javadoc)
114 * @see jgroup.core.arm.RecoveryStrategy#prepareRecovery()
115 */
116 public boolean prepareRecovery()
117 {
118 missing = deltaRedundancy(app);
119 if (log.isDebugEnabled())
120 log.debug("Missing " + missing + " replicas for: " + app);
121 return (app.getCurrentGroupSize() == 0);
122 }
123
124 } // END AbstractRecoveryStrategy