protected RELAY2 createRELAY2(String site_name) {
    RELAY2 relay =
        new RELAY2()
            .site(site_name)
            .enableAddressTagging(false)
            .asyncRelayCreation(true)
            .relayMulticasts(true);

    RelayConfig.SiteConfig lon_cfg = new RelayConfig.SiteConfig(LON, (short) 0),
        sfo_cfg = new RelayConfig.SiteConfig(SFO, (short) 1);

    lon_cfg.addBridge(
        new RelayConfig.ProgrammaticBridgeConfig(BRIDGE_CLUSTER, createBridgeStack()));
    sfo_cfg.addBridge(
        new RelayConfig.ProgrammaticBridgeConfig(BRIDGE_CLUSTER, createBridgeStack()));
    relay.addSite(LON, lon_cfg).addSite(SFO, sfo_cfg);
    return relay;
  }
  protected void waitForBridgeView(
      int expected_size, long timeout, long interval, JChannel... channels) {
    long deadline = System.currentTimeMillis() + timeout;

    while (System.currentTimeMillis() < deadline) {
      boolean views_correct = true;
      for (JChannel ch : channels) {
        RELAY2 relay = (RELAY2) ch.getProtocolStack().findProtocol(RELAY2.class);
        View bridge_view = relay.getBridgeView(BRIDGE_CLUSTER);
        if (bridge_view == null || bridge_view.size() != expected_size) {
          views_correct = false;
          break;
        }
      }
      if (views_correct) break;
      Util.sleep(interval);
    }

    System.out.println("Bridge views:\n");
    for (JChannel ch : channels) {
      RELAY2 relay = (RELAY2) ch.getProtocolStack().findProtocol(RELAY2.class);
      View bridge_view = relay.getBridgeView(BRIDGE_CLUSTER);
      System.out.println(ch.getAddress() + ": " + bridge_view);
    }

    for (JChannel ch : channels) {
      RELAY2 relay = (RELAY2) ch.getProtocolStack().findProtocol(RELAY2.class);
      View bridge_view = relay.getBridgeView(BRIDGE_CLUSTER);
      assert bridge_view != null && bridge_view.size() == expected_size
          : ch.getAddress() + ": bridge view=" + bridge_view + ", expected=" + expected_size;
    }
  }
 protected void waitUntilStatus(
     String site_name,
     RELAY2.RouteStatus expected_status,
     long timeout,
     long interval,
     JChannel ch)
     throws Exception {
   RELAY2 relay = (RELAY2) ch.getProtocolStack().findProtocol(RELAY2.class);
   if (relay == null) throw new IllegalArgumentException("Protocol RELAY2 not found");
   Relayer.Route route = null;
   long deadline = System.currentTimeMillis() + timeout;
   while (System.currentTimeMillis() < deadline) {
     route = relay.getRoute(site_name);
     if (route != null && route.status() == expected_status) break;
     Util.sleep(interval);
   }
   assert route.status() == expected_status
       : "status="
           + (route != null ? route.status() : "n/a")
           + ", expected status="
           + expected_status;
 }
  /** Tests that notifications are routed to all sites. */
  public void testNotificationAndRpcRelay2Transit() throws Exception {
    a.connect(LON_CLUSTER);
    b.connect(LON_CLUSTER);
    rpca.start();
    rpcb.start();
    Util.waitUntilAllChannelsHaveSameSize(30000, 500, a, b);

    x.connect(SFO_CLUSTER);
    y.connect(SFO_CLUSTER);
    rpcx.start();
    rpcy.start();
    Util.waitUntilAllChannelsHaveSameSize(30000, 500, x, y);

    assert x.getView().size() == 2;

    RELAY2 ar = (RELAY2) a.getProtocolStack().findProtocol(RELAY2.class);
    RELAY2 xr = (RELAY2) x.getProtocolStack().findProtocol(RELAY2.class);

    assert ar != null && xr != null;

    JChannel a_bridge = null, x_bridge = null;
    for (int i = 0; i < 20; i++) {
      a_bridge = ar.getBridge(SFO);
      x_bridge = xr.getBridge(LON);
      if (a_bridge != null
          && x_bridge != null
          && a_bridge.getView().size() == 2
          && x_bridge.getView().size() == 2) break;
      Util.sleep(500);
    }

    assert a_bridge != null && x_bridge != null;

    System.out.println("A's bridge channel: " + a_bridge.getView());
    System.out.println("X's bridge channel: " + x_bridge.getView());
    assert a_bridge.getView().size() == 2 : "bridge view is " + a_bridge.getView();
    assert x_bridge.getView().size() == 2 : "bridge view is " + x_bridge.getView();

    Relayer.Route route = getRoute(x, LON);
    System.out.println("Route at sfo to lon: " + route);
    assert route != null;

    System.out.println("B: sending message 0 to the site master of SFO");
    Address sm_sfo = new SiteMaster(SFO);
    b.send(sm_sfo, 0);
    checkMsgDelivery(xl);

    System.out.println("B: sending message to all");
    b.send(null, 0);
    checkMsgDelivery(xl, yl, al, bl);

    MethodCall call = new MethodCall(ServerObject.class.getMethod("foo"));

    System.out.println("B: call foo method on A");
    Object rsp =
        rpcb.callRemoteMethod(a.getAddress(), call, new RequestOptions(ResponseMode.GET_ALL, 5000));
    System.out.println("RSP is: " + rsp);

    System.out.println("B: call foo method on SFO master site");
    rsp = rpcb.callRemoteMethod(sm_sfo, call, new RequestOptions(ResponseMode.GET_ALL, 5000));
    System.out.println("RSP is: " + rsp);

    System.out.println("B: call foo method on all members dest = null");
    RspList<Integer> rsps =
        rpcb.callRemoteMethods(null, call, new RequestOptions(ResponseMode.GET_ALL, 5000));
    System.out.println("RSPs are: \n" + rsps);

    View bridge_view = xr.getBridgeView(BRIDGE_CLUSTER);
    System.out.println("bridge_view = " + bridge_view);

    route = getRoute(x, LON);
    System.out.println("Route at sfo to lon: " + route);
    assert route != null;
  }
 protected Relayer.Route getRoute(JChannel ch, String site_name) {
   RELAY2 relay = (RELAY2) ch.getProtocolStack().findProtocol(RELAY2.class);
   return relay.getRoute(site_name);
 }
