]>
Commit | Line | Data |
---|---|---|
9f95a23c TL |
1 | // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- |
2 | // vim: ts=8 sw=2 smarttab | |
3 | ||
4 | #include "librbd/cache/rwl/ImageCacheState.h" | |
5 | #include "librbd/ImageCtx.h" | |
6 | #include "librbd/Operations.h" | |
7 | #include "common/environment.h" | |
8 | #include "common/hostname.h" | |
9 | #include "common/config_proxy.h" | |
10 | #include "common/ceph_json.h" | |
11 | ||
12 | #define dout_subsys ceph_subsys_rbd_rwl | |
13 | #undef dout_prefix | |
14 | #define dout_prefix *_dout << "librbd::cache::rwl::ImageCacheState: " << this << " " \ | |
15 | << __func__ << ": " | |
16 | ||
17 | namespace librbd { | |
18 | namespace cache { | |
19 | namespace rwl { | |
20 | ||
21 | template <typename I> | |
22 | const std::string ImageCacheState<I>::image_cache_state = ".librbd/image_cache_state"; | |
23 | ||
24 | template <typename I> | |
25 | ImageCacheState<I>::ImageCacheState(I *image_ctx) : m_image_ctx(image_ctx) { | |
26 | ldout(image_ctx->cct, 20) << "Initialize RWL cache state with config data. " << dendl; | |
27 | ||
28 | ConfigProxy &config = image_ctx->config; | |
29 | host = ceph_get_short_hostname(); | |
30 | path = config.get_val<std::string>("rbd_rwl_path"); | |
31 | size = config.get_val<uint64_t>("rbd_rwl_size"); | |
32 | log_periodic_stats = config.get_val<bool>("rbd_rwl_log_periodic_stats"); | |
33 | } | |
34 | ||
35 | template <typename I> | |
36 | ImageCacheState<I>::ImageCacheState(I *image_ctx, JSONFormattable &f) : m_image_ctx(image_ctx) { | |
37 | ldout(image_ctx->cct, 20) << "Initialize RWL cache state with data from server side" << dendl; | |
38 | ||
39 | present = (bool)f["present"]; | |
40 | empty = (bool)f["empty"]; | |
41 | clean = (bool)f["clean"]; | |
42 | host = (string)f["rwl_host"]; | |
43 | path = (string)f["rwl_path"]; | |
44 | uint64_t rwl_size; | |
45 | std::istringstream iss(f["rwl_size"]); | |
46 | iss >> rwl_size; | |
47 | size = rwl_size; | |
48 | ||
49 | // Others from config | |
50 | ConfigProxy &config = image_ctx->config; | |
51 | log_periodic_stats = config.get_val<bool>("rbd_rwl_log_periodic_stats"); | |
52 | } | |
53 | ||
54 | template <typename I> | |
55 | void ImageCacheState<I>::write_image_cache_state(Context *on_finish) { | |
56 | std::shared_lock owner_lock{m_image_ctx->owner_lock}; | |
57 | JSONFormattable f; | |
58 | ::encode_json(image_cache_state.c_str(), *this, &f); | |
59 | std::ostringstream oss; | |
60 | f.flush(oss); | |
61 | std::string image_state_json = oss.str(); | |
62 | ||
63 | ldout(m_image_ctx->cct, 20) << __func__ << " Store state: " << image_state_json << dendl; | |
64 | m_image_ctx->operations->execute_metadata_set(image_cache_state.c_str(), image_state_json, on_finish); | |
65 | } | |
66 | ||
67 | template <typename I> | |
68 | void ImageCacheState<I>::clear_image_cache_state(Context *on_finish) { | |
69 | std::shared_lock owner_lock{m_image_ctx->owner_lock}; | |
70 | ldout(m_image_ctx->cct, 20) << __func__ << " Remove state: " << dendl; | |
71 | m_image_ctx->operations->execute_metadata_remove(image_cache_state.c_str(), on_finish); | |
72 | } | |
73 | ||
74 | template <typename I> | |
75 | void ImageCacheState<I>::dump(ceph::Formatter *f) const { | |
76 | ::encode_json("present", present, f); | |
77 | ::encode_json("empty", empty, f); | |
78 | ::encode_json("clean", clean, f); | |
79 | ::encode_json("cache_type", (int)get_image_cache_type(), f); | |
80 | ::encode_json("rwl_host", host, f); | |
81 | ::encode_json("rwl_path", path, f); | |
82 | ::encode_json("rwl_size", size, f); | |
83 | } | |
84 | ||
85 | } // namespace rwl | |
86 | } // namespace cache | |
87 | } // namespace librbd | |
88 | ||
89 | template class librbd::cache::rwl::ImageCacheState<librbd::ImageCtx>; |