Skip to content

Commit 0257d77

Browse files
Peter Wilhelmsson2hdddg
Peter Wilhelmsson
authored andcommitted
Send connect address to routing procedure
1 parent a5352d5 commit 0257d77

11 files changed

+93
-73
lines changed

src/internal/connection-provider-routing.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ export default class RoutingConnectionProvider extends PooledConnectionProvider
5858

5959
this._seedRouter = address
6060
this._routingTables = {}
61-
this._rediscovery = new Rediscovery(new RoutingUtil(routingContext))
61+
this._rediscovery = new Rediscovery(
62+
new RoutingUtil(routingContext, address.toString())
63+
)
6264
this._loadBalancingStrategy = new LeastConnectedLoadBalancingStrategy(
6365
this._connectionPool
6466
)

src/internal/routing-util.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,11 @@ const PROCEDURE_NOT_FOUND_CODE = 'Neo.ClientError.Procedure.ProcedureNotFound'
3333
const DATABASE_NOT_FOUND_CODE = 'Neo.ClientError.Database.DatabaseNotFound'
3434

3535
export default class RoutingUtil {
36-
constructor (routingContext) {
36+
constructor (routingContext, initialAddress) {
3737
this._routingContext = routingContext
38+
// The address that the driver is connecting to, used by routing as a fallback when routing
39+
// and clustering isn't configured.
40+
this._initialAddress = initialAddress
3841
}
3942

4043
/**
@@ -141,13 +144,14 @@ export default class RoutingUtil {
141144
let query
142145
let params
143146

144-
const version = ServerVersion.fromString(connection.version)
145-
if (version.compareTo(VERSION_4_0_0) >= 0) {
147+
const protocolVersion = connection.protocol().version
148+
if (protocolVersion >= 4) {
146149
query = CALL_GET_ROUTING_TABLE_MULTI_DB
147150
params = {
148-
context: this._routingContext,
151+
context: this._routingContext || {},
149152
database: database || null
150153
}
154+
params.context.address = this._initialAddress
151155
} else {
152156
query = CALL_GET_ROUTING_TABLE
153157
params = { context: this._routingContext }

test/internal/fake-connection.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,10 @@ export default class FakeConnection extends Connection {
6969
}
7070

7171
protocol () {
72-
// return fake protocol object that simply records seen queries and parameters
72+
// return fake protocol object that simply records seen queries and parameters and provide
73+
// a protocol version
7374
return {
75+
version: this.version === 'Neo4j/4.0.0' ? 4 : 3,
7476
run: (query, parameters, protocolOptions) => {
7577
this.seenQueries.push(query)
7678
this.seenParameters.push(parameters)

test/internal/node/routing.driver.boltkit.test.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,21 +2225,21 @@ describe('#stub-routing routing driver with stub server', () => {
22252225
// Given
22262226
const router1 = await boltStub.start(
22272227
'./test/resources/boltstub/v4/acquire_endpoints_aDatabase_no_servers.script',
2228-
9001
2228+
9002
22292229
)
22302230
const router2 = await boltStub.start(
22312231
'./test/resources/boltstub/v4/acquire_endpoints_aDatabase.script',
2232-
9002
2232+
9003
22332233
)
22342234
const reader1 = await boltStub.start(
22352235
'./test/resources/boltstub/v4/read_from_aDatabase.script',
22362236
9005
22372237
)
22382238

2239-
const driver = boltStub.newDriver('neo4j://127.0.0.1:9000', {
2239+
const driver = boltStub.newDriver('neo4j://127.0.0.1:9001', {
22402240
resolver: address => [
2241-
'neo4j://127.0.0.1:9001',
2242-
'neo4j://127.0.0.1:9002'
2241+
'neo4j://127.0.0.1:9002',
2242+
'neo4j://127.0.0.1:9003'
22432243
]
22442244
})
22452245

test/internal/routing-util.test.js

Lines changed: 68 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,58 @@ import {
3737

3838
const ROUTER_ADDRESS = ServerAddress.fromUrl('test.router.com:4242')
3939

40+
class FakeSession {
41+
constructor (runResponse, fakeConnection) {
42+
this._runResponse = runResponse
43+
this._fakeConnection = fakeConnection
44+
this._closed = false
45+
}
46+
47+
static successful (result) {
48+
return new FakeSession(Promise.resolve(result), null)
49+
}
50+
51+
static failed (error) {
52+
return new FakeSession(Promise.reject(error), null)
53+
}
54+
55+
static withFakeConnection (connection) {
56+
return new FakeSession(null, connection)
57+
}
58+
59+
_run (ignoreQuery, ignoreParameters, queryRunner) {
60+
if (this._runResponse) {
61+
return this._runResponse
62+
}
63+
queryRunner(this._fakeConnection)
64+
return Promise.resolve()
65+
}
66+
67+
withBookmark (bookmark) {
68+
this._lastBookmark = bookmark
69+
return this
70+
}
71+
72+
withDatabase (database) {
73+
this._database = database || ''
74+
return this
75+
}
76+
77+
withMode (mode) {
78+
this._mode = mode
79+
return this
80+
}
81+
82+
close () {
83+
this._closed = true
84+
return Promise.resolve()
85+
}
86+
87+
isClosed () {
88+
return this._closed
89+
}
90+
}
91+
4092
describe('#unit RoutingUtil', () => {
4193
it('should return retrieved records when query succeeds', done => {
4294
const session = FakeSession.successful({ records: ['foo', 'bar', 'baz'] })
@@ -185,7 +237,7 @@ describe('#unit RoutingUtil', () => {
185237
'CALL dbms.routing.getRoutingTable($context, $database)'
186238
])
187239
expect(connection.seenParameters).toEqual([
188-
{ context: {}, database: null }
240+
{ context: { address: '127.0.0.1' }, database: null }
189241
])
190242
expect(connection.seenProtocolOptions).toEqual([
191243
jasmine.objectContaining({
@@ -212,7 +264,7 @@ describe('#unit RoutingUtil', () => {
212264
'CALL dbms.routing.getRoutingTable($context, $database)'
213265
])
214266
expect(connection.seenParameters).toEqual([
215-
{ context: {}, database: null }
267+
{ context: { address: '127.0.0.1' }, database: null }
216268
])
217269
expect(connection.seenProtocolOptions).toEqual([
218270
jasmine.objectContaining({
@@ -238,7 +290,7 @@ describe('#unit RoutingUtil', () => {
238290
'CALL dbms.routing.getRoutingTable($context, $database)'
239291
])
240292
expect(connection.seenParameters).toEqual([
241-
{ context: {}, database: null }
293+
{ context: { address: '127.0.0.1' }, database: null }
242294
])
243295
expect(connection.seenProtocolOptions).toEqual([
244296
jasmine.objectContaining({
@@ -261,6 +313,18 @@ describe('#unit RoutingUtil', () => {
261313
))
262314
})
263315

316+
it('should pass initial address while invoking routing procedure', async () => {
317+
const connection = new FakeConnection().withServerVersion('Neo4j/4.0.0')
318+
const session = FakeSession.withFakeConnection(connection)
319+
const util = new RoutingUtil({}, 'initialAddr')
320+
321+
await util.callRoutingProcedure(session, '', ROUTER_ADDRESS)
322+
323+
expect(connection.seenParameters).toEqual([
324+
{ context: { address: 'initialAddr' }, database: null }
325+
])
326+
})
327+
264328
it('should parse valid ttl', () => {
265329
const clock = lolex.install()
266330
try {
@@ -451,7 +515,7 @@ describe('#unit RoutingUtil', () => {
451515
}
452516

453517
function callRoutingProcedure (session, database, routingContext) {
454-
const util = new RoutingUtil(routingContext || {})
518+
const util = new RoutingUtil(routingContext || {}, '127.0.0.1')
455519
return util.callRoutingProcedure(session, database, ROUTER_ADDRESS)
456520
}
457521

@@ -503,56 +567,4 @@ describe('#unit RoutingUtil', () => {
503567
done()
504568
})
505569
}
506-
507-
class FakeSession {
508-
constructor (runResponse, fakeConnection) {
509-
this._runResponse = runResponse
510-
this._fakeConnection = fakeConnection
511-
this._closed = false
512-
}
513-
514-
static successful (result) {
515-
return new FakeSession(Promise.resolve(result), null)
516-
}
517-
518-
static failed (error) {
519-
return new FakeSession(Promise.reject(error), null)
520-
}
521-
522-
static withFakeConnection (connection) {
523-
return new FakeSession(null, connection)
524-
}
525-
526-
_run (ignoreQuery, ignoreParameters, queryRunner) {
527-
if (this._runResponse) {
528-
return this._runResponse
529-
}
530-
queryRunner(this._fakeConnection)
531-
return Promise.resolve()
532-
}
533-
534-
withBookmark (bookmark) {
535-
this._lastBookmark = bookmark
536-
return this
537-
}
538-
539-
withDatabase (database) {
540-
this._database = database || ''
541-
return this
542-
}
543-
544-
withMode (mode) {
545-
this._mode = mode
546-
return this
547-
}
548-
549-
close () {
550-
this._closed = true
551-
return Promise.resolve()
552-
}
553-
554-
isClosed () {
555-
return this._closed
556-
}
557-
}
558570
})

test/resources/boltstub/v4/acquire_endpoints_aDatabase.script

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
!: AUTO RESET
44
!: AUTO GOODBYE
55

6-
C: RUN "CALL dbms.routing.getRoutingTable($context, $database)" {"context": {}, "database": "aDatabase"} {"mode": "r", "db": "system"}
6+
C: RUN "CALL dbms.routing.getRoutingTable($context, $database)" {"context": {"address": "127.0.0.1:9001"}, "database": "aDatabase"} {"mode": "r", "db": "system"}
77
PULL {"n": -1}
88
S: SUCCESS {"fields": ["ttl", "servers"]}
99
RECORD [9223372036854775807, [{"addresses": ["127.0.0.1:9007","127.0.0.1:9008"],"role": "WRITE"}, {"addresses": ["127.0.0.1:9005","127.0.0.1:9006"], "role": "READ"},{"addresses": ["127.0.0.1:9001","127.0.0.1:9002","127.0.0.1:9003"], "role": "ROUTE"}]]

test/resources/boltstub/v4/acquire_endpoints_aDatabase_no_servers.script

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
!: AUTO RESET
44
!: AUTO GOODBYE
55

6-
C: RUN "CALL dbms.routing.getRoutingTable($context, $database)" {"context": {}, "database":"aDatabase"} {"mode": "r", "db": "system"}
6+
C: RUN "CALL dbms.routing.getRoutingTable($context, $database)" {"context": {"address": "127.0.0.1:9001"}, "database":"aDatabase"} {"mode": "r", "db": "system"}
77
PULL {"n": -1}
88
S: SUCCESS {"fields": ["ttl", "servers"]}
99
RECORD [9223372036854775807, []]

test/resources/boltstub/v4/acquire_endpoints_aDatabase_with_bookmark.script

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
!: AUTO RESET
44
!: AUTO GOODBYE
55

6-
C: RUN "CALL dbms.routing.getRoutingTable($context, $database)" {"context": {}, "database": "aDatabase"} {"mode": "r", "db": "system", "bookmarks": ["system:1111", "aDatabase:5555"]}
6+
C: RUN "CALL dbms.routing.getRoutingTable($context, $database)" {"context": {"address":"127.0.0.1:9001"}, "database": "aDatabase"} {"mode": "r", "db": "system", "bookmarks": ["system:1111", "aDatabase:5555"]}
77
PULL {"n": -1}
88
S: SUCCESS {"fields": ["ttl", "servers"]}
99
RECORD [9223372036854775807, [{"addresses": ["127.0.0.1:9007","127.0.0.1:9008"],"role": "WRITE"}, {"addresses": ["127.0.0.1:9005","127.0.0.1:9006"], "role": "READ"},{"addresses": ["127.0.0.1:9001","127.0.0.1:9002","127.0.0.1:9003"], "role": "ROUTE"}]]

test/resources/boltstub/v4/acquire_endpoints_db_not_found.script

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
!: AUTO RESET
44
!: AUTO GOODBYE
55

6-
C: RUN "CALL dbms.routing.getRoutingTable($context, $database)" {"context": {}, "database": "aDatabase"} {"mode": "r", "db": "system"}
6+
C: RUN "CALL dbms.routing.getRoutingTable($context, $database)" {"context": {"address": "127.0.0.1:9001"}, "database": "aDatabase"} {"mode": "r", "db": "system"}
77
PULL {"n": -1}
88
S: FAILURE {"code": "Neo.ClientError.Database.DatabaseNotFound", "message": "database not found"}
99
IGNORED

test/resources/boltstub/v4/acquire_endpoints_default_database.script

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
!: AUTO RESET
44
!: AUTO GOODBYE
55

6-
C: RUN "CALL dbms.routing.getRoutingTable($context, $database)" {"context": {}, "database": null} {"mode": "r", "db": "system"}
6+
C: RUN "CALL dbms.routing.getRoutingTable($context, $database)" {"context": {"address": "127.0.0.1:9001"}, "database": null} {"mode": "r", "db": "system"}
77
PULL {"n": -1}
88
S: SUCCESS {"fields": ["ttl", "servers"]}
99
RECORD [9223372036854775807, [{"addresses": ["127.0.0.1:9007","127.0.0.1:9008"],"role": "WRITE"}, {"addresses": ["127.0.0.1:9005","127.0.0.1:9006"], "role": "READ"},{"addresses": ["127.0.0.1:9001","127.0.0.1:9002","127.0.0.1:9003"], "role": "ROUTE"}]]

test/session.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ describe('#integration session', () => {
4141
driver = neo4j.driver('bolt://localhost', sharedNeo4j.authToken)
4242
session = driver.session()
4343
originalTimeout = jasmine.DEFAULT_TIMEOUT_INTERVAL
44-
jasmine.DEFAULT_TIMEOUT_INTERVAL = 60000
44+
jasmine.DEFAULT_TIMEOUT_INTERVAL = 70000
4545

4646
serverVersion = await sharedNeo4j.cleanupAndGetVersion(driver)
4747
})

0 commit comments

Comments
 (0)