forked from datastax/cpp-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathload_balancing.hpp
128 lines (92 loc) · 3.45 KB
/
load_balancing.hpp
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
/*
Copyright (c) 2014-2016 DataStax
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.
*/
#ifndef __CASS_LOAD_BALANCING_HPP_INCLUDED__
#define __CASS_LOAD_BALANCING_HPP_INCLUDED__
#include "cassandra.h"
#include "constants.hpp"
#include "host.hpp"
#include "request.hpp"
#include <list>
#include <set>
#include <string>
#include <uv.h>
extern "C" {
typedef enum CassBalancingState_ {
CASS_BALANCING_INIT,
CASS_BALANCING_CLEANUP,
CASS_BALANCING_ON_UP,
CASS_BALANCING_ON_DOWN,
CASS_BALANCING_ON_ADD,
CASS_BALANCING_ON_REMOVE,
CASS_BALANCING_DISTANCE,
CASS_BALANCING_NEW_QUERY_PLAN
} CassBalancingState;
typedef enum CassHostDistance_ {
CASS_HOST_DISTANCE_LOCAL,
CASS_HOST_DISTANCE_REMOTE,
CASS_HOST_DISTANCE_IGNORE
} CassHostDistance;
} // extern "C"
namespace cass {
class Random;
class RoutableRequest;
class TokenMap;
inline bool is_dc_local(CassConsistency cl) {
return cl == CASS_CONSISTENCY_LOCAL_ONE || cl == CASS_CONSISTENCY_LOCAL_QUORUM;
}
class QueryPlan {
public:
virtual ~QueryPlan() {}
virtual SharedRefPtr<Host> compute_next() = 0;
bool compute_next(Address* address) {
SharedRefPtr<Host> host = compute_next();
if (host) {
*address = host->address();
return true;
}
return false;
}
};
class LoadBalancingPolicy : public Host::StateListener, public RefCounted<LoadBalancingPolicy> {
public:
LoadBalancingPolicy()
: RefCounted<LoadBalancingPolicy>() {}
virtual ~LoadBalancingPolicy() {}
virtual void init(const SharedRefPtr<Host>& connected_host, const HostMap& hosts, Random* random) = 0;
virtual void register_handles(uv_loop_t* loop) {}
virtual void close_handles() {}
virtual CassHostDistance distance(const SharedRefPtr<Host>& host) const = 0;
virtual QueryPlan* new_query_plan(const std::string& connected_keyspace,
const Request* request,
const TokenMap* token_map,
Request::EncodingCache* cache) = 0;
virtual LoadBalancingPolicy* new_instance() = 0;
};
class ChainedLoadBalancingPolicy : public LoadBalancingPolicy {
public:
ChainedLoadBalancingPolicy(LoadBalancingPolicy* child_policy)
: child_policy_(child_policy) {}
virtual ~ChainedLoadBalancingPolicy() {}
virtual void init(const SharedRefPtr<Host>& connected_host, const HostMap& hosts, Random* random) {
return child_policy_->init(connected_host, hosts, random);
}
virtual CassHostDistance distance(const SharedRefPtr<Host>& host) const { return child_policy_->distance(host); }
virtual void on_add(const SharedRefPtr<Host>& host) { child_policy_->on_add(host); }
virtual void on_remove(const SharedRefPtr<Host>& host) { child_policy_->on_remove(host); }
virtual void on_up(const SharedRefPtr<Host>& host) { child_policy_->on_up(host); }
virtual void on_down(const SharedRefPtr<Host>& host) { child_policy_->on_down(host); }
protected:
ScopedRefPtr<LoadBalancingPolicy> child_policy_;
};
} // namespace cass
#endif