]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/rbd_mirror/ImageDeleter.h
add subtree-ish sources for 12.0.3
[ceph.git] / ceph / src / tools / rbd_mirror / ImageDeleter.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3 /*
4 * Ceph - scalable distributed file system
5 *
6 * Copyright (C) 2016 SUSE LINUX GmbH
7 *
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.
12 *
13 */
14
15 #ifndef CEPH_RBD_MIRROR_IMAGEDELETER_H
16 #define CEPH_RBD_MIRROR_IMAGEDELETER_H
17
18 #include "common/Mutex.h"
19 #include "common/Cond.h"
20 #include "common/Thread.h"
21 #include "common/Timer.h"
22 #include "types.h"
23
24 #include <deque>
25 #include <vector>
26 #include <atomic>
27
28 class ContextWQ;
29
30 namespace rbd {
31 namespace mirror {
32
33 class ImageDeleterAdminSocketHook;
34
35 /**
36 * Manage deletion of non-primary images.
37 */
38 class ImageDeleter {
39 public:
40 static const int EISPRM = 1000;
41
42 ImageDeleter(ContextWQ *work_queue, SafeTimer *timer, Mutex *timer_lock);
43 ~ImageDeleter();
44 ImageDeleter(const ImageDeleter&) = delete;
45 ImageDeleter& operator=(const ImageDeleter&) = delete;
46
47 void schedule_image_delete(RadosRef local_rados,
48 int64_t local_pool_id,
49 const std::string& global_image_id);
50 void wait_for_scheduled_deletion(int64_t local_pool_id,
51 const std::string &global_image_id,
52 Context *ctx,
53 bool notify_on_failed_retry=true);
54 void cancel_waiter(int64_t local_pool_id,
55 const std::string &global_image_id);
56
57 void print_status(Formatter *f, std::stringstream *ss);
58
59 // for testing purposes
60 std::vector<std::string> get_delete_queue_items();
61 std::vector<std::pair<std::string, int> > get_failed_queue_items();
62 void set_failed_timer_interval(double interval);
63
64 private:
65
66 class ImageDeleterThread : public Thread {
67 ImageDeleter *m_image_deleter;
68 public:
69 ImageDeleterThread(ImageDeleter *image_deleter) :
70 m_image_deleter(image_deleter) {}
71 void *entry() override {
72 m_image_deleter->run();
73 return 0;
74 }
75 };
76
77 struct DeleteInfo {
78 RadosRef local_rados;
79 int64_t local_pool_id;
80 std::string global_image_id;
81 int error_code = 0;
82 int retries = 0;
83 bool notify_on_failed_retry = true;
84 Context *on_delete = nullptr;
85
86 DeleteInfo(RadosRef local_rados, int64_t local_pool_id,
87 const std::string& global_image_id) :
88 local_rados(local_rados), local_pool_id(local_pool_id),
89 global_image_id(global_image_id) {
90 }
91
92 bool match(int64_t local_pool_id, const std::string &global_image_id) {
93 return (this->local_pool_id == local_pool_id &&
94 this->global_image_id == global_image_id);
95 }
96 void notify(int r);
97 void to_string(std::stringstream& ss);
98 void print_status(Formatter *f, std::stringstream *ss,
99 bool print_failure_info=false);
100 };
101
102 std::atomic<unsigned> m_running { 0 };
103
104 ContextWQ *m_work_queue;
105
106 std::deque<std::unique_ptr<DeleteInfo> > m_delete_queue;
107 Mutex m_delete_lock;
108 Cond m_delete_queue_cond;
109
110 unique_ptr<DeleteInfo> m_active_delete;
111
112 ImageDeleterThread m_image_deleter_thread;
113
114 std::deque<std::unique_ptr<DeleteInfo>> m_failed_queue;
115 double m_failed_interval;
116 SafeTimer *m_failed_timer;
117 Mutex *m_failed_timer_lock;
118
119 ImageDeleterAdminSocketHook *m_asok_hook;
120
121 void run();
122 bool process_image_delete();
123 int image_has_snapshots_and_children(librados::IoCtx *ioctx,
124 std::string& image_id,
125 bool *has_snapshots);
126
127 void complete_active_delete(int r);
128 void enqueue_failed_delete(int error_code);
129 void retry_failed_deletions();
130
131 unique_ptr<DeleteInfo> const*
132 find_delete_info(int64_t local_pool_id, const std::string &global_image_id);
133
134 };
135
136 } // namespace mirror
137 } // namespace rbd
138
139 #endif // CEPH_RBD_MIRROR_IMAGEDELETER_H