View Javadoc

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.test.performance.upgrade;
20  
21  import java.rmi.RemoteException;
22  import java.util.Random;
23  
24  import jgroup.core.GroupManager;
25  import jgroup.core.JgroupException;
26  import jgroup.core.MemberId;
27  import jgroup.core.MembershipListener;
28  import jgroup.core.View;
29  import jgroup.test.performance.SpeedTest;
30  import jgroup.upgrade.UpgradeListener;
31  import jgroup.util.Abort;
32  
33  import org.apache.log4j.Logger;
34  
35  /**
36   *  Jgroup server for measuring the performance of GMI invocations and
37   *  server upgrade/recovery time.
38   *
39   *  The <code>UpgradeListener</code> is implemented to allow replicas of
40   *  <code>UpgradeServer</code> to be upgraded on the fly.  The magic is
41   *  dealt with using reflection on this class to construct a group
42   *  manager that contains also the upgrade layer.  This class simply
43   *  have to implement the <code>upgraded</code> method of the
44   *  <code>UpgradeListener</code> interface.
45   *
46   *  @author Alberto Montresor
47   *  @author Hein Meling
48   *  @since Jgroup 1.2
49   */
50  public class UpgradeServer2
51    implements SpeedTest, MembershipListener, UpgradeListener
52  {
53  
54  /** Obtain logger for this class */
55    private static Logger log = Logger.getLogger(UpgradeServer.class);
56    ////////////////////////////////////////////////////////////////////////////////////////////
57    // Fields (non-shared state)
58    ////////////////////////////////////////////////////////////////////////////////////////////
59  
60    /** Keep the time of the last view change */
61    private long lastViewChange = 0;
62  
63    /** The member identifier of this replica. */
64    private MemberId me;
65  
66    /** Buffer for storing log output */
67    private StringBuilder buf = new StringBuilder();
68  
69    /** True indicates that this replica may fail at some random point in time */
70    private static boolean randomFailure = false;
71  
72    /** Random generator */
73    private static Random random = new Random();
74  
75    /** Max delay before shutdown, given in milliseconds */
76    private static final int MAX_SHUTDOWN_DELAY = 100000;
77  
78  
79    ////////////////////////////////////////////////////////////////////////////////////////////
80    // Constructor
81    ////////////////////////////////////////////////////////////////////////////////////////////
82  
83    public UpgradeServer2()
84      throws JgroupException
85    {
86      buf.append("UPG2------------------------------------------------------\n");
87      buf.append("Log time : Number of members : Time since last view change\n");
88  
89      GroupManager gm = GroupManager.getGroupManager(this);
90      if (log.isDebugEnabled()) log.debug("UpgradeServer2 initialized");
91    }
92  
93  
94    ////////////////////////////////////////////////////////////////////////////////////////////
95    // Methods from SpeedTest
96    ////////////////////////////////////////////////////////////////////////////////////////////
97  
98    public void mtest()
99      throws RemoteException
100   {
101     if (log.isDebugEnabled()) log.debug("UpgradeServer2 mtest");
102   }
103 
104   public byte[] mtest(byte[] x)
105     throws RemoteException
106   {
107     return x;
108   }
109 
110   public void vmtest(byte[] x)
111     throws RemoteException
112   {
113     return;
114   }
115 
116   public String mtest(String x)
117     throws RemoteException
118   {
119     if (log.isDebugEnabled()) log.debug("mtest, mcast: received: " + x);
120     return x;
121   }
122 
123   public void test()
124     throws RemoteException
125   {
126   }
127 
128   public byte[] test(byte[] x)
129     throws RemoteException
130   {
131     return x;
132   }
133 
134   public String test(String x)
135     throws RemoteException
136   {
137     if (log.isDebugEnabled()) log.debug("mtest: received: " + x);
138     return x;
139   }
140 
141   /**
142    *  Shut down the server
143    */
144   public void shutdown()
145     throws RemoteException
146   {
147     new Thread() {
148       public void run() {
149         try {
150           sleep(random.nextInt(MAX_SHUTDOWN_DELAY));
151         } catch (InterruptedException e) {
152         }
153         Abort.exit(0);
154       }
155     }.start();
156   }
157 
158 
159   ////////////////////////////////////////////////////////////////////////////////////////////
160   // Methods from UpgradeListener
161   ////////////////////////////////////////////////////////////////////////////////////////////
162 
163   public void upgraded()
164   {
165     if (log.isDebugEnabled()) log.debug("UPG2: I've been upgraded!  Now I should leave the group and exit.");
166   }
167 
168 
169   ////////////////////////////////////////////////////////////////////////////////////////////
170   // Methods from MembershipListener
171   ////////////////////////////////////////////////////////////////////////////////////////////
172 
173   public void viewChange(View view)
174   {
175     long currentTime = System.currentTimeMillis();
176 
177     buf.append(currentTime);
178     buf.append(" : ");
179     buf.append(view.size());
180     buf.append(" : ");
181     buf.append((lastViewChange != 0) ? (currentTime - lastViewChange) : 0);
182     buf.append("\n");
183     buf.append(view);
184     buf.append("\n");
185     if (log.isDebugEnabled()) log.debug(buf.toString());
186 
187     /* Keep the time of this view change */
188     lastViewChange = currentTime;
189 
190     if (randomFailure && view.memberHasPosition(0, me)) {
191       try {
192         shutdown();
193       } catch (RemoteException e) {
194       }
195     }
196   }
197 
198 
199   public void hasLeft() { }
200 
201 
202   public void prepareChange() { }
203 
204 
205   ////////////////////////////////////////////////////////////////////////////////////////////
206   // Main method
207   ////////////////////////////////////////////////////////////////////////////////////////////
208 
209   public static void main(String[] argv)
210     throws Exception
211   {
212     for (int i = 0 ; i < argv.length ; i++) {
213       if (argv[i].equals("--randomFailure")) {
214         randomFailure = true;
215       } else {
216         System.err.println("Unknown option " + argv[i]);
217         Abort.usage("upgrserver2 [--randomFailure]");
218       }
219     }
220 
221     UpgradeServer2 server = new UpgradeServer2();
222   }
223 
224 } // END UpgradeServer2