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.upgrade;
20  
21  import java.rmi.RemoteException;
22  
23  import jgroup.core.arm.ReplicationManager;
24  import jgroup.core.registry.DependableRegistry;
25  import jgroup.core.registry.RegistryFactory;
26  import jgroup.relacs.config.AppConfig;
27  import jgroup.util.Abort;
28  
29  /**
30   *  Client program for accessing the <code>UpgradeManager</code>.
31   *
32   *  @author Hein Meling
33   *  @since Jgroup 1.2
34   */
35  public class UpgradeManagementClient {
36  
37  	public static void main(String argv[]) throws Exception {
38  
39  		/* parse params */
40  		boolean create_cmd = true;
41  		boolean remove_cmd = true;
42  		boolean wait_cmd = true;
43  		boolean test_cmd = false;
44  		boolean upgrade_cmd = false;
45  		boolean list_cmd = false;
46  		String cmd = "uwc";
47  		if (argv.length > 0 && argv[0] != null)
48  			cmd = argv[0];
49  		System.out.println("Got the following params: " + cmd);
50  
51  		if (cmd.indexOf('c') >= 0)
52  			create_cmd = true;
53  		else
54  			create_cmd = false;
55  		if (cmd.indexOf('r') >= 0)
56  			remove_cmd = true;
57  		else
58  			remove_cmd = false;
59  		if (cmd.indexOf('w') >= 0)
60  			wait_cmd = true;
61  		else
62  			wait_cmd = false;
63  		if (cmd.indexOf('t') >= 0)
64  			test_cmd = true;
65  		else
66  			test_cmd = false;
67  		if (cmd.indexOf('u') >= 0)
68  			upgrade_cmd = true;
69  		else
70  			upgrade_cmd = false;
71  		if (cmd.indexOf('l') >= 0)
72  			list_cmd = true;
73  		else
74  			list_cmd = false;
75  
76  		/*
77  		 * Obtain a proxy for the depedable registry running in the
78  		 * distributed system described in the config.xml file.
79  		 */
80  		DependableRegistry dregistry = RegistryFactory.getRegistry();
81  
82  		/*
83  		 * Retrieve a proxy for an object group implementing the
84  		 * ReplicationManager interface registered under the name
85  		 * "ReplicationManager".
86  		 */
87  		ReplicationManager replicaManager =
88  			(ReplicationManager) dregistry.lookup("Jgroup/ReplicationManager");
89  		System.out.println("Successful lookup: ReplicationManager");
90  
91  		AppConfig app = null;
92  //    AppConfig app = AppConfig.getApplication(jgroup.test.performance.upgrade.UpgradeServer.class);
93  
94  		if (create_cmd) {
95  			try {
96  				System.out.println("Creating group: " + app);
97  				replicaManager.createGroup(app);
98  			} catch (Exception exception) {
99  				throw new Abort("Failed to create the upgrade server group", exception);
100 			}
101 			System.out.println(
102 				"The group with upgradable servers is successfully created!");
103 		}
104 
105 		if (remove_cmd) {
106 			try {
107 				System.out.println("Removing group: " + app);
108 				replicaManager.removeGroup(app);
109 			} catch (Exception exception) {
110 				throw new Abort("Failed to remove the upgrade server group", exception);
111 			}
112 			System.out.println(
113 				"The group with upgradable servers is successfully removed!");
114 
115 		}
116 
117 
118 		if (wait_cmd) {
119 			System.out.println("Watining 30 sec to send an upgrade request");
120 
121 			/* Sleep for 10 seconds before upgrading */
122 			try {
123 				Thread.sleep(30000);
124 			} catch (InterruptedException ex) {
125 			}
126 		}
127 
128 		if (list_cmd) {
129 			try {
130 				DependableRegistry reg = RegistryFactory.getRegistry();
131 				String objs[] = reg.list();
132 				System.out.println("The following entries in DRegistry:");
133 				for(int i=0; i< objs.length; i++) 
134 				  System.out.println("[" +i+ "]: " + objs[i]);
135 				System.out.println("=============");				  
136 			} catch (RemoteException ex) {
137 				throw new Abort("Am upgrade server test failed ", ex);
138 			}
139 		}
140 
141 
142 		if (test_cmd) {
143 			try {
144 				System.out.println("Trying to getthe Registry ");
145 				DependableRegistry reg = RegistryFactory.getRegistry();
146 				System.out.println("Trying to lookup UpgradeServer");
147 //				SpeedTest u_serv =
148 //					(SpeedTest) reg.lookup("UpgradeServer");
149 //
150 //				System.out.println("Testing the upgrade server ");
151 //				String r_val = u_serv.mtest("Testing");
152 //				if (r_val.compareTo( "Testing" ) == 0)
153 //          System.out.println("Test ok.");
154 //				else
155 //          System.out.println("Test wrong: the server returns unexpcected value: " + r_val);				
156 			} catch (RemoteException ex) {
157 				throw new Abort("Am upgrade server test failed ", ex);
158 			}
159 		}
160 
161 
162 		if (upgrade_cmd) {
163 
164 			UpgradeManager upgrManager =
165 				(UpgradeManager) dregistry.lookup("Jgroup/UpgradeManager");
166 //			app = AppConfig.getApplication(jgroup.test.performance.upgrade.UpgradeServer2.class);
167 			long start = 0; 
168 			long utp = 0;
169 			try {
170 				System.out.println("Upgrading group: " + app);
171 				start = System.currentTimeMillis();
172 				upgrManager.upgradeGroup(app);
173 				utp = System.currentTimeMillis() - start;
174 				System.out.println("Server upgraded, UTP = " + utp);
175 			} catch (Exception exception) {
176 				utp = System.currentTimeMillis() - start;
177 				System.out.println("Server upgrade FAILED, exception thrown after time = " + utp);
178 				throw new Abort("Failed to create upgrserver", exception);
179 			}
180 		}
181 	}
182 
183 } // END UpgradeManagementClient