Merge lp:~tangent-org/libmemcached/1.0-build/ Build: jenkins-Libmemcached-192
[m6w6/libmemcached] / libmemcached / quit.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) 2010 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 #include <libmemcached/common.h>
39
40 /*
41 This closes all connections (forces flush of input as well).
42
43 Maybe add a host specific, or key specific version?
44
45 The reason we send "quit" is that in case we have buffered IO, this
46 will force data to be completed.
47 */
48
49 void memcached_quit_server(org::libmemcached::Instance* instance, bool io_death)
50 {
51 if (instance->valid())
52 {
53 if (io_death == false and memcached_is_udp(instance->root) == false and instance->is_shutting_down() == false)
54 {
55 memcached_return_t rc;
56 if (instance->root->flags.binary_protocol)
57 {
58 protocol_binary_request_quit request= {}; // = {.bytes= {0}};
59
60 initialize_binary_request(instance, request.message.header);
61
62 request.message.header.request.opcode = PROTOCOL_BINARY_CMD_QUIT;
63 request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES;
64
65 libmemcached_io_vector_st vector[]=
66 {
67 { request.bytes, sizeof(request.bytes) }
68 };
69
70 rc= memcached_vdo(instance, vector, 1, true);
71 }
72 else
73 {
74 libmemcached_io_vector_st vector[]=
75 {
76 { memcached_literal_param("quit\r\n") }
77 };
78
79 rc= memcached_vdo(instance, vector, 1, true);
80 }
81
82 instance->start_close_socket();
83
84 /* read until socket is closed, or there is an error
85 * closing the socket before all data is read
86 * results in server throwing away all data which is
87 * not read
88 *
89 * In .40 we began to only do this if we had been doing buffered
90 * requests of had replication enabled.
91 */
92 if (memcached_success(rc) and (instance->root->flags.buffer_requests or instance->root->number_of_replicas))
93 {
94 if (0)
95 {
96 memcached_return_t rc_slurp;
97 while (memcached_continue(rc_slurp= memcached_io_slurp(instance))) {} ;
98 WATCHPOINT_ASSERT(rc_slurp == MEMCACHED_CONNECTION_FAILURE);
99 }
100 else
101 {
102 memcached_io_slurp(instance);
103 }
104 }
105
106 /*
107 * memcached_io_read may call memcached_quit_server with io_death if
108 * it encounters problems, but we don't care about those occurences.
109 * The intention of that loop is to drain the data sent from the
110 * server to ensure that the server processed all of the data we
111 * sent to the server.
112 */
113 instance->server_failure_counter= 0;
114 }
115
116 }
117
118 instance->close_socket();
119
120 instance->state= MEMCACHED_SERVER_STATE_NEW;
121 instance->cursor_active_= 0;
122 instance->io_bytes_sent= 0;
123 instance->write_buffer_offset= size_t(instance->root and memcached_is_udp(instance->root) ? UDP_DATAGRAM_HEADER_LENGTH : 0);
124 instance->read_buffer_length= 0;
125 instance->read_ptr= instance->read_buffer;
126 instance->options.is_shutting_down= false;
127 memcached_server_response_reset(instance);
128
129 // We reset the version so that if we end up talking to a different server
130 // we don't have stale server version information.
131 instance->major_version= instance->minor_version= instance->micro_version= UINT8_MAX;
132
133 if (io_death)
134 {
135 memcached_mark_server_for_timeout(instance);
136 }
137 }
138
139 void send_quit(memcached_st *memc)
140 {
141 for (uint32_t x= 0; x < memcached_server_count(memc); x++)
142 {
143 org::libmemcached::Instance* instance= memcached_instance_fetch(memc, x);
144
145 memcached_quit_server(instance, false);
146 }
147 }
148
149 void memcached_quit(memcached_st *memc)
150 {
151 memcached_return_t rc;
152 if (memcached_failed(rc= initialize_query(memc, true)))
153 {
154 return;
155 }
156
157 send_quit(memc);
158 }