-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsistent_test.py
217 lines (133 loc) · 4.26 KB
/
consistent_test.py
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
import ipaddress
import consistent as cs
import time
import logging
import random
import copy
def delete_route(ch: cs.ConsistentHash, route):
p = cs.Prefix()
p.set_prefix(route)
r = cs.Route(p, set())
ch.del_route(r)
def dump_ch(ch):
print("\n===\nConsistent ", ch)
# print("\nRoutes\n", ch.Routes)
print("\nDesiredContainers\n", ch.DesiredContainers)
print("\nActualContainers\n", ch.ActualContainers)
print("\nSDK\n", ch.SdkObject)
# print("\n", " ".join(str(s) for s in ch.Routes.prefixes()))
base_nh = "192.1.1.1"
base_net = "172.1.1.0"
next_hop_set = set()
prefix_set = set()
#test_dictionary = {}
test_route_list = []
test_full_route_list = []
def simulate_fat_tree_converge(ch: cs.ConsistentHash):
count = 0
for r in test_route_list:
ch.add_route(r)
count += 1
if count % 200 == 0:
print(".")
#time.sleep(1)
def generate_data(routes, next_hops):
global next_hop_set
global prefix_set
next_hop_set = set()
for i in range(next_hops):
addr = ipaddress.ip_address(base_nh) + random.randint(256, 256+10000)
nh = ipaddress.ip_address(addr)
next_hop_set.add(nh)
for i in range(routes):
addr = ipaddress.ip_address(base_net) + random.randint(256, 256+1000000)
route = ipaddress.ip_network(str(ipaddress.ip_address(addr)) + "/24",strict = False)
prefix_set.add(route)
def generate_full_route():
test_full_route_list = []
for prefix in prefix_set:
p = cs.Prefix()
p.set_prefix(prefix)
s = set()
for i in next_hop_set:
s.add(i)
r = cs.Route(p, s)
test_full_route_list.append(r)
def generate_fat_tree_converge():
test_dictionary = {}
print("prefixes {} nh={}".format(len(list(prefix_set)), len(list(next_hop_set))))
for prefix in prefix_set:
p_list = []
p_nh = copy.copy(next_hop_set)
p = cs.Prefix()
p.set_prefix(prefix)
s = set()
for i in range(len(next_hop_set)):
idx = random.randint(0, len(p_nh) - 1)
nh = list(p_nh)[idx]
s.add(nh)
p_nh.remove(nh)
r = cs.Route(p, s)
p_list.append(r)
test_dictionary[prefix] = p_list
# print(", ".join("{}: [{}] ".format(str(a),", ".join(str(c) for c in b)) for a,b in test_dictionary.items()))
while True:
if len(test_dictionary.keys()) == 0:
break
idx = random.randint(0, len(test_dictionary.keys()) - 1 )
key = list(test_dictionary.keys())[idx]
l = test_dictionary[key]
if len(l) == 0:
del test_dictionary[key]
continue
r = l[0]
test_route_list.append(r)
l.remove(r)
if len(l) == 0:
del test_dictionary[key]
# print(cs.pSet(test_route_list))
def generate_run(routes, next_hops):
print("Generating data...\n")
generate_data(routes, next_hops)
print("Generating fat tree converge")
generate_fat_tree_converge()
print("Generate full route list")
generate_full_route()
# ch = cs.ConsistentHash(debug_level = logging.DEBUG)
ch = cs.ConsistentHash()
ch.run()
ch.set_admin_state(True)
print("Simulate fat tree converge...\n")
simulate_fat_tree_converge(ch)
# dump_ch(ch)
time.sleep(10)
dump_ch(ch)
ch.stop()
def apply_routes(prefix_list, nh_list, ch: cs.ConsistentHash):
for prefix in prefix_list:
p = cs.Prefix()
p.set_prefix(prefix)
s = set()
for i in nh_list:
s.add(i)
r = cs.Route(p, s)
ch.add_route(r)
def small_test():
ch = cs.ConsistentHash()
ch.run()
ch.set_admin_state(True)
nh_list = ["10.0.1.1", "10.0.2.2"]
prefix_list = ["192.1.1.1/24", "192.2.2.2/24", "192.3.3.3/24"]
apply_routes(prefix_list, nh_list, ch)
dump_ch(ch)
nh_list = ["10.0.1.1"]
prefix_list = ["192.1.1.1/24", "192.2.2.2/24"]
apply_routes(prefix_list, nh_list, ch)
dump_ch(ch)
nh_list = ["10.0.1.1", "10.0.2.2"]
prefix_list = ["192.1.1.1/24"]
apply_routes(prefix_list, nh_list, ch)
dump_ch(ch)
ch.stop()
#generate_run(200,5)
small_test()