Java Code Examples for org.apache.avro.ipc.Server#close()

The following examples show how to use org.apache.avro.ipc.Server#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AvroProtocol.java    From dubbox with Apache License 2.0 6 votes vote down vote up
@Override
protected <T> Runnable doExport(T impl, Class<T> type, URL url)
        throws RpcException {

    logger.info("impl => " + impl.getClass());
    logger.info("type => " + type.getName());
    logger.info("url => " + url);

    final Server server = new NettyServer(new ReflectResponder(type, impl),
            new InetSocketAddress(url.getHost(), url.getPort()));
    server.start();

    return new Runnable() {
        public void run() {
            try {
                logger.info("Close Avro Server");
                server.close();
            } catch (Throwable e) {
                logger.warn(e.getMessage(), e);
            }
        }
    };
}
 
Example 2
Source File: TestLoadBalancingRpcClient.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Test(expected=FlumeException.class)
public void testCreatingLbClientSingleHost() {
  Server server1 = null;
  RpcClient c = null;
  try {
    server1 = RpcTestUtils.startServer(new OKAvroHandler());
    Properties p = new Properties();
    p.put("host1", "127.0.0.1:" + server1.getPort());
    p.put("hosts", "host1");
    p.put("client.type", "default_loadbalance");
    RpcClientFactory.getInstance(p);
  } finally {
    if (server1 != null) server1.close();
    if (c != null) c.close();
  }
}
 
Example 3
Source File: TestNettyAvroRpcClient.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
/**
 * First connect the client, then shut down the server, then send a request.
 * @throws FlumeException
 * @throws EventDeliveryException
 * @throws InterruptedException
 */
@Test(expected=EventDeliveryException.class)
public void testServerDisconnect() throws FlumeException,
    EventDeliveryException, InterruptedException {
  NettyAvroRpcClient client = null;
  Server server = RpcTestUtils.startServer(new OKAvroHandler());
  try {
    client = RpcTestUtils.getStockLocalClient(server.getPort());
    server.close();
    Thread.sleep(1000L); // wait a second for the close to occur
    try {
      server.join();
    } catch (InterruptedException ex) {
      logger.warn("Thread interrupted during join()", ex);
      Thread.currentThread().interrupt();
    }
    try {
      client.append(EventBuilder.withBody("hello", Charset.forName("UTF8")));
    } finally {
      Assert.assertFalse("Client should not be active", client.isActive());
    }
  } finally {
    RpcTestUtils.stopServer(server);
    if (client != null) client.close();
  }
}
 
Example 4
Source File: TestAvroSink.java    From mt-flume with Apache License 2.0 6 votes vote down vote up
@Test
public void testLifecycle() throws InterruptedException,
    InstantiationException, IllegalAccessException {
  setUp();
  Server server = createServer(new MockAvroServer());

  server.start();

  sink.start();
  Assert.assertTrue(LifecycleController.waitForOneOf(sink,
      LifecycleState.START_OR_ERROR, 5000));

  sink.stop();
  Assert.assertTrue(LifecycleController.waitForOneOf(sink,
      LifecycleState.STOP_OR_ERROR, 5000));

  server.close();
}
 
Example 5
Source File: RunLocalTest.java    From hadoop-arch-book with Apache License 2.0 5 votes vote down vote up
public static void stopTestFlumeServer(Server server) {
  try {
    server.close();
    server.join();
  } catch (InterruptedException ex) {
    LOG.error("Thread interrupted. Exception follows.", ex);
    Thread.currentThread().interrupt();
  }
}
 
Example 6
Source File: TestFailoverRpcClient.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
/**
 * Try writing to some servers and then kill them all.
 *
 * @throws FlumeException
 * @throws EventDeliveryException
 */
@Test(
    expected = EventDeliveryException.class)