Beispiel #6
0
  @Override
  public Channel createChannel(final String id) throws Exception {
    JGroupsLogger.ROOT_LOGGER.debugf(
        "Creating channel %s from stack %s", id, this.configuration.getName());

    PrivilegedExceptionAction<JChannel> action =
        new PrivilegedExceptionAction<JChannel>() {
          @Override
          public JChannel run() throws Exception {
            return new JChannel(JChannelFactory.this);
          }
        };
    final JChannel channel = WildFlySecurityManager.doChecked(action);
    ProtocolStack stack = channel.getProtocolStack();

    TransportConfiguration transportConfig = this.configuration.getTransport();
    SocketBinding binding = transportConfig.getSocketBinding();
    if (binding != null) {
      channel.setSocketFactory(new ManagedSocketFactory(binding.getSocketBindings()));
    }

    // Relay protocol is added to stack programmatically, not via ProtocolStackConfigurator
    RelayConfiguration relayConfig = this.configuration.getRelay();
    if (relayConfig != null) {
      String localSite = relayConfig.getSiteName();
      List<RemoteSiteConfiguration> remoteSites = this.configuration.getRelay().getRemoteSites();
      List<String> sites = new ArrayList<>(remoteSites.size() + 1);
      sites.add(localSite);
      // Collect bridges, eliminating duplicates
      Map<String, RelayConfig.BridgeConfig> bridges = new HashMap<>();
      for (final RemoteSiteConfiguration remoteSite : remoteSites) {
        String siteName = remoteSite.getName();
        sites.add(siteName);
        String clusterName = remoteSite.getClusterName();
        RelayConfig.BridgeConfig bridge =
            new RelayConfig.BridgeConfig(clusterName) {
              @Override
              public JChannel createChannel() throws Exception {
                JChannel channel = (JChannel) remoteSite.getChannel();
                // Don't use FORK in bridge stack
                channel.getProtocolStack().removeProtocol(FORK.class);
                return channel;
              }
            };
        bridges.put(clusterName, bridge);
      }
      RELAY2 relay = new RELAY2().site(localSite);
      for (String site : sites) {
        RelayConfig.SiteConfig siteConfig = new RelayConfig.SiteConfig(site);
        relay.addSite(site, siteConfig);
        if (site.equals(localSite)) {
          for (RelayConfig.BridgeConfig bridge : bridges.values()) {
            siteConfig.addBridge(bridge);
          }
        }
      }
      Map<String, String> relayProperties = new HashMap<>(relayConfig.getProperties());
      Configurator.resolveAndAssignFields(relay, relayProperties);
      Configurator.resolveAndInvokePropertyMethods(relay, relayProperties);
      stack.addProtocol(relay);
      relay.init();
    }

    UnknownForkHandler unknownForkHandler =
        new UnknownForkHandler() {
          private final short id = ClassConfigurator.getProtocolId(RequestCorrelator.class);

          @Override
          public Object handleUnknownForkStack(Message message, String forkStackId) {
            return this.handle(message);
          }

          @Override
          public Object handleUnknownForkChannel(Message message, String forkChannelId) {
            return this.handle(message);
          }

          private Object handle(Message message) {
            Header header = (Header) message.getHeader(this.id);
            // If this is a request expecting a response, don't leave the requester hanging - send
            // an identifiable response on which it can filter
            if ((header != null) && (header.type == Header.REQ) && header.rspExpected()) {
              Message response =
                  message
                      .makeReply()
                      .setFlag(message.getFlags())
                      .clearFlag(Message.Flag.RSVP, Message.Flag.SCOPED);

              response.putHeader(FORK.ID, message.getHeader(FORK.ID));
              response.putHeader(this.id, new Header(Header.RSP, 0, header.corrId));
              response.setBuffer(UNKNOWN_FORK_RESPONSE.array());

              channel.down(new Event(Event.MSG, response));
            }
            return null;
          }
        };

    // Add implicit FORK to the top of the stack
    FORK fork = new FORK();
    fork.setUnknownForkHandler(unknownForkHandler);
    stack.addProtocol(fork);
    fork.init();

    channel.setName(this.configuration.getNodeName());

    TransportConfiguration.Topology topology = this.configuration.getTransport().getTopology();
    if (topology != null) {
      channel.addAddressGenerator(new TopologyAddressGenerator(topology));
    }

    return channel;
  }