Statistics
| Branch: | Tag: | Revision:

one / include / FedReplicaManager.h @ 87b5e5cb

History | View | Annotate | Download (6.19 KB)

1
/* -------------------------------------------------------------------------- */
2
/* Copyright 2002-2017, OpenNebula Project, OpenNebula Systems                */
3
/*                                                                            */
4
/* Licensed under the Apache License, Version 2.0 (the "License"); you may    */
5
/* not use this file except in compliance with the License. You may obtain    */
6
/* a copy of the License at                                                   */
7
/*                                                                            */
8
/* http://www.apache.org/licenses/LICENSE-2.0                                 */
9
/*                                                                            */
10
/* Unless required by applicable law or agreed to in writing, software        */
11
/* distributed under the License is distributed on an "AS IS" BASIS,          */
12
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   */
13
/* See the License for the specific language governing permissions and        */
14
/* limitations under the License.                                             */
15
/* -------------------------------------------------------------------------- */
16

    
17
#ifndef FED_REPLICA_MANAGER_H_
18
#define FED_REPLICA_MANAGER_H_
19

    
20
#include <string>
21
#include <map>
22
#include <vector>
23

    
24
#include "ReplicaManager.h"
25
#include "ActionManager.h"
26

    
27
extern "C" void * frm_loop(void *arg);
28

    
29
class LogDB;
30
class LogDBRecord;
31

    
32
class FedReplicaManager : public ReplicaManager, ActionListener
33
{
34
public:
35

    
36
    /**
37
     *  @param d pointer to underlying DB (LogDB)
38
     */
39
    FedReplicaManager(LogDB * d);
40

    
41
    virtual ~FedReplicaManager();
42

    
43
    /**
44
     *  Sends the replication event to the replica threads. [MASTER]
45
     */
46
    void replicate(const std::string& sql)
47
    {
48
        ReplicaManager::replicate();
49
    }
50

    
51
    /**
52
     *  Updates the current index in the server and applies the command to the
53
     *  server. It also stores the record in the zone log [SLAVE]
54
     *    @param index of the record
55
     *    @param prev of index preceding this one
56
     *    @param sql command to apply to DB
57
     *    @return 0 on success, last_index if missing records, -1 on DB error
58
     */
59
    int apply_log_record(int index, int prev, const std::string& sql);
60

    
61
    /**
62
     *  Record was successfully replicated on zone, increase next index and
63
     *  send any pending records.
64
     *    @param zone_id
65
     */
66
    void replicate_success(int zone_id);
67

    
68
    /**
69
     *  Record could not be replicated on zone, decrease next index and
70
     *  send any pending records.
71
     *    @param zone_id
72
     */
73
    void replicate_failure(int zone_id, int zone_last);
74

    
75
    /**
76
     *  XML-RPC API call to replicate a log entry on slaves
77
     *     @param zone_id
78
     *     @param success status of API call
79
     *     @param last index replicate in zone slave
80
     *     @param error description if any
81
     *     @return 0 on success -1 if a xml-rpc/network error occurred
82
     */
83
    int xmlrpc_replicate_log(int zone_id, bool& success, int& last,
84
            std::string& err);
85

    
86
    /**
87
     *  Finalizes the Federation Replica Manager
88
     */
89
    void finalize()
90
    {
91
        am.finalize();
92
    }
93

    
94
    /**
95
     *  Starts the Federation Replica Manager
96
     */
97
    int start();
98

    
99
    /**
100
     *  Start the replication threads, and updates the server list of the zone
101
     */
102
    void start_replica_threads()
103
    {
104
        std::vector<int> zids;
105

    
106
        update_zones(zids);
107

    
108
        ReplicaManager::start_replica_threads(zids);
109
    }
110

    
111
    /**
112
     *  Updates the list of zones and servers in the zone. This function is
113
     *  invoked when a server becomes leader, or whenever a server is +
114
     *  added/removed to the zone
115
     *    @param zids ids of zones
116
     */
117
    void update_zones(std::vector<int>& zids);
118

    
119
    /**
120
     *  Adds a new zone to the replication list and starts the associated
121
     *  replica thread
122
     *    @param zone_id of the new zone
123
     */
124
    void add_zone(int zone_id);
125

    
126
    /**
127
     *  Deletes zone from the replication list and stops the associated
128
     *  replica thread
129
     *    @param zone_id of the zone
130
     */
131
    void delete_zone(int zone_id);
132

    
133
    /**
134
     *  @return the id of fed. replica thread
135
     */
136
    pthread_t get_thread_id() const
137
    {
138
        return frm_thread;
139
    };
140

    
141
private:
142
    friend void * frm_loop(void *arg);
143

    
144
    /**
145
     *  Creates federation replica thread objects
146
     */
147
    ReplicaThread * thread_factory(int follower_id);
148

    
149
    /**
150
     *  Thread id of the main event loop
151
     */
152
    pthread_t frm_thread;
153

    
154
    /**
155
     *  Controls access to the zone list and server data
156
     */
157
    pthread_mutex_t mutex;
158

    
159
    // -------------------------------------------------------------------------
160
    // Synchronization variables
161
    //   - xmlrpc_timeout. To timeout xml-rpc api calls to replicate log
162
    //   - zones list of zones in the federation with:
163
    //     - list of servers <id, xmlrpc endpoint>
164
    //     - next index to send to this zone
165
    // -------------------------------------------------------------------------
166
    static const time_t xmlrpc_timeout_ms;
167

    
168
    struct ZoneServers
169
    {
170
        ZoneServers(int z, unsigned int l, const std::string& s):
171
            zone_id(z), endpoint(s), next(l), last(-1){};
172

    
173
        ~ZoneServers(){};
174

    
175
        int zone_id;
176

    
177
        std::string  endpoint;
178

    
179
        int next;
180

    
181
        int last;
182
    };
183

    
184
    std::map<int, ZoneServers *> zones;
185

    
186
    LogDB * logdb;
187

    
188
    // -------------------------------------------------------------------------
189
    // Action Listener interface
190
    // -------------------------------------------------------------------------
191
    ActionManager am;
192

    
193
    /**
194
     *  Termination function
195
     */
196
    void finalize_action(const ActionRequest& ar);
197

    
198
    /**
199
     *  Get the nest record to replicate in a zone
200
     *    @param zone_id of the zone
201
     *    @param index of the next record to send
202
     *    @param sql command to replicate
203
     *    @return 0 on success, -1 otherwise
204
     */
205
    int get_next_record(int zone_id, std::string& zedp, LogDBRecord& lr);
206

    
207
};
208

    
209
#endif /*FED_REPLICA_MANAGER_H_*/
210

    
211
/* -------------------------------------------------------------------------- */
212
/* -------------------------------------------------------------------------- */