/
policies.clj
108 lines (89 loc) · 3.51 KB
/
policies.clj
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
;; Copyright (c) 2012-2014 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team
;;
;; Licensed under the Apache License, Version 2.0 (the "License");
;; you may not use this file except in compliance with the License.
;; You may obtain a copy of the License at
;;
;; http://www.apache.org/licenses/LICENSE-2.0
;;
;; Unless required by applicable law or agreed to in writing, software
;; distributed under the License is distributed on an "AS IS" BASIS,
;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
;; See the License for the specific language governing permissions and
;; limitations under the License.
(ns clojurewerkz.cassaforte.policies
"Consistency levels, retry policies, reconnection policies, etc"
(:import [com.datastax.driver.core ConsistencyLevel]
[com.datastax.driver.core.policies
LoadBalancingPolicy DCAwareRoundRobinPolicy DCAwareRoundRobinPolicy$Builder RoundRobinPolicy TokenAwarePolicy
LoggingRetryPolicy DefaultRetryPolicy DowngradingConsistencyRetryPolicy FallthroughRetryPolicy
RetryPolicy ConstantReconnectionPolicy ExponentialReconnectionPolicy]))
;;
;; Load Balancing
;;
(defn round-robin-policy
"Round-robin load balancing policy. Picks nodes to execute requests on in order."
[]
(RoundRobinPolicy.))
(defn dc-aware-round-robin-policy
"Datacenter aware load balancing policy.
Like round-robin but over the nodes located in the same datacenter.
Nodes from other datacenters will be tried only if all requests to local nodes fail."
[^String local-dc]
(-> (DCAwareRoundRobinPolicy/builder)
(.withLocalDc local-dc)
(.build)))
(defn token-aware-policy
"Takes a load balancing policy and makes it token-aware"
[^LoadBalancingPolicy underlying-policy]
(TokenAwarePolicy. underlying-policy))
;;
;; Retries
;;
(def retry-policies {:default (constantly DefaultRetryPolicy/INSTANCE)
:downgrading-consistency (constantly DowngradingConsistencyRetryPolicy/INSTANCE)
:fallthrough (constantly FallthroughRetryPolicy/INSTANCE)})
(defn retry-policy
[rp]
((rp retry-policies)))
(defn logging-retry-policy
"A retry policy that wraps another policy, logging the decision made by its sub-policy."
[^RetryPolicy policy]
(LoggingRetryPolicy. policy))
;;
;; Reconnection
;;
(defn exponential-reconnection-policy
"Reconnection policy that waits exponentially longer between each
reconnection attempt but keeps a constant delay once a maximum delay is reached.
Delays should be given in milliseconds"
[base-delay-ms max-delay-ms]
(ExponentialReconnectionPolicy. base-delay-ms max-delay-ms))
(defn constant-reconnection-policy
"Reconnection policy that waits constantly longer between each
reconnection attempt but keeps a constant delay once a maximum delay is
reached.
Delay should be given in milliseconds"
[delay-ms]
(ConstantReconnectionPolicy. delay-ms))
;;
;; Consistency Level
;;
(def consistency-levels
{:any ConsistencyLevel/ANY
:one ConsistencyLevel/ONE
:two ConsistencyLevel/TWO
:three ConsistencyLevel/THREE
:quorum ConsistencyLevel/QUORUM
:all ConsistencyLevel/ALL
:serial ConsistencyLevel/SERIAL
:local-quorum ConsistencyLevel/LOCAL_QUORUM
:each-quorum ConsistencyLevel/EACH_QUORUM})
(defn consistency-level
[cl]
(get consistency-levels cl))
(defn resolve-consistency-level
[cl]
(if (= (type cl) ConsistencyLevel)
cl
(consistency-level cl)))