3 import java.util.Iterator;
4 import java.util.Random;
5 import java.util.Arrays;
9 import java.util.Vector;
10 import java.util.HashMap;
11 import java.util.HashSet;
12 import java.util.ArrayList;
13 import java.util.Collections;
14 import java.nio.ByteBuffer;
17 * IoTTable data structure. Provides client interface.
18 * @author Brian Demsky
22 final public class Table {
25 static final int FREE_SLOTS = 10; // Number of slots that should be kept free
26 static final int SKIP_THRESHOLD = 10;
27 static final double RESIZE_MULTIPLE = 1.2;
28 static final double RESIZE_THRESHOLD = 0.75;
29 static final int REJECTED_THRESHOLD = 5;
32 private SlotBuffer buffer = null;
33 private CloudComm cloud = null;
34 private Random random = null;
35 private TableStatus liveTableStatus = null;
36 private PendingTransaction pendingTransactionBuilder = null; // Pending Transaction used in building a Pending Transaction
37 private Transaction lastPendingTransactionSpeculatedOn = null; // Last transaction that was speculated on from the pending transaction
38 private Transaction firstPendingTransaction = null; // first transaction in the pending transaction list
41 private int numberOfSlots = 0; // Number of slots stored in buffer
42 private int bufferResizeThreshold = 0; // Threshold on the number of live slots before a resize is needed
43 private long liveSlotCount = 0; // Number of currently live slots
44 private long oldestLiveSlotSequenceNumver = 0; // Smallest sequence number of the slot with a live entry
45 private long localMachineId = 0; // Machine ID of this client device
46 private long sequenceNumber = 0; // Largest sequence number a client has received
47 // private int smallestTableStatusSeen = -1; // Smallest Table Status that was seen in the latest slots sent from the server
48 // private int largestTableStatusSeen = -1; // Largest Table Status that was seen in the latest slots sent from the server
49 private long localTransactionSequenceNumber = 0; // Local sequence number counter for transactions
50 private long lastTransactionSequenceNumberSpeculatedOn = -1; // the last transaction that was speculated on
51 private long oldestTransactionSequenceNumberSpeculatedOn = -1; // the oldest transaction that was speculated on
52 private long localArbitrationSequenceNumber = 0;
53 private boolean hadPartialSendToServer = false;
54 private boolean attemptedToSendToServer = false;
55 private long expectedsize;
56 private boolean didFindTableStatus = false;
57 private long currMaxSize = 0;
59 private Slot lastSlotAttemptedToSend = null;
60 private boolean lastIsNewKey = false;
61 private int lastNewSize = 0;
62 private Map<Transaction, List<Integer>> lastTransactionPartsSent = null;
63 private List<Entry> lastPendingSendArbitrationEntriesToDelete = null;
64 private NewKey lastNewKey = null;
68 private Map<IoTString, KeyValue> committedKeyValueTable = null; // Table of committed key value pairs
69 private Map<IoTString, KeyValue> speculatedKeyValueTable = null; // Table of speculated key value pairs, if there is a speculative value
70 private Map<IoTString, KeyValue> pendingTransactionSpeculatedKeyValueTable = null; // Table of speculated key value pairs, if there is a speculative value from the pending transactions
71 private Map<IoTString, NewKey> liveNewKeyTable = null; // Table of live new keys
72 private HashMap<Long, Pair<Long, Liveness>> lastMessageTable = null; // Last message sent by a client machine id -> (Seq Num, Slot or LastMessage);
73 private HashMap<Long, HashSet<RejectedMessage>> rejectedMessageWatchListTable = null; // Table of machine Ids and the set of rejected messages they have not seen yet
74 private Map<IoTString, Long> arbitratorTable = null; // Table of keys and their arbitrators
75 private Map<Pair<Long, Long>, Abort> liveAbortTable = null; // Table live abort messages
76 private Map<Long, Map<Pair<Long, Integer>, TransactionPart>> newTransactionParts = null; // transaction parts that are seen in this latest round of slots from the server
77 private Map<Long, Map<Pair<Long, Integer>, CommitPart>> newCommitParts = null; // commit parts that are seen in this latest round of slots from the server
78 private Map<Long, Long> lastArbitratedTransactionNumberByArbitratorTable = null; // Last transaction sequence number that an arbitrator arbitrated on
79 private Map<Long, Transaction> liveTransactionBySequenceNumberTable = null; // live transaction grouped by the sequence number
80 private Map<Pair<Long, Long>, Transaction> liveTransactionByTransactionIdTable = null; // live transaction grouped by the transaction ID
81 private Map<Long, Map<Long, Commit>> liveCommitsTable = null;
82 private Map<IoTString, Commit> liveCommitsByKeyTable = null;
83 private Map<Long, Long> lastCommitSeenSequenceNumberByArbitratorTable = null;
84 private Vector<Long> rejectedSlotList = null; // List of rejected slots that have yet to be sent to the server
85 private List<Transaction> pendingTransactionQueue = null;
86 private List<ArbitrationRound> pendingSendArbitrationRounds = null;
87 private List<Entry> pendingSendArbitrationEntriesToDelete = null;
88 private Map<Transaction, List<Integer>> transactionPartsSent = null;
89 private Map<Long, TransactionStatus> outstandingTransactionStatus = null;
90 private Map<Long, Abort> liveAbortsGeneratedByLocal = null;
91 private Set<Pair<Long, Long>> offlineTransactionsCommittedAndAtServer = null;
92 private Map<Long, Pair<String, Integer>> localCommunicationTable = null;
93 private Map<Long, Long> lastTransactionSeenFromMachineFromServer = null;
94 private Map<Long, Long> lastArbitrationDataLocalSequenceNumberSeenFromArbitrator = null;
97 public Table(String baseurl, String password, long _localMachineId, int listeningPort) {
98 localMachineId = _localMachineId;
99 cloud = new CloudComm(this, baseurl, password, listeningPort);
104 public Table(CloudComm _cloud, long _localMachineId) {
105 localMachineId = _localMachineId;
112 * Init all the stuff needed for for table usage
114 private void init() {
116 // Init helper objects
117 random = new Random();
118 buffer = new SlotBuffer();
121 oldestLiveSlotSequenceNumver = 1;
124 committedKeyValueTable = new HashMap<IoTString, KeyValue>();
125 speculatedKeyValueTable = new HashMap<IoTString, KeyValue>();
126 pendingTransactionSpeculatedKeyValueTable = new HashMap<IoTString, KeyValue>();
127 liveNewKeyTable = new HashMap<IoTString, NewKey>();
128 lastMessageTable = new HashMap<Long, Pair<Long, Liveness>>();
129 rejectedMessageWatchListTable = new HashMap<Long, HashSet<RejectedMessage>>();
130 arbitratorTable = new HashMap<IoTString, Long>();
131 liveAbortTable = new HashMap<Pair<Long, Long>, Abort>();
132 newTransactionParts = new HashMap<Long, Map<Pair<Long, Integer>, TransactionPart>>();
133 newCommitParts = new HashMap<Long, Map<Pair<Long, Integer>, CommitPart>>();
134 lastArbitratedTransactionNumberByArbitratorTable = new HashMap<Long, Long>();
135 liveTransactionBySequenceNumberTable = new HashMap<Long, Transaction>();
136 liveTransactionByTransactionIdTable = new HashMap<Pair<Long, Long>, Transaction>();
137 liveCommitsTable = new HashMap<Long, Map<Long, Commit>>();
138 liveCommitsByKeyTable = new HashMap<IoTString, Commit>();
139 lastCommitSeenSequenceNumberByArbitratorTable = new HashMap<Long, Long>();
140 rejectedSlotList = new Vector<Long>();
141 pendingTransactionQueue = new ArrayList<Transaction>();
142 pendingSendArbitrationEntriesToDelete = new ArrayList<Entry>();
143 transactionPartsSent = new HashMap<Transaction, List<Integer>>();
144 outstandingTransactionStatus = new HashMap<Long, TransactionStatus>();
145 liveAbortsGeneratedByLocal = new HashMap<Long, Abort>();
146 offlineTransactionsCommittedAndAtServer = new HashSet<Pair<Long, Long>>();
147 localCommunicationTable = new HashMap<Long, Pair<String, Integer>>();
148 lastTransactionSeenFromMachineFromServer = new HashMap<Long, Long>();
149 pendingSendArbitrationRounds = new ArrayList<ArbitrationRound>();
150 lastArbitrationDataLocalSequenceNumberSeenFromArbitrator = new HashMap<Long, Long>();
154 numberOfSlots = buffer.capacity();
155 setResizeThreshold();
158 // TODO: delete method
159 public synchronized void printSlots() {
160 long o = buffer.getOldestSeqNum();
161 long n = buffer.getNewestSeqNum();
163 int[] types = new int[10];
169 for (long i = o; i < (n + 1); i++) {
170 Slot s = buffer.getSlot(i);
172 Vector<Entry> entries = s.getEntries();
174 for (Entry e : entries) {
176 int type = e.getType();
177 types[type] = types[type] + 1;
186 for (int i = 0; i < 10; i++) {
187 System.out.println(i + " " + types[i]);
189 System.out.println("Live count: " + livec);
190 System.out.println("Dead count: " + deadc);
191 System.out.println("Old: " + o);
192 System.out.println("New: " + n);
193 System.out.println("Size: " + buffer.size());
194 // System.out.println("Commits: " + liveCommitsTable.size());
195 System.out.println("pendingTrans: " + pendingTransactionQueue.size());
196 System.out.println("Trans Status Out: " + outstandingTransactionStatus.size());
198 for (Long k : lastArbitratedTransactionNumberByArbitratorTable.keySet()) {
199 System.out.println(k + ": " + lastArbitratedTransactionNumberByArbitratorTable.get(k));
203 for (Long a : liveCommitsTable.keySet()) {
204 for (Long b : liveCommitsTable.get(a).keySet()) {
205 for (KeyValue kv : liveCommitsTable.get(a).get(b).getKeyValueUpdateSet()) {
206 System.out.print(kv + " ");
208 System.out.print("|| ");
210 System.out.println();
216 * Initialize the table by inserting a table status as the first entry into the table status
217 * also initialize the crypto stuff.
219 public synchronized void initTable() throws ServerException {
220 cloud.initSecurity();
222 // Create the first insertion into the block chain which is the table status
223 Slot s = new Slot(this, 1, localMachineId);
224 TableStatus status = new TableStatus(s, numberOfSlots);
226 Slot[] array = cloud.putSlot(s, numberOfSlots);
229 array = new Slot[] {s};
230 // update local block chain
231 validateAndUpdate(array, true);
232 } else if (array.length == 1) {
233 // in case we did push the slot BUT we failed to init it
234 validateAndUpdate(array, true);
236 throw new Error("Error on initialization");
241 * Rebuild the table from scratch by pulling the latest block chain from the server.
243 public synchronized void rebuild() throws ServerException {
244 // Just pull the latest slots from the server
245 Slot[] newslots = cloud.getSlots(sequenceNumber + 1);
246 validateAndUpdate(newslots, true);
249 // public String toString() {
250 // String retString = " Committed Table: \n";
251 // retString += "---------------------------\n";
252 // retString += commitedTable.toString();
254 // retString += "\n\n";
256 // retString += " Speculative Table: \n";
257 // retString += "---------------------------\n";
258 // retString += speculativeTable.toString();
263 public synchronized void addLocalCommunication(long arbitrator, String hostName, int portNumber) {
264 localCommunicationTable.put(arbitrator, new Pair<String, Integer>(hostName, portNumber));
267 public synchronized Long getArbitrator(IoTString key) {
268 return arbitratorTable.get(key);
271 public synchronized void close() {
275 public synchronized IoTString getCommitted(IoTString key) {
276 KeyValue kv = committedKeyValueTable.get(key);
279 return kv.getValue();
285 public synchronized IoTString getSpeculative(IoTString key) {
286 KeyValue kv = pendingTransactionSpeculatedKeyValueTable.get(key);
289 kv = speculatedKeyValueTable.get(key);
293 kv = committedKeyValueTable.get(key);
297 return kv.getValue();
303 public synchronized IoTString getCommittedAtomic(IoTString key) {
304 KeyValue kv = committedKeyValueTable.get(key);
306 if (arbitratorTable.get(key) == null) {
307 throw new Error("Key not Found.");
310 // Make sure new key value pair matches the current arbitrator
311 if (!pendingTransactionBuilder.checkArbitrator(arbitratorTable.get(key))) {
312 // TODO: Maybe not throw en error
313 throw new Error("Not all Key Values Match Arbitrator.");
317 pendingTransactionBuilder.addKVGuard(new KeyValue(key, kv.getValue()));
318 return kv.getValue();
320 pendingTransactionBuilder.addKVGuard(new KeyValue(key, null));
325 public synchronized IoTString getSpeculativeAtomic(IoTString key) {
326 if (arbitratorTable.get(key) == null) {
327 throw new Error("Key not Found.");
330 // Make sure new key value pair matches the current arbitrator
331 if (!pendingTransactionBuilder.checkArbitrator(arbitratorTable.get(key))) {
332 // TODO: Maybe not throw en error
333 throw new Error("Not all Key Values Match Arbitrator.");
336 KeyValue kv = pendingTransactionSpeculatedKeyValueTable.get(key);
339 kv = speculatedKeyValueTable.get(key);
343 kv = committedKeyValueTable.get(key);
347 pendingTransactionBuilder.addKVGuard(new KeyValue(key, kv.getValue()));
348 return kv.getValue();
350 pendingTransactionBuilder.addKVGuard(new KeyValue(key, null));
355 public synchronized boolean update() {
357 Slot[] newSlots = cloud.getSlots(sequenceNumber + 1);
358 validateAndUpdate(newSlots, false);
362 updateLiveTransactionsAndStatus();
365 } catch (Exception e) {
366 // e.printStackTrace();
372 public synchronized boolean createNewKey(IoTString keyName, long machineId) throws ServerException {
374 if (arbitratorTable.get(keyName) != null) {
375 // There is already an arbitrator
379 NewKey newKey = new NewKey(null, keyName, machineId);
380 if (sendToServer(newKey)) {
381 // If successfully inserted
387 public synchronized void startTransaction() {
388 // Create a new transaction, invalidates any old pending transactions.
389 pendingTransactionBuilder = new PendingTransaction(localMachineId);
392 public synchronized void addKV(IoTString key, IoTString value) {
394 // Make sure it is a valid key
395 if (arbitratorTable.get(key) == null) {
396 throw new Error("Key not Found.");
399 // Make sure new key value pair matches the current arbitrator
400 if (!pendingTransactionBuilder.checkArbitrator(arbitratorTable.get(key))) {
401 // TODO: Maybe not throw en error
402 throw new Error("Not all Key Values Match Arbitrator.");
405 // Add the key value to this transaction
406 KeyValue kv = new KeyValue(key, value);
407 pendingTransactionBuilder.addKV(kv);
410 public synchronized TransactionStatus commitTransaction() {
412 if (pendingTransactionBuilder.getKVUpdates().size() == 0) {
413 // transaction with no updates will have no effect on the system
414 return new TransactionStatus(TransactionStatus.StatusNoEffect, -1);
417 // Set the local transaction sequence number and increment
418 pendingTransactionBuilder.setClientLocalSequenceNumber(localTransactionSequenceNumber);
419 localTransactionSequenceNumber++;
421 // Create the transaction status
422 TransactionStatus transactionStatus = new TransactionStatus(TransactionStatus.StatusPending, pendingTransactionBuilder.getArbitrator());
424 // Create the new transaction
425 Transaction newTransaction = pendingTransactionBuilder.createTransaction();
426 newTransaction.setTransactionStatus(transactionStatus);
428 if (pendingTransactionBuilder.getArbitrator() != localMachineId) {
429 // Add it to the queue and invalidate the builder for safety
430 pendingTransactionQueue.add(newTransaction);
432 arbitrateOnLocalTransaction(newTransaction);
433 updateLiveStateFromLocal();
436 pendingTransactionBuilder = new PendingTransaction(localMachineId);
440 } catch (ServerException e) {
442 Set<Long> arbitratorTriedAndFailed = new HashSet<Long>();
443 for (Iterator<Transaction> iter = pendingTransactionQueue.iterator(); iter.hasNext(); ) {
444 Transaction transaction = iter.next();
446 if (arbitratorTriedAndFailed.contains(transaction.getArbitrator())) {
447 // Already contacted this client so ignore all attempts to contact this client
448 // to preserve ordering for arbitrator
452 Pair<Boolean, Boolean> sendReturn = sendTransactionToLocal(transaction);
454 if (sendReturn.getFirst()) {
455 // Failed to contact over local
456 arbitratorTriedAndFailed.add(transaction.getArbitrator());
458 // Successful contact or should not contact
460 if (sendReturn.getSecond()) {
468 updateLiveStateFromLocal();
470 return transactionStatus;
474 * Get the machine ID for this client
476 public long getMachineId() {
477 return localMachineId;
481 * Decrement the number of live slots that we currently have
483 public void decrementLiveCount() {
488 * Recalculate the new resize threshold
490 private void setResizeThreshold() {
491 int resizeLower = (int) (RESIZE_THRESHOLD * numberOfSlots);
492 bufferResizeThreshold = resizeLower - 1 + random.nextInt(numberOfSlots - resizeLower);
496 boolean lastInsertedNewKey = false;
498 private boolean sendToServer(NewKey newKey) throws ServerException {
500 boolean fromRetry = false;
503 if (hadPartialSendToServer) {
504 Slot[] newSlots = cloud.getSlots(sequenceNumber + 1);
505 if (newSlots.length == 0) {
507 ThreeTuple<Boolean, Boolean, Slot[]> sendSlotsReturn = sendSlotsToServer(lastSlotAttemptedToSend, lastNewSize, lastIsNewKey);
509 if (sendSlotsReturn.getFirst()) {
510 if (newKey != null) {
511 if (lastInsertedNewKey && (lastNewKey.getKey() == newKey.getKey()) && (lastNewKey.getMachineID() == newKey.getMachineID())) {
516 for (Transaction transaction : lastTransactionPartsSent.keySet()) {
517 transaction.resetServerFailure();
519 // Update which transactions parts still need to be sent
520 transaction.removeSentParts(lastTransactionPartsSent.get(transaction));
522 // Add the transaction status to the outstanding list
523 outstandingTransactionStatus.put(transaction.getSequenceNumber(), transaction.getTransactionStatus());
525 // Update the transaction status
526 transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentPartial);
528 // Check if all the transaction parts were successfully sent and if so then remove it from pending
529 if (transaction.didSendAllParts()) {
530 transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentFully);
531 pendingTransactionQueue.remove(transaction);
536 newSlots = sendSlotsReturn.getThird();
538 boolean isInserted = false;
539 for (Slot s : newSlots) {
540 if ((s.getSequenceNumber() == lastSlotAttemptedToSend.getSequenceNumber()) && (s.getMachineID() == localMachineId)) {
546 for (Slot s : newSlots) {
551 // Process each entry in the slot
552 for (Entry entry : s.getEntries()) {
554 if (entry.getType() == Entry.TypeLastMessage) {
555 LastMessage lastMessage = (LastMessage)entry;
556 if ((lastMessage.getMachineID() == localMachineId) && (lastMessage.getSequenceNumber() == lastSlotAttemptedToSend.getSequenceNumber())) {
565 if (newKey != null) {
566 if (lastInsertedNewKey && (lastNewKey.getKey() == newKey.getKey()) && (lastNewKey.getMachineID() == newKey.getMachineID())) {
571 for (Transaction transaction : lastTransactionPartsSent.keySet()) {
572 transaction.resetServerFailure();
574 // Update which transactions parts still need to be sent
575 transaction.removeSentParts(lastTransactionPartsSent.get(transaction));
577 // Add the transaction status to the outstanding list
578 outstandingTransactionStatus.put(transaction.getSequenceNumber(), transaction.getTransactionStatus());
580 // Update the transaction status
581 transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentPartial);
583 // Check if all the transaction parts were successfully sent and if so then remove it from pending
584 if (transaction.didSendAllParts()) {
585 transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentFully);
586 pendingTransactionQueue.remove(transaction);
588 transaction.resetServerFailure();
589 // Set the transaction sequence number back to nothing
590 if (!transaction.didSendAPartToServer()) {
591 transaction.setSequenceNumber(-1);
598 for (Transaction transaction : lastTransactionPartsSent.keySet()) {
599 transaction.resetServerFailure();
600 // Set the transaction sequence number back to nothing
601 if (!transaction.didSendAPartToServer()) {
602 transaction.setSequenceNumber(-1);
606 if (sendSlotsReturn.getThird().length != 0) {
607 // insert into the local block chain
608 validateAndUpdate(sendSlotsReturn.getThird(), true);
612 boolean isInserted = false;
613 for (Slot s : newSlots) {
614 if ((s.getSequenceNumber() == lastSlotAttemptedToSend.getSequenceNumber()) && (s.getMachineID() == localMachineId)) {
620 for (Slot s : newSlots) {
625 // Process each entry in the slot
626 for (Entry entry : s.getEntries()) {
628 if (entry.getType() == Entry.TypeLastMessage) {
629 LastMessage lastMessage = (LastMessage)entry;
630 if ((lastMessage.getMachineID() == localMachineId) && (lastMessage.getSequenceNumber() == lastSlotAttemptedToSend.getSequenceNumber())) {
639 if (newKey != null) {
640 if (lastInsertedNewKey && (lastNewKey.getKey() == newKey.getKey()) && (lastNewKey.getMachineID() == newKey.getMachineID())) {
645 for (Transaction transaction : lastTransactionPartsSent.keySet()) {
646 transaction.resetServerFailure();
648 // Update which transactions parts still need to be sent
649 transaction.removeSentParts(lastTransactionPartsSent.get(transaction));
651 // Add the transaction status to the outstanding list
652 outstandingTransactionStatus.put(transaction.getSequenceNumber(), transaction.getTransactionStatus());
654 // Update the transaction status
655 transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentPartial);
657 // Check if all the transaction parts were successfully sent and if so then remove it from pending
658 if (transaction.didSendAllParts()) {
659 transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentFully);
660 pendingTransactionQueue.remove(transaction);
662 transaction.resetServerFailure();
663 // Set the transaction sequence number back to nothing
664 if (!transaction.didSendAPartToServer()) {
665 transaction.setSequenceNumber(-1);
670 for (Transaction transaction : lastTransactionPartsSent.keySet()) {
671 transaction.resetServerFailure();
672 // Set the transaction sequence number back to nothing
673 if (!transaction.didSendAPartToServer()) {
674 transaction.setSequenceNumber(-1);
679 // insert into the local block chain
680 validateAndUpdate(newSlots, true);
683 } catch (ServerException e) {
689 // While we have stuff that needs inserting into the block chain
690 while ((pendingTransactionQueue.size() > 0) || (pendingSendArbitrationRounds.size() > 0) || (newKey != null)) {
693 if (hadPartialSendToServer) {
694 throw new Error("Should Be error free");
699 // If there is a new key with same name then end
700 if ((newKey != null) && (arbitratorTable.get(newKey.getKey()) != null)) {
701 System.out.println("New Key Fail");
706 Slot slot = new Slot(this, sequenceNumber + 1, localMachineId, buffer.getSlot(sequenceNumber).getHMAC());
708 // Try to fill the slot with data
709 ThreeTuple<Boolean, Integer, Boolean> fillSlotsReturn = fillSlot(slot, false, newKey);
710 boolean needsResize = fillSlotsReturn.getFirst();
711 int newSize = fillSlotsReturn.getSecond();
712 Boolean insertedNewKey = fillSlotsReturn.getThird();
715 // Reset which transaction to send
716 for (Transaction transaction : transactionPartsSent.keySet()) {
717 transaction.resetNextPartToSend();
719 // Set the transaction sequence number back to nothing
720 if (!transaction.didSendAPartToServer() && !transaction.getServerFailure()) {
721 transaction.setSequenceNumber(-1);
725 // Clear the sent data since we are trying again
726 pendingSendArbitrationEntriesToDelete.clear();
727 transactionPartsSent.clear();
729 // We needed a resize so try again
730 fillSlot(slot, true, newKey);
733 lastSlotAttemptedToSend = slot;
734 lastIsNewKey = (newKey != null);
735 lastInsertedNewKey = insertedNewKey;
736 lastNewSize = newSize;
738 lastTransactionPartsSent = new HashMap<Transaction, List<Integer>>(transactionPartsSent);
739 lastPendingSendArbitrationEntriesToDelete = new ArrayList<Entry>(pendingSendArbitrationEntriesToDelete);
742 ThreeTuple<Boolean, Boolean, Slot[]> sendSlotsReturn = sendSlotsToServer(slot, newSize, newKey != null);
744 if (sendSlotsReturn.getFirst()) {
746 // Did insert into the block chain
748 if (insertedNewKey) {
749 // This slot was what was inserted not a previous slot
751 // New Key was successfully inserted into the block chain so dont want to insert it again
755 // Remove the aborts and commit parts that were sent from the pending to send queue
756 for (Iterator<ArbitrationRound> iter = pendingSendArbitrationRounds.iterator(); iter.hasNext(); ) {
757 ArbitrationRound round = iter.next();
758 round.removeParts(pendingSendArbitrationEntriesToDelete);
760 if (round.isDoneSending()) {
761 // Sent all the parts
766 for (Transaction transaction : transactionPartsSent.keySet()) {
767 transaction.resetServerFailure();
769 // Update which transactions parts still need to be sent
770 transaction.removeSentParts(transactionPartsSent.get(transaction));
772 // Add the transaction status to the outstanding list
773 outstandingTransactionStatus.put(transaction.getSequenceNumber(), transaction.getTransactionStatus());
775 // Update the transaction status
776 transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentPartial);
778 // Check if all the transaction parts were successfully sent and if so then remove it from pending
779 if (transaction.didSendAllParts()) {
780 transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentFully);
781 pendingTransactionQueue.remove(transaction);
783 for (KeyValue kv : transaction.getKeyValueUpdateSet()) {
784 System.out.println("Sent: " + kv + " from: " + localMachineId + " Slot:" + slot.getSequenceNumber() + " Claimed:" + transaction.getSequenceNumber());
790 // if (!sendSlotsReturn.getSecond()) {
791 // for (Transaction transaction : lastTransactionPartsSent.keySet()) {
792 // transaction.resetServerFailure();
795 // for (Transaction transaction : lastTransactionPartsSent.keySet()) {
796 // transaction.resetServerFailure();
798 // // Update which transactions parts still need to be sent
799 // transaction.removeSentParts(transactionPartsSent.get(transaction));
801 // // Add the transaction status to the outstanding list
802 // outstandingTransactionStatus.put(transaction.getSequenceNumber(), transaction.getTransactionStatus());
804 // // Update the transaction status
805 // transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentPartial);
807 // // Check if all the transaction parts were successfully sent and if so then remove it from pending
808 // if (transaction.didSendAllParts()) {
809 // transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentFully);
810 // pendingTransactionQueue.remove(transaction);
812 // for (KeyValue kv : transaction.getKeyValueUpdateSet()) {
813 // System.out.println("Sent: " + kv + " from: " + localMachineId + " Slot:" + lastSlotAttemptedToSend.getSequenceNumber() + " Claimed:" + transaction.getSequenceNumber());
819 // Reset which transaction to send
820 for (Transaction transaction : transactionPartsSent.keySet()) {
821 transaction.resetNextPartToSend();
822 // transaction.resetNextPartToSend();
824 // Set the transaction sequence number back to nothing
825 if (!transaction.didSendAPartToServer() && !transaction.getServerFailure()) {
826 transaction.setSequenceNumber(-1);
831 // Clear the sent data in preparation for next send
832 pendingSendArbitrationEntriesToDelete.clear();
833 transactionPartsSent.clear();
835 if (sendSlotsReturn.getThird().length != 0) {
836 // insert into the local block chain
837 validateAndUpdate(sendSlotsReturn.getThird(), true);
841 } catch (ServerException e) {
843 System.out.println("Server Failure: " + e.getType());
844 for (Transaction transaction : transactionPartsSent.keySet()) {
845 for (KeyValue kv : transaction.getKeyValueUpdateSet()) {
846 System.out.println("Sent Error: " + kv + " " + e.getType());
850 if (e.getType() != ServerException.TypeInputTimeout) {
851 // e.printStackTrace();
853 // Nothing was able to be sent to the server so just clear these data structures
854 for (Transaction transaction : transactionPartsSent.keySet()) {
855 transaction.resetNextPartToSend();
857 // Set the transaction sequence number back to nothing
858 if (!transaction.didSendAPartToServer() && !transaction.getServerFailure()) {
859 transaction.setSequenceNumber(-1);
863 // There was a partial send to the server
864 hadPartialSendToServer = true;
868 // lastTransactionPartsSent = new HashMap<Transaction, List<Integer>>(transactionPartsSent);
869 // lastPendingSendArbitrationEntriesToDelete = new ArrayList<Entry>(pendingSendArbitrationEntriesToDelete);
872 // Nothing was able to be sent to the server so just clear these data structures
873 for (Transaction transaction : transactionPartsSent.keySet()) {
874 transaction.resetNextPartToSend();
875 transaction.setServerFailure();
879 pendingSendArbitrationEntriesToDelete.clear();
880 transactionPartsSent.clear();
885 return newKey == null;
888 public synchronized boolean updateFromLocal(long machineId) {
889 Pair<String, Integer> localCommunicationInformation = localCommunicationTable.get(machineId);
890 if (localCommunicationInformation == null) {
891 // Cant talk to that device locally so do nothing
895 // Get the size of the send data
896 int sendDataSize = Integer.BYTES + Long.BYTES;
898 Long lastArbitrationDataLocalSequenceNumber = (long) - 1;
899 if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(machineId) != null) {
900 lastArbitrationDataLocalSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(machineId);
903 byte[] sendData = new byte[sendDataSize];
904 ByteBuffer bbEncode = ByteBuffer.wrap(sendData);
907 bbEncode.putLong(lastArbitrationDataLocalSequenceNumber);
911 byte[] returnData = cloud.sendLocalData(sendData, localCommunicationInformation.getFirst(), localCommunicationInformation.getSecond());
913 if (returnData == null) {
914 // Could not contact server
919 ByteBuffer bbDecode = ByteBuffer.wrap(returnData);
920 int numberOfEntries = bbDecode.getInt();
922 for (int i = 0; i < numberOfEntries; i++) {
923 byte type = bbDecode.get();
924 if (type == Entry.TypeAbort) {
925 Abort abort = (Abort)Abort.decode(null, bbDecode);
927 } else if (type == Entry.TypeCommitPart) {
928 CommitPart commitPart = (CommitPart)CommitPart.decode(null, bbDecode);
929 processEntry(commitPart);
933 updateLiveStateFromLocal();
938 private Pair<Boolean, Boolean> sendTransactionToLocal(Transaction transaction) {
940 // Get the devices local communications
941 Pair<String, Integer> localCommunicationInformation = localCommunicationTable.get(transaction.getArbitrator());
943 if (localCommunicationInformation == null) {
944 // Cant talk to that device locally so do nothing
945 return new Pair<Boolean, Boolean>(true, false);
948 // Get the size of the send data
949 int sendDataSize = Integer.BYTES + Long.BYTES;
950 for (TransactionPart part : transaction.getParts().values()) {
951 sendDataSize += part.getSize();
954 Long lastArbitrationDataLocalSequenceNumber = (long) - 1;
955 if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(transaction.getArbitrator()) != null) {
956 lastArbitrationDataLocalSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(transaction.getArbitrator());
959 // Make the send data size
960 byte[] sendData = new byte[sendDataSize];
961 ByteBuffer bbEncode = ByteBuffer.wrap(sendData);
964 bbEncode.putLong(lastArbitrationDataLocalSequenceNumber);
965 bbEncode.putInt(transaction.getParts().size());
966 for (TransactionPart part : transaction.getParts().values()) {
967 part.encode(bbEncode);
972 byte[] returnData = cloud.sendLocalData(sendData, localCommunicationInformation.getFirst(), localCommunicationInformation.getSecond());
974 if (returnData == null) {
975 // Could not contact server
976 return new Pair<Boolean, Boolean>(true, false);
980 ByteBuffer bbDecode = ByteBuffer.wrap(returnData);
981 boolean didCommit = bbDecode.get() == 1;
982 boolean couldArbitrate = bbDecode.get() == 1;
983 int numberOfEntries = bbDecode.getInt();
984 boolean foundAbort = false;
986 for (int i = 0; i < numberOfEntries; i++) {
987 byte type = bbDecode.get();
988 if (type == Entry.TypeAbort) {
989 Abort abort = (Abort)Abort.decode(null, bbDecode);
991 if ((abort.getTransactionMachineId() == localMachineId) && (abort.getTransactionClientLocalSequenceNumber() == transaction.getClientLocalSequenceNumber())) {
996 } else if (type == Entry.TypeCommitPart) {
997 CommitPart commitPart = (CommitPart)CommitPart.decode(null, bbDecode);
998 processEntry(commitPart);
1002 updateLiveStateFromLocal();
1004 if (couldArbitrate) {
1005 TransactionStatus status = transaction.getTransactionStatus();
1007 status.setStatus(TransactionStatus.StatusCommitted);
1009 status.setStatus(TransactionStatus.StatusAborted);
1012 TransactionStatus status = transaction.getTransactionStatus();
1014 status.setStatus(TransactionStatus.StatusAborted);
1016 status.setStatus(TransactionStatus.StatusCommitted);
1020 return new Pair<Boolean, Boolean>(false, true);
1023 public synchronized byte[] acceptDataFromLocal(byte[] data) {
1026 ByteBuffer bbDecode = ByteBuffer.wrap(data);
1027 long lastArbitratedSequenceNumberSeen = bbDecode.getLong();
1028 int numberOfParts = bbDecode.getInt();
1030 // If we did commit a transaction or not
1031 boolean didCommit = false;
1032 boolean couldArbitrate = false;
1034 if (numberOfParts != 0) {
1036 // decode the transaction
1037 Transaction transaction = new Transaction();
1038 for (int i = 0; i < numberOfParts; i++) {
1040 TransactionPart newPart = (TransactionPart)TransactionPart.decode(null, bbDecode);
1041 transaction.addPartDecode(newPart);
1044 // Arbitrate on transaction and pull relevant return data
1045 Pair<Boolean, Boolean> localArbitrateReturn = arbitrateOnLocalTransaction(transaction);
1046 couldArbitrate = localArbitrateReturn.getFirst();
1047 didCommit = localArbitrateReturn.getSecond();
1049 updateLiveStateFromLocal();
1051 // Transaction was sent to the server so keep track of it to prevent double commit
1052 if (transaction.getSequenceNumber() != -1) {
1053 offlineTransactionsCommittedAndAtServer.add(transaction.getId());
1057 // The data to send back
1058 int returnDataSize = 0;
1059 List<Entry> unseenArbitrations = new ArrayList<Entry>();
1061 // Get the aborts to send back
1062 List<Long> abortLocalSequenceNumbers = new ArrayList<Long >(liveAbortsGeneratedByLocal.keySet());
1063 Collections.sort(abortLocalSequenceNumbers);
1064 for (Long localSequenceNumber : abortLocalSequenceNumbers) {
1065 if (localSequenceNumber <= lastArbitratedSequenceNumberSeen) {
1069 Abort abort = liveAbortsGeneratedByLocal.get(localSequenceNumber);
1070 unseenArbitrations.add(abort);
1071 returnDataSize += abort.getSize();
1074 // Get the commits to send back
1075 Map<Long, Commit> commitForClientTable = liveCommitsTable.get(localMachineId);
1076 if (commitForClientTable != null) {
1077 List<Long> commitLocalSequenceNumbers = new ArrayList<Long>(commitForClientTable.keySet());
1078 Collections.sort(commitLocalSequenceNumbers);
1080 for (Long localSequenceNumber : commitLocalSequenceNumbers) {
1081 Commit commit = commitForClientTable.get(localSequenceNumber);
1083 if (localSequenceNumber <= lastArbitratedSequenceNumberSeen) {
1087 unseenArbitrations.addAll(commit.getParts().values());
1089 for (CommitPart commitPart : commit.getParts().values()) {
1090 returnDataSize += commitPart.getSize();
1095 // Number of arbitration entries to decode
1096 returnDataSize += 2 * Integer.BYTES;
1098 // Boolean of did commit or not
1099 if (numberOfParts != 0) {
1100 returnDataSize += Byte.BYTES;
1103 // Data to send Back
1104 byte[] returnData = new byte[returnDataSize];
1105 ByteBuffer bbEncode = ByteBuffer.wrap(returnData);
1107 if (numberOfParts != 0) {
1109 bbEncode.put((byte)1);
1111 bbEncode.put((byte)0);
1113 if (couldArbitrate) {
1114 bbEncode.put((byte)1);
1116 bbEncode.put((byte)0);
1120 bbEncode.putInt(unseenArbitrations.size());
1121 for (Entry entry : unseenArbitrations) {
1122 entry.encode(bbEncode);
1128 private ThreeTuple<Boolean, Boolean, Slot[]> sendSlotsToServer(Slot slot, int newSize, boolean isNewKey) throws ServerException {
1130 boolean attemptedToSendToServerTmp = attemptedToSendToServer;
1131 attemptedToSendToServer = true;
1133 boolean inserted = false;
1134 boolean lastTryInserted = false;
1136 Slot[] array = cloud.putSlot(slot, newSize);
1137 if (array == null) {
1138 array = new Slot[] {slot};
1139 rejectedSlotList.clear();
1142 if (array.length == 0) {
1143 throw new Error("Server Error: Did not send any slots");
1146 // if (attemptedToSendToServerTmp) {
1147 if (hadPartialSendToServer) {
1149 boolean isInserted = false;
1150 for (Slot s : array) {
1151 if ((s.getSequenceNumber() == slot.getSequenceNumber()) && (s.getMachineID() == localMachineId)) {
1157 for (Slot s : array) {
1162 // Process each entry in the slot
1163 for (Entry entry : s.getEntries()) {
1165 if (entry.getType() == Entry.TypeLastMessage) {
1166 LastMessage lastMessage = (LastMessage)entry;
1168 if ((lastMessage.getMachineID() == localMachineId) && (lastMessage.getSequenceNumber() == slot.getSequenceNumber())) {
1177 rejectedSlotList.add(slot.getSequenceNumber());
1178 lastTryInserted = false;
1180 lastTryInserted = true;
1183 rejectedSlotList.add(slot.getSequenceNumber());
1184 lastTryInserted = false;
1188 return new ThreeTuple<Boolean, Boolean, Slot[]>(inserted, lastTryInserted, array);
1192 * Returns false if a resize was needed
1194 private ThreeTuple<Boolean, Integer, Boolean> fillSlot(Slot slot, boolean resize, NewKey newKeyEntry) {
1196 if (liveSlotCount > bufferResizeThreshold) {
1197 resize = true; //Resize is forced
1201 newSize = (int) (numberOfSlots * RESIZE_MULTIPLE);
1202 TableStatus status = new TableStatus(slot, newSize);
1203 slot.addEntry(status);
1206 // Fill with rejected slots first before doing anything else
1207 doRejectedMessages(slot);
1209 // Do mandatory rescue of entries
1210 ThreeTuple<Boolean, Boolean, Long> mandatoryRescueReturn = doMandatoryResuce(slot, resize);
1212 // Extract working variables
1213 boolean needsResize = mandatoryRescueReturn.getFirst();
1214 boolean seenLiveSlot = mandatoryRescueReturn.getSecond();
1215 long currentRescueSequenceNumber = mandatoryRescueReturn.getThird();
1217 if (needsResize && !resize) {
1218 // We need to resize but we are not resizing so return false
1219 return new ThreeTuple<Boolean, Integer, Boolean>(true, null, null);
1222 boolean inserted = false;
1223 if (newKeyEntry != null) {
1224 newKeyEntry.setSlot(slot);
1225 if (slot.hasSpace(newKeyEntry)) {
1226 slot.addEntry(newKeyEntry);
1231 // Clear the transactions, aborts and commits that were sent previously
1232 transactionPartsSent.clear();
1233 pendingSendArbitrationEntriesToDelete.clear();
1235 for (ArbitrationRound round : pendingSendArbitrationRounds) {
1236 boolean isFull = false;
1237 round.generateParts();
1238 List<Entry> parts = round.getParts();
1240 // Insert pending arbitration data
1241 for (Entry arbitrationData : parts) {
1243 // If it is an abort then we need to set some information
1244 if (arbitrationData instanceof Abort) {
1245 ((Abort)arbitrationData).setSequenceNumber(slot.getSequenceNumber());
1248 if (!slot.hasSpace(arbitrationData)) {
1249 // No space so cant do anything else with these data entries
1254 // Add to this current slot and add it to entries to delete
1255 slot.addEntry(arbitrationData);
1256 pendingSendArbitrationEntriesToDelete.add(arbitrationData);
1264 if (pendingTransactionQueue.size() > 0) {
1266 Transaction transaction = pendingTransactionQueue.get(0);
1268 // Set the transaction sequence number if it has yet to be inserted into the block chain
1269 // if ((!transaction.didSendAPartToServer() && !transaction.getServerFailure()) || (transaction.getSequenceNumber() == -1)) {
1270 // transaction.setSequenceNumber(slot.getSequenceNumber());
1273 if ((!transaction.didSendAPartToServer()) || (transaction.getSequenceNumber() == -1)) {
1274 transaction.setSequenceNumber(slot.getSequenceNumber());
1279 TransactionPart part = transaction.getNextPartToSend();
1282 // Ran out of parts to send for this transaction so move on
1286 if (slot.hasSpace(part)) {
1287 slot.addEntry(part);
1288 List<Integer> partsSent = transactionPartsSent.get(transaction);
1289 if (partsSent == null) {
1290 partsSent = new ArrayList<Integer>();
1291 transactionPartsSent.put(transaction, partsSent);
1293 partsSent.add(part.getPartNumber());
1294 transactionPartsSent.put(transaction, partsSent);
1301 // Fill the remainder of the slot with rescue data
1302 doOptionalRescue(slot, seenLiveSlot, currentRescueSequenceNumber, resize);
1304 return new ThreeTuple<Boolean, Integer, Boolean>(false, newSize, inserted);
1307 private void doRejectedMessages(Slot s) {
1308 if (! rejectedSlotList.isEmpty()) {
1309 /* TODO: We should avoid generating a rejected message entry if
1310 * there is already a sufficient entry in the queue (e.g.,
1311 * equalsto value of true and same sequence number). */
1313 long old_seqn = rejectedSlotList.firstElement();
1314 if (rejectedSlotList.size() > REJECTED_THRESHOLD) {
1315 long new_seqn = rejectedSlotList.lastElement();
1316 RejectedMessage rm = new RejectedMessage(s, s.getSequenceNumber(), localMachineId, old_seqn, new_seqn, false);
1319 long prev_seqn = -1;
1321 /* Go through list of missing messages */
1322 for (; i < rejectedSlotList.size(); i++) {
1323 long curr_seqn = rejectedSlotList.get(i);
1324 Slot s_msg = buffer.getSlot(curr_seqn);
1327 prev_seqn = curr_seqn;
1329 /* Generate rejected message entry for missing messages */
1330 if (prev_seqn != -1) {
1331 RejectedMessage rm = new RejectedMessage(s, s.getSequenceNumber(), localMachineId, old_seqn, prev_seqn, false);
1334 /* Generate rejected message entries for present messages */
1335 for (; i < rejectedSlotList.size(); i++) {
1336 long curr_seqn = rejectedSlotList.get(i);
1337 Slot s_msg = buffer.getSlot(curr_seqn);
1338 long machineid = s_msg.getMachineID();
1339 RejectedMessage rm = new RejectedMessage(s, s.getSequenceNumber(), machineid, curr_seqn, curr_seqn, true);
1346 private ThreeTuple<Boolean, Boolean, Long> doMandatoryResuce(Slot slot, boolean resize) {
1347 long newestSequenceNumber = buffer.getNewestSeqNum();
1348 long oldestSequenceNumber = buffer.getOldestSeqNum();
1349 if (oldestLiveSlotSequenceNumver < oldestSequenceNumber) {
1350 oldestLiveSlotSequenceNumver = oldestSequenceNumber;
1353 long currentSequenceNumber = oldestLiveSlotSequenceNumver;
1354 boolean seenLiveSlot = false;
1355 long firstIfFull = newestSequenceNumber + 1 - numberOfSlots; // smallest seq number in the buffer if it is full
1356 long threshold = firstIfFull + FREE_SLOTS; // we want the buffer to be clear of live entries up to this point
1360 for (; currentSequenceNumber < threshold; currentSequenceNumber++) {
1361 Slot previousSlot = buffer.getSlot(currentSequenceNumber);
1362 // Push slot number forward
1363 if (! seenLiveSlot) {
1364 oldestLiveSlotSequenceNumver = currentSequenceNumber;
1367 if (!previousSlot.isLive()) {
1371 // We have seen a live slot
1372 seenLiveSlot = true;
1374 // Get all the live entries for a slot
1375 Vector<Entry> liveEntries = previousSlot.getLiveEntries(resize);
1377 // Iterate over all the live entries and try to rescue them
1378 for (Entry liveEntry : liveEntries) {
1379 if (slot.hasSpace(liveEntry)) {
1381 // Enough space to rescue the entry
1382 slot.addEntry(liveEntry);
1383 } else if (currentSequenceNumber == firstIfFull) {
1384 //if there's no space but the entry is about to fall off the queue
1385 System.out.println("B"); //?
1386 return new ThreeTuple<Boolean, Boolean, Long>(true, seenLiveSlot, currentSequenceNumber);
1393 return new ThreeTuple<Boolean, Boolean, Long>(false, seenLiveSlot, currentSequenceNumber);
1396 private void doOptionalRescue(Slot s, boolean seenliveslot, long seqn, boolean resize) {
1397 /* now go through live entries from least to greatest sequence number until
1398 * either all live slots added, or the slot doesn't have enough room
1399 * for SKIP_THRESHOLD consecutive entries*/
1401 long newestseqnum = buffer.getNewestSeqNum();
1403 for (; seqn <= newestseqnum; seqn++) {
1404 Slot prevslot = buffer.getSlot(seqn);
1405 //Push slot number forward
1407 oldestLiveSlotSequenceNumver = seqn;
1409 if (!prevslot.isLive())
1411 seenliveslot = true;
1412 Vector<Entry> liveentries = prevslot.getLiveEntries(resize);
1413 for (Entry liveentry : liveentries) {
1414 if (s.hasSpace(liveentry))
1415 s.addEntry(liveentry);
1418 if (skipcount > SKIP_THRESHOLD)
1426 * Checks for malicious activity and updates the local copy of the block chain.
1428 private void validateAndUpdate(Slot[] newSlots, boolean acceptUpdatesToLocal) {
1430 // The cloud communication layer has checked slot HMACs already before decoding
1431 if (newSlots.length == 0) {
1435 // Make sure all slots are newer than the last largest slot this client has seen
1436 long firstSeqNum = newSlots[0].getSequenceNumber();
1437 if (firstSeqNum <= sequenceNumber) {
1438 throw new Error("Server Error: Sent older slots!");
1441 // Create an object that can access both new slots and slots in our local chain
1442 // without committing slots to our local chain
1443 SlotIndexer indexer = new SlotIndexer(newSlots, buffer);
1445 // Check that the HMAC chain is not broken
1446 checkHMACChain(indexer, newSlots);
1448 // Set to keep track of messages from clients
1449 HashSet<Long> machineSet = new HashSet<Long>(lastMessageTable.keySet());
1451 // Process each slots data
1452 for (Slot slot : newSlots) {
1453 processSlot(indexer, slot, acceptUpdatesToLocal, machineSet);
1454 updateExpectedSize();
1457 // If there is a gap, check to see if the server sent us everything.
1458 if (firstSeqNum != (sequenceNumber + 1)) {
1460 // Check the size of the slots that were sent down by the server.
1461 // Can only check the size if there was a gap
1462 checkNumSlots(newSlots.length);
1464 // Since there was a gap every machine must have pushed a slot or must have
1465 // a last message message. If not then the server is hiding slots
1466 if (!machineSet.isEmpty()) {
1467 throw new Error("Missing record for machines: " + machineSet);
1471 // Update the size of our local block chain.
1474 // Commit new to slots to the local block chain.
1475 for (Slot slot : newSlots) {
1477 // Insert this slot into our local block chain copy.
1478 buffer.putSlot(slot);
1480 // Keep track of how many slots are currently live (have live data in them).
1484 // Get the sequence number of the latest slot in the system
1485 sequenceNumber = newSlots[newSlots.length - 1].getSequenceNumber();
1487 updateLiveStateFromServer();
1489 // No Need to remember after we pulled from the server
1490 offlineTransactionsCommittedAndAtServer.clear();
1492 // This is invalidated now
1493 hadPartialSendToServer = false;
1496 private void updateLiveStateFromServer() {
1497 // Process the new transaction parts
1498 processNewTransactionParts();
1500 // Do arbitration on new transactions that were received
1501 arbitrateFromServer();
1503 // Update all the committed keys
1504 boolean didCommitOrSpeculate = updateCommittedTable();
1506 // Delete the transactions that are now dead
1507 updateLiveTransactionsAndStatus();
1510 didCommitOrSpeculate |= updateSpeculativeTable(didCommitOrSpeculate);
1511 updatePendingTransactionSpeculativeTable(didCommitOrSpeculate);
1514 private void updateLiveStateFromLocal() {
1515 // Update all the committed keys
1516 boolean didCommitOrSpeculate = updateCommittedTable();
1518 // Delete the transactions that are now dead
1519 updateLiveTransactionsAndStatus();
1522 didCommitOrSpeculate |= updateSpeculativeTable(didCommitOrSpeculate);
1523 updatePendingTransactionSpeculativeTable(didCommitOrSpeculate);
1526 private void initExpectedSize(long firstSequenceNumber, long numberOfSlots) {
1527 if (didFindTableStatus) {
1530 long prevslots = firstSequenceNumber;
1531 expectedsize = (prevslots < ((long) numberOfSlots)) ? (int) prevslots : numberOfSlots;
1532 currMaxSize = numberOfSlots;
1535 private void updateExpectedSize() {
1537 if (expectedsize > currMaxSize) {
1538 expectedsize = currMaxSize;
1544 * Check the size of the block chain to make sure there are enough slots sent back by the server.
1545 * This is only called when we have a gap between the slots that we have locally and the slots
1546 * sent by the server therefore in the slots sent by the server there will be at least 1 Table
1549 private void checkNumSlots(int numberOfSlots) {
1550 if (numberOfSlots != expectedsize) {
1551 throw new Error("Server Error: Server did not send all slots. Expected: " + expectedsize + " Received:" + numberOfSlots);
1555 private void updateCurrMaxSize(int newmaxsize) {
1556 currMaxSize = newmaxsize;
1561 * Update the size of of the local buffer if it is needed.
1563 private void commitNewMaxSize() {
1564 didFindTableStatus = false;
1566 // Resize the local slot buffer
1567 if (numberOfSlots != currMaxSize) {
1568 buffer.resize((int)currMaxSize);
1571 // Change the number of local slots to the new size
1572 numberOfSlots = (int)currMaxSize;
1574 // Recalculate the resize threshold since the size of the local buffer has changed
1575 setResizeThreshold();
1579 * Process the new transaction parts from this latest round of slots received from the server
1581 private void processNewTransactionParts() {
1583 if (newTransactionParts.size() == 0) {
1584 // Nothing new to process
1588 // Iterate through all the machine Ids that we received new parts for
1589 for (Long machineId : newTransactionParts.keySet()) {
1590 Map<Pair<Long, Integer>, TransactionPart> parts = newTransactionParts.get(machineId);
1592 // Iterate through all the parts for that machine Id
1593 for (Pair<Long, Integer> partId : parts.keySet()) {
1594 TransactionPart part = parts.get(partId);
1596 Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(part.getArbitratorId());
1597 if ((lastTransactionNumber != null) && (lastTransactionNumber >= part.getSequenceNumber())) {
1598 // Set dead the transaction part
1603 // Get the transaction object for that sequence number
1604 Transaction transaction = liveTransactionBySequenceNumberTable.get(part.getSequenceNumber());
1606 if (transaction == null) {
1607 // This is a new transaction that we dont have so make a new one
1608 transaction = new Transaction();
1610 // Insert this new transaction into the live tables
1611 liveTransactionBySequenceNumberTable.put(part.getSequenceNumber(), transaction);
1612 liveTransactionByTransactionIdTable.put(part.getTransactionId(), transaction);
1615 // Add that part to the transaction
1616 transaction.addPartDecode(part);
1620 // Clear all the new transaction parts in preparation for the next time the server sends slots
1621 newTransactionParts.clear();
1625 private long lastSeqNumArbOn = 0;
1627 private void arbitrateFromServer() {
1629 if (liveTransactionBySequenceNumberTable.size() == 0) {
1630 // Nothing to arbitrate on so move on
1634 // Get the transaction sequence numbers and sort from oldest to newest
1635 List<Long> transactionSequenceNumbers = new ArrayList<Long>(liveTransactionBySequenceNumberTable.keySet());
1636 Collections.sort(transactionSequenceNumbers);
1638 // Collection of key value pairs that are
1639 Map<IoTString, KeyValue> speculativeTableTmp = new HashMap<IoTString, KeyValue>();
1641 // The last transaction arbitrated on
1642 long lastTransactionCommitted = -1;
1643 Set<Abort> generatedAborts = new HashSet<Abort>();
1645 for (Long transactionSequenceNumber : transactionSequenceNumbers) {
1646 Transaction transaction = liveTransactionBySequenceNumberTable.get(transactionSequenceNumber);
1648 for (KeyValue kv : transaction.getKeyValueUpdateSet()) {
1649 System.out.println("Arb Seen: " + kv + " " + lastSeqNumArbOn + " " + transactionSequenceNumber + " " + localMachineId + " " + transaction.getArbitrator());
1653 // Check if this machine arbitrates for this transaction if not then we cant arbitrate this transaction
1654 if (transaction.getArbitrator() != localMachineId) {
1658 if (transactionSequenceNumber < lastSeqNumArbOn) {
1662 for (KeyValue kv : transaction.getKeyValueUpdateSet()) {
1663 System.out.println("Arb Seen: " + kv + " " + lastSeqNumArbOn + " " + transactionSequenceNumber + " " + localMachineId);
1667 if (offlineTransactionsCommittedAndAtServer.contains(transaction.getId())) {
1668 // We have seen this already locally so dont commit again
1673 if (!transaction.isComplete()) {
1674 // Will arbitrate in incorrect order if we continue so just break
1679 for (KeyValue kv : transaction.getKeyValueUpdateSet()) {
1680 System.out.println("Arb on: " + kv + " " + lastSeqNumArbOn + " " + transactionSequenceNumber + " " + localMachineId);
1684 // update the largest transaction seen by arbitrator from server
1685 if (lastTransactionSeenFromMachineFromServer.get(transaction.getMachineId()) == null) {
1686 lastTransactionSeenFromMachineFromServer.put(transaction.getMachineId(), transaction.getClientLocalSequenceNumber());
1688 Long lastTransactionSeenFromMachine = lastTransactionSeenFromMachineFromServer.get(transaction.getMachineId());
1689 if (transaction.getClientLocalSequenceNumber() > lastTransactionSeenFromMachine) {
1690 lastTransactionSeenFromMachineFromServer.put(transaction.getMachineId(), transaction.getClientLocalSequenceNumber());
1694 if (transaction.evaluateGuard(committedKeyValueTable, speculativeTableTmp, null)) {
1695 // Guard evaluated as true
1697 // Update the local changes so we can make the commit
1698 for (KeyValue kv : transaction.getKeyValueUpdateSet()) {
1699 speculativeTableTmp.put(kv.getKey(), kv);
1702 // Update what the last transaction committed was for use in batch commit
1703 lastTransactionCommitted = transactionSequenceNumber;
1705 System.out.println("Commit Generated: " + lastTransactionCommitted + " " + localMachineId);
1707 // Guard evaluated was false so create abort
1710 Abort newAbort = new Abort(null,
1711 transaction.getClientLocalSequenceNumber(),
1712 transaction.getSequenceNumber(),
1713 transaction.getMachineId(),
1714 transaction.getArbitrator(),
1715 localArbitrationSequenceNumber);
1716 localArbitrationSequenceNumber++;
1718 generatedAborts.add(newAbort);
1720 // Insert the abort so we can process
1721 processEntry(newAbort);
1724 lastSeqNumArbOn = transactionSequenceNumber;
1726 // liveTransactionBySequenceNumberTable.remove(transactionSequenceNumber);
1729 Commit newCommit = null;
1731 // If there is something to commit
1732 if (speculativeTableTmp.size() != 0) {
1734 // Create the commit and increment the commit sequence number
1735 newCommit = new Commit(localArbitrationSequenceNumber, localMachineId, lastTransactionCommitted);
1736 localArbitrationSequenceNumber++;
1738 // Add all the new keys to the commit
1739 for (KeyValue kv : speculativeTableTmp.values()) {
1740 newCommit.addKV(kv);
1743 // create the commit parts
1744 newCommit.createCommitParts();
1746 // Append all the commit parts to the end of the pending queue waiting for sending to the server
1748 // Insert the commit so we can process it
1749 for (CommitPart commitPart : newCommit.getParts().values()) {
1750 processEntry(commitPart);
1754 if ((newCommit != null) || (generatedAborts.size() > 0)) {
1755 ArbitrationRound arbitrationRound = new ArbitrationRound(newCommit, generatedAborts);
1756 pendingSendArbitrationRounds.add(arbitrationRound);
1758 if (compactArbitrationData()) {
1759 ArbitrationRound newArbitrationRound = pendingSendArbitrationRounds.get(pendingSendArbitrationRounds.size() - 1);
1760 if (newArbitrationRound.getCommit() != null) {
1761 for (CommitPart commitPart : newArbitrationRound.getCommit().getParts().values()) {
1762 processEntry(commitPart);
1769 private Pair<Boolean, Boolean> arbitrateOnLocalTransaction(Transaction transaction) {
1771 // Check if this machine arbitrates for this transaction if not then we cant arbitrate this transaction
1772 if (transaction.getArbitrator() != localMachineId) {
1773 return new Pair<Boolean, Boolean>(false, false);
1776 if (!transaction.isComplete()) {
1777 // Will arbitrate in incorrect order if we continue so just break
1779 return new Pair<Boolean, Boolean>(false, false);
1782 if (transaction.getMachineId() != localMachineId) {
1783 // dont do this check for local transactions
1784 if (lastTransactionSeenFromMachineFromServer.get(transaction.getMachineId()) != null) {
1785 if (lastTransactionSeenFromMachineFromServer.get(transaction.getMachineId()) > transaction.getClientLocalSequenceNumber()) {
1786 // We've have already seen this from the server
1787 return new Pair<Boolean, Boolean>(false, false);
1792 if (transaction.evaluateGuard(committedKeyValueTable, null, null)) {
1793 // Guard evaluated as true
1795 // Create the commit and increment the commit sequence number
1796 Commit newCommit = new Commit(localArbitrationSequenceNumber, localMachineId, -1);
1797 localArbitrationSequenceNumber++;
1799 // Update the local changes so we can make the commit
1800 for (KeyValue kv : transaction.getKeyValueUpdateSet()) {
1801 newCommit.addKV(kv);
1804 // create the commit parts
1805 newCommit.createCommitParts();
1807 // Append all the commit parts to the end of the pending queue waiting for sending to the server
1808 ArbitrationRound arbitrationRound = new ArbitrationRound(newCommit, new HashSet<Abort>());
1809 pendingSendArbitrationRounds.add(arbitrationRound);
1811 if (compactArbitrationData()) {
1812 ArbitrationRound newArbitrationRound = pendingSendArbitrationRounds.get(pendingSendArbitrationRounds.size() - 1);
1813 for (CommitPart commitPart : newArbitrationRound.getCommit().getParts().values()) {
1814 processEntry(commitPart);
1817 // Insert the commit so we can process it
1818 for (CommitPart commitPart : newCommit.getParts().values()) {
1819 processEntry(commitPart);
1823 if (transaction.getMachineId() == localMachineId) {
1824 TransactionStatus status = transaction.getTransactionStatus();
1825 if (status != null) {
1826 status.setStatus(TransactionStatus.StatusCommitted);
1830 updateLiveStateFromLocal();
1831 return new Pair<Boolean, Boolean>(true, true);
1834 if (transaction.getMachineId() == localMachineId) {
1835 // For locally created messages update the status
1837 // Guard evaluated was false so create abort
1838 TransactionStatus status = transaction.getTransactionStatus();
1839 if (status != null) {
1840 status.setStatus(TransactionStatus.StatusAborted);
1843 Set addAbortSet = new HashSet<Abort>();
1847 Abort newAbort = new Abort(null,
1848 transaction.getClientLocalSequenceNumber(),
1850 transaction.getMachineId(),
1851 transaction.getArbitrator(),
1852 localArbitrationSequenceNumber);
1853 localArbitrationSequenceNumber++;
1855 addAbortSet.add(newAbort);
1858 // Append all the commit parts to the end of the pending queue waiting for sending to the server
1859 ArbitrationRound arbitrationRound = new ArbitrationRound(null, addAbortSet);
1860 pendingSendArbitrationRounds.add(arbitrationRound);
1862 if (compactArbitrationData()) {
1863 ArbitrationRound newArbitrationRound = pendingSendArbitrationRounds.get(pendingSendArbitrationRounds.size() - 1);
1864 for (CommitPart commitPart : newArbitrationRound.getCommit().getParts().values()) {
1865 processEntry(commitPart);
1870 updateLiveStateFromLocal();
1871 return new Pair<Boolean, Boolean>(true, false);
1876 * Compacts the arbitration data my merging commits and aggregating aborts so that a single large push of commits can be done instead of many small updates
1878 private boolean compactArbitrationData() {
1880 if (pendingSendArbitrationRounds.size() < 2) {
1881 // Nothing to compact so do nothing
1885 ArbitrationRound lastRound = pendingSendArbitrationRounds.get(pendingSendArbitrationRounds.size() - 1);
1886 if (lastRound.didSendPart()) {
1890 boolean hadCommit = (lastRound.getCommit() == null);
1891 boolean gotNewCommit = false;
1893 int numberToDelete = 1;
1894 while (numberToDelete < pendingSendArbitrationRounds.size()) {
1895 ArbitrationRound round = pendingSendArbitrationRounds.get(pendingSendArbitrationRounds.size() - numberToDelete - 1);
1897 if (round.isFull() || round.didSendPart()) {
1898 // Stop since there is a part that cannot be compacted and we need to compact in order
1902 if (round.getCommit() == null) {
1904 // Try compacting aborts only
1905 int newSize = round.getCurrentSize() + lastRound.getAbortsCount();
1906 if (newSize > ArbitrationRound.MAX_PARTS) {
1907 // Cant compact since it would be too large
1910 lastRound.addAborts(round.getAborts());
1913 // Create a new larger commit
1914 Commit newCommit = Commit.merge(lastRound.getCommit(), round.getCommit(), localArbitrationSequenceNumber);
1915 localArbitrationSequenceNumber++;
1917 // Create the commit parts so that we can count them
1918 newCommit.createCommitParts();
1920 // Calculate the new size of the parts
1921 int newSize = newCommit.getNumberOfParts();
1922 newSize += lastRound.getAbortsCount();
1923 newSize += round.getAbortsCount();
1925 if (newSize > ArbitrationRound.MAX_PARTS) {
1926 // Cant compact since it would be too large
1930 // Set the new compacted part
1931 lastRound.setCommit(newCommit);
1932 lastRound.addAborts(round.getAborts());
1933 gotNewCommit = true;
1939 if (numberToDelete != 1) {
1940 // If there is a compaction
1942 // Delete the previous pieces that are now in the new compacted piece
1943 if (numberToDelete == pendingSendArbitrationRounds.size()) {
1944 pendingSendArbitrationRounds.clear();
1946 for (int i = 0; i < numberToDelete; i++) {
1947 pendingSendArbitrationRounds.remove(pendingSendArbitrationRounds.size() - 1);
1951 // Add the new compacted into the pending to send list
1952 pendingSendArbitrationRounds.add(lastRound);
1954 // Should reinsert into the commit processor
1955 if (hadCommit && gotNewCommit) {
1962 // private boolean compactArbitrationData() {
1967 * Update all the commits and the committed tables, sets dead the dead transactions
1969 private boolean updateCommittedTable() {
1971 if (newCommitParts.size() == 0) {
1972 // Nothing new to process
1976 // Iterate through all the machine Ids that we received new parts for
1977 for (Long machineId : newCommitParts.keySet()) {
1978 Map<Pair<Long, Integer>, CommitPart> parts = newCommitParts.get(machineId);
1980 // Iterate through all the parts for that machine Id
1981 for (Pair<Long, Integer> partId : parts.keySet()) {
1982 CommitPart part = parts.get(partId);
1984 // Get the transaction object for that sequence number
1985 Map<Long, Commit> commitForClientTable = liveCommitsTable.get(part.getMachineId());
1987 if (commitForClientTable == null) {
1988 // This is the first commit from this device
1989 commitForClientTable = new HashMap<Long, Commit>();
1990 liveCommitsTable.put(part.getMachineId(), commitForClientTable);
1993 Commit commit = commitForClientTable.get(part.getSequenceNumber());
1995 if (commit == null) {
1996 // This is a new commit that we dont have so make a new one
1997 commit = new Commit();
1999 // Insert this new commit into the live tables
2000 commitForClientTable.put(part.getSequenceNumber(), commit);
2003 // Add that part to the commit
2004 commit.addPartDecode(part);
2008 // Clear all the new commits parts in preparation for the next time the server sends slots
2009 newCommitParts.clear();
2011 // If we process a new commit keep track of it for future use
2012 boolean didProcessANewCommit = false;
2014 // Process the commits one by one
2015 for (Long arbitratorId : liveCommitsTable.keySet()) {
2017 // Get all the commits for a specific arbitrator
2018 Map<Long, Commit> commitForClientTable = liveCommitsTable.get(arbitratorId);
2020 // Sort the commits in order
2021 List<Long> commitSequenceNumbers = new ArrayList<Long>(commitForClientTable.keySet());
2022 Collections.sort(commitSequenceNumbers);
2024 // Get the last commit seen from this arbitrator
2025 long lastCommitSeenSequenceNumber = -1;
2026 if (lastCommitSeenSequenceNumberByArbitratorTable.get(arbitratorId) != null) {
2027 lastCommitSeenSequenceNumber = lastCommitSeenSequenceNumberByArbitratorTable.get(arbitratorId);
2030 // Go through each new commit one by one
2031 for (int i = 0; i < commitSequenceNumbers.size(); i++) {
2032 Long commitSequenceNumber = commitSequenceNumbers.get(i);
2033 Commit commit = commitForClientTable.get(commitSequenceNumber);
2035 // Special processing if a commit is not complete
2036 if (!commit.isComplete()) {
2037 if (i == (commitSequenceNumbers.size() - 1)) {
2038 // If there is an incomplete commit and this commit is the latest one seen then this commit cannot be processed and there are no other commits
2041 // This is a commit that was already dead but parts of it are still in the block chain (not flushed out yet).
2042 // Delete it and move on
2044 commitForClientTable.remove(commit.getSequenceNumber());
2049 // Update the last transaction that was updated if we can
2050 if (commit.getTransactionSequenceNumber() != -1) {
2051 Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(commit.getMachineId());
2053 // Update the last transaction sequence number that the arbitrator arbitrated on
2054 if ((lastTransactionNumber == null) || (lastTransactionNumber < commit.getTransactionSequenceNumber())) {
2055 lastArbitratedTransactionNumberByArbitratorTable.put(commit.getMachineId(), commit.getTransactionSequenceNumber());
2060 for (KeyValue kv : commit.getKeyValueUpdateSet()) {
2061 System.out.println("Commit Seen: " + kv + " " + commit.getTransactionSequenceNumber() + " " + localMachineId);
2073 // Update the last arbitration data that we have seen so far
2074 if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(commit.getMachineId()) != null) {
2076 long lastArbitrationSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(commit.getMachineId());
2077 if (commit.getSequenceNumber() > lastArbitrationSequenceNumber) {
2079 lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.put(commit.getMachineId(), commit.getSequenceNumber());
2082 // Never seen any data from this arbitrator so record the first one
2083 lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.put(commit.getMachineId(), commit.getSequenceNumber());
2086 // We have already seen this commit before so need to do the full processing on this commit
2087 if (commit.getSequenceNumber() <= lastCommitSeenSequenceNumber) {
2089 // Update the last transaction that was updated if we can
2090 if (commit.getTransactionSequenceNumber() != -1) {
2091 Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(commit.getMachineId());
2093 // Update the last transaction sequence number that the arbitrator arbitrated on
2094 if ((lastTransactionNumber == null) || (lastTransactionNumber < commit.getTransactionSequenceNumber())) {
2095 lastArbitratedTransactionNumberByArbitratorTable.put(commit.getMachineId(), commit.getTransactionSequenceNumber());
2102 // If we got here then this is a brand new commit and needs full processing
2104 // Get what commits should be edited, these are the commits that have live values for their keys
2105 Set<Commit> commitsToEdit = new HashSet<Commit>();
2106 for (KeyValue kv : commit.getKeyValueUpdateSet()) {
2107 commitsToEdit.add(liveCommitsByKeyTable.get(kv.getKey()));
2109 commitsToEdit.remove(null); // remove null since it could be in this set
2111 // Update each previous commit that needs to be updated
2112 for (Commit previousCommit : commitsToEdit) {
2114 // Only bother with live commits (TODO: Maybe remove this check)
2115 if (previousCommit.isLive()) {
2117 // Update which keys in the old commits are still live
2118 for (KeyValue kv : commit.getKeyValueUpdateSet()) {
2119 previousCommit.invalidateKey(kv.getKey());
2122 // if the commit is now dead then remove it
2123 if (!previousCommit.isLive()) {
2124 commitForClientTable.remove(previousCommit);
2129 // Update the last seen sequence number from this arbitrator
2130 if (lastCommitSeenSequenceNumberByArbitratorTable.get(commit.getMachineId()) != null) {
2131 if (commit.getSequenceNumber() > lastCommitSeenSequenceNumberByArbitratorTable.get(commit.getMachineId())) {
2132 lastCommitSeenSequenceNumberByArbitratorTable.put(commit.getMachineId(), commit.getSequenceNumber());
2135 lastCommitSeenSequenceNumberByArbitratorTable.put(commit.getMachineId(), commit.getSequenceNumber());
2138 // We processed a new commit that we havent seen before
2139 didProcessANewCommit = true;
2141 // Update the committed table of keys and which commit is using which key
2142 for (KeyValue kv : commit.getKeyValueUpdateSet()) {
2143 committedKeyValueTable.put(kv.getKey(), kv);
2144 liveCommitsByKeyTable.put(kv.getKey(), commit);
2149 return didProcessANewCommit;
2153 * Create the speculative table from transactions that are still live and have come from the cloud
2155 private boolean updateSpeculativeTable(boolean didProcessNewCommits) {
2156 if (liveTransactionBySequenceNumberTable.keySet().size() == 0) {
2157 // There is nothing to speculate on
2161 // Create a list of the transaction sequence numbers and sort them from oldest to newest
2162 List<Long> transactionSequenceNumbersSorted = new ArrayList<Long>(liveTransactionBySequenceNumberTable.keySet());
2163 Collections.sort(transactionSequenceNumbersSorted);
2165 boolean hasGapInTransactionSequenceNumbers = transactionSequenceNumbersSorted.get(0) != oldestTransactionSequenceNumberSpeculatedOn;
2168 if (hasGapInTransactionSequenceNumbers || didProcessNewCommits) {
2169 // If there is a gap in the transaction sequence numbers then there was a commit or an abort of a transaction
2170 // OR there was a new commit (Could be from offline commit) so a redo the speculation from scratch
2172 // Start from scratch
2173 speculatedKeyValueTable.clear();
2174 lastTransactionSequenceNumberSpeculatedOn = -1;
2175 oldestTransactionSequenceNumberSpeculatedOn = -1;
2179 // Remember the front of the transaction list
2180 oldestTransactionSequenceNumberSpeculatedOn = transactionSequenceNumbersSorted.get(0);
2182 // Find where to start arbitration from
2183 int startIndex = transactionSequenceNumbersSorted.indexOf(lastTransactionSequenceNumberSpeculatedOn) + 1;
2185 if (startIndex >= transactionSequenceNumbersSorted.size()) {
2186 // Make sure we are not out of bounds
2187 return false; // did not speculate
2190 Set<Long> incompleteTransactionArbitrator = new HashSet<Long>();
2191 boolean didSkip = true;
2193 for (int i = startIndex; i < transactionSequenceNumbersSorted.size(); i++) {
2194 long transactionSequenceNumber = transactionSequenceNumbersSorted.get(i);
2195 Transaction transaction = liveTransactionBySequenceNumberTable.get(transactionSequenceNumber);
2197 if (!transaction.isComplete()) {
2198 // If there is an incomplete transaction then there is nothing we can do
2199 // add this transactions arbitrator to the list of arbitrators we should ignore
2200 incompleteTransactionArbitrator.add(transaction.getArbitrator());
2205 if (incompleteTransactionArbitrator.contains(transaction.getArbitrator())) {
2209 lastTransactionSequenceNumberSpeculatedOn = transactionSequenceNumber;
2211 if (transaction.evaluateGuard(committedKeyValueTable, speculatedKeyValueTable, null)) {
2212 // Guard evaluated to true so update the speculative table
2213 for (KeyValue kv : transaction.getKeyValueUpdateSet()) {
2214 speculatedKeyValueTable.put(kv.getKey(), kv);
2220 // Since there was a skip we need to redo the speculation next time around
2221 lastTransactionSequenceNumberSpeculatedOn = -1;
2222 oldestTransactionSequenceNumberSpeculatedOn = -1;
2225 // We did some speculation
2230 * Create the pending transaction speculative table from transactions that are still in the pending transaction buffer
2232 private void updatePendingTransactionSpeculativeTable(boolean didProcessNewCommitsOrSpeculate) {
2233 if (pendingTransactionQueue.size() == 0) {
2234 // There is nothing to speculate on
2239 if (didProcessNewCommitsOrSpeculate || (firstPendingTransaction != pendingTransactionQueue.get(0))) {
2240 // need to reset on the pending speculation
2241 lastPendingTransactionSpeculatedOn = null;
2242 firstPendingTransaction = pendingTransactionQueue.get(0);
2243 pendingTransactionSpeculatedKeyValueTable.clear();
2246 // Find where to start arbitration from
2247 int startIndex = pendingTransactionQueue.indexOf(firstPendingTransaction) + 1;
2249 if (startIndex >= pendingTransactionQueue.size()) {
2250 // Make sure we are not out of bounds
2254 for (int i = startIndex; i < pendingTransactionQueue.size(); i++) {
2255 Transaction transaction = pendingTransactionQueue.get(i);
2257 lastPendingTransactionSpeculatedOn = transaction;
2259 if (transaction.evaluateGuard(committedKeyValueTable, speculatedKeyValueTable, pendingTransactionSpeculatedKeyValueTable)) {
2260 // Guard evaluated to true so update the speculative table
2261 for (KeyValue kv : transaction.getKeyValueUpdateSet()) {
2262 pendingTransactionSpeculatedKeyValueTable.put(kv.getKey(), kv);
2269 * Set dead and remove from the live transaction tables the transactions that are dead
2271 private void updateLiveTransactionsAndStatus() {
2273 // Go through each of the transactions
2274 for (Iterator<Map.Entry<Long, Transaction>> iter = liveTransactionBySequenceNumberTable.entrySet().iterator(); iter.hasNext();) {
2275 Transaction transaction = iter.next().getValue();
2277 // Check if the transaction is dead
2278 Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(transaction.getArbitrator());
2279 if ((lastTransactionNumber != null) && (lastTransactionNumber >= transaction.getSequenceNumber())) {
2281 // Set dead the transaction
2282 transaction.setDead();
2284 // Remove the transaction from the live table
2286 liveTransactionByTransactionIdTable.remove(transaction.getId());
2290 // Go through each of the transactions
2291 for (Iterator<Map.Entry<Long, TransactionStatus>> iter = outstandingTransactionStatus.entrySet().iterator(); iter.hasNext();) {
2292 TransactionStatus status = iter.next().getValue();
2294 // Check if the transaction is dead
2295 Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(status.getTransactionArbitrator());
2296 if ((lastTransactionNumber != null) && (lastTransactionNumber >= status.getTransactionSequenceNumber())) {
2299 status.setStatus(TransactionStatus.StatusCommitted);
2308 * Process this slot, entry by entry. Also update the latest message sent by slot
2310 private void processSlot(SlotIndexer indexer, Slot slot, boolean acceptUpdatesToLocal, HashSet<Long> machineSet) {
2312 // Update the last message seen
2313 updateLastMessage(slot.getMachineID(), slot.getSequenceNumber(), slot, acceptUpdatesToLocal, machineSet);
2315 // Process each entry in the slot
2316 for (Entry entry : slot.getEntries()) {
2317 switch (entry.getType()) {
2319 case Entry.TypeCommitPart:
2320 processEntry((CommitPart)entry);
2323 case Entry.TypeAbort:
2324 processEntry((Abort)entry);
2327 case Entry.TypeTransactionPart:
2328 processEntry((TransactionPart)entry);
2331 case Entry.TypeNewKey:
2332 processEntry((NewKey)entry);
2335 case Entry.TypeLastMessage:
2336 processEntry((LastMessage)entry, machineSet);
2339 case Entry.TypeRejectedMessage:
2340 processEntry((RejectedMessage)entry, indexer);
2343 case Entry.TypeTableStatus:
2344 processEntry((TableStatus)entry, slot.getSequenceNumber());
2348 throw new Error("Unrecognized type: " + entry.getType());
2354 * Update the last message that was sent for a machine Id
2356 private void processEntry(LastMessage entry, HashSet<Long> machineSet) {
2357 // Update what the last message received by a machine was
2358 updateLastMessage(entry.getMachineID(), entry.getSequenceNumber(), entry, false, machineSet);
2362 * Add the new key to the arbitrators table and update the set of live new keys (in case of a rescued new key message)
2364 private void processEntry(NewKey entry) {
2366 // Update the arbitrator table with the new key information
2367 arbitratorTable.put(entry.getKey(), entry.getMachineID());
2369 // Update what the latest live new key is
2370 NewKey oldNewKey = liveNewKeyTable.put(entry.getKey(), entry);
2371 if (oldNewKey != null) {
2372 // Delete the old new key messages
2373 oldNewKey.setDead();
2378 * Process new table status entries and set dead the old ones as new ones come in.
2379 * keeps track of the largest and smallest table status seen in this current round
2380 * of updating the local copy of the block chain
2382 private void processEntry(TableStatus entry, long seq) {
2383 int newNumSlots = entry.getMaxSlots();
2384 updateCurrMaxSize(newNumSlots);
2386 initExpectedSize(seq, newNumSlots);
2388 if (liveTableStatus != null) {
2389 // We have a larger table status so the old table status is no longer alive
2390 liveTableStatus.setDead();
2393 // Make this new table status the latest alive table status
2394 liveTableStatus = entry;
2398 * Check old messages to see if there is a block chain violation. Also
2400 private void processEntry(RejectedMessage entry, SlotIndexer indexer) {
2401 long oldSeqNum = entry.getOldSeqNum();
2402 long newSeqNum = entry.getNewSeqNum();
2403 boolean isequal = entry.getEqual();
2404 long machineId = entry.getMachineID();
2405 long seq = entry.getSequenceNumber();
2408 // Check if we have messages that were supposed to be rejected in our local block chain
2409 for (long seqNum = oldSeqNum; seqNum <= newSeqNum; seqNum++) {
2412 Slot slot = indexer.getSlot(seqNum);
2415 // If we have this slot make sure that it was not supposed to be a rejected slot
2417 long slotMachineId = slot.getMachineID();
2418 if (isequal != (slotMachineId == machineId)) {
2419 throw new Error("Server Error: Trying to insert rejected message for slot " + seqNum);
2425 // Create a list of clients to watch until they see this rejected message entry.
2426 HashSet<Long> deviceWatchSet = new HashSet<Long>();
2427 for (Map.Entry<Long, Pair<Long, Liveness>> lastMessageEntry : lastMessageTable.entrySet()) {
2429 // Machine ID for the last message entry
2430 long lastMessageEntryMachineId = lastMessageEntry.getKey();
2432 // We've seen it, don't need to continue to watch. Our next
2433 // message will implicitly acknowledge it.
2434 if (lastMessageEntryMachineId == localMachineId) {
2438 Pair<Long, Liveness> lastMessageValue = lastMessageEntry.getValue();
2439 long entrySequenceNumber = lastMessageValue.getFirst();
2441 if (entrySequenceNumber < seq) {
2443 // Add this rejected message to the set of messages that this machine ID did not see yet
2444 addWatchList(lastMessageEntryMachineId, entry);
2446 // This client did not see this rejected message yet so add it to the watch set to monitor
2447 deviceWatchSet.add(lastMessageEntryMachineId);
2451 if (deviceWatchSet.isEmpty()) {
2452 // This rejected message has been seen by all the clients so
2455 // We need to watch this rejected message
2456 entry.setWatchSet(deviceWatchSet);
2461 * Check if this abort is live, if not then save it so we can kill it later.
2462 * update the last transaction number that was arbitrated on.
2464 private void processEntry(Abort entry) {
2467 if (entry.getTransactionSequenceNumber() != -1) {
2468 // update the transaction status if it was sent to the server
2469 TransactionStatus status = outstandingTransactionStatus.remove(entry.getTransactionSequenceNumber());
2470 if (status != null) {
2471 status.setStatus(TransactionStatus.StatusAborted);
2475 // Abort has not been seen by the client it is for yet so we need to keep track of it
2476 Abort previouslySeenAbort = liveAbortTable.put(entry.getAbortId(), entry);
2477 if (previouslySeenAbort != null) {
2478 previouslySeenAbort.setDead(); // Delete old version of the abort since we got a rescued newer version
2481 if (entry.getTransactionArbitrator() == localMachineId) {
2482 liveAbortsGeneratedByLocal.put(entry.getArbitratorLocalSequenceNumber(), entry);
2485 if ((entry.getSequenceNumber() != -1) && (lastMessageTable.get(entry.getTransactionMachineId()).getFirst() >= entry.getSequenceNumber())) {
2487 // The machine already saw this so it is dead
2489 liveAbortTable.remove(entry.getAbortId());
2491 if (entry.getTransactionArbitrator() == localMachineId) {
2492 liveAbortsGeneratedByLocal.remove(entry.getArbitratorLocalSequenceNumber());
2501 // Update the last arbitration data that we have seen so far
2502 if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(entry.getTransactionArbitrator()) != null) {
2504 long lastArbitrationSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(entry.getTransactionArbitrator());
2505 if (entry.getSequenceNumber() > lastArbitrationSequenceNumber) {
2507 lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.put(entry.getTransactionArbitrator(), entry.getSequenceNumber());
2510 // Never seen any data from this arbitrator so record the first one
2511 lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.put(entry.getTransactionArbitrator(), entry.getSequenceNumber());
2515 // Set dead a transaction if we can
2516 Transaction transactionToSetDead = liveTransactionByTransactionIdTable.remove(new Pair<Long, Long>(entry.getTransactionMachineId(), entry.getTransactionClientLocalSequenceNumber()));
2517 if (transactionToSetDead != null) {
2518 liveTransactionBySequenceNumberTable.remove(transactionToSetDead.getSequenceNumber());
2521 // Update the last transaction sequence number that the arbitrator arbitrated on
2522 Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(entry.getTransactionArbitrator());
2523 if ((lastTransactionNumber == null) || (lastTransactionNumber < entry.getTransactionSequenceNumber())) {
2526 if (entry.getTransactionSequenceNumber() != -1) {
2527 lastArbitratedTransactionNumberByArbitratorTable.put(entry.getTransactionArbitrator(), entry.getTransactionSequenceNumber());
2533 * Set dead the transaction part if that transaction is dead and keep track of all new parts
2535 private void processEntry(TransactionPart entry) {
2536 // Check if we have already seen this transaction and set it dead OR if it is not alive
2537 Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(entry.getArbitratorId());
2538 if ((lastTransactionNumber != null) && (lastTransactionNumber >= entry.getSequenceNumber())) {
2539 // This transaction is dead, it was already committed or aborted
2544 // This part is still alive
2545 Map<Pair<Long, Integer>, TransactionPart> transactionPart = newTransactionParts.get(entry.getMachineId());
2547 if (transactionPart == null) {
2548 // Dont have a table for this machine Id yet so make one
2549 transactionPart = new HashMap<Pair<Long, Integer>, TransactionPart>();
2550 newTransactionParts.put(entry.getMachineId(), transactionPart);
2553 // Update the part and set dead ones we have already seen (got a rescued version)
2554 TransactionPart previouslySeenPart = transactionPart.put(entry.getPartId(), entry);
2555 if (previouslySeenPart != null) {
2556 previouslySeenPart.setDead();
2561 * Process new commit entries and save them for future use. Delete duplicates
2563 private void processEntry(CommitPart entry) {
2566 // Update the last transaction that was updated if we can
2567 if (entry.getTransactionSequenceNumber() != -1) {
2568 Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(entry.getMachineId());
2570 // Update the last transaction sequence number that the arbitrator arbitrated on
2571 if ((lastTransactionNumber == null) || (lastTransactionNumber < entry.getTransactionSequenceNumber())) {
2572 lastArbitratedTransactionNumberByArbitratorTable.put(entry.getMachineId(), entry.getTransactionSequenceNumber());
2579 Map<Pair<Long, Integer>, CommitPart> commitPart = newCommitParts.get(entry.getMachineId());
2581 if (commitPart == null) {
2582 // Don't have a table for this machine Id yet so make one
2583 commitPart = new HashMap<Pair<Long, Integer>, CommitPart>();
2584 newCommitParts.put(entry.getMachineId(), commitPart);
2587 // Update the part and set dead ones we have already seen (got a rescued version)
2588 CommitPart previouslySeenPart = commitPart.put(entry.getPartId(), entry);
2589 if (previouslySeenPart != null) {
2590 previouslySeenPart.setDead();
2595 * Update the last message seen table. Update and set dead the appropriate RejectedMessages as clients see them.
2596 * Updates the live aborts, removes those that are dead and sets them dead.
2597 * Check that the last message seen is correct and that there is no mismatch of our own last message or that
2598 * other clients have not had a rollback on the last message.
2600 private void updateLastMessage(long machineId, long seqNum, Liveness liveness, boolean acceptUpdatesToLocal, HashSet<Long> machineSet) {
2602 // We have seen this machine ID
2603 machineSet.remove(machineId);
2605 // Get the set of rejected messages that this machine Id is has not seen yet
2606 HashSet<RejectedMessage> watchset = rejectedMessageWatchListTable.get(machineId);
2608 // If there is a rejected message that this machine Id has not seen yet
2609 if (watchset != null) {
2611 // Go through each rejected message that this machine Id has not seen yet
2612 for (Iterator<RejectedMessage> rmit = watchset.iterator(); rmit.hasNext(); ) {
2614 RejectedMessage rm = rmit.next();
2616 // If this machine Id has seen this rejected message...
2617 if (rm.getSequenceNumber() <= seqNum) {
2619 // Remove it from our watchlist
2622 // Decrement machines that need to see this notification
2623 rm.removeWatcher(machineId);
2628 // Set dead the abort
2629 for (Iterator<Map.Entry<Pair<Long, Long>, Abort>> i = liveAbortTable.entrySet().iterator(); i.hasNext();) {
2630 Abort abort = i.next().getValue();
2632 if ((abort.getTransactionMachineId() == machineId) && (abort.getSequenceNumber() <= seqNum)) {
2636 if (abort.getTransactionArbitrator() == localMachineId) {
2637 liveAbortsGeneratedByLocal.remove(abort.getArbitratorLocalSequenceNumber());
2644 if (machineId == localMachineId) {
2645 // Our own messages are immediately dead.
2646 if (liveness instanceof LastMessage) {
2647 ((LastMessage)liveness).setDead();
2648 } else if (liveness instanceof Slot) {
2649 ((Slot)liveness).setDead();
2651 throw new Error("Unrecognized type");
2655 // Get the old last message for this device
2656 Pair<Long, Liveness> lastMessageEntry = lastMessageTable.put(machineId, new Pair<Long, Liveness>(seqNum, liveness));
2657 if (lastMessageEntry == null) {
2658 // If no last message then there is nothing else to process
2662 long lastMessageSeqNum = lastMessageEntry.getFirst();
2663 Liveness lastEntry = lastMessageEntry.getSecond();
2665 // If it is not our machine Id since we already set ours to dead
2666 if (machineId != localMachineId) {
2667 if (lastEntry instanceof LastMessage) {
2668 ((LastMessage)lastEntry).setDead();
2669 } else if (lastEntry instanceof Slot) {
2670 ((Slot)lastEntry).setDead();
2672 throw new Error("Unrecognized type");
2676 // Make sure the server is not playing any games
2677 if (machineId == localMachineId) {
2679 if (hadPartialSendToServer) {
2680 // We were not making any updates and we had a machine mismatch
2681 if (lastMessageSeqNum > seqNum && !acceptUpdatesToLocal) {
2682 throw new Error("Server Error: Mismatch on local machine sequence number, needed at least: " + lastMessageSeqNum + " got: " + seqNum);
2686 // We were not making any updates and we had a machine mismatch
2687 if (lastMessageSeqNum != seqNum && !acceptUpdatesToLocal) {
2688 throw new Error("Server Error: Mismatch on local machine sequence number, needed: " + lastMessageSeqNum + " got: " + seqNum);
2692 if (lastMessageSeqNum > seqNum) {
2693 throw new Error("Server Error: Rollback on remote machine sequence number");
2699 * Add a rejected message entry to the watch set to keep track of which clients have seen that
2700 * rejected message entry and which have not.
2702 private void addWatchList(long machineId, RejectedMessage entry) {
2703 HashSet<RejectedMessage> entries = rejectedMessageWatchListTable.get(machineId);
2704 if (entries == null) {
2705 // There is no set for this machine ID yet so create one
2706 entries = new HashSet<RejectedMessage>();
2707 rejectedMessageWatchListTable.put(machineId, entries);
2713 * Check if the HMAC chain is not violated
2715 private void checkHMACChain(SlotIndexer indexer, Slot[] newSlots) {
2716 for (int i = 0; i < newSlots.length; i++) {
2717 Slot currSlot = newSlots[i];
2718 Slot prevSlot = indexer.getSlot(currSlot.getSequenceNumber() - 1);
2719 if (prevSlot != null &&
2720 !Arrays.equals(prevSlot.getHMAC(), currSlot.getPrevHMAC()))
2721 throw new Error("Server Error: Invalid HMAC Chain" + currSlot + " " + prevSlot);