forked from scylladb/gocql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
policies.go
1361 lines (1138 loc) · 36.7 KB
/
policies.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) 2012 The gocql Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package gocql
//This file will be the future home for more policies
import (
"context"
"errors"
"fmt"
"math"
"math/rand"
"sync"
"sync/atomic"
"time"
"github.com/hailocab/go-hostpool"
)
// cowHostList implements a copy on write host list, its equivalent type is []*HostInfo
type cowHostList struct {
list atomic.Value
mu sync.Mutex
}
func (c *cowHostList) String() string {
return fmt.Sprintf("%+v", c.get())
}
func (c *cowHostList) get() []*HostInfo {
// TODO(zariel): should we replace this with []*HostInfo?
l, ok := c.list.Load().(*[]*HostInfo)
if !ok {
return nil
}
return *l
}
// add will add a host if it not already in the list
func (c *cowHostList) add(host *HostInfo) bool {
c.mu.Lock()
l := c.get()
if n := len(l); n == 0 {
l = []*HostInfo{host}
} else {
newL := make([]*HostInfo, n+1)
for i := 0; i < n; i++ {
if host.Equal(l[i]) {
c.mu.Unlock()
return false
}
newL[i] = l[i]
}
newL[n] = host
l = newL
}
c.list.Store(&l)
c.mu.Unlock()
return true
}
func (c *cowHostList) remove(host *HostInfo) bool {
c.mu.Lock()
l := c.get()
size := len(l)
if size == 0 {
c.mu.Unlock()
return false
}
found := false
newL := make([]*HostInfo, 0, size)
for i := 0; i < len(l); i++ {
if !l[i].Equal(host) {
newL = append(newL, l[i])
} else {
found = true
}
}
if !found {
c.mu.Unlock()
return false
}
newL = newL[: size-1 : size-1]
c.list.Store(&newL)
c.mu.Unlock()
return true
}
// cowTabletList implements a copy on write tablet list, its equivalent type is []*TabletInfo
// Experimental, this interface and use may change
type cowTabletList struct {
list atomic.Value
mu sync.Mutex
}
func (c *cowTabletList) get() []*TabletInfo {
l, ok := c.list.Load().(*[]*TabletInfo)
if !ok {
return nil
}
return *l
}
func (c *cowTabletList) set(tablets []*TabletInfo) {
c.mu.Lock()
defer c.mu.Unlock()
n := len(tablets)
l := make([]*TabletInfo, n)
for i := 0; i < n; i++ {
l[i] = tablets[i]
}
c.list.Store(&l)
}
// RetryableQuery is an interface that represents a query or batch statement that
// exposes the correct functions for the retry policy logic to evaluate correctly.
type RetryableQuery interface {
Attempts() int
SetConsistency(c Consistency)
GetConsistency() Consistency
Context() context.Context
}
type RetryType uint16
const (
Retry RetryType = 0x00 // retry on same connection
RetryNextHost RetryType = 0x01 // retry on another connection
Ignore RetryType = 0x02 // ignore error and return result
Rethrow RetryType = 0x03 // raise error and stop retrying
)
// ErrUnknownRetryType is returned if the retry policy returns a retry type
// unknown to the query executor.
var ErrUnknownRetryType = errors.New("unknown retry type returned by retry policy")
// RetryPolicy interface is used by gocql to determine if a query can be attempted
// again after a retryable error has been received. The interface allows gocql
// users to implement their own logic to determine if a query can be attempted
// again.
//
// See SimpleRetryPolicy as an example of implementing and using a RetryPolicy
// interface.
type RetryPolicy interface {
Attempt(RetryableQuery) bool
GetRetryType(error) RetryType
}
// LWTRetryPolicy is a similar interface to RetryPolicy
// If a query is recognized as an LWT query and its RetryPolicy satisfies this
// interface, then this interface will be used instead of RetryPolicy.
type LWTRetryPolicy interface {
AttemptLWT(RetryableQuery) bool
GetRetryTypeLWT(error) RetryType
}
// SimpleRetryPolicy has simple logic for attempting a query a fixed number of times.
//
// See below for examples of usage:
//
// //Assign to the cluster
// cluster.RetryPolicy = &gocql.SimpleRetryPolicy{NumRetries: 3}
//
// //Assign to a query
// query.RetryPolicy(&gocql.SimpleRetryPolicy{NumRetries: 1})
type SimpleRetryPolicy struct {
NumRetries int //Number of times to retry a query
}
// Attempt tells gocql to attempt the query again based on query.Attempts being less
// than the NumRetries defined in the policy.
func (s *SimpleRetryPolicy) Attempt(q RetryableQuery) bool {
return q.Attempts() <= s.NumRetries
}
func (s *SimpleRetryPolicy) AttemptLWT(q RetryableQuery) bool {
return s.Attempt(q)
}
func (s *SimpleRetryPolicy) GetRetryType(err error) RetryType {
return RetryNextHost
}
// Retrying on a different host is fine for normal (non-LWT) queries,
// but in case of LWTs it will cause Paxos contention and possibly
// even timeouts if other clients send statements touching the same
// partition to the original node at the same time.
func (s *SimpleRetryPolicy) GetRetryTypeLWT(err error) RetryType {
return Retry
}
// ExponentialBackoffRetryPolicy sleeps between attempts
type ExponentialBackoffRetryPolicy struct {
NumRetries int
Min, Max time.Duration
}
func (e *ExponentialBackoffRetryPolicy) Attempt(q RetryableQuery) bool {
if q.Attempts() > e.NumRetries {
return false
}
time.Sleep(e.napTime(q.Attempts()))
return true
}
func (e *ExponentialBackoffRetryPolicy) AttemptLWT(q RetryableQuery) bool {
return e.Attempt(q)
}
// used to calculate exponentially growing time
func getExponentialTime(min time.Duration, max time.Duration, attempts int) time.Duration {
if min <= 0 {
min = 100 * time.Millisecond
}
if max <= 0 {
max = 10 * time.Second
}
minFloat := float64(min)
napDuration := minFloat * math.Pow(2, float64(attempts-1))
// add some jitter
napDuration += rand.Float64()*minFloat - (minFloat / 2)
if napDuration > float64(max) {
return time.Duration(max)
}
return time.Duration(napDuration)
}
func (e *ExponentialBackoffRetryPolicy) GetRetryType(err error) RetryType {
return RetryNextHost
}
// Retrying on a different host is fine for normal (non-LWT) queries,
// but in case of LWTs it will cause Paxos contention and possibly
// even timeouts if other clients send statements touching the same
// partition to the original node at the same time.
func (e *ExponentialBackoffRetryPolicy) GetRetryTypeLWT(err error) RetryType {
return Retry
}
// DowngradingConsistencyRetryPolicy: Next retry will be with the next consistency level
// provided in the slice
//
// On a read timeout: the operation is retried with the next provided consistency
// level.
//
// On a write timeout: if the operation is an :attr:`~.UNLOGGED_BATCH`
// and at least one replica acknowledged the write, the operation is
// retried with the next consistency level. Furthermore, for other
// write types, if at least one replica acknowledged the write, the
// timeout is ignored.
//
// On an unavailable exception: if at least one replica is alive, the
// operation is retried with the next provided consistency level.
type DowngradingConsistencyRetryPolicy struct {
ConsistencyLevelsToTry []Consistency
}
func (d *DowngradingConsistencyRetryPolicy) Attempt(q RetryableQuery) bool {
currentAttempt := q.Attempts()
if currentAttempt > len(d.ConsistencyLevelsToTry) {
return false
} else if currentAttempt > 0 {
q.SetConsistency(d.ConsistencyLevelsToTry[currentAttempt-1])
}
return true
}
func (d *DowngradingConsistencyRetryPolicy) GetRetryType(err error) RetryType {
switch t := err.(type) {
case *RequestErrUnavailable:
if t.Alive > 0 {
return Retry
}
return Rethrow
case *RequestErrWriteTimeout:
if t.WriteType == "SIMPLE" || t.WriteType == "BATCH" || t.WriteType == "COUNTER" {
if t.Received > 0 {
return Ignore
}
return Rethrow
}
if t.WriteType == "UNLOGGED_BATCH" {
return Retry
}
return Rethrow
case *RequestErrReadTimeout:
return Retry
default:
return RetryNextHost
}
}
func (e *ExponentialBackoffRetryPolicy) napTime(attempts int) time.Duration {
return getExponentialTime(e.Min, e.Max, attempts)
}
type HostStateNotifier interface {
AddHost(host *HostInfo)
RemoveHost(host *HostInfo)
HostUp(host *HostInfo)
HostDown(host *HostInfo)
}
type KeyspaceUpdateEvent struct {
Keyspace string
Change string
}
type HostTierer interface {
// HostTier returns an integer specifying how far a host is from the client.
// Tier must start at 0.
// The value is used to prioritize closer hosts during host selection.
// For example this could be:
// 0 - local rack, 1 - local DC, 2 - remote DC
// or:
// 0 - local DC, 1 - remote DC
HostTier(host *HostInfo) uint
// This function returns the maximum possible host tier
MaxHostTier() uint
}
// HostSelectionPolicy is an interface for selecting
// the most appropriate host to execute a given query.
// HostSelectionPolicy instances cannot be shared between sessions.
type HostSelectionPolicy interface {
HostStateNotifier
SetPartitioner
// Experimental, this interface and use may change
SetTablets
KeyspaceChanged(KeyspaceUpdateEvent)
Init(*Session)
// Reset is opprotunity to reset HostSelectionPolicy if Session initilization failed and we want to
// call HostSelectionPolicy.Init() again with new Session
Reset()
IsLocal(host *HostInfo) bool
// Pick returns an iteration function over selected hosts.
// Multiple attempts of a single query execution won't call the returned NextHost function concurrently,
// so it's safe to have internal state without additional synchronization as long as every call to Pick returns
// a different instance of NextHost.
Pick(ExecutableQuery) NextHost
// IsOperational checks if host policy can properly work with given Session/Cluster/ClusterConfig
IsOperational(*Session) error
}
// SelectedHost is an interface returned when picking a host from a host
// selection policy.
type SelectedHost interface {
Info() *HostInfo
Token() Token
Mark(error)
}
type selectedHost struct {
info *HostInfo
token Token
}
func (host selectedHost) Info() *HostInfo {
return host.info
}
func (host selectedHost) Token() Token {
return host.token
}
func (host selectedHost) Mark(err error) {}
// NextHost is an iteration function over picked hosts
type NextHost func() SelectedHost
// RoundRobinHostPolicy is a round-robin load balancing policy, where each host
// is tried sequentially for each query.
func RoundRobinHostPolicy() HostSelectionPolicy {
return &roundRobinHostPolicy{}
}
type roundRobinHostPolicy struct {
hosts cowHostList
lastUsedHostIdx uint64
}
func (r *roundRobinHostPolicy) IsLocal(*HostInfo) bool { return true }
func (r *roundRobinHostPolicy) KeyspaceChanged(KeyspaceUpdateEvent) {}
func (r *roundRobinHostPolicy) SetPartitioner(partitioner string) {}
func (r *roundRobinHostPolicy) Init(*Session) {}
func (r *roundRobinHostPolicy) Reset() {}
func (r *roundRobinHostPolicy) IsOperational(*Session) error { return nil }
// Experimental, this interface and use may change
func (r *roundRobinHostPolicy) SetTablets(tablets []*TabletInfo) {}
func (r *roundRobinHostPolicy) Pick(qry ExecutableQuery) NextHost {
nextStartOffset := atomic.AddUint64(&r.lastUsedHostIdx, 1)
return roundRobbin(int(nextStartOffset), r.hosts.get())
}
func (r *roundRobinHostPolicy) AddHost(host *HostInfo) {
r.hosts.add(host)
}
func (r *roundRobinHostPolicy) RemoveHost(host *HostInfo) {
r.hosts.remove(host)
}
func (r *roundRobinHostPolicy) HostUp(host *HostInfo) {
r.AddHost(host)
}
func (r *roundRobinHostPolicy) HostDown(host *HostInfo) {
r.RemoveHost(host)
}
func ShuffleReplicas() func(*tokenAwareHostPolicy) {
return func(t *tokenAwareHostPolicy) {
t.shuffleReplicas = true
}
}
// AvoidSlowReplicas enabled avoiding slow replicas
//
// TokenAwareHostPolicy normally does not check how busy replica is, with avoidSlowReplicas enabled it avoids replicas
// if they have equal or more than MAX_IN_FLIGHT_THRESHOLD requests in flight
func AvoidSlowReplicas(max_in_flight_threshold int) func(policy *tokenAwareHostPolicy) {
return func(t *tokenAwareHostPolicy) {
t.avoidSlowReplicas = true
MAX_IN_FLIGHT_THRESHOLD = max_in_flight_threshold
}
}
// NonLocalReplicasFallback enables fallback to replicas that are not considered local.
//
// TokenAwareHostPolicy used with DCAwareHostPolicy fallback first selects replicas by partition key in local DC, then
// falls back to other nodes in the local DC. Enabling NonLocalReplicasFallback causes TokenAwareHostPolicy
// to first select replicas by partition key in local DC, then replicas by partition key in remote DCs and fall back
// to other nodes in local DC.
func NonLocalReplicasFallback() func(policy *tokenAwareHostPolicy) {
return func(t *tokenAwareHostPolicy) {
t.nonLocalReplicasFallback = true
}
}
// TokenAwareHostPolicy is a token aware host selection policy, where hosts are
// selected based on the partition key, so queries are sent to the host which
// owns the partition. Fallback is used when routing information is not available.
func TokenAwareHostPolicy(fallback HostSelectionPolicy, opts ...func(*tokenAwareHostPolicy)) HostSelectionPolicy {
p := &tokenAwareHostPolicy{fallback: fallback}
for _, opt := range opts {
opt(p)
}
return p
}
// clusterMeta holds metadata about cluster topology.
// It is used inside atomic.Value and shallow copies are used when replacing it,
// so fields should not be modified in-place. Instead, to modify a field a copy of the field should be made
// and the pointer in clusterMeta updated to point to the new value.
type clusterMeta struct {
// replicas is map[keyspace]map[token]hosts
replicas map[string]tokenRingReplicas
tokenRing *tokenRing
}
var MAX_IN_FLIGHT_THRESHOLD int = 10
type tokenAwareHostPolicy struct {
fallback HostSelectionPolicy
getKeyspaceMetadata func(keyspace string) (*KeyspaceMetadata, error)
getKeyspaceName func() string
shuffleReplicas bool
nonLocalReplicasFallback bool
// mu protects writes to hosts, partitioner, metadata.
// reads can be unlocked as long as they are not used for updating state later.
mu sync.Mutex
hosts cowHostList
partitioner string
metadata atomic.Value // *clusterMeta
logger StdLogger
// Experimental, this interface and use may change
tablets cowTabletList
avoidSlowReplicas bool
}
func (t *tokenAwareHostPolicy) Init(s *Session) {
t.mu.Lock()
defer t.mu.Unlock()
if t.getKeyspaceMetadata != nil {
// Init was already called.
// See https://github.com/scylladb/gocql/issues/94.
panic("sharing token aware host selection policy between sessions is not supported")
}
t.getKeyspaceMetadata = s.KeyspaceMetadata
t.getKeyspaceName = func() string { return s.cfg.Keyspace }
t.logger = s.logger
}
func (t *tokenAwareHostPolicy) Reset() {
t.mu.Lock()
defer t.mu.Unlock()
// Sharing token aware host selection policy between sessions is not supported
// but session initialization can failed for some reasons. So in our application
// may be we want to create new session again.
// Reset method should be called in Session.Close method
t.getKeyspaceMetadata = nil
t.getKeyspaceName = nil
t.logger = nil
}
func (t *tokenAwareHostPolicy) IsOperational(session *Session) error {
return t.fallback.IsOperational(session)
}
func (t *tokenAwareHostPolicy) IsLocal(host *HostInfo) bool {
return t.fallback.IsLocal(host)
}
func (t *tokenAwareHostPolicy) KeyspaceChanged(update KeyspaceUpdateEvent) {
t.mu.Lock()
defer t.mu.Unlock()
meta := t.getMetadataForUpdate()
t.updateReplicas(meta, update.Keyspace)
t.metadata.Store(meta)
}
// updateReplicas updates replicas in clusterMeta.
// It must be called with t.mu mutex locked.
// meta must not be nil and it's replicas field will be updated.
func (t *tokenAwareHostPolicy) updateReplicas(meta *clusterMeta, keyspace string) {
newReplicas := make(map[string]tokenRingReplicas, len(meta.replicas))
ks, err := t.getKeyspaceMetadata(keyspace)
if err == nil {
strat := getStrategy(ks, t.logger)
if strat != nil {
if meta != nil && meta.tokenRing != nil {
newReplicas[keyspace] = strat.replicaMap(meta.tokenRing)
}
}
}
for ks, replicas := range meta.replicas {
if ks != keyspace {
newReplicas[ks] = replicas
}
}
meta.replicas = newReplicas
}
func (t *tokenAwareHostPolicy) SetPartitioner(partitioner string) {
t.mu.Lock()
defer t.mu.Unlock()
if t.partitioner != partitioner {
t.fallback.SetPartitioner(partitioner)
t.partitioner = partitioner
meta := t.getMetadataForUpdate()
meta.resetTokenRing(t.partitioner, t.hosts.get(), t.logger)
t.updateReplicas(meta, t.getKeyspaceName())
t.metadata.Store(meta)
}
}
// Experimental, this interface and use may change
func (t *tokenAwareHostPolicy) SetTablets(tablets []*TabletInfo) {
t.mu.Lock()
defer t.mu.Unlock()
t.tablets.set(tablets)
}
func (t *tokenAwareHostPolicy) AddHost(host *HostInfo) {
t.mu.Lock()
if t.hosts.add(host) {
meta := t.getMetadataForUpdate()
meta.resetTokenRing(t.partitioner, t.hosts.get(), t.logger)
t.updateReplicas(meta, t.getKeyspaceName())
t.metadata.Store(meta)
}
t.mu.Unlock()
t.fallback.AddHost(host)
}
func (t *tokenAwareHostPolicy) AddHosts(hosts []*HostInfo) {
t.mu.Lock()
for _, host := range hosts {
t.hosts.add(host)
}
meta := t.getMetadataForUpdate()
meta.resetTokenRing(t.partitioner, t.hosts.get(), t.logger)
t.updateReplicas(meta, t.getKeyspaceName())
t.metadata.Store(meta)
t.mu.Unlock()
for _, host := range hosts {
t.fallback.AddHost(host)
}
}
func (t *tokenAwareHostPolicy) RemoveHost(host *HostInfo) {
t.mu.Lock()
if t.hosts.remove(host) {
meta := t.getMetadataForUpdate()
meta.resetTokenRing(t.partitioner, t.hosts.get(), t.logger)
t.updateReplicas(meta, t.getKeyspaceName())
t.metadata.Store(meta)
}
t.mu.Unlock()
t.fallback.RemoveHost(host)
}
func (t *tokenAwareHostPolicy) HostUp(host *HostInfo) {
t.fallback.HostUp(host)
}
func (t *tokenAwareHostPolicy) HostDown(host *HostInfo) {
t.fallback.HostDown(host)
}
// getMetadataReadOnly returns current cluster metadata.
// Metadata uses copy on write, so the returned value should be only used for reading.
// To obtain a copy that could be updated, use getMetadataForUpdate instead.
func (t *tokenAwareHostPolicy) getMetadataReadOnly() *clusterMeta {
meta, _ := t.metadata.Load().(*clusterMeta)
return meta
}
// getMetadataForUpdate returns clusterMeta suitable for updating.
// It is a SHALLOW copy of current metadata in case it was already set or new empty clusterMeta otherwise.
// This function should be called with t.mu mutex locked and the mutex should not be released before
// storing the new metadata.
func (t *tokenAwareHostPolicy) getMetadataForUpdate() *clusterMeta {
metaReadOnly := t.getMetadataReadOnly()
meta := new(clusterMeta)
if metaReadOnly != nil {
*meta = *metaReadOnly
}
return meta
}
// resetTokenRing creates a new tokenRing.
// It must be called with t.mu locked.
func (m *clusterMeta) resetTokenRing(partitioner string, hosts []*HostInfo, logger StdLogger) {
if partitioner == "" {
// partitioner not yet set
return
}
// create a new token ring
tokenRing, err := newTokenRing(partitioner, hosts)
if err != nil {
logger.Printf("Unable to update the token ring due to error: %s", err)
return
}
// replace the token ring
m.tokenRing = tokenRing
}
func (t *tokenAwareHostPolicy) Pick(qry ExecutableQuery) NextHost {
if qry == nil {
return t.fallback.Pick(qry)
}
routingKey, err := qry.GetRoutingKey()
if err != nil {
return t.fallback.Pick(qry)
} else if routingKey == nil {
return t.fallback.Pick(qry)
}
meta := t.getMetadataReadOnly()
if meta == nil || meta.tokenRing == nil {
return t.fallback.Pick(qry)
}
partitioner := qry.GetCustomPartitioner()
if partitioner == nil {
partitioner = meta.tokenRing.partitioner
}
token := partitioner.Hash(routingKey)
var replicas []*HostInfo
if qry.GetSession() != nil && qry.GetSession().tabletsRoutingV1 {
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)
hosts := t.hosts.get()
for _, replica := range tablet.Replicas() {
for _, host := range hosts {
if host.hostId == replica.hostId.String() {
replicas = append(replicas, host)
break
}
}
}
}
}
if len(replicas) == 0 {
ht := meta.replicas[qry.Keyspace()].replicasFor(token)
if ht != nil {
replicas = ht.hosts
}
}
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))
for _, h := range replicas {
if h.IsBusy(s) {
unhealthyReplicas = append(unhealthyReplicas, h)
} else {
healthyReplicas = append(healthyReplicas, h)
}
}
replicas = append(healthyReplicas, unhealthyReplicas...)
}
var (
fallbackIter NextHost
i, j, k int
remote [][]*HostInfo
tierer HostTierer
tiererOk bool
maxTier uint
)
if tierer, tiererOk = t.fallback.(HostTierer); tiererOk {
maxTier = tierer.MaxHostTier()
} else {
maxTier = 1
}
if t.nonLocalReplicasFallback {
remote = make([][]*HostInfo, maxTier)
}
used := make(map[*HostInfo]bool, len(replicas))
return func() SelectedHost {
for i < len(replicas) {
h := replicas[i]
i++
var tier uint
if tiererOk {
tier = tierer.HostTier(h)
} else if t.fallback.IsLocal(h) {
tier = 0
} else {
tier = 1
}
if tier != 0 {
if t.nonLocalReplicasFallback {
remote[tier-1] = append(remote[tier-1], h)
}
continue
}
if h.IsUp() {
used[h] = true
return selectedHost{info: h, token: token}
}
}
if t.nonLocalReplicasFallback {
for j < len(remote) && k < len(remote[j]) {
h := remote[j][k]
k++
if k >= len(remote[j]) {
j++
k = 0
}
if h.IsUp() {
used[h] = true
return selectedHost{info: h, token: token}
}
}
}
if fallbackIter == nil {
// fallback
fallbackIter = t.fallback.Pick(qry)
}
// filter the token aware selected hosts from the fallback hosts
for fallbackHost := fallbackIter(); fallbackHost != nil; fallbackHost = fallbackIter() {
if !used[fallbackHost.Info()] {
used[fallbackHost.Info()] = true
return fallbackHost
}
}
return nil
}
}
// HostPoolHostPolicy is a host policy which uses the bitly/go-hostpool library
// to distribute queries between hosts and prevent sending queries to
// unresponsive hosts. When creating the host pool that is passed to the policy
// use an empty slice of hosts as the hostpool will be populated later by gocql.
// See below for examples of usage:
//
// // Create host selection policy using a simple host pool
// cluster.PoolConfig.HostSelectionPolicy = HostPoolHostPolicy(hostpool.New(nil))
//
// // Create host selection policy using an epsilon greedy pool
// cluster.PoolConfig.HostSelectionPolicy = HostPoolHostPolicy(
// hostpool.NewEpsilonGreedy(nil, 0, &hostpool.LinearEpsilonValueCalculator{}),
// )
func HostPoolHostPolicy(hp hostpool.HostPool) HostSelectionPolicy {
return &hostPoolHostPolicy{hostMap: map[string]*HostInfo{}, hp: hp}
}
type hostPoolHostPolicy struct {
hp hostpool.HostPool
mu sync.RWMutex
hostMap map[string]*HostInfo
}
func (r *hostPoolHostPolicy) Init(*Session) {}
func (r *hostPoolHostPolicy) Reset() {}
func (r *hostPoolHostPolicy) IsOperational(*Session) error { return nil }
func (r *hostPoolHostPolicy) KeyspaceChanged(KeyspaceUpdateEvent) {}
func (r *hostPoolHostPolicy) SetPartitioner(string) {}
func (r *hostPoolHostPolicy) IsLocal(*HostInfo) bool { return true }
// Experimental, this interface and use may change
func (r *hostPoolHostPolicy) SetTablets(tablets []*TabletInfo) {}
func (r *hostPoolHostPolicy) SetHosts(hosts []*HostInfo) {
peers := make([]string, len(hosts))
hostMap := make(map[string]*HostInfo, len(hosts))
for i, host := range hosts {
ip := host.ConnectAddress().String()
peers[i] = ip
hostMap[ip] = host
}
r.mu.Lock()
r.hp.SetHosts(peers)
r.hostMap = hostMap
r.mu.Unlock()
}
func (r *hostPoolHostPolicy) AddHost(host *HostInfo) {
ip := host.ConnectAddress().String()
r.mu.Lock()
defer r.mu.Unlock()
// If the host addr is present and isn't nil return
if h, ok := r.hostMap[ip]; ok && h != nil {
return
}
// otherwise, add the host to the map
r.hostMap[ip] = host
// and construct a new peer list to give to the HostPool
hosts := make([]string, 0, len(r.hostMap))
for addr := range r.hostMap {
hosts = append(hosts, addr)
}
r.hp.SetHosts(hosts)
}
func (r *hostPoolHostPolicy) RemoveHost(host *HostInfo) {
ip := host.ConnectAddress().String()
r.mu.Lock()
defer r.mu.Unlock()
if _, ok := r.hostMap[ip]; !ok {
return
}
delete(r.hostMap, ip)
hosts := make([]string, 0, len(r.hostMap))
for _, host := range r.hostMap {
hosts = append(hosts, host.ConnectAddress().String())
}
r.hp.SetHosts(hosts)
}
func (r *hostPoolHostPolicy) HostUp(host *HostInfo) {
r.AddHost(host)
}
func (r *hostPoolHostPolicy) HostDown(host *HostInfo) {
r.RemoveHost(host)
}
func (r *hostPoolHostPolicy) Pick(qry ExecutableQuery) NextHost {
return func() SelectedHost {
r.mu.RLock()
defer r.mu.RUnlock()
if len(r.hostMap) == 0 {
return nil
}
hostR := r.hp.Get()
host, ok := r.hostMap[hostR.Host()]
if !ok {
return nil
}
return selectedHostPoolHost{
policy: r,
info: host,
hostR: hostR,
}
}
}
// selectedHostPoolHost is a host returned by the hostPoolHostPolicy and
// implements the SelectedHost interface
type selectedHostPoolHost struct {
policy *hostPoolHostPolicy
info *HostInfo
hostR hostpool.HostPoolResponse
}
func (host selectedHostPoolHost) Info() *HostInfo {
return host.info
}
func (host selectedHostPoolHost) Token() Token {
return nil
}
func (host selectedHostPoolHost) Mark(err error) {
ip := host.info.ConnectAddress().String()
host.policy.mu.RLock()
defer host.policy.mu.RUnlock()
if _, ok := host.policy.hostMap[ip]; !ok {
// host was removed between pick and mark
return
}
host.hostR.Mark(err)
}
type dcAwareRR struct {
local string
localHosts cowHostList
remoteHosts cowHostList
lastUsedHostIdx uint64
disableDCFailover bool
}
type dcFailoverDisabledPolicy interface {
setDCFailoverDisabled()
}