#include "TableStatus.h"
#include "TransactionStatus.h"
#include "Transaction.h"
+#include "LastMessage.h"
#include "Random.h"
+
Table::Table(IoTString *baseurl, IoTString *password, int64_t _localMachineId, int listeningPort) :
buffer(NULL),
cloud(new CloudComm(this, baseurl, password, listeningPort)),
init();
}
-Table::Table(CloudComm * _cloud, int64_t _localMachineId) :
+Table::Table(CloudComm *_cloud, int64_t _localMachineId) :
buffer(NULL),
cloud(_cloud),
random(NULL),
committedKeyValueTable = new Hashtable<IoTString *, KeyValue *>();
speculatedKeyValueTable = new Hashtable<IoTString *, KeyValue *>();
pendingTransactionSpeculatedKeyValueTable = new Hashtable<IoTString *, KeyValue *>();
- liveNewKeyTable = new Hashtable<IoTString *, NewKey*>();
- lastMessageTable = new Hashtable<int64_t, Pair<int64_t, Liveness*> *>();
- rejectedMessageWatchVectorTable = new Hashtable<int64_t, Hashset<RejectedMessage*>* >();
+ liveNewKeyTable = new Hashtable<IoTString *, NewKey *>();
+ lastMessageTable = new Hashtable<int64_t, Pair<int64_t, Liveness *> *>();
+ rejectedMessageWatchVectorTable = new Hashtable<int64_t, Hashset<RejectedMessage *> * >();
arbitratorTable = new Hashtable<IoTString *, int64_t>();
- liveAbortTable = new Hashtable<Pair<int64_t, int64_t>*, Abort*>();
- newTransactionParts = new Hashtable<int64_t, Hashtable<Pair<int64_t, int32_t>*, TransactionPart*> *>();
- newCommitParts = new Hashtable<int64_t, Hashtable<Pair<int64_t, int32_t>*, CommitPart*> *>();
+ liveAbortTable = new Hashtable<Pair<int64_t, int64_t> *, Abort *>();
+ newTransactionParts = new Hashtable<int64_t, Hashtable<Pair<int64_t, int32_t> *, TransactionPart *> *>();
+ newCommitParts = new Hashtable<int64_t, Hashtable<Pair<int64_t, int32_t> *, CommitPart *> *>();
lastArbitratedTransactionNumberByArbitratorTable = new Hashtable<int64_t, int64_t>();
- liveTransactionBySequenceNumberTable = new Hashtable<int64_t, Transaction*>();
- liveTransactionByTransactionIdTable = new Hashtable<Pair<int64_t, int64_t>*, Transaction*>();
- liveCommitsTable = new Hashtable<int64_t, Hashtable<int64_t, Commit*> >();
- liveCommitsByKeyTable = new Hashtable<IoTString *, Commit*>();
+ liveTransactionBySequenceNumberTable = new Hashtable<int64_t, Transaction *>();
+ liveTransactionByTransactionIdTable = new Hashtable<Pair<int64_t, int64_t> *, Transaction *>();
+ liveCommitsTable = new Hashtable<int64_t, Hashtable<int64_t, Commit *> >();
+ liveCommitsByKeyTable = new Hashtable<IoTString *, Commit *>();
lastCommitSeenSequenceNumberByArbitratorTable = new Hashtable<int64_t, int64_t>();
rejectedSlotVector = new Vector<int64_t>();
- pendingTransactionQueue = new Vector<Transaction*>();
- pendingSendArbitrationEntriesToDelete = new Vector<Entry*>();
- transactionPartsSent = new Hashtable<Transaction*, Vector<int32_t> *>();
- outstandingTransactionStatus = new Hashtable<int64_t, TransactionStatus*>();
- liveAbortsGeneratedByLocal = new Hashtable<int64_t, Abort*>();
- offlineTransactionsCommittedAndAtServer = new Hashset<Pair<int64_t, int64_t>*>();
- localCommunicationTable = new Hashtable<int64_t, Pair<IoTString*, int32_t>*>();
+ pendingTransactionQueue = new Vector<Transaction *>();
+ pendingSendArbitrationEntriesToDelete = new Vector<Entry *>();
+ transactionPartsSent = new Hashtable<Transaction *, Vector<int32_t> *>();
+ outstandingTransactionStatus = new Hashtable<int64_t, TransactionStatus *>();
+ liveAbortsGeneratedByLocal = new Hashtable<int64_t, Abort *>();
+ offlineTransactionsCommittedAndAtServer = new Hashset<Pair<int64_t, int64_t> *>();
+ localCommunicationTable = new Hashtable<int64_t, Pair<IoTString *, int32_t> >();
lastTransactionSeenFromMachineFromServer = new Hashtable<int64_t, int64_t>();
- pendingSendArbitrationRounds = new Vector<ArbitrationRound*>();
+ pendingSendArbitrationRounds = new Vector<ArbitrationRound *>();
lastArbitrationDataLocalSequenceNumberSeenFromArbitrator = new Hashtable<int64_t, int64_t>();
}
/**
- * Initialize the table by inserting a table status as the first entry into the table status
- * also initialize the crypto stuff.
+ * Initialize the table by inserting a table status as the first entry
+ * into the table status also initialize the crypto stuff.
*/
- void Table::initTable() {
+void Table::initTable() {
cloud->initSecurity();
// Create the first insertion into the block chain which is the table status
localSequenceNumber++;
TableStatus *status = new TableStatus(s, numberOfSlots);
s->addEntry(status);
- Array<Slot*> *array = cloud->putSlot(s, numberOfSlots);
+ Array<Slot *> *array = cloud->putSlot(s, numberOfSlots);
if (array == NULL) {
- array = new Array<Slot*>(1);
+ array = new Array<Slot *>(1);
array->set(0, s);
// update local block chain
validateAndUpdate(array, true);
}
/**
- * Rebuild the table from scratch by pulling the latest block chain from the server.
+ * Rebuild the table from scratch by pulling the latest block chain
+ * from the server.
*/
- void Table::rebuild() {
+void Table::rebuild() {
// Just pull the latest slots from the server
- Array<Slot*> *newslots = cloud->getSlots(sequenceNumber + 1);
+ Array<Slot *> *newslots = cloud->getSlots(sequenceNumber + 1);
validateAndUpdate(newslots, true);
sendToServer(NULL);
updateLiveTransactionsAndStatus();
}
- void Table::addLocalCommunication(int64_t arbitrator, IoTString *hostName, int portNumber) {
- localCommunicationTable->put(arbitrator, new Pair<IoTString*, int32_t>(hostName, portNumber));
+void Table::addLocalCommunication(int64_t arbitrator, IoTString *hostName, int portNumber) {
+ localCommunicationTable->put(arbitrator, Pair<IoTString *, int32_t>(hostName, portNumber));
}
- int64_t Table::getArbitrator(IoTString *key) {
+int64_t Table::getArbitrator(IoTString *key) {
return arbitratorTable->get(key);
}
- void Table::close() {
+void Table::close() {
cloud->close();
}
- IoTString * Table::getCommitted(IoTString *key) {
+IoTString *Table::getCommitted(IoTString *key) {
KeyValue *kv = committedKeyValueTable->get(key);
if (kv != NULL) {
}
}
- IoTString * Table::getSpeculative(IoTString *key) {
+IoTString *Table::getSpeculative(IoTString *key) {
KeyValue *kv = pendingTransactionSpeculatedKeyValueTable->get(key);
if (kv == NULL) {
}
}
- IoTString * Table::getCommittedAtomic(IoTString *key) {
+IoTString *Table::getCommittedAtomic(IoTString *key) {
KeyValue *kv = committedKeyValueTable->get(key);
- if (arbitratorTable->get(key) == NULL) {
+ if (!arbitratorTable->contains(key)) {
throw new Error("Key not Found.");
}
}
}
- IoTString * Table::getSpeculativeAtomic(IoTString *key) {
- if (arbitratorTable->get(key) == NULL) {
+IoTString *Table::getSpeculativeAtomic(IoTString *key) {
+ if (!arbitratorTable->contains(key)) {
throw new Error("Key not Found.");
}
}
}
- bool Table::update() {
+bool Table::update() {
try {
- Array<Slot*> *newSlots = cloud->getSlots(sequenceNumber + 1);
+ Array<Slot *> *newSlots = cloud->getSlots(sequenceNumber + 1);
validateAndUpdate(newSlots, false);
sendToServer(NULL);
return true;
} catch (Exception *e) {
- // e->printStackTrace();
-
for (int64_t m : localCommunicationTable->keySet()) {
updateFromLocal(m);
}
return false;
}
- bool Table::createNewKey(IoTString *keyName, int64_t machineId) {
+bool Table::createNewKey(IoTString *keyName, int64_t machineId) {
while (true) {
- if (arbitratorTable->get(keyName) != NULL) {
+ if (!arbitratorTable->contains(keyName)) {
// There is already an arbitrator
return false;
}
- NewKey * newKey = new NewKey(NULL, keyName, machineId);
+ NewKey *newKey = new NewKey(NULL, keyName, machineId);
if (sendToServer(newKey)) {
// If successfully inserted
}
}
- void Table::startTransaction() {
+void Table::startTransaction() {
// Create a new transaction, invalidates any old pending transactions.
pendingTransactionBuilder = new PendingTransaction(localMachineId);
}
- void Table::addKV(IoTString *key, IoTString *value) {
+void Table::addKV(IoTString *key, IoTString *value) {
// Make sure it is a valid key
- if (arbitratorTable->get(key) == NULL) {
+ if (!arbitratorTable->contains(key)) {
throw new Error("Key not Found.");
}
pendingTransactionBuilder->addKV(kv);
}
- TransactionStatus Table::commitTransaction() {
+TransactionStatus *Table::commitTransaction() {
if (pendingTransactionBuilder->getKVUpdates()->size() == 0) {
// transaction with no updates will have no effect on the system
localTransactionSequenceNumber++;
// Create the transaction status
- TransactionStatus transactionStatus = new TransactionStatus(TransactionStatus_StatusPending, pendingTransactionBuilder->getArbitrator());
+ TransactionStatus *transactionStatus = new TransactionStatus(TransactionStatus_StatusPending, pendingTransactionBuilder->getArbitrator());
// Create the new transaction
Transaction *newTransaction = pendingTransactionBuilder->createTransaction();
sendToServer(NULL);
} catch (ServerException *e) {
- Hashset<int64_t>* arbitratorTriedAndFailed = new Hashset<int64_t>();
- for (Iterator<Transaction*> *iter = pendingTransactionQueue->iterator(); iter->hasNext(); ) {
- Transaction * transaction = iter->next();
+ Hashset<int64_t> *arbitratorTriedAndFailed = new Hashset<int64_t>();
+ for (Iterator<Transaction *> *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
continue;
}
- Pair<bool, bool> * sendReturn = sendTransactionToLocal(transaction);
+ Pair<bool, bool> *sendReturn = sendTransactionToLocal(transaction);
if (sendReturn->getFirst()) {
// Failed to contact over local
bool lastInsertedNewKey = false;
-bool Table::sendToServer(NewKey* newKey) {
+bool Table::sendToServer(NewKey *newKey) {
bool fromRetry = false;
try {
if (hadPartialSendToServer) {
- Array<Slot*> *newSlots = cloud->getSlots(sequenceNumber + 1);
+ Array<Slot *> *newSlots = cloud->getSlots(sequenceNumber + 1);
if (newSlots->length() == 0) {
fromRetry = true;
- ThreeTuple<bool, bool, Array<Slot*> *> *sendSlotsReturn = sendSlotsToServer(lastSlotAttemptedToSend, lastNewSize, lastIsNewKey);
+ ThreeTuple<bool, bool, Array<Slot *> *> *sendSlotsReturn = sendSlotsToServer(lastSlotAttemptedToSend, lastNewSize, lastIsNewKey);
if (sendSlotsReturn->getFirst()) {
if (newKey != NULL) {
newSlots = sendSlotsReturn->getThird();
bool isInserted = false;
- for (Slot *s : newSlots) {
+ for (uint si = 0; si < newSlots->length(); si++) {
+ Slot *s = newSlots->get(si);
if ((s->getSequenceNumber() == lastSlotAttemptedToSend->getSequenceNumber()) && (s->getMachineID() == localMachineId)) {
isInserted = true;
break;
}
}
- for (Slot *s : newSlots) {
+ for (uint si = 0; si < newSlots->length(); si++) {
+ Slot *s = newSlots->get(si);
if (isInserted) {
break;
}
// Process each entry in the slot
for (Entry *entry : s->getEntries()) {
-
if (entry->getType() == TypeLastMessage) {
- LastMessage lastMessage = (LastMessage)entry;
+ LastMessage *lastMessage = (LastMessage *)entry;
if ((lastMessage->getMachineID() == localMachineId) && (lastMessage->getSequenceNumber() == lastSlotAttemptedToSend->getSequenceNumber())) {
isInserted = true;
break;
// continue;
} else {
bool isInserted = false;
- for (Slot *s : newSlots) {
+ for (uint si = 0; si < newSlots->length(); si++) {
+ Slot *s = newSlots->get(si);
if ((s->getSequenceNumber() == lastSlotAttemptedToSend->getSequenceNumber()) && (s->getMachineID() == localMachineId)) {
isInserted = true;
break;
}
}
- for (Slot *s : newSlots) {
+ for (uint si = 0; si < newSlots->length(); si++) {
+ Slot *s = newSlots->get(si);
if (isInserted) {
break;
}
for (Entry *entry : s->getEntries()) {
if (entry->getType() == TypeLastMessage) {
- LastMessage lastMessage = (LastMessage)entry;
+ LastMessage *lastMessage = (LastMessage *)entry;
if ((lastMessage->getMachineID() == localMachineId) && (lastMessage->getSequenceNumber() == lastSlotAttemptedToSend->getSequenceNumber())) {
isInserted = true;
break;
// If there is a new key with same name then end
- if ((newKey != NULL) && (arbitratorTable->get(newKey->getKey()) != NULL)) {
+ if ((newKey != NULL) && arbitratorTable->contains(newKey->getKey())) {
return false;
}
lastInsertedNewKey = insertedNewKey;
lastNewSize = newSize;
lastNewKey = newKey;
- lastTransactionPartsSent = new Hashtable<Transaction*, Vector<int32_t>* >(transactionPartsSent);
- lastPendingSendArbitrationEntriesToDelete = new Vector<Entry*>(pendingSendArbitrationEntriesToDelete);
+ lastTransactionPartsSent = new Hashtable<Transaction *, Vector<int32_t> * >(transactionPartsSent);
+ lastPendingSendArbitrationEntriesToDelete = new Vector<Entry *>(pendingSendArbitrationEntriesToDelete);
- ThreeTuple<bool, bool, Array<Slot*> *> sendSlotsReturn = sendSlotsToServer(slot, newSize, newKey != NULL);
+ ThreeTuple<bool, bool, Array<Slot *> *> sendSlotsReturn = sendSlotsToServer(slot, newSize, newKey != NULL);
if (sendSlotsReturn->getFirst()) {
}
// Remove the aborts and commit parts that were sent from the pending to send queue
- for (Iterator<ArbitrationRound> iter = pendingSendArbitrationRounds->iterator(); iter->hasNext(); ) {
- ArbitrationRound round = iter->next();
+ for (Iterator<ArbitrationRound *> *iter = pendingSendArbitrationRounds->iterator(); iter->hasNext(); ) {
+ ArbitrationRound *round = iter->next();
round->removeParts(pendingSendArbitrationEntriesToDelete);
if (round->isDoneSending()) {
return newKey == NULL;
}
- bool Table::updateFromLocal(int64_t machineId) {
- Pair<IoTString*, int32_t> localCommunicationInformation = localCommunicationTable->get(machineId);
+bool Table::updateFromLocal(int64_t machineId) {
+ Pair<IoTString *, int32_t> localCommunicationInformation = localCommunicationTable->get(machineId);
if (localCommunicationInformation == NULL) {
// Cant talk to that device locally so do nothing
return false;
}
Array<char> *sendData = new Array<char>(sendDataSize);
- ByteBuffer * bbEncode = ByteBuffer_wrap(sendData);
+ ByteBuffer *bbEncode = ByteBuffer_wrap(sendData);
// Encode the data
bbEncode->putLong(lastArbitrationDataLocalSequenceNumber);
Pair<bool, bool> Table::sendTransactionToLocal(Transaction *transaction) {
// Get the devices local communications
- Pair<IoTString*, int32_t> localCommunicationInformation = localCommunicationTable->get(transaction->getArbitrator());
+ Pair<IoTString *, int32_t> localCommunicationInformation = localCommunicationTable->get(transaction->getArbitrator());
if (localCommunicationInformation == NULL) {
// Cant talk to that device locally so do nothing
for (int i = 0; i < numberOfEntries; i++) {
char type = bbDecode->get();
if (type == TypeAbort) {
- Abort abort = (Abort)Abort_decode(NULL, bbDecode);
+ Abort *abort = (Abort)Abort_decode(NULL, bbDecode);
if ((abort->getTransactionMachineId() == localMachineId) && (abort->getTransactionClientLocalSequenceNumber() == transaction->getClientLocalSequenceNumber())) {
foundAbort = true;
return new Pair<bool, bool>(false, true);
}
- Array<char> *Table::acceptDataFromLocal(Array<char> *data) {
+Array<char> *Table::acceptDataFromLocal(Array<char> *data) {
// Decode the data
ByteBuffer *bbDecode = ByteBuffer_wrap(data);
Transaction *transaction = new Transaction();
for (int i = 0; i < numberOfParts; i++) {
bbDecode->get();
- TransactionPart newPart = (TransactionPart)TransactionPart.decode(NULL, bbDecode);
+ TransactionPart *newPart = (TransactionPart)TransactionPart.decode(NULL, bbDecode);
transaction->addPartDecode(newPart);
}
// The data to send back
int returnDataSize = 0;
- Vector<Entry*> *unseenArbitrations = new Vector<Entry*>();
+ Vector<Entry *> *unseenArbitrations = new Vector<Entry *>();
// Get the aborts to send back
Vector<int64_t> *abortLocalSequenceNumbers = new Vector<int64_t>(liveAbortsGeneratedByLocal->keySet());
continue;
}
- Abort abort = liveAbortsGeneratedByLocal->get(localSequenceNumber);
+ Abort *abort = liveAbortsGeneratedByLocal->get(localSequenceNumber);
unseenArbitrations->add(abort);
returnDataSize += abort->getSize();
}
// Get the commits to send back
- Hashtable<int64_t, Commit*>* commitForClientTable = liveCommitsTable->get(localMachineId);
+ Hashtable<int64_t, Commit *> *commitForClientTable = liveCommitsTable->get(localMachineId);
if (commitForClientTable != NULL) {
Vector<int64_t> *commitLocalSequenceNumbers = new Vector<int64_t>(commitForClientTable->keySet());
Collections->sort(commitLocalSequenceNumbers);
for (int64_t localSequenceNumber : commitLocalSequenceNumbers) {
- Commit commit = commitForClientTable->get(localSequenceNumber);
+ Commit *commit = commitForClientTable->get(localSequenceNumber);
if (localSequenceNumber <= lastArbitratedSequenceNumberSeen) {
continue;
return returnData;
}
-ThreeTuple<bool, bool, Array<Slot*> *> * Table::sendSlotsToServer(Slot *slot, int newSize, bool isNewKey) {
+ThreeTuple<bool, bool, Array<Slot *> *> *Table::sendSlotsToServer(Slot *slot, int newSize, bool isNewKey) {
bool attemptedToSendToServerTmp = attemptedToSendToServer;
attemptedToSendToServer = true;
bool inserted = false;
bool lastTryInserted = false;
- Array<Slot*> *array = cloud->putSlot(slot, newSize);
+ Array<Slot *> *array = cloud->putSlot(slot, newSize);
if (array == NULL) {
- array = new Array<Slot*>();
+ array = new Array<Slot *>();
array->set(0, slot);
rejectedSlotVector->clear();
inserted = true;
for (Entry *entry : s->getEntries()) {
if (entry->getType() == TypeLastMessage) {
- LastMessage lastMessage = (LastMessage)entry;
+ LastMessage *lastMessage = (LastMessage *)entry;
if ((lastMessage->getMachineID() == localMachineId) && (lastMessage->getSequenceNumber() == slot->getSequenceNumber())) {
isInserted = true;
}
}
- return new ThreeTuple<bool, bool, Array<Slot*> *>(inserted, lastTryInserted, array);
+ return new ThreeTuple<bool, bool, Array<Slot *> *>(inserted, lastTryInserted, array);
}
/**
* Returns false if a resize was needed
*/
-ThreeTuple<bool, int32_t, bool> *Table::fillSlot(Slot *slot, bool resize, NewKey * newKeyEntry) {
+ThreeTuple<bool, int32_t, bool> *Table::fillSlot(Slot *slot, bool resize, NewKey *newKeyEntry) {
int newSize = 0;
transactionPartsSent->clear();
pendingSendArbitrationEntriesToDelete->clear();
- for (ArbitrationRound round : pendingSendArbitrationRounds) {
+ for (ArbitrationRound *round : pendingSendArbitrationRounds) {
bool isFull = false;
round->generateParts();
- Vector<Entry*>* parts = round->getParts();
+ Vector<Entry *> *parts = round->getParts();
// Insert pending arbitration data
- for (Entry arbitrationData : parts) {
+ for (Entry *arbitrationData : parts) {
// If it is an abort then we need to set some information
if (arbitrationData instanceof Abort) {
- ((Abort)arbitrationData)->setSequenceNumber(slot->getSequenceNumber());
+ ((Abort *)arbitrationData)->setSequenceNumber(slot->getSequenceNumber());
}
if (!slot->hasSpace(arbitrationData)) {
if (slot->hasSpace(part)) {
slot->addEntry(part);
- Vector<int32_t>* partsSent = transactionPartsSent->get(transaction);
+ Vector<int32_t> *partsSent = transactionPartsSent->get(transaction);
if (partsSent == NULL) {
partsSent = new Vector<int32_t>();
transactionPartsSent->put(transaction, partsSent);
int64_t old_seqn = rejectedSlotVector->firstElement();
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);
+ RejectedMessage *rm = new RejectedMessage(s, s->getSequenceNumber(), localMachineId, old_seqn, new_seqn, false);
s->addEntry(rm);
} else {
int64_t prev_seqn = -1;
}
/* Generate rejected message entry for missing messages */
if (prev_seqn != -1) {
- RejectedMessage rm = new RejectedMessage(s, s->getSequenceNumber(), localMachineId, old_seqn, prev_seqn, false);
+ RejectedMessage *rm = new RejectedMessage(s, s->getSequenceNumber(), localMachineId, old_seqn, prev_seqn, false);
s->addEntry(rm);
}
/* Generate rejected message entries for present messages */
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);
+ RejectedMessage *rm = new RejectedMessage(s, s->getSequenceNumber(), machineid, curr_seqn, curr_seqn, true);
s->addEntry(rm);
}
}
seenLiveSlot = true;
// Get all the live entries for a slot
- Vector<Entry*>* liveEntries = previousSlot->getLiveEntries(resize);
+ Vector<Entry *> *liveEntries = previousSlot->getLiveEntries(resize);
// Iterate over all the live entries and try to rescue them
- for (Entry liveEntry : liveEntries) {
+ for (Entry *liveEntry : liveEntries) {
if (slot->hasSpace(liveEntry)) {
// Enough space to rescue the entry
int64_t newestseqnum = buffer->getNewestSeqNum();
search:
for (; seqn <= newestseqnum; seqn++) {
- Slot prevslot = buffer->getSlot(seqn);
+ Slot *prevslot = buffer->getSlot(seqn);
//Push slot number forward
if (!seenliveslot)
oldestLiveSlotSequenceNumver = seqn;
if (!prevslot->isLive())
continue;
seenliveslot = true;
- Vector<Entry*>* liveentries = prevslot->getLiveEntries(resize);
+ Vector<Entry *> *liveentries = prevslot->getLiveEntries(resize);
for (Entry *liveentry : liveentries) {
if (s->hasSpace(liveentry))
s->addEntry(liveentry);
/**
* Checks for malicious activity and updates the local copy of the block chain->
*/
-void Table::validateAndUpdate(Array<Slot*> *newSlots, bool acceptUpdatesToLocal) {
+void Table::validateAndUpdate(Array<Slot *> *newSlots, bool acceptUpdatesToLocal) {
// The cloud communication layer has checked slot HMACs already before decoding
if (newSlots->length() == 0) {
// 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);
+ SlotIndexer *indexer = new SlotIndexer(newSlots, buffer);
// Check that the HMAC chain is not broken
checkHMACChain(indexer, newSlots);
// Resize the local slot buffer
if (numberOfSlots != currMaxSize) {
- buffer->resize((int)currMaxSize);
+ buffer->resize((int32_t)currMaxSize);
}
// Change the number of local slots to the new size
- numberOfSlots = (int)currMaxSize;
+ numberOfSlots = (int32_t)currMaxSize;
// Recalculate the resize threshold since the size of the local buffer has changed
// Iterate through all the machine Ids that we received new parts for
for (int64_t machineId : newTransactionParts->keySet()) {
- Hashtable<Pair<int64_t int32_t>*, TransactionPart*> * parts = newTransactionParts->get(machineId);
+ Hashtable<Pair<int64_t int32_t> *, TransactionPart *> *parts = newTransactionParts->get(machineId);
// Iterate through all the parts for that machine Id
- for (Pair<int64_t, int32_t>* partId : parts->keySet()) {
+ for (Pair<int64_t, int32_t> *partId : parts->keySet()) {
TransactionPart *part = parts->get(partId);
int64_t lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable->get(part->getArbitratorId());
Collections->sort(transactionSequenceNumbers);
// Collection of key value pairs that are
- Hashtable<IoTString *, KeyValue*> speculativeTableTmp = new Hashtable<IoTString *, KeyValue*>();
+ Hashtable<IoTString *, KeyValue *> speculativeTableTmp = new Hashtable<IoTString *, KeyValue *>();
// The last transaction arbitrated on
int64_t lastTransactionCommitted = -1;
- Hashset<Abort*>* generatedAborts = new Hashset<Abort*>();
+ Hashset<Abort *> *generatedAborts = new Hashset<Abort *>();
for (int64_t transactionSequenceNumber : transactionSequenceNumbers) {
Transaction *transaction = liveTransactionBySequenceNumberTable->get(transactionSequenceNumber);
// Guard evaluated was false so create abort
// Create the abort
- Abort newAbort = new Abort(NULL,
- transaction->getClientLocalSequenceNumber(),
- transaction->getSequenceNumber(),
- transaction->getMachineId(),
- transaction->getArbitrator(),
- localArbitrationSequenceNumber);
+ Abort *newAbort = new Abort(NULL,
+ transaction->getClientLocalSequenceNumber(),
+ transaction->getSequenceNumber(),
+ transaction->getMachineId(),
+ transaction->getArbitrator(),
+ localArbitrationSequenceNumber);
localArbitrationSequenceNumber++;
generatedAborts->add(newAbort);
// liveTransactionBySequenceNumberTable->remove(transactionSequenceNumber);
}
- Commit newCommit = NULL;
+ Commit *newCommit = NULL;
// If there is something to commit
if (speculativeTableTmp->size() != 0) {
// 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()) {
+ for (CommitPart *commitPart : newCommit->getParts()->values()) {
processEntry(commitPart);
}
}
if ((newCommit != NULL) || (generatedAborts->size() > 0)) {
- ArbitrationRound arbitrationRound = new ArbitrationRound(newCommit, generatedAborts);
+ ArbitrationRound *arbitrationRound = new ArbitrationRound(newCommit, generatedAborts);
pendingSendArbitrationRounds->add(arbitrationRound);
if (compactArbitrationData()) {
- ArbitrationRound newArbitrationRound = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - 1);
+ ArbitrationRound *newArbitrationRound = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - 1);
if (newArbitrationRound->getCommit() != NULL) {
- for (CommitPart commitPart : newArbitrationRound->getCommit()->getParts()->values()) {
+ for (CommitPart *commitPart : newArbitrationRound->getCommit()->getParts()->values()) {
processEntry(commitPart);
}
}
// Guard evaluated as true
// Create the commit and increment the commit sequence number
- Commit newCommit = new Commit(localArbitrationSequenceNumber, localMachineId, -1);
+ Commit *newCommit = new Commit(localArbitrationSequenceNumber, localMachineId, -1);
localArbitrationSequenceNumber++;
// Update the local changes so we can make the commit
pendingSendArbitrationRounds->add(arbitrationRound);
if (compactArbitrationData()) {
- ArbitrationRound newArbitrationRound = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - 1);
- for (CommitPart commitPart : newArbitrationRound->getCommit()->getParts()->values()) {
+ 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()) {
+ for (CommitPart *commitPart : newCommit->getParts()->values()) {
processEntry(commitPart);
}
}
if (transaction->getMachineId() == localMachineId) {
- TransactionStatus status = transaction->getTransactionStatus();
+ TransactionStatus *status = transaction->getTransactionStatus();
if (status != NULL) {
status->setStatus(TransactionStatus_StatusCommitted);
}
status->setStatus(TransactionStatus_StatusAborted);
}
} else {
- Hashset<Abort *> addAbortSet = new Hashset<Abort * >();
+ Hashset<Abort *> *addAbortSet = new Hashset<Abort * >();
// Create the abort
- Abort newAbort = new Abort(NULL,
- transaction->getClientLocalSequenceNumber(),
- -1,
- transaction->getMachineId(),
- transaction->getArbitrator(),
- localArbitrationSequenceNumber);
+ 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);
+ ArbitrationRound *arbitrationRound = new ArbitrationRound(NULL, addAbortSet);
pendingSendArbitrationRounds->add(arbitrationRound);
if (compactArbitrationData()) {
- ArbitrationRound newArbitrationRound = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - 1);
- for (CommitPart commitPart : newArbitrationRound->getCommit()->getParts()->values()) {
+ ArbitrationRound *newArbitrationRound = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - 1);
+ for (CommitPart *commitPart : newArbitrationRound->getCommit()->getParts()->values()) {
processEntry(commitPart);
}
}
return false;
}
- ArbitrationRound lastRound = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - 1);
+ ArbitrationRound *lastRound = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - 1);
if (lastRound->didSendPart()) {
return false;
}
int numberToDelete = 1;
while (numberToDelete < pendingSendArbitrationRounds->size()) {
- ArbitrationRound round = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - numberToDelete - 1);
+ ArbitrationRound *xs round = pendingSendArbitrationRounds->get(pendingSendArbitrationRounds->size() - numberToDelete - 1);
if (round->isFull() || round->didSendPart()) {
// Stop since there is a part that cannot be compacted and we need to compact in order
// Iterate through all the machine Ids that we received new parts for
for (int64_t machineId : newCommitParts->keySet()) {
- Hashtable<Pair<int64_t, int32_t>*, CommitPart*>* parts = newCommitParts->get(machineId);
+ Hashtable<Pair<int64_t, int32_t> *, CommitPart *> *parts = newCommitParts->get(machineId);
// Iterate through all the parts for that machine Id
- for (Pair<int64_t, int32_t>* partId : parts->keySet()) {
- CommitPart part = parts->get(partId);
+ for (Pair<int64_t, int32_t> *partId : parts->keySet()) {
+ CommitPart *part = parts->get(partId);
// Get the transaction object for that sequence number
- Hashtable<int64_t, Commit*>* commitForClientTable = liveCommitsTable->get(part->getMachineId());
+ Hashtable<int64_t, Commit *> *commitForClientTable = liveCommitsTable->get(part->getMachineId());
if (commitForClientTable == NULL) {
// This is the first commit from this device
- commitForClientTable = new Hashtable<int64_t, Commit*>();
+ commitForClientTable = new Hashtable<int64_t, Commit *>();
liveCommitsTable->put(part->getMachineId(), commitForClientTable);
}
- Commit commit = commitForClientTable->get(part->getSequenceNumber());
+ Commit *commit = commitForClientTable->get(part->getSequenceNumber());
if (commit == NULL) {
// This is a new commit that we dont have so make a new one
for (int64_T arbitratorId : liveCommitsTable->keySet()) {
// Get all the commits for a specific arbitrator
- Hashtable<int64_t, Commit*> commitForClientTable = liveCommitsTable->get(arbitratorId);
+ Hashtable<int64_t, Commit *> *commitForClientTable = liveCommitsTable->get(arbitratorId);
// Sort the commits in order
- Vector<int64_t>* commitSequenceNumbers = new Vector<int64_t>(commitForClientTable->keySet());
+ Vector<int64_t> *commitSequenceNumbers = new Vector<int64_t>(commitForClientTable->keySet());
Collections->sort(commitSequenceNumbers);
// Get the last commit seen from this arbitrator
commitsToEdit->remove(NULL); // remove NULL since it could be in this set
// Update each previous commit that needs to be updated
- for (Commit * previousCommit : commitsToEdit) {
+ for (Commit *previousCommit : commitsToEdit) {
// Only bother with live commits (TODO: Maybe remove this check)
if (previousCommit->isLive()) {
// Update which keys in the old commits are still live
- for (KeyValue * kv : commit->getKeyValueUpdateSet()) {
+ for (KeyValue *kv : commit->getKeyValueUpdateSet()) {
previousCommit->invalidateKey(kv->getKey());
}
}
// Create a list of the transaction sequence numbers and sort them from oldest to newest
- Vector<int64_t>* transactionSequenceNumbersSorted = new Vector<int64_t>(liveTransactionBySequenceNumberTable->keySet());
+ Vector<int64_t> *transactionSequenceNumbersSorted = new Vector<int64_t>(liveTransactionBySequenceNumberTable->keySet());
Collections->sort(transactionSequenceNumbersSorted);
bool hasGapInTransactionSequenceNumbers = transactionSequenceNumbersSorted->get(0) != oldestTransactionSequenceNumberSpeculatedOn;
return;
}
-
if (didProcessNewCommitsOrSpeculate || (firstPendingTransaction != pendingTransactionQueue->get(0))) {
// need to reset on the pending speculation
lastPendingTransactionSpeculatedOn = NULL;
void Table::updateLiveTransactionsAndStatus() {
// Go through each of the transactions
- for (Iterator<Map->Entry<int64_t, Transaction> >* iter = liveTransactionBySequenceNumberTable->entrySet()->iterator(); iter->hasNext();) {
+ for (Iterator<Map->Entry<int64_t, Transaction> > *iter = liveTransactionBySequenceNumberTable->entrySet()->iterator(); iter->hasNext();) {
Transaction *transaction = iter->next()->getValue();
// Check if the transaction is dead
}
// Go through each of the transactions
- for (Iterator<Map->Entry<int64_t, TransactionStatus*> >* iter = outstandingTransactionStatus->entrySet()->iterator(); iter->hasNext();) {
- TransactionStatus status = iter->next()->getValue();
+ for (Iterator<Map->Entry<int64_t, TransactionStatus *> > *iter = outstandingTransactionStatus->entrySet()->iterator(); iter->hasNext();) {
+ TransactionStatus *status = iter->next()->getValue();
// Check if the transaction is dead
int64_t lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable->get(status->getTransactionArbitrator());
/**
* Process this slot, entry by entry-> Also update the latest message sent by slot
*/
-void Table::processSlot(SlotIndexer indexer, Slot *slot, bool acceptUpdatesToLocal, Hashset<int64_t> *machineSet) {
+void Table::processSlot(SlotIndexer *indexer, Slot *slot, bool acceptUpdatesToLocal, Hashset<int64_t> *machineSet) {
// Update the last message seen
updateLastMessage(slot->getMachineID(), slot->getSequenceNumber(), slot, acceptUpdatesToLocal, machineSet);
switch (entry->getType()) {
case TypeCommitPart:
- processEntry((CommitPart)entry);
+ processEntry((CommitPart *)entry);
break;
case TypeAbort:
- processEntry((Abort)entry);
+ processEntry((Abort *)entry);
break;
case TypeTransactionPart:
- processEntry((TransactionPart)entry);
+ processEntry((TransactionPart *)entry);
break;
case TypeNewKey:
- processEntry((NewKey)entry);
+ processEntry((NewKey *)entry);
break;
case TypeLastMessage:
- processEntry((LastMessage)entry, machineSet);
+ processEntry((LastMessage *)entry, machineSet);
break;
case TypeRejectedMessage:
- processEntry((RejectedMessage)entry, indexer);
+ processEntry((RejectedMessage *)entry, indexer);
break;
case TypeTableStatus:
- processEntry((TableStatus)entry, slot->getSequenceNumber());
+ processEntry((TableStatus *)entry, slot->getSequenceNumber());
break;
default:
/**
* Update the last message that was sent for a machine Id
*/
-void Table::processEntry(LastMessage entry, Hashset<int64_t> *machineSet) {
+void Table::processEntry(LastMessage *entry, Hashset<int64_t> *machineSet) {
// Update what the last message received by a machine was
updateLastMessage(entry->getMachineID(), entry->getSequenceNumber(), entry, false, machineSet);
}
/**
* 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) {
+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);
+ NewKey *oldNewKey = liveNewKeyTable->put(entry->getKey(), entry);
if (oldNewKey != NULL) {
// Delete the old new key messages
oldNewKey->setDead();
/**
* Check old messages to see if there is a block chain violation-> Also
*/
-void Table::processEntry(RejectedMessage entry, SlotIndexer indexer) {
+void Table::processEntry(RejectedMessage *entry, SlotIndexer *indexer) {
int64_t oldSeqNum = entry->getOldSeqNum();
int64_t newSeqNum = entry->getNewSeqNum();
bool isequal = entry->getEqual();
// Create a list of clients to watch until they see this rejected message entry->
Hashset<int64_t> *deviceWatchSet = new Hashset<int64_t>();
- for (Map->Entry<int64_t, Pair<int64_t, Liveness>*>* lastMessageEntry : lastMessageTable->entrySet()) {
+ for (Map->Entry<int64_t, Pair<int64_t, Liveness *> *> *lastMessageEntry : lastMessageTable->entrySet()) {
// Machine ID for the last message entry
int64_t lastMessageEntryMachineId = lastMessageEntry->getKey();
continue;
}
- Pair<int64_t, Liveness> *lastMessageValue = lastMessageEntry->getValue();
+ Pair<int64_t, Liveness *> *lastMessageValue = lastMessageEntry->getValue();
int64_t entrySequenceNumber = lastMessageValue->getFirst();
if (entrySequenceNumber < seq) {
* 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) {
+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());
+ TransactionStatus *status = outstandingTransactionStatus->remove(entry->getTransactionSequenceNumber());
if (status != NULL) {
status->setStatus(TransactionStatus_StatusAborted);
}
}
// 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);
+ 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
}
return;
}
-
-
-
// Update the last arbitration data that we have seen so far
if (lastArbitrationDataLocalSequenceNumberSeenFromArbitrator->get(entry->getTransactionArbitrator()) != NULL) {
/**
* Set dead the transaction part if that transaction is dead and keep track of all new parts
*/
-void Table::processEntry(TransactionPart entry) {
+void Table::processEntry(TransactionPart *entry) {
// Check if we have already seen this transaction and set it dead OR if it is not alive
int64_t lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable->get(entry->getArbitratorId());
if ((lastTransactionNumber != NULL) && (lastTransactionNumber >= entry->getSequenceNumber())) {
}
// This part is still alive
- Hashtable<Pair<int64_t, int32_t>*, TransactionPart*>* transactionPart = newTransactionParts->get(entry->getMachineId());
+ Hashtable<Pair<int64_t, int32_t> *, TransactionPart *> *transactionPart = newTransactionParts->get(entry->getMachineId());
if (transactionPart == NULL) {
// Dont have a table for this machine Id yet so make one
- transactionPart = new Hashtable<Pair<int64_t, int32_t>*, TransactionPart*>();
+ transactionPart = new Hashtable<Pair<int64_t, int32_t> *, TransactionPart *>();
newTransactionParts->put(entry->getMachineId(), transactionPart);
}
/**
* Process new commit entries and save them for future use-> Delete duplicates
*/
-void Table::processEntry(CommitPart entry) {
-
-
+void Table::processEntry(CommitPart *entry) {
// Update the last transaction that was updated if we can
if (entry->getTransactionSequenceNumber() != -1) {
int64_t lastTransactionNumber = lastArbitratedTransactionNumberByArbitratorTable->get(entry->getMachineId());
- Hashtable<Pair<int64_t, int32_t>*, CommitPart*>* commitPart = newCommitParts->get(entry->getMachineId());
+ Hashtable<Pair<int64_t, int32_t> *, CommitPart *> *commitPart = newCommitParts->get(entry->getMachineId());
if (commitPart == NULL) {
// Don't have a table for this machine Id yet so make one
- commitPart = new Hashtable<Pair<int64_t, int32_t>*, CommitPart*>();
+ commitPart = new Hashtable<Pair<int64_t, int32_t> *, CommitPart *>();
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);
+ CommitPart *previouslySeenPart = commitPart->put(entry->getPartId(), entry);
if (previouslySeenPart != NULL) {
previouslySeenPart->setDead();
}
* 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<intr64_t> *machineSet) {
+void Table::updateLastMessage(int64_t machineId, int64_t seqNum, Liveness *liveness, bool acceptUpdatesToLocal, Hashset<intr64_t> *machineSet) {
// We have seen this machine ID
machineSet->remove(machineId);
if (watchset != NULL) {
// Go through each rejected message that this machine Id has not seen yet
- for (Iterator<RejectedMessage> rmit = watchset->iterator(); rmit->hasNext(); ) {
+ for (Iterator<RejectedMessage *> *rmit = watchset->iterator(); rmit->hasNext(); ) {
- RejectedMessage rm = rmit->next();
+ RejectedMessage *rm = rmit->next();
// If this machine Id has seen this rejected message->->->
if (rm->getSequenceNumber() <= seqNum) {
}
// Set dead the abort
- for (Iterator<Map->Entry<Pair<int64_t, int64_t>*, Abort*> > i = liveAbortTable->entrySet()->iterator(); i->hasNext();) {
- Abort abort = i->next()->getValue();
+ for (Iterator<Map->Entry<Pair<int64_t, int64_t> *, Abort *> > i = liveAbortTable->entrySet()->iterator(); i->hasNext();) {
+ Abort *abort = i->next()->getValue();
if ((abort->getTransactionMachineId() == machineId) && (abort->getSequenceNumber() <= seqNum)) {
abort->setDead();
if (machineId == localMachineId) {
// Our own messages are immediately dead->
if (liveness instanceof LastMessage) {
- ((LastMessage)liveness)->setDead();
+ ((LastMessage *)liveness)->setDead();
} else if (liveness instanceof Slot) {
- ((Slot)liveness)->setDead();
+ ((Slot *)liveness)->setDead();
} else {
throw new Error("Unrecognized type");
}
}
// Get the old last message for this device
- Pair<int64_t, Liveness*> lastMessageEntry = lastMessageTable->put(machineId, new Pair<int64_t, Liveness*>(seqNum, liveness));
+ Pair<int64_t, Liveness *> *lastMessageEntry = lastMessageTable->put(machineId, new Pair<int64_t, Liveness *>(seqNum, liveness));
if (lastMessageEntry == NULL) {
// If no last message then there is nothing else to process
return;
}
int64_t lastMessageSeqNum = lastMessageEntry->getFirst();
- Liveness lastEntry = lastMessageEntry->getSecond();
+ Liveness *lastEntry = lastMessageEntry->getSecond();
// If it is not our machine Id since we already set ours to dead
if (machineId != localMachineId) {
if (lastEntry instanceof LastMessage) {
- ((LastMessage)lastEntry)->setDead();
+ ((LastMessage *)lastEntry)->setDead();
} else if (lastEntry instanceof Slot) {
- ((Slot)lastEntry)->setDead();
+ ((Slot *)lastEntry)->setDead();
} else {
throw new Error("Unrecognized type");
}
* 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) {
+void Table::addWatchVector(int64_t machineId, RejectedMessage *entry) {
Hashset<RejectedMessage *> *entries = rejectedMessageWatchVectorTable->get(machineId);
if (entries == NULL) {
// There is no set for this machine ID yet so create one
/**
* Check if the HMAC chain is not violated
*/
-void Table::checkHMACChain(SlotIndexer indexer, Array<Slot*> *newSlots) {
+void Table::checkHMACChain(SlotIndexer *indexer, Array<Slot *> *newSlots) {
for (int i = 0; i < newSlots->length; i++) {
- Slot currSlot = newSlots[i];
- Slot prevSlot = indexer->getSlot(currSlot->getSequenceNumber() - 1);
+ 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);
+ !prevSlot->getHMAC()->equals(currSlot->getPrevHMAC()))
+ throw new Error("Server Error: Invalid HMAC Chain");
}
}