Skip to content

Commit 15d498b

Browse files
authored
Revert "Introduce bolt+unix scheme support (#1586)" (#1589)
This reverts commit b4f74cc.
1 parent b4f74cc commit 15d498b

File tree

11 files changed

+155
-168
lines changed

11 files changed

+155
-168
lines changed

driver/src/main/java/org/neo4j/driver/internal/DriverFactory.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
import io.netty.channel.local.LocalAddress;
2525
import io.netty.util.concurrent.EventExecutorGroup;
2626
import java.net.URI;
27-
import java.nio.file.Files;
28-
import java.nio.file.Path;
2927
import java.time.Clock;
3028
import java.util.LinkedHashSet;
3129
import java.util.Set;
@@ -113,6 +111,7 @@ public final Driver newInstance(
113111
ownsEventLoopGroup = false;
114112
}
115113

114+
var address = new InternalServerAddress(uri);
116115
var routingSettings = new RoutingSettings(config.routingTablePurgeDelayMillis(), new RoutingContext(uri));
117116

118117
EventExecutorGroup eventExecutorGroup = bootstrap.config().group();
@@ -123,6 +122,7 @@ public final Driver newInstance(
123122
return createDriver(
124123
uri,
125124
securityPlanManager,
125+
address,
126126
bootstrap.group(),
127127
routingSettings,
128128
retryLogic,
@@ -149,6 +149,7 @@ protected static MetricsProvider getOrCreateMetricsProvider(Config config, Clock
149149
private InternalDriver createDriver(
150150
URI uri,
151151
BoltSecurityPlanManager securityPlanManager,
152+
ServerAddress address,
152153
EventLoopGroup eventLoopGroup,
153154
RoutingSettings routingSettings,
154155
RetryLogic retryLogic,
@@ -158,29 +159,11 @@ private InternalDriver createDriver(
158159
boolean ownsEventLoopGroup,
159160
Supplier<Rediscovery> rediscoverySupplier) {
160161
BoltConnectionProvider boltConnectionProvider = null;
161-
BoltServerAddress address;
162-
if (Scheme.BOLT_UNIX_URI_SCHEME.equals(uri.getScheme())) {
163-
var path = Path.of(uri.getPath());
164-
if (!Files.exists(path)) {
165-
throw new IllegalArgumentException(String.format("%s does not exist", path));
166-
}
167-
address = new BoltServerAddress(path);
168-
} else {
169-
var port = uri.getPort();
170-
if (port == -1) {
171-
port = InternalServerAddress.DEFAULT_PORT;
172-
}
173-
if (port >= 0 && port <= 65_535) {
174-
address = new BoltServerAddress(uri.getHost(), port);
175-
} else {
176-
throw new IllegalArgumentException("Illegal port: " + port);
177-
}
178-
}
179162
try {
180163
boltConnectionProvider =
181164
createBoltConnectionProvider(uri, config, eventLoopGroup, routingSettings, rediscoverySupplier);
182165
boltConnectionProvider.init(
183-
address,
166+
new BoltServerAddress(address.host(), address.port()),
184167
new RoutingContext(uri),
185168
DriverInfoUtil.boltAgent(),
186169
config.userAgent(),

driver/src/main/java/org/neo4j/driver/internal/InternalServerAddress.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ private static void requireValidPort(int port) {
3535
throw new IllegalArgumentException("Illegal port: " + port);
3636
}
3737

38+
public InternalServerAddress(String address) {
39+
this(uriFrom(address));
40+
}
41+
3842
public InternalServerAddress(URI uri) {
3943
this(hostFrom(uri), portFrom(uri));
4044
}
@@ -60,6 +64,43 @@ private static RuntimeException invalidAddressFormat(String address) {
6064
return new IllegalArgumentException("Invalid address format `" + address + "`");
6165
}
6266

67+
@SuppressWarnings("DuplicatedCode")
68+
private static URI uriFrom(String address) {
69+
String scheme;
70+
String hostPort;
71+
72+
var schemeSplit = address.split("://");
73+
if (schemeSplit.length == 1) {
74+
// URI can't parse addresses without scheme, prepend fake "bolt://" to reuse the parsing facility
75+
scheme = "bolt://";
76+
hostPort = hostPortFrom(schemeSplit[0]);
77+
} else if (schemeSplit.length == 2) {
78+
scheme = schemeSplit[0] + "://";
79+
hostPort = hostPortFrom(schemeSplit[1]);
80+
} else {
81+
throw invalidAddressFormat(address);
82+
}
83+
84+
return URI.create(scheme + hostPort);
85+
}
86+
87+
private static String hostPortFrom(String address) {
88+
if (address.startsWith("[")) {
89+
// expected to be an IPv6 address like [::1] or [::1]:7687
90+
return address;
91+
}
92+
93+
var containsSingleColon = address.indexOf(":") == address.lastIndexOf(":");
94+
if (containsSingleColon) {
95+
// expected to be an IPv4 address with or without port like 127.0.0.1 or 127.0.0.1:7687
96+
return address;
97+
}
98+
99+
// address contains multiple colons and does not start with '['
100+
// expected to be an IPv6 address without brackets
101+
return "[" + address + "]";
102+
}
103+
63104
@Override
64105
public String toString() {
65106
return String.format("%s:%d", host, port);

driver/src/main/java/org/neo4j/driver/internal/Scheme.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public class Scheme {
2222
public static final String BOLT_URI_SCHEME = "bolt";
2323
public static final String BOLT_HIGH_TRUST_URI_SCHEME = "bolt+s";
2424
public static final String BOLT_LOW_TRUST_URI_SCHEME = "bolt+ssc";
25-
public static final String BOLT_UNIX_URI_SCHEME = "bolt+unix";
2625
public static final String NEO4J_URI_SCHEME = "neo4j";
2726
public static final String NEO4J_HIGH_TRUST_URI_SCHEME = "neo4j+s";
2827
public static final String NEO4J_LOW_TRUST_URI_SCHEME = "neo4j+ssc";
@@ -35,7 +34,6 @@ public static void validateScheme(String scheme) {
3534
case BOLT_URI_SCHEME,
3635
BOLT_LOW_TRUST_URI_SCHEME,
3736
BOLT_HIGH_TRUST_URI_SCHEME,
38-
BOLT_UNIX_URI_SCHEME,
3937
NEO4J_URI_SCHEME,
4038
NEO4J_LOW_TRUST_URI_SCHEME,
4139
NEO4J_HIGH_TRUST_URI_SCHEME -> {}

driver/src/main/java/org/neo4j/driver/internal/bolt/api/BoltServerAddress.java

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import static java.util.Objects.requireNonNull;
2020

2121
import java.net.URI;
22-
import java.nio.file.Path;
2322
import java.util.Objects;
2423
import java.util.stream.Stream;
2524

@@ -36,7 +35,6 @@ public class BoltServerAddress {
3635
// resolved IP address.
3736
protected final int port;
3837
private final String stringValue;
39-
private final Path path;
4038

4139
public BoltServerAddress(String address) {
4240
this(uriFrom(address));
@@ -57,15 +55,6 @@ public BoltServerAddress(String host, String connectionHost, int port) {
5755
this.stringValue = host.equals(connectionHost)
5856
? String.format("%s:%d", host, port)
5957
: String.format("%s(%s):%d", host, connectionHost, port);
60-
this.path = null;
61-
}
62-
63-
public BoltServerAddress(Path path) {
64-
this.host = path.toString();
65-
this.connectionHost = this.host;
66-
this.port = -1;
67-
this.stringValue = this.host;
68-
this.path = path;
6958
}
7059

7160
@Override
@@ -102,10 +91,6 @@ public String connectionHost() {
10291
return connectionHost;
10392
}
10493

105-
public Path path() {
106-
return path;
107-
}
108-
10994
/**
11095
* Create a stream of unicast addresses.
11196
* <p>
@@ -130,6 +115,7 @@ private static int portFrom(URI uri) {
130115
return port == -1 ? DEFAULT_PORT : port;
131116
}
132117

118+
@SuppressWarnings("DuplicatedCode")
133119
private static URI uriFrom(String address) {
134120
String scheme;
135121
String hostPort;
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [https://neo4j.com]
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.neo4j.driver.internal.bolt.basicimpl;
18+
19+
import java.util.Map;
20+
import java.util.concurrent.CompletableFuture;
21+
import java.util.concurrent.CompletionStage;
22+
import org.neo4j.driver.Value;
23+
import org.neo4j.driver.internal.bolt.api.AccessMode;
24+
import org.neo4j.driver.internal.bolt.api.BoltAgent;
25+
import org.neo4j.driver.internal.bolt.api.BoltServerAddress;
26+
import org.neo4j.driver.internal.bolt.api.MetricsListener;
27+
import org.neo4j.driver.internal.bolt.api.NotificationConfig;
28+
import org.neo4j.driver.internal.bolt.api.RoutingContext;
29+
import org.neo4j.driver.internal.bolt.api.SecurityPlan;
30+
import org.neo4j.driver.internal.bolt.basicimpl.spi.Connection;
31+
32+
public interface ConnectionProvider {
33+
34+
CompletionStage<Connection> acquireConnection(
35+
BoltServerAddress address,
36+
SecurityPlan securityPlan,
37+
RoutingContext routingContext,
38+
String databaseName,
39+
Map<String, Value> authMap,
40+
BoltAgent boltAgent,
41+
String userAgent,
42+
AccessMode mode,
43+
int connectTimeoutMillis,
44+
String impersonatedUser,
45+
CompletableFuture<Long> latestAuthMillisFuture,
46+
NotificationConfig notificationConfig,
47+
MetricsListener metricsListener);
48+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) "Neo4j"
3+
* Neo4j Sweden AB [https://neo4j.com]
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.neo4j.driver.internal.bolt.basicimpl;
18+
19+
import io.netty.channel.EventLoopGroup;
20+
import io.netty.channel.local.LocalAddress;
21+
import java.time.Clock;
22+
import org.neo4j.driver.internal.bolt.api.DomainNameResolver;
23+
import org.neo4j.driver.internal.bolt.api.LoggingProvider;
24+
25+
public class ConnectionProviders {
26+
static ConnectionProvider netty(
27+
EventLoopGroup group,
28+
Clock clock,
29+
DomainNameResolver domainNameResolver,
30+
LocalAddress localAddress,
31+
LoggingProvider logging) {
32+
return new NettyConnectionProvider(group, clock, domainNameResolver, localAddress, logging);
33+
}
34+
}

driver/src/main/java/org/neo4j/driver/internal/bolt/basicimpl/NettyBoltConnectionProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public final class NettyBoltConnectionProvider implements BoltConnectionProvider
5353
private final LoggingProvider logging;
5454
private final System.Logger log;
5555

56-
private final NettyConnectionProvider connectionProvider;
56+
private final ConnectionProvider connectionProvider;
5757

5858
private BoltServerAddress address;
5959

@@ -76,7 +76,7 @@ public NettyBoltConnectionProvider(
7676
this.logging = Objects.requireNonNull(logging);
7777
this.log = logging.getLog(getClass());
7878
this.connectionProvider =
79-
new NettyConnectionProvider(eventLoopGroup, clock, domainNameResolver, localAddress, logging);
79+
ConnectionProviders.netty(eventLoopGroup, clock, domainNameResolver, localAddress, logging);
8080
}
8181

8282
@Override

driver/src/main/java/org/neo4j/driver/internal/bolt/basicimpl/NettyConnectionProvider.java

Lines changed: 22 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,10 @@
2525
import io.netty.channel.EventLoopGroup;
2626
import io.netty.channel.local.LocalAddress;
2727
import io.netty.channel.local.LocalChannel;
28-
import io.netty.channel.socket.nio.NioDomainSocketChannel;
2928
import io.netty.channel.socket.nio.NioSocketChannel;
3029
import io.netty.resolver.AddressResolverGroup;
3130
import java.net.InetSocketAddress;
3231
import java.net.SocketAddress;
33-
import java.net.UnixDomainSocketAddress;
3432
import java.time.Clock;
3533
import java.util.Map;
3634
import java.util.concurrent.CompletableFuture;
@@ -54,7 +52,7 @@
5452
import org.neo4j.driver.internal.bolt.basicimpl.messaging.BoltProtocol;
5553
import org.neo4j.driver.internal.bolt.basicimpl.spi.Connection;
5654

57-
public final class NettyConnectionProvider {
55+
public final class NettyConnectionProvider implements ConnectionProvider {
5856
private final EventLoopGroup eventLoopGroup;
5957
private final Clock clock;
6058
private final DomainNameResolver domainNameResolver;
@@ -77,6 +75,7 @@ public NettyConnectionProvider(
7775
this.logging = logging;
7876
}
7977

78+
@Override
8079
public CompletionStage<Connection> acquireConnection(
8180
BoltServerAddress address,
8281
SecurityPlan securityPlan,
@@ -91,9 +90,27 @@ public CompletionStage<Connection> acquireConnection(
9190
CompletableFuture<Long> latestAuthMillisFuture,
9291
NotificationConfig notificationConfig,
9392
MetricsListener metricsListener) {
93+
var bootstrap = new Bootstrap();
94+
bootstrap
95+
.group(this.eventLoopGroup)
96+
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeoutMillis)
97+
.channel(localAddress != null ? LocalChannel.class : NioSocketChannel.class)
98+
.resolver(addressResolverGroup)
99+
.handler(new NettyChannelInitializer(address, securityPlan, connectTimeoutMillis, clock, logging));
100+
101+
SocketAddress socketAddress;
102+
if (localAddress == null) {
103+
try {
104+
socketAddress =
105+
new InetSocketAddress(domainNameResolver.resolve(address.connectionHost())[0], address.port());
106+
} catch (Throwable t) {
107+
socketAddress = InetSocketAddress.createUnresolved(address.connectionHost(), address.port());
108+
}
109+
} else {
110+
socketAddress = localAddress;
111+
}
94112

95-
return installChannelConnectedListeners(
96-
address, connect(address, securityPlan, connectTimeoutMillis), connectTimeoutMillis)
113+
return installChannelConnectedListeners(address, bootstrap.connect(socketAddress), connectTimeoutMillis)
97114
.thenCompose(channel -> BoltProtocol.forChannel(channel)
98115
.initializeChannel(
99116
channel,
@@ -107,39 +124,6 @@ address, connect(address, securityPlan, connectTimeoutMillis), connectTimeoutMil
107124
.thenApply(channel -> new NetworkConnection(channel, logging));
108125
}
109126

110-
private ChannelFuture connect(BoltServerAddress address, SecurityPlan securityPlan, int connectTimeoutMillis) {
111-
Class<? extends Channel> channelClass;
112-
SocketAddress socketAddress;
113-
114-
if (localAddress != null) {
115-
channelClass = LocalChannel.class;
116-
socketAddress = localAddress;
117-
} else {
118-
if (address.path() != null) {
119-
channelClass = NioDomainSocketChannel.class;
120-
socketAddress = UnixDomainSocketAddress.of(address.path());
121-
} else {
122-
channelClass = NioSocketChannel.class;
123-
try {
124-
socketAddress = new InetSocketAddress(
125-
domainNameResolver.resolve(address.connectionHost())[0], address.port());
126-
} catch (Throwable t) {
127-
socketAddress = InetSocketAddress.createUnresolved(address.connectionHost(), address.port());
128-
}
129-
}
130-
}
131-
132-
var bootstrap = new Bootstrap();
133-
bootstrap
134-
.group(this.eventLoopGroup)
135-
.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectTimeoutMillis)
136-
.channel(channelClass)
137-
.resolver(addressResolverGroup)
138-
.handler(new NettyChannelInitializer(address, securityPlan, connectTimeoutMillis, clock, logging));
139-
140-
return bootstrap.connect(socketAddress);
141-
}
142-
143127
private CompletionStage<Channel> installChannelConnectedListeners(
144128
BoltServerAddress address, ChannelFuture channelConnected, int connectTimeoutMillis) {
145129
var pipeline = channelConnected.channel().pipeline();

driver/src/test/java/org/neo4j/driver/internal/DriverFactoryTest.java

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

5656
class DriverFactoryTest {
5757
private static Stream<String> testUris() {
58-
return Stream.of("bolt://localhost:7687", "bolt+unix://localhost:7687", "neo4j://localhost:7687");
58+
return Stream.of("bolt://localhost:7687", "neo4j://localhost:7687");
5959
}
6060

6161
@ParameterizedTest

0 commit comments

Comments
 (0)