Mercurial > prosody-modules
comparison mod_storage_multi/mod_storage_multi.lua @ 1492:8c4a2d85e8bf
mod_storage_multi: Multi-backend storage module (WIP)
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Thu, 21 Aug 2014 23:14:55 +0200 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
1491:e7294423512f | 1492:8c4a2d85e8bf |
---|---|
1 -- mod_storage_multi | |
2 | |
3 local storagemanager = require"core.storagemanager"; | |
4 local backends = module:get_option_array(module.name); -- TODO better name? | |
5 | |
6 -- TODO migrate data "upwards" | |
7 | |
8 -- one → one successful write is success | |
9 -- all → all backends must report success | |
10 -- majority → majority of backends must report success | |
11 local policy = module:get_option_string(module.name.."_policy", "all"); | |
12 | |
13 local keyval_store = {}; | |
14 keyval_store.__index = keyval_store; | |
15 | |
16 function keyval_store:get(username) | |
17 local backends = self.backends; | |
18 local data, err; | |
19 for i = 1, #backends do | |
20 module:log("debug", "%s:%s:get(%q)", tostring(backends[i].get), backends[i]._store, username); | |
21 data, err = backends[i]:get(username); | |
22 if err then | |
23 module:log("error", tostring(err)); | |
24 elseif not data then | |
25 module:log("debug", "No data returned"); | |
26 else | |
27 module:log("debug", "Data returned"); | |
28 return data, err; | |
29 end | |
30 end | |
31 end | |
32 | |
33 -- This is where it gets complicated | |
34 function keyval_store:set(username, data) | |
35 local backends = self.backends; | |
36 local ok, err, backend; | |
37 local all, one, oks = true, false, 0; | |
38 for i = 1, #backends do | |
39 backend = backends[i]; | |
40 module:log("debug", "%s:%s:set(%q)", tostring(backends[i].get), backends[i].store, username); | |
41 ok, err = backend:set(username, data); | |
42 if not ok then | |
43 module:log("error", "Error in storage driver %s: %s", backend.name, tostring(err)); | |
44 else | |
45 oks = oks + 1; | |
46 end | |
47 one = one or ok; -- At least one successful write | |
48 all = all and ok; -- All successful | |
49 end | |
50 if policy == "all" then | |
51 return all, err | |
52 elseif policy == "majority" then | |
53 return oks > (#backends/2), err; | |
54 end | |
55 -- elseif policy == "one" then | |
56 return one, err; | |
57 end | |
58 | |
59 local stores = { | |
60 keyval = keyval_store; | |
61 } | |
62 | |
63 local driver = {}; | |
64 | |
65 function driver:open(store, typ) | |
66 local store_mt = stores[typ or "keyval"]; | |
67 if store_mt then | |
68 local my_backends = {}; | |
69 local driver, opened | |
70 for i = 1, #backends do | |
71 driver = storagemanager.load_driver(module.host, backends[i]); | |
72 opened = driver:open(store, typ); | |
73 my_backends[i] = assert(driver:open(store, typ)); | |
74 my_backends[i]._store = store; | |
75 end | |
76 return setmetatable({ backends = my_backends }, store_mt); | |
77 end | |
78 return nil, "unsupported-store"; | |
79 end | |
80 | |
81 module:provides("storage", driver); |