/**
     * Do a http operation
     *
     * @param urlConnection the HttpURLConnection to be used
     * @param inputData if not null,will be written to the urlconnection.
     * @param hasOutput if true, read content back from the urlconnection
     * @return Response
     */
    private Response doOperation(
        HttpURLConnection urlConnection, byte[] inputData, boolean hasOutput) {
      Response response = null;
      InputStream inputStream = null;
      OutputStream outputStream = null;
      byte[] payload = null;
      try {
        if (inputData != null) {
          urlConnection.setDoOutput(true);
          outputStream = urlConnection.getOutputStream();
          outputStream.write(inputData);
        }
        if (hasOutput) {
          inputStream = urlConnection.getInputStream();
          payload = Util.readFileContents(urlConnection.getInputStream());
        }
        response =
            new Response(urlConnection.getHeaderFields(), urlConnection.getResponseCode(), payload);

      } catch (IOException e) {
        log.error("Error calling service", e);
      } finally {
        Util.close(inputStream);
        Util.close(outputStream);
      }
      return response;
    }
Example #2
0
 /**
  * Do a http operation
  *
  * @param urlConnection the HttpURLConnection to be used
  * @param inputData if not null,will be written to the urlconnection.
  * @param hasOutput if true, read content back from the urlconnection
  * @return Response
  * @throws IOException
  */
 public static HttpResponse doOperation(
     HttpURLConnection urlConnection, byte[] inputData, boolean hasOutput) throws IOException {
   HttpResponse response = null;
   InputStream inputStream = null;
   OutputStream outputStream = null;
   byte[] payload = null;
   try {
     if (inputData != null) {
       urlConnection.setDoOutput(true);
       outputStream = urlConnection.getOutputStream();
       outputStream.write(inputData);
     }
     /*
      * Get response code first. HttpURLConnection does not allow to
      * read inputstream if response code is not success code
      */
     int responseCode = urlConnection.getResponseCode();
     if (hasOutput && isSuccessCode(responseCode)) {
       payload = getBytes(urlConnection.getInputStream());
     }
     response = new HttpResponse(urlConnection.getHeaderFields(), responseCode, payload);
   } finally {
     Util.close(inputStream);
     Util.close(outputStream);
   }
   return response;
 }
Example #3
0
  public void stop() {
    Util.close(srv_sock);

    if (acceptor != null && acceptor.isAlive()) acceptor.interrupt();

    Util.sleep(500);
    if (!sockets.isEmpty()) {
      for (Socket sock : sockets) Util.close(sock);
    }
  }
Example #4
0
  /** Sends a GET_MBR_REQ to *all* GossipRouters, merges responses. */
  private List _getMembers(String group) {
    List ret = new LinkedList();
    Socket sock = null;
    SocketAddress destAddr;
    DataOutputStream out = null;
    DataInputStream in = null;
    IpAddress entry;
    GossipData gossip_req, gossip_rsp;
    Address mbr;

    for (int i = 0; i < gossip_servers.size(); i++) {
      entry = (IpAddress) gossip_servers.elementAt(i);
      if (entry.getIpAddress() == null || entry.getPort() == 0) {
        if (log.isErrorEnabled()) log.error("entry.host or entry.port is null");
        continue;
      }

      try {
        // sock=new Socket(entry.getIpAddress(), entry.getPort());
        sock = new Socket();
        destAddr = new InetSocketAddress(entry.getIpAddress(), entry.getPort());
        sock.connect(destAddr, SOCKET_TIMEOUT);
        out = new DataOutputStream(sock.getOutputStream());

        gossip_req = new GossipData(GossipRouter.GOSSIP_GET, group, null, null);
        // must send GossipData as fast as possible, otherwise the
        // request might be rejected
        gossip_req.writeTo(out);
        out.flush();

        in = new DataInputStream(sock.getInputStream());
        gossip_rsp = new GossipData();
        gossip_rsp.readFrom(in);
        if (gossip_rsp.mbrs != null) { // merge with ret
          for (Iterator it = gossip_rsp.mbrs.iterator(); it.hasNext(); ) {
            mbr = (Address) it.next();
            if (!ret.contains(mbr)) ret.add(mbr);
          }
        }
      } catch (Exception ex) {
        if (log.isErrorEnabled()) log.error("exception connecting to host " + entry);
      } finally {
        Util.close(out);
        Util.close(in);
        if (sock != null) {
          try {
            sock.close();
          } catch (IOException e) {
          }
        }
      }
    }

    return ret;
  }
