forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_i_lib_cache_node.h
More file actions
219 lines (203 loc) · 7.52 KB
/
Copy pathob_i_lib_cache_node.h
File metadata and controls
219 lines (203 loc) · 7.52 KB
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
217
218
219
/**
* Copyright (c) 2021 OceanBase
* OceanBase CE is licensed under Mulan PubL v2.
* You can use this software according to the terms and conditions of the Mulan PubL v2.
* You may obtain a copy of Mulan PubL v2 at:
* http://license.coscl.org.cn/MulanPubL-2.0
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PubL v2 for more details.
*/
#ifndef OCEANBASE_SQL_PLAN_CACHE_OB_I_LIB_CACHE_NODE_
#define OCEANBASE_SQL_PLAN_CACHE_OB_I_LIB_CACHE_NODE_
#include "lib/list/ob_dlist.h"
#include "lib/lock/ob_spin_rwlock.h"
#include "lib/lock/ob_tc_rwlock.h"
#include "lib/stat/ob_latch_define.h"
#include "sql/plan_cache/ob_lib_cache_register.h"
#include "sql/plan_cache/ob_plan_cache_util.h"
namespace oceanbase
{
namespace common
{
class ObIAllocator;
}
namespace sql
{
class ObPlanCache;
class ObILibCacheKey;
class ObILibCacheCtx;
class ObILibCacheObject;
struct StmtStat
{
int64_t memory_used_;
int64_t last_active_timestamp_; // used now
int64_t execute_average_time_;
int64_t execute_slowest_time_;
int64_t execute_slowest_timestamp_;
int64_t execute_count_; // used now
int64_t execute_slow_count_;
int64_t ps_count_;
bool to_delete_;
StmtStat()
: memory_used_(0),
last_active_timestamp_(0),
execute_average_time_(0),
execute_slowest_time_(0),
execute_slowest_timestamp_(0),
execute_count_(0),
execute_slow_count_(0),
ps_count_(0),
to_delete_(false)
{
}
void reset()
{
memory_used_ = 0;
last_active_timestamp_ = 0;
execute_average_time_ = 0;
execute_slowest_time_ = 0;
execute_slowest_timestamp_ = 0;
execute_count_ = 0;
execute_slow_count_ = 0;
ps_count_ = 0;
to_delete_ = false;
}
double weight()
{
int64_t time_interval = common::ObTimeUtility::current_time() - last_active_timestamp_;
double weight = common::OB_PC_WEIGHT_NUMERATOR / static_cast<double>(time_interval);
return weight;
}
TO_STRING_KV(K_(memory_used),
K_(last_active_timestamp),
K_(execute_average_time),
K_(execute_slowest_time),
K_(execute_slowest_timestamp),
K_(execute_count),
K_(execute_slow_count),
K_(ps_count),
K_(to_delete));
};
typedef ObList<ObILibCacheObject*, ObIAllocator> CacheObjList;
// The abstract interface class of library cache node, each object in the ObLibCacheNameSpace
// enum structure needs to inherit from this interface and implement its own implementation class
class ObILibCacheNode
{
friend class ObLCNodeFactory;
public:
ObILibCacheNode(ObPlanCache *lib_cache, lib::MemoryContext &mem_context)
: mem_context_(mem_context),
allocator_(mem_context->get_safe_arena_allocator()),
rwlock_(),
ref_count_(0),
lib_cache_(lib_cache),
co_list_lock_(common::ObLatchIds::PLAN_SET_LOCK),
co_list_(allocator_)
{
lock_timeout_ts_ = GCONF.large_query_threshold;
}
virtual ~ObILibCacheNode();
/**
* @brief initialize library cache node
* @param cache_obj[in], object that need to be cached
* @return if success, return OB_SUCCESS, otherwise, return errno
*/
virtual int init(ObILibCacheCtx &ctx, const ObILibCacheObject *cache_obj);
/**
* @brief decrease the reference count of all cache objects in the library cache value by one
*/
void free_cache_obj_array();
/**
* @brief remove all plan stat from all cache objects in the library cache node
*/
int remove_all_plan_stat();
/**
* @brief remove cache obj from cache_obj_list_
* @param obj_id[in], obj id to remove
* @return if success, return OB_SUCCESS, otherwise, return errno
*/
int remove_cache_obj_entry(const ObCacheObjID obj_id);
/**
* @brief get cache object from library cache
* @param ctx[in], library cache context
* @return if success, return OB_SUCCESS, otherwise, return errno
*/
int get_cache_obj(ObILibCacheCtx &ctx, ObILibCacheKey *key, ObILibCacheObject *&cache_obj);
/**
* @brief add cache object to library cache
* @param ctx[in], library cache context
* @return if success, return OB_SUCCESS, otherwise, return errno
*/
int add_cache_obj(ObILibCacheCtx &ctx, ObILibCacheKey *key, ObILibCacheObject *cache_obj);
/**
* @brief erase cache object from library cache
* @param ctx[in], library cache context
* @return if success, return OB_SUCCESS, otherwise, return errno
*/
//int erase_cache_obj(ObILibCacheCtx &context, ObILibCacheObject *cache_obj);
virtual int lock(bool is_rdlock);
virtual int update_node_stat(ObILibCacheCtx &ctx);
StmtStat *get_node_stat() { return &node_stat_; }
int unlock() { return rwlock_.unlock(); }
int64_t inc_ref_count(const CacheRefHandleID ref_handle);
int64_t dec_ref_count(const CacheRefHandleID ref_handle);
int64_t get_ref_count() const { return ATOMIC_LOAD(&ref_count_); }
common::ObIAllocator *get_allocator() { return &allocator_; }
common::ObIAllocator &get_allocator_ref() { return allocator_; }
lib::MemoryContext &get_mem_context() { return mem_context_; }
int64_t get_mem_size();
ObPlanCache *get_lib_cache() const { return lib_cache_; }
VIRTUAL_TO_STRING_KV(K_(ref_count), K_(lock_timeout_ts));
protected:
void set_lock_timeout_threshold(int64_t threshold)
{
lock_timeout_ts_ = threshold;
}
/**
* @brief called by get_cache_obj(), each object in the ObLibCacheNameSpace enumeration structure
* needs to inherit this interface and implement its own inner get implementation
* @param ctx[in], library cache context
* @return if success, return OB_SUCCESS, otherwise, return errno
*/
virtual int inner_get_cache_obj(ObILibCacheCtx &ctx,
ObILibCacheKey *key,
ObILibCacheObject *&cache_obj) = 0;
/**
* @brief called by add_cache_obj(), each object in the ObLibCacheNameSpace enumeration structure
* needs to inherit this interface and implement its own inner add implementation
* @param ctx[in], library cache context
* @return if success, return OB_SUCCESS, otherwise, return errno
*/
virtual int inner_add_cache_obj(ObILibCacheCtx &ctx,
ObILibCacheKey *key,
ObILibCacheObject *cache_obj) = 0;
/**
* @brief called by erase_cache_obj(), each object in the ObLibCacheNameSpace enumeration structure
* needs to inherit this interface and implement its own inner erase implementation
* @param ctx[in], library cache context
* @return if success, return OB_SUCCESS, otherwise, return errno
*/
//int inner_erase_cache_obj(ObILibCacheCtx &ctx, ObILibCacheObject *cache_obj) = 0;
/**
* @brief called to do something before cache evicted
* @return if success, return OB_SUCCESS, otherwise, return errno
*/
virtual int before_cache_evicted();
protected:
lib::MemoryContext mem_context_;
// Note: all memory allocations in ObILibCacheNode can only use allocator_, when the ObILibCacheNode
// node is destructed, all memory allocated by allocator_ will be released
common::ObIAllocator &allocator_;
common::TCRWLock rwlock_;
int64_t ref_count_;
int64_t lock_timeout_ts_;
StmtStat node_stat_;
ObPlanCache *lib_cache_;
common::SpinRWLock co_list_lock_;
CacheObjList co_list_;
};
} // namespace common
} // namespace oceanbase
#endif // OCEANBASE_SQL_PLAN_CACHE_OB_I_LIB_CACHE_NODE_