List of usage examples for io.netty.buffer UnpooledByteBufAllocator DEFAULT
UnpooledByteBufAllocator DEFAULT
To view the source code for io.netty.buffer UnpooledByteBufAllocator DEFAULT.
Click Source Link
From source file:org.apache.bookkeeper.test.BookieClientTest.java
License:Apache License
@Test public void testWriteGaps() throws Exception { final Object notifyObject = new Object(); byte[] passwd = new byte[20]; Arrays.fill(passwd, (byte) 'a'); BookieSocketAddress addr = bs.getLocalAddress(); ResultStruct arc = new ResultStruct(); BookieClient bc = new BookieClientImpl(new ClientConfiguration(), eventLoopGroup, UnpooledByteBufAllocator.DEFAULT, executor, scheduler, NullStatsLogger.INSTANCE); ByteBufList bb = createByteBuffer(1, 1, 1); bc.addEntry(addr, 1, passwd, 1, bb, wrcb, arc, BookieProtocol.FLAG_NONE, false, WriteFlag.NONE); synchronized (arc) { arc.wait(1000);//from w ww . j a v a 2s .com assertEquals(0, arc.rc); bc.readEntry(addr, 1, 1, recb, arc, BookieProtocol.FLAG_NONE); arc.wait(1000); assertEquals(0, arc.rc); assertEquals(1, arc.entry.getInt()); } bb = createByteBuffer(2, 1, 2); bc.addEntry(addr, 1, passwd, 2, bb, wrcb, null, BookieProtocol.FLAG_NONE, false, WriteFlag.NONE); bb = createByteBuffer(3, 1, 3); bc.addEntry(addr, 1, passwd, 3, bb, wrcb, null, BookieProtocol.FLAG_NONE, false, WriteFlag.NONE); bb = createByteBuffer(5, 1, 5); bc.addEntry(addr, 1, passwd, 5, bb, wrcb, null, BookieProtocol.FLAG_NONE, false, WriteFlag.NONE); bb = createByteBuffer(7, 1, 7); bc.addEntry(addr, 1, passwd, 7, bb, wrcb, null, BookieProtocol.FLAG_NONE, false, WriteFlag.NONE); synchronized (notifyObject) { bb = createByteBuffer(11, 1, 11); bc.addEntry(addr, 1, passwd, 11, bb, wrcb, notifyObject, BookieProtocol.FLAG_NONE, false, WriteFlag.NONE); notifyObject.wait(); } synchronized (arc) { bc.readEntry(addr, 1, 6, recb, arc, BookieProtocol.FLAG_NONE); arc.wait(1000); assertEquals(BKException.Code.NoSuchEntryException, arc.rc); } synchronized (arc) { bc.readEntry(addr, 1, 7, recb, arc, BookieProtocol.FLAG_NONE); arc.wait(1000); assertEquals(0, arc.rc); assertEquals(7, arc.entry.getInt(), BookieProtocol.FLAG_NONE); } synchronized (arc) { bc.readEntry(addr, 1, 1, recb, arc, BookieProtocol.FLAG_NONE); arc.wait(1000); assertEquals(0, arc.rc); assertEquals(1, arc.entry.getInt()); } synchronized (arc) { bc.readEntry(addr, 1, 2, recb, arc, BookieProtocol.FLAG_NONE); arc.wait(1000); assertEquals(0, arc.rc); assertEquals(2, arc.entry.getInt()); } synchronized (arc) { bc.readEntry(addr, 1, 3, recb, arc, BookieProtocol.FLAG_NONE); arc.wait(1000); assertEquals(0, arc.rc); assertEquals(3, arc.entry.getInt()); } synchronized (arc) { bc.readEntry(addr, 1, 4, recb, arc, BookieProtocol.FLAG_NONE); arc.wait(1000); assertEquals(BKException.Code.NoSuchEntryException, arc.rc); } synchronized (arc) { bc.readEntry(addr, 1, 11, recb, arc, BookieProtocol.FLAG_NONE); arc.wait(1000); assertEquals(0, arc.rc); assertEquals(11, arc.entry.getInt()); } synchronized (arc) { bc.readEntry(addr, 1, 5, recb, arc, BookieProtocol.FLAG_NONE); arc.wait(1000); assertEquals(0, arc.rc); assertEquals(5, arc.entry.getInt()); } synchronized (arc) { bc.readEntry(addr, 1, 10, recb, arc, BookieProtocol.FLAG_NONE); arc.wait(1000); assertEquals(BKException.Code.NoSuchEntryException, arc.rc); } synchronized (arc) { bc.readEntry(addr, 1, 12, recb, arc, BookieProtocol.FLAG_NONE); arc.wait(1000); assertEquals(BKException.Code.NoSuchEntryException, arc.rc); } synchronized (arc) { bc.readEntry(addr, 1, 13, recb, arc, BookieProtocol.FLAG_NONE); arc.wait(1000); assertEquals(BKException.Code.NoSuchEntryException, arc.rc); } }
From source file:org.apache.bookkeeper.test.BookieClientTest.java
License:Apache License
@Test public void testNoLedger() throws Exception { ResultStruct arc = new ResultStruct(); BookieSocketAddress addr = bs.getLocalAddress(); BookieClient bc = new BookieClientImpl(new ClientConfiguration(), eventLoopGroup, UnpooledByteBufAllocator.DEFAULT, executor, scheduler, NullStatsLogger.INSTANCE); synchronized (arc) { bc.readEntry(addr, 2, 13, recb, arc, BookieProtocol.FLAG_NONE); arc.wait(1000);//from w w w . j av a2 s . c om assertEquals(BKException.Code.NoSuchLedgerExistsException, arc.rc); } }
From source file:org.apache.bookkeeper.test.BookieClientTest.java
License:Apache License
@Test public void testGetBookieInfo() throws IOException, InterruptedException { BookieSocketAddress addr = bs.getLocalAddress(); BookieClient bc = new BookieClientImpl(new ClientConfiguration(), new NioEventLoopGroup(), UnpooledByteBufAllocator.DEFAULT, executor, scheduler, NullStatsLogger.INSTANCE); long flags = BookkeeperProtocol.GetBookieInfoRequest.Flags.FREE_DISK_SPACE_VALUE | BookkeeperProtocol.GetBookieInfoRequest.Flags.TOTAL_DISK_CAPACITY_VALUE; class CallbackObj { int rc;/*from w ww .jav a 2 s. co m*/ long requested; long freeDiskSpace, totalDiskCapacity; CountDownLatch latch = new CountDownLatch(1); CallbackObj(long requested) { this.requested = requested; this.rc = 0; this.freeDiskSpace = 0L; this.totalDiskCapacity = 0L; } } CallbackObj obj = new CallbackObj(flags); bc.getBookieInfo(addr, flags, new GetBookieInfoCallback() { @Override public void getBookieInfoComplete(int rc, BookieInfo bInfo, Object ctx) { CallbackObj obj = (CallbackObj) ctx; obj.rc = rc; if (rc == Code.OK) { if ((obj.requested & BookkeeperProtocol.GetBookieInfoRequest.Flags.FREE_DISK_SPACE_VALUE) != 0) { obj.freeDiskSpace = bInfo.getFreeDiskSpace(); } if ((obj.requested & BookkeeperProtocol.GetBookieInfoRequest.Flags.TOTAL_DISK_CAPACITY_VALUE) != 0) { obj.totalDiskCapacity = bInfo.getTotalDiskSpace(); } } obj.latch.countDown(); } }, obj); obj.latch.await(); System.out.println("Return code: " + obj.rc + "FreeDiskSpace: " + obj.freeDiskSpace + " TotalCapacity: " + obj.totalDiskCapacity); assertTrue("GetBookieInfo failed with error " + obj.rc, obj.rc == Code.OK); assertTrue("GetBookieInfo failed with error " + obj.rc, obj.freeDiskSpace <= obj.totalDiskCapacity); assertTrue("GetBookieInfo failed with error " + obj.rc, obj.totalDiskCapacity > 0); }
From source file:org.apache.bookkeeper.tools.cli.commands.bookie.ReadLedgerCommand.java
License:Apache License
private boolean readledger(ServerConfiguration serverConf, ReadLedgerFlags flags) throws InterruptedException, BKException, IOException { long lastEntry = flags.lastEntryId; final BookieSocketAddress bookie; if (flags.bookieAddresss != null) { // A particular bookie was specified bookie = new BookieSocketAddress(flags.bookieAddresss); } else {// w w w . java 2 s . c o m bookie = null; } ClientConfiguration conf = new ClientConfiguration(); conf.addConfiguration(serverConf); try (BookKeeperAdmin bk = new BookKeeperAdmin(conf)) { if (flags.forceRecovery) { // Force the opening of the ledger to trigger recovery try (LedgerHandle lh = bk.openLedger(flags.ledgerId)) { if (lastEntry == -1 || lastEntry > lh.getLastAddConfirmed()) { lastEntry = lh.getLastAddConfirmed(); } } } if (bookie == null) { // No bookie was specified, use normal bk client Iterator<LedgerEntry> entries = bk.readEntries(flags.ledgerId, flags.firstEntryId, lastEntry) .iterator(); while (entries.hasNext()) { LedgerEntry entry = entries.next(); formatEntry(entry, flags.msg); } } else { // Use BookieClient to target a specific bookie EventLoopGroup eventLoopGroup = new NioEventLoopGroup(); OrderedExecutor executor = OrderedExecutor.newBuilder().numThreads(1).name("BookieClientScheduler") .build(); ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor( new DefaultThreadFactory("BookKeeperClientSchedulerPool")); BookieClient bookieClient = new BookieClientImpl(conf, eventLoopGroup, UnpooledByteBufAllocator.DEFAULT, executor, scheduler, NullStatsLogger.INSTANCE); LongStream.range(flags.firstEntryId, lastEntry).forEach(entryId -> { CompletableFuture<Void> future = new CompletableFuture<>(); bookieClient.readEntry(bookie, flags.ledgerId, entryId, (rc, ledgerId1, entryId1, buffer, ctx) -> { if (rc != BKException.Code.OK) { LOG.error("Failed to read entry {} -- {}", entryId1, BKException.getMessage(rc)); future.completeExceptionally(BKException.create(rc)); return; } System.out .println("--------- Lid=" + ledgerIdFormatter.formatLedgerId(flags.ledgerId) + ", Eid=" + entryId + " ---------"); if (flags.msg) { System.out.println("Data: " + ByteBufUtil.prettyHexDump(buffer)); } future.complete(null); }, null, BookieProtocol.FLAG_NONE); try { future.get(); } catch (Exception e) { LOG.error("Error future.get while reading entries from ledger {}", flags.ledgerId, e); } }); eventLoopGroup.shutdownGracefully(); executor.shutdown(); bookieClient.close(); } } return true; }
From source file:org.apache.bookkeeper.tools.cli.commands.bookie.ReadLedgerCommandTest.java
License:Apache License
@Override public void setup() throws Exception { super.setup(); PowerMockito.whenNew(ServerConfiguration.class).withNoArguments().thenReturn(conf); PowerMockito.whenNew(BookieSocketAddress.class).withArguments(anyString()).thenReturn(bookieSocketAddress); PowerMockito.whenNew(ClientConfiguration.class).withNoArguments().thenReturn(clientConfiguration); PowerMockito.whenNew(BookKeeperAdmin.class).withParameterTypes(ClientConfiguration.class) .withArguments(eq(clientConfiguration)).thenReturn(bookKeeperAdmin); when(bookKeeperAdmin.openLedger(anyLong())).thenReturn(ledgerHandle); when(ledgerHandle.getLastAddConfirmed()).thenReturn(1L); List<LedgerEntry> entries = new LinkedList<>(); entries.add(entry);/*from ww w . j a v a 2 s . c o m*/ when(entry.getLedgerId()).thenReturn(1L); when(entry.getEntryId()).thenReturn(1L); when(entry.getLength()).thenReturn(1L); when(bookKeeperAdmin.readEntries(anyLong(), anyLong(), anyLong())).thenReturn(entries); PowerMockito.whenNew(NioEventLoopGroup.class).withNoArguments().thenReturn(nioEventLoopGroup); PowerMockito.mockStatic(OrderedExecutor.class); OrderedExecutor.Builder builder = mock(OrderedExecutor.Builder.class); when(OrderedExecutor.newBuilder()).thenReturn(builder); when(builder.numThreads(anyInt())).thenCallRealMethod(); when(builder.name(anyString())).thenCallRealMethod(); when(builder.build()).thenReturn(orderedExecutor); PowerMockito.mockStatic(Executors.class); PowerMockito.whenNew(DefaultThreadFactory.class).withArguments(anyString()) .thenReturn(defaultThreadFactory); when(Executors.newSingleThreadScheduledExecutor(eq(defaultThreadFactory))) .thenReturn(scheduledExecutorService); PowerMockito.whenNew(BookieClientImpl.class) .withArguments(eq(clientConfiguration), eq(nioEventLoopGroup), eq(UnpooledByteBufAllocator.DEFAULT), eq(orderedExecutor), eq(scheduledExecutorService), eq(NullStatsLogger.INSTANCE)) .thenReturn(bookieClient); }
From source file:org.apache.bookkeeper.tools.cli.commands.bookie.ReadLedgerCommandTest.java
License:Apache License
@Test public void testWithBookieAddress() throws Exception { ReadLedgerCommand cmd = new ReadLedgerCommand(); Assert.assertTrue(cmd.apply(bkFlags, new String[] { "-b", "localhost:9000" })); verifyNew(NioEventLoopGroup.class, times(1)).withNoArguments(); verifyNew(DefaultThreadFactory.class, times(1)).withArguments(anyString()); verifyNew(BookieClientImpl.class, times(1)).withArguments(eq(clientConfiguration), eq(nioEventLoopGroup), eq(UnpooledByteBufAllocator.DEFAULT), eq(orderedExecutor), eq(scheduledExecutorService), eq(NullStatsLogger.INSTANCE)); verify(nioEventLoopGroup, times(1)).shutdownGracefully(); verify(orderedExecutor, times(1)).shutdown(); verify(bookieClient, times(1)).close(); }
From source file:org.apache.drill.exec.rpc.control.TestCustomTunnel.java
License:Apache License
public TestCustomTunnel() { buf1 = UnpooledByteBufAllocator.DEFAULT.buffer(1024); Random r = new Random(); this.expected = new byte[1024]; r.nextBytes(expected);/* w ww .ja v a2 s . co m*/ buf1.writeBytes(expected); }
From source file:org.apache.drill.exec.store.CachedSingleFileSystem.java
License:Apache License
public CachedSingleFileSystem(String path) throws IOException { this.path = path; File f = new File(path); long length = f.length(); if (length > Integer.MAX_VALUE) { throw new UnsupportedOperationException("Cached file system only supports files of less than 2GB."); }//from www .j a va2 s . c om System.out.println(length); try (InputStream is = new BufferedInputStream(new FileInputStream(path))) { byte[] buffer = new byte[64 * 1024]; this.file = UnpooledByteBufAllocator.DEFAULT.directBuffer((int) length); int read; while ((read = is.read(buffer)) > 0) { file.writeBytes(buffer, 0, read); } } }
From source file:org.apache.drill.exec.util.DecimalUtility.java
License:Apache License
public static BigDecimal getBigDecimalFromDense(DrillBuf data, int startIndex, int nDecimalDigits, int scale, int maxPrecision, int width) { /* This method converts the dense representation to * an intermediate representation. The intermediate * representation has one more integer than the dense * representation./*from w w w.j av a 2s .c o m*/ */ byte[] intermediateBytes = new byte[((nDecimalDigits + 1) * INTEGER_SIZE)]; // Start storing from the least significant byte of the first integer int intermediateIndex = 3; int[] mask = { 0x03, 0x0F, 0x3F, 0xFF }; int[] reverseMask = { 0xFC, 0xF0, 0xC0, 0x00 }; int maskIndex; int shiftOrder; byte shiftBits; // TODO: Some of the logic here is common with casting from Dense to Sparse types, factor out common code if (maxPrecision == 38) { maskIndex = 0; shiftOrder = 6; shiftBits = 0x00; intermediateBytes[intermediateIndex++] = (byte) (data.getByte(startIndex) & 0x7F); } else if (maxPrecision == 28) { maskIndex = 1; shiftOrder = 4; shiftBits = (byte) ((data.getByte(startIndex) & 0x03) << shiftOrder); intermediateBytes[intermediateIndex++] = (byte) (((data.getByte(startIndex) & 0x3C) & 0xFF) >>> 2); } else { throw new UnsupportedOperationException("Dense types with max precision 38 and 28 are only supported"); } int inputIndex = 1; boolean sign = false; if ((data.getByte(startIndex) & 0x80) != 0) { sign = true; } while (inputIndex < width) { intermediateBytes[intermediateIndex] = (byte) ((shiftBits) | (((data.getByte(startIndex + inputIndex) & reverseMask[maskIndex]) & 0xFF) >>> (8 - shiftOrder))); shiftBits = (byte) ((data.getByte(startIndex + inputIndex) & mask[maskIndex]) << shiftOrder); inputIndex++; intermediateIndex++; if (((inputIndex - 1) % INTEGER_SIZE) == 0) { shiftBits = (byte) ((shiftBits & 0xFF) >>> 2); maskIndex++; shiftOrder -= 2; } } /* copy the last byte */ intermediateBytes[intermediateIndex] = shiftBits; if (sign == true) { intermediateBytes[0] = (byte) (intermediateBytes[0] | 0x80); } final ByteBuf intermediate = UnpooledByteBufAllocator.DEFAULT.buffer(intermediateBytes.length); try { intermediate.setBytes(0, intermediateBytes); BigDecimal ret = getBigDecimalFromIntermediate(intermediate, 0, nDecimalDigits + 1, scale); return ret; } finally { intermediate.release(); } }
From source file:org.apache.flink.runtime.io.network.netty.PartitionRequestClientHandlerTest.java
License:Apache License
/** * Returns a deserialized buffer message as it would be received during runtime. *//*from w w w .j a v a 2s .c om*/ private BufferResponse createBufferResponse(Buffer buffer, int sequenceNumber, InputChannelID receivingChannelId) throws IOException { // Mock buffer to serialize BufferResponse resp = new BufferResponse(buffer, sequenceNumber, receivingChannelId); ByteBuf serialized = resp.write(UnpooledByteBufAllocator.DEFAULT); // Skip general header bytes serialized.readBytes(NettyMessage.HEADER_LENGTH); BufferResponse deserialized = new BufferResponse(); // Deserialize the bytes again. We have to go this way, because we only partly deserialize // the header of the response and wait for a buffer from the buffer pool to copy the payload // data into. deserialized.readFrom(serialized); return deserialized; }