deb4aed42ea82a718ff7d0df6c662a599432f0a0
[awesomized/libmemcached] / libmemcached / storage.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary: Storage related functions, aka set, replace,..
9 *
10 */
11
12 #include "common.h"
13
14 typedef enum {
15 SET_OP,
16 REPLACE_OP,
17 ADD_OP,
18 PREPEND_OP,
19 APPEND_OP,
20 CAS_OP,
21 } memcached_storage_action_t;
22
23 /* Inline this */
24 static inline const char *storage_op_string(memcached_storage_action_t verb)
25 {
26 switch (verb)
27 {
28 case SET_OP:
29 return "set ";
30 case REPLACE_OP:
31 return "replace ";
32 case ADD_OP:
33 return "add ";
34 case PREPEND_OP:
35 return "prepend ";
36 case APPEND_OP:
37 return "append ";
38 case CAS_OP:
39 return "cas ";
40 default:
41 return "tosserror"; /* This is impossible, fixes issue for compiler warning in VisualStudio */
42 }
43
44 /* NOTREACHED */
45 }
46
47 static memcached_return_t memcached_send_binary(memcached_st *ptr,
48 memcached_server_write_instance_st server,
49 uint32_t server_key,
50 const char *key,
51 size_t key_length,
52 const char *value,
53 size_t value_length,
54 time_t expiration,
55 uint32_t flags,
56 uint64_t cas,
57 memcached_storage_action_t verb);
58
59 static inline memcached_return_t memcached_send(memcached_st *ptr,
60 const char *master_key, size_t master_key_length,
61 const char *key, size_t key_length,
62 const char *value, size_t value_length,
63 time_t expiration,
64 uint32_t flags,
65 uint64_t cas,
66 memcached_storage_action_t verb)
67 {
68 bool to_write;
69 size_t write_length;
70 memcached_return_t rc;
71 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
72 uint32_t server_key;
73 memcached_server_write_instance_st instance;
74
75 WATCHPOINT_ASSERT(!(value == NULL && value_length > 0));
76
77 rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol);
78 unlikely (rc != MEMCACHED_SUCCESS)
79 return rc;
80
81 unlikely (memcached_server_count(ptr) == 0)
82 return MEMCACHED_NO_SERVERS;
83
84 if (ptr->flags.verify_key && (memcached_key_test((const char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
85 return MEMCACHED_BAD_KEY_PROVIDED;
86
87 server_key= memcached_generate_hash_with_redistribution(ptr, master_key, master_key_length);
88 instance= memcached_server_instance_fetch(ptr, server_key);
89
90 WATCHPOINT_SET(instance->io_wait_count.read= 0);
91 WATCHPOINT_SET(instance->io_wait_count.write= 0);
92
93 if (ptr->flags.binary_protocol)
94 {
95 rc= memcached_send_binary(ptr, instance, server_key,
96 key, key_length,
97 value, value_length, expiration,
98 flags, cas, verb);
99 WATCHPOINT_IF_LABELED_NUMBER(instance->io_wait_count.read > 2, "read IO_WAIT", instance->io_wait_count.read);
100 WATCHPOINT_IF_LABELED_NUMBER(instance->io_wait_count.write > 2, "write_IO_WAIT", instance->io_wait_count.write);
101 }
102 else
103 {
104
105 if (cas)
106 {
107 int check_length;
108 check_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
109 "%s %.*s%.*s %u %llu %lu %llu%s\r\n",
110 storage_op_string(verb),
111 (int)ptr->prefix_key_length,
112 ptr->prefix_key,
113 (int)key_length, key, flags,
114 (unsigned long long)expiration, (unsigned long)value_length,
115 (unsigned long long)cas,
116 (ptr->flags.no_reply) ? " noreply" : "");
117 if (check_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || check_length < 0)
118 {
119 rc= MEMCACHED_WRITE_FAILURE;
120 memcached_io_reset(instance);
121
122 return rc;
123 }
124 write_length= check_length;
125 }
126 else
127 {
128 char *buffer_ptr= buffer;
129 const char *command= storage_op_string(verb);
130
131 /* Copy in the command, no space needed, we handle that in the command function*/
132 memcpy(buffer_ptr, command, strlen(command));
133
134 /* Copy in the key prefix, switch to the buffer_ptr */
135 buffer_ptr= memcpy((buffer_ptr + strlen(command)), ptr->prefix_key, ptr->prefix_key_length);
136
137 /* Copy in the key, adjust point if a key prefix was used. */
138 buffer_ptr= memcpy(buffer_ptr + (ptr->prefix_key_length ? ptr->prefix_key_length : 0),
139 key, key_length);
140 buffer_ptr+= key_length;
141 buffer_ptr[0]= ' ';
142 buffer_ptr++;
143
144 write_length= (size_t)(buffer_ptr - buffer);
145 int check_length;
146 check_length= snprintf(buffer_ptr, MEMCACHED_DEFAULT_COMMAND_SIZE -(size_t)(buffer_ptr - buffer),
147 "%u %llu %lu%s\r\n",
148 flags,
149 (unsigned long long)expiration, (unsigned long)value_length,
150 ptr->flags.no_reply ? " noreply" : "");
151 if ((size_t)check_length >= MEMCACHED_DEFAULT_COMMAND_SIZE -(size_t)(buffer_ptr - buffer) || check_length < 0)
152 {
153 rc= MEMCACHED_WRITE_FAILURE;
154 memcached_io_reset(instance);
155
156 return rc;
157 }
158
159 write_length+= (size_t)check_length;
160 WATCHPOINT_ASSERT(write_length < MEMCACHED_DEFAULT_COMMAND_SIZE);
161 }
162
163 if (ptr->flags.use_udp && ptr->flags.buffer_requests)
164 {
165 size_t cmd_size= write_length + value_length + 2;
166 if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
167 return MEMCACHED_WRITE_FAILURE;
168 if (cmd_size + instance->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
169 memcached_io_write(instance, NULL, 0, true);
170 }
171
172 if (write_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
173 {
174 rc= MEMCACHED_WRITE_FAILURE;
175 }
176 else
177 {
178 struct libmemcached_io_vector_st vector[]=
179 {
180 { .length= write_length, .buffer= buffer },
181 { .length= value_length, .buffer= value },
182 { .length= 2, .buffer= "\r\n" }
183 };
184
185 if (ptr->flags.buffer_requests && verb == SET_OP)
186 {
187 to_write= false;
188 }
189 else
190 {
191 to_write= true;
192 }
193
194 /* Send command header */
195 rc= memcached_vdo(instance, vector, 3, to_write);
196 if (rc == MEMCACHED_SUCCESS)
197 {
198
199 if (ptr->flags.no_reply)
200 {
201 rc= (to_write == false) ? MEMCACHED_BUFFERED : MEMCACHED_SUCCESS;
202 }
203 else if (to_write == false)
204 {
205 rc= MEMCACHED_BUFFERED;
206 }
207 else
208 {
209 rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
210
211 if (rc == MEMCACHED_STORED)
212 rc= MEMCACHED_SUCCESS;
213 }
214 }
215 }
216
217 if (rc == MEMCACHED_WRITE_FAILURE)
218 memcached_io_reset(instance);
219 }
220
221 WATCHPOINT_IF_LABELED_NUMBER(instance->io_wait_count.read > 2, "read IO_WAIT", instance->io_wait_count.read);
222 WATCHPOINT_IF_LABELED_NUMBER(instance->io_wait_count.write > 2, "write_IO_WAIT", instance->io_wait_count.write);
223
224 return rc;
225 }
226
227
228 memcached_return_t memcached_set(memcached_st *ptr, const char *key, size_t key_length,
229 const char *value, size_t value_length,
230 time_t expiration,
231 uint32_t flags)
232 {
233 memcached_return_t rc;
234 LIBMEMCACHED_MEMCACHED_SET_START();
235 rc= memcached_send(ptr, key, key_length,
236 key, key_length, value, value_length,
237 expiration, flags, 0, SET_OP);
238 LIBMEMCACHED_MEMCACHED_SET_END();
239 return rc;
240 }
241
242 memcached_return_t memcached_add(memcached_st *ptr,
243 const char *key, size_t key_length,
244 const char *value, size_t value_length,
245 time_t expiration,
246 uint32_t flags)
247 {
248 memcached_return_t rc;
249 LIBMEMCACHED_MEMCACHED_ADD_START();
250 rc= memcached_send(ptr, key, key_length,
251 key, key_length, value, value_length,
252 expiration, flags, 0, ADD_OP);
253 LIBMEMCACHED_MEMCACHED_ADD_END();
254 return rc;
255 }
256
257 memcached_return_t memcached_replace(memcached_st *ptr,
258 const char *key, size_t key_length,
259 const char *value, size_t value_length,
260 time_t expiration,
261 uint32_t flags)
262 {
263 memcached_return_t rc;
264 LIBMEMCACHED_MEMCACHED_REPLACE_START();
265 rc= memcached_send(ptr, key, key_length,
266 key, key_length, value, value_length,
267 expiration, flags, 0, REPLACE_OP);
268 LIBMEMCACHED_MEMCACHED_REPLACE_END();
269 return rc;
270 }
271
272 memcached_return_t memcached_prepend(memcached_st *ptr,
273 const char *key, size_t key_length,
274 const char *value, size_t value_length,
275 time_t expiration,
276 uint32_t flags)
277 {
278 memcached_return_t rc;
279 rc= memcached_send(ptr, key, key_length,
280 key, key_length, value, value_length,
281 expiration, flags, 0, PREPEND_OP);
282 return rc;
283 }
284
285 memcached_return_t memcached_append(memcached_st *ptr,
286 const char *key, size_t key_length,
287 const char *value, size_t value_length,
288 time_t expiration,
289 uint32_t flags)
290 {
291 memcached_return_t rc;
292 rc= memcached_send(ptr, key, key_length,
293 key, key_length, value, value_length,
294 expiration, flags, 0, APPEND_OP);
295 return rc;
296 }
297
298 memcached_return_t memcached_cas(memcached_st *ptr,
299 const char *key, size_t key_length,
300 const char *value, size_t value_length,
301 time_t expiration,
302 uint32_t flags,
303 uint64_t cas)
304 {
305 memcached_return_t rc;
306 rc= memcached_send(ptr, key, key_length,
307 key, key_length, value, value_length,
308 expiration, flags, cas, CAS_OP);
309 return rc;
310 }
311
312 memcached_return_t memcached_set_by_key(memcached_st *ptr,
313 const char *master_key __attribute__((unused)),
314 size_t master_key_length __attribute__((unused)),
315 const char *key, size_t key_length,
316 const char *value, size_t value_length,
317 time_t expiration,
318 uint32_t flags)
319 {
320 memcached_return_t rc;
321 LIBMEMCACHED_MEMCACHED_SET_START();
322 rc= memcached_send(ptr, master_key, master_key_length,
323 key, key_length, value, value_length,
324 expiration, flags, 0, SET_OP);
325 LIBMEMCACHED_MEMCACHED_SET_END();
326 return rc;
327 }
328
329 memcached_return_t memcached_add_by_key(memcached_st *ptr,
330 const char *master_key, size_t master_key_length,
331 const char *key, size_t key_length,
332 const char *value, size_t value_length,
333 time_t expiration,
334 uint32_t flags)
335 {
336 memcached_return_t rc;
337 LIBMEMCACHED_MEMCACHED_ADD_START();
338 rc= memcached_send(ptr, master_key, master_key_length,
339 key, key_length, value, value_length,
340 expiration, flags, 0, ADD_OP);
341 LIBMEMCACHED_MEMCACHED_ADD_END();
342 return rc;
343 }
344
345 memcached_return_t memcached_replace_by_key(memcached_st *ptr,
346 const char *master_key, size_t master_key_length,
347 const char *key, size_t key_length,
348 const char *value, size_t value_length,
349 time_t expiration,
350 uint32_t flags)
351 {
352 memcached_return_t rc;
353 LIBMEMCACHED_MEMCACHED_REPLACE_START();
354 rc= memcached_send(ptr, master_key, master_key_length,
355 key, key_length, value, value_length,
356 expiration, flags, 0, REPLACE_OP);
357 LIBMEMCACHED_MEMCACHED_REPLACE_END();
358 return rc;
359 }
360
361 memcached_return_t memcached_prepend_by_key(memcached_st *ptr,
362 const char *master_key, size_t master_key_length,
363 const char *key, size_t key_length,
364 const char *value, size_t value_length,
365 time_t expiration,
366 uint32_t flags)
367 {
368 memcached_return_t rc;
369 rc= memcached_send(ptr, master_key, master_key_length,
370 key, key_length, value, value_length,
371 expiration, flags, 0, PREPEND_OP);
372 return rc;
373 }
374
375 memcached_return_t memcached_append_by_key(memcached_st *ptr,
376 const char *master_key, size_t master_key_length,
377 const char *key, size_t key_length,
378 const char *value, size_t value_length,
379 time_t expiration,
380 uint32_t flags)
381 {
382 memcached_return_t rc;
383 rc= memcached_send(ptr, master_key, master_key_length,
384 key, key_length, value, value_length,
385 expiration, flags, 0, APPEND_OP);
386 return rc;
387 }
388
389 memcached_return_t memcached_cas_by_key(memcached_st *ptr,
390 const char *master_key, size_t master_key_length,
391 const char *key, size_t key_length,
392 const char *value, size_t value_length,
393 time_t expiration,
394 uint32_t flags,
395 uint64_t cas)
396 {
397 memcached_return_t rc;
398 rc= memcached_send(ptr, master_key, master_key_length,
399 key, key_length, value, value_length,
400 expiration, flags, cas, CAS_OP);
401 return rc;
402 }
403
404 static inline uint8_t get_com_code(memcached_storage_action_t verb, bool noreply)
405 {
406 /* 0 isn't a value we want, but GCC 4.2 seems to think ret can otherwise
407 * be used uninitialized in this function. FAIL */
408 uint8_t ret= 0;
409
410 if (noreply)
411 switch (verb)
412 {
413 case SET_OP:
414 ret=PROTOCOL_BINARY_CMD_SETQ;
415 break;
416 case ADD_OP:
417 ret=PROTOCOL_BINARY_CMD_ADDQ;
418 break;
419 case CAS_OP: /* FALLTHROUGH */
420 case REPLACE_OP:
421 ret=PROTOCOL_BINARY_CMD_REPLACEQ;
422 break;
423 case APPEND_OP:
424 ret=PROTOCOL_BINARY_CMD_APPENDQ;
425 break;
426 case PREPEND_OP:
427 ret=PROTOCOL_BINARY_CMD_PREPENDQ;
428 break;
429 default:
430 WATCHPOINT_ASSERT(verb);
431 break;
432 }
433 else
434 switch (verb)
435 {
436 case SET_OP:
437 ret=PROTOCOL_BINARY_CMD_SET;
438 break;
439 case ADD_OP:
440 ret=PROTOCOL_BINARY_CMD_ADD;
441 break;
442 case CAS_OP: /* FALLTHROUGH */
443 case REPLACE_OP:
444 ret=PROTOCOL_BINARY_CMD_REPLACE;
445 break;
446 case APPEND_OP:
447 ret=PROTOCOL_BINARY_CMD_APPEND;
448 break;
449 case PREPEND_OP:
450 ret=PROTOCOL_BINARY_CMD_PREPEND;
451 break;
452 default:
453 WATCHPOINT_ASSERT(verb);
454 break;
455 }
456
457 return ret;
458 }
459
460
461
462 static memcached_return_t memcached_send_binary(memcached_st *ptr,
463 memcached_server_write_instance_st server,
464 uint32_t server_key,
465 const char *key,
466 size_t key_length,
467 const char *value,
468 size_t value_length,
469 time_t expiration,
470 uint32_t flags,
471 uint64_t cas,
472 memcached_storage_action_t verb)
473 {
474 bool flush;
475 protocol_binary_request_set request= {.bytes= {0}};
476 size_t send_length= sizeof(request.bytes);
477
478 bool noreply= server->root->flags.no_reply;
479
480 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
481 request.message.header.request.opcode= get_com_code(verb, noreply);
482 request.message.header.request.keylen= htons((uint16_t)(key_length + ptr->prefix_key_length));
483 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
484 if (verb == APPEND_OP || verb == PREPEND_OP)
485 send_length -= 8; /* append & prepend does not contain extras! */
486 else
487 {
488 request.message.header.request.extlen= 8;
489 request.message.body.flags= htonl(flags);
490 request.message.body.expiration= htonl((uint32_t)expiration);
491 }
492
493 request.message.header.request.bodylen= htonl((uint32_t) (key_length + ptr->prefix_key_length + value_length +
494 request.message.header.request.extlen));
495
496 if (cas)
497 request.message.header.request.cas= htonll(cas);
498
499 flush= (bool) ((server->root->flags.buffer_requests && verb == SET_OP) ? 0 : 1);
500
501 if (server->root->flags.use_udp && ! flush)
502 {
503 size_t cmd_size= send_length + key_length + value_length;
504
505 if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
506 {
507 return MEMCACHED_WRITE_FAILURE;
508 }
509 if (cmd_size + server->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
510 {
511 memcached_io_write(server, NULL, 0, true);
512 }
513 }
514
515 struct libmemcached_io_vector_st vector[]=
516 {
517 { .length= send_length, .buffer= request.bytes },
518 { .length= ptr->prefix_key_length, .buffer= ptr->prefix_key },
519 { .length= key_length, .buffer= key },
520 { .length= value_length, .buffer= value }
521 };
522
523 /* write the header */
524 memcached_return_t rc;
525 if ((rc= memcached_vdo(server, vector, 4, flush)) != MEMCACHED_SUCCESS)
526 {
527 memcached_io_reset(server);
528 return (rc == MEMCACHED_SUCCESS) ? MEMCACHED_WRITE_FAILURE : rc;
529 }
530
531 if (verb == SET_OP && ptr->number_of_replicas > 0)
532 {
533 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_SETQ;
534 WATCHPOINT_STRING("replicating");
535
536 for (uint32_t x= 0; x < ptr->number_of_replicas; x++)
537 {
538 memcached_server_write_instance_st instance;
539
540 ++server_key;
541 if (server_key == memcached_server_count(ptr))
542 server_key= 0;
543
544 instance= memcached_server_instance_fetch(ptr, server_key);
545
546 if (memcached_vdo(instance, vector, 4, false) != MEMCACHED_SUCCESS)
547 {
548 memcached_io_reset(instance);
549 }
550 else
551 {
552 memcached_server_response_decrement(instance);
553 }
554 }
555 }
556
557 if (flush == false)
558 {
559 return MEMCACHED_BUFFERED;
560 }
561
562 if (noreply)
563 {
564 return MEMCACHED_SUCCESS;
565 }
566
567 return memcached_response(server, NULL, 0, NULL);
568 }
569