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);
   }
 }
Ejemplo n.º 2
0
  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");
        }
      }
    }
  }
Ejemplo n.º 3
0
 @Override
 public Address getAddress() {
   if (address == null && channel != null) {
     address = fromJGroupsAddress(channel.getAddress());
   }
   return address;
 }
Ejemplo n.º 4
0
  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());
  }
 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;
 }
 // @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;
   }
 }
Ejemplo n.º 7
0
 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;
 }
Ejemplo n.º 8
0
 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;
 }
Ejemplo n.º 9
0
 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 = TimeoutException.class)
 public void testMethodWithExceptionWithoutWrapping() throws Exception {
   disp.wrapExceptions(false);
   try {
     disp.callRemoteMethod(channel.getAddress(), "bar", null, null, RequestOptions.SYNC());
   } finally {
     disp.wrapExceptions(true);
   }
 }
Ejemplo n.º 11
0
  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());
  }
 // @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 testMethodWithoutException() throws Exception {
   Object retval =
       disp.callRemoteMethod(
           channel.getAddress(),
           "foo",
           null,
           null,
           new RequestOptions(ResponseMode.GET_ALL, 5000));
   System.out.println("retval: " + retval);
   assertNotNull(retval);
 }
Ejemplo n.º 14
0
 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();
 }
Ejemplo n.º 15
0
 @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);
 }
Ejemplo n.º 16
0
 @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());
     }
   }
 }
Ejemplo n.º 17
0
 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();
   }
 }
Ejemplo n.º 18
0
  protected <T> GroupRequest<T> cast(
      final Collection<Address> dests,
      Message msg,
      RequestOptions options,
      boolean block_for_results,
      FutureListener<RspList<T>> listener)
      throws Exception {
    if (msg.getDest() != null && !(msg.getDest() instanceof AnycastAddress))
      throw new IllegalArgumentException("message destination is non-null, cannot send message");

    if (options != null) {
      msg.setFlag(options.getFlags()).setTransientFlag(options.getTransientFlags());
      if (options.getScope() > 0) msg.setScope(options.getScope());
    }

    List<Address> real_dests;
    // we need to clone because we don't want to modify the original
    if (dests != null) {
      real_dests = new ArrayList<Address>();
      for (Address dest : dests) {
        if (dest instanceof SiteAddress || this.members.contains(dest)) {
          if (!real_dests.contains(dest)) real_dests.add(dest);
        }
      }
    } else real_dests = new ArrayList<Address>(members);

    // if local delivery is off, then we should not wait for the message from the local member.
    // therefore remove it from the membership
    Channel tmp = channel;
    if ((tmp != null && tmp.getDiscardOwnMessages())
        || msg.isTransientFlagSet(Message.TransientFlag.DONT_LOOPBACK)) {
      if (local_addr == null) local_addr = tmp != null ? tmp.getAddress() : null;
      if (local_addr != null) real_dests.remove(local_addr);
    }

    if (options != null && options.hasExclusionList()) {
      Address[] exclusion_list = options.exclusionList();
      for (Address excluding : exclusion_list) real_dests.remove(excluding);
    }

    // don't even send the message if the destination list is empty
    if (log.isTraceEnabled()) log.trace("real_dests=" + real_dests);

    if (real_dests.isEmpty()) {
      if (log.isTraceEnabled()) log.trace("destination list is empty, won't send message");
      return null;
    }

    if (options != null) {
      boolean async = options.getMode() == ResponseMode.GET_NONE;
      if (options.getAnycasting()) {
        if (async) async_anycasts.incrementAndGet();
        else sync_anycasts.incrementAndGet();
      } else {
        if (async) async_multicasts.incrementAndGet();
        else sync_multicasts.incrementAndGet();
      }
    }

    GroupRequest<T> req = new GroupRequest<T>(msg, corr, real_dests, options);
    if (listener != null) req.setListener(listener);
    if (options != null) {
      req.setResponseFilter(options.getRspFilter());
      req.setAnycasting(options.getAnycasting());
    }
    req.setBlockForResults(block_for_results);
    req.execute();
    return req;
  }
Ejemplo n.º 19
0
 public void unblock() {
   System.out.println("-- MySimpleReplier[" + channel.getAddress() + "]: unblock()");
 }
Ejemplo n.º 20
0
 public void viewAccepted(View new_view) {
   System.out.println(
       "-- MySimpleReplier[" + channel.getAddress() + "]: viewAccepted(" + new_view + ")");
 }