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(org::libmemcached::Instance
* self
, memcached_st
*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
->weight
= weight
? weight
: 1; // 1 is the default weight value
58 self
->io_wait_count
.read
= 0;
59 self
->io_wait_count
.write
= 0;
60 self
->io_wait_count
.timeouts
= 0;
61 self
->io_wait_count
._bytes_read
= 0;
62 self
->major_version
= UINT8_MAX
;
63 self
->micro_version
= UINT8_MAX
;
64 self
->minor_version
= UINT8_MAX
;
66 self
->error_messages
= NULL
;
67 self
->read_ptr
= self
->read_buffer
;
68 self
->read_buffer_length
= 0;
69 self
->read_data_length
= 0;
70 self
->write_buffer_offset
= 0;
71 self
->address_info
= NULL
;
72 self
->address_info_next
= NULL
;
74 self
->state
= MEMCACHED_SERVER_STATE_NEW
;
80 self
->version
= ++root
->server_info
.version
;
84 self
->version
= UINT_MAX
;
86 self
->limit_maxbytes
= 0;
87 memcpy(self
->hostname
, hostname
.c_str
, hostname
.size
);
88 self
->hostname
[hostname
.size
]= 0;
91 static org::libmemcached::Instance
* _server_create(org::libmemcached::Instance
* self
, const memcached_st
*memc
)
95 self
= libmemcached_xmalloc(memc
, org::libmemcached::Instance
);
99 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
102 self
->options
.is_allocated
= true;
106 self
->options
.is_allocated
= false;
109 self
->options
.is_initialized
= true;
114 void org::libmemcached::Instance::events(short arg
)
116 if ((_events
| arg
) == _events
)
124 void org::libmemcached::Instance::revents(short arg
)
132 _events
&= short(~arg
);
135 org::libmemcached::Instance
* __instance_create_with(memcached_st
*memc
,
136 org::libmemcached::Instance
* self
,
137 const memcached_string_t
& hostname
,
138 const in_port_t port
,
140 const memcached_connection_t type
)
142 if (memcached_is_valid_servername(hostname
) == false)
144 memcached_set_error(*memc
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
, memcached_literal_param("Invalid hostname provided"));
148 self
= _server_create(self
, memc
);
155 _server_init(self
, const_cast<memcached_st
*>(memc
), hostname
, port
, weight
, type
);
157 if (memc
and memcached_is_udp(memc
))
159 self
->write_buffer_offset
= UDP_DATAGRAM_HEADER_LENGTH
;
160 memcached_io_init_udp_header(self
, 0);
166 void __instance_free(org::libmemcached::Instance
* self
)
168 memcached_quit_server(self
, false);
170 if (self
->address_info
)
172 freeaddrinfo(self
->address_info
);
173 self
->address_info
= NULL
;
174 self
->address_info_next
= NULL
;
176 assert(self
->address_info_next
== NULL
);
178 memcached_error_free(*self
);
180 if (memcached_is_allocated(self
))
182 libmemcached_free(self
->root
, self
);
186 self
->options
.is_initialized
= false;
190 void memcached_instance_free(org::libmemcached::Instance
* self
)
194 __instance_free(self
);
198 memcached_return_t
memcached_server_cursor(const memcached_st
* memc
,
199 const memcached_server_fn
*callback
,
201 uint32_t number_of_callbacks
)
203 memcached_return_t rc
;
204 if (memcached_failed(rc
= initialize_const_query(memc
)))
210 for (uint32_t x
= 0; x
< memcached_instance_list_count(memc
); x
++)
212 org::libmemcached::Instance
* instance
= memcached_instance_by_position(memc
, x
);
214 for (uint32_t y
= 0; y
< number_of_callbacks
; y
++)
216 memcached_return_t ret
= (*callback
[y
])(memc
, instance
, context
);
218 if (memcached_failed(ret
))
226 return errors
? MEMCACHED_SOME_ERRORS
: MEMCACHED_SUCCESS
;
229 memcached_return_t
memcached_server_execute(memcached_st
*memc
,
230 memcached_server_execute_fn callback
,
233 if (callback
== NULL
)
235 return MEMCACHED_INVALID_ARGUMENTS
;
238 bool some_errors
= false;;
239 for (uint32_t x
= 0; x
< memcached_instance_list_count(memc
); x
++)
241 org::libmemcached::Instance
* instance
= memcached_instance_fetch(memc
, x
);
243 memcached_return_t rc
= (*callback
)(memc
, instance
, context
);
244 if (rc
== MEMCACHED_INVALID_ARGUMENTS
)
248 else if (memcached_fatal(rc
))
255 return MEMCACHED_SUCCESS
;
258 memcached_server_instance_st
memcached_server_by_key(memcached_st
*memc
,
261 memcached_return_t
*error
)
263 memcached_return_t unused
;
270 memcached_return_t rc
;
271 if (memcached_failed(rc
= initialize_const_query(memc
)))
277 if (memcached_failed((memcached_key_test(*memc
, (const char **)&key
, &key_length
, 1))))
279 *error
= memcached_last_error(memc
);
283 uint32_t server_key
= memcached_generate_hash(memc
, key
, key_length
);
284 return memcached_instance_by_position(memc
, server_key
);
288 If we do not have a valid object to clone from, we toss an error.
290 static org::libmemcached::Instance
* memcached_instance_clone(org::libmemcached::Instance
* source
)
292 /* We just do a normal create if source is missing */
298 memcached_string_t hostname
= { memcached_string_make_from_cstr(source
->hostname
) };
299 return __instance_create_with(source
->root
,
302 source
->port(), source
->weight
,
306 void set_last_disconnected_host(org::libmemcached::Instance
* self
)
311 if (memcached_server_get_last_disconnect(self
->root
) and
312 memcached_server_get_last_disconnect(self
->root
)->version
== self
->version
)
318 memcached_st
*root
= (memcached_st
*)self
->root
;
320 memcached_instance_free((org::libmemcached::Instance
*)(root
->last_disconnected_server
));
322 // We set is_parsing so that no lookup happens
323 root
->state
.is_parsing
= true;
324 root
->last_disconnected_server
= memcached_instance_clone(self
);
325 root
->state
.is_parsing
= false;
327 ((org::libmemcached::Instance
*)memcached_server_get_last_disconnect(root
))->version
= self
->version
;
331 memcached_server_instance_st
memcached_server_get_last_disconnect(const memcached_st
*self
)
333 WATCHPOINT_ASSERT(self
);
339 return (memcached_server_instance_st
)self
->last_disconnected_server
;
342 void memcached_instance_next_retry(memcached_server_instance_st self
, const time_t absolute_time
)
344 WATCHPOINT_ASSERT(self
);
347 ((org::libmemcached::Instance
*)self
)->next_retry
= absolute_time
;
352 namespace libmemcached
{
354 bool Instance::valid() const
356 if (fd
== INVALID_SOCKET
)
364 bool Instance::is_shutting_down() const
366 return options
.is_shutting_down
;
369 } // namespace libmemcached