summaryrefslogtreecommitdiffstats
path: root/src/mds/SnapServer.cc
blob: 08e27339f95dbe773676a1eecb426ae0aabaae3e (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
// -*- 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) 2004-2006 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 "SnapServer.h"
#include "MDSRank.h"
#include "osd/OSDMap.h"
#include "osdc/Objecter.h"
#include "mon/MonClient.h"

#include "include/types.h"
#include "messages/MMDSTableRequest.h"
#include "messages/MRemoveSnaps.h"

#include "msg/Messenger.h"

#include "common/config.h"
#include "include/ceph_assert.h"

#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_mds
#undef dout_prefix
#define dout_prefix *_dout << "mds." << rank << ".snap "

using namespace std;

void SnapServer::reset_state()
{
  last_snap = 1;  /* snapid 1 reserved for initial root snaprealm */
  snaps.clear();
  need_to_purge.clear();
  pending_update.clear();
  pending_destroy.clear();
  pending_noop.clear();

  // find any removed snapshot in data pools
  if (mds) {  // only if I'm running in a live MDS
    snapid_t first_free = 0;
    mds->objecter->with_osdmap([&](const OSDMap& o) {
	for (const auto p : mds->mdsmap->get_data_pools()) {
	  const pg_pool_t *pi = o.get_pg_pool(p);
	  if (!pi) {
	    // If pool isn't in OSDMap yet then can't have any snaps
	    // needing removal, skip.
	    continue;
	  }
	  if (pi->snap_seq > first_free) {
	    first_free = pi->snap_seq;
	  }
	}
      });
    if (first_free > last_snap)
      last_snap = first_free;
  }
  last_created = last_snap;
  last_destroyed = last_snap;
  snaprealm_v2_since = last_snap + 1;

  MDSTableServer::reset_state();
}


// SERVER

void SnapServer::_prepare(const bufferlist& bl, uint64_t reqid, mds_rank_t bymds, bufferlist& out)
{
  using ceph::decode;
  using ceph::encode;
  auto p = bl.cbegin();
  __u32 op;
  decode(op, p);

  switch (op) {
  case TABLE_OP_CREATE:
    {
      SnapInfo info;
      decode(info.ino, p);
      if (!p.end()) {
	decode(info.name, p);
	decode(info.stamp, p);
	info.snapid = ++last_snap;
	pending_update[version] = info;
	dout(10) << "prepare v" << version << " create " << info << dendl;
      } else {
	pending_noop.insert(version);
	dout(10) << "prepare v" << version << " noop" << dendl;
      }

      encode(last_snap, out);
    }
    break;

  case TABLE_OP_DESTROY:
    {
      inodeno_t ino;
      snapid_t snapid;
      decode(ino, p);    // not used, currently.
      decode(snapid, p);

      // bump last_snap... we use it as a version value on the snaprealm.
      ++last_snap;

      pending_destroy[version] = {snapid, last_snap};
      dout(10) << "prepare v" << version << " destroy " << snapid << " seq " << last_snap << dendl;

      encode(last_snap, out);
    }
    break;

  case TABLE_OP_UPDATE:
    {
      SnapInfo info;
      decode(info.ino, p);
      decode(info.snapid, p);
      decode(info.name, p);
      decode(info.stamp, p);

      pending_update[version] = info;
      dout(10) << "prepare v" << version << " update " << info << dendl;
    }
    break;

  default:
    ceph_abort();
  }
  //dump();
}

void SnapServer::_get_reply_buffer(version_t tid, bufferlist *pbl) const
{
  using ceph::encode;
  auto p = pending_update.find(tid);
  if (p != pending_update.end()) {
    if (pbl && !snaps.count(p->second.snapid)) // create
      encode(p->second.snapid, *pbl);
    return;
  }
  auto q = pending_destroy.find(tid);
  if (q != pending_destroy.end()) {
    if (pbl)
      encode(q->second.second, *pbl);
    return;
  }
  auto r = pending_noop.find(tid);
  if (r != pending_noop.end()) {
    if (pbl)
      encode(last_snap, *pbl);
    return;
  }
  assert (0 == "tid not found");
}

void SnapServer::_commit(version_t tid, cref_t<MMDSTableRequest> req)
{
  if (pending_update.count(tid)) {
    SnapInfo &info = pending_update[tid];
    string opname;
    if (snaps.count(info.snapid)) {
      opname = "update";
      if (info.stamp == utime_t())
	info.stamp = snaps[info.snapid].stamp;
    } else {
      opname = "create";
      if (info.snapid > last_created)
	last_created = info.snapid;
    }
    dout(7) << "commit " << tid << " " << opname << " " << info << dendl;
    snaps[info.snapid] = info;
    pending_update.erase(tid);
  }

  else if (pending_destroy.count(tid)) {
    snapid_t sn = pending_destroy[tid].first;
    snapid_t seq = pending_destroy[tid].second;
    dout(7) << "commit " << tid << " destroy " << sn << " seq " << seq << dendl;
    snaps.erase(sn);
    if (seq > last_destroyed)
      last_destroyed = seq;

    for (const auto p : mds->mdsmap->get_data_pools()) {
      need_to_purge[p].insert(sn);
      need_to_purge[p].insert(seq);
    }

    pending_destroy.erase(tid);
  }
  else if (pending_noop.count(tid)) {
    dout(7) << "commit " << tid << " noop" << dendl;
    pending_noop.erase(tid);
  }
  else
    ceph_abort();

  //dump();
}

void SnapServer::_rollback(version_t tid) 
{
  if (pending_update.count(tid)) {
    SnapInfo &info = pending_update[tid];
    string opname;
    if (snaps.count(info.snapid))
      opname = "update";
    else
      opname = "create";
    dout(7) << "rollback " << tid << " " << opname << " " << info << dendl;
    pending_update.erase(tid);
  } 

  else if (pending_destroy.count(tid)) {
    dout(7) << "rollback " << tid << " destroy " << pending_destroy[tid] << dendl;
    pending_destroy.erase(tid);
  }
  
  else if (pending_noop.count(tid)) {
    dout(7) << "rollback " << tid << " noop" << dendl;
    pending_noop.erase(tid);
  }    

  else
    ceph_abort();

  //dump();
}

void SnapServer::_server_update(bufferlist& bl)
{
  using ceph::decode;
  auto p = bl.cbegin();
  map<int, vector<snapid_t> > purge;
  decode(purge, p);

  dout(7) << "_server_update purged " << purge << dendl;
  for (map<int, vector<snapid_t> >::iterator p = purge.begin();
       p != purge.end();
       ++p) {
    for (vector<snapid_t>::iterator q = p->second.begin();
	 q != p->second.end();
	 ++q)
      need_to_purge[p->first].erase(*q);
    if (need_to_purge[p->first].empty())
      need_to_purge.erase(p->first);
  }
}

bool SnapServer::_notify_prep(version_t tid)
{
  using ceph::encode;
  bufferlist bl;
  char type = 'F';
  encode(type, bl);
  encode(snaps, bl);
  encode(pending_update, bl);
  encode(pending_destroy, bl);
  encode(last_created, bl);
  encode(last_destroyed, bl);
  ceph_assert(version == tid);

  for (auto &p : active_clients) {
    auto m = make_message<MMDSTableRequest>(table, TABLESERVER_OP_NOTIFY_PREP, 0, version);
    m->bl = bl;
    mds->send_message_mds(m, p);
  }
  return true;
}

void SnapServer::handle_query(const cref_t<MMDSTableRequest> &req)
{
  using ceph::encode;
  using ceph::decode;
  char op;
  auto p = req->bl.cbegin();
  decode(op, p);

  auto reply = make_message<MMDSTableRequest>(table, TABLESERVER_OP_QUERY_REPLY, req->reqid, version);

  switch (op) {
    case 'F': // full
      version_t have_version;
      decode(have_version, p);
      ceph_assert(have_version <= version);
      if (have_version == version) {
	char type = 'U';
	encode(type, reply->bl);
      } else {
	char type = 'F';
	encode(type, reply->bl);
	encode(snaps, reply->bl);
	encode(pending_update, reply->bl);
	encode(pending_destroy, reply->bl);
	encode(last_created, reply->bl);
	encode(last_destroyed, reply->bl);
      }
      // FIXME: implement incremental change
      break;
    default:
      ceph_abort();
  };

  mds->send_message(reply, req->get_connection());
}

void SnapServer::check_osd_map(bool force)
{
  if (!force && version == last_checked_osdmap) {
    dout(10) << "check_osd_map - version unchanged" << dendl;
    return;
  }
  dout(10) << "check_osd_map need_to_purge=" << need_to_purge << dendl;

  map<int32_t, vector<snapid_t> > all_purge;
  map<int32_t, vector<snapid_t> > all_purged;

  // NOTE: this is only needed for support during upgrades from pre-octopus,
  // since starting with octopus we now get an explicit ack after we remove a
  // snap.
  mds->objecter->with_osdmap(
    [this, &all_purged, &all_purge](const OSDMap& osdmap) {
      for (const auto& p : need_to_purge) {
	int id = p.first;
	const pg_pool_t *pi = osdmap.get_pg_pool(id);
	if (pi == NULL) {
	  // The pool is gone.  So are the snapshots.
	  all_purged[id] = std::vector<snapid_t>(p.second.begin(),
						 p.second.end());
	  continue;
	}

	for (const auto& q : p.second) {
	  if (pi->is_removed_snap(q)) {
	    dout(10) << " osdmap marks " << q << " as removed" << dendl;
	    all_purged[id].push_back(q);
	  } else {
	    all_purge[id].push_back(q);
	  }
	}
      }
  });

  if (!all_purged.empty()) {
    // prepare to remove from need_to_purge list
    bufferlist bl;
    using ceph::encode;
    encode(all_purged, bl);
    do_server_update(bl);
  }

  if (!all_purge.empty()) {
    dout(10) << "requesting removal of " << all_purge << dendl;
    auto m = make_message<MRemoveSnaps>(all_purge);
    mon_client->send_mon_message(m.detach());
  }

  last_checked_osdmap = version;
}

void SnapServer::handle_remove_snaps(const cref_t<MRemoveSnaps> &m)
{
  dout(10) << __func__ << " " << *m << dendl;

  map<int32_t, vector<snapid_t> > all_purged;
  int num = 0;

  for (const auto& [id, snaps] : need_to_purge) {
    auto i = m->snaps.find(id);
    if (i == m->snaps.end()) {
      continue;
    }
    for (const auto& q : snaps) {
      if (std::find(i->second.begin(), i->second.end(), q) != i->second.end()) {
	dout(10) << " mon reports " << q << " is removed" << dendl;
	all_purged[id].push_back(q);
	++num;
      }
    }
  }

  dout(10) << __func__ << " " << num << " now removed" << dendl;
  if (num) {
    bufferlist bl;
    using ceph::encode;
    encode(all_purged, bl);
    do_server_update(bl);
  }
}


void SnapServer::dump(Formatter *f) const
{
  f->open_object_section("snapserver");

  f->dump_int("last_snap", last_snap);
  f->dump_int("last_created", last_created);
  f->dump_int("last_destroyed", last_destroyed);

  f->open_array_section("pending_noop");
  for(set<version_t>::const_iterator i = pending_noop.begin(); i != pending_noop.end(); ++i) {
    f->dump_unsigned("version", *i);
  }
  f->close_section();

  f->open_array_section("snaps");
  for (map<snapid_t, SnapInfo>::const_iterator i = snaps.begin(); i != snaps.end(); ++i) {
    f->open_object_section("snap");
    i->second.dump(f);
    f->close_section();
  }
  f->close_section();

  f->open_object_section("need_to_purge");
  for (map<int, set<snapid_t> >::const_iterator i = need_to_purge.begin(); i != need_to_purge.end(); ++i) {
    CachedStackStringStream css;
    *css << i->first;
    f->open_array_section(css->strv());
    for (set<snapid_t>::const_iterator s = i->second.begin(); s != i->second.end(); ++s) {
      f->dump_unsigned("snapid", s->val);
    }
    f->close_section();
  }
  f->close_section();

  f->open_array_section("pending_update");
  for(map<version_t, SnapInfo>::const_iterator i = pending_update.begin(); i != pending_update.end(); ++i) {
    f->open_object_section("snap");
    f->dump_unsigned("version", i->first);
    f->open_object_section("snapinfo");
    i->second.dump(f);
    f->close_section();
    f->close_section();
  }
  f->close_section();

  f->open_array_section("pending_destroy");
  for(map<version_t, pair<snapid_t, snapid_t> >::const_iterator i = pending_destroy.begin(); i != pending_destroy.end(); ++i) {
    f->open_object_section("snap");
    f->dump_unsigned("version", i->first);
    f->dump_unsigned("removed_snap", i->second.first);
    f->dump_unsigned("seq", i->second.second);
    f->close_section();
  }
  f->close_section();

  f->close_section();
}

void SnapServer::generate_test_instances(std::list<SnapServer*>& ls)
{
  list<SnapInfo*> snapinfo_instances;
  SnapInfo::generate_test_instances(snapinfo_instances);
  SnapInfo populated_snapinfo = *(snapinfo_instances.back());
  for (auto& info : snapinfo_instances) {
    delete info;
    info = nullptr;
  }

  SnapServer *blank = new SnapServer();
  ls.push_back(blank);
  SnapServer *populated = new SnapServer();
  populated->last_snap = 123;
  populated->snaps[456] = populated_snapinfo;
  populated->need_to_purge[2].insert(012);
  populated->pending_update[234] = populated_snapinfo;
  populated->pending_destroy[345].first = 567;
  populated->pending_destroy[345].second = 768;
  populated->pending_noop.insert(890);

  ls.push_back(populated);
}

bool SnapServer::force_update(snapid_t last, snapid_t v2_since,
			      map<snapid_t, SnapInfo>& _snaps)
{
  bool modified = false;
  if (last > last_snap) {
    derr << " updating last_snap " << last_snap << " -> " << last << dendl;
    last_snap = last;
    last_created = last;
    last_destroyed = last;
    modified = true;
  }
  if (v2_since > snaprealm_v2_since) {
    derr << " updating snaprealm_v2_since " << snaprealm_v2_since
	 << " -> " << v2_since << dendl;
    snaprealm_v2_since = v2_since;
    modified = true;
  }
  if (snaps != _snaps) {
    derr << " updating snaps {" << snaps << "} -> {" << _snaps << "}" << dendl;
    snaps = _snaps;
    modified = true;
  }

  if (modified) {
    need_to_purge.clear();
    pending_update.clear();
    pending_destroy.clear();
    pending_noop.clear();
    MDSTableServer::reset_state();
  }
  return modified;
}