Example #5
0
  public void eventLoop() throws Exception {
    int c;

    while (true) {
      System.out.print(
          "[1] Send msgs "
              + "\n[6]Set sender threads ("
              + num_threads
              + ") [7] Set num msgs ("
              + num_msgs
              + ") "
              + "[8] Set msg size ("
              + Util.printBytes(msg_size)
              + ")"
              + "\n[o] Toggle OOB ("
              + oob
              + ") [b] Toggle dont_bundle ("
              + dont_bundle
              + ")\n[q] Quit\n");
      System.out.flush();
      c = System.in.read();
      switch (c) {
        case -1:
          break;
        case '1':
          sendMessages();
          break;
        case '6':
          setSenderThreads();
          break;
        case '7':
          setNumMessages();
          break;
        case '8':
          setMessageSize();
          break;
        case 'o':
          oob = !oob;
          System.out.println("oob=" + oob);
          break;
        case 'b':
          dont_bundle = !dont_bundle;
          System.out.println("dont_bundle = " + dont_bundle);
          break;
        case 'q':
          Util.close(srv_sock);
          Util.close(sock);
          return;
        default:
          break;
      }
    }
  }
  public void testRefcount() throws Exception {
    FORK fork = (FORK) ch.getProtocolStack().findProtocol(FORK.class);
    Protocol prot = fork.get("stack");
    assert prot == null;
    fc1 = new ForkChannel(ch, "stack", "fc1");
    prot = fork.get("stack");
    assert prot != null;
    ForkProtocolStack fork_stack = (ForkProtocolStack) getProtStack(prot);
    int inits = fork_stack.getInits();
    assert inits == 1 : "inits is " + inits + "(expected 1)";

    fc2 = new ForkChannel(ch, "stack", "fc2"); // uses the same fork stack "stack"
    inits = fork_stack.getInits();
    assert inits == 2 : "inits is " + inits + "(expected 2)";

    ch.connect(CLUSTER);

    fc1.connect(CLUSTER);
    int connects = fork_stack.getConnects();
    assert connects == 1 : "connects is " + connects + "(expected 1)";

    fc1.connect(CLUSTER); // duplicate connect()
    connects = fork_stack.getConnects();
    assert connects == 1 : "connects is " + connects + "(expected 1)";

    fc2.connect(CLUSTER);
    connects = fork_stack.getConnects();
    assert connects == 2 : "connects is " + connects + "(expected 2)";

    fc2.disconnect();
    fc2.disconnect(); // duplicate disconnect() !
    connects = fork_stack.getConnects();
    assert connects == 1 : "connects is " + connects + "(expected 1)";

    Util.close(fc2);
    inits = fork_stack.getInits();
    assert inits == 1 : "inits is " + inits + "(expected 1)";

    Util.close(fc2); // duplicate close()
    inits = fork_stack.getInits();
    assert inits == 1 : "inits is " + inits + "(expected 1)";

    Util.close(fc1);
    connects = fork_stack.getConnects();
    assert connects == 0 : "connects is " + connects + "(expected 0)";
    inits = fork_stack.getInits();
    assert inits == 0 : "inits is " + inits + "(expected 0)";

    prot = fork.get("stack");
    assert prot == null;
  }
Example #7
0
  protected List<PingData> read(InputStream in) {
    List<PingData> retval = null;
    try {
      while (true) {
        try {
          String name_str = Util.readToken(in);
          String uuid_str = Util.readToken(in);
          String addr_str = Util.readToken(in);
          String coord_str = Util.readToken(in);
          if (name_str == null || uuid_str == null || addr_str == null || coord_str == null) break;

          UUID uuid = null;
          try {
            long tmp = Long.valueOf(uuid_str);
            uuid = new UUID(0, tmp);
          } catch (Throwable t) {
            uuid = UUID.fromString(uuid_str);
          }

          PhysicalAddress phys_addr = new IpAddress(addr_str);
          boolean is_coordinator = coord_str.trim().equals("T") || coord_str.trim().equals("t");

          if (retval == null) retval = new ArrayList<>();
          retval.add(new PingData(uuid, true, name_str, phys_addr).coord(is_coordinator));
        } catch (Throwable t) {
          log.error(Util.getMessage("FailedReadingLineOfInputStream"), t);
        }
      }
      return retval;
    } finally {
      Util.close(in);
    }
  }
