forked from ekaynar/cache-sim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataCenter.py
257 lines (217 loc) · 8.4 KB
/
dataCenter.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
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
from uhashring import HashRing
from cache import Cache
from directory import Directory
import simpy, re, random, threading
#import pandas as pd
from collections import deque
from stats import JobStat
from timer import Timer
class Mapper:
def __init__(self, name):
self.name = name
self.queue = deque()
self.outstanding_req = 0
self.outstanding_task = []
def setUp(hashType,nodeNum):
nodes={}
if (hashType == "consistent"):
for i in range(int(nodeNum)):
cname='cache'+str(i)
nodes[cname]={'hostname':cname, 'weight': 1}
return nodes
def consistentHashing(nodeNum):
nodes=setUp("consistent",nodeNum)
return HashRing(nodes,vnodes=200,hash_fn='ketama')
class DataCenter:
def __init__(self, name):
self.lock = threading.Lock()
self.name = name
self.config = None
self.cache_layer = {}
self.c_nodes = 0
self.links = {}
self.env = None
self.compute_nodes = 0
self.mapper_list = {}
self.cpu = 0
self.interval = 0
self.placement = None
self.logger = None
self.blk_dir = Directory('blk_dir')
#self.jobStat = JobStat()
self.hash_ring = None
self.nic_count = 0
self.scheduler = None
self.repType = 'rep'
self.repCount = 3
self.ec=[]
self.rep_size = 4
self.chunk_size = 0
self.osdMap = None
self.dl_access = 0
self.outstanding_req = {}
self.jobDoneCount = 0
self.printCount = 0
self.setKeys = set()
self.timer = Timer()
self.timer.start()
self.globalAge = 0
# self.outstanding_delete_op = {}
def build_directory(self):
print('Building Datacenter with block and object directory')
self.blk_dir.interval = self.interval
self.blk_dir.size = int(self.config.get('Directory', 'size'))
self.blk_dir.free_space = int(self.config.get('Directory', 'size'))
self.blk_dir.threshold = int(self.config.get('Directory', 'threshold'))
self.blk_dir.count = int(self.config.get('Directory', 'lru count'))
self.dict = {}
#col_names = ['blkname', 'c_time', 'size', 'location', 'gfreq', 'valid', 'la_time']
#self.blk_dir.df = pd.DataFrame(columns = col_names)
#self.blk_dir.df = self.blk_dir.df.set_index(['blkname'])
#col_names = ['objname', 'c_time', 'size', 'location', 'owner', 'gfreq', 'valid', 'la_time']
#self.blk_dir.obj_df = pd.DataFrame(columns = col_names)
#self.blk_dir.obj_df = self.blk_dir.obj_df.set_index(['objname'])
#print('Building osd mapping for write cache')
#col_names = ['blkname', 'osd_list', 'dirty']
#self.osdMap = pd.DataFrame(columns = col_names)
#self.osdMap = self.osdMap.set_index(['blkname'])
def build_worker_nodes(self):
for r in range(self.compute_nodes):
for c in range(self.cpu):
name = "map"+str(r)+"-"+str(c)
mapper = Mapper(name)
self.mapper_list[name] = mapper
def build_network(self, logger,env):
# nic0 -> client to cache server
# nic1 -> cross rach(between cache servers
# nic2 -> between backend and cache servers
links={}
nic_count = int(self.config.get('Network', 'nic count'))
self.nic_count = nic_count
#print("nic COUNt is %d" %nic_count)
coef = 1
speed_unit = self.config.get('Network', 'unit')
if speed_unit == 'Gbps':
coef = 1024/8 # based on MB
#coef = 1024*1024*1024
elif speed_unit == 'Mbps':
coef = 1
#coef = 1024*1024
elif speed_unit == 'Kbps':
coef = 1024/8
elif speed_unit == 'Bps':
coef = 1
for i in range(self.c_nodes):
for j in range (nic_count-1):
nic_id = "nic"+str(j)+" in"
link_id = "nic"+str(j)+".in."+str(i)
speed = float(self.config.get('Network', nic_id))*coef
#speed = float(self.config.get('Network', nic_id).split("G")[0])/8*1000*1000*1000
links[link_id]=simpy.Container(env, speed, init=speed)
nic_id = "nic"+str(j)+" out"
link_id = "nic"+str(j)+".out."+str(i)
speed = float(self.config.get('Network', nic_id))*coef
#speed = float(self.config.get('Network',nic_id).split("G")[0])/8*1000*1000*1000
links[link_id]=simpy.Container(env, speed, init=speed)
#DL link
#nic_id = "nic"+str(nic_count-1)+" in"
#link_id = "nic"+str(nic_count-1)+".in"
#speed = float(self.config.get('Network', nic_id).split("G")[0])/8*1000*1000*1000
nic_id = "dl"+" nic"+str(nic_count-1)+" in"
link_id = "nic"+str(nic_count-1)+".in"
speed = float(self.config.get('Network', nic_id))*coef
links[link_id]=simpy.Container(env, speed, init=speed)
#nic_id = "nic"+str(nic_count-1)+" out"
#link_id = "nic"+str(nic_count-1)+".out"
#speed = float(self.config.get('Network', nic_id).split("G")[0])/8*1000*1000*1000
nic_id = "dl"+ " nic"+str(nic_count-1)+" out"
link_id = "nic"+str(nic_count-1)+".out"
speed = float(self.config.get('Network', nic_id))*coef
links[link_id]=simpy.Container(env, speed, init=speed)
#print("LINKS ARE %s" %links)
return links
def build(self, config, logger, env):
self.config = config
self.c_nodes = int(config.get('Simulation', 'cache nodes'))
#print("CACHE NODE is %d" %self.c_nodes)
self.placement = config.get('Simulation', 'placement')
policy = config.get('Simulation', 'cache policy')
size = int(config.get('Simulation', 'cache capacity')) #in MB
self.interval = float(config.get('Simulation', 'aging interval'))
self.compute_nodes = int(config.get('Simulation', 'compute nodes'))
self.cpu = int(config.get('Simulation', 'cpu'))
self.chunk_size = int(config.get('Simulation', 'chunk size')) # in MB
self.mapper_size = int(config.get('Simulation', 'mapper size')) # in MB
self.logFile = config.get('Simulation', 'log file')
f = open(self.logFile, "w")
f.close()
self.logger = logger
if (self.placement == "consistent"):
self.hash_ring = consistentHashing(self.c_nodes)
elif (self.placement == "directory"):
self.build_directory()
# for i in range(4):
# self.obj_directory["file"+str(i)] = ["cache12"]
self.repType = config.get('Simulation', 'replication type')
if self.repType == 'rep':
self.repCount = int(config.get('Simulation', 'replication count'))
self.rep_size = self.chunk_size
elif self.repType == 'ec':
tmp = config.get('Simulation', 'replication count').split(',')
self.ec = [int(tmp[0]),int(tmp[1])]
self.rep_size = float (self.chunk_size / self.ec[0])
print("reptyep","repcount",self.repType, self.rep_size)
print ("Building Datacenter with ", self.placement)
logger.info('Building Datacenter with')
for i in range(self.c_nodes):
c_name = "cache"+str(i) #i is rack id
self.cache_layer[c_name]=Cache(c_name, size, policy, self.interval, self.setKeys)
#c_name = 'writeCache'
#self.cache_layer[c_name]=Cache(c_name, size, 'FIFO', self.interval)
#print("CACHE LAYER is %s" %self.cache_layer)
self.links = self.build_network(logger,env)
print ("Building compute nodes and mappers")
logger.info('Building compute nodes and mappers')
self.build_worker_nodes()
print ("Building scheduler")
logger.info('Building scheduler')
def consistent_hash(self,key):
return self.hash_ring.get_node(key)
def get_link_id(self, source, dest):
if source == "DL":
s = "nic"+str(self.nic_count-1)+".in"
return s, None
elif "map" in source:
result = re.search('map(.*)-', source)
rack = result.group(1)
#print("RACK **1** is %s" %rack)
s = "nic0.in."+str(rack)
return s, None
else:
rack = source.split("cache",1)[1]
s = "nic1.out."+str(rack)
if dest == "DL":
d = "nic"+str(self.nic_count-1)+".out"
return None, d
elif "map" in dest:
result = re.search('map(.*)-', dest)
rack = result.group(1)
d = "nic0.out."+str(rack)
return None, d
else:
rack = dest.split("cache",1)[1]
#print("RACK **2** is %s" %rack)
d = "nic1.in."+str(rack)
return s, d
def get_replica_loc(self, cache_id):
candidates = []
for i in range(self.c_nodes):
candidates.append('cache'+str(i))
candidates.remove(cache_id)
if self.repType == 'rep':
return random.sample(candidates, self.repCount-1)
elif self.repType == 'ec':
count = int(self.ec[0])+int(self.ec[1])
return random.sample(candidates, count-1)
def datalake_access(self):
self.dl_access += 1