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

#include "rgw_rados.h"
#include "rgw_zone.h"
#include "rgw_rest_conn.h"
#include "common/ceph_json.h"
#include "common/errno.h"

#include "services/svc_zone.h"

#define dout_subsys ceph_subsys_rgw

#undef dout_prefix
#define dout_prefix (*_dout << "rgw period puller: ")

namespace {

// pull the given period over the connection
int pull_period(RGWRESTConn* conn, const std::string& period_id,
                const std::string& realm_id, RGWPeriod& period)
{
  rgw_user user;
  RGWEnv env;
  req_info info(conn->get_ctx(), &env);
  info.method = "GET";
  info.request_uri = "/admin/realm/period";

  auto& params = info.args.get_params();
  params["realm_id"] = realm_id;
  params["period_id"] = period_id;

  bufferlist data;
#define MAX_REST_RESPONSE (128 * 1024)
  int r = conn->forward(user, info, nullptr, MAX_REST_RESPONSE, nullptr, &data);
  if (r < 0) {
    return r;
  }

  JSONParser parser;
  r = parser.parse(data.c_str(), data.length());
  if (r < 0) {
    lderr(conn->get_ctx()) << "request failed: " << cpp_strerror(-r) << dendl;
    return r;
  }

  try {
    decode_json_obj(period, &parser);
  } catch (JSONDecoder::err& e) {
    lderr(conn->get_ctx()) << "failed to decode JSON input: "
        << e.message << dendl;
    return -EINVAL;
  }
  return 0;
}

} // anonymous namespace

int RGWPeriodPuller::pull(const std::string& period_id, RGWPeriod& period)
{
  // try to read the period from rados
  period.set_id(period_id);
  period.set_epoch(0);
  int r = period.init(store->ctx(), store->svc.sysobj);
  if (r < 0) {
    if (store->svc.zone->is_meta_master()) {
      // can't pull if we're the master
      ldout(store->ctx(), 1) << "metadata master failed to read period "
          << period_id << " from local storage: " << cpp_strerror(r) << dendl;
      return r;
    }
    ldout(store->ctx(), 14) << "pulling period " << period_id
        << " from master" << dendl;
    // request the period from the master zone
    r = pull_period(store->svc.zone->get_master_conn(), period_id,
                    store->svc.zone->get_realm().get_id(), period);
    if (r < 0) {
      lderr(store->ctx()) << "failed to pull period " << period_id << dendl;
      return r;
    }
    // write the period to rados
    r = period.store_info(true);
    if (r == -EEXIST) {
      r = 0;
    } else if (r < 0) {
      lderr(store->ctx()) << "failed to store period " << period_id << dendl;
      return r;
    }
    // update latest epoch
    r = period.update_latest_epoch(period.get_epoch());
    if (r == -EEXIST) {
      // already have this epoch (or a more recent one)
      return 0;
    }
    if (r < 0) {
      lderr(store->ctx()) << "failed to update latest_epoch for period "
          << period_id << dendl;
      return r;
    }
    // reflect period objects if this is the latest version
    if (store->svc.zone->get_realm().get_current_period() == period_id) {
      r = period.reflect();
      if (r < 0) {
        return r;
      }
    }
    ldout(store->ctx(), 14) << "period " << period_id
        << " pulled and written to local storage" << dendl;
  } else {
    ldout(store->ctx(), 14) << "found period " << period_id
        << " in local storage" << dendl;
  }
  return 0;
}