]> git.proxmox.com Git - ceph.git/blob - ceph/src/tools/immutable_object_cache/CacheController.cc
import 15.2.5
[ceph.git] / ceph / src / tools / immutable_object_cache / CacheController.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include "CacheController.h"
5
6 #define dout_context g_ceph_context
7 #define dout_subsys ceph_subsys_immutable_obj_cache
8 #undef dout_prefix
9 #define dout_prefix *_dout << "ceph::cache::CacheController: " << this << " " \
10 << __func__ << ": "
11
12 namespace ceph {
13 namespace immutable_obj_cache {
14
15 CacheController::CacheController(CephContext *cct,
16 const std::vector<const char*> &args):
17 m_args(args), m_cct(cct) {
18 ldout(m_cct, 20) << dendl;
19 }
20
21 CacheController::~CacheController() {
22 delete m_cache_server;
23 delete m_object_cache_store;
24 }
25
26 int CacheController::init() {
27 ldout(m_cct, 20) << dendl;
28
29 m_object_cache_store = new ObjectCacheStore(m_cct);
30 // TODO(dehao): make this configurable
31 int r = m_object_cache_store->init(true);
32 if (r < 0) {
33 lderr(m_cct) << "init error\n" << dendl;
34 return r;
35 }
36
37 r = m_object_cache_store->init_cache();
38 if (r < 0) {
39 lderr(m_cct) << "init error\n" << dendl;
40 }
41
42 return r;
43 }
44
45 int CacheController::shutdown() {
46 ldout(m_cct, 20) << dendl;
47
48 int r;
49 if (m_cache_server != nullptr) {
50 r = m_cache_server->stop();
51 if (r < 0) {
52 lderr(m_cct) << "stop error\n" << dendl;
53 return r;
54 }
55 }
56
57 r = m_object_cache_store->shutdown();
58 if (r < 0) {
59 lderr(m_cct) << "stop error\n" << dendl;
60 return r;
61 }
62
63 return r;
64 }
65
66 void CacheController::handle_signal(int signum) {
67 shutdown();
68 }
69
70 int CacheController::run() {
71 try {
72 std::string controller_path =
73 m_cct->_conf.get_val<std::string>("immutable_object_cache_sock");
74 if (controller_path.empty()) {
75 lderr(m_cct) << "'immutable_object_cache_sock' path not set" << dendl;
76 return -EINVAL;
77 }
78
79 std::remove(controller_path.c_str());
80
81 m_cache_server = new CacheServer(m_cct, controller_path,
82 std::bind(&CacheController::handle_request, this,
83 std::placeholders::_1, std::placeholders::_2));
84
85 int ret = m_cache_server->run();
86 if (ret != 0) {
87 return ret;
88 }
89
90 return 0;
91 } catch (std::exception& e) {
92 lderr(m_cct) << "Exception: " << e.what() << dendl;
93 return -EFAULT;
94 }
95 }
96
97 void CacheController::handle_request(CacheSession* session,
98 ObjectCacheRequest* req) {
99 ldout(m_cct, 20) << dendl;
100
101 switch (req->get_request_type()) {
102 case RBDSC_REGISTER: {
103 // TODO(dehao): skip register and allow clients to lookup directly
104
105 ObjectCacheRequest* reply = new ObjectCacheRegReplyData(
106 RBDSC_REGISTER_REPLY, req->seq);
107 session->send(reply);
108 break;
109 }
110 case RBDSC_READ: {
111 // lookup object in local cache store
112 std::string cache_path;
113 ObjectCacheReadData* req_read_data =
114 reinterpret_cast <ObjectCacheReadData*> (req);
115 int ret = m_object_cache_store->lookup_object(
116 req_read_data->pool_namespace, req_read_data->pool_id,
117 req_read_data->snap_id, req_read_data->oid, cache_path);
118 ObjectCacheRequest* reply = nullptr;
119 if (ret != OBJ_CACHE_PROMOTED) {
120 reply = new ObjectCacheReadRadosData(RBDSC_READ_RADOS, req->seq);
121 } else {
122 reply = new ObjectCacheReadReplyData(RBDSC_READ_REPLY,
123 req->seq, cache_path);
124 }
125 session->send(reply);
126 break;
127 }
128 default:
129 ldout(m_cct, 5) << "can't recongize request" << dendl;
130 ceph_assert(0);
131 }
132 }
133
134 } // namespace immutable_obj_cache
135 } // namespace ceph