Skip to content

Commit

Permalink
Simplify tokenAwareHostPolicy.Pick
Browse files Browse the repository at this point in the history
Main goal is to make `tokenAwareHostPolicy.Pick` code less convoluted.
But there are little functional changes:
Before this PR hosts from tablets are not being shuffled
There are three host information sources: tablets, meta and tokenring,
Before this PR if tables returned no hosts for given token, in certain cases code did not consider meta or tokenring.
And same for meta, it did not consider tokenring.
After this PR, it starts goes through tablets, meta and tokenring, if current host sources yield no hosts it proceed to next one.
  • Loading branch information
dkropachev committed Jun 25, 2024
1 parent dce12c1 commit b79ba6d
Showing 1 changed file with 12 additions and 26 deletions.
38 changes: 12 additions & 26 deletions policies.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,15 +651,12 @@ func (t *tokenAwareHostPolicy) Pick(qry ExecutableQuery) NextHost {
var replicas []*HostInfo

if qry.GetSession() != nil && qry.GetSession().tabletsRoutingV1 {
t.tablets.mu.Lock()
tablets := t.tablets.get()

// Search for tablets with Keyspace and Table from the Query
l, r := findTablets(tablets, qry.Keyspace(), qry.Table())
if l != -1 {
tablet := findTabletForToken(tablets, token, l, r)

replicas = []*HostInfo{}
hosts := t.hosts.get()
for _, replica := range tablet.Replicas() {
for _, host := range hosts {
Expand All @@ -669,36 +666,25 @@ func (t *tokenAwareHostPolicy) Pick(qry ExecutableQuery) NextHost {
}
}
}
} else {
ht := meta.replicas[qry.Keyspace()].replicasFor(token)

if ht == nil {
host, _ := meta.tokenRing.GetHostForToken(token)
replicas = []*HostInfo{host}
} else {
replicas = ht.hosts
}
}

if t.shuffleReplicas && !qry.IsLWT() {
replicas = shuffleHosts(replicas)
}
}

t.tablets.mu.Unlock()
} else {
if len(replicas) == 0 {
ht := meta.replicas[qry.Keyspace()].replicasFor(token)

if ht == nil {
host, _ := meta.tokenRing.GetHostForToken(token)
replicas = []*HostInfo{host}
} else {
if ht != nil {
replicas = ht.hosts
if t.shuffleReplicas && !qry.IsLWT() {
replicas = shuffleHosts(replicas)
}
}
}

if len(replicas) == 0 {
host, _ := meta.tokenRing.GetHostForToken(token)
replicas = []*HostInfo{host}
}

if t.shuffleReplicas && !qry.IsLWT() && len(replicas) > 1 {
replicas = shuffleHosts(replicas)
}

if s := qry.GetSession(); s != nil && t.avoidSlowReplicas {
healthyReplicas := make([]*HostInfo, 0, len(replicas))
unhealthyReplicas := make([]*HostInfo, 0, len(replicas))
Expand Down

0 comments on commit b79ba6d

Please sign in to comment.