-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathMicroRCA.py
More file actions
338 lines (261 loc) · 11.1 KB
/
MicroRCA.py
File metadata and controls
338 lines (261 loc) · 11.1 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
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
325
326
327
328
329
330
331
332
333
334
335
336
337
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: li
"""
import pandas as pd
import numpy as np
import networkx as nx
import csv
from sklearn.cluster import Birch
from sklearn import preprocessing
smoothing_window = 12
# Anomaly Detection
def birch_ad_with_smoothing(latency_df, threshold):
# anomaly detection on response time of service invocation.
# input: response times of service invocations, threshold for birch clustering
# output: anomalous service invocation
anomalies = []
for svc, latency in latency_df.iteritems():
# No anomaly detection in db
if svc != 'timestamp' and 'Unnamed' not in svc and 'rabbitmq' not in svc and 'db' not in svc:
latency = latency.rolling(window=smoothing_window, min_periods=1).mean()
x = np.array(latency)
x = np.where(np.isnan(x), 0, x)
normalized_x = preprocessing.normalize([x])
X = normalized_x.reshape(-1,1)
# threshold = 0.05
brc = Birch(branching_factor=50, n_clusters=None, threshold=threshold, compute_labels=True)
brc.fit(X)
brc.predict(X)
labels = brc.labels_
# centroids = brc.subcluster_centers_
n_clusters = np.unique(labels).size
if n_clusters > 1:
anomalies.append(svc)
return anomalies
def rt_invocations(faults_name):
# retrieve the response time of each invocation from data collection
# input: prefix of the csv file
# output: round-trip time
latency_filename = faults_name + '_latency_source_50.csv' # inbound
latency_df_source = pd.read_csv(latency_filename)
latency_df_source['unknown_front-end'] = 0
latency_filename = faults_name + '_latency_destination_50.csv' # outbound
latency_df_destination = pd.read_csv(latency_filename)
latency_df = latency_df_destination.add(latency_df_source)
return latency_df
def attributed_graph(faults_name):
# build the attributed graph
# input: prefix of the file
# output: attributed graph
filename = faults_name + '_mpg.csv'
df = pd.read_csv(filename)
DG = nx.DiGraph()
for index, row in df.iterrows():
source = row['source']
destination = row['destination']
if 'rabbitmq' not in source and 'rabbitmq' not in destination and 'db' not in destination and 'db' not in source:
DG.add_edge(source, destination)
for node in DG.nodes():
if 'kubernetes' in node:
DG.nodes[node]['type'] = 'host'
else:
DG.nodes[node]['type'] = 'service'
#
# print(DG.nodes(data=True))
# plt.figure(figsize=(9,9))
# nx.draw(DG, with_labels=True, font_weight='bold')
# pos = nx.spring_layout(DG)
# nx.draw(DG, pos, with_labels=True, cmap = plt.get_cmap('jet'), node_size=1500, arrows=True, )
# labels = nx.get_edge_attributes(DG,'weight')
# nx.draw_networkx_edge_labels(DG,pos,edge_labels=labels)
# plt.show()
return DG
def node_weight(svc, anomaly_graph, baseline_df, faults_name):
#Get the average weight of the in_edges
in_edges_weight_avg = 0.0
num = 0
for u, v, data in anomaly_graph.in_edges(svc, data=True):
# print(u, v)
num = num + 1
in_edges_weight_avg = in_edges_weight_avg + data['weight']
if num > 0:
in_edges_weight_avg = in_edges_weight_avg / num
filename = faults_name + '_' + svc + '.csv'
df = pd.read_csv(filename)
node_cols = ['node_cpu', 'node_network', 'node_memory']
max_corr = 0.01
metric = 'node_cpu'
for col in node_cols:
temp = abs(baseline_df[svc].corr(df[col]))
if temp > max_corr:
max_corr = temp
metric = col
data = in_edges_weight_avg * max_corr
return data, metric
def svc_personalization(svc, anomaly_graph, baseline_df, faults_name):
filename = faults_name + '_' + svc + '.csv'
df = pd.read_csv(filename)
ctn_cols = ['ctn_cpu', 'ctn_network', 'ctn_memory']
max_corr = 0.01
metric = 'ctn_cpu'
for col in ctn_cols:
temp = abs(baseline_df[svc].corr(df[col]))
if temp > max_corr:
max_corr = temp
metric = col
edges_weight_avg = 0.0
num = 0
for u, v, data in anomaly_graph.in_edges(svc, data=True):
num = num + 1
edges_weight_avg = edges_weight_avg + data['weight']
for u, v, data in anomaly_graph.out_edges(svc, data=True):
if anomaly_graph.nodes[v]['type'] == 'service':
num = num + 1
edges_weight_avg = edges_weight_avg + data['weight']
edges_weight_avg = edges_weight_avg / num
personalization = edges_weight_avg * max_corr
return personalization, metric
def anomaly_subgraph(DG, anomalies, latency_df, faults_name, alpha):
# Get the anomalous subgraph and rank the anomalous services
# input:
# DG: attributed graph
# anomlies: anoamlous service invocations
# latency_df: service invocations from data collection
# agg_latency_dff: aggregated service invocation
# faults_name: prefix of csv file
# alpha: weight of the anomalous edge
# output:
# anomalous scores
# Get reported anomalous nodes
edges = []
nodes = []
# print(DG.nodes())
baseline_df = pd.DataFrame()
edge_df = {}
for anomaly in anomalies:
edge = anomaly.split('_')
edges.append(tuple(edge))
# nodes.append(edge[0])
svc = edge[1]
nodes.append(svc)
baseline_df[svc] = latency_df[anomaly]
edge_df[svc] = anomaly
# print('edge df:', edge_df)
nodes = set(nodes)
# print(nodes)
personalization = {}
for node in DG.nodes():
if node in nodes:
personalization[node] = 0
# Get the subgraph of anomaly
anomaly_graph = nx.DiGraph()
for node in nodes:
# print(node)
for u, v, data in DG.in_edges(node, data=True):
edge = (u,v)
# print(edge)
if edge in edges:
data = alpha
else:
normal_edge = u + '_' + v
data = baseline_df[v].corr(latency_df[normal_edge])
data = round(data, 3)
anomaly_graph.add_edge(u,v, weight=data)
anomaly_graph.nodes[u]['type'] = DG.nodes[u]['type']
anomaly_graph.nodes[v]['type'] = DG.nodes[v]['type']
# Set personalization with container resource usage
for u, v, data in DG.out_edges(node, data=True):
edge = (u,v)
if edge in edges:
data = alpha
else:
if DG.nodes[v]['type'] == 'host':
data, col = node_weight(u, anomaly_graph, baseline_df, faults_name)
else:
normal_edge = u + '_' + v
data = baseline_df[u].corr(latency_df[normal_edge])
data = round(data, 3)
anomaly_graph.add_edge(u,v, weight=data)
anomaly_graph.nodes[u]['type'] = DG.nodes[u]['type']
anomaly_graph.nodes[v]['type'] = DG.nodes[v]['type']
for node in nodes:
max_corr, col = svc_personalization(node, anomaly_graph, baseline_df, faults_name)
personalization[node] = max_corr / anomaly_graph.degree(node)
# print(node, personalization[node])
anomaly_graph = anomaly_graph.reverse(copy=True)
#
edges = list(anomaly_graph.edges(data=True))
for u, v, d in edges:
if anomaly_graph.nodes[node]['type'] == 'host':
anomaly_graph.remove_edge(u,v)
anomaly_graph.add_edge(v,u,weight=d['weight'])
# plt.figure(figsize=(9,9))
## nx.draw(DG, with_labels=True, font_weight='bold')
# pos = nx.spring_layout(anomaly_graph)
# nx.draw(anomaly_graph, pos, with_labels=True, cmap = plt.get_cmap('jet'), node_size=1500, arrows=True, )
# labels = nx.get_edge_attributes(anomaly_graph,'weight')
# nx.draw_networkx_edge_labels(anomaly_graph,pos,edge_labels=labels)
# plt.show()
#
## personalization['shipping'] = 2
# print('Personalization:', personalization)
anomaly_score = nx.pagerank(anomaly_graph, alpha=0.85, personalization=personalization, max_iter=10000)
anomaly_score = sorted(anomaly_score.items(), key=lambda x: x[1], reverse=True)
# return anomaly_graph
return anomaly_score
def print_rank(anomaly_score, target):
num = 10
for idx, anomaly_target in enumerate(anomaly_score):
if target in anomaly_target:
num = idx + 1
continue
print(target, ' Top K: ', num)
return num
if __name__ == '__main__':
# Tuning parameters
alpha = 0.55
ad_threshold = 0.045
folders = ['1', '2', '3', '4', '5']
faults_type = ['svc_latency', 'service_cpu', 'service_memory'] #, 'service_memory', 'svc_latency'
# faults_type = ['svc_latency', 'service_cpu']
targets = ['front-end', 'catalogue', 'orders', 'user', 'carts', 'payment', 'shipping']
for folder in folders:
for fault_type in faults_type:
for target in targets:
if target == 'front-end' and fault_type != 'svc_latency':
#'skip front-end for service_cpu and service_memory'
continue
print('target:', target, ' fault_type:', fault_type)
# prefix of csv files
faults_name = '../faults/' + folder + '/' + fault_type + '_' + target
latency_df = rt_invocations(faults_name)
if (target == 'payment' or target == 'shipping') and fault_type != 'svc_latency':
threshold = 0.02
else:
threshold = ad_threshold
# anomaly detection on response time of service invocation
anomalies = birch_ad_with_smoothing(latency_df, threshold)
# print(anomalies)
# get the anomalous service
anomaly_nodes = []
for anomaly in anomalies:
edge = anomaly.split('_')
anomaly_nodes.append(edge[1])
anomaly_nodes = set(anomaly_nodes)
# print(anomaly_nodes)
# construct attributed graph
DG = attributed_graph(faults_name)
anomaly_score = anomaly_subgraph(DG, anomalies, latency_df, faults_name, alpha)
print(anomaly_score)
anomaly_score_new = []
for anomaly_target in anomaly_score:
# print(anomaly_target[0])
if anomaly_target[0] in targets:
anomaly_score_new.append(anomaly_target)
num = print_rank(anomaly_score_new, target)
filename = 'MicroRCA_results.csv'
with open(filename,'a') as f:
writer = csv.writer(f)
writer.writerow([folder, target, fault_type, num, anomaly_score_new[:num], anomaly_nodes])