ctdb-scripts: New consistent system memory and swap monitoring
[Samba.git] / ctdb / tests / src / ctdb_update_record.c
blob78d983bccc32cc3bfe5376f5f8e27bf394b115b9
1 /*
2 simple ctdb test tool
3 This test just fetch_locks a record bumps the RSN and then writes new content
5 Copyright (C) Ronnie Sahlberg 2009
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
22 #include "system/filesys.h"
23 #include "popt.h"
24 #include "cmdline.h"
25 #include "ctdb_private.h"
27 static struct ctdb_db_context *ctdb_db;
29 #define TESTKEY "testkey"
33 Just try locking/unlocking a single record once
35 static void fetch_lock_once(struct ctdb_context *ctdb, struct event_context *ev, uint32_t generation)
37 TALLOC_CTX *tmp_ctx = talloc_new(ctdb);
38 TDB_DATA key, data;
39 struct ctdb_record_handle *h;
40 struct ctdb_ltdb_header *header;
41 int ret;
43 key.dptr = discard_const(TESTKEY);
44 key.dsize = strlen(TESTKEY);
46 printf("Trying to fetch lock the record ...\n");
48 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
49 if (h == NULL) {
50 printf("Failed to fetch record '%s' on node %d\n",
51 (const char *)key.dptr, ctdb_get_pnn(ctdb));
52 talloc_free(tmp_ctx);
53 exit(10);
56 printf("Record fetchlocked.\n");
57 header = talloc_memdup(tmp_ctx, ctdb_header_from_record_handle(h), sizeof(*header));
58 printf("RSN:%d\n", (int)header->rsn);
59 talloc_free(h);
60 printf("Record released.\n");
62 printf("Write new record with RSN+10\n");
63 header->rsn += 10;
64 data.dptr = (void *)talloc_asprintf(tmp_ctx, "%d", (int)header->rsn);
65 data.dsize = strlen((char *)data.dptr);
67 ret = ctdb_ctrl_updaterecord(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, ctdb_db, key, header, data);
68 if (ret != 0) {
69 printf("Failed to writerecord, ret==%d\n", ret);
70 exit(1);
73 printf("re-fetch the record\n");
74 h = ctdb_fetch_lock(ctdb_db, tmp_ctx, key, &data);
75 if (h == NULL) {
76 printf("Failed to fetch record '%s' on node %d\n",
77 (const char *)key.dptr, ctdb_get_pnn(ctdb));
78 talloc_free(tmp_ctx);
79 exit(10);
82 printf("Record fetchlocked.\n");
83 header = talloc_memdup(tmp_ctx, ctdb_header_from_record_handle(h), sizeof(*header));
84 printf("RSN:%d\n", (int)header->rsn);
85 talloc_free(h);
86 printf("Record released.\n");
88 talloc_free(tmp_ctx);
92 main program
94 int main(int argc, const char *argv[])
96 struct ctdb_context *ctdb;
98 struct poptOption popt_options[] = {
99 POPT_AUTOHELP
100 POPT_CTDB_CMDLINE
101 POPT_TABLEEND
103 int opt;
104 const char **extra_argv;
105 int extra_argc = 0;
106 poptContext pc;
107 struct event_context *ev;
108 struct ctdb_vnn_map *vnnmap=NULL;
110 pc = poptGetContext(argv[0], argc, argv, popt_options, POPT_CONTEXT_KEEP_FIRST);
112 while ((opt = poptGetNextOpt(pc)) != -1) {
113 switch (opt) {
114 default:
115 fprintf(stderr, "Invalid option %s: %s\n",
116 poptBadOption(pc, 0), poptStrerror(opt));
117 exit(1);
121 /* setup the remaining options for the main program to use */
122 extra_argv = poptGetArgs(pc);
123 if (extra_argv) {
124 extra_argv++;
125 while (extra_argv[extra_argc]) extra_argc++;
128 ev = event_context_init(NULL);
130 ctdb = ctdb_cmdline_client(ev, timeval_current_ofs(5, 0));
131 if (ctdb == NULL) {
132 exit(1);
135 /* attach to a specific database */
136 ctdb_db = ctdb_attach(ctdb, timeval_current_ofs(5, 0), "test.tdb", false, 0);
137 if (!ctdb_db) {
138 printf("ctdb_attach failed - %s\n", ctdb_errstr(ctdb));
139 exit(1);
142 printf("Waiting for cluster\n");
143 while (1) {
144 uint32_t recmode=1;
145 ctdb_ctrl_getrecmode(ctdb, ctdb, timeval_zero(), CTDB_CURRENT_NODE, &recmode);
146 if (recmode == 0) break;
147 event_loop_once(ev);
151 if (ctdb_ctrl_getvnnmap(ctdb, timeval_zero(), CTDB_CURRENT_NODE, ctdb, &vnnmap) != 0) {
152 printf("Unable to get vnnmap from local node\n");
153 exit(1);
155 printf("Current Generation %d\n", (int)vnnmap->generation);
157 fetch_lock_once(ctdb, ev, vnnmap->generation);
159 return 0;