Skip to content

Commit

Permalink
Simplify tokenAwareHostPolicy.Pick
Browse files Browse the repository at this point in the history
  • Loading branch information
dkropachev committed Jun 22, 2024
1 parent dce12c1 commit 598a4ae
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 598a4ae

Please sign in to comment.