]> git.proxmox.com Git - ceph.git/blob - ceph/src/SimpleRADOSStriper.h
2e7984870f9085e3e9b70386d505f3edbc9fcbbd
[ceph.git] / ceph / src / SimpleRADOSStriper.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 /*
5 * Ceph - scalable distributed file system
6 *
7 * Copyright (C) 2021 Red Hat, Inc.
8 *
9 * This is free software; you can redistribute it and/or modify it under the
10 * terms of the GNU Lesser General Public License version 2.1, as published by
11 * the Free Software Foundation. See file COPYING.
12 *
13 */
14
15 #ifndef _SIMPLERADOSSTRIPER_H
16 #define _SIMPLERADOSSTRIPER_H
17
18 #include <queue>
19 #include <string_view>
20 #include <thread>
21
22 #include "include/buffer.h"
23 #include "include/rados/librados.hpp"
24 #include "include/uuid.h"
25 #include "include/types.h"
26
27 #include "common/ceph_time.h"
28 #include "common/perf_counters.h"
29
30 class SimpleRADOSStriper
31 {
32 public:
33 using aiocompletionptr = std::unique_ptr<librados::AioCompletion>;
34 using clock = ceph::coarse_mono_clock;
35 using time = ceph::coarse_mono_time;
36
37 static inline const uint64_t object_size = 22; /* power of 2 */
38 static inline const uint64_t min_growth = (1<<27); /* 128 MB */
39 static int config_logger(CephContext* cct, std::string_view name, std::shared_ptr<PerfCounters>* l);
40
41 SimpleRADOSStriper() = default;
42 SimpleRADOSStriper(librados::IoCtx _ioctx, std::string _oid)
43 : ioctx(std::move(_ioctx))
44 , oid(std::move(_oid))
45 {
46 cookie.generate_random();
47 auto r = librados::Rados(ioctx);
48 myaddrs = r.get_addrs();
49 }
50 SimpleRADOSStriper(const SimpleRADOSStriper&) = delete;
51 SimpleRADOSStriper& operator=(const SimpleRADOSStriper&) = delete;
52 SimpleRADOSStriper& operator=(SimpleRADOSStriper&&) = delete;
53 SimpleRADOSStriper(SimpleRADOSStriper&&) = delete;
54 ~SimpleRADOSStriper();
55
56 int create();
57 int open();
58 int remove();
59 int stat(uint64_t* size);
60 ssize_t write(const void* data, size_t len, uint64_t off);
61 ssize_t read(void* data, size_t len, uint64_t off);
62 int truncate(size_t size);
63 int flush();
64 int lock(uint64_t timeoutms);
65 int unlock();
66 int is_locked() const {
67 return locked;
68 }
69 int print_lockers(std::ostream& out);
70 void set_logger(std::shared_ptr<PerfCounters> l) {
71 logger = std::move(l);
72 }
73 void set_lock_interval(std::chrono::milliseconds t) {
74 lock_keeper_interval = t;
75 }
76 void set_lock_timeout(std::chrono::milliseconds t) {
77 lock_keeper_timeout = t;
78 }
79 void set_blocklist_the_dead(bool b) {
80 blocklist_the_dead = b;
81 }
82
83 protected:
84 struct extent {
85 std::string soid;
86 size_t len;
87 size_t off;
88 };
89
90 ceph::bufferlist str2bl(std::string_view sv);
91 ceph::bufferlist uint2bl(uint64_t v);
92 int set_metadata(uint64_t new_size, bool update_size);
93 int shrink_alloc(uint64_t a);
94 int maybe_shrink_alloc();
95 int wait_for_aios(bool block);
96 int recover_lock();
97 extent get_next_extent(uint64_t off, size_t len) const;
98 extent get_first_extent() const {
99 return get_next_extent(0, 0);
100 }
101
102 private:
103 static inline const char XATTR_EXCL[] = "striper.excl";
104 static inline const char XATTR_SIZE[] = "striper.size";
105 static inline const char XATTR_ALLOCATED[] = "striper.allocated";
106 static inline const char XATTR_VERSION[] = "striper.version";
107 static inline const char XATTR_LAYOUT_STRIPE_UNIT[] = "striper.layout.stripe_unit";
108 static inline const char XATTR_LAYOUT_STRIPE_COUNT[] = "striper.layout.stripe_count";
109 static inline const char XATTR_LAYOUT_OBJECT_SIZE[] = "striper.layout.object_size";
110 static inline const std::string biglock = "striper.lock";
111 static inline const std::string lockdesc = "SimpleRADOSStriper";
112
113 void lock_keeper_main();
114
115 librados::IoCtx ioctx;
116 std::shared_ptr<PerfCounters> logger;
117 std::string oid;
118 std::thread lock_keeper;
119 std::condition_variable lock_keeper_cvar;
120 std::mutex lock_keeper_mutex;
121 time last_renewal = time::min();
122 std::chrono::milliseconds lock_keeper_interval = 2000ms;
123 std::chrono::milliseconds lock_keeper_timeout = 30000ms;
124 std::atomic<bool> blocklisted = false;
125 bool shutdown = false;
126 version_t version = 0;
127 std::string exclusive_holder;
128 uint64_t size = 0;
129 uint64_t allocated = 0;
130 uuid_d cookie{};
131 bool locked = false;
132 bool size_dirty = false;
133 bool blocklist_the_dead = true;
134 std::queue<aiocompletionptr> aios;
135 int aios_failure = 0;
136 std::string myaddrs;
137 };
138
139 #endif /* _SIMPLERADOSSTRIPER_H */