summaryrefslogtreecommitdiffstats
path: root/src/tools/immutable_object_cache/SimplePolicy.cc
blob: 3a7375ba97626acf78b6d66f33cbc188934d72d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab

#include "common/debug.h"
#include "SimplePolicy.h"

#define dout_context g_ceph_context
#define dout_subsys ceph_subsys_immutable_obj_cache
#undef dout_prefix
#define dout_prefix *_dout << "ceph::cache::SimplePolicy: " << this << " " \
                           << __func__ << ": "

namespace ceph {
namespace immutable_obj_cache {

SimplePolicy::SimplePolicy(CephContext *cct, uint64_t cache_size,
                           uint64_t max_inflight, double watermark)
  : cct(cct), m_watermark(watermark), m_max_inflight_ops(max_inflight),
    m_max_cache_size(cache_size) {

  ldout(cct, 20) << "max cache size= " << m_max_cache_size
                 << " ,watermark= " << m_watermark
                 << " ,max inflight ops= " << m_max_inflight_ops << dendl;

  m_cache_size = 0;

}

SimplePolicy::~SimplePolicy() {
  ldout(cct, 20) << dendl;

  for (auto it : m_cache_map) {
    Entry* entry = (it.second);
    delete entry;
  }
}

cache_status_t SimplePolicy::alloc_entry(std::string file_name) {
  ldout(cct, 20) << "alloc entry for: " << file_name << dendl;

  std::unique_lock wlocker{m_cache_map_lock};

  // cache hit when promoting
  if (m_cache_map.find(file_name) != m_cache_map.end()) {
    ldout(cct, 20) << "object is under promoting: " << file_name << dendl;
    return OBJ_CACHE_SKIP;
  }

  if ((m_cache_size < m_max_cache_size) &&
      (inflight_ops < m_max_inflight_ops)) {
    Entry* entry = new Entry();
    ceph_assert(entry != nullptr);
    m_cache_map[file_name] = entry;
    wlocker.unlock();
    update_status(file_name, OBJ_CACHE_SKIP);
    return OBJ_CACHE_NONE;  // start promotion request
  }

  // if there's no free entry, return skip to read from rados
  return OBJ_CACHE_SKIP;
}

cache_status_t SimplePolicy::lookup_object(std::string file_name) {
  ldout(cct, 20) << "lookup: " << file_name << dendl;

  std::shared_lock rlocker{m_cache_map_lock};

  auto entry_it = m_cache_map.find(file_name);
  // simply promote on first lookup
  if (entry_it == m_cache_map.end()) {
      rlocker.unlock();
      return alloc_entry(file_name);
  }

  Entry* entry = entry_it->second;

  if (entry->status == OBJ_CACHE_PROMOTED || entry->status == OBJ_CACHE_DNE) {
    // bump pos in lru on hit
    m_promoted_lru.lru_touch(entry);
  }

  return entry->status;
}

void SimplePolicy::update_status(std::string file_name,
                                 cache_status_t new_status, uint64_t size) {
  ldout(cct, 20) << "update status for: " << file_name
                 << " new status = " << new_status << dendl;

  std::unique_lock locker{m_cache_map_lock};

  auto entry_it = m_cache_map.find(file_name);
  if (entry_it == m_cache_map.end()) {
    return;
  }

  ceph_assert(entry_it != m_cache_map.end());
  Entry* entry = entry_it->second;

  // to promote
  if (entry->status == OBJ_CACHE_NONE && new_status== OBJ_CACHE_SKIP) {
    entry->status = new_status;
    entry->file_name = file_name;
    inflight_ops++;
    return;
  }

  // promoting done
  if (entry->status == OBJ_CACHE_SKIP && (new_status== OBJ_CACHE_PROMOTED ||
                                          new_status== OBJ_CACHE_DNE)) {
    m_promoted_lru.lru_insert_top(entry);
    entry->status = new_status;
    entry->size = size;
    m_cache_size += entry->size;
    inflight_ops--;
    return;
  }

  // promoting failed
  if (entry->status == OBJ_CACHE_SKIP && new_status== OBJ_CACHE_NONE) {
    // mark this entry as free
    entry->file_name = "";
    entry->status = new_status;

    m_cache_map.erase(entry_it);
    inflight_ops--;
    delete entry;
    return;
  }

  // to evict
  if ((entry->status == OBJ_CACHE_PROMOTED || entry->status == OBJ_CACHE_DNE) &&
      new_status== OBJ_CACHE_NONE) {
    // mark this entry as free
    uint64_t size = entry->size;
    entry->file_name = "";
    entry->size = 0;
    entry->status = new_status;

    m_promoted_lru.lru_remove(entry);
    m_cache_map.erase(entry_it);
    m_cache_size -= size;
    delete entry;
    return;
  }
}

int SimplePolicy::evict_entry(std::string file_name) {
  ldout(cct, 20) << "to evict: " << file_name << dendl;

  update_status(file_name, OBJ_CACHE_NONE);

  return 0;
}

cache_status_t SimplePolicy::get_status(std::string file_name) {
  ldout(cct, 20) << file_name << dendl;

  std::shared_lock locker{m_cache_map_lock};
  auto entry_it = m_cache_map.find(file_name);
  if (entry_it == m_cache_map.end()) {
    return OBJ_CACHE_NONE;
  }

  return entry_it->second->status;
}

void SimplePolicy::get_evict_list(std::list<std::string>* obj_list) {
  ldout(cct, 20) << dendl;

  std::unique_lock locker{m_cache_map_lock};
  // check free ratio, pop entries from LRU
  if ((double)m_cache_size > m_max_cache_size * m_watermark) {
    // TODO(dehao): make this configurable
    int evict_num = m_cache_map.size() * 0.1;
    for (int i = 0; i < evict_num; i++) {
      Entry* entry = reinterpret_cast<Entry*>(m_promoted_lru.lru_expire());
      if (entry == nullptr) {
        continue;
      }
      std::string file_name = entry->file_name;
      obj_list->push_back(file_name);
    }
  }
}

// for unit test
uint64_t SimplePolicy::get_free_size() {
  return m_max_cache_size - m_cache_size;
}

uint64_t SimplePolicy::get_promoting_entry_num() {
  uint64_t index = 0;
  std::shared_lock rlocker{m_cache_map_lock};
  for (auto it : m_cache_map) {
    if (it.second->status == OBJ_CACHE_SKIP) {
      index++;
    }
  }
  return index;
}

uint64_t SimplePolicy::get_promoted_entry_num() {
  return m_promoted_lru.lru_get_size();
}

std::string SimplePolicy::get_evict_entry() {
  Entry* entry = reinterpret_cast<Entry*>(m_promoted_lru.lru_get_next_expire());
  if (entry == nullptr) {
    return "";
  }
  return entry->file_name;
}

}  // namespace immutable_obj_cache
}  // namespace ceph