private boolean viewContains(View view, JChannel... channels) { boolean b = true; for (JChannel ch : channels) { b = b && view.containsMember(ch.getAddress()); } return b; }
private void sendMessage() { try { /** * 参数里指定Channel使用的协议栈,如果是空的,则使用默认的协议栈, 位于JGroups包里的udp.xml。参数可以是一个以冒号分隔的字符串, * 或是一个XML文件,在XML文件里定义协议栈。 */ logger.warn("发送监控同步数据通知"); Properties prop = SystemPropertiesUtils.getProp(); String hostName = NetUtils.getLocalHost(); prop.put("node.ip", NetUtils.getIpByHost(hostName)); prop.put("node.host", hostName); prop.put("install.path", getInstallPath()); // 创建一个通道 JChannel channel = new JChannel(); // 加入一个群 channel.connect("MonitorContainer"); // 发送事件 // 这里的Message的第一个参数是发送端地址 // 第二个是接收端地址 // 第三个是发送的字符串 // 具体参见jgroup send API Message msg = new Message(null, null, prop); // 发送 channel.send(msg); // 关闭通道 channel.close(); } catch (Exception e) { logger.error(e.getMessage()); } }
public void testSASLDigestMD5() throws Exception { a = createChannel("A", "DIGEST-MD5", "jack"); b = createChannel("B", "DIGEST-MD5", "jack"); a.connect("SaslTest"); b.connect("SaslTest"); assertTrue(b.isConnected()); }
public void start(HTableIndexCoprocessor idxCoprocessor) throws Exception { this.idxCoprocessor = idxCoprocessor; loadConfiguration(); channel = new JChannel(ClassLoader.getSystemResource("tcp.xml")); channel.setReceiver(this); channel.connect("MasterIndexUpdateCluster"); }
private boolean checkViewSize(int expectedSize, JChannel... channels) { boolean b = true; for (JChannel ch : channels) { b = b && ch.getView().size() == expectedSize; } return b; }
/** * Tests sending 1, 2 (OOB) and 3, where they are received in the order 1, 3, 2. Message 3 should * not get delivered until message 4 is received (http://jira.jboss.com/jira/browse/JGRP-780) */ public void testRegularAndOOBUnicasts() throws Exception { DISCARD discard = new DISCARD(); ProtocolStack stack = a.getProtocolStack(); stack.insertProtocol(discard, ProtocolStack.BELOW, UNICAST.class, UNICAST2.class); Address dest = b.getAddress(); Message m1 = new Message(dest, null, 1); Message m2 = new Message(dest, null, 2); m2.setFlag(Message.OOB); Message m3 = new Message(dest, null, 3); MyReceiver receiver = new MyReceiver("C2"); b.setReceiver(receiver); a.send(m1); discard.setDropDownUnicasts(1); a.send(m2); a.send(m3); Collection<Integer> list = receiver.getMsgs(); int count = 10; while (list.size() < 3 && --count > 0) { Util.sleep(500); // time for potential retransmission sendStableMessages(a, b); } assert list.size() == 3 : "list is " + list; assert list.contains(1) && list.contains(2) && list.contains(3); }
@Override public void run() { try { log.info("Hello from Cluster microservice provider!"); channel = new JChannel((String) context.getProperties().get(CLUSTER_CONFIGURATION)); channel.setReceiver(receiver); channel.connect((String) context.getProperties().get(CLUSTER_GROUP)); channel.send(new Message(null, "Holáryjou!")); try { while (!Thread.currentThread().isInterrupted()) { Thread.sleep(1000); } } catch (InterruptedException ie) { Utils.shutdownLog(log, ie); } finally { channel.close(); } } catch (Exception e) { log.error("Cluster microservice provider failed: ", e); } }
/** * Constructor 1 * * @param props * @param no_channel * @param jmx * @param use_state * @param state_timeout * @param use_unicasts * @param name * @param send_own_state_on_merge * @param gen * @throws Exception */ public JWhiteBoard( String props, boolean no_channel, boolean jmx, boolean use_state, long state_timeout, boolean use_unicasts, String name, boolean send_own_state_on_merge, AddressGenerator gen) throws Exception { this.noChannel = no_channel; this.jmx = jmx; this.useState = use_state; this.stateTimeout = state_timeout; this.use_unicasts = use_unicasts; if (no_channel) return; channel = new JChannel(props); if (gen != null) channel.addAddressGenerator(gen); if (name != null) channel.setName(name); channel.setReceiver(this); channel.addChannelListener(this); this.send_own_state_on_merge = send_own_state_on_merge; }
/** Creates a singleton view for each channel listed and injects it */ protected static void createPartition(JChannel... channels) { for (JChannel ch : channels) { View view = Util.createView(ch.getAddress(), 5, ch.getAddress()); GMS gms = (GMS) ch.getProtocolStack().findProtocol(GMS.class); gms.installView(view); } }
/** Execute when new member join or leave Group */ public void viewAccepted(View v) { memberSize = v.size(); if (mainFrame != null) setTitle(); members.clear(); members.addAll(v.getMembers()); if (v instanceof MergeView) { System.out.println("** " + v); // This is a simple merge function, which fetches the state from the coordinator // on a merge and overwrites all of its own state if (useState && !members.isEmpty()) { Address coord = members.get(0); Address local_addr = channel.getAddress(); if (local_addr != null && !local_addr.equals(coord)) { try { // make a copy of our state first Map<Point, Color> copy = null; if (send_own_state_on_merge) { synchronized (drawPanel.state) { copy = new LinkedHashMap<Point, Color>(drawPanel.state); } } System.out.println("fetching state from " + coord); channel.getState(coord, 5000); if (copy != null) sendOwnState(copy); // multicast my own state so everybody else has it too } catch (Exception e) { e.printStackTrace(); } } } } else System.out.println("** View=" + v); }
/** * Init JWhiteBoard interface * * @throws Exception */ public void go() throws Exception { if (!noChannel && !useState) channel.connect(groupName); mainFrame = new JFrame(); mainFrame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); drawPanel = new DrawPanel(useState); drawPanel.setBackground(backgroundColor); subPanel = new JPanel(); mainFrame.getContentPane().add("Center", drawPanel); clearButton = new JButton("Clean"); clearButton.setFont(defaultFont); clearButton.addActionListener(this); leaveButton = new JButton("Exit"); leaveButton.setFont(defaultFont); leaveButton.addActionListener(this); subPanel.add("South", clearButton); subPanel.add("South", leaveButton); mainFrame.getContentPane().add("South", subPanel); mainFrame.setBackground(backgroundColor); clearButton.setForeground(Color.blue); leaveButton.setForeground(Color.blue); mainFrame.pack(); mainFrame.setLocation(15, 25); mainFrame.setBounds(new Rectangle(250, 250)); if (!noChannel && useState) { channel.connect(groupName, null, stateTimeout); } mainFrame.setVisible(true); }
/** * Constructor 3 * * @param channel * @param use_state: Save state of Group * @param state_timeout: State time out * @throws Exception */ public JWhiteBoard(JChannel channel, boolean use_state, long state_timeout) throws Exception { this.channel = channel; channel.setReceiver(this); channel.addChannelListener(this); this.useState = use_state; this.stateTimeout = state_timeout; }
@Test public void testJoinMessageReceivedForDisconnectedHost() throws Exception { final AtomicInteger counter1 = new AtomicInteger(0); final AtomicInteger counter2 = new AtomicInteger(0); connector1.subscribe(String.class.getName(), new CountingCommandHandler(counter1)); connector1.connect(20); assertTrue( "Expected connector 1 to connect within 10 seconds", connector1.awaitJoined(10, TimeUnit.SECONDS)); connector2.subscribe(String.class.getName(), new CountingCommandHandler(counter2)); connector2.connect(80); assertTrue("Connector 2 failed to connect", connector2.awaitJoined()); // wait for both connectors to have the same view waitForConnectorSync(); ConsistentHash hashBefore = connector1.getConsistentHash(); // secretly insert an illegal message channel1 .getReceiver() .receive( new Message( channel1.getAddress(), new IpAddress(12345), new JoinMessage(10, Collections.<String>emptySet()))); ConsistentHash hash2After = connector1.getConsistentHash(); assertEquals("That message should not have changed the ring", hashBefore, hash2After); }
/** Tests A.startFlush(), followed by another A.startFlush() */ public void testTwoStartFlushesOnDifferentMembersWithTotalFlush() throws Exception { c1 = createChannel(true, 3); c1.connect("testTwoStartFlushesOnDifferentMembersWithTotalFlush"); c2 = createChannel(c1); c2.connect("testTwoStartFlushesOnDifferentMembersWithTotalFlush"); assertViewsReceived(c1, c2); boolean rc = startFlush(c1, false); assert rc; rc = startFlush(c2, 1, 500, false); assert !rc; stopFlush(c1); rc = startFlush(c2, false); assert rc; stopFlush(c2); rc = startFlush(c1, false); assert rc; stopFlush(c2); // c2 can actually stop a flush started by c1 rc = startFlush(c2, true); assert rc; }
/** Tests https://jira.jboss.org/jira/browse/JGRP-1079 for unicast messages */ public void testOOBUnicastMessageLoss() throws Exception { MyReceiver receiver = new MySleepingReceiver("C2", 1000); b.setReceiver(receiver); a.getProtocolStack().getTransport().setOOBRejectionPolicy("discard"); final int NUM = 10; final Address dest = b.getAddress(); for (int i = 1; i <= NUM; i++) { Message msg = new Message(dest, null, i); msg.setFlag(Message.OOB); a.send(msg); } Collection<Integer> msgs = receiver.getMsgs(); for (int i = 0; i < 20; i++) { if (msgs.size() == NUM) break; Util.sleep(1000); // sendStableMessages(c1,c2); // not needed for unicasts ! } assert msgs.size() == NUM : "expected " + NUM + " messages but got " + msgs.size() + ", msgs=" + Util.print(msgs); for (int i = 1; i <= NUM; i++) { assert msgs.contains(i); } }
/** Tests https://jira.jboss.org/jira/browse/JGRP-1079 */ public void testOOBMessageLoss() throws Exception { Util.close(b); // we only need 1 channel MyReceiver receiver = new MySleepingReceiver("C1", 1000); a.setReceiver(receiver); TP transport = a.getProtocolStack().getTransport(); transport.setOOBRejectionPolicy("discard"); final int NUM = 10; for (int i = 1; i <= NUM; i++) { Message msg = new Message(null, null, i); msg.setFlag(Message.OOB); a.send(msg); } STABLE stable = (STABLE) a.getProtocolStack().findProtocol(STABLE.class); if (stable != null) stable.runMessageGarbageCollection(); Collection<Integer> msgs = receiver.getMsgs(); for (int i = 0; i < 20; i++) { if (msgs.size() == NUM) break; Util.sleep(1000); sendStableMessages(a, b); } System.out.println("msgs = " + Util.print(msgs)); assert msgs.size() == NUM : "expected " + NUM + " messages but got " + msgs.size() + ", msgs=" + Util.print(msgs); for (int i = 1; i <= NUM; i++) { assert msgs.contains(i); } }
public void testRegularUnicastsToOthers() throws Exception { send(c1, c2.getAddress(), false, 10); send(c1, c3.getAddress(), false, 10); sendStableMessages(c1, c2, c3); check(r2, 1, false, new Tuple<Address, Integer>(a1, 10)); check(r3, 1, false, new Tuple<Address, Integer>(a1, 10)); }
protected void enableInfoSender(boolean enable, JChannel... channels) throws Exception { for (JChannel ch : channels) { MERGE3 merge = (MERGE3) ch.getProtocolStack().findProtocol(MERGE3.class); Method meth = enable ? startInfoSender : stopInfoSender; meth.invoke(merge); } }
public void testRegularAndOOBMulticasts() throws Exception { DISCARD discard = new DISCARD(); ProtocolStack stack = a.getProtocolStack(); stack.insertProtocol(discard, ProtocolStack.BELOW, NAKACK2.class); a.setDiscardOwnMessages(true); Address dest = null; // send to all Message m1 = new Message(dest, null, 1); Message m2 = new Message(dest, null, 2); m2.setFlag(Message.OOB); Message m3 = new Message(dest, null, 3); MyReceiver receiver = new MyReceiver("C2"); b.setReceiver(receiver); a.send(m1); discard.setDropDownMulticasts(1); a.send(m2); a.send(m3); Util.sleep(500); Collection<Integer> list = receiver.getMsgs(); for (int i = 0; i < 10; i++) { System.out.println("list = " + list); if (list.size() == 3) break; Util.sleep(1000); // give the asynchronous msgs some time to be received sendStableMessages(a, b); } assert list.size() == 3 : "list is " + list; assert list.contains(1) && list.contains(2) && list.contains(3); }
private void sendMessageToBothChannels(int size) throws Exception { long start, stop; d1.setRequestHandler(new MyHandler(new byte[size])); c2 = createChannel(c1); c2.setName("B"); disableBundling(c2); d2 = new MessageDispatcher(c2, null, null, new MyHandler(new byte[size])); c2.connect("MessageDispatcherUnitTest"); Assert.assertEquals(2, c2.getView().size()); System.out.println("casting message"); start = System.currentTimeMillis(); RspList rsps = d1.castMessage(null, new Message(), new RequestOptions(ResponseMode.GET_ALL, 0)); stop = System.currentTimeMillis(); System.out.println("rsps:\n" + rsps); System.out.println("call took " + (stop - start) + " ms"); assertNotNull(rsps); Assert.assertEquals(2, rsps.size()); Rsp rsp = rsps.get(c1.getAddress()); assertNotNull(rsp); byte[] ret = (byte[]) rsp.getValue(); Assert.assertEquals(size, ret.length); rsp = rsps.get(c2.getAddress()); assertNotNull(rsp); ret = (byte[]) rsp.getValue(); Assert.assertEquals(size, ret.length); Util.close(c2); }
protected JChannel createChannel(String name, boolean connect) throws Exception { JChannel retval = new JChannel( new SHARED_LOOPBACK(), new SHARED_LOOPBACK_PING(), new MERGE3() .setValue("min_interval", 3000) .setValue("max_interval", 4000) .setValue("check_interval", 7000), new NAKACK2() .setValue("use_mcast_xmit", false) .setValue("log_discard_msgs", false) .setValue("log_not_found_msgs", false), new UNICAST3(), new STABLE().setValue("max_bytes", 50000), new GMS() .setValue("print_local_addr", false) .setValue("use_merger2", true) .setValue("join_timeout", 100) .setValue("leave_timeout", 100) .setValue("merge_timeout", 5000) .setValue("log_view_warnings", false) .setValue("view_ack_collection_timeout", 50) .setValue("log_collect_msgs", false)) .name(name); if (connect) retval.connect("MergeTest4"); return retval; }
private void send(Address dest) throws Exception { final CountDownLatch latch = new CountDownLatch(1); final BlockingReceiver receiver = new BlockingReceiver(latch); final int NUM = 10; b.setReceiver(receiver); a.send(dest, 1); // the only regular message for (int i = 2; i <= NUM; i++) a.send(new Message(dest, i).setFlag(Message.Flag.OOB)); sendStableMessages(a, b); List<Integer> list = receiver.getMsgs(); for (int i = 0; i < 20; i++) { if (list.size() == NUM - 1) break; sendStableMessages(a, b); Util.sleep(500); // give the asynchronous msgs some time to be received } System.out.println("list = " + list); assert list.size() == NUM - 1 : "list is " + list; assert list.contains(2) && list.contains(10); System.out.println("[" + Thread.currentThread().getName() + "]: releasing latch"); latch.countDown(); for (int i = 0; i < 20; i++) { if (list.size() == NUM) break; sendStableMessages(a, b); Util.sleep(1000); // give the asynchronous msgs some time to be received } System.out.println("list = " + list); assert list.size() == NUM : "list is " + list; for (int i = 1; i <= NUM; i++) assert list.contains(i); }
private static void modifyConfigs(JChannel... channels) throws Exception { for (JChannel ch : channels) { ProtocolStack stack = ch.getProtocolStack(); stack.removeProtocol("MERGE2"); stack.removeProtocol("VERIFY_SUSPECT"); stack.removeProtocol("FC"); } }
public void setUp() throws Exception { super.setUp(); ch1 = new JChannel(props); ch1.connect(GROUP); ch2 = new JChannel(props); ch2.connect(GROUP); }
private static void sendStableMessages(JChannel... channels) { for (JChannel ch : channels) { STABLE stable = (STABLE) ch.getProtocolStack().findProtocol(STABLE.class); if (stable != null) stable.runMessageGarbageCollection(); UNICAST2 uni = (UNICAST2) ch.getProtocolStack().findProtocol(UNICAST2.class); if (uni != null) uni.sendStableMessages(); } }
private static JChannel findChannelByAddress(Address address, JChannel... channels) { for (JChannel ch : channels) { if (ch.getAddress().equals(address)) { return ch; } } return null; }
/** * 启动连接。 * * @throws Exception */ private void start() throws Exception { channle = new JChannel(); channle.setReceiver(this); channle.connect("ChatCluster"); channle.getState(null, 10000); eventloop(); channle.close(); }
@Test(invocationCount = 10) public void testOOBUnicastsToOthers() throws Exception { send(c1, c2.getAddress(), true, 10); send(c1, c3.getAddress(), true, 10); sendStableMessages(c1, c2, c3); check(r2, 1, true, new Tuple<Address, Integer>(a1, 10)); check(r3, 1, true, new Tuple<Address, Integer>(a1, 10)); }
private Address getAddress(String nodeName) { for (Address member : channel.getView()) { if (channel.getName(member).equals(nodeName)) { return member; } } throw new IllegalArgumentException( "Given node doesn't seem to be a member of the DistributedCommandBus"); }
protected static void removeDUPL(JChannel... channels) { for (JChannel ch : channels) { DUPL dupl = (DUPL) ch.getProtocolStack().findProtocol(DUPL.class); if (dupl != null) { dupl.setCopyMulticastMsgs(false); dupl.setCopyUnicastMsgs(false); } } }