1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2004-2006 Sage Weil <sage@newdream.net>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
15 #include "include/types.h"
17 #include "include/rados/librados.hpp"
18 #include "include/rados/rados_types.hpp"
19 #include "include/radosstriper/libradosstriper.hpp"
20 using namespace libradosstriper
;
22 #include "common/config.h"
23 #include "common/ceph_argparse.h"
24 #include "global/global_init.h"
25 #include "common/Cond.h"
26 #include "common/debug.h"
27 #include "common/errno.h"
28 #include "common/Formatter.h"
29 #include "common/obj_bencher.h"
30 #include "common/TextTable.h"
31 #include "include/stringify.h"
32 #include "mds/inode_backtrace.h"
33 #include "auth/Crypto.h"
47 #include "cls/lock/cls_lock_client.h"
48 #include "include/compat.h"
49 #include "include/util.h"
50 #include "common/hobject.h"
53 #include "RadosImport.h"
55 #include "osd/ECUtil.h"
57 using namespace librados
;
59 // two steps seem to be necessary to do this right
60 #define STR(x) _STR(x)
63 void usage(ostream
& out
)
66 "usage: rados [options] [commands]\n"
68 " lspools list pools\n"
69 " mkpool <pool-name> [123[ 4]] create pool <pool-name>'\n"
70 " [with auid 123[and using crush rule 4]]\n"
71 " cppool <pool-name> <dest-pool> copy content of a pool\n"
72 " rmpool <pool-name> [<pool-name> --yes-i-really-really-mean-it]\n"
73 " remove pool <pool-name>'\n"
74 " purge <pool-name> --yes-i-really-really-mean-it\n"
75 " remove all objects from pool <pool-name> without removing it\n"
76 " df show per-pool and total usage\n"
77 " ls list objects in pool\n\n"
78 " chown 123 change the pool owner to auid 123\n"
80 "POOL SNAP COMMANDS\n"
81 " lssnap list snaps\n"
82 " mksnap <snap-name> create snap <snap-name>\n"
83 " rmsnap <snap-name> remove snap <snap-name>\n"
86 " get <obj-name> [outfile] fetch object\n"
87 " put <obj-name> [infile] [--offset offset]\n"
88 " write object with start offset (default:0)\n"
89 " append <obj-name> [infile] append object\n"
90 " truncate <obj-name> length truncate object\n"
91 " create <obj-name> create object\n"
92 " rm <obj-name> ...[--force-full] [force no matter full or not]remove object(s)\n"
93 " cp <obj-name> [target-obj] copy object\n"
94 " listxattr <obj-name>\n"
95 " getxattr <obj-name> attr\n"
96 " setxattr <obj-name> attr val\n"
97 " rmxattr <obj-name> attr\n"
98 " stat <obj-name> stat the named object\n"
99 " mapext <obj-name>\n"
100 " rollback <obj-name> <snap-name> roll back object to snap <snap-name>\n"
102 " listsnaps <obj-name> list the snapshots of this object\n"
103 " bench <seconds> write|seq|rand [-t concurrent_operations] [--no-cleanup] [--run-name run_name] [--no-hints]\n"
104 " default is 16 concurrent IOs and 4 MB ops\n"
105 " default is to clean up after write benchmark\n"
106 " default run-name is 'benchmark_last_metadata'\n"
107 " cleanup [--run-name run_name] [--prefix prefix]\n"
108 " clean up a previous benchmark operation\n"
109 " default run-name is 'benchmark_last_metadata'\n"
110 " load-gen [options] generate load on the cluster\n"
111 " listomapkeys <obj-name> list the keys in the object map\n"
112 " listomapvals <obj-name> list the keys and vals in the object map \n"
113 " getomapval <obj-name> <key> [file] show the value for the specified key\n"
114 " in the object's object map\n"
115 " setomapval <obj-name> <key> <val>\n"
116 " rmomapkey <obj-name> <key>\n"
117 " getomapheader <obj-name> [file]\n"
118 " setomapheader <obj-name> <val>\n"
119 " tmap-to-omap <obj-name> convert tmap keys/values to omap\n"
120 " watch <obj-name> add watcher on this object\n"
121 " notify <obj-name> <message> notify watcher of this object with message\n"
122 " listwatchers <obj-name> list the watchers of this object\n"
123 " set-alloc-hint <obj-name> <expected-object-size> <expected-write-size>\n"
124 " set allocation hint for an object\n"
126 "IMPORT AND EXPORT\n"
127 " export [filename]\n"
128 " Serialize pool contents to a file or standard out.\n"
129 " import [--dry-run] [--no-overwrite] < filename | - >\n"
130 " Load pool contents from a file or standard in\n"
133 " lock list <obj-name>\n"
134 " List all advisory locks on an object\n"
135 " lock get <obj-name> <lock-name>\n"
136 " Try to acquire a lock\n"
137 " lock break <obj-name> <lock-name> <locker-name>\n"
138 " Try to break a lock acquired by another client\n"
139 " lock info <obj-name> <lock-name>\n"
140 " Show lock information\n"
142 " --lock-tag Lock tag, all locks operation should use\n"
144 " --lock-cookie Locker cookie\n"
145 " --lock-description Description of lock\n"
146 " --lock-duration Lock duration (in seconds)\n"
147 " --lock-type Lock type (shared, exclusive)\n"
149 "SCRUB AND REPAIR:\n"
150 " list-inconsistent-pg <pool> list inconsistent PGs in given pool\n"
151 " list-inconsistent-obj <pgid> list inconsistent objects in given pg\n"
152 " list-inconsistent-snapset <pgid> list inconsistent snapsets in the given pg\n"
154 "CACHE POOLS: (for testing/development only)\n"
155 " cache-flush <obj-name> flush cache pool object (blocking)\n"
156 " cache-try-flush <obj-name> flush cache pool object (non-blocking)\n"
157 " cache-evict <obj-name> evict cache pool object\n"
158 " cache-flush-evict-all flush+evict all objects\n"
159 " cache-try-flush-evict-all try-flush+evict all objects\n"
162 " --object_locator object_locator\n"
163 " set object_locator for operation\n"
166 " select given pool by name\n"
167 " --target-pool=pool\n"
168 " select target pool by name\n"
170 " set the block size for put/get ops and for write benchmarking\n"
172 " set the object size for put/get ops and for write benchmarking\n"
174 " set the max number of objects for write benchmarking\n"
177 " select given snap name for (read) IO\n"
180 " create the pool or directory that was specified\n"
182 " --namespace=namespace\n"
183 " specify the namespace to use for the object\n"
185 " Use with ls to list objects in all namespaces\n"
186 " Put in CEPH_ARGS environment variable to make this the default\n"
188 " Use with ls to list objects in default namespace\n"
189 " Takes precedence over --all in case --all is in environment\n"
190 " --target-locator\n"
191 " Use with cp to specify the locator of the new object\n"
193 " Use with cp to specify the namespace of the new object\n"
195 " Use radostriper interface rather than pure rados\n"
196 " Available for stat, get, put, truncate, rm, ls and \n"
197 " all xattr related operations\n"
201 " --concurrent-ios=N\n"
202 " Set number of concurrent I/O operations\n"
204 " prefix output with date/time\n"
206 " do not verify contents of read objects\n"
208 " write contents to the objects\n"
210 " write contents to the omap\n"
212 " write contents to the extended attributes\n"
214 "LOAD GEN OPTIONS:\n"
215 " --num-objects total number of objects\n"
216 " --min-object-size min object size\n"
217 " --max-object-size max object size\n"
218 " --min-op-len min io size of operations\n"
219 " --max-op-len max io size of operations\n"
220 " --max-ops max number of operations\n"
221 " --max-backlog max backlog size\n"
222 " --read-percent percent of operations that are read\n"
223 " --target-throughput target throughput (in bytes)\n"
224 " --run-length total time (in seconds)\n"
225 "CACHE POOLS OPTIONS:\n"
226 " --with-clones include clones when doing flush or evict\n"
228 " --omap-key-file file read the omap key from a file\n";
231 unsigned default_op_size
= 1 << 22;
233 static void usage_exit()
240 template <typename I
, typename T
>
241 static int rados_sistrtoll(I
&i
, T
*val
) {
243 *val
= strict_sistrtoll(i
->second
.c_str(), &err
);
245 cerr
<< "Invalid value for " << i
->first
<< ": " << err
<< std::endl
;
253 static int dump_data(std::string
const &filename
, bufferlist
const &data
)
256 if (filename
== "-") {
259 fd
= TEMP_FAILURE_RETRY(::open(filename
.c_str(), O_WRONLY
|O_CREAT
|O_TRUNC
, 0644));
262 cerr
<< "failed to open file: " << cpp_strerror(err
) << std::endl
;
267 int r
= data
.write_fd(fd
);
270 VOID_TEMP_FAILURE_RETRY(::close(fd
));
277 static int do_get(IoCtx
& io_ctx
, RadosStriper
& striper
,
278 const char *objname
, const char *outfile
, unsigned op_size
,
284 if (strcmp(outfile
, "-") == 0) {
287 fd
= TEMP_FAILURE_RETRY(::open(outfile
, O_WRONLY
|O_CREAT
|O_TRUNC
, 0644));
290 cerr
<< "failed to open file: " << cpp_strerror(err
) << std::endl
;
300 ret
= striper
.read(oid
, &outdata
, op_size
, offset
);
302 ret
= io_ctx
.read(oid
, outdata
, op_size
, offset
);
307 ret
= outdata
.write_fd(fd
);
309 cerr
<< "error writing to file: " << cpp_strerror(ret
) << std::endl
;
312 if (outdata
.length() < op_size
)
314 offset
+= outdata
.length();
320 VOID_TEMP_FAILURE_RETRY(::close(fd
));
324 static int do_copy(IoCtx
& io_ctx
, const char *objname
,
325 IoCtx
& target_ctx
, const char *target_obj
)
327 __le32 src_fadvise_flags
= LIBRADOS_OP_FLAG_FADVISE_SEQUENTIAL
| LIBRADOS_OP_FLAG_FADVISE_NOCACHE
;
328 __le32 dest_fadvise_flags
= LIBRADOS_OP_FLAG_FADVISE_SEQUENTIAL
| LIBRADOS_OP_FLAG_FADVISE_DONTNEED
;
329 ObjectWriteOperation op
;
330 op
.copy_from2(objname
, io_ctx
, 0, src_fadvise_flags
);
331 op
.set_op_flags2(dest_fadvise_flags
);
333 return target_ctx
.operate(target_obj
, &op
);
336 static int do_copy_pool(Rados
& rados
, const char *src_pool
, const char *target_pool
)
338 IoCtx src_ctx
, target_ctx
;
339 int ret
= rados
.ioctx_create(src_pool
, src_ctx
);
341 cerr
<< "cannot open source pool: " << src_pool
<< std::endl
;
344 ret
= rados
.ioctx_create(target_pool
, target_ctx
);
346 cerr
<< "cannot open target pool: " << target_pool
<< std::endl
;
349 src_ctx
.set_namespace(all_nspaces
);
350 librados::NObjectIterator i
= src_ctx
.nobjects_begin();
351 librados::NObjectIterator i_end
= src_ctx
.nobjects_end();
352 for (; i
!= i_end
; ++i
) {
353 string nspace
= i
->get_nspace();
354 string oid
= i
->get_oid();
355 string locator
= i
->get_locator();
357 string target_name
= (nspace
.size() ? nspace
+ "/" : "") + oid
;
358 string src_name
= target_name
;
360 src_name
+= "(@" + locator
+ ")";
361 cout
<< src_pool
<< ":" << src_name
<< " => "
362 << target_pool
<< ":" << target_name
<< std::endl
;
364 src_ctx
.locator_set_key(locator
);
365 src_ctx
.set_namespace(nspace
);
366 target_ctx
.set_namespace(nspace
);
367 ret
= do_copy(src_ctx
, oid
.c_str(), target_ctx
, oid
.c_str());
369 cerr
<< "error copying object: " << cpp_strerror(errno
) << std::endl
;
377 static int do_put(IoCtx
& io_ctx
, RadosStriper
& striper
,
378 const char *objname
, const char *infile
, int op_size
,
379 uint64_t obj_offset
, bool use_striper
)
382 bool stdio
= (strcmp(infile
, "-") == 0);
384 int fd
= STDIN_FILENO
;
386 fd
= open(infile
, O_RDONLY
);
388 cerr
<< "error reading input file " << infile
<< ": " << cpp_strerror(errno
) << std::endl
;
392 uint64_t offset
= obj_offset
;
395 count
= indata
.read_fd(fd
, op_size
);
398 cerr
<< "error reading input file " << infile
<< ": " << cpp_strerror(ret
) << std::endl
;
403 if (offset
== obj_offset
) { // in case we have to create an empty object & if obj_offset > 0 do a hole
405 ret
= striper
.write_full(oid
, indata
); // indata is empty
407 ret
= io_ctx
.write_full(oid
, indata
); // indata is empty
414 ret
= striper
.trunc(oid
, offset
); // before truncate, object must be existed.
416 ret
= io_ctx
.trunc(oid
, offset
); // before truncate, object must be existed.
428 ret
= striper
.write_full(oid
, indata
);
430 ret
= striper
.write(oid
, indata
, count
, offset
);
433 ret
= io_ctx
.write_full(oid
, indata
);
435 ret
= io_ctx
.write(oid
, indata
, count
, offset
);
445 if (fd
!= STDOUT_FILENO
)
446 VOID_TEMP_FAILURE_RETRY(close(fd
));
450 static int do_append(IoCtx
& io_ctx
, RadosStriper
& striper
,
451 const char *objname
, const char *infile
, int op_size
,
455 bool stdio
= (strcmp(infile
, "-") == 0);
457 int fd
= STDIN_FILENO
;
459 fd
= open(infile
, O_RDONLY
);
461 cerr
<< "error reading input file " << infile
<< ": " << cpp_strerror(errno
) << std::endl
;
467 count
= indata
.read_fd(fd
, op_size
);
470 cerr
<< "error reading input file " << infile
<< ": " << cpp_strerror(ret
) << std::endl
;
474 ret
= striper
.append(oid
, indata
, count
);
476 ret
= io_ctx
.append(oid
, indata
, count
);
485 if (fd
!= STDOUT_FILENO
)
486 VOID_TEMP_FAILURE_RETRY(close(fd
));
490 class RadosWatchCtx
: public librados::WatchCtx2
{
494 RadosWatchCtx(IoCtx
& io
, const char *imgname
) : ioctx(io
), name(imgname
) {}
495 ~RadosWatchCtx() override
{}
496 void handle_notify(uint64_t notify_id
,
498 uint64_t notifier_id
,
499 bufferlist
& bl
) override
{
501 << " cookie " << cookie
502 << " notify_id " << notify_id
503 << " from " << notifier_id
506 ioctx
.notify_ack(name
, notify_id
, cookie
, bl
);
508 void handle_error(uint64_t cookie
, int err
) override
{
510 << " cookie " << cookie
511 << " err " << cpp_strerror(err
)
516 static const char alphanum_table
[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
518 int gen_rand_alphanumeric(char *dest
, int size
) /* size should be the required string size + 1 */
520 int ret
= get_random_bytes(dest
, size
);
522 cerr
<< "cannot get random bytes: " << cpp_strerror(ret
) << std::endl
;
527 for (i
=0; i
<size
- 1; i
++) {
528 int pos
= (unsigned)dest
[i
];
529 dest
[i
] = alphanum_table
[pos
& 63];
543 size_t total_completed
;
548 map
<int, obj_info
> objs
;
558 uint64_t max_obj_len
;
563 size_t target_throughput
;
579 librados::AioCompletion
*completion
;
581 LoadGenOp() : id(0), type(0), off(0), len(0), lg(NULL
), completion(NULL
) {}
582 explicit LoadGenOp(LoadGen
*_lg
) : id(0), type(0), off(0), len(0), lg(_lg
), completion(NULL
) {}
587 map
<int, LoadGenOp
*> pending_ops
;
589 void gen_op(LoadGenOp
*op
);
590 uint64_t gen_next_op();
591 void run_op(LoadGenOp
*op
);
593 uint64_t cur_sent_rate() {
594 return total_sent
/ time_passed();
597 uint64_t cur_completed_rate() {
598 return total_completed
/ time_passed();
601 uint64_t total_expected() {
602 return target_throughput
* time_passed();
605 float time_passed() {
606 utime_t now
= ceph_clock_now();
608 uint64_t ns
= now
.nsec();
609 float total
= (float) ns
/ 1000000000.0;
617 explicit LoadGen(Rados
*_rados
) : rados(_rados
), going_down(false), lock("LoadGen") {
620 max_obj_len
= 5ull * 1024ull * 1024ull * 1024ull;
622 target_throughput
= 5 * 1024 * 1024; // B/sec
623 max_op_len
= 2 * 1024 * 1024;
625 max_backlog
= target_throughput
* 2;
633 int bootstrap(const char *pool
);
637 void io_cb(completion_t c
, LoadGenOp
*op
) {
638 Mutex::Locker
l(lock
);
640 total_completed
+= op
->len
;
642 double rate
= (double)cur_completed_rate() / (1024 * 1024);
643 std::streamsize original_precision
= cout
.precision();
645 cout
<< "op " << op
->id
<< " completed, throughput=" << rate
<< "MB/sec" << std::endl
;
646 cout
.precision(original_precision
);
648 map
<int, LoadGenOp
*>::iterator iter
= pending_ops
.find(op
->id
);
649 if (iter
!= pending_ops
.end())
650 pending_ops
.erase(iter
);
653 op
->completion
->release();
661 static void _load_gen_cb(completion_t c
, void *param
)
663 LoadGen::LoadGenOp
*op
= (LoadGen::LoadGenOp
*)param
;
664 op
->lg
->io_cb(c
, op
);
667 int LoadGen::bootstrap(const char *pool
)
673 cerr
<< "ERROR: pool name was not specified" << std::endl
;
677 int ret
= rados
->ioctx_create(pool
, io_ctx
);
679 cerr
<< "error opening pool " << pool
<< ": " << cpp_strerror(ret
) << std::endl
;
684 bufferptr p
= buffer::create(buf_len
);
686 memset(p
.c_str(), 0, buf_len
);
689 list
<librados::AioCompletion
*> completions
;
690 for (i
= 0; i
< num_objs
; i
++) {
692 gen_rand_alphanumeric(buf
, 16);
694 info
.name
.append(buf
);
695 info
.len
= get_random(min_obj_len
, max_obj_len
);
698 while (completions
.size() > max_ops
) {
699 AioCompletion
*c
= completions
.front();
700 c
->wait_for_complete();
701 ret
= c
->get_return_value();
703 completions
.pop_front();
705 cerr
<< "aio_write failed" << std::endl
;
710 librados::AioCompletion
*c
= rados
->aio_create_completion(NULL
, NULL
, NULL
);
711 completions
.push_back(c
);
713 ret
= io_ctx
.aio_write(info
.name
, c
, bl
, buf_len
, info
.len
- buf_len
);
715 cerr
<< "couldn't write obj: " << info
.name
<< " ret=" << ret
<< std::endl
;
721 list
<librados::AioCompletion
*>::iterator iter
;
722 for (iter
= completions
.begin(); iter
!= completions
.end(); ++iter
) {
723 AioCompletion
*c
= *iter
;
724 c
->wait_for_complete();
725 ret
= c
->get_return_value();
727 if (ret
< 0) { // yes, we leak.
728 cerr
<< "aio_write failed" << std::endl
;
735 void LoadGen::run_op(LoadGenOp
*op
)
737 op
->completion
= rados
->aio_create_completion(op
, _load_gen_cb
, NULL
);
741 io_ctx
.aio_read(op
->oid
, op
->completion
, &op
->bl
, op
->len
, op
->off
);
744 bufferptr p
= buffer::create(op
->len
);
745 memset(p
.c_str(), 0, op
->len
);
748 io_ctx
.aio_write(op
->oid
, op
->completion
, op
->bl
, op
->len
, op
->off
);
752 total_sent
+= op
->len
;
755 void LoadGen::gen_op(LoadGenOp
*op
)
757 int i
= get_random(0, objs
.size() - 1);
758 obj_info
& info
= objs
[i
];
761 size_t len
= get_random(min_op_len
, max_op_len
);
764 size_t off
= get_random(0, info
.len
);
766 if (off
+ len
> info
.len
)
767 off
= info
.len
- len
;
772 i
= get_random(1, 100);
773 if (i
> read_percent
)
778 cout
<< (op
->type
== OP_READ
? "READ" : "WRITE") << " : oid=" << op
->oid
<< " off=" << op
->off
<< " len=" << op
->len
<< std::endl
;
781 uint64_t LoadGen::gen_next_op()
785 LoadGenOp
*op
= new LoadGenOp(this);
788 pending_ops
[op
->id
] = op
;
799 start_time
= ceph_clock_now();
800 utime_t end_time
= start_time
;
801 end_time
+= run_length
;
802 utime_t stamp_time
= start_time
;
803 uint32_t total_sec
= 0;
807 utime_t
one_second(1, 0);
808 cond
.WaitInterval(lock
, one_second
);
810 utime_t now
= ceph_clock_now();
815 uint64_t expected
= total_expected();
817 uint64_t sent
= total_sent
;
818 uint64_t completed
= total_completed
;
821 if (now
- stamp_time
>= utime_t(1, 0)) {
822 double rate
= (double)cur_completed_rate() / (1024 * 1024);
824 std::streamsize original_precision
= cout
.precision();
826 cout
<< setw(5) << total_sec
<< ": throughput=" << rate
<< "MB/sec" << " pending data=" << sent
- completed
<< std::endl
;
827 cout
.precision(original_precision
);
831 while (sent
< expected
&&
832 sent
- completed
< max_backlog
&&
833 pending_ops
.size() < max_ops
) {
834 sent
+= gen_next_op();
838 // get a reference to all pending requests
839 vector
<librados::AioCompletion
*> completions
;
842 map
<int, LoadGenOp
*>::iterator iter
;
843 for (iter
= pending_ops
.begin(); iter
!= pending_ops
.end(); ++iter
) {
844 LoadGenOp
*op
= iter
->second
;
845 completions
.push_back(op
->completion
);
849 cout
<< "waiting for all operations to complete" << std::endl
;
851 // now wait on all the pending requests
852 for (vector
<librados::AioCompletion
*>::iterator citer
= completions
.begin(); citer
!= completions
.end(); ++citer
) {
853 librados::AioCompletion
*c
= *citer
;
854 c
->wait_for_complete();
861 void LoadGen::cleanup()
863 cout
<< "cleaning up objects" << std::endl
;
864 map
<int, obj_info
>::iterator iter
;
865 for (iter
= objs
.begin(); iter
!= objs
.end(); ++iter
) {
866 obj_info
& info
= iter
->second
;
867 int ret
= io_ctx
.remove(info
.name
);
869 cerr
<< "couldn't remove obj: " << info
.name
<< " ret=" << ret
<< std::endl
;
874 OP_WRITE_DEST_OBJ
= 2 << 0,
875 OP_WRITE_DEST_OMAP
= 2 << 1,
876 OP_WRITE_DEST_XATTR
= 2 << 2,
879 class RadosBencher
: public ObjBencher
{
880 librados::AioCompletion
**completions
;
881 librados::Rados
& rados
;
882 librados::IoCtx
& io_ctx
;
883 librados::NObjectIterator oi
;
885 OpWriteDest write_destination
;
888 int completions_init(int concurrentios
) override
{
889 completions
= new librados::AioCompletion
*[concurrentios
];
892 void completions_done() override
{
893 delete[] completions
;
896 int create_completion(int slot
, void (*cb
)(void *, void*), void *arg
) override
{
897 completions
[slot
] = rados
.aio_create_completion((void *) arg
, 0, cb
);
899 if (!completions
[slot
])
904 void release_completion(int slot
) override
{
905 completions
[slot
]->release();
906 completions
[slot
] = 0;
909 int aio_read(const std::string
& oid
, int slot
, bufferlist
*pbl
, size_t len
,
910 size_t offset
) override
{
911 return io_ctx
.aio_read(oid
, completions
[slot
], pbl
, len
, 0);
914 int aio_write(const std::string
& oid
, int slot
, bufferlist
& bl
, size_t len
,
915 size_t offset
) override
{
916 librados::ObjectWriteOperation op
;
918 if (write_destination
& OP_WRITE_DEST_OBJ
) {
920 op
.set_alloc_hint2(data
.object_size
, data
.op_size
,
921 ALLOC_HINT_FLAG_SEQUENTIAL_WRITE
|
922 ALLOC_HINT_FLAG_SEQUENTIAL_READ
|
923 ALLOC_HINT_FLAG_APPEND_ONLY
|
924 ALLOC_HINT_FLAG_IMMUTABLE
);
925 op
.write(offset
, bl
);
928 if (write_destination
& OP_WRITE_DEST_OMAP
) {
929 std::map
<std::string
, librados::bufferlist
> omap
;
930 omap
[string("bench-omap-key-") + stringify(offset
)] = bl
;
934 if (write_destination
& OP_WRITE_DEST_XATTR
) {
936 snprintf(key
, sizeof(key
), "bench-xattr-key-%d", (int)offset
);
937 op
.setxattr(key
, bl
);
940 return io_ctx
.aio_operate(oid
, completions
[slot
], &op
);
943 int aio_remove(const std::string
& oid
, int slot
) override
{
944 return io_ctx
.aio_remove(oid
, completions
[slot
]);
947 int sync_read(const std::string
& oid
, bufferlist
& bl
, size_t len
) override
{
948 return io_ctx
.read(oid
, bl
, len
, 0);
950 int sync_write(const std::string
& oid
, bufferlist
& bl
, size_t len
) override
{
951 return io_ctx
.write_full(oid
, bl
);
954 int sync_remove(const std::string
& oid
) override
{
955 return io_ctx
.remove(oid
);
958 bool completion_is_done(int slot
) override
{
959 return completions
[slot
]->is_safe();
962 int completion_wait(int slot
) override
{
963 return completions
[slot
]->wait_for_safe_and_cb();
965 int completion_ret(int slot
) override
{
966 return completions
[slot
]->get_return_value();
969 bool get_objects(std::list
<Object
>* objects
, int num
) override
{
972 if (!iterator_valid
) {
973 oi
= io_ctx
.nobjects_begin();
974 iterator_valid
= true;
977 librados::NObjectIterator ei
= io_ctx
.nobjects_end();
980 iterator_valid
= false;
985 for ( ; oi
!= ei
&& count
< num
; ++oi
) {
986 Object
obj(oi
->get_oid(), oi
->get_nspace());
987 objects
->push_back(obj
);
994 void set_namespace( const std::string
& ns
) override
{
995 io_ctx
.set_namespace(ns
);
999 RadosBencher(CephContext
*cct_
, librados::Rados
& _r
, librados::IoCtx
& _i
)
1000 : ObjBencher(cct_
), completions(NULL
), rados(_r
), io_ctx(_i
), iterator_valid(false), write_destination(OP_WRITE_DEST_OBJ
) {}
1001 ~RadosBencher() override
{ }
1003 void set_write_destination(OpWriteDest dest
) {
1004 write_destination
= dest
;
1008 static int do_lock_cmd(std::vector
<const char*> &nargs
,
1009 const std::map
< std::string
, std::string
> &opts
,
1011 Formatter
*formatter
)
1013 if (nargs
.size() < 3)
1016 string
cmd(nargs
[1]);
1017 string
oid(nargs
[2]);
1021 string lock_description
;
1022 int lock_duration
= 0;
1023 ClsLockType lock_type
= LOCK_EXCLUSIVE
;
1025 map
<string
, string
>::const_iterator i
;
1026 i
= opts
.find("lock-tag");
1027 if (i
!= opts
.end()) {
1028 lock_tag
= i
->second
;
1030 i
= opts
.find("lock-cookie");
1031 if (i
!= opts
.end()) {
1032 lock_cookie
= i
->second
;
1034 i
= opts
.find("lock-description");
1035 if (i
!= opts
.end()) {
1036 lock_description
= i
->second
;
1038 i
= opts
.find("lock-duration");
1039 if (i
!= opts
.end()) {
1040 if (rados_sistrtoll(i
, &lock_duration
)) {
1044 i
= opts
.find("lock-type");
1045 if (i
!= opts
.end()) {
1046 const string
& type_str
= i
->second
;
1047 if (type_str
.compare("exclusive") == 0) {
1048 lock_type
= LOCK_EXCLUSIVE
;
1049 } else if (type_str
.compare("shared") == 0) {
1050 lock_type
= LOCK_SHARED
;
1052 cerr
<< "unknown lock type was specified, aborting" << std::endl
;
1057 if (cmd
.compare("list") == 0) {
1059 int ret
= rados::cls::lock::list_locks(ioctx
, oid
, &locks
);
1061 cerr
<< "ERROR: rados_list_locks(): " << cpp_strerror(ret
) << std::endl
;
1065 formatter
->open_object_section("object");
1066 formatter
->dump_string("objname", oid
);
1067 formatter
->open_array_section("locks");
1068 list
<string
>::iterator iter
;
1069 for (iter
= locks
.begin(); iter
!= locks
.end(); ++iter
) {
1070 formatter
->open_object_section("lock");
1071 formatter
->dump_string("name", *iter
);
1072 formatter
->close_section();
1074 formatter
->close_section();
1075 formatter
->close_section();
1076 formatter
->flush(cout
);
1080 if (nargs
.size() < 4)
1083 string
lock_name(nargs
[3]);
1085 if (cmd
.compare("info") == 0) {
1086 map
<rados::cls::lock::locker_id_t
, rados::cls::lock::locker_info_t
> lockers
;
1087 ClsLockType type
= LOCK_NONE
;
1089 int ret
= rados::cls::lock::get_lock_info(ioctx
, oid
, lock_name
, &lockers
, &type
, &tag
);
1091 cerr
<< "ERROR: rados_lock_get_lock_info(): " << cpp_strerror(ret
) << std::endl
;
1095 formatter
->open_object_section("lock");
1096 formatter
->dump_string("name", lock_name
);
1097 formatter
->dump_string("type", cls_lock_type_str(type
));
1098 formatter
->dump_string("tag", tag
);
1099 formatter
->open_array_section("lockers");
1100 map
<rados::cls::lock::locker_id_t
, rados::cls::lock::locker_info_t
>::iterator iter
;
1101 for (iter
= lockers
.begin(); iter
!= lockers
.end(); ++iter
) {
1102 const rados::cls::lock::locker_id_t
& id
= iter
->first
;
1103 const rados::cls::lock::locker_info_t
& info
= iter
->second
;
1104 formatter
->open_object_section("locker");
1105 formatter
->dump_stream("name") << id
.locker
;
1106 formatter
->dump_string("cookie", id
.cookie
);
1107 formatter
->dump_string("description", info
.description
);
1108 formatter
->dump_stream("expiration") << info
.expiration
;
1109 formatter
->dump_stream("addr") << info
.addr
;
1110 formatter
->close_section();
1112 formatter
->close_section();
1113 formatter
->close_section();
1114 formatter
->flush(cout
);
1117 } else if (cmd
.compare("get") == 0) {
1118 rados::cls::lock::Lock
l(lock_name
);
1119 l
.set_cookie(lock_cookie
);
1120 l
.set_tag(lock_tag
);
1121 l
.set_duration(utime_t(lock_duration
, 0));
1122 l
.set_description(lock_description
);
1124 switch (lock_type
) {
1126 ret
= l
.lock_shared(ioctx
, oid
);
1129 ret
= l
.lock_exclusive(ioctx
, oid
);
1132 cerr
<< "ERROR: failed locking: " << cpp_strerror(ret
) << std::endl
;
1139 if (nargs
.size() < 5)
1142 if (cmd
.compare("break") == 0) {
1143 string
locker(nargs
[4]);
1144 rados::cls::lock::Lock
l(lock_name
);
1145 l
.set_cookie(lock_cookie
);
1146 l
.set_tag(lock_tag
);
1148 if (!name
.parse(locker
)) {
1149 cerr
<< "ERROR: failed to parse locker name (" << locker
<< ")" << std::endl
;
1152 int ret
= l
.break_lock(ioctx
, oid
, name
);
1154 cerr
<< "ERROR: failed breaking lock: " << cpp_strerror(ret
) << std::endl
;
1164 static int do_cache_flush(IoCtx
& io_ctx
, string oid
)
1166 ObjectReadOperation op
;
1168 librados::AioCompletion
*completion
=
1169 librados::Rados::aio_create_completion();
1170 io_ctx
.aio_operate(oid
.c_str(), completion
, &op
,
1171 librados::OPERATION_IGNORE_CACHE
|
1172 librados::OPERATION_IGNORE_OVERLAY
,
1174 completion
->wait_for_safe();
1175 int r
= completion
->get_return_value();
1176 completion
->release();
1180 static int do_cache_try_flush(IoCtx
& io_ctx
, string oid
)
1182 ObjectReadOperation op
;
1183 op
.cache_try_flush();
1184 librados::AioCompletion
*completion
=
1185 librados::Rados::aio_create_completion();
1186 io_ctx
.aio_operate(oid
.c_str(), completion
, &op
,
1187 librados::OPERATION_IGNORE_CACHE
|
1188 librados::OPERATION_IGNORE_OVERLAY
|
1189 librados::OPERATION_SKIPRWLOCKS
,
1191 completion
->wait_for_safe();
1192 int r
= completion
->get_return_value();
1193 completion
->release();
1197 static int do_cache_evict(IoCtx
& io_ctx
, string oid
)
1199 ObjectReadOperation op
;
1201 librados::AioCompletion
*completion
=
1202 librados::Rados::aio_create_completion();
1203 io_ctx
.aio_operate(oid
.c_str(), completion
, &op
,
1204 librados::OPERATION_IGNORE_CACHE
|
1205 librados::OPERATION_IGNORE_OVERLAY
|
1206 librados::OPERATION_SKIPRWLOCKS
,
1208 completion
->wait_for_safe();
1209 int r
= completion
->get_return_value();
1210 completion
->release();
1214 static int do_cache_flush_evict_all(IoCtx
& io_ctx
, bool blocking
)
1217 io_ctx
.set_namespace(all_nspaces
);
1219 librados::NObjectIterator i
= io_ctx
.nobjects_begin();
1220 librados::NObjectIterator i_end
= io_ctx
.nobjects_end();
1221 for (; i
!= i_end
; ++i
) {
1223 cout
<< i
->get_nspace() << "\t" << i
->get_oid() << "\t" << i
->get_locator() << std::endl
;
1224 if (i
->get_locator().size()) {
1225 io_ctx
.locator_set_key(i
->get_locator());
1227 io_ctx
.locator_set_key(string());
1229 io_ctx
.set_namespace(i
->get_nspace());
1231 io_ctx
.snap_set_read(LIBRADOS_SNAP_DIR
);
1232 r
= io_ctx
.list_snaps(i
->get_oid(), &ls
);
1234 cerr
<< "error listing snap shots " << i
->get_nspace() << "/" << i
->get_oid() << ": "
1235 << cpp_strerror(r
) << std::endl
;
1239 std::vector
<clone_info_t
>::iterator ci
= ls
.clones
.begin();
1241 if (ci
== ls
.clones
.end()) {
1242 io_ctx
.snap_set_read(CEPH_NOSNAP
);
1244 r
= do_cache_flush(io_ctx
, i
->get_oid());
1246 r
= do_cache_try_flush(io_ctx
, i
->get_oid());
1248 cerr
<< "failed to flush " << i
->get_nspace() << "/" << i
->get_oid() << ": "
1249 << cpp_strerror(r
) << std::endl
;
1253 r
= do_cache_evict(io_ctx
, i
->get_oid());
1255 cerr
<< "failed to evict " << i
->get_nspace() << "/" << i
->get_oid() << ": "
1256 << cpp_strerror(r
) << std::endl
;
1262 for (std::vector
<clone_info_t
>::iterator ci
= ls
.clones
.begin();
1263 ci
!= ls
.clones
.end(); ++ci
) {
1264 io_ctx
.snap_set_read(ci
->cloneid
);
1266 r
= do_cache_flush(io_ctx
, i
->get_oid());
1268 r
= do_cache_try_flush(io_ctx
, i
->get_oid());
1270 cerr
<< "failed to flush " << i
->get_nspace() << "/" << i
->get_oid() << ": "
1271 << cpp_strerror(r
) << std::endl
;
1275 r
= do_cache_evict(io_ctx
, i
->get_oid());
1277 cerr
<< "failed to evict " << i
->get_nspace() << "/" << i
->get_oid() << ": "
1278 << cpp_strerror(r
) << std::endl
;
1286 catch (const std::runtime_error
& e
) {
1287 cerr
<< e
.what() << std::endl
;
1290 return errors
? -1 : 0;
1293 static int do_get_inconsistent_pg_cmd(const std::vector
<const char*> &nargs
,
1295 Formatter
& formatter
)
1297 if (nargs
.size() < 2) {
1300 int64_t pool_id
= rados
.pool_lookup(nargs
[1]);
1302 cerr
<< "pool \"" << nargs
[1] << "\" not found" << std::endl
;
1303 return (int)pool_id
;
1305 std::vector
<PlacementGroup
> pgs
;
1306 int ret
= rados
.get_inconsistent_pgs(pool_id
, &pgs
);
1310 formatter
.open_array_section("pgs");
1311 for (auto& pg
: pgs
) {
1312 formatter
.dump_stream("pg") << pg
;
1314 formatter
.close_section();
1315 formatter
.flush(cout
);
1320 static void dump_errors(const err_t
&err
, Formatter
&f
, const char *name
)
1322 f
.open_array_section(name
);
1323 if (err
.has_shard_missing())
1324 f
.dump_string("error", "missing");
1325 if (err
.has_stat_error())
1326 f
.dump_string("error", "stat_error");
1327 if (err
.has_read_error())
1328 f
.dump_string("error", "read_error");
1329 if (err
.has_data_digest_mismatch_info())
1330 f
.dump_string("error", "data_digest_mismatch_info");
1331 if (err
.has_omap_digest_mismatch_info())
1332 f
.dump_string("error", "omap_digest_mismatch_info");
1333 if (err
.has_size_mismatch_info())
1334 f
.dump_string("error", "size_mismatch_info");
1335 if (err
.has_ec_hash_error())
1336 f
.dump_string("error", "ec_hash_error");
1337 if (err
.has_ec_size_error())
1338 f
.dump_string("error", "ec_size_error");
1339 if (err
.has_info_missing())
1340 f
.dump_string("error", "info_missing");
1341 if (err
.has_info_corrupted())
1342 f
.dump_string("error", "info_corrupted");
1343 if (err
.has_obj_size_info_mismatch())
1344 f
.dump_string("error", "obj_size_info_mismatch");
1345 if (err
.has_snapset_missing())
1346 f
.dump_string("error", "snapset_missing");
1347 if (err
.has_snapset_corrupted())
1348 f
.dump_string("error", "snapset_corrupted");
1349 if (err
.has_hinfo_missing())
1350 f
.dump_string("error", "hinfo_missing");
1351 if (err
.has_hinfo_corrupted())
1352 f
.dump_string("error", "hinfo_corrupted");
1356 static void dump_shard(const shard_info_t
& shard
,
1357 const inconsistent_obj_t
& inc
,
1360 dump_errors(shard
, f
, "errors");
1362 if (shard
.has_shard_missing())
1365 if (!shard
.has_stat_error())
1366 f
.dump_unsigned("size", shard
.size
);
1367 if (shard
.omap_digest_present
) {
1368 f
.dump_format("omap_digest", "0x%08x", shard
.omap_digest
);
1370 if (shard
.data_digest_present
) {
1371 f
.dump_format("data_digest", "0x%08x", shard
.data_digest
);
1374 if ((inc
.union_shards
.has_info_missing()
1375 || inc
.union_shards
.has_info_corrupted()
1376 || inc
.has_object_info_inconsistency()
1377 || shard
.has_obj_size_info_mismatch()) &&
1378 !shard
.has_info_missing()) {
1379 map
<std::string
, ceph::bufferlist
>::iterator k
= (const_cast<shard_info_t
&>(shard
)).attrs
.find(OI_ATTR
);
1380 assert(k
!= shard
.attrs
.end()); // Can't be missing
1381 if (!shard
.has_info_corrupted()) {
1384 bufferlist::iterator bliter
= k
->second
.begin();
1385 ::decode(oi
, bliter
); // Can't be corrupted
1386 f
.open_object_section("object_info");
1391 f
.dump_string("object_info", cleanbin(k
->second
, b64
));
1394 if ((inc
.union_shards
.has_snapset_missing()
1395 || inc
.union_shards
.has_snapset_corrupted()
1396 || inc
.has_snapset_inconsistency()) &&
1397 !shard
.has_snapset_missing()) {
1398 map
<std::string
, ceph::bufferlist
>::iterator k
= (const_cast<shard_info_t
&>(shard
)).attrs
.find(SS_ATTR
);
1399 assert(k
!= shard
.attrs
.end()); // Can't be missing
1400 if (!shard
.has_snapset_corrupted()) {
1403 bufferlist::iterator bliter
= k
->second
.begin();
1404 decode(ss
, bliter
); // Can't be corrupted
1405 f
.open_object_section("snapset");
1410 f
.dump_string("snapset", cleanbin(k
->second
, b64
));
1413 if ((inc
.union_shards
.has_hinfo_missing()
1414 || inc
.union_shards
.has_hinfo_corrupted()
1415 || inc
.has_hinfo_inconsistency()) &&
1416 !shard
.has_hinfo_missing()) {
1417 map
<std::string
, ceph::bufferlist
>::iterator k
= (const_cast<shard_info_t
&>(shard
)).attrs
.find(ECUtil::get_hinfo_key());
1418 assert(k
!= shard
.attrs
.end()); // Can't be missing
1419 if (!shard
.has_hinfo_corrupted()) {
1420 ECUtil::HashInfo hi
;
1422 bufferlist::iterator bliter
= k
->second
.begin();
1423 decode(hi
, bliter
); // Can't be corrupted
1424 f
.open_object_section("hashinfo");
1429 f
.dump_string("hashinfo", cleanbin(k
->second
, b64
));
1432 if (inc
.has_attr_name_mismatch() || inc
.has_attr_value_mismatch()) {
1433 f
.open_array_section("attrs");
1434 for (auto kv
: shard
.attrs
) {
1435 // System attribute handled above
1436 if (kv
.first
== OI_ATTR
|| kv
.first
[0] != '_')
1438 f
.open_object_section("attr");
1439 // Skip leading underscore since only giving user attrs
1440 f
.dump_string("name", kv
.first
.substr(1));
1442 f
.dump_string("value", cleanbin(kv
.second
, b64
));
1443 f
.dump_bool("Base64", b64
);
1450 static void dump_obj_errors(const obj_err_t
&err
, Formatter
&f
)
1452 f
.open_array_section("errors");
1453 if (err
.has_object_info_inconsistency())
1454 f
.dump_string("error", "object_info_inconsistency");
1455 if (err
.has_data_digest_mismatch())
1456 f
.dump_string("error", "data_digest_mismatch");
1457 if (err
.has_omap_digest_mismatch())
1458 f
.dump_string("error", "omap_digest_mismatch");
1459 if (err
.has_size_mismatch())
1460 f
.dump_string("error", "size_mismatch");
1461 if (err
.has_attr_value_mismatch())
1462 f
.dump_string("error", "attr_value_mismatch");
1463 if (err
.has_attr_name_mismatch())
1464 f
.dump_string("error", "attr_name_mismatch");
1465 if (err
.has_snapset_inconsistency())
1466 f
.dump_string("error", "snapset_inconsistency");
1467 if (err
.has_hinfo_inconsistency())
1468 f
.dump_string("error", "hinfo_inconsistency");
1472 static void dump_object_id(const object_id_t
& object
,
1475 f
.dump_string("name", object
.name
);
1476 f
.dump_string("nspace", object
.nspace
);
1477 f
.dump_string("locator", object
.locator
);
1478 switch (object
.snap
) {
1480 f
.dump_string("snap", "head");
1483 f
.dump_string("snap", "snapdir");
1486 f
.dump_unsigned("snap", object
.snap
);
1491 static void dump_inconsistent(const inconsistent_obj_t
& inc
,
1494 f
.open_object_section("object");
1495 dump_object_id(inc
.object
, f
);
1496 f
.dump_unsigned("version", inc
.version
);
1499 dump_obj_errors(inc
, f
);
1500 dump_errors(inc
.union_shards
, f
, "union_shard_errors");
1501 for (const auto& shard_info
: inc
.shards
) {
1502 shard_info_t shard
= const_cast<shard_info_t
&>(shard_info
.second
);
1503 if (shard
.selected_oi
) {
1506 auto k
= shard
.attrs
.find(OI_ATTR
);
1507 assert(k
!= shard
.attrs
.end()); // Can't be missing
1508 bufferlist::iterator bliter
= k
->second
.begin();
1509 ::decode(oi
, bliter
); // Can't be corrupted
1510 f
.open_object_section("selected_object_info");
1516 f
.open_array_section("shards");
1517 for (const auto& shard_info
: inc
.shards
) {
1518 f
.open_object_section("shard");
1519 auto& osd_shard
= shard_info
.first
;
1520 f
.dump_int("osd", osd_shard
.osd
);
1521 f
.dump_bool("primary", shard_info
.second
.primary
);
1522 auto shard
= osd_shard
.shard
;
1523 if (shard
!= shard_id_t::NO_SHARD
)
1524 f
.dump_unsigned("shard", shard
);
1525 dump_shard(shard_info
.second
, inc
, f
);
1531 static void dump_inconsistent(const inconsistent_snapset_t
& inc
,
1534 dump_object_id(inc
.object
, f
);
1536 if (inc
.ss_bl
.length()) {
1538 bufferlist bl
= inc
.ss_bl
;
1539 bufferlist::iterator bliter
= bl
.begin();
1540 decode(ss
, bliter
); // Can't be corrupted
1541 f
.open_object_section("snapset");
1545 f
.open_array_section("errors");
1546 if (inc
.snapset_missing())
1547 f
.dump_string("error", "snapset_missing");
1548 if (inc
.snapset_corrupted())
1549 f
.dump_string("error", "snapset_corrupted");
1550 if (inc
.info_missing())
1551 f
.dump_string("error", "info_missing");
1552 if (inc
.info_corrupted())
1553 f
.dump_string("error", "info_corrupted");
1554 if (inc
.snapset_error())
1555 f
.dump_string("error", "snapset_error");
1556 if (inc
.head_mismatch())
1557 f
.dump_string("error", "head_mismatch");
1559 f
.dump_string("error", "headless");
1560 if (inc
.size_mismatch())
1561 f
.dump_string("error", "size_mismatch");
1562 if (inc
.extra_clones())
1563 f
.dump_string("error", "extra_clones");
1564 if (inc
.clone_missing())
1565 f
.dump_string("error", "clone_missing");
1568 if (inc
.extra_clones()) {
1569 f
.open_array_section("extra clones");
1570 for (auto snap
: inc
.clones
) {
1571 f
.dump_unsigned("snap", snap
);
1576 if (inc
.clone_missing()) {
1577 f
.open_array_section("missing");
1578 for (auto snap
: inc
.missing
) {
1579 f
.dump_unsigned("snap", snap
);
1585 // dispatch the call by type
1586 static int do_get_inconsistent(Rados
& rados
,
1587 const PlacementGroup
& pg
,
1588 const librados::object_id_t
&start
,
1589 unsigned max_return
,
1591 std::vector
<inconsistent_obj_t
>* objs
,
1594 return rados
.get_inconsistent_objects(pg
, start
, max_return
, c
,
1598 static int do_get_inconsistent(Rados
& rados
,
1599 const PlacementGroup
& pg
,
1600 const librados::object_id_t
&start
,
1601 unsigned max_return
,
1603 std::vector
<inconsistent_snapset_t
>* snapsets
,
1606 return rados
.get_inconsistent_snapsets(pg
, start
, max_return
, c
,
1607 snapsets
, interval
);
1610 template <typename T
>
1611 static int do_get_inconsistent_cmd(const std::vector
<const char*> &nargs
,
1613 Formatter
& formatter
)
1615 if (nargs
.size() < 2) {
1620 ret
= pg
.parse(nargs
[1]);
1622 cerr
<< "bad pg: " << nargs
[1] << std::endl
;
1625 uint32_t interval
= 0, first_interval
= 0;
1626 const unsigned max_item_num
= 32;
1627 bool opened
= false;
1628 for (librados::object_id_t start
;;) {
1629 std::vector
<T
> items
;
1630 auto completion
= librados::Rados::aio_create_completion();
1631 ret
= do_get_inconsistent(rados
, pg
, start
, max_item_num
, completion
,
1633 completion
->wait_for_safe();
1634 ret
= completion
->get_return_value();
1635 completion
->release();
1638 cerr
<< "interval#" << interval
<< " expired." << std::endl
;
1639 else if (ret
== -ENOENT
)
1640 cerr
<< "No scrub information available for pg " << pg
<< std::endl
;
1642 cerr
<< "Unknown error " << cpp_strerror(ret
) << std::endl
;
1645 // It must be the same interval every time. EAGAIN would
1646 // occur if interval changes.
1647 assert(start
.name
.empty() || first_interval
== interval
);
1648 if (start
.name
.empty()) {
1649 first_interval
= interval
;
1650 formatter
.open_object_section("info");
1651 formatter
.dump_int("epoch", interval
);
1652 formatter
.open_array_section("inconsistents");
1655 for (auto& inc
: items
) {
1656 formatter
.open_object_section("inconsistent");
1657 dump_inconsistent(inc
, formatter
);
1658 formatter
.close_section();
1660 if (items
.size() < max_item_num
) {
1661 formatter
.close_section();
1664 if (!items
.empty()) {
1665 start
= items
.back().object
;
1670 formatter
.close_section();
1671 formatter
.flush(cout
);
1676 /**********************************************
1678 **********************************************/
1679 static int rados_tool_common(const std::map
< std::string
, std::string
> &opts
,
1680 std::vector
<const char*> &nargs
)
1683 bool create_pool
= false;
1684 const char *pool_name
= NULL
;
1685 const char *target_pool_name
= NULL
;
1686 string oloc
, target_oloc
, nspace
, target_nspace
;
1687 int concurrent_ios
= 16;
1688 unsigned op_size
= default_op_size
;
1689 unsigned object_size
= 0;
1690 unsigned max_objects
= 0;
1691 uint64_t obj_offset
= 0;
1692 bool block_size_specified
= false;
1693 int bench_write_dest
= 0;
1694 bool cleanup
= true;
1695 bool hints
= true; // for rados bench
1696 bool no_verify
= false;
1697 bool use_striper
= false;
1698 bool with_clones
= false;
1699 const char *snapname
= NULL
;
1700 snap_t snapid
= CEPH_NOSNAP
;
1701 std::map
<std::string
, std::string
>::const_iterator i
;
1703 uint64_t min_obj_len
= 0;
1704 uint64_t max_obj_len
= 0;
1705 uint64_t min_op_len
= 0;
1706 uint64_t max_op_len
= 0;
1707 uint64_t max_ops
= 0;
1708 uint64_t max_backlog
= 0;
1709 uint64_t target_throughput
= 0;
1710 int64_t read_percent
= -1;
1711 uint64_t num_objs
= 0;
1714 bool show_time
= false;
1715 bool wildcard
= false;
1717 std::string run_name
;
1719 bool forcefull
= false;
1720 Formatter
*formatter
= NULL
;
1721 bool pretty_format
= false;
1722 const char *output
= NULL
;
1723 bool omap_key_valid
= false;
1724 std::string omap_key
;
1725 std::string omap_key_pretty
;
1729 RadosStriper striper
;
1731 i
= opts
.find("create");
1732 if (i
!= opts
.end()) {
1735 i
= opts
.find("pool");
1736 if (i
!= opts
.end()) {
1737 pool_name
= i
->second
.c_str();
1739 i
= opts
.find("target_pool");
1740 if (i
!= opts
.end()) {
1741 target_pool_name
= i
->second
.c_str();
1743 i
= opts
.find("object_locator");
1744 if (i
!= opts
.end()) {
1747 i
= opts
.find("target_locator");
1748 if (i
!= opts
.end()) {
1749 target_oloc
= i
->second
;
1751 i
= opts
.find("target_nspace");
1752 if (i
!= opts
.end()) {
1753 target_nspace
= i
->second
;
1755 i
= opts
.find("concurrent-ios");
1756 if (i
!= opts
.end()) {
1757 if (rados_sistrtoll(i
, &concurrent_ios
)) {
1761 i
= opts
.find("run-name");
1762 if (i
!= opts
.end()) {
1763 run_name
= i
->second
;
1766 i
= opts
.find("force-full");
1767 if (i
!= opts
.end()) {
1770 i
= opts
.find("prefix");
1771 if (i
!= opts
.end()) {
1774 i
= opts
.find("block-size");
1775 if (i
!= opts
.end()) {
1776 if (rados_sistrtoll(i
, &op_size
)) {
1779 block_size_specified
= true;
1781 i
= opts
.find("object-size");
1782 if (i
!= opts
.end()) {
1783 if (rados_sistrtoll(i
, &object_size
)) {
1786 block_size_specified
= true;
1788 i
= opts
.find("max-objects");
1789 if (i
!= opts
.end()) {
1790 if (rados_sistrtoll(i
, &max_objects
)) {
1794 i
= opts
.find("offset");
1795 if (i
!= opts
.end()) {
1796 if (rados_sistrtoll(i
, &obj_offset
)) {
1800 i
= opts
.find("snap");
1801 if (i
!= opts
.end()) {
1802 snapname
= i
->second
.c_str();
1804 i
= opts
.find("snapid");
1805 if (i
!= opts
.end()) {
1806 if (rados_sistrtoll(i
, &snapid
)) {
1810 i
= opts
.find("min-object-size");
1811 if (i
!= opts
.end()) {
1812 if (rados_sistrtoll(i
, &min_obj_len
)) {
1816 i
= opts
.find("max-object-size");
1817 if (i
!= opts
.end()) {
1818 if (rados_sistrtoll(i
, &max_obj_len
)) {
1822 i
= opts
.find("min-op-len");
1823 if (i
!= opts
.end()) {
1824 if (rados_sistrtoll(i
, &min_op_len
)) {
1828 i
= opts
.find("max-op-len");
1829 if (i
!= opts
.end()) {
1830 if (rados_sistrtoll(i
, &max_op_len
)) {
1834 i
= opts
.find("max-ops");
1835 if (i
!= opts
.end()) {
1836 if (rados_sistrtoll(i
, &max_ops
)) {
1840 i
= opts
.find("max-backlog");
1841 if (i
!= opts
.end()) {
1842 if (rados_sistrtoll(i
, &max_backlog
)) {
1846 i
= opts
.find("target-throughput");
1847 if (i
!= opts
.end()) {
1848 if (rados_sistrtoll(i
, &target_throughput
)) {
1852 i
= opts
.find("read-percent");
1853 if (i
!= opts
.end()) {
1854 if (rados_sistrtoll(i
, &read_percent
)) {
1858 i
= opts
.find("num-objects");
1859 if (i
!= opts
.end()) {
1860 if (rados_sistrtoll(i
, &num_objs
)) {
1864 i
= opts
.find("run-length");
1865 if (i
!= opts
.end()) {
1866 if (rados_sistrtoll(i
, &run_length
)) {
1870 i
= opts
.find("show-time");
1871 if (i
!= opts
.end()) {
1874 i
= opts
.find("no-cleanup");
1875 if (i
!= opts
.end()) {
1878 i
= opts
.find("no-hints");
1879 if (i
!= opts
.end()) {
1882 i
= opts
.find("pretty-format");
1883 if (i
!= opts
.end()) {
1884 pretty_format
= true;
1886 i
= opts
.find("format");
1887 if (i
!= opts
.end()) {
1888 const char *format
= i
->second
.c_str();
1889 formatter
= Formatter::create(format
);
1891 cerr
<< "unrecognized format: " << format
<< std::endl
;
1895 i
= opts
.find("namespace");
1896 if (i
!= opts
.end()) {
1899 i
= opts
.find("no-verify");
1900 if (i
!= opts
.end()) {
1903 i
= opts
.find("output");
1904 if (i
!= opts
.end()) {
1905 output
= i
->second
.c_str();
1907 i
= opts
.find("write-dest-obj");
1908 if (i
!= opts
.end()) {
1909 bench_write_dest
|= static_cast<int>(OP_WRITE_DEST_OBJ
);
1911 i
= opts
.find("write-dest-omap");
1912 if (i
!= opts
.end()) {
1913 bench_write_dest
|= static_cast<int>(OP_WRITE_DEST_OMAP
);
1915 i
= opts
.find("write-dest-xattr");
1916 if (i
!= opts
.end()) {
1917 bench_write_dest
|= static_cast<int>(OP_WRITE_DEST_XATTR
);
1919 i
= opts
.find("with-clones");
1920 if (i
!= opts
.end()) {
1923 i
= opts
.find("omap-key-file");
1924 if (i
!= opts
.end()) {
1927 ret
= indata
.read_file(i
->second
.c_str(), &err
);
1929 cerr
<< err
<< std::endl
;
1933 omap_key_valid
= true;
1934 omap_key
= std::string(indata
.c_str(), indata
.length());
1935 omap_key_pretty
= omap_key
;
1936 if (std::find_if_not(omap_key
.begin(), omap_key
.end(),
1937 (int (*)(int))isprint
) != omap_key
.end()) {
1938 omap_key_pretty
= "(binary key)";
1943 ret
= rados
.init_with_context(g_ceph_context
);
1945 cerr
<< "couldn't initialize rados: " << cpp_strerror(ret
) << std::endl
;
1949 ret
= rados
.connect();
1951 cerr
<< "couldn't connect to cluster: " << cpp_strerror(ret
) << std::endl
;
1956 if (create_pool
&& !pool_name
) {
1957 cerr
<< "--create-pool requested but pool_name was not specified!" << std::endl
;
1962 ret
= rados
.pool_create(pool_name
, 0, 0);
1964 cerr
<< "error creating pool " << pool_name
<< ": "
1965 << cpp_strerror(ret
) << std::endl
;
1972 ret
= rados
.ioctx_create(pool_name
, io_ctx
);
1974 cerr
<< "error opening pool " << pool_name
<< ": "
1975 << cpp_strerror(ret
) << std::endl
;
1982 ret
= io_ctx
.pool_requires_alignment2(&requires
);
1984 cerr
<< "error checking pool alignment requirement"
1985 << cpp_strerror(ret
) << std::endl
;
1991 ret
= io_ctx
.pool_required_alignment2(&align
);
1993 cerr
<< "error getting pool alignment"
1994 << cpp_strerror(ret
) << std::endl
;
1998 const uint64_t prev_op_size
= op_size
;
1999 op_size
= uint64_t((op_size
+ align
- 1) / align
) * align
;
2000 // Warn: if user specified and it was rounded
2001 if (prev_op_size
!= default_op_size
&& prev_op_size
!= op_size
)
2002 cerr
<< "INFO: op_size has been rounded to " << op_size
<< std::endl
;
2006 // create striper interface
2007 if (opts
.find("striper") != opts
.end()) {
2008 ret
= RadosStriper::striper_create(io_ctx
, &striper
);
2010 cerr
<< "error opening pool " << pool_name
<< " with striper interface: "
2011 << cpp_strerror(ret
) << std::endl
;
2021 cerr
<< "pool name must be specified with --snap" << std::endl
;
2025 ret
= io_ctx
.snap_lookup(snapname
, &snapid
);
2027 cerr
<< "error looking up snap '" << snapname
<< "': " << cpp_strerror(ret
) << std::endl
;
2033 cerr
<< "pool name must be specified with --object_locator" << std::endl
;
2037 io_ctx
.locator_set_key(oloc
);
2039 // Use namespace from command line if specified
2040 if (opts
.find("namespace") != opts
.end()) {
2042 cerr
<< "pool name must be specified with --namespace" << std::endl
;
2046 io_ctx
.set_namespace(nspace
);
2047 // Use wildcard if --all specified and --default NOT specified
2048 } else if (opts
.find("all") != opts
.end() && opts
.find("default") == opts
.end()) {
2049 // Only the ls should ever set namespace to special value
2052 if (snapid
!= CEPH_NOSNAP
) {
2054 cerr
<< "pool name must be specified with --snapid" << std::endl
;
2059 ret
= io_ctx
.snap_get_name(snapid
, &name
);
2061 cerr
<< "snapid " << snapid
<< " doesn't exist in pool "
2062 << io_ctx
.get_pool_name() << std::endl
;
2065 io_ctx
.snap_set_read(snapid
);
2066 cout
<< "selected snap " << snapid
<< " '" << name
<< "'" << std::endl
;
2069 assert(!nargs
.empty());
2072 if (strcmp(nargs
[0], "lspools") == 0) {
2074 ret
= rados
.pool_list(vec
);
2076 cerr
<< "error listing pools: " << cpp_strerror(ret
) << std::endl
;
2079 for (list
<string
>::iterator i
= vec
.begin(); i
!= vec
.end(); ++i
)
2080 cout
<< *i
<< std::endl
;
2082 else if (strcmp(nargs
[0], "df") == 0) {
2087 ret
= rados
.pool_list(vec
);
2089 cerr
<< "error listing pools: " << cpp_strerror(ret
) << std::endl
;
2093 vec
.push_back(pool_name
);
2096 map
<string
,librados::pool_stat_t
> stats
;
2097 ret
= rados
.get_pool_stats(vec
, stats
);
2099 cerr
<< "error fetching pool stats: " << cpp_strerror(ret
) << std::endl
;
2106 tab
.define_column("POOL_NAME", TextTable::LEFT
, TextTable::LEFT
);
2107 tab
.define_column("USED", TextTable::LEFT
, TextTable::RIGHT
);
2108 tab
.define_column("OBJECTS", TextTable::LEFT
, TextTable::RIGHT
);
2109 tab
.define_column("CLONES", TextTable::LEFT
, TextTable::RIGHT
);
2110 tab
.define_column("COPIES", TextTable::LEFT
, TextTable::RIGHT
);
2111 tab
.define_column("MISSING_ON_PRIMARY", TextTable::LEFT
, TextTable::RIGHT
);
2112 tab
.define_column("UNFOUND", TextTable::LEFT
, TextTable::RIGHT
);
2113 tab
.define_column("DEGRADED", TextTable::LEFT
, TextTable::RIGHT
);
2114 tab
.define_column("RD_OPS", TextTable::LEFT
, TextTable::RIGHT
);
2115 tab
.define_column("RD", TextTable::LEFT
, TextTable::RIGHT
);
2116 tab
.define_column("WR_OPS", TextTable::LEFT
, TextTable::RIGHT
);
2117 tab
.define_column("WR", TextTable::LEFT
, TextTable::RIGHT
);
2119 formatter
->open_object_section("stats");
2120 formatter
->open_array_section("pools");
2122 for (map
<string
,librados::pool_stat_t
>::iterator i
= stats
.begin();
2125 const char *pool_name
= i
->first
.c_str();
2126 librados::pool_stat_t
& s
= i
->second
;
2129 << si_t(s
.num_bytes
)
2131 << s
.num_object_clones
2132 << s
.num_object_copies
2133 << s
.num_objects_missing_on_primary
2134 << s
.num_objects_unfound
2135 << s
.num_objects_degraded
2137 << si_t(s
.num_rd_kb
<< 10)
2139 << si_t(s
.num_wr_kb
<< 10)
2140 << TextTable::endrow
;
2142 formatter
->open_object_section("pool");
2143 int64_t pool_id
= rados
.pool_lookup(pool_name
);
2144 formatter
->dump_string("name", pool_name
);
2146 formatter
->dump_int("id", pool_id
);
2148 cerr
<< "ERROR: lookup_pg_pool_name for name=" << pool_name
2149 << " returned " << pool_id
<< std::endl
;
2150 formatter
->dump_int("size_bytes",s
.num_bytes
);
2151 formatter
->dump_int("size_kb", s
.num_kb
);
2152 formatter
->dump_int("num_objects", s
.num_objects
);
2153 formatter
->dump_int("num_object_clones", s
.num_object_clones
);
2154 formatter
->dump_int("num_object_copies", s
.num_object_copies
);
2155 formatter
->dump_int("num_objects_missing_on_primary", s
.num_objects_missing_on_primary
);
2156 formatter
->dump_int("num_objects_unfound", s
.num_objects_unfound
);
2157 formatter
->dump_int("num_objects_degraded", s
.num_objects_degraded
);
2158 formatter
->dump_int("read_ops", s
.num_rd
);
2159 formatter
->dump_int("read_bytes", s
.num_rd_kb
* 1024ull);
2160 formatter
->dump_int("write_ops", s
.num_wr
);
2161 formatter
->dump_int("write_bytes", s
.num_wr_kb
* 1024ull);
2162 formatter
->close_section();
2171 cluster_stat_t tstats
;
2172 ret
= rados
.cluster_stat(tstats
);
2174 cerr
<< "error getting total cluster usage: " << cpp_strerror(ret
) << std::endl
;
2179 cout
<< "total_objects " << tstats
.num_objects
2181 cout
<< "total_used " << si_t(tstats
.kb_used
<< 10)
2183 cout
<< "total_avail " << si_t(tstats
.kb_avail
<< 10)
2185 cout
<< "total_space " << si_t(tstats
.kb
<< 10)
2188 formatter
->close_section();
2189 formatter
->dump_int("total_objects", tstats
.num_objects
);
2190 formatter
->dump_int("total_used", tstats
.kb_used
);
2191 formatter
->dump_int("total_avail", tstats
.kb_avail
);
2192 formatter
->dump_int("total_space", tstats
.kb
);
2193 formatter
->close_section();
2194 formatter
->flush(cout
);
2198 else if (strcmp(nargs
[0], "ls") == 0) {
2200 cerr
<< "pool name was not specified" << std::endl
;
2206 io_ctx
.set_namespace(all_nspaces
);
2207 bool use_stdout
= (nargs
.size() < 2) || (strcmp(nargs
[1], "-") == 0);
2212 outstream
= new ofstream(nargs
[1]);
2216 formatter
->open_array_section("objects");
2218 librados::NObjectIterator i
= io_ctx
.nobjects_begin();
2219 librados::NObjectIterator i_end
= io_ctx
.nobjects_end();
2220 for (; i
!= i_end
; ++i
) {
2222 // in case of --striper option, we only list striped
2223 // objects, so we only display the first object of
2224 // each, without its suffix '.000...000'
2225 size_t l
= i
->get_oid().length();
2227 (0 != i
->get_oid().compare(l
-17, 17,".0000000000000000"))) continue;
2230 // Only include namespace in output when wildcard specified
2232 *outstream
<< i
->get_nspace() << "\t";
2234 *outstream
<< i
->get_oid().substr(0, i
->get_oid().length()-17);
2236 *outstream
<< i
->get_oid();
2238 if (i
->get_locator().size())
2239 *outstream
<< "\t" << i
->get_locator();
2240 *outstream
<< std::endl
;
2242 formatter
->open_object_section("object");
2243 formatter
->dump_string("namespace", i
->get_nspace());
2245 formatter
->dump_string("name", i
->get_oid().substr(0, i
->get_oid().length()-17));
2247 formatter
->dump_string("name", i
->get_oid());
2249 if (i
->get_locator().size())
2250 formatter
->dump_string("locator", i
->get_locator());
2251 formatter
->close_section(); //object
2255 catch (const std::runtime_error
& e
) {
2256 cerr
<< e
.what() << std::endl
;
2262 formatter
->close_section(); //objects
2263 formatter
->flush(*outstream
);
2265 *outstream
<< std::endl
;
2266 formatter
->flush(*outstream
);
2271 else if (strcmp(nargs
[0], "chown") == 0) {
2272 if (!pool_name
|| nargs
.size() < 2)
2275 char* endptr
= NULL
;
2276 uint64_t new_auid
= strtol(nargs
[1], &endptr
, 10);
2278 cerr
<< "Invalid value for new-auid: '" << nargs
[1] << "'" << std::endl
;
2282 ret
= io_ctx
.set_auid(new_auid
);
2284 cerr
<< "error changing auid on pool " << io_ctx
.get_pool_name() << ':'
2285 << cpp_strerror(ret
) << std::endl
;
2286 } else cerr
<< "changed auid on pool " << io_ctx
.get_pool_name()
2287 << " to " << new_auid
<< std::endl
;
2289 else if (strcmp(nargs
[0], "mapext") == 0) {
2290 if (!pool_name
|| nargs
.size() < 2)
2292 string
oid(nargs
[1]);
2293 std::map
<uint64_t,uint64_t> m
;
2294 ret
= io_ctx
.mapext(oid
, 0, -1, m
);
2296 cerr
<< "mapext error on " << pool_name
<< "/" << oid
<< ": " << cpp_strerror(ret
) << std::endl
;
2299 std::map
<uint64_t,uint64_t>::iterator iter
;
2300 for (iter
= m
.begin(); iter
!= m
.end(); ++iter
) {
2301 cout
<< hex
<< iter
->first
<< "\t" << iter
->second
<< dec
<< std::endl
;
2304 else if (strcmp(nargs
[0], "stat") == 0) {
2305 if (!pool_name
|| nargs
.size() < 2)
2307 string
oid(nargs
[1]);
2311 ret
= striper
.stat(oid
, &size
, &mtime
);
2313 ret
= io_ctx
.stat(oid
, &size
, &mtime
);
2316 cerr
<< " error stat-ing " << pool_name
<< "/" << oid
<< ": "
2317 << cpp_strerror(ret
) << std::endl
;
2320 utime_t
t(mtime
, 0);
2321 cout
<< pool_name
<< "/" << oid
2322 << " mtime " << t
<< ", size " << size
<< std::endl
;
2325 else if (strcmp(nargs
[0], "get") == 0) {
2326 if (!pool_name
|| nargs
.size() < 3)
2328 ret
= do_get(io_ctx
, striper
, nargs
[1], nargs
[2], op_size
, use_striper
);
2330 cerr
<< "error getting " << pool_name
<< "/" << nargs
[1] << ": " << cpp_strerror(ret
) << std::endl
;
2334 else if (strcmp(nargs
[0], "put") == 0) {
2335 if (!pool_name
|| nargs
.size() < 3)
2337 ret
= do_put(io_ctx
, striper
, nargs
[1], nargs
[2], op_size
, obj_offset
, use_striper
);
2339 cerr
<< "error putting " << pool_name
<< "/" << nargs
[1] << ": " << cpp_strerror(ret
) << std::endl
;
2343 else if (strcmp(nargs
[0], "append") == 0) {
2344 if (!pool_name
|| nargs
.size() < 3)
2346 ret
= do_append(io_ctx
, striper
, nargs
[1], nargs
[2], op_size
, use_striper
);
2348 cerr
<< "error appending " << pool_name
<< "/" << nargs
[1] << ": " << cpp_strerror(ret
) << std::endl
;
2352 else if (strcmp(nargs
[0], "truncate") == 0) {
2353 if (!pool_name
|| nargs
.size() < 3)
2356 string
oid(nargs
[1]);
2357 char* endptr
= NULL
;
2358 long size
= strtoll(nargs
[2], &endptr
, 10);
2360 cerr
<< "Invalid value for size: '" << nargs
[2] << "'" << std::endl
;
2365 cerr
<< "error, cannot truncate to negative value" << std::endl
;
2369 ret
= striper
.trunc(oid
, size
);
2371 ret
= io_ctx
.trunc(oid
, size
);
2374 cerr
<< "error truncating oid "
2375 << oid
<< " to " << size
<< ": "
2376 << cpp_strerror(ret
) << std::endl
;
2381 else if (strcmp(nargs
[0], "setxattr") == 0) {
2382 if (!pool_name
|| nargs
.size() < 3 || nargs
.size() > 4)
2385 string
oid(nargs
[1]);
2386 string
attr_name(nargs
[2]);
2388 if (nargs
.size() == 4) {
2389 string
attr_val(nargs
[3]);
2390 bl
.append(attr_val
.c_str(), attr_val
.length());
2393 ret
= bl
.read_fd(STDIN_FILENO
, 1024); // from stdin
2400 ret
= striper
.setxattr(oid
, attr_name
.c_str(), bl
);
2402 ret
= io_ctx
.setxattr(oid
, attr_name
.c_str(), bl
);
2405 cerr
<< "error setting xattr " << pool_name
<< "/" << oid
<< "/" << attr_name
<< ": " << cpp_strerror(ret
) << std::endl
;
2411 else if (strcmp(nargs
[0], "getxattr") == 0) {
2412 if (!pool_name
|| nargs
.size() < 3)
2415 string
oid(nargs
[1]);
2416 string
attr_name(nargs
[2]);
2420 ret
= striper
.getxattr(oid
, attr_name
.c_str(), bl
);
2422 ret
= io_ctx
.getxattr(oid
, attr_name
.c_str(), bl
);
2425 cerr
<< "error getting xattr " << pool_name
<< "/" << oid
<< "/" << attr_name
<< ": " << cpp_strerror(ret
) << std::endl
;
2430 string
s(bl
.c_str(), bl
.length());
2432 } else if (strcmp(nargs
[0], "rmxattr") == 0) {
2433 if (!pool_name
|| nargs
.size() < 3)
2436 string
oid(nargs
[1]);
2437 string
attr_name(nargs
[2]);
2440 ret
= striper
.rmxattr(oid
, attr_name
.c_str());
2442 ret
= io_ctx
.rmxattr(oid
, attr_name
.c_str());
2445 cerr
<< "error removing xattr " << pool_name
<< "/" << oid
<< "/" << attr_name
<< ": " << cpp_strerror(ret
) << std::endl
;
2448 } else if (strcmp(nargs
[0], "listxattr") == 0) {
2449 if (!pool_name
|| nargs
.size() < 2)
2452 string
oid(nargs
[1]);
2453 map
<std::string
, bufferlist
> attrset
;
2456 ret
= striper
.getxattrs(oid
, attrset
);
2458 ret
= io_ctx
.getxattrs(oid
, attrset
);
2461 cerr
<< "error getting xattr set " << pool_name
<< "/" << oid
<< ": " << cpp_strerror(ret
) << std::endl
;
2465 for (map
<std::string
, bufferlist
>::iterator iter
= attrset
.begin();
2466 iter
!= attrset
.end(); ++iter
) {
2467 cout
<< iter
->first
<< std::endl
;
2469 } else if (strcmp(nargs
[0], "getomapheader") == 0) {
2470 if (!pool_name
|| nargs
.size() < 2)
2473 string
oid(nargs
[1]);
2475 if (nargs
.size() >= 3) {
2480 ret
= io_ctx
.omap_get_header(oid
, &header
);
2482 cerr
<< "error getting omap header " << pool_name
<< "/" << oid
2483 << ": " << cpp_strerror(ret
) << std::endl
;
2486 if (!outfile
.empty()) {
2487 cerr
<< "Writing to " << outfile
<< std::endl
;
2488 dump_data(outfile
, header
);
2490 cout
<< "header (" << header
.length() << " bytes) :\n";
2491 header
.hexdump(cout
);
2496 } else if (strcmp(nargs
[0], "setomapheader") == 0) {
2497 if (!pool_name
|| nargs
.size() < 3)
2500 string
oid(nargs
[1]);
2501 string
val(nargs
[2]);
2506 ret
= io_ctx
.omap_set_header(oid
, bl
);
2508 cerr
<< "error setting omap value " << pool_name
<< "/" << oid
2509 << ": " << cpp_strerror(ret
) << std::endl
;
2514 } else if (strcmp(nargs
[0], "setomapval") == 0) {
2515 uint32_t min_args
= (omap_key_valid
? 2 : 3);
2516 if (!pool_name
|| nargs
.size() < min_args
|| nargs
.size() > min_args
+ 1) {
2520 string
oid(nargs
[1]);
2521 if (!omap_key_valid
) {
2522 omap_key
= nargs
[2];
2523 omap_key_pretty
= omap_key
;
2527 if (nargs
.size() > min_args
) {
2528 string
val(nargs
[min_args
]);
2532 ret
= bl
.read_fd(STDIN_FILENO
, 1024); // from stdin
2539 map
<string
, bufferlist
> values
;
2540 values
[omap_key
] = bl
;
2542 ret
= io_ctx
.omap_set(oid
, values
);
2544 cerr
<< "error setting omap value " << pool_name
<< "/" << oid
<< "/"
2545 << omap_key_pretty
<< ": " << cpp_strerror(ret
) << std::endl
;
2550 } else if (strcmp(nargs
[0], "getomapval") == 0) {
2551 uint32_t min_args
= (omap_key_valid
? 2 : 3);
2552 if (!pool_name
|| nargs
.size() < min_args
|| nargs
.size() > min_args
+ 1) {
2556 string
oid(nargs
[1]);
2557 if (!omap_key_valid
) {
2558 omap_key
= nargs
[2];
2559 omap_key_pretty
= omap_key
;
2563 keys
.insert(omap_key
);
2565 std::string outfile
;
2566 if (nargs
.size() > min_args
) {
2567 outfile
= nargs
[min_args
];
2570 map
<string
, bufferlist
> values
;
2571 ret
= io_ctx
.omap_get_vals_by_keys(oid
, keys
, &values
);
2573 cerr
<< "error getting omap value " << pool_name
<< "/" << oid
<< "/"
2574 << omap_key_pretty
<< ": " << cpp_strerror(ret
) << std::endl
;
2580 if (values
.size() && values
.begin()->first
== omap_key
) {
2581 if (!outfile
.empty()) {
2582 cerr
<< "Writing to " << outfile
<< std::endl
;
2583 dump_data(outfile
, values
.begin()->second
);
2585 cout
<< "value (" << values
.begin()->second
.length() << " bytes) :\n";
2586 values
.begin()->second
.hexdump(cout
);
2591 cout
<< "No such key: " << pool_name
<< "/" << oid
<< "/"
2592 << omap_key_pretty
<< std::endl
;
2596 } else if (strcmp(nargs
[0], "rmomapkey") == 0) {
2597 uint32_t num_args
= (omap_key_valid
? 2 : 3);
2598 if (!pool_name
|| nargs
.size() != num_args
) {
2602 string
oid(nargs
[1]);
2603 if (!omap_key_valid
) {
2604 omap_key
= nargs
[2];
2605 omap_key_pretty
= omap_key
;
2608 keys
.insert(omap_key
);
2610 ret
= io_ctx
.omap_rm_keys(oid
, keys
);
2612 cerr
<< "error removing omap key " << pool_name
<< "/" << oid
<< "/"
2613 << omap_key_pretty
<< ": " << cpp_strerror(ret
) << std::endl
;
2618 } else if (strcmp(nargs
[0], "listomapvals") == 0) {
2619 if (!pool_name
|| nargs
.size() < 2)
2622 string
oid(nargs
[1]);
2623 string last_read
= "";
2626 map
<string
, bufferlist
> values
;
2627 ret
= io_ctx
.omap_get_vals(oid
, last_read
, MAX_READ
, &values
);
2629 cerr
<< "error getting omap keys " << pool_name
<< "/" << oid
<< ": "
2630 << cpp_strerror(ret
) << std::endl
;
2633 ret
= values
.size();
2634 for (map
<string
, bufferlist
>::const_iterator it
= values
.begin();
2635 it
!= values
.end(); ++it
) {
2636 last_read
= it
->first
;
2637 // dump key in hex if it contains nonprintable characters
2638 if (std::count_if(it
->first
.begin(), it
->first
.end(),
2639 (int (*)(int))isprint
) < (int)it
->first
.length()) {
2640 cout
<< "key (" << it
->first
.length() << " bytes):\n";
2642 keybl
.append(it
->first
);
2643 keybl
.hexdump(cout
);
2648 cout
<< "value (" << it
->second
.length() << " bytes) :\n";
2649 it
->second
.hexdump(cout
);
2652 } while (ret
== MAX_READ
);
2655 else if (strcmp(nargs
[0], "cp") == 0) {
2659 if (nargs
.size() < 2 || nargs
.size() > 3)
2662 const char *target
= target_pool_name
;
2666 const char *target_obj
;
2667 if (nargs
.size() < 3) {
2668 if (strcmp(target
, pool_name
) == 0) {
2669 cerr
<< "cannot copy object into itself" << std::endl
;
2673 target_obj
= nargs
[1];
2675 target_obj
= nargs
[2];
2680 ret
= rados
.ioctx_create(target
, target_ctx
);
2682 cerr
<< "error opening target pool " << target
<< ": "
2683 << cpp_strerror(ret
) << std::endl
;
2686 if (target_oloc
.size()) {
2687 target_ctx
.locator_set_key(target_oloc
);
2689 if (target_nspace
.size()) {
2690 target_ctx
.set_namespace(target_nspace
);
2693 ret
= do_copy(io_ctx
, nargs
[1], target_ctx
, target_obj
);
2695 cerr
<< "error copying " << pool_name
<< "/" << nargs
[1] << " => " << target
<< "/" << target_obj
<< ": " << cpp_strerror(ret
) << std::endl
;
2698 } else if (strcmp(nargs
[0], "rm") == 0) {
2699 if (!pool_name
|| nargs
.size() < 2)
2701 vector
<const char *>::iterator iter
= nargs
.begin();
2703 for (; iter
!= nargs
.end(); ++iter
) {
2704 const string
& oid
= *iter
;
2707 ret
= striper
.remove(oid
, CEPH_OSD_FLAG_FULL_FORCE
);
2709 ret
= striper
.remove(oid
);
2713 ret
= io_ctx
.remove(oid
, CEPH_OSD_FLAG_FULL_FORCE
);
2715 ret
= io_ctx
.remove(oid
);
2719 string name
= (nspace
.size() ? nspace
+ "/" : "" ) + oid
;
2720 cerr
<< "error removing " << pool_name
<< ">" << name
<< ": " << cpp_strerror(ret
) << std::endl
;
2725 else if (strcmp(nargs
[0], "create") == 0) {
2726 if (!pool_name
|| nargs
.size() < 2)
2728 string
oid(nargs
[1]);
2729 ret
= io_ctx
.create(oid
, true);
2731 cerr
<< "error creating " << pool_name
<< "/" << oid
<< ": " << cpp_strerror(ret
) << std::endl
;
2736 else if (strcmp(nargs
[0], "tmap") == 0) {
2737 if (nargs
.size() < 3)
2739 if (strcmp(nargs
[1], "dump") == 0) {
2741 string
oid(nargs
[2]);
2742 ret
= io_ctx
.read(oid
, outdata
, 0, 0);
2744 cerr
<< "error reading " << pool_name
<< "/" << oid
<< ": " << cpp_strerror(ret
) << std::endl
;
2747 bufferlist::iterator p
= outdata
.begin();
2749 map
<string
, bufferlist
> kv
;
2751 ::decode(header
, p
);
2754 catch (buffer::error
& e
) {
2755 cerr
<< "error decoding tmap " << pool_name
<< "/" << oid
<< std::endl
;
2759 cout
<< "header (" << header
.length() << " bytes):\n";
2760 header
.hexdump(cout
);
2762 cout
<< kv
.size() << " keys\n";
2763 for (map
<string
,bufferlist
>::iterator q
= kv
.begin(); q
!= kv
.end(); ++q
) {
2764 cout
<< "key '" << q
->first
<< "' (" << q
->second
.length() << " bytes):\n";
2765 q
->second
.hexdump(cout
);
2769 else if (strcmp(nargs
[1], "set") == 0 ||
2770 strcmp(nargs
[1], "create") == 0) {
2771 if (nargs
.size() < 5)
2773 string
oid(nargs
[2]);
2777 char c
= (strcmp(nargs
[1], "set") == 0) ? CEPH_OSD_TMAP_SET
: CEPH_OSD_TMAP_CREATE
;
2781 ret
= io_ctx
.tmap_update(oid
, bl
);
2785 else if (strcmp(nargs
[0], "tmap-to-omap") == 0) {
2786 if (!pool_name
|| nargs
.size() < 2)
2788 string
oid(nargs
[1]);
2791 int r
= io_ctx
.tmap_get(oid
, bl
);
2794 cerr
<< "error reading tmap " << pool_name
<< "/" << oid
2795 << ": " << cpp_strerror(ret
) << std::endl
;
2799 map
<string
, bufferlist
> kv
;
2800 bufferlist::iterator p
= bl
.begin();
2805 catch (buffer::error
& e
) {
2806 cerr
<< "error decoding tmap " << pool_name
<< "/" << oid
<< std::endl
;
2811 cerr
<< "error decoding tmap (stray trailing data) in " << pool_name
<< "/" << oid
<< std::endl
;
2815 librados::ObjectWriteOperation wr
;
2816 wr
.omap_set_header(hdr
);
2818 wr
.truncate(0); // delete the old tmap data
2819 r
= io_ctx
.operate(oid
, &wr
);
2822 cerr
<< "error writing tmap data as omap on " << pool_name
<< "/" << oid
2823 << ": " << cpp_strerror(ret
) << std::endl
;
2829 else if (strcmp(nargs
[0], "mkpool") == 0) {
2831 __u8 crush_rule
= 0;
2832 if (nargs
.size() < 2)
2834 if (nargs
.size() > 2) {
2835 char* endptr
= NULL
;
2836 auid
= strtol(nargs
[2], &endptr
, 10);
2838 cerr
<< "Invalid value for auid: '" << nargs
[2] << "'" << std::endl
;
2842 cerr
<< "setting auid:" << auid
<< std::endl
;
2843 if (nargs
.size() > 3) {
2844 crush_rule
= (__u8
)strtol(nargs
[3], &endptr
, 10);
2846 cerr
<< "Invalid value for crush-rule: '" << nargs
[3] << "'" << std::endl
;
2850 cerr
<< "using crush rule " << (int)crush_rule
<< std::endl
;
2853 ret
= rados
.pool_create(nargs
[1], auid
, crush_rule
);
2855 cerr
<< "error creating pool " << nargs
[1] << ": "
2856 << cpp_strerror(ret
) << std::endl
;
2859 cout
<< "successfully created pool " << nargs
[1] << std::endl
;
2861 else if (strcmp(nargs
[0], "cppool") == 0) {
2862 bool force
= nargs
.size() == 4 && !strcmp(nargs
[3], "--yes-i-really-mean-it");
2863 if (nargs
.size() != 3 && !(nargs
.size() == 4 && force
))
2865 const char *src_pool
= nargs
[1];
2866 const char *target_pool
= nargs
[2];
2868 if (strcmp(src_pool
, target_pool
) == 0) {
2869 cerr
<< "cannot copy pool into itself" << std::endl
;
2874 cerr
<< "WARNING: pool copy does not preserve user_version, which some "
2875 << " apps may rely on." << std::endl
;
2877 if (rados
.get_pool_is_selfmanaged_snaps_mode(src_pool
)) {
2878 cerr
<< "WARNING: pool " << src_pool
<< " has selfmanaged snaps, which are not preserved\n"
2879 << " by the cppool operation. This will break any snapshot user."
2882 cerr
<< " If you insist on making a broken copy, you can pass\n"
2883 << " --yes-i-really-mean-it to proceed anyway."
2889 ret
= do_copy_pool(rados
, src_pool
, target_pool
);
2891 cerr
<< "error copying pool " << src_pool
<< " => " << target_pool
<< ": "
2892 << cpp_strerror(ret
) << std::endl
;
2895 cout
<< "successfully copied pool " << nargs
[1] << std::endl
;
2897 else if (strcmp(nargs
[0], "rmpool") == 0) {
2898 if (nargs
.size() < 2)
2900 if (nargs
.size() < 4 ||
2901 strcmp(nargs
[1], nargs
[2]) != 0 ||
2902 strcmp(nargs
[3], "--yes-i-really-really-mean-it") != 0) {
2903 cerr
<< "WARNING:\n"
2904 << " This will PERMANENTLY DESTROY an entire pool of objects with no way back.\n"
2905 << " To confirm, pass the pool to remove twice, followed by\n"
2906 << " --yes-i-really-really-mean-it" << std::endl
;
2910 ret
= rados
.pool_delete(nargs
[1]);
2912 cout
<< "successfully deleted pool " << nargs
[1] << std::endl
;
2914 cerr
<< "pool " << nargs
[1] << " could not be removed" << std::endl
;
2915 cerr
<< "Check your monitor configuration - `mon allow pool delete` is set to false by default,"
2916 << " change it to true to allow deletion of pools" << std::endl
;
2919 else if (strcmp(nargs
[0], "purge") == 0) {
2920 if (nargs
.size() < 2)
2922 if (nargs
.size() < 3 ||
2923 strcmp(nargs
[2], "--yes-i-really-really-mean-it") != 0) {
2924 cerr
<< "WARNING:\n"
2925 << " This will PERMANENTLY DESTROY all objects from a pool with no way back.\n"
2926 << " To confirm, follow pool with --yes-i-really-really-mean-it" << std::endl
;
2930 ret
= rados
.ioctx_create(nargs
[1], io_ctx
);
2932 cerr
<< "error pool " << nargs
[1] << ": "
2933 << cpp_strerror(ret
) << std::endl
;
2936 io_ctx
.set_namespace(all_nspaces
);
2937 io_ctx
.set_osdmap_full_try();
2938 RadosBencher
bencher(g_ceph_context
, rados
, io_ctx
);
2939 ret
= bencher
.clean_up_slow("", concurrent_ios
);
2941 cout
<< "successfully purged pool " << nargs
[1] << std::endl
;
2943 cerr
<< "pool " << nargs
[1] << " could not be purged" << std::endl
;
2944 cerr
<< "Check your monitor configuration - `mon allow pool delete` is set to false by default,"
2945 << " change it to true to allow deletion of pools" << std::endl
;
2948 else if (strcmp(nargs
[0], "lssnap") == 0) {
2949 if (!pool_name
|| nargs
.size() != 1)
2952 vector
<snap_t
> snaps
;
2953 io_ctx
.snap_list(&snaps
);
2954 for (vector
<snap_t
>::iterator i
= snaps
.begin();
2959 if (io_ctx
.snap_get_name(*i
, &s
) < 0)
2961 if (io_ctx
.snap_get_stamp(*i
, &t
) < 0)
2964 localtime_r(&t
, &bdt
);
2965 cout
<< *i
<< "\t" << s
<< "\t";
2967 std::ios_base::fmtflags original_flags
= cout
.flags();
2968 cout
.setf(std::ios::right
);
2970 cout
<< std::setw(4) << (bdt
.tm_year
+1900)
2971 << '.' << std::setw(2) << (bdt
.tm_mon
+1)
2972 << '.' << std::setw(2) << bdt
.tm_mday
2974 << std::setw(2) << bdt
.tm_hour
2975 << ':' << std::setw(2) << bdt
.tm_min
2976 << ':' << std::setw(2) << bdt
.tm_sec
2978 cout
.flags(original_flags
);
2980 cout
<< snaps
.size() << " snaps" << std::endl
;
2983 else if (strcmp(nargs
[0], "mksnap") == 0) {
2984 if (!pool_name
|| nargs
.size() < 2)
2987 ret
= io_ctx
.snap_create(nargs
[1]);
2989 cerr
<< "error creating pool " << pool_name
<< " snapshot " << nargs
[1]
2990 << ": " << cpp_strerror(ret
) << std::endl
;
2993 cout
<< "created pool " << pool_name
<< " snap " << nargs
[1] << std::endl
;
2996 else if (strcmp(nargs
[0], "rmsnap") == 0) {
2997 if (!pool_name
|| nargs
.size() < 2)
3000 ret
= io_ctx
.snap_remove(nargs
[1]);
3002 cerr
<< "error removing pool " << pool_name
<< " snapshot " << nargs
[1]
3003 << ": " << cpp_strerror(ret
) << std::endl
;
3006 cout
<< "removed pool " << pool_name
<< " snap " << nargs
[1] << std::endl
;
3009 else if (strcmp(nargs
[0], "rollback") == 0) {
3010 if (!pool_name
|| nargs
.size() < 3)
3013 ret
= io_ctx
.snap_rollback(nargs
[1], nargs
[2]);
3015 cerr
<< "error rolling back pool " << pool_name
<< " to snapshot " << nargs
[1]
3016 << cpp_strerror(ret
) << std::endl
;
3019 cout
<< "rolled back pool " << pool_name
3020 << " to snapshot " << nargs
[2] << std::endl
;
3022 else if (strcmp(nargs
[0], "bench") == 0) {
3023 if (!pool_name
|| nargs
.size() < 3)
3025 char* endptr
= NULL
;
3026 int seconds
= strtol(nargs
[1], &endptr
, 10);
3028 cerr
<< "Invalid value for seconds: '" << nargs
[1] << "'" << std::endl
;
3033 if (strcmp(nargs
[2], "write") == 0)
3034 operation
= OP_WRITE
;
3035 else if (strcmp(nargs
[2], "seq") == 0)
3036 operation
= OP_SEQ_READ
;
3037 else if (strcmp(nargs
[2], "rand") == 0)
3038 operation
= OP_RAND_READ
;
3041 if (operation
!= OP_WRITE
) {
3042 if (block_size_specified
) {
3043 cerr
<< "-b|--block_size option can be used only with 'write' bench test"
3048 if (bench_write_dest
!= 0) {
3049 cerr
<< "--write-object, --write-omap and --write-xattr options can "
3050 "only be used with the 'write' bench test"
3056 else if (bench_write_dest
== 0) {
3057 bench_write_dest
= OP_WRITE_DEST_OBJ
;
3060 if (!formatter
&& output
) {
3061 cerr
<< "-o|--output option can only be used with '--format' option"
3066 RadosBencher
bencher(g_ceph_context
, rados
, io_ctx
);
3067 bencher
.set_show_time(show_time
);
3068 bencher
.set_write_destination(static_cast<OpWriteDest
>(bench_write_dest
));
3070 ostream
*outstream
= NULL
;
3072 bencher
.set_formatter(formatter
);
3074 outstream
= new ofstream(output
);
3077 bencher
.set_outstream(*outstream
);
3080 object_size
= op_size
;
3081 else if (object_size
< op_size
)
3082 op_size
= object_size
;
3083 cout
<< "hints = " << (int)hints
<< std::endl
;
3084 ret
= bencher
.aio_bench(operation
, seconds
,
3085 concurrent_ios
, op_size
, object_size
,
3086 max_objects
, cleanup
, hints
, run_name
, no_verify
);
3088 cerr
<< "error during benchmark: " << cpp_strerror(ret
) << std::endl
;
3089 if (formatter
&& output
)
3092 else if (strcmp(nargs
[0], "cleanup") == 0) {
3096 io_ctx
.set_namespace(all_nspaces
);
3097 RadosBencher
bencher(g_ceph_context
, rados
, io_ctx
);
3098 ret
= bencher
.clean_up(prefix
, concurrent_ios
, run_name
);
3100 cerr
<< "error during cleanup: " << cpp_strerror(ret
) << std::endl
;
3102 else if (strcmp(nargs
[0], "watch") == 0) {
3103 if (!pool_name
|| nargs
.size() < 2)
3105 string
oid(nargs
[1]);
3106 RadosWatchCtx
ctx(io_ctx
, oid
.c_str());
3108 ret
= io_ctx
.watch2(oid
, &cookie
, &ctx
);
3110 cerr
<< "error calling watch: " << cpp_strerror(ret
) << std::endl
;
3112 cout
<< "press enter to exit..." << std::endl
;
3114 io_ctx
.unwatch2(cookie
);
3115 rados
.watch_flush();
3118 else if (strcmp(nargs
[0], "notify") == 0) {
3119 if (!pool_name
|| nargs
.size() < 3)
3121 string
oid(nargs
[1]);
3122 string
msg(nargs
[2]);
3123 bufferlist bl
, replybl
;
3125 ret
= io_ctx
.notify2(oid
, bl
, 10000, &replybl
);
3127 cerr
<< "error calling notify: " << cpp_strerror(ret
) << std::endl
;
3128 if (replybl
.length()) {
3129 map
<pair
<uint64_t,uint64_t>,bufferlist
> rm
;
3130 set
<pair
<uint64_t,uint64_t> > missed
;
3131 bufferlist::iterator p
= replybl
.begin();
3133 ::decode(missed
, p
);
3134 for (map
<pair
<uint64_t,uint64_t>,bufferlist
>::iterator p
= rm
.begin();
3137 cout
<< "reply client." << p
->first
.first
3138 << " cookie " << p
->first
.second
3139 << " : " << p
->second
.length() << " bytes" << std::endl
;
3140 if (p
->second
.length())
3141 p
->second
.hexdump(cout
);
3143 for (multiset
<pair
<uint64_t,uint64_t> >::iterator p
= missed
.begin();
3144 p
!= missed
.end(); ++p
) {
3145 cout
<< "timeout client." << p
->first
3146 << " cookie " << p
->second
<< std::endl
;
3149 } else if (strcmp(nargs
[0], "set-alloc-hint") == 0) {
3150 if (!pool_name
|| nargs
.size() < 4)
3153 string
oid(nargs
[1]);
3154 uint64_t expected_object_size
= strict_strtoll(nargs
[2], 10, &err
);
3156 cerr
<< "couldn't parse expected_object_size: " << err
<< std::endl
;
3159 uint64_t expected_write_size
= strict_strtoll(nargs
[3], 10, &err
);
3161 cerr
<< "couldn't parse expected_write_size: " << err
<< std::endl
;
3164 ret
= io_ctx
.set_alloc_hint(oid
, expected_object_size
, expected_write_size
);
3166 cerr
<< "error setting alloc-hint " << pool_name
<< "/" << oid
<< ": "
3167 << cpp_strerror(ret
) << std::endl
;
3170 } else if (strcmp(nargs
[0], "load-gen") == 0) {
3172 cerr
<< "error: must specify pool" << std::endl
;
3177 lg
.min_obj_len
= min_obj_len
;
3179 lg
.max_obj_len
= max_obj_len
;
3181 lg
.min_op_len
= min_op_len
;
3183 lg
.max_op_len
= max_op_len
;
3185 lg
.max_ops
= max_ops
;
3187 lg
.max_backlog
= max_backlog
;
3188 if (target_throughput
)
3189 lg
.target_throughput
= target_throughput
<< 20;
3190 if (read_percent
>= 0)
3191 lg
.read_percent
= read_percent
;
3193 lg
.num_objs
= num_objs
;
3195 lg
.run_length
= run_length
;
3197 cout
<< "run length " << run_length
<< " seconds" << std::endl
;
3198 cout
<< "preparing " << lg
.num_objs
<< " objects" << std::endl
;
3199 ret
= lg
.bootstrap(pool_name
);
3201 cerr
<< "load-gen bootstrap failed" << std::endl
;
3204 cout
<< "load-gen will run " << lg
.run_length
<< " seconds" << std::endl
;
3207 } else if (strcmp(nargs
[0], "listomapkeys") == 0) {
3208 if (!pool_name
|| nargs
.size() < 2)
3211 set
<string
> out_keys
;
3212 ret
= io_ctx
.omap_get_keys(nargs
[1], "", LONG_MAX
, &out_keys
);
3214 cerr
<< "error getting omap key set " << pool_name
<< "/"
3215 << nargs
[1] << ": " << cpp_strerror(ret
) << std::endl
;
3219 for (set
<string
>::iterator iter
= out_keys
.begin();
3220 iter
!= out_keys
.end(); ++iter
) {
3221 cout
<< *iter
<< std::endl
;
3223 } else if (strcmp(nargs
[0], "lock") == 0) {
3228 formatter
= new JSONFormatter(pretty_format
);
3230 ret
= do_lock_cmd(nargs
, opts
, &io_ctx
, formatter
);
3231 } else if (strcmp(nargs
[0], "listwatchers") == 0) {
3232 if (!pool_name
|| nargs
.size() < 2)
3235 string
oid(nargs
[1]);
3236 std::list
<obj_watch_t
> lw
;
3238 ret
= io_ctx
.list_watchers(oid
, &lw
);
3240 cerr
<< "error listing watchers " << pool_name
<< "/" << oid
<< ": " << cpp_strerror(ret
) << std::endl
;
3246 for (std::list
<obj_watch_t
>::iterator i
= lw
.begin(); i
!= lw
.end(); ++i
) {
3247 cout
<< "watcher=" << i
->addr
<< " client." << i
->watcher_id
<< " cookie=" << i
->cookie
<< std::endl
;
3249 } else if (strcmp(nargs
[0], "listsnaps") == 0) {
3250 if (!pool_name
|| nargs
.size() < 2)
3253 string
oid(nargs
[1]);
3256 io_ctx
.snap_set_read(LIBRADOS_SNAP_DIR
);
3257 ret
= io_ctx
.list_snaps(oid
, &ls
);
3259 cerr
<< "error listing snap shots " << pool_name
<< "/" << oid
<< ": " << cpp_strerror(ret
) << std::endl
;
3265 map
<snap_t
,string
> snamemap
;
3266 if (formatter
|| pretty_format
) {
3267 vector
<snap_t
> snaps
;
3268 io_ctx
.snap_list(&snaps
);
3269 for (vector
<snap_t
>::iterator i
= snaps
.begin();
3270 i
!= snaps
.end(); ++i
) {
3272 if (io_ctx
.snap_get_name(*i
, &s
) < 0)
3274 snamemap
.insert(pair
<snap_t
,string
>(*i
, s
));
3279 formatter
->open_object_section("object");
3280 formatter
->dump_string("name", oid
);
3281 formatter
->open_array_section("clones");
3283 cout
<< oid
<< ":" << std::endl
;
3284 cout
<< "cloneid snaps size overlap" << std::endl
;
3287 for (std::vector
<clone_info_t
>::iterator ci
= ls
.clones
.begin();
3288 ci
!= ls
.clones
.end(); ++ci
) {
3290 if (formatter
) formatter
->open_object_section("clone");
3292 if (ci
->cloneid
== librados::SNAP_HEAD
) {
3294 formatter
->dump_string("id", "head");
3299 formatter
->dump_unsigned("id", ci
->cloneid
);
3301 cout
<< ci
->cloneid
;
3305 formatter
->open_array_section("snapshots");
3309 if (!formatter
&& ci
->snaps
.empty()) {
3312 for (std::vector
<snap_t
>::const_iterator snapindex
= ci
->snaps
.begin();
3313 snapindex
!= ci
->snaps
.end(); ++snapindex
) {
3315 map
<snap_t
,string
>::iterator si
;
3317 if (formatter
|| pretty_format
) si
= snamemap
.find(*snapindex
);
3320 formatter
->open_object_section("snapshot");
3321 formatter
->dump_unsigned("id", *snapindex
);
3322 if (si
!= snamemap
.end())
3323 formatter
->dump_string("name", si
->second
);
3324 formatter
->close_section(); //snapshot
3326 if (snapindex
!= ci
->snaps
.begin()) cout
<< ",";
3327 if (!pretty_format
|| (si
== snamemap
.end()))
3330 cout
<< si
->second
<< "(" << *snapindex
<< ")";
3335 formatter
->close_section(); //Snapshots
3336 formatter
->dump_unsigned("size", ci
->size
);
3338 cout
<< "\t" << ci
->size
;
3341 if (ci
->cloneid
!= librados::SNAP_HEAD
) {
3343 formatter
->open_array_section("overlaps");
3347 for (std::vector
< std::pair
<uint64_t,uint64_t> >::iterator ovi
= ci
->overlap
.begin();
3348 ovi
!= ci
->overlap
.end(); ++ovi
) {
3350 formatter
->open_object_section("section");
3351 formatter
->dump_unsigned("start", ovi
->first
);
3352 formatter
->dump_unsigned("length", ovi
->second
);
3353 formatter
->close_section(); //section
3355 if (ovi
!= ci
->overlap
.begin()) cout
<< ",";
3356 cout
<< ovi
->first
<< "~" << ovi
->second
;
3360 formatter
->close_section(); //overlaps
3362 cout
<< "]" << std::endl
;
3364 if (formatter
) formatter
->close_section(); //clone
3367 formatter
->close_section(); //clones
3368 formatter
->close_section(); //object
3369 formatter
->flush(cout
);
3373 } else if (strcmp(nargs
[0], "list-inconsistent-pg") == 0) {
3375 formatter
= new JSONFormatter(pretty_format
);
3377 ret
= do_get_inconsistent_pg_cmd(nargs
, rados
, *formatter
);
3378 } else if (strcmp(nargs
[0], "list-inconsistent-obj") == 0) {
3380 formatter
= new JSONFormatter(pretty_format
);
3382 ret
= do_get_inconsistent_cmd
<inconsistent_obj_t
>(nargs
, rados
, *formatter
);
3383 } else if (strcmp(nargs
[0], "list-inconsistent-snapset") == 0) {
3385 formatter
= new JSONFormatter(pretty_format
);
3387 ret
= do_get_inconsistent_cmd
<inconsistent_snapset_t
>(nargs
, rados
, *formatter
);
3388 } else if (strcmp(nargs
[0], "cache-flush") == 0) {
3389 if (!pool_name
|| nargs
.size() < 2)
3391 string
oid(nargs
[1]);
3394 io_ctx
.snap_set_read(LIBRADOS_SNAP_DIR
);
3395 ret
= io_ctx
.list_snaps(oid
, &ls
);
3397 cerr
<< "error listing snapshots " << pool_name
<< "/" << oid
<< ": "
3398 << cpp_strerror(ret
) << std::endl
;
3401 for (std::vector
<clone_info_t
>::iterator ci
= ls
.clones
.begin();
3402 ci
!= ls
.clones
.end(); ++ci
) {
3403 if (snapid
!= CEPH_NOSNAP
&& ci
->cloneid
> snapid
)
3405 io_ctx
.snap_set_read(ci
->cloneid
);
3406 ret
= do_cache_flush(io_ctx
, oid
);
3408 cerr
<< "error from cache-flush " << oid
<< ": "
3409 << cpp_strerror(ret
) << std::endl
;
3414 ret
= do_cache_flush(io_ctx
, oid
);
3416 cerr
<< "error from cache-flush " << oid
<< ": "
3417 << cpp_strerror(ret
) << std::endl
;
3421 } else if (strcmp(nargs
[0], "cache-try-flush") == 0) {
3422 if (!pool_name
|| nargs
.size() < 2)
3424 string
oid(nargs
[1]);
3427 io_ctx
.snap_set_read(LIBRADOS_SNAP_DIR
);
3428 ret
= io_ctx
.list_snaps(oid
, &ls
);
3430 cerr
<< "error listing snapshots " << pool_name
<< "/" << oid
<< ": "
3431 << cpp_strerror(ret
) << std::endl
;
3434 for (std::vector
<clone_info_t
>::iterator ci
= ls
.clones
.begin();
3435 ci
!= ls
.clones
.end(); ++ci
) {
3436 if (snapid
!= CEPH_NOSNAP
&& ci
->cloneid
> snapid
)
3438 io_ctx
.snap_set_read(ci
->cloneid
);
3439 ret
= do_cache_try_flush(io_ctx
, oid
);
3441 cerr
<< "error from cache-flush " << oid
<< ": "
3442 << cpp_strerror(ret
) << std::endl
;
3447 ret
= do_cache_try_flush(io_ctx
, oid
);
3449 cerr
<< "error from cache-flush " << oid
<< ": "
3450 << cpp_strerror(ret
) << std::endl
;
3454 } else if (strcmp(nargs
[0], "cache-evict") == 0) {
3455 if (!pool_name
|| nargs
.size() < 2)
3457 string
oid(nargs
[1]);
3460 io_ctx
.snap_set_read(LIBRADOS_SNAP_DIR
);
3461 ret
= io_ctx
.list_snaps(oid
, &ls
);
3463 cerr
<< "error listing snapshots " << pool_name
<< "/" << oid
<< ": "
3464 << cpp_strerror(ret
) << std::endl
;
3467 for (std::vector
<clone_info_t
>::iterator ci
= ls
.clones
.begin();
3468 ci
!= ls
.clones
.end(); ++ci
) {
3469 if (snapid
!= CEPH_NOSNAP
&& ci
->cloneid
> snapid
)
3471 io_ctx
.snap_set_read(ci
->cloneid
);
3472 ret
= do_cache_evict(io_ctx
, oid
);
3474 cerr
<< "error from cache-flush " << oid
<< ": "
3475 << cpp_strerror(ret
) << std::endl
;
3480 ret
= do_cache_evict(io_ctx
, oid
);
3482 cerr
<< "error from cache-flush " << oid
<< ": "
3483 << cpp_strerror(ret
) << std::endl
;
3487 } else if (strcmp(nargs
[0], "cache-flush-evict-all") == 0) {
3490 ret
= do_cache_flush_evict_all(io_ctx
, true);
3492 cerr
<< "error from cache-flush-evict-all: "
3493 << cpp_strerror(ret
) << std::endl
;
3496 } else if (strcmp(nargs
[0], "cache-try-flush-evict-all") == 0) {
3499 ret
= do_cache_flush_evict_all(io_ctx
, false);
3501 cerr
<< "error from cache-try-flush-evict-all: "
3502 << cpp_strerror(ret
) << std::endl
;
3505 } else if (strcmp(nargs
[0], "set-redirect") == 0) {
3509 const char *target
= target_pool_name
;
3513 const char *target_obj
;
3514 if (nargs
.size() < 3) {
3515 if (strcmp(target
, pool_name
) == 0) {
3516 cerr
<< "cannot copy object into itself" << std::endl
;
3520 target_obj
= nargs
[1];
3522 target_obj
= nargs
[2];
3526 ret
= rados
.ioctx_create(target
, target_ctx
);
3527 if (target_oloc
.size()) {
3528 target_ctx
.locator_set_key(target_oloc
);
3530 if (target_nspace
.size()) {
3531 target_ctx
.set_namespace(target_nspace
);
3534 ObjectWriteOperation op
;
3535 op
.set_redirect(target_obj
, target_ctx
, 0);
3536 ret
= io_ctx
.operate(nargs
[1], &op
);
3538 cerr
<< "error set-redirect " << pool_name
<< "/" << nargs
[1] << " => " << target
<< "/" << target_obj
<< ": " << cpp_strerror(ret
) << std::endl
;
3541 } else if (strcmp(nargs
[0], "export") == 0) {
3542 // export [filename]
3543 if (!pool_name
|| nargs
.size() > 2) {
3548 if (nargs
.size() < 2 || std::string(nargs
[1]) == "-") {
3549 file_fd
= STDOUT_FILENO
;
3551 file_fd
= open(nargs
[1], O_WRONLY
|O_CREAT
|O_TRUNC
, 0666);
3553 cerr
<< "Error opening '" << nargs
[1] << "': "
3554 << cpp_strerror(file_fd
) << std::endl
;
3560 ret
= PoolDump(file_fd
).dump(&io_ctx
);
3562 if (file_fd
!= STDIN_FILENO
) {
3563 VOID_TEMP_FAILURE_RETRY(::close(file_fd
));
3567 cerr
<< "error from export: "
3568 << cpp_strerror(ret
) << std::endl
;
3571 } else if (strcmp(nargs
[0], "import") == 0) {
3572 // import [--no-overwrite] [--dry-run] <filename | - >
3573 if (!pool_name
|| nargs
.size() > 4 || nargs
.size() < 2) {
3577 // Last arg is the filename
3578 std::string
const filename
= nargs
[nargs
.size() - 1];
3580 // All other args may be flags
3581 bool dry_run
= false;
3582 bool no_overwrite
= false;
3583 for (unsigned i
= 1; i
< nargs
.size() - 1; ++i
) {
3584 std::string
arg(nargs
[i
]);
3586 if (arg
== std::string("--no-overwrite")) {
3587 no_overwrite
= true;
3588 } else if (arg
== std::string("--dry-run")) {
3591 std::cerr
<< "Invalid argument '" << arg
<< "'" << std::endl
;
3598 if (filename
== "-") {
3599 file_fd
= STDIN_FILENO
;
3601 file_fd
= open(filename
.c_str(), O_RDONLY
);
3603 cerr
<< "Error opening '" << filename
<< "': "
3604 << cpp_strerror(file_fd
) << std::endl
;
3610 ret
= RadosImport(file_fd
, 0, dry_run
).import(io_ctx
, no_overwrite
);
3612 if (file_fd
!= STDIN_FILENO
) {
3613 VOID_TEMP_FAILURE_RETRY(::close(file_fd
));
3617 cerr
<< "error from import: "
3618 << cpp_strerror(ret
) << std::endl
;
3622 cerr
<< "unrecognized command " << nargs
[0] << "; -h or --help for usage" << std::endl
;
3628 cerr
<< "error " << (-ret
) << ": " << cpp_strerror(ret
) << std::endl
;
3632 return (ret
< 0) ? 1 : 0;
3635 int main(int argc
, const char **argv
)
3637 vector
<const char*> args
;
3638 argv_to_vec(argc
, argv
, args
);
3641 std::map
< std::string
, std::string
> opts
;
3644 // Necessary to support usage of -f for formatting,
3645 // since global_init will remove the -f using ceph
3646 // argparse procedures.
3647 for (auto j
= args
.begin(); j
!= args
.end(); ++j
) {
3648 if (strcmp(*j
, "--") == 0) {
3650 } else if ((j
+1) == args
.end()) {
3651 // This can't be a formatting call (no format arg)
3653 } else if (strcmp(*j
, "-f") == 0) {
3655 unique_ptr
<Formatter
> formatter(Formatter::create(val
.c_str()));
3659 opts
["format"] = val
;
3667 auto cct
= global_init(NULL
, args
, CEPH_ENTITY_TYPE_CLIENT
,
3668 CODE_ENVIRONMENT_UTILITY
, 0);
3669 common_init_finish(g_ceph_context
);
3671 std::vector
<const char*>::iterator i
;
3672 for (i
= args
.begin(); i
!= args
.end(); ) {
3673 if (ceph_argparse_double_dash(args
, i
)) {
3675 } else if (ceph_argparse_flag(args
, i
, "-h", "--help", (char*)NULL
)) {
3678 } else if (ceph_argparse_flag(args
, i
, "-f", "--force", (char*)NULL
)) {
3679 opts
["force"] = "true";
3680 } else if (ceph_argparse_flag(args
, i
, "--force-full", (char*)NULL
)) {
3681 opts
["force-full"] = "true";
3682 } else if (ceph_argparse_flag(args
, i
, "-d", "--delete-after", (char*)NULL
)) {
3683 opts
["delete-after"] = "true";
3684 } else if (ceph_argparse_flag(args
, i
, "-C", "--create", "--create-pool",
3686 opts
["create"] = "true";
3687 } else if (ceph_argparse_flag(args
, i
, "--pretty-format", (char*)NULL
)) {
3688 opts
["pretty-format"] = "true";
3689 } else if (ceph_argparse_flag(args
, i
, "--show-time", (char*)NULL
)) {
3690 opts
["show-time"] = "true";
3691 } else if (ceph_argparse_flag(args
, i
, "--no-cleanup", (char*)NULL
)) {
3692 opts
["no-cleanup"] = "true";
3693 } else if (ceph_argparse_flag(args
, i
, "--no-hints", (char*)NULL
)) {
3694 opts
["no-hints"] = "true";
3695 } else if (ceph_argparse_flag(args
, i
, "--no-verify", (char*)NULL
)) {
3696 opts
["no-verify"] = "true";
3697 } else if (ceph_argparse_witharg(args
, i
, &val
, "--run-name", (char*)NULL
)) {
3698 opts
["run-name"] = val
;
3699 } else if (ceph_argparse_witharg(args
, i
, &val
, "--prefix", (char*)NULL
)) {
3700 opts
["prefix"] = val
;
3701 } else if (ceph_argparse_witharg(args
, i
, &val
, "-p", "--pool", (char*)NULL
)) {
3703 } else if (ceph_argparse_witharg(args
, i
, &val
, "--target-pool", (char*)NULL
)) {
3704 opts
["target_pool"] = val
;
3705 } else if (ceph_argparse_witharg(args
, i
, &val
, "--object-locator" , (char *)NULL
)) {
3706 opts
["object_locator"] = val
;
3707 } else if (ceph_argparse_witharg(args
, i
, &val
, "--target-locator" , (char *)NULL
)) {
3708 opts
["target_locator"] = val
;
3709 } else if (ceph_argparse_witharg(args
, i
, &val
, "--target-nspace" , (char *)NULL
)) {
3710 opts
["target_nspace"] = val
;
3711 } else if (ceph_argparse_flag(args
, i
, "--striper" , (char *)NULL
)) {
3712 opts
["striper"] = "true";
3713 } else if (ceph_argparse_witharg(args
, i
, &val
, "-t", "--concurrent-ios", (char*)NULL
)) {
3714 opts
["concurrent-ios"] = val
;
3715 } else if (ceph_argparse_witharg(args
, i
, &val
, "--block-size", (char*)NULL
)) {
3716 opts
["block-size"] = val
;
3717 } else if (ceph_argparse_witharg(args
, i
, &val
, "-b", (char*)NULL
)) {
3718 opts
["block-size"] = val
;
3719 } else if (ceph_argparse_witharg(args
, i
, &val
, "--object-size", (char*)NULL
)) {
3720 opts
["object-size"] = val
;
3721 } else if (ceph_argparse_witharg(args
, i
, &val
, "--max-objects", (char*)NULL
)) {
3722 opts
["max-objects"] = val
;
3723 } else if (ceph_argparse_witharg(args
, i
, &val
, "--offset", (char*)NULL
)) {
3724 opts
["offset"] = val
;
3725 } else if (ceph_argparse_witharg(args
, i
, &val
, "-o", (char*)NULL
)) {
3726 opts
["object-size"] = val
;
3727 } else if (ceph_argparse_witharg(args
, i
, &val
, "-s", "--snap", (char*)NULL
)) {
3729 } else if (ceph_argparse_witharg(args
, i
, &val
, "-S", "--snapid", (char*)NULL
)) {
3730 opts
["snapid"] = val
;
3731 } else if (ceph_argparse_witharg(args
, i
, &val
, "--min-object-size", (char*)NULL
)) {
3732 opts
["min-object-size"] = val
;
3733 } else if (ceph_argparse_witharg(args
, i
, &val
, "--max-object-size", (char*)NULL
)) {
3734 opts
["max-object-size"] = val
;
3735 } else if (ceph_argparse_witharg(args
, i
, &val
, "--min-op-len", (char*)NULL
)) {
3736 opts
["min-op-len"] = val
;
3737 } else if (ceph_argparse_witharg(args
, i
, &val
, "--max-op-len", (char*)NULL
)) {
3738 opts
["max-op-len"] = val
;
3739 } else if (ceph_argparse_witharg(args
, i
, &val
, "--max-ops", (char*)NULL
)) {
3740 opts
["max-ops"] = val
;
3741 } else if (ceph_argparse_witharg(args
, i
, &val
, "--max-backlog", (char*)NULL
)) {
3742 opts
["max-backlog"] = val
;
3743 } else if (ceph_argparse_witharg(args
, i
, &val
, "--target-throughput", (char*)NULL
)) {
3744 opts
["target-throughput"] = val
;
3745 } else if (ceph_argparse_witharg(args
, i
, &val
, "--read-percent", (char*)NULL
)) {
3746 opts
["read-percent"] = val
;
3747 } else if (ceph_argparse_witharg(args
, i
, &val
, "--num-objects", (char*)NULL
)) {
3748 opts
["num-objects"] = val
;
3749 } else if (ceph_argparse_witharg(args
, i
, &val
, "--run-length", (char*)NULL
)) {
3750 opts
["run-length"] = val
;
3751 } else if (ceph_argparse_witharg(args
, i
, &val
, "--workers", (char*)NULL
)) {
3752 opts
["workers"] = val
;
3753 } else if (ceph_argparse_witharg(args
, i
, &val
, "--format", (char*)NULL
)) {
3754 opts
["format"] = val
;
3755 } else if (ceph_argparse_witharg(args
, i
, &val
, "--lock-tag", (char*)NULL
)) {
3756 opts
["lock-tag"] = val
;
3757 } else if (ceph_argparse_witharg(args
, i
, &val
, "--lock-cookie", (char*)NULL
)) {
3758 opts
["lock-cookie"] = val
;
3759 } else if (ceph_argparse_witharg(args
, i
, &val
, "--lock-description", (char*)NULL
)) {
3760 opts
["lock-description"] = val
;
3761 } else if (ceph_argparse_witharg(args
, i
, &val
, "--lock-duration", (char*)NULL
)) {
3762 opts
["lock-duration"] = val
;
3763 } else if (ceph_argparse_witharg(args
, i
, &val
, "--lock-type", (char*)NULL
)) {
3764 opts
["lock-type"] = val
;
3765 } else if (ceph_argparse_witharg(args
, i
, &val
, "-N", "--namespace", (char*)NULL
)) {
3766 opts
["namespace"] = val
;
3767 } else if (ceph_argparse_flag(args
, i
, "--all", (char*)NULL
)) {
3768 opts
["all"] = "true";
3769 } else if (ceph_argparse_flag(args
, i
, "--default", (char*)NULL
)) {
3770 opts
["default"] = "true";
3771 } else if (ceph_argparse_witharg(args
, i
, &val
, "-o", "--output", (char*)NULL
)) {
3772 opts
["output"] = val
;
3773 } else if (ceph_argparse_flag(args
, i
, "--write-omap", (char*)NULL
)) {
3774 opts
["write-dest-omap"] = "true";
3775 } else if (ceph_argparse_flag(args
, i
, "--write-object", (char*)NULL
)) {
3776 opts
["write-dest-obj"] = "true";
3777 } else if (ceph_argparse_flag(args
, i
, "--write-xattr", (char*)NULL
)) {
3778 opts
["write-dest-xattr"] = "true";
3779 } else if (ceph_argparse_flag(args
, i
, "--with-clones", (char*)NULL
)) {
3780 opts
["with-clones"] = "true";
3781 } else if (ceph_argparse_witharg(args
, i
, &val
, "--omap-key-file", (char*)NULL
)) {
3782 opts
["omap-key-file"] = val
;
3791 cerr
<< "rados: you must give an action. Try --help" << std::endl
;
3795 return rados_tool_common(opts
, args
);