Exemplo n.º 1
0
  public void testMultipleDiscoveryGroups() throws Exception {
    final InetAddress groupAddress = InetAddress.getByName(address1);
    final int groupPort = getUDPDiscoveryPort();
    final int timeout = 500;

    String nodeID = RandomUtil.randomString();

    bg = newBroadcast(nodeID, RandomUtil.randomString(), null, -1, groupAddress, groupPort);

    bg.start();

    TransportConfiguration live1 = generateTC();

    bg.addConnector(live1);

    dg1 =
        newDiscoveryGroup(
            RandomUtil.randomString(),
            RandomUtil.randomString(),
            null,
            groupAddress,
            groupPort,
            timeout);

    dg2 =
        newDiscoveryGroup(
            RandomUtil.randomString(),
            RandomUtil.randomString(),
            null,
            groupAddress,
            groupPort,
            timeout);

    dg3 =
        newDiscoveryGroup(
            RandomUtil.randomString(),
            RandomUtil.randomString(),
            null,
            groupAddress,
            groupPort,
            timeout);

    dg1.start();
    dg2.start();
    dg3.start();

    bg.broadcastConnectors();

    boolean ok = dg1.waitForBroadcast(1000);
    Assert.assertTrue(ok);
    List<DiscoveryEntry> entries = dg1.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1), entries);

    ok = dg2.waitForBroadcast(1000);
    Assert.assertTrue(ok);
    entries = dg2.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1), entries);

    ok = dg3.waitForBroadcast(1000);
    Assert.assertTrue(ok);
    entries = dg3.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1), entries);

    bg.stop();

    dg1.stop();
    dg2.stop();
    dg3.stop();
  }
Exemplo n.º 2
0
  public void testConnectorsUpdatedMultipleBroadcasters() throws Exception {
    final InetAddress groupAddress = InetAddress.getByName(address1);
    final int groupPort = getUDPDiscoveryPort();
    final int timeout = 500;

    String node1 = RandomUtil.randomString();
    String node2 = RandomUtil.randomString();
    String node3 = RandomUtil.randomString();

    bg1 = newBroadcast(node1, RandomUtil.randomString(), null, -1, groupAddress, groupPort);
    bg1.start();

    bg2 = newBroadcast(node2, RandomUtil.randomString(), null, -1, groupAddress, groupPort);
    bg2.start();

    bg3 = newBroadcast(node3, RandomUtil.randomString(), null, -1, groupAddress, groupPort);
    bg3.start();

    TransportConfiguration live1 = generateTC();
    bg1.addConnector(live1);

    TransportConfiguration live2 = generateTC();
    bg2.addConnector(live2);

    TransportConfiguration live3 = generateTC();
    bg3.addConnector(live3);

    dg =
        newDiscoveryGroup(
            RandomUtil.randomString(),
            RandomUtil.randomString(),
            null,
            groupAddress,
            groupPort,
            timeout);

    MyListener listener1 = new MyListener();
    dg.registerListener(listener1);
    MyListener listener2 = new MyListener();
    dg.registerListener(listener2);

    dg.start();

    verifyBroadcast(bg1, dg);
    List<DiscoveryEntry> entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1), entries);
    Assert.assertTrue(listener1.called);
    Assert.assertTrue(listener2.called);
    listener1.called = false;
    listener2.called = false;

    verifyBroadcast(bg2, dg);
    entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1, live2), entries);
    Assert.assertTrue(listener1.called);
    Assert.assertTrue(listener2.called);
    listener1.called = false;
    listener2.called = false;

    verifyBroadcast(bg3, dg);
    entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries);
    Assert.assertTrue(listener1.called);
    Assert.assertTrue(listener2.called);
    listener1.called = false;
    listener2.called = false;

    verifyBroadcast(bg1, dg);
    entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries);
    Assert.assertFalse(listener1.called);
    Assert.assertFalse(listener2.called);
    listener1.called = false;
    listener2.called = false;

    verifyBroadcast(bg2, dg);
    entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries);
    Assert.assertFalse(listener1.called);
    Assert.assertFalse(listener2.called);
    listener1.called = false;
    listener2.called = false;

    verifyBroadcast(bg3, dg);
    entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries);
    Assert.assertFalse(listener1.called);
    Assert.assertFalse(listener2.called);
    listener1.called = false;
    listener2.called = false;

    bg2.removeConnector(live2);
    verifyBroadcast(bg2, dg);

    // Connector2 should still be there since not timed out yet

    entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1, live2, live3), entries);
    Assert.assertFalse(listener1.called);
    Assert.assertFalse(listener2.called);
    listener1.called = false;
    listener2.called = false;

    Thread.sleep(timeout * 2);

    bg1.broadcastConnectors();
    boolean ok = dg.waitForBroadcast(1000);
    bg2.broadcastConnectors();
    ok = dg.waitForBroadcast(1000);
    bg3.broadcastConnectors();
    ok = dg.waitForBroadcast(1000);

    entries = dg.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1, live3), entries);
    Assert.assertTrue(listener1.called);
    Assert.assertTrue(listener2.called);
    listener1.called = false;
    listener2.called = false;

    bg1.removeConnector(live1);
    bg3.removeConnector(live3);

    Thread.sleep(timeout * 2);

    bg1.broadcastConnectors();
    ok = dg.waitForBroadcast(1000);
    bg2.broadcastConnectors();
    ok = dg.waitForBroadcast(1000);
    bg3.broadcastConnectors();
    ok = dg.waitForBroadcast(1000);

    entries = dg.getDiscoveryEntries();
    Assert.assertNotNull(entries);
    Assert.assertEquals(0, entries.size());
    Assert.assertTrue(listener1.called);
    Assert.assertTrue(listener2.called);
    listener1.called = false;
    listener2.called = false;

    bg1.broadcastConnectors();
    ok = dg.waitForBroadcast(1000);
    bg2.broadcastConnectors();
    ok = dg.waitForBroadcast(1000);
    bg3.broadcastConnectors();
    ok = dg.waitForBroadcast(1000);

    entries = dg.getDiscoveryEntries();
    Assert.assertNotNull(entries);
    Assert.assertEquals(0, entries.size());
    Assert.assertFalse(listener1.called);
    Assert.assertFalse(listener2.called);
  }
