Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Random store failures without any error reported #3

Closed
Yuras opened this issue Dec 4, 2014 · 0 comments · Fixed by #4
Closed

Random store failures without any error reported #3

Yuras opened this issue Dec 4, 2014 · 0 comments · Fixed by #4

Comments

@Yuras
Copy link
Contributor

Yuras commented Dec 4, 2014

See symisc/vedis#3. The same code is used in unqlite, and the same bugs exist.

There are 3 fixes to be applied: symisc/vedis#5, symisc/vedis#6, symisc/vedis#7

Here is example program to reproduce the issue (it is the vedis version adopted for unqlite)

#include <unqlite.h>

#include <stdlib.h>
#include <stdio.h>
#include <time.h>

// generate random bytes of variable (important!) length
char *randomBytes(int* len)
{
    *len = rand() % 4 + 1;
    char *res = (char*)malloc(*len);

    int i;
    for (i = 0; i < *len; i++)
    {
        res[i] = rand() % 256;
    }

    return res;
}

int main()
{
    unqlite *db;
    int res = unqlite_open(&db, "test.db", UNQLITE_OPEN_CREATE);
    if (res != UNQLITE_OK)
    {
        printf("Error opening unqlite: %i\n", (int)res);
        return 0;
    }

    srand(time(NULL));

    char buffer[255];
    unqlite_int64 inout;

    int block = 0;
    int count = 0;

    while(1)
    {
        int keyLen;
        int valLen;
        char *key = randomBytes(&keyLen);
        char *val = randomBytes(&valLen);

        // insert random key
        res = unqlite_kv_store(db, key, keyLen, val, valLen);
        if (res != UNQLITE_OK)
        {
            printf("Unable to store: %i\n", (int)res);
            return 0;
        }

        // verify it was stored correctly
        inout = 255;
        res = unqlite_kv_fetch(db, key, keyLen, &buffer[0], &inout);
        if (res != UNQLITE_OK)
        {
            printf("Unable to fetch: %i", (int)res);
            return 0;
        }

        // generate new random key and delete if it exists
        free(key);
        key = randomBytes(&keyLen);

        inout = 255;
        res = unqlite_kv_fetch(db, key, keyLen, &buffer[0], &inout);
        if (res == UNQLITE_OK)
        {
            res = unqlite_kv_delete(db, key, keyLen);
            if (res != UNQLITE_OK)
            {
                printf("Unable to delete: %i\n", (int)res);
                return 0;
            }
        }

        free(key);
        free(val);

        count++;
        if (count > 10000)
        {
            block++;
            count = 0;
            printf("%i 10K iterations\n", block);
        }
    }
    return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant