+
+static memcached_return 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 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() % (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;
+ }
+
+ if (success)
+ break;
+ }
+
+ return rc;
+}
+
+static memcached_return binary_mget_by_key(memcached_st *ptr,
+ unsigned int master_server_key,
+ bool is_master_key_set,
+ const char * const *keys,
+ const size_t *key_length,
+ size_t number_of_keys,
+ bool mget_mode)
+{
+ memcached_return rc;
+
+ if (ptr->number_of_replicas == 0)
+ {
+ rc= simple_binary_mget(ptr, master_server_key, is_master_key_set,
+ keys, key_length, number_of_keys, mget_mode);
+ }
+ else
+ {
+ uint32_t* hash;
+ bool* dead_servers;
+
+ hash= ptr->call_malloc(ptr, sizeof(uint32_t) * number_of_keys);
+ dead_servers= ptr->call_calloc(ptr, ptr->number_of_hosts, sizeof(bool));
+
+ if (hash == NULL || dead_servers == NULL)
+ {
+ ptr->call_free(ptr, hash);
+ ptr->call_free(ptr, dead_servers);
+ return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
+ }
+
+ if (is_master_key_set)
+ for (unsigned int x= 0; x < number_of_keys; x++)
+ hash[x]= master_server_key;
+ else
+ for (unsigned int x= 0; x < number_of_keys; x++)
+ hash[x]= memcached_generate_hash(ptr, keys[x], key_length[x]);
+
+ rc= replication_binary_mget(ptr, hash, dead_servers, keys,
+ key_length, number_of_keys);
+
+ ptr->call_free(ptr, hash);
+ ptr->call_free(ptr, dead_servers);
+
+ return MEMCACHED_SUCCESS;
+ }
+
+ return rc;
+}