Impala
Impalaistheopensource,nativeanalyticdatabaseforApacheHadoop.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
failure-detector.cc
Go to the documentation of this file.
1 // Copyright 2012 Cloudera Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
16 
17 #include <boost/assign.hpp>
18 #include <boost/thread.hpp>
19 
20 #include "common/logging.h"
21 
22 #include "common/names.h"
23 
24 using boost::get_system_time;
25 using boost::posix_time::time_duration;
26 using boost::system_time;
27 using namespace impala;
28 
29 static const map<FailureDetector::PeerState, string> PEER_STATE_TO_STRING =
30  boost::assign::map_list_of
31  (FailureDetector::OK, "OK")
32  (FailureDetector::SUSPECTED, "SUSPECTED")
33  (FailureDetector::UNKNOWN, "UNKNOWN")
34  (FailureDetector::FAILED, "FAILED");
35 
37  map<FailureDetector::PeerState, string>::const_iterator it =
38  PEER_STATE_TO_STRING.find(peer_state);
39  DCHECK(it != PEER_STATE_TO_STRING.end());
40  return it->second;
41 }
42 
44  const string& peer, bool seen) {
45  {
46  lock_guard<mutex> l(lock_);
47  if (seen) peer_heartbeat_record_[peer] = get_system_time();
48  }
49  return GetPeerState(peer);
50 }
51 
53  lock_guard<mutex> l(lock_);
54  map<string, system_time>::iterator heartbeat_record = peer_heartbeat_record_.find(peer);
55  if (heartbeat_record == peer_heartbeat_record_.end()) return UNKNOWN;
56 
57  time_duration duration = get_system_time() - heartbeat_record->second;
58  if (duration > failure_timeout_) {
59  return FAILED;
60  } else if (duration > suspect_timeout_) {
61  return SUSPECTED;
62  }
63 
64  return OK;
65 }
66 
67 void TimeoutFailureDetector::EvictPeer(const string& peer) {
68  lock_guard<mutex> l(lock_);
69  peer_heartbeat_record_.erase(peer);
70 }
71 
73  const string& peer, bool seen) {
74  {
75  lock_guard<mutex> l(lock_);
76  if (seen) {
77  missed_heartbeat_counts_[peer] = 0;
78  return OK;
79  } else {
81  }
82  }
83 
84  return GetPeerState(peer);
85 }
86 
88  const string& peer) {
89  lock_guard<mutex> l(lock_);
90  map<string, int32_t>::iterator heartbeat_record = missed_heartbeat_counts_.find(peer);
91 
92  if (heartbeat_record == missed_heartbeat_counts_.end()) {
93  return UNKNOWN;
94  } else if (heartbeat_record->second > max_missed_heartbeats_) {
95  return FAILED;
96  } else if (heartbeat_record->second > suspect_missed_heartbeats_) {
97  return SUSPECTED;
98  }
99 
100  return OK;
101 }
102 
104  lock_guard<mutex> l(lock_);
105  missed_heartbeat_counts_.erase(peer);
106 }
const boost::posix_time::time_duration suspect_timeout_
virtual PeerState GetPeerState(const std::string &peer)
Returns the current estimated state of a peer.
boost::mutex lock_
Protects all members.
virtual void EvictPeer(const std::string &peer)
Remove a peer from the failure detector completely.
static const map< FailureDetector::PeerState, string > PEER_STATE_TO_STRING
const boost::posix_time::time_duration failure_timeout_
virtual PeerState GetPeerState(const std::string &peer)
Returns the current estimated state of a peer.
virtual void EvictPeer(const std::string &peer)
Remove a peer from the failure detector completely.
boost::mutex lock_
Protects all members.
static const std::string & PeerStateToString(PeerState peer_state)
Utility method to convert a PeerState enum to a string.
std::map< std::string, boost::system_time > peer_heartbeat_record_
Record of last time a successful heartbeat was received.
std::map< std::string, int32_t > missed_heartbeat_counts_
Number of consecutive heartbeats missed by peer.
virtual PeerState UpdateHeartbeat(const std::string &peer, bool seen)
virtual PeerState UpdateHeartbeat(const std::string &peer, bool seen)