Example #8
0
  /** 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);
    }
  }
Example #9
0
  public void testFlushWithCrashedParticipants() throws Exception {
    JChannel c1 = null;
    JChannel c2 = null;
    JChannel c3 = null;

    try {
      c1 = createChannel(true, 3, "C1");
      changeProps(c1);
      c1.connect("testFlushWithCrashedFlushCoordinator");

      c2 = createChannel(c1, "C2");
      changeProps(c2);
      c2.connect("testFlushWithCrashedFlushCoordinator");

      c3 = createChannel(c1, "C3");
      changeProps(c3);
      c3.connect("testFlushWithCrashedFlushCoordinator");

      // and then kill members other than flush coordinator
      Util.shutdown(c3);
      Util.shutdown(c1);

      // start flush
      Util.startFlush(c2);

      c2.stopFlush();
      Util.waitUntilAllChannelsHaveSameSize(10000, 500, c2);

      // cluster should not hang and one remaining member should have a correct view
      assertTrue("correct view size", c2.getView().size() == 1);
    } finally {
      Util.close(c3, c2, c1);
    }
  }
Example #10
0
 public void close() throws IOException {
   // can close even if start was never called...
   send_lock.lock();
   try {
     connectionPeerReceiver.stop();
     if (isSenderUsed()) {
       sender.stop();
     }
     Util.close(sock);
     Util.close(out);
     Util.close(in);
   } finally {
     send_lock.unlock();
   }
   mapper.notifyConnectionClosed(peer_addr);
 }
Example #11
0
  /** Tests a merge between ViewIds of the same coord, e.g. A|6, A|7, A|8, A|9 */
  public void testViewsBySameCoord() {
    View v1 =
        View.create(
            a.getAddress(),
            6,
            a.getAddress(),
            b.getAddress(),
            c.getAddress(),
            d.getAddress()); // {A,B,C,D}
    View v2 =
        View.create(a.getAddress(), 7, a.getAddress(), b.getAddress(), c.getAddress()); // {A,B,C}
    View v3 = View.create(a.getAddress(), 8, a.getAddress(), b.getAddress()); // {A,B}
    View v4 = View.create(a.getAddress(), 9, a.getAddress()); // {A}

    Util.close(b, c, d); // not interested in those...

    MERGE3 merge = (MERGE3) a.getProtocolStack().findProtocol(MERGE3.class);
    for (View view : Arrays.asList(v1, v2, v4, v3)) {
      MERGE3.MergeHeader hdr = MERGE3.MergeHeader.createInfo(view.getViewId(), null, null);
      Message msg = new Message(null, a.getAddress(), null).putHeader(merge.getId(), hdr);
      merge.up(new Event(Event.MSG, msg));
    }

    merge.checkInconsistencies(); // no merge will happen

    Util.waitUntilAllChannelsHaveSameSize(10000, 500, a);
    System.out.println("A's view: " + a.getView());
    assert a.getView().size() == 1;
    assert a.getView().containsMember(a.getAddress());
  }
Example #12
0
  /**
   * Tests A|6, A|7, A|8 and B|7, B|8, B|9 -> we should have a subviews in MergeView consisting of
   * only 2 elements: A|5 and B|5
   */
  public void testMultipleViewsBySameMembers() throws Exception {
    View a1 =
        View.create(
            a.getAddress(),
            6,
            a.getAddress(),
            b.getAddress(),
            c.getAddress(),
            d.getAddress()); // {A,B,C,D}
    View a2 =
        View.create(a.getAddress(), 7, a.getAddress(), b.getAddress(), c.getAddress()); // {A,B,C}
    View a3 = View.create(a.getAddress(), 8, a.getAddress(), b.getAddress()); // {A,B}
    View a4 = View.create(a.getAddress(), 9, a.getAddress()); // {A}

    View b1 = View.create(b.getAddress(), 7, b.getAddress(), c.getAddress(), d.getAddress());
    View b2 = View.create(b.getAddress(), 8, b.getAddress(), c.getAddress());
    View b3 = View.create(b.getAddress(), 9, b.getAddress());

    Util.close(c, d); // not interested in those...

    // A and B cannot communicate:
    discard(true, a, b);

    // inject view A|6={A} into A and B|5={B} into B
    injectView(a4, a);
    injectView(b3, b);

    assert a.getView().equals(a4);
    assert b.getView().equals(b3);

    List<Event> merge_events = new ArrayList<>();
    for (View view : Arrays.asList(a3, a4, a2, a1)) {
      MERGE3.MergeHeader hdr = MERGE3.MergeHeader.createInfo(view.getViewId(), null, null);
      Message msg = new Message(null, a.getAddress(), null).putHeader(merge_id, hdr);
      merge_events.add(new Event(Event.MSG, msg));
    }
    for (View view : Arrays.asList(b2, b3, b1)) {
      MERGE3.MergeHeader hdr = MERGE3.MergeHeader.createInfo(view.getViewId(), null, null);
      Message msg = new Message(null, b.getAddress(), null).putHeader(merge_id, hdr);
      merge_events.add(new Event(Event.MSG, msg));
    }

    // A and B can communicate again
    discard(false, a, b);

    injectMergeEvents(merge_events, a, b);
    checkInconsistencies(a, b); // merge will happen between A and B
    Util.waitUntilAllChannelsHaveSameSize(10000, 500, a, b);
    System.out.println("A's view: " + a.getView() + "\nB's view: " + b.getView());
    assert a.getView().size() == 2;
    assert a.getView().containsMember(a.getAddress());
    assert a.getView().containsMember(b.getAddress());
    assert a.getView().equals(b.getView());
    for (View merge_view : Arrays.asList(getViewFromGMS(a), getViewFromGMS(b))) {
      System.out.println(merge_view);
      assert merge_view instanceof MergeView;
      List<View> subviews = ((MergeView) merge_view).getSubgroups();
      assert subviews.size() == 2;
    }
  }
  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);
  }
