Loading...
Searching...
No Matches
viewchange_manager.h
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#pragma once
21
22#include <semaphore.h>
23
30#include "platform/proto/viewchange_message.pb.h"
31
32namespace resdb {
33
35
37 public:
39 std::string hash, uint64_t start_time,
40 uint64_t timeout_length_)
41 : type(type),
42 view(view),
44 hash(hash),
46 timeout_time(start_time + timeout_length_) {}
47
49 uint64_t view;
50 uint64_t proxy_id;
51 std::string hash;
52 uint64_t start_time;
53 uint64_t timeout_time;
54
55 bool operator<(const ViewChangeTimeout& other) const {
56 return timeout_time > other.timeout_time;
57 }
58};
59
61 public:
64 std::shared_ptr<ViewChangeTimeout> SetComplaining(std::string hash,
65 uint64_t view);
66 void ReleaseComplaining(std::string hash);
67 void set_proxy_id(uint64_t proxy_id) { this->proxy_id = proxy_id; }
68
69 uint CountViewChangeTimeout(std::string hash);
70 void EraseViewChangeTimeout(std::string hash);
71
72 protected:
73 uint64_t proxy_id;
77 std::set<std::string> viewchange_timeout_set;
78};
79
81 public:
82 ViewChangeManager(const ResDBConfig& config,
83 CheckPointManager* checkpoint_manager,
84 MessageManager* message_manager, SystemInfo* system_info,
85 ReplicaCommunicator* replica_communicator,
86 SignatureVerifier* verifier);
87 virtual ~ViewChangeManager();
88
89 int ProcessViewChange(std::unique_ptr<Context> context,
90 std::unique_ptr<Request> request);
91 int ProcessNewView(std::unique_ptr<Context> context,
92 std::unique_ptr<Request> request);
93 bool IsInViewChange();
94 // If the monitor is not running, start to monitor.
95 void MayStart();
96
103
104 void AddComplaintTimer(uint64_t proxy_id, std::string hash);
109
110 private:
111 void SendViewChangeMsg();
112 void SendNewViewMsg(uint64_t view_number);
113 bool IsValidViewChangeMsg(const ViewChangeMessage& view_change_message);
114 uint32_t AddRequest(const ViewChangeMessage& viewchange_message,
115 uint32_t sender);
116 bool IsNextPrimary(uint64_t view_number);
117 void SetCurrentViewAndNewPrimary(uint64_t view_number);
118 std::vector<std::unique_ptr<Request>> GetPrepareMsg(
119 const NewViewMessage& new_view_message, bool need_sign = true);
120
121 bool ChangeStatue(ViewChangeStatus status);
122
123 void MonitoringViewChangeTimeOut();
124 bool CheckTimeOut(ViewChangeTimeout& info);
125 void MonitoringCheckpointState();
126
127 protected:
134 std::thread monitor_thread_;
135 std::map<uint64_t, std::map<uint32_t, ViewChangeMessage>> viewchange_request_;
137 bool new_view_is_sent_ = false;
139 std::atomic<bool> started_;
141
142 std::mutex vc_mutex_;
147 // LockFreeQueue<ViewChangeTimeout> timeout_info_queue;
148 std::map<uint64_t, std::priority_queue<std::shared_ptr<ViewChangeTimeout>>>
150 std::map<uint64_t, ComplaningClients> complaining_clients_;
151 std::atomic<bool> stop_;
152 uint64_t timeout_length_ = 10000000;
153
156};
157
158} // namespace resdb
void EraseViewChangeTimeout(std::string hash)
uint CountViewChangeTimeout(std::string hash)
std::shared_ptr< ViewChangeTimeout > SetComplaining(std::string hash, uint64_t view)
void set_proxy_id(uint64_t proxy_id)
std::set< std::string > viewchange_timeout_set
void ReleaseComplaining(std::string hash)
int ProcessViewChange(std::unique_ptr< Context > context, std::unique_ptr< Request > request)
DuplicateManager * duplicate_manager_
std::map< uint64_t, std::map< uint32_t, ViewChangeMessage > > viewchange_request_
int ProcessNewView(std::unique_ptr< Context > context, std::unique_ptr< Request > request)
MessageManager * message_manager_
std::map< uint64_t, std::priority_queue< std::shared_ptr< ViewChangeTimeout > > > viewchange_timeout_min_heap_
SignatureVerifier * verifier_
std::atomic< bool > started_
std::map< uint64_t, ComplaningClients > complaining_clients_
ReplicaCommunicator * replica_communicator_
void SetDuplicateManager(DuplicateManager *manager)
LockFreeCollectorPool * collector_pool_
CheckPointManager * checkpoint_manager_
void AddComplaintTimer(uint64_t proxy_id, std::string hash)
std::thread server_checking_timeout_thread_
bool operator<(const ViewChangeTimeout &other) const
ViewChangeTimeout(ViewChangeTimerType type, uint64_t view, uint64_t proxy_id, std::string hash, uint64_t start_time, uint64_t timeout_length_)