public void testFailedServers() throws FlumeException, EventDeliveryException {
  FailoverRpcClient client = null;
  Server server1 = RpcTestUtils.startServer(new OKAvroHandler());
  Server server2 = RpcTestUtils.startServer(new OKAvroHandler());
  Server server3 = RpcTestUtils.startServer(new OKAvroHandler());
  Properties props = new Properties();
  props.put("client.type", "default_failover");

  props.put("hosts", "host1 host2 host3");
  props.put("hosts.host1", "localhost:" + String.valueOf(server1.getPort()));
  props.put("hosts.host2", "localhost:" + String.valueOf(server2.getPort()));
  props.put("hosts.host3", " localhost:" + String.valueOf(server3.getPort()));
  client = (FailoverRpcClient) RpcClientFactory.getInstance(props);
  List<Event> events = new ArrayList<Event>();
  for (int i = 0; i < 50; i++) {
    events.add(EventBuilder.withBody("evt: " + i, Charset.forName("UTF8")));
  }
  client.appendBatch(events);
  server1.close();
  server2.close();
  server3.close();
  events = new ArrayList<Event>();
  for (int i = 0; i < 50; i++) {
    events.add(EventBuilder.withBody("evt: " + i, Charset.forName("UTF8")));
  }
  client.appendBatch(events);
}
 
Example 7
Source File: RpcTestUtils.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
/**
 * Request that the specified Server stop, and attempt to wait for it to exit.
 * @param server A running NettyServer
 */
public static void stopServer(Server server) {
  try {
    server.close();
    server.join();
  } catch (InterruptedException ex) {
    logger.error("Thread interrupted. Exception follows.", ex);
    Thread.currentThread().interrupt();
  }
}
 
Example 8
Source File: TestAvroSink.java    From mt-flume with Apache License 2.0 5 votes vote down vote up
@Test
public void testProcess() throws InterruptedException,
    EventDeliveryException, InstantiationException, IllegalAccessException {
  setUp();

  Event event = EventBuilder.withBody("test event 1", Charsets.UTF_8);
  Server server = createServer(new MockAvroServer());

  server.start();

  sink.start();
  Assert.assertTrue(LifecycleController.waitForOneOf(sink,
      LifecycleState.START_OR_ERROR, 5000));

  Transaction transaction = channel.getTransaction();

  transaction.begin();
  for (int i = 0; i < 10; i++) {
    channel.put(event);
  }
  transaction.commit();
  transaction.close();

  for (int i = 0; i < 5; i++) {
    Sink.Status status = sink.process();
    Assert.assertEquals(Sink.Status.READY, status);
  }

  Assert.assertEquals(Sink.Status.BACKOFF, sink.process());

  sink.stop();
  Assert.assertTrue(LifecycleController.waitForOneOf(sink,
      LifecycleState.STOP_OR_ERROR, 5000));

  server.close();
}
 
Example 9
Source File: TestFailoverRpcClient.java    From mt-flume with Apache License 2.0 4 votes vote down vote up
/**
 * Test a bunch of servers closing the one we are writing to and bringing
 * another one back online.
 *
 * @throws FlumeException
 * @throws EventDeliveryException
 * @throws InterruptedException
 */

