-
Notifications
You must be signed in to change notification settings - Fork 3
/
MVCC.java
436 lines (329 loc) · 15.4 KB
/
MVCC.java
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
package main;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import static java.lang.Integer.max;
public class MVCC {
private ConcurrentHashMap<String, ConcurrentHashMap<Integer, Integer>> database;
private ConcurrentHashMap<String, Integer> committed;
private volatile int counter;
private boolean earlyAborts;
private volatile int lastCommit;
// private ConcurrentHashMap<String, Transaction> rts;
private ConcurrentHashMap<String, Integer> wts;
private ConcurrentHashMap<String, Transaction> rts;
private int precommit;
private List<Transaction> active;
private ConcurrentHashMap<String, String> locks;
private ConcurrentHashMap<String, List<Transaction>> touched;
private ConcurrentHashMap<String, Lock> writeLocks;
private ReadWriteLock rwlock = new ReentrantReadWriteLock();
public MVCC(boolean earlyAborts) {
this.earlyAborts = earlyAborts;
this.database = new ConcurrentHashMap<>();
this.committed = new ConcurrentHashMap<>();
this.active = Collections.synchronizedList(new ArrayList<>());
this.counter = 0;
this.lastCommit = 0;
this.wts = new ConcurrentHashMap<String, Integer>();
this.rts = new ConcurrentHashMap<>();
this.locks = new ConcurrentHashMap<>();
this.touched = new ConcurrentHashMap<>();
this.writeLocks = new ConcurrentHashMap<>();
}
public synchronized int issue(Transaction transaction) {
int token = this.counter;
this.counter = this.counter + 1;
abort(transaction);
return token;
}
public void dump() {
System.out.println(database);
}
public void ensure_keys(String... keys) {
for (String key : keys) {
System.out.println(String.format("%s doesn't exist, creating", key));
ConcurrentHashMap<Integer, Integer> newdata = new ConcurrentHashMap<>();
database.put(key, newdata);
touched.put(key, Collections.synchronizedList(new ArrayList<Transaction>()));
}
}
public Integer getLatest(String key) {
return database.get(key).get(committed.get(key));
}
public Integer getHighest(String key) {
ArrayList<Integer> versions = new ArrayList<>(database.get(key).keySet());
versions.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
return database.get(key).get(versions.get(0));
}
public boolean versionsInOrder(String key) {
ConcurrentHashMap<Integer, Integer> versionDb = database.get(key);
ArrayList<Integer> versions = Collections.list(versionDb.keys());
versions.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o1 - o2;
}
});
int previous = versionDb.get(versions.get(0));
System.out.println(previous);
for (int i = 1; i < versions.size(); i++) {
Integer version = versions.get(i);
int difference = versionDb.get(version) - previous;
if (difference > 1) {
System.out.println(versionDb.get(version));
System.out.println(difference);
return false;
}
previous = versionDb.get(version);
}
return true;
}
public void clear(Transaction transaction) {
abort(transaction);
}
class Writehandle {
String key;
public Integer timestamp;
public Writehandle(String key, Integer timestamp) {
this.key = key;
this.timestamp = timestamp;
}
}
public interface Transaction {
List<Writehandle> getWritehandles();
int getTimestamp();
void setAborted(boolean aborted);
void clear(MVCC mvcc);
void addWrite(Writehandle writehandle);
boolean getAborted();
void setTimestamp(int timestamp);
void cancel();
boolean getCancelled();
void addChallenger(Transaction transaction);
List<Transaction> getChallengers();
void addRead(Read readHandle);
List<Read> getReadHandles();
boolean checkChallengers(MVCC mvcc, Transaction transaction);
void markPrecommit();
boolean getPrecommit();
void markRestart(boolean restart);
boolean getRestart();
int getNumberOfAttempts();
void markSuccessful();
boolean getSuccessful();
void addLock(String key, Lock lock);
Lock getLock(String key);
}
public Writehandle intend_to_write(Transaction transaction, String key, Integer value, Integer timestamp) {
String conflictType = "";
boolean restart = false;
if (earlyAborts) {
Transaction peek = rts.get(key);
if (peek != null && shouldRestart(transaction, peek)) {
System.out.println(String.format("%d wins against %d", peek, transaction.getTimestamp()));
restart = true;
}
if (restart) {
// System.out.println(String.format("%d %s failed - early abort", transaction.getTimestamp(), conflictType));
return null;
}
}
Lock lock = null;
if (writeLocks.containsKey(key)) {
lock = writeLocks.get(key);
} else {
lock = rwlock.writeLock();
writeLocks.put(key, lock);
}
System.out.println(String.format("t%d write %d", transaction.getTimestamp(), value));
transaction.addLock(key, lock);
lock.lock();
database.get(key).put(transaction.getTimestamp(), value);
wts.put(key, transaction.getTimestamp());
Writehandle writehandle = new Writehandle(key, timestamp);
transaction.addWrite(writehandle);
return writehandle;
}
public Read read(Transaction transaction, String key) {
ConcurrentHashMap<Integer, Integer> values = database.get(key);
Integer lastKnownCommit = committed.get(key);
// System.out.println(String.format("%d Values in database for key %s %s committed %d", transaction.getTimestamp(), key, values, committed.get(key)));
ArrayList<Integer> versions = new ArrayList<>(values.keySet());
versions.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
// read your own writes
if (values.containsKey(transaction.getTimestamp())) {
return new Read(values.get(transaction.getTimestamp()), transaction.getTimestamp());
}
for (Integer version : versions) {
// System.out.println(String.format("t%d Inspecting version %d", transaction.getTimestamp(), version));
if (version <= transaction.getTimestamp()) {
if (version >= lastCommit && version.equals(lastKnownCommit)) {
Integer read = values.get(version);
Transaction peek = rts.get(key);
if (peek != null && (shouldRestart(transaction, peek))) {
System.out.println(String.format("t%d %d should win %b", transaction.getTimestamp(), peek.getTimestamp(), peek.getAborted()));
Thread.yield();
return null;
}
if (peek != null) {
peek.addChallenger(transaction);
}
rts.put(key, transaction);
touched.get(key).add(transaction);
int peekTimestamp = 0;
if (peek != null) {
peekTimestamp = peek.getTimestamp();
}
System.out.println(String.format("%d t%d read %s %d %d", System.nanoTime(), transaction.getTimestamp(), key, read, peekTimestamp));
if (read == null) {
System.out.println("ERROR");
throw new IllegalArgumentException();
}
Read readHandle = new Read(read, lastKnownCommit);
transaction.addRead(readHandle);
return readHandle;
} else {
// System.out.println(String.format("t%d %d Version is not equal to committed %d %d", transaction.getTimestamp(), version, lastKnownCommit, lastCommit));
}
} else {
// System.out.println("Ignoring newer value");
}
}
// System.out.println(String.format("%d No versions valid", transaction.getTimestamp()));
return null;
}
public boolean shouldRestart(Transaction transaction, Transaction peek) {
boolean defeated = (((peek.getTimestamp() < transaction.getTimestamp() ||
(transaction.getNumberOfAttempts() < peek.getNumberOfAttempts())) && peek.getPrecommit()) ||
peek.getPrecommit() && (peek.getTimestamp() > transaction.getTimestamp() ||
(peek.getNumberOfAttempts() > transaction.getNumberOfAttempts() && peek.getPrecommit())
&& !peek.getRestart()));
return defeated;
}
public class Read {
Integer value;
Integer timestamp;
public Read(Integer value, Integer timestamp) {
this.value = value;
this.timestamp = timestamp;
}
}
public void commit(Transaction transaction) {
transaction.markPrecommit();
// synchronized (locks.get(transaction.createLockKey())) {
boolean restart = false;
String conflictType = "";
if (transaction.getTimestamp() < lastCommit) {
transaction.markRestart(true);
conflictType = "ahead";
}
for (Writehandle writehandle : transaction.getWritehandles()) {
Transaction peek = rts.get(writehandle.key);
int peakTimestamp = 0;
if (peek != null) {
peakTimestamp = peek.getTimestamp();
}
System.out.println(String.format("%d t%d begin commit (peek=%d)", System.nanoTime(), transaction.getTimestamp(), peakTimestamp));
if (peek != null && (shouldRestart(transaction, peek))) {
System.out.println(String.format("%d wins against %d", peek.getTimestamp(), transaction.getTimestamp()));
transaction.markRestart(true);
conflictType = "read";
break;
}
if (touched.containsKey(writehandle.key)) {
List<Transaction> transactions = touched.get(writehandle.key);
System.out.println(String.format("t%d checking touched transactions", transaction.getTimestamp()));
synchronized (transactions) {
for (Transaction other : transactions) {
if (shouldRestart(transaction, other)) {
transaction.markRestart(true);
break;
}
}
}
System.out.println(String.format("t%d finished checking touched transactions", transaction.getTimestamp()));
}
if (committed.containsKey(writehandle.key) && writehandle.timestamp != null && !committed.get(writehandle.key).equals(writehandle.timestamp)) {
transaction.markRestart(true);
conflictType = "commit";
break;
}
}
System.out.println(String.format("t%d finished committed checks", transaction.getTimestamp()));
// System.out.println(String.format("%d Checking challengers %d", transaction.getTimestamp(), challengers.size()));
if (transaction.checkChallengers(this, transaction)) {
transaction.markRestart(true);
conflictType = "challenger";
}
System.out.println(String.format("t%d Challengers checked", transaction.getTimestamp()));
precommit = max(transaction.getTimestamp(), precommit);
// transaction.markSuccessful();
if (transaction.getRestart()) {
System.out.println(String.format("t%d Conflict %s Restarting transaction", transaction.getTimestamp(), conflictType));
abort(transaction);
Thread.yield();
} else {
System.out.println(String.format("%d Passed checks, committing...", transaction.getTimestamp()));
transaction.setAborted(false);
for (Writehandle writehandle : transaction.getWritehandles()) {
System.out.println(String.format("t%d Updating committed...", transaction.getTimestamp()));
committed.put(writehandle.key, transaction.getTimestamp());
System.out.println(String.format("t%d Wrote committed", transaction.getTimestamp()));
System.out.println(String.format("%d t%d write %s %d", System.nanoTime(), transaction.getTimestamp(), writehandle.key, database.get(writehandle.key).get(transaction.getTimestamp())));
if (rts.get(writehandle.key) == transaction) {
rts.remove(writehandle.key);
}
touched.get(writehandle.key).clear();
transaction.getLock(writehandle.key).unlock();
}
lastCommit = transaction.getTimestamp();
System.out.println(String.format("%d t%d won committed", System.nanoTime(), transaction.getTimestamp()));
transaction.setTimestamp(Integer.MAX_VALUE);
}
// }
}
public void abort(Transaction transaction) {
transaction.setAborted(true);
for (Writehandle writehandle : transaction.getWritehandles()) {
database.get(writehandle.key).remove(transaction.getTimestamp());
if (rts.get(writehandle.key) == transaction) {
rts.remove(writehandle.key);
}
List<Transaction> transactions = touched.get(writehandle.key);
transactions.remove(transaction);
}
transaction.clear(this);
}
public void printDuplicates(String key) {
HashSet<Integer> found = new HashSet<>();
for (Integer version : database.get(key).keySet()) {
Integer value = database.get(key).get(version);
if (!found.add(value)) {
System.out.println(String.format("%s %d is duplicated %d ", key, value, version));
}
}
}
public Integer getHighestVersion(String key) {
ArrayList<Integer> list = Collections.list(database.get(key).keys());
list.sort(new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
return o2 - o1;
}
});
return list.get(0);
}
}