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
|
package pl.edu.mimuw.cloudatlas.agent;
import pl.edu.mimuw.cloudatlas.agent.messages.*;
import pl.edu.mimuw.cloudatlas.agent.modules.GossipGirlStrategies;
import pl.edu.mimuw.cloudatlas.agent.modules.TimerScheduledTask;
import pl.edu.mimuw.cloudatlas.model.*;
import java.lang.reflect.UndeclaredThrowableException;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
public class HierarchyConfig {
private GossipGirlStrategies gossipGirlStrategies;
private GossipGirlStrategies.ZoneSelectionStrategy zoneSelectionStrategy;
private Random random = new Random();
private PathName ourPath;
private EventBus eventBus;
HierarchyConfig(EventBus eventBus, String zonePath, String zoneStrategy) {
zoneSelectionStrategy = parseStrategy(zoneStrategy);
ourPath = new PathName(zonePath);
gossipGirlStrategies = new GossipGirlStrategies(ourPath);
this.eventBus = eventBus;
}
private GossipGirlStrategies.ZoneSelectionStrategy parseStrategy(String selectionStrategy) {
switch (selectionStrategy) {
case "RoundRobinExp":
return GossipGirlStrategies.ZoneSelectionStrategy.ROUND_ROBIN_EXP_FREQ;
case "RoundRobinUniform":
return GossipGirlStrategies.ZoneSelectionStrategy.ROUND_ROBIN_SAME_FREQ;
case "RandomExp":
return GossipGirlStrategies.ZoneSelectionStrategy.RANDOM_DECR_EXP;
case "RandomUniform":
return GossipGirlStrategies.ZoneSelectionStrategy.RANDOM_UNFIORM;
default:
throw new UnsupportedOperationException("Selection strategy doesnt exist");
}
}
public void startGossip(long gossipPeriod, String zonePath) {
Supplier<TimerScheduledTask> taskSupplier = () ->
new TimerScheduledTask() {
public void run() {
try {
System.out.println("INFO: initiating gossip");
StateMessage state = getState();
PathName gossipLevel = gossipGirlStrategies.selectStrategy(zoneSelectionStrategy);
ValueContact contact;
if (random.nextDouble() < 0.2) {
contact = selectFallbackContact(state.getContacts());
} else {
contact = selectContactFromLevel(gossipLevel, state);
}
if (contact != null) {
System.out.println("INFO: found a contact " + contact.toString());
InitiateGossipMessage message = new InitiateGossipMessage("", 0, new PathName(zonePath), contact);
sendMessage(message);
} else {
System.out.println("DEBUG: couldn't find contact for gossip");
}
} catch (InterruptedException e) {
System.out.println("Interrupted while initiating gossip");
} catch (Exception e) {
System.out.println("ERROR: something happened " + e.toString());
e.printStackTrace();
}
}
};
AgentUtils.startRecursiveTask(taskSupplier, gossipPeriod, eventBus);
}
private ValueContact selectContactFromLevel(PathName path, StateMessage state) throws Exception {
ZMI root = state.getZMI();
List<ZMI> siblings = getSiblings(root, path);
filterEmptyContacts(siblings);
if (siblings.isEmpty()) {
return selectFallbackContact(state.getContacts());
}
ZMI zmi = selectZMI(siblings);
ValueSet contactsValue = (ValueSet) zmi.getAttributes().getOrNull("contacts");
Set<Value> valueSetOrig = contactsValue.getValue();
Set<Value> valueSet = new HashSet(valueSetOrig);
filterOurContact(valueSet);
return selectContactFromSet(valueSet);
}
private void filterOurContact(Set<Value> contacts) {
Iterator it = contacts.iterator();
while (it.hasNext()) {
Value value = (Value) it.next();
if (value.getType().getPrimaryType() == Type.PrimaryType.CONTACT) {
ValueContact contact = (ValueContact) value;
if (contact.getName().equals(ourPath)) {
it.remove();
}
} else {
System.out.println("WARN: non contact value passed to filterOurContact");
}
}
}
private StateMessage getState() throws Exception {
CompletableFuture<ResponseMessage> responseFuture = new CompletableFuture();
this.eventBus.addMessage(new RequestStateMessage("", 0, responseFuture));
return (StateMessage) responseFuture.get();
}
private ValueContact selectFallbackContact(Set<ValueContact> contacts) throws Exception {
if (contacts.isEmpty()) {
return null;
} else {
return selectContactFromSet(contacts);
}
}
private ZMI selectZMI(List<ZMI> zmis) throws Exception {
int i = random.nextInt(zmis.size());
for (ZMI zmi : zmis) {
if (i == 0) {
return zmi;
}
i--;
}
System.out.println("ERROR: empty list passed to selectZMI");
throw new Exception("empty list passed to selectZMI");
}
private <T> ValueContact selectContactFromSet(Set<T> contacts) throws Exception {
if (contacts.size() == 0) {
return null;
}
int i = random.nextInt(contacts.size());
for (T contact : contacts) {
if (i == 0) {
return (ValueContact) contact;
}
i--;
}
System.out.println("ERROR: empty list passed to selectContactFromSet");
throw new Exception("empty list passed to selectContactFromSet");
}
private List<ZMI> getSiblings(ZMI root, PathName path) {
try {
List<ZMI> siblingsImm = root.findDescendant(path).getFather().getSons();
List<ZMI> siblings = new ArrayList();
for (ZMI siblingOrI : siblingsImm) {
siblings.add(siblingOrI);
}
return siblings;
} catch (ZMI.NoSuchZoneException e) {
System.out.println("ERROR: didn't find path when looking for siblings");
return new ArrayList();
}
}
private void filterEmptyContacts(List<ZMI> zmis) {
Iterator<ZMI> iterator = zmis.iterator();
while (iterator.hasNext()) {
ZMI zmi = iterator.next();
ValueSet contacts = (ValueSet) zmi.getAttributes().getOrNull("contacts");
if (contacts == null || contacts.isNull() || contacts.isEmpty() || onlyContactIsUs(contacts)) {
iterator.remove();
}
}
}
private boolean onlyContactIsUs(ValueSet contacts) {
for (Value value : contacts) {
if (value.getType().getPrimaryType() == Type.PrimaryType.CONTACT) {
ValueContact contact = (ValueContact) value;
if (!contact.getName().equals(ourPath)) {
return false;
}
} else {
System.out.println("WARN: non contact value passed to onlyContactIsUs");
}
}
return true;
}
public void startQueries(long queriesPeriod) {
Supplier<TimerScheduledTask> taskSupplier = () ->
new TimerScheduledTask() {
public void run() {
try {
sendMessage(new RunQueriesMessage("", 0));
} catch (InterruptedException e) {
System.out.println("Interrupted while triggering queries");
}
}
};
AgentUtils.startRecursiveTask(taskSupplier, queriesPeriod, eventBus);
}
public void startCleaningGossips(long gossipCleanPeriod) {
Supplier<TimerScheduledTask> taskSupplier = () ->
new TimerScheduledTask() {
public void run() {
try {
System.out.println("INFO: Scheduling old gossip cleanup");
sendMessage(new CleanOldGossipsMessage("", 0, ValueUtils.addToTime(ValueUtils.currentTime(), -gossipCleanPeriod)));
} catch (InterruptedException e) {
System.out.println("Interrupted while triggering queries");
}
}
};
AgentUtils.startRecursiveTask(taskSupplier, gossipCleanPeriod, eventBus);
}
}
|