static int cdb_readv(knot_db_t *db, knot_db_val_t *key, knot_db_val_t *val, int maxcount) { if (!db || !key || !val) { return kr_error(EINVAL); } struct memcached_cli *cli = db; /* Convert to libmemcached query format */ assert(maxcount < 1000); /* Sane upper bound */ const char *keys [maxcount]; size_t lengths [maxcount]; for (int i = 0; i < maxcount; ++i) { keys[i] = key[i].data; lengths[i] = key[i].len; } /* Execute multiple get and retrieve results */ memcached_return_t status = memcached_mget(cli->handle, keys, lengths, maxcount); memcached_result_free(&cli->res); memcached_result_create(cli->handle, &cli->res); for (int i = 0; i < maxcount; ++i) { memcached_result_st *res = memcached_fetch_result(cli->handle, &cli->res, &status); if (!res) { /* Less results than expected */ return kr_error(ENOENT); } val[i].len = memcached_result_length(res); val[i].data = (void *)memcached_result_value(res); } return 0; }
virtual void execute(memcached_st* mem) { this->debug && printf("%s\n", "FetchResultJob execute"); memcached_result_st* res; while(1) { res = memcached_fetch_result(mem, NULL, &rc); if (res == NULL) break; results.push_back(res); } }
void test_memcached_cas(void) { memcached_return rc; const char *key = "caskey"; size_t key_len = strlen(key); const char* keys[2] = { (char *)key, NULL }; size_t key_lens[2] = { key_len, 0 }; memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1); grn_test_memcached_assert( memcached_set(memc, key, key_len, "cas test", 8, 0, 0xdeadbeefU), cut_message("memcached set failed.")); { uint64_t cas; memcached_result_st *results; memcached_result_st results_obj; results = memcached_result_create(memc, &results_obj); grn_test_memcached_assert( memcached_mget(memc, keys, key_lens, 1), cut_message("memcached mget failed.")); results = memcached_fetch_result(memc, &results_obj, &rc); cut_assert_not_null(results, cut_message("memcached fetch result failed.")); cas = memcached_result_cas(results); cut_assert_operator(cas, !=, 0, cut_message("memcached cas value is non-zero.")); grn_test_memcached_assert( memcached_cas(memc, key, key_len, "cas changed", 12, 0, 0, cas), cut_message("memcached cas failed.")); grn_test_memcached_assert_equal_rc( MEMCACHED_NOTSTORED, memcached_cas(memc, key, key_len, "cas changed", 12, 0, 0, cas), cut_message("memcached cas value is same.")); memcached_result_free(&results_obj); } }
void test_memcached_cas(void) { memcached_return rc; const char *key = "caskey"; size_t key_len = strlen(key); char* keys[2] = { (char *)key, NULL }; size_t key_lens[2] = { key_len, 0 }; memcached_behavior_set(memc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, 1); rc = memcached_set(memc, key, key_len, "cas test", 8, 0, 0xdeadbeefU); cut_set_message("memcached set failed."); cut_assert_equal_int(MEMCACHED_SUCCESS, rc); { uint64_t cas; memcached_result_st *results; memcached_result_st results_obj; results = memcached_result_create(memc, &results_obj); rc = memcached_mget(memc, keys, key_lens, 1); cut_set_message("memcached mget failed."); cut_assert_equal_int(MEMCACHED_SUCCESS, rc); results = memcached_fetch_result(memc, &results_obj, &rc); cas = memcached_result_cas(results); cut_set_message("memcached cas value is non-zero."); cut_assert_operator_int(cas, !=, 0); rc = memcached_cas(memc, key, key_len, "cas changed", 12, 0, 0, cas); cut_set_message("memcached cas failed."); cut_assert_equal_int(MEMCACHED_SUCCESS, rc); rc = memcached_cas(memc, key, key_len, "cas changed", 12, 0, 0, cas); cut_set_message("memcached cas value is same."); /* TODO: fix rc after libmemcached fix */ cut_assert_equal_int(MEMCACHED_PROTOCOL_ERROR, rc); /* cut_assert_equal_int(MEMCACHED_DATA_EXISTS, rc); */ memcached_result_free(&results_obj); } }
/// Retrieve the AoR data for a given SIP URI, creating it if there isn't /// any already, and returning NULL if we can't get a connection. AoR* MemcachedStore::get_aor_data(const std::string& aor_id) ///< the SIP URI { memcached_return_t rc; MemcachedAoR* aor_data = NULL; // Try to get a connection struct timespec wait_time; wait_time.tv_sec = 0; wait_time.tv_nsec = 100 * 1000 * 1000; memcached_st* st = memcached_pool_fetch(_pool, &wait_time, &rc); if (st != NULL) { // Got one: use it. const char* key_ptr = aor_id.data(); const size_t key_len = aor_id.length(); rc = memcached_mget(st, &key_ptr, &key_len, 1); if (memcached_success(rc)) { memcached_result_st result; memcached_result_create(st, &result); memcached_fetch_result(st, &result, &rc); if (memcached_success(rc)) { aor_data = deserialize_aor(std::string(memcached_result_value(&result), memcached_result_length(&result))); aor_data->set_cas(memcached_result_cas(&result)); int now = time(NULL); expire_bindings(aor_data, now); } else { // AoR does not exist, so create it. aor_data = new MemcachedAoR(); } } memcached_pool_release(_pool, st); } return (AoR*)aor_data; }
memcached_return_t BaseMemcachedStore::get_from_replica(memcached_st* replica, const char* key_ptr, const size_t key_len, std::string& data, uint64_t& cas) { memcached_return_t rc = MEMCACHED_ERROR; cas = 0; // We must use memcached_mget because memcached_get does not retrieve CAS // values. rc = memcached_mget(replica, &key_ptr, &key_len, 1); if (memcached_success(rc)) { // memcached_mget command was successful, so retrieve the result. TRC_DEBUG("Fetch result"); memcached_result_st result; memcached_result_create(replica, &result); memcached_fetch_result(replica, &result, &rc); if (memcached_success(rc)) { // Found a record, so exit the read loop. TRC_DEBUG("Found record on replica"); // Copy the record into a string. std::string::assign copies its // arguments when used with a char*, so we can free the result // afterwards. data.assign(memcached_result_value(&result), memcached_result_length(&result)); cas = memcached_result_cas(&result); } memcached_result_free(&result); } return rc; }
Variant c_Memcache::t_get(CVarRef key, VRefParam flags /*= null*/) { if (key.is(KindOfArray)) { std::vector<const char *> real_keys; std::vector<size_t> key_len; Array keyArr = key.toArray(); real_keys.reserve(keyArr.size()); key_len.reserve(keyArr.size()); for (ArrayIter iter(keyArr); iter; ++iter) { real_keys.push_back(const_cast<char *>(iter.second().toString().c_str())); key_len.push_back(iter.second().toString().length()); } if (!real_keys.empty()) { const char *payload = NULL; size_t payload_len = 0; uint32_t flags = 0; const char *res_key = NULL; size_t res_key_len = 0; memcached_result_st result; memcached_return_t ret = memcached_mget(&m_memcache, &real_keys[0], &key_len[0], real_keys.size()); memcached_result_create(&m_memcache, &result); Array return_val; while ((memcached_fetch_result(&m_memcache, &result, &ret)) != NULL) { if (ret != MEMCACHED_SUCCESS) { // should probably notify about errors continue; } payload = memcached_result_value(&result); payload_len = memcached_result_length(&result); flags = memcached_result_flags(&result); res_key = memcached_result_key_value(&result); res_key_len = memcached_result_key_length(&result); return_val.set(String(res_key, res_key_len, CopyString), memcache_fetch_from_storage(payload, payload_len, flags)); } memcached_result_free(&result); return return_val; } } else { char *payload = NULL; size_t payload_len = 0; uint32_t flags = 0; memcached_return_t ret; String skey = key.toString(); if (skey.length() == 0) { return false; } payload = memcached_get(&m_memcache, skey.c_str(), skey.length(), &payload_len, &flags, &ret); /* This is for historical reasons from libmemcached*/ if (ret == MEMCACHED_END) { ret = MEMCACHED_NOTFOUND; } if (ret == MEMCACHED_NOTFOUND) { return false; } Variant retval = memcache_fetch_from_storage(payload, payload_len, flags); free(payload); return retval; } return false; }
static Variant HHVM_METHOD(Memcache, get, const Variant& key, VRefParam flags /*= null*/) { auto data = Native::data<MemcacheData>(this_); if (key.is(KindOfArray)) { std::vector<const char *> real_keys; std::vector<size_t> key_len; Array keyArr = key.toArray(); real_keys.reserve(keyArr.size()); key_len.reserve(keyArr.size()); for (ArrayIter iter(keyArr); iter; ++iter) { auto key = iter.second().toString(); String serializedKey = memcache_prepare_key(key); char *k = new char[serializedKey.length()+1]; std::strcpy(k, serializedKey.c_str()); real_keys.push_back(k); key_len.push_back(serializedKey.length()); } if (!real_keys.empty()) { const char *payload = NULL; size_t payload_len = 0; uint32_t flags = 0; const char *res_key = NULL; size_t res_key_len = 0; memcached_result_st result; memcached_return_t ret = memcached_mget(&data->m_memcache, &real_keys[0], &key_len[0], real_keys.size()); memcached_result_create(&data->m_memcache, &result); Array return_val; while ((memcached_fetch_result(&data->m_memcache, &result, &ret)) != nullptr) { if (ret != MEMCACHED_SUCCESS) { // should probably notify about errors continue; } payload = memcached_result_value(&result); payload_len = memcached_result_length(&result); flags = memcached_result_flags(&result); res_key = memcached_result_key_value(&result); res_key_len = memcached_result_key_length(&result); return_val.set(String(res_key, res_key_len, CopyString), memcache_fetch_from_storage(payload, payload_len, flags)); } memcached_result_free(&result); for ( size_t i = 0 ; i < real_keys.size() ; i++ ) { delete [] real_keys[i]; } return return_val; } } else { char *payload = NULL; size_t payload_len = 0; uint32_t flags = 0; memcached_return_t ret; String serializedKey = memcache_prepare_key(key.toString()); if (serializedKey.length() == 0) { return false; } payload = memcached_get(&data->m_memcache, serializedKey.c_str(), serializedKey.length(), &payload_len, &flags, &ret); /* This is for historical reasons from libmemcached*/ if (ret == MEMCACHED_END) { ret = MEMCACHED_NOTFOUND; } if (ret == MEMCACHED_NOTFOUND) { return false; } Variant retval = memcache_fetch_from_storage(payload, payload_len, flags); free(payload); return retval; } return false; }
/// Retrieve the data for a given namespace and key. Store::Status MemcachedStore::get_data(const std::string& table, const std::string& key, std::string& data, uint64_t& cas, SAS::TrailId trail) { Store::Status status = Store::Status::OK; // Construct the fully qualified key. std::string fqkey = table + "\\\\" + key; const char* key_ptr = fqkey.data(); const size_t key_len = fqkey.length(); const std::vector<memcached_st*>& replicas = get_replicas(fqkey, Op::READ); if (trail != 0) { SAS::Event start(trail, SASEvent::MEMCACHED_GET_START, 0); start.add_var_param(fqkey); SAS::report_event(start); } LOG_DEBUG("%d read replicas for key %s", replicas.size(), fqkey.c_str()); // Read from all replicas until we get a positive result. memcached_return_t rc = MEMCACHED_ERROR; bool active_not_found = false; size_t failed_replicas = 0; size_t ii; // If we only have one replica, we should try it twice - // libmemcached won't notice a dropped TCP connection until it tries // to make a request on it, and will fail the request then // reconnect, so the second attempt could still work. size_t attempts = (replicas.size() == 1) ? 2 : replicas.size(); for (ii = 0; ii < attempts; ++ii) { size_t replica_idx; if ((replicas.size() == 1) && (ii == 1)) { if (rc != MEMCACHED_CONNECTION_FAILURE) { // This is a legitimate error, not a server failure, so we // shouldn't retry. break; } replica_idx = 0; LOG_WARNING("Failed to read from sole memcached replica: retrying once"); } else { replica_idx = ii; } // We must use memcached_mget because memcached_get does not retrieve CAS // values. LOG_DEBUG("Attempt to read from replica %d (connection %p)", replica_idx, replicas[replica_idx]); rc = memcached_mget(replicas[replica_idx], &key_ptr, &key_len, 1); if (memcached_success(rc)) { // memcached_mget command was successful, so retrieve the result. LOG_DEBUG("Fetch result"); memcached_result_st result; memcached_result_create(replicas[replica_idx], &result); memcached_fetch_result(replicas[replica_idx], &result, &rc); if (memcached_success(rc)) { // Found a record, so exit the read loop. LOG_DEBUG("Found record on replica %d", replica_idx); data.assign(memcached_result_value(&result), memcached_result_length(&result)); cas = (active_not_found) ? 0 : memcached_result_cas(&result); // std::string::assign copies its arguments when used with a // char*, so this is safe. memcached_result_free(&result); break; } else { // Free the result and continue the read loop. memcached_result_free(&result); } } if (rc == MEMCACHED_NOTFOUND) { // Failed to find a record on an active replica. Flag this so if we do // find data on a later replica we can reset the cas value returned to // zero to ensure a subsequent write will succeed. LOG_DEBUG("Read for %s on replica %d returned NOTFOUND", fqkey.c_str(), replica_idx); active_not_found = true; } else { // Error from this node, so consider it inactive. LOG_DEBUG("Read for %s on replica %d returned error %d (%s)", fqkey.c_str(), replica_idx, rc, memcached_strerror(replicas[replica_idx], rc)); ++failed_replicas; } } if (memcached_success(rc)) { if (trail != 0) { SAS::Event got_data(trail, SASEvent::MEMCACHED_GET_SUCCESS, 0); got_data.add_var_param(fqkey); got_data.add_var_param(data); got_data.add_static_param(cas); SAS::report_event(got_data); } // Return the data and CAS value. The CAS value is either set to the CAS // value from the result, or zero if an earlier active replica returned // NOT_FOUND. This ensures that a subsequent set operation will succeed // on the earlier active replica. LOG_DEBUG("Read %d bytes from table %s key %s, CAS = %ld", data.length(), table.c_str(), key.c_str(), cas); } else if (failed_replicas < replicas.size()) { // At least one replica returned NOT_FOUND. if (trail != 0) { SAS::Event not_found(trail, SASEvent::MEMCACHED_GET_NOT_FOUND, 0); not_found.add_var_param(fqkey); SAS::report_event(not_found); } LOG_DEBUG("At least one replica returned not found, so return NOT_FOUND"); status = Store::Status::NOT_FOUND; } else { // All replicas returned an error, so log the error and return the // failure. if (trail != 0) { SAS::Event err(trail, SASEvent::MEMCACHED_GET_ERROR, 0); err.add_var_param(fqkey); SAS::report_event(err); } LOG_ERROR("Failed to read data for %s from %d replicas", fqkey.c_str(), replicas.size()); status = Store::Status::ERROR; } return status; }
static VALUE mc_get(int argc, VALUE *argv, VALUE self) { memcached_st *mc; VALUE cas, keys, results, key, result; VALUE scalar_key = Qnil; memcached_return status; Data_Get_Struct(self, memcached_st, mc); rb_scan_args(argc, argv, "11", &keys, &cas); memcached_behavior_set(mc, MEMCACHED_BEHAVIOR_SUPPORT_CAS, RTEST(cas) ? 1 : 0); if (RTEST(cas) && TYPE(keys) != T_ARRAY) { scalar_key = keys; keys = rb_ary_new4(1, &keys); } if (TYPE(keys) != T_ARRAY) { char* str; size_t len; uint32_t flags; key = use_binary(mc) ? keys : escape_key(keys, NULL); str = memcached_get(mc, RSTRING_PTR(key), RSTRING_LEN(key), &len, &flags, &status); if (str == NULL) return Qnil; if (status == MEMCACHED_SUCCESS) { result = rb_hash_new(); rb_hash_aset(result, sym_value, rb_str_new(str, len)); rb_hash_aset(result, sym_flags, INT2NUM(flags)); free(str); return result; } else { printf("Memcache read error: %s %u\n", memcached_strerror(mc, status), status); return Qnil; } } else { memcached_result_st* mc_result; size_t num_keys, i; const char** key_strings; size_t* key_lengths; bool escaped; results = rb_hash_new(); num_keys = RARRAY_LEN(keys); if (num_keys == 0) return results; key_strings = (const char**) malloc(num_keys * sizeof(char *)); key_lengths = (size_t *) malloc(num_keys * sizeof(size_t)); for (i = 0; i < RARRAY_LEN(keys); i++) { key = RARRAY_PTR(keys)[i]; if (!use_binary(mc)) key = escape_key(key, &escaped); key_lengths[i] = RSTRING_LEN(key); key_strings[i] = RSTRING_PTR(key); } memcached_mget(mc, key_strings, key_lengths, num_keys); while ((mc_result = memcached_fetch_result(mc, NULL, &status))) { if (escaped) { key = unescape_key(memcached_result_key_value(mc_result), memcached_result_key_length(mc_result)); } else { key = rb_str_new(memcached_result_key_value(mc_result), memcached_result_key_length(mc_result)); } if (status == MEMCACHED_SUCCESS) { result = rb_hash_new(); rb_hash_aset(result, sym_value, rb_str_new(memcached_result_value(mc_result), memcached_result_length(mc_result))); rb_hash_aset(result, sym_flags, INT2NUM(memcached_result_flags(mc_result))); if (RTEST(cas)) rb_hash_aset(result, sym_cas, ULL2NUM(memcached_result_cas(mc_result))); memcached_result_free(mc_result); rb_hash_aset(results, key, result); } else { printf("Memcache read error: %s %u\n", memcached_strerror(mc, status), status); } } free(key_strings); free(key_lengths); if (!NIL_P(scalar_key)) return rb_hash_aref(results, scalar_key); return results; } }