@Test
public void testFailover()
    throws FlumeException, EventDeliveryException,InterruptedException {
  FailoverRpcClient client = null;
  Server server1 = RpcTestUtils.startServer(new OKAvroHandler());
  Server server2 = RpcTestUtils.startServer(new OKAvroHandler());
  Server server3 = RpcTestUtils.startServer(new OKAvroHandler());
  Properties props = new Properties();
  int s1Port = server1.getPort();
  int s2Port = server2.getPort();
  int s3Port = server3.getPort();
  props.put("client.type", "default_failover");
  props.put("hosts", "host1 host2 host3");
  props.put("hosts.host1", "127.0.0.1:" + String.valueOf(s1Port));
  props.put("hosts.host2", "127.0.0.1:" + String.valueOf(s2Port));
  props.put("hosts.host3", "127.0.0.1:" + String.valueOf(s3Port));
  client = (FailoverRpcClient) RpcClientFactory.getInstance(props);
  List<Event> events = new ArrayList<Event>();
  for (int i = 0; i < 50; i++) {
    events.add(EventBuilder.withBody("evt: " + i, Charset.forName("UTF8")));
  }
  client.appendBatch(events);
  Assert.assertEquals(client.getLastConnectedServerAddress(),
      new InetSocketAddress("127.0.0.1", server1.getPort()));
  server1.close();
  Thread.sleep(1000L); // wait a second for the close to occur
  events = new ArrayList<Event>();
  for (int i = 0; i < 50; i++) {
    events.add(EventBuilder.withBody("evt: " + i, Charset.forName("UTF8")));
  }
  client.appendBatch(events);
  Assert.assertEquals(new InetSocketAddress("localhost", server2.getPort()),
      client.getLastConnectedServerAddress());
  server2.close();
  Thread.sleep(1000L); // wait a second for the close to occur
  client.append(EventBuilder.withBody("Had a sandwich?",
      Charset.forName("UTF8")));
  Assert.assertEquals(new InetSocketAddress("localhost", server3.getPort()),
      client.getLastConnectedServerAddress());
  // Bring server 2 back.
  Server server4 = RpcTestUtils.startServer(new OKAvroHandler(), s2Port);
  server3.close();
  Thread.sleep(1000L); // wait a second for the close to occur
  events = new ArrayList<Event>();
  for (int i = 0; i < 50; i++) {
    events.add(EventBuilder.withBody("evt: " + i, Charset.forName("UTF8")));
  }
  client.appendBatch(events);
  Assert.assertEquals(new InetSocketAddress("localhost", s2Port),
      client.getLastConnectedServerAddress());

  Server server5 = RpcTestUtils.startServer(new OKAvroHandler(), s1Port);
  // Make sure we are still talking to server 4

  client
  .append(EventBuilder.withBody("Had a mango?", Charset.forName("UTF8")));
  Assert.assertEquals(new InetSocketAddress("localhost", s2Port),
      client.getLastConnectedServerAddress());
  server4.close();
  Thread.sleep(1000L); // wait a second for the close to occur
  events = new ArrayList<Event>();
  for (int i = 0; i < 50; i++) {
    events.add(EventBuilder.withBody("evt: " + i, Charset.forName("UTF8")));
  }
  client.appendBatch(events);
  Assert.assertEquals(new InetSocketAddress("localhost", s1Port),
      client.getLastConnectedServerAddress());
  server5.close();
  Thread.sleep(1000L); // wait a second for the close to occur
  Server server6 = RpcTestUtils.startServer(new OKAvroHandler(), s1Port);
  client
  .append(EventBuilder.withBody("Had a whole watermelon?",
      Charset.forName("UTF8")));
  Assert.assertEquals(new InetSocketAddress("localhost", s1Port),
      client.getLastConnectedServerAddress());

  server6.close();
  Thread.sleep(1000L); // wait a second for the close to occur
  Server server7 = RpcTestUtils.startServer(new OKAvroHandler(), s3Port);
  events = new ArrayList<Event>();
  for (int i = 0; i < 50; i++) {
    events.add(EventBuilder.withBody("evt: " + i, Charset.forName("UTF8")));
  }
  client.appendBatch(events);
  Assert.assertEquals(new InetSocketAddress("localhost", s3Port),
      client.getLastConnectedServerAddress());
  server7.close();
}
 
Example 10
Source File: TestLoadBalancingRpcClient.java    From mt-flume with Apache License 2.0 4 votes vote down vote up
/**
 * Ensure that we can tolerate a host that is completely down.
 * @throws Exception
 */
