Skip to content

Commit 2a1339e

Browse files
committed
Adjust ClientBuilder and add debugging print statements...
1 parent b0062cc commit 2a1339e

File tree

5 files changed

+82
-39
lines changed

5 files changed

+82
-39
lines changed

src/main/java/com/timgroup/statsd/NonBlockingStatsDClientBuilder.java

Lines changed: 43 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -329,10 +329,27 @@ public static Callable<SocketAddress> volatileAddressResolution(final String hos
329329
if (port == 0) {
330330
return new Callable<SocketAddress>() {
331331
@Override public SocketAddress call() throws UnknownHostException {
332-
return new UnixSocketAddressWithTransport(
333-
new UnixSocketAddress(hostname),
334-
UnixSocketAddressWithTransport.TransportType.UDS
335-
);
332+
if (VersionUtils.hasNativeUdsSupport()) {
333+
try {
334+
Class<?> udsAddressClass = Class.forName("java.net.UnixDomainSocketAddress");
335+
SocketAddress udsAddress = (SocketAddress)
336+
udsAddressClass.getMethod("of", String.class).invoke(null, hostname);
337+
System.out.println("================UnixSocketAddressWithTransport returned with: " + udsAddress);
338+
return new UnixSocketAddressWithTransport(
339+
udsAddress,
340+
UnixSocketAddressWithTransport.TransportType.UDS
341+
);
342+
} catch (Exception e) {
343+
throw new UnknownHostException("Failed to create UnixDomainSocketAddress: " + e.getMessage());
344+
}
345+
} else {
346+
SocketAddress socketAddress = new UnixSocketAddress(hostname);
347+
System.out.println("================UnixSocketAddressWithTransport returned with: " + socketAddress);
348+
return new UnixSocketAddressWithTransport(
349+
socketAddress,
350+
UnixSocketAddressWithTransport.TransportType.UDS
351+
);
352+
}
336353
}
337354
};
338355
} else {
@@ -374,12 +391,29 @@ protected static Callable<SocketAddress> staticNamedPipeResolution(String namedP
374391
protected static Callable<SocketAddress> staticUnixResolution(
375392
final String path,
376393
final UnixSocketAddressWithTransport.TransportType transportType) {
377-
return new Callable<SocketAddress>() {
378-
@Override public SocketAddress call() {
379-
final UnixSocketAddress socketAddress = new UnixSocketAddress(path);
380-
return new UnixSocketAddressWithTransport(socketAddress, transportType);
394+
if (VersionUtils.hasNativeUdsSupport()) {
395+
try {
396+
Class<?> udsAddressClass = Class.forName("java.net.UnixDomainSocketAddress");
397+
final SocketAddress udsAddress = (SocketAddress) udsAddressClass.getMethod("of", String.class).invoke(null, path);
398+
System.out.println("================new Callable<SocketAddress> returned with udsAddress: " + udsAddress);
399+
return new Callable<SocketAddress>() {
400+
@Override public SocketAddress call() {
401+
System.out.println("================UnixSocketAddressWithTransport returned with: " + udsAddress);
402+
return new UnixSocketAddressWithTransport(udsAddress, transportType);
403+
}
404+
};
405+
} catch (Exception e) {
406+
throw new RuntimeException("Failed to create UnixDomainSocketAddress: " + e.getMessage(), e);
381407
}
382-
};
408+
} else {
409+
return new Callable<SocketAddress>() {
410+
@Override public SocketAddress call() {
411+
final UnixSocketAddress socketAddress = new UnixSocketAddress(path);
412+
System.out.println("================UnixSocketAddressWithTransport returned with: " + socketAddress);
413+
return new UnixSocketAddressWithTransport(socketAddress, transportType);
414+
}
415+
};
416+
}
383417
}
384418

385419
private static Callable<SocketAddress> staticAddress(final String hostname, final int port) {

src/main/java/com/timgroup/statsd/UnixDatagramClientChannel.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ class UnixDatagramClientChannel extends DatagramClientChannel {
2222
}
2323

2424
private static DatagramChannel createChannel(SocketAddress address) throws IOException {
25-
if (ClientChannelUtils.hasNativeUdsSupport()) {
25+
if (VersionUtils.hasNativeUdsSupport()) {
2626
return DatagramChannel.open();
2727
} else {
2828
return UnixDatagramChannel.open();
2929
}
3030
}
3131

3232
private void configureChannel(int timeout, int bufferSize) throws IOException {
33-
if (ClientChannelUtils.hasNativeUdsSupport()) {
33+
if (VersionUtils.hasNativeUdsSupport()) {
3434
if (timeout > 0) {
3535
delegate.socket().setSoTimeout(timeout);
3636
}

src/main/java/com/timgroup/statsd/UnixStreamClientChannel.java

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public class UnixStreamClientChannel implements ClientChannel {
3434
this.timeout = timeout;
3535
this.connectionTimeout = connectionTimeout;
3636
this.bufferSize = bufferSize;
37+
System.out.println("================Created UnixStreamClientChannel with address: " + address);
3738
}
3839

3940
@Override
@@ -98,18 +99,16 @@ private void connect() throws IOException {
9899

99100
long deadline = System.nanoTime() + connectionTimeout * 1_000_000L;
100101
// Use native JDK Unix domain socket support for compatible versions (Java 16+). Fall back to JNR support otherwise.
101-
if (ClientChannelUtils.hasNativeUdsSupport()) {
102+
if (VersionUtils.hasNativeUdsSupport()) {
102103
connectJdkSocket(deadline);
103104
} else {
104105
connectJnrSocket(deadline);
105106
}
106107
}
107108

108109
private void connectJdkSocket(long deadline) throws IOException {
109-
String socketPath = address.toString();
110-
110+
String socketPath = address.toString();
111111
try {
112-
// Use reflection to avoid compile-time dependency on Java 16+ classes
113112
Class<?> udsAddressClass = Class.forName("java.net.UnixDomainSocketAddress");
114113
Object udsAddress = udsAddressClass.getMethod("of", String.class).invoke(null, socketPath);
115114

@@ -118,32 +117,34 @@ private void connectJdkSocket(long deadline) throws IOException {
118117
delegate.socket().setSoTimeout(connectionTimeout);
119118
}
120119

121-
try {
122-
delegate.configureBlocking(false);
123-
if (!delegate.connect((SocketAddress) udsAddress)) {
124-
if (connectionTimeout > 0 && System.nanoTime() > deadline) {
125-
throw new IOException("Connection timed out");
126-
}
127-
if (!delegate.finishConnect()) {
128-
throw new IOException("Connection failed");
129-
}
130-
}
131-
delegate.configureBlocking(true);
132-
delegate.socket().setSoTimeout(Math.max(timeout, 0));
133-
if (bufferSize > 0) {
134-
delegate.socket().setSendBufferSize(bufferSize);
120+
delegate.configureBlocking(false);
121+
System.out.println("================Attempting to connect delegate to: " + udsAddress);
122+
if (!delegate.connect((SocketAddress) udsAddress)) {
123+
System.out.println("================Initial connect returned false, checking deadline");
124+
if (connectionTimeout > 0 && System.nanoTime() > deadline) {
125+
throw new IOException("Connection timed out");
135126
}
136-
this.delegate = delegate;
137-
} catch (Exception e) {
138-
try {
139-
delegate.close();
140-
} catch (IOException __) {
141-
// ignore
127+
System.out.println("================Finishing connection");
128+
if (!delegate.finishConnect()) {
129+
throw new IOException("Connection failed");
142130
}
143-
throw new IOException("Failed to connect to Unix Domain Socket: " + socketPath, e);
144131
}
145-
} catch (ReflectiveOperationException e) {
146-
throw new IOException("Failed to create UnixDomainSocketAddress: Java 16+ required", e);
132+
System.out.println("================Connection successful");
133+
delegate.configureBlocking(true);
134+
delegate.socket().setSoTimeout(Math.max(timeout, 0));
135+
if (bufferSize > 0) {
136+
delegate.socket().setSendBufferSize(bufferSize);
137+
}
138+
this.delegate = delegate;
139+
System.out.println("================Set up complete.");
140+
} catch (Exception e) {
141+
System.out.println("================Failed to connect to UDS at: " + socketPath);
142+
try {
143+
delegate.close();
144+
} catch (IOException __) {
145+
// ignore
146+
}
147+
throw new IOException("Failed to connect to Unix Domain Socket: " + socketPath, e);
147148
}
148149
}
149150

@@ -155,6 +156,7 @@ private void connectJnrSocket(long deadline) throws IOException {
155156
delegate.setOption(UnixSocketOptions.SO_SNDTIMEO, connectionTimeout);
156157
}
157158
try {
159+
System.out.println("================Attempting to connect delegate to: " + address);
158160
if (!delegate.connect((UnixSocketAddress) address)) {
159161
if (connectionTimeout > 0 && System.nanoTime() > deadline) {
160162
throw new IOException("Connection timed out");
@@ -163,11 +165,13 @@ private void connectJnrSocket(long deadline) throws IOException {
163165
throw new IOException("Connection failed");
164166
}
165167
}
168+
System.out.println("================Connection successful");
166169
delegate.setOption(UnixSocketOptions.SO_SNDTIMEO, Math.max(timeout, 0));
167170
if (bufferSize > 0) {
168171
delegate.setOption(UnixSocketOptions.SO_SNDBUF, bufferSize);
169172
}
170173
this.delegate = delegate;
174+
System.out.println("================Set up complete.");
171175
} catch (Exception e) {
172176
try {
173177
delegate.close();

src/main/java/com/timgroup/statsd/ClientChannelUtils.java renamed to src/main/java/com/timgroup/statsd/VersionUtils.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
// Logic copied from dd-trace-java Platform class. See:
77
// https://github.com/DataDog/dd-trace-java/blob/master/internal-api/src/main/java/datadog/trace/api/Platform.java
8-
public class ClientChannelUtils {
8+
public class VersionUtils {
99
private static final Version JAVA_VERSION = parseJavaVersion(System.getProperty("java.version"));
1010
private static final int NATIVE_UDS_MIN_VERSION = 16; // Java 16+ has native Unix Domain Socket support
1111

src/test/java/com/timgroup/statsd/UnixStreamSocketDummyStatsDServer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public UnixStreamSocketDummyStatsDServer(String socketPath) throws IOException {
2323
server = UnixServerSocketChannel.open();
2424
server.configureBlocking(true);
2525
server.socket().bind(new UnixSocketAddress(socketPath));
26+
System.out.println("================Server bound to " + socketPath);
2627
this.listen();
2728
}
2829

@@ -39,6 +40,7 @@ protected void receive(ByteBuffer packet) throws IOException {
3940
@Override
4041
protected void listen() {
4142
logger.info("Listening on " + server.getLocalSocketAddress());
43+
System.out.println("================Server listening on " + server.getLocalSocketAddress());
4244
Thread thread = new Thread(new Runnable() {
4345
@Override
4446
public void run() {
@@ -48,7 +50,9 @@ public void run() {
4850
}
4951
try {
5052
logger.info("Waiting for connection");
53+
System.out.println("================Server waiting for connection");
5154
UnixSocketChannel clientChannel = server.accept();
55+
System.out.println("================Server accepted connection");
5256
if (clientChannel != null) {
5357
clientChannel.configureBlocking(true);
5458
try {
@@ -60,6 +64,7 @@ public void run() {
6064
readChannel(clientChannel);
6165
}
6266
} catch (IOException e) {
67+
System.out.println("================Server caught IOException: " + e);
6368
}
6469
}
6570
}

0 commit comments

Comments
 (0)