summaryrefslogtreecommitdiffstats
path: root/src/librbd/io/ObjectDispatchSpec.h
blob: a26d89fe4560eae0d034acb6d1bf05241c594fed (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#ifndef CEPH_LIBRBD_IO_OBJECT_DISPATCH_SPEC_H
#define CEPH_LIBRBD_IO_OBJECT_DISPATCH_SPEC_H

#include "include/int_types.h"
#include "include/buffer.h"
#include "include/Context.h"
#include "include/rados/librados.hpp"
#include "common/snap_types.h"
#include "common/zipkin_trace.h"
#include "librbd/io/Types.h"
#include <boost/variant/variant.hpp>

namespace librbd {
namespace io {

struct ObjectDispatcherInterface;

struct ObjectDispatchSpec {
private:
  // helper to avoid extra heap allocation per object IO
  struct C_Dispatcher : public Context {
    ObjectDispatchSpec* object_dispatch_spec;
    Context* on_finish;

    C_Dispatcher(ObjectDispatchSpec* object_dispatch_spec, Context* on_finish)
      : object_dispatch_spec(object_dispatch_spec), on_finish(on_finish) {
    }

    void complete(int r) override;
    void finish(int r) override;
  };

public:
  struct RequestBase {
    std::string oid;
    uint64_t object_no;
    uint64_t object_off;

    RequestBase(const std::string& oid, uint64_t object_no, uint64_t object_off)
      : oid(oid), object_no(object_no), object_off(object_off) {
    }
  };

  struct ReadRequest : public RequestBase {
    uint64_t object_len;
    librados::snap_t snap_id;
    ceph::bufferlist* read_data;
    ExtentMap* extent_map;

    ReadRequest(const std::string& oid, uint64_t object_no, uint64_t object_off,
                uint64_t object_len, librados::snap_t snap_id,
                ceph::bufferlist* read_data, ExtentMap* extent_map)
      : RequestBase(oid, object_no, object_off),
        object_len(object_len), snap_id(snap_id), read_data(read_data),
        extent_map(extent_map) {
    }
  };

  struct WriteRequestBase : public RequestBase {
    ::SnapContext snapc;
    uint64_t journal_tid;

    WriteRequestBase(const std::string& oid, uint64_t object_no,
                     uint64_t object_off, const ::SnapContext& snapc,
                     uint64_t journal_tid)
      : RequestBase(oid, object_no, object_off), snapc(snapc),
        journal_tid(journal_tid) {
    }
  };

  struct DiscardRequest : public WriteRequestBase {
    uint64_t object_len;
    int discard_flags;

    DiscardRequest(const std::string& oid, uint64_t object_no,
                   uint64_t object_off, uint64_t object_len,
                   int discard_flags, const ::SnapContext& snapc,
                   uint64_t journal_tid)
      : WriteRequestBase(oid, object_no, object_off, snapc, journal_tid),
        object_len(object_len), discard_flags(discard_flags) {
    }
  };

  struct WriteRequest : public WriteRequestBase {
    ceph::bufferlist data;

    WriteRequest(const std::string& oid, uint64_t object_no,
                 uint64_t object_off, ceph::bufferlist&& data,
                 const ::SnapContext& snapc, uint64_t journal_tid)
      : WriteRequestBase(oid, object_no, object_off, snapc, journal_tid),
        data(std::move(data)) {
    }
  };

  struct WriteSameRequest : public WriteRequestBase {
    uint64_t object_len;
    Extents buffer_extents;
    ceph::bufferlist data;

    WriteSameRequest(const std::string& oid, uint64_t object_no,
                     uint64_t object_off, uint64_t object_len,
                     Extents&& buffer_extents, ceph::bufferlist&& data,
                     const ::SnapContext& snapc, uint64_t journal_tid)
    : WriteRequestBase(oid, object_no, object_off, snapc, journal_tid),
      object_len(object_len), buffer_extents(std::move(buffer_extents)),
      data(std::move(data)) {
    }
  };

  struct CompareAndWriteRequest : public WriteRequestBase {
    ceph::bufferlist cmp_data;
    ceph::bufferlist data;
    uint64_t* mismatch_offset;

    CompareAndWriteRequest(const std::string& oid, uint64_t object_no,
                           uint64_t object_off, ceph::bufferlist&& cmp_data,
                           ceph::bufferlist&& data, uint64_t* mismatch_offset,
                           const ::SnapContext& snapc, uint64_t journal_tid)
      : WriteRequestBase(oid, object_no, object_off, snapc, journal_tid),
        cmp_data(std::move(cmp_data)), data(std::move(data)),
        mismatch_offset(mismatch_offset) {
    }
  };

  struct FlushRequest {
    FlushSource flush_source;

    FlushRequest(FlushSource flush_source) : flush_source(flush_source) {
    }
  };

  typedef boost::variant<ReadRequest,
                         DiscardRequest,
                         WriteRequest,
                         WriteSameRequest,
                         CompareAndWriteRequest,
                         FlushRequest> Request;

  C_Dispatcher dispatcher_ctx;

  ObjectDispatcherInterface* object_dispatcher;
  ObjectDispatchLayer object_dispatch_layer;
  int object_dispatch_flags = 0;
  DispatchResult dispatch_result = DISPATCH_RESULT_INVALID;

  Request request;
  int op_flags;
  ZTracer::Trace parent_trace;

  template <typename ImageCtxT>
  static ObjectDispatchSpec* create_read(
      ImageCtxT* image_ctx, ObjectDispatchLayer object_dispatch_layer,
      const std::string &oid, uint64_t object_no, uint64_t object_off,
      uint64_t object_len, librados::snap_t snap_id, int op_flags,
      const ZTracer::Trace &parent_trace, ceph::bufferlist* read_data,
      ExtentMap* extent_map, Context* on_finish) {
    return new ObjectDispatchSpec(image_ctx->io_object_dispatcher,
                                  object_dispatch_layer,
                                  ReadRequest{oid, object_no, object_off,
                                              object_len, snap_id, read_data,
                                              extent_map},
                                  op_flags, parent_trace, on_finish);
  }

  template <typename ImageCtxT>
  static ObjectDispatchSpec* create_discard(
      ImageCtxT* image_ctx, ObjectDispatchLayer object_dispatch_layer,
      const std::string &oid, uint64_t object_no, uint64_t object_off,
      uint64_t object_len, const ::SnapContext &snapc, int discard_flags,
      uint64_t journal_tid, const ZTracer::Trace &parent_trace,
      Context *on_finish) {
    return new ObjectDispatchSpec(image_ctx->io_object_dispatcher,
                                  object_dispatch_layer,
                                  DiscardRequest{oid, object_no, object_off,
                                                 object_len, discard_flags,
                                                 snapc, journal_tid},
                                  0, parent_trace, on_finish);
  }

  template <typename ImageCtxT>
  static ObjectDispatchSpec* create_write(
      ImageCtxT* image_ctx, ObjectDispatchLayer object_dispatch_layer,
      const std::string &oid, uint64_t object_no, uint64_t object_off,
      ceph::bufferlist&& data, const ::SnapContext &snapc, int op_flags,
      uint64_t journal_tid, const ZTracer::Trace &parent_trace,
      Context *on_finish) {
    return new ObjectDispatchSpec(image_ctx->io_object_dispatcher,
                                  object_dispatch_layer,
                                  WriteRequest{oid, object_no, object_off,
                                               std::move(data), snapc,
                                               journal_tid},
                                  op_flags, parent_trace, on_finish);
  }

  template <typename ImageCtxT>
  static ObjectDispatchSpec* create_write_same(
      ImageCtxT* image_ctx, ObjectDispatchLayer object_dispatch_layer,
      const std::string &oid, uint64_t object_no, uint64_t object_off,
      uint64_t object_len, Extents&& buffer_extents,
      ceph::bufferlist&& data, const ::SnapContext &snapc, int op_flags,
      uint64_t journal_tid, const ZTracer::Trace &parent_trace,
      Context *on_finish) {
    return new ObjectDispatchSpec(image_ctx->io_object_dispatcher,
                                  object_dispatch_layer,
                                  WriteSameRequest{oid, object_no, object_off,
                                                   object_len,
                                                   std::move(buffer_extents),
                                                   std::move(data), snapc,
                                                   journal_tid},
                                  op_flags, parent_trace, on_finish);
  }

  template <typename ImageCtxT>
  static ObjectDispatchSpec* create_compare_and_write(
      ImageCtxT* image_ctx, ObjectDispatchLayer object_dispatch_layer,
      const std::string &oid, uint64_t object_no, uint64_t object_off,
      ceph::bufferlist&& cmp_data, ceph::bufferlist&& write_data,
      const ::SnapContext &snapc, uint64_t *mismatch_offset, int op_flags,
      uint64_t journal_tid, const ZTracer::Trace &parent_trace,
      Context *on_finish) {
    return new ObjectDispatchSpec(image_ctx->io_object_dispatcher,
                                  object_dispatch_layer,
                                  CompareAndWriteRequest{oid, object_no,
                                                         object_off,
                                                         std::move(cmp_data),
                                                         std::move(write_data),
                                                         mismatch_offset,
                                                         snapc, journal_tid},
                                  op_flags, parent_trace, on_finish);
  }

  template <typename ImageCtxT>
  static ObjectDispatchSpec* create_flush(
      ImageCtxT* image_ctx, ObjectDispatchLayer object_dispatch_layer,
      FlushSource flush_source, const ZTracer::Trace &parent_trace,
      Context *on_finish) {
    return new ObjectDispatchSpec(image_ctx->io_object_dispatcher,
                                  object_dispatch_layer,
                                  FlushRequest{flush_source}, 0,
                                  parent_trace, on_finish);
  }

  void send();
  void fail(int r);

private:
  template <typename> friend class ObjectDispatcher;

  ObjectDispatchSpec(ObjectDispatcherInterface* object_dispatcher,
                     ObjectDispatchLayer object_dispatch_layer,
                     Request&& request, int op_flags,
                     const ZTracer::Trace& parent_trace, Context* on_finish)
    : dispatcher_ctx(this, on_finish), object_dispatcher(object_dispatcher),
      object_dispatch_layer(object_dispatch_layer), request(std::move(request)),
      op_flags(op_flags), parent_trace(parent_trace) {
  }

};

} // namespace io
} // namespace librbd

#endif // CEPH_LIBRBD_IO_OBJECT_DISPATCH_SPEC_H