forked from Mons/tnt-config
-
Notifications
You must be signed in to change notification settings - Fork 12
/
config.lua
1414 lines (1268 loc) · 44.5 KB
/
config.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---@diagnostic disable: inject-field
local log = require 'log'
if log.new then
log = log.new('moonlibs.config')
end
local fio = require 'fio'
local json = require 'json'.new()
local yaml = require 'yaml'.new()
local digest = require 'digest'
local fiber = require 'fiber'
local clock = require 'clock'
json.cfg{ encode_invalid_as_nil = true }
yaml.cfg{ encode_use_tostring = true }
---Retrieves all upvalues of given function and returns them as kv-map
---@param fun fun()
---@return table<string,any> variables
local function lookaround(fun)
local vars = {}
local i = 1
while true do
local n,v = debug.getupvalue(fun,i)
if not n then break end
vars[n] = v
i = i + 1
end
return vars
end
---@private
---@class moonlibs.config.reflect_internals
---@field dynamic_cfg table<string,boolean>
---@field default_cfg table<string,any>
---@field upgrade_cfg? fun(cfg: table<string,any>, translate_cfg: table): table<string,any>
---@field template_cfg? table
---@field translate_cfg? table
---@field log? table
---Unwraps box.cfg and retrieves dynamic_cfg, default_cfg tables
---@return moonlibs.config.reflect_internals
local function reflect_internals()
local peek = {
dynamic_cfg = {};
default_cfg = {};
upgrade_cfg = true;
translate_cfg = true;
template_cfg = true;
log = true;
}
local steps = {}
local peekf = box.cfg
local allow_unwrap = true
while true do
local prevf = peekf
local mt = debug.getmetatable(peekf)
if type(peekf) == 'function' then
-- pass
table.insert(steps,"func")
elseif mt and mt.__call then
peekf = mt.__call
table.insert(steps,"mt_call")
else
error(string.format("Neither function nor callable argument %s after steps: %s", peekf, table.concat(steps, ", ")))
end
local vars = lookaround(peekf)
if type(vars.default_cfg) == 'table' then
for k in pairs(vars.default_cfg) do
peek.default_cfg[k] = vars.default_cfg[k]
end
end
if allow_unwrap and (vars.orig_cfg or vars.origin_cfg) then
-- It's a wrap of tarantoolctl/tt, unwrap and repeat
peekf = (vars.orig_cfg or vars.origin_cfg)
allow_unwrap = false
table.insert(steps,"ctl-orig")
elseif vars.dynamic_cfg then
log.info("Found config by steps: %s", table.concat(steps, ", "))
for k in pairs(vars.dynamic_cfg) do
peek.dynamic_cfg[k] = true
end
for k in pairs(peek) do
if peek[k] == true then
if vars[k] ~= nil then
peek[k] = vars[k]
else
peek[k] = nil
end
end
end
break
elseif vars.lock and vars.f and type(vars.f) == 'function' then
peekf = vars.f
table.insert(steps,"lock-unwrap")
elseif vars.old_call and type(vars.old_call) == 'function' then
peekf = vars.old_call
table.insert(steps,"ctl-oldcall")
elseif vars.orig_cfg_call and type(vars.orig_cfg_call) == 'function' then
peekf = vars.orig_cfg_call
table.insert(steps,"ctl-orig_cfg_call")
elseif vars.load_cfg_apply_dynamic then
table.insert(steps,"load_cfg_apply_dynamic")
for k in pairs(peek) do
if peek[k] == true then
if vars[k] ~= nil then
peek[k] = vars[k]
end
end
end
peekf = vars.load_cfg_apply_dynamic
elseif vars.dynamic_cfg_modules then
-- print(yaml.encode(vars.dynamic_cfg_modules))
log.info("Found config by steps: %s", table.concat(steps, ", "))
for k, v in pairs(vars.dynamic_cfg_modules) do
peek.dynamic_cfg[k] = true
for op in pairs(v.options) do
peek.dynamic_cfg[op] = true
end
end
break;
elseif vars.reload_cfg then
table.insert(steps,"reload_cfg")
peekf = vars.reload_cfg
elseif vars.reconfig_modules then
table.insert(steps,"reconfig_modules")
for k in pairs(peek) do
if peek[k] == true then
if vars[k] ~= nil then
peek[k] = vars[k]
end
end
end
peekf = vars.reconfig_modules
elseif vars.orig_call and vars.wrapper_impl and type(vars.orig_call) == 'function' and type(vars.wrapper_impl) == 'function' then
peekf = vars.orig_call
table.insert(steps,"queue-cfg_call")
else
for k,v in pairs(vars) do log.info("var %s=%s",k,v) end
error(string.format("Bad vars for %s after steps: %s", peekf, table.concat(steps, ", ")))
end
if prevf == peekf then
error(string.format("Recursion for %s after steps: %s", peekf, table.concat(steps, ", ")))
end
end
return peek
end
local load_cfg = reflect_internals()
---Filters only valid keys from given cfg
---
---Edits given cfg and returns only clear config
---@param cfg table<string,any>
---@return table<string,any>
local function prepare_box_cfg(cfg)
-- 1. take config, if have upgrade, upgrade it
if load_cfg.upgrade_cfg then
cfg = load_cfg.upgrade_cfg(cfg, load_cfg.translate_cfg)
end
-- 2. check non-dynamic, and wipe them out
if type(box.cfg) ~= 'function' then
for key, val in pairs(cfg) do
if load_cfg.dynamic_cfg[key] == nil and box.cfg[key] ~= val then
local warn = string.format(
"Can't change option '%s' dynamically from '%s' to '%s'",
key,box.cfg[key],val
)
log.warn("%s",warn)
print(warn)
cfg[key] = nil
end
end
end
return cfg
end
local readonly_mt = {
__index = function(_,k) return rawget(_,k) end;
__newindex = function(_,k)
error("Modification of readonly key "..tostring(k),2)
end;
__serialize = function(_)
local t = {}
for k,v in pairs(_) do
t[k]=v
end
return t
end;
}
local function flatten (t,prefix,result)
prefix = prefix or ''
local protect = not result
result = result or {}
for k,v in pairs(t) do
if type(v) == 'table' then
flatten(v, prefix..k..'.',result)
end
result[prefix..k] = v
end
if protect then
return setmetatable(result,readonly_mt)
end
return result
end
local function get_opt()
local take = false
local key
for _,v in ipairs(arg) do
if take then
if key == 'config' or key == 'c' then
return v
end
else
if string.sub( v, 1, 2) == "--" then
local x = string.find( v, "=", 1, true )
if x then
key = string.sub( v, 3, x-1 )
-- print("have key=")
if key == 'config' then
return string.sub( v, x+1 )
end
else
-- print("have key, turn take")
key = string.sub( v, 3 )
take = true
end
elseif string.sub( v, 1, 1 ) == "-" then
if string.len(v) == 2 then
key = string.sub(v,2,2)
take = true
else
key = string.sub(v,2,2)
if key == 'c' then
return string.sub( v, 3 )
end
end
end
end
end
end
local function deep_merge(dst,src,keep)
-- TODO: think of cyclic
if not src or not dst then error("Call to deepmerge with bad args",2) end
for k,v in pairs(src) do
if type(v) == 'table' then
if not dst[k] then dst[k] = {} end
deep_merge(dst[k],src[k],keep)
else
if dst[k] == nil or not keep then
dst[k] = src[k]
end
end
end
end
local function deep_copy(src)
local t = {}
deep_merge(t, src)
return t
end
local function is_array(a)
local len = 0
for k in pairs(a) do
len = len + 1
if type(k) ~= 'number' then
return false
end
end
return #a == len
end
--[[
returns config diff
1. deleted values returned as box.NULL
2. arrays is replaced completely
3. nil means no diff (and not stored in tables)
]]
local function value_diff(old,new)
if type(old) ~= type(new) then
return new
elseif type(old) == 'table' then
if new == old then return end
if is_array(old) then
if #new ~= #old then return new end
for i = 1,#old do
local diff = value_diff(old[i], new[i])
if diff ~= nil then
return new
end
end
else
local diff = {}
for k in pairs(old) do
if new[ k ] == nil then
diff[k] = box.NULL
else
local vdiff = value_diff(old[k], new[k])
if vdiff ~= nil then
diff[k] = vdiff
end
end
end
for k in pairs(new) do
if old[ k ] == nil then
diff[k] = new[k]
end
end
if next(diff) then
return diff
end
end
else
if old ~= new then
return new
end
end
-- no diff
end
local function toboolean(v)
if v then
if type(v) == 'boolean' then return v end
v = tostring(v):lower()
local n = tonumber(v)
if n then return n ~= 0 end
if v == 'true' or v == 'yes' then
return true
end
end
return false
end
---@type table<string, fun(M: moonlibs.config, instance_name: string, common_cfg: table, instance_cfg: table, cluster_cfg: table, local_cfg: table):table >
local master_selection_policies;
master_selection_policies = {
['etcd.instance.single'] = function(M, instance_name, common_cfg, instance_cfg, cluster_cfg, local_cfg)
local cfg = {}
deep_merge(cfg, common_cfg)
deep_merge(cfg, instance_cfg)
if cluster_cfg then
error("Cluster config should not exist for single instance config")
end
deep_merge(cfg, local_cfg)
if cfg.box.read_only == nil then
log.info("Instance have no read_only option, set read_only=false")
cfg.box.read_only = false
end
if cfg.box.instance_uuid and not cfg.box.replicaset_uuid then
cfg.box.replicaset_uuid = cfg.box.instance_uuid
end
log.info("Using policy etcd.instance.single, read_only=%s",cfg.box.read_only)
return cfg
end;
['etcd.instance.read_only'] = function(M, instance_name, common_cfg, instance_cfg, cluster_cfg, local_cfg)
local cfg = {}
deep_merge(cfg, common_cfg)
deep_merge(cfg, instance_cfg)
if cluster_cfg then
log.info("cluster=%s",json.encode(cluster_cfg))
assert(cluster_cfg.replicaset_uuid,"Need cluster uuid")
cfg.box.replicaset_uuid = cluster_cfg.replicaset_uuid
end
deep_merge(cfg, local_cfg)
if M.default_read_only and cfg.box.read_only == nil then
log.info("Instance have no read_only option, set read_only=true")
cfg.box.read_only = true
end
log.info("Using policy etcd.instance.read_only, read_only=%s",cfg.box.read_only)
return cfg
end;
['etcd.cluster.master'] = function(M, instance_name, common_cfg, instance_cfg, cluster_cfg, local_cfg)
log.info("Using policy etcd.cluster.master")
local cfg = {}
deep_merge(cfg, common_cfg)
deep_merge(cfg, instance_cfg)
assert(cluster_cfg.replicaset_uuid,"Need cluster uuid")
cfg.box.replicaset_uuid = cluster_cfg.replicaset_uuid
if cfg.box.read_only ~= nil then
log.info("Ignore box.read_only=%s value due to config policy",cfg.box.read_only)
end
if cluster_cfg.master then
if cluster_cfg.master == instance_name then
log.info("Instance is declared as cluster master, set read_only=false")
cfg.box.read_only = false
if cfg.box.bootstrap_strategy ~= 'auto' then
cfg.box.replication_connect_quorum = 1
cfg.box.replication_connect_timeout = 1
end
else
log.info("Cluster has another master %s, not me %s, set read_only=true", cluster_cfg.master, instance_name)
cfg.box.read_only = true
end
else
log.info("Cluster have no declared master, set read_only=true")
cfg.box.read_only = true
end
deep_merge(cfg, local_cfg)
return cfg
end;
['etcd.cluster.vshard'] = function(M, instance_name, common_cfg, instance_cfg, cluster_cfg, local_cfg)
log.info("Using policy etcd.cluster.vshard")
if instance_cfg.cluster then
return master_selection_policies['etcd.cluster.master'](M, instance_name, common_cfg, instance_cfg, cluster_cfg, local_cfg)
else
return master_selection_policies['etcd.instance.single'](M, instance_name, common_cfg, instance_cfg, cluster_cfg, local_cfg)
end
end;
['etcd.cluster.raft'] = function(M, instance_name, common_cfg, instance_cfg, cluster_cfg, local_cfg)
log.info("Using policy etcd.cluster.raft")
local cfg = {}
deep_merge(cfg, common_cfg)
deep_merge(cfg, instance_cfg)
assert(cluster_cfg.replicaset_uuid,"Need cluster uuid")
cfg.box.replicaset_uuid = cluster_cfg.replicaset_uuid
if not cfg.box.election_mode then
cfg.box.election_mode = M.default_election_mode
end
-- TODO: anonymous replica
if cfg.box.election_mode == 'off' then
log.info("Force box.read_only=true for election_mode=off")
cfg.box.read_only = true
end
if not cfg.box.replication_synchro_quorum then
cfg.box.replication_synchro_quorum = M.default_synchro_quorum
end
if cfg.box.election_mode == "candidate" then
cfg.box.read_only = false
end
deep_merge(cfg, local_cfg)
return cfg
end;
}
local function cast_types(c)
if c then
for k,v in pairs(c) do
if load_cfg.template_cfg[k] == 'boolean' and type(v) == 'string' then
c[k] = c[k] == 'true'
end
end
end
end
local function gen_instance_uuid(instance_name)
local k,d1,d2 = instance_name:match("^([A-Za-z_]+)_(%d+)_(%d+)$")
if k then
return string.format(
"%08s-%04d-%04d-%04d-%012x",
digest.sha1_hex(k .. "_instance"):sub(1,8),
d1,d2,0,0
)
end
k,d1 = instance_name:match("^([A-Za-z_]+)_(%d+)$")
if k then
return string.format(
"%08s-%04d-%04d-%04d-%012d",
digest.sha1_hex(k):sub(1,8),
0,0,0,d1
)
end
error("Can't generate uuid for instance "..instance_name, 2)
end
local function gen_cluster_uuid(cluster_name)
local k,d1 = cluster_name:match("^([A-Za-z_]+)_(%d+)$")
if k then
return string.format(
"%08s-%04d-%04d-%04d-%012d",
digest.sha1_hex(k .. "_shard"):sub(1,8),
d1,0,0,0
)
end
error("Can't generate uuid for cluster "..cluster_name, 2)
end
---@class moonlibs.config.opts.etcd:moonlibs.config.etcd.opts
---@field instance_name string Mandatory name of the instance
---@field prefix string Mandatory prefix inside etcd tree
---@field uuid? 'auto' When auto config generates replicaset_uuid and instance_uuid for nodes
---@field fixed? table Optional ETCD tree
---Loads configuration from etcd and evaluate master_selection_policy
---@param M moonlibs.config
---@param etcd_conf moonlibs.config.opts.etcd
---@param local_cfg table<string,any>
---@return table<string, any>
local function etcd_load( M, etcd_conf, local_cfg )
local etcd
local instance_name = assert(etcd_conf.instance_name,"etcd.instance_name is required")
local prefix = assert(etcd_conf.prefix,"etcd.prefix is required")
if etcd_conf.fixed then
etcd = setmetatable({ data = etcd_conf.fixed },{__index = {
discovery = function() end;
list = function(e,k)
if k:sub(1,#prefix) == prefix then
k = k:sub(#prefix + 1)
end
local v = e.data
for key in k:gmatch("([^/]+)") do
if type(v) ~= "table" then return end
v = v[key]
end
return v
end;
}})
else
etcd = require 'config.etcd' (etcd_conf)
end
M.etcd = etcd
function M.etcd.get_common(e)
local common_cfg = e:list(prefix .. "/common")
assert(common_cfg.box,"no box config in etcd common tree")
cast_types(common_cfg.box)
return common_cfg
end
function M.etcd.get_instances(e)
local all_instances_cfg = e:list(prefix .. "/instances")
for inst_name,inst_cfg in pairs(all_instances_cfg) do
cast_types(inst_cfg.box)
if etcd_conf.uuid == 'auto' and not inst_cfg.box.instance_uuid then
inst_cfg.box.instance_uuid = gen_instance_uuid(inst_name)
end
end
return all_instances_cfg
end
function M.etcd.get_clusters(e)
local all_clusters_cfg = e:list(prefix .. "/clusters") or etcd:list(prefix .. "/shards")
for cluster_name,cluster_cfg in pairs(all_clusters_cfg) do
cast_types(cluster_cfg)
if etcd_conf.uuid == 'auto' and not cluster_cfg.replicaset_uuid then
cluster_cfg.replicaset_uuid = gen_cluster_uuid(cluster_name)
end
end
return all_clusters_cfg
end
function M.etcd.get_all(e)
local all_cfg = e:list(prefix)
cast_types(all_cfg.common.box)
for inst_name,inst_cfg in pairs(all_cfg.instances) do
cast_types(inst_cfg.box)
if etcd_conf.uuid == 'auto' and not inst_cfg.box.instance_uuid then
inst_cfg.box.instance_uuid = gen_instance_uuid(inst_name)
end
end
for cluster_name,cluster_cfg in pairs(all_cfg.clusters or all_cfg.shards or {}) do
cast_types(cluster_cfg)
if etcd_conf.uuid == 'auto' and not cluster_cfg.replicaset_uuid then
cluster_cfg.replicaset_uuid = gen_cluster_uuid(cluster_name)
end
end
return all_cfg
end
etcd:discovery()
local all_cfg = etcd:get_all()
if etcd_conf.print_config then
print("Loaded config from etcd",yaml.encode(all_cfg))
end
local common_cfg = all_cfg.common
local all_instances_cfg = all_cfg.instances
local instance_cfg = all_instances_cfg[instance_name]
assert(instance_cfg,"Instance name "..instance_name.." is not known to etcd")
local all_clusters_cfg = all_cfg.clusters or all_cfg.shards
local master_selection_policy
local cluster_cfg
if instance_cfg.cluster or local_cfg.cluster then
cluster_cfg = all_clusters_cfg[ (instance_cfg.cluster or local_cfg.cluster) ]
assert(cluster_cfg,"Cluster section required");
assert(cluster_cfg.replicaset_uuid,"Need cluster uuid")
master_selection_policy = M.master_selection_policy or 'etcd.instance.read_only'
elseif instance_cfg.router then
-- TODO
master_selection_policy = M.master_selection_policy or 'etcd.instance.single'
else
master_selection_policy = M.master_selection_policy or 'etcd.instance.single'
end
local master_policy = master_selection_policies[ master_selection_policy ]
if not master_policy then
error(string.format("Unknown master_selection_policy: %s",M.master_selection_policy),0)
end
local cfg = master_policy(M, instance_name, common_cfg, instance_cfg, cluster_cfg, local_cfg)
local members = {}
for _,v in pairs(all_instances_cfg) do
if v.cluster == cfg.cluster then -- and k ~= instance_name then
if not toboolean(v.disabled) then
table.insert(members,v)
else
log.warn("Member '%s' from cluster '%s' listening on %s is disabled", instance_name, v.cluster, v.box.listen)
end
end
end
if cfg.cluster then
--if cfg.box.read_only then
local repl = {}
for _,member in pairs(members) do
if member.box.remote_addr then
table.insert(repl, member.box.remote_addr)
else
table.insert(repl, member.box.listen)
end
end
table.sort(repl, function(a,b)
local ha,pa = a:match('^([^:]+):(.+)')
local hb,pb = a:match('^([^:]+):(.+)')
if pa and pb then
if pa < pb then return true end
if ha < hb then return true end
end
return a < b
end)
if cfg.box.replication then
print(
"Start instance ",cfg.box.listen,
" with locally overriden replication:",table.concat(cfg.box.replication,", "),
" instead of etcd's:", table.concat(repl,", ")
)
else
cfg.box.replication = repl
print(
"Start instance "..cfg.box.listen,
" with replication:"..table.concat(cfg.box.replication,", "),
string.format("timeout: %s, quorum: %s, lag: %s",
cfg.box.replication_connect_timeout
or ('def:%s'):format(load_cfg.default_cfg.replication_connect_timeout or 30),
cfg.box.replication_connect_quorum or 'def:full',
cfg.box.replication_sync_lag
or ('def:%s'):format(load_cfg.default_cfg.replication_sync_lag or 10)
)
)
end
--end
end
-- print(yaml.encode(cfg))
return cfg
end
local function is_replication_changed (old_conf, new_conf)
if type(old_conf) == 'table' and type(new_conf) == 'table' then
local changed_replicas = {}
for _, replica in pairs(old_conf) do
changed_replicas[replica] = true
end
for _, replica in pairs(new_conf) do
if changed_replicas[replica] then
changed_replicas[replica] = nil
else
return true
end
end
-- if we have some changed_replicas left, then we definitely need to reconnect
return not not next(changed_replicas)
else
return old_conf ~= new_conf
end
end
local function optimal_rcq(upstreams)
local n_ups = #(upstreams or {})
local rcq
if n_ups == 0 then
rcq = 0
else
rcq = 1+math.floor(n_ups/2)
end
return rcq
end
local function do_cfg(boxcfg, cfg)
for key, val in pairs(cfg) do
if load_cfg.template_cfg[key] == nil then
local warn = string.format("Dropping non-boxcfg option '%s' given '%s'",key,val)
log.warn("%s",warn)
print(warn)
cfg[key] = nil
end
end
if type(box.cfg) ~= 'function' then
for key, val in pairs(cfg) do
if load_cfg.dynamic_cfg[key] == nil and box.cfg[key] ~= val then
local warn = string.format(
"Dropping dynamic option '%s' previous value '%s' new value '%s'",
key,box.cfg[key],val
)
log.warn("%s",warn)
print(warn)
cfg[key] = nil
end
end
end
log.info("Just before first box.cfg %s", yaml.encode(cfg))
-- Some boxcfg loves to rewrite passing table. We pass a copy of configuration
boxcfg(deep_copy(cfg))
end
---@class moonlibs.config.opts
---@field bypass_non_dynamic? boolean (default: true) drops every changed non-dynamic option on reconfiguration
---@field tidy_load? boolean (default: true) recoveries tarantool with read_only=true
---@field mkdir? boolean (default: false) should moonlibs/config create memtx_dir and wal_dir
---@field etcd? moonlibs.config.opts.etcd [legacy] configuration of etcd
---@field default_replication_connect_timeout? number (default: 1.1) default RCT in seconds
---@field default_election_mode? election_mode (default: candidate) option is respected only when etcd.cluster.raft is used
---@field default_synchro_quorum? string|number (default: 'N/2+1') option is respected only when etcd.cluster.raft is used
---@field default_read_only? boolean (default: false) option is respected only when etcd.instance.read_only is used (deprecated)
---@field master_selection_policy? 'etcd.cluster.master'|'etcd.cluster.vshard'|'etcd.cluster.raft'|'etcd.instance.single' master selection policy
---@field strict_mode? boolean (default: false) stricts config retrievals. if key is not found config.get will raise an exception
---@field strict? boolean (default: false) stricts config retrievals. if key is not found config.get will raise an exception
---@field default? table<string,any> (default: nil) globally default options for config.get
---@field on_load? fun(conf: moonlibs.config, cfg: table<string,any>) callback which is called every time config is loaded from file and ETCD
---@field load? fun(conf: moonlibs.config, cfg: table<string,any>): table<string,any> do not use this callback
---@field on_before_cfg? fun(conf: moonlibs.config, cfg: table<string,any>) callback is called right before running box.cfg (but after on_load)
---@field boxcfg? fun(cfg: table<string,any>) [legacy] when provided this function will be called instead box.cfg. tidy_load and everything else will not be used.
---@field wrap_box_cfg? fun(cfg: table<string,any>) callback is called instead box.cfg. But tidy_load is respected. Use this, if you need to proxy every option to box.cfg on application side
---@field on_after_cfg? fun(conf: moonlibs.config, cfg: table<string,any>) callback which is called after full tarantool configuration
---@class moonlibs.config: moonlibs.config.opts
---@field etcd moonlibs.config.etcd
---@field public _load_cfg table
---@field public _flat table
---@field public _fencing_f? Fiber
---@field public _enforced_ro? boolean
---@operator call(moonlibs.config.opts): moonlibs.config
---@type moonlibs.config
local M
M = setmetatable({
_VERSION = '0.7.2',
console = {};
---Retrieves value from config
---@overload fun(k: string, def: any?): any?
---@param self moonlibs.config
---@param k string path inside config
---@param def? any optional default value
---@return any?
get = function(self,k,def)
if self ~= M then
def = k
k = self
end
if M._flat[k] ~= nil then
return M._flat[k]
elseif def ~= nil then
return def
else
if M.strict_mode then
error(string.format("no %s found in config", k))
else
return
end
end
end,
enforce_ro = function()
if not M._ro_enforcable then
return false, 'cannot enforce readonly'
end
M._enforced_ro = true
return true, {
info_ro = box.info.ro,
cfg_ro = box.cfg.read_only,
enforce_ro = M._enforced_ro,
}
end,
_load_cfg = load_cfg,
},{
---Reinitiates moonlibs.config
---@param args moonlibs.config.opts
---@return moonlibs.config
__call = function(_, args)
-- args MUST belong to us, because of modification
local file
if type(args) == 'string' then
file = args
args = {}
elseif type(args) == 'table' then
args = deep_copy(args)
file = args.file
else
args = {}
end
if args.bypass_non_dynamic == nil then
args.bypass_non_dynamic = true
end
if args.tidy_load == nil then
args.tidy_load = true
end
M.default_replication_connect_timeout = args.default_replication_connect_timeout or 1.1
M.default_election_mode = args.default_election_mode or 'candidate'
M.default_synchro_quorum = args.default_synchro_quorum or 'N/2+1'
M.default_read_only = args.default_read_only or false
M.master_selection_policy = args.master_selection_policy
M.default = args.default
M.strict_mode = args.strict_mode or args.strict or false
-- print("config", "loading ",file, json.encode(args))
if not file then
file = get_opt()
-- todo: maybe etcd?
if not file then error("Neither config call option given not -c|--config option passed",2) end
end
print(string.format("Loading config %s %s", file, json.encode(args)))
local function load_config()
local methods = {}
function methods.merge(dst, src, keep)
if src ~= nil then
deep_merge( dst, src, keep )
end
return dst
end
function methods.include(path, opts)
path = fio.pathjoin(fio.dirname(file), path)
opts = opts or { if_exists = false }
if not fio.path.exists(path) then
if opts.if_exists then
return
end
error("Not found include file `"..path.."'", 2)
end
local f,e = loadfile(path)
if not f then error(e,2) end
setfenv(f, getfenv(2))
local ret = f()
if ret ~= nil then
print("Return value from "..path.." is ignored")
end
end
function methods.print(...)
local p = {...}
for i = 1, select('#', ...) do
if type(p[i]) == 'table'
and not debug.getmetatable(p[i])
then
p[i] = json.encode(p[i])
end
end
print(unpack(p))
end
local f,e = loadfile(file)
if not f then error(e,2) end
local cfg = setmetatable({}, {
__index = setmetatable(methods, {
__index = setmetatable(args,{ __index = _G })
})
})
setfenv(f, cfg)
local ret = f()
if ret ~= nil then
print("Return value from "..file.." is ignored")
end
setmetatable(cfg,nil)
setmetatable(args,nil)
deep_merge(cfg,args.default or {},'keep')
-- subject to change, just a PoC
local etcd_conf = args.etcd or cfg.etcd
-- we can enforce ro during recovery only if we have etcd config
M._ro_enforcable = M._ro_enforcable and etcd_conf ~= nil
if etcd_conf then
local s = clock.time()
cfg = etcd_load(M, etcd_conf, cfg)
log.info("etcd_load took %.3fs", clock.time()-s)
end
if args.load then
cfg = args.load(M, cfg)
end
if not cfg.box then
error("No box.* config given", 2)
end
if cfg.box.remote_addr then
cfg.box.remote_addr = nil
end
if args.bypass_non_dynamic then
cfg.box = prepare_box_cfg(cfg.box)
end
deep_merge(cfg,{
sys = deep_copy(args)
})
cfg.sys.boxcfg = nil
cfg.sys.on_load = nil
-- latest modifications and fixups
if args.on_load then
args.on_load(M,cfg)
end
return cfg
end
-- We cannot enforce ro if any of theese conditions not satisfied
-- Tarantool must be bootstraping with tidy_load and do not overwraps personal boxcfg
M._ro_enforcable = args.boxcfg == nil and args.tidy_load and type(box.cfg) == 'function'
local cfg = load_config() --[[@as table]]
M._flat = flatten(cfg)
if args.on_before_cfg then
args.on_before_cfg(M,cfg)
end
if args.mkdir then
if not ( fio.path and fio.mkdir ) then
error(string.format("Tarantool version %s is too old for mkdir: fio.path is not supported", _TARANTOOL),2)
end
for _,key in pairs({"work_dir", "wal_dir", "snap_dir", "memtx_dir", "vinyl_dir"}) do
local v = cfg.box[key]
if v and not fio.path.exists(v) then
local r,e = fio.mktree(v)
if not r then error(string.format("Failed to create path '%s' for %s: %s",v,key,e),2) end
end
end
local v = cfg.box.pid_file
if v then
v = fio.dirname(v);
if v and not fio.path.exists(v) then
local r,e = fio.mktree(v)
if not r then error(string.format("Failed to create path '%s' for pid_file: %s",v,e),2) end
end
end
end
-- The code below is very hard to understand and quite hard to fix when any bugs occurs.
-- First, you must remember that this part of code is executed several times in very different environments:
-- 1) Tarantool may be started with tarantool <script-name>.lua and this part is required from the script
-- 2) Tarantool may be started under tarantoolctl (such as tarantoolctl start <script-name>.lua) then box.cfg will be wrapped
-- by tarantoolctl itself, and it be returned back to table box.cfg after first successfull execution
-- 3) Tarantool may be started inside docker container and default docker-entrypoint.lua also rewraps box.cfg
-- 4) User might want to overwrite box.cfg with his function via args.boxcfg. Though, this method is not recommended
-- it is possible in some environments
-- 5) User might want to "wrap" box.cfg with his own middleware via (args.wrap_box_cfg). It is more recommended, because
-- full algorithm of tidy_load and ro-enforcing is preserved for the user.
-- Moreover, first run of box.cfg in the life of the process allows to specify static box.cfg options, such as pid_file, log
-- and many others.
-- But, second reconfiguration of box.cfg (due to reload, or reconfiguration in fencing must never touch static options)
-- Part of this is fixed in `do_cfg` method of this codebase.
-- Because many wrappers in docker-entrypoint.lua and tarantoolctl LOVES to perform non-redoable actions inside box.cfg and
-- switch box.cfg back to builtin tarantool box.cfg, following code MUST NEVER cache value of box.cfg
if args.boxcfg then