Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #2014 - Unix Socket Client #2025

Merged
merged 49 commits into from
Jan 13, 2018
Merged
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
fd61e8f
WIP add unix domain sockets support in HttpClient
olamy Dec 6, 2017
966640b
move unix socket client part to unix socket module #2014
olamy Dec 6, 2017
322cff6
some cleanup #2014
olamy Dec 6, 2017
9dccecc
add missing headers #2014
olamy Dec 6, 2017
9765b1c
add TODO
olamy Dec 6, 2017
088bcb6
UnixSocket client refactor
gregw Dec 6, 2017
eeb5b6d
cleanup test and pom
olamy Dec 6, 2017
79ab83f
minor changes, use LOG.isDebugEnabled() before using debug method
olamy Dec 6, 2017
994c507
add UNIX SOCKET http client test with all other tests, push this to s…
olamy Dec 7, 2017
b592dd1
fix some unit tests
olamy Dec 7, 2017
fad3952
fix more tests
olamy Dec 7, 2017
ef199d1
fix load test
olamy Dec 7, 2017
c2ac3bb
Merge branch 'feature/unix_socket_client' of https://github.com/olamy…
gregw Dec 7, 2017
af1aa63
UnixSocket client
gregw Dec 7, 2017
6e1983e
Merge branch 'jetty-9.4.x' into jetty-9.4.x-2014-unixsocket-client
gregw Dec 7, 2017
33ebd90
Demonstrate JNR bug
gregw Dec 7, 2017
1921220
Worked around JNR bug 50
gregw Dec 7, 2017
0b8fa17
Merge branch 'jetty-9.4.x' into jetty-9.4.x-2014-unixsocket-client
gregw Dec 25, 2017
f846b87
close channel on client side as well
olamy Dec 8, 2017
52a3b91
more details in log
olamy Dec 8, 2017
6a0ff1c
log file path as well
olamy Dec 8, 2017
8672f0e
#2014 disable test per default as doesn't work on some environement
olamy Dec 28, 2017
44ddc42
Merge pull request #2082 from olamy/jetty-9.4.x-2014-unixsocket-clien…
gregw Dec 28, 2017
27dc3a8
Merge branch 'jetty-9.4.x' into jetty-9.4.x-2014-unixsocket-client
gregw Dec 28, 2017
7530148
Revert "#2014 disable test per default as doesn't work on some enviro…
gregw Dec 28, 2017
d8f7d5c
test only on unix
gregw Dec 28, 2017
5929306
Allow test of specific transport(s)
gregw Dec 28, 2017
a81aec0
Move unix socket to /tmp
gregw Jan 2, 2018
eb592f7
move test socket to /tmp
gregw Jan 2, 2018
6ad955f
move test socket to /tmp
gregw Jan 2, 2018
fdadc8b
Merge branch 'jetty-9.4.x' into jetty-9.4.x-2014-unixsocket-client
gregw Jan 8, 2018
2f5cb23
ignore failing tests for now
gregw Jan 8, 2018
26e7e83
fix bean name and possible to use sys prop org.eclipse.jetty.http.cli…
olamy Jan 7, 2018
8169b71
test isBlank as surefire props is not null
olamy Jan 7, 2018
7b0217c
correctly create tmp file with @Before
olamy Jan 7, 2018
c20b32d
do not delete file
olamy Jan 8, 2018
01966c3
use /tmp as build directory doesn't seem to work within docker...
olamy Jan 8, 2018
5826fc6
do not delete sock file on client as it is own by the server
olamy Jan 8, 2018
fec0955
file must not exist when binding unix socket
olamy Jan 8, 2018
de43987
Merge pull request #2106 from olamy/jetty-9.4.x-2014-unixsocket-clien…
gregw Jan 9, 2018
6a3b4c3
Merge branch 'jetty-9.4.x' into jetty-9.4.x-2014-unixsocket-client
gregw Jan 10, 2018
5ad783f
#2014 fix license header
olamy Jan 10, 2018
e77a064
Merge pull request #2112 from olamy/jetty-9.4.x-2014-unixsocket-clien…
joakime Jan 10, 2018
3dfaf7b
Merge branch 'jetty-9.4.x' into jetty-9.4.x-2014-unixsocket-client
gregw Jan 11, 2018
f5020fe
network specific tests assumed
gregw Jan 11, 2018
503f2da
Fixed to handle null selector keys
gregw Jan 11, 2018
d606a6f
Merge branch 'jetty-9.4.x' into jetty-9.4.x-2014-unixsocket-client
gregw Jan 11, 2018
287e8fa
Merge branch 'jetty-9.4.x' into jetty-9.4.x-2014-unixsocket-client
gregw Jan 11, 2018
6fdfbdf
add assume for tests that assume a network connector
gregw Jan 12, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion jetty-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@
<version>${project.version}</version>
<optional>true</optional>
</dependency>

