-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluate_cromlech_solution.py
1530 lines (1281 loc) · 67.3 KB
/
evaluate_cromlech_solution.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import copy
import yaml
from pyvis.network import Network
from apgl.graph import SparseGraph
from difflib import SequenceMatcher
import pyomo.environ as pyo
from pyomo.util.infeasible import log_infeasible_constraints
from itertools import product
from pyomo.opt import SolverStatus, TerminationCondition
import sys
# Graph is the starting graph of the architecture, keys are operations and values are entities
graph = {}
# Nodes_dict associates each number to an operation or entity
nodes_dict = {}
# This is the dict with the entities as keys and related operations as values
edges_from_e_to_o = {}
# Contains the edges as keys and the type of relationship as values
relationship_dict = {}
# Number of replicas
num_replicas = 0
# Associations between operations and the columns they read
op_reads = {}
# Associations between operations and the columns they write
op_writes = {}
# Associations between forced operations
forced_operations = {}
# Associations between each attribute index and its name
attributes_iton = {}
# Associations between each attribute name and its index
attributes_ntoi = {}
# The location of the primary replica for each attribute, represented by the index of the microservice
primary_replicas_locations = {}
# Static attributes are attributes which are not written by any operation, and thus do not have a primary replica and do not count in the operative costs
# Association between the index of an attribute and a boolean saying if it is static
static_status = {}
# High consistency write attributes are attributes which are written by high consistency operations
# Association between the index of an attribute and a boolean saying if it is hc write
hc_write_status = {}
# High consistency read-only attributes are attributes which are read but not written by high consistency operations
# Association between the index of an attribute and a boolean saying if it is hc readonly
hc_readonly_status = {}
# Number of operations
op_num = -1
# List of all attributes indexes
attributes = []
# Associates each attribute to the ops which read it
attr_read_by = {}
# Associates each attribute to the ops which write it
attr_written_by = {}
# Words which are not counted in the LCS algorithm
banned_words = ['get', 'put', 'delete', 'read', 'write', 'retrieve', 'create', 'remove', 'save', 'query',
'add', 'all', 'upload', 'insert', 'update', 'change', 'modify', 'by', 'fetch', 'from', 'find']
# name of operations cleaned of banned_words
cleaned_names = {}
class Graph:
def __init__(self):
self.__graph = copy.deepcopy(graph)
self.__nodes_dict = copy.deepcopy(nodes_dict)
self.__edges_from_e_to_o = copy.deepcopy(edges_from_e_to_o)
self.__relationshp_dict = copy.deepcopy(relationship_dict)
def get_graph(self):
return self.__graph
def get_nodes_dict(self):
return self.__nodes_dict
def get_edges_from_e_to_o(self):
return self.__edges_from_e_to_o
def get_relationshp_dict(self):
return self.__relationshp_dict
class Operation:
def __init__(self, name, index, frequency, consistency):
self.__name = name
self.__index = index
self.__frequency = frequency
self.__consistency = consistency
def get_name(self):
return self.__name
def set_name(self, name):
self.__name = name
def get_index(self):
return self.__index
def get_frequency(self):
return self.__frequency
def set_frequency(self, frequency):
self.__frequency = frequency
def get_consistency(self):
return self.__consistency
def set_consistency(self, consistency):
self.__consistency = consistency
class Entity:
def __init__(self, name, index):
self.__name = name
self.__index = index
def get_name(self):
return self.__name
def set_name(self, name):
self.__name = name
def get_index(self):
return self.__index
def parse_arch_yaml(yaml_filename):
with open(yaml_filename, 'r') as stream:
try:
op_names = []
data = yaml.safe_load(stream)
op_index = 0
ent_index = len(data['operations'])
entities = []
for o in data['operations']:
if o['name'] in op_names:
raise Exception("There are two operations named " + o['name'])
op_names.append(o['name'])
new_op = Operation(o['name'], op_index, int(o['frequency']), o['consistency'])
nodes_dict.update({op_index: new_op})
graph.update({op_index: []})
op_reads.update({op_index: []})
op_writes.update({op_index: []})
print(o['name'], o['database_access'])
for e in o['database_access']:
new_ent = Entity(e['entity_name'], ent_index)
already_listed = False
for e2 in entities:
if e2.get_name() == new_ent.get_name():
already_listed = True
if not already_listed:
entities.append(new_ent)
edges_from_e_to_o.update({ent_index: []})
nodes_dict.update({ent_index: new_ent})
ent_index += 1
try:
if e['read_attributes']:
for entity in entities:
if entity.get_name() == e['entity_name']:
relationship_dict.update({(new_op.get_index(), entity.get_index()): 'R'})
graph.get(new_op.get_index()).append(entity.get_index())
if new_op.get_index() not in edges_from_e_to_o.get(entity.get_index()):
edges_from_e_to_o.get(entity.get_index()).append(new_op.get_index())
for a in e['read_attributes']:
if e['entity_name'] + '.' + a not in op_writes.get(new_op.get_index()):
op_reads.get(new_op.get_index()).append(e['entity_name'] + '.' + a)
else:
raise Exception("Attribute " + e['entity_name'] + '.' + a + " of operation " + new_op.get_name() +
" is both read and written. This is not allowed.")
except KeyError:
pass
try:
if e['write_attributes']:
for entity in entities:
if entity.get_name() == e['entity_name']:
relationship_dict.update({(new_op.get_index(), entity.get_index()): 'W'})
graph.get(new_op.get_index()).append(entity.get_index())
if new_op.get_index() not in edges_from_e_to_o.get(entity.get_index()):
edges_from_e_to_o.get(entity.get_index()).append(new_op.get_index())
for a in e['write_attributes']:
if e['entity_name'] + '.' + a not in op_reads.get(new_op.get_index()):
op_writes.get(new_op.get_index()).append(e['entity_name'] + '.' + a)
else:
raise Exception("Attribute " + e['entity_name'] + '.' + a + " of operation " + new_op.get_name() +
" is both read and written. This is not allowed.")
except KeyError:
pass
graph.update({op_index: list(set(graph.get(new_op.get_index())))})
op_index += 1
for o in data['operations']:
fops = []
if not o['forced_operations']:
o['forced_operations'] = [None]
for f in o['forced_operations']:
association = False
for n in nodes_dict:
if nodes_dict.get(n).get_name() == o['name'] and n < ent_index:
break
for n2 in nodes_dict:
if nodes_dict.get(n2).get_name() == f and n2 < ent_index:
fops.append(n2)
association = True
if not association and f:
raise Exception("No operation named " + f + ' exists, it cannot be forced along ' + o['name'])
forced_operations.update({n: fops})
# print([e.get_name() for e in entities])
except yaml.YAMLError as exc:
print(exc)
# Draws the graph with names instead of ids
def draw_graph_verbose(gr, html_filename):
net = Network(height='1500px', width='100%', bgcolor="#dddddd")
i = 0
for n in gr.get_graph():
net.add_node(gr.get_nodes_dict().get(n).get_name(), size=8, color='blue')
for e in gr.get_graph().get(n):
net.add_node(gr.get_nodes_dict().get(e).get_name(), size=8, color='seagreen')
net.add_edge(gr.get_nodes_dict().get(n).get_name(), gr.get_nodes_dict().get(e).get_name())
net.show(html_filename)
# Draws the graph showing the forced operations relationships
def draw_forcedops(html_filename):
net = Network(height='2500px', width='100%', bgcolor="#dddddd")
for g in graph:
net.add_node(nodes_dict.get(g).get_name(), size=8, color='royalblue', mass=1.5)
for f in forced_operations:
for o in forced_operations.get(f):
net.add_edge(nodes_dict.get(f).get_name(), nodes_dict.get(o).get_name())
net.show(html_filename)
# Turns the microservice architecure in a format drawable with pyvis
def format_and_draw(microservices, html_filename):
net = Network(height='2500px', width='100%', bgcolor="#dddddd")
i = 0
for m in microservices:
ops = [x for x in m if x < op_num]
attrs = [x for x in m if x >= op_num]
for o in ops:
if nodes_dict.get(o).get_consistency() == 'H':
if op_writes.get(o):
net.add_node(nodes_dict.get(o).get_name(), size=8, color='black', mass=1.5)
else:
net.add_node(nodes_dict.get(o).get_name(), size=8, color='navy', mass=1.5)
else:
net.add_node(nodes_dict.get(o).get_name(), size=8, color='royalblue', mass=1.5)
for a in attrs:
net.add_node(attributes_iton.get(a) + '@' + str(i), size=8, color='seagreen', shape='square', mass=1.5)
for o in ops:
accesses = op_reads.get(o) + op_writes.get(o)
accesses_i = []
for a in accesses:
accesses_i.append(attributes_ntoi.get(a))
for a in accesses_i:
if a in attrs:
net.add_edge(nodes_dict.get(o).get_name(), attributes_iton.get(a) + '@' + str(i))
i += 1
net.show(html_filename)
# Turns the microservice architecure in a format drawable with pyvis
def format_and_draw_complete(microservices, html_filename):
net = Network(height='2500px', width='100%', bgcolor="#dddddd")
i = 0
for m in microservices:
net_single = Network(height='2500px', width='100%', bgcolor="#dddddd")
ops = [x for x in m if x < op_num]
attrs = [x for x in m if x >= op_num]
for o in ops:
if nodes_dict.get(o).get_consistency() == 'H':
if op_writes.get(o):
net.add_node(nodes_dict.get(o).get_name(), size=8, color='black', mass=1.5)
net_single.add_node(nodes_dict.get(o).get_name(), size=8, color='black', mass=1.5)
else:
net.add_node(nodes_dict.get(o).get_name(), size=8, color='navy', mass=1.5)
net_single.add_node(nodes_dict.get(o).get_name(), size=8, color='navy', mass=1.5)
else:
net.add_node(nodes_dict.get(o).get_name(), size=8, color='royalblue', mass=1.5)
net_single.add_node(nodes_dict.get(o).get_name(), size=8, color='royalblue', mass=1.5)
for a in attrs:
net.add_node(attributes_iton.get(a) + '@' + str(i), size=8, color='darkgreen', shape='square', mass=1.5)
net_single.add_node(attributes_iton.get(a) + '@' + str(i), size=8, color='darkgreen', shape='square', mass=1.5)
for o in ops:
accesses = op_reads.get(o) + op_writes.get(o)
accesses_i = []
for a in accesses:
accesses_i.append(attributes_ntoi.get(a))
for a in accesses_i:
if a in attrs:
net.add_edge(nodes_dict.get(o).get_name(), attributes_iton.get(a) + '@' + str(i))
net_single.add_edge(nodes_dict.get(o).get_name(), attributes_iton.get(a) + '@' + str(i))
net_single.show(html_filename + '_' + str(i) + ".html")
i += 1
net.show(html_filename)
def format_and_draw_final(microservices, html_filename):
net = Network(height='2500px', width='100%', bgcolor="#dddddd")
i = 0
for m in microservices:
net_single = Network(height='2500px', width='100%', bgcolor="#dddddd")
ops = [x for x in m if isinstance(x, int)]
attrs = [x for x in m if isinstance(x, str)]
for o in ops:
if nodes_dict.get(o).get_consistency() == 'H':
if op_writes.get(o):
net.add_node(nodes_dict.get(o).get_name(), size=8 + nodes_dict.get(o).get_frequency()*1.2, color='navy', mass=1.5, group=i)
net_single.add_node(nodes_dict.get(o).get_name(), size=8 + nodes_dict.get(o).get_frequency()*1.2, color='navy', mass=1.5, group=i)
else:
net.add_node(nodes_dict.get(o).get_name(), size=8 + nodes_dict.get(o).get_frequency() * 1.2,
color='mediumseagreen', mass=1.5, group=i)
net_single.add_node(nodes_dict.get(o).get_name(), size=8 + nodes_dict.get(o).get_frequency() * 1.2,
color='mediumseagreen', mass=1.5, group=i)
else:
if op_writes.get(o):
net.add_node(nodes_dict.get(o).get_name(), size=8 + nodes_dict.get(o).get_frequency()*1.2, color='dodgerblue', mass=1.5, group=i)
net_single.add_node(nodes_dict.get(o).get_name(), size=8 + nodes_dict.get(o).get_frequency()*1.2, color='dodgerblue', mass=1.5, group=i)
else:
net.add_node(nodes_dict.get(o).get_name(), size=8 + nodes_dict.get(o).get_frequency()*1.2,
color='mediumseagreen', mass=1.5, group=i)
net_single.add_node(nodes_dict.get(o).get_name(), size=8 + nodes_dict.get(o).get_frequency()*1.2,
color='mediumseagreen', mass=1.5, group=i)
attr_count = {}
for a in attrs:
attr_id = attributes_iton.get(int(a[:len(a) - 1]))
count = 0
for m2 in microservices:
attr_int = [int(x[:len(x)-1]) for x in m2 if isinstance(x, str)]
if attributes_ntoi.get(attr_id) in attr_int:
count += 1
attr_count.update({int(a[:len(a) - 1]): count })
repl_status = a[len(a) - 1]
if repl_status == 'N':
net.add_node(attr_id + '@' + str(i) + '/' + str(count), size=8, color='yellow', shape='square', mass=1.5, group=i)
net_single.add_node(attr_id + '@' + str(i) + '/' + str(count), size=8, color='yellow', shape='square', mass=1.5, group=i)
if repl_status == 'R':
net.add_node(attr_id + '@' + str(i) + '/' + str(count) , size=8, color='orange', shape='square', mass=1.5, group=i)
net_single.add_node(attr_id + '@' + str(i) + '/' + str(count), size=8, color='orange', shape='square', mass=1.5, group=i)
if repl_status == 'P':
net.add_node(attr_id + '@' + str(i) + '/' + str(count), size=8, color='red', shape='square', mass=1.5, group=i)
net_single.add_node(attr_id + '@' + str(i) + '/' + str(count), size=8, color='red', shape='square', mass=1.5, group=i)
for o in ops:
accesses = op_reads.get(o) + op_writes.get(o)
accesses_i = []
attrs_num = []
for a in accesses:
accesses_i.append(attributes_ntoi.get(a))
for a in attrs:
attrs_num.append(int(a[:len(a) - 1]))
for a in accesses_i:
if a in attrs_num:
net.add_edge(nodes_dict.get(o).get_name(), attributes_iton.get(a) + '@' + str(i) + '/' + str(attr_count.get(a)) )
net_single.add_edge(nodes_dict.get(o).get_name(), attributes_iton.get(a) + '@' + str(i) + '/' + str(attr_count.get(a)) )
# hack: duplicate all edges, otherwise pyvis does not properly render the colors of nodes
net.add_edge(nodes_dict.get(o).get_name(), attributes_iton.get(a) + '@' + str(i) + '/' + str(attr_count.get(a)) )
net_single.add_edge(nodes_dict.get(o).get_name(), attributes_iton.get(a) + '@' + str(i) + '/' + str(attr_count.get(a)) )
net_single.show(html_filename + '_' + str(i) + ".html", notebook=False)
i += 1
net.show(html_filename, notebook=False)
# Returns connected components of graph
def get_connected_components(gr):
size = len(gr.get_nodes_dict())
g = SparseGraph(size, frmt='lil')
gra = gr.get_graph()
for n in gra:
for n2 in gra.get(n):
g.addEdge(n, n2)
return g.findConnectedComponents()
def compute_communication_cost(microservices):
w_op = 0
location_dict = {}
op_scores = {}
for g in graph:
for m in microservices:
if g in m:
location_dict.update({g: microservices.index(m)})
for a in primary_replicas_locations:
if not static_status.get(a):
m_index = 0
for m in microservices:
if m_index == primary_replicas_locations.get(a):
m_index += 1
continue
for o in m:
if o in attr_written_by.get(a):
w_op += nodes_dict.get(o).get_frequency()
ent_name = attributes_iton.get(a).split('.')[0]
if op_scores.get(ent_name):
op_scores.update({ent_name: op_scores.get(ent_name) + nodes_dict.get(o).get_frequency()})
else:
op_scores.update({ent_name: nodes_dict.get(o).get_frequency()})
m_index += 1
r_op = 0
repl = 0
caches = {}
for a in attributes:
ent_name = attributes_iton.get(a).split('.')[0]
m_index = -1
for m in microservices:
m_index += 1
if a in m and not primary_replicas_locations.get(a) == m_index:
read_in_same = 0
total_write = 0
for o in m:
if o < 10000 and attributes_iton.get(a) in op_reads.get(o):
read_in_same += nodes_dict.get(o).get_frequency()
for m2 in microservices:
for o in m2:
if o < 10000 and attributes_iton.get(a) in op_writes.get(o):
total_write += nodes_dict.get(o).get_frequency()
if read_in_same >= total_write:
repl += total_write
caches.update({(a, m_index): True})
if op_scores.get(ent_name):
op_scores.update(
{ent_name: op_scores.get(ent_name) + total_write})
else:
op_scores.update({ent_name: total_write})
else:
r_op += read_in_same
caches.update({(a, m_index): False})
if op_scores.get(ent_name):
op_scores.update(
{ent_name: op_scores.get(ent_name) + read_in_same})
else:
op_scores.update({ent_name: read_in_same})
else:
continue
repl2 = 0
for a in attributes:
replica_num = 1
total_write = 0
m_index = -1
for m in microservices:
m_index += 1
if a in m and caches.get((a, m_index)):
replica_num += 1
for o in m:
if o < 10000 and attributes_iton.get(a) in op_writes.get(o):
total_write += nodes_dict.get(o).get_frequency()
if replica_num > 1:
repl2 += total_write * (replica_num - 1)
ent_name = attributes_iton.get(a).split('.')[0]
if op_scores.get(ent_name):
op_scores.update({ent_name: op_scores.get(ent_name) + total_write * (replica_num - 1)})
else:
op_scores.update({ent_name: total_write * (replica_num - 1)})
#print("ROP " + str(r_op))
#print("REPL " + str(repl) + " - " + str(repl2))
#print("WOP " + str(w_op))
return w_op + r_op + repl
def post_processing_communication_cost(result):
repl = 0
for a in attributes:
repl_num = 0
total_write = 0
for m in result:
attr_int = [int(x[:len(x)-1]) for x in m if isinstance(x, str)]
if a in attr_int and (str(a) + 'R') in m:
repl_num += 1
for o in graph:
if o < op_num:
if attributes_iton.get(a) in op_writes.get(o):
total_write += nodes_dict.get(o).get_frequency()
repl += total_write * repl_num
rop = 0
for a in attributes:
for m in result:
attr_int = [int(x[:len(x)-1]) for x in m if isinstance(x, str)]
ops = [x for x in m if isinstance(x, int)]
if a in attr_int and (str(a) + 'N') in m:
for o in ops:
if attributes_iton.get(a) in op_reads.get(o):
rop += nodes_dict.get(o).get_frequency()
wop = 0
for a in attributes:
for m in result:
attr_int = [int(x[:len(x)-1]) for x in m if isinstance(x, str)]
ops = [x for x in m if isinstance(x, int)]
if a in attr_int and (str(a) + 'P') not in m:
for o in ops:
if attributes_iton.get(a) in op_writes.get(o):
wop += nodes_dict.get(o).get_frequency()
print("POST PROCESSING:")
print("WOP " + str(wop))
print("ROP " + str(rop))
print("REPL " + str(repl))
return wop + rop + repl
# Removes all columns which only create noise (as they are only accessed by a single operation) and returns
# the list of operations which have no accessed columns, which can then be trimmed from the architecture
def noise_removal():
all_columns = []
to_remove = []
for op in graph:
all_columns += list(set(op_reads.get(op) + op_writes.get(op)))
for op in graph:
related_columns = op_reads.get(op) + op_writes.get(op)
for c in related_columns:
if all_columns.count(c) == 1:
if c in op_reads.get(op):
#print("Removed " + c)
op_reads.get(op).remove(c)
if c in op_writes.get(op):
op_writes.get(op).remove(c)
if len(op_reads.get(op) + op_writes.get(op)) == 0:
to_remove.append(op)
return to_remove
# Finds the static attributes and populates the static_status dict
def identify_static_attributes():
written = []
for o in op_writes:
for a in op_writes.get(o):
written.append(attributes_ntoi.get(a))
written = list(set(written))
read = []
for o in op_reads:
for a in op_reads.get(o):
read.append(attributes_ntoi.get(a))
read = list(set(read))
all_attrs = list(set(written + read))
for a in all_attrs:
static_status.update({a: False})
if a not in written:
static_status.update({a: True})
# Finds the hc read only attributes and populates the hc_readonly_status dictionary
def identify_hc_readonly_attributes():
written = []
for o in op_writes:
if nodes_dict.get(o).get_consistency() == 'H':
for a in op_writes.get(o):
written.append(attributes_ntoi.get(a))
written = list(set(written))
read = []
for o in op_reads:
if nodes_dict.get(o).get_consistency() == 'H':
for a in op_reads.get(o):
read.append(attributes_ntoi.get(a))
read = list(set(read))
all_attrs = list(set(written + read))
for a in all_attrs:
if a not in written:
hc_readonly_status.update({a: True})
def elect_primary_replicas(microservices):
for a in attributes:
if not static_status.get(a):
if hc_write_status.get(a):
elect_hcw(a, microservices)
else:
elect_w(a, microservices)
else:
elect_s(a, microservices)
return
def elect_s(attribute, microservices):
for m in microservices:
if attribute in m:
primary_replicas_locations.update({attribute: microservices.index(m)})
return
def elect_hcw(attribute, microservices):
for m in microservices:
if attribute in m:
ops = [n for n in m if n < op_num and nodes_dict.get(n).get_consistency() == 'H']
for o2 in ops:
if attributes_iton.get(attribute) in op_writes.get(o2) + op_reads.get(o2) and op_writes.get(o2):
primary_replicas_locations.update({attribute: microservices.index(m)})
return
raise Exception("Attribute " + attributes_iton.get(attribute) + " was wrongly categorized as hc write.")
def elect_w(attribute, microservices):
frequencies = []
for m in microservices:
if attribute in m:
ops = [n for n in m if n < op_num]
frequency = 0
for o2 in ops:
if attributes_iton.get(attribute) in op_writes.get(o2):
frequency += nodes_dict.get(o2).get_frequency()
frequencies.append(frequency)
else:
frequencies.append(0)
if max(frequencies) == 0:
raise Exception("Attribute " + attributes_iton.get(attribute) + " was wrongly categorized as lc write.")
primary_replicas_locations.update({attribute: frequencies.index(max(frequencies))})
return
# Joins the forced operations
def force_operations():
services_temp = []
services = []
for o in op_reads:
for o2 in op_reads:
if o2 != o and nodes_dict.get(o).get_consistency() == 'H' and nodes_dict.get(o2).get_consistency() == 'H' and op_writes.get(o) and op_writes.get(o2):
if compute_op_similarity(o, o2) > 0 and o2 not in forced_operations.get(o):
forced_operations.get(o).append(o2)
services_temp.append([o] + forced_operations.get(o))
for o in op_reads:
indices = []
for s in services_temp:
if o in s:
indices.append(services_temp.index(s))
new_serv = []
for i in indices:
new_serv += services_temp[i]
services_temp.append(new_serv)
for i in indices:
services_temp.remove(services_temp[i])
for i2 in indices:
if i2 > i:
indices[indices.index(i2)] -= 1
for s in services_temp:
if s:
services.append(list(set(s)))
return services
def clean_names():
for g in graph:
clean = nodes_dict.get(g).get_name().lower()
for w in banned_words:
if w in clean:
clean = clean.replace(w, '')
cleaned_names.update({g: clean})
def set_overlap(o1, o2):
accesses1 = set(op_writes.get(o1) + op_reads.get(o1))
accesses2 = set(op_writes.get(o2) + op_reads.get(o2))
return float(len(accesses1.intersection(accesses2)) / min(len(accesses1), len(accesses2)))
def semantic_overlap(o1, o2):
name1 = cleaned_names.get(o1)
name2 = cleaned_names.get(o2)
seqMatch = SequenceMatcher(None, name1, name2)
match = seqMatch.find_longest_match(0, len(name1), 0, len(name2))
if match.size < 4:
return 0
else:
return match.size / min(len(name1), len(name2))
def compute_semantic_score(services):
sim = []
for s in services:
ops = [x for x in s if x < op_num]
similarities = []
for o in ops:
for o2 in ops:
similarities.append(semantic_overlap(o, o2))
sim.append(round((sum(similarities) * len(ops)) / (op_num * len(similarities)), 4))
return sum(sim)
def compute_op_similarity(o1, o2):
if o1 == o2:
return 1
set_o = set_overlap(o1, o2)
if set_o == 1:
semantic_penalty = 1
name1 = cleaned_names.get(o1)
name2 = cleaned_names.get(o2)
seqMatch = SequenceMatcher(None, name1, name2)
match = seqMatch.find_longest_match(0, len(name1), 0, len(name2))
if match.size < 4:
semantic_penalty = 0.9 #OCCHIO
return set_o * semantic_penalty
return set_o
def compute_total_coupling(services):
sim = []
suca = 0
for s in services:
ops = [x for x in s if x < op_num]
similarities = []
for o in ops:
for o2 in ops:
similarities.append(compute_op_similarity(o, o2))
sim.append(round((sum(similarities) * len(ops)) / (op_num * len(similarities)), 4))
suca += len(ops)
return sum(sim)
def write_dat_file(num_services):
read_dependencies = {}
write_dependencies = {}
access_dependencies = {}
for g in graph:
reads = [attributes_ntoi.get(a) for a in op_reads.get(g)]
writes = [attributes_ntoi.get(a) for a in op_writes.get(g)]
for a in attributes:
access_dependencies.update({(g, a - 100000): 1})
if a in reads:
read_dependencies.update({(g, a - 100000): 1})
else:
read_dependencies.update({(g, a - 100000): 0})
if a in writes:
write_dependencies.update({(g, a - 100000): 1})
else:
write_dependencies.update({(g, a - 100000): 0})
if a not in reads and a not in writes:
access_dependencies.update({(g, a - 100000): 0})
data = open('data.dat', 'w')
data.write("data;\n\n")
data.write("set MICROSERVICES := ")
for n in range(0, num_services):
data.write("M" + str(n) + " ")
data.write(";\n\n")
data.write("set OPERATIONS := ")
for n in range(0, op_num):
data.write("O" + str(n) + " ")
data.write(";\n\n")
data.write("set ENTITIES := ")
for n in range(0, len(attributes)):
data.write("E" + str(n) + " ")
data.write(";\n\n")
data.write("param frequencies:\n ")
for n in range(0, op_num):
data.write("O" + str(n) + " ")
data.write(":=")
data.write("\nF ")
for n in range(0, op_num):
spaces = " "
for l in range(0, len(str(n)) + 1 - len(str(nodes_dict.get(n).get_frequency()))):
spaces += " "
data.write(str(nodes_dict.get(n).get_frequency()) + spaces)
data.write(";\n\n")
data.write("param acc:\n ")
for n in range(0, len(attributes)):
data.write("E" + str(n) + " ")
data.write(":=")
for n in range(0, op_num):
spaces = " "
for l in range(0, 2 - len(str(n))):
spaces += " "
data.write("\nO" + str(n) + spaces)
for a in range(0, len(attributes)):
spaces = " "
for l in range(0, len(str(a))):
spaces += " "
if access_dependencies.get((n, a)) == 1:
data.write(str(1) + spaces)
else:
data.write(str(0) + spaces)
data.write(";\n\n")
data.write("param accr:\n ")
for n in range(0, len(attributes)):
data.write("E" + str(n) + " ")
data.write(":=")
for n in range(0, op_num):
spaces = " "
for l in range(0, 2 - len(str(n))):
spaces += " "
data.write("\nO" + str(n) + spaces)
for a in range(0, len(attributes)):
spaces = " "
for l in range(0, len(str(a))):
spaces += " "
if read_dependencies.get((n, a)) == 1:
data.write(str(1) + spaces)
else:
data.write(str(0) + spaces)
data.write(";\n\n")
data.write("param accrw:\n ")
for n in range(0, len(attributes)):
data.write("E" + str(n) + " ")
data.write(":=")
for n in range(0, op_num):
spaces = " "
for l in range(0, 2 - len(str(n))):
spaces += " "
data.write("\nO" + str(n) + spaces)
for a in range(0, len(attributes)):
spaces = " "
for l in range(0, len(str(a))):
spaces += " "
if write_dependencies.get((n, a)) == 1:
data.write(str(1) + spaces)
else:
data.write(str(0) + spaces)
data.write(";\n\n")
data.write("param coloc:\n ")
for n in range(0, op_num):
data.write("O" + str(n) + " ")
data.write(":=")
for n in range(0, op_num):
spaces = " "
for l in range(0, 2 - len(str(n))):
spaces += " "
data.write("\nO" + str(n) + spaces)
for a in range(0, op_num):
spaces = " "
for l in range(0, len(str(a))):
spaces += " "
if bound_ops.get((n, a)) == 1:
data.write(str(1) + spaces)
else:
data.write(str(0) + spaces)
data.write(";\n\n")
data.write("param tr:\n ")
for o in range(0, op_num):
data.write("O" + str(o) + " ")
data.write(":=")
data.write("\nT ")
for o in range(0, op_num):
spaces = " "
for l in range(0, len(str(o))):
spaces += " "
if nodes_dict.get(o).get_consistency() == 'H':
data.write(str(1) + spaces)
else:
data.write(str(0) + spaces)
data.write(";\n\n")
data.write("param similarity:\n ")
for n in range(0, op_num):
data.write("O" + str(n) + " ")
data.write(":=")
for n in range(0, op_num):
spaces = " "
for l in range(0, 2 - len(str(n))):
spaces += " "
data.write("\nO" + str(n) + spaces)
for n2 in range(0, op_num):
spaces = " "
for l in range(0, len(str(n2))):
spaces += " "
data.write(str(compute_op_similarity(n, n2)) + spaces)
data.write(";")
data.close()
def trim_operations(to_remove):
op_num = len(graph)
for t in to_remove:
#print("Removed op. " + nodes_dict.get(t).get_name())
graph.pop(t)
nodes_dict.pop(t)
op_reads.pop(t)
op_writes.pop(t)
forced_operations.pop(t)
for i in range(t, op_num):
graph.update({i: graph.get(i + 1)})
nodes_dict.update({i: nodes_dict.get(i + 1)})
op_reads.update({i: op_reads.get(i + 1)})
op_writes.update({i: op_writes.get(i + 1)})
forced_operations.update({i: forced_operations.get(i + 1)})
graph.pop(op_num - 1)
nodes_dict.pop(op_num - 1)
op_reads.pop(op_num - 1)
op_writes.pop(op_num - 1)
forced_operations.pop(op_num - 1)
for f in forced_operations:
if t in forced_operations.get(f):
forced_operations.get(f).remove(t)
for f2 in forced_operations.get(f):
if f2 > t:
forced_operations.get(f)[forced_operations.get(f).index(f2)] -= 1
op_num = len(graph)
for t2 in range(0, len(to_remove)):
if to_remove[t2] > t:
to_remove[t2] -= 1
def build_attr_relationships():
attrs = []
for g in graph:
for a in op_writes.get(g):
attrs.append(a)
for a in op_reads.get(g):
attrs.append(a)
all_attrs = list(set(attrs))
all_attrs.sort()
index = 100000
for a in all_attrs:
attributes_iton.update({index: a})
attributes_ntoi.update({a: index})
index += 1
read_by = []
written_by = []
for o in graph:
if a in op_reads.get(o):
read_by.append(o)
if a in op_writes.get(o):
written_by.append(o)
attr_read_by.update({attributes_ntoi.get(a): read_by})
attr_written_by.update({attributes_ntoi.get(a): written_by})
res = [attributes_ntoi.get(x) for x in all_attrs]
return res
def build_hcw_relationships():
# Lists the high consistency write operations
hc_writes = []
for o in graph:
if nodes_dict.get(o).get_consistency() == 'H' and op_writes.get(o) != []:
hc_writes.append(o)
hcwassociations = {}
# Lists the columns which are written
hc_written_columns = []
for o in hc_writes:
for x in op_writes.get(o) + op_reads.get(o):
hc_written_columns.append(x)
hcwassociations.update({attributes_ntoi.get(x) - 100000: o})
hc_written_columns = list(set(hc_written_columns))
for a in attributes:
if not hcwassociations.get(a - 100000):
hcwassociations.update({a - 100000: -1})
for a in attributes:
if attributes_iton.get(a) in hc_written_columns:
hc_write_status.update({a: True})
else:
hc_write_status.update(({a: False}))
def optimizer(op_num, max_com_cost, num_services, alpha):
model = pyo.AbstractModel()
opt = pyo.SolverFactory('gurobi', solver_io="lp")
M = 10000
######################
# INPUT VARIABLES #
######################
# set of ENTITIES
model.ENTITIES = pyo.Set()
# set of operations
model.OPERATIONS = pyo.Set()
# num of available microservices
model.MICROSERVICES = pyo.Set()
# operation frequencies
model.frequencies = pyo.Param({'F'}, model.OPERATIONS, within=pyo.Integers)
# matrix of dependencies between operations and ENTITIES
model.acc = pyo.Param(model.OPERATIONS, model.ENTITIES, within=pyo.Binary)
# matrix of dependencies between operations and ENTITIES
model.accr = pyo.Param(model.OPERATIONS, model.ENTITIES, within=pyo.Binary)
# matrix of dependencies between operations and ENTITIES
model.accrw = pyo.Param(model.OPERATIONS, model.ENTITIES, within=pyo.Binary)
# matrix of dependencies between operations and operations
model.coloc = pyo.Param(model.OPERATIONS, model.OPERATIONS, within=pyo.Binary)
# vector representing if an operation has transactional requirements
model.tr = pyo.Param({'T'}, model.OPERATIONS, within=pyo.Binary)
# matrix representing similarity between pairs of operations
model.similarity = pyo.Param(model.OPERATIONS, model.OPERATIONS, within=pyo.Reals)
######################
# DECISION VARIABLES #
######################
# True iff operation o is in microservice m
model.x = pyo.Var(model.OPERATIONS, model.MICROSERVICES, within=pyo.Binary)
# True iff attribute a is in microservice m
model.y = pyo.Var(model.ENTITIES, model.MICROSERVICES, within=pyo.Binary)
# True iff attribute a has primary location in service m
model.l = pyo.Var(model.ENTITIES, model.MICROSERVICES, within=pyo.Binary)
########################
# AUXILIARY VARIABLES #
########################
# Variable which is true when two operations are in the same service (needed by colocated operations and cohm constraints)
model.same_location = pyo.Var(model.OPERATIONS, model.OPERATIONS, model.MICROSERVICES, within=pyo.Binary)
# Variable to linearize product of x and acc
model.accesses_e_in_m = pyo.Var(model.OPERATIONS, model.ENTITIES, model.MICROSERVICES, within=pyo.Binary)
# Variable to linearize product of booleans in transactional constraint
model.transactional = pyo.Var(model.OPERATIONS, model.ENTITIES, model.MICROSERVICES, within=pyo.Binary)
# Variable to linearize product between same_location and similarity (used by cohm constraint)
model.similarity_if_in_same_location = pyo.Var(model.OPERATIONS, model.OPERATIONS, model.MICROSERVICES, bounds=(0, 1), within=pyo.Reals)
# Variable to replace division in cohm computation
model.cohesion_temp = pyo.Var(model.MICROSERVICES, bounds=(0, 1), within=pyo.Reals)
# Variable to compute boolean and for ROP computation (to determine if an entity is read remotely) (needed by rop computation)
model.reads_from_remote = pyo.Var(model.OPERATIONS, model.ENTITIES, model.MICROSERVICES, within=pyo.Binary)
# Variable to store frequency of operations if it reads from remote (needed by rop computation)
model.read_frequency_from_remote = pyo.Var(model.OPERATIONS, model.ENTITIES, model.MICROSERVICES, within=pyo.Integers)
# Variable to compute boolean and for WOP computation (to determine if a leader is written remotely) (needed by wop computation)
model.writes_from_remote = pyo.Var(model.OPERATIONS, model.ENTITIES, model.MICROSERVICES, within=pyo.Binary)