Skip to content

Commit 272ac3d

Browse files
4.0 address hint for routing call (#394)
* added address in routing context * fixed stubtests * fixed integration tests for the changes * fixup
1 parent 8285006 commit 272ac3d

28 files changed

+95
-103
lines changed

neo4j/io/__init__.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -531,12 +531,25 @@ def opener(addr, timeout):
531531
return pool
532532

533533
def __init__(self, opener, pool_config, workspace_config, routing_context, addresses):
534+
"""
535+
536+
:param opener:
537+
:param pool_config:
538+
:param workspace_config:
539+
:param routing_context: Dictionary with routing information
540+
:param addresses:
541+
"""
534542
super(Neo4jPool, self).__init__(opener, pool_config, workspace_config)
535543
# Each database have a routing table, the default database is a special case.
536544
log.debug("[#0000] C: <NEO4J POOL> routing addresses %r", addresses)
545+
self.init_address = addresses[0]
537546
self.routing_tables = {workspace_config.database: RoutingTable(database=workspace_config.database, routers=addresses)}
538547
self.routing_context = routing_context
539-
# self.missing_writer = False
548+
if self.routing_context is None:
549+
self.routing_context = {}
550+
elif "address" in self.routing_context:
551+
raise ConfigurationError("The key 'address' is reserved for routing context.")
552+
self.routing_context["address"] = str(self.init_address)
540553
self.refresh_lock = Lock()
541554

542555
def __repr__(self):
@@ -580,6 +593,8 @@ def fetch_routing_info(self, *, address, timeout, database):
580593
:param address: router address
581594
:param timeout: seconds
582595
:param database: the data base name to get routing table for
596+
:param init_address: the address by which the client initially contacted the server as a hint for inclusion in the returned routing table.
597+
583598
:return: list of routing records or
584599
None if no connection could be established
585600
:raise ServiceUnavailable: if the server does not support routing or

tests/stub/conftest.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
# log = logging.getLogger("neo4j")
3636

3737

38-
class StubServer():
38+
class StubServer:
3939

4040
def __init__(self, port, script):
4141
self.port = port
@@ -62,7 +62,7 @@ def kill(self):
6262
self._process.kill()
6363

6464

65-
class StubCluster(object):
65+
class StubCluster:
6666

6767
def __init__(self, servers):
6868
self.servers = {port: StubServer(port, script) for port, script in dict(servers).items()}
@@ -99,7 +99,7 @@ def run(self):
9999
check_call(["python", "-m", "boltkit.legacy.stub", "-v", str(self.port), self.script])
100100

101101

102-
class LegacyStubCluster(object):
102+
class LegacyStubCluster:
103103

104104
def __init__(self, servers):
105105
self.servers = {port: LegacyStubServer(port, script) for port, script in dict(servers).items()}
@@ -157,8 +157,8 @@ def driver_info():
157157
""" Base class for test cases that integrate with a server.
158158
"""
159159
return {
160-
"uri": "bolt://localhost:7687",
161-
"bolt_routing_uri": "neo4j://localhost:7687",
160+
"uri_bolt": "bolt://localhost:9001",
161+
"uri_neo4j": "neo4j://localhost:9001",
162162
"user": "test",
163163
"password": "test",
164164
"auth_token": ("test", "test")

tests/stub/scripts/v3/broken_router.script

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
!: AUTO HELLO
33
!: AUTO GOODBYE
44
!: AUTO RESET
5+
!: PORT 9001
56

6-
C: RUN "CALL dbms.cluster.routing.getRoutingTable($context)" {"context": {}} {}
7+
C: RUN "CALL dbms.cluster.routing.getRoutingTable($context)" {"context": {"address": "localhost:9001"}} {}
78
PULL_ALL
89
S: FAILURE {"code": "Neo.DatabaseError.General.UnknownError", "message": "An unknown error occurred."}
910
IGNORED

tests/stub/scripts/v3/dbms_cluster_routing_get_routing_table_system.script

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
C: HELLO {"user_agent": "test", "scheme": "basic", "principal": "test", "credentials": "test"}
55
S: SUCCESS {"server": "Neo4j/3.5.0", "connection_id": "12345678-1234-1234-1234-123456789000"}
66

7-
C: RUN "CALL dbms.cluster.routing.getRoutingTable($context)" {"context": {}} {"mode": "r"}
7+
C: RUN "CALL dbms.cluster.routing.getRoutingTable($context)" {"context": {"address": "localhost:9001"}} {"mode": "r"}
88
PULL_ALL
99
S: SUCCESS {"fields": ["ttl", "servers"]}
1010
RECORD [1234, [{"role":"WRITE", "addresses":["127.0.0.1:9001"]}, {"role":"READ", "addresses":["127.0.0.1:9002", "127.0.0.1:9003"]}, {"role":"ROUTE", "addresses":["127.0.0.1:9001", "127.0.0.1:9002", "127.0.0.1:9003"]}]]

tests/stub/scripts/v3/get_routing_table.script

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
!: AUTO HELLO
33
!: AUTO GOODBYE
44
!: AUTO RESET
5+
!: PORT 9001
56

6-
C: RUN "CALL dbms.cluster.routing.getRoutingTable($context)" {"context": {}} {"mode": "r"}
7+
C: RUN "CALL dbms.cluster.routing.getRoutingTable($context)" {"context": {"address": "localhost:9001"}} {"mode": "r"}
78
PULL_ALL
89
S: SUCCESS {"fields": ["ttl", "servers"]}
910
RECORD [9223372036854775807, [{"addresses": ["127.0.0.1:9001"],"role": "WRITE"}, {"addresses": ["127.0.0.1:9002"], "role": "READ"},{"addresses": ["127.0.0.1:9001", "127.0.0.1:9002"], "role": "ROUTE"}]]

tests/stub/scripts/v3/get_routing_table_with_context.script

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
!: AUTO HELLO
33
!: AUTO GOODBYE
44
!: AUTO RESET
5+
!: PORT 9001
56

6-
C: RUN "CALL dbms.cluster.routing.getRoutingTable($context)" {"context": {"name": "molly", "age": "1"}} {"mode": "r"}
7+
C: RUN "CALL dbms.cluster.routing.getRoutingTable($context)" {"context": {"name": "molly", "age": "1", "address": "localhost:9001"}} {"mode": "r"}
78
PULL_ALL
89
S: SUCCESS {"fields": ["ttl", "servers"]}
910
RECORD [9223372036854775807, [{"addresses": ["127.0.0.1:9001"],"role": "WRITE"}, {"addresses": ["127.0.0.1:9002"], "role": "READ"},{"addresses": ["127.0.0.1:9001", "127.0.0.1:9002"], "role": "ROUTE"}]]

tests/stub/scripts/v3/non_router.script

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
!: BOLT 3
22
!: AUTO HELLO
33
!: AUTO GOODBYE
4+
!: PORT 9001
45

5-
C: RUN "CALL dbms.cluster.routing.getRoutingTable($context)" {"context": {}} {"mode":"r"}
6+
C: RUN "CALL dbms.cluster.routing.getRoutingTable($context)" {"context": {"address": "localhost:9001"}} {"mode":"r"}
67
PULL_ALL
78
S: FAILURE {"code": "Neo.ClientError.Procedure.ProcedureNotFound", "message": "Not a router"}
89
IGNORED

tests/stub/scripts/v3/router.script

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
!: AUTO RESET
55
!: PORT 9001
66

7-
C: RUN "CALL dbms.cluster.routing.getRoutingTable($context)" {"context": {}} {"mode": "r"}
7+
C: RUN "CALL dbms.cluster.routing.getRoutingTable($context)" {"context": {"address": "localhost:9001"}} {"mode": "r"}
88
PULL_ALL
99
S: SUCCESS {"fields": ["ttl", "servers"]}
1010
RECORD [300, [{"role":"ROUTE","addresses":["127.0.0.1:9001","127.0.0.1:9002","127.0.0.1:9003"]},{"role":"READ","addresses":["127.0.0.1:9004","127.0.0.1:9005"]},{"role":"WRITE","addresses":["127.0.0.1:9006"]}]]

tests/stub/scripts/v3/router_no_readers.script

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
!: AUTO HELLO
33
!: AUTO GOODBYE
44
!: AUTO RESET
5+
!: PORT 9001
56

6-
C: RUN "CALL dbms.cluster.routing.getRoutingTable($context)" {"context": {}} {"mode": "r"}
7+
C: RUN "CALL dbms.cluster.routing.getRoutingTable($context)" {"context": {"address": "localhost:9001"}} {"mode": "r"}
78
PULL_ALL
89
S: SUCCESS {"fields": ["ttl", "servers"]}
910
RECORD [300, [{"role":"ROUTE","addresses":["127.0.0.1:9001","127.0.0.1:9002","127.0.0.1:9003"]},{"role":"READ","addresses":[]},{"role":"WRITE","addresses":["127.0.0.1:9006"]}]]

tests/stub/scripts/v3/router_no_routers.script

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@
22
!: AUTO HELLO
33
!: AUTO GOODBYE
44
!: AUTO RESET
5+
!: PORT 9001
56

6-
C: RUN "CALL dbms.cluster.routing.getRoutingTable($context)" {"context": {}} {"mode": "r"}
7+
C: RUN "CALL dbms.cluster.routing.getRoutingTable($context)" {"context": {"address": "localhost:9001"}} {"mode": "r"}
78
PULL_ALL
89
S: SUCCESS {"fields": ["ttl", "servers"]}
910
RECORD [300, [{"role":"ROUTE","addresses":[]},{"role":"READ","addresses":["127.0.0.1:9004","127.0.0.1:9005"]},{"role":"WRITE","addresses":["127.0.0.1:9006"]}]]

0 commit comments

Comments
 (0)