<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -753,7 +753,7 @@ public void run()
List<EndPoint> end_points = new ArrayList<>();
for (SelectionKey key : _selector.keys())
{
if (key.isValid())
if (key!=null && key.isValid())
{
Object attachment = key.attachment();
if (attachment instanceof EndPoint)
Expand Down
7 changes: 6 additions & 1 deletion jetty-unixsocket/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,12 @@
<dependency>
<groupId>com.github.jnr</groupId>
<artifactId>jnr-unixsocket</artifactId>
<version>0.18</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-client</artifactId>
<version>${project.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.eclipse.jetty.toolchain</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,8 @@ public void open() throws IOException
serverChannel.configureBlocking(getAcceptors()>0);
serverChannel.socket().bind(bindAddress, getAcceptQueueSize());
addBean(serverChannel);

LOG.debug("opened {}",serverChannel);
if (LOG.isDebugEnabled())
LOG.debug("opened {}",serverChannel);
_acceptChannel = serverChannel;
}
}
Expand Down Expand Up @@ -430,9 +430,11 @@ protected boolean isConnectionPending(SelectableChannel channel)
@Override
protected SelectableChannel doAccept(SelectableChannel server) throws IOException
{
LOG.debug("doAccept async {}",server);
if (LOG.isDebugEnabled())
LOG.debug("doAccept async {}",server);
UnixSocketChannel channel = ((UnixServerSocketChannel)server).accept();
LOG.debug("accepted async {}",channel);
if (LOG.isDebugEnabled())
LOG.debug("accepted async {}",channel);
return channel;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;

import org.eclipse.jetty.io.ChannelEndPoint;
import org.eclipse.jetty.io.EofException;
import org.eclipse.jetty.io.ManagedSelector;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;
import org.eclipse.jetty.util.thread.Scheduler;
Expand All @@ -32,8 +35,9 @@

public class UnixSocketEndPoint extends ChannelEndPoint
{
public final static InetSocketAddress NOIP=new InetSocketAddress(0);
private static final Logger LOG = Log.getLogger(UnixSocketEndPoint.class);
private static final Logger CEPLOG = Log.getLogger(ChannelEndPoint.class);


private final UnixSocketChannel _channel;

Expand Down Expand Up @@ -71,4 +75,50 @@ protected void doShutdownOutput()
LOG.debug(e);
}
}


@Override
public boolean flush(ByteBuffer... buffers) throws IOException
{
// TODO this is a work around for https://github.com/jnr/jnr-unixsocket/issues/50
long flushed=0;
try
{
for (ByteBuffer b : buffers)
{
if (b.hasRemaining())
{
int r=b.remaining();
int p=b.position();
int l=_channel.write(b);
if (l>=0)
{
b.position(p+l);
flushed+=l;
}

if (CEPLOG.isDebugEnabled())
CEPLOG.debug("flushed {}/{} r={} {}", l,r,b.remaining(), this);

if (b.hasRemaining())
break;
}
}

}
catch (IOException e)
{
throw new EofException(e);
}

if (flushed>0)
notIdle();

for (ByteBuffer b : buffers)
if (!BufferUtil.isEmpty(b))
return false;

return true;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
//
// ========================================================================
// Copyright (c) 1995-2017 Mort Bay Consulting Pty. Ltd.
// ------------------------------------------------------------------------
// All rights reserved. This program and the accompanying materials
// are made available under the terms of the Eclipse Public License v1.0
// and Apache License v2.0 which accompanies this distribution.
//
// The Eclipse Public License is available at
// http://www.eclipse.org/legal/epl-v10.html
//
// The Apache License v2.0 is available at
// http://www.opensource.org/licenses/apache2.0.php
//
// You may elect to redistribute this code under either of these licenses.
// ========================================================================
//

package org.eclipse.jetty.unixsocket.client;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.util.Map;

import org.eclipse.jetty.client.HttpClient;
import org.eclipse.jetty.client.HttpDestination;
import org.eclipse.jetty.client.http.HttpClientTransportOverHTTP;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.ManagedSelector;
import org.eclipse.jetty.io.SelectorManager;
import org.eclipse.jetty.io.ssl.SslClientConnectionFactory;
import org.eclipse.jetty.unixsocket.UnixSocketEndPoint;
import org.eclipse.jetty.util.log.Log;
import org.eclipse.jetty.util.log.Logger;

import jnr.enxio.channels.NativeSelectorProvider;
import jnr.unixsocket.UnixSocketAddress;
import jnr.unixsocket.UnixSocketChannel;

public class HttpClientTransportOverUnixSockets
extends HttpClientTransportOverHTTP
{
private static final Logger LOG = Log.getLogger( HttpClientTransportOverUnixSockets.class );

private String _unixSocket;
private SelectorManager selectorManager;


public HttpClientTransportOverUnixSockets( String unixSocket )
{
if ( unixSocket == null )
{
throw new IllegalArgumentException( "Unix socket file cannot be null" );
}
this._unixSocket = unixSocket;
}

@Override
protected SelectorManager newSelectorManager(HttpClient client)
{
return selectorManager = new UnixSocketSelectorManager(client,getSelectors());
}

@Override
public void connect( InetSocketAddress address, Map<String, Object> context )
{
UnixSocketChannel channel = null;
try
{
InetAddress inet = address.getAddress();
if (!inet.isLoopbackAddress() && !inet.isLinkLocalAddress() && !inet.isSiteLocalAddress())
throw new IOException("UnixSocket cannot connect to "+address.getHostString());

// Open a unix socket
UnixSocketAddress unixAddress = new UnixSocketAddress( this._unixSocket );
channel = UnixSocketChannel.open( unixAddress );

HttpDestination destination = (HttpDestination)context.get(HTTP_DESTINATION_CONTEXT_KEY);
HttpClient client = destination.getHttpClient();

configure(client, channel);

channel.configureBlocking(false);
selectorManager.accept(channel, context);
}
// Must catch all exceptions, since some like
// UnresolvedAddressException are not IOExceptions.
catch (Throwable x)
{
// If IPv6 is not deployed, a generic SocketException "Network is unreachable"
// exception is being thrown, so we attempt to provide a better error message.
if (x.getClass() == SocketException.class)
x = new SocketException("Could not connect to " + address).initCause(x);

try
{
if (channel != null)
channel.close();
}
catch (IOException xx)
{
LOG.ignore(xx);
}
finally
{
connectFailed(context, x);
}
}
}

public class UnixSocketSelectorManager extends ClientSelectorManager
{
protected UnixSocketSelectorManager(HttpClient client, int selectors)
{
super(client,selectors);
}


@Override
protected Selector newSelector() throws IOException
{
return NativeSelectorProvider.getInstance().openSelector();
}

@Override
protected EndPoint newEndPoint(SelectableChannel channel, ManagedSelector selector, SelectionKey key)
{
UnixSocketEndPoint endp = new UnixSocketEndPoint((UnixSocketChannel)channel, selector, key, getScheduler());
endp.setIdleTimeout(getHttpClient().getIdleTimeout());
return endp;
}



}
}
Loading