Update logic around UDP.
[awesomized/libmemcached] / libmemcached / storage.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38
39 #include <libmemcached/common.h>
40
41 enum memcached_storage_action_t {
42 SET_OP,
43 REPLACE_OP,
44 ADD_OP,
45 PREPEND_OP,
46 APPEND_OP,
47 CAS_OP
48 };
49
50 /* Inline this */
51 static inline const char *storage_op_string(memcached_storage_action_t verb)
52 {
53 switch (verb)
54 {
55 case REPLACE_OP:
56 return "replace ";
57
58 case ADD_OP:
59 return "add ";
60
61 case PREPEND_OP:
62 return "prepend ";
63
64 case APPEND_OP:
65 return "append ";
66
67 case CAS_OP:
68 return "cas ";
69
70 case SET_OP:
71 break;
72 }
73
74 return "set ";
75 }
76
77 static inline uint8_t get_com_code(const memcached_storage_action_t verb, const bool reply)
78 {
79 if (reply == false)
80 {
81 switch (verb)
82 {
83 case SET_OP:
84 return PROTOCOL_BINARY_CMD_SETQ;
85
86 case ADD_OP:
87 return PROTOCOL_BINARY_CMD_ADDQ;
88
89 case CAS_OP: /* FALLTHROUGH */
90 case REPLACE_OP:
91 return PROTOCOL_BINARY_CMD_REPLACEQ;
92
93 case APPEND_OP:
94 return PROTOCOL_BINARY_CMD_APPENDQ;
95
96 case PREPEND_OP:
97 return PROTOCOL_BINARY_CMD_PREPENDQ;
98 }
99 }
100
101 switch (verb)
102 {
103 case SET_OP:
104 break;
105
106 case ADD_OP:
107 return PROTOCOL_BINARY_CMD_ADD;
108
109 case CAS_OP: /* FALLTHROUGH */
110 case REPLACE_OP:
111 return PROTOCOL_BINARY_CMD_REPLACE;
112
113 case APPEND_OP:
114 return PROTOCOL_BINARY_CMD_APPEND;
115
116 case PREPEND_OP:
117 return PROTOCOL_BINARY_CMD_PREPEND;
118 }
119
120 return PROTOCOL_BINARY_CMD_SET;
121 }
122
123 static memcached_return_t memcached_send_binary(memcached_st *ptr,
124 memcached_server_write_instance_st server,
125 uint32_t server_key,
126 const char *key,
127 const size_t key_length,
128 const char *value,
129 const size_t value_length,
130 const time_t expiration,
131 const uint32_t flags,
132 const uint64_t cas,
133 const bool flush,
134 const bool reply,
135 memcached_storage_action_t verb)
136 {
137 protocol_binary_request_set request= {};
138 size_t send_length= sizeof(request.bytes);
139
140 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
141 request.message.header.request.opcode= get_com_code(verb, reply);
142 request.message.header.request.keylen= htons((uint16_t)(key_length + memcached_array_size(ptr->_namespace)));
143 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
144 if (verb == APPEND_OP or verb == PREPEND_OP)
145 {
146 send_length -= 8; /* append & prepend does not contain extras! */
147 }
148 else
149 {
150 request.message.header.request.extlen= 8;
151 request.message.body.flags= htonl(flags);
152 request.message.body.expiration= htonl((uint32_t)expiration);
153 }
154
155 request.message.header.request.bodylen= htonl((uint32_t) (key_length + memcached_array_size(ptr->_namespace) + value_length +
156 request.message.header.request.extlen));
157
158 if (cas)
159 {
160 request.message.header.request.cas= memcached_htonll(cas);
161 }
162
163 struct libmemcached_io_vector_st vector[]=
164 {
165 { request.bytes, send_length },
166 { memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace) },
167 { key, key_length },
168 { value, value_length }
169 };
170
171 /* write the header */
172 memcached_return_t rc;
173 if ((rc= memcached_vdo(server, vector, 4, flush)) != MEMCACHED_SUCCESS)
174 {
175 memcached_io_reset(server);
176
177 if (memcached_has_error(ptr))
178 {
179 memcached_set_error(*server, rc, MEMCACHED_AT);
180 }
181
182 return MEMCACHED_WRITE_FAILURE;
183 }
184
185 if (verb == SET_OP and ptr->number_of_replicas > 0)
186 {
187 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_SETQ;
188 WATCHPOINT_STRING("replicating");
189
190 for (uint32_t x= 0; x < ptr->number_of_replicas; x++)
191 {
192 ++server_key;
193 if (server_key == memcached_server_count(ptr))
194 {
195 server_key= 0;
196 }
197
198 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, server_key);
199
200 if (memcached_vdo(instance, vector, 4, false) != MEMCACHED_SUCCESS)
201 {
202 memcached_io_reset(instance);
203 }
204 else
205 {
206 memcached_server_response_decrement(instance);
207 }
208 }
209 }
210
211 if (flush == false)
212 {
213 return MEMCACHED_BUFFERED;
214 }
215
216 // No reply always assumes success
217 if (reply == false)
218 {
219 return MEMCACHED_SUCCESS;
220 }
221
222 return memcached_response(server, NULL, 0, NULL);
223 }
224
225 static memcached_return_t memcached_send_ascii(memcached_st *ptr,
226 memcached_server_write_instance_st instance,
227 const char *key,
228 const size_t key_length,
229 const char *value,
230 const size_t value_length,
231 const time_t expiration,
232 const uint32_t flags,
233 const uint64_t cas,
234 const bool flush,
235 const bool reply,
236 const memcached_storage_action_t verb)
237 {
238 char flags_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1];
239 int flags_buffer_length= snprintf(flags_buffer, sizeof(flags_buffer), " %u", flags);
240 if (size_t(flags_buffer_length) >= sizeof(flags_buffer) or flags_buffer_length < 0)
241 {
242 return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
243 memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)"));
244 }
245
246 char expiration_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1];
247 int expiration_buffer_length= snprintf(expiration_buffer, sizeof(expiration_buffer), " %llu", (unsigned long long)expiration);
248 if (size_t(expiration_buffer_length) >= sizeof(expiration_buffer) or expiration_buffer_length < 0)
249 {
250 return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
251 memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)"));
252 }
253
254 char value_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1];
255 int value_buffer_length= snprintf(value_buffer, sizeof(value_buffer), " %llu", (unsigned long long)value_length);
256 if (size_t(value_buffer_length) >= sizeof(value_buffer) or value_buffer_length < 0)
257 {
258 return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
259 memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)"));
260 }
261
262 char cas_buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1];
263 int cas_buffer_length= 0;
264 if (cas)
265 {
266 cas_buffer_length= snprintf(cas_buffer, sizeof(cas_buffer), " %llu", (unsigned long long)cas);
267 if (size_t(cas_buffer_length) >= sizeof(cas_buffer) or cas_buffer_length < 0)
268 {
269 return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
270 memcached_literal_param("snprintf(MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH)"));
271 }
272 }
273
274 struct libmemcached_io_vector_st vector[]=
275 {
276 { storage_op_string(verb), strlen(storage_op_string(verb))},
277 { memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace) },
278 { key, key_length },
279 { flags_buffer, flags_buffer_length },
280 { expiration_buffer, expiration_buffer_length },
281 { value_buffer, value_buffer_length },
282 { cas_buffer, cas_buffer_length },
283 { " noreply", reply ? 0 : memcached_literal_param_size(" noreply") },
284 { memcached_literal_param("\r\n") },
285 { value, value_length },
286 { memcached_literal_param("\r\n") }
287 };
288
289 /* Send command header */
290 memcached_return_t rc= memcached_vdo(instance, vector, 11, flush);
291 if (rc == MEMCACHED_SUCCESS)
292 {
293 if (flush == false)
294 {
295 return MEMCACHED_BUFFERED;
296 }
297
298 if (reply == false)
299 {
300 return MEMCACHED_SUCCESS;
301 }
302
303 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
304 rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
305
306 if (rc == MEMCACHED_STORED)
307 {
308 return MEMCACHED_SUCCESS;
309 }
310 }
311
312 if (rc == MEMCACHED_WRITE_FAILURE)
313 {
314 memcached_io_reset(instance);
315 }
316
317 assert(memcached_failed(rc));
318 if (memcached_has_error(ptr) == false)
319 {
320 return memcached_set_error(*ptr, rc, MEMCACHED_AT);
321 }
322
323 return rc;
324 }
325
326 static inline memcached_return_t memcached_send(memcached_st *ptr,
327 const char *group_key, size_t group_key_length,
328 const char *key, size_t key_length,
329 const char *value, size_t value_length,
330 const time_t expiration,
331 const uint32_t flags,
332 const uint64_t cas,
333 memcached_storage_action_t verb)
334 {
335 memcached_return_t rc;
336 if (memcached_failed(rc= initialize_query(ptr, true)))
337 {
338 return rc;
339 }
340
341 if (memcached_failed(rc= memcached_validate_key_length(key_length, memcached_is_binary(ptr))))
342 {
343 return rc;
344 }
345
346 if (memcached_failed(memcached_key_test(*ptr, (const char **)&key, &key_length, 1)))
347 {
348 return MEMCACHED_BAD_KEY_PROVIDED;
349 }
350
351 uint32_t server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
352 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, server_key);
353
354 WATCHPOINT_SET(instance->io_wait_count.read= 0);
355 WATCHPOINT_SET(instance->io_wait_count.write= 0);
356
357
358 bool flush= true;
359 if (memcached_is_buffering(instance->root) and verb == SET_OP)
360 {
361 flush= false;
362 }
363
364 bool reply= memcached_is_replying(ptr);
365
366 if (memcached_is_binary(ptr))
367 {
368 return memcached_send_binary(ptr, instance, server_key,
369 key, key_length,
370 value, value_length, expiration,
371 flags, cas, flush, reply, verb);
372 }
373
374 return memcached_send_ascii(ptr, instance,
375 key, key_length,
376 value, value_length, expiration,
377 flags, cas, flush, reply, verb);
378 }
379
380
381 memcached_return_t memcached_set(memcached_st *ptr, const char *key, size_t key_length,
382 const char *value, size_t value_length,
383 time_t expiration,
384 uint32_t flags)
385 {
386 memcached_return_t rc;
387 LIBMEMCACHED_MEMCACHED_SET_START();
388 rc= memcached_send(ptr, key, key_length,
389 key, key_length, value, value_length,
390 expiration, flags, 0, SET_OP);
391 LIBMEMCACHED_MEMCACHED_SET_END();
392 return rc;
393 }
394
395 memcached_return_t memcached_add(memcached_st *ptr,
396 const char *key, size_t key_length,
397 const char *value, size_t value_length,
398 time_t expiration,
399 uint32_t flags)
400 {
401 memcached_return_t rc;
402 LIBMEMCACHED_MEMCACHED_ADD_START();
403 rc= memcached_send(ptr, key, key_length,
404 key, key_length, value, value_length,
405 expiration, flags, 0, ADD_OP);
406
407 if (rc == MEMCACHED_NOTSTORED or rc == MEMCACHED_DATA_EXISTS)
408 {
409 memcached_set_error(*ptr, rc, MEMCACHED_AT);
410 }
411 LIBMEMCACHED_MEMCACHED_ADD_END();
412 return rc;
413 }
414
415 memcached_return_t memcached_replace(memcached_st *ptr,
416 const char *key, size_t key_length,
417 const char *value, size_t value_length,
418 time_t expiration,
419 uint32_t flags)
420 {
421 memcached_return_t rc;
422 LIBMEMCACHED_MEMCACHED_REPLACE_START();
423 rc= memcached_send(ptr, key, key_length,
424 key, key_length, value, value_length,
425 expiration, flags, 0, REPLACE_OP);
426 LIBMEMCACHED_MEMCACHED_REPLACE_END();
427 return rc;
428 }
429
430 memcached_return_t memcached_prepend(memcached_st *ptr,
431 const char *key, size_t key_length,
432 const char *value, size_t value_length,
433 time_t expiration,
434 uint32_t flags)
435 {
436 memcached_return_t rc;
437 rc= memcached_send(ptr, key, key_length,
438 key, key_length, value, value_length,
439 expiration, flags, 0, PREPEND_OP);
440 return rc;
441 }
442
443 memcached_return_t memcached_append(memcached_st *ptr,
444 const char *key, size_t key_length,
445 const char *value, size_t value_length,
446 time_t expiration,
447 uint32_t flags)
448 {
449 memcached_return_t rc;
450 rc= memcached_send(ptr, key, key_length,
451 key, key_length, value, value_length,
452 expiration, flags, 0, APPEND_OP);
453 return rc;
454 }
455
456 memcached_return_t memcached_cas(memcached_st *ptr,
457 const char *key, size_t key_length,
458 const char *value, size_t value_length,
459 time_t expiration,
460 uint32_t flags,
461 uint64_t cas)
462 {
463 memcached_return_t rc;
464 rc= memcached_send(ptr, key, key_length,
465 key, key_length, value, value_length,
466 expiration, flags, cas, CAS_OP);
467 return rc;
468 }
469
470 memcached_return_t memcached_set_by_key(memcached_st *ptr,
471 const char *group_key,
472 size_t group_key_length,
473 const char *key, size_t key_length,
474 const char *value, size_t value_length,
475 time_t expiration,
476 uint32_t flags)
477 {
478 memcached_return_t rc;
479 LIBMEMCACHED_MEMCACHED_SET_START();
480 rc= memcached_send(ptr, group_key, group_key_length,
481 key, key_length, value, value_length,
482 expiration, flags, 0, SET_OP);
483 LIBMEMCACHED_MEMCACHED_SET_END();
484 return rc;
485 }
486
487 memcached_return_t memcached_add_by_key(memcached_st *ptr,
488 const char *group_key, size_t group_key_length,
489 const char *key, size_t key_length,
490 const char *value, size_t value_length,
491 time_t expiration,
492 uint32_t flags)
493 {
494 memcached_return_t rc;
495 LIBMEMCACHED_MEMCACHED_ADD_START();
496 rc= memcached_send(ptr, group_key, group_key_length,
497 key, key_length, value, value_length,
498 expiration, flags, 0, ADD_OP);
499 LIBMEMCACHED_MEMCACHED_ADD_END();
500 return rc;
501 }
502
503 memcached_return_t memcached_replace_by_key(memcached_st *ptr,
504 const char *group_key, size_t group_key_length,
505 const char *key, size_t key_length,
506 const char *value, size_t value_length,
507 time_t expiration,
508 uint32_t flags)
509 {
510 memcached_return_t rc;
511 LIBMEMCACHED_MEMCACHED_REPLACE_START();
512 rc= memcached_send(ptr, group_key, group_key_length,
513 key, key_length, value, value_length,
514 expiration, flags, 0, REPLACE_OP);
515 LIBMEMCACHED_MEMCACHED_REPLACE_END();
516 return rc;
517 }
518
519 memcached_return_t memcached_prepend_by_key(memcached_st *ptr,
520 const char *group_key, size_t group_key_length,
521 const char *key, size_t key_length,
522 const char *value, size_t value_length,
523 time_t expiration,
524 uint32_t flags)
525 {
526 return memcached_send(ptr, group_key, group_key_length,
527 key, key_length, value, value_length,
528 expiration, flags, 0, PREPEND_OP);
529 }
530
531 memcached_return_t memcached_append_by_key(memcached_st *ptr,
532 const char *group_key, size_t group_key_length,
533 const char *key, size_t key_length,
534 const char *value, size_t value_length,
535 time_t expiration,
536 uint32_t flags)
537 {
538 return memcached_send(ptr, group_key, group_key_length,
539 key, key_length, value, value_length,
540 expiration, flags, 0, APPEND_OP);
541 }
542
543 memcached_return_t memcached_cas_by_key(memcached_st *ptr,
544 const char *group_key, size_t group_key_length,
545 const char *key, size_t key_length,
546 const char *value, size_t value_length,
547 time_t expiration,
548 uint32_t flags,
549 uint64_t cas)
550 {
551 return memcached_send(ptr, group_key, group_key_length,
552 key, key_length, value, value_length,
553 expiration, flags, cas, CAS_OP);
554 }
555