/** * Processes a packet read from either the multicast or unicast socket. Needs to be synchronized * because mcast or unicast socket reads can be concurrent */ void handleIncomingUdpPacket(byte[] data) { ByteArrayInputStream inp_stream; ObjectInputStream inp; Message msg = null; List l; // used if bundling is enabled try { // skip the first n bytes (default: 4), this is the version info inp_stream = new ByteArrayInputStream(data, VERSION_LENGTH, data.length - VERSION_LENGTH); inp = new ObjectInputStream(inp_stream); if (enable_bundling) { l = new List(); l.readExternal(inp); for (Enumeration en = l.elements(); en.hasMoreElements(); ) { msg = (Message) en.nextElement(); try { handleMessage(msg); } catch (Throwable t) { Trace.error("UDP.handleIncomingUdpPacket()", "failure: " + t.toString()); } } } else { msg = new Message(); msg.readExternal(inp); handleMessage(msg); } } catch (Throwable e) { Trace.error("UDP.handleIncomingUdpPacket()", "exception=" + Trace.getStackTrace(e)); } }
public static void testVariableSubstitution() { String val = "hello world", replacement; replacement = Util.substituteVariable(val); Assert.assertEquals(val, replacement); val = "my name is ${user.name}"; replacement = Util.substituteVariable(val); Assert.assertNotSame(val, replacement); assert !(val.equals(replacement)); val = "my name is ${user.name} and ${user.name}"; replacement = Util.substituteVariable(val); assert !(val.equals(replacement)); Assert.assertEquals(-1, replacement.indexOf("${")); val = "my name is ${unknown.var:Bela Ban}"; replacement = Util.substituteVariable(val); assert replacement.contains("Bela Ban"); Assert.assertEquals(-1, replacement.indexOf("${")); val = "my name is ${unknown.var}"; replacement = Util.substituteVariable(val); assert replacement.contains("${"); val = "here is an invalid ${argument because it doesn't contains a closing bracket"; try { replacement = Util.substituteVariable(val); assert false : "should be an IllegalArgumentException"; } catch (Throwable t) { Assert.assertEquals(IllegalArgumentException.class, t.getClass()); } }
public static void main(String[] args) { String props = null; String name = null; for (int i = 0; i < args.length; i++) { if ("-props".equals(args[i])) { props = args[++i]; continue; } if ("-name".equals(args[i])) { name = args[++i]; continue; } help(); return; } UPerf test = null; try { test = new UPerf(); test.init(props, name); test.eventLoop(); } catch (Throwable ex) { ex.printStackTrace(); if (test != null) test.stop(); } }
/** Runs the merge protocol as a leader */ public void run() { // 1. Generate merge_id MergeId new_merge_id = MergeId.create(gms.local_addr); Collection<Address> coordsCopy = null; try { boolean success = setMergeId(null, new_merge_id); if (!success) { log.warn("failed to set my own merge_id (" + merge_id + ") to " + new_merge_id); return; } coordsCopy = new ArrayList<Address>(coords.keySet()); /* 2. Fetch the current Views/Digests from all subgroup coordinators */ success = getMergeDataFromSubgroupCoordinators(coords, new_merge_id, gms.merge_timeout); if (!success) throw new Exception( "merge leader did not get data from all partition coordinators " + coords.keySet()); /* 3. Remove rejected MergeData elements from merge_rsp and coords (so we'll send the new view * only to members who accepted the merge request) */ removeRejectedMergeRequests(coords.keySet()); if (merge_rsps.size() == 0) throw new Exception("did not get any merge responses from partition coordinators"); if (!coords .keySet() .contains( gms.local_addr)) // another member might have invoked a merge req on us before we // got there... throw new Exception("merge leader rejected merge request"); /* 4. Combine all views and digests into 1 View/1 Digest */ Vector<MergeData> merge_data = new Vector<MergeData>(merge_rsps.getResults().values()); MergeData combined_merge_data = consolidateMergeData(merge_data); if (combined_merge_data == null) throw new Exception("could not consolidate merge"); /* 4. Send the new View/Digest to all coordinators (including myself). On reception, they will install the digest and view in all of their subgroup members */ sendMergeView(coords.keySet(), combined_merge_data, new_merge_id); } catch (Throwable ex) { if (log.isWarnEnabled()) log.warn(gms.local_addr + ": " + ex.getLocalizedMessage() + ", merge is cancelled"); sendMergeCancelledMessage(coordsCopy, new_merge_id); } finally { gms.getViewHandler().resume(new_merge_id); stopMergeCanceller(); // this is probably not necessary /*5. if flush is in stack stop the flush for entire cluster [JGRP-700] - FLUSH: flushing should span merge */ gms.stopFlush(); if (log.isDebugEnabled()) log.debug(gms.local_addr + ": merge leader completed merge task"); thread = null; } }
public void run() { final byte[] buf = new byte[msg_size]; Object[] put_args = {0, buf}; Object[] get_args = {0}; MethodCall get_call = new MethodCall(GET, get_args); MethodCall put_call = new MethodCall(PUT, put_args); RequestOptions get_options = new RequestOptions(ResponseMode.GET_ALL, 40000, false, null); RequestOptions put_options = new RequestOptions( sync ? ResponseMode.GET_ALL : ResponseMode.GET_NONE, 40000, true, null); if (oob) { get_options.setFlags(Message.OOB); put_options.setFlags(Message.OOB); } if (sync) { get_options.setFlags(Message.DONT_BUNDLE, Message.NO_FC); put_options.setFlags(Message.DONT_BUNDLE, Message.NO_FC); } while (true) { long i = num_msgs_sent.getAndIncrement(); if (i >= num_msgs_to_send) break; boolean get = Util.tossWeightedCoin(read_percentage); try { if (get) { // sync GET Address target = pickTarget(); get_args[0] = i; disp.callRemoteMethod(target, get_call, get_options); num_gets++; } else { // sync or async (based on value of 'sync') PUT Collection<Address> targets = pickAnycastTargets(); put_args[0] = i; disp.callRemoteMethods(targets, put_call, put_options); num_puts++; } } catch (Throwable throwable) { throwable.printStackTrace(); } } }
/** * Main function * * @param args */ public static void main(String[] args) { JWhiteBoard whiteBoard = null; String props = null; boolean no_channel = false; boolean jmx = true; boolean use_state = false; String group_name = null; long state_timeout = 5000; boolean use_unicasts = false; String name = null; boolean send_own_state_on_merge = true; AddressGenerator generator = null; // Get startup parameters for JWhiteBoard for (int i = 0; i < args.length; i++) { // Show help if ("-help".equals(args[i])) { help(); return; } // Properties for Channel if ("-props".equals(args[i])) { props = args[++i]; continue; } // If existed, use no channel if ("-no_channel".equals(args[i])) { no_channel = true; continue; } // Use Java Management Extensions or not if ("-jmx".equals(args[i])) { jmx = Boolean.parseBoolean(args[++i]); continue; } // If existed, set name for the Group if ("-clustername".equals(args[i])) { group_name = args[++i]; continue; } // If existed, save Group's state if ("-state".equals(args[i])) { use_state = true; continue; } // If existed, set timeout for state if ("-timeout".equals(args[i])) { state_timeout = Long.parseLong(args[++i]); continue; } if ("-bind_addr".equals(args[i])) { System.setProperty("jgroups.bind_addr", args[++i]); continue; } if ("-use_unicasts".equals(args[i])) { use_unicasts = true; continue; } if ("-name".equals(args[i])) { name = args[++i]; continue; } if ("-send_own_state_on_merge".equals(args[i])) { send_own_state_on_merge = Boolean.getBoolean(args[++i]); continue; } if ("-uuid".equals(args[i])) { generator = new OneTimeAddressGenerator(Long.valueOf(args[++i])); continue; } help(); return; } try { whiteBoard = new JWhiteBoard( props, no_channel, jmx, use_state, state_timeout, use_unicasts, name, send_own_state_on_merge, generator); if (group_name == null) whiteBoard.setGroupName(group_name); whiteBoard.go(); } catch (Throwable e) { e.printStackTrace(System.err); System.exit(0); } }