diff --git a/include/RaftManager.h b/include/RaftManager.h index 4bb0522193..7bb5fd4f31 100644 --- a/include/RaftManager.h +++ b/include/RaftManager.h @@ -91,15 +91,6 @@ public: */ void replicate_log(ReplicaRequest * rr); - /** - * Allocate a replica request fot the given index. - * @param rindex of the record for the request - */ - void replicate_allocate(int rindex) - { - requests.allocate(rindex); - } - /** * Finalizes the Raft Consensus Manager */ diff --git a/include/ReplicaRequest.h b/include/ReplicaRequest.h index a57e4c07d9..f0fd3f5528 100644 --- a/include/ReplicaRequest.h +++ b/include/ReplicaRequest.h @@ -135,7 +135,7 @@ public: std::map::iterator it = requests.find(rindex); - if ( it != requests.end() && it->second != 0 ) + if ( it != requests.end() ) { it->second->add_replica(); @@ -152,25 +152,6 @@ public: return to_commit; } - /** - * Allocated an empty replica request. It marks a writer thread will wait - * on this request. - * @param rindex of the request - */ - void allocate(int rindex) - { - pthread_mutex_lock(&mutex); - - std::map::iterator it = requests.find(rindex); - - if ( it == requests.end() ) - { - requests.insert(std::make_pair(rindex, (ReplicaRequest*) 0)); - } - - pthread_mutex_unlock(&mutex); - } - /** * Set the replication request associated to this index. If there is no * previous request associated to the index it is created. @@ -187,28 +168,6 @@ public: { requests.insert(std::make_pair(rindex, rr)); } - else if ( it->second == 0 ) - { - it->second = rr; - } - - pthread_mutex_unlock(&mutex); - } - - /** - * Remove a replication request associated to this index - * @param rindex of the request - */ - void remove(int rindex) - { - pthread_mutex_lock(&mutex); - - std::map::iterator it = requests.find(rindex); - - if ( it != requests.end() ) - { - requests.erase(it); - } pthread_mutex_unlock(&mutex); } @@ -248,28 +207,18 @@ public: */ bool need_replica(int db_index, int rindex) { - if ( db_index <= rindex ) + bool rc = false; + + if ( db_index > rindex ) { - return false; + return true; } - pthread_mutex_lock(&mutex); - - std::map::iterator it = requests.find(rindex + 1); - - bool rc = true; - - if ( it != requests.end() && it->second == 0 ) - { - rc = false; - } - - pthread_mutex_unlock(&mutex); - return rc; } private: + pthread_mutex_t mutex; /** diff --git a/src/raft/RaftManager.cc b/src/raft/RaftManager.cc index 53f85e2566..38b2f7ba96 100644 --- a/src/raft/RaftManager.cc +++ b/src/raft/RaftManager.cc @@ -581,13 +581,24 @@ void RaftManager::replicate_log(ReplicaRequest * request) { request->notify(); - requests.remove(request->index()); - pthread_mutex_unlock(&mutex); return; } - if ( num_servers <= 1 ) + //Count servers that need to replicate this record + int to_commit = num_servers / 2; + + std::map::iterator it; + + for (it = next.begin(); it != next.end() && to_commit > 0; ++it) + { + if ( request->index() < (int) it->second ) + { + to_commit--; + } + } + + if ( to_commit <= 0 ) { request->notify(); @@ -595,12 +606,10 @@ void RaftManager::replicate_log(ReplicaRequest * request) request->timeout = false; commit = request->index(); - - requests.remove(request->index()); } else { - request->to_commit(num_servers / 2 ); + request->to_commit(to_commit); requests.set(request->index(), request); } diff --git a/src/sql/LogDB.cc b/src/sql/LogDB.cc index 010b11d6d5..cf25a54c79 100644 --- a/src/sql/LogDB.cc +++ b/src/sql/LogDB.cc @@ -407,12 +407,6 @@ int LogDB::insert_log_record(unsigned int term, std::ostringstream& sql, return -1; } - //allocate a replication request if log record is going to be replicated - if ( timestamp == 0 ) - { - Nebula::instance().get_raftm()->replicate_allocate(next_index); - } - last_index = next_index; last_term = term;