Exemple #1
0
/** Put a document into the database at this key
 * If the "Prefers: ETag" header is set, we perform a CAS operation */
static int __put(h2o_req_t *req, kstr_t* key)
{
    char* sv_etag = __remove_stored_etag(key);
    h2o_iovec_t cli_etag = __get_if_match_header_value(req, key);
    if (0 < cli_etag.len &&
        !__should_etag_conditional_put_succeed(req, key, sv_etag, &cli_etag))
    {
        if (sv_etag)
            free(sv_etag);
        return h2oh_respond_with_error(req, 412, "BAD ETAG");
    }

    if (sv_etag)
        free(sv_etag);

    batch_item_t item = {
        .flags       = 0,
        .key.mv_data = key->s,
        .key.mv_size = key->len,
        .val.mv_data = req->entity.base,
        .val.mv_size = req->entity.len,
    };

    int e = bmon_offer(&sv->batch, &item);
    if (0 != e)
        return h2oh_respond_with_error(req, 400, batcher_error);
    return h2oh_respond_with_success(req, 200);
}

typedef struct
{
    h2o_generator_t super;
    h2o_req_t *req;
    MDB_txn *txn;
    MDB_cursor* curs;
    kstr_t* key;
} get_keys_generator_t;

static void __get_keys_close(h2o_generator_t *_self, h2o_req_t *req)
{
    get_keys_generator_t *gen = (void*)_self;

    mdb_cursor_close(gen->curs);

    int e = mdb_txn_commit(gen->txn);
    if (0 != e)
        mdb_fatal(e);
}
Exemple #2
0
/** HTTP POST entry point for receiving entries from client
 * Provide the user with an ID */
static int __http_get_id(h2o_handler_t *self, h2o_req_t *req)
{
    static h2o_generator_t generator = { NULL, NULL };

    if (!h2o_memis(req->method.base, req->method.len, H2O_STRLIT("POST")))
        return -1;

    /* redirect to leader if needed */
    int leader = raft_get_current_leader(sv->raft);
    if (-1 == leader)
    {
        return h2oh_respond_with_error(req, 503, "Leader unavailable");
    }
    else if (leader != sv->node_idx)
    {
        raft_node_t* node = raft_get_node(sv->raft, leader);
        peer_connection_t* leader_conn = raft_node_get_udata(node);
        char leader_url[LEADER_URL_LEN];

        static h2o_generator_t generator = { NULL, NULL };
        static h2o_iovec_t body = { .base = "", .len = 0 };
        req->res.status = 301;
        req->res.reason = "Moved Permanently";
        h2o_start_response(req, &generator);
        snprintf(leader_url, LEADER_URL_LEN, "http://%s:%d/",
                 inet_ntoa(leader_conn->addr.sin_addr),
                 leader_conn->http_port);
        h2o_add_header(&req->pool,
                       &req->res.headers,
                       H2O_TOKEN_LOCATION,
                       leader_url,
                       strlen(leader_url));
        h2o_send(req, &body, 1, 1);
        return 0;
    }

    int e;

    unsigned int ticket = __generate_ticket();

    msg_entry_t entry;
    entry.id = rand();
    entry.data.buf = (void*)&ticket;
    entry.data.len = sizeof(ticket);

    uv_mutex_lock(&sv->raft_lock);

    msg_entry_response_t r;
    e = raft_recv_entry(sv->raft, sv->node_idx, &entry, &r);
    if (0 != e)
        return h2oh_respond_with_error(req, 500, "BAD");

    /* block until the entry is committed */
    int done = 0;
    do
    {
        uv_cond_wait(&sv->appendentries_received, &sv->raft_lock);
        e = raft_msg_entry_response_committed(sv->raft, &r);
        switch (e)
        {
        case 0:
            /* not committed yet */
            break;
        case 1:
            done = 1;
            uv_mutex_unlock(&sv->raft_lock);
            break;
        case -1:
            uv_mutex_unlock(&sv->raft_lock);
            return h2oh_respond_with_error(req, 400, "TRY AGAIN");
        }
    }
    while (!done);

    /* serialize ID */
    char id_str[100];
    h2o_iovec_t body;
    sprintf(id_str, "%d", entry.id);
    body = h2o_iovec_init(id_str, strlen(id_str));

    req->res.status = 200;
    req->res.reason = "OK";
    h2o_start_response(req, &generator);
    h2o_send(req, &body, 1, 1);
    return 0;
}