X-Git-Url: http://plrg.eecs.uci.edu/git/?p=iotcloud.git;a=blobdiff_plain;f=version2%2Fsrc%2FC%2FTable.cc;h=531337cc5336f1c69dac6aed68018b2899355c98;hp=07923d58fc2ca67852265a3928340e58e713791f;hb=b7ed1849727b50e226f3b9d1c432d3071d739368;hpb=c3666021ffae881f13e3c293ab427c292ccbfd02 diff --git a/version2/src/C/Table.cc b/version2/src/C/Table.cc index 07923d5..531337c 100644 --- a/version2/src/C/Table.cc +++ b/version2/src/C/Table.cc @@ -1,2735 +1,2871 @@ +#include "Table.h" +#include "CloudComm.h" +#include "SlotBuffer.h" +#include "NewKey.h" +#include "Slot.h" +#include "KeyValue.h" +#include "Error.h" +#include "PendingTransaction.h" +#include "TableStatus.h" +#include "TransactionStatus.h" +#include "Transaction.h" +#include "LastMessage.h" +#include "SecureRandom.h" +#include "ByteBuffer.h" +#include "Abort.h" +#include "CommitPart.h" +#include "ArbitrationRound.h" +#include "TransactionPart.h" +#include "Commit.h" +#include "RejectedMessage.h" +#include "SlotIndexer.h" +#include + +int compareInt64(const void *a, const void *b) { + const int64_t *pa = (const int64_t *) a; + const int64_t *pb = (const int64_t *) b; + if (*pa < *pb) + return -1; + else if (*pa > *pb) + return 1; + else + return 0; +} +Table::Table(IoTString *baseurl, IoTString *password, int64_t _localMachineId, int listeningPort) : + buffer(NULL), + cloud(new CloudComm(this, baseurl, password, listeningPort)), + random(NULL), + liveTableStatus(NULL), + pendingTransactionBuilder(NULL), + lastPendingTransactionSpeculatedOn(NULL), + firstPendingTransaction(NULL), + numberOfSlots(0), + bufferResizeThreshold(0), + liveSlotCount(0), + oldestLiveSlotSequenceNumver(1), + localMachineId(_localMachineId), + sequenceNumber(0), + localSequenceNumber(0), + localTransactionSequenceNumber(0), + lastTransactionSequenceNumberSpeculatedOn(0), + oldestTransactionSequenceNumberSpeculatedOn(0), + localArbitrationSequenceNumber(0), + hadPartialSendToServer(false), + attemptedToSendToServer(false), + expectedsize(0), + didFindTableStatus(false), + currMaxSize(0), + lastSlotAttemptedToSend(NULL), + lastIsNewKey(false), + lastNewSize(0), + lastTransactionPartsSent(NULL), + lastPendingSendArbitrationEntriesToDelete(NULL), + lastNewKey(NULL), + committedKeyValueTable(NULL), + speculatedKeyValueTable(NULL), + pendingTransactionSpeculatedKeyValueTable(NULL), + liveNewKeyTable(NULL), + lastMessageTable(NULL), + rejectedMessageWatchVectorTable(NULL), + arbitratorTable(NULL), + liveAbortTable(NULL), + newTransactionParts(NULL), + newCommitParts(NULL), + lastArbitratedTransactionNumberByArbitratorTable(NULL), + liveTransactionBySequenceNumberTable(NULL), + liveTransactionByTransactionIdTable(NULL), + liveCommitsTable(NULL), + liveCommitsByKeyTable(NULL), + lastCommitSeenSequenceNumberByArbitratorTable(NULL), + rejectedSlotVector(NULL), + pendingTransactionQueue(NULL), + pendingSendArbitrationRounds(NULL), + pendingSendArbitrationEntriesToDelete(NULL), + transactionPartsSent(NULL), + outstandingTransactionStatus(NULL), + liveAbortsGeneratedByLocal(NULL), + offlineTransactionsCommittedAndAtServer(NULL), + localCommunicationTable(NULL), + lastTransactionSeenFromMachineFromServer(NULL), + lastArbitrationDataLocalSequenceNumberSeenFromArbitrator(NULL), + lastInsertedNewKey(false), + lastSeqNumArbOn(0) +{ + init(); +} -/** - * IoTTable data structure. Provides client interface. - * @author Brian Demsky - * @version 1.0 - */ - -final class Table { - - /* Constants */ - static final int FREE_SLOTS = 2; // Number of slots that should be kept free // 10 - static final int SKIP_THRESHOLD = 10; - static final double RESIZE_MULTIPLE = 1.2; - static final double RESIZE_THRESHOLD = 0.75; - static final int REJECTED_THRESHOLD = 5; - - /* Helper Objects */ - SlotBuffer buffer = NULL; - CloudComm cloud = NULL; - Random random = NULL; - TableStatus liveTableStatus = NULL; - PendingTransaction pendingTransactionBuilder = NULL; // Pending Transaction used in building a Pending Transaction - Transaction lastPendingTransactionSpeculatedOn = NULL; // Last transaction that was speculated on from the pending transaction - Transaction firstPendingTransaction = NULL; // first transaction in the pending transaction list - - /* Variables */ - int numberOfSlots = 0; // Number of slots stored in buffer - int bufferResizeThreshold = 0; // Threshold on the number of live slots before a resize is needed - int64_t liveSlotCount = 0; // Number of currently live slots - int64_t oldestLiveSlotSequenceNumver = 0; // Smallest sequence number of the slot with a live entry - int64_t localMachineId = 0; // Machine ID of this client device - int64_t sequenceNumber = 0; // Largest sequence number a client has received - int64_t localSequenceNumber = 0; - - // int smallestTableStatusSeen = -1; // Smallest Table Status that was seen in the latest slots sent from the server - // int largestTableStatusSeen = -1; // Largest Table Status that was seen in the latest slots sent from the server - int64_t localTransactionSequenceNumber = 0; // Local sequence number counter for transactions - int64_t lastTransactionSequenceNumberSpeculatedOn = -1; // the last transaction that was speculated on - int64_t oldestTransactionSequenceNumberSpeculatedOn = -1; // the oldest transaction that was speculated on - int64_t localArbitrationSequenceNumber = 0; - bool hadPartialSendToServer = false; - bool attemptedToSendToServer = false; - int64_t expectedsize; - bool didFindTableStatus = false; - int64_t currMaxSize = 0; - - Slot lastSlotAttemptedToSend = NULL; - bool lastIsNewKey = false; - int lastNewSize = 0; - Map> lastTransactionPartsSent = NULL; - List lastPendingSendArbitrationEntriesToDelete = NULL; - NewKey lastNewKey = NULL; - - - /* Data Structures */ - Map committedKeyValueTable = NULL; // Table of committed key value pairs - Map speculatedKeyValueTable = NULL; // Table of speculated key value pairs, if there is a speculative value - Map pendingTransactionSpeculatedKeyValueTable = NULL; // Table of speculated key value pairs, if there is a speculative value from the pending transactions - Map liveNewKeyTable = NULL; // Table of live new keys - HashMap> lastMessageTable = NULL; // Last message sent by a client machine id -> (Seq Num, Slot or LastMessage); - HashMap> rejectedMessageWatchListTable = NULL; // Table of machine Ids and the set of rejected messages they have not seen yet - Map arbitratorTable = NULL; // Table of keys and their arbitrators - Map, Abort> liveAbortTable = NULL; // Table live abort messages - Map, TransactionPart>> newTransactionParts = NULL; // transaction parts that are seen in this latest round of slots from the server - Map, CommitPart>> newCommitParts = NULL; // commit parts that are seen in this latest round of slots from the server - Map lastArbitratedTransactionNumberByArbitratorTable = NULL; // Last transaction sequence number that an arbitrator arbitrated on - Map liveTransactionBySequenceNumberTable = NULL; // live transaction grouped by the sequence number - Map, Transaction> liveTransactionByTransactionIdTable = NULL; // live transaction grouped by the transaction ID - Map> liveCommitsTable = NULL; - Map liveCommitsByKeyTable = NULL; - Map lastCommitSeenSequenceNumberByArbitratorTable = NULL; - Vector rejectedSlotList = NULL; // List of rejected slots that have yet to be sent to the server - List pendingTransactionQueue = NULL; - List pendingSendArbitrationRounds = NULL; - List pendingSendArbitrationEntriesToDelete = NULL; - Map> transactionPartsSent = NULL; - Map outstandingTransactionStatus = NULL; - Map liveAbortsGeneratedByLocal = NULL; - Set> offlineTransactionsCommittedAndAtServer = NULL; - Map> localCommunicationTable = NULL; - Map lastTransactionSeenFromMachineFromServer = NULL; - Map lastArbitrationDataLocalSequenceNumberSeenFromArbitrator = NULL; - - - Table(String baseurl, String password, int64_t _localMachineId, int listeningPort) { - localMachineId = _localMachineId; - cloud = new CloudComm(this, baseurl, password, listeningPort); - - init(); - } - - Table(CloudComm _cloud, int64_t _localMachineId) { - localMachineId = _localMachineId; - cloud = _cloud; - - init(); - } - - /** - * Init all the stuff needed for for table usage - */ - void init() { - - // Init helper objects - random = new Random(); - buffer = new SlotBuffer(); - - // Set Variables - oldestLiveSlotSequenceNumver = 1; - - // init data structs - committedKeyValueTable = new HashMap(); - speculatedKeyValueTable = new HashMap(); - pendingTransactionSpeculatedKeyValueTable = new HashMap(); - liveNewKeyTable = new HashMap(); - lastMessageTable = new HashMap>(); - rejectedMessageWatchListTable = new HashMap>(); - arbitratorTable = new HashMap(); - liveAbortTable = new HashMap, Abort>(); - newTransactionParts = new HashMap, TransactionPart>>(); - newCommitParts = new HashMap, CommitPart>>(); - lastArbitratedTransactionNumberByArbitratorTable = new HashMap(); - liveTransactionBySequenceNumberTable = new HashMap(); - liveTransactionByTransactionIdTable = new HashMap, Transaction>(); - liveCommitsTable = new HashMap>(); - liveCommitsByKeyTable = new HashMap(); - lastCommitSeenSequenceNumberByArbitratorTable = new HashMap(); - rejectedSlotList = new Vector(); - pendingTransactionQueue = new ArrayList(); - pendingSendArbitrationEntriesToDelete = new ArrayList(); - transactionPartsSent = new HashMap>(); - outstandingTransactionStatus = new HashMap(); - liveAbortsGeneratedByLocal = new HashMap(); - offlineTransactionsCommittedAndAtServer = new HashSet>(); - localCommunicationTable = new HashMap>(); - lastTransactionSeenFromMachineFromServer = new HashMap(); - pendingSendArbitrationRounds = new ArrayList(); - lastArbitrationDataLocalSequenceNumberSeenFromArbitrator = new HashMap(); - - - // Other init stuff - numberOfSlots = buffer.capacity(); - setResizeThreshold(); - } - - // TODO: delete method - synchronized void printSlots() { - int64_t o = buffer.getOldestSeqNum(); - int64_t n = buffer.getNewestSeqNum(); - - int[] types = new int[10]; - - int num = 0; - - int livec = 0; - int deadc = 0; - - int casdasd = 0; - - int liveslo = 0; - - for (int64_t i = o; i < (n + 1); i++) { - Slot s = buffer.getSlot(i); - - - if (s.isLive()) { - liveslo++; - } - - Vector entries = s.getEntries(); - - for (Entry e : entries) { - if (e.isLive()) { - int type = e.getType(); - - - if (type == 6) { - RejectedMessage rej = (RejectedMessage)e; - casdasd++; - - System.out.println(rej.getMachineID()); - } - - - types[type] = types[type] + 1; - num++; - livec++; - } else { - deadc++; - } - } - } - - for (int i = 0; i < 10; i++) { - System.out.println(i + " " + types[i]); - } - System.out.println("Live count: " + livec); - System.out.println("Live Slot count: " + liveslo); - - System.out.println("Dead count: " + deadc); - System.out.println("Old: " + o); - System.out.println("New: " + n); - System.out.println("Size: " + buffer.size()); - // System.out.println("Commits: " + liveCommitsTable.size()); - System.out.println("pendingTrans: " + pendingTransactionQueue.size()); - System.out.println("Trans Status Out: " + outstandingTransactionStatus.size()); - - for (Long k : lastArbitratedTransactionNumberByArbitratorTable.keySet()) { - System.out.println(k + ": " + lastArbitratedTransactionNumberByArbitratorTable.get(k)); - } - - - for (Long a : liveCommitsTable.keySet()) { - for (Long b : liveCommitsTable.get(a).keySet()) { - for (KeyValue kv : liveCommitsTable.get(a).get(b).getKeyValueUpdateSet()) { - System.out.print(kv + " "); - } - System.out.print("|| "); - } - System.out.println(); - } - - } - - /** - * Initialize the table by inserting a table status as the first entry into the table status - * also initialize the crypto stuff. - */ - synchronized void initTable() throws ServerException { - cloud.initSecurity(); - - // Create the first insertion into the block chain which is the table status - Slot s = new Slot(this, 1, localMachineId, localSequenceNumber); - localSequenceNumber++; - TableStatus status = new TableStatus(s, numberOfSlots); - s.addEntry(status); - Slot[] array = cloud.putSlot(s, numberOfSlots); - - if (array == NULL) { - array = new Slot[] {s}; - // update local block chain - validateAndUpdate(array, true); - } else if (array.length == 1) { - // in case we did push the slot BUT we failed to init it - validateAndUpdate(array, true); - } else { - throw new Error("Error on initialization"); - } - } - - /** - * Rebuild the table from scratch by pulling the latest block chain from the server. - */ - synchronized void rebuild() throws ServerException { - // Just pull the latest slots from the server - Slot[] newslots = cloud.getSlots(sequenceNumber + 1); - validateAndUpdate(newslots, true); - sendToServer(NULL); - updateLiveTransactionsAndStatus(); +Table::Table(CloudComm *_cloud, int64_t _localMachineId) : + buffer(NULL), + cloud(_cloud), + random(NULL), + liveTableStatus(NULL), + pendingTransactionBuilder(NULL), + lastPendingTransactionSpeculatedOn(NULL), + firstPendingTransaction(NULL), + numberOfSlots(0), + bufferResizeThreshold(0), + liveSlotCount(0), + oldestLiveSlotSequenceNumver(1), + localMachineId(_localMachineId), + sequenceNumber(0), + localSequenceNumber(0), + localTransactionSequenceNumber(0), + lastTransactionSequenceNumberSpeculatedOn(0), + oldestTransactionSequenceNumberSpeculatedOn(0), + localArbitrationSequenceNumber(0), + hadPartialSendToServer(false), + attemptedToSendToServer(false), + expectedsize(0), + didFindTableStatus(false), + currMaxSize(0), + lastSlotAttemptedToSend(NULL), + lastIsNewKey(false), + lastNewSize(0), + lastTransactionPartsSent(NULL), + lastPendingSendArbitrationEntriesToDelete(NULL), + lastNewKey(NULL), + committedKeyValueTable(NULL), + speculatedKeyValueTable(NULL), + pendingTransactionSpeculatedKeyValueTable(NULL), + liveNewKeyTable(NULL), + lastMessageTable(NULL), + rejectedMessageWatchVectorTable(NULL), + arbitratorTable(NULL), + liveAbortTable(NULL), + newTransactionParts(NULL), + newCommitParts(NULL), + lastArbitratedTransactionNumberByArbitratorTable(NULL), + liveTransactionBySequenceNumberTable(NULL), + liveTransactionByTransactionIdTable(NULL), + liveCommitsTable(NULL), + liveCommitsByKeyTable(NULL), + lastCommitSeenSequenceNumberByArbitratorTable(NULL), + rejectedSlotVector(NULL), + pendingTransactionQueue(NULL), + pendingSendArbitrationRounds(NULL), + pendingSendArbitrationEntriesToDelete(NULL), + transactionPartsSent(NULL), + outstandingTransactionStatus(NULL), + liveAbortsGeneratedByLocal(NULL), + offlineTransactionsCommittedAndAtServer(NULL), + localCommunicationTable(NULL), + lastTransactionSeenFromMachineFromServer(NULL), + lastArbitrationDataLocalSequenceNumberSeenFromArbitrator(NULL), + lastInsertedNewKey(false), + lastSeqNumArbOn(0) +{ + init(); +} +Table::~Table() { + delete cloud; + delete random; + delete buffer; + // init data structs + delete committedKeyValueTable; + delete speculatedKeyValueTable; + delete pendingTransactionSpeculatedKeyValueTable; + delete liveNewKeyTable; + { + SetIterator *> *lmit = getKeyIterator(lastMessageTable); + while (lmit->hasNext()) { + Pair * pair = lastMessageTable->get(lmit->next()); + } + delete lmit; + delete lastMessageTable; } - - // String toString() { - // String retString = " Committed Table: \n"; - // retString += "---------------------------\n"; - // retString += commitedTable.toString(); - - // retString += "\n\n"; - - // retString += " Speculative Table: \n"; - // retString += "---------------------------\n"; - // retString += speculativeTable.toString(); - - // return retString; - // } - - synchronized void addLocalCommunication(int64_t arbitrator, String hostName, int portNumber) { - localCommunicationTable.put(arbitrator, new Pair(hostName, portNumber)); + if (pendingTransactionBuilder != NULL) + delete pendingTransactionBuilder; + { + SetIterator *> *rmit = getKeyIterator(rejectedMessageWatchVectorTable); + while(rmit->hasNext()) { + int64_t machineid = rmit->next(); + Hashset * rmset = rejectedMessageWatchVectorTable->get(machineid); + SetIterator * mit = rmset->iterator(); + while (mit->hasNext()) { + RejectedMessage * rm = mit->next(); + delete rm; + } + delete mit; + delete rmset; + } + delete rmit; + delete rejectedMessageWatchVectorTable; } - - synchronized Long getArbitrator(IoTString key) { - return arbitratorTable.get(key); + delete arbitratorTable; + delete liveAbortTable; + { + SetIterator *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *> *partsit = getKeyIterator(newTransactionParts); + while (partsit->hasNext()) { + int64_t machineId = partsit->next(); + Hashtable *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *parts = newTransactionParts->get(machineId); + delete parts; + } + delete partsit; + delete newTransactionParts; } - - synchronized void close() { - cloud.close(); + { + SetIterator *, CommitPart *, uintptr_t, 0, pairHashFunction, pairEquals> *> *partsit = getKeyIterator(newCommitParts); + while (partsit->hasNext()) { + int64_t machineId = partsit->next(); + Hashtable *, CommitPart *, uintptr_t, 0, pairHashFunction, pairEquals> *parts = newCommitParts->get(machineId); + delete parts; + } + delete partsit; + delete newCommitParts; } - - synchronized IoTString getCommitted(IoTString key) { - KeyValue kv = committedKeyValueTable.get(key); - - if (kv != NULL) { - return kv.getValue(); - } else { - return NULL; + delete lastArbitratedTransactionNumberByArbitratorTable; + delete liveTransactionBySequenceNumberTable; + delete liveTransactionByTransactionIdTable; + { + SetIterator *> *liveit = getKeyIterator(liveCommitsTable); + while (liveit->hasNext()) { + int64_t arbitratorId = liveit->next(); + + // Get all the commits for a specific arbitrator + Hashtable *commitForClientTable = liveCommitsTable->get(arbitratorId); + { + SetIterator *clientit = getKeyIterator(commitForClientTable); + while (clientit->hasNext()) { + int64_t id = clientit->next(); + delete commitForClientTable->get(id); + } + delete clientit; + } + + delete commitForClientTable; } + delete liveit; + delete liveCommitsTable; } - - synchronized IoTString getSpeculative(IoTString key) { - KeyValue kv = pendingTransactionSpeculatedKeyValueTable.get(key); - - if (kv == NULL) { - kv = speculatedKeyValueTable.get(key); - } - - if (kv == NULL) { - kv = committedKeyValueTable.get(key); - } - - if (kv != NULL) { - return kv.getValue(); - } else { - return NULL; - } + delete liveCommitsByKeyTable; + delete lastCommitSeenSequenceNumberByArbitratorTable; + delete rejectedSlotVector; + { + uint size = pendingTransactionQueue->size(); + for (uint iter = 0; iter < size; iter++) { + delete pendingTransactionQueue->get(iter); + } + delete pendingTransactionQueue; } - - synchronized IoTString getCommittedAtomic(IoTString key) { - KeyValue kv = committedKeyValueTable.get(key); - - if (arbitratorTable.get(key) == NULL) { - throw new Error("Key not Found."); - } - - // Make sure new key value pair matches the current arbitrator - if (!pendingTransactionBuilder.checkArbitrator(arbitratorTable.get(key))) { - // TODO: Maybe not throw en error - throw new Error("Not all Key Values Match Arbitrator."); - } - - if (kv != NULL) { - pendingTransactionBuilder.addKVGuard(new KeyValue(key, kv.getValue())); - return kv.getValue(); - } else { - pendingTransactionBuilder.addKVGuard(new KeyValue(key, NULL)); - return NULL; - } + delete pendingSendArbitrationEntriesToDelete; + delete transactionPartsSent; + delete outstandingTransactionStatus; + delete liveAbortsGeneratedByLocal; + delete offlineTransactionsCommittedAndAtServer; + delete localCommunicationTable; + delete lastTransactionSeenFromMachineFromServer; + { + for(uint i = 0; i < pendingSendArbitrationRounds->size(); i++) { + delete pendingSendArbitrationRounds->get(i); + } + delete pendingSendArbitrationRounds; } + if (lastTransactionPartsSent != NULL) + delete lastTransactionPartsSent; + delete lastArbitrationDataLocalSequenceNumberSeenFromArbitrator; +} - synchronized IoTString getSpeculativeAtomic(IoTString key) { - if (arbitratorTable.get(key) == NULL) { - throw new Error("Key not Found."); - } - - // Make sure new key value pair matches the current arbitrator - if (!pendingTransactionBuilder.checkArbitrator(arbitratorTable.get(key))) { - // TODO: Maybe not throw en error - throw new Error("Not all Key Values Match Arbitrator."); - } - - KeyValue kv = pendingTransactionSpeculatedKeyValueTable.get(key); - - if (kv == NULL) { - kv = speculatedKeyValueTable.get(key); - } - - if (kv == NULL) { - kv = committedKeyValueTable.get(key); - } +/** + * Init all the stuff needed for for table usage + */ +void Table::init() { + // Init helper objects + random = new SecureRandom(); + buffer = new SlotBuffer(); + + // init data structs + committedKeyValueTable = new Hashtable(); + speculatedKeyValueTable = new Hashtable(); + pendingTransactionSpeculatedKeyValueTable = new Hashtable(); + liveNewKeyTable = new Hashtable(); + lastMessageTable = new Hashtable * >(); + rejectedMessageWatchVectorTable = new Hashtable * >(); + arbitratorTable = new Hashtable(); + liveAbortTable = new Hashtable *, Abort *, uintptr_t, 0, pairHashFunction, pairEquals>(); + newTransactionParts = new Hashtable *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *>(); + newCommitParts = new Hashtable *, CommitPart *, uintptr_t, 0, pairHashFunction, pairEquals> *>(); + lastArbitratedTransactionNumberByArbitratorTable = new Hashtable(); + liveTransactionBySequenceNumberTable = new Hashtable(); + liveTransactionByTransactionIdTable = new Hashtable *, Transaction *, uintptr_t, 0, pairHashFunction, pairEquals>(); + liveCommitsTable = new Hashtable * >(); + liveCommitsByKeyTable = new Hashtable(); + lastCommitSeenSequenceNumberByArbitratorTable = new Hashtable(); + rejectedSlotVector = new Vector(); + pendingTransactionQueue = new Vector(); + pendingSendArbitrationEntriesToDelete = new Vector(); + transactionPartsSent = new Hashtable *>(); + outstandingTransactionStatus = new Hashtable(); + liveAbortsGeneratedByLocal = new Hashtable(); + offlineTransactionsCommittedAndAtServer = new Hashset *, uintptr_t, 0, pairHashFunction, pairEquals>(); + localCommunicationTable = new Hashtable *>(); + lastTransactionSeenFromMachineFromServer = new Hashtable(); + pendingSendArbitrationRounds = new Vector(); + lastArbitrationDataLocalSequenceNumberSeenFromArbitrator = new Hashtable(); + + // Other init stuff + numberOfSlots = buffer->capacity(); + setResizeThreshold(); +} - if (kv != NULL) { - pendingTransactionBuilder.addKVGuard(new KeyValue(key, kv.getValue())); - return kv.getValue(); - } else { - pendingTransactionBuilder.addKVGuard(new KeyValue(key, NULL)); - return NULL; - } +/** + * Initialize the table by inserting a table status as the first entry + * into the table status also initialize the crypto stuff. + */ +void Table::initTable() { + cloud->initSecurity(); + + // Create the first insertion into the block chain which is the table status + Slot *s = new Slot(this, 1, localMachineId, localSequenceNumber); + localSequenceNumber++; + TableStatus *status = new TableStatus(s, numberOfSlots); + s->addEntry(status); + Array *array = cloud->putSlot(s, numberOfSlots); + + if (array == NULL) { + array = new Array(1); + array->set(0, s); + // update local block chain + validateAndUpdate(array, true); + delete array; + } else if (array->length() == 1) { + // in case we did push the slot BUT we failed to init it + validateAndUpdate(array, true); + delete array; + } else { + delete array; + throw new Error("Error on initialization"); } +} - synchronized bool update() { - try { - Slot[] newSlots = cloud.getSlots(sequenceNumber + 1); - validateAndUpdate(newSlots, false); - sendToServer(NULL); +/** + * Rebuild the table from scratch by pulling the latest block chain + * from the server. + */ +void Table::rebuild() { + // Just pull the latest slots from the server + Array *newslots = cloud->getSlots(sequenceNumber + 1); + validateAndUpdate(newslots, true); + delete newslots; + sendToServer(NULL); + updateLiveTransactionsAndStatus(); +} +void Table::addLocalCommunication(int64_t arbitrator, IoTString *hostName, int portNumber) { + localCommunicationTable->put(arbitrator, new Pair(hostName, portNumber)); +} - updateLiveTransactionsAndStatus(); +int64_t Table::getArbitrator(IoTString *key) { + return arbitratorTable->get(key); +} - return true; - } catch (Exception e) { - // e.printStackTrace(); +void Table::close() { + cloud->closeCloud(); +} - for (Long m : localCommunicationTable.keySet()) { - updateFromLocal(m); - } - } +IoTString *Table::getCommitted(IoTString *key) { + KeyValue *kv = committedKeyValueTable->get(key); - return false; + if (kv != NULL) { + return new IoTString(kv->getValue()); + } else { + return NULL; } +} - synchronized bool createNewKey(IoTString keyName, int64_t machineId) throws ServerException { - while (true) { - if (arbitratorTable.get(keyName) != NULL) { - // There is already an arbitrator - return false; - } - - NewKey newKey = new NewKey(NULL, keyName, machineId); +IoTString *Table::getSpeculative(IoTString *key) { + KeyValue *kv = pendingTransactionSpeculatedKeyValueTable->get(key); - if (sendToServer(newKey)) { - // If successfully inserted - return true; - } - } + if (kv == NULL) { + kv = speculatedKeyValueTable->get(key); } - synchronized void startTransaction() { - // Create a new transaction, invalidates any old pending transactions. - pendingTransactionBuilder = new PendingTransaction(localMachineId); + if (kv == NULL) { + kv = committedKeyValueTable->get(key); } - synchronized void addKV(IoTString key, IoTString value) { - - // Make sure it is a valid key - if (arbitratorTable.get(key) == NULL) { - throw new Error("Key not Found."); - } - - // Make sure new key value pair matches the current arbitrator - if (!pendingTransactionBuilder.checkArbitrator(arbitratorTable.get(key))) { - // TODO: Maybe not throw en error - throw new Error("Not all Key Values Match Arbitrator."); - } - - // Add the key value to this transaction - KeyValue kv = new KeyValue(key, value); - pendingTransactionBuilder.addKV(kv); + if (kv != NULL) { + return new IoTString(kv->getValue()); + } else { + return NULL; } +} - synchronized TransactionStatus commitTransaction() { - - if (pendingTransactionBuilder.getKVUpdates().size() == 0) { - // transaction with no updates will have no effect on the system - return new TransactionStatus(TransactionStatus.StatusNoEffect, -1); - } - - // Set the local transaction sequence number and increment - pendingTransactionBuilder.setClientLocalSequenceNumber(localTransactionSequenceNumber); - localTransactionSequenceNumber++; - - // Create the transaction status - TransactionStatus transactionStatus = new TransactionStatus(TransactionStatus.StatusPending, pendingTransactionBuilder.getArbitrator()); - - // Create the new transaction - Transaction newTransaction = pendingTransactionBuilder.createTransaction(); - newTransaction.setTransactionStatus(transactionStatus); - - if (pendingTransactionBuilder.getArbitrator() != localMachineId) { - // Add it to the queue and invalidate the builder for safety - pendingTransactionQueue.add(newTransaction); - } else { - arbitrateOnLocalTransaction(newTransaction); - updateLiveStateFromLocal(); - } - - pendingTransactionBuilder = new PendingTransaction(localMachineId); - - try { - sendToServer(NULL); - } catch (ServerException e) { - - Set arbitratorTriedAndFailed = new HashSet(); - for (Iterator iter = pendingTransactionQueue.iterator(); iter.hasNext(); ) { - Transaction transaction = iter.next(); - - if (arbitratorTriedAndFailed.contains(transaction.getArbitrator())) { - // Already contacted this client so ignore all attempts to contact this client - // to preserve ordering for arbitrator - continue; - } - - Pair sendReturn = sendTransactionToLocal(transaction); - - if (sendReturn.getFirst()) { - // Failed to contact over local - arbitratorTriedAndFailed.add(transaction.getArbitrator()); - } else { - // Successful contact or should not contact - - if (sendReturn.getSecond()) { - // did arbitrate - iter.remove(); - } - } - } - } - - updateLiveStateFromLocal(); +IoTString *Table::getCommittedAtomic(IoTString *key) { + KeyValue *kv = committedKeyValueTable->get(key); - return transactionStatus; + if (!arbitratorTable->contains(key)) { + throw new Error("Key not Found."); } - /** - * Get the machine ID for this client - */ - int64_t getMachineId() { - return localMachineId; + // Make sure new key value pair matches the current arbitrator + if (!pendingTransactionBuilder->checkArbitrator(arbitratorTable->get(key))) { + // TODO: Maybe not throw en error + throw new Error("Not all Key Values Match Arbitrator."); } - /** - * Decrement the number of live slots that we currently have - */ - void decrementLiveCount() { - liveSlotCount--; + if (kv != NULL) { + pendingTransactionBuilder->addKVGuard(new KeyValue(key, kv->getValue())); + return new IoTString(kv->getValue()); + } else { + pendingTransactionBuilder->addKVGuard(new KeyValue(key, NULL)); + return NULL; } +} - /** - * Recalculate the new resize threshold - */ - void setResizeThreshold() { - int resizeLower = (int) (RESIZE_THRESHOLD * numberOfSlots); - bufferResizeThreshold = resizeLower - 1 + random.nextInt(numberOfSlots - resizeLower); +IoTString *Table::getSpeculativeAtomic(IoTString *key) { + if (!arbitratorTable->contains(key)) { + throw new Error("Key not Found."); } - int64_t getLocalSequenceNumber() { - return localSequenceNumber; + // Make sure new key value pair matches the current arbitrator + if (!pendingTransactionBuilder->checkArbitrator(arbitratorTable->get(key))) { + // TODO: Maybe not throw en error + throw new Error("Not all Key Values Match Arbitrator."); } + KeyValue *kv = pendingTransactionSpeculatedKeyValueTable->get(key); - bool lastInsertedNewKey = false; - - bool sendToServer(NewKey newKey) throws ServerException { - - bool fromRetry = false; - - try { - if (hadPartialSendToServer) { - Slot[] newSlots = cloud.getSlots(sequenceNumber + 1); - if (newSlots.length == 0) { - fromRetry = true; - ThreeTuple sendSlotsReturn = sendSlotsToServer(lastSlotAttemptedToSend, lastNewSize, lastIsNewKey); - - if (sendSlotsReturn.getFirst()) { - if (newKey != NULL) { - if (lastInsertedNewKey && (lastNewKey.getKey() == newKey.getKey()) && (lastNewKey.getMachineID() == newKey.getMachineID())) { - newKey = NULL; - } - } - - for (Transaction transaction : lastTransactionPartsSent.keySet()) { - transaction.resetServerFailure(); - - // Update which transactions parts still need to be sent - transaction.removeSentParts(lastTransactionPartsSent.get(transaction)); - - // Add the transaction status to the outstanding list - outstandingTransactionStatus.put(transaction.getSequenceNumber(), transaction.getTransactionStatus()); - - // Update the transaction status - transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentPartial); - - // Check if all the transaction parts were successfully sent and if so then remove it from pending - if (transaction.didSendAllParts()) { - transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentFully); - pendingTransactionQueue.remove(transaction); - } - } - } else { - - newSlots = sendSlotsReturn.getThird(); - - bool isInserted = false; - for (Slot s : newSlots) { - if ((s.getSequenceNumber() == lastSlotAttemptedToSend.getSequenceNumber()) && (s.getMachineID() == localMachineId)) { - isInserted = true; - break; - } - } - - for (Slot s : newSlots) { - if (isInserted) { - break; - } - - // Process each entry in the slot - for (Entry entry : s.getEntries()) { - - if (entry.getType() == Entry.TypeLastMessage) { - LastMessage lastMessage = (LastMessage)entry; - if ((lastMessage.getMachineID() == localMachineId) && (lastMessage.getSequenceNumber() == lastSlotAttemptedToSend.getSequenceNumber())) { - isInserted = true; - break; - } - } - } - } - - if (isInserted) { - if (newKey != NULL) { - if (lastInsertedNewKey && (lastNewKey.getKey() == newKey.getKey()) && (lastNewKey.getMachineID() == newKey.getMachineID())) { - newKey = NULL; - } - } - - for (Transaction transaction : lastTransactionPartsSent.keySet()) { - transaction.resetServerFailure(); - - // Update which transactions parts still need to be sent - transaction.removeSentParts(lastTransactionPartsSent.get(transaction)); - - // Add the transaction status to the outstanding list - outstandingTransactionStatus.put(transaction.getSequenceNumber(), transaction.getTransactionStatus()); - - // Update the transaction status - transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentPartial); - - // Check if all the transaction parts were successfully sent and if so then remove it from pending - if (transaction.didSendAllParts()) { - transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentFully); - pendingTransactionQueue.remove(transaction); - } else { - transaction.resetServerFailure(); - // Set the transaction sequence number back to nothing - if (!transaction.didSendAPartToServer()) { - transaction.setSequenceNumber(-1); - } - } - } - } - } - - for (Transaction transaction : lastTransactionPartsSent.keySet()) { - transaction.resetServerFailure(); - // Set the transaction sequence number back to nothing - if (!transaction.didSendAPartToServer()) { - transaction.setSequenceNumber(-1); - } - } - - if (sendSlotsReturn.getThird().length != 0) { - // insert into the local block chain - validateAndUpdate(sendSlotsReturn.getThird(), true); - } - // continue; - } else { - bool isInserted = false; - for (Slot s : newSlots) { - if ((s.getSequenceNumber() == lastSlotAttemptedToSend.getSequenceNumber()) && (s.getMachineID() == localMachineId)) { - isInserted = true; - break; - } - } - - for (Slot s : newSlots) { - if (isInserted) { - break; - } - - // Process each entry in the slot - for (Entry entry : s.getEntries()) { - - if (entry.getType() == Entry.TypeLastMessage) { - LastMessage lastMessage = (LastMessage)entry; - if ((lastMessage.getMachineID() == localMachineId) && (lastMessage.getSequenceNumber() == lastSlotAttemptedToSend.getSequenceNumber())) { - isInserted = true; - break; - } - } - } - } - - if (isInserted) { - if (newKey != NULL) { - if (lastInsertedNewKey && (lastNewKey.getKey() == newKey.getKey()) && (lastNewKey.getMachineID() == newKey.getMachineID())) { - newKey = NULL; - } - } - - for (Transaction transaction : lastTransactionPartsSent.keySet()) { - transaction.resetServerFailure(); - - // Update which transactions parts still need to be sent - transaction.removeSentParts(lastTransactionPartsSent.get(transaction)); - - // Add the transaction status to the outstanding list - outstandingTransactionStatus.put(transaction.getSequenceNumber(), transaction.getTransactionStatus()); - - // Update the transaction status - transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentPartial); - - // Check if all the transaction parts were successfully sent and if so then remove it from pending - if (transaction.didSendAllParts()) { - transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentFully); - pendingTransactionQueue.remove(transaction); - } else { - transaction.resetServerFailure(); - // Set the transaction sequence number back to nothing - if (!transaction.didSendAPartToServer()) { - transaction.setSequenceNumber(-1); - } - } - } - } else { - for (Transaction transaction : lastTransactionPartsSent.keySet()) { - transaction.resetServerFailure(); - // Set the transaction sequence number back to nothing - if (!transaction.didSendAPartToServer()) { - transaction.setSequenceNumber(-1); - } - } - } - - // insert into the local block chain - validateAndUpdate(newSlots, true); - } - } - } catch (ServerException e) { - throw e; - } - - - - try { - // While we have stuff that needs inserting into the block chain - while ((pendingTransactionQueue.size() > 0) || (pendingSendArbitrationRounds.size() > 0) || (newKey != NULL)) { - - fromRetry = false; - - if (hadPartialSendToServer) { - throw new Error("Should Be error free"); - } - - - - // If there is a new key with same name then end - if ((newKey != NULL) && (arbitratorTable.get(newKey.getKey()) != NULL)) { - return false; - } - - // Create the slot - Slot slot = new Slot(this, sequenceNumber + 1, localMachineId, buffer.getSlot(sequenceNumber).getHMAC(), localSequenceNumber); - localSequenceNumber++; - - // Try to fill the slot with data - ThreeTuple fillSlotsReturn = fillSlot(slot, false, newKey); - bool needsResize = fillSlotsReturn.getFirst(); - int newSize = fillSlotsReturn.getSecond(); - Boolean insertedNewKey = fillSlotsReturn.getThird(); - - if (needsResize) { - // Reset which transaction to send - for (Transaction transaction : transactionPartsSent.keySet()) { - transaction.resetNextPartToSend(); - - // Set the transaction sequence number back to nothing - if (!transaction.didSendAPartToServer() && !transaction.getServerFailure()) { - transaction.setSequenceNumber(-1); - } - } - - // Clear the sent data since we are trying again - pendingSendArbitrationEntriesToDelete.clear(); - transactionPartsSent.clear(); - - // We needed a resize so try again - fillSlot(slot, true, newKey); - } - - lastSlotAttemptedToSend = slot; - lastIsNewKey = (newKey != NULL); - lastInsertedNewKey = insertedNewKey; - lastNewSize = newSize; - lastNewKey = newKey; - lastTransactionPartsSent = new HashMap>(transactionPartsSent); - lastPendingSendArbitrationEntriesToDelete = new ArrayList(pendingSendArbitrationEntriesToDelete); - - - ThreeTuple sendSlotsReturn = sendSlotsToServer(slot, newSize, newKey != NULL); - - if (sendSlotsReturn.getFirst()) { - - // Did insert into the block chain - - if (insertedNewKey) { - // This slot was what was inserted not a previous slot - - // New Key was successfully inserted into the block chain so dont want to insert it again - newKey = NULL; - } - - // Remove the aborts and commit parts that were sent from the pending to send queue - for (Iterator iter = pendingSendArbitrationRounds.iterator(); iter.hasNext(); ) { - ArbitrationRound round = iter.next(); - round.removeParts(pendingSendArbitrationEntriesToDelete); - - if (round.isDoneSending()) { - // Sent all the parts - iter.remove(); - } - } - - for (Transaction transaction : transactionPartsSent.keySet()) { - transaction.resetServerFailure(); - - // Update which transactions parts still need to be sent - transaction.removeSentParts(transactionPartsSent.get(transaction)); - - // Add the transaction status to the outstanding list - outstandingTransactionStatus.put(transaction.getSequenceNumber(), transaction.getTransactionStatus()); - - // Update the transaction status - transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentPartial); - - // Check if all the transaction parts were successfully sent and if so then remove it from pending - if (transaction.didSendAllParts()) { - transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentFully); - pendingTransactionQueue.remove(transaction); - } - } - } else { - - // if (!sendSlotsReturn.getSecond()) { - // for (Transaction transaction : lastTransactionPartsSent.keySet()) { - // transaction.resetServerFailure(); - // } - // } else { - // for (Transaction transaction : lastTransactionPartsSent.keySet()) { - // transaction.resetServerFailure(); - - // // Update which transactions parts still need to be sent - // transaction.removeSentParts(transactionPartsSent.get(transaction)); - - // // Add the transaction status to the outstanding list - // outstandingTransactionStatus.put(transaction.getSequenceNumber(), transaction.getTransactionStatus()); - - // // Update the transaction status - // transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentPartial); - - // // Check if all the transaction parts were successfully sent and if so then remove it from pending - // if (transaction.didSendAllParts()) { - // transaction.getTransactionStatus().setStatus(TransactionStatus.StatusSentFully); - // pendingTransactionQueue.remove(transaction); - - // for (KeyValue kv : transaction.getKeyValueUpdateSet()) { - // System.out.println("Sent: " + kv + " from: " + localMachineId + " Slot:" + lastSlotAttemptedToSend.getSequenceNumber() + " Claimed:" + transaction.getSequenceNumber()); - // } - // } - // } - // } - - // Reset which transaction to send - for (Transaction transaction : transactionPartsSent.keySet()) { - transaction.resetNextPartToSend(); - // transaction.resetNextPartToSend(); - - // Set the transaction sequence number back to nothing - if (!transaction.didSendAPartToServer() && !transaction.getServerFailure()) { - transaction.setSequenceNumber(-1); - } - } - } - - // Clear the sent data in preparation for next send - pendingSendArbitrationEntriesToDelete.clear(); - transactionPartsSent.clear(); - - if (sendSlotsReturn.getThird().length != 0) { - // insert into the local block chain - validateAndUpdate(sendSlotsReturn.getThird(), true); - } - } - - } catch (ServerException e) { - - if (e.getType() != ServerException.TypeInputTimeout) { - // e.printStackTrace(); - - // Nothing was able to be sent to the server so just clear these data structures - for (Transaction transaction : transactionPartsSent.keySet()) { - transaction.resetNextPartToSend(); - - // Set the transaction sequence number back to nothing - if (!transaction.didSendAPartToServer() && !transaction.getServerFailure()) { - transaction.setSequenceNumber(-1); - } - } - } else { - // There was a partial send to the server - hadPartialSendToServer = true; - - - // if (!fromRetry) { - // lastTransactionPartsSent = new HashMap>(transactionPartsSent); - // lastPendingSendArbitrationEntriesToDelete = new ArrayList(pendingSendArbitrationEntriesToDelete); - // } - - // Nothing was able to be sent to the server so just clear these data structures - for (Transaction transaction : transactionPartsSent.keySet()) { - transaction.resetNextPartToSend(); - transaction.setServerFailure(); - } - } - - pendingSendArbitrationEntriesToDelete.clear(); - transactionPartsSent.clear(); - - throw e; - } - - return newKey == NULL; + if (kv == NULL) { + kv = speculatedKeyValueTable->get(key); } - synchronized bool updateFromLocal(int64_t machineId) { - Pair localCommunicationInformation = localCommunicationTable.get(machineId); - if (localCommunicationInformation == NULL) { - // Cant talk to that device locally so do nothing - return false; - } + if (kv == NULL) { + kv = committedKeyValueTable->get(key); + } - // Get the size of the send data - int sendDataSize = sizeof(int32_t) + sizeof(int64_t); + if (kv != NULL) { + pendingTransactionBuilder->addKVGuard(new KeyValue(key, kv->getValue())); + return new IoTString(kv->getValue()); + } else { + pendingTransactionBuilder->addKVGuard(new KeyValue(key, NULL)); + return NULL; + } +} - Long lastArbitrationDataLocalSequenceNumber = (int64_t) - 1; - if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(machineId) != NULL) { - lastArbitrationDataLocalSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(machineId); +bool Table::update() { + try { + Array *newSlots = cloud->getSlots(sequenceNumber + 1); + validateAndUpdate(newSlots, false); + delete newSlots; + sendToServer(NULL); + updateLiveTransactionsAndStatus(); + return true; + } catch (Exception *e) { + SetIterator *> *kit = getKeyIterator(localCommunicationTable); + while (kit->hasNext()) { + int64_t m = kit->next(); + updateFromLocal(m); } + delete kit; + } - char[] sendData = new char[sendDataSize]; - ByteBuffer bbEncode = ByteBuffer.wrap(sendData); - - // Encode the data - bbEncode.putLong(lastArbitrationDataLocalSequenceNumber); - bbEncode.putInt(0); - - // Send by local - char[] returnData = cloud.sendLocalData(sendData, localSequenceNumber, localCommunicationInformation.getFirst(), localCommunicationInformation.getSecond()); - localSequenceNumber++; + return false; +} - if (returnData == NULL) { - // Could not contact server +bool Table::createNewKey(IoTString *keyName, int64_t machineId) { + while (true) { + if (arbitratorTable->contains(keyName)) { + // There is already an arbitrator return false; } + NewKey *newKey = new NewKey(NULL, keyName, machineId); - // Decode the data - ByteBuffer bbDecode = ByteBuffer.wrap(returnData); - int numberOfEntries = bbDecode.getInt(); - - for (int i = 0; i < numberOfEntries; i++) { - char type = bbDecode.get(); - if (type == Entry.TypeAbort) { - Abort abort = (Abort)Abort.decode(NULL, bbDecode); - processEntry(abort); - } else if (type == Entry.TypeCommitPart) { - CommitPart commitPart = (CommitPart)CommitPart.decode(NULL, bbDecode); - processEntry(commitPart); - } + if (sendToServer(newKey)) { + // If successfully inserted + return true; } - - updateLiveStateFromLocal(); - - return true; } +} - Pair sendTransactionToLocal(Transaction transaction) { - - // Get the devices local communications - Pair localCommunicationInformation = localCommunicationTable.get(transaction.getArbitrator()); - - if (localCommunicationInformation == NULL) { - // Cant talk to that device locally so do nothing - return new Pair(true, false); - } - - // Get the size of the send data - int sendDataSize = sizeof(int32_t) + sizeof(int64_t); - for (TransactionPart part : transaction.getParts().values()) { - sendDataSize += part.getSize(); - } - - Long lastArbitrationDataLocalSequenceNumber = (int64_t) - 1; - if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(transaction.getArbitrator()) != NULL) { - lastArbitrationDataLocalSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(transaction.getArbitrator()); - } - - // Make the send data size - char[] sendData = new char[sendDataSize]; - ByteBuffer bbEncode = ByteBuffer.wrap(sendData); - - // Encode the data - bbEncode.putLong(lastArbitrationDataLocalSequenceNumber); - bbEncode.putInt(transaction.getParts().size()); - for (TransactionPart part : transaction.getParts().values()) { - part.encode(bbEncode); - } +void Table::startTransaction() { + // Create a new transaction, invalidates any old pending transactions. + if (pendingTransactionBuilder != NULL) + delete pendingTransactionBuilder; + pendingTransactionBuilder = new PendingTransaction(localMachineId); +} +void Table::put(IoTString *key, IoTString *value) { + // Make sure it is a valid key + if (!arbitratorTable->contains(key)) { + throw new Error("Key not Found."); + } - // Send by local - char[] returnData = cloud.sendLocalData(sendData, localSequenceNumber, localCommunicationInformation.getFirst(), localCommunicationInformation.getSecond()); - localSequenceNumber++; + // Make sure new key value pair matches the current arbitrator + if (!pendingTransactionBuilder->checkArbitrator(arbitratorTable->get(key))) { + // TODO: Maybe not throw en error + throw new Error("Not all Key Values Match Arbitrator."); + } - if (returnData == NULL) { - // Could not contact server - return new Pair(true, false); - } + // Add the key value to this transaction + KeyValue *kv = new KeyValue(new IoTString(key), new IoTString(value)); + pendingTransactionBuilder->addKV(kv); +} - // Decode the data - ByteBuffer bbDecode = ByteBuffer.wrap(returnData); - bool didCommit = bbDecode.get() == 1; - bool couldArbitrate = bbDecode.get() == 1; - int numberOfEntries = bbDecode.getInt(); - bool foundAbort = false; +TransactionStatus *Table::commitTransaction() { + if (pendingTransactionBuilder->getKVUpdates()->size() == 0) { + // transaction with no updates will have no effect on the system + return new TransactionStatus(TransactionStatus_StatusNoEffect, -1); + } - for (int i = 0; i < numberOfEntries; i++) { - char type = bbDecode.get(); - if (type == Entry.TypeAbort) { - Abort abort = (Abort)Abort.decode(NULL, bbDecode); + // Set the local transaction sequence number and increment + pendingTransactionBuilder->setClientLocalSequenceNumber(localTransactionSequenceNumber); + localTransactionSequenceNumber++; - if ((abort.getTransactionMachineId() == localMachineId) && (abort.getTransactionClientLocalSequenceNumber() == transaction.getClientLocalSequenceNumber())) { - foundAbort = true; - } + // Create the transaction status + TransactionStatus *transactionStatus = new TransactionStatus(TransactionStatus_StatusPending, pendingTransactionBuilder->getArbitrator()); - processEntry(abort); - } else if (type == Entry.TypeCommitPart) { - CommitPart commitPart = (CommitPart)CommitPart.decode(NULL, bbDecode); - processEntry(commitPart); - } - } + // Create the new transaction + Transaction *newTransaction = pendingTransactionBuilder->createTransaction(); + newTransaction->setTransactionStatus(transactionStatus); + if (pendingTransactionBuilder->getArbitrator() != localMachineId) { + // Add it to the queue and invalidate the builder for safety + pendingTransactionQueue->add(newTransaction); + } else { + arbitrateOnLocalTransaction(newTransaction); + delete newTransaction; updateLiveStateFromLocal(); - - if (couldArbitrate) { - TransactionStatus status = transaction.getTransactionStatus(); - if (didCommit) { - status.setStatus(TransactionStatus.StatusCommitted); - } else { - status.setStatus(TransactionStatus.StatusAborted); - } - } else { - TransactionStatus status = transaction.getTransactionStatus(); - if (foundAbort) { - status.setStatus(TransactionStatus.StatusAborted); - } else { - status.setStatus(TransactionStatus.StatusCommitted); - } - } - - return new Pair(false, true); } + if (pendingTransactionBuilder != NULL) + delete pendingTransactionBuilder; + + pendingTransactionBuilder = new PendingTransaction(localMachineId); - synchronized char[] acceptDataFromLocal(char[] data) { - - // Decode the data - ByteBuffer bbDecode = ByteBuffer.wrap(data); - int64_t lastArbitratedSequenceNumberSeen = bbDecode.getLong(); - int numberOfParts = bbDecode.getInt(); - - // If we did commit a transaction or not - bool didCommit = false; - bool couldArbitrate = false; - - if (numberOfParts != 0) { - - // decode the transaction - Transaction transaction = new Transaction(); - for (int i = 0; i < numberOfParts; i++) { - bbDecode.get(); - TransactionPart newPart = (TransactionPart)TransactionPart.decode(NULL, bbDecode); - transaction.addPartDecode(newPart); - } - - // Arbitrate on transaction and pull relevant return data - Pair localArbitrateReturn = arbitrateOnLocalTransaction(transaction); - couldArbitrate = localArbitrateReturn.getFirst(); - didCommit = localArbitrateReturn.getSecond(); - - updateLiveStateFromLocal(); - - // Transaction was sent to the server so keep track of it to prevent double commit - if (transaction.getSequenceNumber() != -1) { - offlineTransactionsCommittedAndAtServer.add(transaction.getId()); - } - } - - // The data to send back - int returnDataSize = 0; - List unseenArbitrations = new ArrayList(); - - // Get the aborts to send back - List abortLocalSequenceNumbers = new ArrayList(liveAbortsGeneratedByLocal.keySet()); - Collections.sort(abortLocalSequenceNumbers); - for (Long localSequenceNumber : abortLocalSequenceNumbers) { - if (localSequenceNumber <= lastArbitratedSequenceNumberSeen) { + try { + sendToServer(NULL); + } catch (ServerException *e) { + + Hashset *arbitratorTriedAndFailed = new Hashset(); + uint size = pendingTransactionQueue->size(); + uint oldindex = 0; + for (uint iter = 0; iter < size; iter++) { + Transaction *transaction = pendingTransactionQueue->get(iter); + pendingTransactionQueue->set(oldindex++, pendingTransactionQueue->get(iter)); + + if (arbitratorTriedAndFailed->contains(transaction->getArbitrator())) { + // Already contacted this client so ignore all attempts to contact this client + // to preserve ordering for arbitrator continue; } - Abort abort = liveAbortsGeneratedByLocal.get(localSequenceNumber); - unseenArbitrations.add(abort); - returnDataSize += abort.getSize(); - } - - // Get the commits to send back - Map commitForClientTable = liveCommitsTable.get(localMachineId); - if (commitForClientTable != NULL) { - List commitLocalSequenceNumbers = new ArrayList(commitForClientTable.keySet()); - Collections.sort(commitLocalSequenceNumbers); - - for (Long localSequenceNumber : commitLocalSequenceNumbers) { - Commit commit = commitForClientTable.get(localSequenceNumber); - - if (localSequenceNumber <= lastArbitratedSequenceNumberSeen) { - continue; - } + Pair sendReturn = sendTransactionToLocal(transaction); - unseenArbitrations.addAll(commit.getParts().values()); - - for (CommitPart commitPart : commit.getParts().values()) { - returnDataSize += commitPart.getSize(); - } - } - } - - // Number of arbitration entries to decode - returnDataSize += 2 * sizeof(int32_t); - - // Boolean of did commit or not - if (numberOfParts != 0) { - returnDataSize += sizeof(char); - } - - // Data to send Back - char[] returnData = new char[returnDataSize]; - ByteBuffer bbEncode = ByteBuffer.wrap(returnData); - - if (numberOfParts != 0) { - if (didCommit) { - bbEncode.put((char)1); - } else { - bbEncode.put((char)0); - } - if (couldArbitrate) { - bbEncode.put((char)1); + if (sendReturn.getFirst()) { + // Failed to contact over local + arbitratorTriedAndFailed->add(transaction->getArbitrator()); } else { - bbEncode.put((char)0); - } - } - - bbEncode.putInt(unseenArbitrations.size()); - for (Entry entry : unseenArbitrations) { - entry.encode(bbEncode); - } - - - localSequenceNumber++; - return returnData; - } - - ThreeTuple sendSlotsToServer(Slot slot, int newSize, bool isNewKey) throws ServerException { - - bool attemptedToSendToServerTmp = attemptedToSendToServer; - attemptedToSendToServer = true; - - bool inserted = false; - bool lastTryInserted = false; - - Slot[] array = cloud.putSlot(slot, newSize); - if (array == NULL) { - array = new Slot[] {slot}; - rejectedSlotList.clear(); - inserted = true; - } else { - if (array.length == 0) { - throw new Error("Server Error: Did not send any slots"); - } - - // if (attemptedToSendToServerTmp) { - if (hadPartialSendToServer) { - - bool isInserted = false; - for (Slot s : array) { - if ((s.getSequenceNumber() == slot.getSequenceNumber()) && (s.getMachineID() == localMachineId)) { - isInserted = true; - break; - } - } - - for (Slot s : array) { - if (isInserted) { - break; - } - - // Process each entry in the slot - for (Entry entry : s.getEntries()) { - - if (entry.getType() == Entry.TypeLastMessage) { - LastMessage lastMessage = (LastMessage)entry; - - if ((lastMessage.getMachineID() == localMachineId) && (lastMessage.getSequenceNumber() == slot.getSequenceNumber())) { - isInserted = true; - break; - } - } - } - } + // Successful contact or should not contact - if (!isInserted) { - rejectedSlotList.add(slot.getSequenceNumber()); - lastTryInserted = false; - } else { - lastTryInserted = true; + if (sendReturn.getSecond()) { + // did arbitrate + delete transaction; + oldindex--; } - } else { - rejectedSlotList.add(slot.getSequenceNumber()); - lastTryInserted = false; } } - - return new ThreeTuple(inserted, lastTryInserted, array); + pendingTransactionQueue->setSize(oldindex); } - /** - * Returns false if a resize was needed - */ - ThreeTuple fillSlot(Slot slot, bool resize, NewKey newKeyEntry) { - - - int newSize = 0; - if (liveSlotCount > bufferResizeThreshold) { - resize = true; //Resize is forced - - } - - if (resize) { - newSize = (int) (numberOfSlots * RESIZE_MULTIPLE); - TableStatus status = new TableStatus(slot, newSize); - slot.addEntry(status); - } - - // Fill with rejected slots first before doing anything else - doRejectedMessages(slot); - - // Do mandatory rescue of entries - ThreeTuple mandatoryRescueReturn = doMandatoryResuce(slot, resize); - - // Extract working variables - bool needsResize = mandatoryRescueReturn.getFirst(); - bool seenLiveSlot = mandatoryRescueReturn.getSecond(); - int64_t currentRescueSequenceNumber = mandatoryRescueReturn.getThird(); + updateLiveStateFromLocal(); - if (needsResize && !resize) { - // We need to resize but we are not resizing so return false - return new ThreeTuple(true, NULL, NULL); - } - - bool inserted = false; - if (newKeyEntry != NULL) { - newKeyEntry.setSlot(slot); - if (slot.hasSpace(newKeyEntry)) { - - slot.addEntry(newKeyEntry); - inserted = true; - } - } - - // Clear the transactions, aborts and commits that were sent previously - transactionPartsSent.clear(); - pendingSendArbitrationEntriesToDelete.clear(); - - for (ArbitrationRound round : pendingSendArbitrationRounds) { - bool isFull = false; - round.generateParts(); - List parts = round.getParts(); + return transactionStatus; +} - // Insert pending arbitration data - for (Entry arbitrationData : parts) { +/** + * Recalculate the new resize threshold + */ +void Table::setResizeThreshold() { + int resizeLower = (int) (Table_RESIZE_THRESHOLD * numberOfSlots); + bufferResizeThreshold = resizeLower - 1 + random->nextInt(numberOfSlots - resizeLower); +} - // If it is an abort then we need to set some information - if (arbitrationData instanceof Abort) { - ((Abort)arbitrationData).setSequenceNumber(slot.getSequenceNumber()); - } +int64_t Table::getLocalSequenceNumber() { + return localSequenceNumber; +} - if (!slot.hasSpace(arbitrationData)) { - // No space so cant do anything else with these data entries - isFull = true; - break; +NewKey * Table::handlePartialSend(NewKey * newKey) { + //Didn't receive acknowledgement for last send + //See if the server has received a newer slot + + Array *newSlots = cloud->getSlots(sequenceNumber + 1); + if (newSlots->length() == 0) { + //Retry sending old slot + bool wasInserted = false; + bool sendSlotsReturn = sendSlotsToServer(lastSlotAttemptedToSend, lastNewSize, lastIsNewKey, &wasInserted, &newSlots); + + if (sendSlotsReturn) { + if (newKey != NULL) { + if (lastInsertedNewKey && (lastNewKey->getKey() == newKey->getKey()) && (lastNewKey->getMachineID() == newKey->getMachineID())) { + newKey = NULL; } - - // Add to this current slot and add it to entries to delete - slot.addEntry(arbitrationData); - pendingSendArbitrationEntriesToDelete.add(arbitrationData); } - - if (isFull) { - break; - } - } - - if (pendingTransactionQueue.size() > 0) { - - Transaction transaction = pendingTransactionQueue.get(0); - - // Set the transaction sequence number if it has yet to be inserted into the block chain - // if ((!transaction.didSendAPartToServer() && !transaction.getServerFailure()) || (transaction.getSequenceNumber() == -1)) { - // transaction.setSequenceNumber(slot.getSequenceNumber()); - // } - - if ((!transaction.didSendAPartToServer()) || (transaction.getSequenceNumber() == -1)) { - transaction.setSequenceNumber(slot.getSequenceNumber()); - } - - - while (true) { - TransactionPart part = transaction.getNextPartToSend(); - - if (part == NULL) { - // Ran out of parts to send for this transaction so move on - break; - } - - if (slot.hasSpace(part)) { - slot.addEntry(part); - List partsSent = transactionPartsSent.get(transaction); - if (partsSent == NULL) { - partsSent = new ArrayList(); - transactionPartsSent.put(transaction, partsSent); - } - partsSent.add(part.getPartNumber()); - transactionPartsSent.put(transaction, partsSent); - } else { - break; + + SetIterator *> *trit = getKeyIterator(lastTransactionPartsSent); + while (trit->hasNext()) { + Transaction *transaction = trit->next(); + transaction->resetServerFailure(); + // Update which transactions parts still need to be sent + transaction->removeSentParts(lastTransactionPartsSent->get(transaction)); + // Add the transaction status to the outstanding list + outstandingTransactionStatus->put(transaction->getSequenceNumber(), transaction->getTransactionStatus()); + + // Update the transaction status + transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentPartial); + + // Check if all the transaction parts were successfully + // sent and if so then remove it from pending + if (transaction->didSendAllParts()) { + transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentFully); + pendingTransactionQueue->remove(transaction); } } - } - - // Fill the remainder of the slot with rescue data - doOptionalRescue(slot, seenLiveSlot, currentRescueSequenceNumber, resize); - - return new ThreeTuple(false, newSize, inserted); - } - - void doRejectedMessages(Slot s) { - if (! rejectedSlotList.isEmpty()) { - /* TODO: We should avoid generating a rejected message entry if - * there is already a sufficient entry in the queue (e.g., - * equalsto value of true and same sequence number). */ - - int64_t old_seqn = rejectedSlotList.firstElement(); - if (rejectedSlotList.size() > REJECTED_THRESHOLD) { - int64_t new_seqn = rejectedSlotList.lastElement(); - RejectedMessage rm = new RejectedMessage(s, s.getSequenceNumber(), localMachineId, old_seqn, new_seqn, false); - s.addEntry(rm); - } else { - int64_t prev_seqn = -1; - int i = 0; - /* Go through list of missing messages */ - for (; i < rejectedSlotList.size(); i++) { - int64_t curr_seqn = rejectedSlotList.get(i); - Slot s_msg = buffer.getSlot(curr_seqn); - if (s_msg != NULL) - break; - prev_seqn = curr_seqn; + delete trit; + } else { + if (checkSend(newSlots, lastSlotAttemptedToSend)) { + if (newKey != NULL) { + if (lastInsertedNewKey && (lastNewKey->getKey() == newKey->getKey()) && (lastNewKey->getMachineID() == newKey->getMachineID())) { + newKey = NULL; + } } - /* Generate rejected message entry for missing messages */ - if (prev_seqn != -1) { - RejectedMessage rm = new RejectedMessage(s, s.getSequenceNumber(), localMachineId, old_seqn, prev_seqn, false); - s.addEntry(rm); + + SetIterator *> *trit = getKeyIterator(lastTransactionPartsSent); + while (trit->hasNext()) { + Transaction *transaction = trit->next(); + transaction->resetServerFailure(); + + // Update which transactions parts still need to be sent + transaction->removeSentParts(lastTransactionPartsSent->get(transaction)); + + // Add the transaction status to the outstanding list + outstandingTransactionStatus->put(transaction->getSequenceNumber(), transaction->getTransactionStatus()); + + // Update the transaction status + transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentPartial); + + // Check if all the transaction parts were successfully sent and if so then remove it from pending + if (transaction->didSendAllParts()) { + transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentFully); + pendingTransactionQueue->remove(transaction); + } else { + transaction->resetServerFailure(); + // Set the transaction sequence number back to nothing + if (!transaction->didSendAPartToServer()) { + transaction->setSequenceNumber(-1); + } + } } - /* Generate rejected message entries for present messages */ - for (; i < rejectedSlotList.size(); i++) { - int64_t curr_seqn = rejectedSlotList.get(i); - Slot s_msg = buffer.getSlot(curr_seqn); - int64_t machineid = s_msg.getMachineID(); - RejectedMessage rm = new RejectedMessage(s, s.getSequenceNumber(), machineid, curr_seqn, curr_seqn, true); - s.addEntry(rm); + delete trit; + } + } + + SetIterator *> *trit = getKeyIterator(lastTransactionPartsSent); + while (trit->hasNext()) { + Transaction *transaction = trit->next(); + transaction->resetServerFailure(); + // Set the transaction sequence number back to nothing + if (!transaction->didSendAPartToServer()) { + transaction->setSequenceNumber(-1); + } + } + delete trit; + + if (newSlots->length() != 0) { + // insert into the local block chain + validateAndUpdate(newSlots, true); + } + } else { + if (checkSend(newSlots, lastSlotAttemptedToSend)) { + if (newKey != NULL) { + if (lastInsertedNewKey && (lastNewKey->getKey() == newKey->getKey()) && (lastNewKey->getMachineID() == newKey->getMachineID())) { + newKey = NULL; } } - } - } - - ThreeTuple doMandatoryResuce(Slot slot, bool resize) { - int64_t newestSequenceNumber = buffer.getNewestSeqNum(); - int64_t oldestSequenceNumber = buffer.getOldestSeqNum(); - if (oldestLiveSlotSequenceNumver < oldestSequenceNumber) { - oldestLiveSlotSequenceNumver = oldestSequenceNumber; - } - - int64_t currentSequenceNumber = oldestLiveSlotSequenceNumver; - bool seenLiveSlot = false; - int64_t firstIfFull = newestSequenceNumber + 1 - numberOfSlots; // smallest seq number in the buffer if it is full - int64_t threshold = firstIfFull + FREE_SLOTS; // we want the buffer to be clear of live entries up to this point - - - // Mandatory Rescue - for (; currentSequenceNumber < threshold; currentSequenceNumber++) { - Slot previousSlot = buffer.getSlot(currentSequenceNumber); - // Push slot number forward - if (! seenLiveSlot) { - oldestLiveSlotSequenceNumver = currentSequenceNumber; - } - - if (!previousSlot.isLive()) { - continue; - } - - // We have seen a live slot - seenLiveSlot = true; - - // Get all the live entries for a slot - Vector liveEntries = previousSlot.getLiveEntries(resize); - - // Iterate over all the live entries and try to rescue them - for (Entry liveEntry : liveEntries) { - if (slot.hasSpace(liveEntry)) { - - // Enough space to rescue the entry - slot.addEntry(liveEntry); - } else if (currentSequenceNumber == firstIfFull) { - //if there's no space but the entry is about to fall off the queue - System.out.println("B"); //? - return new ThreeTuple(true, seenLiveSlot, currentSequenceNumber); - + + SetIterator *> *trit = getKeyIterator(lastTransactionPartsSent); + while (trit->hasNext()) { + Transaction *transaction = trit->next(); + transaction->resetServerFailure(); + + // Update which transactions parts still need to be sent + transaction->removeSentParts(lastTransactionPartsSent->get(transaction)); + + // Add the transaction status to the outstanding list + outstandingTransactionStatus->put(transaction->getSequenceNumber(), transaction->getTransactionStatus()); + + // Update the transaction status + transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentPartial); + + // Check if all the transaction parts were successfully sent and if so then remove it from pending + if (transaction->didSendAllParts()) { + transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentFully); + pendingTransactionQueue->remove(transaction); + } else { + transaction->resetServerFailure(); + // Set the transaction sequence number back to nothing + if (!transaction->didSendAPartToServer()) { + transaction->setSequenceNumber(-1); + } } } - } - - // Did not resize - return new ThreeTuple(false, seenLiveSlot, currentSequenceNumber); - } - - void doOptionalRescue(Slot s, bool seenliveslot, int64_t seqn, bool resize) { - /* now go through live entries from least to greatest sequence number until - * either all live slots added, or the slot doesn't have enough room - * for SKIP_THRESHOLD consecutive entries*/ - int skipcount = 0; - int64_t newestseqnum = buffer.getNewestSeqNum(); - search: - for (; seqn <= newestseqnum; seqn++) { - Slot prevslot = buffer.getSlot(seqn); - //Push slot number forward - if (!seenliveslot) - oldestLiveSlotSequenceNumver = seqn; - - if (!prevslot.isLive()) - continue; - seenliveslot = true; - Vector liveentries = prevslot.getLiveEntries(resize); - for (Entry liveentry : liveentries) { - if (s.hasSpace(liveentry)) - s.addEntry(liveentry); - else { - skipcount++; - if (skipcount > SKIP_THRESHOLD) - break search; + delete trit; + } else { + SetIterator *> *trit = getKeyIterator(lastTransactionPartsSent); + while (trit->hasNext()) { + Transaction *transaction = trit->next(); + transaction->resetServerFailure(); + // Set the transaction sequence number back to nothing + if (!transaction->didSendAPartToServer()) { + transaction->setSequenceNumber(-1); } } + delete trit; } + + // insert into the local block chain + validateAndUpdate(newSlots, true); } + delete newSlots; + return newKey; +} - /** - * Checks for malicious activity and updates the local copy of the block chain. - */ - void validateAndUpdate(Slot[] newSlots, bool acceptUpdatesToLocal) { - - // The cloud communication layer has checked slot HMACs already before decoding - if (newSlots.length == 0) { - return; - } - - // Make sure all slots are newer than the last largest slot this client has seen - int64_t firstSeqNum = newSlots[0].getSequenceNumber(); - if (firstSeqNum <= sequenceNumber) { - throw new Error("Server Error: Sent older slots!"); - } - - // Create an object that can access both new slots and slots in our local chain - // without committing slots to our local chain - SlotIndexer indexer = new SlotIndexer(newSlots, buffer); - - // Check that the HMAC chain is not broken - checkHMACChain(indexer, newSlots); - - // Set to keep track of messages from clients - HashSet machineSet = new HashSet(lastMessageTable.keySet()); - - // Process each slots data - for (Slot slot : newSlots) { - processSlot(indexer, slot, acceptUpdatesToLocal, machineSet); - - updateExpectedSize(); - } - - // If there is a gap, check to see if the server sent us everything. - if (firstSeqNum != (sequenceNumber + 1)) { - - // Check the size of the slots that were sent down by the server. - // Can only check the size if there was a gap - checkNumSlots(newSlots.length); +bool Table::sendToServer(NewKey *newKey) { + if (hadPartialSendToServer) { + newKey = handlePartialSend(newKey); + } - // Since there was a gap every machine must have pushed a slot or must have - // a last message message. If not then the server is hiding slots - if (!machineSet.isEmpty()) { - throw new Error("Missing record for machines: " + machineSet); + try { + // While we have stuff that needs inserting into the block chain + while ((pendingTransactionQueue->size() > 0) || (pendingSendArbitrationRounds->size() > 0) || (newKey != NULL)) { + if (hadPartialSendToServer) { + throw new Error("Should Be error free"); + } + + // If there is a new key with same name then end + if ((newKey != NULL) && arbitratorTable->contains(newKey->getKey())) { + return false; } - } - - // Update the size of our local block chain. - commitNewMaxSize(); - - // Commit new to slots to the local block chain. - for (Slot slot : newSlots) { - - // Insert this slot into our local block chain copy. - buffer.putSlot(slot); - - // Keep track of how many slots are currently live (have live data in them). - liveSlotCount++; - } - - // Get the sequence number of the latest slot in the system - sequenceNumber = newSlots[newSlots.length - 1].getSequenceNumber(); - - updateLiveStateFromServer(); - // No Need to remember after we pulled from the server - offlineTransactionsCommittedAndAtServer.clear(); + // Create the slot + Slot *slot = new Slot(this, sequenceNumber + 1, localMachineId, new Array(buffer->getSlot(sequenceNumber)->getHMAC()), localSequenceNumber); + localSequenceNumber++; - // This is invalidated now - hadPartialSendToServer = false; - } + // Try to fill the slot with data + int newSize = 0; + bool insertedNewKey = false; + bool needsResize = fillSlot(slot, false, newKey, newSize, insertedNewKey); - void updateLiveStateFromServer() { - // Process the new transaction parts - processNewTransactionParts(); + if (needsResize) { + // Reset which transaction to send + SetIterator *> *trit = getKeyIterator(transactionPartsSent); + while (trit->hasNext()) { + Transaction *transaction = trit->next(); + transaction->resetNextPartToSend(); - // Do arbitration on new transactions that were received - arbitrateFromServer(); + // Set the transaction sequence number back to nothing + if (!transaction->didSendAPartToServer() && !transaction->getServerFailure()) { + transaction->setSequenceNumber(-1); + } + } + delete trit; + + // Clear the sent data since we are trying again + pendingSendArbitrationEntriesToDelete->clear(); + transactionPartsSent->clear(); + + // We needed a resize so try again + fillSlot(slot, true, newKey, newSize, insertedNewKey); + } + + lastSlotAttemptedToSend = slot; + lastIsNewKey = (newKey != NULL); + lastInsertedNewKey = insertedNewKey; + lastNewSize = newSize; + lastNewKey = newKey; + if (lastTransactionPartsSent != NULL) + delete lastTransactionPartsSent; + lastTransactionPartsSent = transactionPartsSent->clone(); + lastPendingSendArbitrationEntriesToDelete = new Vector(pendingSendArbitrationEntriesToDelete); + + Array * newSlots = NULL; + bool wasInserted = false; + bool sendSlotsReturn = sendSlotsToServer(slot, newSize, newKey != NULL, &wasInserted, &newSlots); + + if (sendSlotsReturn) { + // Did insert into the block chain + if (insertedNewKey) { + // This slot was what was inserted not a previous slot + // New Key was successfully inserted into the block chain so dont want to insert it again + newKey = NULL; + } - // Update all the committed keys - bool didCommitOrSpeculate = updateCommittedTable(); + // Remove the aborts and commit parts that were sent from the pending to send queue + uint size = pendingSendArbitrationRounds->size(); + uint oldcount = 0; + for (uint i = 0; i < size; i++) { + ArbitrationRound *round = pendingSendArbitrationRounds->get(i); + round->removeParts(pendingSendArbitrationEntriesToDelete); + + if (!round->isDoneSending()) { + //Add part back in + pendingSendArbitrationRounds->set(oldcount++, + pendingSendArbitrationRounds->get(i)); + } else + delete pendingSendArbitrationRounds->get(i); + } + pendingSendArbitrationRounds->setSize(oldcount); - // Delete the transactions that are now dead - updateLiveTransactionsAndStatus(); + SetIterator *> *trit = getKeyIterator(transactionPartsSent); + while (trit->hasNext()) { + Transaction *transaction = trit->next(); + transaction->resetServerFailure(); - // Do speculations - didCommitOrSpeculate |= updateSpeculativeTable(didCommitOrSpeculate); - updatePendingTransactionSpeculativeTable(didCommitOrSpeculate); - } + // Update which transactions parts still need to be sent + transaction->removeSentParts(transactionPartsSent->get(transaction)); - void updateLiveStateFromLocal() { - // Update all the committed keys - bool didCommitOrSpeculate = updateCommittedTable(); + // Add the transaction status to the outstanding list + outstandingTransactionStatus->put(transaction->getSequenceNumber(), transaction->getTransactionStatus()); - // Delete the transactions that are now dead - updateLiveTransactionsAndStatus(); + // Update the transaction status + transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentPartial); - // Do speculations - didCommitOrSpeculate |= updateSpeculativeTable(didCommitOrSpeculate); - updatePendingTransactionSpeculativeTable(didCommitOrSpeculate); - } + // Check if all the transaction parts were successfully sent and if so then remove it from pending + if (transaction->didSendAllParts()) { + transaction->getTransactionStatus()->setStatus(TransactionStatus_StatusSentFully); + pendingTransactionQueue->remove(transaction); + } + } + delete trit; + } else { + // Reset which transaction to send + SetIterator *> *trit = getKeyIterator(transactionPartsSent); + while (trit->hasNext()) { + Transaction *transaction = trit->next(); + transaction->resetNextPartToSend(); - void initExpectedSize(int64_t firstSequenceNumber, int64_t numberOfSlots) { - // if (didFindTableStatus) { - // return; - // } - int64_t prevslots = firstSequenceNumber; + // Set the transaction sequence number back to nothing + if (!transaction->didSendAPartToServer() && !transaction->getServerFailure()) { + transaction->setSequenceNumber(-1); + } + } + delete trit; + } + // Clear the sent data in preparation for next send + pendingSendArbitrationEntriesToDelete->clear(); + transactionPartsSent->clear(); - if (didFindTableStatus) { - // expectedsize = (prevslots < ((int64_t) numberOfSlots)) ? (int) prevslots : expectedsize; - // System.out.println("Here2: " + expectedsize + " " + numberOfSlots + " " + prevslots); + if (newSlots->length() != 0) { + // insert into the local block chain + validateAndUpdate(newSlots, true); + } + delete newSlots; + } + } catch (ServerException *e) { + if (e->getType() != ServerException_TypeInputTimeout) { + // Nothing was able to be sent to the server so just clear these data structures + SetIterator *> *trit = getKeyIterator(transactionPartsSent); + while (trit->hasNext()) { + Transaction *transaction = trit->next(); + transaction->resetNextPartToSend(); + // Set the transaction sequence number back to nothing + if (!transaction->didSendAPartToServer() && !transaction->getServerFailure()) { + transaction->setSequenceNumber(-1); + } + } + delete trit; } else { - expectedsize = (prevslots < ((int64_t) numberOfSlots)) ? (int) prevslots : numberOfSlots; - // System.out.println("Here: " + expectedsize); + // There was a partial send to the server + hadPartialSendToServer = true; + + // Nothing was able to be sent to the server so just clear these data structures + SetIterator *> *trit = getKeyIterator(transactionPartsSent); + while (trit->hasNext()) { + Transaction *transaction = trit->next(); + transaction->resetNextPartToSend(); + transaction->setServerFailure(); + } + delete trit; } - // System.out.println(numberOfSlots); + pendingSendArbitrationEntriesToDelete->clear(); + transactionPartsSent->clear(); - didFindTableStatus = true; - currMaxSize = numberOfSlots; + throw e; } - void updateExpectedSize() { - expectedsize++; + return newKey == NULL; +} - if (expectedsize > currMaxSize) { - expectedsize = currMaxSize; - } - } +bool Table::updateFromLocal(int64_t machineId) { + if (!localCommunicationTable->contains(machineId)) + return false; + Pair *localCommunicationInformation = localCommunicationTable->get(machineId); - /** - * Check the size of the block chain to make sure there are enough slots sent back by the server. - * This is only called when we have a gap between the slots that we have locally and the slots - * sent by the server therefore in the slots sent by the server there will be at least 1 Table - * status message - */ - void checkNumSlots(int numberOfSlots) { - if (numberOfSlots != expectedsize) { - throw new Error("Server Error: Server did not send all slots. Expected: " + expectedsize + " Received:" + numberOfSlots); - } - } + // Get the size of the send data + int sendDataSize = sizeof(int32_t) + sizeof(int64_t); - void updateCurrMaxSize(int newmaxsize) { - currMaxSize = newmaxsize; + int64_t lastArbitrationDataLocalSequenceNumber = (int64_t) -1; + if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->contains(machineId)) { + lastArbitrationDataLocalSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->get(machineId); } + Array *sendData = new Array(sendDataSize); + ByteBuffer *bbEncode = ByteBuffer_wrap(sendData); - /** - * Update the size of of the local buffer if it is needed. - */ - void commitNewMaxSize() { - didFindTableStatus = false; - - // Resize the local slot buffer - if (numberOfSlots != currMaxSize) { - buffer.resize((int)currMaxSize); - } - - // Change the number of local slots to the new size - numberOfSlots = (int)currMaxSize; + // Encode the data + bbEncode->putLong(lastArbitrationDataLocalSequenceNumber); + bbEncode->putInt(0); + // Send by local + Array *returnData = cloud->sendLocalData(sendData, localSequenceNumber, localCommunicationInformation->getFirst(), localCommunicationInformation->getSecond()); + localSequenceNumber++; - // Recalculate the resize threshold since the size of the local buffer has changed - setResizeThreshold(); + if (returnData == NULL) { + // Could not contact server + return false; } - /** - * Process the new transaction parts from this latest round of slots received from the server - */ - void processNewTransactionParts() { + // Decode the data + ByteBuffer *bbDecode = ByteBuffer_wrap(returnData); + int numberOfEntries = bbDecode->getInt(); - if (newTransactionParts.size() == 0) { - // Nothing new to process - return; + for (int i = 0; i < numberOfEntries; i++) { + char type = bbDecode->get(); + if (type == TypeAbort) { + Abort *abort = (Abort *)Abort_decode(NULL, bbDecode); + processEntry(abort); + } else if (type == TypeCommitPart) { + CommitPart *commitPart = (CommitPart *)CommitPart_decode(NULL, bbDecode); + processEntry(commitPart); } + } - // Iterate through all the machine Ids that we received new parts for - for (Long machineId : newTransactionParts.keySet()) { - Map, TransactionPart> parts = newTransactionParts.get(machineId); - - // Iterate through all the parts for that machine Id - for (Pair partId : parts.keySet()) { - TransactionPart part = parts.get(partId); + updateLiveStateFromLocal(); - Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(part.getArbitratorId()); - if ((lastTransactionNumber != NULL) && (lastTransactionNumber >= part.getSequenceNumber())) { - // Set dead the transaction part - part.setDead(); - continue; - } + return true; +} - // Get the transaction object for that sequence number - Transaction transaction = liveTransactionBySequenceNumberTable.get(part.getSequenceNumber()); +Pair Table::sendTransactionToLocal(Transaction *transaction) { - if (transaction == NULL) { - // This is a new transaction that we dont have so make a new one - transaction = new Transaction(); + // Get the devices local communications + if (!localCommunicationTable->contains(transaction->getArbitrator())) + return Pair(true, false); - // Insert this new transaction into the live tables - liveTransactionBySequenceNumberTable.put(part.getSequenceNumber(), transaction); - liveTransactionByTransactionIdTable.put(part.getTransactionId(), transaction); - } + Pair *localCommunicationInformation = localCommunicationTable->get(transaction->getArbitrator()); - // Add that part to the transaction - transaction.addPartDecode(part); - } + // Get the size of the send data + int sendDataSize = sizeof(int32_t) + sizeof(int64_t); + { + Vector *tParts = transaction->getParts(); + uint tPartsSize = tParts->size(); + for (uint i = 0; i < tPartsSize; i++) { + TransactionPart *part = tParts->get(i); + sendDataSize += part->getSize(); } - - // Clear all the new transaction parts in preparation for the next time the server sends slots - newTransactionParts.clear(); } + int64_t lastArbitrationDataLocalSequenceNumber = (int64_t) -1; + if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->contains(transaction->getArbitrator())) { + lastArbitrationDataLocalSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->get(transaction->getArbitrator()); + } - int64_t lastSeqNumArbOn = 0; - - void arbitrateFromServer() { + // Make the send data size + Array *sendData = new Array(sendDataSize); + ByteBuffer *bbEncode = ByteBuffer_wrap(sendData); - if (liveTransactionBySequenceNumberTable.size() == 0) { - // Nothing to arbitrate on so move on - return; + // Encode the data + bbEncode->putLong(lastArbitrationDataLocalSequenceNumber); + bbEncode->putInt(transaction->getParts()->size()); + { + Vector *tParts = transaction->getParts(); + uint tPartsSize = tParts->size(); + for (uint i = 0; i < tPartsSize; i++) { + TransactionPart *part = tParts->get(i); + part->encode(bbEncode); } + } - // Get the transaction sequence numbers and sort from oldest to newest - List transactionSequenceNumbers = new ArrayList(liveTransactionBySequenceNumberTable.keySet()); - Collections.sort(transactionSequenceNumbers); - - // Collection of key value pairs that are - Map speculativeTableTmp = new HashMap(); - - // The last transaction arbitrated on - int64_t lastTransactionCommitted = -1; - Set generatedAborts = new HashSet(); + // Send by local + Array *returnData = cloud->sendLocalData(sendData, localSequenceNumber, localCommunicationInformation->getFirst(), localCommunicationInformation->getSecond()); + localSequenceNumber++; - for (Long transactionSequenceNumber : transactionSequenceNumbers) { - Transaction transaction = liveTransactionBySequenceNumberTable.get(transactionSequenceNumber); + if (returnData == NULL) { + // Could not contact server + return Pair(true, false); + } + // Decode the data + ByteBuffer *bbDecode = ByteBuffer_wrap(returnData); + bool didCommit = bbDecode->get() == 1; + bool couldArbitrate = bbDecode->get() == 1; + int numberOfEntries = bbDecode->getInt(); + bool foundAbort = false; + for (int i = 0; i < numberOfEntries; i++) { + char type = bbDecode->get(); + if (type == TypeAbort) { + Abort *abort = (Abort *)Abort_decode(NULL, bbDecode); - // Check if this machine arbitrates for this transaction if not then we cant arbitrate this transaction - if (transaction.getArbitrator() != localMachineId) { - continue; + if ((abort->getTransactionMachineId() == localMachineId) && (abort->getTransactionClientLocalSequenceNumber() == transaction->getClientLocalSequenceNumber())) { + foundAbort = true; } - if (transactionSequenceNumber < lastSeqNumArbOn) { - continue; - } + processEntry(abort); + } else if (type == TypeCommitPart) { + CommitPart *commitPart = (CommitPart *)CommitPart_decode(NULL, bbDecode); + processEntry(commitPart); + } + } - if (offlineTransactionsCommittedAndAtServer.contains(transaction.getId())) { - // We have seen this already locally so dont commit again - continue; - } + updateLiveStateFromLocal(); + if (couldArbitrate) { + TransactionStatus *status = transaction->getTransactionStatus(); + if (didCommit) { + status->setStatus(TransactionStatus_StatusCommitted); + } else { + status->setStatus(TransactionStatus_StatusAborted); + } + } else { + TransactionStatus *status = transaction->getTransactionStatus(); + if (foundAbort) { + status->setStatus(TransactionStatus_StatusAborted); + } else { + status->setStatus(TransactionStatus_StatusCommitted); + } + } - if (!transaction.isComplete()) { - // Will arbitrate in incorrect order if we continue so just break - // Most likely this - break; - } + return Pair(false, true); +} +Array *Table::acceptDataFromLocal(Array *data) { + // Decode the data + ByteBuffer *bbDecode = ByteBuffer_wrap(data); + int64_t lastArbitratedSequenceNumberSeen = bbDecode->getLong(); + int numberOfParts = bbDecode->getInt(); - // update the largest transaction seen by arbitrator from server - if (lastTransactionSeenFromMachineFromServer.get(transaction.getMachineId()) == NULL) { - lastTransactionSeenFromMachineFromServer.put(transaction.getMachineId(), transaction.getClientLocalSequenceNumber()); - } else { - Long lastTransactionSeenFromMachine = lastTransactionSeenFromMachineFromServer.get(transaction.getMachineId()); - if (transaction.getClientLocalSequenceNumber() > lastTransactionSeenFromMachine) { - lastTransactionSeenFromMachineFromServer.put(transaction.getMachineId(), transaction.getClientLocalSequenceNumber()); - } - } + // If we did commit a transaction or not + bool didCommit = false; + bool couldArbitrate = false; - if (transaction.evaluateGuard(committedKeyValueTable, speculativeTableTmp, NULL)) { - // Guard evaluated as true + if (numberOfParts != 0) { - // Update the local changes so we can make the commit - for (KeyValue kv : transaction.getKeyValueUpdateSet()) { - speculativeTableTmp.put(kv.getKey(), kv); - } + // decode the transaction + Transaction *transaction = new Transaction(); + for (int i = 0; i < numberOfParts; i++) { + bbDecode->get(); + TransactionPart *newPart = (TransactionPart *)TransactionPart_decode(NULL, bbDecode); + transaction->addPartDecode(newPart); + } - // Update what the last transaction committed was for use in batch commit - lastTransactionCommitted = transactionSequenceNumber; - } else { - // Guard evaluated was false so create abort + // Arbitrate on transaction and pull relevant return data + Pair localArbitrateReturn = arbitrateOnLocalTransaction(transaction); + couldArbitrate = localArbitrateReturn.getFirst(); + didCommit = localArbitrateReturn.getSecond(); - // Create the abort - Abort newAbort = new Abort(NULL, - transaction.getClientLocalSequenceNumber(), - transaction.getSequenceNumber(), - transaction.getMachineId(), - transaction.getArbitrator(), - localArbitrationSequenceNumber); - localArbitrationSequenceNumber++; + updateLiveStateFromLocal(); - generatedAborts.add(newAbort); + // Transaction was sent to the server so keep track of it to prevent double commit + if (transaction->getSequenceNumber() != -1) { + offlineTransactionsCommittedAndAtServer->add(new Pair(transaction->getId())); + } + } - // Insert the abort so we can process - processEntry(newAbort); - } + // The data to send back + int returnDataSize = 0; + Vector *unseenArbitrations = new Vector(); + + // Get the aborts to send back + Vector *abortLocalSequenceNumbers = new Vector(); + { + SetIterator *abortit = getKeyIterator(liveAbortsGeneratedByLocal); + while (abortit->hasNext()) + abortLocalSequenceNumbers->add(abortit->next()); + delete abortit; + } - lastSeqNumArbOn = transactionSequenceNumber; + qsort(abortLocalSequenceNumbers->expose(), abortLocalSequenceNumbers->size(), sizeof(int64_t), compareInt64); - // liveTransactionBySequenceNumberTable.remove(transactionSequenceNumber); + uint asize = abortLocalSequenceNumbers->size(); + for (uint i = 0; i < asize; i++) { + int64_t localSequenceNumber = abortLocalSequenceNumbers->get(i); + if (localSequenceNumber <= lastArbitratedSequenceNumberSeen) { + continue; } - Commit newCommit = NULL; + Abort *abort = liveAbortsGeneratedByLocal->get(localSequenceNumber); + unseenArbitrations->add(abort); + returnDataSize += abort->getSize(); + } - // If there is something to commit - if (speculativeTableTmp.size() != 0) { + // Get the commits to send back + Hashtable *commitForClientTable = liveCommitsTable->get(localMachineId); + if (commitForClientTable != NULL) { + Vector *commitLocalSequenceNumbers = new Vector(); + { + SetIterator *commitit = getKeyIterator(commitForClientTable); + while (commitit->hasNext()) + commitLocalSequenceNumbers->add(commitit->next()); + delete commitit; + } + qsort(commitLocalSequenceNumbers->expose(), commitLocalSequenceNumbers->size(), sizeof(int64_t), compareInt64); - // Create the commit and increment the commit sequence number - newCommit = new Commit(localArbitrationSequenceNumber, localMachineId, lastTransactionCommitted); - localArbitrationSequenceNumber++; + uint clsSize = commitLocalSequenceNumbers->size(); + for (uint clsi = 0; clsi < clsSize; clsi++) { + int64_t localSequenceNumber = commitLocalSequenceNumbers->get(clsi); + Commit *commit = commitForClientTable->get(localSequenceNumber); - // Add all the new keys to the commit - for (KeyValue kv : speculativeTableTmp.values()) { - newCommit.addKV(kv); + if (localSequenceNumber <= lastArbitratedSequenceNumberSeen) { + continue; } - // create the commit parts - newCommit.createCommitParts(); - - // Append all the commit parts to the end of the pending queue waiting for sending to the server - - // Insert the commit so we can process it - for (CommitPart commitPart : newCommit.getParts().values()) { - processEntry(commitPart); + { + Vector *parts = commit->getParts(); + uint nParts = parts->size(); + for (uint i = 0; i < nParts; i++) { + CommitPart *commitPart = parts->get(i); + unseenArbitrations->add(commitPart); + returnDataSize += commitPart->getSize(); + } } } + } - if ((newCommit != NULL) || (generatedAborts.size() > 0)) { - ArbitrationRound arbitrationRound = new ArbitrationRound(newCommit, generatedAborts); - pendingSendArbitrationRounds.add(arbitrationRound); + // Number of arbitration entries to decode + returnDataSize += 2 * sizeof(int32_t); - if (compactArbitrationData()) { - ArbitrationRound newArbitrationRound = pendingSendArbitrationRounds.get(pendingSendArbitrationRounds.size() - 1); - if (newArbitrationRound.getCommit() != NULL) { - for (CommitPart commitPart : newArbitrationRound.getCommit().getParts().values()) { - processEntry(commitPart); - } - } - } - } + // bool of did commit or not + if (numberOfParts != 0) { + returnDataSize += sizeof(char); } - Pair arbitrateOnLocalTransaction(Transaction transaction) { + // Data to send Back + Array *returnData = new Array(returnDataSize); + ByteBuffer *bbEncode = ByteBuffer_wrap(returnData); - // Check if this machine arbitrates for this transaction if not then we cant arbitrate this transaction - if (transaction.getArbitrator() != localMachineId) { - return new Pair(false, false); + if (numberOfParts != 0) { + if (didCommit) { + bbEncode->put((char)1); + } else { + bbEncode->put((char)0); } - - if (!transaction.isComplete()) { - // Will arbitrate in incorrect order if we continue so just break - // Most likely this - return new Pair(false, false); + if (couldArbitrate) { + bbEncode->put((char)1); + } else { + bbEncode->put((char)0); } + } - if (transaction.getMachineId() != localMachineId) { - // dont do this check for local transactions - if (lastTransactionSeenFromMachineFromServer.get(transaction.getMachineId()) != NULL) { - if (lastTransactionSeenFromMachineFromServer.get(transaction.getMachineId()) > transaction.getClientLocalSequenceNumber()) { - // We've have already seen this from the server - return new Pair(false, false); - } - } - } + bbEncode->putInt(unseenArbitrations->size()); + uint size = unseenArbitrations->size(); + for (uint i = 0; i < size; i++) { + Entry *entry = unseenArbitrations->get(i); + entry->encode(bbEncode); + } - if (transaction.evaluateGuard(committedKeyValueTable, NULL, NULL)) { - // Guard evaluated as true + localSequenceNumber++; + return returnData; +} - // Create the commit and increment the commit sequence number - Commit newCommit = new Commit(localArbitrationSequenceNumber, localMachineId, -1); - localArbitrationSequenceNumber++; +/** Checks whether a given slot was sent using new slots in + array. Returns true if sent and false otherwise. */ - // Update the local changes so we can make the commit - for (KeyValue kv : transaction.getKeyValueUpdateSet()) { - newCommit.addKV(kv); +bool Table::checkSend(Array * array, Slot *checkSlot) { + uint size = array->length(); + for (uint i = 0; i < size; i++) { + Slot *s = array->get(i); + if ((s->getSequenceNumber() == checkSlot->getSequenceNumber()) && (s->getMachineID() == localMachineId)) { + return true; + } + } + + //Also need to see if other machines acknowledged our message + for (uint i = 0; i < size; i++) { + Slot *s = array->get(i); + + // Process each entry in the slot + Vector *entries = s->getEntries(); + uint eSize = entries->size(); + for (uint ei = 0; ei < eSize; ei++) { + Entry *entry = entries->get(ei); + + if (entry->getType() == TypeLastMessage) { + LastMessage *lastMessage = (LastMessage *)entry; + + if ((lastMessage->getMachineID() == localMachineId) && (lastMessage->getSequenceNumber() == checkSlot->getSequenceNumber())) { + return true; + } } + } + } + //Not found + return false; +} - // create the commit parts - newCommit.createCommitParts(); +/** Method tries to send slot to server. Returns status in tuple. + isInserted returns whether last un-acked send (if any) was + successful. Returns whether send was confirmed.x + */ - // Append all the commit parts to the end of the pending queue waiting for sending to the server - ArbitrationRound arbitrationRound = new ArbitrationRound(newCommit, new HashSet()); - pendingSendArbitrationRounds.add(arbitrationRound); +bool Table::sendSlotsToServer(Slot *slot, int newSize, bool isNewKey, bool *isInserted, Array **array) { + attemptedToSendToServer = true; - if (compactArbitrationData()) { - ArbitrationRound newArbitrationRound = pendingSendArbitrationRounds.get(pendingSendArbitrationRounds.size() - 1); - for (CommitPart commitPart : newArbitrationRound.getCommit().getParts().values()) { - processEntry(commitPart); - } - } else { - // Insert the commit so we can process it - for (CommitPart commitPart : newCommit.getParts().values()) { - processEntry(commitPart); - } - } + *array = cloud->putSlot(slot, newSize); + if (*array == NULL) { + *array = new Array(1); + (*array)->set(0, slot); + rejectedSlotVector->clear(); + *isInserted = false; + return true; + } else { + if ((*array)->length() == 0) { + throw new Error("Server Error: Did not send any slots"); + } - if (transaction.getMachineId() == localMachineId) { - TransactionStatus status = transaction.getTransactionStatus(); - if (status != NULL) { - status.setStatus(TransactionStatus.StatusCommitted); - } - } + if (hadPartialSendToServer) { + *isInserted = checkSend(*array, slot); - updateLiveStateFromLocal(); - return new Pair(true, true); + if (!(*isInserted)) { + rejectedSlotVector->add(slot->getSequenceNumber()); + } + + return false; } else { + rejectedSlotVector->add(slot->getSequenceNumber()); + *isInserted = false; + return false; + } + } +} - if (transaction.getMachineId() == localMachineId) { - // For locally created messages update the status - - // Guard evaluated was false so create abort - TransactionStatus status = transaction.getTransactionStatus(); - if (status != NULL) { - status.setStatus(TransactionStatus.StatusAborted); - } - } else { - Set addAbortSet = new HashSet(); - +/** + * Returns true if a resize was needed but not done. + */ +bool Table::fillSlot(Slot *slot, bool resize, NewKey *newKeyEntry, int & newSize, bool & insertedKey) { + newSize = 0;//special value to indicate no resize + if (liveSlotCount > bufferResizeThreshold) { + resize = true;//Resize is forced + } - // Create the abort - Abort newAbort = new Abort(NULL, - transaction.getClientLocalSequenceNumber(), - -1, - transaction.getMachineId(), - transaction.getArbitrator(), - localArbitrationSequenceNumber); - localArbitrationSequenceNumber++; + if (resize) { + newSize = (int) (numberOfSlots * Table_RESIZE_MULTIPLE); + TableStatus *status = new TableStatus(slot, newSize); + slot->addEntry(status); + } - addAbortSet.add(newAbort); + // Fill with rejected slots first before doing anything else + doRejectedMessages(slot); + // Do mandatory rescue of entries + ThreeTuple mandatoryRescueReturn = doMandatoryResuce(slot, resize); - // Append all the commit parts to the end of the pending queue waiting for sending to the server - ArbitrationRound arbitrationRound = new ArbitrationRound(NULL, addAbortSet); - pendingSendArbitrationRounds.add(arbitrationRound); + // Extract working variables + bool needsResize = mandatoryRescueReturn.getFirst(); + bool seenLiveSlot = mandatoryRescueReturn.getSecond(); + int64_t currentRescueSequenceNumber = mandatoryRescueReturn.getThird(); - if (compactArbitrationData()) { - ArbitrationRound newArbitrationRound = pendingSendArbitrationRounds.get(pendingSendArbitrationRounds.size() - 1); - for (CommitPart commitPart : newArbitrationRound.getCommit().getParts().values()) { - processEntry(commitPart); - } - } - } + if (needsResize && !resize) { + // We need to resize but we are not resizing so return true to force on retry + return true; + } - updateLiveStateFromLocal(); - return new Pair(true, false); + insertedKey = false; + if (newKeyEntry != NULL) { + newKeyEntry->setSlot(slot); + if (slot->hasSpace(newKeyEntry)) { + slot->addEntry(newKeyEntry); + insertedKey = true; } } - /** - * 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 - */ - bool compactArbitrationData() { + // Clear the transactions, aborts and commits that were sent previously + transactionPartsSent->clear(); + pendingSendArbitrationEntriesToDelete->clear(); + uint size = pendingSendArbitrationRounds->size(); + for (uint i = 0; i < size; i++) { + ArbitrationRound *round = pendingSendArbitrationRounds->get(i); + bool isFull = false; + round->generateParts(); + Vector *parts = round->getParts(); + + // Insert pending arbitration data + uint vsize = parts->size(); + for (uint vi = 0; vi < vsize; vi++) { + Entry *arbitrationData = parts->get(vi); + + // If it is an abort then we need to set some information + if (arbitrationData->getType() == TypeAbort) { + ((Abort *)arbitrationData)->setSequenceNumber(slot->getSequenceNumber()); + } + + if (!slot->hasSpace(arbitrationData)) { + // No space so cant do anything else with these data entries + isFull = true; + break; + } - if (pendingSendArbitrationRounds.size() < 2) { - // Nothing to compact so do nothing - return false; + // Add to this current slot and add it to entries to delete + slot->addEntry(arbitrationData); + pendingSendArbitrationEntriesToDelete->add(arbitrationData); } - ArbitrationRound lastRound = pendingSendArbitrationRounds.get(pendingSendArbitrationRounds.size() - 1); - if (lastRound.didSendPart()) { - return false; + if (isFull) { + break; } + } - bool hadCommit = (lastRound.getCommit() == NULL); - bool gotNewCommit = false; - - int numberToDelete = 1; - while (numberToDelete < pendingSendArbitrationRounds.size()) { - ArbitrationRound round = pendingSendArbitrationRounds.get(pendingSendArbitrationRounds.size() - numberToDelete - 1); + if (pendingTransactionQueue->size() > 0) { + Transaction *transaction = pendingTransactionQueue->get(0); + // Set the transaction sequence number if it has yet to be inserted into the block chain + if ((!transaction->didSendAPartToServer()) || (transaction->getSequenceNumber() == -1)) { + transaction->setSequenceNumber(slot->getSequenceNumber()); + } - if (round.isFull() || round.didSendPart()) { - // Stop since there is a part that cannot be compacted and we need to compact in order + while (true) { + TransactionPart *part = transaction->getNextPartToSend(); + if (part == NULL) { + // Ran out of parts to send for this transaction so move on break; } - if (round.getCommit() == NULL) { - - // Try compacting aborts only - int newSize = round.getCurrentSize() + lastRound.getAbortsCount(); - if (newSize > ArbitrationRound.MAX_PARTS) { - // Cant compact since it would be too large - break; + if (slot->hasSpace(part)) { + slot->addEntry(part); + Vector *partsSent = transactionPartsSent->get(transaction); + if (partsSent == NULL) { + partsSent = new Vector(); + transactionPartsSent->put(transaction, partsSent); } - lastRound.addAborts(round.getAborts()); + partsSent->add(part->getPartNumber()); + transactionPartsSent->put(transaction, partsSent); } else { + break; + } + } + } - // Create a new larger commit - Commit newCommit = Commit.merge(lastRound.getCommit(), round.getCommit(), localArbitrationSequenceNumber); - localArbitrationSequenceNumber++; - - // Create the commit parts so that we can count them - newCommit.createCommitParts(); + // Fill the remainder of the slot with rescue data + doOptionalRescue(slot, seenLiveSlot, currentRescueSequenceNumber, resize); - // Calculate the new size of the parts - int newSize = newCommit.getNumberOfParts(); - newSize += lastRound.getAbortsCount(); - newSize += round.getAbortsCount(); + return false; +} - if (newSize > ArbitrationRound.MAX_PARTS) { - // Cant compact since it would be too large +void Table::doRejectedMessages(Slot *s) { + if (!rejectedSlotVector->isEmpty()) { + /* TODO: We should avoid generating a rejected message entry if + * there is already a sufficient entry in the queue (e->g->, + * equalsto value of true and same sequence number)-> */ + + int64_t old_seqn = rejectedSlotVector->get(0); + if (rejectedSlotVector->size() > Table_REJECTED_THRESHOLD) { + int64_t new_seqn = rejectedSlotVector->lastElement(); + RejectedMessage *rm = new RejectedMessage(s, s->getSequenceNumber(), localMachineId, old_seqn, new_seqn, false); + s->addEntry(rm); + } else { + int64_t prev_seqn = -1; + uint i = 0; + /* Go through list of missing messages */ + for (; i < rejectedSlotVector->size(); i++) { + int64_t curr_seqn = rejectedSlotVector->get(i); + Slot *s_msg = buffer->getSlot(curr_seqn); + if (s_msg != NULL) break; - } - - // Set the new compacted part - lastRound.setCommit(newCommit); - lastRound.addAborts(round.getAborts()); - gotNewCommit = true; + prev_seqn = curr_seqn; + } + /* Generate rejected message entry for missing messages */ + if (prev_seqn != -1) { + RejectedMessage *rm = new RejectedMessage(s, s->getSequenceNumber(), localMachineId, old_seqn, prev_seqn, false); + s->addEntry(rm); + } + /* Generate rejected message entries for present messages */ + for (; i < rejectedSlotVector->size(); i++) { + int64_t curr_seqn = rejectedSlotVector->get(i); + Slot *s_msg = buffer->getSlot(curr_seqn); + int64_t machineid = s_msg->getMachineID(); + RejectedMessage *rm = new RejectedMessage(s, s->getSequenceNumber(), machineid, curr_seqn, curr_seqn, true); + s->addEntry(rm); } + } + } +} + +ThreeTuple Table::doMandatoryResuce(Slot *slot, bool resize) { + int64_t newestSequenceNumber = buffer->getNewestSeqNum(); + int64_t oldestSequenceNumber = buffer->getOldestSeqNum(); + if (oldestLiveSlotSequenceNumver < oldestSequenceNumber) { + oldestLiveSlotSequenceNumver = oldestSequenceNumber; + } + + int64_t currentSequenceNumber = oldestLiveSlotSequenceNumver; + bool seenLiveSlot = false; + int64_t firstIfFull = newestSequenceNumber + 1 - numberOfSlots; // smallest seq number in the buffer if it is full + int64_t threshold = firstIfFull + Table_FREE_SLOTS; // we want the buffer to be clear of live entries up to this point - numberToDelete++; + + // Mandatory Rescue + for (; currentSequenceNumber < threshold; currentSequenceNumber++) { + Slot *previousSlot = buffer->getSlot(currentSequenceNumber); + // Push slot number forward + if (!seenLiveSlot) { + oldestLiveSlotSequenceNumver = currentSequenceNumber; } - if (numberToDelete != 1) { - // If there is a compaction + if (!previousSlot->isLive()) { + continue; + } - // Delete the previous pieces that are now in the new compacted piece - if (numberToDelete == pendingSendArbitrationRounds.size()) { - pendingSendArbitrationRounds.clear(); - } else { - for (int i = 0; i < numberToDelete; i++) { - pendingSendArbitrationRounds.remove(pendingSendArbitrationRounds.size() - 1); - } - } + // We have seen a live slot + seenLiveSlot = true; - // Add the new compacted into the pending to send list - pendingSendArbitrationRounds.add(lastRound); + // Get all the live entries for a slot + Vector *liveEntries = previousSlot->getLiveEntries(resize); - // Should reinsert into the commit processor - if (hadCommit && gotNewCommit) { - return true; + // Iterate over all the live entries and try to rescue them + uint lESize = liveEntries->size(); + for (uint i = 0; i < lESize; i++) { + Entry *liveEntry = liveEntries->get(i); + if (slot->hasSpace(liveEntry)) { + // Enough space to rescue the entry + slot->addEntry(liveEntry); + } else if (currentSequenceNumber == firstIfFull) { + //if there's no space but the entry is about to fall off the queue + return ThreeTuple(true, seenLiveSlot, currentSequenceNumber); } } - - return false; } - // bool compactArbitrationData() { - // return false; - // } - /** - * Update all the commits and the committed tables, sets dead the dead transactions - */ - bool updateCommittedTable() { + // Did not resize + return ThreeTuple(false, seenLiveSlot, currentSequenceNumber); +} - if (newCommitParts.size() == 0) { - // Nothing new to process - return false; +void Table::doOptionalRescue(Slot *s, bool seenliveslot, int64_t seqn, bool resize) { + /* now go through live entries from least to greatest sequence number until + * either all live slots added, or the slot doesn't have enough room + * for SKIP_THRESHOLD consecutive entries*/ + int skipcount = 0; + int64_t newestseqnum = buffer->getNewestSeqNum(); + for (; seqn <= newestseqnum; seqn++) { + Slot *prevslot = buffer->getSlot(seqn); + //Push slot number forward + if (!seenliveslot) + oldestLiveSlotSequenceNumver = seqn; + + if (!prevslot->isLive()) + continue; + seenliveslot = true; + Vector *liveentries = prevslot->getLiveEntries(resize); + uint lESize = liveentries->size(); + for (uint i = 0; i < lESize; i++) { + Entry *liveentry = liveentries->get(i); + if (s->hasSpace(liveentry)) + s->addEntry(liveentry); + else { + skipcount++; + if (skipcount > Table_SKIP_THRESHOLD) { + delete liveentries; + goto donesearch; + } + } } + delete liveentries; + } +donesearch: + ; +} - // Iterate through all the machine Ids that we received new parts for - for (Long machineId : newCommitParts.keySet()) { - Map, CommitPart> parts = newCommitParts.get(machineId); +/** + * Checks for malicious activity and updates the local copy of the block chain-> + */ +void Table::validateAndUpdate(Array *newSlots, bool acceptUpdatesToLocal) { + // The cloud communication layer has checked slot HMACs already + // before decoding + if (newSlots->length() == 0) { + return; + } - // Iterate through all the parts for that machine Id - for (Pair partId : parts.keySet()) { - CommitPart part = parts.get(partId); + // Make sure all slots are newer than the last largest slot this + // client has seen + int64_t firstSeqNum = newSlots->get(0)->getSequenceNumber(); + if (firstSeqNum <= sequenceNumber) { + throw new Error("Server Error: Sent older slots!"); + } - // Get the transaction object for that sequence number - Map commitForClientTable = liveCommitsTable.get(part.getMachineId()); + // Create an object that can access both new slots and slots in our + // local chain without committing slots to our local chain + SlotIndexer *indexer = new SlotIndexer(newSlots, buffer); - if (commitForClientTable == NULL) { - // This is the first commit from this device - commitForClientTable = new HashMap(); - liveCommitsTable.put(part.getMachineId(), commitForClientTable); - } + // Check that the HMAC chain is not broken + checkHMACChain(indexer, newSlots); - Commit commit = commitForClientTable.get(part.getSequenceNumber()); + // Set to keep track of messages from clients + Hashset *machineSet = new Hashset(); + { + SetIterator *> *lmit = getKeyIterator(lastMessageTable); + while (lmit->hasNext()) + machineSet->add(lmit->next()); + delete lmit; + } - if (commit == NULL) { - // This is a new commit that we dont have so make a new one - commit = new Commit(); + // Process each slots data + { + uint numSlots = newSlots->length(); + for (uint i = 0; i < numSlots; i++) { + Slot *slot = newSlots->get(i); + processSlot(indexer, slot, acceptUpdatesToLocal, machineSet); + updateExpectedSize(); + } + } + delete indexer; + + // If there is a gap, check to see if the server sent us + // everything-> + if (firstSeqNum != (sequenceNumber + 1)) { + + // Check the size of the slots that were sent down by the server-> + // Can only check the size if there was a gap + checkNumSlots(newSlots->length()); + + // Since there was a gap every machine must have pushed a slot or + // must have a last message message-> If not then the server is + // hiding slots + if (!machineSet->isEmpty()) { + delete machineSet; + throw new Error("Missing record for machines: "); + } + } + delete machineSet; + // Update the size of our local block chain-> + commitNewMaxSize(); - // Insert this new commit into the live tables - commitForClientTable.put(part.getSequenceNumber(), commit); - } + // Commit new to slots to the local block chain-> + { + uint numSlots = newSlots->length(); + for (uint i = 0; i < numSlots; i++) { + Slot *slot = newSlots->get(i); - // Add that part to the commit - commit.addPartDecode(part); - } - } + // Insert this slot into our local block chain copy-> + buffer->putSlot(slot); - // Clear all the new commits parts in preparation for the next time the server sends slots - newCommitParts.clear(); + // Keep track of how many slots are currently live (have live data + // in them)-> + liveSlotCount++; + } + } + // Get the sequence number of the latest slot in the system + sequenceNumber = newSlots->get(newSlots->length() - 1)->getSequenceNumber(); + updateLiveStateFromServer(); - // If we process a new commit keep track of it for future use - bool didProcessANewCommit = false; + // No Need to remember after we pulled from the server + offlineTransactionsCommittedAndAtServer->clear(); - // Process the commits one by one - for (Long arbitratorId : liveCommitsTable.keySet()) { + // This is invalidated now + hadPartialSendToServer = false; +} - // Get all the commits for a specific arbitrator - Map commitForClientTable = liveCommitsTable.get(arbitratorId); +void Table::updateLiveStateFromServer() { + // Process the new transaction parts + processNewTransactionParts(); - // Sort the commits in order - List commitSequenceNumbers = new ArrayList(commitForClientTable.keySet()); - Collections.sort(commitSequenceNumbers); + // Do arbitration on new transactions that were received + arbitrateFromServer(); - // Get the last commit seen from this arbitrator - int64_t lastCommitSeenSequenceNumber = -1; - if (lastCommitSeenSequenceNumberByArbitratorTable.get(arbitratorId) != NULL) { - lastCommitSeenSequenceNumber = lastCommitSeenSequenceNumberByArbitratorTable.get(arbitratorId); - } + // Update all the committed keys + bool didCommitOrSpeculate = updateCommittedTable(); - // Go through each new commit one by one - for (int i = 0; i < commitSequenceNumbers.size(); i++) { - Long commitSequenceNumber = commitSequenceNumbers.get(i); - Commit commit = commitForClientTable.get(commitSequenceNumber); + // Delete the transactions that are now dead + updateLiveTransactionsAndStatus(); - // Special processing if a commit is not complete - if (!commit.isComplete()) { - if (i == (commitSequenceNumbers.size() - 1)) { - // 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 - break; - } else { - // This is a commit that was already dead but parts of it are still in the block chain (not flushed out yet). - // Delete it and move on - commit.setDead(); - commitForClientTable.remove(commit.getSequenceNumber()); - continue; - } - } + // Do speculations + didCommitOrSpeculate |= updateSpeculativeTable(didCommitOrSpeculate); + updatePendingTransactionSpeculativeTable(didCommitOrSpeculate); +} - // Update the last transaction that was updated if we can - if (commit.getTransactionSequenceNumber() != -1) { - Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(commit.getMachineId()); +void Table::updateLiveStateFromLocal() { + // Update all the committed keys + bool didCommitOrSpeculate = updateCommittedTable(); - // Update the last transaction sequence number that the arbitrator arbitrated on - if ((lastTransactionNumber == NULL) || (lastTransactionNumber < commit.getTransactionSequenceNumber())) { - lastArbitratedTransactionNumberByArbitratorTable.put(commit.getMachineId(), commit.getTransactionSequenceNumber()); - } - } + // Delete the transactions that are now dead + updateLiveTransactionsAndStatus(); - // Update the last arbitration data that we have seen so far - if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(commit.getMachineId()) != NULL) { + // Do speculations + didCommitOrSpeculate |= updateSpeculativeTable(didCommitOrSpeculate); + updatePendingTransactionSpeculativeTable(didCommitOrSpeculate); +} - int64_t lastArbitrationSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(commit.getMachineId()); - if (commit.getSequenceNumber() > lastArbitrationSequenceNumber) { - // Is larger - lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.put(commit.getMachineId(), commit.getSequenceNumber()); - } - } else { - // Never seen any data from this arbitrator so record the first one - lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.put(commit.getMachineId(), commit.getSequenceNumber()); - } +void Table::initExpectedSize(int64_t firstSequenceNumber, int64_t numberOfSlots) { + int64_t prevslots = firstSequenceNumber; - // We have already seen this commit before so need to do the full processing on this commit - if (commit.getSequenceNumber() <= lastCommitSeenSequenceNumber) { + if (didFindTableStatus) { + } else { + expectedsize = (prevslots < ((int64_t) numberOfSlots)) ? (int) prevslots : numberOfSlots; + } - // Update the last transaction that was updated if we can - if (commit.getTransactionSequenceNumber() != -1) { - Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(commit.getMachineId()); + didFindTableStatus = true; + currMaxSize = numberOfSlots; +} - // Update the last transaction sequence number that the arbitrator arbitrated on - if ((lastTransactionNumber == NULL) || (lastTransactionNumber < commit.getTransactionSequenceNumber())) { - lastArbitratedTransactionNumberByArbitratorTable.put(commit.getMachineId(), commit.getTransactionSequenceNumber()); - } - } +void Table::updateExpectedSize() { + expectedsize++; - continue; - } + if (expectedsize > currMaxSize) { + expectedsize = currMaxSize; + } +} - // If we got here then this is a brand new commit and needs full processing - // Get what commits should be edited, these are the commits that have live values for their keys - Set commitsToEdit = new HashSet(); - for (KeyValue kv : commit.getKeyValueUpdateSet()) { - commitsToEdit.add(liveCommitsByKeyTable.get(kv.getKey())); - } - commitsToEdit.remove(NULL); // remove NULL since it could be in this set +/** + * Check the size of the block chain to make sure there are enough + * slots sent back by the server-> This is only called when we have a + * gap between the slots that we have locally and the slots sent by + * the server therefore in the slots sent by the server there will be + * at least 1 Table status message + */ +void Table::checkNumSlots(int numberOfSlots) { + if (numberOfSlots != expectedsize) { + throw new Error("Server Error: Server did not send all slots-> Expected: "); + } +} - // Update each previous commit that needs to be updated - for (Commit previousCommit : commitsToEdit) { +/** + * Update the size of of the local buffer if it is needed-> + */ +void Table::commitNewMaxSize() { + didFindTableStatus = false; - // Only bother with live commits (TODO: Maybe remove this check) - if (previousCommit.isLive()) { + // Resize the local slot buffer + if (numberOfSlots != currMaxSize) { + buffer->resize((int32_t)currMaxSize); + } - // Update which keys in the old commits are still live - for (KeyValue kv : commit.getKeyValueUpdateSet()) { - previousCommit.invalidateKey(kv.getKey()); - } + // Change the number of local slots to the new size + numberOfSlots = (int32_t)currMaxSize; - // if the commit is now dead then remove it - if (!previousCommit.isLive()) { - commitForClientTable.remove(previousCommit); - } - } - } + // Recalculate the resize threshold since the size of the local + // buffer has changed + setResizeThreshold(); +} - // Update the last seen sequence number from this arbitrator - if (lastCommitSeenSequenceNumberByArbitratorTable.get(commit.getMachineId()) != NULL) { - if (commit.getSequenceNumber() > lastCommitSeenSequenceNumberByArbitratorTable.get(commit.getMachineId())) { - lastCommitSeenSequenceNumberByArbitratorTable.put(commit.getMachineId(), commit.getSequenceNumber()); - } - } else { - lastCommitSeenSequenceNumberByArbitratorTable.put(commit.getMachineId(), commit.getSequenceNumber()); - } +/** + * Process the new transaction parts from this latest round of slots + * received from the server + */ +void Table::processNewTransactionParts() { - // We processed a new commit that we havent seen before - didProcessANewCommit = true; + if (newTransactionParts->size() == 0) { + // Nothing new to process + return; + } - // Update the committed table of keys and which commit is using which key - for (KeyValue kv : commit.getKeyValueUpdateSet()) { - committedKeyValueTable.put(kv.getKey(), kv); - liveCommitsByKeyTable.put(kv.getKey(), commit); + // Iterate through all the machine Ids that we received new parts + // for + SetIterator *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *> *tpit = getKeyIterator(newTransactionParts); + while (tpit->hasNext()) { + int64_t machineId = tpit->next(); + Hashtable *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *parts = newTransactionParts->get(machineId); + + SetIterator *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *ptit = getKeyIterator(parts); + // Iterate through all the parts for that machine Id + while (ptit->hasNext()) { + Pair *partId = ptit->next(); + TransactionPart *part = parts->get(partId); + + if (lastArbitratedTransactionNumberByArbitratorTable->contains(part->getArbitratorId())) { + int64_t lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable->get(part->getArbitratorId()); + if (lastTransactionNumber >= part->getSequenceNumber()) { + // Set dead the transaction part + part->setDead(); + continue; } } - } - return didProcessANewCommit; - } + // Get the transaction object for that sequence number + Transaction *transaction = liveTransactionBySequenceNumberTable->get(part->getSequenceNumber()); - /** - * Create the speculative table from transactions that are still live and have come from the cloud - */ - bool updateSpeculativeTable(bool didProcessNewCommits) { - if (liveTransactionBySequenceNumberTable.keySet().size() == 0) { - // There is nothing to speculate on - return false; - } + if (transaction == NULL) { + // This is a new transaction that we dont have so make a new one + transaction = new Transaction(); + + // Add that part to the transaction + transaction->addPartDecode(part); - // Create a list of the transaction sequence numbers and sort them from oldest to newest - List transactionSequenceNumbersSorted = new ArrayList(liveTransactionBySequenceNumberTable.keySet()); - Collections.sort(transactionSequenceNumbersSorted); + // Insert this new transaction into the live tables + liveTransactionBySequenceNumberTable->put(part->getSequenceNumber(), transaction); + liveTransactionByTransactionIdTable->put(transaction->getId(), transaction); + } + } + delete ptit; + } + delete tpit; + // Clear all the new transaction parts in preparation for the next + // time the server sends slots + { + SetIterator *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *> *partsit = getKeyIterator(newTransactionParts); + while (partsit->hasNext()) { + int64_t machineId = partsit->next(); + Hashtable *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *parts = newTransactionParts->get(machineId); + delete parts; + } + delete partsit; + newTransactionParts->clear(); + } +} - bool hasGapInTransactionSequenceNumbers = transactionSequenceNumbersSorted.get(0) != oldestTransactionSequenceNumberSpeculatedOn; +void Table::arbitrateFromServer() { + if (liveTransactionBySequenceNumberTable->size() == 0) { + // Nothing to arbitrate on so move on + return; + } + // Get the transaction sequence numbers and sort from oldest to newest + Vector *transactionSequenceNumbers = new Vector(); + { + SetIterator *trit = getKeyIterator(liveTransactionBySequenceNumberTable); + while (trit->hasNext()) + transactionSequenceNumbers->add(trit->next()); + delete trit; + } + qsort(transactionSequenceNumbers->expose(), transactionSequenceNumbers->size(), sizeof(int64_t), compareInt64); - if (hasGapInTransactionSequenceNumbers || didProcessNewCommits) { - // If there is a gap in the transaction sequence numbers then there was a commit or an abort of a transaction - // OR there was a new commit (Could be from offline commit) so a redo the speculation from scratch + // Collection of key value pairs that are + Hashtable *speculativeTableTmp = new Hashtable(); - // Start from scratch - speculatedKeyValueTable.clear(); - lastTransactionSequenceNumberSpeculatedOn = -1; - oldestTransactionSequenceNumberSpeculatedOn = -1; + // The last transaction arbitrated on + int64_t lastTransactionCommitted = -1; + Hashset *generatedAborts = new Hashset(); + uint tsnSize = transactionSequenceNumbers->size(); + for (uint i = 0; i < tsnSize; i++) { + int64_t transactionSequenceNumber = transactionSequenceNumbers->get(i); + Transaction *transaction = liveTransactionBySequenceNumberTable->get(transactionSequenceNumber); + // Check if this machine arbitrates for this transaction if not + // then we cant arbitrate this transaction + if (transaction->getArbitrator() != localMachineId) { + continue; } - // Remember the front of the transaction list - oldestTransactionSequenceNumberSpeculatedOn = transactionSequenceNumbersSorted.get(0); - - // Find where to start arbitration from - int startIndex = transactionSequenceNumbersSorted.indexOf(lastTransactionSequenceNumberSpeculatedOn) + 1; - - if (startIndex >= transactionSequenceNumbersSorted.size()) { - // Make sure we are not out of bounds - return false; // did not speculate + if (transactionSequenceNumber < lastSeqNumArbOn) { + continue; } - Set incompleteTransactionArbitrator = new HashSet(); - bool didSkip = true; + if (offlineTransactionsCommittedAndAtServer->contains(transaction->getId())) { + // We have seen this already locally so dont commit again + continue; + } - for (int i = startIndex; i < transactionSequenceNumbersSorted.size(); i++) { - int64_t transactionSequenceNumber = transactionSequenceNumbersSorted.get(i); - Transaction transaction = liveTransactionBySequenceNumberTable.get(transactionSequenceNumber); + if (!transaction->isComplete()) { + // Will arbitrate in incorrect order if we continue so just break + // Most likely this + break; + } - if (!transaction.isComplete()) { - // If there is an incomplete transaction then there is nothing we can do - // add this transactions arbitrator to the list of arbitrators we should ignore - incompleteTransactionArbitrator.add(transaction.getArbitrator()); - didSkip = true; - continue; + // update the largest transaction seen by arbitrator from server + if (!lastTransactionSeenFromMachineFromServer->contains(transaction->getMachineId())) { + lastTransactionSeenFromMachineFromServer->put(transaction->getMachineId(), transaction->getClientLocalSequenceNumber()); + } else { + int64_t lastTransactionSeenFromMachine = lastTransactionSeenFromMachineFromServer->get(transaction->getMachineId()); + if (transaction->getClientLocalSequenceNumber() > lastTransactionSeenFromMachine) { + lastTransactionSeenFromMachineFromServer->put(transaction->getMachineId(), transaction->getClientLocalSequenceNumber()); } + } - if (incompleteTransactionArbitrator.contains(transaction.getArbitrator())) { - continue; + if (transaction->evaluateGuard(committedKeyValueTable, speculativeTableTmp, NULL)) { + // Guard evaluated as true + // Update the local changes so we can make the commit + SetIterator *kvit = transaction->getKeyValueUpdateSet()->iterator(); + while (kvit->hasNext()) { + KeyValue *kv = kvit->next(); + speculativeTableTmp->put(kv->getKey(), kv); } + delete kvit; - lastTransactionSequenceNumberSpeculatedOn = transactionSequenceNumber; + // Update what the last transaction committed was for use in batch commit + lastTransactionCommitted = transactionSequenceNumber; + } else { + // Guard evaluated was false so create abort + // Create the abort + Abort *newAbort = new Abort(NULL, + transaction->getClientLocalSequenceNumber(), + transaction->getSequenceNumber(), + transaction->getMachineId(), + transaction->getArbitrator(), + localArbitrationSequenceNumber); + localArbitrationSequenceNumber++; + generatedAborts->add(newAbort); + + // Insert the abort so we can process + processEntry(newAbort); + } - if (transaction.evaluateGuard(committedKeyValueTable, speculatedKeyValueTable, NULL)) { - // Guard evaluated to true so update the speculative table - for (KeyValue kv : transaction.getKeyValueUpdateSet()) { - speculatedKeyValueTable.put(kv.getKey(), kv); + lastSeqNumArbOn = transactionSequenceNumber; + } + + Commit *newCommit = NULL; + + // If there is something to commit + if (speculativeTableTmp->size() != 0) { + // Create the commit and increment the commit sequence number + newCommit = new Commit(localArbitrationSequenceNumber, localMachineId, lastTransactionCommitted); + localArbitrationSequenceNumber++; + + // Add all the new keys to the commit + SetIterator *spit = getKeyIterator(speculativeTableTmp); + while (spit->hasNext()) { + IoTString *string = spit->next(); + KeyValue *kv = speculativeTableTmp->get(string); + newCommit->addKV(kv); + } + delete spit; + + // create the commit parts + newCommit->createCommitParts(); + + // Append all the commit parts to the end of the pending queue + // waiting for sending to the server + // Insert the commit so we can process it + Vector *parts = newCommit->getParts(); + uint partsSize = parts->size(); + for (uint i = 0; i < partsSize; i++) { + CommitPart *commitPart = parts->get(i); + processEntry(commitPart); + } + } + delete speculativeTableTmp; + + if ((newCommit != NULL) || (generatedAborts->size() > 0)) { + ArbitrationRound *arbitrationRound = new ArbitrationRound(newCommit, generatedAborts); + pendingSendArbitrationRounds->add(arbitrationRound); + + if (compactArbitrationData()) { + ArbitrationRound *newArbitrationRound = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - 1); + if (newArbitrationRound->getCommit() != NULL) { + Vector *parts = newArbitrationRound->getCommit()->getParts(); + uint partsSize = parts->size(); + for (uint i = 0; i < partsSize; i++) { + CommitPart *commitPart = parts->get(i); + processEntry(commitPart); } } } + } +} - if (didSkip) { - // Since there was a skip we need to redo the speculation next time around - lastTransactionSequenceNumberSpeculatedOn = -1; - oldestTransactionSequenceNumberSpeculatedOn = -1; - } +Pair Table::arbitrateOnLocalTransaction(Transaction *transaction) { - // We did some speculation - return true; + // Check if this machine arbitrates for this transaction if not then + // we cant arbitrate this transaction + if (transaction->getArbitrator() != localMachineId) { + return Pair(false, false); } - /** - * Create the pending transaction speculative table from transactions that are still in the pending transaction buffer - */ - void updatePendingTransactionSpeculativeTable(bool didProcessNewCommitsOrSpeculate) { - if (pendingTransactionQueue.size() == 0) { - // There is nothing to speculate on - return; - } - + if (!transaction->isComplete()) { + // Will arbitrate in incorrect order if we continue so just break + // Most likely this + return Pair(false, false); + } - if (didProcessNewCommitsOrSpeculate || (firstPendingTransaction != pendingTransactionQueue.get(0))) { - // need to reset on the pending speculation - lastPendingTransactionSpeculatedOn = NULL; - firstPendingTransaction = pendingTransactionQueue.get(0); - pendingTransactionSpeculatedKeyValueTable.clear(); + if (transaction->getMachineId() != localMachineId) { + // dont do this check for local transactions + if (lastTransactionSeenFromMachineFromServer->contains(transaction->getMachineId())) { + if (lastTransactionSeenFromMachineFromServer->get(transaction->getMachineId()) > transaction->getClientLocalSequenceNumber()) { + // We've have already seen this from the server + return Pair(false, false); + } } + } - // Find where to start arbitration from - int startIndex = pendingTransactionQueue.indexOf(firstPendingTransaction) + 1; + if (transaction->evaluateGuard(committedKeyValueTable, NULL, NULL)) { + // Guard evaluated as true Create the commit and increment the + // commit sequence number + Commit *newCommit = new Commit(localArbitrationSequenceNumber, localMachineId, -1); + localArbitrationSequenceNumber++; + + // Update the local changes so we can make the commit + SetIterator *kvit = transaction->getKeyValueUpdateSet()->iterator(); + while (kvit->hasNext()) { + KeyValue *kv = kvit->next(); + newCommit->addKV(kv); + } + delete kvit; + + // create the commit parts + newCommit->createCommitParts(); + + // Append all the commit parts to the end of the pending queue + // waiting for sending to the server + ArbitrationRound *arbitrationRound = new ArbitrationRound(newCommit, new Hashset()); + pendingSendArbitrationRounds->add(arbitrationRound); + + if (compactArbitrationData()) { + ArbitrationRound *newArbitrationRound = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - 1); + Vector *parts = newArbitrationRound->getCommit()->getParts(); + uint partsSize = parts->size(); + for (uint i = 0; i < partsSize; i++) { + CommitPart *commitPart = parts->get(i); + processEntry(commitPart); + } + } else { + // Insert the commit so we can process it + Vector *parts = newCommit->getParts(); + uint partsSize = parts->size(); + for (uint i = 0; i < partsSize; i++) { + CommitPart *commitPart = parts->get(i); + processEntry(commitPart); + } + } - if (startIndex >= pendingTransactionQueue.size()) { - // Make sure we are not out of bounds - return; + if (transaction->getMachineId() == localMachineId) { + TransactionStatus *status = transaction->getTransactionStatus(); + if (status != NULL) { + status->setStatus(TransactionStatus_StatusCommitted); + } } - for (int i = startIndex; i < pendingTransactionQueue.size(); i++) { - Transaction transaction = pendingTransactionQueue.get(i); + updateLiveStateFromLocal(); + return Pair(true, true); + } else { + if (transaction->getMachineId() == localMachineId) { + // For locally created messages update the status + // Guard evaluated was false so create abort + TransactionStatus *status = transaction->getTransactionStatus(); + if (status != NULL) { + status->setStatus(TransactionStatus_StatusAborted); + } + } else { + Hashset *addAbortSet = new Hashset(); + + // Create the abort + Abort *newAbort = new Abort(NULL, + transaction->getClientLocalSequenceNumber(), + -1, + transaction->getMachineId(), + transaction->getArbitrator(), + localArbitrationSequenceNumber); + localArbitrationSequenceNumber++; + addAbortSet->add(newAbort); + + // Append all the commit parts to the end of the pending queue + // waiting for sending to the server + ArbitrationRound *arbitrationRound = new ArbitrationRound(NULL, addAbortSet); + pendingSendArbitrationRounds->add(arbitrationRound); - lastPendingTransactionSpeculatedOn = transaction; + if (compactArbitrationData()) { + ArbitrationRound *newArbitrationRound = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - 1); - if (transaction.evaluateGuard(committedKeyValueTable, speculatedKeyValueTable, pendingTransactionSpeculatedKeyValueTable)) { - // Guard evaluated to true so update the speculative table - for (KeyValue kv : transaction.getKeyValueUpdateSet()) { - pendingTransactionSpeculatedKeyValueTable.put(kv.getKey(), kv); + Vector *parts = newArbitrationRound->getCommit()->getParts(); + uint partsSize = parts->size(); + for (uint i = 0; i < partsSize; i++) { + CommitPart *commitPart = parts->get(i); + processEntry(commitPart); } } } + + updateLiveStateFromLocal(); + return Pair(true, false); } +} - /** - * Set dead and remove from the live transaction tables the transactions that are dead - */ - void updateLiveTransactionsAndStatus() { +/** + * Compacts the arbitration data by merging commits and aggregating + * aborts so that a single large push of commits can be done instead + * of many small updates + */ +bool Table::compactArbitrationData() { + if (pendingSendArbitrationRounds->size() < 2) { + // Nothing to compact so do nothing + return false; + } - // Go through each of the transactions - for (Iterator> iter = liveTransactionBySequenceNumberTable.entrySet().iterator(); iter.hasNext();) { - Transaction transaction = iter.next().getValue(); + ArbitrationRound *lastRound = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - 1); + if (lastRound->getDidSendPart()) { + return false; + } - // Check if the transaction is dead - Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(transaction.getArbitrator()); - if ((lastTransactionNumber != NULL) && (lastTransactionNumber >= transaction.getSequenceNumber())) { + bool hadCommit = (lastRound->getCommit() == NULL); + bool gotNewCommit = false; - // Set dead the transaction - transaction.setDead(); + uint numberToDelete = 1; + + while (numberToDelete < pendingSendArbitrationRounds->size()) { + ArbitrationRound *round = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - numberToDelete - 1); - // Remove the transaction from the live table - iter.remove(); - liveTransactionByTransactionIdTable.remove(transaction.getId()); - } + if (round->isFull() || round->getDidSendPart()) { + // Stop since there is a part that cannot be compacted and we + // need to compact in order + break; } - // Go through each of the transactions - for (Iterator> iter = outstandingTransactionStatus.entrySet().iterator(); iter.hasNext();) { - TransactionStatus status = iter.next().getValue(); + if (round->getCommit() == NULL) { + // Try compacting aborts only + int newSize = round->getCurrentSize() + lastRound->getAbortsCount(); + if (newSize > ArbitrationRound_MAX_PARTS) { + // Cant compact since it would be too large + break; + } + lastRound->addAborts(round->getAborts()); + } else { + // Create a new larger commit + Commit *newCommit = Commit_merge(lastRound->getCommit(), round->getCommit(), localArbitrationSequenceNumber); + localArbitrationSequenceNumber++; - // Check if the transaction is dead - Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(status.getTransactionArbitrator()); - if ((lastTransactionNumber != NULL) && (lastTransactionNumber >= status.getTransactionSequenceNumber())) { + // Create the commit parts so that we can count them + newCommit->createCommitParts(); - // Set committed - status.setStatus(TransactionStatus.StatusCommitted); + // Calculate the new size of the parts + int newSize = newCommit->getNumberOfParts(); + newSize += lastRound->getAbortsCount(); + newSize += round->getAbortsCount(); - // Remove - iter.remove(); + if (newSize > ArbitrationRound_MAX_PARTS) { + // Can't compact since it would be too large + if (lastRound->getCommit() != newCommit && + round->getCommit() != newCommit) + delete newCommit; + break; } + // Set the new compacted part + if (lastRound->getCommit() == newCommit) + lastRound->setCommit(NULL); + if (round->getCommit() == newCommit) + round->setCommit(NULL); + + if (lastRound->getCommit() != NULL) { + Commit * oldcommit = lastRound->getCommit(); + lastRound->setCommit(NULL); + delete oldcommit; + } + lastRound->setCommit(newCommit); + lastRound->addAborts(round->getAborts()); + gotNewCommit = true; } - } - /** - * Process this slot, entry by entry. Also update the latest message sent by slot - */ - void processSlot(SlotIndexer indexer, Slot slot, bool acceptUpdatesToLocal, HashSet machineSet) { - - // Update the last message seen - updateLastMessage(slot.getMachineID(), slot.getSequenceNumber(), slot, acceptUpdatesToLocal, machineSet); + numberToDelete++; + } - // Process each entry in the slot - for (Entry entry : slot.getEntries()) { - switch (entry.getType()) { + if (numberToDelete != 1) { + // If there is a compaction + // Delete the previous pieces that are now in the new compacted piece + for (uint i = 2; i <= numberToDelete; i++) { + delete pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size()-i); + } + pendingSendArbitrationRounds->setSize(pendingSendArbitrationRounds->size() - numberToDelete); - case Entry.TypeCommitPart: - processEntry((CommitPart)entry); - break; + pendingSendArbitrationRounds->add(lastRound); - case Entry.TypeAbort: - processEntry((Abort)entry); - break; + // Should reinsert into the commit processor + if (hadCommit && gotNewCommit) { + return true; + } + } - case Entry.TypeTransactionPart: - processEntry((TransactionPart)entry); - break; + return false; +} - case Entry.TypeNewKey: - processEntry((NewKey)entry); - break; +/** + * Update all the commits and the committed tables, sets dead the dead + * transactions + */ +bool Table::updateCommittedTable() { + if (newCommitParts->size() == 0) { + // Nothing new to process + return false; + } - case Entry.TypeLastMessage: - processEntry((LastMessage)entry, machineSet); - break; + // Iterate through all the machine Ids that we received new parts for + SetIterator *, CommitPart *, uintptr_t, 0, pairHashFunction, pairEquals> *> *partsit = getKeyIterator(newCommitParts); + while (partsit->hasNext()) { + int64_t machineId = partsit->next(); + Hashtable *, CommitPart *, uintptr_t, 0, pairHashFunction, pairEquals> *parts = newCommitParts->get(machineId); - case Entry.TypeRejectedMessage: - processEntry((RejectedMessage)entry, indexer); - break; + // Iterate through all the parts for that machine Id + SetIterator *, CommitPart *, uintptr_t, 0, pairHashFunction, pairEquals> *pairit = getKeyIterator(parts); + while (pairit->hasNext()) { + Pair *partId = pairit->next(); + CommitPart *part = parts->get(partId); - case Entry.TypeTableStatus: - processEntry((TableStatus)entry, slot.getSequenceNumber()); - break; + // Get the transaction object for that sequence number + Hashtable *commitForClientTable = liveCommitsTable->get(part->getMachineId()); - default: - throw new Error("Unrecognized type: " + entry.getType()); + if (commitForClientTable == NULL) { + // This is the first commit from this device + commitForClientTable = new Hashtable(); + liveCommitsTable->put(part->getMachineId(), commitForClientTable); } - } - } - /** - * Update the last message that was sent for a machine Id - */ - void processEntry(LastMessage entry, HashSet machineSet) { - // Update what the last message received by a machine was - updateLastMessage(entry.getMachineID(), entry.getSequenceNumber(), entry, false, machineSet); - } + Commit *commit = commitForClientTable->get(part->getSequenceNumber()); - /** - * Add the new key to the arbitrators table and update the set of live new keys (in case of a rescued new key message) - */ - void processEntry(NewKey entry) { + if (commit == NULL) { + // This is a new commit that we dont have so make a new one + commit = new Commit(); - // Update the arbitrator table with the new key information - arbitratorTable.put(entry.getKey(), entry.getMachineID()); + // Insert this new commit into the live tables + commitForClientTable->put(part->getSequenceNumber(), commit); + } - // Update what the latest live new key is - NewKey oldNewKey = liveNewKeyTable.put(entry.getKey(), entry); - if (oldNewKey != NULL) { - // Delete the old new key messages - oldNewKey.setDead(); + // Add that part to the commit + commit->addPartDecode(part); } + delete pairit; + delete parts; } + delete partsit; + + // Clear all the new commits parts in preparation for the next time + // the server sends slots + newCommitParts->clear(); + + // If we process a new commit keep track of it for future use + bool didProcessANewCommit = false; + + // Process the commits one by one + SetIterator *> *liveit = getKeyIterator(liveCommitsTable); + while (liveit->hasNext()) { + int64_t arbitratorId = liveit->next(); + // Get all the commits for a specific arbitrator + Hashtable *commitForClientTable = liveCommitsTable->get(arbitratorId); + + // Sort the commits in order + Vector *commitSequenceNumbers = new Vector(); + { + SetIterator *clientit = getKeyIterator(commitForClientTable); + while (clientit->hasNext()) + commitSequenceNumbers->add(clientit->next()); + delete clientit; + } + + qsort(commitSequenceNumbers->expose(), commitSequenceNumbers->size(), sizeof(int64_t), compareInt64); + + // Get the last commit seen from this arbitrator + int64_t lastCommitSeenSequenceNumber = -1; + if (lastCommitSeenSequenceNumberByArbitratorTable->contains(arbitratorId)) { + lastCommitSeenSequenceNumber = lastCommitSeenSequenceNumberByArbitratorTable->get(arbitratorId); + } + + // Go through each new commit one by one + for (uint i = 0; i < commitSequenceNumbers->size(); i++) { + int64_t commitSequenceNumber = commitSequenceNumbers->get(i); + Commit *commit = commitForClientTable->get(commitSequenceNumber); + // Special processing if a commit is not complete + if (!commit->isComplete()) { + if (i == (commitSequenceNumbers->size() - 1)) { + // 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 + break; + } else { + // This is a commit that was already dead but parts of it + // are still in the block chain (not flushed out yet)-> + // Delete it and move on + commit->setDead(); + commitForClientTable->remove(commit->getSequenceNumber()); + delete commit; + continue; + } + } - /** - * Process new table status entries and set dead the old ones as new ones come in. - * keeps track of the largest and smallest table status seen in this current round - * of updating the local copy of the block chain - */ - void processEntry(TableStatus entry, int64_t seq) { - int newNumSlots = entry.getMaxSlots(); - updateCurrMaxSize(newNumSlots); + // Update the last transaction that was updated if we can + if (commit->getTransactionSequenceNumber() != -1) { + // Update the last transaction sequence number that the arbitrator arbitrated on1 + if (!lastArbitratedTransactionNumberByArbitratorTable->contains(commit->getMachineId()) || lastArbitratedTransactionNumberByArbitratorTable->get(commit->getMachineId()) < commit->getTransactionSequenceNumber()) { + lastArbitratedTransactionNumberByArbitratorTable->put(commit->getMachineId(), commit->getTransactionSequenceNumber()); + } + } - initExpectedSize(seq, newNumSlots); + // Update the last arbitration data that we have seen so far + if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->contains(commit->getMachineId())) { + int64_t lastArbitrationSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->get(commit->getMachineId()); + if (commit->getSequenceNumber() > lastArbitrationSequenceNumber) { + // Is larger + lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->put(commit->getMachineId(), commit->getSequenceNumber()); + } + } else { + // Never seen any data from this arbitrator so record the first one + lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->put(commit->getMachineId(), commit->getSequenceNumber()); + } - if (liveTableStatus != NULL) { - // We have a larger table status so the old table status is no int64_ter alive - liveTableStatus.setDead(); - } + // We have already seen this commit before so need to do the + // full processing on this commit + if (commit->getSequenceNumber() <= lastCommitSeenSequenceNumber) { + // Update the last transaction that was updated if we can + if (commit->getTransactionSequenceNumber() != -1) { + int64_t lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable->get(commit->getMachineId()); + if (!lastArbitratedTransactionNumberByArbitratorTable->contains(commit->getMachineId()) || + lastArbitratedTransactionNumberByArbitratorTable->get(commit->getMachineId()) < commit->getTransactionSequenceNumber()) { + lastArbitratedTransactionNumberByArbitratorTable->put(commit->getMachineId(), commit->getTransactionSequenceNumber()); + } + } + continue; + } - // Make this new table status the latest alive table status - liveTableStatus = entry; - } + // If we got here then this is a brand new commit and needs full + // processing + // Get what commits should be edited, these are the commits that + // have live values for their keys + Hashset *commitsToEdit = new Hashset(); + { + SetIterator *kvit = commit->getKeyValueUpdateSet()->iterator(); + while (kvit->hasNext()) { + KeyValue *kv = kvit->next(); + Commit *commit = liveCommitsByKeyTable->get(kv->getKey()); + if (commit != NULL) + commitsToEdit->add(commit); + } + delete kvit; + } - /** - * Check old messages to see if there is a block chain violation. Also - */ - void processEntry(RejectedMessage entry, SlotIndexer indexer) { - int64_t oldSeqNum = entry.getOldSeqNum(); - int64_t newSeqNum = entry.getNewSeqNum(); - bool isequal = entry.getEqual(); - int64_t machineId = entry.getMachineID(); - int64_t seq = entry.getSequenceNumber(); + // Update each previous commit that needs to be updated + SetIterator *commitit = commitsToEdit->iterator(); + while (commitit->hasNext()) { + Commit *previousCommit = commitit->next(); + // Only bother with live commits (TODO: Maybe remove this check) + if (previousCommit->isLive()) { - // Check if we have messages that were supposed to be rejected in our local block chain - for (int64_t seqNum = oldSeqNum; seqNum <= newSeqNum; seqNum++) { + // Update which keys in the old commits are still live + { + SetIterator *kvit = commit->getKeyValueUpdateSet()->iterator(); + while (kvit->hasNext()) { + KeyValue *kv = kvit->next(); + previousCommit->invalidateKey(kv->getKey()); + } + delete kvit; + } + + // if the commit is now dead then remove it + if (!previousCommit->isLive()) { + commitForClientTable->remove(previousCommit->getSequenceNumber()); + delete previousCommit; + } + } + } + delete commitit; + delete commitsToEdit; - // Get the slot - Slot slot = indexer.getSlot(seqNum); + // Update the last seen sequence number from this arbitrator + if (lastCommitSeenSequenceNumberByArbitratorTable->contains(commit->getMachineId())) { + if (commit->getSequenceNumber() > lastCommitSeenSequenceNumberByArbitratorTable->get(commit->getMachineId())) { + lastCommitSeenSequenceNumberByArbitratorTable->put(commit->getMachineId(), commit->getSequenceNumber()); + } + } else { + lastCommitSeenSequenceNumberByArbitratorTable->put(commit->getMachineId(), commit->getSequenceNumber()); + } - if (slot != NULL) { - // If we have this slot make sure that it was not supposed to be a rejected slot + // We processed a new commit that we havent seen before + didProcessANewCommit = true; - int64_t slotMachineId = slot.getMachineID(); - if (isequal != (slotMachineId == machineId)) { - throw new Error("Server Error: Trying to insert rejected message for slot " + seqNum); + // Update the committed table of keys and which commit is using which key + { + SetIterator *kvit = commit->getKeyValueUpdateSet()->iterator(); + while (kvit->hasNext()) { + KeyValue *kv = kvit->next(); + committedKeyValueTable->put(kv->getKey(), kv); + liveCommitsByKeyTable->put(kv->getKey(), commit); } + delete kvit; } } + } + delete liveit; + + return didProcessANewCommit; +} +/** + * Create the speculative table from transactions that are still live + * and have come from the cloud + */ +bool Table::updateSpeculativeTable(bool didProcessNewCommits) { + if (liveTransactionBySequenceNumberTable->size() == 0) { + // There is nothing to speculate on + return false; + } - // Create a list of clients to watch until they see this rejected message entry. - HashSet deviceWatchSet = new HashSet(); - for (Map.Entry> lastMessageEntry : lastMessageTable.entrySet()) { + // Create a list of the transaction sequence numbers and sort them + // from oldest to newest + Vector *transactionSequenceNumbersSorted = new Vector(); + { + SetIterator *trit = getKeyIterator(liveTransactionBySequenceNumberTable); + while (trit->hasNext()) + transactionSequenceNumbersSorted->add(trit->next()); + delete trit; + } - // Machine ID for the last message entry - int64_t lastMessageEntryMachineId = lastMessageEntry.getKey(); + qsort(transactionSequenceNumbersSorted->expose(), transactionSequenceNumbersSorted->size(), sizeof(int64_t), compareInt64); + + bool hasGapInTransactionSequenceNumbers = transactionSequenceNumbersSorted->get(0) != oldestTransactionSequenceNumberSpeculatedOn; - // We've seen it, don't need to continue to watch. Our next - // message will implicitly acknowledge it. - if (lastMessageEntryMachineId == localMachineId) { - continue; - } - Pair lastMessageValue = lastMessageEntry.getValue(); - int64_t entrySequenceNumber = lastMessageValue.getFirst(); + if (hasGapInTransactionSequenceNumbers || didProcessNewCommits) { + // If there is a gap in the transaction sequence numbers then + // there was a commit or an abort of a transaction OR there was a + // new commit (Could be from offline commit) so a redo the + // speculation from scratch - if (entrySequenceNumber < seq) { + // Start from scratch + speculatedKeyValueTable->clear(); + lastTransactionSequenceNumberSpeculatedOn = -1; + oldestTransactionSequenceNumberSpeculatedOn = -1; + } - // Add this rejected message to the set of messages that this machine ID did not see yet - addWatchList(lastMessageEntryMachineId, entry); + // Remember the front of the transaction list + oldestTransactionSequenceNumberSpeculatedOn = transactionSequenceNumbersSorted->get(0); - // This client did not see this rejected message yet so add it to the watch set to monitor - deviceWatchSet.add(lastMessageEntryMachineId); - } - } + // Find where to start arbitration from + uint startIndex = 0; - if (deviceWatchSet.isEmpty()) { - // This rejected message has been seen by all the clients so - entry.setDead(); - } else { - // We need to watch this rejected message - entry.setWatchSet(deviceWatchSet); - } + for (; startIndex < transactionSequenceNumbersSorted->size(); startIndex++) + if (transactionSequenceNumbersSorted->get(startIndex) == lastTransactionSequenceNumberSpeculatedOn) + break; + startIndex++; + + if (startIndex >= transactionSequenceNumbersSorted->size()) { + // Make sure we are not out of bounds + return false; // did not speculate } - /** - * Check if this abort is live, if not then save it so we can kill it later. - * update the last transaction number that was arbitrated on. - */ - void processEntry(Abort entry) { + Hashset *incompleteTransactionArbitrator = new Hashset(); + bool didSkip = true; + for (uint i = startIndex; i < transactionSequenceNumbersSorted->size(); i++) { + int64_t transactionSequenceNumber = transactionSequenceNumbersSorted->get(i); + Transaction *transaction = liveTransactionBySequenceNumberTable->get(transactionSequenceNumber); - if (entry.getTransactionSequenceNumber() != -1) { - // update the transaction status if it was sent to the server - TransactionStatus status = outstandingTransactionStatus.remove(entry.getTransactionSequenceNumber()); - if (status != NULL) { - status.setStatus(TransactionStatus.StatusAborted); - } + if (!transaction->isComplete()) { + // If there is an incomplete transaction then there is nothing + // we can do add this transactions arbitrator to the list of + // arbitrators we should ignore + incompleteTransactionArbitrator->add(transaction->getArbitrator()); + didSkip = true; + continue; } - // Abort has not been seen by the client it is for yet so we need to keep track of it - Abort previouslySeenAbort = liveAbortTable.put(entry.getAbortId(), entry); - if (previouslySeenAbort != NULL) { - previouslySeenAbort.setDead(); // Delete old version of the abort since we got a rescued newer version + if (incompleteTransactionArbitrator->contains(transaction->getArbitrator())) { + continue; } - if (entry.getTransactionArbitrator() == localMachineId) { - liveAbortsGeneratedByLocal.put(entry.getArbitratorLocalSequenceNumber(), entry); + lastTransactionSequenceNumberSpeculatedOn = transactionSequenceNumber; + + if (transaction->evaluateGuard(committedKeyValueTable, speculatedKeyValueTable, NULL)) { + // Guard evaluated to true so update the speculative table + { + SetIterator *kvit = transaction->getKeyValueUpdateSet()->iterator(); + while (kvit->hasNext()) { + KeyValue *kv = kvit->next(); + speculatedKeyValueTable->put(kv->getKey(), kv); + } + delete kvit; + } } + } - if ((entry.getSequenceNumber() != -1) && (lastMessageTable.get(entry.getTransactionMachineId()).getFirst() >= entry.getSequenceNumber())) { + if (didSkip) { + // Since there was a skip we need to redo the speculation next time around + lastTransactionSequenceNumberSpeculatedOn = -1; + oldestTransactionSequenceNumberSpeculatedOn = -1; + } - // The machine already saw this so it is dead - entry.setDead(); - liveAbortTable.remove(entry.getAbortId()); + // We did some speculation + return true; +} - if (entry.getTransactionArbitrator() == localMachineId) { - liveAbortsGeneratedByLocal.remove(entry.getArbitratorLocalSequenceNumber()); - } +/** + * Create the pending transaction speculative table from transactions + * that are still in the pending transaction buffer + */ +void Table::updatePendingTransactionSpeculativeTable(bool didProcessNewCommitsOrSpeculate) { + if (pendingTransactionQueue->size() == 0) { + // There is nothing to speculate on + return; + } - return; - } + if (didProcessNewCommitsOrSpeculate || (firstPendingTransaction != pendingTransactionQueue->get(0))) { + // need to reset on the pending speculation + lastPendingTransactionSpeculatedOn = NULL; + firstPendingTransaction = pendingTransactionQueue->get(0); + pendingTransactionSpeculatedKeyValueTable->clear(); + } + // Find where to start arbitration from + uint startIndex = 0; + for (; startIndex < pendingTransactionQueue->size(); startIndex++) + if (pendingTransactionQueue->get(startIndex) == firstPendingTransaction) + break; + if (startIndex >= pendingTransactionQueue->size()) { + // Make sure we are not out of bounds + return; + } + + for (uint i = startIndex; i < pendingTransactionQueue->size(); i++) { + Transaction *transaction = pendingTransactionQueue->get(i); - // Update the last arbitration data that we have seen so far - if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(entry.getTransactionArbitrator()) != NULL) { + lastPendingTransactionSpeculatedOn = transaction; - int64_t lastArbitrationSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.get(entry.getTransactionArbitrator()); - if (entry.getSequenceNumber() > lastArbitrationSequenceNumber) { - // Is larger - lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.put(entry.getTransactionArbitrator(), entry.getSequenceNumber()); + if (transaction->evaluateGuard(committedKeyValueTable, speculatedKeyValueTable, pendingTransactionSpeculatedKeyValueTable)) { + // Guard evaluated to true so update the speculative table + SetIterator *kvit = transaction->getKeyValueUpdateSet()->iterator(); + while (kvit->hasNext()) { + KeyValue *kv = kvit->next(); + pendingTransactionSpeculatedKeyValueTable->put(kv->getKey(), kv); } - } else { - // Never seen any data from this arbitrator so record the first one - lastArbitrationDataLocalSequenceNumberSeenFromArbitrator.put(entry.getTransactionArbitrator(), entry.getSequenceNumber()); + delete kvit; } + } +} +/** + * Set dead and remove from the live transaction tables the + * transactions that are dead + */ +void Table::updateLiveTransactionsAndStatus() { + // Go through each of the transactions + { + SetIterator *iter = getKeyIterator(liveTransactionBySequenceNumberTable); + while (iter->hasNext()) { + int64_t key = iter->next(); + Transaction *transaction = liveTransactionBySequenceNumberTable->get(key); - // Set dead a transaction if we can - Transaction transactionToSetDead = liveTransactionByTransactionIdTable.remove(new Pair(entry.getTransactionMachineId(), entry.getTransactionClientLocalSequenceNumber())); - if (transactionToSetDead != NULL) { - liveTransactionBySequenceNumberTable.remove(transactionToSetDead.getSequenceNumber()); - } - - // Update the last transaction sequence number that the arbitrator arbitrated on - Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(entry.getTransactionArbitrator()); - if ((lastTransactionNumber == NULL) || (lastTransactionNumber < entry.getTransactionSequenceNumber())) { + // Check if the transaction is dead + if (lastArbitratedTransactionNumberByArbitratorTable->contains(transaction->getArbitrator()) + && lastArbitratedTransactionNumberByArbitratorTable->get(transaction->getArbitrator()) >= transaction->getSequenceNumber()) { + // Set dead the transaction + transaction->setDead(); - // Is a valid one - if (entry.getTransactionSequenceNumber() != -1) { - lastArbitratedTransactionNumberByArbitratorTable.put(entry.getTransactionArbitrator(), entry.getTransactionSequenceNumber()); + // Remove the transaction from the live table + iter->remove(); + liveTransactionByTransactionIdTable->remove(transaction->getId()); + delete transaction; } } + delete iter; } - /** - * Set dead the transaction part if that transaction is dead and keep track of all new parts - */ - void processEntry(TransactionPart entry) { - // Check if we have already seen this transaction and set it dead OR if it is not alive - Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(entry.getArbitratorId()); - if ((lastTransactionNumber != NULL) && (lastTransactionNumber >= entry.getSequenceNumber())) { - // This transaction is dead, it was already committed or aborted - entry.setDead(); - return; - } + // Go through each of the transactions + { + SetIterator *iter = getKeyIterator(outstandingTransactionStatus); + while (iter->hasNext()) { + int64_t key = iter->next(); + TransactionStatus *status = outstandingTransactionStatus->get(key); - // This part is still alive - Map, TransactionPart> transactionPart = newTransactionParts.get(entry.getMachineId()); + // Check if the transaction is dead + if (lastArbitratedTransactionNumberByArbitratorTable->contains(status->getTransactionArbitrator()) + && (lastArbitratedTransactionNumberByArbitratorTable->get(status->getTransactionArbitrator()) >= status->getTransactionSequenceNumber())) { + // Set committed + status->setStatus(TransactionStatus_StatusCommitted); - if (transactionPart == NULL) { - // Dont have a table for this machine Id yet so make one - transactionPart = new HashMap, TransactionPart>(); - newTransactionParts.put(entry.getMachineId(), transactionPart); + // Remove + iter->remove(); + } } + delete iter; + } +} - // Update the part and set dead ones we have already seen (got a rescued version) - TransactionPart previouslySeenPart = transactionPart.put(entry.getPartId(), entry); - if (previouslySeenPart != NULL) { - previouslySeenPart.setDead(); +/** + * Process this slot, entry by entry-> Also update the latest message sent by slot + */ +void Table::processSlot(SlotIndexer *indexer, Slot *slot, bool acceptUpdatesToLocal, Hashset *machineSet) { + + // Update the last message seen + updateLastMessage(slot->getMachineID(), slot->getSequenceNumber(), slot, acceptUpdatesToLocal, machineSet); + + // Process each entry in the slot + Vector *entries = slot->getEntries(); + uint eSize = entries->size(); + for (uint ei = 0; ei < eSize; ei++) { + Entry *entry = entries->get(ei); + switch (entry->getType()) { + case TypeCommitPart: + processEntry((CommitPart *)entry); + break; + case TypeAbort: + processEntry((Abort *)entry); + break; + case TypeTransactionPart: + processEntry((TransactionPart *)entry); + break; + case TypeNewKey: + processEntry((NewKey *)entry); + break; + case TypeLastMessage: + processEntry((LastMessage *)entry, machineSet); + break; + case TypeRejectedMessage: + processEntry((RejectedMessage *)entry, indexer); + break; + case TypeTableStatus: + processEntry((TableStatus *)entry, slot->getSequenceNumber()); + break; + default: + throw new Error("Unrecognized type: "); } } +} + +/** + * Update the last message that was sent for a machine Id + */ +void Table::processEntry(LastMessage *entry, Hashset *machineSet) { + // Update what the last message received by a machine was + updateLastMessage(entry->getMachineID(), entry->getSequenceNumber(), entry, false, machineSet); +} - /** - * Process new commit entries and save them for future use. Delete duplicates - */ - void processEntry(CommitPart entry) { +/** + * Add the new key to the arbitrators table and update the set of live + * new keys (in case of a rescued new key message) + */ +void Table::processEntry(NewKey *entry) { + // Update the arbitrator table with the new key information + arbitratorTable->put(entry->getKey(), entry->getMachineID()); + + // Update what the latest live new key is + NewKey *oldNewKey = liveNewKeyTable->put(entry->getKey(), entry); + if (oldNewKey != NULL) { + // Delete the old new key messages + oldNewKey->setDead(); + } +} +/** + * Process new table status entries and set dead the old ones as new + * ones come in-> keeps track of the largest and smallest table status + * seen in this current round of updating the local copy of the block + * chain + */ +void Table::processEntry(TableStatus *entry, int64_t seq) { + int newNumSlots = entry->getMaxSlots(); + updateCurrMaxSize(newNumSlots); + initExpectedSize(seq, newNumSlots); + + if (liveTableStatus != NULL) { + // We have a larger table status so the old table status is no + // int64_ter alive + liveTableStatus->setDead(); + } - // Update the last transaction that was updated if we can - if (entry.getTransactionSequenceNumber() != -1) { - Long lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable.get(entry.getMachineId()); + // Make this new table status the latest alive table status + liveTableStatus = entry; +} - // Update the last transaction sequence number that the arbitrator arbitrated on - if ((lastTransactionNumber == NULL) || (lastTransactionNumber < entry.getTransactionSequenceNumber())) { - lastArbitratedTransactionNumberByArbitratorTable.put(entry.getMachineId(), entry.getTransactionSequenceNumber()); +/** + * Check old messages to see if there is a block chain violation-> + * Also + */ +void Table::processEntry(RejectedMessage *entry, SlotIndexer *indexer) { + int64_t oldSeqNum = entry->getOldSeqNum(); + int64_t newSeqNum = entry->getNewSeqNum(); + bool isequal = entry->getEqual(); + int64_t machineId = entry->getMachineID(); + int64_t seq = entry->getSequenceNumber(); + + // Check if we have messages that were supposed to be rejected in + // our local block chain + for (int64_t seqNum = oldSeqNum; seqNum <= newSeqNum; seqNum++) { + // Get the slot + Slot *slot = indexer->getSlot(seqNum); + + if (slot != NULL) { + // If we have this slot make sure that it was not supposed to be + // a rejected slot + int64_t slotMachineId = slot->getMachineID(); + if (isequal != (slotMachineId == machineId)) { + throw new Error("Server Error: Trying to insert rejected message for slot "); } } + } + // Create a list of clients to watch until they see this rejected + // message entry-> + Hashset *deviceWatchSet = new Hashset(); + SetIterator *> *iter = getKeyIterator(lastMessageTable); + while (iter->hasNext()) { + // Machine ID for the last message entry + int64_t lastMessageEntryMachineId = iter->next(); + // We've seen it, don't need to continue to watch-> Our next + // message will implicitly acknowledge it-> + if (lastMessageEntryMachineId == localMachineId) { + continue; + } + Pair *lastMessageValue = lastMessageTable->get(lastMessageEntryMachineId); + int64_t entrySequenceNumber = lastMessageValue->getFirst(); - Map, CommitPart> commitPart = newCommitParts.get(entry.getMachineId()); - - if (commitPart == NULL) { - // Don't have a table for this machine Id yet so make one - commitPart = new HashMap, CommitPart>(); - newCommitParts.put(entry.getMachineId(), commitPart); + if (entrySequenceNumber < seq) { + // Add this rejected message to the set of messages that this + // machine ID did not see yet + addWatchVector(lastMessageEntryMachineId, entry); + // This client did not see this rejected message yet so add it + // to the watch set to monitor + deviceWatchSet->add(lastMessageEntryMachineId); } + } + delete iter; + + if (deviceWatchSet->isEmpty()) { + // This rejected message has been seen by all the clients so + entry->setDead(); + delete deviceWatchSet; + } else { + // We need to watch this rejected message + entry->setWatchSet(deviceWatchSet); + } +} - // Update the part and set dead ones we have already seen (got a rescued version) - CommitPart previouslySeenPart = commitPart.put(entry.getPartId(), entry); - if (previouslySeenPart != NULL) { - previouslySeenPart.setDead(); +/** + * Check if this abort is live, if not then save it so we can kill it + * later-> update the last transaction number that was arbitrated on-> + */ +void Table::processEntry(Abort *entry) { + if (entry->getTransactionSequenceNumber() != -1) { + // update the transaction status if it was sent to the server + TransactionStatus *status = outstandingTransactionStatus->remove(entry->getTransactionSequenceNumber()); + if (status != NULL) { + status->setStatus(TransactionStatus_StatusAborted); } } - /** - * Update the last message seen table. Update and set dead the appropriate RejectedMessages as clients see them. - * Updates the live aborts, removes those that are dead and sets them dead. - * Check that the last message seen is correct and that there is no mismatch of our own last message or that - * other clients have not had a rollback on the last message. - */ - void updateLastMessage(int64_t machineId, int64_t seqNum, Liveness liveness, bool acceptUpdatesToLocal, HashSet machineSet) { + // Abort has not been seen by the client it is for yet so we need to + // keep track of it - // We have seen this machine ID - machineSet.remove(machineId); + Abort *previouslySeenAbort = liveAbortTable->put(new Pair(entry->getAbortId()), entry); + if (previouslySeenAbort != NULL) { + previouslySeenAbort->setDead(); // Delete old version of the abort since we got a rescued newer version + } - // Get the set of rejected messages that this machine Id is has not seen yet - HashSet watchset = rejectedMessageWatchListTable.get(machineId); + if (entry->getTransactionArbitrator() == localMachineId) { + liveAbortsGeneratedByLocal->put(entry->getArbitratorLocalSequenceNumber(), entry); + } - // If there is a rejected message that this machine Id has not seen yet - if (watchset != NULL) { + if ((entry->getSequenceNumber() != -1) && (lastMessageTable->get(entry->getTransactionMachineId())->getFirst() >= entry->getSequenceNumber())) { + // The machine already saw this so it is dead + entry->setDead(); + Pair abortid = entry->getAbortId(); + liveAbortTable->remove(&abortid); - // Go through each rejected message that this machine Id has not seen yet - for (Iterator rmit = watchset.iterator(); rmit.hasNext(); ) { + if (entry->getTransactionArbitrator() == localMachineId) { + liveAbortsGeneratedByLocal->remove(entry->getArbitratorLocalSequenceNumber()); + } + return; + } - RejectedMessage rm = rmit.next(); + // Update the last arbitration data that we have seen so far + if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->contains(entry->getTransactionArbitrator())) { + int64_t lastArbitrationSequenceNumber = lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->get(entry->getTransactionArbitrator()); + if (entry->getSequenceNumber() > lastArbitrationSequenceNumber) { + // Is larger + lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->put(entry->getTransactionArbitrator(), entry->getSequenceNumber()); + } + } else { + // Never seen any data from this arbitrator so record the first one + lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->put(entry->getTransactionArbitrator(), entry->getSequenceNumber()); + } - // If this machine Id has seen this rejected message... - if (rm.getSequenceNumber() <= seqNum) { + // Set dead a transaction if we can + Pair deadPair = Pair(entry->getTransactionMachineId(), entry->getTransactionClientLocalSequenceNumber()); - // Remove it from our watchlist - rmit.remove(); + Transaction *transactionToSetDead = liveTransactionByTransactionIdTable->remove(&deadPair); + if (transactionToSetDead != NULL) { + liveTransactionBySequenceNumberTable->remove(transactionToSetDead->getSequenceNumber()); + } - // Decrement machines that need to see this notification - rm.removeWatcher(machineId); - } - } + // Update the last transaction sequence number that the arbitrator + // arbitrated on + if (!lastArbitratedTransactionNumberByArbitratorTable->contains(entry->getTransactionArbitrator()) || + (lastArbitratedTransactionNumberByArbitratorTable->get(entry->getTransactionArbitrator()) < entry->getTransactionSequenceNumber())) { + // Is a valid one + if (entry->getTransactionSequenceNumber() != -1) { + lastArbitratedTransactionNumberByArbitratorTable->put(entry->getTransactionArbitrator(), entry->getTransactionSequenceNumber()); } + } +} - // Set dead the abort - for (Iterator, Abort>> i = liveAbortTable.entrySet().iterator(); i.hasNext();) { - Abort abort = i.next().getValue(); - - if ((abort.getTransactionMachineId() == machineId) && (abort.getSequenceNumber() <= seqNum)) { - abort.setDead(); - i.remove(); +/** + * Set dead the transaction part if that transaction is dead and keep + * track of all new parts + */ +void Table::processEntry(TransactionPart *entry) { + // Check if we have already seen this transaction and set it dead OR + // if it is not alive + if (lastArbitratedTransactionNumberByArbitratorTable->contains(entry->getArbitratorId()) && (lastArbitratedTransactionNumberByArbitratorTable->get(entry->getArbitratorId()) >= entry->getSequenceNumber())) { + // This transaction is dead, it was already committed or aborted + entry->setDead(); + return; + } - if (abort.getTransactionArbitrator() == localMachineId) { - liveAbortsGeneratedByLocal.remove(abort.getArbitratorLocalSequenceNumber()); - } - } - } + // This part is still alive + Hashtable *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals> *transactionPart = newTransactionParts->get(entry->getMachineId()); + if (transactionPart == NULL) { + // Dont have a table for this machine Id yet so make one + transactionPart = new Hashtable *, TransactionPart *, uintptr_t, 0, pairHashFunction, pairEquals>(); + newTransactionParts->put(entry->getMachineId(), transactionPart); + } + // Update the part and set dead ones we have already seen (got a + // rescued version) + TransactionPart *previouslySeenPart = transactionPart->put(new Pair(entry->getPartId()), entry); + if (previouslySeenPart != NULL) { + previouslySeenPart->setDead(); + } +} - if (machineId == localMachineId) { - // Our own messages are immediately dead. - if (liveness instanceof LastMessage) { - ((LastMessage)liveness).setDead(); - } else if (liveness instanceof Slot) { - ((Slot)liveness).setDead(); - } else { - throw new Error("Unrecognized type"); - } +/** + * Process new commit entries and save them for future use-> Delete duplicates + */ +void Table::processEntry(CommitPart *entry) { + // Update the last transaction that was updated if we can + if (entry->getTransactionSequenceNumber() != -1) { + if (!lastArbitratedTransactionNumberByArbitratorTable->contains(entry->getMachineId()) || + lastArbitratedTransactionNumberByArbitratorTable->get(entry->getMachineId()) < entry->getTransactionSequenceNumber()) { + lastArbitratedTransactionNumberByArbitratorTable->put(entry->getMachineId(), entry->getTransactionSequenceNumber()); } + } - // Get the old last message for this device - Pair lastMessageEntry = lastMessageTable.put(machineId, new Pair(seqNum, liveness)); - if (lastMessageEntry == NULL) { - // If no last message then there is nothing else to process - return; - } + Hashtable *, CommitPart *, uintptr_t, 0, pairHashFunction, pairEquals> *commitPart = newCommitParts->get(entry->getMachineId()); + if (commitPart == NULL) { + // Don't have a table for this machine Id yet so make one + commitPart = new Hashtable *, CommitPart *, uintptr_t, 0, pairHashFunction, pairEquals>(); + newCommitParts->put(entry->getMachineId(), commitPart); + } + // Update the part and set dead ones we have already seen (got a + // rescued version) + CommitPart *previouslySeenPart = commitPart->put(entry->getPartId(), entry); + if (previouslySeenPart != NULL) { + previouslySeenPart->setDead(); + } +} + +/** + * Update the last message seen table-> Update and set dead the + * appropriate RejectedMessages as clients see them-> Updates the live + * aborts, removes those that are dead and sets them dead-> Check that + * the last message seen is correct and that there is no mismatch of + * our own last message or that other clients have not had a rollback + * on the last message-> + */ +void Table::updateLastMessage(int64_t machineId, int64_t seqNum, Liveness *liveness, bool acceptUpdatesToLocal, Hashset *machineSet) { + // We have seen this machine ID + machineSet->remove(machineId); + + // Get the set of rejected messages that this machine Id is has not seen yet + Hashset *watchset = rejectedMessageWatchVectorTable->get(machineId); + // If there is a rejected message that this machine Id has not seen yet + if (watchset != NULL) { + // Go through each rejected message that this machine Id has not + // seen yet + + SetIterator *rmit = watchset->iterator(); + while (rmit->hasNext()) { + RejectedMessage *rm = rmit->next(); + // If this machine Id has seen this rejected message->->-> + if (rm->getSequenceNumber() <= seqNum) { + // Remove it from our watchlist + rmit->remove(); + // Decrement machines that need to see this notification + rm->removeWatcher(machineId); + } + } + delete rmit; + } - int64_t lastMessageSeqNum = lastMessageEntry.getFirst(); - Liveness lastEntry = lastMessageEntry.getSecond(); + // Set dead the abort + SetIterator *, Abort *, uintptr_t, 0, pairHashFunction, pairEquals> *abortit = getKeyIterator(liveAbortTable); - // If it is not our machine Id since we already set ours to dead - if (machineId != localMachineId) { - if (lastEntry instanceof LastMessage) { - ((LastMessage)lastEntry).setDead(); - } else if (lastEntry instanceof Slot) { - ((Slot)lastEntry).setDead(); - } else { - throw new Error("Unrecognized type"); + while (abortit->hasNext()) { + Pair *key = abortit->next(); + Abort *abort = liveAbortTable->get(key); + if ((abort->getTransactionMachineId() == machineId) && (abort->getSequenceNumber() <= seqNum)) { + abort->setDead(); + abortit->remove(); + if (abort->getTransactionArbitrator() == localMachineId) { + liveAbortsGeneratedByLocal->remove(abort->getArbitratorLocalSequenceNumber()); } } + } + delete abortit; + if (machineId == localMachineId) { + // Our own messages are immediately dead-> + char livenessType = liveness->getType(); + if (livenessType == TypeLastMessage) { + ((LastMessage *)liveness)->setDead(); + } else if (livenessType == TypeSlot) { + ((Slot *)liveness)->setDead(); + } else { + throw new Error("Unrecognized type"); + } + } + // Get the old last message for this device + Pair *lastMessageEntry = lastMessageTable->put(machineId, new Pair(seqNum, liveness)); + if (lastMessageEntry == NULL) { + // If no last message then there is nothing else to process + return; + } - // Make sure the server is not playing any games - if (machineId == localMachineId) { + int64_t lastMessageSeqNum = lastMessageEntry->getFirst(); + Liveness *lastEntry = lastMessageEntry->getSecond(); + delete lastMessageEntry; - if (hadPartialSendToServer) { - // We were not making any updates and we had a machine mismatch - if (lastMessageSeqNum > seqNum && !acceptUpdatesToLocal) { - throw new Error("Server Error: Mismatch on local machine sequence number, needed at least: " + lastMessageSeqNum + " got: " + seqNum); - } + // If it is not our machine Id since we already set ours to dead + if (machineId != localMachineId) { + char lastEntryType = lastEntry->getType(); - } else { - // We were not making any updates and we had a machine mismatch - if (lastMessageSeqNum != seqNum && !acceptUpdatesToLocal) { - throw new Error("Server Error: Mismatch on local machine sequence number, needed: " + lastMessageSeqNum + " got: " + seqNum); - } + if (lastEntryType == TypeLastMessage) { + ((LastMessage *)lastEntry)->setDead(); + } else if (lastEntryType == TypeSlot) { + ((Slot *)lastEntry)->setDead(); + } else { + throw new Error("Unrecognized type"); + } + } + // Make sure the server is not playing any games + if (machineId == localMachineId) { + if (hadPartialSendToServer) { + // We were not making any updates and we had a machine mismatch + if (lastMessageSeqNum > seqNum && !acceptUpdatesToLocal) { + throw new Error("Server Error: Mismatch on local machine sequence number, needed at least: "); } } else { - if (lastMessageSeqNum > seqNum) { - throw new Error("Server Error: Rollback on remote machine sequence number"); + // We were not making any updates and we had a machine mismatch + if (lastMessageSeqNum != seqNum && !acceptUpdatesToLocal) { + throw new Error("Server Error: Mismatch on local machine sequence number, needed: "); } } + } else { + if (lastMessageSeqNum > seqNum) { + throw new Error("Server Error: Rollback on remote machine sequence number"); + } } +} - /** - * Add a rejected message entry to the watch set to keep track of which clients have seen that - * rejected message entry and which have not. - */ - void addWatchList(int64_t machineId, RejectedMessage entry) { - HashSet entries = rejectedMessageWatchListTable.get(machineId); - if (entries == NULL) { - // There is no set for this machine ID yet so create one - entries = new HashSet(); - rejectedMessageWatchListTable.put(machineId, entries); - } - entries.add(entry); +/** + * Add a rejected message entry to the watch set to keep track of + * which clients have seen that rejected message entry and which have + * not. + */ +void Table::addWatchVector(int64_t machineId, RejectedMessage *entry) { + Hashset *entries = rejectedMessageWatchVectorTable->get(machineId); + if (entries == NULL) { + // There is no set for this machine ID yet so create one + entries = new Hashset(); + rejectedMessageWatchVectorTable->put(machineId, entries); } + entries->add(entry); +} - /** - * Check if the HMAC chain is not violated - */ - void checkHMACChain(SlotIndexer indexer, Slot[] newSlots) { - for (int i = 0; i < newSlots.length; i++) { - Slot currSlot = newSlots[i]; - Slot prevSlot = indexer.getSlot(currSlot.getSequenceNumber() - 1); - if (prevSlot != NULL && - !Arrays.equals(prevSlot.getHMAC(), currSlot.getPrevHMAC())) - throw new Error("Server Error: Invalid HMAC Chain" + currSlot + " " + prevSlot); - } +/** + * Check if the HMAC chain is not violated + */ +void Table::checkHMACChain(SlotIndexer *indexer, Array *newSlots) { + for (uint i = 0; i < newSlots->length(); i++) { + Slot *currSlot = newSlots->get(i); + Slot *prevSlot = indexer->getSlot(currSlot->getSequenceNumber() - 1); + if (prevSlot != NULL && + !prevSlot->getHMAC()->equals(currSlot->getPrevHMAC())) + throw new Error("Server Error: Invalid HMAC Chain"); } }