static void req_forward_local_dc(struct context *ctx, struct conn *c_conn, struct msg *msg, struct mbuf *orig_mbuf, uint8_t *key, uint32_t keylen, struct datacenter *dc) { struct server_pool *pool = c_conn->owner; if (request_send_to_all_local_racks(msg)) { // send request to all local racks req_forward_all_local_racks(ctx, c_conn, msg, orig_mbuf, key, keylen, dc); } else { // send request to only local token owner ASSERT(msg->is_read); msg->rsp_handler = msg_get_rsp_handler(msg); struct rack * rack = server_get_rack_by_dc_rack(pool, &pool->rack, &pool->dc); remote_req_forward(ctx, c_conn, msg, rack, key, keylen); } }
static void req_forward(struct context *ctx, struct conn *c_conn, struct msg *msg) { struct server_pool *pool = c_conn->owner; uint8_t *key; uint32_t keylen; ASSERT(c_conn->type == CONN_CLIENT); if (msg->is_read) { if (msg->type != MSG_REQ_REDIS_PING) stats_pool_incr(ctx, client_read_requests); } else stats_pool_incr(ctx, client_write_requests); key = NULL; keylen = 0; // add the message to the dict log_debug(LOG_DEBUG, "conn %p adding message %d:%d", c_conn, msg->id, msg->parent_id); dictAdd(c_conn->outstanding_msgs_dict, &msg->id, msg); if (!string_empty(&pool->hash_tag)) { struct string *tag = &pool->hash_tag; uint8_t *tag_start, *tag_end; tag_start = dn_strchr(msg->key_start, msg->key_end, tag->data[0]); if (tag_start != NULL) { tag_end = dn_strchr(tag_start + 1, msg->key_end, tag->data[1]); if (tag_end != NULL) { key = tag_start + 1; keylen = (uint32_t)(tag_end - key); } } } if (keylen == 0) { key = msg->key_start; keylen = (uint32_t)(msg->key_end - msg->key_start); } // need to capture the initial mbuf location as once we add in the dynomite // headers (as mbufs to the src msg), that will bork the request sent to // secondary racks struct mbuf *orig_mbuf = STAILQ_FIRST(&msg->mhdr); if (ctx->admin_opt == 1) { if (msg->type == MSG_REQ_REDIS_DEL || msg->type == MSG_REQ_MC_DELETE) { struct rack * rack = server_get_rack_by_dc_rack(pool, &pool->rack, &pool->dc); admin_local_req_forward(ctx, c_conn, msg, rack, key, keylen); return; } } if (msg->msg_routing == ROUTING_LOCAL_NODE_ONLY) { // Strictly local host only msg->consistency = DC_ONE; msg->rsp_handler = msg_local_one_rsp_handler; local_req_forward(ctx, c_conn, msg, key, keylen); return; } if (msg->is_read) { msg->consistency = conn_get_read_consistency(c_conn); } else { msg->consistency = conn_get_write_consistency(c_conn); } /* forward the request */ uint32_t dc_cnt = array_n(&pool->datacenters); uint32_t dc_index; for(dc_index = 0; dc_index < dc_cnt; dc_index++) { struct datacenter *dc = array_get(&pool->datacenters, dc_index); if (dc == NULL) { log_error("Wow, this is very bad, dc is NULL"); return; } if (string_compare(dc->name, &pool->dc) == 0) req_forward_local_dc(ctx, c_conn, msg, orig_mbuf, key, keylen, dc); else if (request_send_to_all_dcs(msg)) { req_forward_remote_dc(ctx, c_conn, msg, orig_mbuf, key, keylen, dc); } } }
static void req_forward(struct context *ctx, struct conn *c_conn, struct msg *msg) { struct server_pool *pool = c_conn->owner; uint8_t *key; uint32_t keylen; ASSERT(c_conn->client && !c_conn->proxy); if (msg->is_read) stats_pool_incr(ctx, pool, client_read_requests); else stats_pool_incr(ctx, pool, client_write_requests); key = NULL; keylen = 0; if (!string_empty(&pool->hash_tag)) { struct string *tag = &pool->hash_tag; uint8_t *tag_start, *tag_end; tag_start = dn_strchr(msg->key_start, msg->key_end, tag->data[0]); if (tag_start != NULL) { tag_end = dn_strchr(tag_start + 1, msg->key_end, tag->data[1]); if (tag_end != NULL) { key = tag_start + 1; keylen = (uint32_t)(tag_end - key); } } } if (keylen == 0) { key = msg->key_start; keylen = (uint32_t)(msg->key_end - msg->key_start); } // need to capture the initial mbuf location as once we add in the dynomite headers (as mbufs to the src msg), // that will bork the request sent to secondary racks struct mbuf *orig_mbuf = STAILQ_FIRST(&msg->mhdr); if (request_send_to_all_racks(msg)) { uint32_t dc_cnt = array_n(&pool->datacenters); uint32_t dc_index; for(dc_index = 0; dc_index < dc_cnt; dc_index++) { struct datacenter *dc = array_get(&pool->datacenters, dc_index); if (dc == NULL) { log_error("Wow, this is very bad, dc is NULL"); return; } if (string_compare(dc->name, &pool->dc) == 0) { //send to all local racks //log_debug(LOG_DEBUG, "dc name '%.*s'", dc->name->len, dc->name->data); uint32_t rack_cnt = array_n(&dc->racks); uint32_t rack_index; for(rack_index = 0; rack_index < rack_cnt; rack_index++) { struct rack *rack = array_get(&dc->racks, rack_index); //log_debug(LOG_DEBUG, "rack name '%.*s'", rack->name->len, rack->name->data); struct msg *rack_msg; if (string_compare(rack->name, &pool->rack) == 0 ) { rack_msg = msg; } else { rack_msg = msg_get(c_conn, msg->request, msg->redis); if (rack_msg == NULL) { log_debug(LOG_VERB, "whelp, looks like yer screwed now, buddy. no inter-rack messages for you!"); continue; } msg_clone(msg, orig_mbuf, rack_msg); rack_msg->noreply = true; } log_debug(LOG_DEBUG, "forwarding request to conn '%s' on rack '%.*s'", dn_unresolve_peer_desc(c_conn->sd), rack->name->len, rack->name->data); remote_req_forward(ctx, c_conn, rack_msg, rack, key, keylen); } } else { uint32_t rack_cnt = array_n(&dc->racks); if (rack_cnt == 0) continue; uint32_t ran_index = rand() % rack_cnt; struct rack *rack = array_get(&dc->racks, ran_index); struct msg *rack_msg = msg_get(c_conn, msg->request, msg->redis); if (rack_msg == NULL) { log_debug(LOG_VERB, "whelp, looks like yer screwed now, buddy. no inter-rack messages for you!"); continue; } msg_clone(msg, orig_mbuf, rack_msg); rack_msg->noreply = true; log_debug(LOG_DEBUG, "forwarding request to conn '%s' on rack '%.*s'", dn_unresolve_peer_desc(c_conn->sd), rack->name->len, rack->name->data); remote_req_forward(ctx, c_conn, rack_msg, rack, key, keylen); } } } else { //for read only requests struct rack * rack = server_get_rack_by_dc_rack(pool, &pool->rack, &pool->dc); remote_req_forward(ctx, c_conn, msg, rack, key, keylen); } }