Changing encryption in the phone app of the fourth benchmark from AES-ECB to AES...
[iot2.git] / benchmarks / other / PhoneInterface / Control / app / src / main / java / iotcloud / Table.java
1 package iotcloud;
2
3 import java.util.Iterator;
4 import java.util.Random;
5 import java.util.Arrays;
6 import java.util.Map;
7 import java.util.Set;
8 import java.util.List;
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;
15 import android.content.*;
16
17 /**
18  * IoTTable data structure.  Provides client interface.
19  * @author Brian Demsky
20  * @version 1.0
21  */
22
23 final public class Table {
24
25         /* Constants */
26         static final int FREE_SLOTS = 10; // Number of slots that should be kept free
27         static final int SKIP_THRESHOLD = 10;
28         static final double RESIZE_MULTIPLE = 1.2;
29         static final double RESIZE_THRESHOLD = 0.75;
30         static final int REJECTED_THRESHOLD = 5;
31
32         /* Helper Objects */
33         private SlotBuffer buffer = null;
34         private CloudComm cloud = null;
35         private Random random = null;
36         private TableStatus liveTableStatus = null;
37         private PendingTransaction pendingTransactionBuilder = null; // Pending Transaction used in building a Pending Transaction
38         private Transaction lastPendingTransactionSpeculatedOn = null; // Last transaction that was speculated on from the pending transaction
39         private Transaction firstPendingTransaction = null; // first transaction in the pending transaction list
40
41         /* Variables */
42         private int numberOfSlots = 0;  // Number of slots stored in buffer
43         private int bufferResizeThreshold = 0; // Threshold on the number of live slots before a resize is needed
44         private long liveSlotCount = 0; // Number of currently live slots
45         private long oldestLiveSlotSequenceNumver = 0;  // Smallest sequence number of the slot with a live entry
46         private long localMachineId = 0; // Machine ID of this client device
47         private long sequenceNumber = 0; // Largest sequence number a client has received
48         private long localSequenceNumber = 0;
49
50         // private int smallestTableStatusSeen = -1; // Smallest Table Status that was seen in the latest slots sent from the server
51         // private int largestTableStatusSeen = -1; // Largest Table Status that was seen in the latest slots sent from the server
52         private long localTransactionSequenceNumber = 0; // Local sequence number counter for transactions
53         private long lastTransactionSequenceNumberSpeculatedOn = -1; // the last transaction that was speculated on
54         private long oldestTransactionSequenceNumberSpeculatedOn = -1; // the oldest transaction that was speculated on
55         private long localArbitrationSequenceNumber = 0;
56         private boolean hadPartialSendToServer = false;
57         private boolean attemptedToSendToServer = false;
58         private long expectedsize;
59         private boolean didFindTableStatus = false;
60         private long currMaxSize = 0;
61
62         private Slot lastSlotAttemptedToSend = null;
63         private boolean lastIsNewKey = false;
64         private int lastNewSize = 0;
65         private Map<Transaction, List<Integer>> lastTransactionPartsSent = null;
66         private List<Entry> lastPendingSendArbitrationEntriesToDelete = null;
67         private NewKey lastNewKey = null;
68
69
70         /* Data Structures  */
71         private Map<IoTString, KeyValue> committedKeyValueTable = null; // Table of committed key value pairs
72         private Map<IoTString, KeyValue> speculatedKeyValueTable = null; // Table of speculated key value pairs, if there is a speculative value
73         private Map<IoTString, KeyValue> pendingTransactionSpeculatedKeyValueTable = null; // Table of speculated key value pairs, if there is a speculative value from the pending transactions
74         private Map<IoTString, NewKey> liveNewKeyTable = null; // Table of live new keys
75         private HashMap<Long, Pair<Long, Liveness>> lastMessageTable = null; // Last message sent by a client machine id -> (Seq Num, Slot or LastMessage);
76         private HashMap<Long, HashSet<RejectedMessage>> rejectedMessageWatchListTable = null; // Table of machine Ids and the set of rejected messages they have not seen yet
77         private Map<IoTString, Long> arbitratorTable = null; // Table of keys and their arbitrators
78         private Map<Pair<Long, Long>, Abort> liveAbortTable = null; // Table live abort messages
79         private Map<Long, Map<Pair<Long, Integer>, TransactionPart>> newTransactionParts = null; // transaction parts that are seen in this latest round of slots from the server
80         private Map<Long, Map<Pair<Long, Integer>, CommitPart>> newCommitParts = null; // commit parts that are seen in this latest round of slots from the server
81         private Map<Long, Long> lastArbitratedTransactionNumberByArbitratorTable = null; // Last transaction sequence number that an arbitrator arbitrated on
82         private Map<Long, Transaction> liveTransactionBySequenceNumberTable = null; // live transaction grouped by the sequence number
83         private Map<Pair<Long, Long>, Transaction> liveTransactionByTransactionIdTable = null; // live transaction grouped by the transaction ID
84         private Map<Long, Map<Long, Commit>> liveCommitsTable = null;
85         private Map<IoTString, Commit> liveCommitsByKeyTable = null;
86         private Map<Long, Long> lastCommitSeenSequenceNumberByArbitratorTable = null;
87         private Vector<Long> rejectedSlotList = null; // List of rejected slots that have yet to be sent to the server
88         private List<Transaction> pendingTransactionQueue = null;
89         private List<ArbitrationRound> pendingSendArbitrationRounds = null;
90         private List<Entry> pendingSendArbitrationEntriesToDelete = null;
91         private Map<Transaction, List<Integer>> transactionPartsSent = null;
92         private Map<Long, TransactionStatus> outstandingTransactionStatus = null;
93         private Map<Long, Abort> liveAbortsGeneratedByLocal = null;
94         private Set<Pair<Long, Long>> offlineTransactionsCommittedAndAtServer = null;
95         private Map<Long, Pair<String, Integer>> localCommunicationTable = null;
96         private Map<Long, Long> lastTransactionSeenFromMachineFromServer = null;
97         private Map<Long, Long> lastArbitrationDataLocalSequenceNumberSeenFromArbitrator = null;
98
99
100         public Table(String baseurl, String password, long _localMachineId, int listeningPort, Context context) {
101                 localMachineId = _localMachineId;
102                 cloud = new CloudComm(this, baseurl, password, listeningPort, context);
103
104                 init();
105         }
106
107         public Table(CloudComm _cloud, long _localMachineId) {
108                 localMachineId = _localMachineId;
109                 cloud = _cloud;
110
111                 init();
112         }
113
114         /**
115          * Init all the stuff needed for for table usage
116          */
117         private void init() {
118
119                 // Init helper objects
120                 random = new Random();
121                 buffer = new SlotBuffer();
122
123                 // Set Variables
124                 oldestLiveSlotSequenceNumver = 1;
125
126                 // init data structs
127                 committedKeyValueTable = new HashMap<IoTString, KeyValue>();
128                 speculatedKeyValueTable = new HashMap<IoTString, KeyValue>();
129                 pendingTransactionSpeculatedKeyValueTable = new HashMap<IoTString, KeyValue>();
130                 liveNewKeyTable = new HashMap<IoTString, NewKey>();
131                 lastMessageTable = new HashMap<Long, Pair<Long, Liveness>>();
132                 rejectedMessageWatchListTable = new HashMap<Long, HashSet<RejectedMessage>>();
133                 arbitratorTable = new HashMap<IoTString, Long>();
134                 liveAbortTable = new HashMap<Pair<Long, Long>, Abort>();
135                 newTransactionParts = new HashMap<Long, Map<Pair<Long, Integer>, TransactionPart>>();
136                 newCommitParts = new HashMap<Long, Map<Pair<Long, Integer>, CommitPart>>();
137                 lastArbitratedTransactionNumberByArbitratorTable = new HashMap<Long, Long>();
138                 liveTransactionBySequenceNumberTable = new HashMap<Long, Transaction>();
139                 liveTransactionByTransactionIdTable = new HashMap<Pair<Long, Long>, Transaction>();
140                 liveCommitsTable = new HashMap<Long, Map<Long, Commit>>();
141                 liveCommitsByKeyTable = new HashMap<IoTString, Commit>();
142                 lastCommitSeenSequenceNumberByArbitratorTable = new HashMap<Long, Long>();
143                 rejectedSlotList = new Vector<Long>();
144                 pendingTransactionQueue = new ArrayList<Transaction>();
145                 pendingSendArbitrationEntriesToDelete = new ArrayList<Entry>();
146                 transactionPartsSent = new HashMap<Transaction, List<Integer>>();
147                 outstandingTransactionStatus = new HashMap<Long, TransactionStatus>();
148                 liveAbortsGeneratedByLocal = new HashMap<Long, Abort>();
149                 offlineTransactionsCommittedAndAtServer = new HashSet<Pair<Long, Long>>();
150                 localCommunicationTable = new HashMap<Long, Pair<String, Integer>>();
151                 lastTransactionSeenFromMachineFromServer = new HashMap<Long, Long>();
152                 pendingSendArbitrationRounds = new ArrayList<ArbitrationRound>();
153                 lastArbitrationDataLocalSequenceNumberSeenFromArbitrator = new HashMap<Long, Long>();
154
155
156                 // Other init stuff
157                 numberOfSlots = buffer.capacity();
158                 setResizeThreshold();
159         }
160
161         // TODO: delete method
162         public synchronized void printSlots() {
163                 long o = buffer.getOldestSeqNum();
164                 long n = buffer.getNewestSeqNum();
165
166                 int[] types = new int[10];
167
168                 int num = 0;
169
170                 int livec = 0;
171                 int deadc = 0;
172
173                 int casdasd = 0;
174
175                 int liveslo = 0;
176
177                 for (long i = o; i < (n + 1); i++) {
178                         Slot s = buffer.getSlot(i);
179
180
181                         if (s.isLive()) {
182                                 liveslo++;
183                         }
184
185                         Vector<Entry> entries = s.getEntries();
186
187                         for (Entry e : entries) {
188                                 if (e.isLive()) {
189                                         int type = e.getType();
190
191
192                                         if (type == 6) {
193                                                 RejectedMessage rej = (RejectedMessage)e;
194                                                 casdasd++;
195
196                                                 System.out.println(rej.getMachineID());
197                                         }
198
199
200                                         types[type] = types[type] + 1;
201                                         num++;
202                                         livec++;
203                                 } else {
204                                         deadc++;
205                                 }
206                         }
207                 }
208
209                 for (int i = 0; i < 10; i++) {
210                         System.out.println(i + "    " + types[i]);
211                 }
212                 System.out.println("Live count:   " + livec);
213                 System.out.println("Live Slot count:   " + liveslo);
214
215                 System.out.println("Dead count:   " + deadc);
216                 System.out.println("Old:   " + o);
217                 System.out.println("New:   " + n);
218                 System.out.println("Size:   " + buffer.size());
219                 // System.out.println("Commits:   " + liveCommitsTable.size());
220                 System.out.println("pendingTrans:   " + pendingTransactionQueue.size());
221                 System.out.println("Trans Status Out:   " + outstandingTransactionStatus.size());
222
223                 for (Long k : lastArbitratedTransactionNumberByArbitratorTable.keySet()) {
224                         System.out.println(k + ": " + lastArbitratedTransactionNumberByArbitratorTable.get(k));
225                 }
226
227
228                 for (Long a : liveCommitsTable.keySet()) {
229                         for (Long b : liveCommitsTable.get(a).keySet()) {
230                                 for (KeyValue kv : liveCommitsTable.get(a).get(b).getKeyValueUpdateSet()) {
231                                         System.out.print(kv + " ");
232                                 }
233                                 System.out.print("|| ");
234                         }
235                         System.out.println();
236                 }
237
238         }
239
240         /**
241          * Initialize the table by inserting a table status as the first entry into the table status
242          * also initialize the crypto stuff.
243          */
244         public synchronized void initTable() throws ServerException {
245                 cloud.initSecurity();
246
247                 // Create the first insertion into the block chain which is the table status
248                 Slot s = new Slot(this, 1, localMachineId, localSequenceNumber);
249                 localSequenceNumber++;
250                 TableStatus status = new TableStatus(s, numberOfSlots);
251                 s.addEntry(status);
252                 Slot[] array = cloud.putSlot(s, numberOfSlots);
253
254                 if (array == null) {
255                         array = new Slot[] {s};
256                         // update local block chain
257                         validateAndUpdate(array, true);
258                 } else if (array.length == 1) {
259                         // in case we did push the slot BUT we failed to init it
260                         validateAndUpdate(array, true);
261                 } else {
262                         throw new Error("Error on initialization");
263                 }
264         }
265
266         /**
267          * Rebuild the table from scratch by pulling the latest block chain from the server.
268          */
269         public synchronized void rebuild() throws ServerException {
270                 // Just pull the latest slots from the server
271                 Slot[] newslots = cloud.getSlots(sequenceNumber + 1);
272                 validateAndUpdate(newslots, true);
273                 sendToServer(null);
274                 updateLiveTransactionsAndStatus();
275
276         }
277
278         // public String toString() {
279         //      String retString = " Committed Table: \n";
280         //      retString += "---------------------------\n";
281         //      retString += commitedTable.toString();
282
283         //      retString += "\n\n";
284
285         //      retString += " Speculative Table: \n";
286         //      retString += "---------------------------\n";
287         //      retString += speculativeTable.toString();
288
289         //      return retString;
290         // }
291
292         public synchronized void addLocalCommunication(long arbitrator, String hostName, int portNumber) {
293                 localCommunicationTable.put(arbitrator, new Pair<String, Integer>(hostName, portNumber));
294         }
295
296         public synchronized Long getArbitrator(IoTString key) {
297                 return arbitratorTable.get(key);
298         }
299
300         public synchronized void close() {
301                 cloud.close();
302         }
303
304         public synchronized IoTString getCommitted(IoTString key)  {
305                 KeyValue kv = committedKeyValueTable.get(key);
306
307                 if (kv != null) {
308                         return kv.getValue();
309                 } else {
310                         return null;
311                 }
312         }
313
314         public synchronized IoTString getSpeculative(IoTString key) {
315                 KeyValue kv = pendingTransactionSpeculatedKeyValueTable.get(key);
316
317                 if (kv == null) {
318                         kv = speculatedKeyValueTable.get(key);
319                 }
320
321                 if (kv == null) {
322                         kv = committedKeyValueTable.get(key);
323                 }
324
325                 if (kv != null) {
326                         return kv.getValue();
327                 } else {
328                         return null;
329                 }
330         }
331
332         public synchronized IoTString getCommittedAtomic(IoTString key) {
333                 KeyValue kv = committedKeyValueTable.get(key);
334
335                 if (arbitratorTable.get(key) == null) {
336                         throw new Error("Key not Found.");
337                 }
338
339                 // Make sure new key value pair matches the current arbitrator
340                 if (!pendingTransactionBuilder.checkArbitrator(arbitratorTable.get(key))) {
341                         // TODO: Maybe not throw en error
342                         throw new Error("Not all Key Values Match Arbitrator.");
343                 }
344
345                 if (kv != null) {
346                         pendingTransactionBuilder.addKVGuard(new KeyValue(key, kv.getValue()));
347                         return kv.getValue();
348                 } else {
349                         pendingTransactionBuilder.addKVGuard(new KeyValue(key, null));
350                         return null;
351                 }
352         }
353
354         public synchronized IoTString getSpeculativeAtomic(IoTString key) {
355                 if (arbitratorTable.get(key) == null) {
356                         throw new Error("Key not Found.");
357                 }
358
359                 // Make sure new key value pair matches the current arbitrator
360                 if (!pendingTransactionBuilder.checkArbitrator(arbitratorTable.get(key))) {
361                         // TODO: Maybe not throw en error
362                         throw new Error("Not all Key Values Match Arbitrator.");
363                 }
364
365                 KeyValue kv = pendingTransactionSpeculatedKeyValueTable.get(key);
366
367                 if (kv == null) {
368                         kv = speculatedKeyValueTable.get(key);
369                 }
370
371                 if (kv == null) {
372                         kv = committedKeyValueTable.get(key);
373                 }
374
375                 if (kv != null) {
376                         pendingTransactionBuilder.addKVGuard(new KeyValue(key, kv.getValue()));
377                         return kv.getValue();
378                 } else {
379                         pendingTransactionBuilder.addKVGuard(new KeyValue(key, null));
380                         return null;
381                 }
382         }
383
384         public synchronized boolean update()  {
385                 try {
386                         Slot[] newSlots = cloud.getSlots(sequenceNumber + 1);
387                         validateAndUpdate(newSlots, false);
388                         sendToServer(null);
389
390
391                         updateLiveTransactionsAndStatus();
392
393                         return true;
394                 } catch (Exception e) {
395                         // e.printStackTrace();
396
397                         for (Long m : localCommunicationTable.keySet()) {
398                                 updateFromLocal(m);
399                         }
400                 }
401
402                 return false;
403         }
404
405         public synchronized boolean createNewKey(IoTString keyName, long machineId) throws ServerException {
406                 while (true) {
407                         if (arbitratorTable.get(keyName) != null) {
408                                 // There is already an arbitrator
409                                 return false;
410                         }
411
412                         NewKey newKey = new NewKey(null, keyName, machineId);
413
414                         if (sendToServer(newKey)) {
415                                 // If successfully inserted
416                                 return true;
417                         }
418                 }
419         }
420
421         public synchronized void startTransaction() {
422                 // Create a new transaction, invalidates any old pending transactions.
423                 pendingTransactionBuilder = new PendingTransaction(localMachineId);
424         }
425
426         public synchronized void addKV(IoTString key, IoTString value) {
427
428                 // Make sure it is a valid key
429                 if (arbitratorTable.get(key) == null) {
430                         throw new Error("Key not Found.");
431                 }
432
433                 // Make sure new key value pair matches the current arbitrator
434                 if (!pendingTransactionBuilder.checkArbitrator(arbitratorTable.get(key))) {
435                         // TODO: Maybe not throw en error
436                         throw new Error("Not all Key Values Match Arbitrator.");
437                 }
438
439                 // Add the key value to this transaction
440                 KeyValue kv = new KeyValue(key, value);
441                 pendingTransactionBuilder.addKV(kv);
442         }
443
444         public synchronized TransactionStatus commitTransaction() {
445
446                 if (pendingTransactionBuilder.getKVUpdates().size() == 0) {
447                         // transaction with no updates will have no effect on the system
448                         return new TransactionStatus(TransactionStatus.StatusNoEffect, -1);
449                 }
450
451                 // Set the local transaction sequence number and increment
452                 pendingTransactionBuilder.setClientLocalSequenceNumber(localTransactionSequenceNumber);
453                 localTransactionSequenceNumber++;
454
455                 // Create the transaction status
456                 TransactionStatus transactionStatus = new TransactionStatus(TransactionStatus.StatusPending, pendingTransactionBuilder.getArbitrator());
457
458                 // Create the new transaction
459                 Transaction newTransaction = pendingTransactionBuilder.createTransaction();
460                 newTransaction.setTransactionStatus(transactionStatus);
461
462                 if (pendingTransactionBuilder.getArbitrator() != localMachineId) {
463                         // Add it to the queue and invalidate the builder for safety
464                         pendingTransactionQueue.add(newTransaction);
465                 } else {
466                         arbitrateOnLocalTransaction(newTransaction);
467                         updateLiveStateFromLocal();
468                 }
469
470                 pendingTransactionBuilder = new PendingTransaction(localMachineId);
471
472                 try {
473                         sendToServer(null);
474                 } catch (ServerException e) {
475
476                         Set<Long> arbitratorTriedAndFailed = new HashSet<Long>();
477                         for (Iterator<Transaction> iter = pendingTransactionQueue.iterator(); iter.hasNext(); ) {
478                                 Transaction transaction = iter.next();
479
480                                 if (arbitratorTriedAndFailed.contains(transaction.getArbitrator())) {
481                                         // Already contacted this client so ignore all attempts to contact this client
482                                         // to preserve ordering for arbitrator
483                                         continue;
484                                 }
485
486                                 Pair<Boolean, Boolean> sendReturn = sendTransactionToLocal(transaction);
487
488                                 if (sendReturn.getFirst()) {
489                                         // Failed to contact over local
490                                         arbitratorTriedAndFailed.add(transaction.getArbitrator());
491                                 } else {
492                                         // Successful contact or should not contact
493
494                                         if (sendReturn.getSecond()) {
495                                                 // did arbitrate
496                                                 iter.remove();
497                                         }
498                                 }
499                         }
500                 }
501
502                 updateLiveStateFromLocal();
503
504                 return transactionStatus;
505         }
506
507         /**
508          * Get the machine ID for this client
509          */
510         public long getMachineId() {
511                 return localMachineId;
512         }
513
514         /**
515          * Decrement the number of live slots that we currently have
516          */
517         public void decrementLiveCount() {
518                 liveSlotCount--;
519         }
520
521         /**
522          * Recalculate the new resize threshold
523          */
524         private void setResizeThreshold() {
525                 int resizeLower = (int) (RESIZE_THRESHOLD * numberOfSlots);
526                 bufferResizeThreshold = resizeLower - 1 + random.nextInt(numberOfSlots - resizeLower);
527         }
528
529         public long getLocalSequenceNumber() {
530                 return localSequenceNumber;
531         }
532
533
534         boolean lastInsertedNewKey = false;
535
536         private boolean sendToServer(NewKey newKey) throws ServerException {
537
538                 boolean fromRetry = false;
539
540                 try {
541                         if (hadPartialSendToServer) {
542                                 Slot[] newSlots = cloud.getSlots(sequenceNumber + 1);
543                                 if (newSlots.length == 0) {
544                                         fromRetry = true;
545                                         ThreeTuple<Boolean, Boolean, Slot[]> sendSlotsReturn = sendSlotsToServer(lastSlotAttemptedToSend, lastNewSize, lastIsNewKey);
546
547                                         if (sendSlotsReturn.getFirst()) {
548                                                 if (newKey != null) {
549                                                         if (lastInsertedNewKey && (lastNewKey.getKey() == newKey.getKey()) && (lastNewKey.getMachineID() == newKey.getMachineID())) {
550                                                                 newKey = null;
551                                                         }
552                                                 }
553
554                                                 for (Transaction transaction : lastTransactionPartsSent.keySet()) {
555                                                         transaction.resetServerFailure();
556
557                                                         // Update which transactions parts still need to be sent
558                                                         transaction.removeSentParts(lastTransactionPartsSent.get(transaction));
559
560                                                         // Add the transaction status to the outstanding list
561                                                         outstandingTransactionStatus.put(transaction.getSequenceNumber(), transaction.getTransactionStatus());
562
563                                                         // Update the transaction status
564                                                         transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentPartial);
565
566                                                         // Check if all the transaction parts were successfully sent and if so then remove it from pending
567                                                         if (transaction.didSendAllParts()) {
568                                                                 transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentFully);
569                                                                 pendingTransactionQueue.remove(transaction);
570                                                         }
571                                                 }
572                                         } else {
573
574                                                 newSlots = sendSlotsReturn.getThird();
575
576                                                 boolean isInserted = false;
577                                                 for (Slot s : newSlots) {
578                                                         if ((s.getSequenceNumber() == lastSlotAttemptedToSend.getSequenceNumber()) && (s.getMachineID() == localMachineId)) {
579                                                                 isInserted = true;
580                                                                 break;
581                                                         }
582                                                 }
583
584                                                 for (Slot s : newSlots) {
585                                                         if (isInserted) {
586                                                                 break;
587                                                         }
588
589                                                         // Process each entry in the slot
590                                                         for (Entry entry : s.getEntries()) {
591
592                                                                 if (entry.getType() == Entry.TypeLastMessage) {
593                                                                         LastMessage lastMessage = (LastMessage)entry;
594                                                                         if ((lastMessage.getMachineID() == localMachineId) && (lastMessage.getSequenceNumber() == lastSlotAttemptedToSend.getSequenceNumber())) {
595                                                                                 isInserted = true;
596                                                                                 break;
597                                                                         }
598                                                                 }
599                                                         }
600                                                 }
601
602                                                 if (isInserted) {
603                                                         if (newKey != null) {
604                                                                 if (lastInsertedNewKey && (lastNewKey.getKey() == newKey.getKey()) && (lastNewKey.getMachineID() == newKey.getMachineID())) {
605                                                                         newKey = null;
606                                                                 }
607                                                         }
608
609                                                         for (Transaction transaction : lastTransactionPartsSent.keySet()) {
610                                                                 transaction.resetServerFailure();
611
612                                                                 // Update which transactions parts still need to be sent
613                                                                 transaction.removeSentParts(lastTransactionPartsSent.get(transaction));
614
615                                                                 // Add the transaction status to the outstanding list
616                                                                 outstandingTransactionStatus.put(transaction.getSequenceNumber(), transaction.getTransactionStatus());
617
618                                                                 // Update the transaction status
619                                                                 transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentPartial);
620
621                                                                 // Check if all the transaction parts were successfully sent and if so then remove it from pending
622                                                                 if (transaction.didSendAllParts()) {
623                                                                         transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentFully);
624                                                                         pendingTransactionQueue.remove(transaction);
625                                                                 } else {
626                                                                         transaction.resetServerFailure();
627                                                                         // Set the transaction sequence number back to nothing
628                                                                         if (!transaction.didSendAPartToServer()) {
629                                                                                 transaction.setSequenceNumber(-1);
630                                                                         }
631                                                                 }
632                                                         }
633                                                 }
634                                         }
635
636                                         for (Transaction transaction : lastTransactionPartsSent.keySet()) {
637                                                 transaction.resetServerFailure();
638                                                 // Set the transaction sequence number back to nothing
639                                                 if (!transaction.didSendAPartToServer()) {
640                                                         transaction.setSequenceNumber(-1);
641                                                 }
642                                         }
643
644                                         if (sendSlotsReturn.getThird().length != 0) {
645                                                 // insert into the local block chain
646                                                 validateAndUpdate(sendSlotsReturn.getThird(), true);
647                                         }
648                                         // continue;
649                                 } else {
650                                         boolean isInserted = false;
651                                         for (Slot s : newSlots) {
652                                                 if ((s.getSequenceNumber() == lastSlotAttemptedToSend.getSequenceNumber()) && (s.getMachineID() == localMachineId)) {
653                                                         isInserted = true;
654                                                         break;
655                                                 }
656                                         }
657
658                                         for (Slot s : newSlots) {
659                                                 if (isInserted) {
660                                                         break;
661                                                 }
662
663                                                 // Process each entry in the slot
664                                                 for (Entry entry : s.getEntries()) {
665
666                                                         if (entry.getType() == Entry.TypeLastMessage) {
667                                                                 LastMessage lastMessage = (LastMessage)entry;
668                                                                 if ((lastMessage.getMachineID() == localMachineId) && (lastMessage.getSequenceNumber() == lastSlotAttemptedToSend.getSequenceNumber())) {
669                                                                         isInserted = true;
670                                                                         break;
671                                                                 }
672                                                         }
673                                                 }
674                                         }
675
676                                         if (isInserted) {
677                                                 if (newKey != null) {
678                                                         if (lastInsertedNewKey && (lastNewKey.getKey() == newKey.getKey()) && (lastNewKey.getMachineID() == newKey.getMachineID())) {
679                                                                 newKey = null;
680                                                         }
681                                                 }
682
683                                                 for (Transaction transaction : lastTransactionPartsSent.keySet()) {
684                                                         transaction.resetServerFailure();
685
686                                                         // Update which transactions parts still need to be sent
687                                                         transaction.removeSentParts(lastTransactionPartsSent.get(transaction));
688
689                                                         // Add the transaction status to the outstanding list
690                                                         outstandingTransactionStatus.put(transaction.getSequenceNumber(), transaction.getTransactionStatus());
691
692                                                         // Update the transaction status
693                                                         transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentPartial);
694
695                                                         // Check if all the transaction parts were successfully sent and if so then remove it from pending
696                                                         if (transaction.didSendAllParts()) {
697                                                                 transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentFully);
698                                                                 pendingTransactionQueue.remove(transaction);
699                                                         } else {
700                                                                 transaction.resetServerFailure();
701                                                                 // Set the transaction sequence number back to nothing
702                                                                 if (!transaction.didSendAPartToServer()) {
703                                                                         transaction.setSequenceNumber(-1);
704                                                                 }
705                                                         }
706                                                 }
707                                         } else {
708                                                 for (Transaction transaction : lastTransactionPartsSent.keySet()) {
709                                                         transaction.resetServerFailure();
710                                                         // Set the transaction sequence number back to nothing
711                                                         if (!transaction.didSendAPartToServer()) {
712                                                                 transaction.setSequenceNumber(-1);
713                                                         }
714                                                 }
715                                         }
716
717                                         // insert into the local block chain
718                                         validateAndUpdate(newSlots, true);
719                                 }
720                         }
721                 } catch (ServerException e) {
722                         throw e;
723                 }
724
725
726
727                 try {
728                         // While we have stuff that needs inserting into the block chain
729                         while ((pendingTransactionQueue.size() > 0) || (pendingSendArbitrationRounds.size() > 0) || (newKey != null)) {
730
731                                 fromRetry = false;
732
733                                 if (hadPartialSendToServer) {
734                                         throw new Error("Should Be error free");
735                                 }
736
737
738
739                                 // If there is a new key with same name then end
740                                 if ((newKey != null) && (arbitratorTable.get(newKey.getKey()) != null)) {
741                                         return false;
742                                 }
743
744                                 // Create the slot
745                                 Slot slot = new Slot(this, sequenceNumber + 1, localMachineId, buffer.getSlot(sequenceNumber).getHMAC(), localSequenceNumber);
746                                 localSequenceNumber++;
747
748                                 // Try to fill the slot with data
749                                 ThreeTuple<Boolean, Integer, Boolean> fillSlotsReturn = fillSlot(slot, false, newKey);
750                                 boolean needsResize = fillSlotsReturn.getFirst();
751                                 int newSize = fillSlotsReturn.getSecond();
752                                 Boolean insertedNewKey = fillSlotsReturn.getThird();
753
754                                 if (needsResize) {
755                                         // Reset which transaction to send
756                                         for (Transaction transaction : transactionPartsSent.keySet()) {
757                                                 transaction.resetNextPartToSend();
758
759                                                 // Set the transaction sequence number back to nothing
760                                                 if (!transaction.didSendAPartToServer() && !transaction.getServerFailure()) {
761                                                         transaction.setSequenceNumber(-1);
762                                                 }
763                                         }
764
765                                         // Clear the sent data since we are trying again
766                                         pendingSendArbitrationEntriesToDelete.clear();
767                                         transactionPartsSent.clear();
768
769                                         // We needed a resize so try again
770                                         fillSlot(slot, true, newKey);
771                                 }
772
773                                 lastSlotAttemptedToSend = slot;
774                                 lastIsNewKey = (newKey != null);
775                                 lastInsertedNewKey = insertedNewKey;
776                                 lastNewSize = newSize;
777                                 lastNewKey = newKey;
778                                 lastTransactionPartsSent = new HashMap<Transaction, List<Integer>>(transactionPartsSent);
779                                 lastPendingSendArbitrationEntriesToDelete = new ArrayList<Entry>(pendingSendArbitrationEntriesToDelete);
780
781
782                                 ThreeTuple<Boolean, Boolean, Slot[]> sendSlotsReturn = sendSlotsToServer(slot, newSize, newKey != null);
783
784                                 if (sendSlotsReturn.getFirst()) {
785
786                                         // Did insert into the block chain
787
788                                         if (insertedNewKey) {
789                                                 // This slot was what was inserted not a previous slot
790
791                                                 // New Key was successfully inserted into the block chain so dont want to insert it again
792                                                 newKey = null;
793                                         }
794
795                                         // Remove the aborts and commit parts that were sent from the pending to send queue
796                                         for (Iterator<ArbitrationRound> iter = pendingSendArbitrationRounds.iterator(); iter.hasNext(); ) {
797                                                 ArbitrationRound round = iter.next();
798                                                 round.removeParts(pendingSendArbitrationEntriesToDelete);
799
800                                                 if (round.isDoneSending()) {
801                                                         // Sent all the parts
802                                                         iter.remove();
803                                                 }
804                                         }
805
806                                         for (Transaction transaction : transactionPartsSent.keySet()) {
807                                                 transaction.resetServerFailure();
808
809                                                 // Update which transactions parts still need to be sent
810                                                 transaction.removeSentParts(transactionPartsSent.get(transaction));
811
812                                                 // Add the transaction status to the outstanding list
813                                                 outstandingTransactionStatus.put(transaction.getSequenceNumber(), transaction.getTransactionStatus());
814
815                                                 // Update the transaction status
816                                                 transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentPartial);
817
818                                                 // Check if all the transaction parts were successfully sent and if so then remove it from pending
819                                                 if (transaction.didSendAllParts()) {
820                                                         transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentFully);
821                                                         pendingTransactionQueue.remove(transaction);
822                                                 }
823                                         }
824                                 } else {
825
826                                         // if (!sendSlotsReturn.getSecond()) {
827                                         //      for (Transaction transaction : lastTransactionPartsSent.keySet()) {
828                                         //              transaction.resetServerFailure();
829                                         //      }
830                                         // } else {
831                                         //      for (Transaction transaction : lastTransactionPartsSent.keySet()) {
832                                         //              transaction.resetServerFailure();
833
834                                         //              // Update which transactions parts still need to be sent
835                                         //              transaction.removeSentParts(transactionPartsSent.get(transaction));
836
837                                         //              // Add the transaction status to the outstanding list
838                                         //              outstandingTransactionStatus.put(transaction.getSequenceNumber(), transaction.getTransactionStatus());
839
840                                         //              // Update the transaction status
841                                         //              transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentPartial);
842
843                                         //              // Check if all the transaction parts were successfully sent and if so then remove it from pending
844                                         //              if (transaction.didSendAllParts()) {
845                                         //                      transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentFully);
846                                         //                      pendingTransactionQueue.remove(transaction);
847
848                                         //                      for (KeyValue kv : transaction.getKeyValueUpdateSet()) {
849                                         //                              System.out.println("Sent: " + kv + "  from: " + localMachineId + "   Slot:" + lastSlotAttemptedToSend.getSequenceNumber() + "  Claimed:" + transaction.getSequenceNumber());
850                                         //                      }
851                                         //              }
852                                         //      }
853                                         // }
854
855                                         // Reset which transaction to send
856                                         for (Transaction transaction : transactionPartsSent.keySet()) {
857                                                 transaction.resetNextPartToSend();
858                                                 // transaction.resetNextPartToSend();
859
860                                                 // Set the transaction sequence number back to nothing
861                                                 if (!transaction.didSendAPartToServer() && !transaction.getServerFailure()) {
862                                                         transaction.setSequenceNumber(-1);
863                                                 }
864                                         }
865                                 }
866
867                                 // Clear the sent data in preparation for next send
868                                 pendingSendArbitrationEntriesToDelete.clear();
869                                 transactionPartsSent.clear();
870
871                                 if (sendSlotsReturn.getThird().length != 0) {
872                                         // insert into the local block chain
873                                         validateAndUpdate(sendSlotsReturn.getThird(), true);
874                                 }
875                         }
876
877                 } catch (ServerException e) {
878
879                         if (e.getType() != ServerException.TypeInputTimeout) {
880                                 // e.printStackTrace();
881
882                                 // Nothing was able to be sent to the server so just clear these data structures
883                                 for (Transaction transaction : transactionPartsSent.keySet()) {
884                                         transaction.resetNextPartToSend();
885
886                                         // Set the transaction sequence number back to nothing
887                                         if (!transaction.didSendAPartToServer() && !transaction.getServerFailure()) {
888                                                 transaction.setSequenceNumber(-1);
889                                         }
890                                 }
891                         } else {
892                                 // There was a partial send to the server
893                                 hadPartialSendToServer = true;
894
895
896                                 // if (!fromRetry) {
897                                 //      lastTransactionPartsSent = new HashMap<Transaction, List<Integer>>(transactionPartsSent);
898                                 //      lastPendingSendArbitrationEntriesToDelete = new ArrayList<Entry>(pendingSendArbitrationEntriesToDelete);
899                                 // }
900
901                                 // Nothing was able to be sent to the server so just clear these data structures
902                                 for (Transaction transaction : transactionPartsSent.keySet()) {
903                                         transaction.resetNextPartToSend();
904                                         transaction.setServerFailure();
905                                 }
906                         }
907
908                         pendingSendArbitrationEntriesToDelete.clear();
909                         transactionPartsSent.clear();
910
911                         throw e;
912                 }
913
914                 return newKey == null;
915         }
916
917         private synchronized boolean updateFromLocal(long machineId) {
918                 Pair<String, Integer> localCommunicationInformation = localCommunicationTable.get(machineId);
919                 if (localCommunicationInformation == null) {
920                         // Cant talk to that device locally so do nothing
921                         return false;
922                 }
923
924                 // Get the size of the send data
925                 //int sendDataSize = Integer.BYTES + Long.BYTES;
926                 int sendDataSize = Integer.SIZE/8 + Long.SIZE/8;
927
928                 Long lastArbitrationDataLocalSequenceNumber = (long) - 1;
929                 if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(machineId) != null) {
930                         lastArbitrationDataLocalSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(machineId);
931                 }
932
933                 byte[] sendData = new byte[sendDataSize];
934                 ByteBuffer bbEncode = ByteBuffer.wrap(sendData);
935
936                 // Encode the data
937                 bbEncode.putLong(lastArbitrationDataLocalSequenceNumber);
938                 bbEncode.putInt(0);
939
940                 // Send by local
941                 byte[] returnData = cloud.sendLocalData(sendData, localSequenceNumber, localCommunicationInformation.getFirst(), localCommunicationInformation.getSecond());
942                 localSequenceNumber++;
943
944                 if (returnData == null) {
945                         // Could not contact server
946                         return false;
947                 }
948
949                 // Decode the data
950                 ByteBuffer bbDecode = ByteBuffer.wrap(returnData);
951                 int numberOfEntries = bbDecode.getInt();
952
953                 for (int i = 0; i < numberOfEntries; i++) {
954                         byte type = bbDecode.get();
955                         if (type == Entry.TypeAbort) {
956                                 Abort abort = (Abort)Abort.decode(null, bbDecode);
957                                 processEntry(abort);
958                         } else if (type == Entry.TypeCommitPart) {
959                                 CommitPart commitPart = (CommitPart)CommitPart.decode(null, bbDecode);
960                                 processEntry(commitPart);
961                         }
962                 }
963
964                 updateLiveStateFromLocal();
965
966                 return true;
967         }
968
969         private Pair<Boolean, Boolean> sendTransactionToLocal(Transaction transaction) {
970
971                 // Get the devices local communications
972                 Pair<String, Integer> localCommunicationInformation = localCommunicationTable.get(transaction.getArbitrator());
973
974                 if (localCommunicationInformation == null) {
975                         // Cant talk to that device locally so do nothing
976                         return new Pair<Boolean, Boolean>(true, false);
977                 }
978
979                 // Get the size of the send data
980                 //int sendDataSize = Integer.BYTES + Long.BYTES;
981                 int sendDataSize = Integer.SIZE/8 + Long.SIZE/8;
982                 for (TransactionPart part : transaction.getParts().values()) {
983                         sendDataSize += part.getSize();
984                 }
985
986                 Long lastArbitrationDataLocalSequenceNumber = (long) - 1;
987                 if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(transaction.getArbitrator()) != null) {
988                         lastArbitrationDataLocalSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(transaction.getArbitrator());
989                 }
990
991                 // Make the send data size
992                 byte[] sendData = new byte[sendDataSize];
993                 ByteBuffer bbEncode = ByteBuffer.wrap(sendData);
994
995                 // Encode the data
996                 bbEncode.putLong(lastArbitrationDataLocalSequenceNumber);
997                 bbEncode.putInt(transaction.getParts().size());
998                 for (TransactionPart part : transaction.getParts().values()) {
999                         part.encode(bbEncode);
1000                 }
1001
1002
1003                 // Send by local
1004                 byte[] returnData = cloud.sendLocalData(sendData, localSequenceNumber, localCommunicationInformation.getFirst(), localCommunicationInformation.getSecond());
1005                 localSequenceNumber++;
1006
1007                 if (returnData == null) {
1008                         // Could not contact server
1009                         return new Pair<Boolean, Boolean>(true, false);
1010                 }
1011
1012                 // Decode the data
1013                 ByteBuffer bbDecode = ByteBuffer.wrap(returnData);
1014                 boolean didCommit = bbDecode.get() == 1;
1015                 boolean couldArbitrate = bbDecode.get() == 1;
1016                 int numberOfEntries = bbDecode.getInt();
1017                 boolean foundAbort = false;
1018
1019                 for (int i = 0; i < numberOfEntries; i++) {
1020                         byte type = bbDecode.get();
1021                         if (type == Entry.TypeAbort) {
1022                                 Abort abort = (Abort)Abort.decode(null, bbDecode);
1023
1024                                 if ((abort.getTransactionMachineId() == localMachineId) && (abort.getTransactionClientLocalSequenceNumber() == transaction.getClientLocalSequenceNumber())) {
1025                                         foundAbort = true;
1026                                 }
1027
1028                                 processEntry(abort);
1029                         } else if (type == Entry.TypeCommitPart) {
1030                                 CommitPart commitPart = (CommitPart)CommitPart.decode(null, bbDecode);
1031                                 processEntry(commitPart);
1032                         }
1033                 }
1034
1035                 updateLiveStateFromLocal();
1036
1037                 if (couldArbitrate) {
1038                         TransactionStatus status =  transaction.getTransactionStatus();
1039                         if (didCommit) {
1040                                 status.setStatus(TransactionStatus.StatusCommitted);
1041                         } else {
1042                                 status.setStatus(TransactionStatus.StatusAborted);
1043                         }
1044                 } else {
1045                         TransactionStatus status =  transaction.getTransactionStatus();
1046                         if (foundAbort) {
1047                                 status.setStatus(TransactionStatus.StatusAborted);
1048                         } else {
1049                                 status.setStatus(TransactionStatus.StatusCommitted);
1050                         }
1051                 }
1052
1053                 return new Pair<Boolean, Boolean>(false, true);
1054         }
1055
1056         public synchronized byte[] acceptDataFromLocal(byte[] data) {
1057
1058                 // Decode the data
1059                 ByteBuffer bbDecode = ByteBuffer.wrap(data);
1060                 long lastArbitratedSequenceNumberSeen = bbDecode.getLong();
1061                 int numberOfParts = bbDecode.getInt();
1062
1063                 // If we did commit a transaction or not
1064                 boolean didCommit = false;
1065                 boolean couldArbitrate = false;
1066
1067                 if (numberOfParts != 0) {
1068
1069                         // decode the transaction
1070                         Transaction transaction = new Transaction();
1071                         for (int i = 0; i < numberOfParts; i++) {
1072                                 bbDecode.get();
1073                                 TransactionPart newPart = (TransactionPart)TransactionPart.decode(null, bbDecode);
1074                                 transaction.addPartDecode(newPart);
1075                         }
1076
1077                         // Arbitrate on transaction and pull relevant return data
1078                         Pair<Boolean, Boolean> localArbitrateReturn = arbitrateOnLocalTransaction(transaction);
1079                         couldArbitrate = localArbitrateReturn.getFirst();
1080                         didCommit = localArbitrateReturn.getSecond();
1081
1082                         updateLiveStateFromLocal();
1083
1084                         // Transaction was sent to the server so keep track of it to prevent double commit
1085                         if (transaction.getSequenceNumber() != -1) {
1086                                 offlineTransactionsCommittedAndAtServer.add(transaction.getId());
1087                         }
1088                 }
1089
1090                 // The data to send back
1091                 int returnDataSize = 0;
1092                 List<Entry> unseenArbitrations = new ArrayList<Entry>();
1093
1094                 // Get the aborts to send back
1095                 List<Long> abortLocalSequenceNumbers = new ArrayList<Long >(liveAbortsGeneratedByLocal.keySet());
1096                 Collections.sort(abortLocalSequenceNumbers);
1097                 for (Long localSequenceNumber : abortLocalSequenceNumbers) {
1098                         if (localSequenceNumber <= lastArbitratedSequenceNumberSeen) {
1099                                 continue;
1100                         }
1101
1102                         Abort abort = liveAbortsGeneratedByLocal.get(localSequenceNumber);
1103                         unseenArbitrations.add(abort);
1104                         returnDataSize += abort.getSize();
1105                 }
1106
1107                 // Get the commits to send back
1108                 Map<Long, Commit> commitForClientTable = liveCommitsTable.get(localMachineId);
1109                 if (commitForClientTable != null) {
1110                         List<Long> commitLocalSequenceNumbers = new ArrayList<Long>(commitForClientTable.keySet());
1111                         Collections.sort(commitLocalSequenceNumbers);
1112
1113                         for (Long localSequenceNumber : commitLocalSequenceNumbers) {
1114                                 Commit commit = commitForClientTable.get(localSequenceNumber);
1115
1116                                 if (localSequenceNumber <= lastArbitratedSequenceNumberSeen) {
1117                                         continue;
1118                                 }
1119
1120                                 unseenArbitrations.addAll(commit.getParts().values());
1121
1122                                 for (CommitPart commitPart : commit.getParts().values()) {
1123                                         returnDataSize += commitPart.getSize();
1124                                 }
1125                         }
1126                 }
1127
1128                 // Number of arbitration entries to decode
1129                 //returnDataSize += 2 * Integer.BYTES;
1130                 returnDataSize += 2 * Integer.SIZE/8;
1131
1132                 // Boolean of did commit or not
1133                 if (numberOfParts != 0) {
1134                         //returnDataSize += Byte.BYTES;
1135                         returnDataSize += Byte.SIZE/8;
1136                 }
1137
1138                 // Data to send Back
1139                 byte[] returnData = new byte[returnDataSize];
1140                 ByteBuffer bbEncode = ByteBuffer.wrap(returnData);
1141
1142                 if (numberOfParts != 0) {
1143                         if (didCommit) {
1144                                 bbEncode.put((byte)1);
1145                         } else {
1146                                 bbEncode.put((byte)0);
1147                         }
1148                         if (couldArbitrate) {
1149                                 bbEncode.put((byte)1);
1150                         } else {
1151                                 bbEncode.put((byte)0);
1152                         }
1153                 }
1154
1155                 bbEncode.putInt(unseenArbitrations.size());
1156                 for (Entry entry : unseenArbitrations) {
1157                         entry.encode(bbEncode);
1158                 }
1159
1160
1161                 localSequenceNumber++;
1162                 return returnData;
1163         }
1164
1165         private ThreeTuple<Boolean, Boolean, Slot[]> sendSlotsToServer(Slot slot, int newSize, boolean isNewKey)  throws ServerException {
1166
1167                 boolean attemptedToSendToServerTmp = attemptedToSendToServer;
1168                 attemptedToSendToServer = true;
1169
1170                 boolean inserted = false;
1171                 boolean lastTryInserted = false;
1172
1173                 Slot[] array = cloud.putSlot(slot, newSize);
1174                 if (array == null) {
1175                         array = new Slot[] {slot};
1176                         rejectedSlotList.clear();
1177                         inserted = true;
1178                 }       else {
1179                         if (array.length == 0) {
1180                                 throw new Error("Server Error: Did not send any slots");
1181                         }
1182
1183                         // if (attemptedToSendToServerTmp) {
1184                         if (hadPartialSendToServer) {
1185
1186                                 boolean isInserted = false;
1187                                 for (Slot s : array) {
1188                                         if ((s.getSequenceNumber() == slot.getSequenceNumber()) && (s.getMachineID() == localMachineId)) {
1189                                                 isInserted = true;
1190                                                 break;
1191                                         }
1192                                 }
1193
1194                                 for (Slot s : array) {
1195                                         if (isInserted) {
1196                                                 break;
1197                                         }
1198
1199                                         // Process each entry in the slot
1200                                         for (Entry entry : s.getEntries()) {
1201
1202                                                 if (entry.getType() == Entry.TypeLastMessage) {
1203                                                         LastMessage lastMessage = (LastMessage)entry;
1204
1205                                                         if ((lastMessage.getMachineID() == localMachineId) && (lastMessage.getSequenceNumber() == slot.getSequenceNumber())) {
1206                                                                 isInserted = true;
1207                                                                 break;
1208                                                         }
1209                                                 }
1210                                         }
1211                                 }
1212
1213                                 if (!isInserted) {
1214                                         rejectedSlotList.add(slot.getSequenceNumber());
1215                                         lastTryInserted = false;
1216                                 } else {
1217                                         lastTryInserted = true;
1218                                 }
1219                         } else {
1220                                 rejectedSlotList.add(slot.getSequenceNumber());
1221                                 lastTryInserted = false;
1222                         }
1223                 }
1224
1225                 return new ThreeTuple<Boolean, Boolean, Slot[]>(inserted, lastTryInserted, array);
1226         }
1227
1228         /**
1229          * Returns false if a resize was needed
1230          */
1231         private ThreeTuple<Boolean, Integer, Boolean> fillSlot(Slot slot, boolean resize, NewKey newKeyEntry) {
1232
1233
1234                 int newSize = 0;
1235                 if (liveSlotCount > bufferResizeThreshold) {
1236                         resize = true; //Resize is forced
1237
1238                 }
1239
1240                 if (resize) {
1241                         newSize = (int) (numberOfSlots * RESIZE_MULTIPLE);
1242                         TableStatus status = new TableStatus(slot, newSize);
1243                         slot.addEntry(status);
1244                 }
1245
1246                 // Fill with rejected slots first before doing anything else
1247                 doRejectedMessages(slot);
1248
1249                 // Do mandatory rescue of entries
1250                 ThreeTuple<Boolean, Boolean, Long> mandatoryRescueReturn = doMandatoryResuce(slot, resize);
1251
1252                 // Extract working variables
1253                 boolean needsResize = mandatoryRescueReturn.getFirst();
1254                 boolean seenLiveSlot = mandatoryRescueReturn.getSecond();
1255                 long currentRescueSequenceNumber = mandatoryRescueReturn.getThird();
1256
1257                 if (needsResize && !resize) {
1258                         // We need to resize but we are not resizing so return false
1259                         return new ThreeTuple<Boolean, Integer, Boolean>(true, null, null);
1260                 }
1261
1262                 boolean inserted = false;
1263                 if (newKeyEntry != null) {
1264                         newKeyEntry.setSlot(slot);
1265                         if (slot.hasSpace(newKeyEntry)) {
1266
1267                                 slot.addEntry(newKeyEntry);
1268                                 inserted = true;
1269                         }
1270                 }
1271
1272                 // Clear the transactions, aborts and commits that were sent previously
1273                 transactionPartsSent.clear();
1274                 pendingSendArbitrationEntriesToDelete.clear();
1275
1276                 for (ArbitrationRound round : pendingSendArbitrationRounds) {
1277                         boolean isFull = false;
1278                         round.generateParts();
1279                         List<Entry> parts = round.getParts();
1280
1281                         // Insert pending arbitration data
1282                         for (Entry arbitrationData : parts) {
1283
1284                                 // If it is an abort then we need to set some information
1285                                 if (arbitrationData instanceof Abort) {
1286                                         ((Abort)arbitrationData).setSequenceNumber(slot.getSequenceNumber());
1287                                 }
1288
1289                                 if (!slot.hasSpace(arbitrationData)) {
1290                                         // No space so cant do anything else with these data entries
1291                                         isFull = true;
1292                                         break;
1293                                 }
1294
1295                                 // Add to this current slot and add it to entries to delete
1296                                 slot.addEntry(arbitrationData);
1297                                 pendingSendArbitrationEntriesToDelete.add(arbitrationData);
1298                         }
1299
1300                         if (isFull) {
1301                                 break;
1302                         }
1303                 }
1304
1305                 if (pendingTransactionQueue.size() > 0) {
1306
1307                         Transaction transaction = pendingTransactionQueue.get(0);
1308
1309                         // Set the transaction sequence number if it has yet to be inserted into the block chain
1310                         // if ((!transaction.didSendAPartToServer() && !transaction.getServerFailure()) || (transaction.getSequenceNumber() == -1)) {
1311                         //      transaction.setSequenceNumber(slot.getSequenceNumber());
1312                         // }
1313
1314                         if ((!transaction.didSendAPartToServer()) || (transaction.getSequenceNumber() == -1)) {
1315                                 transaction.setSequenceNumber(slot.getSequenceNumber());
1316                         }
1317
1318
1319                         while (true) {
1320                                 TransactionPart part = transaction.getNextPartToSend();
1321
1322                                 if (part == null) {
1323                                         // Ran out of parts to send for this transaction so move on
1324                                         break;
1325                                 }
1326
1327                                 if (slot.hasSpace(part)) {
1328                                         slot.addEntry(part);
1329                                         List<Integer> partsSent = transactionPartsSent.get(transaction);
1330                                         if (partsSent == null) {
1331                                                 partsSent = new ArrayList<Integer>();
1332                                                 transactionPartsSent.put(transaction, partsSent);
1333                                         }
1334                                         partsSent.add(part.getPartNumber());
1335                                         transactionPartsSent.put(transaction, partsSent);
1336                                 } else {
1337                                         break;
1338                                 }
1339                         }
1340                 }
1341
1342                 // Fill the remainder of the slot with rescue data
1343                 doOptionalRescue(slot, seenLiveSlot, currentRescueSequenceNumber, resize);
1344
1345                 return new ThreeTuple<Boolean, Integer, Boolean>(false, newSize, inserted);
1346         }
1347
1348         private void doRejectedMessages(Slot s) {
1349                 if (! rejectedSlotList.isEmpty()) {
1350                         /* TODO: We should avoid generating a rejected message entry if
1351                          * there is already a sufficient entry in the queue (e.g.,
1352                          * equalsto value of true and same sequence number).  */
1353
1354                         long old_seqn = rejectedSlotList.firstElement();
1355                         if (rejectedSlotList.size() > REJECTED_THRESHOLD) {
1356                                 long new_seqn = rejectedSlotList.lastElement();
1357                                 RejectedMessage rm = new RejectedMessage(s, s.getSequenceNumber(), localMachineId, old_seqn, new_seqn, false);
1358                                 s.addEntry(rm);
1359                         } else {
1360                                 long prev_seqn = -1;
1361                                 int i = 0;
1362                                 /* Go through list of missing messages */
1363                                 for (; i < rejectedSlotList.size(); i++) {
1364                                         long curr_seqn = rejectedSlotList.get(i);
1365                                         Slot s_msg = buffer.getSlot(curr_seqn);
1366                                         if (s_msg != null)
1367                                                 break;
1368                                         prev_seqn = curr_seqn;
1369                                 }
1370                                 /* Generate rejected message entry for missing messages */
1371                                 if (prev_seqn != -1) {
1372                                         RejectedMessage rm = new RejectedMessage(s, s.getSequenceNumber(), localMachineId, old_seqn, prev_seqn, false);
1373                                         s.addEntry(rm);
1374                                 }
1375                                 /* Generate rejected message entries for present messages */
1376                                 for (; i < rejectedSlotList.size(); i++) {
1377                                         long curr_seqn = rejectedSlotList.get(i);
1378                                         Slot s_msg = buffer.getSlot(curr_seqn);
1379                                         long machineid = s_msg.getMachineID();
1380                                         RejectedMessage rm = new RejectedMessage(s, s.getSequenceNumber(), machineid, curr_seqn, curr_seqn, true);
1381                                         s.addEntry(rm);
1382                                 }
1383                         }
1384                 }
1385         }
1386
1387         private ThreeTuple<Boolean, Boolean, Long> doMandatoryResuce(Slot slot, boolean resize) {
1388                 long newestSequenceNumber = buffer.getNewestSeqNum();
1389                 long oldestSequenceNumber = buffer.getOldestSeqNum();
1390                 if (oldestLiveSlotSequenceNumver < oldestSequenceNumber) {
1391                         oldestLiveSlotSequenceNumver = oldestSequenceNumber;
1392                 }
1393
1394                 long currentSequenceNumber = oldestLiveSlotSequenceNumver;
1395                 boolean seenLiveSlot = false;
1396                 long firstIfFull = newestSequenceNumber + 1 - numberOfSlots;    // smallest seq number in the buffer if it is full
1397                 long threshold = firstIfFull + FREE_SLOTS;      // we want the buffer to be clear of live entries up to this point
1398
1399
1400                 // Mandatory Rescue
1401                 for (; currentSequenceNumber < threshold; currentSequenceNumber++) {
1402                         Slot previousSlot = buffer.getSlot(currentSequenceNumber);
1403                         // Push slot number forward
1404                         if (! seenLiveSlot) {
1405                                 oldestLiveSlotSequenceNumver = currentSequenceNumber;
1406                         }
1407
1408                         if (!previousSlot.isLive()) {
1409                                 continue;
1410                         }
1411
1412                         // We have seen a live slot
1413                         seenLiveSlot = true;
1414
1415                         // Get all the live entries for a slot
1416                         Vector<Entry> liveEntries = previousSlot.getLiveEntries(resize);
1417
1418                         // Iterate over all the live entries and try to rescue them
1419                         for (Entry liveEntry : liveEntries) {
1420                                 if (slot.hasSpace(liveEntry)) {
1421
1422                                         // Enough space to rescue the entry
1423                                         slot.addEntry(liveEntry);
1424                                 } else if (currentSequenceNumber == firstIfFull) {
1425                                         //if there's no space but the entry is about to fall off the queue
1426                                         System.out.println("B"); //?
1427                                         return new ThreeTuple<Boolean, Boolean, Long>(true, seenLiveSlot, currentSequenceNumber);
1428
1429                                 }
1430                         }
1431                 }
1432
1433                 // Did not resize
1434                 return new ThreeTuple<Boolean, Boolean, Long>(false, seenLiveSlot, currentSequenceNumber);
1435         }
1436
1437         private void  doOptionalRescue(Slot s, boolean seenliveslot, long seqn, boolean resize) {
1438                 /* now go through live entries from least to greatest sequence number until
1439                  * either all live slots added, or the slot doesn't have enough room
1440                  * for SKIP_THRESHOLD consecutive entries*/
1441                 int skipcount = 0;
1442                 long newestseqnum = buffer.getNewestSeqNum();
1443                 search:
1444                 for (; seqn <= newestseqnum; seqn++) {
1445                         Slot prevslot = buffer.getSlot(seqn);
1446                         //Push slot number forward
1447                         if (!seenliveslot)
1448                                 oldestLiveSlotSequenceNumver = seqn;
1449
1450                         if (!prevslot.isLive())
1451                                 continue;
1452                         seenliveslot = true;
1453                         Vector<Entry> liveentries = prevslot.getLiveEntries(resize);
1454                         for (Entry liveentry : liveentries) {
1455                                 if (s.hasSpace(liveentry))
1456                                         s.addEntry(liveentry);
1457                                 else {
1458                                         skipcount++;
1459                                         if (skipcount > SKIP_THRESHOLD)
1460                                                 break search;
1461                                 }
1462                         }
1463                 }
1464         }
1465
1466         /**
1467          * Checks for malicious activity and updates the local copy of the block chain.
1468          */
1469         private void validateAndUpdate(Slot[] newSlots, boolean acceptUpdatesToLocal) {
1470
1471                 // The cloud communication layer has checked slot HMACs already before decoding
1472                 if (newSlots.length == 0) {
1473                         return;
1474                 }
1475
1476                 // Make sure all slots are newer than the last largest slot this client has seen
1477                 long firstSeqNum = newSlots[0].getSequenceNumber();
1478                 if (firstSeqNum <= sequenceNumber) {
1479                         throw new Error("Server Error: Sent older slots!");
1480                 }
1481
1482                 // Create an object that can access both new slots and slots in our local chain
1483                 // without committing slots to our local chain
1484                 SlotIndexer indexer = new SlotIndexer(newSlots, buffer);
1485
1486                 // Check that the HMAC chain is not broken
1487                 checkHMACChain(indexer, newSlots);
1488
1489                 // Set to keep track of messages from clients
1490                 HashSet<Long> machineSet = new HashSet<Long>(lastMessageTable.keySet());
1491
1492                 // Process each slots data
1493                 for (Slot slot : newSlots) {
1494                         processSlot(indexer, slot, acceptUpdatesToLocal, machineSet);
1495
1496                         updateExpectedSize();
1497                 }
1498
1499                 // If there is a gap, check to see if the server sent us everything.
1500                 if (firstSeqNum != (sequenceNumber + 1)) {
1501
1502                         // Check the size of the slots that were sent down by the server.
1503                         // Can only check the size if there was a gap
1504                         checkNumSlots(newSlots.length);
1505
1506                         // Since there was a gap every machine must have pushed a slot or must have
1507                         // a last message message.  If not then the server is hiding slots
1508                         if (!machineSet.isEmpty()) {
1509                                 throw new Error("Missing record for machines: " + machineSet);
1510                         }
1511                 }
1512
1513                 // Update the size of our local block chain.
1514                 commitNewMaxSize();
1515
1516                 // Commit new to slots to the local block chain.
1517                 for (Slot slot : newSlots) {
1518
1519                         // Insert this slot into our local block chain copy.
1520                         buffer.putSlot(slot);
1521
1522                         // Keep track of how many slots are currently live (have live data in them).
1523                         liveSlotCount++;
1524                 }
1525
1526                 // Get the sequence number of the latest slot in the system
1527                 sequenceNumber = newSlots[newSlots.length - 1].getSequenceNumber();
1528
1529                 updateLiveStateFromServer();
1530
1531                 // No Need to remember after we pulled from the server
1532                 offlineTransactionsCommittedAndAtServer.clear();
1533
1534                 // This is invalidated now
1535                 hadPartialSendToServer = false;
1536         }
1537
1538         private void updateLiveStateFromServer() {
1539                 // Process the new transaction parts
1540                 processNewTransactionParts();
1541
1542                 // Do arbitration on new transactions that were received
1543                 arbitrateFromServer();
1544
1545                 // Update all the committed keys
1546                 boolean didCommitOrSpeculate = updateCommittedTable();
1547
1548                 // Delete the transactions that are now dead
1549                 updateLiveTransactionsAndStatus();
1550
1551                 // Do speculations
1552                 didCommitOrSpeculate |= updateSpeculativeTable(didCommitOrSpeculate);
1553                 updatePendingTransactionSpeculativeTable(didCommitOrSpeculate);
1554         }
1555
1556         private void updateLiveStateFromLocal() {
1557                 // Update all the committed keys
1558                 boolean didCommitOrSpeculate = updateCommittedTable();
1559
1560                 // Delete the transactions that are now dead
1561                 updateLiveTransactionsAndStatus();
1562
1563                 // Do speculations
1564                 didCommitOrSpeculate |= updateSpeculativeTable(didCommitOrSpeculate);
1565                 updatePendingTransactionSpeculativeTable(didCommitOrSpeculate);
1566         }
1567
1568         private void initExpectedSize(long firstSequenceNumber, long numberOfSlots) {
1569                 // if (didFindTableStatus) {
1570                 // return;
1571                 // }
1572                 long prevslots = firstSequenceNumber;
1573
1574
1575                 if (didFindTableStatus) {
1576                         // expectedsize = (prevslots < ((long) numberOfSlots)) ? (int) prevslots : expectedsize;
1577                         // System.out.println("Here2: " + expectedsize + "    " + numberOfSlots + "   " + prevslots);
1578
1579                 } else {
1580                         expectedsize = (prevslots < ((long) numberOfSlots)) ? (int) prevslots : numberOfSlots;
1581                         // System.out.println("Here: " + expectedsize);
1582                 }
1583
1584                 // System.out.println(numberOfSlots);
1585
1586                 didFindTableStatus = true;
1587                 currMaxSize = numberOfSlots;
1588         }
1589
1590         private void updateExpectedSize() {
1591                 expectedsize++;
1592
1593                 if (expectedsize > currMaxSize) {
1594                         expectedsize = currMaxSize;
1595                 }
1596         }
1597
1598
1599         /**
1600          * Check the size of the block chain to make sure there are enough slots sent back by the server.
1601          * This is only called when we have a gap between the slots that we have locally and the slots
1602          * sent by the server therefore in the slots sent by the server there will be at least 1 Table
1603          * status message
1604          */
1605         private void checkNumSlots(int numberOfSlots) {
1606                 if (numberOfSlots != expectedsize) {
1607                         throw new Error("Server Error: Server did not send all slots.  Expected: " + expectedsize + " Received:" + numberOfSlots);
1608                 }
1609         }
1610
1611         private void updateCurrMaxSize(int newmaxsize) {
1612                 currMaxSize = newmaxsize;
1613         }
1614
1615
1616         /**
1617          * Update the size of of the local buffer if it is needed.
1618          */
1619         private void commitNewMaxSize() {
1620                 didFindTableStatus = false;
1621
1622                 // Resize the local slot buffer
1623                 if (numberOfSlots != currMaxSize) {
1624                         buffer.resize((int)currMaxSize);
1625                 }
1626
1627                 // Change the number of local slots to the new size
1628                 numberOfSlots = (int)currMaxSize;
1629
1630
1631                 // Recalculate the resize threshold since the size of the local buffer has changed
1632                 setResizeThreshold();
1633         }
1634
1635         /**
1636          * Process the new transaction parts from this latest round of slots received from the server
1637          */
1638         private void processNewTransactionParts() {
1639
1640                 if (newTransactionParts.size() == 0) {
1641                         // Nothing new to process
1642                         return;
1643                 }
1644
1645                 // Iterate through all the machine Ids that we received new parts for
1646                 for (Long machineId : newTransactionParts.keySet()) {
1647                         Map<Pair<Long, Integer>, TransactionPart> parts = newTransactionParts.get(machineId);
1648
1649                         // Iterate through all the parts for that machine Id
1650                         for (Pair<Long, Integer> partId : parts.keySet()) {
1651                                 TransactionPart part = parts.get(partId);
1652
1653                                 Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(part.getArbitratorId());
1654                                 if ((lastTransactionNumber != null) && (lastTransactionNumber >= part.getSequenceNumber())) {
1655                                         // Set dead the transaction part
1656                                         part.setDead();
1657                                         continue;
1658                                 }
1659
1660                                 // Get the transaction object for that sequence number
1661                                 Transaction transaction = liveTransactionBySequenceNumberTable.get(part.getSequenceNumber());
1662
1663                                 if (transaction == null) {
1664                                         // This is a new transaction that we dont have so make a new one
1665                                         transaction = new Transaction();
1666
1667                                         // Insert this new transaction into the live tables
1668                                         liveTransactionBySequenceNumberTable.put(part.getSequenceNumber(), transaction);
1669                                         liveTransactionByTransactionIdTable.put(part.getTransactionId(), transaction);
1670                                 }
1671
1672                                 // Add that part to the transaction
1673                                 transaction.addPartDecode(part);
1674                         }
1675                 }
1676
1677                 // Clear all the new transaction parts in preparation for the next time the server sends slots
1678                 newTransactionParts.clear();
1679         }
1680
1681
1682         private long lastSeqNumArbOn = 0;
1683
1684         private void arbitrateFromServer() {
1685
1686                 if (liveTransactionBySequenceNumberTable.size() == 0) {
1687                         // Nothing to arbitrate on so move on
1688                         return;
1689                 }
1690
1691                 // Get the transaction sequence numbers and sort from oldest to newest
1692                 List<Long> transactionSequenceNumbers = new ArrayList<Long>(liveTransactionBySequenceNumberTable.keySet());
1693                 Collections.sort(transactionSequenceNumbers);
1694
1695                 // Collection of key value pairs that are
1696                 Map<IoTString, KeyValue> speculativeTableTmp = new HashMap<IoTString, KeyValue>();
1697
1698                 // The last transaction arbitrated on
1699                 long lastTransactionCommitted = -1;
1700                 Set<Abort> generatedAborts = new HashSet<Abort>();
1701
1702                 for (Long transactionSequenceNumber : transactionSequenceNumbers) {
1703                         Transaction transaction = liveTransactionBySequenceNumberTable.get(transactionSequenceNumber);
1704
1705
1706
1707                         // Check if this machine arbitrates for this transaction if not then we cant arbitrate this transaction
1708                         if (transaction.getArbitrator() != localMachineId) {
1709                                 continue;
1710                         }
1711
1712                         if (transactionSequenceNumber < lastSeqNumArbOn) {
1713                                 continue;
1714                         }
1715
1716                         if (offlineTransactionsCommittedAndAtServer.contains(transaction.getId())) {
1717                                 // We have seen this already locally so dont commit again
1718                                 continue;
1719                         }
1720
1721
1722                         if (!transaction.isComplete()) {
1723                                 // Will arbitrate in incorrect order if we continue so just break
1724                                 // Most likely this
1725                                 break;
1726                         }
1727
1728
1729                         // update the largest transaction seen by arbitrator from server
1730                         if (lastTransactionSeenFromMachineFromServer.get(transaction.getMachineId()) == null) {
1731                                 lastTransactionSeenFromMachineFromServer.put(transaction.getMachineId(), transaction.getClientLocalSequenceNumber());
1732                         } else {
1733                                 Long lastTransactionSeenFromMachine = lastTransactionSeenFromMachineFromServer.get(transaction.getMachineId());
1734                                 if (transaction.getClientLocalSequenceNumber() > lastTransactionSeenFromMachine) {
1735                                         lastTransactionSeenFromMachineFromServer.put(transaction.getMachineId(), transaction.getClientLocalSequenceNumber());
1736                                 }
1737                         }
1738
1739                         if (transaction.evaluateGuard(committedKeyValueTable, speculativeTableTmp, null)) {
1740                                 // Guard evaluated as true
1741
1742                                 // Update the local changes so we can make the commit
1743                                 for (KeyValue kv : transaction.getKeyValueUpdateSet()) {
1744                                         speculativeTableTmp.put(kv.getKey(), kv);
1745                                 }
1746
1747                                 // Update what the last transaction committed was for use in batch commit
1748                                 lastTransactionCommitted = transactionSequenceNumber;
1749                         } else {
1750                                 // Guard evaluated was false so create abort
1751
1752                                 // Create the abort
1753                                 Abort newAbort = new Abort(null,
1754                                                 transaction.getClientLocalSequenceNumber(),
1755                                                 transaction.getSequenceNumber(),
1756                                                 transaction.getMachineId(),
1757                                                 transaction.getArbitrator(),
1758                                                 localArbitrationSequenceNumber);
1759                                 localArbitrationSequenceNumber++;
1760
1761                                 generatedAborts.add(newAbort);
1762
1763                                 // Insert the abort so we can process
1764                                 processEntry(newAbort);
1765                         }
1766
1767                         lastSeqNumArbOn = transactionSequenceNumber;
1768
1769                         // liveTransactionBySequenceNumberTable.remove(transactionSequenceNumber);
1770                 }
1771
1772                 Commit newCommit = null;
1773
1774                 // If there is something to commit
1775                 if (speculativeTableTmp.size() != 0) {
1776
1777                         // Create the commit and increment the commit sequence number
1778                         newCommit = new Commit(localArbitrationSequenceNumber, localMachineId, lastTransactionCommitted);
1779                         localArbitrationSequenceNumber++;
1780
1781                         // Add all the new keys to the commit
1782                         for (KeyValue kv : speculativeTableTmp.values()) {
1783                                 newCommit.addKV(kv);
1784                         }
1785
1786                         // create the commit parts
1787                         newCommit.createCommitParts();
1788
1789                         // Append all the commit parts to the end of the pending queue waiting for sending to the server
1790
1791                         // Insert the commit so we can process it
1792                         for (CommitPart commitPart : newCommit.getParts().values()) {
1793                                 processEntry(commitPart);
1794                         }
1795                 }
1796
1797                 if ((newCommit != null) || (generatedAborts.size() > 0)) {
1798                         ArbitrationRound arbitrationRound = new ArbitrationRound(newCommit, generatedAborts);
1799                         pendingSendArbitrationRounds.add(arbitrationRound);
1800
1801                         if (compactArbitrationData()) {
1802                                 ArbitrationRound newArbitrationRound = pendingSendArbitrationRounds.get(pendingSendArbitrationRounds.size() - 1);
1803                                 if (newArbitrationRound.getCommit() != null) {
1804                                         for (CommitPart commitPart : newArbitrationRound.getCommit().getParts().values()) {
1805                                                 processEntry(commitPart);
1806                                         }
1807                                 }
1808                         }
1809                 }
1810         }
1811
1812         private Pair<Boolean, Boolean> arbitrateOnLocalTransaction(Transaction transaction) {
1813
1814                 // Check if this machine arbitrates for this transaction if not then we cant arbitrate this transaction
1815                 if (transaction.getArbitrator() != localMachineId) {
1816                         return new Pair<Boolean, Boolean>(false, false);
1817                 }
1818
1819                 if (!transaction.isComplete()) {
1820                         // Will arbitrate in incorrect order if we continue so just break
1821                         // Most likely this
1822                         return new Pair<Boolean, Boolean>(false, false);
1823                 }
1824
1825                 if (transaction.getMachineId() != localMachineId) {
1826                         // dont do this check for local transactions
1827                         if (lastTransactionSeenFromMachineFromServer.get(transaction.getMachineId()) != null) {
1828                                 if (lastTransactionSeenFromMachineFromServer.get(transaction.getMachineId()) > transaction.getClientLocalSequenceNumber()) {
1829                                         // We've have already seen this from the server
1830                                         return new Pair<Boolean, Boolean>(false, false);
1831                                 }
1832                         }
1833                 }
1834
1835                 if (transaction.evaluateGuard(committedKeyValueTable, null, null)) {
1836                         // Guard evaluated as true
1837
1838                         // Create the commit and increment the commit sequence number
1839                         Commit newCommit = new Commit(localArbitrationSequenceNumber, localMachineId, -1);
1840                         localArbitrationSequenceNumber++;
1841
1842                         // Update the local changes so we can make the commit
1843                         for (KeyValue kv : transaction.getKeyValueUpdateSet()) {
1844                                 newCommit.addKV(kv);
1845                         }
1846
1847                         // create the commit parts
1848                         newCommit.createCommitParts();
1849
1850                         // Append all the commit parts to the end of the pending queue waiting for sending to the server
1851                         ArbitrationRound arbitrationRound = new ArbitrationRound(newCommit, new HashSet<Abort>());
1852                         pendingSendArbitrationRounds.add(arbitrationRound);
1853
1854                         if (compactArbitrationData()) {
1855                                 ArbitrationRound newArbitrationRound = pendingSendArbitrationRounds.get(pendingSendArbitrationRounds.size() - 1);
1856                                 for (CommitPart commitPart : newArbitrationRound.getCommit().getParts().values()) {
1857                                         processEntry(commitPart);
1858                                 }
1859                         } else {
1860                                 // Insert the commit so we can process it
1861                                 for (CommitPart commitPart : newCommit.getParts().values()) {
1862                                         processEntry(commitPart);
1863                                 }
1864                         }
1865
1866                         if (transaction.getMachineId() == localMachineId) {
1867                                 TransactionStatus status = transaction.getTransactionStatus();
1868                                 if (status != null) {
1869                                         status.setStatus(TransactionStatus.StatusCommitted);
1870                                 }
1871                         }
1872
1873                         updateLiveStateFromLocal();
1874                         return new Pair<Boolean, Boolean>(true, true);
1875                 } else {
1876
1877                         if (transaction.getMachineId() == localMachineId) {
1878                                 // For locally created messages update the status
1879
1880                                 // Guard evaluated was false so create abort
1881                                 TransactionStatus status = transaction.getTransactionStatus();
1882                                 if (status != null) {
1883                                         status.setStatus(TransactionStatus.StatusAborted);
1884                                 }
1885                         } else {
1886                                 Set addAbortSet = new HashSet<Abort>();
1887
1888
1889                                 // Create the abort
1890                                 Abort newAbort = new Abort(null,
1891                                                 transaction.getClientLocalSequenceNumber(),
1892                                                 -1,
1893                                                 transaction.getMachineId(),
1894                                                 transaction.getArbitrator(),
1895                                                 localArbitrationSequenceNumber);
1896                                 localArbitrationSequenceNumber++;
1897
1898                                 addAbortSet.add(newAbort);
1899
1900
1901                                 // Append all the commit parts to the end of the pending queue waiting for sending to the server
1902                                 ArbitrationRound arbitrationRound = new ArbitrationRound(null, addAbortSet);
1903                                 pendingSendArbitrationRounds.add(arbitrationRound);
1904
1905                                 if (compactArbitrationData()) {
1906                                         ArbitrationRound newArbitrationRound = pendingSendArbitrationRounds.get(pendingSendArbitrationRounds.size() - 1);
1907                                         for (CommitPart commitPart : newArbitrationRound.getCommit().getParts().values()) {
1908                                                 processEntry(commitPart);
1909                                         }
1910                                 }
1911                         }
1912
1913                         updateLiveStateFromLocal();
1914                         return new Pair<Boolean, Boolean>(true, false);
1915                 }
1916         }
1917
1918         /**
1919          * 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
1920          */
1921         private boolean compactArbitrationData() {
1922
1923                 if (pendingSendArbitrationRounds.size() < 2) {
1924                         // Nothing to compact so do nothing
1925                         return false;
1926                 }
1927
1928                 ArbitrationRound lastRound = pendingSendArbitrationRounds.get(pendingSendArbitrationRounds.size() - 1);
1929                 if (lastRound.didSendPart()) {
1930                         return false;
1931                 }
1932
1933                 boolean hadCommit = (lastRound.getCommit() == null);
1934                 boolean gotNewCommit = false;
1935
1936                 int numberToDelete = 1;
1937                 while (numberToDelete < pendingSendArbitrationRounds.size()) {
1938                         ArbitrationRound round = pendingSendArbitrationRounds.get(pendingSendArbitrationRounds.size() - numberToDelete - 1);
1939
1940                         if (round.isFull() || round.didSendPart()) {
1941                                 // Stop since there is a part that cannot be compacted and we need to compact in order
1942                                 break;
1943                         }
1944
1945                         if (round.getCommit() == null) {
1946
1947                                 // Try compacting aborts only
1948                                 int newSize = round.getCurrentSize() + lastRound.getAbortsCount();
1949                                 if (newSize > ArbitrationRound.MAX_PARTS) {
1950                                         // Cant compact since it would be too large
1951                                         break;
1952                                 }
1953                                 lastRound.addAborts(round.getAborts());
1954                         } else {
1955
1956                                 // Create a new larger commit
1957                                 Commit newCommit = Commit.merge(lastRound.getCommit(), round.getCommit(), localArbitrationSequenceNumber);
1958                                 localArbitrationSequenceNumber++;
1959
1960                                 // Create the commit parts so that we can count them
1961                                 newCommit.createCommitParts();
1962
1963                                 // Calculate the new size of the parts
1964                                 int newSize = newCommit.getNumberOfParts();
1965                                 newSize += lastRound.getAbortsCount();
1966                                 newSize += round.getAbortsCount();
1967
1968                                 if (newSize > ArbitrationRound.MAX_PARTS) {
1969                                         // Cant compact since it would be too large
1970                                         break;
1971                                 }
1972
1973                                 // Set the new compacted part
1974                                 lastRound.setCommit(newCommit);
1975                                 lastRound.addAborts(round.getAborts());
1976                                 gotNewCommit = true;
1977                         }
1978
1979                         numberToDelete++;
1980                 }
1981
1982                 if (numberToDelete != 1) {
1983                         // If there is a compaction
1984
1985                         // Delete the previous pieces that are now in the new compacted piece
1986                         if (numberToDelete == pendingSendArbitrationRounds.size()) {
1987                                 pendingSendArbitrationRounds.clear();
1988                         } else {
1989                                 for (int i = 0; i < numberToDelete; i++) {
1990                                         pendingSendArbitrationRounds.remove(pendingSendArbitrationRounds.size() - 1);
1991                                 }
1992                         }
1993
1994                         // Add the new compacted into the pending to send list
1995                         pendingSendArbitrationRounds.add(lastRound);
1996
1997                         // Should reinsert into the commit processor
1998                         if (hadCommit && gotNewCommit) {
1999                                 return true;
2000                         }
2001                 }
2002
2003                 return false;
2004         }
2005         // private boolean compactArbitrationData() {
2006         //      return false;
2007         // }
2008
2009         /**
2010          * Update all the commits and the committed tables, sets dead the dead transactions
2011          */
2012         private boolean updateCommittedTable() {
2013
2014                 if (newCommitParts.size() == 0) {
2015                         // Nothing new to process
2016                         return false;
2017                 }
2018
2019                 // Iterate through all the machine Ids that we received new parts for
2020                 for (Long machineId : newCommitParts.keySet()) {
2021                         Map<Pair<Long, Integer>, CommitPart> parts = newCommitParts.get(machineId);
2022
2023                         // Iterate through all the parts for that machine Id
2024                         for (Pair<Long, Integer> partId : parts.keySet()) {
2025                                 CommitPart part = parts.get(partId);
2026
2027                                 // Get the transaction object for that sequence number
2028                                 Map<Long, Commit> commitForClientTable = liveCommitsTable.get(part.getMachineId());
2029
2030                                 if (commitForClientTable == null) {
2031                                         // This is the first commit from this device
2032                                         commitForClientTable = new HashMap<Long, Commit>();
2033                                         liveCommitsTable.put(part.getMachineId(), commitForClientTable);
2034                                 }
2035
2036                                 Commit commit = commitForClientTable.get(part.getSequenceNumber());
2037
2038                                 if (commit == null) {
2039                                         // This is a new commit that we dont have so make a new one
2040                                         commit = new Commit();
2041
2042                                         // Insert this new commit into the live tables
2043                                         commitForClientTable.put(part.getSequenceNumber(), commit);
2044                                 }
2045
2046                                 // Add that part to the commit
2047                                 commit.addPartDecode(part);
2048                         }
2049                 }
2050
2051                 // Clear all the new commits parts in preparation for the next time the server sends slots
2052                 newCommitParts.clear();
2053
2054                 // If we process a new commit keep track of it for future use
2055                 boolean didProcessANewCommit = false;
2056
2057                 // Process the commits one by one
2058                 for (Long arbitratorId : liveCommitsTable.keySet()) {
2059
2060                         // Get all the commits for a specific arbitrator
2061                         Map<Long, Commit> commitForClientTable = liveCommitsTable.get(arbitratorId);
2062
2063                         // Sort the commits in order
2064                         List<Long> commitSequenceNumbers = new ArrayList<Long>(commitForClientTable.keySet());
2065                         Collections.sort(commitSequenceNumbers);
2066
2067                         // Get the last commit seen from this arbitrator
2068                         long lastCommitSeenSequenceNumber = -1;
2069                         if (lastCommitSeenSequenceNumberByArbitratorTable.get(arbitratorId) != null) {
2070                                 lastCommitSeenSequenceNumber = lastCommitSeenSequenceNumberByArbitratorTable.get(arbitratorId);
2071                         }
2072
2073                         // Go through each new commit one by one
2074                         for (int i = 0; i < commitSequenceNumbers.size(); i++) {
2075                                 Long commitSequenceNumber = commitSequenceNumbers.get(i);
2076                                 Commit commit = commitForClientTable.get(commitSequenceNumber);
2077
2078                                 // Special processing if a commit is not complete
2079                                 if (!commit.isComplete()) {
2080                                         if (i == (commitSequenceNumbers.size() - 1)) {
2081                                                 // 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
2082                                                 break;
2083                                         } else {
2084                                                 // This is a commit that was already dead but parts of it are still in the block chain (not flushed out yet).
2085                                                 // Delete it and move on
2086                                                 commit.setDead();
2087                                                 commitForClientTable.remove(commit.getSequenceNumber());
2088                                                 continue;
2089                                         }
2090                                 }
2091
2092                                 // Update the last transaction that was updated if we can
2093                                 if (commit.getTransactionSequenceNumber() != -1) {
2094                                         Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(commit.getMachineId());
2095
2096                                         // Update the last transaction sequence number that the arbitrator arbitrated on
2097                                         if ((lastTransactionNumber == null) || (lastTransactionNumber < commit.getTransactionSequenceNumber())) {
2098                                                 lastArbitratedTransactionNumberByArbitratorTable.put(commit.getMachineId(), commit.getTransactionSequenceNumber());
2099                                         }
2100                                 }
2101
2102                                 // Update the last arbitration data that we have seen so far
2103                                 if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(commit.getMachineId()) != null) {
2104
2105                                         long lastArbitrationSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(commit.getMachineId());
2106                                         if (commit.getSequenceNumber() > lastArbitrationSequenceNumber) {
2107                                                 // Is larger
2108                                                 lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.put(commit.getMachineId(), commit.getSequenceNumber());
2109                                         }
2110                                 } else {
2111                                         // Never seen any data from this arbitrator so record the first one
2112                                         lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.put(commit.getMachineId(), commit.getSequenceNumber());
2113                                 }
2114
2115                                 // We have already seen this commit before so need to do the full processing on this commit
2116                                 if (commit.getSequenceNumber() <= lastCommitSeenSequenceNumber) {
2117
2118                                         // Update the last transaction that was updated if we can
2119                                         if (commit.getTransactionSequenceNumber() != -1) {
2120                                                 Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(commit.getMachineId());
2121
2122                                                 // Update the last transaction sequence number that the arbitrator arbitrated on
2123                                                 if ((lastTransactionNumber == null) || (lastTransactionNumber < commit.getTransactionSequenceNumber())) {
2124                                                         lastArbitratedTransactionNumberByArbitratorTable.put(commit.getMachineId(), commit.getTransactionSequenceNumber());
2125                                                 }
2126                                         }
2127
2128                                         continue;
2129                                 }
2130
2131                                 // If we got here then this is a brand new commit and needs full processing
2132
2133                                 // Get what commits should be edited, these are the commits that have live values for their keys
2134                                 Set<Commit> commitsToEdit = new HashSet<Commit>();
2135                                 for (KeyValue kv : commit.getKeyValueUpdateSet()) {
2136                                         commitsToEdit.add(liveCommitsByKeyTable.get(kv.getKey()));
2137                                 }
2138                                 commitsToEdit.remove(null); // remove null since it could be in this set
2139
2140                                 // Update each previous commit that needs to be updated
2141                                 for (Commit previousCommit : commitsToEdit) {
2142
2143                                         // Only bother with live commits (TODO: Maybe remove this check)
2144                                         if (previousCommit.isLive()) {
2145
2146                                                 // Update which keys in the old commits are still live
2147                                                 for (KeyValue kv : commit.getKeyValueUpdateSet()) {
2148                                                         previousCommit.invalidateKey(kv.getKey());
2149                                                 }
2150
2151                                                 // if the commit is now dead then remove it
2152                                                 if (!previousCommit.isLive()) {
2153                                                         commitForClientTable.remove(previousCommit);
2154                                                 }
2155                                         }
2156                                 }
2157
2158                                 // Update the last seen sequence number from this arbitrator
2159                                 if (lastCommitSeenSequenceNumberByArbitratorTable.get(commit.getMachineId()) != null) {
2160                                         if (commit.getSequenceNumber() > lastCommitSeenSequenceNumberByArbitratorTable.get(commit.getMachineId())) {
2161                                                 lastCommitSeenSequenceNumberByArbitratorTable.put(commit.getMachineId(), commit.getSequenceNumber());
2162                                         }
2163                                 } else {
2164                                         lastCommitSeenSequenceNumberByArbitratorTable.put(commit.getMachineId(), commit.getSequenceNumber());
2165                                 }
2166
2167                                 // We processed a new commit that we havent seen before
2168                                 didProcessANewCommit = true;
2169
2170                                 // Update the committed table of keys and which commit is using which key
2171                                 for (KeyValue kv : commit.getKeyValueUpdateSet()) {
2172                                         committedKeyValueTable.put(kv.getKey(), kv);
2173                                         liveCommitsByKeyTable.put(kv.getKey(), commit);
2174                                 }
2175                         }
2176                 }
2177
2178                 return didProcessANewCommit;
2179         }
2180
2181         /**
2182          * Create the speculative table from transactions that are still live and have come from the cloud
2183          */
2184         private boolean updateSpeculativeTable(boolean didProcessNewCommits) {
2185                 if (liveTransactionBySequenceNumberTable.keySet().size() == 0) {
2186                         // There is nothing to speculate on
2187                         return false;
2188                 }
2189
2190                 // Create a list of the transaction sequence numbers and sort them from oldest to newest
2191                 List<Long> transactionSequenceNumbersSorted = new ArrayList<Long>(liveTransactionBySequenceNumberTable.keySet());
2192                 Collections.sort(transactionSequenceNumbersSorted);
2193
2194                 boolean hasGapInTransactionSequenceNumbers = transactionSequenceNumbersSorted.get(0) != oldestTransactionSequenceNumberSpeculatedOn;
2195
2196
2197                 if (hasGapInTransactionSequenceNumbers || didProcessNewCommits) {
2198                         // If there is a gap in the transaction sequence numbers then there was a commit or an abort of a transaction
2199                         // OR there was a new commit (Could be from offline commit) so a redo the speculation from scratch
2200
2201                         // Start from scratch
2202                         speculatedKeyValueTable.clear();
2203                         lastTransactionSequenceNumberSpeculatedOn = -1;
2204                         oldestTransactionSequenceNumberSpeculatedOn = -1;
2205
2206                 }
2207
2208                 // Remember the front of the transaction list
2209                 oldestTransactionSequenceNumberSpeculatedOn = transactionSequenceNumbersSorted.get(0);
2210
2211                 // Find where to start arbitration from
2212                 int startIndex = transactionSequenceNumbersSorted.indexOf(lastTransactionSequenceNumberSpeculatedOn) + 1;
2213
2214                 if (startIndex >= transactionSequenceNumbersSorted.size()) {
2215                         // Make sure we are not out of bounds
2216                         return false; // did not speculate
2217                 }
2218
2219                 Set<Long> incompleteTransactionArbitrator = new HashSet<Long>();
2220                 boolean didSkip = true;
2221
2222                 for (int i = startIndex; i < transactionSequenceNumbersSorted.size(); i++) {
2223                         long transactionSequenceNumber = transactionSequenceNumbersSorted.get(i);
2224                         Transaction transaction = liveTransactionBySequenceNumberTable.get(transactionSequenceNumber);
2225
2226                         if (!transaction.isComplete()) {
2227                                 // If there is an incomplete transaction then there is nothing we can do
2228                                 // add this transactions arbitrator to the list of arbitrators we should ignore
2229                                 incompleteTransactionArbitrator.add(transaction.getArbitrator());
2230                                 didSkip = true;
2231                                 continue;
2232                         }
2233
2234                         if (incompleteTransactionArbitrator.contains(transaction.getArbitrator())) {
2235                                 continue;
2236                         }
2237
2238                         lastTransactionSequenceNumberSpeculatedOn = transactionSequenceNumber;
2239
2240                         if (transaction.evaluateGuard(committedKeyValueTable, speculatedKeyValueTable, null)) {
2241                                 // Guard evaluated to true so update the speculative table
2242                                 for (KeyValue kv : transaction.getKeyValueUpdateSet()) {
2243                                         speculatedKeyValueTable.put(kv.getKey(), kv);
2244                                 }
2245                         }
2246                 }
2247
2248                 if (didSkip) {
2249                         // Since there was a skip we need to redo the speculation next time around
2250                         lastTransactionSequenceNumberSpeculatedOn = -1;
2251                         oldestTransactionSequenceNumberSpeculatedOn = -1;
2252                 }
2253
2254                 // We did some speculation
2255                 return true;
2256         }
2257
2258         /**
2259          * Create the pending transaction speculative table from transactions that are still in the pending transaction buffer
2260          */
2261         private void updatePendingTransactionSpeculativeTable(boolean didProcessNewCommitsOrSpeculate) {
2262                 if (pendingTransactionQueue.size() == 0) {
2263                         // There is nothing to speculate on
2264                         return;
2265                 }
2266
2267
2268                 if (didProcessNewCommitsOrSpeculate || (firstPendingTransaction != pendingTransactionQueue.get(0))) {
2269                         // need to reset on the pending speculation
2270                         lastPendingTransactionSpeculatedOn = null;
2271                         firstPendingTransaction = pendingTransactionQueue.get(0);
2272                         pendingTransactionSpeculatedKeyValueTable.clear();
2273                 }
2274
2275                 // Find where to start arbitration from
2276                 int startIndex = pendingTransactionQueue.indexOf(firstPendingTransaction) + 1;
2277
2278                 if (startIndex >= pendingTransactionQueue.size()) {
2279                         // Make sure we are not out of bounds
2280                         return;
2281                 }
2282
2283                 for (int i = startIndex; i < pendingTransactionQueue.size(); i++) {
2284                         Transaction transaction = pendingTransactionQueue.get(i);
2285
2286                         lastPendingTransactionSpeculatedOn = transaction;
2287
2288                         if (transaction.evaluateGuard(committedKeyValueTable, speculatedKeyValueTable, pendingTransactionSpeculatedKeyValueTable)) {
2289                                 // Guard evaluated to true so update the speculative table
2290                                 for (KeyValue kv : transaction.getKeyValueUpdateSet()) {
2291                                         pendingTransactionSpeculatedKeyValueTable.put(kv.getKey(), kv);
2292                                 }
2293                         }
2294                 }
2295         }
2296
2297         /**
2298          * Set dead and remove from the live transaction tables the transactions that are dead
2299          */
2300         private void updateLiveTransactionsAndStatus() {
2301
2302                 // Go through each of the transactions
2303                 for (Iterator<Map.Entry<Long, Transaction>> iter = liveTransactionBySequenceNumberTable.entrySet().iterator(); iter.hasNext();) {
2304                         Transaction transaction = iter.next().getValue();
2305
2306                         // Check if the transaction is dead
2307                         Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(transaction.getArbitrator());
2308                         if ((lastTransactionNumber != null) && (lastTransactionNumber >= transaction.getSequenceNumber())) {
2309
2310                                 // Set dead the transaction
2311                                 transaction.setDead();
2312
2313                                 // Remove the transaction from the live table
2314                                 iter.remove();
2315                                 liveTransactionByTransactionIdTable.remove(transaction.getId());
2316                         }
2317                 }
2318
2319                 // Go through each of the transactions
2320                 for (Iterator<Map.Entry<Long, TransactionStatus>> iter = outstandingTransactionStatus.entrySet().iterator(); iter.hasNext();) {
2321                         TransactionStatus status = iter.next().getValue();
2322
2323                         // Check if the transaction is dead
2324                         Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(status.getTransactionArbitrator());
2325                         if ((lastTransactionNumber != null) && (lastTransactionNumber >= status.getTransactionSequenceNumber())) {
2326
2327                                 // Set committed
2328                                 status.setStatus(TransactionStatus.StatusCommitted);
2329
2330                                 // Remove
2331                                 iter.remove();
2332                         }
2333                 }
2334         }
2335
2336         /**
2337          * Process this slot, entry by entry.  Also update the latest message sent by slot
2338          */
2339         private void processSlot(SlotIndexer indexer, Slot slot, boolean acceptUpdatesToLocal, HashSet<Long> machineSet) {
2340
2341                 // Update the last message seen
2342                 updateLastMessage(slot.getMachineID(), slot.getSequenceNumber(), slot, acceptUpdatesToLocal, machineSet);
2343
2344                 // Process each entry in the slot
2345                 for (Entry entry : slot.getEntries()) {
2346                         switch (entry.getType()) {
2347
2348                                 case Entry.TypeCommitPart:
2349                                         processEntry((CommitPart)entry);
2350                                         break;
2351
2352                                 case Entry.TypeAbort:
2353                                         processEntry((Abort)entry);
2354                                         break;
2355
2356                                 case Entry.TypeTransactionPart:
2357                                         processEntry((TransactionPart)entry);
2358                                         break;
2359
2360                                 case Entry.TypeNewKey:
2361                                         processEntry((NewKey)entry);
2362                                         break;
2363
2364                                 case Entry.TypeLastMessage:
2365                                         processEntry((LastMessage)entry, machineSet);
2366                                         break;
2367
2368                                 case Entry.TypeRejectedMessage:
2369                                         processEntry((RejectedMessage)entry, indexer);
2370                                         break;
2371
2372                                 case Entry.TypeTableStatus:
2373                                         processEntry((TableStatus)entry, slot.getSequenceNumber());
2374                                         break;
2375
2376                                 default:
2377                                         throw new Error("Unrecognized type: " + entry.getType());
2378                         }
2379                 }
2380         }
2381
2382         /**
2383          * Update the last message that was sent for a machine Id
2384          */
2385         private void processEntry(LastMessage entry, HashSet<Long> machineSet) {
2386                 // Update what the last message received by a machine was
2387                 updateLastMessage(entry.getMachineID(), entry.getSequenceNumber(), entry, false, machineSet);
2388         }
2389
2390         /**
2391          * Add the new key to the arbitrators table and update the set of live new keys (in case of a rescued new key message)
2392          */
2393         private void processEntry(NewKey entry) {
2394
2395                 // Update the arbitrator table with the new key information
2396                 arbitratorTable.put(entry.getKey(), entry.getMachineID());
2397
2398                 // Update what the latest live new key is
2399                 NewKey oldNewKey = liveNewKeyTable.put(entry.getKey(), entry);
2400                 if (oldNewKey != null) {