diff --git a/include/RaftManager.h b/include/RaftManager.h index c4af7e2b89..4bb0522193 100644 --- a/include/RaftManager.h +++ b/include/RaftManager.h @@ -92,12 +92,12 @@ public: void replicate_log(ReplicaRequest * rr); /** - * @return true if there is a replication request set (i.e. thread waiting - * for the replication) + * Allocate a replica request fot the given index. + * @param rindex of the record for the request */ - bool is_replica_request_set(int rindex) + void replicate_allocate(int rindex) { - return requests.is_set(rindex); + requests.allocate(rindex); } /** diff --git a/include/ReplicaRequest.h b/include/ReplicaRequest.h index b6724b16f0..d04f3fd71b 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() ) + if ( it != requests.end() && it->second != 0 ) { it->second->add_replica(); @@ -152,6 +152,25 @@ 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. @@ -168,6 +187,10 @@ public: { requests.insert(std::make_pair(rindex, rr)); } + else if ( it->second == 0 ) + { + it->second = rr; + } pthread_mutex_unlock(&mutex); } @@ -218,7 +241,7 @@ public: bool rc = true; - if ( it != requests.end() ) + if ( it != requests.end() && it->second == 0 ) { rc = false; } @@ -228,25 +251,6 @@ public: return rc; } - /** - * @return true if a thread is ready and waiting for this request index - * (rindex) - */ - bool is_set(int rindex) - { - bool ready = false; - - pthread_mutex_lock(&mutex); - - std::map::iterator it = requests.find(rindex); - - ready = it != requests.end(); - - pthread_mutex_unlock(&mutex); - - return ready; - } - private: pthread_mutex_t mutex; diff --git a/src/raft/ReplicaThread.cc b/src/raft/ReplicaThread.cc index a27c1d712e..b4084cd6fa 100644 --- a/src/raft/ReplicaThread.cc +++ b/src/raft/ReplicaThread.cc @@ -186,12 +186,6 @@ int RaftReplicaThread::replicate() int next_index = raftm->get_next_index(follower_id); - //Do not trigger the log replication if the DB writer thread is not ready - if ( !raftm->is_replica_request_set(next_index) ) - { - return 0; - } - if ( logdb->get_log_record(next_index, lr) != 0 ) { ostringstream ess; @@ -206,13 +200,6 @@ int RaftReplicaThread::replicate() if ( raftm->xmlrpc_replicate_log(follower_id, &lr, success, follower_term, error) != 0 ) { - std::ostringstream oss; - - oss << "Faild to replicate log record at index: " << next_index - << " on follower: " << follower_id << ", error: " << error; - - NebulaLog::log("RCM", Log::DEBUG, oss); - return -1; } diff --git a/src/sql/LogDB.cc b/src/sql/LogDB.cc index cf25a54c79..010b11d6d5 100644 --- a/src/sql/LogDB.cc +++ b/src/sql/LogDB.cc @@ -407,6 +407,12 @@ 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;