Example #15
0
  public void run() {
    final byte[] receive_buf = new byte[65535];
    DatagramPacket packet = new DatagramPacket(receive_buf, receive_buf.length);
    byte[] data;
    ByteArrayInputStream inp_stream;
    DataInputStream inp = null;
    Message msg;

    while (mcast_sock != null && receiver != null && Thread.currentThread().equals(receiver)) {
      packet.setData(receive_buf, 0, receive_buf.length);
      try {
        mcast_sock.receive(packet);
        data = packet.getData();
        inp_stream = new ExposedByteArrayInputStream(data, 0, data.length);
        inp = new DataInputStream(inp_stream);
        msg = new Message();
        msg.readFrom(inp);
        up(new Event(Event.MSG, msg));
      } catch (SocketException socketEx) {
        break;
      } catch (Throwable ex) {
        log.error("failed receiving packet (from " + packet.getSocketAddress() + ")", ex);
      } finally {
        Util.close(inp);
      }
    }
    if (log.isTraceEnabled()) log.trace("receiver thread terminated");
  }
Example #16
0
 public void run() {
   while (!srv_sock.isClosed()) {
     Socket client_sock = null;
     DataInputStream in = null;
     try {
       client_sock = srv_sock.accept();
       client_sock.setTcpNoDelay(TCP_NODELAY);
       client_sock.setReceiveBufferSize(SOCK_RECV_BUF_SIZE);
       client_sock.setSendBufferSize(SOCK_SEND_BUF_SIZE);
       in = new DataInputStream(new BufferedInputStream(client_sock.getInputStream()));
       while (!client_sock.isClosed()) handleRequest(in);
     } catch (Exception e) {
       Util.close(client_sock);
       Util.close(in);
     }
   }
 }
Example #17
0
 public static XmlConfigurator getInstance(URL url, Boolean validate) throws java.io.IOException {
   InputStream is = url.openStream();
   try {
     return getInstance(is, validate);
   } finally {
     Util.close(is);
   }
 }
  public void testLifecycle() throws Exception {
    fc1 = new ForkChannel(ch, "stack", "fc1");
    assert fc1.isOpen() && !fc1.isConnected() && !fc1.isClosed() : "state=" + fc1.getState();

    ch.connect(CLUSTER);
    assert fc1.isOpen() && !fc1.isConnected() && !fc1.isClosed() : "state=" + fc1.getState();

    fc1.connect("bla");
    assert fc1.isOpen() && fc1.isConnected() && !fc1.isClosed() : "state=" + fc1.getState();

    assert ch.getAddress().equals(fc1.getAddress());
    assert ch.getClusterName().equals(fc1.getClusterName());
    assert ch.getView().equals(fc1.getView());

    fc1.disconnect();
    assert fc1.isOpen() && !fc1.isConnected() && !fc1.isClosed() : "state=" + fc1.getState();

    fc1.connect("foobar");
    assert fc1.isOpen() && fc1.isConnected() && !fc1.isClosed() : "state=" + fc1.getState();

    Util.close(fc1);
    assert !fc1.isOpen() && !fc1.isConnected() && fc1.isClosed() : "state=" + fc1.getState();

    try {
      fc1.connect("whocares");
      assert false : "a closed fork channel cannot be reconnected";
    } catch (Exception ex) {
      assert ex instanceof IllegalStateException;
    }
    assert !fc1.isOpen() && !fc1.isConnected() && fc1.isClosed() : "state=" + fc1.getState();

    Util.close(ch);
    assert !fc1.isOpen() && !fc1.isConnected() && fc1.isClosed() : "state=" + fc1.getState();

    try {
      fc1.send(null, "hello");
      assert false
          : "sending on a fork-channel with a disconnected main-channel should throw an exception";
    } catch (Throwable t) {
      System.out.println(
          "got an exception (as expected) sending on a fork-channel where the main-channel is disconnected: "
              + t);
    }
  }
