Added byteOrder and directBuffer flag to GridNioSslHandler.
Project: http://git-wip-us.apache.org/repos/asf/incubator-ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-ignite/commit/5082d63a Tree: http://git-wip-us.apache.org/repos/asf/incubator-ignite/tree/5082d63a Diff: http://git-wip-us.apache.org/repos/asf/incubator-ignite/diff/5082d63a Branch: refs/heads/ignite-961 Commit: 5082d63a3fbdc1a4ba8da38bb5d10c27a629aebc Parents: ccebd43 Author: nikolay_tikhonov <ntikho...@gridgain.com> Authored: Fri Jul 24 15:18:29 2015 +0300 Committer: nikolay_tikhonov <ntikho...@gridgain.com> Committed: Fri Jul 24 15:18:29 2015 +0300 ---------------------------------------------------------------------- .../GridClientConnectionManagerAdapter.java | 2 +- .../client/router/impl/GridTcpRouterImpl.java | 2 +- .../rest/protocols/tcp/GridTcpRestProtocol.java | 3 +- .../util/nio/ssl/BlockingSslHandler.java | 37 ++++++++++- .../internal/util/nio/ssl/GridNioSslFilter.java | 49 ++++---------- .../util/nio/ssl/GridNioSslHandler.java | 69 ++++++++++++++++++-- .../communication/tcp/TcpCommunicationSpi.java | 3 +- .../internal/util/nio/GridNioSslSelfTest.java | 2 +- 8 files changed, 117 insertions(+), 50 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5082d63a/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientConnectionManagerAdapter.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientConnectionManagerAdapter.java b/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientConnectionManagerAdapter.java index 166eed5..e8d29b4 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientConnectionManagerAdapter.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/client/impl/connection/GridClientConnectionManagerAdapter.java @@ -146,7 +146,7 @@ public abstract class GridClientConnectionManagerAdapter implements GridClientCo GridNioFilter codecFilter = new GridNioCodecFilter(new GridTcpRestParser(routerClient), gridLog, false); if (sslCtx != null) { - GridNioSslFilter sslFilter = new GridNioSslFilter(sslCtx, gridLog); + GridNioSslFilter sslFilter = new GridNioSslFilter(sslCtx, true, ByteOrder.nativeOrder(), gridLog); sslFilter.directMode(false); sslFilter.clientMode(true); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5082d63a/modules/core/src/main/java/org/apache/ignite/internal/client/router/impl/GridTcpRouterImpl.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/client/router/impl/GridTcpRouterImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/client/router/impl/GridTcpRouterImpl.java index cd4c607..7b1cd8b 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/client/router/impl/GridTcpRouterImpl.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/client/router/impl/GridTcpRouterImpl.java @@ -228,7 +228,7 @@ public class GridTcpRouterImpl implements GridTcpRouter, GridTcpRouterMBean, Lif GridNioFilter[] filters; if (sslCtx != null) { - GridNioSslFilter sslFilter = new GridNioSslFilter(sslCtx, log); + GridNioSslFilter sslFilter = new GridNioSslFilter(sslCtx, false, ByteOrder.nativeOrder(), log); sslFilter.wantClientAuth(wantClientAuth); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5082d63a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/GridTcpRestProtocol.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/GridTcpRestProtocol.java b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/GridTcpRestProtocol.java index c37c17d..689e050 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/GridTcpRestProtocol.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/processors/rest/protocols/tcp/GridTcpRestProtocol.java @@ -215,7 +215,8 @@ public class GridTcpRestProtocol extends GridRestProtocolAdapter { GridNioFilter[] filters; if (sslCtx != null) { - GridNioSslFilter sslFilter = new GridNioSslFilter(sslCtx, log); + GridNioSslFilter sslFilter = new GridNioSslFilter(sslCtx, + cfg.isDirectBuffer(), ByteOrder.nativeOrder(), log); sslFilter.directMode(false); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5082d63a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/ssl/BlockingSslHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/ssl/BlockingSslHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/ssl/BlockingSslHandler.java index 9890efe..ee95308 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/ssl/BlockingSslHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/ssl/BlockingSslHandler.java @@ -18,7 +18,6 @@ package org.apache.ignite.internal.util.nio.ssl; import org.apache.ignite.*; -import org.apache.ignite.internal.util.future.*; import org.apache.ignite.internal.util.nio.*; import org.apache.ignite.internal.util.typedef.internal.*; @@ -30,7 +29,6 @@ import java.nio.channels.*; import static javax.net.ssl.SSLEngineResult.HandshakeStatus.*; import static javax.net.ssl.SSLEngineResult.Status.*; -import static org.apache.ignite.internal.util.nio.ssl.GridNioSslFilter.*; /** * @@ -483,4 +481,39 @@ public class BlockingSslHandler { throw new IgniteCheckedException("Failed to write byte to socket.", e); } } + + /** + * Expands the given byte buffer to the requested capacity. + * + * @param original Original byte buffer. + * @param cap Requested capacity. + * @return Expanded byte buffer. + */ + private ByteBuffer expandBuffer(ByteBuffer original, int cap) { + ByteBuffer res = ByteBuffer.allocate(cap); + + res.order(ByteOrder.nativeOrder()); + + original.flip(); + + res.put(original); + + return res; + } + + /** + * Copies the given byte buffer. + * + * @param original Byte buffer to copy. + * @return Copy of the original byte buffer. + */ + private ByteBuffer copy(ByteBuffer original) { + ByteBuffer cp = ByteBuffer.allocate(original.remaining()); + + cp.put(original); + + cp.flip(); + + return cp; + } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5082d63a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/ssl/GridNioSslFilter.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/ssl/GridNioSslFilter.java b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/ssl/GridNioSslFilter.java index 596344b..c3cb084 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/ssl/GridNioSslFilter.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/ssl/GridNioSslFilter.java @@ -52,6 +52,12 @@ public class GridNioSslFilter extends GridNioFilterAdapter { /** SSL context to use. */ private SSLContext sslCtx; + /** Order. */ + private ByteOrder order; + + /** Allocate direct buffer or heap buffer. */ + private boolean directBuf; + /** Whether SSLEngine should use client mode. */ private boolean clientMode; @@ -62,13 +68,17 @@ public class GridNioSslFilter extends GridNioFilterAdapter { * Creates SSL filter. * * @param sslCtx SSL context. + * @param directBuf Direct buffer flag. + * @param order Byte order. * @param log Logger to use. */ - public GridNioSslFilter(SSLContext sslCtx, IgniteLogger log) { + public GridNioSslFilter(SSLContext sslCtx, boolean directBuf, ByteOrder order, IgniteLogger log) { super("SSL filter"); this.log = log; this.sslCtx = sslCtx; + this.directBuf = directBuf; + this.order = order; } /** @@ -151,7 +161,7 @@ public class GridNioSslFilter extends GridNioFilterAdapter { engine.setEnabledProtocols(enabledProtos); try { - GridNioSslHandler hnd = new GridNioSslHandler(this, ses, engine, log); + GridNioSslHandler hnd = new GridNioSslHandler(this, ses, engine, directBuf, order, log); ses.addMeta(SSL_HANDLER.ordinal(), hnd); @@ -384,39 +394,4 @@ public class GridNioSslFilter extends GridNioFilterAdapter { return (ByteBuffer)msg; } - - /** - * Expands the given byte buffer to the requested capacity. - * - * @param original Original byte buffer. - * @param cap Requested capacity. - * @return Expanded byte buffer. - */ - public static ByteBuffer expandBuffer(ByteBuffer original, int cap) { - ByteBuffer res = ByteBuffer.allocate(cap); - - res.order(ByteOrder.nativeOrder()); - - original.flip(); - - res.put(original); - - return res; - } - - /** - * Copies the given byte buffer. - * - * @param original Byte buffer to copy. - * @return Copy of the original byte buffer. - */ - public static ByteBuffer copy(ByteBuffer original) { - ByteBuffer cp = ByteBuffer.allocate(original.remaining()); - - cp.put(original); - - cp.flip(); - - return cp; - } } http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5082d63a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/ssl/GridNioSslHandler.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/ssl/GridNioSslHandler.java b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/ssl/GridNioSslHandler.java index dc3d870..8c1ac5a 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/util/nio/ssl/GridNioSslHandler.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/util/nio/ssl/GridNioSslHandler.java @@ -45,6 +45,12 @@ class GridNioSslHandler extends ReentrantLock { /** SSL engine. */ private SSLEngine sslEngine; + /** Order. */ + private ByteOrder order; + + /** Allocate direct buffer or heap buffer. */ + private boolean directBuf; + /** Session of this handler. */ private GridNioSession ses; @@ -82,10 +88,16 @@ class GridNioSslHandler extends ReentrantLock { * @param ses Session for which this handler was created. * @param engine SSL engine instance for this handler. * @param log Logger to use. + * @param directBuf Direct buffer flag. + * @param order Byte order. * @throws SSLException If exception occurred when starting SSL handshake. */ - GridNioSslHandler(GridNioSslFilter parent, GridNioSession ses, SSLEngine engine, IgniteLogger log) - throws SSLException { + GridNioSslHandler(GridNioSslFilter parent, + GridNioSession ses, + SSLEngine engine, + boolean directBuf, + ByteOrder order, + IgniteLogger log) throws SSLException { assert parent != null; assert ses != null; assert engine != null; @@ -93,6 +105,8 @@ class GridNioSslHandler extends ReentrantLock { this.parent = parent; this.ses = ses; + this.order = order; + this.directBuf = directBuf; this.log = log; sslEngine = engine; @@ -111,8 +125,13 @@ class GridNioSslHandler extends ReentrantLock { // Allocate a little bit more so SSL engine would not return buffer overflow status. int netBufSize = sslEngine.getSession().getPacketBufferSize() + 50; - outNetBuf = ByteBuffer.allocate(netBufSize); - inNetBuf = ByteBuffer.allocate(netBufSize); + outNetBuf = directBuf ? ByteBuffer.allocateDirect(netBufSize) : ByteBuffer.allocate(netBufSize); + + outNetBuf.order(order); + + inNetBuf = directBuf ? ByteBuffer.allocateDirect(netBufSize) : ByteBuffer.allocate(netBufSize); + + inNetBuf.order(order); // Initially buffer is empty. outNetBuf.position(0); @@ -120,9 +139,9 @@ class GridNioSslHandler extends ReentrantLock { int appBufSize = Math.max(sslEngine.getSession().getApplicationBufferSize() + 50, netBufSize * 2); - appBuf = ByteBuffer.allocate(appBufSize); + appBuf = directBuf ? ByteBuffer.allocateDirect(appBufSize) : ByteBuffer.allocate(appBufSize); - appBuf.order(ByteOrder.nativeOrder()); + appBuf.order(order); if (log.isDebugEnabled()) log.debug("Started SSL session [netBufSize=" + netBufSize + ", appBufSize=" + appBufSize + ']'); @@ -588,6 +607,44 @@ class GridNioSslHandler extends ReentrantLock { } /** + * Expands the given byte buffer to the requested capacity. + * + * @param original Original byte buffer. + * @param cap Requested capacity. + * @return Expanded byte buffer. + */ + private ByteBuffer expandBuffer(ByteBuffer original, int cap) { + ByteBuffer res = directBuf ? ByteBuffer.allocateDirect(cap) : ByteBuffer.allocate(cap); + + res.order(order); + + original.flip(); + + res.put(original); + + return res; + } + + /** + * Copies the given byte buffer. + * + * @param original Byte buffer to copy. + * @return Copy of the original byte buffer. + */ + private ByteBuffer copy(ByteBuffer original) { + ByteBuffer cp = directBuf ? ByteBuffer.allocateDirect(original.remaining()) : + ByteBuffer.allocate(original.remaining()); + + cp.order(order); + + cp.put(original); + + cp.flip(); + + return cp; + } + + /** * Write request for cases while handshake is not finished yet. */ private static class WriteRequest { http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5082d63a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java index a665a3a..a0acb5c 100644 --- a/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java +++ b/modules/core/src/main/java/org/apache/ignite/spi/communication/tcp/TcpCommunicationSpi.java @@ -1478,7 +1478,8 @@ public class TcpCommunicationSpi extends IgniteSpiAdapter if (isSslEnabled()) { GridNioSslFilter sslFilter = - new GridNioSslFilter(ignite.configuration().getSslContextFactory().create(), log); + new GridNioSslFilter(ignite.configuration().getSslContextFactory().create(), + true, ByteOrder.nativeOrder(), log); sslFilter.directMode(true); http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/5082d63a/modules/core/src/test/java/org/apache/ignite/internal/util/nio/GridNioSslSelfTest.java ---------------------------------------------------------------------- diff --git a/modules/core/src/test/java/org/apache/ignite/internal/util/nio/GridNioSslSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/util/nio/GridNioSslSelfTest.java index 930b5d1..ba22395 100644 --- a/modules/core/src/test/java/org/apache/ignite/internal/util/nio/GridNioSslSelfTest.java +++ b/modules/core/src/test/java/org/apache/ignite/internal/util/nio/GridNioSslSelfTest.java @@ -69,7 +69,7 @@ public class GridNioSslSelfTest extends GridNioSelfTest { .sendQueueLimit(0) .filters( new GridNioCodecFilter(parser, log, false), - new GridNioSslFilter(sslCtx, log)) + new GridNioSslFilter(sslCtx, true, ByteOrder.nativeOrder(), log)) .build(); srvr.start();