Ejemplo n.º 1
0
/**
 * Purge all non-complete (i.e. not 'resdone') entries and invoke their
 * callback, setting the result's error code with the specified error
 */
static void purge_entries(lcb_durability_set_t *dset, lcb_error_t err)
{
    lcb_size_t ii;
    dset->us_timeout = 0;
    dset->next_state = STATE_IGNORE;

    /**
     * Each time we call 'ent_set_resdone' we might cause the refcount to drop
     * to zero, making 'dset' point to freed memory. To avoid this, we bump
     * up the refcount before the loop and defer the possible free operation
     * until the end.
     */
    dset_ref(dset);

    for (ii = 0; ii < dset->nentries; ii++) {
        lcb_durability_entry_t *ent = dset->entries + ii;
        if (ent->done) {
            continue;
        }
        RESFLD(ent, err) = err;
        ent_set_resdone(ent);
    }

    dset_unref(dset);
}
Ejemplo n.º 2
0
/**
 * Called when the criteria is to ensure the key exists somewhow
 */
static void check_positive_durability(lcb_durability_entry_t *ent,
                                      const lcb_observe_resp_t *res)
{
    switch (res->v.v0.status) {

    case LCB_OBSERVE_NOT_FOUND:
    case LCB_OBSERVE_LOGICALLY_DELETED:
        /**
         * If we get NOT_FOUND from the master, this means the key
         * simply does not exists (and we don't have to continue polling)
         */
        if (res->v.v0.from_master) {
            RESFLD(ent, err) = LCB_KEY_ENOENT;
            ent_set_resdone(ent);
        }
        return;

    case LCB_OBSERVE_PERSISTED:
        RESFLD(ent, npersisted)++;

        if (res->v.v0.from_master) {
            RESFLD(ent, persisted_master) = 1;
            RESFLD(ent, exists_master) = 1;

        } else {
            RESFLD(ent, nreplicated)++;
        }
        break;

    case LCB_OBSERVE_FOUND:
        if (res->v.v0.from_master) {
            RESFLD(ent, exists_master) = 1;
            break; /* don't care */
        }
        RESFLD(ent, nreplicated)++;
        break;

    default:
        RESFLD(ent, err) = LCB_EINTERNAL;
        ent_set_resdone(ent);
        break;
    }
}
Ejemplo n.º 3
0
/**
 * Called when the criteria is to ensure that the key is deleted somehow
 */
static void check_negative_durability(lcb_durability_entry_t *ent,
                                      const lcb_observe_resp_t *res)
{
    switch (res->v.v0.status) {
    case LCB_OBSERVE_PERSISTED:
    case LCB_OBSERVE_FOUND:
        return;

    case LCB_OBSERVE_LOGICALLY_DELETED:
        /**
         * The key has been removed from cache, but not actually deleted from
         * disk
         */
        RESFLD(ent, nreplicated)++;

        if (res->v.v0.from_master) {
            RESFLD(ent, exists_master) = 1;
        }
        break;

    case LCB_OBSERVE_NOT_FOUND:
        /**
         * No knowledge of key.
         */
        RESFLD(ent, npersisted)++;

        if (res->v.v0.from_master) {
            RESFLD(ent, persisted_master) = 1;
            RESFLD(ent, exists_master) = 1;

        } else {
            RESFLD(ent, nreplicated)++;
        }

        break;

    default:
        RESFLD(ent, err) = LCB_EINTERNAL;
        ent_set_resdone(ent);
        break;
    }
}
Ejemplo n.º 4
0
/**
 * Observe callback. Called internally by libcouchbase's observe handlers
 */
