ec4793573d1447b6f92a1b359a0594566fad9d0e | Author: Michal Matczuk <michal@scylladb.com>
| 2019-02-19 23:14:29+01:00
policy: tokenAwareHostPolicy host selection bug fix (#1261)
* policy: tokenAwareHostPolicy host selection bug fix
Bug:
`tokenAwareHostPolicy` always selects a primary replica of a token.
When using `tokenAwareHostPolicy` backed by `dcAwareRR`, for tokens
with primary replica in a different DC, this results in selecting
a RANDOM host in a given DC.
This is caused by two issues in `tokenAwareHostPolicy::getReplicas` func.
* `tokenAwareHostPolicy::meta` is always nil unless a keyspace changed
event it received
* even when metadata is available it uses the calculated token value of
a partition key to find replicas, however, the replicas are stored
in a map where the key is the primary replica token aka the `end_token`
(nodetool describering)
Solution:
This commit extends `tokenRing::GetHostForPartitionKey` to return
the endToken that is later used in the `Pick` function, and forces
a call to `KeyspaceChanged` when session is initialised.
Signed-off-by: Michał Matczuk <michal@scylladb.com>
* policy: tokenAwareHostPolicy update keyspace metadata on host add or remove events
Currently when host is added or removed tokenAwareHostPolicy uses old
replica information. This is fixed by extracting the `KeyspaceChanged`
function body as `updateKeyspaceMetadata` and calling after the token
ring is updated.
Signed-off-by: Michał Matczuk <michal@scylladb.com>
* policy: tokenAwareHostPolicy type assertion bug fix
Fixes inappropriate type assertion
```
--- FAIL: TestTokenAwareConnPool (0.64s)
panic: interface conversion: interface {} is nil, not *gocql.tokenRing [recovered]
panic: interface conversion: interface {} is nil, not *gocql.tokenRing
goroutine 4058 [running]:
testing.tRunner.func1(0xc42010c0f0)
/home/travis/.gimme/versions/go1.10.linux.amd64/src/testing/testing.go:742 +0x567
panic(0x886740, 0xc420318cc0)
/home/travis/.gimme/versions/go1.10.linux.amd64/src/runtime/panic.go:505 +0x24a
github.com/gocql/gocql.(*tokenAwareHostPolicy).updateKeyspaceMetadata(0xc4210f1d80, 0x8dd7aa, 0xa)
/home/travis/gopath/src/github.com/gocql/gocql/policies.go:445 +0x55c
github.com/gocql/gocql.(*tokenAwareHostPolicy).AddHost(0xc4210f1d80, 0xc420668580)
/home/travis/gopath/src/github.com/gocql/gocql/policies.go:478 +0xcc
github.com/gocql/gocql.(*Session).addNewNode(0xc420f40380, 0xc420668580)
/home/travis/gopath/src/github.com/gocql/gocql/events.go:188 +0xee
github.com/gocql/gocql.(*Session).handleNodeUp(0xc420f40380, 0xc420f00630, 0x10, 0x10, 0x2352, 0x0)
/home/travis/gopath/src/github.com/gocql/gocql/events.go:273 +0x2e9
github.com/gocql/gocql.(*controlConn).setupConn(0xc42113c080, 0xc420782400, 0x3, 0x4)
/home/travis/gopath/src/github.com/gocql/gocql/control.go:289 +0x1fc
github.com/gocql/gocql.(*controlConn).connect(0xc42113c080, 0xc42113e820, 0x3, 0x4, 0xc42113e820, 0x3)
/home/travis/gopath/src/github.com/gocql/gocql/control.go:252 +0x218
github.com/gocql/gocql.(*Session).init(0xc420f40380, 0xc420c338f0, 0x0)
/home/travis/gopath/src/github.com/gocql/gocql/session.go:197 +0x80e
github.com/gocql/gocql.NewSession(0xc420094960, 0x3, 0x3, 0x8dc0cd, 0x5, 0x4, 0xdf8475800, 0x23c34600, 0x2352, 0x8dd7aa, ...)
/home/travis/gopath/src/github.com/gocql/gocql/session.go:160 +0x1138
github.com/gocql/gocql.(*ClusterConfig).CreateSession(0xc421122000, 0xc420566de0, 0x0, 0xc420046800)
/home/travis/gopath/src/github.com/gocql/gocql/cluster.go:185 +0x8d
github.com/gocql/gocql.createSessionFromCluster(0xc421122000, 0x9421a0, 0xc42010c0f0, 0x0)
/home/travis/gopath/src/github.com/gocql/gocql/common_test.go:136 +0x116
github.com/gocql/gocql.TestTokenAwareConnPool(0xc42010c0f0)
/home/travis/gopath/src/github.com/gocql/gocql/cassandra_test.go:2520 +0x195
testing.tRunner(0xc42010c0f0, 0x8f98a0)
/home/travis/.gimme/versions/go1.10.linux.amd64/src/testing/testing.go:777 +0x16e
created by testing.(*T).Run
/home/travis/.gimme/versions/go1.10.linux.amd64/src/testing/testing.go:824 +0x565
exit status 2
```
Signed-off-by: Michał Matczuk <michal@scylladb.com>