python/sanlock.py | 12 +++ python/sanlockmod.c | 171 ++++++++++++++++++++++++++++++++++++++-------------- python/setup.py | 3 sanlock.spec | 1 src/Makefile | 2 5 files changed, 140 insertions(+), 49 deletions(-)
New commits: commit 5b8a39c88fda035fb0d2c7d90884d85498b9dd7f Author: Federico Simoncelli fsimonce@redhat.com Date: Fri Apr 15 16:18:24 2011 +0100
python: add sanlock init functions and exception
diff --git a/python/sanlock.py b/python/sanlock.py index 35ccb3b..87e33b3 100644 --- a/python/sanlock.py +++ b/python/sanlock.py @@ -7,10 +7,18 @@ import sys import sanlockmod
-SANLOCK_FUNCTIONS = ('register', 'add_lockspace', - 'rem_lockspace', 'acquire', 'release') +SANLOCK_FUNCTIONS = ( + 'register', 'add_lockspace', 'rem_lockspace', 'acquire', 'release' +) + +SanlockException = sanlockmod.exception
for skfun in SANLOCK_FUNCTIONS: setattr(sys.modules[__name__], skfun, getattr(sanlockmod, skfun)) del skfun
+def init_lockspace(lockspace, max_hosts=2000, use_aio=True): + sanlockmod.init_lockspace(lockspace, max_hosts, 0, use_aio) + +def init_resource(resource, num_hosts, max_hosts=2000, use_aio=True): + sanlockmod.init_resource(resource, max_hosts, num_hosts, use_aio) diff --git a/python/sanlockmod.c b/python/sanlockmod.c index 65ead2a..789246a 100644 --- a/python/sanlockmod.c +++ b/python/sanlockmod.c @@ -10,6 +10,7 @@ #include <sanlock.h> #include <sanlock_resource.h> #include <sanlock_admin.h> +#include <sanlock_direct.h>
int __sanlockmod_fd = -1; PyObject *py_module; @@ -17,6 +18,45 @@ PyObject *py_module; /* SANLock exception */ static PyObject *sanlockmod_exception;
+static int +__parse_lockspace(char *lockspace, struct sanlk_lockspace *ret_ls) +{ + char *lockspace_arg; + + /* sanlock_str_to_lockspace is destructive */ + lockspace_arg = strdup(lockspace); + + if (lockspace_arg == NULL) { + PyErr_SetString(sanlockmod_exception, "SANLock extension memory error"); + return -1; + } + + /* convert lockspace string to structure */ + if (sanlock_str_to_lockspace(lockspace_arg, ret_ls) != 0) { + PyErr_SetString(sanlockmod_exception, "Invalid SANLock lockspace"); + goto exit_fail; + } + + free(lockspace_arg); + return 0; + +exit_fail: + free(lockspace_arg); + return -1; +} + +static int +__parse_resource(char *resource, struct sanlk_resource **ret_res) +{ + /* convert resource string to structure */ + if (sanlock_str_to_res(resource, ret_res) != 0) { + PyErr_SetString(sanlockmod_exception, "Invalid SANLock resource"); + return -1; + } + + return 0; +} + static PyObject * py_register(PyObject *self, PyObject *args) { @@ -32,45 +72,80 @@ py_register(PyObject *self, PyObject *args) Py_RETURN_NONE; }
-static int -__parse_lockspace(PyObject *args, struct sanlk_lockspace *ret_ls) +static PyObject *py_init_lockspace(PyObject *self, PyObject *args) { - char *lockspace, *lockspace_arg; + int rv, max_hosts, num_hosts, use_aio; + char *lockspace; + struct sanlk_lockspace ls;
/* parse python tuple */ - if (!PyArg_ParseTuple(args, "s", &lockspace)) { - return -1; + if (!PyArg_ParseTuple(args, "siii", &lockspace, + &max_hosts, &num_hosts, &use_aio)) { + return NULL; }
- /* sanlock_str_to_lockspace is destructive */ - lockspace_arg = strdup(lockspace); + /* parse and check sanlock lockspace */ + if (__parse_lockspace(lockspace, &ls) != 0) { + return NULL; + }
- if (lockspace_arg == NULL) { - PyErr_SetString(sanlockmod_exception, "SANLock extension memory error"); - return -1; + /* init sanlock lockspace (gil disabled) */ + Py_BEGIN_ALLOW_THREADS + rv = sanlock_direct_init(&ls, NULL, max_hosts, num_hosts, use_aio); + Py_END_ALLOW_THREADS + + if (rv != 0) { + PyErr_SetString(sanlockmod_exception, "SANLock lockspace init failure"); + return NULL; }
- /* convert lockspace string to structure */ - if (sanlock_str_to_lockspace(lockspace_arg, ret_ls) != 0) { - PyErr_SetString(sanlockmod_exception, "Invalid SANLock lockspace"); - goto exit_fail; + Py_RETURN_NONE; +} + +static PyObject *py_init_resource(PyObject *self, PyObject *args) +{ + int rv, max_hosts, num_hosts, use_aio; + char *resource; + struct sanlk_resource *res; + + /* parse python tuple */ + if (!PyArg_ParseTuple(args, "siii", &resource, + &max_hosts, &num_hosts, &use_aio)) { + return NULL; }
- free(lockspace_arg); - return 0; + /* parse and check sanlock resource */ + if (__parse_resource(resource, &res) != 0) { + return NULL; + }
-exit_fail: - free(lockspace_arg); - return -1; + /* init sanlock resource (gil disabled) */ + Py_BEGIN_ALLOW_THREADS + rv = sanlock_direct_init(NULL, res, max_hosts, num_hosts, use_aio); + Py_END_ALLOW_THREADS + + if (rv != 0) { + PyErr_SetString(sanlockmod_exception, "SANLock resource init failure"); + return NULL; + } + + Py_RETURN_NONE; }
static PyObject * py_add_lockspace(PyObject *self, PyObject *args) { int rv; + char *lockspace; struct sanlk_lockspace ls;
- if (__parse_lockspace(args, &ls) != 0) { + /* parse python tuple */ + if (!PyArg_ParseTuple(args, "s", &lockspace)) { + return NULL; + } + + /* parse and check sanlock lockspace */ + if (__parse_lockspace(lockspace, &ls) != 0) { return NULL; }
@@ -91,9 +166,16 @@ static PyObject * py_rem_lockspace(PyObject *self, PyObject *args) { int rv; + char *lockspace; struct sanlk_lockspace ls;
- if (__parse_lockspace(args, &ls) != 0) { + /* parse python tuple */ + if (!PyArg_ParseTuple(args, "s", &lockspace)) { + return NULL; + } + + /* parse and check sanlock lockspace */ + if (__parse_lockspace(lockspace, &ls) != 0) { return NULL; }
@@ -110,36 +192,24 @@ py_rem_lockspace(PyObject *self, PyObject *args) Py_RETURN_NONE; }
-static int -__parse_resource(PyObject *args, struct sanlk_resource **ret_res) +static PyObject * +py_acquire(PyObject *self, PyObject *args) { + int rv; char *resource; + struct sanlk_resource *res;
/* parse python tuple */ if (!PyArg_ParseTuple(args, "s", &resource)) { - return -1; - } - - /* convert resource string to structure */ - if (sanlock_str_to_res(resource, ret_res) != 0) { - PyErr_SetString(sanlockmod_exception, "Invalid SANLock resource"); - return -1; + return NULL; }
- return 0; -} - -static PyObject * -py_acquire(PyObject *self, PyObject *args) -{ - int rv; - struct sanlk_resource *res; - - if (__parse_resource(args, &res) != 0) { + /* parse and check sanlock resource */ + if (__parse_resource(resource, &res) != 0) { return NULL; }
- /* acquire resource (gil disabled) */ + /* acquire sanlock resource (gil disabled) */ Py_BEGIN_ALLOW_THREADS rv = sanlock_acquire(__sanlockmod_fd, -1, 0, 1, &res, 0); Py_END_ALLOW_THREADS @@ -161,13 +231,20 @@ static PyObject * py_release(PyObject *self, PyObject *args) { int rv; + char *resource; struct sanlk_resource *res;
- if (__parse_resource(args, &res) != 0) { + /* parse python tuple */ + if (!PyArg_ParseTuple(args, "s", &resource)) { + return NULL; + } + + /* parse and check sanlock resource */ + if (__parse_resource(resource, &res) != 0) { return NULL; }
- /* release resource (gil disabled)*/ + /* release sanlock resource (gil disabled)*/ Py_BEGIN_ALLOW_THREADS rv = sanlock_release(__sanlockmod_fd, -1, 0, 1, &res); Py_END_ALLOW_THREADS @@ -188,14 +265,18 @@ exit_fail: static PyMethodDef sanlockmod_methods[] = { {"register", py_register, METH_NOARGS, "Register to SANLock daemon."}, + {"init_lockspace", py_init_lockspace, METH_VARARGS, + "Initialize a device to be used as SANLock lockspace."}, + {"init_resource", py_init_resource, METH_VARARGS, + "Initialize a device to be used as SANLock resource."}, {"add_lockspace", py_add_lockspace, METH_VARARGS, "Add a lockspace, acquiring a host_id in it."}, {"rem_lockspace", py_rem_lockspace, METH_VARARGS, "Remove a lockspace, releasing our host_id in it."}, {"acquire", py_acquire, METH_VARARGS, - "Acquire leases for the current process."}, + "Acquire a resource lease for the current process."}, {"release", py_release, METH_VARARGS, - "Release leases for the current process."}, + "Release a resource lease for the current process."}, {NULL, NULL, 0, NULL} };
diff --git a/python/setup.py b/python/setup.py index 843216c..f74076f 100644 --- a/python/setup.py +++ b/python/setup.py @@ -6,11 +6,12 @@
from distutils.core import setup, Extension
+sanlocklib = ['sanlock', 'sanlock_direct', 'blkid', 'rt'] sanlockmod = Extension(name = 'sanlockmod', sources = ['sanlockmod.c'], include_dirs = ['../src'], library_dirs = ['../src'], - libraries = ['sanlock']) + libraries = sanlocklib)
setup(name = 'SANLock', version = '1.0',
commit df08cf012ae4f13559acbd8fb5cf55da84ed1183 Author: Federico Simoncelli fsimonce@redhat.com Date: Fri Apr 15 16:18:23 2011 +0100
rpm: add sanlock_direct header
diff --git a/sanlock.spec b/sanlock.spec index a1ec029..d502b2f 100644 --- a/sanlock.spec +++ b/sanlock.spec @@ -127,6 +127,7 @@ developing applications that use %{name}. %{_includedir}/sanlock.h %{_includedir}/sanlock_admin.h %{_includedir}/sanlock_resource.h +%{_includedir}/sanlock_direct.h
%changelog * Mon Apr 4 2011 Federico Simoncelli fsimonce@redhat.com - 1.1.0-3
commit c3b8de0091700911b1857759bb19694f19fe377e Author: Federico Simoncelli fsimonce@redhat.com Date: Fri Apr 15 16:18:22 2011 +0100
makefile: fix install typo
diff --git a/src/Makefile b/src/Makefile index a7ce394..41c11a3 100644 --- a/src/Makefile +++ b/src/Makefile @@ -102,5 +102,5 @@ install: all $(INSTALL) -c -m 755 $(SHLIB_TARGET) $(DESTDIR)/$(LIB_LIBDIR) $(INSTALL) -c -m 755 $(SHLIB2_TARGET) $(DESTDIR)/$(LIB_LIBDIR) cp -a $(LIB_TARGET).so $(DESTDIR)/$(LIB_LIBDIR) - cp -a $(LIB2_TARGET).so.$(SOMAJOR) $(DESTDIR)/$(LIB_LIBDIR) + cp -a $(LIB2_TARGET).so $(DESTDIR)/$(LIB_LIBDIR) $(INSTALL) -c -m 644 $(HEADER_TARGET) $(DESTDIR)/$(HEADER_DIR)
sanlock-devel@lists.fedorahosted.org