summaryrefslogtreecommitdiffstats
path: root/src/osd/PGTransaction.h
blob: 3b5b9e72cfa076bd29b7a8624ce8ddbbdbaebc2b (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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
// -*- 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) 2016 Red Hat
 *
 * 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.
 *
 */
#ifndef PGTRANSACTION_H
#define PGTRANSACTION_H

#include <map>
#include <memory>
#include <optional>

#include "common/hobject.h"
#include "osd/osd_types.h"
#include "osd/osd_internal_types.h"
#include "common/interval_map.h"
#include "common/inline_variant.h"

/**
 * This class represents transactions which can be submitted to
 * a PGBackend.  For expediency, there are some constraints on
 * the operations submitted:
 * 1) Rename sources may only be referenced prior to the rename
 *    operation to the destination.
 * 2) The graph formed by edges of source->destination for clones
 *    (Create) and Renames must be acyclic.
 * 3) clone_range sources must not be modified by the same
 *    transaction
 */
class PGTransaction {
public:
  std::map<hobject_t, ObjectContextRef> obc_map;

  class ObjectOperation {
  public:
    struct Init
    {
      struct None {};
      struct Create {};
      struct Clone {
	hobject_t source;
      };
      struct Rename {
	hobject_t source; // must be temp object
      };
    };
    using InitType = boost::variant<
      Init::None,
      Init::Create,
      Init::Clone,
      Init::Rename>;

    InitType init_type = Init::None();
    bool delete_first = false;

    /**
     * is_none() && is_delete() indicates that we are deleting an
     * object which already exists and not recreating it. delete_first means
     * that the transaction logically removes the object.

     * There are really 4 cases:

     * 1) We are modifying an existing object (is_none() &&
     *    !is_delete())
     *    a) If it's an append, we just write into the log entry the old size
     *    b) If it's an actual overwrite, we save the old versions of the
     *       extents being overwritten and write those offsets into the log
     *       entry
     * 2) We are removing and then recreating an object (!is_none() && is_delete())
     *    -- stash
     * 3) We are removing an object (is_none() && is_delete()) -- stash
     * 4) We are creating an object (!is_none() && !is_delete()) -- create (no
     *    stash)
     *
     * Create, Clone, Rename are the three ways we can recreate it.
     * ECBackend transaction planning needs this context
     * to figure out how to perform the transaction.
     */
    bool deletes_first() const {
      return delete_first;
    }
    bool is_delete() const {
      return boost::get<Init::None>(&init_type) != nullptr && delete_first;
    }
    bool is_none() const {
      return boost::get<Init::None>(&init_type) != nullptr && !delete_first;
    }
    bool is_fresh_object() const {
      return boost::get<Init::None>(&init_type) == nullptr;
    }
    bool is_rename() const {
      return boost::get<Init::Rename>(&init_type) != nullptr;
    }
    bool has_source(hobject_t *source = nullptr) const {
      return match(
	init_type,
	[&](const Init::Clone &op) -> bool {
	  if (source)
	    *source = op.source;
	  return true;
	},
	[&](const Init::Rename &op) -> bool {
	  if (source)
	    *source = op.source;
	  return true;
	},
	[&](const Init::None &) -> bool { return false; },
	[&](const Init::Create &) -> bool { return false; });
    }

    bool clear_omap = false;

    /**
     * truncate
     * <lowest, last> ?
     *
     * truncate is represented as a pair because in the event of
     * multiple truncates within a single transaction we need to
     * remember the lowest truncate and the final object size
     * (the last truncate).  We also adjust the buffers map
     * to account for truncates overriding previous writes */
    std::optional<std::pair<uint64_t, uint64_t> > truncate = std::nullopt;

    std::map<std::string, std::optional<ceph::buffer::list> > attr_updates;

    enum class OmapUpdateType {Remove, Insert, RemoveRange};
    std::vector<std::pair<OmapUpdateType, ceph::buffer::list> > omap_updates;

    std::optional<ceph::buffer::list> omap_header;

    /// (old, new) -- only valid with no truncate or buffer updates
    std::optional<std::pair<std::set<snapid_t>, std::set<snapid_t>>> updated_snaps;

    struct alloc_hint_t {
      uint64_t expected_object_size;
      uint64_t expected_write_size;
      uint32_t flags;
    };
    std::optional<alloc_hint_t> alloc_hint;

    struct BufferUpdate {
      struct Write {
	ceph::buffer::list buffer;
	uint32_t fadvise_flags;
      };
      struct Zero {
	uint64_t len;
      };
      struct CloneRange {
	hobject_t from;
	uint64_t offset;
	uint64_t len;
      };
    };
    using BufferUpdateType = boost::variant<
      BufferUpdate::Write,
      BufferUpdate::Zero,
      BufferUpdate::CloneRange>;

  private:
    struct SplitMerger {
      BufferUpdateType split(
	uint64_t offset,
	uint64_t len,
	const BufferUpdateType &bu) const {
	return match(
	  bu,
	  [&](const BufferUpdate::Write &w) -> BufferUpdateType {
	    ceph::buffer::list bl;
	    bl.substr_of(w.buffer, offset, len);
	    return BufferUpdate::Write{bl, w.fadvise_flags};
	  },
	  [&](const BufferUpdate::Zero &) -> BufferUpdateType {
	    return BufferUpdate::Zero{len};
	  },
	  [&](const BufferUpdate::CloneRange &c) -> BufferUpdateType {
	    return BufferUpdate::CloneRange{c.from, c.offset + offset, len};
	  });
      }
      uint64_t length(
	const BufferUpdateType &left) const {
	return match(
	  left,
	  [&](const BufferUpdate::Write &w) -> uint64_t {
	    return w.buffer.length();
	  },
	  [&](const BufferUpdate::Zero &z) -> uint64_t {
	    return z.len;
	  },
	  [&](const BufferUpdate::CloneRange &c) -> uint64_t {
	    return c.len;
	  });
      }
      bool can_merge(
	const BufferUpdateType &left,
	const BufferUpdateType &right) const {
	return match(
	  left,
	  [&](const BufferUpdate::Write &w) -> bool {
	    auto r = boost::get<BufferUpdate::Write>(&right);
	    return r != nullptr && (w.fadvise_flags == r->fadvise_flags);
	  },
	  [&](const BufferUpdate::Zero &) -> bool {
	    auto r = boost::get<BufferUpdate::Zero>(&right);
	    return r != nullptr;
	  },
	  [&](const BufferUpdate::CloneRange &c) -> bool {
	    return false;
	  });
      }
      BufferUpdateType merge(
	BufferUpdateType &&left,
	BufferUpdateType &&right) const {
	return match(
	  left,
	  [&](const BufferUpdate::Write &w) -> BufferUpdateType {
	    auto r = boost::get<BufferUpdate::Write>(&right);
	    ceph_assert(r && w.fadvise_flags == r->fadvise_flags);
	    ceph::buffer::list bl = w.buffer;
	    bl.append(r->buffer);
	    return BufferUpdate::Write{bl, w.fadvise_flags};
	  },
	  [&](const BufferUpdate::Zero &z) -> BufferUpdateType {
	    auto r = boost::get<BufferUpdate::Zero>(&right);
	    ceph_assert(r);
	    return BufferUpdate::Zero{z.len + r->len};
	  },
	  [&](const BufferUpdate::CloneRange &c) -> BufferUpdateType {
	    ceph_abort_msg("violates can_merge condition");
	    return left;
	  });
      }
    };
  public:
    using buffer_update_type = interval_map<
      uint64_t, BufferUpdateType, SplitMerger>;
    buffer_update_type buffer_updates;

    friend class PGTransaction;
  };
  std::map<hobject_t, ObjectOperation> op_map;
private:
  ObjectOperation &get_object_op_for_modify(const hobject_t &hoid) {
    auto &op = op_map[hoid];
    ceph_assert(!op.is_delete());
    return op;
  }
  ObjectOperation &get_object_op(const hobject_t &hoid) {
    return op_map[hoid];
  }
public:
  void add_obc(
    ObjectContextRef obc) {
    ceph_assert(obc);
    obc_map[obc->obs.oi.soid] = obc;
  }
  /// Sets up state for new object
  void create(
    const hobject_t &hoid
    ) {
    auto &op = op_map[hoid];
    ceph_assert(op.is_none() || op.is_delete());
    op.init_type = ObjectOperation::Init::Create();
  }

  /// Sets up state for target cloned from source
  void clone(
    const hobject_t &target,       ///< [in] obj to clone to
    const hobject_t &source        ///< [in] obj to clone from
    ) {
    auto &op = op_map[target];
    ceph_assert(op.is_none() || op.is_delete());
    op.init_type = ObjectOperation::Init::Clone{source};
  }

  /// Sets up state for target renamed from source
  void rename(
    const hobject_t &target,       ///< [in] to, must not exist, be non-temp
    const hobject_t &source        ///< [in] source (must be a temp object)
    ) {
    ceph_assert(source.is_temp());
    ceph_assert(!target.is_temp());
    auto &op = op_map[target];
    ceph_assert(op.is_none() || op.is_delete());

    bool del_first = op.is_delete();
    auto iter = op_map.find(source);
    if (iter != op_map.end()) {
      op = iter->second;
      op_map.erase(iter);
      op.delete_first = del_first;
    }

    op.init_type = ObjectOperation::Init::Rename{source};
  }

  /// Remove -- must not be called on rename target
  void remove(
    const hobject_t &hoid          ///< [in] obj to remove
    ) {
    auto &op = get_object_op_for_modify(hoid);
    if (!op.is_fresh_object()) {
      ceph_assert(!op.updated_snaps);
      op = ObjectOperation();
      op.delete_first = true;
    } else {
      ceph_assert(!op.is_rename());
      op_map.erase(hoid); // make it a noop if it's a fresh object
    }
  }

  void update_snaps(
    const hobject_t &hoid,         ///< [in] object for snaps
    const std::set<snapid_t> &old_snaps,///< [in] old snaps value
    const std::set<snapid_t> &new_snaps ///< [in] new snaps value
    ) {
    auto &op = get_object_op(hoid);
    ceph_assert(!op.updated_snaps);
    ceph_assert(op.buffer_updates.empty());
    ceph_assert(!op.truncate);
    op.updated_snaps = make_pair(
      old_snaps,
      new_snaps);
  }

  /// Clears, truncates
  void omap_clear(
    const hobject_t &hoid          ///< [in] object to clear omap
    ) {
    auto &op = get_object_op_for_modify(hoid);
    op.clear_omap = true;
    op.omap_updates.clear();
    op.omap_header = std::nullopt;
  }
  void truncate(
    const hobject_t &hoid,         ///< [in] object
    uint64_t off                   ///< [in] offset to truncate to
    ) {
    auto &op = get_object_op_for_modify(hoid);
    ceph_assert(!op.updated_snaps);
    op.buffer_updates.erase(
      off,
      std::numeric_limits<uint64_t>::max() - off);
    if (!op.truncate || off < op.truncate->first) {
      op.truncate = std::pair<uint64_t, uint64_t>(off, off);
    } else {
      op.truncate->second = off;
    }
  }

  /// Attr ops
  void setattrs(
    const hobject_t &hoid,         ///< [in] object to write
    std::map<std::string, ceph::buffer::list> &attrs ///< [in] attrs, may be cleared
    ) {
    auto &op = get_object_op_for_modify(hoid);
    for (auto &&i: attrs) {
      auto& d = op.attr_updates[i.first];
      d = i.second;
      d->rebuild();
    }
  }
  void setattr(
    const hobject_t &hoid,         ///< [in] object to write
    const std::string &attrname,        ///< [in] attr to write
    ceph::buffer::list &bl                 ///< [in] val to write, may be claimed
    ) {
    auto &op = get_object_op_for_modify(hoid);
    auto& d = op.attr_updates[attrname];
    d = bl;
    d->rebuild();
  }
  void rmattr(
    const hobject_t &hoid,         ///< [in] object to write
    const std::string &attrname         ///< [in] attr to remove
    ) {
    auto &op = get_object_op_for_modify(hoid);
    op.attr_updates[attrname] = std::nullopt;
  }

  /// set alloc hint
  void set_alloc_hint(
    const hobject_t &hoid,         ///< [in] object (must exist)
    uint64_t expected_object_size, ///< [in]
    uint64_t expected_write_size,
    uint32_t flags
    ) {
    auto &op = get_object_op_for_modify(hoid);
    op.alloc_hint = ObjectOperation::alloc_hint_t{
      expected_object_size, expected_write_size, flags};
  }

  /// Buffer updates
  void write(
    const hobject_t &hoid,         ///< [in] object to write
    uint64_t off,                  ///< [in] off at which to write
    uint64_t len,                  ///< [in] len to write from bl
    ceph::buffer::list &bl,                ///< [in] bl to write will be claimed to len
    uint32_t fadvise_flags = 0     ///< [in] fadvise hint
    ) {
    auto &op = get_object_op_for_modify(hoid);
    ceph_assert(!op.updated_snaps);
    ceph_assert(len > 0);
    ceph_assert(len == bl.length());
    op.buffer_updates.insert(
      off,
      len,
      ObjectOperation::BufferUpdate::Write{bl, fadvise_flags});
  }
  void clone_range(
    const hobject_t &from,         ///< [in] from
    const hobject_t &to,           ///< [in] to
    uint64_t fromoff,              ///< [in] offset
    uint64_t len,                  ///< [in] len
    uint64_t tooff                 ///< [in] offset
    ) {
    auto &op = get_object_op_for_modify(to);
    ceph_assert(!op.updated_snaps);
    op.buffer_updates.insert(
      tooff,
      len,
      ObjectOperation::BufferUpdate::CloneRange{from, fromoff, len});
  }
  void zero(
    const hobject_t &hoid,         ///< [in] object
    uint64_t off,                  ///< [in] offset to start zeroing at
    uint64_t len                   ///< [in] amount to zero
    ) {
    auto &op = get_object_op_for_modify(hoid);
    ceph_assert(!op.updated_snaps);
    op.buffer_updates.insert(
      off,
      len,
      ObjectOperation::BufferUpdate::Zero{len});
  }

  /// Omap updates
  void omap_setkeys(
    const hobject_t &hoid,         ///< [in] object to write
    ceph::buffer::list &keys_bl            ///< [in] encoded map<string, ceph::buffer::list>
    ) {
    auto &op = get_object_op_for_modify(hoid);
    op.omap_updates.emplace_back(
      std::make_pair(
	ObjectOperation::OmapUpdateType::Insert,
	keys_bl));
  }
  void omap_setkeys(
    const hobject_t &hoid,         ///< [in] object to write
    std::map<std::string, ceph::buffer::list> &keys  ///< [in] omap keys, may be cleared
    ) {
    using ceph::encode;
    ceph::buffer::list bl;
    encode(keys, bl);
    omap_setkeys(hoid, bl);
  }
  void omap_rmkeys(
    const hobject_t &hoid,         ///< [in] object to write
    ceph::buffer::list &keys_bl            ///< [in] encode set<string>
    ) {
    auto &op = get_object_op_for_modify(hoid);
    op.omap_updates.emplace_back(
      std::make_pair(
	ObjectOperation::OmapUpdateType::Remove,
	keys_bl));
  }
  void omap_rmkeys(
    const hobject_t &hoid,         ///< [in] object to write
    std::set<std::string> &keys              ///< [in] omap keys, may be cleared
    ) {
    using ceph::encode;
    ceph::buffer::list bl;
    encode(keys, bl);
    omap_rmkeys(hoid, bl);
  }
  void omap_rmkeyrange(
    const hobject_t &hoid,         ///< [in] object to write
    ceph::buffer::list &range_bl           ///< [in] encode string[2]
    ) {
    auto &op = get_object_op_for_modify(hoid);
    op.omap_updates.emplace_back(
      std::make_pair(
	ObjectOperation::OmapUpdateType::RemoveRange,
	range_bl));
  }
  void omap_rmkeyrange(
    const hobject_t &hoid,         ///< [in] object to write
    std::string& key_begin,        ///< [in] first key in range
    std::string& key_end           ///< [in] first key past range, range is [first,last)
    ) {
    ceph::buffer::list bl;
    ::encode(key_begin, bl);
    ::encode(key_end, bl);
    omap_rmkeyrange(hoid, bl);
  }
  void omap_setheader(
    const hobject_t &hoid,         ///< [in] object to write
    ceph::buffer::list &header             ///< [in] header
    ) {
    auto &op = get_object_op_for_modify(hoid);
    op.omap_header = header;
  }

  bool empty() const {
    return op_map.empty();
  }

  uint64_t get_bytes_written() const {
    uint64_t ret = 0;
    for (auto &&i: op_map) {
      for (auto &&j: i.second.buffer_updates) {
	ret += j.get_len();
      }
    }
    return ret;
  }

  void nop(
    const hobject_t &hoid ///< [in] obj to which we are doing nothing
    ) {
    get_object_op_for_modify(hoid);
  }

  /* Calls t() on all pair<hobject_t, ObjectOperation> & such that clone/rename
   * sinks are always called before clone sources
   *
   * TODO: add a fast path for the single object case and possibly the single
   * object clone from source case (make_writeable made a clone).
   *
   * This structure only requires that the source->sink graph be acyclic.
   * This is much more general than is actually required by PrimaryLogPG.
   * Only 4 flavors of multi-object transactions actually happen:
   * 1) rename temp -> object for copyfrom
   * 2) clone head -> clone, modify head for make_writeable on normal head write
   * 3) clone clone -> head for rollback
   * 4) 2 + 3
   *
   * We can bypass the below logic for single object transactions trivially
   * (including case 1 above since temp doesn't show up again).
   * For 2-3, we could add something ad-hoc to ensure that they happen in the
   * right order, but it actually seems easier to just do the graph construction.
   */
  template <typename T>
  void safe_create_traverse(T &&t) {
    std::map<hobject_t, std::list<hobject_t>> dgraph;
    std::list<hobject_t> stack;

    // Populate stack with roots, dgraph with edges
    for (auto &&opair: op_map) {
      hobject_t source;
      if (opair.second.has_source(&source)) {
	auto &l = dgraph[source];
	if (l.empty() && !op_map.count(source)) {
	  /* Source oids not in op_map need to be added as roots
	   * (but only once!) */
	  stack.push_back(source);
	}
	l.push_back(opair.first);
      } else {
	stack.push_back(opair.first);
      }
    }

    /* Why don't we need to worry about accessing the same node
     * twice?  dgraph nodes always have in-degree at most 1 because
     * the inverse graph nodes (source->dest) can have out-degree
     * at most 1 (only one possible source).  We do a post-order
     * depth-first traversal here to ensure we call f on children
     * before parents.
     */
    while (!stack.empty()) {
      hobject_t &cur = stack.front();
      auto diter = dgraph.find(cur);
      if (diter == dgraph.end()) {
	/* Leaf: pop and call t() */
	auto opiter = op_map.find(cur);
	if (opiter != op_map.end())
	  t(*opiter);
	stack.pop_front();
      } else {
	/* Internal node: push children onto stack, remove edge,
	 * recurse.  When this node is encountered again, it'll
	 * be a leaf */
	ceph_assert(!diter->second.empty());
	stack.splice(stack.begin(), diter->second);
	dgraph.erase(diter);
      }
    }
  }
};
using PGTransactionUPtr = std::unique_ptr<PGTransaction>;

#endif