Merge remote-tracking branch 'couchbase/3.0.x' into sherlock
[ep-engine.git] / tests / ep_test_apis.h
1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2012 Couchbase, Inc
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17
18 #ifndef TESTS_EP_TEST_APIS_H_
19 #define TESTS_EP_TEST_APIS_H_ 1
20
21 #include "config.h"
22
23 #include <memcached/engine.h>
24 #include <memcached/engine_testapp.h>
25
26 #include <map>
27 #include <string>
28
29 #include "ep-engine/command_ids.h"
30 #include "ext_meta_parser.h"
31 #include "item.h"
32
33 extern "C" bool abort_msg(const char *expr, const char *msg, int line);
34
35 template <typename T>
36 static void checkeqfn(T exp, T got, const char *msg, const char *file,
37                       const int linenum) {
38     if (exp != got) {
39         std::stringstream ss;
40         ss << "Expected `" << exp << "', got `" << got << "' - " << msg;
41         abort_msg(ss.str().c_str(), file, linenum);
42     }
43 }
44
45 #define checkeq(a, b, c) checkeqfn(a, b, c, __FILE__, __LINE__)
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51 bool add_response(const void *key, uint16_t keylen, const void *ext,
52                   uint8_t extlen, const void *body, uint32_t bodylen,
53                   uint8_t datatype, uint16_t status, uint64_t cas,
54                   const void *cookie);
55
56 void add_stats(const char *key, const uint16_t klen, const char *val,
57                const uint32_t vlen, const void *cookie);
58
59 ENGINE_ERROR_CODE vb_map_response(const void *cookie, const void *map,
60                                   size_t mapsize);
61
62 #ifdef __cplusplus
63 }
64 #endif
65
66 extern AtomicValue<protocol_binary_response_status> last_status;
67 extern std::string last_key;
68 extern std::string last_body;
69 extern bool dump_stats;
70 extern std::map<std::string, std::string> vals;
71 extern AtomicValue<uint64_t> last_cas;
72 extern AtomicValue<uint8_t> last_datatype;
73 extern uint64_t last_uuid;
74 extern uint64_t last_seqno;
75 extern bool last_deleted_flag;
76 extern AtomicValue<uint8_t> last_conflict_resolution_mode;
77 extern ItemMetaData last_meta;
78
79 extern uint8_t dcp_last_op;
80 extern uint8_t dcp_last_status;
81 extern uint8_t dcp_last_nru;
82 extern uint16_t dcp_last_vbucket;
83 extern uint32_t dcp_last_opaque;
84 extern uint32_t dcp_last_flags;
85 extern uint32_t dcp_last_stream_opaque;
86 extern uint32_t dcp_last_locktime;
87 extern uint32_t dcp_last_packet_size;
88 extern uint64_t dcp_last_cas;
89 extern uint64_t dcp_last_start_seqno;
90 extern uint64_t dcp_last_end_seqno;
91 extern uint64_t dcp_last_vbucket_uuid;
92 extern uint64_t dcp_last_high_seqno;
93 extern uint64_t dcp_last_byseqno;
94 extern uint64_t dcp_last_revseqno;
95 extern uint64_t dcp_last_snap_start_seqno;
96 extern uint64_t dcp_last_snap_end_seqno;
97 extern uint16_t dcp_last_nmeta;
98 extern void *dcp_last_meta;
99 extern std::string dcp_last_key;
100 extern vbucket_state_t dcp_last_vbucket_state;
101
102
103 void decayingSleep(useconds_t *sleepTime);
104
105
106 protocol_binary_request_header* createPacket(uint8_t opcode,
107                                              uint16_t vbid = 0,
108                                              uint64_t cas = 0,
109                                              const char *ext = NULL,
110                                              uint8_t extlen = 0,
111                                              const char *key = NULL,
112                                              uint32_t keylen = 0,
113                                              const char *val = NULL,
114                                              uint32_t vallen = 0,
115                                              uint8_t datatype = 0x00,
116                                              const char *meta = NULL,
117                                              uint16_t nmeta = 0);
118
119 // Basic Operations
120 ENGINE_ERROR_CODE del(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *key,
121                       uint64_t cas, uint16_t vbucket, const void* cookie = NULL);
122 void disable_traffic(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1);
123 void enable_traffic(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1);
124 void evict_key(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *key,
125                uint16_t vbucketId = 0, const char *msg = NULL,
126                bool expectError = false);
127 size_t estimateVBucketMove(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
128                            uint16_t vbid = 0, const char* tap_name = "");
129 void gat(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char* key,
130          uint16_t vb, uint32_t exp, bool quiet = false);
131 bool get_item_info(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, item_info *info,
132                    const char* key, uint16_t vb = 0);
133 bool get_key(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, item *i,
134              std::string &key);
135 void getl(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char* key, uint16_t vb,
136           uint32_t lock_timeout);
137 void get_replica(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char* key,
138                  uint16_t vb);
139 void observe(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
140              std::map<std::string, uint16_t> obskeys);
141 void observe_seqno(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, uint16_t vb_id ,
142                    uint64_t uuid);
143 protocol_binary_request_header* prepare_get_replica(ENGINE_HANDLE *h,
144                                                     ENGINE_HANDLE_V1 *h1,
145                                                     vbucket_state_t state,
146                                                     bool makeinvalidkey = false);
147 bool set_param(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, protocol_binary_engine_param_t paramtype,
148                const char *param, const char *val);
149 bool set_vbucket_state(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
150                        uint16_t vb, vbucket_state_t state);
151 bool get_all_vb_seqnos(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
152                        vbucket_state_t state, const void *cookie);
153 void verify_all_vb_seqnos(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
154                           uint16_t vb_start, uint16_t vb_end);
155 void start_persistence(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1);
156 void stop_persistence(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1);
157 ENGINE_ERROR_CODE store(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
158                         const void *cookie, ENGINE_STORE_OPERATION op,
159                         const char *key, const char *value, item **outitem,
160                         uint64_t casIn = 0, uint16_t vb = 0,
161                         uint32_t exp = 3600, uint8_t datatype = 0x00);
162 ENGINE_ERROR_CODE storeCasVb11(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
163                                const void *cookie, ENGINE_STORE_OPERATION op,
164                                const char *key, const char *value, size_t vlen,
165                                uint32_t flags, item **outitem, uint64_t casIn,
166                                uint16_t vb, uint32_t exp = 3600,
167                                uint8_t datatype = 0x00);
168 void touch(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char* key,
169            uint16_t vb, uint32_t exp);
170 void unl(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char* key,
171          uint16_t vb, uint64_t cas = 0);
172 ENGINE_ERROR_CODE verify_key(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
173                              const char* key, uint16_t vbucket = 0);
174 bool verify_vbucket_missing(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
175                             uint16_t vb);
176 bool verify_vbucket_state(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, uint16_t vb,
177                           vbucket_state_t expected, bool mute = false);
178
179 void sendDcpAck(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
180                 const void* cookie, protocol_binary_command opcode,
181                 protocol_binary_response_status status, uint32_t opaque);
182
183 // Checkpoint Operations
184 void createCheckpoint(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1);
185 void extendCheckpoint(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
186                       uint32_t checkpoint_num);
187 ENGINE_ERROR_CODE checkpointPersistence(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
188                                         uint64_t checkpoint_id, uint16_t vb);
189 ENGINE_ERROR_CODE seqnoPersistence(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
190                                    uint16_t vbucket, uint64_t seqno);
191
192 // Stats Operations
193 int get_int_stat(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *statname,
194                  const char *statkey = NULL);
195 float get_float_stat(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *statname,
196                      const char *statkey = NULL);
197 uint64_t get_ull_stat(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *statname,
198                       const char *statkey = NULL);
199 std::string get_str_stat(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
200                          const char *statname, const char *statkey = NULL);
201 void verify_curr_items(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, int exp,
202                        const char *msg);
203 void wait_for_stat_change(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
204                           const char *stat, int initial,
205                           const char *statkey = NULL,
206                           const time_t wait_time = 60);
207 void wait_for_stat_to_be(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *stat,
208                          int final, const char* stat_key = NULL,
209                          const time_t wait_time = 60);
210 void wait_for_stat_to_be_lte(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
211                              const char *stat, int final,
212                              const char* stat_key = NULL,
213                              const time_t max_wait_time_in_secs = 60);
214 void wait_for_stat_to_be_gte(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
215                              const char *stat, int final,
216                              const char* stat_key = NULL,
217                              const time_t wait_time = 60);
218 void wait_for_str_stat_to_be(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
219                              const char *stat, const char* final,
220                              const char* stat_key,
221                              const time_t wait_time = 60);
222 bool wait_for_warmup_complete(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1);
223 void wait_for_flusher_to_settle(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1);
224 void wait_for_persisted_value(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
225                               const char *key, const char *val,
226                               uint16_t vbucketId = 0);
227
228 void wait_for_memory_usage_below(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
229                                  int mem_threshold, const time_t wait_time = 60);
230
231 // Tap Operations
232 void changeVBFilter(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, std::string name,
233                     std::map<uint16_t, uint64_t> &filtermap);
234
235 // VBucket operations
236 void vbucketDelete(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, uint16_t vb,
237                    const char* args = NULL);
238
239 void compact_db(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
240                 const uint16_t vbid,
241                 const uint64_t purge_before_ts,
242                 const uint64_t purge_before_seq,
243                 const uint8_t  drop_deletes);
244
245 // XDCR Operations
246 void set_drift_counter_state(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1,
247                              int64_t initialDrift, uint8_t timeSync);
248 void add_with_meta(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *key,
249                    const size_t keylen, const char *val, const size_t vallen,
250                    const uint32_t vb, ItemMetaData *itemMeta,
251                    bool skipConflictResolution = false,
252                    uint8_t datatype = 0x00, bool includeExtMeta = false,
253                    int64_t adjusted_time = 0);
254 bool get_meta(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char* key,
255               bool reqExtMeta = false);
256 void del_with_meta(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *key,
257                    const size_t keylen, const uint32_t vb,
258                    ItemMetaData *itemMeta, uint64_t cas_for_delete = 0,
259                    bool skipConflictResolution = false,
260                    bool includeExtMeta = false,
261                    int64_t adjustedTime = 0, uint8_t conflictResMode = 0,
262                    const void *cookie = NULL);
263 void set_with_meta(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *key,
264                    const size_t keylen, const char *val, const size_t vallen,
265                    const uint32_t vb, ItemMetaData *itemMeta,
266                    uint64_t cas_for_set, bool skipConflictResolution = false,
267                    uint8_t datatype = 0x00, bool includeExtMeta = false,
268                    int64_t adjustedTime = 0, uint8_t conflictResMode = 0,
269                    const void *cookie = NULL);
270 void return_meta(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *key,
271                  const size_t keylen, const char *val, const size_t vallen,
272                  const uint32_t vb, const uint64_t cas, const uint32_t flags,
273                  const uint32_t exp, const uint32_t type,
274                  uint8_t datatype = 0x00, const void *cookie = NULL);
275 void set_ret_meta(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *key,
276                   const size_t keylen, const char *val, const size_t vallen,
277                   const uint32_t vb, const uint64_t cas = 0,
278                   const uint32_t flags = 0, const uint32_t exp = 0,
279                   uint8_t datatype = 0x00, const void *cookie = NULL);
280 void add_ret_meta(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *key,
281                   const size_t keylen, const char *val, const size_t vallen,
282                   const uint32_t vb, const uint64_t cas = 0,
283                   const uint32_t flags = 0, const uint32_t exp = 0,
284                   uint8_t datatype = 0x00, const void *cookie = NULL);
285 void del_ret_meta(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const char *key,
286                   const size_t keylen, const uint32_t vb,
287                   const uint64_t cas = 0, const void *cookie = NULL);
288
289 // DCP Operations
290 void dcp_step(ENGINE_HANDLE *h, ENGINE_HANDLE_V1 *h1, const void* cookie);
291
292 void set_degraded_mode(ENGINE_HANDLE *h,
293                        ENGINE_HANDLE_V1 *h1,
294                        const void* cookie,
295                        bool enable);
296
297 #endif  // TESTS_EP_TEST_APIS_H_