@Test
public void testTwoHostsOneDead() throws Exception {
  LOGGER.info("Running testTwoHostsOneDead...");
  Server s1 = null;
  RpcClient c1 = null, c2 = null;
  try {
    LoadBalancedAvroHandler h1 = new LoadBalancedAvroHandler();
    s1 = RpcTestUtils.startServer(h1);
    // do not create a 2nd server (assume it's "down")

    Properties p = new Properties();
    p.put("hosts", "h1 h2");
    p.put("client.type", "default_loadbalance");
    p.put("hosts.h1", "127.0.0.1:" + 0); // port 0 should always be closed
    p.put("hosts.h2", "127.0.0.1:" + s1.getPort());

    // test batch API
    c1 = RpcClientFactory.getInstance(p);
    Assert.assertTrue(c1 instanceof LoadBalancingRpcClient);

    for (int i = 0; i < 10; i++) {
      c1.appendBatch(getBatchedEvent(i));
    }
    Assert.assertEquals(10, h1.getAppendBatchCount());

    // test non-batch API
    c2 = RpcClientFactory.getInstance(p);
    Assert.assertTrue(c2 instanceof LoadBalancingRpcClient);

    for (int i = 0; i < 10; i++) {
      c2.append(getEvent(i));
    }
    Assert.assertEquals(10, h1.getAppendCount());


  } finally {
    if (s1 != null) s1.close();
    if (c1 != null) c1.close();
    if (c2 != null) c2.close();
  }
}
 
Example 11
Source File: TestAvroSink.java    From mt-flume with Apache License 2.0 4 votes vote down vote up
@Test
public void testFailedConnect() throws InterruptedException,
    EventDeliveryException, InstantiationException, IllegalAccessException {

  setUp();
  Event event = EventBuilder.withBody("test event 1",
      Charset.forName("UTF8"));
  Server server = createServer(new MockAvroServer());

  server.start();
  sink.start();
  Assert.assertTrue(LifecycleController.waitForOneOf(sink,
      LifecycleState.START_OR_ERROR, 5000));

  Thread.sleep(500L); // let socket startup
  server.close();
  Thread.sleep(500L); // sleep a little to allow close occur

  Transaction transaction = channel.getTransaction();

  transaction.begin();
  for (int i = 0; i < 10; i++) {
    channel.put(event);
  }
  transaction.commit();
  transaction.close();

  for (int i = 0; i < 5; i++) {
    boolean threwException = false;
    try {
      sink.process();
    } catch (EventDeliveryException e) {
      threwException = true;
    }
    Assert.assertTrue("Must throw EventDeliveryException if disconnected",
        threwException);
  }

  server = createServer(new MockAvroServer());
  server.start();

  for (int i = 0; i < 5; i++) {
    Sink.Status status = sink.process();
    Assert.assertEquals(Sink.Status.READY, status);
  }

  Assert.assertEquals(Sink.Status.BACKOFF, sink.process());

  sink.stop();
  Assert.assertTrue(LifecycleController.waitForOneOf(sink,
      LifecycleState.STOP_OR_ERROR, 5000));
  server.close();
}
 
