mirror of
https://github.com/samba-team/samba.git
synced 2024-12-22 13:34:15 +03:00
ctdb-common: Add request id abstraction
Signed-off-by: Amitay Isaacs <amitay@gmail.com> Reviewed-by: Martin Schwenke <martin@meltin.net>
This commit is contained in:
parent
a7ea6b094e
commit
9fd4d07ca6
89
ctdb/common/reqid.c
Normal file
89
ctdb/common/reqid.c
Normal file
@ -0,0 +1,89 @@
|
||||
/*
|
||||
ctdb request id handling code
|
||||
|
||||
Copyright (C) Amitay Isaacs 2015
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "replace.h"
|
||||
|
||||
#include <talloc.h>
|
||||
|
||||
#include "lib/util/idtree.h"
|
||||
#include "reqid.h"
|
||||
|
||||
struct reqid_context {
|
||||
struct idr_context *idr;
|
||||
uint32_t lastid;
|
||||
};
|
||||
|
||||
int reqid_init(TALLOC_CTX *mem_ctx, int start_id,
|
||||
struct reqid_context **result)
|
||||
{
|
||||
struct reqid_context *reqid_ctx;
|
||||
|
||||
reqid_ctx = talloc_zero(mem_ctx, struct reqid_context);
|
||||
if (reqid_ctx == NULL) {
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
reqid_ctx->idr = idr_init(reqid_ctx);
|
||||
if (reqid_ctx->idr == NULL) {
|
||||
talloc_free(reqid_ctx);
|
||||
return ENOMEM;
|
||||
}
|
||||
|
||||
if (start_id <= 0) {
|
||||
start_id = 1;
|
||||
}
|
||||
reqid_ctx->lastid = start_id;
|
||||
|
||||
*result = reqid_ctx;
|
||||
return 0;
|
||||
}
|
||||
|
||||
uint32_t reqid_new(struct reqid_context *reqid_ctx, void *private_data)
|
||||
{
|
||||
int id;
|
||||
|
||||
id = idr_get_new_above(reqid_ctx->idr, private_data,
|
||||
reqid_ctx->lastid+1, INT_MAX);
|
||||
if (id < 0) {
|
||||
/* reqid wrapped */
|
||||
id = idr_get_new(reqid_ctx->idr, private_data, INT_MAX);
|
||||
}
|
||||
if (id == -1) {
|
||||
return REQID_INVALID;
|
||||
}
|
||||
|
||||
reqid_ctx->lastid = id;
|
||||
return id;
|
||||
}
|
||||
|
||||
void *_reqid_find(struct reqid_context *reqid_ctx, uint32_t reqid)
|
||||
{
|
||||
return idr_find(reqid_ctx->idr, reqid);
|
||||
}
|
||||
|
||||
int reqid_remove(struct reqid_context *reqid_ctx, uint32_t reqid)
|
||||
{
|
||||
int ret;
|
||||
|
||||
ret = idr_remove(reqid_ctx->idr, reqid);
|
||||
if (ret < 0) {
|
||||
return ENOENT;
|
||||
}
|
||||
return 0;
|
||||
}
|
89
ctdb/common/reqid.h
Normal file
89
ctdb/common/reqid.h
Normal file
@ -0,0 +1,89 @@
|
||||
/*
|
||||
Request id database
|
||||
|
||||
Copyright (C) Amitay Isaacs 2015
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#ifndef __CTDB_REQID_H__
|
||||
#define __CTDB_REQID_H__
|
||||
|
||||
#include <talloc.h>
|
||||
|
||||
/**
|
||||
* @file reqid.h
|
||||
*
|
||||
* @brief Request id database
|
||||
*
|
||||
* CTDB tracks messsages using request id. CTDB stores client state for each
|
||||
* request id to process the replies correctly.
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief Abstract struct to store request id database
|
||||
*/
|
||||
struct reqid_context;
|
||||
|
||||
#define REQID_INVALID 0xffffffff
|
||||
|
||||
/**
|
||||
* @brief Initialize request id database
|
||||
*
|
||||
* This returns a new request id context. Freeing this context will free
|
||||
* all the memory associated with request id database.
|
||||
*
|
||||
* @param[in] mem_ctx Talloc memory context
|
||||
* @param[in] start_id The initial id
|
||||
* @param[out] result The new talloc_context structure
|
||||
* @return 0 on success, errno on failure
|
||||
*/
|
||||
int reqid_init(TALLOC_CTX *mem_ctx, int start_id,
|
||||
struct reqid_context **result);
|
||||
|
||||
/**
|
||||
* @brief Generate new request id and associate given data with the request id
|
||||
*
|
||||
* @param[in] reqid_ctx The request id context
|
||||
* @param[in] private_data The state to associate with new request id
|
||||
* @return new request id, REQID_INVALID on failure
|
||||
*/
|
||||
uint32_t reqid_new(struct reqid_context *reqid_ctx, void *private_data);
|
||||
|
||||
#ifdef DOXYGEN
|
||||
/**
|
||||
* @brief Fetch the data associated with the request id
|
||||
*
|
||||
* @param[in] reqid_ctx The request id context
|
||||
* @param[in] reqid The request id
|
||||
* @param[in] type The data type of the stored data
|
||||
* @return the data stored for the reqid, NULL on failure
|
||||
*/
|
||||
type *reqid_find(struct reqid_context *reqid_ctx, uint32_t reqid, #type);
|
||||
#else
|
||||
void *_reqid_find(struct reqid_context *reqid_ctx, uint32_t reqid);
|
||||
#define reqid_find(ctx, reqid, type) \
|
||||
(type *)talloc_check_name(_reqid_find(ctx, reqid), #type)
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Remove the data associated with the request id
|
||||
*
|
||||
* @param[in] reqid_ctx The request id context
|
||||
* @param[in] reqid The request id
|
||||
* @return 0 on success, errno on failure
|
||||
*/
|
||||
int reqid_remove(struct reqid_context *reqid_ctx, uint32_t reqid);
|
||||
|
||||
#endif /* __CTDB_REQID_H__ */
|
13
ctdb/tests/cunit/reqid_test_001.sh
Executable file
13
ctdb/tests/cunit/reqid_test_001.sh
Executable file
@ -0,0 +1,13 @@
|
||||
#!/bin/sh
|
||||
|
||||
. "${TEST_SCRIPTS_DIR}/unit.sh"
|
||||
|
||||
output=$(
|
||||
for i in $(seq 0 1023) ; do
|
||||
echo "WARNING: attempt to remove unset id $i in idtree"
|
||||
done
|
||||
)
|
||||
|
||||
ok "$output"
|
||||
|
||||
unit_test reqid_test
|
72
ctdb/tests/src/reqid_test.c
Normal file
72
ctdb/tests/src/reqid_test.c
Normal file
@ -0,0 +1,72 @@
|
||||
/*
|
||||
reqid tests
|
||||
|
||||
Copyright (C) Amitay Isaacs 2015
|
||||
|
||||
This program is free software; you can redistribute it and/or modify
|
||||
it under the terms of the GNU General Public License as published by
|
||||
the Free Software Foundation; either version 3 of the License, or
|
||||
(at your option) any later version.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "replace.h"
|
||||
|
||||
#include <assert.h>
|
||||
|
||||
#include "common/reqid.c"
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
struct reqid_context *reqid_ctx;
|
||||
TALLOC_CTX *mem_ctx = talloc_new(NULL);
|
||||
int i, ret;
|
||||
uint32_t reqid;
|
||||
int *data, *tmp;
|
||||
|
||||
ret = reqid_init(mem_ctx, INT_MAX-200, &reqid_ctx);
|
||||
assert(ret == 0);
|
||||
|
||||
data = talloc_zero(mem_ctx, int);
|
||||
assert(data != 0);
|
||||
|
||||
for (i=0; i<1024*1024; i++) {
|
||||
reqid = reqid_new(reqid_ctx, data);
|
||||
assert(reqid != -1);
|
||||
}
|
||||
|
||||
for (i=0; i<1024; i++) {
|
||||
tmp = reqid_find(reqid_ctx, i, int);
|
||||
assert(tmp == data);
|
||||
}
|
||||
|
||||
for (i=0; i<1024; i++) {
|
||||
ret = reqid_remove(reqid_ctx, i);
|
||||
assert(ret == 0);
|
||||
}
|
||||
|
||||
for (i=0; i<1024; i++) {
|
||||
tmp = reqid_find(reqid_ctx, i, int);
|
||||
assert(tmp == NULL);
|
||||
}
|
||||
|
||||
for (i=0; i<1024; i++) {
|
||||
ret = reqid_remove(reqid_ctx, i);
|
||||
assert(ret == ENOENT);
|
||||
}
|
||||
|
||||
talloc_free(reqid_ctx);
|
||||
assert(talloc_get_size(mem_ctx) == 0);
|
||||
|
||||
talloc_free(mem_ctx);
|
||||
|
||||
return 0;
|
||||
}
|
@ -333,7 +333,7 @@ def build(bld):
|
||||
|
||||
bld.SAMBA_SUBSYSTEM('ctdb-util',
|
||||
source=bld.SUBDIR('common',
|
||||
'''db_hash.c srvid.c'''),
|
||||
'''db_hash.c srvid.c reqid.c'''),
|
||||
deps='replace talloc tevent tdb')
|
||||
|
||||
bld.SAMBA_SUBSYSTEM('ctdb-client',
|
||||
@ -585,6 +585,11 @@ def build(bld):
|
||||
deps='talloc tevent tdb',
|
||||
install_path='${CTDB_TEST_LIBDIR}')
|
||||
|
||||
bld.SAMBA_BINARY('reqid_test',
|
||||
source='tests/src/reqid_test.c',
|
||||
deps='samba-util',
|
||||
install_path='${CTDB_TEST_LIBDIR}')
|
||||
|
||||
# Test binaries
|
||||
ctdb_tests = [
|
||||
'rb_test',
|
||||
|
Loading…
Reference in New Issue
Block a user