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);
  }
  public void testNullMessageToAll() throws Exception {
    d1.setRequestHandler(new MyHandler(null));

    c2 = createChannel(c1);
    c2.setName("B");
    disableBundling(c2);
    long stop, start = System.currentTimeMillis();
    d2 = new MessageDispatcher(c2, null, null, new MyHandler(null));
    stop = System.currentTimeMillis();
    c2.connect("MessageDispatcherUnitTest");
    Assert.assertEquals(2, c2.getView().size());
    System.out.println("view: " + c2.getView());

    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);
    Object ret = rsp.getValue();
    assert ret == null;

    rsp = rsps.get(c2.getAddress());
    assertNotNull(rsp);
    ret = rsp.getValue();
    assert ret == null;

    Util.close(c2);
  }
Ejemplo n.º 3
0
  /**
   * Sends a unicast message and - depending on the options - returns a result
   *
   * @param msg the message to be sent. The destination needs to be non-null
   * @param opts the options to be used
   * @return T the result
   * @throws Exception If there was problem sending the request, processing it at the receiver, or
   *     processing it at the sender.
   * @throws TimeoutException If the call didn't succeed within the timeout defined in options (if
   *     set)
   */
  public <T> T sendMessage(Message msg, RequestOptions opts) throws Exception {
    Address dest = msg.getDest();
    if (dest == null)
      throw new IllegalArgumentException("message destination is null, cannot send message");

    if (opts != null) {
      msg.setFlag(opts.getFlags()).setTransientFlag(opts.getTransientFlags());
      if (opts.getScope() > 0) msg.setScope(opts.getScope());
      if (opts.getMode() == ResponseMode.GET_NONE) async_unicasts.incrementAndGet();
      else sync_unicasts.incrementAndGet();
    }

    UnicastRequest<T> req = new UnicastRequest<T>(msg, corr, dest, opts);
    req.execute();

    if (opts != null && opts.getMode() == ResponseMode.GET_NONE) return null;

    Rsp<T> rsp = req.getResult();
    if (rsp.wasSuspected()) throw new SuspectedException(dest);

    Throwable exception = rsp.getException();
    if (exception != null) {
      if (exception instanceof Error) throw (Error) exception;
      else if (exception instanceof RuntimeException) throw (RuntimeException) exception;
      else if (exception instanceof Exception) throw (Exception) exception;
      else throw new RuntimeException(exception);
    }

    if (rsp.wasUnreachable()) throw new UnreachableException(dest);
    if (!rsp.wasReceived() && !req.responseReceived())
      throw new TimeoutException("timeout sending message to " + dest);
    return rsp.getValue();
  }
Ejemplo n.º 4
0
  /** Kicks off the benchmark on all cluster nodes */
  void startBenchmark() throws Throwable {
    RequestOptions options = new RequestOptions(ResponseMode.GET_ALL, 0);
    options.setFlags(Message.OOB, Message.DONT_BUNDLE, Message.NO_FC);
    RspList<Object> responses = disp.callRemoteMethods(null, new MethodCall(START), options);

    long total_reqs = 0;
    long total_time = 0;

    System.out.println("\n======================= Results: ===========================");
    for (Map.Entry<Address, Rsp<Object>> entry : responses.entrySet()) {
      Address mbr = entry.getKey();
      Rsp rsp = entry.getValue();
      Results result = (Results) rsp.getValue();
      total_reqs += result.num_gets + result.num_puts;
      total_time += result.time;
      System.out.println(mbr + ": " + result);
    }
    double total_reqs_sec = total_reqs / (total_time / 1000.0);
    double throughput = total_reqs_sec * msg_size;
    double ms_per_req = total_time / (double) total_reqs;
    Protocol prot = channel.getProtocolStack().findProtocol(unicast_protocols);
    System.out.println("\n");
    System.out.println(
        Util.bold(
            "Average of "
                + f.format(total_reqs_sec)
                + " requests / sec ("
                + Util.printBytes(throughput)
                + " / sec), "
                + f.format(ms_per_req)
                + " ms /request (prot="
                + prot.getName()
                + ")"));
    System.out.println("\n\n");
  }
