void endTargetBlock(byte[] packetBuf) throws IOException { DFSPacket packet = new DFSPacket(packetBuf, 0, blockOffset4Target, seqNo4Target++, stripedWriter.getChecksumSize(), true); packet.writeTo(targetOutputStream); targetOutputStream.flush(); }
DFSPacket packet = new DFSPacket(packetBuf, stripedWriter.getMaxChunksPerPacket(), blockOffset4Target, seqNo4Target++,
/** * For heartbeat packets, create buffer directly by new byte[] * since heartbeats should not be blocked. */ private DFSPacket createHeartbeatPacket() { final byte[] buf = new byte[PacketHeader.PKT_MAX_HEADER_LEN]; return new DFSPacket(buf, 0, 0, DFSPacket.HEART_BEAT_SEQNO, 0, false); }
/** * For heartbeat packets, create buffer directly by new byte[] * since heartbeats should not be blocked. */ private DFSPacket createHeartbeatPacket() throws InterruptedIOException { final byte[] buf = new byte[PacketHeader.PKT_MAX_HEADER_LEN]; return new DFSPacket(buf, 0, 0, DFSPacket.HEART_BEAT_SEQNO, getChecksumSize(), false); }
/** * For heartbeat packets, create buffer directly by new byte[] * since heartbeats should not be blocked. */ private DFSPacket createHeartbeatPacket() throws InterruptedIOException { final byte[] buf = new byte[PacketHeader.PKT_MAX_HEADER_LEN]; return new DFSPacket(buf, 0, 0, DFSPacket.HEART_BEAT_SEQNO, getChecksumSize(), false); }
/** Use {@link ByteArrayManager} to create buffer for non-heartbeat packets.*/ private DFSPacket createPacket(int packetSize, int chunksPerPkt, long offsetInBlock, long seqno, boolean lastPacketInBlock) throws InterruptedIOException { final byte[] buf; final int bufferSize = PacketHeader.PKT_MAX_HEADER_LEN + packetSize; try { buf = byteArrayManager.newByteArray(bufferSize); } catch (InterruptedException ie) { final InterruptedIOException iioe = new InterruptedIOException( "seqno=" + seqno); iioe.initCause(ie); throw iioe; } return new DFSPacket(buf, chunksPerPkt, offsetInBlock, seqno, getChecksumSize(), lastPacketInBlock); }
/** Use {@link ByteArrayManager} to create buffer for non-heartbeat packets.*/ private DFSPacket createPacket(int packetSize, int chunksPerPkt, long offsetInBlock, long seqno, boolean lastPacketInBlock) throws InterruptedIOException { final byte[] buf; final int bufferSize = PacketHeader.PKT_MAX_HEADER_LEN + packetSize; try { buf = byteArrayManager.newByteArray(bufferSize); } catch (InterruptedException ie) { final InterruptedIOException iioe = new InterruptedIOException( "seqno=" + seqno); iioe.initCause(ie); throw iioe; } return new DFSPacket(buf, chunksPerPkt, offsetInBlock, seqno, getChecksumSize(), lastPacketInBlock); }
/** Use {@link ByteArrayManager} to create buffer for non-heartbeat packets.*/ protected DFSPacket createPacket(int packetSize, int chunksPerPkt, long offsetInBlock, long seqno, boolean lastPacketInBlock) throws InterruptedIOException { final byte[] buf; final int bufferSize = PacketHeader.PKT_MAX_HEADER_LEN + packetSize; try { buf = byteArrayManager.newByteArray(bufferSize); } catch (InterruptedException ie) { final InterruptedIOException iioe = new InterruptedIOException( "seqno=" + seqno); iioe.initCause(ie); throw iioe; } return new DFSPacket(buf, chunksPerPkt, offsetInBlock, seqno, getChecksumSize(), lastPacketInBlock); }
@Test public void testPacket() throws Exception { Random r = new Random(12345L); byte[] data = new byte[chunkSize]; r.nextBytes(data); byte[] checksum = new byte[checksumSize]; r.nextBytes(checksum); DataOutputBuffer os = new DataOutputBuffer(data.length * 2); byte[] packetBuf = new byte[data.length * 2]; DFSPacket p = new DFSPacket(packetBuf, maxChunksPerPacket, 0, 0, checksumSize, false); p.setSyncBlock(true); p.writeData(data, 0, data.length); p.writeChecksum(checksum, 0, checksum.length); p.writeTo(os); //we have set syncBlock to true, so the header has the maximum length int headerLen = PacketHeader.PKT_MAX_HEADER_LEN; byte[] readBuf = os.getData(); assertArrayRegionsEqual(readBuf, headerLen, checksum, 0, checksum.length); assertArrayRegionsEqual(readBuf, headerLen + checksum.length, data, 0, data.length); }
@Test public void testPacket() throws Exception { Random r = new Random(12345L); byte[] data = new byte[chunkSize]; r.nextBytes(data); byte[] checksum = new byte[checksumSize]; r.nextBytes(checksum); DataOutputBuffer os = new DataOutputBuffer(data.length * 2); byte[] packetBuf = new byte[data.length * 2]; DFSPacket p = new DFSPacket(packetBuf, maxChunksPerPacket, 0, 0, checksumSize, false); p.setSyncBlock(true); p.writeData(data, 0, data.length); p.writeChecksum(checksum, 0, checksum.length); p.writeTo(os); //we have set syncBlock to true, so the header has the maximum length int headerLen = PacketHeader.PKT_MAX_HEADER_LEN; byte[] readBuf = os.getData(); assertArrayRegionsEqual(readBuf, headerLen, checksum, 0, checksum.length); assertArrayRegionsEqual(readBuf, headerLen + checksum.length, data, 0, data.length); }
@Test public void testAddParentsGetParents() throws Exception { DFSPacket p = new DFSPacket(null, maxChunksPerPacket, 0, 0, checksumSize, false); long parents[] = p.getTraceParents(); Assert.assertEquals(0, parents.length); p.addTraceParent(123); p.addTraceParent(123); parents = p.getTraceParents(); Assert.assertEquals(1, parents.length); Assert.assertEquals(123, parents[0]); parents = p.getTraceParents(); // test calling 'get' again. Assert.assertEquals(1, parents.length); Assert.assertEquals(123, parents[0]); p.addTraceParent(1); p.addTraceParent(456); p.addTraceParent(789); parents = p.getTraceParents(); Assert.assertEquals(4, parents.length); Assert.assertEquals(1, parents[0]); Assert.assertEquals(123, parents[1]); Assert.assertEquals(456, parents[2]); Assert.assertEquals(789, parents[3]); } }
@Test public void testAddParentsGetParents() throws Exception { DFSPacket p = new DFSPacket(null, maxChunksPerPacket, 0, 0, checksumSize, false); SpanId parents[] = p.getTraceParents(); Assert.assertEquals(0, parents.length); p.addTraceParent(new SpanId(0, 123)); p.addTraceParent(new SpanId(0, 123)); parents = p.getTraceParents(); Assert.assertEquals(1, parents.length); Assert.assertEquals(new SpanId(0, 123), parents[0]); parents = p.getTraceParents(); // test calling 'get' again. Assert.assertEquals(1, parents.length); Assert.assertEquals(new SpanId(0, 123), parents[0]); p.addTraceParent(new SpanId(0, 1)); p.addTraceParent(new SpanId(0, 456)); p.addTraceParent(new SpanId(0, 789)); parents = p.getTraceParents(); Assert.assertEquals(4, parents.length); Assert.assertEquals(new SpanId(0, 1), parents[0]); Assert.assertEquals(new SpanId(0, 123), parents[1]); Assert.assertEquals(new SpanId(0, 456), parents[2]); Assert.assertEquals(new SpanId(0, 789), parents[3]); } }