1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
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.
38 #include <libmemcached/common.h>
40 static inline void _server_init(memcached_instance_st
* self
, Memcached
*root
,
41 const memcached_string_t
& hostname
,
43 uint32_t weight
, memcached_connection_t type
)
45 self
->options
.is_shutting_down
= false;
46 self
->options
.is_dead
= false;
47 self
->options
.ready
= false;
50 self
->cursor_active_
= 0;
52 self
->fd
= INVALID_SOCKET
;
53 self
->io_bytes_sent
= 0;
55 self
->server_failure_counter
= 0;
56 self
->server_failure_counter_query_id
= 0;
57 self
->server_timeout_counter
= 0;
58 self
->server_timeout_counter_query_id
= 0;
59 self
->weight
= weight
? weight
: 1; // 1 is the default weight value
60 self
->io_wait_count
.read
= 0;
61 self
->io_wait_count
.write
= 0;
62 self
->io_wait_count
.timeouts
= 0;
63 self
->io_wait_count
._bytes_read
= 0;
64 self
->major_version
= UINT8_MAX
;
65 self
->micro_version
= UINT8_MAX
;
66 self
->minor_version
= UINT8_MAX
;
68 self
->error_messages
= NULL
;
69 self
->read_ptr
= self
->read_buffer
;
70 self
->read_buffer_length
= 0;
71 self
->read_data_length
= 0;
72 self
->write_buffer_offset
= 0;
73 self
->address_info
= NULL
;
74 self
->address_info_next
= NULL
;
76 self
->state
= MEMCACHED_SERVER_STATE_NEW
;
82 self
->version
= ++root
->server_info
.version
;
86 self
->version
= UINT_MAX
;
88 self
->limit_maxbytes
= 0;
89 self
->hostname(hostname
);
92 static memcached_instance_st
* _server_create(memcached_instance_st
* self
, const memcached_st
*memc
)
96 self
= libmemcached_xmalloc(memc
, memcached_instance_st
);
100 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
103 self
->options
.is_allocated
= true;
107 self
->options
.is_allocated
= false;
110 self
->options
.is_initialized
= true;
115 void memcached_instance_st::events(short arg
)
117 if ((_events
| arg
) == _events
)
125 void memcached_instance_st::revents(short arg
)
133 _events
&= short(~arg
);
136 memcached_instance_st
* __instance_create_with(memcached_st
*memc
,
137 memcached_instance_st
* self
,
138 const memcached_string_t
& hostname
,
139 const in_port_t port
,
141 const memcached_connection_t type
)
143 if (memcached_is_valid_servername(hostname
) == false)
145 memcached_set_error(*memc
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
, memcached_literal_param("Invalid hostname provided"));
149 self
= _server_create(self
, memc
);
156 _server_init(self
, const_cast<memcached_st
*>(memc
), hostname
, port
, weight
, type
);
158 if (memc
and memcached_is_udp(memc
))
160 self
->write_buffer_offset
= UDP_DATAGRAM_HEADER_LENGTH
;
161 memcached_io_init_udp_header(self
, 0);
167 void __instance_free(memcached_instance_st
* self
)
169 memcached_quit_server(self
, false);
171 self
->clear_addrinfo();
172 assert(self
->address_info_next
== NULL
);
174 memcached_error_free(*self
);
176 if (memcached_is_allocated(self
))
178 libmemcached_free(self
->root
, self
);
182 self
->options
.is_initialized
= false;
186 void memcached_instance_free(memcached_instance_st
* self
)
190 __instance_free(self
);
194 memcached_return_t
memcached_server_cursor(const memcached_st
* shell
,
195 const memcached_server_fn
*callback
,
197 uint32_t number_of_callbacks
)
199 const Memcached
* memc
= memcached2Memcached(shell
);
200 memcached_return_t rc
;
201 if (memcached_failed(rc
= initialize_const_query(memc
)))
207 for (uint32_t x
= 0; x
< memcached_instance_list_count(memc
); x
++)
209 memcached_instance_st
* instance
= memcached_instance_by_position(memc
, x
);
211 for (uint32_t y
= 0; y
< number_of_callbacks
; y
++)
213 memcached_return_t ret
= (*callback
[y
])(memc
, instance
, context
);
215 if (memcached_failed(ret
))
223 return errors
? MEMCACHED_SOME_ERRORS
: MEMCACHED_SUCCESS
;
226 memcached_return_t
memcached_server_execute(memcached_st
*memc
,
227 memcached_server_execute_fn callback
,
230 if (callback
== NULL
)
232 return MEMCACHED_INVALID_ARGUMENTS
;
235 bool some_errors
= false;;
236 for (uint32_t x
= 0; x
< memcached_instance_list_count(memc
); x
++)
238 memcached_instance_st
* instance
= memcached_instance_fetch(memc
, x
);
240 memcached_return_t rc
= (*callback
)(memc
, instance
, context
);
241 if (rc
== MEMCACHED_INVALID_ARGUMENTS
)
245 else if (memcached_fatal(rc
))
252 return MEMCACHED_SUCCESS
;
255 const memcached_instance_st
* memcached_server_by_key(memcached_st
*shell
,
258 memcached_return_t
*error
)
260 Memcached
* memc
= memcached2Memcached(shell
);
261 memcached_return_t unused
;
268 memcached_return_t rc
;
269 if (memcached_failed(rc
= initialize_const_query(memc
)))
275 if (memcached_failed((memcached_key_test(*memc
, (const char **)&key
, &key_length
, 1))))
277 *error
= memcached_last_error(memc
);
281 uint32_t server_key
= memcached_generate_hash(memc
, key
, key_length
);
282 return memcached_instance_by_position(memc
, server_key
);
286 If we do not have a valid object to clone from, we toss an error.
288 static memcached_instance_st
* memcached_instance_clone(memcached_instance_st
* source
)
290 /* We just do a normal create if source is missing */
296 memcached_string_t hostname_
= { memcached_string_make_from_cstr(source
->hostname()) };
297 return __instance_create_with(source
->root
,
300 source
->port(), source
->weight
,
304 void set_last_disconnected_host(memcached_instance_st
* self
)
309 if (memcached_server_get_last_disconnect(self
->root
) and
310 memcached_server_get_last_disconnect(self
->root
)->version
== self
->version
)
316 memcached_st
*root
= (memcached_st
*)self
->root
;
318 memcached_instance_free((memcached_instance_st
*)(root
->last_disconnected_server
));
320 // We set is_parsing so that no lookup happens
321 root
->state
.is_parsing
= true;
322 root
->last_disconnected_server
= memcached_instance_clone(self
);
323 root
->state
.is_parsing
= false;
325 ((memcached_instance_st
*)memcached_server_get_last_disconnect(root
))->version
= self
->version
;
329 const memcached_instance_st
* memcached_server_get_last_disconnect(const memcached_st
*shell
)
331 const Memcached
* self
= memcached2Memcached(shell
);
334 return (const memcached_instance_st
*)self
->last_disconnected_server
;
340 void memcached_instance_next_retry(const memcached_instance_st
* self
, const time_t absolute_time
)
342 WATCHPOINT_ASSERT(self
);
345 ((memcached_instance_st
*)self
)->next_retry
= absolute_time
;
349 bool memcached_instance_st::valid() const
351 if (fd
== INVALID_SOCKET
)
359 bool memcached_instance_st::is_shutting_down() const
361 return options
.is_shutting_down
;