KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jgroups > tests > LargeState


1 // $Id: LargeState.java,v 1.9 2004/07/05 14:15:11 belaban Exp $
2

3
4 package org.jgroups.tests;
5
6
7 import org.jgroups.*;
8
9
10 /**
11  * Tests transfer of large states. Start first instance with -provider flag and -size flag (default = 1MB).
12  * The start second instance without these flags: it should acquire the state from the first instance. Possibly
13  * tracing should be turned on for FRAG to see the fragmentation taking place, e.g.:
14  * <pre>
15  * trace1=FRAG DEBUG STDOUT
16  * </pre><br>
17  * Note that because fragmentation might generate a lot of small fragments at basically the same time (e.g. size1MB,
18  * FRAG.frag-size=4096 generates a lot of fragments), the send buffer of the unicast socket in UDP might be overloaded,
19  * causing it to drop some packets (default size is 8096 bytes). Therefore the send (and receive) buffers for the unicast
20  * socket have been increased (see ucast_send_buf_size and ucast_recv_buf_size below).<p>
21  * If we didn't do this, we would have some retransmission, slowing the state transfer down.
22  *
23  * @author Bela Ban Dec 13 2001
24  */

25 public class LargeState {
26     Channel channel;
27     byte[] state=null;
28     Thread JavaDoc getter=null;
29     boolean rc=false;
30     String JavaDoc props;
31     long start, stop;
32     boolean provider=true;
33
34
35     public void start(boolean provider, long size, String JavaDoc props) throws Exception JavaDoc {
36         this.provider=provider;
37         channel=new JChannel(props);
38         channel.setOpt(Channel.GET_STATE_EVENTS, Boolean.TRUE);
39         channel.connect("TestChannel");
40
41         if(provider) {
42
43 // channel.send(new Message(null, null, "Hello1"));
44
// channel.send(new Message(null, null, "Hello2"));
45
// channel.send(new Message(null, null, "Hello3"));
46
// channel.send(new Message(null, null, "Hello4"));
47

48             System.out.println("Creating state of " + size + " bytes");
49             state=createLargeState(size);
50             System.out.println("Done. Waiting for other members to join and fetch large state");
51         }
52         else {
53
54 // channel.send(null, null, "Hello5");
55
// channel.send(null, null, "Hello6");
56
// channel.send(null, null, "Hello7");
57
// channel.send(null, null, "Hello8");
58

59             System.out.println("Getting state");
60             start=System.currentTimeMillis();
61             rc=channel.getState(null, 20000);
62             System.out.println("getState(), rc=" + rc);
63         }
64
65         mainLoop();
66         channel.close();
67
68     }
69
70
71     public void mainLoop() {
72         Object JavaDoc ret;
73
74         try {
75             while(true) {
76                 ret=channel.receive(0);
77
78                 if(ret instanceof Message) {
79                     System.out.println("-- received msg " + ((Message)ret).getObject() + " from " +
80                             ((Message)ret).getSrc());
81                 }
82                 else if(ret instanceof GetStateEvent) {
83                     System.out.println("--> returned state: " + ret);
84                     channel.returnState(state);
85                 }
86                 else if(ret instanceof SetStateEvent) {
87                     stop=System.currentTimeMillis();
88                     byte[] new_state=((SetStateEvent)ret).getArg();
89                     if(new_state != null) {
90                         state=new_state;
91                         System.out.println("<-- Received state, size =" + state.length +
92                                 " (took " + (stop-start) + "ms)");
93                     }
94                     if(!provider)
95                         break;
96                 }
97             }
98         }
99         catch(Exception JavaDoc e) {
100         }
101     }
102
103
104     byte[] createLargeState(long size) {
105         StringBuffer JavaDoc ret=new StringBuffer JavaDoc();
106         for(int i=0; i < size; i++)
107             ret.append('.');
108         return ret.toString().getBytes();
109     }
110
111
112     public static void main(String JavaDoc[] args) {
113         boolean provider=false;
114         long size=1024 * 1024;
115         String JavaDoc props="UDP(mcast_addr=224.0.0.35;mcast_port=45566;ip_ttl=32;" +
116                 "mcast_send_buf_size=150000;mcast_recv_buf_size=80000;" +
117                 "ucast_send_buf_size=80000;ucast_recv_buf_size=150000):" +
118                 "AUTOCONF:" +
119                 "PING(timeout=2000;num_initial_members=3):" +
120                 "MERGE2(min_interval=5000;max_interval=10000):" +
121                 "FD_SOCK:" +
122                 "VERIFY_SUSPECT(timeout=1500):" +
123                 "pbcast.NAKACK(gc_lag=50;retransmit_timeout=300,600,1200,2400,4800):" +
124                 "UNICAST(timeout=1000):" +
125                 "pbcast.STABLE(desired_avg_gossip=20000):" +
126                 "FRAG(frag_size=16000;down_thread=false;up_thread=false):" +
127                 "pbcast.GMS(join_timeout=5000;join_retry_timeout=2000;" +
128                 "shun=false;print_local_addr=true):" +
129                 "pbcast.STATE_TRANSFER";
130
131
132
133         for(int i=0; i < args.length; i++) {
134             if("-help".equals(args[i])) {
135                 help();
136                 return;
137             }
138             if("-provider".equals(args[i])) {
139                 provider=true;
140                 continue;
141             }
142             if("-size".equals(args[i])) {
143                 size=Long.parseLong(args[++i]);
144                 continue;
145             }
146             if("-props".equals(args[i])) {
147                 props=args[++i];
148                 continue;
149             }
150         }
151
152
153         try {
154             new LargeState().start(provider, size, props);
155         }
156         catch(Exception JavaDoc e) {
157             System.err.println(e);
158         }
159     }
160
161     static void help() {
162         System.out.println("LargeState [-help] [-size <size of state in bytes] [-provider] [-props <properties>]");
163     }
164
165 }
166
Popular Tags