Ejemplo n.º 5
0
 @Override
 public Object dispatch(
     Collection<Address> addresses, String sessionId, Method method, Object[] args)
     throws Exception {
   try {
     if (addresses.size() == 1) {
       return dispatch(ToolBox.random(addresses), sessionId, method, args);
     } else {
       Address address = ToolBox.random(addresses);
       CheckMaster ch = method.getAnnotation(CheckMaster.class);
       if (ch == null && "execute".equals(method.getName()))
         ch = args[0].getClass().getAnnotation(CheckMaster.class);
       RspList<Boolean> ret =
           iDispatcher.callRemoteMethods(
               addresses,
               "hasMaster",
               new Object[] {sessionId},
               new Class[] {String.class},
               SolverServerImplementation.sAllResponses);
       if (ch != null && ch.value() == Master.REQUIRED) {
         for (Rsp<Boolean> rsp : ret) {
           if (rsp != null && rsp.getValue()) {
             address = rsp.getSender();
             break;
           }
         }
       } else {
         List<Address> slaves = new ArrayList<Address>();
         for (Rsp<Boolean> rsp : ret) {
           if (rsp != null && !rsp.getValue()) {
             slaves.add(rsp.getSender());
           }
         }
         if (!slaves.isEmpty()) address = ToolBox.random(slaves);
       }
       return dispatch(address, sessionId, method, args);
     }
   } catch (InvocationTargetException e) {
     throw (Exception) e.getTargetException();
   }
 }
 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.º 7
0
 @Override
 public Address getLocalAddress() {
   try {
     RspList<Boolean> ret =
         iDispatcher.callRemoteMethods(
             null, "isLocal", new Object[] {}, new Class[] {}, sAllResponses);
     for (Rsp<Boolean> local : ret) {
       if (Boolean.TRUE.equals(local.getValue())) return local.getSender();
     }
     return null;
   } catch (Exception e) {
     sLog.error("Failed to retrieve local address: " + e.getMessage(), e);
     return null;
   }
 }
 private boolean containsOnlyNulls(RspList<Object> l) {
   for (Rsp<Object> r : l.values()) {
     if (r.getValue() != null || !r.wasReceived() || r.wasSuspected()) return false;
   }
   return true;
 }
Ejemplo n.º 9
0
  @Override
  public Map<Address, Response> invokeRemotely(
      Collection<Address> recipients,
      ReplicableCommand rpcCommand,
      ResponseMode mode,
      long timeout,
      boolean usePriorityQueue,
      ResponseFilter responseFilter,
      boolean supportReplay)
      throws Exception {

    if (recipients != null && recipients.isEmpty()) {
      // don't send if dest list is empty
      log.trace("Destination list is empty: no need to send message");
      return Collections.emptyMap();
    }

    if (trace)
      log.tracef(
          "dests=%s, command=%s, mode=%s, timeout=%s", recipients, rpcCommand, mode, timeout);

    if (mode.isSynchronous() && recipients != null && !getMembers().containsAll(recipients)) {
      if (mode == ResponseMode.SYNCHRONOUS)
        throw new SuspectException(
            "One or more nodes have left the cluster while replicating command " + rpcCommand);
      else { // SYNCHRONOUS_IGNORE_LEAVERS || WAIT_FOR_VALID_RESPONSE
        recipients = new ArrayList<Address>(recipients);
        recipients.retainAll(getMembers());
      }
    }
    boolean asyncMarshalling = mode == ResponseMode.ASYNCHRONOUS;
    if (!usePriorityQueue
        && (ResponseMode.SYNCHRONOUS == mode || ResponseMode.SYNCHRONOUS_IGNORE_LEAVERS == mode))
      usePriorityQueue = true;

    boolean broadcast = recipients == null || recipients.size() == members.size();
    RspList<Object> rsps =
        dispatcher.invokeRemoteCommands(
            toJGroupsAddressList(recipients),
            rpcCommand,
            toJGroupsMode(mode),
            timeout,
            recipients != null,
            usePriorityQueue,
            toJGroupsFilter(responseFilter),
            supportReplay,
            asyncMarshalling,
            broadcast);

    if (mode.isAsynchronous()) return Collections.emptyMap(); // async case

    // short-circuit no-return-value calls.
    if (rsps == null) return Collections.emptyMap();
    Map<Address, Response> retval = new HashMap<Address, Response>(rsps.size());

    boolean ignoreLeavers =
        mode == ResponseMode.SYNCHRONOUS_IGNORE_LEAVERS
            || mode == ResponseMode.WAIT_FOR_VALID_RESPONSE;
    boolean noValidResponses = true;
    for (Rsp<Object> rsp : rsps.values()) {
      noValidResponses &=
          parseResponseAndAddToResponseList(
              rsp.getValue(),
              rsp.getException(),
              retval,
              rsp.wasSuspected(),
              rsp.wasReceived(),
              fromJGroupsAddress(rsp.getSender()),
              responseFilter != null,
              ignoreLeavers);
    }

    if (noValidResponses) throw new TimeoutException("Timed out waiting for valid responses!");
    return retval;
  }
