Skip to content

Commit b1d5332

Browse files
authored
Include bookmarks when fetching routing table
This is only needed for 4.x. Bookmarks are sent when calling the routing table procedure (4.0 to 4.2) and sending a ROUTE message (4.3).
1 parent 79c4f2e commit b1d5332

File tree

16 files changed

+76
-57
lines changed

16 files changed

+76
-57
lines changed

neo4j/db/connection.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ type Connection interface {
9595
// Gets routing table for specified database name or the default database if
9696
// database equals DefaultDatabase. If the underlying connection does not support
9797
// multiple databases, DefaultDatabase should be used as database.
98-
GetRoutingTable(database string, context map[string]string) (*RoutingTable, error)
98+
GetRoutingTable(context map[string]string, bookmarks []string, database string) (*RoutingTable, error)
9999
}
100100

101101
type RoutingTable struct {

neo4j/directrouter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ type directRouter struct {
2626
address string
2727
}
2828

29-
func (r *directRouter) Readers(ctx context.Context, database string) ([]string, error) {
29+
func (r *directRouter) Readers(ctx context.Context, bookmarks []string, database string) ([]string, error) {
3030
return []string{r.address}, nil
3131
}
3232

33-
func (r *directRouter) Writers(ctx context.Context, database string) ([]string, error) {
33+
func (r *directRouter) Writers(ctx context.Context, bookmarks []string, database string) ([]string, error) {
3434
return []string{r.address}, nil
3535
}
3636

neo4j/driver.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,8 @@ func routingContextFromUrl(useRouting bool, u *url.URL) (map[string]string, erro
224224
}
225225

226226
type sessionRouter interface {
227-
Readers(ctx context.Context, database string) ([]string, error)
228-
Writers(ctx context.Context, database string) ([]string, error)
227+
Readers(ctx context.Context, bookmarks []string, database string) ([]string, error)
228+
Writers(ctx context.Context, bookmarks []string, database string) ([]string, error)
229229
Invalidate(database string)
230230
CleanUp()
231231
}

neo4j/internal/bolt/bolt3.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -682,7 +682,7 @@ func (b *bolt3) Reset() {
682682
}
683683
}
684684

685-
func (b *bolt3) GetRoutingTable(database string, context map[string]string) (*db.RoutingTable, error) {
685+
func (b *bolt3) GetRoutingTable(context map[string]string, bookmarks []string, database string) (*db.RoutingTable, error) {
686686
if err := b.assertState(bolt3_ready); err != nil {
687687
return nil, err
688688
}

neo4j/internal/bolt/bolt4.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -847,25 +847,25 @@ func (b *bolt4) Reset() {
847847
}
848848
}
849849

850-
func (b *bolt4) GetRoutingTable(database string, context map[string]string) (*db.RoutingTable, error) {
850+
func (b *bolt4) GetRoutingTable(context map[string]string, bookmarks []string, database string) (*db.RoutingTable, error) {
851851
if err := b.assertState(bolt4_ready); err != nil {
852852
return nil, err
853853
}
854854

855855
b.log.Infof(log.Bolt4, b.logId, "Retrieving routing table")
856856
if b.minor > 2 {
857-
b.out.appendRoute(database, context)
857+
b.out.appendRoute(context, bookmarks, database)
858858
b.out.send(b.conn)
859859
succ := b.receiveSuccess()
860860
if b.err != nil {
861861
return nil, b.err
862862
}
863863
return succ.routingTable, nil
864864
}
865-
return b.callGetRoutingTable(database, context)
865+
return b.callGetRoutingTable(context, bookmarks, database)
866866
}
867867

868-
func (b *bolt4) callGetRoutingTable(database string, context map[string]string) (*db.RoutingTable, error) {
868+
func (b *bolt4) callGetRoutingTable(context map[string]string, bookmarks []string, database string) (*db.RoutingTable, error) {
869869
// The query should run in system database, preserve current setting and restore it when
870870
// done.
871871
originalDatabaseName := b.databaseName
@@ -882,7 +882,7 @@ func (b *bolt4) callGetRoutingTable(database string, context map[string]string)
882882
runCommand.Cypher = "CALL dbms.routing.getRoutingTable($context, $database)"
883883
runCommand.Params["database"] = database
884884
}
885-
txConfig := db.TxConfig{Mode: db.ReadMode}
885+
txConfig := db.TxConfig{Mode: db.ReadMode, Bookmarks: bookmarks}
886886
streamHandle, err := b.Run(runCommand, txConfig)
887887
if err != nil {
888888
return nil, err

neo4j/internal/bolt/bolt4_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -820,7 +820,7 @@ func TestBolt4(ot *testing.T) {
820820
defer cleanup()
821821
defer bolt.Close()
822822

823-
rt, err := bolt.GetRoutingTable("thedb", map[string]string{"region": "space"})
823+
rt, err := bolt.GetRoutingTable(map[string]string{"region": "space"}, nil, "thedb")
824824
AssertNoError(t, err)
825825
ert := &db.RoutingTable{Routers: []string{"router1"}, TimeToLive: 1000}
826826
if !reflect.DeepEqual(rt, ert) {

neo4j/internal/bolt/outgoing.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,18 @@ func (o *outgoing) appendPullAll() {
120120
o.end()
121121
}
122122

123-
func (o *outgoing) appendRoute(database string, context map[string]string) {
123+
func (o *outgoing) appendRoute(context map[string]string, bookmarks []string, database string) {
124124
o.begin()
125-
o.packer.StructHeader(byte(msgRoute), 2)
125+
o.packer.StructHeader(byte(msgRoute), 3)
126126
o.packer.MapHeader(len(context))
127127
for k, v := range context {
128128
o.packer.String(k)
129129
o.packer.String(v)
130130
}
131+
o.packer.ArrayHeader(len(bookmarks))
132+
for _, bookmark := range bookmarks {
133+
o.packer.String(bookmark)
134+
}
131135
if database == db.DefaultDatabase {
132136
o.packer.Nil()
133137
} else {

neo4j/internal/bolt/outgoing_test.go

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -257,21 +257,41 @@ func TestOutgoing(ot *testing.T) {
257257
{
258258
name: "route",
259259
build: func() {
260-
out.appendRoute("adb", map[string]string{"key1": "val1", "key2": "val2"})
260+
out.appendRoute(map[string]string{"key1": "val1", "key2": "val2"}, []string{"deutsch-mark", "mark-twain"}, "adb")
261261
},
262262
expect: &testStruct{
263263
tag: byte(msgRoute),
264-
fields: []interface{}{map[string]interface{}{"key1": "val1", "key2": "val2"}, "adb"},
264+
fields: []interface{}{map[string]interface{}{"key1": "val1", "key2": "val2"}, []interface{}{"deutsch-mark", "mark-twain"}, "adb"},
265265
},
266266
},
267267
{
268268
name: "route, default database",
269269
build: func() {
270-
out.appendRoute(db.DefaultDatabase, map[string]string{"key1": "val1", "key2": "val2"})
270+
out.appendRoute(map[string]string{"key1": "val1", "key2": "val2"}, []string{"deutsch-mark", "mark-twain"}, db.DefaultDatabase)
271271
},
272272
expect: &testStruct{
273273
tag: byte(msgRoute),
274-
fields: []interface{}{map[string]interface{}{"key1": "val1", "key2": "val2"}, nil},
274+
fields: []interface{}{map[string]interface{}{"key1": "val1", "key2": "val2"}, []interface{}{"deutsch-mark", "mark-twain"}, nil},
275+
},
276+
},
277+
{
278+
name: "route, default bookmarks",
279+
build: func() {
280+
out.appendRoute(map[string]string{"key1": "val1", "key2": "val2"}, nil, "adb")
281+
},
282+
expect: &testStruct{
283+
tag: byte(msgRoute),
284+
fields: []interface{}{map[string]interface{}{"key1": "val1", "key2": "val2"}, []interface{}{}, "adb"},
285+
},
286+
},
287+
{
288+
name: "route, default bookmarks and database",
289+
build: func() {
290+
out.appendRoute(map[string]string{"key1": "val1", "key2": "val2"}, nil, db.DefaultDatabase)
291+
},
292+
expect: &testStruct{
293+
tag: byte(msgRoute),
294+
fields: []interface{}{map[string]interface{}{"key1": "val1", "key2": "val2"}, []interface{}{}, nil},
275295
},
276296
},
277297
}

neo4j/internal/router/readtable.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import (
2727

2828
// Tries to read routing table from any of the specified routers using new or existing connection
2929
// from the supplied pool.
30-
func readTable(ctx context.Context, pool Pool, database string, routers []string, routerContext map[string]string) (*db.RoutingTable, error) {
30+
func readTable(ctx context.Context, pool Pool, routers []string, routerContext map[string]string, bookmarks []string, database string) (*db.RoutingTable, error) {
3131
// Preserve last error to be returned, set a default for case of no routers
3232
var err error = &ReadRoutingTableError{}
3333

@@ -47,7 +47,7 @@ func readTable(ctx context.Context, pool Pool, database string, routers []string
4747

4848
// We have a connection to the "router"
4949
var table *db.RoutingTable
50-
table, err = conn.GetRoutingTable(database, routerContext)
50+
table, err = conn.GetRoutingTable(routerContext, bookmarks, database)
5151
pool.Return(conn)
5252
if err == nil {
5353
return table, nil

neo4j/internal/router/readtable_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ func TestReadTableTable(ot *testing.T) {
166166
ot.Run(c.name, func(t *testing.T) {
167167
ctx, cancel := context.WithCancel(context.Background())
168168
c.pool.cancel = cancel
169-
table, err := readTable(ctx, c.pool, "dbname", c.routers, nil)
169+
table, err := readTable(ctx, c.pool, c.routers, nil, nil, "dbname")
170170
c.assert(t, table, err)
171171
if err != nil && c.assertErr != nil {
172172
c.assertErr(t, err)

0 commit comments

Comments
 (0)