-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosdmaptool.cc
334 lines (301 loc) · 9.47 KB
/
osdmaptool.cc
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
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab
/*
* Ceph - scalable distributed file system
*
* Copyright (C) 2004-2006 Sage Weil <[email protected]>
*
* This is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software
* Foundation. See file COPYING.
*
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <iostream>
#include <string>
using namespace std;
#include "common/config.h"
#include "common/errno.h"
#include "osd/OSDMap.h"
#include "mon/MonMap.h"
#include "common/ceph_argparse.h"
#include "global/global_init.h"
void usage()
{
cout << " usage: [--print] [--createsimple <numosd> [--clobber] [--pg_bits <bitsperosd>]] <mapfilename>" << std::endl;
cout << " --export-crush <file> write osdmap's crush map to <file>" << std::endl;
cout << " --import-crush <file> replace osdmap's crush map with <file>" << std::endl;
cout << " --test-map-pg <pgid> map a pgid to osds" << std::endl;
exit(1);
}
int main(int argc, const char **argv)
{
vector<const char*> args;
argv_to_vec(argc, argv, args);
env_to_vec(args);
global_init(NULL, args, CEPH_ENTITY_TYPE_CLIENT, CODE_ENVIRONMENT_UTILITY,
CINIT_FLAG_NO_DEFAULT_CONFIG_FILE);
common_init_finish(g_ceph_context);
const char *me = argv[0];
std::string fn;
bool print = false;
bool print_json = false;
bool tree = false;
bool createsimple = false;
bool create_from_conf = false;
int num_osd = 0;
int pg_bits = g_conf->osd_pg_bits;
int pgp_bits = g_conf->osd_pgp_bits;
bool clobber = false;
bool modified = false;
std::string export_crush, import_crush, test_map_pg, test_map_object;
list<entity_addr_t> add, rm;
bool test_crush = false;
int range_first = -1;
int range_last = -1;
std::string val;
std::ostringstream err;
for (std::vector<const char*>::iterator i = args.begin(); i != args.end(); ) {
if (ceph_argparse_double_dash(args, i)) {
break;
} else if (ceph_argparse_flag(args, i, "-h", "--help", (char*)NULL)) {
usage();
} else if (ceph_argparse_flag(args, i, "-p", "--print", (char*)NULL)) {
print = true;
} else if (ceph_argparse_flag(args, i, "--dump-json", (char*)NULL)) {
print_json = true;
} else if (ceph_argparse_flag(args, i, "--tree", (char*)NULL)) {
tree = true;
} else if (ceph_argparse_withint(args, i, &num_osd, &err, "--createsimple", (char*)NULL)) {
if (!err.str().empty()) {
cerr << err.str() << std::endl;
exit(EXIT_FAILURE);
}
createsimple = true;
} else if (ceph_argparse_flag(args, i, "--create-from-conf", (char*)NULL)) {
create_from_conf = true;
} else if (ceph_argparse_flag(args, i, "--clobber", (char*)NULL)) {
clobber = true;
} else if (ceph_argparse_withint(args, i, &pg_bits, &err, "--pg_bits", (char*)NULL)) {
if (!err.str().empty()) {
cerr << err.str() << std::endl;
exit(EXIT_FAILURE);
}
} else if (ceph_argparse_withint(args, i, &pgp_bits, &err, "--pgp_bits", (char*)NULL)) {
if (!err.str().empty()) {
cerr << err.str() << std::endl;
exit(EXIT_FAILURE);
}
} else if (ceph_argparse_witharg(args, i, &val, "--export_crush", (char*)NULL)) {
export_crush = val;
} else if (ceph_argparse_witharg(args, i, &val, "--import_crush", (char*)NULL)) {
import_crush = val;
} else if (ceph_argparse_witharg(args, i, &val, "--test_map_pg", (char*)NULL)) {
test_map_pg = val;
} else if (ceph_argparse_witharg(args, i, &val, "--test_map_object", (char*)NULL)) {
test_map_object = val;
} else if (ceph_argparse_flag(args, i, "--test_crush", (char*)NULL)) {
test_crush = true;
} else if (ceph_argparse_withint(args, i, &range_first, &err, "--range_first", (char*)NULL)) {
} else if (ceph_argparse_withint(args, i, &range_last, &err, "--range_last", (char*)NULL)) {
} else {
++i;
}
}
if (args.size() < 1) {
cerr << me << ": must specify osdmap filename" << std::endl;
usage();
}
else if (args.size() > 1) {
cerr << me << ": too many arguments" << std::endl;
usage();
}
fn = args[0];
if (range_first >= 0 && range_last >= 0) {
set<OSDMap*> maps;
OSDMap *prev = NULL;
for (int i=range_first; i <= range_last; i++) {
ostringstream f;
f << fn << "/" << i;
bufferlist bl;
string error, s = f.str();
bl.read_file(s.c_str(), &error);
cout << s << " got " << bl.length() << " bytes" << std::endl;
OSDMap *o = new OSDMap;
o->decode(bl);
maps.insert(o);
if (prev)
OSDMap::dedup(prev, o);
prev = o;
}
exit(0);
}
OSDMap osdmap;
bufferlist bl;
cout << me << ": osdmap file '" << fn << "'" << std::endl;
int r = 0;
struct stat st;
if (!createsimple && !create_from_conf && !clobber) {
std::string error;
r = bl.read_file(fn.c_str(), &error);
if (r == 0) {
try {
osdmap.decode(bl);
}
catch (const buffer::error &e) {
cerr << me << ": error decoding osdmap '" << fn << "'" << std::endl;
return -1;
}
}
else {
cerr << me << ": couldn't open " << fn << ": " << error << std::endl;
return -1;
}
}
else if ((createsimple || create_from_conf) && !clobber && ::stat(fn.c_str(), &st) == 0) {
cerr << me << ": " << fn << " exists, --clobber to overwrite" << std::endl;
return -1;
}
if (createsimple) {
if (num_osd < 1) {
cerr << me << ": osd count must be > 0" << std::endl;
exit(1);
}
uuid_d fsid;
memset(&fsid, 0, sizeof(uuid_d));
osdmap.build_simple(g_ceph_context, 0, fsid, num_osd, pg_bits, pgp_bits);
modified = true;
}
if (create_from_conf) {
uuid_d fsid;
memset(&fsid, 0, sizeof(uuid_d));
int r = osdmap.build_simple_from_conf(g_ceph_context, 0, fsid, pg_bits, pgp_bits);
if (r < 0)
return -1;
modified = true;
}
if (!import_crush.empty()) {
bufferlist cbl;
std::string error;
r = cbl.read_file(import_crush.c_str(), &error);
if (r) {
cerr << me << ": error reading crush map from " << import_crush
<< ": " << error << std::endl;
exit(1);
}
// validate
CrushWrapper cw;
bufferlist::iterator p = cbl.begin();
cw.decode(p);
if (cw.get_max_devices() > osdmap.get_max_osd()) {
cerr << me << ": crushmap max_devices " << cw.get_max_devices()
<< " > osdmap max_osd " << osdmap.get_max_osd() << std::endl;
exit(1);
}
// apply
OSDMap::Incremental inc;
inc.fsid = osdmap.get_fsid();
inc.epoch = osdmap.get_epoch()+1;
inc.crush = cbl;
osdmap.apply_incremental(inc);
cout << me << ": imported " << cbl.length() << " byte crush map from " << import_crush << std::endl;
modified = true;
}
if (!export_crush.empty()) {
bufferlist cbl;
osdmap.crush->encode(cbl);
r = cbl.write_file(export_crush.c_str());
if (r < 0) {
cerr << me << ": error writing crush map to " << import_crush << std::endl;
exit(1);
}
cout << me << ": exported crush map to " << export_crush << std::endl;
}
if (!test_map_object.empty()) {
object_t oid(test_map_object);
ceph_object_layout ol = osdmap.make_object_layout(oid, 0);
pg_t pgid;
pgid = ol.ol_pgid;
vector<int> acting;
osdmap.pg_to_acting_osds(pgid, acting);
cout << " object '" << oid
<< "' -> " << pgid
<< " -> " << acting
<< std::endl;
}
if (!test_map_pg.empty()) {
pg_t pgid;
if (pgid.parse(test_map_pg.c_str()) < 0) {
cerr << me << ": failed to parse pg '" << test_map_pg
<< "', r = " << r << std::endl;
usage();
}
cout << " parsed '" << test_map_pg << "' -> " << pgid << std::endl;
vector<int> raw, up, acting;
osdmap.pg_to_osds(pgid, raw);
osdmap.pg_to_up_acting_osds(pgid, up, acting);
cout << pgid << " raw " << raw << " up " << up << " acting " << acting << std::endl;
}
if (test_crush) {
int pass = 0;
while (1) {
cout << "pass " << ++pass << std::endl;
hash_map<pg_t,vector<int> > m;
for (map<int64_t,pg_pool_t>::const_iterator p = osdmap.get_pools().begin();
p != osdmap.get_pools().end();
p++) {
const pg_pool_t *pool = osdmap.get_pg_pool(p->first);
for (ps_t ps = 0; ps < pool->get_pg_num(); ps++) {
pg_t pgid(ps, p->first, -1);
for (int i=0; i<100; i++) {
cout << pgid << " attempt " << i << std::endl;
vector<int> r, s;
osdmap.pg_to_acting_osds(pgid, r);
//cout << pgid << " " << r << std::endl;
if (m.count(pgid)) {
if (m[pgid] != r) {
cout << pgid << " had " << m[pgid] << " now " << r << std::endl;
assert(0);
}
} else
m[pgid] = r;
}
}
}
}
}
if (!print && !print_json && !tree && !modified &&
export_crush.empty() && import_crush.empty() &&
test_map_pg.empty() && test_map_object.empty()) {
cerr << me << ": no action specified?" << std::endl;
usage();
}
if (modified)
osdmap.inc_epoch();
if (print)
osdmap.print(cout);
if (print_json)
osdmap.dump_json(cout);
if (tree)
osdmap.print_tree(cout);
if (modified) {
bl.clear();
osdmap.encode(bl);
// write it out
cout << me << ": writing epoch " << osdmap.get_epoch()
<< " to " << fn
<< std::endl;
int r = bl.write_file(fn.c_str());
if (r) {
cerr << "osdmaptool: error writing to '" << fn << "': "
<< cpp_strerror(r) << std::endl;
return 1;
}
}
return 0;
}