-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataCenterManager.cpp
More file actions
138 lines (125 loc) · 4.58 KB
/
dataCenterManager.cpp
File metadata and controls
138 lines (125 loc) · 4.58 KB
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
#include "dataCenterManager.h"
StatusType DataCenterManager::MergeDataCenters(int dataCenter1, int dataCenter2){
if(dataCenter1>this->num_of_DCs || dataCenter1<=0 || dataCenter2>this->num_of_DCs || dataCenter2<=0) return INVALID_INPUT;
try{
this->DCGroups.unionDCs(dataCenter1,dataCenter2);
return SUCCESS;
}
catch(std::bad_alloc& e){
return ALLOCATION_ERROR;
}
}
StatusType DataCenterManager::AddServer(int dataCenterID, int serverID){
if(dataCenterID>this->num_of_DCs || dataCenterID<=0 || serverID<=0) return INVALID_INPUT;
try{
this->servers_hash_table.add(serverID,dataCenterID);
this->num_of_servers++;
return SUCCESS;
}
catch(std::bad_alloc& e){
return ALLOCATION_ERROR;
}
catch(HashTable::ServerExsist& e){
return FAILURE;
}
catch(HashTable::ServerNotExsist& e){
return FAILURE;
}
}
StatusType DataCenterManager::RemoveServer(int serverID){
if(serverID<=0) return INVALID_INPUT;
try{
int server_traffic = this->servers_hash_table.getTraffic(serverID);
int server_DC_ID = this->servers_hash_table.getDCID(serverID);
Key key(serverID,server_traffic);
try{
this->servers_traffic.delete_element(key);
this->DCGroups.removeServer(server_DC_ID, serverID, server_traffic);
}
catch(Avl<Key,Server>::KeyNotFound& e){
}
this->servers_hash_table.remove(serverID);
this->num_of_servers--;
return SUCCESS;
}
catch(std::bad_alloc& e){
return ALLOCATION_ERROR;
}
catch(HashTable::ServerNotExsist& e){
return FAILURE;
}
}
StatusType DataCenterManager::SetTraffic(int serverID, int traffic){
if(serverID<=0 || traffic<0) return INVALID_INPUT;
try{
int server_traffic_old = this->servers_hash_table.getTraffic(serverID);
int server_DC_ID = this->servers_hash_table.getDCID(serverID);
Key key(serverID,server_traffic_old);
try{
this->servers_traffic.delete_element(key);
this->DCGroups.removeServer(server_DC_ID, serverID, server_traffic_old);
}
catch(Avl<Key,Server>::KeyNotFound& e){
}
this->servers_hash_table.setTraffic(serverID,traffic);
Key new_key(serverID,traffic);
this->servers_traffic.insert(new_key,this->servers_hash_table.getServer(serverID),traffic);
this->DCGroups.addServer(server_DC_ID,serverID,traffic,this->servers_hash_table.getServer(serverID));
return SUCCESS;
}
catch(std::bad_alloc& e){
return ALLOCATION_ERROR;
}
catch(HashTable::ServerNotExsist& e){
return FAILURE;
}
}
int sumTraffic(const std::shared_ptr<Node<Key,Server>>& root, int k, int sum_traffic){
int right_count;
if(root->getRight() == nullptr){
right_count = 0;
}
else{
right_count = root->getRight()->getNodeCount();
}
// then your k from right(max)
if(right_count == k-1){
if(root->getRight() != nullptr) return root->getRight()->getTraffic() + root->getSelfTraffic() + sum_traffic;
else return root->getSelfTraffic() + sum_traffic;
}
// search right
if(right_count > k-1){
return sumTraffic(root->getRight(),k, sum_traffic);
}
// search left and increase sum_traffic of bigger servers by traffic.
// if(right_count < k-1 && root->getLeft() != nullptr)
else{
if(root->getRight() != nullptr) sum_traffic += root->getRight()->getTraffic() + root->getSelfTraffic();
else sum_traffic += root->getSelfTraffic();
if(root->getLeft() == nullptr) return sum_traffic;
else return sumTraffic(root->getLeft(),k - right_count -1, sum_traffic);
}
}
StatusType DataCenterManager::SumHighestTrafficServers(int dataCenterID, int k, int* traffic){
if(k < 0 || dataCenterID > this->num_of_DCs || dataCenterID < 0 || traffic == nullptr) return INVALID_INPUT;
if(k==0){
*traffic = 0;
return SUCCESS;
}
try{
if(dataCenterID !=0){
std::shared_ptr<DataCenterGroup> DC_group = this->DCGroups.findDCGroup(dataCenterID);
std::shared_ptr<Node<Key,Server>> root = DC_group->getTrafficRankTree()->getHead();
if(root == nullptr) *traffic = 0;
else *traffic = sumTraffic(root,k,0);
}
else{
if(this->servers_traffic.getHead() == nullptr) *traffic = 0;
else *traffic = sumTraffic(this->servers_traffic.getHead(),k,0);
}
return SUCCESS;
}
catch(std::bad_alloc& e){
return ALLOCATION_ERROR;
}
}