+ All Categories
Home > Technology > Netty Notes Part 2 - Transports and Buffers

Netty Notes Part 2 - Transports and Buffers

Date post: 25-Jan-2017
Category:
Upload: rick-hightower
View: 1,187 times
Download: 0 times
Share this document with a friend
26
NOTES ON NETTY PART 2 RICK HIGHTOWER’S TRANSPORTS AND BUFFERS
Transcript
Page 1: Netty Notes Part 2 - Transports and Buffers

NOTES ON NETTY PART 2

RICK HIGHTOWER’S

TRANSPORTS AND BUFFERS

Page 2: Netty Notes Part 2 - Transports and Buffers

ABOUT RICK HIGHTOWERABOUT RICK

• Implemented Microservices, Vert.x/Netty at massive scale

• Author of QBit, microservices lib and Boon, Json parser and utility lib

• Founder of Mammatus Technology

• Rick’s Twitter, Rick’s LinkedIn, Rick’s Blog, Rick’s Slideshare

Page 3: Netty Notes Part 2 - Transports and Buffers

Great book on Netty!

https://www.manning.com/books/netty-in-action

Page 4: Netty Notes Part 2 - Transports and Buffers

Great talk about Netty Best Practices given at Facebook,then Twitter University

https://goo.gl/LbXheq

Page 5: Netty Notes Part 2 - Transports and Buffers

http://www.infoq.com/presentations/apple-netty

Great talk about Netty at scale

Page 6: Netty Notes Part 2 - Transports and Buffers

PREVIOUS SLIDE DECKPREVIOUS SLIDE DECK

• Notes on Netty Basics Part 1

• Slideshare

• http://www.slideshare.net/richardhightower/notes-on-netty-baics

• Notes on Netty Basics Part 1

• Google Slides

• https://goo.gl/aUGm2N

Page 7: Netty Notes Part 2 - Transports and Buffers

CHANNELS AND TRANSPORTS

Page 8: Netty Notes Part 2 - Transports and Buffers

NETTY TRANSPORTSTRANSPORTS

• OIO - old school Java networking IO lib pre NIO

• (IMO Deprecated)

• NIO - Uses Java Async API

• Epoll - Uses Linux Network libs via Java Native Interface (JNI)

• Local Transport - Async communication same JVM

• Embedded Transport - for unit testing your channel handlers

• UDS - Uses Unix Domain Sockets for same Server Interprocess communication (IPC)

Page 9: Netty Notes Part 2 - Transports and Buffers

IN THE END IT IS ALL STREAMS OF OCTETSSTREAMS OF BYTES

• Old school blocking API for Java is easy to use but nothing like NIO

• Hard to switch from one to the other

• Netty provides common interface of

• blocking API (Why?),

• Java NIO (works on OS X, Windows, BSD),

• faster Linux epoll NIO,

• UDS, internal JVM, embedded for unit tests

• Same interface easy to switch between implementations

Page 10: Netty Notes Part 2 - Transports and Buffers

FASTER WHEN YOU DON’T HAVE TO COPY BYTESZERO COPY

• Only available with NIO and epoll

• Moves data from file system to network without copying buffers from user space (your program) and kernel space (Linux, OS X, etc.)

• Works great for HTTP, FTP, anytime you can move files

Page 11: Netty Notes Part 2 - Transports and Buffers

HIGHLY SCALABLE IO EVENT NOTIFICATIONEPOLL

• Part of Linux Kernel since 2002 (other OS have similar event systems)

• Faster than select/poll system calls

• Java NIO uses epoll and equiv.

• Netty now has highly tuned Linux epoll implementation

• uses corking,

• send file,

• edge limit,

• much less synchronization than Java NIO

Page 12: Netty Notes Part 2 - Transports and Buffers

WHEN SHOULD YOU USE OIO?OIO

• Probably never

• If doing blocking IO operations (like JDBC) maybe, but…

• You should just use your own thread pools (JDK concurrent) instead or something like Vert.x or QBit

• Vert.x allows pools of workers called worker verticles

• QBit allows pools of service queues

• Both use Netty

Page 13: Netty Notes Part 2 - Transports and Buffers

TRANSPORTSTRANSPORTS

• NIO

• TCP, UDP, SCTP, UDT (Interprocess communication)

• Epoll Linux only

• TCP, UDP

• OIO (IMO deprecated)

• TCP, UDP, SCTP, UDT

• SCTP is stream message oriented

• When to use SCTP over TCP? Who uses SCTP? 4G LTE, telephony

Page 14: Netty Notes Part 2 - Transports and Buffers

NETTY BUFFERS

Page 15: Netty Notes Part 2 - Transports and Buffers

BIG PERFORMANCE GAINS TO BE HADBYTEBUF

• It is all about the bytes

• Performance is reducing byte copies

• Performance is not zero-ing out buffers when not needed

• Performance is reducing the amount of direct buffer creation by pooling buffers

Page 16: Netty Notes Part 2 - Transports and Buffers

BYTEBUF, BYTEBUFHOLDERBYTE BUF API

• Easier to use API than JDK ByteBuffer