Example #19
0
 /**
  * Acceptor thread. Continuously accept new connections. Create a new thread for each new
  * connection and put it in conns. When the thread should stop, it is interrupted by the thread
  * creator.
  */
 public void run() {
   while (!srv_sock.isClosed() && !Thread.currentThread().isInterrupted()) {
     TCPConnection conn = null;
     Socket client_sock = null;
     try {
       client_sock = srv_sock.accept();
       conn = new TCPConnection(client_sock);
       Address peer_addr = conn.getPeerAddress();
       mapper.getLock().lock();
       try {
         boolean currentConnectionOpen = mapper.hasOpenConnection(peer_addr);
         boolean replaceWithNewConnection = false;
         if (currentConnectionOpen) {
           replaceWithNewConnection = peer_addr.compareTo(local_addr) > 0;
         }
         if (!currentConnectionOpen || replaceWithNewConnection) {
           mapper.removeConnection(peer_addr);
           mapper.addConnection(peer_addr, conn);
           conn.start(mapper.getThreadFactory()); // starts handler thread on this socket
         } else {
           Util.close(conn);
         }
       } finally {
         mapper.getLock().unlock();
       }
     } catch (SocketException se) {
       boolean threadExiting = srv_sock.isClosed() || Thread.currentThread().isInterrupted();
       if (threadExiting) {
         break;
       } else {
         if (log.isWarnEnabled()) log.warn("Could not accept connection from peer ", se);
         Util.close(conn);
         Util.close(client_sock);
       }
     } catch (Exception ex) {
       if (log.isWarnEnabled()) log.warn("Could not read accept connection from peer " + ex);
       Util.close(conn);
       Util.close(client_sock);
     }
   }
   if (log.isTraceEnabled()) log.trace(Thread.currentThread().getName() + " terminated");
 }
  public void testRefcount2() throws Exception {
    Prot p1 = new Prot("P1"), p2 = new Prot("P2");
    fc1 = new ForkChannel(ch, "stack", "fc1", p1, p2);
    fc2 = new ForkChannel(ch, "stack", "fc2"); // uses p1 and p2 from fc1
    fc3 = new ForkChannel(ch, "stack", "fc3"); // uses p1 and p2 from fc1

    assert p1.inits == 1 && p2.inits == 1;

    FORK fork = (FORK) ch.getProtocolStack().findProtocol(FORK.class);
    Protocol prot = fork.get("stack");
    ForkProtocolStack fork_stack = (ForkProtocolStack) getProtStack(prot);
    int inits = fork_stack.getInits();
    assert inits == 3;

    ch.connect(CLUSTER);
    fc1.connect(CLUSTER);
    int connects = fork_stack.getConnects();
    assert connects == 1;
    assert p1.starts == 1 && p2.starts == 1;

    fc2.connect(CLUSTER);
    fc3.connect(CLUSTER);
    connects = fork_stack.getConnects();
    assert connects == 3;
    assert p1.starts == 1 && p2.starts == 1;

    fc3.disconnect();
    fc2.disconnect();
    assert p1.starts == 1 && p2.starts == 1;
    assert p1.stops == 0 && p2.stops == 0;

    fc1.disconnect();
    assert p1.starts == 1 && p2.starts == 1;
    assert p1.stops == 1 && p2.stops == 1;

    Util.close(fc3, fc2);
    assert p1.destroys == 0 && p2.destroys == 0;

    Util.close(fc1);
    assert p1.destroys == 1 && p2.destroys == 1;
  }
 public void testIncorrectLifecycle() throws Exception {
   fc1 = new ForkChannel(ch, "stack", "fc1");
   ch.connect(CLUSTER);
   fc1.connect(CLUSTER);
   Util.close(fc1);
   try {
     fc1.connect(CLUSTER);
     assert false : "reconnecting a closed fork channel must throw an exception";
   } catch (Exception ex) {
     assert ex instanceof IllegalStateException;
     System.out.println("got exception as expected: " + ex);
   }
 }