Ejemplo n.º 10
0
  @Override
  public Map<Address, Response> invokeRemotely(
      Collection<Address> recipients,
      ReplicableCommand rpcCommand,
      ResponseMode mode,
      long timeout,
      boolean usePriorityQueue,
      ResponseFilter responseFilter)
      throws Exception {

    if (recipients != null && recipients.isEmpty()) {
      // don't send if recipients list is empty
      log.trace("Destination list is empty: no need to send message");
      return Collections.emptyMap();
    }

    if (trace)
      log.tracef(
          "dests=%s, command=%s, mode=%s, timeout=%s", recipients, rpcCommand, mode, timeout);
    Address self = getAddress();
    boolean ignoreLeavers =
        mode == ResponseMode.SYNCHRONOUS_IGNORE_LEAVERS
            || mode == ResponseMode.WAIT_FOR_VALID_RESPONSE;
    if (mode.isSynchronous() && recipients != null && !getMembers().containsAll(recipients)) {
      if (ignoreLeavers) { // SYNCHRONOUS_IGNORE_LEAVERS || WAIT_FOR_VALID_RESPONSE
        recipients = new HashSet<Address>(recipients);
        recipients.retainAll(getMembers());
      } else { // SYNCHRONOUS
        throw new SuspectException(
            "One or more nodes have left the cluster while replicating command " + rpcCommand);
      }
    }
    boolean asyncMarshalling = mode == ResponseMode.ASYNCHRONOUS;
    if (!usePriorityQueue
        && (ResponseMode.SYNCHRONOUS == mode || ResponseMode.SYNCHRONOUS_IGNORE_LEAVERS == mode))
      usePriorityQueue = true;

    List<org.jgroups.Address> jgAddressList = toJGroupsAddressListExcludingSelf(recipients);
    int membersSize = members.size();
    boolean broadcast = jgAddressList == null || recipients.size() == membersSize;
    if (membersSize < 3 || (jgAddressList != null && jgAddressList.size() < 2)) broadcast = false;
    RspList<Object> rsps = null;
    Response singleResponse = null;
    org.jgroups.Address singleJGAddress = null;

    if (broadcast) {
      rsps =
          dispatcher.broadcastRemoteCommands(
              rpcCommand,
              toJGroupsMode(mode),
              timeout,
              recipients != null,
              usePriorityQueue,
              toJGroupsFilter(responseFilter),
              asyncMarshalling);
    } else {
      if (jgAddressList == null || !jgAddressList.isEmpty()) {
        boolean singleRecipient =
            !ignoreLeavers && jgAddressList != null && jgAddressList.size() == 1;
        boolean skipRpc = false;
        if (jgAddressList == null) {
          ArrayList<Address> others = new ArrayList<Address>(members);
          others.remove(self);
          skipRpc = others.isEmpty();
          singleRecipient = !ignoreLeavers && others.size() == 1;
          if (singleRecipient) singleJGAddress = toJGroupsAddress(others.get(0));
        }
        if (!skipRpc) {
          if (singleRecipient) {
            if (singleJGAddress == null) singleJGAddress = jgAddressList.get(0);
            singleResponse =
                dispatcher.invokeRemoteCommand(
                    singleJGAddress,
                    rpcCommand,
                    toJGroupsMode(mode),
                    timeout,
                    usePriorityQueue,
                    asyncMarshalling);
          } else {
            rsps =
                dispatcher.invokeRemoteCommands(
                    jgAddressList,
                    rpcCommand,
                    toJGroupsMode(mode),
                    timeout,
                    recipients != null,
                    usePriorityQueue,
                    toJGroupsFilter(responseFilter),
                    asyncMarshalling);
          }
        }
      }
    }

    if (mode.isAsynchronous()) return Collections.emptyMap(); // async case

    Map<Address, Response> responses;
    if (rsps == null) {
      if (singleJGAddress == null
          || (singleResponse == null && rpcCommand instanceof ClusteredGetCommand)) {
        responses = Collections.emptyMap();
      } else {
        responses = Collections.singletonMap(fromJGroupsAddress(singleJGAddress), singleResponse);
      }
    } else {
      Map<Address, Response> retval = new HashMap<Address, Response>(rsps.size());

      boolean noValidResponses = true;
      for (Rsp<Object> rsp : rsps.values()) {
        noValidResponses &=
            parseResponseAndAddToResponseList(
                rsp.getValue(),
                rsp.getException(),
                retval,
                rsp.wasSuspected(),
                rsp.wasReceived(),
                fromJGroupsAddress(rsp.getSender()),
                responseFilter != null,
                ignoreLeavers);
      }

      if (noValidResponses) throw new TimeoutException("Timed out waiting for valid responses!");
      responses = retval;
    }
    return responses;
  }