summaryrefslogtreecommitdiffstats
path: root/src/crimson/crush/CrushLocation.cc
blob: d45264000bee2a09ea39d48839b2503caf28509f (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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
#include "CrushLocation.h"

#include <vector>
#include <boost/algorithm/string/trim.hpp>
#include <seastar/util/process.hh>
#include <seastar/util/later.hh>

#include "crush/CrushWrapper.h"
#include "crimson/common/log.h"
#include "crimson/common/config_proxy.h"

static seastar::logger& logger() {
  return crimson::get_logger(ceph_subsys_crush);
}

using namespace crimson::common;

namespace crimson::crush {

seastar::future<> CrushLocation::update_from_conf()
{
  auto crush_location = local_conf().get_val<std::string>("crush_location");
  if (crush_location.length()) {
    _parse(crush_location);
  }

  return seastar::now();
}

void CrushLocation::_parse(const std::string& s)
{
  std::multimap<std::string, std::string> new_crush_location;
  std::vector<std::string> lvec;
  get_str_vec(s, ";, \t", lvec);
  int r = CrushWrapper::parse_loc_multimap(lvec, &new_crush_location);
  if (r < 0) {
    logger().error("CrushWrapper::parse_loc_multimap error, keeping original\
      crush_location {}", *this);
    return;
  }

  loc.swap(new_crush_location);
  logger().info("{}: crush_location is {}", __func__, *this);
  return;
}

seastar::future<> CrushLocation::update_from_hook()
{
  auto crush_location_hook = local_conf().get_val<std::string>("crush_location_hook");
  if (crush_location_hook.length() == 0)
    return seastar::now();

  return seastar::file_exists(
    crush_location_hook
  ).then([this] (bool result) {
    if (!result) {
      std::stringstream errmsg;
      errmsg << "the user define crush location hook: "
             << local_conf().get_val<std::string>("crush_location_hook")
             << " is not exists.";
      logger().error("{}", errmsg.str());
      throw std::runtime_error(errmsg.str());
    }

    return seastar::file_accessible(
      local_conf().get_val<std::string>("crush_location_hook"),
      seastar::access_flags::execute
    ).then([this] (bool result) {
      if (!result) {
        std::stringstream errmsg;
        errmsg << "the user define crush location hook: "
               << local_conf().get_val<std::string>("crush_location_hook")
               << " is not executable.";
        logger().error("{}", errmsg.str());
        throw std::runtime_error(errmsg.str());
      }

      seastar::experimental::spawn_parameters params = {
        .argv = {
          local_conf().get_val<std::string>("crush_location_hook"),
          "--cluster",
          local_conf()->cluster,
          "--id",
          local_conf()->name.get_id(),
          "--type",
          local_conf()->name.get_type_str()
        }
      };
      return seastar::experimental::spawn_process(
        local_conf().get_val<std::string>("crush_location_hook"),
        std::move(params)
      ).then([this] (auto process) {
        auto stdout = process.stdout();
        return do_with(
          std::move(process),
          std::move(stdout),
          [this](auto& process, auto& stdout)
        {
          return stdout.read().then([] (seastar::temporary_buffer<char> buf) {
            auto out = std::string(buf.get(), buf.size());
            boost::algorithm::trim_if(out, boost::algorithm::is_any_of(" \n\r\t"));
            return seastar::make_ready_future<std::string>(std::move(out));
          }).then([&process, this] (auto out) {
            return process.wait(
            ).then([out = std::move(out), this] (auto wstatus) {
              auto* exit_signal = std::get_if<seastar::experimental::process::wait_signaled>(&wstatus);
              if (exit_signal != nullptr) {
                std::stringstream errmsg;
                errmsg << "the user define crush location hook: "
                       << local_conf().get_val<std::string>("crush_location_hook")
                       << " terminated, terminated signal is "
                       << exit_signal->terminating_signal;
                logger().error("{}", errmsg.str());
                throw std::runtime_error(errmsg.str());
              }

              auto* exit_status = std::get_if<seastar::experimental::process::wait_exited>(&wstatus);
              if (exit_status->exit_code != 0) {
                std::stringstream errmsg;
                errmsg << "the user define crush location hook: "
                       << local_conf().get_val<std::string>("crush_location_hook")
                       << " execute failed, exit_code is " << exit_status->exit_code;
                logger().error("{}", errmsg.str());
                throw std::runtime_error(errmsg.str());
              } else {
                _parse(out);
              }
              return seastar::now();
            });
          });
        });
      });
    });
  });
}

seastar::future<> CrushLocation::init_on_startup()
{
  if (local_conf().get_val<std::string>("crush_location").length()) {
    return update_from_conf();
  }
  if (local_conf().get_val<std::string>("crush_location_hook").length()) {
    return update_from_hook();
  }

  // start with a sane default
  char hostname[HOST_NAME_MAX + 1];
  int r = gethostname(hostname, sizeof(hostname));
  if (r < 0)
    strcpy(hostname, "unknown_host");
  // use short hostname
  for (unsigned i=0; hostname[i]; ++i) {
    if (hostname[i] == '.') {
      hostname[i] = '\0';
      break;
    }
  }

  loc.clear();
  loc.insert(std::make_pair<std::string, std::string>("host", hostname));
  loc.insert(std::make_pair<std::string, std::string>("root", "default"));
  return seastar::now();
}

std::multimap<std::string,std::string> CrushLocation::get_location() const
{
  return loc;
}

std::ostream& operator<<(std::ostream& os, const CrushLocation& loc)
{
  bool first = true;
  for (auto& [type, pos] : loc.get_location()) {
    if (first) {
      first = false;
    } else {
      os << ", ";
    }
    os << '"' << type << '=' << pos << '"';
  }
  return os;
}

}