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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
|
/*
ctdb_call protocol code
Copyright (C) Andrew Tridgell 2006
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, see <http://www.gnu.org/licenses/>.
*/
/*
see http://wiki.samba.org/index.php/Samba_%26_Clustering for
protocol design and packet details
*/
#include "replace.h"
#include "system/network.h"
#include "system/filesys.h"
#include <talloc.h>
#include <tevent.h>
#include "lib/util/dlinklist.h"
#include "lib/util/debug.h"
#include "lib/util/samba_util.h"
#include "lib/util/sys_rw.h"
#include "lib/util/util_process.h"
#include "ctdb_private.h"
#include "ctdb_client.h"
#include "common/rb_tree.h"
#include "common/reqid.h"
#include "common/system.h"
#include "common/common.h"
#include "common/logging.h"
#include "common/hash_count.h"
struct ctdb_sticky_record {
struct ctdb_context *ctdb;
struct ctdb_db_context *ctdb_db;
TDB_CONTEXT *pindown;
};
/*
find the ctdb_db from a db index
*/
struct ctdb_db_context *find_ctdb_db(struct ctdb_context *ctdb, uint32_t id)
{
struct ctdb_db_context *ctdb_db;
for (ctdb_db=ctdb->db_list; ctdb_db; ctdb_db=ctdb_db->next) {
if (ctdb_db->db_id == id) {
break;
}
}
return ctdb_db;
}
/*
a varient of input packet that can be used in lock requeue
*/
static void ctdb_call_input_pkt(void *p, struct ctdb_req_header *hdr)
{
struct ctdb_context *ctdb = talloc_get_type(p, struct ctdb_context);
ctdb_input_pkt(ctdb, hdr);
}
/*
send an error reply
*/
static void ctdb_send_error(struct ctdb_context *ctdb,
struct ctdb_req_header *hdr, uint32_t status,
const char *fmt, ...) PRINTF_ATTRIBUTE(4,5);
static void ctdb_send_error(struct ctdb_context *ctdb,
struct ctdb_req_header *hdr, uint32_t status,
const char *fmt, ...)
{
va_list ap;
struct ctdb_reply_error_old *r;
char *msg;
int msglen, len;
if (ctdb->methods == NULL) {
DEBUG(DEBUG_INFO,(__location__ " Failed to send error. Transport is DOWN\n"));
return;
}
va_start(ap, fmt);
msg = talloc_vasprintf(ctdb, fmt, ap);
if (msg == NULL) {
ctdb_fatal(ctdb, "Unable to allocate error in ctdb_send_error\n");
}
va_end(ap);
msglen = strlen(msg)+1;
len = offsetof(struct ctdb_reply_error_old, msg);
r = ctdb_transport_allocate(ctdb, msg, CTDB_REPLY_ERROR, len + msglen,
struct ctdb_reply_error_old);
CTDB_NO_MEMORY_FATAL(ctdb, r);
r->hdr.destnode = hdr->srcnode;
r->hdr.reqid = hdr->reqid;
r->status = status;
r->msglen = msglen;
memcpy(&r->msg[0], msg, msglen);
ctdb_queue_packet(ctdb, &r->hdr);
talloc_free(msg);
}
/**
* send a redirect reply
*
* The logic behind this function is this:
*
* A client wants to grab a record and sends a CTDB_REQ_CALL packet
* to its local ctdb (ctdb_request_call). If the node is not itself
* the record's DMASTER, it first redirects the packet to the
* record's LMASTER. The LMASTER then redirects the call packet to
* the current DMASTER. Note that this works because of this: When
* a record is migrated off a node, then the new DMASTER is stored
* in the record's copy on the former DMASTER.
*/
static void ctdb_call_send_redirect(struct ctdb_context *ctdb,
struct ctdb_db_context *ctdb_db,
TDB_DATA key,
struct ctdb_req_call_old *c,
struct ctdb_ltdb_header *header)
{
uint32_t lmaster = ctdb_lmaster(ctdb, &key);
c->hdr.destnode = lmaster;
if (ctdb->pnn == lmaster) {
c->hdr.destnode = header->dmaster;
}
c->hopcount++;
if (c->hopcount%100 > 95) {
DEBUG(DEBUG_WARNING,("High hopcount %d dbid:%s "
"key:0x%08x reqid=%08x pnn:%d src:%d lmaster:%d "
"header->dmaster:%d dst:%d\n",
c->hopcount, ctdb_db->db_name, ctdb_hash(&key),
c->hdr.reqid, ctdb->pnn, c->hdr.srcnode, lmaster,
header->dmaster, c->hdr.destnode));
}
ctdb_queue_packet(ctdb, &c->hdr);
}
/*
send a dmaster reply
caller must have the chainlock before calling this routine. Caller must be
the lmaster
*/
static void ctdb_send_dmaster_reply(struct ctdb_db_context *ctdb_db,
struct ctdb_ltdb_header *header,
TDB_DATA key, TDB_DATA data,
uint32_t new_dmaster,
uint32_t reqid)
{
struct ctdb_context *ctdb = ctdb_db->ctdb;
struct ctdb_reply_dmaster_old *r;
int ret, len;
TALLOC_CTX *tmp_ctx;
if (ctdb->pnn != ctdb_lmaster(ctdb, &key)) {
DEBUG(DEBUG_ALERT,(__location__ " Caller is not lmaster!\n"));
return;
}
header->dmaster = new_dmaster;
ret = ctdb_ltdb_store(ctdb_db, key, header, data);
if (ret != 0) {
ctdb_fatal(ctdb, "ctdb_send_dmaster_reply unable to update dmaster");
return;
}
if (ctdb->methods == NULL) {
ctdb_fatal(ctdb, "ctdb_send_dmaster_reply cant update dmaster since transport is down");
return;
}
/* put the packet on a temporary context, allowing us to safely free
it below even if ctdb_reply_dmaster() has freed it already */
tmp_ctx = talloc_new(ctdb);
/* send the CTDB_REPLY_DMASTER */
len = offsetof(struct ctdb_reply_dmaster_old, data) + key.dsize + data.dsize + sizeof(uint32_t);
r = ctdb_transport_allocate(ctdb, tmp_ctx, CTDB_REPLY_DMASTER, len,
struct ctdb_reply_dmaster_old);
CTDB_NO_MEMORY_FATAL(ctdb, r);
r->hdr.destnode = new_dmaster;
r->hdr.reqid = reqid;
r->hdr.generation = ctdb_db->generation;
r->rsn = header->rsn;
r->keylen = key.dsize;
r->datalen = data.dsize;
r->db_id = ctdb_db->db_id;
memcpy(&r->data[0], key.dptr, key.dsize);
memcpy(&r->data[key.dsize], data.dptr, data.dsize);
memcpy(&r->data[key.dsize+data.dsize], &header->flags, sizeof(uint32_t));
ctdb_queue_packet(ctdb, &r->hdr);
talloc_free(tmp_ctx);
}
/*
send a dmaster request (give another node the dmaster for a record)
This is always sent to the lmaster, which ensures that the lmaster
always knows who the dmaster is. The lmaster will then send a
CTDB_REPLY_DMASTER to the new dmaster
*/
static void ctdb_call_send_dmaster(struct ctdb_db_context *ctdb_db,
struct ctdb_req_call_old *c,
struct ctdb_ltdb_header *header,
TDB_DATA *key, TDB_DATA *data)
{
struct ctdb_req_dmaster_old *r;
struct ctdb_context *ctdb = ctdb_db->ctdb;
int len;
uint32_t lmaster = ctdb_lmaster(ctdb, key);
if (ctdb->methods == NULL) {
ctdb_fatal(ctdb, "Failed ctdb_call_send_dmaster since transport is down");
return;
}
if (data->dsize != 0) {
header->flags |= CTDB_REC_FLAG_MIGRATED_WITH_DATA;
}
if (lmaster == ctdb->pnn) {
ctdb_send_dmaster_reply(ctdb_db, header, *key, *data,
c->hdr.srcnode, c->hdr.reqid);
return;
}
len = offsetof(struct ctdb_req_dmaster_old, data) + key->dsize + data->dsize
+ sizeof(uint32_t);
r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REQ_DMASTER, len,
struct ctdb_req_dmaster_old);
CTDB_NO_MEMORY_FATAL(ctdb, r);
r->hdr.destnode = lmaster;
r->hdr.reqid = c->hdr.reqid;
r->hdr.generation = ctdb_db->generation;
r->db_id = c->db_id;
r->rsn = header->rsn;
r->dmaster = c->hdr.srcnode;
r->keylen = key->dsize;
r->datalen = data->dsize;
memcpy(&r->data[0], key->dptr, key->dsize);
memcpy(&r->data[key->dsize], data->dptr, data->dsize);
memcpy(&r->data[key->dsize + data->dsize], &header->flags, sizeof(uint32_t));
header->dmaster = c->hdr.srcnode;
if (ctdb_ltdb_store(ctdb_db, *key, header, *data) != 0) {
ctdb_fatal(ctdb, "Failed to store record in ctdb_call_send_dmaster");
}
ctdb_queue_packet(ctdb, &r->hdr);
talloc_free(r);
}
static void ctdb_sticky_pindown_timeout(struct tevent_context *ev,
struct tevent_timer *te,
struct timeval t, void *private_data)
{
struct ctdb_sticky_record *sr = talloc_get_type(private_data,
struct ctdb_sticky_record);
DEBUG(DEBUG_ERR,("Pindown timeout db:%s unstick record\n", sr->ctdb_db->db_name));
if (sr->pindown != NULL) {
talloc_free(sr->pindown);
sr->pindown = NULL;
}
}
static int
ctdb_set_sticky_pindown(struct ctdb_context *ctdb, struct ctdb_db_context *ctdb_db, TDB_DATA key)
{
TALLOC_CTX *tmp_ctx = talloc_new(NULL);
uint32_t *k;
struct ctdb_sticky_record *sr;
k = ctdb_key_to_idkey(tmp_ctx, key);
if (k == NULL) {
DEBUG(DEBUG_ERR,("Failed to allocate key for sticky record\n"));
talloc_free(tmp_ctx);
return -1;
}
sr = trbt_lookuparray32(ctdb_db->sticky_records, k[0], &k[0]);
if (sr == NULL) {
talloc_free(tmp_ctx);
return 0;
}
talloc_free(tmp_ctx);
if (sr->pindown == NULL) {
DEBUG(DEBUG_ERR,("Pinning down record in %s for %d ms\n", ctdb_db->db_name, ctdb->tunable.sticky_pindown));
sr->pindown = talloc_new(sr);
if (sr->pindown == NULL) {
DEBUG(DEBUG_ERR,("Failed to allocate pindown context for sticky record\n"));
return -1;
}
tevent_add_timer(ctdb->ev, sr->pindown,
timeval_current_ofs(ctdb->tunable.sticky_pindown / 1000,
(ctdb->tunable.sticky_pindown * 1000) % 1000000),
ctdb_sticky_pindown_timeout, sr);
}
return 0;
}
/*
called when a CTDB_REPLY_DMASTER packet comes in, or when the lmaster
gets a CTDB_REQUEST_DMASTER for itself. We become the dmaster.
must be called with the chainlock held. This function releases the chainlock
*/
static void ctdb_become_dmaster(struct ctdb_db_context *ctdb_db,
struct ctdb_req_header *hdr,
TDB_DATA key, TDB_DATA data,
uint64_t rsn, uint32_t record_flags)
{
struct ctdb_call_state *state;
struct ctdb_context *ctdb = ctdb_db->ctdb;
struct ctdb_ltdb_header header;
int ret;
DEBUG(DEBUG_DEBUG,("pnn %u dmaster response %08x\n", ctdb->pnn, ctdb_hash(&key)));
ZERO_STRUCT(header);
header.rsn = rsn;
header.dmaster = ctdb->pnn;
header.flags = record_flags;
state = reqid_find(ctdb->idr, hdr->reqid, struct ctdb_call_state);
if (state) {
if (state->call->flags & CTDB_CALL_FLAG_VACUUM_MIGRATION) {
/*
* We temporarily add the VACUUM_MIGRATED flag to
* the record flags, so that ctdb_ltdb_store can
* decide whether the record should be stored or
* deleted.
*/
header.flags |= CTDB_REC_FLAG_VACUUM_MIGRATED;
}
}
if (ctdb_ltdb_store(ctdb_db, key, &header, data) != 0) {
ctdb_fatal(ctdb, "ctdb_reply_dmaster store failed\n");
ret = ctdb_ltdb_unlock(ctdb_db, key);
if (ret != 0) {
DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
}
return;
}
/* we just became DMASTER and this database is "sticky",
see if the record is flagged as "hot" and set up a pin-down
context to stop migrations for a little while if so
*/
if (ctdb_db_sticky(ctdb_db)) {
ctdb_set_sticky_pindown(ctdb, ctdb_db, key);
}
if (state == NULL) {
DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_become_dmaster from node %u\n",
ctdb->pnn, hdr->reqid, hdr->srcnode));
ret = ctdb_ltdb_unlock(ctdb_db, key);
if (ret != 0) {
DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
}
return;
}
if (key.dsize != state->call->key.dsize || memcmp(key.dptr, state->call->key.dptr, key.dsize)) {
DEBUG(DEBUG_ERR, ("Got bogus DMASTER packet reqid:%u from node %u. Key does not match key held in matching idr.\n", hdr->reqid, hdr->srcnode));
ret = ctdb_ltdb_unlock(ctdb_db, key);
if (ret != 0) {
DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
}
return;
}
if (hdr->reqid != state->reqid) {
/* we found a record but it was the wrong one */
DEBUG(DEBUG_ERR, ("Dropped orphan in ctdb_become_dmaster with reqid:%u\n from node %u", hdr->reqid, hdr->srcnode));
ret = ctdb_ltdb_unlock(ctdb_db, key);
if (ret != 0) {
DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
}
return;
}
(void) hash_count_increment(ctdb_db->migratedb, key);
ctdb_call_local(ctdb_db, state->call, &header, state, &data, true);
ret = ctdb_ltdb_unlock(ctdb_db, state->call->key);
if (ret != 0) {
DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
}
state->state = CTDB_CALL_DONE;
if (state->async.fn) {
state->async.fn(state);
}
}
struct dmaster_defer_call {
struct dmaster_defer_call *next, *prev;
struct ctdb_context *ctdb;
struct ctdb_req_header *hdr;
};
struct dmaster_defer_queue {
struct ctdb_db_context *ctdb_db;
uint32_t generation;
struct dmaster_defer_call *deferred_calls;
};
static void dmaster_defer_reprocess(struct tevent_context *ev,
struct tevent_timer *te,
struct timeval t,
void *private_data)
{
struct dmaster_defer_call *call = talloc_get_type(
private_data, struct dmaster_defer_call);
ctdb_input_pkt(call->ctdb, call->hdr);
talloc_free(call);
}
static int dmaster_defer_queue_destructor(struct dmaster_defer_queue *ddq)
{
/* Ignore requests, if database recovery happens in-between. */
if (ddq->generation != ddq->ctdb_db->generation) {
return 0;
}
while (ddq->deferred_calls != NULL) {
struct dmaster_defer_call *call = ddq->deferred_calls;
DLIST_REMOVE(ddq->deferred_calls, call);
talloc_steal(call->ctdb, call);
tevent_add_timer(call->ctdb->ev, call, timeval_zero(),
dmaster_defer_reprocess, call);
}
return 0;
}
static void *insert_ddq_callback(void *parm, void *data)
{
if (data) {
talloc_free(data);
}
return parm;
}
/**
* This function is used to reigster a key in database that needs to be updated.
* Any requests for that key should get deferred till this is completed.
*/
static int dmaster_defer_setup(struct ctdb_db_context *ctdb_db,
struct ctdb_req_header *hdr,
TDB_DATA key)
{
uint32_t *k;
struct dmaster_defer_queue *ddq;
k = ctdb_key_to_idkey(hdr, key);
if (k == NULL) {
DEBUG(DEBUG_ERR, ("Failed to allocate key for dmaster defer setup\n"));
return -1;
}
/* Already exists */
ddq = trbt_lookuparray32(ctdb_db->defer_dmaster, k[0], k);
if (ddq != NULL) {
if (ddq->generation == ctdb_db->generation) {
talloc_free(k);
return 0;
}
/* Recovery ocurred - get rid of old queue. All the deferred
* requests will be resent anyway from ctdb_call_resend_db.
*/
talloc_free(ddq);
}
ddq = talloc(hdr, struct dmaster_defer_queue);
if (ddq == NULL) {
DEBUG(DEBUG_ERR, ("Failed to allocate dmaster defer queue\n"));
talloc_free(k);
return -1;
}
ddq->ctdb_db = ctdb_db;
ddq->generation = hdr->generation;
ddq->deferred_calls = NULL;
trbt_insertarray32_callback(ctdb_db->defer_dmaster, k[0], k,
insert_ddq_callback, ddq);
talloc_set_destructor(ddq, dmaster_defer_queue_destructor);
talloc_free(k);
return 0;
}
static int dmaster_defer_add(struct ctdb_db_context *ctdb_db,
struct ctdb_req_header *hdr,
TDB_DATA key)
{
struct dmaster_defer_queue *ddq;
struct dmaster_defer_call *call;
uint32_t *k;
k = ctdb_key_to_idkey(hdr, key);
if (k == NULL) {
DEBUG(DEBUG_ERR, ("Failed to allocate key for dmaster defer add\n"));
return -1;
}
ddq = trbt_lookuparray32(ctdb_db->defer_dmaster, k[0], k);
if (ddq == NULL) {
talloc_free(k);
return -1;
}
talloc_free(k);
if (ddq->generation != hdr->generation) {
talloc_set_destructor(ddq, NULL);
talloc_free(ddq);
return -1;
}
call = talloc(ddq, struct dmaster_defer_call);
if (call == NULL) {
DEBUG(DEBUG_ERR, ("Failed to allocate dmaster defer call\n"));
return -1;
}
call->ctdb = ctdb_db->ctdb;
call->hdr = talloc_steal(call, hdr);
DLIST_ADD_END(ddq->deferred_calls, call);
return 0;
}
/*
called when a CTDB_REQ_DMASTER packet comes in
this comes into the lmaster for a record when the current dmaster
wants to give up the dmaster role and give it to someone else
*/
void ctdb_request_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
{
struct ctdb_req_dmaster_old *c = (struct ctdb_req_dmaster_old *)hdr;
TDB_DATA key, data, data2;
struct ctdb_ltdb_header header;
struct ctdb_db_context *ctdb_db;
uint32_t record_flags = 0;
size_t len;
int ret;
key.dptr = c->data;
key.dsize = c->keylen;
data.dptr = c->data + c->keylen;
data.dsize = c->datalen;
len = offsetof(struct ctdb_req_dmaster_old, data) + key.dsize + data.dsize
+ sizeof(uint32_t);
if (len <= c->hdr.length) {
memcpy(&record_flags, &c->data[c->keylen + c->datalen],
sizeof(record_flags));
}
ctdb_db = find_ctdb_db(ctdb, c->db_id);
if (!ctdb_db) {
ctdb_send_error(ctdb, hdr, -1,
"Unknown database in request. db_id==0x%08x",
c->db_id);
return;
}
dmaster_defer_setup(ctdb_db, hdr, key);
/* fetch the current record */
ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, key, &header, hdr, &data2,
ctdb_call_input_pkt, ctdb, false);
if (ret == -1) {
ctdb_fatal(ctdb, "ctdb_req_dmaster failed to fetch record");
return;
}
if (ret == -2) {
DEBUG(DEBUG_INFO,(__location__ " deferring ctdb_request_dmaster\n"));
return;
}
if (ctdb_lmaster(ctdb, &key) != ctdb->pnn) {
DEBUG(DEBUG_ERR, ("dmaster request to non-lmaster "
"db=%s lmaster=%u gen=%u curgen=%u\n",
ctdb_db->db_name, ctdb_lmaster(ctdb, &key),
hdr->generation, ctdb_db->generation));
ctdb_fatal(ctdb, "ctdb_req_dmaster to non-lmaster");
}
DEBUG(DEBUG_DEBUG,("pnn %u dmaster request on %08x for %u from %u\n",
ctdb->pnn, ctdb_hash(&key), c->dmaster, c->hdr.srcnode));
/* its a protocol error if the sending node is not the current dmaster */
if (header.dmaster != hdr->srcnode) {
DEBUG(DEBUG_ALERT,("pnn %u dmaster request for new-dmaster %u from non-master %u real-dmaster=%u key %08x dbid 0x%08x gen=%u curgen=%u c->rsn=%llu header.rsn=%llu reqid=%u keyval=0x%08x\n",
ctdb->pnn, c->dmaster, hdr->srcnode, header.dmaster, ctdb_hash(&key),
ctdb_db->db_id, hdr->generation, ctdb->vnn_map->generation,
(unsigned long long)c->rsn, (unsigned long long)header.rsn, c->hdr.reqid,
(key.dsize >= 4)?(*(uint32_t *)key.dptr):0));
if (header.rsn != 0 || header.dmaster != ctdb->pnn) {
DEBUG(DEBUG_ERR,("ctdb_req_dmaster from non-master. Force a recovery.\n"));
ctdb->recovery_mode = CTDB_RECOVERY_ACTIVE;
ctdb_ltdb_unlock(ctdb_db, key);
return;
}
}
if (header.rsn > c->rsn) {
DEBUG(DEBUG_ALERT,("pnn %u dmaster request with older RSN new-dmaster %u from %u real-dmaster=%u key %08x dbid 0x%08x gen=%u curgen=%u c->rsn=%llu header.rsn=%llu reqid=%u\n",
ctdb->pnn, c->dmaster, hdr->srcnode, header.dmaster, ctdb_hash(&key),
ctdb_db->db_id, hdr->generation, ctdb->vnn_map->generation,
(unsigned long long)c->rsn, (unsigned long long)header.rsn, c->hdr.reqid));
}
/* use the rsn from the sending node */
header.rsn = c->rsn;
/* store the record flags from the sending node */
header.flags = record_flags;
/* check if the new dmaster is the lmaster, in which case we
skip the dmaster reply */
if (c->dmaster == ctdb->pnn) {
ctdb_become_dmaster(ctdb_db, hdr, key, data, c->rsn, record_flags);
} else {
ctdb_send_dmaster_reply(ctdb_db, &header, key, data, c->dmaster, hdr->reqid);
ret = ctdb_ltdb_unlock(ctdb_db, key);
if (ret != 0) {
DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
}
}
}
static void ctdb_sticky_record_timeout(struct tevent_context *ev,
struct tevent_timer *te,
struct timeval t, void *private_data)
{
struct ctdb_sticky_record *sr = talloc_get_type(private_data,
struct ctdb_sticky_record);
talloc_free(sr);
}
static void *ctdb_make_sticky_record_callback(void *parm, void *data)
{
if (data) {
DEBUG(DEBUG_ERR,("Already have sticky record registered. Free old %p and create new %p\n", data, parm));
talloc_free(data);
}
return parm;
}
static int
ctdb_make_record_sticky(struct ctdb_context *ctdb, struct ctdb_db_context *ctdb_db, TDB_DATA key)
{
TALLOC_CTX *tmp_ctx = talloc_new(NULL);
uint32_t *k;
struct ctdb_sticky_record *sr;
k = ctdb_key_to_idkey(tmp_ctx, key);
if (k == NULL) {
DEBUG(DEBUG_ERR,("Failed to allocate key for sticky record\n"));
talloc_free(tmp_ctx);
return -1;
}
sr = trbt_lookuparray32(ctdb_db->sticky_records, k[0], &k[0]);
if (sr != NULL) {
talloc_free(tmp_ctx);
return 0;
}
sr = talloc(ctdb_db->sticky_records, struct ctdb_sticky_record);
if (sr == NULL) {
talloc_free(tmp_ctx);
DEBUG(DEBUG_ERR,("Failed to allocate sticky record structure\n"));
return -1;
}
sr->ctdb = ctdb;
sr->ctdb_db = ctdb_db;
sr->pindown = NULL;
DEBUG(DEBUG_ERR,("Make record sticky for %d seconds in db %s key:0x%08x.\n",
ctdb->tunable.sticky_duration,
ctdb_db->db_name, ctdb_hash(&key)));
trbt_insertarray32_callback(ctdb_db->sticky_records, k[0], &k[0], ctdb_make_sticky_record_callback, sr);
tevent_add_timer(ctdb->ev, sr,
timeval_current_ofs(ctdb->tunable.sticky_duration, 0),
ctdb_sticky_record_timeout, sr);
talloc_free(tmp_ctx);
return 0;
}
struct pinned_down_requeue_handle {
struct ctdb_context *ctdb;
struct ctdb_req_header *hdr;
};
struct pinned_down_deferred_call {
struct ctdb_context *ctdb;
struct ctdb_req_header *hdr;
};
static void pinned_down_requeue(struct tevent_context *ev,
struct tevent_timer *te,
struct timeval t, void *private_data)
{
struct pinned_down_requeue_handle *handle = talloc_get_type(private_data, struct pinned_down_requeue_handle);
struct ctdb_context *ctdb = handle->ctdb;
talloc_steal(ctdb, handle->hdr);
ctdb_call_input_pkt(ctdb, handle->hdr);
talloc_free(handle);
}
static int pinned_down_destructor(struct pinned_down_deferred_call *pinned_down)
{
struct ctdb_context *ctdb = pinned_down->ctdb;
struct pinned_down_requeue_handle *handle = talloc(ctdb, struct pinned_down_requeue_handle);
handle->ctdb = pinned_down->ctdb;
handle->hdr = pinned_down->hdr;
talloc_steal(handle, handle->hdr);
tevent_add_timer(ctdb->ev, handle, timeval_zero(),
pinned_down_requeue, handle);
return 0;
}
static int
ctdb_defer_pinned_down_request(struct ctdb_context *ctdb, struct ctdb_db_context *ctdb_db, TDB_DATA key, struct ctdb_req_header *hdr)
{
TALLOC_CTX *tmp_ctx = talloc_new(NULL);
uint32_t *k;
struct ctdb_sticky_record *sr;
struct pinned_down_deferred_call *pinned_down;
k = ctdb_key_to_idkey(tmp_ctx, key);
if (k == NULL) {
DEBUG(DEBUG_ERR,("Failed to allocate key for sticky record\n"));
talloc_free(tmp_ctx);
return -1;
}
sr = trbt_lookuparray32(ctdb_db->sticky_records, k[0], &k[0]);
if (sr == NULL) {
talloc_free(tmp_ctx);
return -1;
}
talloc_free(tmp_ctx);
if (sr->pindown == NULL) {
return -1;
}
pinned_down = talloc(sr->pindown, struct pinned_down_deferred_call);
if (pinned_down == NULL) {
DEBUG(DEBUG_ERR,("Failed to allocate structure for deferred pinned down request\n"));
return -1;
}
pinned_down->ctdb = ctdb;
pinned_down->hdr = hdr;
talloc_set_destructor(pinned_down, pinned_down_destructor);
talloc_steal(pinned_down, hdr);
return 0;
}
static void
ctdb_update_db_stat_hot_keys(struct ctdb_db_context *ctdb_db, TDB_DATA key,
int count)
{
int i, id;
char *keystr;
/* smallest value is always at index 0 */
if (count <= ctdb_db->statistics.hot_keys[0].count) {
return;
}
/* see if we already know this key */
for (i = 0; i < MAX_HOT_KEYS; i++) {
if (key.dsize != ctdb_db->statistics.hot_keys[i].key.dsize) {
continue;
}
if (memcmp(key.dptr, ctdb_db->statistics.hot_keys[i].key.dptr, key.dsize)) {
continue;
}
/* found an entry for this key */
if (count <= ctdb_db->statistics.hot_keys[i].count) {
return;
}
ctdb_db->statistics.hot_keys[i].count = count;
goto sort_keys;
}
if (ctdb_db->statistics.num_hot_keys < MAX_HOT_KEYS) {
id = ctdb_db->statistics.num_hot_keys;
ctdb_db->statistics.num_hot_keys++;
} else {
id = 0;
}
if (ctdb_db->statistics.hot_keys[id].key.dptr != NULL) {
talloc_free(ctdb_db->statistics.hot_keys[id].key.dptr);
}
ctdb_db->statistics.hot_keys[id].key.dsize = key.dsize;
ctdb_db->statistics.hot_keys[id].key.dptr = talloc_memdup(ctdb_db, key.dptr, key.dsize);
ctdb_db->statistics.hot_keys[id].count = count;
keystr = hex_encode_talloc(ctdb_db,
(unsigned char *)key.dptr, key.dsize);
DEBUG(DEBUG_NOTICE,("Updated hot key database=%s key=%s id=%d "
"count=%d\n", ctdb_db->db_name,
keystr ? keystr : "" , id, count));
talloc_free(keystr);
sort_keys:
for (i = 1; i < MAX_HOT_KEYS; i++) {
if (ctdb_db->statistics.hot_keys[i].count == 0) {
continue;
}
if (ctdb_db->statistics.hot_keys[i].count < ctdb_db->statistics.hot_keys[0].count) {
count = ctdb_db->statistics.hot_keys[i].count;
ctdb_db->statistics.hot_keys[i].count = ctdb_db->statistics.hot_keys[0].count;
ctdb_db->statistics.hot_keys[0].count = count;
key = ctdb_db->statistics.hot_keys[i].key;
ctdb_db->statistics.hot_keys[i].key = ctdb_db->statistics.hot_keys[0].key;
ctdb_db->statistics.hot_keys[0].key = key;
}
}
}
/*
called when a CTDB_REQ_CALL packet comes in
*/
void ctdb_request_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
{
struct ctdb_req_call_old *c = (struct ctdb_req_call_old *)hdr;
TDB_DATA data;
struct ctdb_reply_call_old *r;
int ret, len;
struct ctdb_ltdb_header header;
struct ctdb_call *call;
struct ctdb_db_context *ctdb_db;
int tmp_count, bucket;
if (ctdb->methods == NULL) {
DEBUG(DEBUG_INFO,(__location__ " Failed ctdb_request_call. Transport is DOWN\n"));
return;
}
ctdb_db = find_ctdb_db(ctdb, c->db_id);
if (!ctdb_db) {
ctdb_send_error(ctdb, hdr, -1,
"Unknown database in request. db_id==0x%08x",
c->db_id);
return;
}
call = talloc(hdr, struct ctdb_call);
CTDB_NO_MEMORY_FATAL(ctdb, call);
call->call_id = c->callid;
call->key.dptr = c->data;
call->key.dsize = c->keylen;
call->call_data.dptr = c->data + c->keylen;
call->call_data.dsize = c->calldatalen;
call->reply_data.dptr = NULL;
call->reply_data.dsize = 0;
/* If this record is pinned down we should defer the
request until the pindown times out
*/
if (ctdb_db_sticky(ctdb_db)) {
if (ctdb_defer_pinned_down_request(ctdb, ctdb_db, call->key, hdr) == 0) {
DEBUG(DEBUG_WARNING,
("Defer request for pinned down record in %s\n", ctdb_db->db_name));
talloc_free(call);
return;
}
}
if (dmaster_defer_add(ctdb_db, hdr, call->key) == 0) {
talloc_free(call);
return;
}
/* determine if we are the dmaster for this key. This also
fetches the record data (if any), thus avoiding a 2nd fetch of the data
if the call will be answered locally */
ret = ctdb_ltdb_lock_fetch_requeue(ctdb_db, call->key, &header, hdr, &data,
ctdb_call_input_pkt, ctdb, false);
if (ret == -1) {
ctdb_send_error(ctdb, hdr, ret, "ltdb fetch failed in ctdb_request_call");
talloc_free(call);
return;
}
if (ret == -2) {
DEBUG(DEBUG_INFO,(__location__ " deferred ctdb_request_call\n"));
talloc_free(call);
return;
}
/* Dont do READONLY if we don't have a tracking database */
if ((c->flags & CTDB_WANT_READONLY) && !ctdb_db_readonly(ctdb_db)) {
c->flags &= ~CTDB_WANT_READONLY;
}
if (header.flags & CTDB_REC_RO_REVOKE_COMPLETE) {
header.flags &= ~CTDB_REC_RO_FLAGS;
CTDB_INCREMENT_STAT(ctdb, total_ro_revokes);
CTDB_INCREMENT_DB_STAT(ctdb_db, db_ro_revokes);
if (ctdb_ltdb_store(ctdb_db, call->key, &header, data) != 0) {
ctdb_fatal(ctdb, "Failed to write header with cleared REVOKE flag");
}
/* and clear out the tracking data */
if (tdb_delete(ctdb_db->rottdb, call->key) != 0) {
DEBUG(DEBUG_ERR,(__location__ " Failed to clear out trackingdb record\n"));
}
}
/* if we are revoking, we must defer all other calls until the revoke
* had completed.
*/
if (header.flags & CTDB_REC_RO_REVOKING_READONLY) {
talloc_free(data.dptr);
ret = ctdb_ltdb_unlock(ctdb_db, call->key);
if (ctdb_add_revoke_deferred_call(ctdb, ctdb_db, call->key, hdr, ctdb_call_input_pkt, ctdb) != 0) {
ctdb_fatal(ctdb, "Failed to add deferred call for revoke child");
}
talloc_free(call);
return;
}
/*
* If we are not the dmaster and are not hosting any delegations,
* then we redirect the request to the node than can answer it
* (the lmaster or the dmaster).
*/
if ((header.dmaster != ctdb->pnn)
&& (!(header.flags & CTDB_REC_RO_HAVE_DELEGATIONS)) ) {
talloc_free(data.dptr);
ctdb_call_send_redirect(ctdb, ctdb_db, call->key, c, &header);
ret = ctdb_ltdb_unlock(ctdb_db, call->key);
if (ret != 0) {
DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
}
talloc_free(call);
return;
}
if ( (!(c->flags & CTDB_WANT_READONLY))
&& (header.flags & (CTDB_REC_RO_HAVE_DELEGATIONS|CTDB_REC_RO_HAVE_READONLY)) ) {
header.flags |= CTDB_REC_RO_REVOKING_READONLY;
if (ctdb_ltdb_store(ctdb_db, call->key, &header, data) != 0) {
ctdb_fatal(ctdb, "Failed to store record with HAVE_DELEGATIONS set");
}
ret = ctdb_ltdb_unlock(ctdb_db, call->key);
if (ctdb_start_revoke_ro_record(ctdb, ctdb_db, call->key, &header, data) != 0) {
ctdb_fatal(ctdb, "Failed to start record revoke");
}
talloc_free(data.dptr);
if (ctdb_add_revoke_deferred_call(ctdb, ctdb_db, call->key, hdr, ctdb_call_input_pkt, ctdb) != 0) {
ctdb_fatal(ctdb, "Failed to add deferred call for revoke child");
}
talloc_free(call);
return;
}
/* If this is the first request for delegation. bump rsn and set
* the delegations flag
*/
if ((c->flags & CTDB_WANT_READONLY)
&& (c->callid == CTDB_FETCH_WITH_HEADER_FUNC)
&& (!(header.flags & CTDB_REC_RO_HAVE_DELEGATIONS))) {
header.rsn += 3;
header.flags |= CTDB_REC_RO_HAVE_DELEGATIONS;
if (ctdb_ltdb_store(ctdb_db, call->key, &header, data) != 0) {
ctdb_fatal(ctdb, "Failed to store record with HAVE_DELEGATIONS set");
}
}
if ((c->flags & CTDB_WANT_READONLY)
&& (call->call_id == CTDB_FETCH_WITH_HEADER_FUNC)) {
TDB_DATA tdata;
tdata = tdb_fetch(ctdb_db->rottdb, call->key);
if (ctdb_trackingdb_add_pnn(ctdb, &tdata, c->hdr.srcnode) != 0) {
ctdb_fatal(ctdb, "Failed to add node to trackingdb");
}
if (tdb_store(ctdb_db->rottdb, call->key, tdata, TDB_REPLACE) != 0) {
ctdb_fatal(ctdb, "Failed to store trackingdb data");
}
free(tdata.dptr);
ret = ctdb_ltdb_unlock(ctdb_db, call->key);
if (ret != 0) {
DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
}
len = offsetof(struct ctdb_reply_call_old, data) + data.dsize + sizeof(struct ctdb_ltdb_header);
r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CALL, len,
struct ctdb_reply_call_old);
CTDB_NO_MEMORY_FATAL(ctdb, r);
r->hdr.destnode = c->hdr.srcnode;
r->hdr.reqid = c->hdr.reqid;
r->hdr.generation = ctdb_db->generation;
r->status = 0;
r->datalen = data.dsize + sizeof(struct ctdb_ltdb_header);
header.rsn -= 2;
header.flags |= CTDB_REC_RO_HAVE_READONLY;
header.flags &= ~CTDB_REC_RO_HAVE_DELEGATIONS;
memcpy(&r->data[0], &header, sizeof(struct ctdb_ltdb_header));
if (data.dsize) {
memcpy(&r->data[sizeof(struct ctdb_ltdb_header)], data.dptr, data.dsize);
}
ctdb_queue_packet(ctdb, &r->hdr);
CTDB_INCREMENT_STAT(ctdb, total_ro_delegations);
CTDB_INCREMENT_DB_STAT(ctdb_db, db_ro_delegations);
talloc_free(r);
talloc_free(call);
return;
}
CTDB_UPDATE_STAT(ctdb, max_hop_count, c->hopcount);
tmp_count = c->hopcount;
bucket = 0;
while (tmp_count) {
tmp_count >>= 1;
bucket++;
}
if (bucket >= MAX_COUNT_BUCKETS) {
bucket = MAX_COUNT_BUCKETS - 1;
}
CTDB_INCREMENT_STAT(ctdb, hop_count_bucket[bucket]);
CTDB_INCREMENT_DB_STAT(ctdb_db, hop_count_bucket[bucket]);
/* If this database supports sticky records, then check if the
hopcount is big. If it is it means the record is hot and we
should make it sticky.
*/
if (ctdb_db_sticky(ctdb_db) &&
c->hopcount >= ctdb->tunable.hopcount_make_sticky) {
ctdb_make_record_sticky(ctdb, ctdb_db, call->key);
}
/* Try if possible to migrate the record off to the caller node.
* From the clients perspective a fetch of the data is just as
* expensive as a migration.
*/
if (c->hdr.srcnode != ctdb->pnn) {
if (ctdb_db->persistent_state) {
DEBUG(DEBUG_INFO, (__location__ " refusing migration"
" of key %s while transaction is active\n",
(char *)call->key.dptr));
} else {
DEBUG(DEBUG_DEBUG,("pnn %u starting migration of %08x to %u\n",
ctdb->pnn, ctdb_hash(&(call->key)), c->hdr.srcnode));
ctdb_call_send_dmaster(ctdb_db, c, &header, &(call->key), &data);
talloc_free(data.dptr);
ret = ctdb_ltdb_unlock(ctdb_db, call->key);
if (ret != 0) {
DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
}
}
talloc_free(call);
return;
}
ret = ctdb_call_local(ctdb_db, call, &header, hdr, &data, true);
if (ret != 0) {
DEBUG(DEBUG_ERR,(__location__ " ctdb_call_local failed\n"));
call->status = -1;
}
ret = ctdb_ltdb_unlock(ctdb_db, call->key);
if (ret != 0) {
DEBUG(DEBUG_ERR,(__location__ " ctdb_ltdb_unlock() failed with error %d\n", ret));
}
len = offsetof(struct ctdb_reply_call_old, data) + call->reply_data.dsize;
r = ctdb_transport_allocate(ctdb, ctdb, CTDB_REPLY_CALL, len,
struct ctdb_reply_call_old);
CTDB_NO_MEMORY_FATAL(ctdb, r);
r->hdr.destnode = hdr->srcnode;
r->hdr.reqid = hdr->reqid;
r->hdr.generation = ctdb_db->generation;
r->status = call->status;
r->datalen = call->reply_data.dsize;
if (call->reply_data.dsize) {
memcpy(&r->data[0], call->reply_data.dptr, call->reply_data.dsize);
}
ctdb_queue_packet(ctdb, &r->hdr);
talloc_free(r);
talloc_free(call);
}
/**
* called when a CTDB_REPLY_CALL packet comes in
*
* This packet comes in response to a CTDB_REQ_CALL request packet. It
* contains any reply data from the call
*/
void ctdb_reply_call(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
{
struct ctdb_reply_call_old *c = (struct ctdb_reply_call_old *)hdr;
struct ctdb_call_state *state;
state = reqid_find(ctdb->idr, hdr->reqid, struct ctdb_call_state);
if (state == NULL) {
DEBUG(DEBUG_ERR, (__location__ " reqid %u not found\n", hdr->reqid));
return;
}
if (hdr->reqid != state->reqid) {
/* we found a record but it was the wrong one */
DEBUG(DEBUG_ERR, ("Dropped orphaned call reply with reqid:%u\n",hdr->reqid));
return;
}
/* read only delegation processing */
/* If we got a FETCH_WITH_HEADER we should check if this is a ro
* delegation since we may need to update the record header
*/
if (state->c->callid == CTDB_FETCH_WITH_HEADER_FUNC) {
struct ctdb_db_context *ctdb_db = state->ctdb_db;
struct ctdb_ltdb_header *header = (struct ctdb_ltdb_header *)&c->data[0];
struct ctdb_ltdb_header oldheader;
TDB_DATA key, data, olddata;
int ret;
if (!(header->flags & CTDB_REC_RO_HAVE_READONLY)) {
goto finished_ro;
return;
}
key.dsize = state->c->keylen;
key.dptr = state->c->data;
ret = ctdb_ltdb_lock_requeue(ctdb_db, key, hdr,
ctdb_call_input_pkt, ctdb, false);
if (ret == -2) {
return;
}
if (ret != 0) {
DEBUG(DEBUG_ERR,(__location__ " Failed to get lock in ctdb_reply_call\n"));
return;
}
ret = ctdb_ltdb_fetch(ctdb_db, key, &oldheader, state, &olddata);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Failed to fetch old record in ctdb_reply_call\n"));
ctdb_ltdb_unlock(ctdb_db, key);
goto finished_ro;
}
if (header->rsn <= oldheader.rsn) {
ctdb_ltdb_unlock(ctdb_db, key);
goto finished_ro;
}
if (c->datalen < sizeof(struct ctdb_ltdb_header)) {
DEBUG(DEBUG_ERR,(__location__ " Got FETCH_WITH_HEADER reply with too little data: %d bytes\n", c->datalen));
ctdb_ltdb_unlock(ctdb_db, key);
goto finished_ro;
}
data.dsize = c->datalen - sizeof(struct ctdb_ltdb_header);
data.dptr = &c->data[sizeof(struct ctdb_ltdb_header)];
ret = ctdb_ltdb_store(ctdb_db, key, header, data);
if (ret != 0) {
DEBUG(DEBUG_ERR, ("Failed to store new record in ctdb_reply_call\n"));
ctdb_ltdb_unlock(ctdb_db, key);
goto finished_ro;
}
ctdb_ltdb_unlock(ctdb_db, key);
}
finished_ro:
state->call->reply_data.dptr = c->data;
state->call->reply_data.dsize = c->datalen;
state->call->status = c->status;
talloc_steal(state, c);
state->state = CTDB_CALL_DONE;
if (state->async.fn) {
state->async.fn(state);
}
}
/**
* called when a CTDB_REPLY_DMASTER packet comes in
*
* This packet comes in from the lmaster in response to a CTDB_REQ_CALL
* request packet. It means that the current dmaster wants to give us
* the dmaster role.
*/
void ctdb_reply_dmaster(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
{
struct ctdb_reply_dmaster_old *c = (struct ctdb_reply_dmaster_old *)hdr;
struct ctdb_db_context *ctdb_db;
TDB_DATA key, data;
uint32_t record_flags = 0;
size_t len;
int ret;
ctdb_db = find_ctdb_db(ctdb, c->db_id);
if (ctdb_db == NULL) {
DEBUG(DEBUG_ERR,("Unknown db_id 0x%x in ctdb_reply_dmaster\n", c->db_id));
return;
}
key.dptr = c->data;
key.dsize = c->keylen;
data.dptr = &c->data[key.dsize];
data.dsize = c->datalen;
len = offsetof(struct ctdb_reply_dmaster_old, data) + key.dsize + data.dsize
+ sizeof(uint32_t);
if (len <= c->hdr.length) {
memcpy(&record_flags, &c->data[c->keylen + c->datalen],
sizeof(record_flags));
}
dmaster_defer_setup(ctdb_db, hdr, key);
ret = ctdb_ltdb_lock_requeue(ctdb_db, key, hdr,
ctdb_call_input_pkt, ctdb, false);
if (ret == -2) {
return;
}
if (ret != 0) {
DEBUG(DEBUG_ERR,(__location__ " Failed to get lock in ctdb_reply_dmaster\n"));
return;
}
ctdb_become_dmaster(ctdb_db, hdr, key, data, c->rsn, record_flags);
}
/*
called when a CTDB_REPLY_ERROR packet comes in
*/
void ctdb_reply_error(struct ctdb_context *ctdb, struct ctdb_req_header *hdr)
{
struct ctdb_reply_error_old *c = (struct ctdb_reply_error_old *)hdr;
struct ctdb_call_state *state;
state = reqid_find(ctdb->idr, hdr->reqid, struct ctdb_call_state);
if (state == NULL) {
DEBUG(DEBUG_ERR,("pnn %u Invalid reqid %u in ctdb_reply_error\n",
ctdb->pnn, hdr->reqid));
return;
}
if (hdr->reqid != state->reqid) {
/* we found a record but it was the wrong one */
DEBUG(DEBUG_ERR, ("Dropped orphaned error reply with reqid:%u\n",hdr->reqid));
return;
}
talloc_steal(state, c);
state->state = CTDB_CALL_ERROR;
state->errmsg = (char *)c->msg;
if (state->async.fn) {
state->async.fn(state);
}
}
/*
destroy a ctdb_call
*/
static int ctdb_call_destructor(struct ctdb_call_state *state)
{
DLIST_REMOVE(state->ctdb_db->pending_calls, state);
reqid_remove(state->ctdb_db->ctdb->idr, state->reqid);
return 0;
}
/*
called when a ctdb_call needs to be resent after a reconfigure event
*/
static void ctdb_call_resend(struct ctdb_call_state *state)
{
struct ctdb_context *ctdb = state->ctdb_db->ctdb;
state->generation = state->ctdb_db->generation;
/* use a new reqid, in case the old reply does eventually come in */
reqid_remove(ctdb->idr, state->reqid);
state->reqid = reqid_new(ctdb->idr, state);
state->c->hdr.reqid = state->reqid;
/* update the generation count for this request, so its valid with the new vnn_map */
state->c->hdr.generation = state->generation;
/* send the packet to ourselves, it will be redirected appropriately */
state->c->hdr.destnode = ctdb->pnn;
ctdb_queue_packet(ctdb, &state->c->hdr);
DEBUG(DEBUG_NOTICE,("resent ctdb_call for db %s reqid %u generation %u\n",
state->ctdb_db->db_name, state->reqid, state->generation));
}
/*
resend all pending calls on recovery
*/
void ctdb_call_resend_db(struct ctdb_db_context *ctdb_db)
{
struct ctdb_call_state *state, *next;
for (state = ctdb_db->pending_calls; state; state = next) {
next = state->next;
ctdb_call_resend(state);
}
}
void ctdb_call_resend_all(struct ctdb_context *ctdb)
{
struct ctdb_db_context *ctdb_db;
for (ctdb_db = ctdb->db_list; ctdb_db; ctdb_db = ctdb_db->next) {
ctdb_call_resend_db(ctdb_db);
}
}
/*
this allows the caller to setup a async.fn
*/
static void call_local_trigger(struct tevent_context *ev,
struct tevent_timer *te,
struct timeval t, void *private_data)
{
struct ctdb_call_state *state = talloc_get_type(private_data, struct ctdb_call_state);
if (state->async.fn) {
state->async.fn(state);
}
}
/*
construct an event driven local ctdb_call
this is used so that locally processed ctdb_call requests are processed
in an event driven manner
*/
struct ctdb_call_state *ctdb_call_local_send(struct ctdb_db_context *ctdb_db,
struct ctdb_call *call,
struct ctdb_ltdb_header *header,
TDB_DATA *data)
{
struct ctdb_call_state *state;
struct ctdb_context *ctdb = ctdb_db->ctdb;
int ret;
state = talloc_zero(ctdb_db, struct ctdb_call_state);
CTDB_NO_MEMORY_NULL(ctdb, state);
talloc_steal(state, data->dptr);
state->state = CTDB_CALL_DONE;
state->call = talloc(state, struct ctdb_call);
CTDB_NO_MEMORY_NULL(ctdb, state->call);
*(state->call) = *call;
state->ctdb_db = ctdb_db;
ret = ctdb_call_local(ctdb_db, state->call, header, state, data, true);
if (ret != 0) {
DEBUG(DEBUG_DEBUG,("ctdb_call_local() failed, ignoring return code %d\n", ret));
}
tevent_add_timer(ctdb->ev, state, timeval_zero(),
call_local_trigger, state);
return state;
}
/*
make a remote ctdb call - async send. Called in daemon context.
This constructs a ctdb_call request and queues it for processing.
This call never blocks.
*/
struct ctdb_call_state *ctdb_daemon_call_send_remote(struct ctdb_db_context *ctdb_db,
struct ctdb_call *call,
struct ctdb_ltdb_header *header)
{
uint32_t len;
struct ctdb_call_state *state;
struct ctdb_context *ctdb = ctdb_db->ctdb;
struct ctdb_req_call_old *c;
if (ctdb->methods == NULL) {
DEBUG(DEBUG_INFO,(__location__ " Failed send packet. Transport is down\n"));
return NULL;
}
state = talloc_zero(ctdb_db, struct ctdb_call_state);
CTDB_NO_MEMORY_NULL(ctdb, state);
state->call = talloc(state, struct ctdb_call);
CTDB_NO_MEMORY_NULL(ctdb, state->call);
state->reqid = reqid_new(ctdb->idr, state);
state->ctdb_db = ctdb_db;
state->state = CTDB_CALL_WAIT;
state->generation = ctdb_db->generation;
len = offsetof(struct ctdb_req_call_old, data) + call->key.dsize +
call->call_data.dsize;
c = ctdb_transport_allocate(ctdb,
state,
CTDB_REQ_CALL,
len,
struct ctdb_req_call_old);
CTDB_NO_MEMORY_NULL(ctdb, c);
state->c = c;
c->hdr.destnode = header->dmaster;
c->hdr.reqid = state->reqid;
c->hdr.generation = ctdb_db->generation;
c->flags = call->flags;
c->db_id = ctdb_db->db_id;
c->callid = call->call_id;
c->hopcount = 0;
c->keylen = call->key.dsize;
c->calldatalen = call->call_data.dsize;
memcpy(&c->data[0], call->key.dptr, call->key.dsize);
memcpy(&c->data[call->key.dsize],
call->call_data.dptr,
call->call_data.dsize);
*(state->call) = *call;
state->call->call_data.dptr = &c->data[call->key.dsize];
state->call->key.dptr = &c->data[0];
DLIST_ADD(ctdb_db->pending_calls, state);
talloc_set_destructor(state, ctdb_call_destructor);
ctdb_queue_packet(ctdb, &state->c->hdr);
return state;
}
/*
make a remote ctdb call - async recv - called in daemon context
This is called when the program wants to wait for a ctdb_call to complete and get the
results. This call will block unless the call has already completed.
*/
int ctdb_daemon_call_recv(struct ctdb_call_state *state, struct ctdb_call *call)
{
while (state->state < CTDB_CALL_DONE) {
tevent_loop_once(state->ctdb_db->ctdb->ev);
}
if (state->state != CTDB_CALL_DONE) {
ctdb_set_error(state->ctdb_db->ctdb, "%s", state->errmsg);
talloc_free(state);
return -1;
}
if (state->call->reply_data.dsize) {
call->reply_data.dptr = talloc_memdup(call,
state->call->reply_data.dptr,
state->call->reply_data.dsize);
call->reply_data.dsize = state->call->reply_data.dsize;
} else {
call->reply_data.dptr = NULL;
call->reply_data.dsize = 0;
}
call->status = state->call->status;
talloc_free(state);
return 0;
}
struct revokechild_deferred_call {
struct revokechild_deferred_call *prev, *next;
struct ctdb_context *ctdb;
struct ctdb_req_header *hdr;
deferred_requeue_fn fn;
void *ctx;
struct revokechild_handle *rev_hdl;
};
struct revokechild_handle {
struct revokechild_handle *next, *prev;
struct ctdb_context *ctdb;
struct ctdb_db_context *ctdb_db;
struct tevent_fd *fde;
int status;
int fd[2];
pid_t child;
TDB_DATA key;
struct revokechild_deferred_call *deferred_call_list;
};
static void deferred_call_requeue(struct tevent_context *ev,
struct tevent_timer *te,
struct timeval t, void *private_data)
{
struct revokechild_deferred_call *dlist = talloc_get_type_abort(
private_data, struct revokechild_deferred_call);
while (dlist != NULL) {
struct revokechild_deferred_call *dcall = dlist;
talloc_set_destructor(dcall, NULL);
DLIST_REMOVE(dlist, dcall);
dcall->fn(dcall->ctx, dcall->hdr);
talloc_free(dcall);
}
}
static int deferred_call_destructor(struct revokechild_deferred_call *dcall)
{
struct revokechild_handle *rev_hdl = dcall->rev_hdl;
DLIST_REMOVE(rev_hdl->deferred_call_list, dcall);
return 0;
}
static int revokechild_destructor(struct revokechild_handle *rev_hdl)
{
struct revokechild_deferred_call *now_list = NULL;
struct revokechild_deferred_call *delay_list = NULL;
if (rev_hdl->fde != NULL) {
talloc_free(rev_hdl->fde);
}
if (rev_hdl->fd[0] != -1) {
close(rev_hdl->fd[0]);
}
if (rev_hdl->fd[1] != -1) {
close(rev_hdl->fd[1]);
}
ctdb_kill(rev_hdl->ctdb, rev_hdl->child, SIGKILL);
DLIST_REMOVE(rev_hdl->ctdb_db->revokechild_active, rev_hdl);
while (rev_hdl->deferred_call_list != NULL) {
struct revokechild_deferred_call *dcall;
dcall = rev_hdl->deferred_call_list;
DLIST_REMOVE(rev_hdl->deferred_call_list, dcall);
/* If revoke is successful, then first process all the calls
* that need write access, and delay readonly requests by 1
* second grace.
*
* If revoke is unsuccessful, most likely because of node
* failure, delay all the pending requests, so database can
* be recovered.
*/
if (rev_hdl->status == 0) {
struct ctdb_req_call_old *c;
c = (struct ctdb_req_call_old *)dcall->hdr;
if (c->flags & CTDB_WANT_READONLY) {
DLIST_ADD(delay_list, dcall);
} else {
DLIST_ADD(now_list, dcall);
}
} else {
DLIST_ADD(delay_list, dcall);
}
}
if (now_list != NULL) {
tevent_add_timer(rev_hdl->ctdb->ev,
rev_hdl->ctdb_db,
tevent_timeval_current_ofs(0, 0),
deferred_call_requeue,
now_list);
}
if (delay_list != NULL) {
tevent_add_timer(rev_hdl->ctdb->ev,
rev_hdl->ctdb_db,
tevent_timeval_current_ofs(1, 0),
deferred_call_requeue,
delay_list);
}
return 0;
}
static void revokechild_handler(struct tevent_context *ev,
struct tevent_fd *fde,
uint16_t flags, void *private_data)
{
struct revokechild_handle *rev_hdl =
talloc_get_type(private_data, struct revokechild_handle);
int ret;
char c;
ret = sys_read(rev_hdl->fd[0], &c, 1);
if (ret != 1) {
DEBUG(DEBUG_ERR,("Failed to read status from revokechild. errno:%d\n", errno));
rev_hdl->status = -1;
talloc_free(rev_hdl);
return;
}
if (c != 0) {
DEBUG(DEBUG_ERR,("revokechild returned failure. status:%d\n", c));
rev_hdl->status = -1;
talloc_free(rev_hdl);
return;
}
talloc_free(rev_hdl);
}
struct ctdb_revoke_state {
struct ctdb_db_context *ctdb_db;
TDB_DATA key;
struct ctdb_ltdb_header *header;
TDB_DATA data;
int count;
int status;
int finished;
};
static void update_record_cb(struct ctdb_client_control_state *state)
{
struct ctdb_revoke_state *revoke_state;
int ret;
int32_t res;
if (state == NULL) {
return;
}
revoke_state = state->async.private_data;
state->async.fn = NULL;
ret = ctdb_control_recv(state->ctdb, state, state, NULL, &res, NULL);
if ((ret != 0) || (res != 0)) {
DEBUG(DEBUG_ERR,("Recv for revoke update record failed ret:%d res:%d\n", ret, res));
revoke_state->status = -1;
}
revoke_state->count--;
if (revoke_state->count <= 0) {
revoke_state->finished = 1;
}
}
static void revoke_send_cb(struct ctdb_context *ctdb, uint32_t pnn, void *private_data)
{
struct ctdb_revoke_state *revoke_state = private_data;
struct ctdb_client_control_state *state;
state = ctdb_ctrl_updaterecord_send(ctdb, revoke_state, timeval_current_ofs(ctdb->tunable.control_timeout,0), pnn, revoke_state->ctdb_db, revoke_state->key, revoke_state->header, revoke_state->data);
if (state == NULL) {
DEBUG(DEBUG_ERR,("Failure to send update record to revoke readonly delegation\n"));
revoke_state->status = -1;
return;
}
state->async.fn = update_record_cb;
state->async.private_data = revoke_state;
revoke_state->count++;
}
static void ctdb_revoke_timeout_handler(struct tevent_context *ev,
struct tevent_timer *te,
struct timeval yt, void *private_data)
{
struct ctdb_revoke_state *state = private_data;
DEBUG(DEBUG_ERR,("Timed out waiting for revoke to finish\n"));
state->finished = 1;
state->status = -1;
}
static int ctdb_revoke_all_delegations(struct ctdb_context *ctdb, struct ctdb_db_context *ctdb_db, TDB_DATA tdata, TDB_DATA key, struct ctdb_ltdb_header *header, TDB_DATA data)
{
struct ctdb_revoke_state *state = talloc_zero(ctdb, struct ctdb_revoke_state);
struct ctdb_ltdb_header new_header;
TDB_DATA new_data;
state->ctdb_db = ctdb_db;
state->key = key;
state->header = header;
state->data = data;
ctdb_trackingdb_traverse(ctdb, tdata, revoke_send_cb, state);
tevent_add_timer(ctdb->ev, state,
timeval_current_ofs(ctdb->tunable.control_timeout, 0),
ctdb_revoke_timeout_handler, state);
while (state->finished == 0) {
tevent_loop_once(ctdb->ev);
}
if (ctdb_ltdb_lock(ctdb_db, key) != 0) {
DEBUG(DEBUG_ERR,("Failed to chainlock the database in revokechild\n"));
talloc_free(state);
return -1;
}
if (ctdb_ltdb_fetch(ctdb_db, key, &new_header, state, &new_data) != 0) {
ctdb_ltdb_unlock(ctdb_db, key);
DEBUG(DEBUG_ERR,("Failed for fetch tdb record in revokechild\n"));
talloc_free(state);
return -1;
}
header->rsn++;
if (new_header.rsn > header->rsn) {
ctdb_ltdb_unlock(ctdb_db, key);
DEBUG(DEBUG_ERR,("RSN too high in tdb record in revokechild\n"));
talloc_free(state);
return -1;
}
if ( (new_header.flags & (CTDB_REC_RO_REVOKING_READONLY|CTDB_REC_RO_HAVE_DELEGATIONS)) != (CTDB_REC_RO_REVOKING_READONLY|CTDB_REC_RO_HAVE_DELEGATIONS) ) {
ctdb_ltdb_unlock(ctdb_db, key);
DEBUG(DEBUG_ERR,("Flags are wrong in tdb record in revokechild\n"));
talloc_free(state);
return -1;
}
/*
* If revoke on all nodes succeed, revoke is complete. Otherwise,
* remove CTDB_REC_RO_REVOKING_READONLY flag and retry.
*/
if (state->status == 0) {
new_header.rsn++;
new_header.flags |= CTDB_REC_RO_REVOKE_COMPLETE;
} else {
DEBUG(DEBUG_NOTICE, ("Revoke all delegations failed, retrying.\n"));
new_header.flags &= ~CTDB_REC_RO_REVOKING_READONLY;
}
if (ctdb_ltdb_store(ctdb_db, key, &new_header, new_data) != 0) {
ctdb_ltdb_unlock(ctdb_db, key);
DEBUG(DEBUG_ERR,("Failed to write new record in revokechild\n"));
talloc_free(state);
return -1;
}
ctdb_ltdb_unlock(ctdb_db, key);
talloc_free(state);
return 0;
}
int ctdb_start_revoke_ro_record(struct ctdb_context *ctdb,
struct ctdb_db_context *ctdb_db,
TDB_DATA key,
struct ctdb_ltdb_header *header,
TDB_DATA data)
{
TDB_DATA tdata;
struct revokechild_handle *rev_hdl;
pid_t parent = getpid();
int ret;
header->flags &= ~(CTDB_REC_RO_REVOKING_READONLY |
CTDB_REC_RO_HAVE_DELEGATIONS |
CTDB_REC_RO_HAVE_READONLY);
header->flags |= CTDB_REC_FLAG_MIGRATED_WITH_DATA;
header->rsn -= 1;
rev_hdl = talloc_zero(ctdb_db, struct revokechild_handle);
if (rev_hdl == NULL) {
D_ERR("Failed to allocate revokechild_handle\n");
return -1;
}
tdata = tdb_fetch(ctdb_db->rottdb, key);
if (tdata.dsize > 0) {
uint8_t *tmp;
tmp = tdata.dptr;
tdata.dptr = talloc_memdup(rev_hdl, tdata.dptr, tdata.dsize);
free(tmp);
}
rev_hdl->status = 0;
rev_hdl->ctdb = ctdb;
rev_hdl->ctdb_db = ctdb_db;
rev_hdl->fd[0] = -1;
rev_hdl->fd[1] = -1;
rev_hdl->key.dsize = key.dsize;
rev_hdl->key.dptr = talloc_memdup(rev_hdl, key.dptr, key.dsize);
if (rev_hdl->key.dptr == NULL) {
D_ERR("Failed to allocate key for revokechild_handle\n");
goto err_out;
}
ret = pipe(rev_hdl->fd);
if (ret != 0) {
D_ERR("Failed to allocate key for revokechild_handle\n");
goto err_out;
}
rev_hdl->child = ctdb_fork(ctdb);
if (rev_hdl->child == (pid_t)-1) {
D_ERR("Failed to fork child for revokechild\n");
goto err_out;
}
if (rev_hdl->child == 0) {
char c = 0;
close(rev_hdl->fd[0]);
prctl_set_comment("ctdb_revokechild");
if (switch_from_server_to_client(ctdb) != 0) {
D_ERR("Failed to switch from server to client "
"for revokechild process\n");
c = 1;
goto child_finished;
}
c = ctdb_revoke_all_delegations(ctdb,
ctdb_db,
tdata,
key,
header,
data);
child_finished:
sys_write(rev_hdl->fd[1], &c, 1);
ctdb_wait_for_process_to_exit(parent);
_exit(0);
}
close(rev_hdl->fd[1]);
rev_hdl->fd[1] = -1;
set_close_on_exec(rev_hdl->fd[0]);
rev_hdl->fde = tevent_add_fd(ctdb->ev,
rev_hdl,
rev_hdl->fd[0],
TEVENT_FD_READ,
revokechild_handler,
(void *)rev_hdl);
if (rev_hdl->fde == NULL) {
D_ERR("Failed to set up fd event for revokechild process\n");
talloc_free(rev_hdl);
}
tevent_fd_set_auto_close(rev_hdl->fde);
/* This is an active revokechild child process */
DLIST_ADD_END(ctdb_db->revokechild_active, rev_hdl);
talloc_set_destructor(rev_hdl, revokechild_destructor);
return 0;
err_out:
talloc_free(rev_hdl);
return -1;
}
int ctdb_add_revoke_deferred_call(struct ctdb_context *ctdb, struct ctdb_db_context *ctdb_db, TDB_DATA key, struct ctdb_req_header *hdr, deferred_requeue_fn fn, void *call_context)
{
struct revokechild_handle *rev_hdl;
struct revokechild_deferred_call *deferred_call;
for (rev_hdl = ctdb_db->revokechild_active;
rev_hdl;
rev_hdl = rev_hdl->next) {
if (rev_hdl->key.dsize == 0) {
continue;
}
if (rev_hdl->key.dsize != key.dsize) {
continue;
}
if (!memcmp(rev_hdl->key.dptr, key.dptr, key.dsize)) {
break;
}
}
if (rev_hdl == NULL) {
DEBUG(DEBUG_ERR,("Failed to add deferred call to revoke list. revoke structure not found\n"));
return -1;
}
deferred_call = talloc(call_context, struct revokechild_deferred_call);
if (deferred_call == NULL) {
DEBUG(DEBUG_ERR,("Failed to allocate deferred call structure for revoking record\n"));
return -1;
}
deferred_call->ctdb = ctdb;
deferred_call->hdr = talloc_steal(deferred_call, hdr);
deferred_call->fn = fn;
deferred_call->ctx = call_context;
deferred_call->rev_hdl = rev_hdl;
talloc_set_destructor(deferred_call, deferred_call_destructor);
DLIST_ADD(rev_hdl->deferred_call_list, deferred_call);
return 0;
}
static void ctdb_migration_count_handler(TDB_DATA key, uint64_t counter,
void *private_data)
{
struct ctdb_db_context *ctdb_db = talloc_get_type_abort(
private_data, struct ctdb_db_context);
int value;
value = (counter < INT_MAX ? counter : INT_MAX);
ctdb_update_db_stat_hot_keys(ctdb_db, key, value);
}
static void ctdb_migration_cleandb_event(struct tevent_context *ev,
struct tevent_timer *te,
struct timeval current_time,
void *private_data)
{
struct ctdb_db_context *ctdb_db = talloc_get_type_abort(
private_data, struct ctdb_db_context);
if (ctdb_db->migratedb == NULL) {
return;
}
hash_count_expire(ctdb_db->migratedb, NULL);
te = tevent_add_timer(ctdb_db->ctdb->ev, ctdb_db->migratedb,
tevent_timeval_current_ofs(10, 0),
ctdb_migration_cleandb_event, ctdb_db);
if (te == NULL) {
DEBUG(DEBUG_ERR,
("Memory error in migration cleandb event for %s\n",
ctdb_db->db_name));
TALLOC_FREE(ctdb_db->migratedb);
}
}
int ctdb_migration_init(struct ctdb_db_context *ctdb_db)
{
struct timeval one_second = { 1, 0 };
struct tevent_timer *te;
int ret;
if (! ctdb_db_volatile(ctdb_db)) {
return 0;
}
ret = hash_count_init(ctdb_db, one_second,
ctdb_migration_count_handler, ctdb_db,
&ctdb_db->migratedb);
if (ret != 0) {
DEBUG(DEBUG_ERR,
("Memory error in migration init for %s\n",
ctdb_db->db_name));
return -1;
}
te = tevent_add_timer(ctdb_db->ctdb->ev, ctdb_db->migratedb,
tevent_timeval_current_ofs(10, 0),
ctdb_migration_cleandb_event, ctdb_db);
if (te == NULL) {
DEBUG(DEBUG_ERR,
("Memory error in migration init for %s\n",
ctdb_db->db_name));
TALLOC_FREE(ctdb_db->migratedb);
return -1;
}
return 0;
}
|