void lcb_durability_dset_update(lcb_t instance,
                                lcb_durability_set_t *dset,
                                lcb_error_t err,
                                const lcb_observe_resp_t *resp)
{
    lcb_durability_entry_t *ent;

    /**
     * So we have two counters to decrement. One is the global 'done' counter
     * and the other is the iteration counter.
     *
     * The iteration counter is only decremented when we receive a NULL signal
     * in the callback, whereas the global counter is decremented once, whenever
     * the entry's criteria have been satisfied
     */

    if (resp->v.v0.key == NULL) {
        dset_done_waiting(dset);
        return;
    }

    if (dset->nentries == 1) {
        ent = &dset->single.ent;
    } else {
        ent = genhash_find(dset->ht, resp->v.v0.key, resp->v.v0.nkey);
    }

    if (ent->done) {
        /* ignore subsequent errors */
        return;
    }

    if (err != LCB_SUCCESS) {
        RESFLD(ent, err) = err;
        return;
    }

    RESFLD(ent, nresponses)++;

    if (resp->v.v0.cas && resp->v.v0.from_master) {

        RESFLD(ent, cas) = resp->v.v0.cas;

        if (REQFLD(ent, cas) && REQFLD(ent, cas) != resp->v.v0.cas) {
            RESFLD(ent, err) = LCB_KEY_EEXISTS;
            ent_set_resdone(ent);
            return;
        }
    }

    if (DSET_OPTFLD(ent->parent, check_delete)) {
        check_negative_durability(ent, resp);

    } else {
        check_positive_durability(ent, resp);
    }

    if (ent_is_complete(ent)) {
        /* clear any transient errors */
        RESFLD(ent, err) = LCB_SUCCESS;
        ent_set_resdone(ent);
    }

    (void)instance;

}
Ejemplo n.º 5
0
/**
 * Schedules a single sweep of observe requests.
 */
static void poll_once(lcb_durability_set_t *dset)
{
    lcb_size_t ii, oix;
    lcb_error_t err;

    /**
     * We should never be called while an 'iter' operation is still
     * in progress
     */
    lcb_assert(dset->waiting == 0);
    dset_ref(dset);

    for (ii = 0, oix = 0; ii < dset->nentries; ii++) {
        struct lcb_durability_entry_st *ent = dset->entries + ii;

        if (ent->done) {
            continue;
        }

        /* reset all the per-iteration fields */
        RESFLD(ent, persisted_master) = 0;
        RESFLD(ent, exists_master) = 0;
        RESFLD(ent, npersisted) = 0;
        RESFLD(ent, nreplicated) = 0;
        RESFLD(ent, cas) = 0;
        RESFLD(ent, err) = LCB_SUCCESS;

        dset->valid_entries[oix++] = ent;
    }

    lcb_assert(oix == dset->nremaining);

    err = lcb_observe_ex(dset->instance,
                         dset,
                         dset->nremaining,
                         (const void * const *)dset->valid_entries,
                         LCB_OBSERVE_TYPE_DURABILITY);

    if (err != LCB_SUCCESS) {
        for (ii = 0; ii < dset->nentries; ii++) {
            lcb_durability_entry_t *ent = dset->entries + ii;
            if (ent->done) {
                continue;
            }
            RESFLD(ent, err) = err;
            ent_set_resdone(ent);
        }

    } else {
        dset->waiting = 1;
        dset_ref(dset);
    }

    if (dset->waiting && oix) {
        lcb_uint32_t us_now = (lcb_uint32_t)(gethrtime() / 1000);
        lcb_uint32_t us_tmo;

        if (dset->us_timeout > us_now) {
            us_tmo = dset->us_timeout - us_now;
        } else {
            us_tmo = 1;
        }

        timer_schedule(dset,
                       us_tmo,
                       STATE_TIMEOUT);

    } else {
        purge_entries(dset, LCB_ERROR);
    }

    dset_unref(dset);
}
Ejemplo n.º 6
0
/**
 * Schedules a single sweep of observe requests.
 */
