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.
39 This is a partial implementation for fetching/creating memcached_instance_st objects.
41 #include <libmemcached/common.h>
43 static inline void _server_init(memcached_instance_st
*self
, memcached_st
*root
,
44 const memcached_string_t
& hostname
,
46 uint32_t weight
, memcached_connection_t type
)
48 self
->options
.is_shutting_down
= false;
49 self
->options
.is_dead
= false;
50 self
->number_of_hosts
= 0;
51 self
->cursor_active
= 0;
53 self
->fd
= INVALID_SOCKET
;
54 self
->io_bytes_sent
= 0;
56 self
->server_failure_counter
= 0;
57 self
->server_failure_counter_query_id
= 0;
58 self
->weight
= weight
? weight
: 1; // 1 is the default weight value
59 self
->io_wait_count
.read
= 0;
60 self
->io_wait_count
.write
= 0;
61 self
->io_wait_count
.timeouts
= 0;
62 self
->io_wait_count
._bytes_read
= 0;
63 self
->major_version
= UINT8_MAX
;
64 self
->micro_version
= UINT8_MAX
;
65 self
->minor_version
= UINT8_MAX
;
67 self
->error_messages
= NULL
;
68 self
->read_ptr
= self
->read_buffer
;
69 self
->read_buffer_length
= 0;
70 self
->read_data_length
= 0;
71 self
->write_buffer_offset
= 0;
72 self
->address_info
= NULL
;
73 self
->address_info_next
= NULL
;
75 self
->state
= MEMCACHED_SERVER_STATE_NEW
;
81 self
->version
= ++root
->server_info
.version
;
85 self
->version
= UINT_MAX
;
87 self
->limit_maxbytes
= 0;
88 memcpy(self
->hostname
, hostname
.c_str
, hostname
.size
);
89 self
->hostname
[hostname
.size
]= 0;
92 static memcached_instance_st
*_server_create(memcached_instance_st
*self
, const memcached_st
*memc
)
96 self
= libmemcached_xmalloc(memc
, struct 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 memcached_instance_st
*__instance_create_with(memcached_st
*memc
,
116 memcached_instance_st
* self
,
117 const memcached_string_t
& hostname
,
118 const in_port_t port
,
120 const memcached_connection_t type
)
122 if (memcached_is_valid_servername(hostname
) == false)
124 memcached_set_error(*memc
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
, memcached_literal_param("Invalid hostname provided"));
128 self
= _server_create(self
, memc
);
135 _server_init(self
, const_cast<memcached_st
*>(memc
), hostname
, port
, weight
, type
);
137 if (memc
and memcached_is_udp(memc
))
139 self
->write_buffer_offset
= UDP_DATAGRAM_HEADER_LENGTH
;
140 memcached_io_init_udp_header(self
, 0);
145 memcached_connect_try(self
);
151 void __instance_free(memcached_instance_st
*self
)
153 memcached_quit_server(self
, false);
155 if (self
->address_info
)
157 freeaddrinfo(self
->address_info
);
158 self
->address_info
= NULL
;
159 self
->address_info_next
= NULL
;
162 memcached_error_free(*self
);
164 if (memcached_is_allocated(self
))
166 libmemcached_free(self
->root
, self
);
170 self
->options
.is_initialized
= false;
174 void memcached_instance_free(memcached_instance_st
*self
)
181 if (memcached_instance_count(self
))
183 memcached_instance_list_free(self
, memcached_instance_count(self
));
187 __instance_free(self
);
191 If we do not have a valid object to clone from, we toss an error.
193 memcached_server_st
*memcached_instance_2_server(memcached_instance_st
*source
)
195 /* We just do a normal create if source is missing */
201 memcached_string_t hostname
= { memcached_string_make_from_cstr(source
->hostname
) };
202 return __server_create_with(source
->root
, NULL
,
204 source
->port
, source
->weight
,
209 memcached_return_t
memcached_server_cursor(const memcached_st
*ptr
,
210 const memcached_server_fn
*callback
,
212 uint32_t number_of_callbacks
)
214 memcached_return_t rc
;
215 if (memcached_failed(rc
= initialize_const_query(ptr
)))
221 for (uint32_t x
= 0; x
< memcached_instance_list_count(ptr
); x
++)
223 memcached_server_instance_st instance
=
224 memcached_server_instance_by_position(ptr
, x
);
226 for (uint32_t y
= 0; y
< number_of_callbacks
; y
++)
228 memcached_return_t ret
= (*callback
[y
])(ptr
, instance
, context
);
230 if (memcached_failed(ret
))
238 return errors
? MEMCACHED_SOME_ERRORS
: MEMCACHED_SUCCESS
;
241 memcached_return_t
memcached_server_execute(memcached_st
*ptr
,
242 memcached_server_execute_fn callback
,
245 if (callback
== NULL
)
247 return MEMCACHED_INVALID_ARGUMENTS
;
250 bool some_errors
= false;;
251 for (uint32_t x
= 0; x
< memcached_instance_list_count(ptr
); x
++)
253 memcached_instance_st
* instance
= memcached_server_instance_fetch(ptr
, x
);
255 memcached_return_t rc
= (*callback
)(ptr
, instance
, context
);
256 if (rc
== MEMCACHED_INVALID_ARGUMENTS
)
260 else if (memcached_fatal(rc
))
267 return MEMCACHED_SUCCESS
;
270 memcached_server_instance_st
memcached_server_by_key(memcached_st
*ptr
,
273 memcached_return_t
*error
)
275 memcached_return_t unused
;
282 memcached_return_t rc
;
283 if (memcached_failed(rc
= initialize_const_query(ptr
)))
289 if (memcached_failed((memcached_key_test(*ptr
, (const char **)&key
, &key_length
, 1))))
291 *error
= memcached_last_error(ptr
);
295 uint32_t server_key
= memcached_generate_hash(ptr
, key
, key_length
);
296 return memcached_server_instance_by_position(ptr
, server_key
);
300 If we do not have a valid object to clone from, we toss an error.
302 static memcached_instance_st
*memcached_instance_clone(memcached_instance_st
*source
)
304 /* We just do a normal create if source is missing */
310 memcached_string_t hostname
= { memcached_string_make_from_cstr(source
->hostname
) };
311 return __instance_create_with(source
->root
,
314 source
->port
, source
->weight
,
318 void set_last_disconnected_host(memcached_server_write_instance_st self
)
321 if (self
->root
== NULL
)
326 if (self
->root
->last_disconnected_server
and self
->root
->last_disconnected_server
->version
== self
->version
)
332 memcached_st
*root
= (memcached_st
*)self
->root
;
334 memcached_instance_free(root
->last_disconnected_server
);
335 root
->last_disconnected_server
= memcached_instance_clone(self
);
336 root
->last_disconnected_server
->version
= self
->version
;
339 memcached_server_instance_st
memcached_server_get_last_disconnect(const memcached_st
*self
)
341 WATCHPOINT_ASSERT(self
);
347 return self
->last_disconnected_server
;
350 uint32_t memcached_instance_set_count(memcached_instance_st
*servers
, uint32_t count
)
352 WATCHPOINT_ASSERT(servers
);
358 return servers
->number_of_hosts
= count
;
361 const char *memcached_instance_name(const memcached_server_instance_st self
)
363 WATCHPOINT_ASSERT(self
);
367 return self
->hostname
;
370 in_port_t
memcached_instance_port(const memcached_server_instance_st self
)
372 WATCHPOINT_ASSERT(self
);
381 uint32_t memcached_instance_response_count(const memcached_instance_st
* self
)
383 WATCHPOINT_ASSERT(self
);
389 return self
->cursor_active
;