summaryrefslogtreecommitdiffstats
path: root/src/crimson/osd/osd_operations/compound_peering_request.cc
blob: e55760096e4e82ff2dbf543736908218f113515f (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include <seastar/core/future.hh>

#include "osd/PeeringState.h"

#include "messages/MOSDPGQuery.h"
#include "messages/MOSDPGCreate2.h"

#include "common/Formatter.h"

#include "crimson/common/exception.h"
#include "crimson/osd/pg.h"
#include "crimson/osd/osd.h"
#include "crimson/osd/osd_operations/compound_peering_request.h"

namespace {
  seastar::logger& logger() {
    return crimson::get_logger(ceph_subsys_osd);
  }
}

namespace {
using namespace crimson::osd;

struct compound_state {
  seastar::promise<BufferedRecoveryMessages> promise;
  // assuming crimson-osd won't need to be compatible with pre-octopus
  // releases
  BufferedRecoveryMessages ctx{ceph_release_t::octopus};
  compound_state() = default;
  ~compound_state() {
    promise.set_value(std::move(ctx));
  }
};
using compound_state_ref = seastar::lw_shared_ptr<compound_state>;

class PeeringSubEvent : public RemotePeeringEvent {
  compound_state_ref state;
public:
  template <typename... Args>
  PeeringSubEvent(compound_state_ref state, Args &&... args) :
    RemotePeeringEvent(std::forward<Args>(args)...), state(state) {}

  seastar::future<> complete_rctx(Ref<crimson::osd::PG> pg) final {
    logger().debug("{}: submitting ctx transaction", *this);
    state->ctx.accept_buffered_messages(ctx);
    state = {};
    if (!pg) {
      ceph_assert(ctx.transaction.empty());
      return seastar::now();
    } else {
      return osd.get_shard_services().dispatch_context_transaction(
	pg->get_collection_ref(), ctx);
    }
  }
};

std::vector<OperationRef> handle_pg_create(
  OSD &osd,
  crimson::net::ConnectionRef conn,
  compound_state_ref state,
  Ref<MOSDPGCreate2> m)
{
  std::vector<OperationRef> ret;
  for (auto& [pgid, when] : m->pgs) {
    const auto &[created, created_stamp] = when;
    auto q = m->pg_extra.find(pgid);
    ceph_assert(q != m->pg_extra.end());
    auto& [history, pi] = q->second;
    logger().debug(
      "{}: {} e{} @{} "
      "history {} pi {}",
      __func__, pgid, created, created_stamp,
      history, pi);
    if (!pi.empty() &&
	m->epoch < pi.get_bounds().second) {
      logger().error(
        "got pg_create on {} epoch {}  "
        "unmatched past_intervals {} (history {})",
        pgid, m->epoch,
        pi, history);
    } else {
      auto op = osd.get_shard_services().start_operation<PeeringSubEvent>(
	  state,
	  osd,
	  conn,
	  osd.get_shard_services(),
	  pg_shard_t(),
	  pgid,
	  m->epoch,
	  m->epoch,
	  NullEvt(),
	  true,
	  new PGCreateInfo(pgid, m->epoch, history, pi, true)).first;
      ret.push_back(op);
    }
  }
  return ret;
}

struct SubOpBlocker : BlockerT<SubOpBlocker> {
  static constexpr const char * type_name = "CompoundOpBlocker";

  std::vector<OperationRef> subops;
  SubOpBlocker(std::vector<OperationRef> &&subops) : subops(subops) {}

  virtual void dump_detail(Formatter *f) const {
    f->open_array_section("dependent_operations");
    {
      for (auto &i : subops) {
	i->dump_brief(f);
      }
    }
    f->close_section();
  }
};

} // namespace

namespace crimson::osd {

CompoundPeeringRequest::CompoundPeeringRequest(
  OSD &osd, crimson::net::ConnectionRef conn, Ref<Message> m)
  : osd(osd),
    conn(conn),
    m(m)
{}

void CompoundPeeringRequest::print(std::ostream &lhs) const
{
  lhs << *m;
}

void CompoundPeeringRequest::dump_detail(Formatter *f) const
{
  f->dump_stream("message") << *m;
}

seastar::future<> CompoundPeeringRequest::start()
{
  logger().info("{}: starting", *this);
  auto state = seastar::make_lw_shared<compound_state>();
  auto blocker = std::make_unique<SubOpBlocker>(
    [&] {
      assert((m->get_type() == MSG_OSD_PG_CREATE2));
      return handle_pg_create(
        osd,
	conn,
	state,
	boost::static_pointer_cast<MOSDPGCreate2>(m));
    }());

  IRef ref = this;
  logger().info("{}: about to fork future", *this);
  return crimson::common::handle_system_shutdown(
    [this, ref, blocker=std::move(blocker), state]() mutable {
    return with_blocking_future(
      blocker->make_blocking_future(state->promise.get_future())
    ).then([this, blocker=std::move(blocker)](auto &&ctx) {
      logger().info("{}: sub events complete", *this);
      return osd.get_shard_services().dispatch_context_messages(std::move(ctx));
    }).then([this, ref=std::move(ref)] {
      logger().info("{}: complete", *this);
    });
  });
}

} // namespace crimson::osd