static void poll_once(lcb_DURSET *dset)
{
    unsigned ii, n_added = 0;
    lcb_error_t err;
    lcb_MULTICMD_CTX *mctx = NULL;

    /**
     * We should never be called while an 'iter' operation is still
     * in progress
     */
    lcb_assert(dset->waiting == 0);
    dset_ref(dset);
    mctx = lcb_observe_ctx_dur_new(dset->instance);
    if (!mctx) {
        err = LCB_CLIENT_ENOMEM;
        goto GT_ERR;
    }

    for (ii = 0; ii < dset->nentries; ii++) {
        lcb_CMDOBSERVE cmd = { 0 };
        struct lcb_durability_entry_st *ent = dset->entries + ii;
        if (ent->done) {
            continue;
        }

        /* reset all the per-iteration fields */
        RESFLD(ent, persisted_master) = 0;
        RESFLD(ent, exists_master) = 0;
        RESFLD(ent, npersisted) = 0;
        RESFLD(ent, nreplicated) = 0;
        RESFLD(ent, cas) = 0;
        RESFLD(ent, rc) = LCB_SUCCESS;

        LCB_KREQ_SIMPLE(&cmd.key, RESFLD(ent, key), RESFLD(ent, nkey));
        cmd.hashkey = ent->hashkey;

        err = mctx->addcmd(mctx, (lcb_CMDBASE *)&cmd);
        if (err != LCB_SUCCESS) {
            goto GT_ERR;
        }
        n_added ++;
    }

    lcb_assert(n_added == dset->nremaining);

    if (n_added) {
        lcb_sched_enter(dset->instance);
        mctx->done(mctx, dset);
        lcb_sched_leave(dset->instance);
    }

    GT_ERR:
    if (err != LCB_SUCCESS) {
        if (mctx) {
            mctx->fail(mctx);
        }

        for (ii = 0; ii < dset->nentries; ii++) {
            lcb_DURITEM *ent = dset->entries + ii;
            if (ent->done) {
                continue;
            }
            RESFLD(ent, rc) = err;
            ent_set_resdone(ent);
        }

    } else {
        dset->waiting = 1;
        dset_ref(dset);
    }

    if (dset->waiting && n_added) {
        lcb_uint32_t us_now = (lcb_uint32_t)(gethrtime() / 1000);
        lcb_uint32_t us_tmo;
        if (dset->us_timeout > us_now) {
            us_tmo = dset->us_timeout - us_now;
        } else {
            us_tmo = 1;
        }
        timer_schedule(dset, us_tmo, STATE_TIMEOUT);
    } else {
        purge_entries(dset, LCB_ERROR);
    }

    dset_unref(dset);
}
Ejemplo n.º 7
0
/**
 * Observe callback. Called internally by libcouchbase's observe handlers
 */
void
lcb_durability_dset_update(lcb_t instance,
    lcb_DURSET *dset, lcb_error_t err, const lcb_RESPOBSERVE *resp)
{
    lcb_DURITEM *ent;

    /**
     * So we have two counters to decrement. One is the global 'done' counter
     * and the other is the iteration counter.
     *
     * The iteration counter is only decremented when we receive a NULL signal
     * in the callback, whereas the global counter is decremented once, whenever
     * the entry's criteria have been satisfied
     */

    if (resp->key == NULL) {
        dset_done_waiting(dset);
        return;
    }

    if (dset->nentries == 1) {
        ent = &dset->single.ent;
    } else {
        ent = genhash_find(dset->ht, resp->key, resp->nkey);
    }

    if (ent->done) {
        /* ignore subsequent errors */
        return;
    }

    if (err != LCB_SUCCESS) {
        RESFLD(ent, rc) = err;
        /* If it's a non-scheduling error then the item will be retried in the
         * next iteration */
        if (err == LCB_SCHEDFAIL_INTERNAL) {
            ent_set_resdone(ent);
        }
        return;
    }

    RESFLD(ent, nresponses)++;
    if (resp->cas && resp->ismaster) {
        RESFLD(ent, cas) = resp->cas;

        if (ent->reqcas && ent->reqcas != resp->cas) {
            RESFLD(ent, rc) = LCB_KEY_EEXISTS;
            ent_set_resdone(ent);
            return;
        }
    }

    if (DSET_OPTFLD(ent->parent, check_delete)) {
        check_negative_durability(ent, resp);
    } else {
        check_positive_durability(ent, resp);
    }

    if (ent_is_complete(ent)) {
        /* clear any transient errors */
        RESFLD(ent, rc) = LCB_SUCCESS;
        ent_set_resdone(ent);
    }

    (void)instance;

}