root/trunk/server/memcached.h @ 627

Revision 627, 12.7 kB (checked in by plindner, 2 years ago)

update for prepend operation, thread safe version from Maxim replacing Filipe's implementation

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1/* -*- Mode: C; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/* $Id$ */
3
4#ifdef HAVE_CONFIG_H
5#include "config.h"
6#endif
7
8#include <sys/types.h>
9#include <sys/socket.h>
10#include <sys/time.h>
11#include <netinet/in.h>
12#include <event.h>
13
14#define DATA_BUFFER_SIZE 2048
15#define UDP_READ_BUFFER_SIZE 65536
16#define UDP_MAX_PAYLOAD_SIZE 1400
17#define UDP_HEADER_SIZE 8
18#define MAX_SENDBUF_SIZE (256 * 1024 * 1024)
19
20/** Initial size of list of items being returned by "get". */
21#define ITEM_LIST_INITIAL 200
22
23/** Initial size of the sendmsg() scatter/gather array. */
24#define IOV_LIST_INITIAL 400
25
26/** Initial number of sendmsg() argument structures to allocate. */
27#define MSG_LIST_INITIAL 10
28
29/** High water marks for buffer shrinking */
30#define READ_BUFFER_HIGHWAT 8192
31#define ITEM_LIST_HIGHWAT 400
32#define IOV_LIST_HIGHWAT 600
33#define MSG_LIST_HIGHWAT 100
34
35/* Get a consistent bool type */
36#if HAVE_STDBOOL_H
37# include <stdbool.h>
38#else
39  typedef enum {false = 0, true = 1} bool;
40#endif
41
42#if HAVE_STDINT_H
43# include <stdint.h>
44#else
45 typedef unsigned char             uint8_t;
46#endif
47
48/* unistd.h is here */
49#if HAVE_UNISTD_H
50# include <unistd.h>
51#endif
52
53/** Time relative to server start. Smaller than time_t on 64-bit systems. */
54typedef unsigned int rel_time_t;
55
56struct stats {
57    unsigned int  curr_items;
58    unsigned int  total_items;
59    uint64_t      curr_bytes;
60    unsigned int  curr_conns;
61    unsigned int  total_conns;
62    unsigned int  conn_structs;
63    uint64_t      get_cmds;
64    uint64_t      set_cmds;
65    uint64_t      get_hits;
66    uint64_t      get_misses;
67    uint64_t      evictions;
68    time_t        started;          /* when the process was started */
69    uint64_t      bytes_read;
70    uint64_t      bytes_written;
71};
72
73#define MAX_VERBOSITY_LEVEL 2
74
75struct settings {
76    size_t maxbytes;
77    int maxconns;
78    int port;
79    int udpport;
80    struct in_addr interf;
81    int verbose;
82    rel_time_t oldest_live; /* ignore existing items older than this */
83    bool managed;          /* if 1, a tracker manages virtual buckets */
84    int evict_to_free;
85    char *socketpath;   /* path to unix socket if using local socket */
86    double factor;          /* chunk size growth factor */
87    int chunk_size;
88    int num_threads;        /* number of libevent threads to run */
89    char prefix_delimiter;  /* character that marks a key prefix (for stats) */
90    int detail_enabled;     /* nonzero if we're collecting detailed stats */
91};
92
93extern struct stats stats;
94extern struct settings settings;
95
96#define ITEM_LINKED 1
97#define ITEM_DELETED 2
98
99/* temp */
100#define ITEM_SLABBED 4
101
102typedef struct _stritem {
103    struct _stritem *next;
104    struct _stritem *prev;
105    struct _stritem *h_next;    /* hash chain next */
106    rel_time_t      time;       /* least recent access */
107    rel_time_t      exptime;    /* expire time */
108    int             nbytes;     /* size of data */
109    unsigned short  refcount;
110    uint8_t         nsuffix;    /* length of flags-and-length string */
111    uint8_t         it_flags;   /* ITEM_* above */
112    uint8_t         slabs_clsid;/* which slab class we're in */
113    uint8_t         nkey;       /* key length, w/terminating null and padding */
114    void * end[];
115    /* then null-terminated key */
116    /* then " flags length\r\n" (no terminating null) */
117    /* then data with terminating \r\n (no terminating null; it's binary!) */
118} item;
119
120#define ITEM_key(item) ((char*)&((item)->end[0]))
121
122/* warning: don't use these macros with a function, as it evals its arg twice */
123#define ITEM_suffix(item) ((char*) &((item)->end[0]) + (item)->nkey + 1)
124#define ITEM_data(item) ((char*) &((item)->end[0]) + (item)->nkey + 1 + (item)->nsuffix)
125#define ITEM_ntotal(item) (sizeof(struct _stritem) + (item)->nkey + 1 + (item)->nsuffix + (item)->nbytes)
126
127enum conn_states {
128    conn_listening,  /** the socket which listens for connections */
129    conn_read,       /** reading in a command line */
130    conn_write,      /** writing out a simple response */
131    conn_nread,      /** reading in a fixed number of bytes */
132    conn_swallow,    /** swallowing unnecessary bytes w/o storing */
133    conn_closing,    /** closing this connection */
134    conn_mwrite      /** writing out many items sequentially */
135};
136
137#define NREAD_ADD 1
138#define NREAD_SET 2
139#define NREAD_REPLACE 3
140#define NREAD_APPEND 4
141#define NREAD_PREPEND 5
142
143typedef struct {
144    int    sfd;
145    int    state;
146    struct event event;
147    short  ev_flags;
148    short  which;   /** which events were just triggered */
149
150    char   *rbuf;   /** buffer to read commands into */
151    char   *rcurr;  /** but if we parsed some already, this is where we stopped */
152    int    rsize;   /** total allocated size of rbuf */
153    int    rbytes;  /** how much data, starting from rcur, do we have unparsed */
154
155    char   *wbuf;
156    char   *wcurr;
157    int    wsize;
158    int    wbytes;
159    int    write_and_go; /** which state to go into after finishing current write */
160    void   *write_and_free; /** free this memory after finishing writing */
161
162    char   *ritem;  /** when we read in an item's value, it goes here */
163    int    rlbytes;
164
165    /* data for the nread state */
166
167    /**
168     * item is used to hold an item structure created after reading the command
169     * line of set/add/replace commands, but before we finished reading the actual
170     * data. The data is read into ITEM_data(item) to avoid extra copying.
171     */
172
173    void   *item;     /* for commands set/add/replace  */
174    int    item_comm; /* which one is it: set/add/replace */
175
176    /* data for the swallow state */
177    int    sbytes;    /* how many bytes to swallow */
178
179    /* data for the mwrite state */
180    struct iovec *iov;
181    int    iovsize;   /* number of elements allocated in iov[] */
182    int    iovused;   /* number of elements used in iov[] */
183
184    struct msghdr *msglist;
185    int    msgsize;   /* number of elements allocated in msglist[] */
186    int    msgused;   /* number of elements used in msglist[] */
187    int    msgcurr;   /* element in msglist[] being transmitted now */
188    int    msgbytes;  /* number of bytes in current msg */
189
190    item   **ilist;   /* list of items to write out */
191    int    isize;
192    item   **icurr;
193    int    ileft;
194
195    /* data for UDP clients */
196    bool   udp;       /* is this is a UDP "connection" */
197    int    request_id; /* Incoming UDP request ID, if this is a UDP "connection" */
198    struct sockaddr request_addr; /* Who sent the most recent request */
199    socklen_t request_addr_size;
200    unsigned char *hdrbuf; /* udp packet headers */
201    int    hdrsize;   /* number of headers' worth of space is allocated */
202
203    int    binary;    /* are we in binary mode */
204    int    bucket;    /* bucket number for the next command, if running as
205                         a managed instance. -1 (_not_ 0) means invalid. */
206    int    gen;       /* generation requested for the bucket */
207} conn;
208
209/* number of virtual buckets for a managed instance */
210#define MAX_BUCKETS 32768
211
212/* current time of day (updated periodically) */
213extern volatile rel_time_t current_time;
214
215/* temporary hack */
216/* #define assert(x) if(!(x)) { printf("assert failure: %s\n", #x); pre_gdb(); }
217   void pre_gdb (); */
218
219/*
220 * Functions
221 */
222
223conn *do_conn_from_freelist();
224bool do_conn_add_to_freelist(conn *c);
225char *do_defer_delete(item *item, time_t exptime);
226void do_run_deferred_deletes(void);
227char *do_add_delta(item *item, const bool incr, const int64_t delta, char *buf);
228int do_store_item(item *item, int comm);
229conn *conn_new(const int sfd, const int init_state, const int event_flags, const int read_buffer_size, const bool is_udp, struct event_base *base);
230
231
232#include "stats.h"
233#include "slabs.h"
234#include "assoc.h"
235#include "items.h"
236
237
238/*
239 * In multithreaded mode, we wrap certain functions with lock management and
240 * replace the logic of some other functions. All wrapped functions have
241 * "mt_" and "do_" variants. In multithreaded mode, the plain version of a
242 * function is #define-d to the "mt_" variant, which often just grabs a
243 * lock and calls the "do_" function. In singlethreaded mode, the "do_"
244 * function is called directly.
245 *
246 * Functions such as the libevent-related calls that need to do cross-thread
247 * communication in multithreaded mode (rather than actually doing the work
248 * in the current thread) are called via "dispatch_" frontends, which are
249 * also #define-d to directly call the underlying code in singlethreaded mode.
250 */
251#ifdef USE_THREADS
252
253void thread_init(int nthreads, struct event_base *main_base);
254int  dispatch_event_add(int thread, conn *c);
255void dispatch_conn_new(int sfd, int init_state, int event_flags, int read_buffer_size, int is_udp);
256
257/* Lock wrappers for cache functions that are called from main loop. */
258char *mt_add_delta(item *item, const int incr, const int64_t delta, char *buf);
259void mt_assoc_move_next_bucket(void);
260conn *mt_conn_from_freelist(void);
261bool  mt_conn_add_to_freelist(conn *c);
262char *mt_defer_delete(item *it, time_t exptime);
263int   mt_is_listen_thread(void);
264item *mt_item_alloc(char *key, size_t nkey, int flags, rel_time_t exptime, int nbytes);
265char *mt_item_cachedump(const unsigned int slabs_clsid, const unsigned int limit, unsigned int *bytes);
266void  mt_item_flush_expired(void);
267item *mt_item_get_notedeleted(const char *key, const size_t nkey, bool *delete_locked);
268int   mt_item_link(item *it);
269void  mt_item_remove(item *it);
270int   mt_item_replace(item *it, item *new_it);
271char *mt_item_stats(int *bytes);
272char *mt_item_stats_sizes(int *bytes);
273void  mt_item_unlink(item *it);
274void  mt_item_update(item *it);
275void  mt_run_deferred_deletes(void);
276void *mt_slabs_alloc(size_t size);
277void  mt_slabs_free(void *ptr, size_t size);
278int   mt_slabs_reassign(unsigned char srcid, unsigned char dstid);
279char *mt_slabs_stats(int *buflen);
280void  mt_stats_lock(void);
281void  mt_stats_unlock(void);
282int   mt_store_item(item *item, int comm);
283
284
285# define add_delta(x,y,z,a)          mt_add_delta(x,y,z,a)
286# define assoc_move_next_bucket()    mt_assoc_move_next_bucket()
287# define conn_from_freelist()        mt_conn_from_freelist()
288# define conn_add_to_freelist(x)     mt_conn_add_to_freelist(x)
289# define defer_delete(x,y)           mt_defer_delete(x,y)
290# define is_listen_thread()          mt_is_listen_thread()
291# define item_alloc(x,y,z,a,b)       mt_item_alloc(x,y,z,a,b)
292# define item_cachedump(x,y,z)       mt_item_cachedump(x,y,z)
293# define item_flush_expired()        mt_item_flush_expired()
294# define item_get_notedeleted(x,y,z) mt_item_get_notedeleted(x,y,z)
295# define item_link(x)                mt_item_link(x)
296# define item_remove(x)              mt_item_remove(x)
297# define item_replace(x,y)           mt_item_replace(x,y)
298# define item_stats(x)               mt_item_stats(x)
299# define item_stats_sizes(x)         mt_item_stats_sizes(x)
300# define item_update(x)              mt_item_update(x)
301# define item_unlink(x)              mt_item_unlink(x)
302# define run_deferred_deletes()      mt_run_deferred_deletes()
303# define slabs_alloc(x)              mt_slabs_alloc(x)
304# define slabs_free(x,y)             mt_slabs_free(x,y)
305# define slabs_reassign(x,y)         mt_slabs_reassign(x,y)
306# define slabs_stats(x)              mt_slabs_stats(x)
307# define store_item(x,y)             mt_store_item(x,y)
308
309# define STATS_LOCK()                mt_stats_lock()
310# define STATS_UNLOCK()              mt_stats_unlock()
311
312#else /* !USE_THREADS */
313
314# define add_delta(x,y,z,a)          do_add_delta(x,y,z,a)
315# define assoc_move_next_bucket()    do_assoc_move_next_bucket()
316# define conn_from_freelist()        do_conn_from_freelist()
317# define conn_add_to_freelist(x)     do_conn_add_to_freelist(x)
318# define defer_delete(x,y)           do_defer_delete(x,y)
319# define dispatch_conn_new(x,y,z,a,b) conn_new(x,y,z,a,b,main_base)
320# define dispatch_event_add(t,c)     event_add(&(c)->event, 0)
321# define is_listen_thread()          1
322# define item_alloc(x,y,z,a,b)       do_item_alloc(x,y,z,a,b)
323# define item_cachedump(x,y,z)       do_item_cachedump(x,y,z)
324# define item_flush_expired()        do_item_flush_expired()
325# define item_get_notedeleted(x,y,z) do_item_get_notedeleted(x,y,z)
326# define item_link(x)                do_item_link(x)
327# define item_remove(x)              do_item_remove(x)
328# define item_replace(x,y)           do_item_replace(x,y)
329# define item_stats(x)               do_item_stats(x)
330# define item_stats_sizes(x)         do_item_stats_sizes(x)
331# define item_unlink(x)              do_item_unlink(x)
332# define item_update(x)              do_item_update(x)
333# define run_deferred_deletes()      do_run_deferred_deletes()
334# define slabs_alloc(x)              do_slabs_alloc(x)
335# define slabs_free(x,y)             do_slabs_free(x,y)
336# define slabs_reassign(x,y)         do_slabs_reassign(x,y)
337# define slabs_stats(x)              do_slabs_stats(x)
338# define store_item(x,y)             do_store_item(x,y)
339# define thread_init(x,y)            0
340
341# define STATS_LOCK()                /**/
342# define STATS_UNLOCK()              /**/
343
344#endif /* !USE_THREADS */
345
346
Note: See TracBrowser for help on using the browser.