List of usage examples for io.netty.buffer ByteBuf readBytes
public abstract ByteBuf readBytes(ByteBuffer dst);
From source file:com.linkedin.pinot.transport.perf.ScatterGatherPerfClient.java
License:Apache License
/** * Helper to send request to server and get back response * @param request//w w w .jav a 2 s. c om * @return * @throws InterruptedException * @throws ExecutionException * @throws IOException * @throws ClassNotFoundException */ private String sendRequestAndGetResponse(SimpleScatterGatherRequest request) throws InterruptedException, ExecutionException, IOException, ClassNotFoundException { CompositeFuture<ServerInstance, ByteBuf> future = _scatterGather.scatterGather(request); ByteBuf b = future.getOne(); String r = null; if (null != b) { byte[] b2 = new byte[b.readableBytes()]; b.readBytes(b2); r = new String(b2); } return r; }
From source file:com.linkedin.pinot.transport.scattergather.ScatterGatherTest.java
License:Apache License
@Test public void testSingleServer() throws Exception { MetricsRegistry registry = new MetricsRegistry(); // Server start int serverPort = 7071; NettyTCPServer server1 = new NettyTCPServer(serverPort, new TestRequestHandlerFactory(0, 1), null); Thread t1 = new Thread(server1); t1.start();/*from w ww . j av a2 s. c om*/ //Client setup ScheduledExecutorService timedExecutor = new ScheduledThreadPoolExecutor(1); ExecutorService poolExecutor = MoreExecutors.sameThreadExecutor(); ExecutorService service = new ThreadPoolExecutor(1, 1, 1, TimeUnit.DAYS, new LinkedBlockingDeque<Runnable>()); EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); NettyClientMetrics clientMetrics = new NettyClientMetrics(registry, "client_"); PooledNettyClientResourceManager rm = new PooledNettyClientResourceManager(eventLoopGroup, new HashedWheelTimer(), clientMetrics); KeyedPoolImpl<ServerInstance, NettyClientConnection> pool = new KeyedPoolImpl<ServerInstance, NettyClientConnection>( 1, 1, 300000, 1, rm, timedExecutor, poolExecutor, registry); rm.setPool(pool); ScatterGatherImpl scImpl = new ScatterGatherImpl(pool, service); SegmentIdSet pg = new SegmentIdSet(); pg.addSegment(new SegmentId("0")); ServerInstance serverInstance1 = new ServerInstance("localhost", serverPort); List<ServerInstance> instances = new ArrayList<ServerInstance>(); instances.add(serverInstance1); Map<ServerInstance, SegmentIdSet> pgMap = new HashMap<ServerInstance, SegmentIdSet>(); pgMap.put(serverInstance1, pg); String request = "request_0"; Map<SegmentIdSet, String> pgMapStr = new HashMap<SegmentIdSet, String>(); pgMapStr.put(pg, request); ScatterGatherRequest req = new TestScatterGatherRequest(pgMap, pgMapStr); CompositeFuture<ServerInstance, ByteBuf> fut = scImpl.scatterGather(req); Map<ServerInstance, ByteBuf> v = fut.get(); ByteBuf b = v.get(serverInstance1); byte[] b2 = new byte[b.readableBytes()]; b.readBytes(b2); String response = new String(b2); Assert.assertEquals(response, "response_0_0"); Assert.assertEquals(v.size(), 1); server1.shutdownGracefully(); pool.shutdown(); service.shutdown(); eventLoopGroup.shutdownGracefully(); }
From source file:com.linkedin.pinot.transport.scattergather.ScatterGatherTest.java
License:Apache License
@Test public void testMultipleServerHappy() throws Exception { MetricsRegistry registry = new MetricsRegistry(); // Server start int serverPort1 = 7071; int serverPort2 = 7072; int serverPort3 = 7073; int serverPort4 = 7074; NettyTCPServer server1 = new NettyTCPServer(serverPort1, new TestRequestHandlerFactory(0, 1), null); NettyTCPServer server2 = new NettyTCPServer(serverPort2, new TestRequestHandlerFactory(1, 1), null); NettyTCPServer server3 = new NettyTCPServer(serverPort3, new TestRequestHandlerFactory(2, 1), null); NettyTCPServer server4 = new NettyTCPServer(serverPort4, new TestRequestHandlerFactory(3, 1), null); Thread t1 = new Thread(server1); Thread t2 = new Thread(server2); Thread t3 = new Thread(server3); Thread t4 = new Thread(server4); t1.start();/*from w w w .jav a2s. co m*/ t2.start(); t3.start(); t4.start(); //Client setup ScheduledExecutorService timedExecutor = new ScheduledThreadPoolExecutor(1); ExecutorService poolExecutor = MoreExecutors.sameThreadExecutor(); ExecutorService service = new ThreadPoolExecutor(1, 1, 1, TimeUnit.DAYS, new LinkedBlockingDeque<Runnable>()); EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); NettyClientMetrics clientMetrics = new NettyClientMetrics(registry, "client_"); PooledNettyClientResourceManager rm = new PooledNettyClientResourceManager(eventLoopGroup, new HashedWheelTimer(), clientMetrics); KeyedPoolImpl<ServerInstance, NettyClientConnection> pool = new KeyedPoolImpl<ServerInstance, NettyClientConnection>( 1, 1, 300000, 1, rm, timedExecutor, poolExecutor, registry); rm.setPool(pool); SegmentIdSet pg1 = new SegmentIdSet(); pg1.addSegment(new SegmentId("0")); SegmentIdSet pg2 = new SegmentIdSet(); pg2.addSegment(new SegmentId("1")); SegmentIdSet pg3 = new SegmentIdSet(); pg3.addSegment(new SegmentId("2")); SegmentIdSet pg4 = new SegmentIdSet(); pg4.addSegment(new SegmentId("3")); ServerInstance serverInstance1 = new ServerInstance("localhost", serverPort1); ServerInstance serverInstance2 = new ServerInstance("localhost", serverPort2); ServerInstance serverInstance3 = new ServerInstance("localhost", serverPort3); ServerInstance serverInstance4 = new ServerInstance("localhost", serverPort4); Map<ServerInstance, SegmentIdSet> pgMap = new HashMap<ServerInstance, SegmentIdSet>(); pgMap.put(serverInstance1, pg1); pgMap.put(serverInstance2, pg2); pgMap.put(serverInstance3, pg3); pgMap.put(serverInstance4, pg4); String request1 = "request_0"; String request2 = "request_1"; String request3 = "request_2"; String request4 = "request_3"; Map<SegmentIdSet, String> pgMapStr = new HashMap<SegmentIdSet, String>(); pgMapStr.put(pg1, request1); pgMapStr.put(pg2, request2); pgMapStr.put(pg3, request3); pgMapStr.put(pg4, request4); ScatterGatherRequest req = new TestScatterGatherRequest(pgMap, pgMapStr); ScatterGatherImpl scImpl = new ScatterGatherImpl(pool, service); CompositeFuture<ServerInstance, ByteBuf> fut = scImpl.scatterGather(req); Map<ServerInstance, ByteBuf> v = fut.get(); Assert.assertEquals(v.size(), 4); ByteBuf b = v.get(serverInstance1); byte[] b2 = new byte[b.readableBytes()]; b.readBytes(b2); String response = new String(b2); Assert.assertEquals(response, "response_0_0"); b = v.get(serverInstance2); b2 = new byte[b.readableBytes()]; b.readBytes(b2); response = new String(b2); Assert.assertEquals(response, "response_1_0"); b = v.get(serverInstance3); b2 = new byte[b.readableBytes()]; b.readBytes(b2); response = new String(b2); Assert.assertEquals(response, "response_2_0"); b = v.get(serverInstance4); b2 = new byte[b.readableBytes()]; b.readBytes(b2); response = new String(b2); Assert.assertEquals(response, "response_3_0"); server1.shutdownGracefully(); server2.shutdownGracefully(); server3.shutdownGracefully(); server4.shutdownGracefully(); pool.shutdown(); service.shutdown(); eventLoopGroup.shutdownGracefully(); }
From source file:com.linkedin.pinot.transport.scattergather.ScatterGatherTest.java
License:Apache License
@Test public void testMultipleServerTimeout() throws Exception { MetricsRegistry registry = new MetricsRegistry(); // Server start int serverPort1 = 7081; int serverPort2 = 7082; int serverPort3 = 7083; int serverPort4 = 7084; // Timeout server NettyTCPServer server1 = new NettyTCPServer(serverPort1, new TestRequestHandlerFactory(0, 1), null); NettyTCPServer server2 = new NettyTCPServer(serverPort2, new TestRequestHandlerFactory(1, 1), null); NettyTCPServer server3 = new NettyTCPServer(serverPort3, new TestRequestHandlerFactory(2, 1), null); NettyTCPServer server4 = new NettyTCPServer(serverPort4, new TestRequestHandlerFactory(3, 1, 7000, false), null);//from w w w. j a va 2s .com Thread t1 = new Thread(server1); Thread t2 = new Thread(server2); Thread t3 = new Thread(server3); Thread t4 = new Thread(server4); t1.start(); t2.start(); t3.start(); t4.start(); //Client setup ScheduledExecutorService timedExecutor = new ScheduledThreadPoolExecutor(1); ExecutorService service = new ThreadPoolExecutor(5, 5, 5, TimeUnit.DAYS, new LinkedBlockingDeque<Runnable>()); EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); NettyClientMetrics clientMetrics = new NettyClientMetrics(registry, "client_"); PooledNettyClientResourceManager rm = new PooledNettyClientResourceManager(eventLoopGroup, new HashedWheelTimer(), clientMetrics); KeyedPoolImpl<ServerInstance, NettyClientConnection> pool = new KeyedPoolImpl<ServerInstance, NettyClientConnection>( 1, 1, 300000, 1, rm, timedExecutor, service, registry); rm.setPool(pool); SegmentIdSet pg1 = new SegmentIdSet(); pg1.addSegment(new SegmentId("0")); SegmentIdSet pg2 = new SegmentIdSet(); pg2.addSegment(new SegmentId("1")); SegmentIdSet pg3 = new SegmentIdSet(); pg3.addSegment(new SegmentId("2")); SegmentIdSet pg4 = new SegmentIdSet(); pg4.addSegment(new SegmentId("3")); ServerInstance serverInstance1 = new ServerInstance("localhost", serverPort1); ServerInstance serverInstance2 = new ServerInstance("localhost", serverPort2); ServerInstance serverInstance3 = new ServerInstance("localhost", serverPort3); ServerInstance serverInstance4 = new ServerInstance("localhost", serverPort4); Map<ServerInstance, SegmentIdSet> pgMap = new HashMap<ServerInstance, SegmentIdSet>(); pgMap.put(serverInstance1, pg1); pgMap.put(serverInstance2, pg2); pgMap.put(serverInstance3, pg3); pgMap.put(serverInstance4, pg4); String request1 = "request_0"; String request2 = "request_1"; String request3 = "request_2"; String request4 = "request_3"; Map<SegmentIdSet, String> pgMapStr = new HashMap<SegmentIdSet, String>(); pgMapStr.put(pg1, request1); pgMapStr.put(pg2, request2); pgMapStr.put(pg3, request3); pgMapStr.put(pg4, request4); ScatterGatherRequest req = new TestScatterGatherRequest(pgMap, pgMapStr, new RoundRobinReplicaSelection(), ReplicaSelectionGranularity.SEGMENT_ID_SET, 0, 1000); ScatterGatherImpl scImpl = new ScatterGatherImpl(pool, service); CompositeFuture<ServerInstance, ByteBuf> fut = scImpl.scatterGather(req); Map<ServerInstance, ByteBuf> v = fut.get(); //Only 3 servers return value. Assert.assertEquals(v.size(), 3); ByteBuf b = v.get(serverInstance1); byte[] b2 = new byte[b.readableBytes()]; b.readBytes(b2); String response = new String(b2); Assert.assertEquals(response, "response_0_0"); b = v.get(serverInstance2); b2 = new byte[b.readableBytes()]; b.readBytes(b2); response = new String(b2); Assert.assertEquals(response, "response_1_0"); b = v.get(serverInstance3); b2 = new byte[b.readableBytes()]; b.readBytes(b2); response = new String(b2); Assert.assertEquals(response, "response_2_0"); //No response from 4th server Assert.assertNull(v.get(serverInstance4), "No response from 4th server"); Map<ServerInstance, Throwable> errorMap = fut.getError(); Assert.assertEquals(errorMap.size(), 1, "One error"); Assert.assertNotNull(errorMap.get(serverInstance4), "Server4 returned timeout"); System.out.println("Error is :" + errorMap.get(serverInstance4)); Thread.sleep(3000); System.out.println("Pool Stats :" + pool.getStats()); pool.getStats().refresh(); Assert.assertEquals(pool.getStats().getTotalBadDestroyed(), 1, "Total Bad destroyed"); pool.shutdown(); service.shutdown(); eventLoopGroup.shutdownGracefully(); server1.shutdownGracefully(); server2.shutdownGracefully(); server3.shutdownGracefully(); server4.shutdownGracefully(); }
From source file:com.linkedin.pinot.transport.scattergather.ScatterGatherTest.java
License:Apache License
@Test public void testMultipleServerError() throws Exception { MetricsRegistry registry = new MetricsRegistry(); // Server start int serverPort1 = 7091; int serverPort2 = 7092; int serverPort3 = 7093; int serverPort4 = 7094; // error server NettyTCPServer server1 = new NettyTCPServer(serverPort1, new TestRequestHandlerFactory(0, 1), null); NettyTCPServer server2 = new NettyTCPServer(serverPort2, new TestRequestHandlerFactory(1, 1), null); NettyTCPServer server3 = new NettyTCPServer(serverPort3, new TestRequestHandlerFactory(2, 1), null); NettyTCPServer server4 = new NettyTCPServer(serverPort4, new TestRequestHandlerFactory(3, 1, 1000, true), null);/*from w ww. j ava2 s. c o m*/ Thread t1 = new Thread(server1); Thread t2 = new Thread(server2); Thread t3 = new Thread(server3); Thread t4 = new Thread(server4); t1.start(); t2.start(); t3.start(); t4.start(); //Client setup ScheduledExecutorService timedExecutor = new ScheduledThreadPoolExecutor(1); ExecutorService poolExecutor = MoreExecutors.sameThreadExecutor(); ExecutorService service = new ThreadPoolExecutor(1, 1, 1, TimeUnit.DAYS, new LinkedBlockingDeque<Runnable>()); EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); NettyClientMetrics clientMetrics = new NettyClientMetrics(registry, "client_"); PooledNettyClientResourceManager rm = new PooledNettyClientResourceManager(eventLoopGroup, new HashedWheelTimer(), clientMetrics); KeyedPoolImpl<ServerInstance, NettyClientConnection> pool = new KeyedPoolImpl<ServerInstance, NettyClientConnection>( 1, 1, 300000, 1, rm, timedExecutor, poolExecutor, registry); rm.setPool(pool); SegmentIdSet pg1 = new SegmentIdSet(); pg1.addSegment(new SegmentId("0")); SegmentIdSet pg2 = new SegmentIdSet(); pg2.addSegment(new SegmentId("1")); SegmentIdSet pg3 = new SegmentIdSet(); pg3.addSegment(new SegmentId("2")); SegmentIdSet pg4 = new SegmentIdSet(); pg4.addSegment(new SegmentId("3")); ServerInstance serverInstance1 = new ServerInstance("localhost", serverPort1); ServerInstance serverInstance2 = new ServerInstance("localhost", serverPort2); ServerInstance serverInstance3 = new ServerInstance("localhost", serverPort3); ServerInstance serverInstance4 = new ServerInstance("localhost", serverPort4); Map<ServerInstance, SegmentIdSet> pgMap = new HashMap<ServerInstance, SegmentIdSet>(); pgMap.put(serverInstance1, pg1); pgMap.put(serverInstance2, pg2); pgMap.put(serverInstance3, pg3); pgMap.put(serverInstance4, pg4); String request1 = "request_0"; String request2 = "request_1"; String request3 = "request_2"; String request4 = "request_3"; Map<SegmentIdSet, String> pgMapStr = new HashMap<SegmentIdSet, String>(); pgMapStr.put(pg1, request1); pgMapStr.put(pg2, request2); pgMapStr.put(pg3, request3); pgMapStr.put(pg4, request4); ScatterGatherRequest req = new TestScatterGatherRequest(pgMap, pgMapStr, new RoundRobinReplicaSelection(), ReplicaSelectionGranularity.SEGMENT_ID_SET, 0, 1000); ScatterGatherImpl scImpl = new ScatterGatherImpl(pool, service); CompositeFuture<ServerInstance, ByteBuf> fut = scImpl.scatterGather(req); Map<ServerInstance, ByteBuf> v = fut.get(); //Only 3 servers return value. Assert.assertEquals(v.size(), 3); ByteBuf b = v.get(serverInstance1); byte[] b2 = new byte[b.readableBytes()]; b.readBytes(b2); String response = new String(b2); Assert.assertEquals(response, "response_0_0"); b = v.get(serverInstance2); b2 = new byte[b.readableBytes()]; b.readBytes(b2); response = new String(b2); Assert.assertEquals(response, "response_1_0"); b = v.get(serverInstance3); b2 = new byte[b.readableBytes()]; b.readBytes(b2); response = new String(b2); Assert.assertEquals(response, "response_2_0"); //No response from 4th server Assert.assertNull(v.get(serverInstance4), "No response from 4th server"); Map<ServerInstance, Throwable> errorMap = fut.getError(); Assert.assertEquals(errorMap.size(), 1, "One error"); Assert.assertNotNull(errorMap.get(serverInstance4), "Server4 returned timeout"); System.out.println("Error is :" + errorMap.get(serverInstance4)); Thread.sleep(3000); System.out.println("Pool Stats :" + pool.getStats()); pool.getStats().refresh(); Assert.assertEquals(pool.getStats().getTotalBadDestroyed(), 1, "Total Bad destroyed"); pool.shutdown(); service.shutdown(); eventLoopGroup.shutdownGracefully(); server1.shutdownGracefully(); server2.shutdownGracefully(); server3.shutdownGracefully(); server4.shutdownGracefully(); }
From source file:com.ltln.modules.openflow.core.util.StringByteSerializer.java
License:Apache License
public static String readFrom(final ByteBuf data, final int length) { byte[] stringBytes = new byte[length]; data.readBytes(stringBytes); // find the first index of 0 int index = 0; for (byte b : stringBytes) { if (0 == b) break; ++index;/*w w w . j a v a 2s .c o m*/ } return new String(Arrays.copyOf(stringBytes, index), Charset.forName("ascii")); }
From source file:com.mastfrog.acteur.sse.SseTest.java
License:Open Source License
@Test(timeout = 90000) public void test(TestHarness harn) throws Throwable { long when = System.currentTimeMillis(); System.err.println("PORT " + harn.getPort()); System.err.flush();// w w w .j a va2 s . com harn.get("/foo").setTimeout(Duration.standardSeconds(60)).log().go().assertStatus(NOT_FOUND); final StringBuilder content = new StringBuilder(); final CallResult[] res = new CallResult[1]; final AtomicInteger count = new AtomicInteger(); res[0] = harn.get("/sse").on(StateType.ContentReceived, new Receiver<HttpContent>() { @Override public void receive(HttpContent c) { if (c == null) { System.out.println("GOT NULL BUF"); return; } ByteBuf buf = c.content(); byte[] bytes = new byte[buf.readableBytes()]; buf.readBytes(bytes); String s = new String(bytes, CharsetUtil.UTF_8); System.out.println("READ " + s); content.append(s); int ct = count.incrementAndGet(); if (ct > 5) { res[0].cancel(); } } }).setTimeout(Duration.standardSeconds(60)).go(); try { res[0].await(); } catch (InterruptedException ex) { ex.printStackTrace(); } List<String> ids = new ArrayList<>(); List<String> items = new ArrayList<>(); for (String line : content.toString().split("\n")) { if (line.startsWith("id: ")) { ids.add(line.substring(4)); } if (line.startsWith("data: ")) { items.add(line.substring(6)); } } int last = -1; for (String s : ids) { Matcher m = Pattern.compile("(\\d+)\\-(\\d*)$").matcher(s); assertTrue(m.find()); int id = Integer.parseInt(m.group(1)); long ts = Long.parseLong(m.group(2)); assertTrue(id > last); assertTrue(ts > when); if (last != -1) { assertEquals(last + 1, id); } last = id; } last = -1; for (String item : items) { Matcher m = Pattern.compile("hello (\\d+)").matcher(item); assertTrue(m.find()); int val = Integer.parseInt(m.group(1)); if (last != -1) { assertEquals(last + 1, val); } last = val; } }
From source file:com.mastfrog.netty.http.client.ResponseHandler.java
License:Open Source License
protected void internalReceive(HttpResponseStatus status, HttpHeaders headers, ByteBuf content) { try {//from w w w .j av a2 s. co m if (status.code() > 399) { byte[] b = new byte[content.readableBytes()]; content.readBytes(b); onErrorResponse(status, headers, new String(b, CharsetUtil.UTF_8)); return; } if (type == ByteBuf.class) { _doReceive(status, headers, type.cast(content)); } else if (type == String.class || type == CharSequence.class) { byte[] b = new byte[content.readableBytes()]; content.readBytes(b); _doReceive(status, headers, type.cast(new String(b, CharsetUtil.UTF_8))); } else if (type == byte[].class) { byte[] b = new byte[content.readableBytes()]; content.readBytes(b); _doReceive(status, headers, type.cast(b)); } else { byte[] b = new byte[content.readableBytes()]; content.readBytes(b); try { Object o = mapper.readValue(b, type); _doReceive(status, headers, type.cast(o)); } catch (JsonParseException ex) { content.resetReaderIndex(); try { String s = Streams.readString(new ByteBufInputStream(content), "UTF-8"); onErrorResponse(HttpResponseStatus.REQUESTED_RANGE_NOT_SATISFIABLE, headers, s); } catch (IOException ex1) { Exceptions.chuck(ex1); } } catch (Exception ex) { Exceptions.chuck(ex); } } } finally { latch.countDown(); } }
From source file:com.mastfrog.scamper.password.crypto.EncryptingCodec.java
License:Open Source License
protected void compress(ByteBuf in, ByteBuf out) throws IOException { byte[] toRead = new byte[in.readableBytes()]; in.readBytes(toRead); byte[] outBound = encrypt.encrypt(toRead); out.writeBytes(outBound);// w w w. j ava 2 s . c om }
From source file:com.mastfrog.scamper.password.crypto.EncryptingCodec.java
License:Open Source License
protected void uncompress(ByteBuf in, ByteBuf out) throws IOException { byte[] toWrite = new byte[in.readableBytes()]; in.readBytes(toWrite); byte[] outBound = encrypt.decrypt(toWrite); out.writeBytes(outBound);//from w w w.j a va 2 s . c o m }