2017-04-27 02:03:44 +03:00
/* -------------------------------------------------------------------------- */
2024-07-29 15:25:20 +03:00
/* Copyright 2002-2024, OpenNebula Project, OpenNebula Systems */
2017-04-27 02:03:44 +03:00
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); you may */
/* not use this file except in compliance with the License. You may obtain */
/* a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/* -------------------------------------------------------------------------- */
# ifndef REPLICA_MANAGER_H_
# define REPLICA_MANAGER_H_
# include <string>
# include <map>
2017-04-28 20:35:57 +03:00
# include <vector>
2017-04-27 02:03:44 +03:00
2017-05-15 00:48:46 +03:00
class ReplicaThread ;
2017-04-27 02:03:44 +03:00
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
2017-05-15 00:48:46 +03:00
// Replication Manager. This is a generic replication manager it starts, stops
// and send control events to replica threads.
2017-04-27 02:03:44 +03:00
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
class ReplicaManager
{
public :
/**
* Start the replication threads , one for each server in the zone
*/
2017-05-16 18:08:24 +03:00
void start_replica_threads ( std : : vector < int > & fids ) ;
2017-04-27 02:03:44 +03:00
/**
* Stop the replication threads ( leader becomes follower )
*/
void stop_replica_threads ( ) ;
/**
* Triggers a replication event on the replica threads
*/
void replicate ( ) ;
2017-04-27 12:12:30 +03:00
/**
* Triggers a replication event on the follower thread
* @ param follower id
*/
void replicate ( int follower ) ;
2017-04-27 02:03:44 +03:00
/**
* Deletes a replication thread for a follower ( e . g . server deleted )
* @ param follower_id server id
*/
void delete_replica_thread ( int follower_id ) ;
/**
* Adds a replication thread for a follower ( e . g . server add )
* @ param follower_id server id
*/
void add_replica_thread ( int follower_id ) ;
2017-05-15 00:48:46 +03:00
protected :
2024-06-03 12:40:24 +03:00
ReplicaManager ( ) { } ;
2017-05-15 00:48:46 +03:00
virtual ~ ReplicaManager ( )
{
stop_replica_threads ( ) ;
} ;
virtual ReplicaThread * thread_factory ( int follower_id ) = 0 ;
2017-04-27 02:03:44 +03:00
private :
/**
* The replication thread pool
*/
std : : map < int , ReplicaThread * > thread_pool ;
/**
* @ param server_id of the follower
* @ return pointer to the replica thread associated to a follower
*/
ReplicaThread * get_thread ( int server_id ) ;
} ;
2017-05-15 00:48:46 +03:00
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
// RaftReplicaManager to manage the raft replication thread pool
// -----------------------------------------------------------------------------
// -----------------------------------------------------------------------------
class RaftReplicaManager : public ReplicaManager
{
public :
2024-06-03 12:40:24 +03:00
RaftReplicaManager ( ) : ReplicaManager ( ) { } ;
2017-05-15 00:48:46 +03:00
2024-06-03 12:40:24 +03:00
virtual ~ RaftReplicaManager ( ) { } ;
2017-05-15 00:48:46 +03:00
private :
2023-02-07 10:50:30 +03:00
ReplicaThread * thread_factory ( int follower_id ) override ;
2017-05-15 00:48:46 +03:00
} ;
2017-06-03 20:24:19 +03:00
class HeartBeatManager : public ReplicaManager
{
public :
2024-06-03 12:40:24 +03:00
HeartBeatManager ( ) : ReplicaManager ( ) { } ;
2017-06-03 20:24:19 +03:00
2024-06-03 12:40:24 +03:00
virtual ~ HeartBeatManager ( ) { } ;
2017-06-03 20:24:19 +03:00
private :
2023-02-07 10:50:30 +03:00
ReplicaThread * thread_factory ( int follower_id ) override ;
2017-06-03 20:24:19 +03:00
} ;
2017-04-27 02:03:44 +03:00
# endif /*REPLICA_MANAGER_H_*/