Example #22
0
  /** Tests the scenario described by Dan in https://issues.jboss.org/browse/JGRP-1876 */
  public void testJGRP_1876_Dan() throws Exception {
    Util.close(d, c, b, a);
    s = createChannel("S", true);
    t = createChannel("T", true);
    u = createChannel("U", true);
    v = createChannel("V", true);
    Util.waitUntilAllChannelsHaveSameSize(10000, 500, s, t, u, v);
    enableInfoSender(false, s, t, u, v); // stops INFO sending in MERGE3
    for (JChannel ch : Arrays.asList(s, t, u, v))
      System.out.println(ch.getName() + ": " + ch.getView());
    View v1 = View.create(s.getAddress(), 10, s.getAddress());
    View v2 = View.create(t.getAddress(), 10, t.getAddress());
    View v3 = View.create(u.getAddress(), 11, u.getAddress(), v.getAddress());

    injectView(v1, s);
    injectView(v2, t);
    injectView(v3, u, v);
    enableInfoSender(false, s, t, u, v); // stops INFO sending in MERGE3
    Util.waitUntilAllChannelsHaveSameSize(10000, 500, s);
    Util.waitUntilAllChannelsHaveSameSize(10000, 500, t);
    Util.waitUntilAllChannelsHaveSameSize(10000, 500, u, v);

    System.out.printf("\nPartitions:\n");
    for (JChannel ch : Arrays.asList(s, t, u, v))
      System.out.println(ch.getName() + ": " + ch.getView());

    enableInfoSender(true, s, t, u, v);
    System.out.println("\nEnabling INFO sending in merge protocols to merge subclusters");

    Util.waitUntilAllChannelsHaveSameSize(30000, 1000, s, t, u, v);
    System.out.println("\nResulting views:");
    for (JChannel ch : Arrays.asList(s, t, u, v)) {
      GMS gms = (GMS) ch.getProtocolStack().findProtocol(GMS.class);
      View mv = gms.view();
      System.out.println(mv);
    }
    for (JChannel ch : Arrays.asList(s, t, u, v)) {
      GMS gms = (GMS) ch.getProtocolStack().findProtocol(GMS.class);
      View mv = gms.view();
      assert mv instanceof MergeView;
      assert mv.size() == 4;
      assert ((MergeView) mv).getSubgroups().size() == 3;
    }
    for (JChannel ch : Arrays.asList(s, t, u, v)) {
      View view = ch.getView();
      assert view.size() == 4 : "view should have 4 members: " + view;
    }
  }
Example #23
0
 public void close() throws IOException {
   send_lock.lock();
   try {
     Util.close(out, in, sock);
     if (receiver != null) {
       receiver.stop();
       receiver = null;
     }
     if (sender != null) {
       sender.stop();
       sender = null;
     }
   } finally {
     send_lock.unlock();
   }
 }
Example #24
0
  /** A: {A,B} B: {A,B} C: {C} C receives INFO from B only */
  public void testMergeWithIncompleteInfos() throws Exception {
    Util.close(d);
    enableInfoSender(false, a, b, c);
    View one = View.create(a.getAddress(), 10, a.getAddress(), b.getAddress());
    View two = View.create(c.getAddress(), 10, c.getAddress());
    injectView(one, a, b);
    injectView(two, c);
    enableInfoSender(false, a, b, c);

    Util.waitUntilAllChannelsHaveSameSize(10000, 500, a, b);
    Util.waitUntilAllChannelsHaveSameSize(10000, 500, c);
    System.out.printf("\nPartitions:\n");
    for (JChannel ch : Arrays.asList(a, b, c))
      System.out.println(ch.getName() + ": " + ch.getView());

    MERGE3.MergeHeader hdr = MERGE3.MergeHeader.createInfo(one.getViewId(), null, null);
    Message msg =
        new Message(null, b.getAddress(), null)
            .putHeader(merge_id, hdr); // B sends the INFO message to C
    Event merge_event = new Event(Event.MSG, msg);
    MERGE3 merge = (MERGE3) c.getProtocolStack().findProtocol(MERGE3.class);
    merge.up(merge_event);
    enableInfoSender(true, a, b, c);
    System.out.println("\nEnabling INFO sending in merge protocols to merge subclusters");

    Util.waitUntilAllChannelsHaveSameSize(30000, 1000, a, b, c);
    System.out.println("\nResulting views:");
    for (JChannel ch : Arrays.asList(a, b, c)) {
      GMS gms = (GMS) ch.getProtocolStack().findProtocol(GMS.class);
      View mv = gms.view();
      System.out.println(mv);
    }
    for (JChannel ch : Arrays.asList(a, b, c)) {
      GMS gms = (GMS) ch.getProtocolStack().findProtocol(GMS.class);
      View mv = gms.view();
      assert mv instanceof MergeView;
      assert mv.size() == 3;
      assert ((MergeView) mv).getSubgroups().size() == 2;
    }
    for (JChannel ch : Arrays.asList(a, b, c)) {
      View view = ch.getView();
      assert view.size() == 3 : "view should have 3 members: " + view;
    }
  }
