Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / os / filestore / FDCache.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) 2013 Inktank Storage, Inc.
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_FDCACHE_H
16 #define CEPH_FDCACHE_H
17
18 #include <memory>
19 #include <errno.h>
20 #include <cstdio>
21 #include "common/hobject.h"
22 #include "common/Mutex.h"
23 #include "common/Cond.h"
24 #include "common/shared_cache.hpp"
25 #include "include/compat.h"
26 #include "include/intarith.h"
27
28 /**
29  * FD Cache
30  */
31 class FDCache : public md_config_obs_t {
32 public:
33   /**
34    * FD
35    *
36    * Wrapper for an fd.  Destructor closes the fd.
37    */
38   class FD {
39   public:
40     const int fd;
41     explicit FD(int _fd) : fd(_fd) {
42       assert(_fd >= 0);
43     }
44     int operator*() const {
45       return fd;
46     }
47     ~FD() {
48       VOID_TEMP_FAILURE_RETRY(::close(fd));
49     }
50   };
51
52 private:
53   CephContext *cct;
54   const int registry_shards;
55   SharedLRU<ghobject_t, FD> *registry;
56
57 public:
58   explicit FDCache(CephContext *cct) : cct(cct),
59   registry_shards(MAX(cct->_conf->filestore_fd_cache_shards, 1)) {
60     assert(cct);
61     cct->_conf->add_observer(this);
62     registry = new SharedLRU<ghobject_t, FD>[registry_shards];
63     for (int i = 0; i < registry_shards; ++i) {
64       registry[i].set_cct(cct);
65       registry[i].set_size(
66           MAX((cct->_conf->filestore_fd_cache_size / registry_shards), 1));
67     }
68   }
69   ~FDCache() override {
70     cct->_conf->remove_observer(this);
71     delete[] registry;
72   }
73   typedef ceph::shared_ptr<FD> FDRef;
74
75   FDRef lookup(const ghobject_t &hoid) {
76     int registry_id = hoid.hobj.get_hash() % registry_shards;
77     return registry[registry_id].lookup(hoid);
78   }
79
80   FDRef add(const ghobject_t &hoid, int fd, bool *existed) {
81     int registry_id = hoid.hobj.get_hash() % registry_shards;
82     return registry[registry_id].add(hoid, new FD(fd), existed);
83   }
84
85   /// clear cached fd for hoid, subsequent lookups will get an empty FD
86   void clear(const ghobject_t &hoid) {
87     int registry_id = hoid.hobj.get_hash() % registry_shards;
88     registry[registry_id].purge(hoid);
89   }
90
91   /// md_config_obs_t
92   const char** get_tracked_conf_keys() const override {
93     static const char* KEYS[] = {
94       "filestore_fd_cache_size",
95       NULL
96     };
97     return KEYS;
98   }
99   void handle_conf_change(const md_config_t *conf,
100                           const std::set<std::string> &changed) override {
101     if (changed.count("filestore_fd_cache_size")) {
102       for (int i = 0; i < registry_shards; ++i)
103         registry[i].set_size(
104               MAX((conf->filestore_fd_cache_size / registry_shards), 1));
105     }
106   }
107
108 };
109 typedef FDCache::FDRef FDRef;
110
111 #endif