void TestClient::sendGet(std::string key, mc_res_t expectedResult,
                         uint32_t timeoutMs) {
  inflight_++;
  fm_.addTask([key, expectedResult, this, timeoutMs]() {
      auto msg = createMcMsgRef(key.c_str());
      msg->op = mc_op_get;
      McRequestWithMcOp<mc_op_get> req{std::move(msg)};
      try {
        auto reply = client_->sendSync(req,
                                       std::chrono::milliseconds(timeoutMs));
        if (reply.result() == mc_res_found) {
          auto value = getRange(reply.value());
          if (req.fullKey() == "empty") {
            checkLogic(reply.hasValue(), "Reply has no value");
            checkLogic(value.empty(), "Expected empty value, got {}", value);
          } else {
            checkLogic(value == req.fullKey(),
                       "Expected {}, got {}", req.fullKey(), value);
          }
        }
        checkLogic(expectedResult == reply.result(), "Expected {}, got {}",
                   mc_res_to_string(expectedResult),
                   mc_res_to_string(reply.result()));
      } catch (const std::exception& e) {
        CHECK(false) << "Failed: " << e.what();
      }
      inflight_--;
    });
}
std::string MessagePrinter::serializeMessageHeader(
    folly::StringPiece messageName,
    mc_res_t result,
    const std::string& key) {
  std::string out;

  if (options_.script) {
    out.append(folly::sformat("\"type\": \"{}\"", messageName.data()));
    if (result != mc_res_unknown) {
      out.append(
          folly::sformat(",\n  \"result\": \"{}\"", mc_res_to_string(result)));
    }
    if (!key.empty()) {
      out.append(
          folly::sformat(",\n  \"key\": \"{}\"", folly::backslashify(key)));
    }
  } else {
    out.append(messageName.data());
    if (result != mc_res_unknown) {
      out.push_back(' ');
      out.append(mc_res_to_string(result));
    }
    if (!key.empty()) {
      out.push_back(' ');
      out.append(folly::backslashify(key));
    }
  }

  return out;
}
Example #3
0
void logShadowValidationError(proxy_t& proxy,
                              const ShadowValidationData& valData) {
  VLOG_EVERY_N(1,100)
      << "Mismatch between shadow and normal reply" << std::endl
      << "Key:" << valData.fullKey << std::endl
      << "Expected Result:"
      << mc_res_to_string(valData.normalResult) << std::endl
      << "Shadow Result:"
      << mc_res_to_string(valData.shadowResult) << std::endl;
}
Example #4
0
static std::unordered_map<std::string, mc_res_t> makeStringToMcRes() {
  std::unordered_map<std::string, mc_res_t> resMap;
  for (mc_res_t i = mc_res_unknown; i < mc_nres; i = mc_res_t(i + 1)) {
    resMap[mc_res_to_string(i)] = i;
  }
  return resMap;
}
Example #5
0
static String HHVM_STATIC_METHOD(MCRouter, getResultName, int64_t res) {
  auto name = mc_res_to_string((mc_res_t)res);
  if (!name) {
    std::string msg = "Unknown mc_res_* value: ";
    msg += res;
    mcr_throwException(msg, mc_op_unknown, (mc_res_t)res);
  }
  return name;
}
void TestClient::sendSet(std::string key, std::string value,
                         mc_res_t expectedResult) {
  inflight_++;
  fm_.addTask([key, value, expectedResult, this]() {
      auto msg = createMcMsgRef(key.c_str(), value.c_str());
      msg->op = mc_op_set;
      McRequestWithMcOp<mc_op_set> req{std::move(msg)};

      auto reply = client_->sendSync(req,
                                     std::chrono::milliseconds(200));

      CHECK(expectedResult == reply.result())
        << "Expected: " << mc_res_to_string(expectedResult)
        << " got " << mc_res_to_string(reply.result());

      inflight_--;
    });
}
Example #7
0
  void moduleInit() override {
    HHVM_ME(MCRouter, __construct);

    HHVM_NAMED_ME(MCRouter, get,  mcr_str<mc_op_get>);
    HHVM_NAMED_ME(MCRouter, gets, mcr_str<mc_op_gets>);

    HHVM_NAMED_ME(MCRouter, add, mcr_set<mc_op_add>);
    HHVM_NAMED_ME(MCRouter, set, mcr_set<mc_op_set>);
    HHVM_NAMED_ME(MCRouter, replace, mcr_set<mc_op_replace>);
    HHVM_NAMED_ME(MCRouter, prepend, mcr_aprepend<mc_op_prepend>);
    HHVM_NAMED_ME(MCRouter, append, mcr_aprepend<mc_op_append>);

    HHVM_NAMED_ME(MCRouter, incr, mcr_str_delta<mc_op_incr>);
    HHVM_NAMED_ME(MCRouter, decr, mcr_str_delta<mc_op_decr>);

    HHVM_NAMED_ME(MCRouter, del, mcr_str<mc_op_delete>);
    HHVM_NAMED_ME(MCRouter, flushAll, mcr_int<mc_op_flushall>);

    HHVM_NAMED_ME(MCRouter, version, mcr_void<mc_op_version>);

    HHVM_ME(MCRouter, cas);

    Native::registerNativeDataInfo<MCRouter>(s_MCRouter.get());

    HHVM_STATIC_ME(MCRouter, getOpName);
    HHVM_STATIC_ME(MCRouter, getResultName);

    std::string opname("mc_op_");
    for (int i = 0; i < mc_nops; ++i) {
      std::string name;
      name = opname + mc_op_to_string((mc_op_t)i);
      // mcrouter defines op names as foo-bar,
      // but PHP wants constants like foo_bar
      for (int j = opname.size(); j < name.size(); ++j) {
        if (name[j] == '-') {
          name[j] = '_';
        }
      }
      Native::registerClassConstant<KindOfInt64>(
        s_MCRouter.get(),
        makeStaticString(name),
        i);
    }
    for (int i = 0; i < mc_nres; ++i) {
      Native::registerClassConstant<KindOfInt64>(
        s_MCRouter.get(),
        makeStaticString(mc_res_to_string((mc_res_t)i)),
        i);
    }

    loadSystemlib();
  }