Example 12
Source File: TestAvroSink.java    From mt-flume with Apache License 2.0 4 votes vote down vote up
@Test
public void testReset() throws Exception {

  setUp();
  Server server = createServer(new MockAvroServer());

  server.start();

  Context context = new Context();

  context.put("hostname", hostname);
  context.put("port", String.valueOf(port));
  context.put("batch-size", String.valueOf(2));
  context.put("connect-timeout", String.valueOf(2000L));
  context.put("request-timeout", String.valueOf(3000L));
  context.put("reset-connection-interval", String.valueOf("5"));

  sink.setChannel(channel);
  Configurables.configure(sink, context);
  sink.start();
  RpcClient firstClient = sink.getUnderlyingClient();
  Thread.sleep(6000);
  // Make sure they are not the same object, connection should be reset
  Assert.assertFalse(firstClient == sink.getUnderlyingClient());
  sink.stop();

  context.put("hostname", hostname);
  context.put("port", String.valueOf(port));
  context.put("batch-size", String.valueOf(2));
  context.put("connect-timeout", String.valueOf(2000L));
  context.put("request-timeout", String.valueOf(3000L));
  context.put("reset-connection-interval", String.valueOf("0"));

  sink.setChannel(channel);
  Configurables.configure(sink, context);
  sink.start();
  firstClient = sink.getUnderlyingClient();
  Thread.sleep(6000);
  // Make sure they are the same object, since connection should not be reset
  Assert.assertTrue(firstClient == sink.getUnderlyingClient());
  sink.stop();

  context.clear();
  context.put("hostname", hostname);
  context.put("port", String.valueOf(port));
  context.put("batch-size", String.valueOf(2));
  context.put("connect-timeout", String.valueOf(2000L));
  context.put("request-timeout", String.valueOf(3000L));

  sink.setChannel(channel);
  Configurables.configure(sink, context);
  sink.start();
  firstClient = sink.getUnderlyingClient();
  Thread.sleep(6000);
  // Make sure they are the same object, since connection should not be reset
  Assert.assertTrue(firstClient == sink.getUnderlyingClient());
  sink.stop();
  server.close();
}
 
Example 13
Source File: TestAvroSink.java    From mt-flume with Apache License 2.0 4 votes vote down vote up
@Test
public void testSslProcess() throws InterruptedException,
    EventDeliveryException, InstantiationException, IllegalAccessException {
  setUp();
  Event event = EventBuilder.withBody("test event 1", Charsets.UTF_8);
  Server server = createSslServer(new MockAvroServer());

  server.start();

  Context context = new Context();

  context.put("hostname", hostname);
  context.put("port", String.valueOf(port));
  context.put("ssl", String.valueOf(true));
  context.put("trust-all-certs", String.valueOf(true));
  context.put("batch-size", String.valueOf(2));
  context.put("connect-timeout", String.valueOf(2000L));
  context.put("request-timeout", String.valueOf(3000L));

  Configurables.configure(sink, context);

  sink.start();
  Assert.assertTrue(LifecycleController.waitForOneOf(sink,
      LifecycleState.START_OR_ERROR, 5000));

  Transaction transaction = channel.getTransaction();

  transaction.begin();
  for (int i = 0; i < 10; i++) {
    channel.put(event);
  }
  transaction.commit();
  transaction.close();

  for (int i = 0; i < 5; i++) {
    Sink.Status status = sink.process();
    Assert.assertEquals(Sink.Status.READY, status);
  }

  Assert.assertEquals(Sink.Status.BACKOFF, sink.process());

  sink.stop();
  Assert.assertTrue(LifecycleController.waitForOneOf(sink,
      LifecycleState.STOP_OR_ERROR, 5000));

  server.close();
}
 
Example 14
Source File: TestAvroSink.java    From mt-flume with Apache License 2.0 4 votes vote down vote up
@Test
public void testSslProcessWithTrustStore() throws InterruptedException,
    EventDeliveryException, InstantiationException, IllegalAccessException {
  setUp();
  Event event = EventBuilder.withBody("test event 1", Charsets.UTF_8);
  Server server = createSslServer(new MockAvroServer());

  server.start();

  Context context = new Context();

  context.put("hostname", hostname);
  context.put("port", String.valueOf(port));
  context.put("ssl", String.valueOf(true));
  context.put("truststore", "src/test/resources/truststore.jks");
  context.put("truststore-password", "password");
  context.put("batch-size", String.valueOf(2));
  context.put("connect-timeout", String.valueOf(2000L));
  context.put("request-timeout", String.valueOf(3000L));

  Configurables.configure(sink, context);

  sink.start();
  Assert.assertTrue(LifecycleController.waitForOneOf(sink,
      LifecycleState.START_OR_ERROR, 5000));

  Transaction transaction = channel.getTransaction();

  transaction.begin();
  for (int i = 0; i < 10; i++) {
    channel.put(event);
  }
  transaction.commit();
  transaction.close();

  for (int i = 0; i < 5; i++) {
    Sink.Status status = sink.process();
    Assert.assertEquals(Sink.Status.READY, status);
  }

  Assert.assertEquals(Sink.Status.BACKOFF, sink.process());

  sink.stop();
  Assert.assertTrue(LifecycleController.waitForOneOf(sink,
      LifecycleState.STOP_OR_ERROR, 5000));

  server.close();
}
 
