Skip to content

Commit f172fb9

Browse files
committed
librbd/migration: make SourceSpecBuilder::parse_source_spec() static
In preparation for divorcing NativeFormat from FormatInterface and changing when/how src_image_ctx is created, make parse_source_spec() independent of src_image_ctx. The "invalid source-spec JSON" error is duplicated by the "failed to parse migration source-spec" error, so just get rid of the former to spare having to pass CephContext to parse_source_spec(). Signed-off-by: Ilya Dryomov <[email protected]>
1 parent c14356b commit f172fb9

File tree

4 files changed

+34
-36
lines changed

4 files changed

+34
-36
lines changed

src/librbd/migration/OpenSourceImageRequest.cc

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,26 +33,8 @@ OpenSourceImageRequest<I>::OpenSourceImageRequest(
3333

3434
template <typename I>
3535
void OpenSourceImageRequest<I>::send() {
36-
open_source();
37-
}
38-
39-
template <typename I>
40-
void OpenSourceImageRequest<I>::open_source() {
4136
ldout(m_cct, 10) << dendl;
4237

43-
// note that all source image ctx properties are placeholders
44-
*m_src_image_ctx = I::create("", "", CEPH_NOSNAP, m_dst_io_ctx, true);
45-
auto src_image_ctx = *m_src_image_ctx;
46-
src_image_ctx->child = m_dst_image_ctx;
47-
48-
// use default layout values (can be overridden by source layers later)
49-
src_image_ctx->order = 22;
50-
src_image_ctx->layout = file_layout_t();
51-
src_image_ctx->layout.stripe_count = 1;
52-
src_image_ctx->layout.stripe_unit = 1ULL << src_image_ctx->order;
53-
src_image_ctx->layout.object_size = 1Ull << src_image_ctx->order;
54-
src_image_ctx->layout.pool_id = -1;
55-
5638
bool import_only = true;
5739
auto source_spec = m_migration_info.source_spec;
5840
if (source_spec.empty()) {
@@ -67,20 +49,40 @@ void OpenSourceImageRequest<I>::open_source() {
6749
<< "source_snap_id=" << m_src_snap_id << ", "
6850
<< "import_only=" << import_only << dendl;
6951

70-
SourceSpecBuilder<I> source_spec_builder{src_image_ctx};
7152
json_spirit::mObject source_spec_object;
72-
int r = source_spec_builder.parse_source_spec(source_spec,
73-
&source_spec_object);
53+
int r = SourceSpecBuilder<I>::parse_source_spec(source_spec,
54+
&source_spec_object);
7455
if (r < 0) {
7556
lderr(m_cct) << "failed to parse migration source-spec: "
7657
<< cpp_strerror(r) << dendl;
77-
(*m_src_image_ctx)->state->close();
7858
finish(r);
7959
return;
8060
}
8161

82-
r = source_spec_builder.build_format(source_spec_object, import_only,
83-
&m_format);
62+
open_source(source_spec_object, import_only);
63+
}
64+
65+
template <typename I>
66+
void OpenSourceImageRequest<I>::open_source(
67+
const json_spirit::mObject& source_spec_object, bool import_only) {
68+
ldout(m_cct, 10) << dendl;
69+
70+
// note that all source image ctx properties are placeholders
71+
*m_src_image_ctx = I::create("", "", CEPH_NOSNAP, m_dst_io_ctx, true);
72+
auto src_image_ctx = *m_src_image_ctx;
73+
src_image_ctx->child = m_dst_image_ctx;
74+
75+
// use default layout values (can be overridden by source layers later)
76+
src_image_ctx->order = 22;
77+
src_image_ctx->layout = file_layout_t();
78+
src_image_ctx->layout.stripe_count = 1;
79+
src_image_ctx->layout.stripe_unit = 1ULL << src_image_ctx->order;
80+
src_image_ctx->layout.object_size = 1Ull << src_image_ctx->order;
81+
src_image_ctx->layout.pool_id = -1;
82+
83+
SourceSpecBuilder<I> source_spec_builder{src_image_ctx};
84+
int r = source_spec_builder.build_format(source_spec_object, import_only,
85+
&m_format);
8486
if (r < 0) {
8587
lderr(m_cct) << "failed to build migration format handler: "
8688
<< cpp_strerror(r) << dendl;

src/librbd/migration/OpenSourceImageRequest.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "include/rados/librados_fwd.hpp"
88
#include "librbd/Types.h"
9+
#include "json_spirit/json_spirit.h"
910
#include <map>
1011
#include <memory>
1112

@@ -78,7 +79,8 @@ class OpenSourceImageRequest {
7879
uint64_t m_image_size = 0;
7980
SnapInfos m_snap_infos;
8081

81-
void open_source();
82+
void open_source(const json_spirit::mObject& source_spec_object,
83+
bool import_only);
8284
void handle_open_source(int r);
8385

8486
void get_image_size();

src/librbd/migration/SourceSpecBuilder.cc

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,20 +30,16 @@ const std::string TYPE_KEY{"type"};
3030
template <typename I>
3131
int SourceSpecBuilder<I>::parse_source_spec(
3232
const std::string& source_spec,
33-
json_spirit::mObject* source_spec_object) const {
34-
auto cct = m_image_ctx->cct;
35-
ldout(cct, 10) << dendl;
36-
33+
json_spirit::mObject* source_spec_object) {
3734
json_spirit::mValue json_root;
38-
if(json_spirit::read(source_spec, json_root)) {
35+
if (json_spirit::read(source_spec, json_root)) {
3936
try {
4037
*source_spec_object = json_root.get_obj();
4138
return 0;
4239
} catch (std::runtime_error&) {
4340
}
4441
}
4542

46-
lderr(cct) << "invalid source-spec JSON" << dendl;
4743
return -EBADMSG;
4844
}
4945

src/librbd/migration/SourceSpecBuilder.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
#include "include/int_types.h"
88
#include <json_spirit/json_spirit.h>
99
#include <memory>
10-
#include <optional>
1110
#include <string>
1211

1312
struct Context;
@@ -25,12 +24,12 @@ struct StreamInterface;
2524
template <typename ImageCtxT>
2625
class SourceSpecBuilder {
2726
public:
27+
static int parse_source_spec(const std::string& source_spec,
28+
json_spirit::mObject* source_spec_object);
29+
2830
SourceSpecBuilder(ImageCtxT* image_ctx) : m_image_ctx(image_ctx) {
2931
}
3032

31-
int parse_source_spec(const std::string& source_spec,
32-
json_spirit::mObject* source_spec_object) const;
33-
3433
int build_format(const json_spirit::mObject& format_object, bool import_only,
3534
std::unique_ptr<FormatInterface>* format) const;
3635

@@ -43,7 +42,6 @@ class SourceSpecBuilder {
4342

4443
private:
4544
ImageCtxT* m_image_ctx;
46-
4745
};
4846

4947
} // namespace migration

0 commit comments

Comments
 (0)