public CommandAwareRpcDispatcher( Channel channel, JGroupsTransport transport, ExecutorService asyncExecutor, TimeService timeService, InboundInvocationHandler globalHandler) { this.server_obj = transport; this.asyncExecutor = asyncExecutor; this.transport = transport; this.timeService = timeService; this.handler = globalHandler; // MessageDispatcher superclass constructors will call start() so perform all init here this.setMembershipListener(transport); this.setChannel(channel); // If existing up handler is a muxing up handler, setChannel(..) will not have replaced it UpHandler handler = channel.getUpHandler(); if (handler instanceof Muxer<?>) { @SuppressWarnings("unchecked") Muxer<UpHandler> mux = (Muxer<UpHandler>) handler; mux.setDefaultHandler(this.prot_adapter); } channel.addChannelListener(this); asyncDispatching(true); }
@Override public void stop() { try { if (stopChannel && channel != null && channel.isOpen()) { log.disconnectAndCloseJGroups(); // Unregistering before disconnecting/closing because // after that the cluster name is null if (globalStatsEnabled) { JmxConfigurator.unregisterChannel( (JChannel) channel, mbeanServer, domain, channel.getClusterName()); } channel.disconnect(); channel.close(); } } catch (Exception toLog) { log.problemClosingChannel(toLog); } channel = null; if (dispatcher != null) { log.stoppingRpcDispatcher(); dispatcher.stop(); } members = Collections.emptyList(); coordinator = null; isCoordinator = false; dispatcher = null; }
protected void startJGroupsChannelIfNeeded() { if (startChannel) { String clusterName = configuration.getClusterName(); try { channel.connect(clusterName); } catch (Exception e) { throw new CacheException("Unable to start JGroups Channel", e); } try { // Normally this would be done by CacheManagerJmxRegistration but // the channel is not started when the cache manager starts but // when first cache starts, so it's safer to do it here. globalStatsEnabled = configuration.isExposeGlobalJmxStatistics(); if (globalStatsEnabled) { String groupName = String.format("type=channel,cluster=%s", ObjectName.quote(clusterName)); mbeanServer = JmxUtil.lookupMBeanServer(configuration); domain = JmxUtil.buildJmxDomain(configuration, mbeanServer, groupName); JmxConfigurator.registerChannel( (JChannel) channel, mbeanServer, domain, clusterName, true); } } catch (Exception e) { throw new CacheException("Channel connected, but unable to register MBeans", e); } } address = fromJGroupsAddress(channel.getAddress()); if (!startChannel) { // the channel was already started externally, we need to initialize our member list viewAccepted(channel.getView()); } if (log.isInfoEnabled()) log.localAndPhysicalAddress(getAddress(), getPhysicalAddresses()); }
private static void setStableGossip(JChannel... channels) { for (Channel channel : channels) { ProtocolStack stack = channel.getProtocolStack(); STABLE stable = (STABLE) stack.findProtocol(STABLE.class); stable.setDesiredAverageGossip(2000); } }
private static void setOOBPoolSize(JChannel... channels) { for (Channel channel : channels) { TP transport = channel.getProtocolStack().getTransport(); transport.setOOBThreadPoolMinThreads(1); transport.setOOBThreadPoolMaxThreads(2); } }
@Override public synchronized void start() throws Exception { String clusterName = clusteringConfiguration.getClusterName(); if (clusterName == null) { throw new IllegalStateException(BusI18n.clusterNameRequired.text()); } if (channel != null) { // Disconnect from any previous channel ... channel.removeChannelListener(listener); channel.setReceiver(null); } // Create the new channel by calling the delegate method ... channel = newChannel(); // Add a listener through which we'll know what's going on within the cluster ... channel.addChannelListener(listener); // Set the receiver through which we'll receive all of the changes ... channel.setReceiver(receiver); // Now connect to the cluster ... channel.connect(clusterName); // start the delegate delegate.start(); }
public void start() throws Exception { int c; channel = new JChannel(); channel.connect("PullPushTestMux"); adapter = new PullPushAdapter(channel); adapter.setListener(this); listeners = new MyListener[10]; for (int i = 0; i < listeners.length; i++) { listeners[i] = new MyListener(i, adapter); } while ((c = choice()) != 'q') { c -= 48; if (c < 0 || c > 9) { System.err.println("Choose between 0 and 9"); continue; } if (c == 0) adapter.send(new Message(null, null, "Message from default message listener")); else listeners[c].sendMessage(); } channel.close(); System.exit(0); }
public static DISCARD getDiscardForCache(Cache<?, ?> c) throws Exception { JGroupsTransport jgt = (JGroupsTransport) TestingUtil.extractComponent(c, Transport.class); Channel ch = jgt.getChannel(); ProtocolStack ps = ch.getProtocolStack(); DISCARD discard = new DISCARD(); ps.insertProtocol(discard, ProtocolStack.ABOVE, TP.class); return discard; }
private boolean startFlush(Channel ch, boolean automatic_resume) { log.debug( "starting flush on " + ch.getAddress() + " with automatic resume=" + automatic_resume); boolean result = Util.startFlush(ch); if (automatic_resume) { ch.stopFlush(); } return result; }
private boolean startFlush(Channel ch, int num_attempts, long timeout, boolean automatic_resume) { log.debug( "starting flush on " + ch.getAddress() + " with automatic resume=" + automatic_resume); boolean result = Util.startFlush(ch, num_attempts, 10, timeout); if (automatic_resume) { ch.stopFlush(); } return result; }
public void testMergeWithAsymetricViewsCoordIsolated() { // Isolate the coord Address coord = a.getView().getCreator(); System.out.println("Isolating coord: " + coord); List<Address> members = new ArrayList<>(); members.add(coord); View coord_view = new View(coord, 4, members); System.out.println("coord_view: " + coord_view); Channel coord_channel = findChannel(coord); System.out.println("coord_channel: " + coord_channel.getAddress()); MutableDigest digest = new MutableDigest(coord_view.getMembersRaw()); NAKACK2 nakack = (NAKACK2) coord_channel.getProtocolStack().findProtocol(NAKACK2.class); digest.merge(nakack.getDigest(coord)); GMS gms = (GMS) coord_channel.getProtocolStack().findProtocol(GMS.class); gms.installView(coord_view, digest); System.out.println("gms.getView() " + gms.getView()); System.out.println("Views are:"); for (JChannel ch : Arrays.asList(a, b, c, d)) System.out.println(ch.getAddress() + ": " + ch.getView()); JChannel merge_leader = findChannel(coord); MyReceiver receiver = new MyReceiver(); merge_leader.setReceiver(receiver); System.out.println("merge_leader: " + merge_leader.getAddressAsString()); System.out.println("Injecting MERGE event into merge leader " + merge_leader.getAddress()); Map<Address, View> merge_views = new HashMap<>(4); merge_views.put(a.getAddress(), a.getView()); merge_views.put(b.getAddress(), b.getView()); merge_views.put(c.getAddress(), c.getView()); merge_views.put(d.getAddress(), d.getView()); gms = (GMS) merge_leader.getProtocolStack().findProtocol(GMS.class); gms.up(new Event(Event.MERGE, merge_views)); Util.waitUntilAllChannelsHaveSameSize(10000, 1000, a, b, c, d); System.out.println("Views are:"); for (JChannel ch : Arrays.asList(a, b, c, d)) { View view = ch.getView(); System.out.println(ch.getAddress() + ": " + view); assert view.size() == 4; } MergeView merge_view = receiver.getView(); System.out.println("merge_view = " + merge_view); assert merge_view.size() == 4; assert merge_view.getSubgroups().size() == 2; for (View view : merge_view.getSubgroups()) assert contains(view, a.getAddress()) || contains(view, b.getAddress(), c.getAddress(), d.getAddress()); }
/** * Inserts a DELAY protocol in the JGroups stack used by the cache, and returns it. The DELAY * protocol can then be used to inject delays in milliseconds both at receiver and sending side. * * @param cache * @param in_delay_millis * @param out_delay_millis * @return a reference to the DELAY instance being used by the JGroups stack * @throws Exception */ public static DELAY setDelayForCache(Cache<?, ?> cache, int in_delay_millis, int out_delay_millis) throws Exception { JGroupsTransport jgt = (JGroupsTransport) TestingUtil.extractComponent(cache, Transport.class); Channel ch = jgt.getChannel(); ProtocolStack ps = ch.getProtocolStack(); DELAY delay = new DELAY(); delay.setInDelay(in_delay_millis); delay.setOutDelay(out_delay_millis); ps.insertProtocol(delay, ProtocolStack.ABOVE, TP.class); return delay; }
public MessageDispatcher(Channel channel, MessageListener l, MembershipListener l2) { this.channel = channel; prot_adapter = new ProtocolAdapter(); if (channel != null) { local_addr = channel.getAddress(); channel.addChannelListener(this); } setMessageListener(l); setMembershipListener(l2); if (channel != null) installUpHandler(prot_adapter, true); start(); }
private void send( final Address dest, final int num_msgs, final int num_threads, final double oob_prob) throws Exception { if (num_threads <= 0) throw new IllegalArgumentException("number of threads <= 0"); if (num_msgs % num_threads != 0) throw new IllegalArgumentException( "number of messages ( " + num_msgs + ") needs to be divisible by " + "the number o threads (" + num_threads + ")"); if (num_threads > 1) { final int msgs_per_thread = num_msgs / num_threads; Thread[] threads = new Thread[num_threads]; final AtomicInteger counter = new AtomicInteger(0); for (int i = 0; i < threads.length; i++) { threads[i] = new Thread() { public void run() { for (int j = 0; j < msgs_per_thread; j++) { Channel sender = Util.tossWeightedCoin(0.5) ? a : b; boolean oob = Util.tossWeightedCoin(oob_prob); int num = counter.incrementAndGet(); Message msg = new Message(dest, null, num); if (oob) msg.setFlag(Message.OOB); try { sender.send(msg); } catch (Exception e) { e.printStackTrace(); } } } }; threads[i].start(); } for (int i = 0; i < threads.length; i++) { threads[i].join(20000); } return; } for (int i = 0; i < num_msgs; i++) { Channel sender = Util.tossWeightedCoin(0.5) ? a : b; boolean oob = Util.tossWeightedCoin(oob_prob); Message msg = new Message(dest, null, i); if (oob) msg.setFlag(Message.OOB); sender.send(msg); } }
@Override public List<Address> getPhysicalAddresses() { if (physicalAddress == null && channel != null) { org.jgroups.Address addr = (org.jgroups.Address) channel.down(new Event(Event.GET_PHYSICAL_ADDRESS, channel.getAddress())); if (addr == null) { return Collections.emptyList(); } physicalAddress = new JGroupsAddress(addr); } return Collections.singletonList(physicalAddress); }
private static void verifyResponse( Map<Address, Rsp<Object>> responses, Channel channel, Object expected) { Rsp<?> response = responses.get(channel.getAddress()); String address = channel.getAddress().toString(); Assert.assertNotNull(response, address); Assert.assertFalse(response.wasSuspected(), address); if (expected != null) { Assert.assertTrue(response.wasReceived(), address); Assert.assertEquals(response.getValue(), expected, address); } else { Assert.assertFalse(response.wasReceived(), address); } }
@Override public void channelConnected(Channel channel) { // Validate view String localName = channel.getName(); Address localAddress = channel.getAddress(); for (Address address : channel.getView()) { String name = channel.getName(address); if ((name != null) && name.equals(localName) && !address.equals(localAddress)) { channel.close(); throw JGroupsLogger.ROOT_LOGGER.duplicateNodeName( this.factory.getValue().getProtocolStackConfiguration().getEnvironment().getNodeName()); } } }
protected void initChannel() { if (channel == null) { buildChannel(); String transportNodeName = configuration.getTransportNodeName(); if (transportNodeName != null && transportNodeName.length() > 0) { long range = Short.MAX_VALUE * 2; long randomInRange = (long) ((Math.random() * range) % range) + 1; transportNodeName = transportNodeName + "-" + randomInRange; channel.setName(transportNodeName); } } // Channel.LOCAL *must* be set to false so we don't see our own messages - otherwise // invalidations targeted at remote instances will be received by self. channel.setDiscardOwnMessages(true); // if we have a TopologyAwareConsistentHash, we need to set our own address generator in // JGroups if (configuration.hasTopologyInfo()) { // We can do this only if the channel hasn't been started already if (startChannel) { ((JChannel) channel) .setAddressGenerator( new AddressGenerator() { @Override public org.jgroups.Address generateAddress() { return TopologyUUID.randomUUID( channel.getName(), configuration.getSiteId(), configuration.getRackId(), configuration.getMachineId()); } }); } else { if (channel.getAddress() instanceof TopologyUUID) { TopologyUUID topologyAddress = (TopologyUUID) channel.getAddress(); if (!configuration.getSiteId().equals(topologyAddress.getSiteId()) || !configuration.getRackId().equals(topologyAddress.getRackId()) || !configuration.getMachineId().equals(topologyAddress.getMachineId())) { throw new CacheException( "Topology information does not match the one set by the provided JGroups channel"); } } else { throw new CacheException("JGroups address does not contain topology coordinates"); } } } }
public void receive(Message msg) { Message reply = new Message(msg.getSrc()); try { System.out.println( "-- MySimpleReplier[" + channel.getAddress() + "]: received message from " + msg.getSrc()); if (handle_requests) { System.out.println(", sending reply"); channel.send(reply); } else System.out.println("\n"); } catch (Exception e) { e.printStackTrace(); } }
@Override public Address getAddress() { if (address == null && channel != null) { address = fromJGroupsAddress(channel.getAddress()); } return address; }
public void testMethodReturningException() throws Exception { Object retval = disp.callRemoteMethod( channel.getAddress(), "returnException", null, null, RequestOptions.SYNC()); System.out.println("retval: " + retval); assertNotNull(retval); assert retval instanceof IllegalArgumentException; }
@Override public synchronized void shutdown() { if (channel != null) { // Mark this as not accepting any more ... isOpen.set(false); try { // Disconnect from the channel and close it ... channel.removeChannelListener(listener); channel.setReceiver(null); channel.close(); } finally { channel = null; // Now that we're not receiving any more messages, shut down the delegate delegate.shutdown(); } } }
@Override public int getViewId() { if (channel == null) throw new CacheException("The cache has been stopped and invocations are not allowed!"); View view = channel.getView(); if (view == null) return -1; return (int) view.getVid().getId(); }
@Test(expectedExceptions = TimeoutException.class) public void testMethodWithExceptionWithoutWrapping() throws Exception { disp.wrapExceptions(false); try { disp.callRemoteMethod(channel.getAddress(), "bar", null, null, RequestOptions.SYNC()); } finally { disp.wrapExceptions(true); } }
// @Test(expectedExceptions=IllegalArgumentException.class) public void testMethodWithException2() throws Exception { try { disp.callRemoteMethod(channel.getAddress(), "foobar", null, null, RequestOptions.SYNC()); } catch (Throwable t) { System.out.println("t = " + t); assert t instanceof InvocationTargetException; assert t.getCause() instanceof IllegalArgumentException; } }
public void stop() { if (corr != null) corr.stop(); if (channel instanceof JChannel) { TP transport = channel.getProtocolStack().getTransport(); transport.unregisterProbeHandler(probe_handler); corr.unregisterProbeHandler(transport); } }
public void setChannel(Channel ch) { if (ch == null) return; this.channel = ch; local_addr = channel.getAddress(); if (prot_adapter == null) prot_adapter = new ProtocolAdapter(); // Don't force installing the UpHandler so subclasses can use this // method and still integrate with a MuxUpHandler installUpHandler(prot_adapter, false); }
// @Test(expectedExceptions=InvocationTargetException.class) public void testMethodWithException() throws Exception { try { disp.callRemoteMethod(channel.getAddress(), "bar", null, null, RequestOptions.SYNC()); assert false : "method should have thrown an exception"; } catch (Exception ex) { assert ex instanceof InvocationTargetException; Throwable cause = ex.getCause(); assert cause instanceof TimeoutException; } }
public void start() { if (corr == null) corr = createRequestCorrelator(prot_adapter, this, local_addr) .asyncDispatching(async_dispatching); correlatorStarted(); corr.start(); if (channel != null) { List<Address> tmp_mbrs = channel.getView() != null ? channel.getView().getMembers() : null; setMembers(tmp_mbrs); if (channel instanceof JChannel) { TP transport = channel.getProtocolStack().getTransport(); corr.registerProbeHandler(transport); } TP transport = channel.getProtocolStack().getTransport(); hardware_multicast_supported = transport.supportsMulticasting(); transport.registerProbeHandler(probe_handler); } }
@Deprecated public RpcDispatcher( Channel channel, MessageListener l, MembershipListener l2, Object server_obj, boolean deadlock_detection) { super(channel, l, l2); channel.addChannelListener(this); this.server_obj = server_obj; }