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
25 #define dout_subsys ceph_subsys_mds_balancer
27 #define dout_prefix *_dout << "mds.mantle "
28 #define mantle_dout(lvl) \
30 auto subsys = ceph_subsys_mds;\
31 if ((dout_context)->_conf->subsys.should_gather(ceph_subsys_mds_balancer, lvl)) {\
32 subsys = ceph_subsys_mds_balancer;\
34 dout_impl(dout_context, subsys, lvl) dout_prefix
36 #define mantle_dendl dendl; } while (0)
39 static int dout_wrapper(lua_State *L)
41 int level = luaL_checkinteger(L, 1);
42 lua_concat(L, lua_gettop(L)-1);
43 mantle_dout(level) << lua_tostring(L, 2) << mantle_dendl;
47 int Mantle::balance(const std::string &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.c_str())) {
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 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 */
122 luaopen_coroutine(L);
128 /* setup debugging */
129 lua_register(L, "BAL_LOG", dout_wrapper);