List of usage examples for org.apache.thrift.server TServer isServing
boolean isServing
To view the source code for org.apache.thrift.server TServer isServing.
Click Source Link
From source file:com.bfd.harpc.test.thrift.ThriftTest.java
License:Apache License
public static void main(String[] args) throws InterruptedException { TServerSocket serverTransport;//from w w w . j a v a2 s . c o m try { serverTransport = new TServerSocket(19091); } catch (TTransportException e) { throw new RpcException(RpcException.NETWORK_EXCEPTION, e); } Factory portFactory = new TBinaryProtocol.Factory(true, true); Args arg = new Args(serverTransport); Iface echoService = new EchoServiceImpl(); TProcessor processor = new EchoService.Processor<Iface>(echoService); arg.processor(processor); arg.protocolFactory(portFactory); arg.maxWorkerThreads(100); // ?? arg.minWorkerThreads(10); TServer server = new TThreadPoolServer(arg); new TServerThread(server).start(); while (true) { System.out.println(server.isServing()); Thread.sleep(1000); } }
From source file:com.liveramp.hank.client.TestHankSmartClient.java
License:Apache License
@Test public void testIt() throws Exception { int server1Port = 12345; int server2Port = 12346; int server3Port = 12347; // launch server 1 final MockPartitionServerHandler iface1 = new MockPartitionServerHandler(VALUE_1); TNonblockingServerTransport transport1 = createPartitionServerTransport(server1Port); final TServer server1 = createPartitionServer(transport1, iface1); Thread thread1 = new Thread(new ServerRunnable(server1), "mock partition server thread 1"); thread1.start();/* ww w. j a v a 2 s . co m*/ // launch server 2; final MockPartitionServerHandler iface2 = new MockPartitionServerHandler(VALUE_2); TNonblockingServerTransport transport2 = createPartitionServerTransport(server2Port); final TServer server2 = createPartitionServer(transport2, iface2); Thread thread2 = new Thread(new ServerRunnable(server2), "mock partition server thread 2"); thread2.start(); // launch server 3; final MockPartitionServerHandler iface3 = new MockPartitionServerHandler(VALUE_3); TNonblockingServerTransport transport3 = createPartitionServerTransport(server3Port); final TServer server3 = createPartitionServer(transport3, iface3); Thread thread3 = new Thread(new ServerRunnable(server3), "mock partition server thread 3"); thread3.start(); final MockDomain existentDomain = new MockDomain("existent_domain", 0, 2, new MapPartitioner(KEY_1, 0, KEY_2, 1, KEY_NOT_FOUND, 0), null, null, null); final MockDomain newDomain = new MockDomain("new_domain", 1, 1, new MapPartitioner(KEY_3, 0), null, null, null); final Host host1 = getHost(existentDomain, new PartitionServerAddress("localhost", server1Port), 0); final Host host2 = getHost(existentDomain, new PartitionServerAddress("localhost", server2Port), 1); final Host host3 = getHost(newDomain, new PartitionServerAddress("localhost", server3Port), 0); final Set<Host> mockRingHosts = new HashSet<Host>(); mockRingHosts.add(host1); mockRingHosts.add(host2); final MockRing mockRing = new MockRing(null, null, 1) { @Override public Set<Host> getHosts() { return mockRingHosts; } }; MockDomainGroup mockDomainGroup = new MockDomainGroup("myDomainGroup") { @Override public Set<DomainAndVersion> getDomainVersions() { return new HashSet<DomainAndVersion>(Arrays.asList(new DomainAndVersion(existentDomain, 1))); } }; final MockRingGroup mockRingGroup = new MockRingGroup(mockDomainGroup, "myRingGroup", Sets.newHashSet()) { @Override public Set<Ring> getRings() { return Collections.singleton((Ring) mockRing); } }; Coordinator mockCoord = new MockCoordinator() { @Override public RingGroup getRingGroup(String ringGroupName) { return mockRingGroup; } @Override public Domain getDomain(String domainName) { if (domainName.equals("existent_domain")) { return existentDomain; } else if (domainName.equals("new_domain")) { return newDomain; } else { return null; } } }; WaitUntil.orDie(() -> server1.isServing() && server2.isServing() && server3.isServing()); try { final HankSmartClient client = new HankSmartClient(mockCoord, "myRingGroup", new HankSmartClientOptions().setQueryTimeoutMs(1000)); final HankSmartClient cachingClient = new HankSmartClient(mockCoord, "myRingGroup", new HankSmartClientOptions().setResponseCacheEnabled(true).setResponseCacheNumItemsCapacity(1) .setResponseCacheNumBytesCapacity(-1).setResponseCacheExpirationSeconds(1)); // Test invalid get assertEquals(HankResponse.xception(HankException.no_such_domain(true)), client.get("nonexistent_domain", null)); // Test get assertEquals(HankResponse.value(VALUE_1), client.get("existent_domain", KEY_1)); assertEquals(HankResponse.value(VALUE_2), client.get("existent_domain", KEY_2)); // Test invalid getBulk assertEquals(HankBulkResponse.xception(HankException.no_such_domain(true)), client.getBulk("nonexistent_domain", null)); // Test getBulk HankBulkResponse bulkResponse1 = HankBulkResponse.responses(new ArrayList<>()); bulkResponse1.get_responses().add(HankResponse.value(VALUE_1)); bulkResponse1.get_responses().add(HankResponse.value(VALUE_2)); List<ByteBuffer> bulkRequest1 = new ArrayList<>(); bulkRequest1.add(KEY_1); bulkRequest1.add(KEY_2); assertEquals(bulkResponse1, client.getBulk("existent_domain", bulkRequest1)); // Test get with null key try { client.get("existent_domain", null); fail("Should throw an exception."); } catch (NullKeyException e) { // Good } // Test get with empty key try { client.get("existent_domain", ByteBuffer.wrap(new byte[0])); fail("Should throw an exception."); } catch (EmptyKeyException e) { // Good } // Host is not available host1.setState(HostState.UPDATING); assertEquals(HankResponse.xception(HankException.no_connection_available(true)), client.get("existent_domain", KEY_1)); // Host is offline but it's the only one, use it opportunistically host2.setState(HostState.OFFLINE); assertEquals(HankResponse.value(VALUE_2), client.get("existent_domain", KEY_2)); host1.setState(HostState.SERVING); host2.setState(HostState.SERVING); // Test location changes // Add new host that has new domain mockRingHosts.add(host3); // Should not be able to query new domain assertTrue(client.get("new_domain", KEY_3).get_xception().is_set_no_replica()); // Notify client of data location change client.onDataLocationChange(mockCoord.getRingGroup("myRingGroup")); // Should be able to query new domain when the client has done updating its cache WaitUntil.orDie(() -> HankResponse.value(VALUE_3).equals(client.get("new_domain", KEY_3))); assertEquals(HankResponse.value(VALUE_3), client.get("new_domain", KEY_3)); // TODO: Test not querying deletable partitions // Simulate servers that fail to perform gets iface1.setMode(MockPartitionServerHandler.Mode.FAILING); iface2.setMode(MockPartitionServerHandler.Mode.FAILING); assertTrue(client.get("existent_domain", KEY_1).get_xception().get_failed_retries() > 0); assertTrue(client.get("existent_domain", KEY_2).get_xception().get_failed_retries() > 0); // Simulate servers that throws an error iface1.setMode(MockPartitionServerHandler.Mode.THROWING_ERROR); iface2.setMode(MockPartitionServerHandler.Mode.THROWING_ERROR); assertTrue(client.get("existent_domain", KEY_1).get_xception().get_failed_retries() > 0); assertTrue(client.get("existent_domain", KEY_2).get_xception().get_failed_retries() > 0); // Simulate servers that hangs iface1.setMode(MockPartitionServerHandler.Mode.HANGING); iface2.setMode(MockPartitionServerHandler.Mode.HANGING); assertTrue(client.get("existent_domain", KEY_1).get_xception().get_failed_retries() > 0); assertTrue(client.get("existent_domain", KEY_2).get_xception().get_failed_retries() > 0); // Test caching iface1.setMode(MockPartitionServerHandler.Mode.NORMAL); iface2.setMode(MockPartitionServerHandler.Mode.NORMAL); iface3.setMode(MockPartitionServerHandler.Mode.NORMAL); iface1.clearNumRequests(); // One request assertEquals(HankResponse.value(VALUE_1), cachingClient.get("existent_domain", KEY_1)); assertEquals(1, iface1.getNumRequests()); // One cached request assertEquals(HankResponse.value(VALUE_1), cachingClient.get("existent_domain", KEY_1)); assertEquals(1, iface1.getNumRequests()); iface1.clearNumRequests(); // One not found request assertEquals(HankResponse.not_found(true), cachingClient.get("existent_domain", KEY_NOT_FOUND)); assertEquals(1, iface1.getNumRequests()); // One not found cached request assertEquals(HankResponse.not_found(true), cachingClient.get("existent_domain", KEY_NOT_FOUND)); assertEquals(1, iface1.getNumRequests()); // Wait for cache to expire Thread.sleep(2000); // Should not be in cache anymore assertEquals(HankResponse.not_found(true), cachingClient.get("existent_domain", KEY_NOT_FOUND)); assertEquals(2, iface1.getNumRequests()); } finally { server1.stop(); server2.stop(); thread1.join(); thread2.join(); transport1.close(); transport2.close(); } }
From source file:org.apache.accumulo.core.client.TestThrift1474.java
License:Apache License
@Test public void test() throws IOException, TException, InterruptedException { TServerSocket serverTransport = new TServerSocket(0); serverTransport.listen();/*from ww w. j a va2s. co m*/ int port = serverTransport.getServerSocket().getLocalPort(); TestServer handler = new TestServer(); ThriftTest.Processor<ThriftTest.Iface> processor = new ThriftTest.Processor<ThriftTest.Iface>(handler); TThreadPoolServer.Args args = new TThreadPoolServer.Args(serverTransport); args.stopTimeoutVal = 10; args.stopTimeoutUnit = TimeUnit.MILLISECONDS; final TServer server = new TThreadPoolServer(args.processor(processor)); Thread thread = new Thread() { @Override public void run() { server.serve(); } }; thread.start(); while (!server.isServing()) { UtilWaitThread.sleep(10); } TTransport transport = new TSocket("localhost", port); transport.open(); TProtocol protocol = new TBinaryProtocol(transport); ThriftTest.Client client = new ThriftTest.Client(protocol); assertTrue(client.success()); assertFalse(client.fails()); try { client.throwsError(); fail("no exception thrown"); } catch (ThriftSecurityException ex) { // expected } server.stop(); thread.join(); }
From source file:org.apache.accumulo.proxy.ProxyDurabilityIT.java
License:Apache License
@Test public void testDurability() throws Exception { Connector c = getConnector();//from w w w .ja v a2 s . c om Properties props = new Properties(); props.put("instance", c.getInstance().getInstanceName()); props.put("zookeepers", c.getInstance().getZooKeepers()); props.put("tokenClass", PasswordToken.class.getName()); TJSONProtocol.Factory protocol = new TJSONProtocol.Factory(); int proxyPort = PortUtils.getRandomFreePort(); final TServer proxyServer = Proxy.createProxyServer(HostAndPort.fromParts("localhost", proxyPort), protocol, props).server; while (!proxyServer.isServing()) UtilWaitThread.sleep(100); Client client = new TestProxyClient("localhost", proxyPort, protocol).proxy(); Map<String, String> properties = new TreeMap<String, String>(); properties.put("password", ROOT_PASSWORD); ByteBuffer login = client.login("root", properties); String tableName = getUniqueNames(1)[0]; client.createTable(login, tableName, true, TimeType.MILLIS); assertTrue(c.tableOperations().exists(tableName)); WriterOptions options = new WriterOptions(); options.setDurability(Durability.NONE); String writer = client.createWriter(login, tableName, options); Map<ByteBuffer, List<ColumnUpdate>> cells = new TreeMap<ByteBuffer, List<ColumnUpdate>>(); ColumnUpdate column = new ColumnUpdate(bytes("cf"), bytes("cq")); column.setValue("value".getBytes()); cells.put(bytes("row"), Collections.singletonList(column)); client.update(writer, cells); client.closeWriter(writer); assertEquals(1, count(tableName)); restartTServer(); assertEquals(0, count(tableName)); ConditionalWriterOptions cfg = new ConditionalWriterOptions(); cfg.setDurability(Durability.LOG); String cwriter = client.createConditionalWriter(login, tableName, cfg); ConditionalUpdates updates = new ConditionalUpdates(); updates.addToConditions(new Condition(new Column(bytes("cf"), bytes("cq"), bytes("")))); updates.addToUpdates(column); Map<ByteBuffer, ConditionalStatus> status = client.updateRowsConditionally(cwriter, Collections.singletonMap(bytes("row"), updates)); assertEquals(ConditionalStatus.ACCEPTED, status.get(bytes("row"))); assertEquals(1, count(tableName)); restartTServer(); assertEquals(0, count(tableName)); proxyServer.stop(); }
From source file:org.apache.accumulo.test.proxy.ProxyDurabilityIT.java
License:Apache License
@Test public void testDurability() throws Exception { Connector c = getConnector();/*w w w. j av a2 s .c o m*/ Properties props = new Properties(); // Avoid issues with locally installed client configuration files with custom properties File emptyFile = Files.createTempFile(null, null).toFile(); emptyFile.deleteOnExit(); props.put("instance", c.getInstance().getInstanceName()); props.put("zookeepers", c.getInstance().getZooKeepers()); props.put("tokenClass", PasswordToken.class.getName()); props.put("clientConfigurationFile", emptyFile.toString()); TJSONProtocol.Factory protocol = new TJSONProtocol.Factory(); int proxyPort = PortUtils.getRandomFreePort(); final TServer proxyServer = Proxy.createProxyServer(HostAndPort.fromParts("localhost", proxyPort), protocol, props).server; while (!proxyServer.isServing()) UtilWaitThread.sleep(100); Client client = new TestProxyClient("localhost", proxyPort, protocol).proxy(); Map<String, String> properties = new TreeMap<>(); properties.put("password", ROOT_PASSWORD); ByteBuffer login = client.login("root", properties); String tableName = getUniqueNames(1)[0]; client.createTable(login, tableName, true, TimeType.MILLIS); assertTrue(c.tableOperations().exists(tableName)); WriterOptions options = new WriterOptions(); options.setDurability(Durability.NONE); String writer = client.createWriter(login, tableName, options); Map<ByteBuffer, List<ColumnUpdate>> cells = new TreeMap<>(); ColumnUpdate column = new ColumnUpdate(bytes("cf"), bytes("cq")); column.setValue("value".getBytes()); cells.put(bytes("row"), Collections.singletonList(column)); client.update(writer, cells); client.closeWriter(writer); assertEquals(1, count(tableName)); restartTServer(); assertEquals(0, count(tableName)); ConditionalWriterOptions cfg = new ConditionalWriterOptions(); cfg.setDurability(Durability.SYNC); String cwriter = client.createConditionalWriter(login, tableName, cfg); ConditionalUpdates updates = new ConditionalUpdates(); updates.addToConditions(new Condition(new Column(bytes("cf"), bytes("cq"), bytes("")))); updates.addToUpdates(column); Map<ByteBuffer, ConditionalStatus> status = client.updateRowsConditionally(cwriter, Collections.singletonMap(bytes("row"), updates)); assertEquals(ConditionalStatus.ACCEPTED, status.get(bytes("row"))); assertEquals(1, count(tableName)); restartTServer(); assertEquals(1, count(tableName)); proxyServer.stop(); }
From source file:org.springframework.data.hadoop.hive.BasicHiveTest.java
License:Apache License
@Test // disabled due to antlr incompatibility between Pig (which requires antlr 3.4) and Hive (3.0.x) public void testHiveServer() throws Exception { assumeThat(ctx.containsBean("hive-server"), is(Boolean.TRUE)); TServer server = ctx.getBean("hive-server", TServer.class); assertNotNull(server);/*from ww w . j ava 2 s .c o m*/ assertTrue(server.isServing()); }