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
|
package pl.edu.mimuw.cloudatlas.agent.modules;
import java.util.AbstractMap.SimpleImmutableEntry;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import pl.edu.mimuw.cloudatlas.agent.messages.AttributesMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.HejkaMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.NoCoTamMessage;
import pl.edu.mimuw.cloudatlas.agent.messages.QueryMessage;
import pl.edu.mimuw.cloudatlas.model.Attribute;
import pl.edu.mimuw.cloudatlas.model.AttributesMap;
import pl.edu.mimuw.cloudatlas.model.PathName;
import pl.edu.mimuw.cloudatlas.model.ValueContact;
import pl.edu.mimuw.cloudatlas.model.ValueDuration;
import pl.edu.mimuw.cloudatlas.model.ValueInt;
import pl.edu.mimuw.cloudatlas.model.ValueQuery;
import pl.edu.mimuw.cloudatlas.model.ValueTime;
import pl.edu.mimuw.cloudatlas.model.ValueUtils;
import pl.edu.mimuw.cloudatlas.model.ZMI;
public class GossipGirlState {
public enum State {
WAIT_FOR_STATE_INITIALIZER,
APPLY_HEJKA,
WAIT_FOR_STATE_RESPONDER,
SEND_HEJKA,
SEND_NO_CO_TAM,
SEND_INFO,
SEND_INFO_AND_FINISH,
WAIT_FOR_NO_CO_TAM,
WAIT_FOR_FIRST_INFO,
WAIT_FOR_INFO,
FINISHED,
ERROR
}
public ValueTime lastAction;
public PathName ourPath;
public ValueContact theirContact;
public long gossipId;
public long theirGossipId;
public long timeOffest;
public State state;
public ZMI hierarchy;
public Map<Attribute, ValueQuery> queries;
public ValueTime hejkaSendTimestamp;
public ValueTime hejkaReceiveTimestamp;
public ValueTime noCoTamSendTimestamp;
public ValueTime noCoTamReceiveTimestamp;
public ValueDuration offset;
private Map<PathName, ValueTime> theirZoneTimestamps;
private Map<Attribute, ValueTime> theirQueryTimestamps;
private List<PathName> zonesToSend;
private List<Attribute> queriesToSend;
private Set<PathName> waitingForZones;
private Set<Attribute> waitingForQueries;
private boolean initiating;
public GossipGirlState(long gossipId, PathName ourPath, ValueContact theirContact, boolean initiating) {
this.gossipId = gossipId;
this.ourPath = ourPath;
this.theirContact = theirContact;
this.initiating = initiating;
System.out.println("INFO: initializing Gossip state, their contact " + theirContact.toString());
if (initiating) {
state = State.WAIT_FOR_STATE_INITIALIZER;
} else {
state = State.APPLY_HEJKA;
}
this.lastAction = ValueUtils.currentTime();
}
public void setLastAction() {
lastAction = ValueUtils.currentTime();
}
public void setState(ZMI hierarchy, Map<Attribute, ValueQuery> queries) {
switch (state) {
case WAIT_FOR_STATE_INITIALIZER:
this.hierarchy = hierarchy;
this.queries = queries;
state = State.SEND_HEJKA;
break;
case WAIT_FOR_STATE_RESPONDER:
this.hierarchy = hierarchy;
this.queries = queries;
state = State.SEND_NO_CO_TAM;
break;
default:
System.out.println("ERROR: tried to set gossip state when not expected");
state = State.ERROR;
}
}
public void sentHejka() {
switch (state) {
case SEND_HEJKA:
state = state.WAIT_FOR_NO_CO_TAM;
break;
default:
System.out.println("ERROR: tried to set gossip state when not expected");
state = State.ERROR;
}
}
public void sentNoCoTam() {
switch (state) {
case SEND_NO_CO_TAM:
state = state.WAIT_FOR_FIRST_INFO;
break;
default:
System.out.println("ERROR: tried to set gossip state when not expected");
state = State.ERROR;
}
}
public void handleHejka(HejkaMessage message) {
switch (state) {
case APPLY_HEJKA:
theirGossipId = message.getSenderGossipId();
theirZoneTimestamps = message.getZoneTimestamps();
theirQueryTimestamps = message.getQueryTimestamps();
hejkaSendTimestamp = message.getSentTimestamp();
hejkaReceiveTimestamp = message.getReceivedTimestamp();
state = State.WAIT_FOR_STATE_RESPONDER;
break;
default:
System.out.println("ERROR: tried to set gossip state when not expected");
state = State.ERROR;
}
}
public void handleNoCoTam(NoCoTamMessage message) {
System.out.println("DEBUG: in GossipGirlState handleNoCoTam");
switch (state) {
case WAIT_FOR_NO_CO_TAM:
System.out.println("DEBUG: lets do this");
theirGossipId = message.getSenderGossipId();
theirZoneTimestamps = message.getZoneTimestamps();
theirQueryTimestamps = message.getQueryTimestamps();
hejkaSendTimestamp = message.getHejkaSendTimestamp();
hejkaReceiveTimestamp = message.getHejkaReceiveTimestamp();
noCoTamSendTimestamp = message.getSentTimestamp();
noCoTamReceiveTimestamp = message.getReceivedTimestamp();
computeOffset();
System.out.println("DEBUG: set basic stuff");
setZonesToSend();
setQueriesToSend();
setWaitingFor();
System.out.println("DEBUG: set big stuff");
state = State.SEND_INFO;
break;
default:
System.out.println("ERROR: tried to set gossip state when not expected");
state = State.ERROR;
}
}
public void computeOffset() {
ValueDuration rtd = (ValueDuration) (noCoTamReceiveTimestamp.subtract(hejkaSendTimestamp))
.subtract(noCoTamSendTimestamp.subtract(hejkaReceiveTimestamp));
offset = (ValueDuration) (noCoTamSendTimestamp.addValue(rtd.divide(new ValueInt(2l))))
.subtract(noCoTamReceiveTimestamp);
System.out.println("INFO: GossipGirlState calculated offset: " + offset.toString());
}
public ValueDuration delta() {
ValueDuration delta = offset;
if (!initiating) {
delta = delta.negate();
}
return delta;
}
public AttributesMap modifyAttributes(AttributesMap attributes) {
attributes.addOrChange("timestamp", attributes.getOrNull("timestamp").subtract(delta()));
return attributes;
}
private void setWaitingFor() {
setWaitingForZones();
setWaitingForQueries();
}
private void setWaitingForZones() {
waitingForZones = new HashSet(theirZoneTimestamps.keySet());
for (PathName path : zonesToSend) {
waitingForZones.remove(path);
}
}
private void setWaitingForQueries() {
waitingForQueries = new HashSet(theirQueryTimestamps.keySet());
for (Attribute name : queriesToSend) {
waitingForQueries.remove(name);
}
}
public Map<PathName, ValueTime> getZoneTimestampsToSend() {
Map<PathName, ValueTime> timestamps = new HashMap();
System.out.println("Getting zone timestamps to send to " + theirContact.getName().toString());
System.out.println("hierarchy is " + hierarchy.toString());
collectZoneTimestamps(timestamps, hierarchy, theirContact.getName());
return timestamps;
}
public Map<Attribute, ValueTime> getQueryTimestampsToSend() {
Map<Attribute, ValueTime> queryTimestamps= new HashMap();
for (Entry<Attribute, ValueQuery> query : queries.entrySet()) {
queryTimestamps.put(query.getKey(), new ValueTime(query.getValue().getTimestamp()));
}
return queryTimestamps;
}
public void setZonesToSend() {
zonesToSend = new LinkedList();
System.out.println("DEBUG: timestamps to send: " + getZoneTimestampsToSend().toString());
for (Entry<PathName, ValueTime> timestampedPath : getZoneTimestampsToSend().entrySet()) {
ValueTime theirTimestamp = theirZoneTimestamps.get(timestampedPath.getKey());
if (theirTimestamp == null || ValueUtils.valueLower(theirTimestamp.subtract(delta()), timestampedPath.getValue())) {
zonesToSend.add(timestampedPath.getKey());
}
}
System.out.println("DEBUG: zones to send: " + zonesToSend.toString());
}
public void setQueriesToSend() {
queriesToSend = new LinkedList();
for (Entry<Attribute, ValueTime> timestampedQuery : getQueryTimestampsToSend().entrySet()) {
ValueTime theirTimestamp = theirQueryTimestamps.get(timestampedQuery.getKey());
if (theirTimestamp == null || ValueUtils.valueLower(theirTimestamp, timestampedQuery.getValue())) {
queriesToSend.add(timestampedQuery.getKey());
}
}
System.out.println("DEBUG: Queries to send: " + queriesToSend.toString());
}
public List<ZMI> getZMIsToSend() {
List<ZMI> zmis = new LinkedList();
for (PathName path : zonesToSend) {
try {
zmis.add(hierarchy.findDescendant(path));
} catch (ZMI.NoSuchZoneException e) {
System.out.println("ERROR: didn't find a zone we wanted to send in getZMIsToSend");
}
}
return zmis;
}
public List<Entry<Attribute, ValueQuery>> getQueriesToSend() {
List<Entry<Attribute, ValueQuery>> queryList = new LinkedList();
for (Attribute name : queriesToSend) {
queryList.add(
new SimpleImmutableEntry(
name,
queries.get(name)
)
);
}
return queryList;
}
public void collectZoneTimestamps(Map<PathName, ValueTime> timestamps, ZMI currentZMI, PathName recipientPath) {
System.out.println("collecting timestamps, on " + currentZMI.getPathName().toString());
for (ZMI zmi : currentZMI.getSons()) {
if (interestedIn(recipientPath, zmi.getPathName())) {
ValueTime timestamp = (ValueTime) zmi.getAttributes().getOrNull("timestamp");
if (timestamp != null) {
timestamps.put(zmi.getPathName(), timestamp);
} else {
System.out.println("ERROR: collectZoneTimestamps encountered a zone with no timestamp");
}
} else {
collectZoneTimestamps(timestamps, zmi, recipientPath);
}
}
}
public boolean interestedIn(PathName recipientPath, PathName zmiPath) {
return ValueUtils.isPrefix(zmiPath.levelUp(), recipientPath) && !ValueUtils.isPrefix(zmiPath, recipientPath);
}
public void sentInfo() {
switch (state) {
case SEND_INFO:
state = State.WAIT_FOR_INFO;
break;
case SEND_INFO_AND_FINISH:
state = State.FINISHED;
break;
default:
System.out.println("ERROR: tried to set gossip state when not expected");
state = State.ERROR;
}
}
public void gotAttributes(AttributesMessage message) {
switch (state) {
case WAIT_FOR_INFO:
if (!waitingForZones.remove(message.getPath())) {
System.out.println("DEBUG: got zone attributes we weren't expecting");
}
if (waitingForZones.isEmpty() && waitingForQueries.isEmpty()) {
System.out.println("INFO: done waiting for info");
state = state.FINISHED;
}
break;
case WAIT_FOR_FIRST_INFO:
// TODO: use offset to setup GTP
offset = message.getOffset();
setZonesToSend();
setQueriesToSend();
setWaitingFor();
state = State.SEND_INFO;
if (!waitingForZones.remove(message.getPath())) {
System.out.println("DEBUG: got zone attributes we weren't expecting");
}
if (waitingForZones.isEmpty() && waitingForQueries.isEmpty()) {
System.out.println("INFO: done waiting for info");
state = state.SEND_INFO_AND_FINISH;
}
break;
default:
System.out.println("ERROR: got attributes when not expected");
state = State.ERROR;
}
}
public void gotQuery(QueryMessage message) {
switch (state) {
case WAIT_FOR_FIRST_INFO:
// TODO: use offset to setup GTP
offset = message.getOffset();
setZonesToSend();
setQueriesToSend();
setWaitingFor();
state = State.SEND_INFO;
if (!waitingForQueries.remove(message.getName())) {
System.out.println("DEBUG: got query we weren't expecting");
}
if (waitingForZones.isEmpty() && waitingForQueries.isEmpty()) {
System.out.println("INFO: done waiting for info");
state = state.SEND_INFO_AND_FINISH;
}
break;
case WAIT_FOR_INFO:
if (!waitingForQueries.remove(message.getName())) {
System.out.println("DEBUG: got query we weren't expecting");
}
if (waitingForZones.isEmpty() && waitingForQueries.isEmpty()) {
System.out.println("INFO: done waiting for info");
state = state.FINISHED;
}
break;
default:
System.out.println("ERROR: got query when not expected");
state = State.ERROR;
}
}
public ValueTime getTheirQueryTimestamp(Attribute name) {
return theirQueryTimestamps.get(name);
}
}
|