summaryrefslogtreecommitdiffstats
path: root/src/common/HeartbeatMap.cc
blob: 54442709229550cac3f9527494ef76c4e7f192de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
// vim: ts=8 sw=2 smarttab
/*
 * Ceph - scalable distributed file system
 *
 * Copyright (C) 2011 Sage Weil <sage@newdream.net>
 *
 * This is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License version 2.1, as published by the Free Software 
 * Foundation.  See file COPYING.
 * 
 */

#include <utime.h>
#include <signal.h>

#include "HeartbeatMap.h"
#include "ceph_context.h"
#include "common/errno.h"
#include "common/valgrind.h"
#include "debug.h"

#define dout_subsys ceph_subsys_heartbeatmap
#undef dout_prefix
#define dout_prefix *_dout << "heartbeat_map "

using std::chrono::duration_cast;
using std::chrono::seconds;
using std::string;

namespace ceph {

HeartbeatMap::HeartbeatMap(CephContext *cct)
  : m_cct(cct),
    m_unhealthy_workers(0),
    m_total_workers(0)
{
}

HeartbeatMap::~HeartbeatMap()
{
  ceph_assert(m_workers.empty());
}

heartbeat_handle_d *HeartbeatMap::add_worker(const string& name, pthread_t thread_id)
{
  std::unique_lock locker{m_rwlock};
  ldout(m_cct, 10) << "add_worker '" << name << "'" << dendl;
  heartbeat_handle_d *h = new heartbeat_handle_d(name);
  ANNOTATE_BENIGN_RACE_SIZED(&h->timeout, sizeof(h->timeout),
                             "heartbeat_handle_d timeout");
  ANNOTATE_BENIGN_RACE_SIZED(&h->suicide_timeout, sizeof(h->suicide_timeout),
                             "heartbeat_handle_d suicide_timeout");
  m_workers.push_front(h);
  h->list_item = m_workers.begin();
  h->thread_id = thread_id;
  return h;
}

void HeartbeatMap::remove_worker(const heartbeat_handle_d *h)
{
  std::unique_lock locker{m_rwlock};
  ldout(m_cct, 10) << "remove_worker '" << h->name << "'" << dendl;
  m_workers.erase(h->list_item);
  delete h;
}

bool HeartbeatMap::_check(const heartbeat_handle_d *h, const char *who,
			  ceph::coarse_mono_time now)
{
  bool healthy = true;
  if (auto was = h->timeout.load(std::memory_order_relaxed);
      !clock::is_zero(was) && was < now) {
    ldout(m_cct, 1) << who << " '" << h->name << "'"
		    << " had timed out after " << h->grace << dendl;
    healthy = false;
  }
  if (auto was = h->suicide_timeout.load(std::memory_order_relaxed);
      !clock::is_zero(was) && was < now) {
    ldout(m_cct, 1) << who << " '" << h->name << "'"
		    << " had suicide timed out after " << h->suicide_grace << dendl;
    pthread_kill(h->thread_id, SIGABRT);
    sleep(1);
    ceph_abort_msg("hit suicide timeout");
  }
  return healthy;
}

void HeartbeatMap::reset_timeout(heartbeat_handle_d *h,
				 ceph::timespan grace,
				 ceph::timespan suicide_grace)
{
  ldout(m_cct, 20) << "reset_timeout '" << h->name << "' grace " << grace
		   << " suicide " << suicide_grace << dendl;
  const auto now = clock::now();
  _check(h, "reset_timeout", now);

  h->timeout.store(now + grace, std::memory_order_relaxed);
  h->grace = grace;

  if (suicide_grace > ceph::timespan::zero()) {
    h->suicide_timeout.store(now + suicide_grace, std::memory_order_relaxed);
  } else {
    h->suicide_timeout.store(clock::zero(), std::memory_order_relaxed);
  }
  h->suicide_grace = suicide_grace;
}

void HeartbeatMap::clear_timeout(heartbeat_handle_d *h)
{
  ldout(m_cct, 20) << "clear_timeout '" << h->name << "'" << dendl;
  auto now = clock::now();
  _check(h, "clear_timeout", now);
  h->timeout.store(clock::zero(), std::memory_order_relaxed);
  h->suicide_timeout.store(clock::zero(), std::memory_order_relaxed);
}

bool HeartbeatMap::is_healthy()
{
  int unhealthy = 0;
  int total = 0;
  m_rwlock.lock_shared();
  auto now = ceph::coarse_mono_clock::now();
  if (m_cct->_conf->heartbeat_inject_failure) {
    ldout(m_cct, 0) << "is_healthy injecting failure for next " << m_cct->_conf->heartbeat_inject_failure << " seconds" << dendl;
    m_inject_unhealthy_until = now + std::chrono::seconds(m_cct->_conf->heartbeat_inject_failure);
    m_cct->_conf.set_val("heartbeat_inject_failure", "0");
  }

  bool healthy = true;
  if (now < m_inject_unhealthy_until) {
    auto sec = std::chrono::duration_cast<std::chrono::seconds>(m_inject_unhealthy_until - now).count();
    ldout(m_cct, 0) << "is_healthy = false, injected failure for next "
                    << sec << " seconds" << dendl;
    healthy = false;
  }

  for (auto p = m_workers.begin();
       p != m_workers.end();
       ++p) {
    heartbeat_handle_d *h = *p;
    if (!_check(h, "is_healthy", now)) {
      healthy = false;
      unhealthy++;
    }
    total++;
  }
  m_rwlock.unlock_shared();

  m_unhealthy_workers = unhealthy;
  m_total_workers = total;

  ldout(m_cct, 20) << "is_healthy = " << (healthy ? "healthy" : "NOT HEALTHY")
    << ", total workers: " << total << ", number of unhealthy: " << unhealthy << dendl;
  return healthy;
}

int HeartbeatMap::get_unhealthy_workers() const
{
  return m_unhealthy_workers;
}

int HeartbeatMap::get_total_workers() const
{
  return m_total_workers;
}

void HeartbeatMap::check_touch_file()
{
  string path = m_cct->_conf->heartbeat_file;
  if (path.length() && is_healthy()) {
    int fd = ::open(path.c_str(), O_WRONLY|O_CREAT|O_CLOEXEC, 0644);
    if (fd >= 0) {
      ::utime(path.c_str(), NULL);
      ::close(fd);
    } else {
      ldout(m_cct, 0) << "unable to touch " << path << ": "
                     << cpp_strerror(errno) << dendl;
    }
  }
}

}