/
box.cc
2104 lines (1875 loc) · 54.2 KB
/
box.cc
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
/*
* Copyright 2010-2016, Tarantool AUTHORS, please see AUTHORS file.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
*
* 1. Redistributions of source code must retain the above
* copyright notice, this list of conditions and the
* following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials
* provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY <COPYRIGHT HOLDER> ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* <COPYRIGHT HOLDER> OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "box/box.h"
#include "trivia/config.h"
#include "lua/utils.h" /* lua_hash() */
#include "fiber_pool.h"
#include <say.h>
#include <scoped_guard.h>
#include "identifier.h"
#include "iproto.h"
#include "iproto_constants.h"
#include "recovery.h"
#include "wal.h"
#include "relay.h"
#include "applier.h"
#include <rmean.h>
#include "main.h"
#include "tuple.h"
#include "session.h"
#include "schema.h"
#include "engine.h"
#include "memtx_engine.h"
#include "sysview_engine.h"
#include "vinyl.h"
#include "space.h"
#include "index.h"
#include "port.h"
#include "txn.h"
#include "user.h"
#include "cfg.h"
#include "coio.h"
#include "replication.h" /* replica */
#include "title.h"
#include "xrow.h"
#include "xrow_io.h"
#include "xstream.h"
#include "authentication.h"
#include "path_lock.h"
#include "gc.h"
#include "checkpoint.h"
#include "sql.h"
#include "systemd.h"
#include "call.h"
#include "func.h"
#include "sequence.h"
static char status[64] = "unknown";
/** box.stat rmean */
struct rmean *rmean_box;
static void title(const char *new_status)
{
snprintf(status, sizeof(status), "%s", new_status);
title_set_status(new_status);
title_update();
systemd_snotify("STATUS=%s", status);
}
bool box_checkpoint_is_in_progress = false;
/**
* If backup is in progress, this points to the gc consumer
* object that prevents the garbage collector from deleting
* the checkpoint files that are currently being backed up.
*/
static struct gc_consumer *backup_gc;
/**
* The instance is in read-write mode: the local checkpoint
* and all write ahead logs are processed. For a replica,
* it also means we've successfully connected to the master
* and began receiving updates from it.
*/
static bool is_box_configured = false;
static bool is_ro = true;
static fiber_cond ro_cond;
/**
* The following flag is set if the instance failed to
* synchronize to a sufficient number of replicas to form
* a quorum and so was forced to switch to read-only mode.
*/
static bool is_orphan = true;
/* Use the shared instance of xstream for all appliers */
static struct xstream join_stream;
static struct xstream subscribe_stream;
/**
* The pool of fibers in the transaction processor thread
* working on incoming messages from net, wal and other
* threads.
*/
static struct fiber_pool tx_fiber_pool;
/**
* A separate endpoint for WAL wakeup messages, to
* ensure that WAL messages are delivered even
* if all fibers in tx_fiber_pool are used. Without
* this endpoint, tx thread could deadlock when there
* are too many messages in flight (gh-1892).
*/
static struct cbus_endpoint tx_prio_endpoint;
static int
box_check_writable(void)
{
/* box is only writable if box.cfg.read_only == false and */
if (is_ro || is_orphan) {
diag_set(ClientError, ER_READONLY);
diag_log();
return -1;
}
return 0;
}
static void
box_check_writable_xc(void)
{
if (box_check_writable() != 0)
diag_raise();
}
static void
box_check_memtx_min_tuple_size(ssize_t memtx_min_tuple_size)
{
if (memtx_min_tuple_size < 8 || memtx_min_tuple_size > 1048280)
tnt_raise(ClientError, ER_CFG, "memtx_min_tuple_size",
"specified value is out of bounds");
}
int
box_process_rw(struct request *request, struct space *space,
struct tuple **result)
{
assert(iproto_type_is_dml(request->type));
rmean_collect(rmean_box, request->type, 1);
if (access_check_space(space, PRIV_W) != 0)
return -1;
struct txn *txn = txn_begin_stmt(space);
if (txn == NULL)
return -1;
struct tuple *tuple;
if (space_execute_dml(space, txn, request, &tuple) != 0) {
txn_rollback_stmt();
return -1;
}
/*
* Pin the tuple locally before the commit,
* otherwise it may go away during yield in
* when WAL is written in autocommit mode.
*/
TupleRefNil ref(tuple);
if (txn_commit_stmt(txn, request) != 0)
return -1;
if (result != NULL) {
if (tuple != NULL && tuple_bless(tuple) == NULL)
return -1;
*result = tuple;
}
return 0;
}
void
box_set_ro(bool ro)
{
is_ro = ro;
fiber_cond_broadcast(&ro_cond);
}
bool
box_is_ro(void)
{
return is_ro || is_orphan;
}
int
box_wait_ro(bool ro, double timeout)
{
double deadline = ev_monotonic_now(loop()) + timeout;
while (box_is_ro() != ro) {
if (fiber_cond_wait_deadline(&ro_cond, deadline) != 0)
return -1;
if (fiber_is_cancelled()) {
diag_set(FiberIsCancelled);
return -1;
}
}
return 0;
}
void
box_clear_orphan(void)
{
if (!is_orphan)
return; /* nothing to do */
is_orphan = false;
fiber_cond_broadcast(&ro_cond);
/* Update the title to reflect the new status. */
title("running");
}
struct wal_stream {
struct xstream base;
/** How many rows have been recovered so far. */
size_t rows;
/** Yield once per 'yield' rows. */
size_t yield;
};
/**
* A stub used in txn_commit() during local recovery. We "replay"
* transactions during local recovery, with WAL turned off.
* Since each transaction attempts to write itself to WAL at
* commit, we need an implementation which would fake WAL write.
*/
struct recovery_journal {
struct journal base;
struct vclock *vclock;
};
/**
* Use the current row LSN as commit LSN - vinyl needs to see the
* exact same signature during local recovery to properly mark
* min/max LSN of created LSM levels.
*/
static int64_t
recovery_journal_write(struct journal *base,
struct journal_entry * /* entry */)
{
struct recovery_journal *journal = (struct recovery_journal *) base;
return vclock_sum(journal->vclock);
}
static inline void
recovery_journal_create(struct recovery_journal *journal, struct vclock *v)
{
journal_create(&journal->base, recovery_journal_write, NULL);
journal->vclock = v;
}
static inline void
apply_row(struct xstream *stream, struct xrow_header *row)
{
assert(row->bodycnt == 1); /* always 1 for read */
(void) stream;
struct request request;
xrow_decode_dml_xc(row, &request, dml_request_key_map(row->type));
struct space *space = space_cache_find_xc(request.space_id);
if (box_process_rw(&request, space, NULL) != 0) {
say_error("error applying row: %s", request_str(&request));
diag_raise();
}
}
static void
apply_wal_row(struct xstream *stream, struct xrow_header *row)
{
apply_row(stream, row);
struct wal_stream *xstream =
container_of(stream, struct wal_stream, base);
/**
* Yield once in a while, but not too often,
* mostly to allow signal handling to take place.
*/
if (++xstream->rows % xstream->yield == 0)
fiber_sleep(0);
}
static void
wal_stream_create(struct wal_stream *ctx, size_t wal_max_rows)
{
xstream_create(&ctx->base, apply_wal_row);
ctx->rows = 0;
/**
* Make the yield logic covered by the functional test
* suite, which has a small setting for rows_per_wal.
* Each yield can take up to 1ms if there are no events,
* so we can't afford many of them during recovery.
*/
ctx->yield = (wal_max_rows >> 4) + 1;
}
static void
apply_initial_join_row(struct xstream *stream, struct xrow_header *row)
{
(void) stream;
struct request request;
xrow_decode_dml_xc(row, &request, dml_request_key_map(row->type));
struct space *space = space_cache_find_xc(request.space_id);
/* no access checks here - applier always works with admin privs */
space_apply_initial_join_row_xc(space, &request);
}
/* {{{ configuration bindings */
static void
box_check_say()
{
const char *log = cfg_gets("log");
if (log == NULL)
return;
enum say_logger_type type;
if (say_parse_logger_type(&log, &type) < 0) {
tnt_raise(ClientError, ER_CFG, "log",
diag_last_error(diag_get())->errmsg);
}
if (say_check_init_str(log) == -1) {
diag_raise();
}
if (type == SAY_LOGGER_SYSLOG) {
struct say_syslog_opts opts;
if (say_parse_syslog_opts(log, &opts) < 0) {
if (diag_last_error(diag_get())->type ==
&type_IllegalParams) {
tnt_raise(ClientError, ER_CFG, "log",
diag_last_error(diag_get())->errmsg);
}
}
say_free_syslog_opts(&opts);
diag_raise();
}
const char *log_format = cfg_gets("log_format");
enum say_format format = say_format_by_name(log_format);
if (format == say_format_MAX)
diag_set(ClientError, ER_CFG, "log_format",
"expected 'plain' or 'json'");
if (type == SAY_LOGGER_SYSLOG && format == SF_JSON) {
tnt_raise(ClientError, ER_ILLEGAL_PARAMS, "log, log_format");
}
int log_nonblock = cfg_getb("log_nonblock");
if (log_nonblock == 1 && type == SAY_LOGGER_FILE) {
tnt_raise(ClientError, ER_ILLEGAL_PARAMS, "log, log_nonblock");
}
}
static enum say_format
box_check_log_format(const char *log_format)
{
enum say_format format = say_format_by_name(log_format);
if (format == say_format_MAX)
tnt_raise(ClientError, ER_CFG, "log_format",
"expected 'plain' or 'json'");
return format;
}
static void
box_check_uri(const char *source, const char *option_name)
{
if (source == NULL)
return;
struct uri uri;
/* URI format is [host:]service */
if (uri_parse(&uri, source) || !uri.service) {
tnt_raise(ClientError, ER_CFG, option_name,
"expected host:service or /unix.socket");
}
}
static void
box_check_replication(void)
{
int count = cfg_getarr_size("replication");
for (int i = 0; i < count; i++) {
const char *source = cfg_getarr_elem("replication", i);
box_check_uri(source, "replication");
}
}
static double
box_check_replication_timeout(void)
{
double timeout = cfg_getd("replication_timeout");
if (timeout <= 0) {
tnt_raise(ClientError, ER_CFG, "replication_timeout",
"the value must be greather than 0");
}
return timeout;
}
static double
box_check_replication_connect_timeout(void)
{
double timeout = cfg_getd("replication_connect_timeout");
if (timeout <= 0) {
tnt_raise(ClientError, ER_CFG, "replication_connect_timeout",
"the value must be greather than 0");
}
return timeout;
}
static int
box_check_replication_connect_quorum(void)
{
int quorum = cfg_geti_default("replication_connect_quorum",
REPLICATION_CONNECT_QUORUM_ALL);
if (quorum < 0) {
tnt_raise(ClientError, ER_CFG, "replication_connect_quorum",
"the value must be greater or equal to 0");
}
return quorum;
}
static double
box_check_replication_sync_lag(void)
{
double lag = cfg_getd_default("replication_sync_lag", TIMEOUT_INFINITY);
if (lag <= 0) {
tnt_raise(ClientError, ER_CFG, "replication_sync_lag",
"the value must be greater than 0");
}
return lag;
}
static void
box_check_instance_uuid(struct tt_uuid *uuid)
{
*uuid = uuid_nil;
const char *uuid_str = cfg_gets("instance_uuid");
if (uuid_str != NULL && tt_uuid_from_string(uuid_str, uuid) != 0)
tnt_raise(ClientError, ER_CFG, "instance_uuid", uuid_str);
}
static void
box_check_replicaset_uuid(struct tt_uuid *uuid)
{
*uuid = uuid_nil;
const char *uuid_str = cfg_gets("replicaset_uuid");
if (uuid_str != NULL && tt_uuid_from_string(uuid_str, uuid) != 0)
tnt_raise(ClientError, ER_CFG, "replicaset_uuid", uuid_str);
}
static enum wal_mode
box_check_wal_mode(const char *mode_name)
{
assert(mode_name != NULL); /* checked in Lua */
int mode = strindex(wal_mode_STRS, mode_name, WAL_MODE_MAX);
if (mode == WAL_MODE_MAX)
tnt_raise(ClientError, ER_CFG, "wal_mode", mode_name);
return (enum wal_mode) mode;
}
static void
box_check_readahead(int readahead)
{
enum { READAHEAD_MIN = 128, READAHEAD_MAX = 2147483647 };
if (readahead < (int) READAHEAD_MIN ||
readahead > (int) READAHEAD_MAX) {
tnt_raise(ClientError, ER_CFG, "readahead",
"specified value is out of bounds");
}
}
static void
box_check_checkpoint_count(int checkpoint_count)
{
if (checkpoint_count < 1) {
tnt_raise(ClientError, ER_CFG, "checkpoint_count",
"the value must not be less than one");
}
}
static int64_t
box_check_wal_max_rows(int64_t wal_max_rows)
{
/* check rows_per_wal configuration */
if (wal_max_rows <= 1) {
tnt_raise(ClientError, ER_CFG, "rows_per_wal",
"the value must be greater than one");
}
return wal_max_rows;
}
static int64_t
box_check_wal_max_size(int64_t wal_max_size)
{
/* check wal_max_bytes configuration */
if (wal_max_size <= 1) {
tnt_raise(ClientError, ER_CFG, "wal_max_size",
"the value must be greater than one");
}
return wal_max_size;
}
static void
box_check_vinyl_options(void)
{
int read_threads = cfg_geti("vinyl_read_threads");
int write_threads = cfg_geti("vinyl_write_threads");
int64_t range_size = cfg_geti64("vinyl_range_size");
int64_t page_size = cfg_geti64("vinyl_page_size");
int run_count_per_level = cfg_geti("vinyl_run_count_per_level");
double run_size_ratio = cfg_getd("vinyl_run_size_ratio");
double bloom_fpr = cfg_getd("vinyl_bloom_fpr");
if (read_threads < 1) {
tnt_raise(ClientError, ER_CFG, "vinyl_read_threads",
"must be greater than or equal to 1");
}
if (write_threads < 2) {
tnt_raise(ClientError, ER_CFG, "vinyl_write_threads",
"must be greater than or equal to 2");
}
if (range_size <= 0) {
tnt_raise(ClientError, ER_CFG, "vinyl_range_size",
"must be greater than 0");
}
if (page_size <= 0 || page_size > range_size) {
tnt_raise(ClientError, ER_CFG, "vinyl_page_size",
"must be greater than 0 and less than "
"or equal to vinyl_range_size");
}
if (run_count_per_level <= 0) {
tnt_raise(ClientError, ER_CFG, "vinyl_run_count_per_level",
"must be greater than 0");
}
if (run_size_ratio <= 1) {
tnt_raise(ClientError, ER_CFG, "vinyl_run_size_ratio",
"must be greater than 1");
}
if (bloom_fpr <= 0 || bloom_fpr > 1) {
tnt_raise(ClientError, ER_CFG, "vinyl_bloom_fpr",
"must be greater than 0 and less than or equal to 1");
}
}
void
box_check_config()
{
struct tt_uuid uuid;
box_check_say();
box_check_uri(cfg_gets("listen"), "listen");
box_check_instance_uuid(&uuid);
box_check_replicaset_uuid(&uuid);
box_check_replication();
box_check_replication_timeout();
box_check_replication_connect_timeout();
box_check_replication_connect_quorum();
box_check_replication_sync_lag();
box_check_readahead(cfg_geti("readahead"));
box_check_checkpoint_count(cfg_geti("checkpoint_count"));
box_check_wal_max_rows(cfg_geti64("rows_per_wal"));
box_check_wal_max_size(cfg_geti64("wal_max_size"));
box_check_wal_mode(cfg_gets("wal_mode"));
box_check_memtx_min_tuple_size(cfg_geti64("memtx_min_tuple_size"));
box_check_vinyl_options();
}
/*
* Parse box.cfg.replication and create appliers.
*/
static struct applier **
cfg_get_replication(int *p_count)
{
/* Use static buffer for result */
static struct applier *appliers[VCLOCK_MAX];
int count = cfg_getarr_size("replication");
if (count >= VCLOCK_MAX) {
tnt_raise(ClientError, ER_CFG, "replication",
"too many replicas");
}
for (int i = 0; i < count; i++) {
const char *source = cfg_getarr_elem("replication", i);
struct applier *applier = applier_new(source,
&join_stream,
&subscribe_stream);
if (applier == NULL) {
/* Delete created appliers */
while (--i >= 0)
applier_delete(appliers[i]);
return NULL;
}
appliers[i] = applier; /* link to the list */
}
*p_count = count;
return appliers;
}
/*
* Sync box.cfg.replication with the cluster registry, but
* don't start appliers.
*/
static void
box_sync_replication(double timeout, bool connect_all)
{
int count = 0;
struct applier **appliers = cfg_get_replication(&count);
if (appliers == NULL)
diag_raise();
auto guard = make_scoped_guard([=]{
for (int i = 0; i < count; i++)
applier_delete(appliers[i]); /* doesn't affect diag */
});
replicaset_connect(appliers, count, timeout, connect_all);
guard.is_active = false;
}
void
box_set_replication(void)
{
if (!is_box_configured) {
/*
* Do nothing, we're in local hot standby mode, this instance
* will automatically begin following the replica when local
* hot standby mode is finished, see box_cfg().
*/
return;
}
box_check_replication();
/* Try to connect to all replicas within the timeout period */
box_sync_replication(replication_connect_timeout, true);
/* Follow replica */
replicaset_follow();
}
void
box_set_replication_timeout(void)
{
replication_timeout = box_check_replication_timeout();
}
void
box_set_replication_connect_timeout(void)
{
replication_connect_timeout = box_check_replication_connect_timeout();
}
void
box_set_replication_connect_quorum(void)
{
replication_connect_quorum = box_check_replication_connect_quorum();
if (is_box_configured)
replicaset_check_quorum();
}
void
box_set_replication_skip_conflict(void)
{
replication_skip_conflict = cfg_geti("replication_skip_conflict");
}
void
box_bind(void)
{
const char *uri = cfg_gets("listen");
box_check_uri(uri, "listen");
iproto_bind(uri);
}
void
box_listen(void)
{
iproto_listen();
}
void
box_set_log_level(void)
{
say_set_log_level(cfg_geti("log_level"));
}
void
box_set_log_format(void)
{
enum say_format format = box_check_log_format(cfg_gets("log_format"));
say_set_log_format(format);
}
void
box_set_io_collect_interval(void)
{
ev_set_io_collect_interval(loop(), cfg_getd("io_collect_interval"));
}
void
box_set_snap_io_rate_limit(void)
{
struct memtx_engine *memtx;
memtx = (struct memtx_engine *)engine_by_name("memtx");
assert(memtx != NULL);
memtx_engine_set_snap_io_rate_limit(memtx,
cfg_getd("snap_io_rate_limit"));
}
void
box_set_memtx_max_tuple_size(void)
{
struct memtx_engine *memtx;
memtx = (struct memtx_engine *)engine_by_name("memtx");
assert(memtx != NULL);
memtx_engine_set_max_tuple_size(memtx,
cfg_geti("memtx_max_tuple_size"));
}
void
box_set_too_long_threshold(void)
{
too_long_threshold = cfg_getd("too_long_threshold");
struct vinyl_engine *vinyl;
vinyl = (struct vinyl_engine *)engine_by_name("vinyl");
assert(vinyl != NULL);
vinyl_engine_set_too_long_threshold(vinyl, too_long_threshold);
}
void
box_set_readahead(void)
{
int readahead = cfg_geti("readahead");
box_check_readahead(readahead);
iproto_readahead = readahead;
}
void
box_set_checkpoint_count(void)
{
int checkpoint_count = cfg_geti("checkpoint_count");
box_check_checkpoint_count(checkpoint_count);
gc_set_checkpoint_count(checkpoint_count);
}
void
box_set_vinyl_max_tuple_size(void)
{
struct vinyl_engine *vinyl;
vinyl = (struct vinyl_engine *)engine_by_name("vinyl");
assert(vinyl != NULL);
vinyl_engine_set_max_tuple_size(vinyl,
cfg_geti("vinyl_max_tuple_size"));
}
void
box_set_vinyl_cache(void)
{
struct vinyl_engine *vinyl;
vinyl = (struct vinyl_engine *)engine_by_name("vinyl");
assert(vinyl != NULL);
vinyl_engine_set_cache(vinyl, cfg_geti64("vinyl_cache"));
}
void
box_set_vinyl_timeout(void)
{
struct vinyl_engine *vinyl;
vinyl = (struct vinyl_engine *)engine_by_name("vinyl");
assert(vinyl != NULL);
vinyl_engine_set_timeout(vinyl, cfg_getd("vinyl_timeout"));
}
void
box_set_net_msg_max(void)
{
int new_iproto_msg_max = cfg_geti("net_msg_max");
iproto_set_msg_max(new_iproto_msg_max);
fiber_pool_set_max_size(&tx_fiber_pool,
new_iproto_msg_max *
IPROTO_FIBER_POOL_SIZE_FACTOR);
}
/* }}} configuration bindings */
/**
* Execute a request against a given space id with
* a variable-argument tuple described in format.
*
* @example: you want to insert 5 into space 1:
* boxk(IPROTO_INSERT, 1, "[%u]", 5);
*
* @example: you want to set field 3 (base 0) of
* a tuple with key [10, 20] in space 1 to 1000:
* boxk(IPROTO_UPDATE, 1, "[%u%u][[%s%u%u]]", 10, 20, "=", 3, 1000);
*
* @note Since this is for internal use, it has
* no boundary or misuse checks.
*/
int
boxk(int type, uint32_t space_id, const char *format, ...)
{
va_list ap;
struct request request;
memset(&request, 0, sizeof(request));
request.type = type;
request.space_id = space_id;
va_start(ap, format);
size_t buf_size = mp_vformat(NULL, 0, format, ap);
char *buf = (char *)region_alloc(&fiber()->gc, buf_size);
va_end(ap);
if (buf == NULL)
return -1;
va_start(ap, format);
if (mp_vformat(buf, buf_size, format, ap) != buf_size)
assert(0);
va_end(ap);
const char *data = buf;
const char *data_end = buf + buf_size;
switch (type) {
case IPROTO_INSERT:
case IPROTO_REPLACE:
request.tuple = data;
request.tuple_end = data_end;
break;
case IPROTO_DELETE:
request.key = data;
request.key_end = data_end;
break;
case IPROTO_UPDATE:
request.key = data;
mp_next(&data);
request.key_end = data;
request.tuple = data;
mp_next(&data);
request.tuple_end = data;
request.index_base = 0;
break;
default:
unreachable();
}
struct space *space = space_cache_find(space_id);
if (space == NULL)
return -1;
return box_process_rw(&request, space, NULL);
}
int
box_return_tuple(box_function_ctx_t *ctx, box_tuple_t *tuple)
{
return port_tuple_add(ctx->port, tuple);
}
/* schema_find_id()-like method using only public API */
uint32_t
space_id_by_name(uint32_t system_space_id, const char *name, uint32_t len)
{
if (len > BOX_NAME_MAX)
return BOX_ID_NIL;
uint32_t size = mp_sizeof_array(1) + mp_sizeof_str(len);
char *begin = (char *) region_alloc(&fiber()->gc, size);
if (begin == NULL) {
diag_set(OutOfMemory, size, "region_alloc", "begin");
return BOX_ID_NIL;
}
char *end = mp_encode_array(begin, 1);
end = mp_encode_str(end, name, len);
/* NOTE: error and missing key cases are indistinguishable */
box_tuple_t *tuple;
if (box_index_get(system_space_id, 2, begin, end, &tuple) != 0)
return BOX_ID_NIL;
if (tuple == NULL)
return BOX_ID_NIL;
uint32_t result = BOX_ID_NIL;
(void) tuple_field_u32(tuple, BOX_SPACE_FIELD_ID, &result);
return result;
}
uint32_t
box_space_id_by_name(const char *name, uint32_t len)
{
return space_id_by_name(BOX_VSPACE_ID, name, len);
}
uint32_t
box_index_id_by_name(uint32_t space_id, const char *name, uint32_t len)
{
if (len > BOX_NAME_MAX)
return BOX_ID_NIL;
uint32_t size = mp_sizeof_array(2) + mp_sizeof_uint(space_id) +
mp_sizeof_str(len);
char *begin = (char *) region_alloc(&fiber()->gc, size);
if (begin == NULL) {
diag_set(OutOfMemory, size, "region_alloc", "begin");
return BOX_ID_NIL;
}
char *end = mp_encode_array(begin, 2);
end = mp_encode_uint(end, space_id);
end = mp_encode_str(end, name, len);
/* NOTE: error and missing key cases are indistinguishable */
box_tuple_t *tuple;
if (box_index_get(BOX_VINDEX_ID, 2, begin, end, &tuple) != 0)
return BOX_ID_NIL;
if (tuple == NULL)
return BOX_ID_NIL;
uint32_t result = BOX_ID_NIL;
(void) tuple_field_u32(tuple, BOX_INDEX_FIELD_ID, &result);
return result;
}
/** \endcond public */
int
box_process1(struct request *request, box_tuple_t **result)
{
/* Allow to write to temporary spaces in read-only mode. */
struct space *space = space_cache_find(request->space_id);
if (space == NULL)
return -1;
if (!space->def->opts.temporary && box_check_writable() != 0)
return -1;
return box_process_rw(request, space, result);
}
int
box_select(uint32_t space_id, uint32_t index_id,
int iterator, uint32_t offset, uint32_t limit,
const char *key, const char *key_end,
struct port *port)
{
(void)key_end;
rmean_collect(rmean_box, IPROTO_SELECT, 1);
if (iterator < 0 || iterator >= iterator_type_MAX) {
diag_set(ClientError, ER_ILLEGAL_PARAMS,
"Invalid iterator type");
diag_log();
return -1;
}
struct space *space = space_cache_find(space_id);
if (space == NULL)
return -1;
if (access_check_space(space, PRIV_R) != 0)
return -1;
struct index *index = index_find(space, index_id);
if (index == NULL)
return -1;
enum iterator_type type = (enum iterator_type) iterator;
uint32_t part_count = key ? mp_decode_array(&key) : 0;
if (key_validate(index->def, type, key, part_count))
return -1;
ERROR_INJECT(ERRINJ_TESTING, {
diag_set(ClientError, ER_INJECTION, "ERRINJ_TESTING");
return -1;
});
struct txn *txn;
if (txn_begin_ro_stmt(space, &txn) != 0)
return -1;
struct iterator *it = index_create_iterator(index, type,
key, part_count);
if (it == NULL) {
txn_rollback_stmt();
return -1;
}
int rc = 0;
uint32_t found = 0;
struct tuple *tuple;
port_tuple_create(port);