Example #8
0
 // Store the important parts of the exception in non-thread vars
 // to bubble up during unserialize
 void setResultException(const mcr::mcrouter_msg_t* msg) {
   m_op = msg->req->op;
   m_replyCode = msg->reply.result();
   m_exception  = mc_op_to_string(m_op);
   m_exception += " failed with result ";
   m_exception += mc_res_to_string(m_replyCode);
   if (msg->reply.value().length() > 0) {
     m_exception += ": ";
     m_exception += std::string((char*)msg->reply.value().data(),
                                msg->reply.value().length());
   }
   m_key = std::string(msg->req->key.str, msg->req->key.len);
 }
void FailoverErrorsSettings::List::init(std::vector<std::string> errors) {
  failover_ = folly::make_unique<std::array<bool, mc_nres>>();

  for (const auto& error : errors) {
    int i;
    for (i = 0; i < mc_nres; ++i) {
      mc_res_t errorType = static_cast<mc_res_t>(i);
      folly::StringPiece errorName(mc_res_to_string(errorType));
      errorName.removePrefix("mc_res_");

      if (mc_res_is_err(errorType) && error == errorName) {
        (*failover_)[i] = true;
        break;
      }
    }

    checkLogic(i < mc_nres,
        "Failover error '{}' is not a valid error type.", error);
  }
}
Example #10
0
std::string proxy_client_stat_to_str(void* ptr) {
  ProxyDestination* pdstn = (ProxyDestination*) ptr;

  uint64_t peak = 0;
  if (pdstn->stats.rtt_timer) {
    peak = fb_timer_get_recent_peak(pdstn->stats.rtt_timer);
  }

  std::string out = folly::stringPrintf(
    SERVER_STATS_FORMAT,
    proxy_client_state_to_string(pdstn->state()), peak);

  for (int ii = 0; ii < mc_nres; ii ++) {
    if (pdstn->stats.results[ii]) {
      out += folly::stringPrintf(
        SERVER_STATS_RESULT_FORMAT,
        mc_res_to_string((mc_res_t)ii) + sizeof("mc_res"),
        pdstn->stats.results[ii]);
    }
  }

  return out;
}
Example #11
0
std::string MessagePrinter::serializeMessageHeader(mc_op_t op,
                                                   mc_res_t result,
                                                   const std::string& key) {
  std::string out;

  if (op != mc_op_unknown) {
    out.append(mc_op_to_string(op));
  }
  if (result != mc_res_unknown) {
    if (out.size() > 0) {
      out.push_back(' ');
    }
    out.append(mc_res_to_string(result));
  }
  if (key.size()) {
    if (out.size() > 0) {
      out.push_back(' ');
    }
    out.append(folly::backslashify(key));
  }

  return out;
}
Example #12
0
size_t mc_ascii_response_write_iovs(mc_ascii_response_buf_t* buf,
                                    const mc_msg_t* req,
                                    const mc_msg_t* reply,
                                    struct iovec* iovs,
                                    size_t max_niovs) {
    size_t niovs = 0;
    buf->offset = 0;

    if (mc_res_is_err(reply->result)) {
        if (reply->value.len > 0) {
            if (reply->result == mc_res_client_error) {
                IOV_WRITE_CONST_STR("CLIENT_ERROR ");
            } else {
                IOV_WRITE_CONST_STR("SERVER_ERROR ");
            }
            if (reply->err_code != 0) {
                IOV_FORMAT(buf, "%" PRIu32 " ", reply->err_code);
            }
            IOV_WRITE_NSTRING(reply->value);
            IOV_WRITE_CONST_STR("\r\n");
        } else {
            IOV_WRITE_STR(mc_res_to_response_string(reply->result));
        }
        return niovs;
    }

    switch (req->op) {
    case mc_op_incr:
    case mc_op_decr:
        switch (reply->result) {
        case mc_res_stored:
            IOV_FORMAT(buf, "%" PRIu64 "\r\n", reply->delta);
            break;
        case mc_res_notfound:
            IOV_WRITE_CONST_STR("NOT_FOUND\r\n");
            break;
        default:
            goto UNEXPECTED;
        }
        break;

    case mc_op_set:
    case mc_op_lease_set:
    case mc_op_add:
    case mc_op_replace:
    case mc_op_append:
    case mc_op_prepend:
    case mc_op_cas:
        switch (reply->result) {
        case mc_res_ok:
            IOV_WRITE_STR(mc_res_to_response_string(mc_res_stored));
            break;

        case mc_res_stored:
        case mc_res_stalestored:
        case mc_res_found:
        case mc_res_notstored:
        case mc_res_notfound:
        case mc_res_exists:
            IOV_WRITE_STR(mc_res_to_response_string(reply->result));
            break;

        default:
            goto UNEXPECTED;
        }
        break;

    case mc_op_delete:
        switch (reply->result) {
        case mc_res_deleted:
        case mc_res_notfound:
            IOV_WRITE_STR(mc_res_to_response_string(reply->result));
            break;
        default:
            goto UNEXPECTED;
        }

        break;

    case mc_op_get:
    case mc_op_lease_get:
    case mc_op_gets:
        switch (reply->result) {
        case mc_res_found:
            IOV_WRITE_CONST_STR("VALUE ");
            IOV_WRITE_NSTRING(req->key);
            IOV_FORMAT(buf, " %" PRIu64 " %lu", reply->flags,
                       reply->value.len);
            if (req->op == mc_op_gets) {
                IOV_FORMAT(buf, " %" PRIu64, reply->cas);
            }
            IOV_WRITE_CONST_STR("\r\n");
            IOV_WRITE_NSTRING(reply->value);
            IOV_WRITE_CONST_STR("\r\n");
            break;

        case mc_res_notfound:
            if (req->op != mc_op_lease_get) {
                // misses should have been suppressed!
                goto UNEXPECTED;
            }
            // but lease-get always has a response
            IOV_WRITE_CONST_STR("LVALUE ");
            IOV_WRITE_NSTRING(req->key);
            IOV_FORMAT(buf, " %" PRIu64 " %"PRIu64 " %zu\r\n",
                       reply->lease_id,
                       reply->flags,
                       reply->value.len);
            IOV_WRITE_NSTRING(reply->value);
            IOV_WRITE_CONST_STR("\r\n");
            break;

        default:
            goto UNEXPECTED;
        }
        break;

    case mc_op_metaget:
        switch (reply->result) {
        case mc_res_found:
            /* (META key age: (unknown|\d+); exptime: \d+;
               from: (\d+\.\d+\.\d+\.\d+|unknown); is_transient: (1|0)\r\n) */
            IOV_WRITE_CONST_STR("META ");
            IOV_WRITE_NSTRING(req->key);
            IOV_WRITE_CONST_STR(" age: ");
            if (reply->number == (uint32_t) -1) {
                IOV_WRITE_CONST_STR("unknown");
            }
            else {
                IOV_FORMAT(buf, "%d", reply->number);
            }
            IOV_WRITE_CONST_STR("; exptime: ");
            IOV_FORMAT(buf, "%d", reply->exptime);
            IOV_WRITE_CONST_STR("; from: ");
            if (reply->ipv == 0) {
                IOV_WRITE_CONST_STR("unknown");
            }
            else {
                IOV_WRITE_IP(buf, reply->ipv, &(reply->ip_addr));
            }
            IOV_WRITE_CONST_STR("; is_transient: ");
            IOV_FORMAT(buf, "%" PRIu64, reply->flags);
            IOV_WRITE_CONST_STR("\r\n");
            break;
        case mc_res_notfound:
            break;
        default:
            goto UNEXPECTED;
        }
        break;

    case mc_op_end:
        if (reply->result == mc_res_found) {
            IOV_WRITE_CONST_STR("END\r\n");
        }
        else {
            IOV_WRITE_STR(mc_res_to_response_string(reply->result));
        }
        break;

    case mc_op_stats:
        switch (reply->result) {
        case mc_res_ok:
        {
            size_t length = 0;
            char* stats;
            if (reply->stats) {
                /* TODO(agartrell) assert(!reply->value.str)
                 *
                 * The assert here can't be turned on until
                 * mcrouter/stats.c:560 has been fixed to not set both
                 * value and stats on the libmc reply
                 */
                stats = stats_reply_to_string(reply->stats, reply->number, &length);
                buf->stats = stats;
            } else {
                stats = reply->value.str;
                length = reply->value.len;
            }

            if (!stats) {
                return 0;
            }

            IOV_WRITE(stats, length);
            break;
        }
        default:
            goto UNEXPECTED;
        }
        break;

    case mc_op_flushall:
    case mc_op_flushre:
        IOV_WRITE_CONST_STR("OK\r\n");
        break;

    case mc_op_version:
        IOV_WRITE_CONST_STR("VERSION ");
        IOV_WRITE_NSTRING(reply->value);
        IOV_WRITE_CONST_STR("\r\n");
        break;

    case mc_op_shutdown:
        if (reply->result == mc_res_ok) {
            IOV_WRITE_CONST_STR("OK\r\n");
        }
        else {
            goto UNEXPECTED;
        }
        break;

    case mc_op_exec:
        switch (reply->result) {
        case mc_res_ok:
            IOV_WRITE_NSTRING(reply->value);
            IOV_WRITE_CONST_STR("\r\n");
            break;
        default:
            goto UNEXPECTED;
        }
        break;

    default:
        IOV_WRITE_CONST_STR("SERVER_ERROR unhandled token ");
        IOV_WRITE_STR(mc_op_to_string(req->op));
        IOV_FORMAT(buf, " (%d)\r\n", (int)req->op);
        break;
    }

    return niovs;

UNEXPECTED:
    FBI_ASSERT(niovs == 0);
    IOV_WRITE_CONST_STR("SERVER_ERROR unexpected result ");
    IOV_WRITE_STR(mc_res_to_string(reply->result));
    IOV_FORMAT(buf, " (%d) for ", (int)reply->result);
    IOV_WRITE_STR(mc_op_to_string(req->op));
    IOV_FORMAT(buf, " (%d)\r\n", (int)req->op);
    return niovs;
}