]>
git.proxmox.com Git - ceph.git/blob - ceph/src/mds/Mantle.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
4 * Ceph - scalable distributed file system
6 * Copyright (C) 2016 Michael Sevilla <mikesevilla3@gmail.com>
8 * This is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License version 2.1, as published by the Free Software
11 * Foundation. See file COPYING.
18 #include "msg/Messenger.h"
19 #include "common/Clock.h"
24 #define dout_context g_ceph_context
26 #define dout_prefix *_dout << "mds.mantle "
27 #define mantle_dout(lvl) \
29 auto subsys = ceph_subsys_mds;\
30 if ((dout_context)->_conf->subsys.should_gather(ceph_subsys_mds_balancer, lvl)) {\
31 subsys = ceph_subsys_mds_balancer;\
33 dout_impl(dout_context, ceph::dout::need_dynamic(subsys), lvl) dout_prefix
35 #define mantle_dendl dendl; } while (0)
38 static int dout_wrapper(lua_State
*L
)
40 int level
= luaL_checkinteger(L
, 1);
41 lua_concat(L
, lua_gettop(L
)-1);
42 mantle_dout(ceph::dout::need_dynamic(level
)) << lua_tostring(L
, 2)
47 int Mantle::balance(std::string_view script
,
49 const std::vector
<std::map
<std::string
, double>> &metrics
,
50 std::map
<mds_rank_t
, double> &my_targets
)
52 lua_settop(L
, 0); /* clear the stack */
54 /* load the balancer */
55 if (luaL_loadstring(L
, script
.data())) {
56 mantle_dout(0) << "WARNING: mantle could not load balancer: "
57 << lua_tostring(L
, -1) << mantle_dendl
;
61 /* tell the balancer which mds is making the decision */
62 lua_pushinteger(L
, (lua_Integer
)whoami
);
63 lua_setglobal(L
, "whoami");
65 /* global mds metrics to hold all dictionaries */
68 /* push name of mds (i) and its metrics onto Lua stack */
69 for (size_t i
=0; i
< metrics
.size(); i
++) {
72 /* push values into this mds's table; setfield assigns key/pops val */
73 for (const auto &it
: metrics
[i
]) {
74 lua_pushnumber(L
, it
.second
);
75 lua_setfield(L
, -2, it
.first
.c_str());
78 /* in global mds table at stack[-3], set k=stack[-1] to v=stack[-2] */
82 /* set the name of the global mds table */
83 lua_setglobal(L
, "mds");
85 ceph_assert(lua_gettop(L
) == 1);
86 if (lua_pcall(L
, 0, 1, 0) != LUA_OK
) {
87 mantle_dout(0) << "WARNING: mantle could not execute script: "
88 << lua_tostring(L
, -1) << mantle_dendl
;
92 /* parse response by iterating over Lua stack */
93 if (lua_istable(L
, -1) == 0) {
94 mantle_dout(0) << "WARNING: mantle script returned a malformed response" << mantle_dendl
;
98 /* fill in return value */
99 for (lua_pushnil(L
); lua_next(L
, -2); lua_pop(L
, 1)) {
100 if (!lua_isinteger(L
, -2) || !lua_isnumber(L
, -1)) {
101 mantle_dout(0) << "WARNING: mantle script returned a malformed response" << mantle_dendl
;
104 mds_rank_t
rank(lua_tointeger(L
, -2));
105 my_targets
[rank
] = lua_tonumber(L
, -1);
111 Mantle::Mantle (void)
113 /* build lua vm state */
116 mantle_dout(0) << "WARNING: mantle could not load Lua state" << mantle_dendl
;
117 throw std::bad_alloc();
120 /* balancer policies can use basic Lua functions */
121 static const luaL_Reg loadedlibs
[] = {
122 {"_G", luaopen_base
},
123 {LUA_COLIBNAME
, luaopen_coroutine
},
124 {LUA_STRLIBNAME
, luaopen_string
},
125 {LUA_MATHLIBNAME
, luaopen_math
},
126 {LUA_TABLIBNAME
, luaopen_table
},
127 {LUA_UTF8LIBNAME
, luaopen_utf8
},
132 for (lib
= loadedlibs
; lib
->func
; lib
++) {
133 luaL_requiref(L
, lib
->name
, lib
->func
, 1);
134 lua_pop(L
, 1); /* remove lib */
137 /* setup debugging */
138 lua_register(L
, "BAL_LOG", dout_wrapper
);