+ }
+
+
+ return rc;
+}
+
+static memcached_return_t replication_binary_mget(memcached_st *ptr,
+ uint32_t* hash,
+ bool* dead_servers,
+ const char *const *keys,
+ const size_t *key_length,
+ size_t number_of_keys)
+{
+ memcached_return_t rc= MEMCACHED_NOTFOUND;
+ uint32_t x, start= 0;
+ uint64_t randomize_read= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_RANDOMIZE_REPLICA_READ);
+
+ if (randomize_read)
+ start= (uint32_t)random() % (uint32_t)(ptr->number_of_replicas + 1);
+
+ /* Loop for each replica */
+ for (uint32_t replica= 0; replica <= ptr->number_of_replicas; ++replica)
+ {
+ bool success= true;
+
+ for (x= 0; x < number_of_keys; ++x)
+ {
+ if (hash[x] == ptr->number_of_hosts)
+ continue; /* Already successfully sent */
+
+ uint32_t server= hash[x] + replica;
+
+ /* In case of randomized reads */
+ if (randomize_read && ((server + start) <= (hash[x] + ptr->number_of_replicas)))
+ server += start;
+
+ while (server >= ptr->number_of_hosts)
+ server -= ptr->number_of_hosts;
+
+ if (dead_servers[server])
+ continue;
+
+ if (memcached_server_response_count(&ptr->hosts[server]) == 0)
+ {
+ rc= memcached_connect(&ptr->hosts[server]);
+ if (rc != MEMCACHED_SUCCESS)
+ {
+ memcached_io_reset(&ptr->hosts[server]);
+ dead_servers[server]= true;
+ success= false;
+ continue;
+ }
+ }
+
+ protocol_binary_request_getk request= {
+ .message.header.request= {
+ .magic= PROTOCOL_BINARY_REQ,
+ .opcode= PROTOCOL_BINARY_CMD_GETK,
+ .keylen= htons((uint16_t)key_length[x]),
+ .datatype= PROTOCOL_BINARY_RAW_BYTES,
+ .bodylen= htonl((uint32_t)key_length[x])
+ }
+ };
+
+ /*
+ * We need to disable buffering to actually know that the request was
+ * successfully sent to the server (so that we should expect a result
+ * back). It would be nice to do this in buffered mode, but then it
+ * would be complex to handle all error situations if we got to send
+ * some of the messages, and then we failed on writing out some others
+ * and we used the callback interface from memcached_mget_execute so
+ * that we might have processed some of the responses etc. For now,
+ * just make sure we work _correctly_
+ */
+ if ((memcached_io_write(&ptr->hosts[server], request.bytes,
+ sizeof(request.bytes), 0) == -1) ||
+ (memcached_io_write(&ptr->hosts[server], keys[x],
+ key_length[x], 1) == -1))
+ {
+ memcached_io_reset(&ptr->hosts[server]);
+ dead_servers[server]= true;
+ success= false;
+ continue;
+ }
+
+ memcached_server_response_increment(&ptr->hosts[server]);
+ hash[x]= ptr->number_of_hosts;