diff options
Diffstat (limited to 'src/cls/numops')
-rw-r--r-- | src/cls/numops/cls_numops.cc | 161 | ||||
-rw-r--r-- | src/cls/numops/cls_numops_client.cc | 79 | ||||
-rw-r--r-- | src/cls/numops/cls_numops_client.h | 50 |
3 files changed, 290 insertions, 0 deletions
diff --git a/src/cls/numops/cls_numops.cc b/src/cls/numops/cls_numops.cc new file mode 100644 index 00000000..bccfd6d3 --- /dev/null +++ b/src/cls/numops/cls_numops.cc @@ -0,0 +1,161 @@ +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2015 CERN + * + * Author: Joaquim Rocha <joaquim.rocha@cern.ch> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + */ + +/** \file + * + * This is an OSD class that implements methods for object numeric options on + * its omap values. + * + */ + +#include "objclass/objclass.h" +#include <errno.h> +#include <string> +#include <sstream> +#include <cstdio> +#include <include/compat.h> + +#define DECIMAL_PRECISION 10 + +CLS_VER(1,0) +CLS_NAME(numops) + +static int add(cls_method_context_t hctx, bufferlist *in, bufferlist *out) +{ + string key, diff_str; + + auto iter = in->cbegin(); + try { + decode(key, iter); + decode(diff_str, iter); + } catch (const buffer::error &err) { + CLS_LOG(20, "add: invalid decode of input"); + return -EINVAL; + } + + char *end_ptr = 0; + double difference = strtod(diff_str.c_str(), &end_ptr); + + if (end_ptr && *end_ptr != '\0') { + CLS_ERR("add: invalid input value: %s", diff_str.c_str()); + return -EINVAL; + } + + bufferlist bl; + int ret = cls_cxx_map_get_val(hctx, key, &bl); + + double value; + + if (ret == -ENODATA || bl.length() == 0) { + value = 0; + } else if (ret < 0) { + if (ret != -ENOENT) { + CLS_ERR("add: error reading omap key %s: %d", key.c_str(), ret); + } + return ret; + } else { + std::string stored_value(bl.c_str(), bl.length()); + end_ptr = 0; + value = strtod(stored_value.c_str(), &end_ptr); + + if (end_ptr && *end_ptr != '\0') { + CLS_ERR("add: invalid stored value: %s", stored_value.c_str()); + return -EBADMSG; + } + } + + value += difference; + + std::stringstream stream; + stream << std::setprecision(DECIMAL_PRECISION) << value; + + bufferlist new_value; + new_value.append(stream.str()); + + return cls_cxx_map_set_val(hctx, key, &new_value); +} + +static int mul(cls_method_context_t hctx, bufferlist *in, bufferlist *out) +{ + string key, diff_str; + + auto iter = in->cbegin(); + try { + decode(key, iter); + decode(diff_str, iter); + } catch (const buffer::error &err) { + CLS_LOG(20, "mul: invalid decode of input"); + return -EINVAL; + } + + char *end_ptr = 0; + double difference = strtod(diff_str.c_str(), &end_ptr); + + if (end_ptr && *end_ptr != '\0') { + CLS_ERR("mul: invalid input value: %s", diff_str.c_str()); + return -EINVAL; + } + + bufferlist bl; + int ret = cls_cxx_map_get_val(hctx, key, &bl); + + double value; + + if (ret == -ENODATA || bl.length() == 0) { + value = 0; + } else if (ret < 0) { + if (ret != -ENOENT) { + CLS_ERR("mul: error reading omap key %s: %d", key.c_str(), ret); + } + return ret; + } else { + std::string stored_value(bl.c_str(), bl.length()); + end_ptr = 0; + value = strtod(stored_value.c_str(), &end_ptr); + + if (end_ptr && *end_ptr != '\0') { + CLS_ERR("mul: invalid stored value: %s", stored_value.c_str()); + return -EBADMSG; + } + } + + value *= difference; + + std::stringstream stream; + stream << std::setprecision(DECIMAL_PRECISION) << value; + + bufferlist new_value; + new_value.append(stream.str()); + + return cls_cxx_map_set_val(hctx, key, &new_value); +} + +CLS_INIT(numops) +{ + CLS_LOG(20, "loading cls_numops"); + + cls_handle_t h_class; + cls_method_handle_t h_add; + cls_method_handle_t h_mul; + + cls_register("numops", &h_class); + + cls_register_cxx_method(h_class, "add", + CLS_METHOD_RD | CLS_METHOD_WR, + add, &h_add); + + cls_register_cxx_method(h_class, "mul", + CLS_METHOD_RD | CLS_METHOD_WR, + mul, &h_mul); +} diff --git a/src/cls/numops/cls_numops_client.cc b/src/cls/numops/cls_numops_client.cc new file mode 100644 index 00000000..fa1a69f2 --- /dev/null +++ b/src/cls/numops/cls_numops_client.cc @@ -0,0 +1,79 @@ +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2015 CERN + * + * Author: Joaquim Rocha <joaquim.rocha@cern.ch> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + */ + +#include "cls/numops/cls_numops_client.h" +#include "include/encoding.h" +#include "include/rados/librados.hpp" + +#include <errno.h> +#include <sstream> + +namespace rados { + namespace cls { + namespace numops { + + int add(librados::IoCtx *ioctx, + const std::string& oid, + const std::string& key, + double value_to_add) + { + bufferlist in, out; + encode(key, in); + + std::stringstream stream; + stream << value_to_add; + + encode(stream.str(), in); + + return ioctx->exec(oid, "numops", "add", in, out); + } + + int sub(librados::IoCtx *ioctx, + const std::string& oid, + const std::string& key, + double value_to_subtract) + { + return add(ioctx, oid, key, -value_to_subtract); + } + + int mul(librados::IoCtx *ioctx, + const std::string& oid, + const std::string& key, + double value_to_multiply) + { + bufferlist in, out; + encode(key, in); + + std::stringstream stream; + stream << value_to_multiply; + + encode(stream.str(), in); + + return ioctx->exec(oid, "numops", "mul", in, out); + } + + int div(librados::IoCtx *ioctx, + const std::string& oid, + const std::string& key, + double value_to_divide) + { + if (value_to_divide == 0) + return -EINVAL; + + return mul(ioctx, oid, key, 1 / value_to_divide); + } + + } // namespace numops + } // namespace cls +} // namespace rados diff --git a/src/cls/numops/cls_numops_client.h b/src/cls/numops/cls_numops_client.h new file mode 100644 index 00000000..0b0ccbe5 --- /dev/null +++ b/src/cls/numops/cls_numops_client.h @@ -0,0 +1,50 @@ +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2015 CERN + * + * Author: Joaquim Rocha <joaquim.rocha@cern.ch> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + */ + +#ifndef CEPH_LIBRBD_CLS_NUMOPS_CLIENT_H +#define CEPH_LIBRBD_CLS_NUMOPS_CLIENT_H + +#include "include/rados/librados_fwd.hpp" +#include <string> + +namespace rados { + namespace cls { + namespace numops { + + extern int add(librados::IoCtx *ioctx, + const std::string& oid, + const std::string& key, + double value_to_add); + + extern int sub(librados::IoCtx *ioctx, + const std::string& oid, + const std::string& key, + double value_to_subtract); + + extern int mul(librados::IoCtx *ioctx, + const std::string& oid, + const std::string& key, + double value_to_multiply); + + extern int div(librados::IoCtx *ioctx, + const std::string& oid, + const std::string& key, + double value_to_divide); + + } // namespace numops + } // namespace cls +} // namespace rados + +#endif // CEPH_LIBRBD_CLS_NUMOPS_CLIENT_H + |