Skip to content

Commit 96c4f53

Browse files
Marcus Linkemarcuslinke
authored andcommitted
Experiment with jnr-unixsocket and netty
wip wip wip wip wip wip wip wip
1 parent 74d506d commit 96c4f53

File tree

8 files changed

+671
-47
lines changed

8 files changed

+671
-47
lines changed

pom.xml

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
23
<modelVersion>4.0.0</modelVersion>
34

45
<parent>
@@ -252,6 +253,16 @@
252253
<version>4.12</version>
253254
<scope>test</scope>
254255
</dependency>
256+
<dependency>
257+
<groupId>com.github.jnr</groupId>
258+
<artifactId>jnr-unixsocket</artifactId>
259+
<version>0.12</version>
260+
</dependency>
261+
<dependency>
262+
<groupId>me.lessis</groupId>
263+
<artifactId>unisockets-core_2.11</artifactId>
264+
<version>0.1.0</version>
265+
</dependency>
255266
</dependencies>
256267

257268
<distributionManagement>
@@ -266,13 +277,13 @@
266277
</distributionManagement>
267278

268279
<build>
269-
<!-- <extensions> -->
270-
<!-- <extension> -->
271-
<!-- <groupId>kr.motd.maven</groupId> -->
272-
<!-- <artifactId>os-maven-plugin</artifactId> -->
273-
<!-- <version>1.2.3.Final</version> -->
274-
<!-- </extension> -->
275-
<!-- </extensions> -->
280+
<!-- <extensions> -->
281+
<!-- <extension> -->
282+
<!-- <groupId>kr.motd.maven</groupId> -->
283+
<!-- <artifactId>os-maven-plugin</artifactId> -->
284+
<!-- <version>1.2.3.Final</version> -->
285+
<!-- </extension> -->
286+
<!-- </extensions> -->
276287
<pluginManagement>
277288
<plugins>
278289

@@ -421,7 +432,7 @@
421432
<groups>integration</groups>
422433
<excludedGroups>integration-auth</excludedGroups>
423434
<includes>
424-
<include>**/*Test.java</include>
435+
<include>**/*ExecTest.java</include>
425436
</includes>
426437
</configuration>
427438
</execution>
@@ -483,7 +494,8 @@
483494
<failOnViolation>true</failOnViolation>
484495
<logViolationsToConsole>true</logViolationsToConsole>
485496
<linkXRef>false</linkXRef>
486-
<!-- if some IDE has integration and requires other place, propose it -->
497+
<!-- if some IDE has integration and requires other place, propose
498+
it -->
487499
<configLocation>
488500
src/test/resources/checkstyle/checkstyle-config.xml
489501
</configLocation>

src/main/java/com/github/dockerjava/netty/InvocationBuilder.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,7 @@ public void run() {
322322
}
323323

324324
// we close the writing side of the socket, but keep the read side open to transfer stdout/stderr
325+
System.out.println("shutdownOutput");
325326
channel.shutdownOutput();
326327

327328
}

src/main/java/com/github/dockerjava/netty/NettyDockerCmdExecFactory.java

Lines changed: 54 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,19 @@
11
package com.github.dockerjava.netty;
22

3+
import static com.google.common.base.Preconditions.checkNotNull;
4+
5+
import java.io.IOException;
6+
import java.net.InetAddress;
7+
import java.net.InetSocketAddress;
8+
import java.net.SocketAddress;
9+
import java.nio.channels.spi.SelectorProvider;
10+
import java.security.Security;
11+
12+
import javax.net.ssl.SSLEngine;
13+
import javax.net.ssl.SSLParameters;
14+
15+
import org.bouncycastle.jce.provider.BouncyCastleProvider;
16+
317
import com.github.dockerjava.api.command.AttachContainerCmd;
418
import com.github.dockerjava.api.command.AuthCmd;
519
import com.github.dockerjava.api.command.BuildImageCmd;
@@ -39,6 +53,7 @@
3953
import com.github.dockerjava.api.command.RemoveImageCmd;
4054
import com.github.dockerjava.api.command.RemoveNetworkCmd;
4155
import com.github.dockerjava.api.command.RemoveVolumeCmd;
56+
import com.github.dockerjava.api.command.RenameContainerCmd;
4257
import com.github.dockerjava.api.command.RestartContainerCmd;
4358
import com.github.dockerjava.api.command.SaveImageCmd;
4459
import com.github.dockerjava.api.command.SearchImagesCmd;
@@ -51,7 +66,6 @@
5166
import com.github.dockerjava.api.command.UpdateContainerCmd;
5267
import com.github.dockerjava.api.command.VersionCmd;
5368
import com.github.dockerjava.api.command.WaitContainerCmd;
54-
import com.github.dockerjava.api.command.RenameContainerCmd;
5569
import com.github.dockerjava.core.DockerClientConfig;
5670
import com.github.dockerjava.core.DockerClientImpl;
5771
import com.github.dockerjava.core.SSLConfig;
@@ -93,6 +107,7 @@
93107
import com.github.dockerjava.netty.exec.RemoveImageCmdExec;
94108
import com.github.dockerjava.netty.exec.RemoveNetworkCmdExec;
95109
import com.github.dockerjava.netty.exec.RemoveVolumeCmdExec;
110+
import com.github.dockerjava.netty.exec.RenameContainerCmdExec;
96111
import com.github.dockerjava.netty.exec.RestartContainerCmdExec;
97112
import com.github.dockerjava.netty.exec.SaveImageCmdExec;
98113
import com.github.dockerjava.netty.exec.SearchImagesCmdExec;
@@ -105,36 +120,22 @@
105120
import com.github.dockerjava.netty.exec.UpdateContainerCmdExec;
106121
import com.github.dockerjava.netty.exec.VersionCmdExec;
107122
import com.github.dockerjava.netty.exec.WaitContainerCmdExec;
108-
import com.github.dockerjava.netty.exec.RenameContainerCmdExec;
109123