Example #25
0
  /**
   * Initialisation if a supplied key is defined in the properties. This supplied key must be in a
   * keystore which can be generated using the keystoreGenerator file in demos. The keystore must be
   * on the classpath to find it.
   *
   * @throws KeyStoreException
   * @throws Exception
   * @throws IOException
   * @throws NoSuchAlgorithmException
   * @throws CertificateException
   * @throws UnrecoverableKeyException
   */
  private void initConfiguredKey() throws Exception {
    InputStream inputStream = null;
    // must not use default keystore type - as does not support secret keys
    KeyStore store = KeyStore.getInstance("JCEKS");

    SecretKey tempKey = null;
    try {
      // load in keystore using this thread's classloader
      inputStream =
          Thread.currentThread().getContextClassLoader().getResourceAsStream(keyStoreName);
      if (inputStream == null) inputStream = new FileInputStream(keyStoreName);
      // we can't find a keystore here -
      if (inputStream == null) {
        throw new Exception(
            "Unable to load keystore " + keyStoreName + " ensure file is on classpath");
      }
      // we have located a file lets load the keystore
      try {
        store.load(inputStream, storePassword.toCharArray());
        // loaded keystore - get the key
        tempKey = (SecretKey) store.getKey(alias, keyPassword.toCharArray());
      } catch (IOException e) {
        throw new Exception("Unable to load keystore " + keyStoreName + ": " + e);
      } catch (NoSuchAlgorithmException e) {
        throw new Exception("No Such algorithm " + keyStoreName + ": " + e);
      } catch (CertificateException e) {
        throw new Exception("Certificate exception " + keyStoreName + ": " + e);
      }

      if (tempKey == null)
        throw new Exception("Unable to retrieve key '" + alias + "' from keystore " + keyStoreName);
      // set the key here
      setSecretKey(tempKey);

      if (symAlgorithm.equals(DEFAULT_SYM_ALGO)) symAlgorithm = tempKey.getAlgorithm();

      // set the fact we are using a supplied key
      suppliedKey = true;
      queue_down = queue_up = false;
    } finally {
      Util.close(inputStream);
    }
  }
Example #26
0
 protected void connect(Address dest, boolean send_local_addr) throws Exception {
   SocketAddress destAddr =
       new InetSocketAddress(((IpAddress) dest).getIpAddress(), ((IpAddress) dest).getPort());
   try {
     if (!server.defer_client_binding)
       this.sock.bind(new InetSocketAddress(server.client_bind_addr, server.client_bind_port));
     if (this.sock.getLocalSocketAddress() != null
         && this.sock.getLocalSocketAddress().equals(destAddr))
       throw new IllegalStateException(
           "socket's bind and connect address are the same: " + destAddr);
     Util.connect(this.sock, destAddr, server.sock_conn_timeout);
     this.out = new DataOutputStream(new BufferedOutputStream(sock.getOutputStream()));
     this.in = new DataInputStream(new BufferedInputStream(sock.getInputStream()));
     if (send_local_addr) sendLocalAddress(server.localAddress());
   } catch (Exception t) {
     Util.close(this.sock);
     throw t;
   }
 }
