summaryrefslogtreecommitdiff
path: root/src/common/HeartbeatMap.cc
blob: 48e6e021fc92f074b16025e1206a7f4d32398ce1 (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
// -*- 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 <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

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

#include "debug.h"
#define dout_subsys ceph_subsys_heartbeatmap
#undef dout_prefix
#define dout_prefix *_dout << "heartbeat_map "

namespace ceph {

HeartbeatMap::HeartbeatMap(CephContext *cct)
  : m_cct(cct),
    m_rwlock("HeartbeatMap::m_rwlock")
{
}

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

heartbeat_handle_d *HeartbeatMap::add_worker(string name)
{
  m_rwlock.get_write();
  ldout(m_cct, 10) << "add_worker '" << name << "'" << dendl;
  heartbeat_handle_d *h = new heartbeat_handle_d(name);
  m_workers.push_front(h);
  h->list_item = m_workers.begin();
  m_rwlock.put_write();
  return h;
}

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

bool HeartbeatMap::_check(heartbeat_handle_d *h, const char *who, time_t now)
{
  bool healthy = true;
  time_t was;

  was = h->timeout.read();
  if (was && was < now) {
    ldout(m_cct, 1) << who << " '" << h->name << "'"
		    << " had timed out after " << h->grace << dendl;
    healthy = false;
  }
  was = h->suicide_timeout.read();
  if (was && was < now) {
    ldout(m_cct, 1) << who << " '" << h->name << "'"
		    << " had suicide timed out after " << h->suicide_grace << dendl;
    assert(0 == "hit suicide timeout");
  }
  return healthy;
}

void HeartbeatMap::reset_timeout(heartbeat_handle_d *h, time_t grace, time_t suicide_grace)
{
  ldout(m_cct, 20) << "reset_timeout '" << h->name << "' grace " << grace
		   << " suicide " << suicide_grace << dendl;
  time_t now = time(NULL);
  _check(h, "reset_timeout", now);

  h->timeout.set(now + grace);
  h->grace = grace;

  if (suicide_grace)
    h->suicide_timeout.set(now + suicide_grace);
  else
    h->suicide_timeout.set(0);
  h->suicide_grace = suicide_grace;
}

void HeartbeatMap::clear_timeout(heartbeat_handle_d *h)
{
  ldout(m_cct, 20) << "clear_timeout '" << h->name << "'" << dendl;
  time_t now = time(NULL);
  _check(h, "clear_timeout", now);
  h->timeout.set(0);
  h->suicide_timeout.set(0);
}

bool HeartbeatMap::is_healthy()
{
  m_rwlock.get_read();
  time_t now = time(NULL);
  bool healthy = true;
  for (list<heartbeat_handle_d*>::iterator p = m_workers.begin();
       p != m_workers.end();
       ++p) {
    heartbeat_handle_d *h = *p;
    if (!_check(h, "is_healthy", now)) {
      healthy = false;
    }
  }
  m_rwlock.put_read();
  ldout(m_cct, 20) << "is_healthy = " << (healthy ? "healthy" : "NOT HEALTH") << dendl;
  return healthy;
}

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

}