forked from open-io/oio-sds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmeta2_backend.c
1900 lines (1644 loc) · 51.1 KB
/
meta2_backend.c
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
/*
OpenIO SDS meta2v2
Copyright (C) 2014 Worldline, as part of Redcurrant
Copyright (C) 2015-2018 OpenIO SAS, as part of OpenIO SDS
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <glib.h>
#include <metautils/lib/metautils.h>
#include <metautils/lib/common_variables.h>
#include <server/server_variables.h>
#include <meta2v2/meta2_variables.h>
#include <cluster/lib/gridcluster.h>
#include <sqliterepo/sqlite_utils.h>
#include <sqliterepo/sqliterepo.h>
#include <sqliterepo/election.h>
#include <events/oio_events_queue.h>
#include <meta2v2/generic.h>
#include <meta2v2/autogen.h>
#include <meta2v2/meta2_macros.h>
#include <meta2v2/meta2_utils_lb.h>
#include <meta2v2/meta2_backend_internals.h>
#include <resolver/hc_resolver.h>
enum m2v2_open_type_e
{
M2V2_OPEN_LOCAL = 0x000,
M2V2_OPEN_MASTERONLY = 0x001,
M2V2_OPEN_SLAVEONLY = 0x002,
M2V2_OPEN_MASTERSLAVE = 0x003,
#define M2V2_OPEN_REPLIMODE 0x00F
M2V2_OPEN_AUTOCREATE = 0x010,
#define M2V2_OPEN_FLAGS 0x0F0
// Set an OR'ed combination of the following flags to require
// a check on the container's status during the open phase.
// No flag set means no check.
M2V2_OPEN_ENABLED = 0x100,
M2V2_OPEN_FROZEN = 0x200,
M2V2_OPEN_DISABLED = 0x400,
#define M2V2_OPEN_STATUS 0xF00
};
struct m2_prepare_data
{
gint64 max_versions;
gint64 quota;
gint64 size;
gchar storage_policy[LIMIT_LENGTH_STGPOLICY];
};
static void _meta2_backend_force_prepare_data(struct meta2_backend_s *m2b,
const gchar *key, struct sqlx_sqlite3_s *sq3);
static void m2b_add_modified_container(struct meta2_backend_s *m2b,
struct sqlx_sqlite3_s *sq3);
static enum m2v2_open_type_e
_mode_masterslave(guint32 flags)
{
if (flags & M2V2_FLAG_MASTER)
return M2V2_OPEN_MASTERONLY;
return (flags & M2V2_FLAG_LOCAL)
? M2V2_OPEN_LOCAL : M2V2_OPEN_MASTERSLAVE;
}
static enum m2v2_open_type_e
_mode_readonly(guint32 flags)
{
return M2V2_OPEN_ENABLED|M2V2_OPEN_FROZEN|_mode_masterslave(flags);
}
static gint64
_maxvers(struct sqlx_sqlite3_s *sq3)
{
return m2db_get_max_versions(sq3, meta2_max_versions);
}
static gint64
_retention_delay(struct sqlx_sqlite3_s *sq3)
{
return m2db_get_keep_deleted_delay(sq3, meta2_retention_period);
}
static gchar*
_stgpol(struct sqlx_sqlite3_s *sq3)
{
gchar *stgpol = sqlx_admin_get_str(sq3, M2V2_ADMIN_STORAGE_POLICY);
return stgpol ?: oio_var_get_string(oio_ns_storage_policy);
}
/* Backend ------------------------------------------------------------------ */
static GError*
_check_policy(struct meta2_backend_s *m2, const gchar *polname)
{
GError *err = NULL;
struct storage_policy_s *policy = NULL;
if (!*polname)
return NEWERROR(CODE_BAD_REQUEST, "Invalid policy: %s", "empty");
g_mutex_lock (&m2->nsinfo_lock);
policy = storage_policy_init(m2->nsinfo, polname);
g_mutex_unlock (&m2->nsinfo_lock);
if (!policy)
err = NEWERROR(CODE_POLICY_NOT_SUPPORTED, "Invalid policy: %s", "not found");
else
storage_policy_clean(policy);
return err;
}
const gchar*
meta2_backend_get_local_addr(struct meta2_backend_s *m2)
{
return sqlx_repository_get_local_addr(m2->repo);
}
GError *
meta2_backend_init(struct meta2_backend_s **result,
struct sqlx_repository_s *repo, const gchar *ns,
struct oio_lb_s *lb, struct hc_resolver_s *resolver)
{
EXTRA_ASSERT(result != NULL);
EXTRA_ASSERT(repo != NULL);
EXTRA_ASSERT(lb != NULL);
EXTRA_ASSERT(resolver != NULL);
if (!*ns || strlen(ns) >= LIMIT_LENGTH_NSNAME)
return BADREQ("Invalid namespace name");
struct meta2_backend_s *m2 = g_malloc0(sizeof(struct meta2_backend_s));
g_strlcpy(m2->ns_name, ns, sizeof(m2->ns_name));
m2->type = NAME_SRVTYPE_META2;
m2->repo = repo;
m2->lb = lb;
m2->policies = service_update_policies_create();
g_mutex_init(&m2->nsinfo_lock);
m2->flag_precheck_on_generate = meta2_flag_precheck_on_generate;
// TODO: use a custom hash function
m2->prepare_data_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
g_free, g_free);
g_rw_lock_init(&(m2->prepare_data_lock));
GError *err = sqlx_repository_configure_type(m2->repo,
NAME_SRVTYPE_META2, schema);
if (NULL != err) {
meta2_backend_clean(m2);
g_prefix_error(&err, "Backend init error: ");
return err;
}
m2->resolver = resolver;
*result = m2;
GRID_DEBUG("M2V2 backend created for NS[%s] and repo[%p]",
m2->ns_name, m2->repo);
return NULL;
}
void
meta2_backend_clean(struct meta2_backend_s *m2)
{
if (!m2)
return;
if (m2->policies)
service_update_policies_destroy(m2->policies);
if (m2->resolver)
m2->resolver = NULL;
g_hash_table_unref(m2->prepare_data_cache);
m2->prepare_data_cache = NULL;
g_rw_lock_clear(&(m2->prepare_data_lock));
g_mutex_clear(&m2->nsinfo_lock);
namespace_info_free(m2->nsinfo);
g_free(m2);
}
void
meta2_backend_configure_nsinfo(struct meta2_backend_s *m2,
struct namespace_info_s *ni)
{
EXTRA_ASSERT(m2 != NULL);
EXTRA_ASSERT(ni != NULL);
struct namespace_info_s *old = NULL, *copy = NULL;
copy = namespace_info_dup (ni);
g_mutex_lock(&m2->nsinfo_lock);
old = m2->nsinfo;
m2->nsinfo = copy;
g_mutex_unlock(&m2->nsinfo_lock);
if (old)
namespace_info_free (old);
}
struct namespace_info_s *
meta2_backend_get_nsinfo (struct meta2_backend_s *m2)
{
EXTRA_ASSERT(m2 != NULL);
struct namespace_info_s *out = NULL;
g_mutex_lock(&m2->nsinfo_lock);
if (m2->nsinfo)
out = namespace_info_dup (m2->nsinfo);
g_mutex_unlock(&m2->nsinfo_lock);
return out;
}
gboolean
meta2_backend_initiated(struct meta2_backend_s *m2)
{
EXTRA_ASSERT(m2 != NULL);
g_mutex_lock (&m2->nsinfo_lock);
gboolean rc = (NULL != m2->nsinfo);
g_mutex_unlock (&m2->nsinfo_lock);
return rc;
}
/* Container -------------------------------------------------------------- */
static enum sqlx_open_type_e
m2_to_sqlx(enum m2v2_open_type_e t)
{
enum sqlx_open_type_e result = SQLX_OPEN_LOCAL;
if (t & M2V2_OPEN_MASTERONLY)
result |= SQLX_OPEN_MASTERONLY;
if (t & M2V2_OPEN_SLAVEONLY)
result |= SQLX_OPEN_SLAVEONLY;
if (t & M2V2_OPEN_AUTOCREATE)
result |= SQLX_OPEN_CREATE;
if (t & M2V2_OPEN_ENABLED)
result |= SQLX_OPEN_ENABLED;
if (t & M2V2_OPEN_FROZEN)
result |= SQLX_OPEN_FROZEN;
if (t & M2V2_OPEN_DISABLED)
result |= SQLX_OPEN_DISABLED;
return result;
}
static gboolean
_is_container_initiated(struct sqlx_sqlite3_s *sq3)
{
if (sqlx_admin_has(sq3, META2_INIT_FLAG))
return TRUE;
/* workaround for a known bug, when the container has no flag because
* of some failed replication (yet to be determined) but it is used
* because of a (now-fixed) inexistant check on the flag. */
if (sqlx_admin_has(sq3, M2V2_ADMIN_OBJ_COUNT)
|| sqlx_admin_has(sq3, M2V2_ADMIN_SIZE)) {
GRID_DEBUG("DB partially initiated: [%s][%.s]",
sq3->name.base, sq3->name.type);
return TRUE;
}
return FALSE;
}
/* TODO(jfs): merge the container's state with the m2_prepare_data and keep
* it cached? */
static gchar *
_container_state (struct sqlx_sqlite3_s *sq3)
{
void sep (GString *gs) {
if (gs->len > 1 && !strchr(",[{", gs->str[gs->len-1]))
g_string_append_c (gs, ',');
}
void append_int64 (GString *gs, const char *k, gint64 v) {
sep (gs);
g_string_append_printf (gs, "\"%s\":%"G_GINT64_FORMAT, k, v);
}
void append_const (GString *gs, const char *k, const char *v) {
sep (gs);
oio_str_gstring_append_json_pair(gs, k, v);
}
struct oio_url_s *u = sqlx_admin_get_url (sq3);
/* Patch the SUBTYPE: the URL must represent an object as managed by meta2
* services (and also by end-user APIs). Unlike for sqlx services, the user
* doesn't know anything about the meta2, and never mentions it in URL.
* This is why we strip this from the service type. */
if (!strcmp(sq3->name.type, NAME_SRVTYPE_META2))
oio_url_set (u, OIOURL_TYPE, "");
else if (g_str_has_prefix (sq3->name.type, NAME_SRVTYPE_META2 "."))
oio_url_set (u, OIOURL_TYPE, sq3->name.type + sizeof(NAME_SRVTYPE_META2));
GString *gs = oio_event__create (META2_EVENTS_PREFIX ".container.state", u);
g_string_append_static (gs, ",\"data\":{");
append_const (gs, "policy", sqlx_admin_get_str(sq3, M2V2_ADMIN_STORAGE_POLICY));
append_int64 (gs, "ctime", m2db_get_ctime(sq3));
append_int64 (gs, "bytes-count", m2db_get_size(sq3));
append_int64 (gs, "object-count", m2db_get_obj_count(sq3));
g_string_append_static (gs, "}}");
oio_url_clean (u);
return g_string_free(gs, FALSE);
}
static void
m2b_add_modified_container(struct meta2_backend_s *m2b,
struct sqlx_sqlite3_s *sq3)
{
EXTRA_ASSERT(m2b != NULL);
if (m2b->notifier)
oio_events_queue__send_overwritable(m2b->notifier,
sqlx_admin_get_str(sq3, SQLX_ADMIN_BASENAME),
_container_state(sq3));
gboolean has_peers = FALSE;
NAME2CONST(n, sq3->name);
GError *err = election_has_peers(sq3->manager, &n, FALSE, &has_peers);
if (!err && !has_peers) {
meta2_backend_change_callback(sq3, m2b);
}
g_clear_error(&err);
}
static void
m2b_close(struct sqlx_sqlite3_s *sq3)
{
if (sq3) {
sqlx_repository_unlock_and_close_noerror(sq3);
}
}
static void
m2b_destroy(struct sqlx_sqlite3_s *sq3)
{
if (sq3) {
GRID_INFO("Closing and destroying [%s][%s]", sq3->name.base, sq3->name.type);
sq3->deleted = TRUE;
sqlx_repository_unlock_and_close_noerror(sq3);
}
}
static GError *
m2b_open(struct meta2_backend_s *m2, struct oio_url_s *url,
enum m2v2_open_type_e how, struct sqlx_sqlite3_s **result)
{
GError *err = NULL;
struct sqlx_sqlite3_s *sq3 = NULL;
EXTRA_ASSERT(url != NULL);
EXTRA_ASSERT(result != NULL);
EXTRA_ASSERT(m2 != NULL);
EXTRA_ASSERT(m2->repo != NULL);
struct sqlx_name_inline_s n0;
sqlx_inline_name_fill (&n0, url, NAME_SRVTYPE_META2, 1);
NAME2CONST(n,n0);
err = sqlx_repository_open_and_lock(m2->repo, &n, m2_to_sqlx(how), &sq3, NULL);
if (NULL != err) {
if (err->code == CODE_CONTAINER_NOTFOUND)
err->domain = GQ();
return err;
}
/* beware that LOCAL is maybe == 0 */
const gboolean _local = (M2V2_OPEN_LOCAL == (how & M2V2_OPEN_REPLIMODE));
const gboolean _create = (M2V2_OPEN_AUTOCREATE == (how & M2V2_OPEN_AUTOCREATE));
sq3->no_peers = _local;
// If the container is being deleted, this is sad ...
// This MIGHT happen if a cache is present (and this is the
// common case for m2v2), because the deletion will happen
// when the base exit the cache.
// In facts this SHOULD NOT happend because a base being deleted
// is closed with an instruction to exit the cache immediately.
// TODO FIXME this is maybe a good place for an assert().
if (sq3->deleted) {
m2b_close(sq3);
return NEWERROR(CODE_CONTAINER_FROZEN, "destruction pending");
}
/* The kind of check we do depend of the kind of opening:
* - creation : init not done
* - local access : no check
* - replicated access : int done */
if (_create) {
if (_is_container_initiated(sq3)) {
m2b_close(sq3);
return NEWERROR(CODE_CONTAINER_EXISTS,
"container already initiated");
}
} else if (!_local) {
if (!_is_container_initiated(sq3)) {
m2b_close(sq3);
return NEWERROR(CODE_CONTAINER_NOTFOUND,
"container created but not initiated");
}
}
/* Complete URL with full VNS and container name */
void set(gchar *k, int f) {
if (oio_url_has(url, f))
return;
gchar *s = sqlx_admin_get_str (sq3, k);
if (s) {
oio_url_set (url, f, s);
g_free (s);
}
}
set (SQLX_ADMIN_NAMESPACE, OIOURL_NS);
set (SQLX_ADMIN_ACCOUNT, OIOURL_ACCOUNT);
set (SQLX_ADMIN_USERNAME, OIOURL_USER);
set (SQLX_ADMIN_USERTYPE, OIOURL_TYPE);
*result = sq3;
return NULL;
}
static GError *
m2b_open_if_needed(struct meta2_backend_s *m2, struct oio_url_s *url,
enum m2v2_open_type_e how, struct sqlx_sqlite3_s **result)
{
if (*result)
return NULL;
return m2b_open(m2, url, how, result);
}
static GError*
_transaction_begin(struct sqlx_sqlite3_s *sq3, struct oio_url_s *url,
struct sqlx_repctx_s **result)
{
struct sqlx_repctx_s* repctx = NULL;
EXTRA_ASSERT(result != NULL);
*result = NULL;
GError *err = sqlx_transaction_begin(sq3, &repctx);
if (NULL != err)
return err;
m2db_set_container_name(sq3, url);
*result = repctx;
return NULL;
}
GError *
meta2_backend_has_container(struct meta2_backend_s *m2,
struct oio_url_s *url)
{
GError *err = NULL;
EXTRA_ASSERT(m2 != NULL);
EXTRA_ASSERT(url != NULL);
GRID_DEBUG("HAS(%s)", oio_url_get(url, OIOURL_WHOLE));
struct sqlx_name_inline_s n0;
sqlx_inline_name_fill (&n0, url, NAME_SRVTYPE_META2, 1);
NAME2CONST(n,n0);
err = sqlx_repository_has_base(m2->repo, &n);
if (NULL != err) {
g_prefix_error(&err, "File error: ");
return err;
}
struct sqlx_sqlite3_s *sq3 = NULL;
err = m2b_open(m2, url, M2V2_OPEN_LOCAL, &sq3);
if (NULL == err) {
/* The base is used in LOCAL mode, and with that option the INIT
* flag is not checked. As an exception, we want the check to be
* performed here. */
if (!_is_container_initiated(sq3))
err = NEWERROR(CODE_CONTAINER_NOTFOUND,
"Container created but not initiated");
m2b_close(sq3);
}
return err;
}
static GError *
_check_if_container_empty (struct sqlx_sqlite3_s *sq3)
{
GError *err = NULL;
sqlite3_stmt *stmt = NULL;
gint64 count = 0;
int rc = sqlite3_prepare(sq3->db,
"SELECT exists(SELECT 1 FROM chunks LIMIT 1)",
-1, &stmt, NULL);
while (SQLITE_ROW == (rc = sqlite3_step(stmt)))
count = sqlite3_column_int64 (stmt, 0);
if (rc != SQLITE_OK && rc != SQLITE_DONE) {
if (err) {
GRID_WARN("SQLite error: (%d) %s",
rc, sqlite3_errmsg(sq3->db));
} else {
err = NEWERROR(CODE_INTERNAL_ERROR, "SQLite error: (%d) %s",
rc, sqlite3_errmsg(sq3->db));
}
}
(void) sqlite3_finalize (stmt);
if (!err && count > 0)
err = NEWERROR(CODE_CONTAINER_NOTEMPTY, "Container not empty");
return err;
}
GError *
meta2_backend_container_isempty (struct meta2_backend_s *m2,
struct oio_url_s *url)
{
EXTRA_ASSERT(m2 != NULL);
EXTRA_ASSERT(url != NULL);
GRID_DEBUG("ISEMPTY(%s)", oio_url_get(url, OIOURL_WHOLE));
struct sqlx_sqlite3_s *sq3 = NULL;
GError *err = m2b_open(m2, url, _mode_masterslave(0), &sq3);
if (!err) {
err = _check_if_container_empty (sq3);
m2b_close(sq3);
}
return err;
}
GError*
meta2_backend_get_max_versions(struct meta2_backend_s *m2b,
struct oio_url_s *url, gint64 *result)
{
GError *err = NULL;
struct sqlx_sqlite3_s *sq3 = NULL;
EXTRA_ASSERT(m2b != NULL);
EXTRA_ASSERT(url != NULL);
EXTRA_ASSERT(result != NULL);
err = m2b_open(m2b, url, _mode_readonly(0), &sq3);
if (!err) {
*result = _maxvers(sq3);
m2b_close(sq3);
}
return err;
}
static GError *
_init_container(struct sqlx_sqlite3_s *sq3,
struct oio_url_s *url, struct m2v2_create_params_s *params)
{
GError *err = NULL;
struct sqlx_repctx_s *repctx = NULL;
if (!params->local && (err = _transaction_begin(sq3, url, &repctx)))
return err;
if (!err) {
m2db_set_ctime (sq3, oio_ext_real_time());
m2db_set_size(sq3, 0);
m2db_set_obj_count(sq3, 0);
sqlx_admin_set_status(sq3, ADMIN_STATUS_ENABLED);
sqlx_admin_init_i64(sq3, META2_INIT_FLAG, 1);
}
if (!err && params->properties) {
for (gchar **p=params->properties; *p && *(p+1) ;p+=2)
sqlx_admin_set_str (sq3, *p, *(p+1));
}
if (!err && params->storage_policy)
err = m2db_set_storage_policy(sq3, params->storage_policy, TRUE);
if (!err && params->version_policy) {
gint64 max = g_ascii_strtoll(params->version_policy, NULL, 10);
m2db_set_max_versions(sq3, max);
}
if (!params->local)
err = sqlx_transaction_end(repctx, err);
return err;
}
static GError *
_get_meta2_peers(struct sqlx_sqlite3_s *sq3, struct meta2_backend_s *m2,
GString *peers_array)
{
GError *err = NULL;
gchar **peers = NULL;
NAME2CONST(name, sq3->name);
EXTRA_ASSERT(peers_array != NULL);
const enum election_status_e election_status = sq3->election;
if (election_status == ELECTION_LEADER){
err = sqlx_repository_get_peers(m2->repo, &name, &peers);
if (err)
return err;
}
const gchar *local_addr = sqlx_repository_get_local_addr(m2->repo);
EXTRA_ASSERT(local_addr != NULL);
g_string_append_static(peers_array, "\"peers\":[");
oio_str_gstring_append_json_quote(peers_array, local_addr);
// This is either a NULL terminated array of all the peers or a NULL ptr.
// If there's no elections or no peers, then we find ourselves with
// a NULL ptr.
for (gchar **peer = peers; peers && *peer; peer++) {
g_string_append_c(peers_array, ',');
oio_str_gstring_append_json_quote(peers_array, *peer);
}
g_string_append(peers_array, "]");
g_free(peers);
return err;
}
GError *
meta2_backend_create_container(struct meta2_backend_s *m2,
struct oio_url_s *url, struct m2v2_create_params_s *params)
{
GError *err = NULL;
struct sqlx_sqlite3_s *sq3 = NULL;
GRID_DEBUG("CREATE(%s,%s,%s)%s", oio_url_get(url, OIOURL_WHOLE),
params?params->storage_policy:NULL,
params?params->version_policy:NULL,
(params && params->local)? " (local)" : "");
/* We must check storage policy BEFORE opening the base if we don't
* want to have an empty base in case of invalid policy */
if (params->storage_policy) {
if (NULL != (err = _check_policy(m2, params->storage_policy)))
return err;
}
/* Defer the `m2.init` check to the m2b_open() */
enum m2v2_open_type_e open_mode = M2V2_OPEN_AUTOCREATE |
(params->local ? M2V2_OPEN_LOCAL : M2V2_OPEN_MASTERONLY);
err = m2b_open(m2, url, open_mode, &sq3);
EXTRA_ASSERT((sq3 != NULL) ^ (err != NULL));
if (err)
return err;
/* At this point the base exist and it has nt been initiated yet */
err = _init_container(sq3, url, params);
if (err) {
m2b_destroy(sq3);
return err;
}
/* Fire an event to notify the world this container exists */
const enum election_status_e s = sq3->election;
if (!params->local && m2->notifier && (!s || s == ELECTION_LEADER)) {
GString *peers_list = g_string_sized_new(1024);
err = _get_meta2_peers(sq3, m2, peers_list);
if (err != NULL){
m2b_destroy(sq3);
g_string_free(peers_list, TRUE);
return err;
}
GString *gs = oio_event__create(META2_EVENTS_PREFIX".container.new", url);
g_string_append_static(gs, ",\"data\":{\"properties\":");
KV_encode_gstr2(gs, params->properties);
g_string_append_c(gs, ',');
g_string_append(gs, peers_list->str);
g_string_free(peers_list, TRUE);
g_string_append_static(gs, "}}");
oio_events_queue__send(m2->notifier, g_string_free (gs, FALSE));
}
/* Reload any cache maybe already associated with the container.
* It happens that the cache sometimes exists because created during a
* M2_PREP that occured after a GETVERS (the meta1 is filled) but before
* the CREATE. */
meta2_backend_change_callback(sq3, m2);
m2b_close(sq3);
return err;
}
GError *
meta2_backend_destroy_container(struct meta2_backend_s *m2,
struct oio_url_s *url, guint32 flags)
{
gboolean send_event = BOOL(flags & M2V2_DESTROY_EVENT);
gboolean force = BOOL(flags & M2V2_DESTROY_FORCE);
struct sqlx_sqlite3_s *sq3 = NULL;
GError *err = NULL;
err = m2b_open(m2, url, M2V2_OPEN_LOCAL, &sq3);
if (!err) {
EXTRA_ASSERT(sq3 != NULL);
if (!force)
err = _check_if_container_empty (sq3);
/* TODO(jfs): manage base's subtype */
hc_decache_reference_service(m2->resolver, url, NAME_SRVTYPE_META2);
#ifdef HAVE_ENBUG
if (send_event && !err) {
gint32 random = oio_ext_rand_int_range(0,3);
switch (random) {
case 0:
err = NEWERROR(CODE_BAD_REQUEST, "Fake error (meta2)");
break;
case 1:
sleep(100);
break;
}
}
#endif
if (!err) {
m2b_destroy(sq3);
if (m2->notifier && send_event) {
/* This request handler is local, it will be called on each
* service hosting the base. We must only signal our own
* address; the other peers will do the same. */
const gchar *me = sqlx_repository_get_local_addr(m2->repo);
/* FIXME(FVE): do this at sqliterepo level, to be consistent
* with DB_REMOVE request. */
GString *gs = oio_event__create(
META2_EVENTS_PREFIX ".container.deleted", url);
g_string_append_printf(
gs, ",\"data\":{\"peers\":[\"%s\"]}}", me);
oio_events_queue__send(m2->notifier, g_string_free(gs, FALSE));
}
} else {
m2b_close(sq3);
}
}
return err;
}
GError *
meta2_backend_flush_container(struct meta2_backend_s *m2, struct oio_url_s *url,
m2_onbean_cb cb, gpointer u0, gboolean *truncated)
{
GError *err = NULL;
struct sqlx_sqlite3_s *sq3 = NULL;
struct sqlx_repctx_s *repctx = NULL;
err = m2b_open(m2, url, M2V2_OPEN_MASTERONLY|M2V2_OPEN_ENABLED
|M2V2_OPEN_FROZEN, &sq3);
if (!err) {
EXTRA_ASSERT(sq3 != NULL);
if (!(err = sqlx_transaction_begin(sq3, &repctx))) {
err = m2db_flush_container(sq3, cb, u0, truncated);
err = sqlx_transaction_end(repctx, err);
}
if (!err) {
m2b_add_modified_container(m2, sq3);
if (!(*truncated)) {
int rc = sqlx_exec(sq3->db, "VACUUM");
if (rc != SQLITE_OK)
err = SQLITE_GERROR(sq3->db, rc);
if (!err) {
if (!(err = sqlx_transaction_begin(sq3, &repctx))) {
if (!err)
sqlx_transaction_notify_huge_changes(repctx);
err = sqlx_transaction_end(repctx, err);
}
}
}
}
m2b_close(sq3);
}
return err;
}
GError *
meta2_backend_purge_container(struct meta2_backend_s *m2, struct oio_url_s *url,
gint64 *pmaxvers, m2_onbean_cb cb, gpointer u0)
{
GError *err;
struct sqlx_sqlite3_s *sq3 = NULL;
struct sqlx_repctx_s *repctx = NULL;
err = m2b_open(m2, url, M2V2_OPEN_MASTERONLY|M2V2_OPEN_ENABLED, &sq3);
if (!err) {
EXTRA_ASSERT(sq3 != NULL);
if (!(err = sqlx_transaction_begin(sq3, &repctx))) {
gint64 maxvers;
if (pmaxvers)
maxvers = *pmaxvers;
else
maxvers = _maxvers(sq3);
err = m2db_purge(sq3, maxvers, _retention_delay(sq3), NULL,
cb, u0);
err = sqlx_transaction_end(repctx, err);
}
if (!err)
m2b_add_modified_container(m2, sq3);
m2b_close(sq3);
}
return err;
}
/* Contents --------------------------------------------------------------- */
GError*
meta2_backend_list_aliases(struct meta2_backend_s *m2b, struct oio_url_s *url,
struct list_params_s *lp, GSList *headers,
m2_onbean_cb cb, gpointer u0, gchar ***out_properties)
{
GError *err = NULL;
struct sqlx_sqlite3_s *sq3 = NULL;
EXTRA_ASSERT(m2b != NULL);
EXTRA_ASSERT(url != NULL);
EXTRA_ASSERT(lp != NULL);
guint32 open_mode = lp->flag_local? M2V2_FLAG_LOCAL: 0;
err = m2b_open(m2b, url, _mode_readonly(open_mode), &sq3);
if (!err) {
err = m2db_list_aliases(sq3, lp, headers, cb, u0);
if (!err && out_properties)
*out_properties = sqlx_admin_get_keyvalues (sq3);
m2b_close(sq3);
}
return err;
}
GError*
meta2_backend_get_alias(struct meta2_backend_s *m2b,
struct oio_url_s *url, guint32 flags,
m2_onbean_cb cb, gpointer u0)
{
GError *err = NULL;
struct sqlx_sqlite3_s *sq3 = NULL;
EXTRA_ASSERT(m2b != NULL);
EXTRA_ASSERT(url != NULL);
err = m2b_open(m2b, url, _mode_readonly(flags), &sq3);
if (!err) {
err = m2db_get_alias(sq3, url, flags, cb, u0);
m2b_close(sq3);
}
return err;
}
/* TODO(jfs): maybe is there a way to keep this in a cache */
GError *
meta2_backend_notify_container_state(struct meta2_backend_s *m2b,
struct oio_url_s *url)
{
GError *err = NULL;
struct sqlx_sqlite3_s *sq3 = NULL;
if (!(err = m2b_open(m2b, url, M2V2_OPEN_MASTERONLY, &sq3))) {
m2b_add_modified_container(m2b, sq3);
m2b_close(sq3);
}
return err;
}
GError*
meta2_backend_refresh_container_size(struct meta2_backend_s *m2b,
struct oio_url_s *url, gboolean recompute)
{
GError *err = NULL;
struct sqlx_sqlite3_s *sq3 = NULL;
EXTRA_ASSERT(m2b != NULL);
EXTRA_ASSERT(url != NULL);
if (!(err = m2b_open(m2b, url, M2V2_OPEN_MASTERONLY, &sq3))) {
if (recompute) {
guint64 size = 0u;
gint64 count = 0;
m2db_get_container_size_and_obj_count(sq3->db, FALSE,
&size, &count);
m2db_set_size(sq3, size);
m2db_set_obj_count(sq3, count);
}
m2b_add_modified_container(m2b, sq3);
m2b_close(sq3);
}
return err;
}
GError *
meta2_backend_drain_content(struct meta2_backend_s *m2,
struct oio_url_s *url, m2_onbean_cb cb, gpointer u0)
{
GError *err = NULL;
struct sqlx_sqlite3_s *sq3 = NULL;
struct sqlx_repctx_s *repctx = NULL;
err = m2b_open(m2, url, M2V2_OPEN_MASTERONLY | M2V2_OPEN_ENABLED, &sq3);
if (!err) {
EXTRA_ASSERT(sq3 != NULL);
if (!(err = sqlx_transaction_begin(sq3, &repctx))) {
err = m2db_drain_content(sq3, url, cb, u0);
err = sqlx_transaction_end(repctx, err);
}
m2b_close(sq3);
}
return err;
}
GError*
meta2_backend_delete_alias(struct meta2_backend_s *m2b,
struct oio_url_s *url, m2_onbean_cb cb, gpointer u0)
{
GError *err = NULL;
struct sqlx_sqlite3_s *sq3 = NULL;
EXTRA_ASSERT(m2b != NULL);
EXTRA_ASSERT(url != NULL);
err = m2b_open(m2b, url, M2V2_OPEN_MASTERONLY|M2V2_OPEN_ENABLED, &sq3);
if (!err) {
struct sqlx_repctx_s *repctx = NULL;
const gint64 max_versions = _maxvers(sq3);
if (!(err = _transaction_begin(sq3, url, &repctx))) {
if (!(err = m2db_delete_alias(sq3, max_versions, url, cb, u0))) {
m2db_increment_version(sq3);
}
err = sqlx_transaction_end(repctx, err);
}
if (!err)
m2b_add_modified_container(m2b, sq3);
m2b_close(sq3);
}
return err;
}
GError*
meta2_backend_put_alias(struct meta2_backend_s *m2b, struct oio_url_s *url,
GSList *in, m2_onbean_cb cb_deleted, gpointer u0_deleted,
m2_onbean_cb cb_added, gpointer u0_added)
{
GError *err = NULL;
struct sqlx_sqlite3_s *sq3 = NULL;
struct sqlx_repctx_s *repctx = NULL;
EXTRA_ASSERT(m2b != NULL);
EXTRA_ASSERT(url != NULL);
if (!in)
return NEWERROR(CODE_BAD_REQUEST, "No bean");
err = m2b_open(m2b, url, M2V2_OPEN_MASTERONLY|M2V2_OPEN_ENABLED, &sq3);
if (!err) {
struct m2db_put_args_s args;
memset(&args, 0, sizeof(args));
args.sq3 = sq3;
args.url = url;
args.ns_max_versions = meta2_max_versions;
args.worm_mode = oio_ns_mode_worm && !oio_ext_is_admin();
if (!(err = _transaction_begin(sq3, url, &repctx))) {
if (!(err = m2db_put_alias(&args, in, cb_deleted, u0_deleted,
cb_added, u0_added)))
m2db_increment_version(sq3);
err = sqlx_transaction_end(repctx, err);
if (!err)
m2b_add_modified_container(m2b, sq3);
}
m2b_close(sq3);
}
return err;
}
GError*
meta2_backend_change_alias_policy(struct meta2_backend_s *m2b,
struct oio_url_s *url, GSList *in,
m2_onbean_cb cb_deleted, gpointer u0_deleted,
m2_onbean_cb cb_added, gpointer u0_added)
{
GError *err = NULL;