summaryrefslogtreecommitdiffstats
path: root/src/journal/Journaler.cc
blob: 231cc0400d764f126727be3485baee06ef3da0cf (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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "journal/Journaler.h"
#include "include/stringify.h"
#include "common/errno.h"
#include "journal/Entry.h"
#include "journal/FutureImpl.h"
#include "journal/JournalMetadata.h"
#include "journal/JournalPlayer.h"
#include "journal/JournalRecorder.h"
#include "journal/JournalTrimmer.h"
#include "journal/ReplayEntry.h"
#include "journal/ReplayHandler.h"
#include "cls/journal/cls_journal_client.h"
#include "cls/journal/cls_journal_types.h"

#define dout_subsys ceph_subsys_journaler
#undef dout_prefix
#define dout_prefix *_dout << "Journaler: "

namespace journal {

namespace {

static const std::string JOURNAL_HEADER_PREFIX = "journal.";
static const std::string JOURNAL_OBJECT_PREFIX = "journal_data.";

struct C_DeleteRecorder : public Context {
  JournalRecorder *recorder;
  Context *on_safe;
  C_DeleteRecorder(JournalRecorder *_recorder, Context *_on_safe)
    : recorder(_recorder), on_safe(_on_safe) {
  }
  virtual void finish(int r) {
    delete recorder;
    on_safe->complete(r);
  }
};

} // anonymous namespace

using namespace cls::journal;

Journaler::Journaler(librados::IoCtx &header_ioctx,
		     const std::string &journal_id,
		     const std::string &client_id, double commit_interval)
  : m_client_id(client_id), m_metadata(NULL), m_player(NULL), m_recorder(NULL),
    m_trimmer(NULL)
{
  m_header_ioctx.dup(header_ioctx);
  m_cct = reinterpret_cast<CephContext *>(m_header_ioctx.cct());

  m_header_oid = JOURNAL_HEADER_PREFIX + journal_id;
  m_object_oid_prefix = JOURNAL_OBJECT_PREFIX +
    stringify(m_header_ioctx.get_id()) + "." + journal_id + ".";

  m_metadata = new JournalMetadata(m_header_ioctx, m_header_oid, m_client_id,
                                   commit_interval);
  m_metadata->get();
}

Journaler::~Journaler() {
  if (m_metadata != NULL) {
    m_metadata->put();
    m_metadata = NULL;
  }
  delete m_trimmer;
  assert(m_player == NULL);
  assert(m_recorder == NULL);
}

int Journaler::exists(bool *header_exists) const {
  int r = m_header_ioctx.stat(m_header_oid, NULL, NULL);
  if (r < 0 && r != -ENOENT) {
    return r;
  }

  *header_exists = (r == 0);
  return 0;
}

void Journaler::init(Context *on_init) {
  m_metadata->init(new C_InitJournaler(this, on_init));
}

int Journaler::init_complete() {
  int64_t pool_id = m_metadata->get_pool_id();

  if (pool_id < 0 || pool_id == m_header_ioctx.get_id()) {
    ldout(m_cct, 20) << "using image pool for journal data" << dendl;
    m_data_ioctx.dup(m_header_ioctx);
  } else {
    ldout(m_cct, 20) << "using pool id=" << pool_id << " for journal data"
		     << dendl;
    librados::Rados rados(m_header_ioctx);
    int r = rados.ioctx_create2(pool_id, m_data_ioctx);
    if (r < 0) {
      if (r == -ENOENT) {
	ldout(m_cct, 1) << "pool id=" << pool_id << " no longer exists"
			<< dendl;
      }
      return r;
    }
  }
  m_trimmer = new JournalTrimmer(m_data_ioctx, m_object_oid_prefix,
                                 m_metadata);
  return 0;
}

int Journaler::create(uint8_t order, uint8_t splay_width, int64_t pool_id) {
  if (order > 64 || order < 12) {
    lderr(m_cct) << "order must be in the range [12, 64]" << dendl;
    return -EDOM;
  }
  if (splay_width == 0) {
    return -EINVAL;
  }

  ldout(m_cct, 5) << "creating new journal: " << m_header_oid << dendl;
  int r = client::create(m_header_ioctx, m_header_oid, order, splay_width,
			 pool_id);
  if (r < 0) {
    lderr(m_cct) << "failed to create journal: " << cpp_strerror(r) << dendl;
    return r;
  }
  return 0;
}

int Journaler::remove() {
  m_metadata->shutdown();

  int r = m_trimmer->remove_objects();
  if (r < 0) {
    lderr(m_cct) << "failed to remove journal objects: " << cpp_strerror(r)
                 << dendl;
    return r;
  }

  r = m_header_ioctx.remove(m_header_oid);
  if (r < 0) {
    lderr(m_cct) << "failed to remove journal header: " << cpp_strerror(r)
                 << dendl;
    return r;
  }
  return 0;
}

int Journaler::register_client(const std::string &description) {
  return m_metadata->register_client(description);
}

int Journaler::unregister_client() {
  return m_metadata->unregister_client();
}

void Journaler::start_replay(ReplayHandler *replay_handler) {
  create_player(replay_handler);
  m_player->prefetch();
}

void Journaler::start_live_replay(ReplayHandler *replay_handler,
                                  double interval) {
  create_player(replay_handler);
  m_player->prefetch_and_watch(interval);
}

bool Journaler::try_pop_front(ReplayEntry *replay_entry) {
  assert(m_player != NULL);

  Entry entry;
  uint64_t commit_tid;
  if (!m_player->try_pop_front(&entry, &commit_tid)) {
    return false;
  }

  *replay_entry = ReplayEntry(entry.get_data(), commit_tid);
  return true;
}

void Journaler::stop_replay() {
  assert(m_player != NULL);
  m_player->unwatch();
  delete m_player;
  m_player = NULL;
}

void Journaler::committed(const ReplayEntry &replay_entry) {
  m_trimmer->committed(replay_entry.get_commit_tid());
}

void Journaler::committed(const Future &future) {
  FutureImplPtr future_impl = future.get_future_impl();
  m_trimmer->committed(future_impl->get_commit_tid());
}

void Journaler::start_append(int flush_interval, uint64_t flush_bytes,
			     double flush_age) {
  assert(m_recorder == NULL);

  // TODO verify active object set >= current replay object set

  m_recorder = new JournalRecorder(m_data_ioctx, m_object_oid_prefix,
				   m_metadata, flush_interval, flush_bytes,
				   flush_age);
}

void Journaler::stop_append(Context *on_safe) {
  assert(m_recorder != NULL);

  flush(new C_DeleteRecorder(m_recorder, on_safe));
  m_recorder = NULL;
}

Future Journaler::append(const std::string &tag, const bufferlist &payload_bl) {
  return m_recorder->append(tag, payload_bl);
}

void Journaler::flush(Context *on_safe) {
  m_recorder->flush(on_safe);
}

void Journaler::create_player(ReplayHandler *replay_handler) {
  assert(m_player == NULL);
  m_player = new JournalPlayer(m_data_ioctx, m_object_oid_prefix, m_metadata,
                               replay_handler);
}

std::ostream &operator<<(std::ostream &os,
			 const Journaler &journaler) {
  os << "[metadata=";
  if (journaler.m_metadata != NULL) {
    os << *journaler.m_metadata;
  } else {
    os << "NULL";
  }
  os << "]";
  return os;
}

} // namespace journal