• no flip (index for read, index for write)

• Capacity is expanded on demand like ArrayList, StringBuilder

• Fluent API (method-chaining)

• Performance improvements

• Zero copy by using built-in composite buffers

• Pooling is supported

Page 17: Netty Notes Part 2 - Transports and Buffers

BYTEBUF, BYTEBUFHOLDERBYTE BUF API

• read…(), skip…() operation increase read index

• write…() operation increase write index

• get…() operations to not update the indexes

• markReaderIndex(), markWriterIndex(), resetReaderIndex(), resetWriterIndex(), writerIndex(index), readerIndex(index) to move indexes around, reset to 0, etc.

• forEachByte(predicate) to find a location in the buffer.

• If you try to read beyond what is written you get an IndexOutOfBoundException

• You can specify max capacity, defaults to MAX_VALUE on Integer

Page 18: Netty Notes Part 2 - Transports and Buffers

THERE ARE TWO MAIN TYPES OF BYTE BUFFERSTWO TYPES OF BYTE BUFS

• Array backed - backed by a byte[] (byte array)

• Direct buffer

• memory gets allocated off JVM heap

• GOOD: Allows native IO calls that do not have to copy buffers to IO lib

• BAD: More expensive to allocate and release

• You need to copy data into heap to use it, can’t just ask for the array (hasArray() == false, byteBuf.array() throws exception)

Page 19: Netty Notes Part 2 - Transports and Buffers

AGGREGATED VIEW OF MULTIPLE BYTEBUFSCOMPOSITE BUFFERS

• ByteBuf, CompositeByteBuf

• includes N number of on-heap (array backed) and direct byte buffers

• hasArray() only true if only backed by one on heap instance

• Use addComponent(byteBuf) to combine buffers and then no need to copy buffer contents

• Two areas responsible for different parts of the message but the message sent back to client

• e.g., one area creates header response, one area creates response body.

• Instead of copying buffers use CompositeByteBuf (add headers and then add response body buf then send composite)

Page 20: Netty Notes Part 2 - Transports and Buffers

VIEW OR SLICE OF AN EXISTING BUFFERDERIVED BUFFERS

• duplicate(), slice(), slice(start, stop), unmodifiableBuffer(…), order(ByteOrder), readSlice(index)

• Return new buffer view with their own indexes and marker indexes

• Changes also change underlying buffer (use copy(), copy(start, stop) if this is not the behavior that you want)

Page 21: Netty Notes Part 2 - Transports and Buffers

HOLDS A BYTE BUFFER, USED FOR BUFFER REF COUNTINGBYTE BUF HOLDER

• holds a ByteBuf

• content() returns held ByteBuf

• copy() gives a copy of the held ByteBuf

• duplicate() gives a view with unique read/write/mark indexes to the same ByteBuf buffer data as held ByteBuf

Page 22: Netty Notes Part 2 - Transports and Buffers

ALLOCATING BUFFERS FROM POOLSBYTE BUF ALLOCATORS

• ByteBufAllocator (you get it from the channel.alloc())

• Used to allocate a buffer, buffer(…), heapBuffer(…), directBuffer(…), compositeBuffer(…), ioBuffer(…)

• May create a new buffer or use a pooled buffer

• Netty ByteBuf Allocators use Java version of jemalloc

• see talk cited in into area of this slide deck to learn more about jemalloc

• efficient pools of buffers by allowing threads to have their own cache of buffers and then fallback to larger pool if buffer not available in thread local cache

• reduces overhead, fast, Fast, FAST

• First rule of using pools, if you allocated it, you must free it

• buffer.release()

Page 23: Netty Notes Part 2 - Transports and Buffers

THANKS AND BUY NORMAN’S BOOK

Page 24: Netty Notes Part 2 - Transports and Buffers

NETTY IN ACTIONIDEAS FOR SLIDES

• Many ideas for slides are directly derived from Netty in Action book by Norman et al and/or his talks on Netty

• BUY THE BOOK Netty in Action!

• The slides are a study aid for myself so I can better learn Netty

• I’ve worked with ByteBuffer, NIO, Vert.x, and have often wanted to use parts of Netty on projects but lacked the knowledge of Netty internals so used NIO or ByteBuffer or OIO when I really wanted to use Netty instead

Page 25: Netty Notes Part 2 - Transports and Buffers

PREVIOUS SLIDE DECKPREVIOUS SLIDE DECK

• Notes on Netty Basics Part 1

• Slideshare

• http://www.slideshare.net/richardhightower/notes-on-netty-baics

• Notes on Netty Basics Part 1

• Google Slides

• https://goo.gl/aUGm2N

Page 26: Netty Notes Part 2 - Transports and Buffers

ABOUT RICK HIGHTOWERABOUT RICK

• Implemented Microservices, Vert.x/Netty at massive scale

• Author of QBit, microservices lib and Boon, Json parser and utility lib

• Founder of Mammatus Technology

• Rick’s Twitter, Rick’s LinkedIn, Rick’s Blog, Rick’s Slideshare

The end… sleepless dev.


Recommended