/
loadbalancer.cc
324 lines (269 loc) · 9.99 KB
/
loadbalancer.cc
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
/*
* Copyright (c) 2014 Juniper Networks, Inc. All rights reserved.
*/
#include "loadbalancer.h"
#include "ifmap/ifmap_node.h"
#include "oper/ifmap_dependency_manager.h"
#include "loadbalancer_properties.h"
#include <oper/agent_sandesh.h>
#include <oper/agent_types.h>
#include <cfg/cfg_listener.h>
class LoadbalancerData : public AgentData {
public:
typedef LoadbalancerProperties Properties;
LoadbalancerData(IFMapNode *node)
: node_(node) {
}
LoadbalancerData(const Properties &properties)
: node_(NULL), properties_(properties) {
}
IFMapNode *node() { return node_; }
const Properties &properties() { return properties_; }
private:
IFMapNode *node_;
Properties properties_;
};
Loadbalancer::Loadbalancer() {
}
Loadbalancer::~Loadbalancer() {
}
bool Loadbalancer::IsLess(const DBEntry &rhs) const {
const Loadbalancer &lb = static_cast<const Loadbalancer &>(rhs);
return uuid_ < lb.uuid_;
}
std::string Loadbalancer::ToString() const {
return UuidToString(uuid_);
}
void Loadbalancer::SetKey(const DBRequestKey *key) {
const LoadbalancerKey *lbkey = static_cast<const LoadbalancerKey *>(key);
uuid_ = lbkey->instance_id();
}
DBEntryBase::KeyPtr Loadbalancer::GetDBRequestKey() const {
LoadbalancerKey *key = new LoadbalancerKey(uuid_);
return KeyPtr(key);
}
void Loadbalancer::set_properties(const Properties &properties) {
properties_.reset(new Properties(properties));
}
const Loadbalancer::Properties *Loadbalancer::properties() const {
return properties_.get();
}
static boost::uuids::uuid IdPermsGetUuid(const autogen::IdPermsType &id) {
boost::uuids::uuid uuid;
CfgUuidSet(id.uuid.uuid_mslong, id.uuid.uuid_lslong, uuid);
return uuid;
}
static void PropertiesAddMember(
IFMapNode *node, LoadbalancerProperties *properties) {
autogen::LoadbalancerMember *member =
static_cast<autogen::LoadbalancerMember *>(node->GetObject());
properties->members()->insert(
std::make_pair(IdPermsGetUuid(member->id_perms()),
member->properties()));
}
static void PropertiesAddHealthmonitor(
IFMapNode *node, LoadbalancerProperties *properties) {
autogen::LoadbalancerHealthmonitor *healthmon =
static_cast<autogen::LoadbalancerHealthmonitor *>(
node->GetObject());
properties->healthmonitors()->insert(
std::make_pair(IdPermsGetUuid(healthmon->id_perms()),
healthmon->properties()));
}
void Loadbalancer::CalculateProperties(DBGraph *graph, Properties *properties) {
autogen::LoadbalancerPool *pool =
static_cast<autogen::LoadbalancerPool *>(node_->GetObject());
properties->set_pool_properties(pool->properties());
properties->set_custom_attributes(pool->custom_attributes());
for (DBGraphVertex::adjacency_iterator iter = node_->begin(graph);
iter != node_->end(graph); ++iter) {
IFMapNode *adj = static_cast<IFMapNode *>(iter.operator->());
const char *adjtype = adj->table()->Typename();
if (strcmp(adjtype, "virtual-ip") == 0) {
autogen::VirtualIp *vip =
static_cast<autogen::VirtualIp *>(adj->GetObject());
properties->set_vip_uuid(IdPermsGetUuid(vip->id_perms()));
properties->set_vip_properties(vip->properties());
} else if (strcmp(adjtype, "loadbalancer-member") == 0) {
PropertiesAddMember(adj, properties);
} else if (strcmp(adjtype, "loadbalancer-healthmonitor") == 0) {
PropertiesAddHealthmonitor(adj, properties);
}
}
}
bool Loadbalancer::DBEntrySandesh(Sandesh *sresp, std::string &name) const {
LoadBalancerResp *resp = static_cast<LoadBalancerResp*> (sresp);
std::string str_uuid = UuidToString(uuid_);
if (! name.empty() && str_uuid != name) {
return false;
}
LoadBalancerSandeshData data;
data.set_uuid(str_uuid);
const autogen::VirtualIpType &vip =
properties_.get()->vip_properties();
data.set_vip_address(vip.address);
data.set_port(vip.protocol_port);
const autogen::LoadbalancerPoolType &pool = properties()->pool_properties();
data.set_mode(pool.protocol);
data.set_balance(pool.loadbalancer_method);
if (properties()->healthmonitors().size()) {
const autogen::LoadbalancerHealthmonitorType &hm =
properties()->healthmonitors().begin()->second;
data.set_inter(hm.timeout * 1000);
data.set_fall(hm.max_retries);
data.set_rise(1);
if (!hm.expected_codes.empty()) {
data.set_expected_codes(hm.expected_codes);
if (!hm.http_method.empty()) {
data.set_http_method(hm.http_method);
}
if (!hm.url_path.empty()) {
data.set_url_path(hm.url_path);
}
}
}
std::vector<std::string> members;
for (LoadbalancerProperties::MemberMap::const_iterator iter =
properties()->members().begin();
iter != properties()->members().end(); ++iter) {
autogen::LoadbalancerMemberType member = iter->second;
members.push_back(member.address);
}
data.set_member_list(members);
std::vector<LoadBalancerSandeshData> &list =
const_cast<std::vector<LoadBalancerSandeshData>&>
(resp->get_load_balancer_list());
list.push_back(data);
return true;
}
void LoadBalancerReq::HandleRequest() const {
AgentSandeshPtr sand(new AgentLoadBalancerSandesh(context(),get_uuid()));
sand->DoSandesh(sand);
}
AgentSandeshPtr LoadbalancerTable::GetAgentSandesh
(const AgentSandeshArguments *args, const std::string &context) {
return AgentSandeshPtr
(new AgentLoadBalancerSandesh(context, args->GetString("name")));
}
/*
* LoadbalancerTable class
*/
LoadbalancerTable::LoadbalancerTable(DB *db, const std::string &name)
: AgentDBTable(db, name),
graph_(NULL), dependency_manager_(NULL) {
}
LoadbalancerTable::~LoadbalancerTable() {
}
#if 0
void LoadbalancerTable::Clear() {
DBEntry *db, *db_next;
DBTablePartition *partition = static_cast<DBTablePartition *>(
GetTablePartition(0));
for (db = partition->GetFirst(); db!= NULL; db = db_next) {
db_next = partition->GetNext(db);
if (db->IsDeleted()) {
continue;
}
partition->Delete(db);
}
}
#endif
std::auto_ptr<DBEntry> LoadbalancerTable::AllocEntry(
const DBRequestKey *key) const {
std::auto_ptr<DBEntry> entry(new Loadbalancer());
entry->SetKey(key);
return entry;
}
DBEntry *LoadbalancerTable::Add(const DBRequest *request) {
Loadbalancer *loadbalancer = new Loadbalancer();
loadbalancer->SetKey(request->key.get());
LoadbalancerData *data =
static_cast<LoadbalancerData *>(request->data.get());
loadbalancer->set_node(data->node());
assert(dependency_manager_);
loadbalancer->SetIFMapNodeState(
dependency_manager_->SetState(data->node()));
dependency_manager_->SetObject(data->node(), loadbalancer);
return loadbalancer;
}
bool LoadbalancerTable::Delete(DBEntry *entry, const DBRequest *request) {
Loadbalancer *loadbalancer = static_cast<Loadbalancer *>(entry);
assert(dependency_manager_);
dependency_manager_->SetObject(loadbalancer->node(), NULL);
loadbalancer->SetIFMapNodeState(NULL);
return true;
}
bool LoadbalancerTable::OnChange(DBEntry *entry, const DBRequest *request) {
Loadbalancer *loadbalancer = static_cast<Loadbalancer *>(entry);
LoadbalancerData *data = static_cast<LoadbalancerData *>(
request->data.get());
if (data->node() == NULL) {
loadbalancer->set_properties(data->properties());
return true;
}
return false;
}
void LoadbalancerTable::Initialize(
DBGraph *graph, IFMapDependencyManager *dependency_manager) {
graph_ = graph;
dependency_manager_ = dependency_manager;
dependency_manager_->Register(
"loadbalancer-pool",
boost::bind(&LoadbalancerTable::ChangeEventHandler, this, _1, _2));
}
bool LoadbalancerTable::IFNodeToUuid(IFMapNode *node, boost::uuids::uuid &u) {
autogen::LoadbalancerPool *pool =
static_cast<autogen::LoadbalancerPool *>(node->GetObject());
const autogen::IdPermsType &id = pool->id_perms();
u = IdPermsGetUuid(id);
return true;
}
bool LoadbalancerTable::IFNodeToReq(IFMapNode *node, DBRequest &request) {
boost::uuids::uuid id;
if (agent() && agent()->cfg_listener()) {
agent()->cfg_listener()->GetCfgDBStateUuid(node, id);
} else {
IFNodeToUuid(node, id);
}
if (boost::uuids::nil_uuid() == id)
return false;
request.key.reset(new LoadbalancerKey(id));
if (!node->IsDeleted()) {
request.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
request.data.reset(new LoadbalancerData(node));
} else {
request.oper = DBRequest::DB_ENTRY_DELETE;
}
return true;
}
void LoadbalancerTable::ChangeEventHandler(IFMapNode *node, DBEntry *entry) {
if (entry == NULL)
return;
Loadbalancer *loadbalancer = static_cast<Loadbalancer *>(entry);
/*
* Do not enqueue an ADD_CHANGE operation after the DELETE generated
* by IFNodeToReq.
*/
if (loadbalancer->node()->IsDeleted()) {
return;
}
assert(graph_);
LoadbalancerProperties properties;
loadbalancer->CalculateProperties(graph_, &properties);
const LoadbalancerProperties *current = loadbalancer->properties();
if (current == NULL || properties.CompareTo(*current) != 0) {
LOG(DEBUG, "loadbalancer property change "
<< properties.DiffString(current));
DBRequest request;
request.oper = DBRequest::DB_ENTRY_ADD_CHANGE;
request.key = loadbalancer->GetDBRequestKey();
request.data.reset(new LoadbalancerData(properties));
Enqueue(&request);
}
}
DBTableBase *LoadbalancerTable::CreateTable(
DB *db, const std::string &name) {
LoadbalancerTable *table = new LoadbalancerTable(db, name);
table->Init();
return table;
}