Example #27
0
  void _unregister(String group, Address mbr) {
    Socket sock = null;
    DataOutputStream out = null;
    IpAddress entry;
    GossipData gossip_req;

    for (int i = 0; i < gossip_servers.size(); i++) {
      entry = (IpAddress) gossip_servers.elementAt(i);
      if (entry.getIpAddress() == null || entry.getPort() == 0) {
        if (log.isErrorEnabled()) log.error("entry.host or entry.port is null");
        continue;
      }
      try {
        if (log.isTraceEnabled())
          log.trace(
              "UNREGISTER("
                  + group
                  + ", "
                  + mbr
                  + ") with GossipRouter at "
                  + entry.getIpAddress()
                  + ':'
                  + entry.getPort());
        sock = new Socket(entry.getIpAddress(), entry.getPort());
        out = new DataOutputStream(sock.getOutputStream());
        gossip_req = new GossipData(GossipRouter.UNREGISTER, group, mbr, null);
        // must send GossipData as fast as possible, otherwise the
        // request might be rejected
        gossip_req.writeTo(out);
        out.flush();
      } catch (Exception ex) {
        if (log.isErrorEnabled()) log.error("exception connecting to host " + entry);
      } finally {
        Util.close(out);
        if (sock != null) {
          try {
            sock.close();
          } catch (IOException e) {
          }
        }
      }
    }
  }
Example #28
0
 public void run() {
   try {
     while (!Thread.currentThread().isInterrupted() && canRun()) {
       try {
         int len = in.readInt();
         byte[] buf = new byte[len];
         in.readFully(buf, 0, len);
         updateLastAccessed();
         receiver.receive(peer_addr, buf, 0, len);
       } catch (OutOfMemoryError mem_ex) {
         break; // continue;
       } catch (IOException io_ex) {
         break;
       } catch (Throwable e) {
       }
     }
   } finally {
     Util.close(TCPConnection.this);
   }
 }
Example #29
0
  public void testFlushWithCrashedFlushCoordinator() throws Exception {
    JChannel a = null, b = null, c = null;
    try {
      a = createChannel(true, 3, "A");
      changeProps(a);
      a.connect("testFlushWithCrashedFlushCoordinator");

      b = createChannel(a, "B");
      changeProps(b);
      b.connect("testFlushWithCrashedFlushCoordinator");

      c = createChannel(a, "C");
      changeProps(c);
      c.connect("testFlushWithCrashedFlushCoordinator");

      System.out.println("shutting down flush coordinator B");
      b.down(new Event(Event.SUSPEND_BUT_FAIL)); // send out START_FLUSH and then return

      // now shut down B. This means, after failure detection kicks in and the new coordinator takes
      // over
      // (either A or C), that the current flush started by B will be cancelled and a new flush (by
      // A or C)
      // will be started
      Util.shutdown(b);

      a.getProtocolStack().findProtocol(FLUSH.class).setLevel("debug");
      c.getProtocolStack().findProtocol(FLUSH.class).setLevel("debug");

      Util.waitUntilAllChannelsHaveSameSize(10000, 500, a, c);

      // cluster should not hang and two remaining members should have a correct view
      assertTrue("correct view size", a.getView().size() == 2);
      assertTrue("correct view size", c.getView().size() == 2);

      a.getProtocolStack().findProtocol(FLUSH.class).setLevel("warn");
      c.getProtocolStack().findProtocol(FLUSH.class).setLevel("warn");

    } finally {
      Util.close(c, b, a);
    }
  }
Example #30
0
  public void testFlushWithCrashedParticipant() throws Exception {
    JChannel c1 = null;
    JChannel c2 = null;
    JChannel c3 = null;

    try {
      c1 = createChannel(true, 3, "C1");
      changeProps(c1);
      c1.connect("testFlushWithCrashedParticipant");

      c2 = createChannel(c1, "C2");
      changeProps(c2);
      c2.connect("testFlushWithCrashedParticipant");

      c3 = createChannel(c1, "C3");
      changeProps(c3);
      c3.connect("testFlushWithCrashedParticipant");

      System.out.println("shutting down C3");
      Util.shutdown(c3); // kill a flush participant

      System.out.println("C2: starting flush");
      boolean rc = Util.startFlush(c2);
      System.out.println("flush " + (rc ? " was successful" : "failed"));
      assert rc;

      System.out.println("stopping flush");
      c2.stopFlush();

      System.out.println("waiting for view to contain C1 and C2");
      Util.waitUntilAllChannelsHaveSameSize(10000, 500, c1, c2);

      // cluster should not hang and two remaining members should have a correct view
      System.out.println("C1: view=" + c1.getView() + "\nC2: view=" + c2.getView());
      assertTrue("correct view size", c1.getView().size() == 2);
      assertTrue("correct view size", c2.getView().size() == 2);
    } finally {
      Util.close(c3, c2, c1);
    }
  }