Example 15
Source File: TestAvroSink.java    From mt-flume with Apache License 2.0 4 votes vote down vote up
@Test
public void testSslSinkWithNonSslServer() throws InterruptedException,
    EventDeliveryException, InstantiationException, IllegalAccessException {
  setUp();
  Event event = EventBuilder.withBody("test event 1", Charsets.UTF_8);
  Server server = createServer(new MockAvroServer());

  server.start();

  Context context = new Context();

  context.put("hostname", hostname);
  context.put("port", String.valueOf(port));
  context.put("ssl", String.valueOf(true));
  context.put("trust-all-certs", String.valueOf(true));
  context.put("batch-size", String.valueOf(2));
  context.put("connect-timeout", String.valueOf(2000L));
  context.put("request-timeout", String.valueOf(3000L));

  Configurables.configure(sink, context);

  sink.start();
  Assert.assertTrue(LifecycleController.waitForOneOf(sink,
      LifecycleState.START_OR_ERROR, 5000));

  Transaction transaction = channel.getTransaction();

  transaction.begin();
  for (int i = 0; i < 10; i++) {
    channel.put(event);
  }
  transaction.commit();
  transaction.close();

  boolean failed = false;
  try {
    for (int i = 0; i < 5; i++) {
      sink.process();
      failed = true;
    }
  } catch (EventDeliveryException ex) {
    logger.info("Correctly failed to send event", ex);
  }


  sink.stop();
  Assert.assertTrue(LifecycleController.waitForOneOf(sink,
      LifecycleState.STOP_OR_ERROR, 5000));

  server.close();

  if (failed) {
    Assert.fail("SSL-enabled sink successfully connected to a non-SSL-enabled server, that's wrong.");
  }
}
 
Example 16
Source File: TestAvroSink.java    From mt-flume with Apache License 2.0 4 votes vote down vote up
@Test
public void testSslSinkWithNonTrustedCert() throws InterruptedException,
    EventDeliveryException, InstantiationException, IllegalAccessException {
  setUp();
  Event event = EventBuilder.withBody("test event 1", Charsets.UTF_8);
  Server server = createSslServer(new MockAvroServer());

  server.start();

  Context context = new Context();

  context.put("hostname", hostname);
  context.put("port", String.valueOf(port));
  context.put("ssl", String.valueOf(true));
  context.put("batch-size", String.valueOf(2));
  context.put("connect-timeout", String.valueOf(2000L));
  context.put("request-timeout", String.valueOf(3000L));

  Configurables.configure(sink, context);

  sink.start();
  Assert.assertTrue(LifecycleController.waitForOneOf(sink,
      LifecycleState.START_OR_ERROR, 5000));

  Transaction transaction = channel.getTransaction();

  transaction.begin();
  for (int i = 0; i < 10; i++) {
    channel.put(event);
  }
  transaction.commit();
  transaction.close();

  boolean failed = false;
  try {
    for (int i = 0; i < 5; i++) {
      sink.process();
      failed = true;
    }
  } catch (EventDeliveryException ex) {
    logger.info("Correctly failed to send event", ex);
  }


  sink.stop();
  Assert.assertTrue(LifecycleController.waitForOneOf(sink,
      LifecycleState.STOP_OR_ERROR, 5000));

  server.close();

  if (failed) {
    Assert.fail("SSL-enabled sink successfully connected to a server with an untrusted certificate when it should have failed");
  }
}