forked from ceph/ceph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathceph_context.cc
195 lines (167 loc) · 4.31 KB
/
ceph_context.cc
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
185
186
187
188
189
190
191
192
193
194
195
// -*- 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 New Dream Network
*
* 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 "common/admin_socket.h"
#include "common/DoutStreambuf.h"
#include "common/perf_counters.h"
#include "common/Thread.h"
#include "common/ceph_context.h"
#include "common/config.h"
#include "common/debug.h"
#include "common/HeartbeatMap.h"
#include <iostream>
#include <pthread.h>
#include <semaphore.h>
using ceph::HeartbeatMap;
class CephContextServiceThread : public Thread
{
public:
CephContextServiceThread(CephContext *cct)
: _reopen_logs(false), _exit_thread(false), _cct(cct)
{
sem_init(&_sem, 0, 0);
};
~CephContextServiceThread()
{
sem_destroy(&_sem);
};
void *entry()
{
while (1) {
if (_cct->_conf->heartbeat_interval) {
struct timespec timeout;
clock_gettime(CLOCK_REALTIME, &timeout);
timeout.tv_sec += _cct->_conf->heartbeat_interval;
sem_timedwait(&_sem, &timeout);
} else {
sem_wait(&_sem);
}
if (_exit_thread) {
break;
}
if (_reopen_logs) {
_cct->_doss->reopen_logs(_cct->_conf);
_reopen_logs = false;
}
_cct->_heartbeat_map->check_touch_file();
}
return NULL;
}
void reopen_logs()
{
_reopen_logs = true;
sem_post(&_sem);
}
void exit_thread()
{
_exit_thread = true;
sem_post(&_sem);
}
private:
volatile bool _reopen_logs;
volatile bool _exit_thread;
sem_t _sem;
CephContext *_cct;
};
CephContext::CephContext(uint32_t module_type_)
: _conf(new md_config_t()),
_doss(new DoutStreambuf <char, std::basic_string<char>::traits_type>()),
_dout(_doss),
_module_type(module_type_),
_service_thread(NULL),
_admin_socket_config_obs(NULL),
_perf_counters_collection(NULL),
_heartbeat_map(NULL)
{
pthread_spin_init(&_service_thread_lock, PTHREAD_PROCESS_SHARED);
_perf_counters_collection = new PerfCountersCollection(this);
_conf->add_observer(_doss);
_admin_socket_config_obs = new AdminSocketConfigObs(this);
_conf->add_observer(_admin_socket_config_obs);
_heartbeat_map = new HeartbeatMap(this);
}
CephContext::~CephContext()
{
join_service_thread();
delete _heartbeat_map;
_conf->remove_observer(_admin_socket_config_obs);
_conf->remove_observer(_doss);
delete _perf_counters_collection;
_perf_counters_collection = NULL;
delete _perf_counters_conf_obs;
_perf_counters_conf_obs = NULL;
delete _doss;
_doss = NULL;
delete _conf;
pthread_spin_destroy(&_service_thread_lock);
}
void CephContext::start_service_thread()
{
pthread_spin_lock(&_service_thread_lock);
if (_service_thread) {
pthread_spin_unlock(&_service_thread_lock);
return;
}
_service_thread = new CephContextServiceThread(this);
_service_thread->create();
pthread_spin_unlock(&_service_thread_lock);
}
void CephContext::reopen_logs()
{
pthread_spin_lock(&_service_thread_lock);
if (_service_thread)
_service_thread->reopen_logs();
pthread_spin_unlock(&_service_thread_lock);
}
void CephContext::dout_lock(DoutLocker *locker)
{
pthread_mutex_t *lock = &_doss->lock;
pthread_mutex_lock(lock);
locker->lock = lock;
}
void CephContext::dout_trylock(DoutLocker *locker)
{
static const int MAX_DOUT_TRYLOCK_TRIES = 3;
/* Try a few times to get the lock. If we can't seem to get it, just give up. */
pthread_mutex_t *lock = &_doss->lock;
for (int i = 0; i < MAX_DOUT_TRYLOCK_TRIES; ++i) {
if (pthread_mutex_trylock(lock) == 0) {
locker->lock = lock;
return;
}
usleep(50000);
}
}
void CephContext::join_service_thread()
{
pthread_spin_lock(&_service_thread_lock);
CephContextServiceThread *thread = _service_thread;
if (!thread) {
pthread_spin_unlock(&_service_thread_lock);
return;
}
_service_thread = NULL;
pthread_spin_unlock(&_service_thread_lock);
thread->exit_thread();
thread->join();
delete thread;
}
uint32_t CephContext::get_module_type() const
{
return _module_type;
}
PerfCountersCollection *CephContext::get_perfcounters_collection()
{
return _perf_counters_collection;
}