MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
memcached.h
Go to the documentation of this file.
1 /* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 #ifndef MEMCACHED_H
3 #define MEMCACHED_H
4 
9 #include <event.h>
10 #include <pthread.h>
11 #include <config_static.h>
12 
13 #include <memcached/protocol_binary.h>
14 #include <memcached/engine.h>
15 #include <memcached/extension.h>
16 
17 #include "cache.h"
18 #include "topkeys.h"
19 
20 #include "sasl_defs.h"
21 
23 #define KEY_MAX_LENGTH 250
24 
26 #define INCR_MAX_STORAGE_LEN 24
27 
28 #define DATA_BUFFER_SIZE 2048
29 #define UDP_READ_BUFFER_SIZE 65536
30 #define UDP_MAX_PAYLOAD_SIZE 1400
31 #define UDP_HEADER_SIZE 8
32 #define MAX_SENDBUF_SIZE (256 * 1024 * 1024)
33 /* I'm told the max length of a 64-bit num converted to string is 20 bytes.
34  * Plus a few for spaces, \r\n, \0 */
35 #define SUFFIX_SIZE 24
36 
38 #define ITEM_LIST_INITIAL 200
39 
41 #define SUFFIX_LIST_INITIAL 20
42 
44 #define IOV_LIST_INITIAL 400
45 
47 #define MSG_LIST_INITIAL 10
48 
50 #define READ_BUFFER_HIGHWAT 8192
51 #define ITEM_LIST_HIGHWAT 400
52 #define IOV_LIST_HIGHWAT 600
53 #define MSG_LIST_HIGHWAT 100
54 
55 /* Binary protocol stuff */
56 #define MIN_BIN_PKT_LENGTH 16
57 #define BIN_PKT_HDR_WORDS (MIN_BIN_PKT_LENGTH/sizeof(uint32_t))
58 
59 /* Slab sizing definitions. */
60 #define POWER_SMALLEST 1
61 #define POWER_LARGEST 200
62 #define CHUNK_ALIGN_BYTES 8
63 #define DONT_PREALLOC_SLABS
64 #define MAX_NUMBER_OF_SLAB_CLASSES (POWER_LARGEST + 1)
65 
66 
67 #define STAT_KEY_LEN 128
68 #define STAT_VAL_LEN 128
69 
70 #define DEFAULT_REQS_PER_EVENT 20
71 #define DEFAULT_REQS_PER_TAP_EVENT 50
72 
74 #define APPEND_STAT(name, fmt, val) \
75  append_stat(name, add_stats, c, fmt, val);
76 
79 #define APPEND_NUM_FMT_STAT(name_fmt, num, name, fmt, val) \
80  klen = snprintf(key_str, STAT_KEY_LEN, name_fmt, num, name); \
81  vlen = snprintf(val_str, STAT_VAL_LEN, fmt, val); \
82  add_stats(key_str, klen, val_str, vlen, c);
83 
85 #define APPEND_NUM_STAT(num, name, fmt, val) \
86  APPEND_NUM_FMT_STAT("%d:%s", num, name, fmt, val)
87 
88 enum bin_substates {
89  bin_no_state,
90  bin_reading_set_header,
91  bin_reading_cas_header,
92  bin_read_set_value,
93  bin_reading_get_key,
94  bin_reading_stat,
95  bin_reading_del_header,
96  bin_reading_incr_header,
97  bin_read_flush_exptime,
98  bin_reading_sasl_auth,
99  bin_reading_sasl_auth_data,
100  bin_reading_packet
101 };
102 
103 enum protocol {
104  ascii_prot = 3, /* arbitrary value. */
105  binary_prot,
106  negotiating_prot /* Discovering the protocol */
107 };
108 
109 enum network_transport {
110  local_transport, /* Unix sockets*/
111  tcp_transport,
112  udp_transport
113 };
114 
115 #define IS_UDP(x) (x == udp_transport)
116 
118 struct slab_stats {
119  uint64_t cmd_set;
120  uint64_t get_hits;
121  uint64_t delete_hits;
122  uint64_t cas_hits;
123  uint64_t cas_badval;
124 };
125 
129 struct thread_stats {
130  pthread_mutex_t mutex;
131  uint64_t cmd_get;
132  uint64_t get_misses;
133  uint64_t delete_misses;
134  uint64_t incr_misses;
135  uint64_t decr_misses;
136  uint64_t incr_hits;
137  uint64_t decr_hits;
138  uint64_t cas_misses;
139  uint64_t bytes_read;
140  uint64_t bytes_written;
141  uint64_t cmd_flush;
142  uint64_t conn_yields; /* # of yields for connections (-R option)*/
143  uint64_t auth_cmds;
144  uint64_t auth_errors;
145  struct slab_stats slab_stats[MAX_NUMBER_OF_SLAB_CLASSES];
146 };
147 
148 
154  struct thread_stats thread_stats[];
155 };
156 
160 struct stats {
161  pthread_mutex_t mutex;
162  unsigned int daemon_conns; /* conns used by the server */
163  unsigned int curr_conns;
164  unsigned int total_conns;
165  unsigned int conn_structs;
166  time_t started; /* when the process was started */
167  uint64_t rejected_conns; /* number of times I reject a client */
168 };
169 
170 #define MAX_VERBOSITY_LEVEL 2
171 
172 /* When adding a setting, be sure to update process_stat_settings */
176 struct settings {
177  size_t maxbytes;
178  int maxconns;
179  int port;
180  int udpport;
181  char *inter;
182  int verbose;
183  rel_time_t oldest_live; /* ignore existing items older than this */
184  int evict_to_free;
185  char *socketpath; /* path to unix socket if using local socket */
186  int access; /* access mask (a la chmod) for unix domain socket */
187  double factor; /* chunk size growth factor */
188  int chunk_size;
189  int num_threads; /* number of worker (without dispatcher) libevent threads to run */
190  int num_threads_per_udp; /* number of worker threads serving each udp socket */
191  char prefix_delimiter; /* character that marks a key prefix (for stats) */
192  int detail_enabled; /* nonzero if we're collecting detailed stats */
193  bool allow_detailed; /* detailed stats commands are allowed */
194  int reqs_per_event; /* Maximum number of io to process on each
195  io-event. */
196  int reqs_per_tap_event; /* Maximum number of tap io to process on each
197  io-event. */
198  bool use_cas;
199  enum protocol binding_protocol;
200  int backlog;
201  size_t item_size_max; /* Maximum item size, and upper end for slabs */
202  bool sasl; /* SASL on/off */
203  bool require_sasl; /* require SASL auth */
204  int topkeys; /* Number of top keys to track */
205  union {
206  ENGINE_HANDLE *v0;
207  ENGINE_HANDLE_V1 *v1;
208  } engine;
209  struct {
213  } extensions;
214 };
215 
217  EVENT_CALLBACK cb;
218  const void *cb_data;
219  struct engine_event_handler *next;
220 };
221 
222 extern struct stats stats;
223 extern struct settings settings;
224 
225 enum thread_type {
226  GENERAL = 11,
227  TAP = 13,
228  DISPATCHER = 15
229 };
230 
231 typedef struct {
232  pthread_t thread_id; /* unique ID of this thread */
233  struct event_base *base; /* libevent handle this thread uses */
234  struct event notify_event; /* listen event for notify pipe */
235  SOCKET notify[2]; /* notification pipes */
236  struct conn_queue *new_conn_queue; /* queue of new connections to handle */
237  cache_t *suffix_cache; /* suffix cache */
238  pthread_mutex_t mutex; /* Mutex to lock protect access to the pending_io */
239  bool is_locked;
240  struct conn *pending_io; /* List of connection with pending async io ops */
241  int index; /* index of this thread in the threads array */
242  enum thread_type type; /* Type of IO this thread processes */
243 
244  rel_time_t last_checked;
245  struct conn *pending_close; /* list of connections close at a later time */
247 
248 #define LOCK_THREAD(t) \
249  if (pthread_mutex_lock(&t->mutex) != 0) { \
250  abort(); \
251  } \
252  assert(t->is_locked == false); \
253  t->is_locked = true;
254 
255 #define UNLOCK_THREAD(t) \
256  assert(t->is_locked == true); \
257  t->is_locked = false; \
258  if (pthread_mutex_unlock(&t->mutex) != 0) { \
259  abort(); \
260  }
261 
262 extern void notify_thread(LIBEVENT_THREAD *thread);
263 extern void notify_dispatcher(void);
264 extern bool create_notification_pipe(LIBEVENT_THREAD *me);
265 
266 extern LIBEVENT_THREAD* tap_thread;
267 
268 typedef struct conn conn;
269 typedef bool (*STATE_FUNC)(conn *);
270 
274 struct conn {
275  SOCKET sfd;
276  int nevents;
278  STATE_FUNC state;
279  enum bin_substates substate;
280 #ifdef DEBUG
281  bool registered_in_libevent;
282 #endif
283  struct event event;
284  short ev_flags;
285  short which;
287  char *rbuf;
288  char *rcurr;
289  uint32_t rsize;
290  uint32_t rbytes;
292  char *wbuf;
293  char *wcurr;
294  uint32_t wsize;
295  uint32_t wbytes;
297  STATE_FUNC write_and_go;
298  void *write_and_free;
300  char *ritem;
301  uint32_t rlbytes;
302 
303  /* data for the nread state */
304 
311  void *item; /* for commands set/add/replace */
312  ENGINE_STORE_OPERATION store_op; /* which one is it: set/add/replace */
313 
314 
315  /* data for the swallow state */
316  int sbytes; /* how many bytes to swallow */
317 
318  /* data for the mwrite state */
319  struct iovec *iov;
320  int iovsize; /* number of elements allocated in iov[] */
321  int iovused; /* number of elements used in iov[] */
322 
323  struct msghdr *msglist;
324  int msgsize; /* number of elements allocated in msglist[] */
325  int msgused; /* number of elements used in msglist[] */
326  int msgcurr; /* element in msglist[] being transmitted now */
327  int msgbytes; /* number of bytes in current msg */
328 
329  item **ilist; /* list of items to write out */
330  int isize;
331  item **icurr;
332  int ileft;
333 
334  char **suffixlist;
335  int suffixsize;
336  char **suffixcurr;
337  int suffixleft;
338 
339  enum protocol protocol; /* which protocol this connection speaks */
340  enum network_transport transport; /* what transport is used by this connection */
341 
342  /* data for UDP clients */
343  int request_id; /* Incoming UDP request ID, if this is a UDP "connection" */
344  struct sockaddr_storage request_addr; /* Who sent the most recent request */
345  socklen_t request_addr_size;
346  unsigned char *hdrbuf; /* udp packet headers */
347  int hdrsize; /* number of headers' worth of space is allocated */
348 
349  bool noreply; /* True if the reply should not be sent. */
350  /* current stats command */
351 
352  uint8_t refcount; /* number of references to the object */
353 
354  struct {
355  char *buffer;
356  size_t size;
357  size_t offset;
358  } dynamic_buffer;
359 
360  void *engine_storage;
361 
364 
365 
366  /* Binary protocol stuff */
367  /* This is where the binary header goes */
368  protocol_binary_request_header binary_header;
369  uint64_t cas; /* the cas to return */
370  short cmd; /* current command being processed */
371  int opaque;
372  int keylen;
373 
374  int list_state; /* bitmask of list state data for this connection */
375  conn *next; /* Used for generating a list of conn structures */
376  LIBEVENT_THREAD *thread; /* Pointer to the thread object serving this connection */
377 
378  ENGINE_ERROR_CODE aiostat;
379  bool ewouldblock;
380  bool tap_nack_mode;
381  TAP_ITERATOR tap_iterator;
382 };
383 
384 /* States for the connection list_state */
385 #define LIST_STATE_PROCESSING 1
386 #define LIST_STATE_REQ_PENDING_IO 2
387 #define LIST_STATE_REQ_PENDING_CLOSE 4
388 
389 /*
390  * Functions
391  */
392 conn *conn_new(const SOCKET sfd, STATE_FUNC init_state, const int event_flags,
393  const int read_buffer_size, enum network_transport transport,
394  struct event_base *base, struct timeval *timeout);
395 #ifndef WIN32
396 extern int daemonize(int nochdir, int noclose);
397 #endif
398 
399 #include "stats.h"
400 #include "trace.h"
401 #include "hash.h"
402 #include <memcached/util.h>
403 
404 /*
405  * Functions to add / update the connection to libevent
406  */
407 bool register_event(conn *c, struct timeval *timeout);
408 bool unregister_event(conn *c);
409 bool update_event(conn *c, const int new_flags);
410 
411 /*
412  * Functions such as the libevent-related calls that need to do cross-thread
413  * communication in multithreaded mode (rather than actually doing the work
414  * in the current thread) are called via "dispatch_" frontends, which are
415  * also #define-d to directly call the underlying code in singlethreaded mode.
416  */
417 
418 void thread_init(int nthreads, struct event_base *main_base,
419  void (*dispatcher_callback)(int, short, void *));
420 void threads_shutdown(void);
421 
422 int dispatch_event_add(int thread, conn *c);
423 void dispatch_conn_new(SOCKET sfd, STATE_FUNC init_state, int event_flags,
424  int read_buffer_size, enum network_transport transport);
425 
426 /* Lock wrappers for cache functions that are called from main loop. */
427 void accept_new_conns(const bool do_accept);
428 conn *conn_from_freelist(void);
429 bool conn_add_to_freelist(conn *c);
430 int is_listen_thread(void);
431 
432 void STATS_LOCK(void);
433 void STATS_UNLOCK(void);
434 void threadlocal_stats_clear(struct thread_stats *stats);
435 void threadlocal_stats_reset(struct thread_stats *thread_stats);
436 void threadlocal_stats_aggregate(struct thread_stats *thread_stats, struct thread_stats *stats);
437 void slab_stats_aggregate(struct thread_stats *stats, struct slab_stats *out);
438 
439 /* Stat processing functions */
440 void append_stat(const char *name, ADD_STAT add_stats, conn *c,
441  const char *fmt, ...);
442 
443 void notify_io_complete(const void *cookie, ENGINE_ERROR_CODE status);
444 void conn_set_state(conn *c, STATE_FUNC state);
445 const char *state_text(STATE_FUNC state);
446 void safe_close(SOCKET sfd);
447 
448 
449 // Number of times this connection is in the given pending list
450 int number_of_pending(conn *c, conn *pending);
451 bool has_cycle(conn *c);
452 bool list_contains(conn *h, conn *n);
453 conn *list_remove(conn *h, conn *n);
454 size_t list_to_array(conn **dest, size_t max_items, conn **l);
455 void enlist_conn(conn *c, conn **list);
456 void finalize_list(conn **list, size_t items);
457 bool set_socket_nonblocking(SOCKET sfd);
458 
459 void conn_close(conn *c);
460 
461 
462 #if HAVE_DROP_PRIVILEGES
463 extern void drop_privileges(void);
464 #else
465 #define drop_privileges()
466 #endif
467 
468 /* connection state machine */
469 bool conn_listening(conn *c);
470 bool conn_new_cmd(conn *c);
471 bool conn_waiting(conn *c);
472 bool conn_read(conn *c);
473 bool conn_parse_cmd(conn *c);
474 bool conn_write(conn *c);
475 bool conn_nread(conn *c);
476 bool conn_swallow(conn *c);
477 bool conn_pending_close(conn *c);
478 bool conn_immediate_close(conn *c);
479 bool conn_closing(conn *c);
480 bool conn_mwrite(conn *c);
481 bool conn_ship_log(conn *c);
482 bool conn_add_tap_client(conn *c);
483 bool conn_setup_tap_stream(conn *c);
484 
485 /* If supported, give compiler hints for branch prediction. */
486 #if !defined(__GNUC__) || (__GNUC__ == 2 && __GNUC_MINOR__ < 96)
487 #define __builtin_expect(x, expected_value) (x)
488 #endif
489 
490 #define likely(x) __builtin_expect((x),1)
491 #define unlikely(x) __builtin_expect((x),0)
492 #endif