110124
import io.netty.bootstrap.Bootstrap;
125+
import io.netty.channel.ChannelFactory;
111126
import io.netty.channel.ChannelInitializer;
112127
import io.netty.channel.EventLoopGroup;
113-
import io.netty.channel.epoll.EpollDomainSocketChannel;
114-
import io.netty.channel.epoll.EpollEventLoopGroup;
115128
import io.netty.channel.nio.NioEventLoopGroup;
116129
import io.netty.channel.socket.DuplexChannel;
117130
import io.netty.channel.socket.SocketChannel;
118131
import io.netty.channel.socket.nio.NioSocketChannel;
119-
import io.netty.channel.unix.DomainSocketAddress;
120-
import io.netty.channel.unix.UnixChannel;
121132
import io.netty.handler.codec.http.HttpClientCodec;
122133
import io.netty.handler.logging.LoggingHandler;
123134
import io.netty.handler.ssl.SslHandler;
124135
import io.netty.util.concurrent.DefaultThreadFactory;
125-
126-
import org.bouncycastle.jce.provider.BouncyCastleProvider;
127-
128-
import javax.net.ssl.SSLEngine;
129-
import javax.net.ssl.SSLParameters;
130-
131-
import java.io.IOException;
132-
import java.net.InetAddress;
133-
import java.net.InetSocketAddress;
134-
import java.net.SocketAddress;
135-
import java.security.Security;
136-
137-
import static com.google.common.base.Preconditions.checkNotNull;
136+
import jnr.enxio.channels.NativeSelectorProvider;
137+
import jnr.unixsocket.UnixSocketAddress;
138+
import jnr.unixsocket.UnixSocketChannel;
138139

139140
/**
140141
* Experimental implementation of {@link DockerCmdExecFactory} that supports http connection hijacking that is needed to pass STDIN to the
@@ -215,22 +216,50 @@ private interface NettyInitializer {
215216
}
216217

217218
private class UnixDomainSocketInitializer implements NettyInitializer {
219+
220+
final java.io.File path = new java.io.File("/var/run/docker.sock");
221+
218222
@Override
219223
public EventLoopGroup init(Bootstrap bootstrap, DockerClientConfig dockerClientConfig) {
220-
EventLoopGroup epollEventLoopGroup = new EpollEventLoopGroup(0, new DefaultThreadFactory(threadPrefix));
221-
bootstrap.group(epollEventLoopGroup).channel(EpollDomainSocketChannel.class)
222-
.handler(new ChannelInitializer<UnixChannel>() {
224+
final SelectorProvider nativeSelectorProvider = NativeSelectorProvider.getInstance();
225+
226+
EventLoopGroup nioEventLoopGroup = new NioEventLoopGroup(0,
227+
new DefaultThreadFactory(threadPrefix), nativeSelectorProvider);
228+
229+
ChannelFactory<NioSocketChannel> factory = new ChannelFactory<NioSocketChannel>() {
230+
231+
@Override
232+
public NioSocketChannel newChannel() {
233+
try {
234+
return new NioSocketChannel(UnixSocketChannel.create());
235+
} catch (IOException e) {
236+
throw new RuntimeException();
237+
}
238+
}
239+
};
240+
241+
bootstrap.group(nioEventLoopGroup).channelFactory(factory)
242+
.handler(new ChannelInitializer<SocketChannel>() {
223243
@Override
224-
protected void initChannel(final UnixChannel channel) throws Exception {
244+
protected void initChannel(final SocketChannel channel) throws Exception {
245+
channel.pipeline().addLast(new LoggingHandler(getClass()));
225246
channel.pipeline().addLast(new HttpClientCodec());
226247
}
227248
});
228-
return epollEventLoopGroup;
249+
250+
return nioEventLoopGroup;
229251
}
230252

231253
@Override
232254
public DuplexChannel connect(Bootstrap bootstrap) throws InterruptedException {
233-
return (DuplexChannel) bootstrap.connect(new DomainSocketAddress("/var/run/docker.sock")).sync().channel();
255+
256+
if (!path.exists()) {
257+
throw new RuntimeException("socket not found: " + path);
258+
}
259+
260+
UnixSocketAddress address = new UnixSocketAddress(path);
261+
262+
return (DuplexChannel) bootstrap.connect(address).sync().channel();
234263
}
235264
}
236265

0 commit comments

Comments
 (0)