Exemplo n.º 3
0
  public void testMultipleGroups() throws Exception {
    final int groupPort1 = getUDPDiscoveryPort();

    final int groupPort2 = getUDPDiscoveryPort(1);

    final int groupPort3 = getUDPDiscoveryPort(2);

    final InetAddress groupAddress1 = InetAddress.getByName(address1);

    final InetAddress groupAddress2 = InetAddress.getByName(address2);

    final InetAddress groupAddress3 = InetAddress.getByName(address3);

    final int timeout = 5000;

    String node1 = UUIDGenerator.getInstance().generateStringUUID();

    String node2 = UUIDGenerator.getInstance().generateStringUUID();

    String node3 = UUIDGenerator.getInstance().generateStringUUID();

    bg1 = newBroadcast(node1, RandomUtil.randomString(), null, -1, groupAddress1, groupPort1);

    bg2 = newBroadcast(node2, RandomUtil.randomString(), null, -1, groupAddress2, groupPort2);

    bg3 = newBroadcast(node3, RandomUtil.randomString(), null, -1, groupAddress3, groupPort3);

    bg2.start();
    bg1.start();
    bg3.start();

    TransportConfiguration live1 = generateTC("live1");

    TransportConfiguration live2 = generateTC("live2");

    TransportConfiguration live3 = generateTC("live3");

    bg1.addConnector(live1);
    bg2.addConnector(live2);
    bg3.addConnector(live3);

    dg1 =
        newDiscoveryGroup(
            "group-1::" + RandomUtil.randomString(),
            "group-1::" + RandomUtil.randomString(),
            null,
            groupAddress1,
            groupPort1,
            timeout);
    dg1.start();

    dg2 =
        newDiscoveryGroup(
            "group-2::" + RandomUtil.randomString(),
            "group-2::" + RandomUtil.randomString(),
            null,
            groupAddress2,
            groupPort2,
            timeout);
    dg2.start();

    dg3 =
        newDiscoveryGroup(
            "group-3::" + RandomUtil.randomString(),
            "group-3::" + RandomUtil.randomString(),
            null,
            groupAddress3,
            groupPort3,
            timeout);
    dg3.start();

    bg1.broadcastConnectors();

    bg2.broadcastConnectors();

    bg3.broadcastConnectors();

    boolean ok = dg1.waitForBroadcast(timeout);
    Assert.assertTrue(ok);
    List<DiscoveryEntry> entries = dg1.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live1), entries);

    ok = dg2.waitForBroadcast(timeout);
    Assert.assertTrue(ok);
    entries = dg2.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live2), entries);

    ok = dg3.waitForBroadcast(timeout);
    Assert.assertTrue(ok);
    entries = dg3.getDiscoveryEntries();
    assertEqualsDiscoveryEntries(Arrays.asList(live3), entries);
  }
