void *cproxy_make_bin_header(conn *c, uint8_t magic) { protocol_binary_response_header *rh = (protocol_binary_response_header *) add_conn_suffix(c); if (rh != NULL) { memset(rh, 0, sizeof(protocol_binary_response_header)); rh->response.magic = magic; rh->response.datatype = (uint8_t) PROTOCOL_BINARY_RAW_BYTES; return rh; } return NULL; }
/* Forward an upstream command that came with item data, * like set/add/replace/etc. */ bool cproxy_forward_a2a_item_downstream(downstream *d, short cmd, item *it, conn *uc) { assert(d != NULL); assert(d->ptd != NULL); assert(d->ptd->proxy != NULL); assert(d->downstream_conns != NULL); assert(it != NULL); assert(uc != NULL); assert(uc->next == NULL); // Assuming we're already connected to downstream. // bool self = false; conn *c = cproxy_find_downstream_conn(d, ITEM_key(it), it->nkey, &self); if (c != NULL) { if (self) { cproxy_optimize_to_self(d, uc, uc->cmd_start); complete_nread_ascii(uc); return true; } if (cproxy_prep_conn_for_write(c)) { assert(c->state == conn_pause); char *verb = nread_text(cmd); assert(verb != NULL); char *str_flags = ITEM_suffix(it); char *str_length = strchr(str_flags + 1, ' '); int len_flags = str_length - str_flags; int len_length = it->nsuffix - len_flags - 2; char *str_exptime = add_conn_suffix(c); char *str_cas = (cmd == NREAD_CAS ? add_conn_suffix(c) : NULL); if (str_flags != NULL && str_length != NULL && len_flags > 1 && len_length > 1 && str_exptime != NULL && (cmd != NREAD_CAS || str_cas != NULL)) { sprintf(str_exptime, " %u", it->exptime); if (str_cas != NULL) sprintf(str_cas, " %llu", (unsigned long long) ITEM_get_cas(it)); if (add_iov(c, verb, strlen(verb)) == 0 && add_iov(c, ITEM_key(it), it->nkey) == 0 && add_iov(c, str_flags, len_flags) == 0 && add_iov(c, str_exptime, strlen(str_exptime)) == 0 && add_iov(c, str_length, len_length) == 0 && (str_cas == NULL || add_iov(c, str_cas, strlen(str_cas)) == 0) && (uc->noreply == false || add_iov(c, " noreply", 8) == 0) && add_iov(c, ITEM_data(it) - 2, it->nbytes + 2) == 0) { conn_set_state(c, conn_mwrite); c->write_and_go = conn_new_cmd; if (update_event(c, EV_WRITE | EV_PERSIST)) { d->downstream_used_start = 1; d->downstream_used = 1; if (cproxy_dettach_if_noreply(d, uc) == false) { cproxy_start_downstream_timeout(d, c); // During a synchronous (with-reply) SET, // handle fire-&-forget SET optimization. // if (cmd == NREAD_SET && cproxy_optimize_set_ascii(d, uc, ITEM_key(it), it->nkey)) { d->ptd->stats.stats.tot_optimize_sets++; } } else { c->write_and_go = conn_pause; mcache_delete(&d->ptd->proxy->front_cache, ITEM_key(it), it->nkey); } return true; } } d->ptd->stats.stats.err_oom++; cproxy_close_conn(c); } else { // TODO: Handle this weird error case. } } else { d->ptd->stats.stats.err_downstream_write_prep++; cproxy_close_conn(c); } if (settings.verbose > 1) fprintf(stderr, "Proxy item write out of memory"); } return false; }
/** * @param cas_emit 1: emit CAS. * 0: do not emit CAS. * -1: data driven. */ void cproxy_upstream_ascii_item_response(item *it, conn *uc, int cas_emit) { assert(it != NULL); assert(uc != NULL); assert(uc->state == conn_pause); assert(uc->funcs != NULL); assert(IS_ASCII(uc->protocol)); assert(IS_PROXY(uc->protocol)); if (settings.verbose > 2) { char key[KEY_MAX_LENGTH + 10]; assert(it->nkey <= KEY_MAX_LENGTH); memcpy(key, ITEM_key(it), it->nkey); key[it->nkey] = '\0'; moxi_log_write("<%d cproxy ascii item response, key %s\n", uc->sfd, key); } if (strncmp(ITEM_data(it) + it->nbytes - 2, "\r\n", 2) == 0) { // TODO: Need to clean up half-written add_iov()'s. // Consider closing the upstream_conns? // uint64_t cas = ITEM_get_cas(it); if ((cas_emit == 0) || (cas_emit < 0 && cas == CPROXY_NOT_CAS)) { if (add_conn_item(uc, it)) { it->refcount++; if (add_iov(uc, "VALUE ", 6) == 0 && add_iov(uc, ITEM_key(it), it->nkey) == 0 && add_iov(uc, ITEM_suffix(it), it->nsuffix + it->nbytes) == 0) { if (settings.verbose > 2) { moxi_log_write("<%d cproxy ascii item response success\n", uc->sfd); } } } } else { char *suffix = add_conn_suffix(uc); if (suffix != NULL) { sprintf(suffix, " %llu\r\n", (unsigned long long) cas); if (add_conn_item(uc, it)) { it->refcount++; if (add_iov(uc, "VALUE ", 6) == 0 && add_iov(uc, ITEM_key(it), it->nkey) == 0 && add_iov(uc, ITEM_suffix(it), it->nsuffix - 2) == 0 && add_iov(uc, suffix, strlen(suffix)) == 0 && add_iov(uc, ITEM_data(it), it->nbytes) == 0) { if (settings.verbose > 2) { moxi_log_write("<%d cproxy ascii item response ok\n", uc->sfd); } } } } } } else { if (settings.verbose > 1) { moxi_log_write("ERROR: unexpected downstream data block"); } } }