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  
23  import jgroup.core.GroupManager;
24  import jgroup.core.JgroupException;
25  import jgroup.core.MemberId;
26  import jgroup.core.MembershipListener;
27  import jgroup.core.View;
28  import jgroup.test.performance.SpeedTest;
29  import jgroup.upgrade.UpgradeListener;
30  import jgroup.util.Abort;
31  
32  import org.apache.log4j.Logger;
33  
34  /**
35   *  Jgroup server for measuring the performance of GMI invocations and
36   *  server upgrade/recovery time.
37   *
38   *  The <code>UpgradeListener</code> is implemented to allow replicas of
39   *  <code>UpgradeServer</code> to be upgraded on the fly.  The magic is
40   *  dealt with using reflection on this class to construct a group
41   *  manager that contains also the upgrade layer.  This class simply
42   *  have to implement the <code>upgraded</code> method of the
43   *  <code>UpgradeListener</code> interface.
44   *
45   *  @author Alberto Montresor
46   *  @author Hein Meling
47   *  @since Jgroup 1.2
48   */
49  public class UpgradeServer
50    implements SpeedTest, MembershipListener, UpgradeListener
51  {
52  
53  /** Obtain logger for this class */
54    private static Logger log = Logger.getLogger(UpgradeServer.class);
55  
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    /** Last view received */
73    private View lastView;
74  
75    /** number of test invocations of each of the test methods: 1 - test(),
76     * 2 - mtest(), 3 - test(byte[]), 4 -mtest(byte[])
77     */
78    private int test[] = new int[4];
79    
80    /** 
81     * times of the first invocation of this method
82     */
83    private long testStart[] = new long[4];
84    private long testStop[] = new long[4];
85    ////////////////////////////////////////////////////////////////////////////////////////////
86    // Constructor
87    ////////////////////////////////////////////////////////////////////////////////////////////
88  
89    public UpgradeServer()
90      throws JgroupException
91    {
92      buf.append("UPG1------------------------------------------------------\n");
93      buf.append("Log time : Number of members : Time since last view change\n");
94  
95      GroupManager gm = GroupManager.getGroupManager(this);
96      log.info("UpgradeServer initialized");
97    }
98  
99  
100   ////////////////////////////////////////////////////////////////////////////////////////////
101   // Methods from SpeedTest
102   ////////////////////////////////////////////////////////////////////////////////////////////
103 
104   public void mtest()
105     throws RemoteException
106   {
107      if(test[1] == 0) testStart[1]=System.currentTimeMillis();
108      test[1]++;
109     testStop[1] = System.currentTimeMillis();
110   }
111 
112   public byte[] mtest(byte[] x)
113     throws RemoteException
114   {
115     if(test[3] == 0) testStart[3]=System.currentTimeMillis();
116     test[3]++;
117     testStop[3] = System.currentTimeMillis();
118     return x;
119   }
120 
121   public void vmtest(byte[] x)
122     throws RemoteException
123   {
124     if(test[3] == 0) testStart[3]=System.currentTimeMillis();
125     test[3]++;
126     testStop[3] = System.currentTimeMillis();
127   }
128 
129   public String mtest(String x)
130     throws RemoteException
131   {
132     if (log.isDebugEnabled()) log.debug("mtest: received: " + x);
133     if (log.isDebugEnabled()) log.debug("Last view: " + lastView);
134     log.info("Invocation number since last reset: [0] - " + test[0] +
135              ", [1] - " + test[1] +", [2] - " + test[2] +", [3] - " + test[3] );
136     
137     float f[] = new float[4];
138     long cTime= System.currentTimeMillis();
139     for(int i=0; i<4; i++){
140       if(test[i] >1) f[i] = (float)(test[i]-1)/(testStop[i]-testStart[i]);
141       test[i]=0;
142     }
143     log.info("Average invocation frequency: reset: [0] - " + f[0] +
144              ", [1] - " + f[1] +", [2] - " + f[2] +", [3] - " + f[3] );
145     
146     return x;
147   }
148 
149   public void test()
150     throws RemoteException
151   {
152     if(test[0] == 0) testStart[0]=System.currentTimeMillis();
153      test[0]++;
154     testStop[0] = System.currentTimeMillis();
155   }
156 
157   public byte[] test(byte[] x)
158     throws RemoteException
159   {
160     if(test[2] == 0) testStart[2]=System.currentTimeMillis();
161     test[2]++;
162     testStop[2] = System.currentTimeMillis();
163     return x;
164   }
165 
166   public String test(String x)
167     throws RemoteException
168   {
169     return x;
170   }
171 
172   /**
173    *  Shut down the server
174    */
175   public void shutdown()
176     throws RemoteException
177   {
178   }
179 /*    new Thread() {
180       public void run() {
181         try {
182           sleep(random.nextInt(MAX_SHUTDOWN_DELAY));
183         } catch (InterruptedException e) {
184         }
185         Abort.exit(0);
186       }
187     }.start();
188   }
189 
190 */
191   ////////////////////////////////////////////////////////////////////////////////////////////
192   // Methods from UpgradeListener
193   ////////////////////////////////////////////////////////////////////////////////////////////
194 
195   public void upgraded()
196   {
197     System.out.println("I've been upgraded!  Now I should leave the group and exit.");
198   }
199 
200 
201   ////////////////////////////////////////////////////////////////////////////////////////////
202   // Methods from MembershipListener
203   ////////////////////////////////////////////////////////////////////////////////////////////
204 
205   public void viewChange(View view)
206   {
207     long currentTime = System.currentTimeMillis();
208 
209     buf.append(currentTime);
210     buf.append(" : ");
211     buf.append(view.size());
212     buf.append(" : ");
213     buf.append((lastViewChange != 0) ? (currentTime - lastViewChange) : 0);
214     buf.append("\n");
215     buf.append(view);
216     buf.append("\n");
217     if (log.isDebugEnabled()) log.debug(buf.toString());
218 
219     /* Keep the time of this view change */
220     lastViewChange = currentTime;
221 
222     if (randomFailure && view.memberHasPosition(0, me)) {
223       try {
224         shutdown();
225       } catch (RemoteException e) {
226       }
227     }
228     lastView = view;
229   }
230 
231 
232   public void hasLeft() { 
233   	if (log.isDebugEnabled()) log.debug("has_left() called");
234   }
235 
236 
237   public void prepareChange() { 
238     if (log.isDebugEnabled()) log.debug("prepareChange() called");
239     }
240 
241 
242   ////////////////////////////////////////////////////////////////////////////////////////////
243   // Main method
244   ////////////////////////////////////////////////////////////////////////////////////////////
245 
246   public static void main(String[] argv)
247     throws JgroupException
248   {
249     for (int i = 0 ; i < argv.length ; i++) {
250       if (argv[i].equals("--randomFailure")) {
251         randomFailure = true;
252       } else {
253         System.err.println("Unknown option " + argv[i]);
254         Abort.usage("upgrserver [--randomFailure]");
255       }
256     }
257 
258     UpgradeServer server = new UpgradeServer();
259   }
260 
261 } // END UpgradeServer