Exemplo n.º 4
0
 /**
  * @param discoveryGroup
  * @throws Exception
  */
 private static void verifyNonBroadcast(
     BroadcastGroup broadcastGroup, DiscoveryGroup discoveryGroup) throws Exception {
   broadcastGroup.broadcastConnectors();
   Assert.assertFalse("NO broadcast received", discoveryGroup.waitForBroadcast(2000));
 }
Exemplo n.º 5
0
  public ClientSessionFactory createSessionFactory() throws HornetQException {
    assertOpen();

    initialise();

    if (initialConnectors == null && discoveryGroup != null) {
      // Wait for an initial broadcast to give us at least one node in the cluster
      long timeout =
          clusterConnection ? 0 : discoveryGroupConfiguration.getDiscoveryInitialWaitTimeout();
      boolean ok = discoveryGroup.waitForBroadcast(timeout);

      if (!ok) {
        throw HornetQMessageBundle.BUNDLE.connectionTimedOutInInitialBroadcast();
      }
    }

    ClientSessionFactoryInternal factory = null;

    synchronized (this) {
      boolean retry;
      int attempts = 0;
      do {
        retry = false;

        TransportConfiguration tc = selectConnector();
        if (tc == null) {
          throw HornetQMessageBundle.BUNDLE.noTCForSessionFactory();
        }

        // try each factory in the list until we find one which works

        try {
          factory =
              new ClientSessionFactoryImpl(
                  this,
                  tc,
                  callTimeout,
                  callFailoverTimeout,
                  clientFailureCheckPeriod,
                  connectionTTL,
                  retryInterval,
                  retryIntervalMultiplier,
                  maxRetryInterval,
                  reconnectAttempts,
                  threadPool,
                  scheduledThreadPool,
                  interceptors);
          try {
            addToConnecting(factory);
            factory.connect(initialConnectAttempts, failoverOnInitialConnection);
          } finally {
            removeFromConnecting(factory);
          }
        } catch (HornetQException e) {
          factory.close();
          factory = null;
          if (e.getType() == HornetQExceptionType.NOT_CONNECTED) {
            attempts++;

            if (topologyArray != null && attempts == topologyArray.length) {
              throw HornetQMessageBundle.BUNDLE.cannotConnectToServers();
            }
            if (topologyArray == null
                && initialConnectors != null
                && attempts == initialConnectors.length) {
              throw HornetQMessageBundle.BUNDLE.cannotConnectToServers();
            }
            retry = true;
          } else {
            throw e;
          }
        }
      } while (retry);

      if (ha || clusterConnection) {
        final long timeout = System.currentTimeMillis() + 30000;
        while (!isClosed() && !receivedTopology && timeout > System.currentTimeMillis()) {
          // Now wait for the topology

          try {
            wait(1000);
          } catch (InterruptedException ignore) {
          }
        }

        if (System.currentTimeMillis() > timeout && !receivedTopology) {
          throw HornetQMessageBundle.BUNDLE.connectionTimedOutOnReceiveTopology(discoveryGroup);
        }
      }

      addFactory(factory);

      return factory;
    }
  }