Skip to content

Commit 22d8219

Browse files
author
Tiago Alves
committed
WL#12209 EXTRACT MY.CNF GENERATION TO OWN FUNCTION
Simplify code-logic by extracting my.cnf generation to it's own function. Changes are motivated by the need of having a simple way to turn-off my.cnf generation (needed to support config.ini). As consequence, this change also enable the simplification of erro handling code (i.e. closing open files when having error). Finally, made my.cnf generation logic more lineant by not calling abort() when settings were added for AP_CLUSTER or AP_ALL and simply skipping them.
1 parent 527d8e7 commit 22d8219

File tree

1 file changed

+64
-60
lines changed

1 file changed

+64
-60
lines changed

storage/ndb/test/run-test/files.cpp

Lines changed: 64 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <portlib/NdbDir.hpp>
2020
#include "atrt.hpp"
2121

22+
static bool generate_my_cnf(BaseString mycnf, atrt_config& config);
2223
static bool create_directory(const char* path);
2324

2425
bool setup_directories(atrt_config& config, int setup) {
@@ -125,6 +126,63 @@ static char* dirname(const char* path) {
125126
#define pclose _pclose
126127
#endif
127128

129+
static bool generate_my_cnf(BaseString mycnf, atrt_config& config) {
130+
FILE* out = fopen(mycnf.c_str(), "a+");
131+
if (out == NULL) {
132+
g_logger.error("Failed to open %s for append", mycnf.c_str());
133+
return false;
134+
}
135+
136+
time_t now = time(0);
137+
fprintf(out, "#\n# Generated by atrt\n");
138+
fprintf(out, "# %s\n", ctime(&now));
139+
140+
for (unsigned i = 0; i < config.m_clusters.size(); i++) {
141+
atrt_cluster& cluster = *config.m_clusters[i];
142+
printfile(out, cluster.m_options.m_generated, "[mysql_cluster%s]",
143+
cluster.m_name.c_str());
144+
145+
for (unsigned j = 0; j < cluster.m_processes.size(); j++) {
146+
atrt_process& proc = *cluster.m_processes[j];
147+
148+
switch (proc.m_type) {
149+
case atrt_process::AP_NDB_MGMD:
150+
printfile(out, proc.m_options.m_generated,
151+
"[cluster_config.ndb_mgmd.%d%s]", proc.m_index,
152+
proc.m_cluster->m_name.c_str());
153+
break;
154+
case atrt_process::AP_NDBD:
155+
printfile(out, proc.m_options.m_generated,
156+
"[cluster_config.ndbd.%d%s]", proc.m_index,
157+
proc.m_cluster->m_name.c_str());
158+
break;
159+
case atrt_process::AP_MYSQLD:
160+
printfile(out, proc.m_options.m_generated, "[mysqld.%d%s]",
161+
proc.m_index, proc.m_cluster->m_name.c_str());
162+
break;
163+
case atrt_process::AP_CLIENT:
164+
printfile(out, proc.m_options.m_generated, "[client.%d%s]",
165+
proc.m_index, proc.m_cluster->m_name.c_str());
166+
break;
167+
case atrt_process::AP_CUSTOM:
168+
printfile(out, proc.m_options.m_generated, "[%s.%d%s]",
169+
proc.m_name.c_str(), proc.m_index,
170+
proc.m_cluster->m_name.c_str());
171+
break;
172+
case atrt_process::AP_NDB_API:
173+
// fall-through
174+
case atrt_process::AP_ALL:
175+
// fall-through
176+
case atrt_process::AP_CLUSTER:
177+
break;
178+
}
179+
}
180+
}
181+
182+
fclose(out);
183+
return true;
184+
}
185+
128186
bool setup_files(atrt_config& config, int setup, int sshx) {
129187
/**
130188
* 0 = validate
@@ -235,65 +293,18 @@ bool setup_files(atrt_config& config, int setup, int sshx) {
235293
}
236294
}
237295

238-
FILE* out = NULL;
239-
bool retval = true;
240296
if (config.m_generated == false) {
241-
g_logger.info("Nothing configured...");
297+
g_logger.info("Skipping my.cnf generation...");
242298
} else {
243-
out = fopen(mycnf.c_str(), "a+");
244-
if (out == 0) {
245-
g_logger.error("Failed to open %s for append", mycnf.c_str());
246-
return false;
247-
}
248-
time_t now = time(0);
249-
fprintf(out, "#\n# Generated by atrt\n");
250-
fprintf(out, "# %s\n", ctime(&now));
299+
bool ok = generate_my_cnf(mycnf, config);
300+
if (!ok) return false;
251301
}
252302

253303
for (unsigned i = 0; i < config.m_clusters.size(); i++) {
254304
atrt_cluster& cluster = *config.m_clusters[i];
255-
if (out) {
256-
Properties::Iterator it(&cluster.m_options.m_generated);
257-
printfile(out, cluster.m_options.m_generated, "[mysql_cluster%s]",
258-
cluster.m_name.c_str());
259-
}
260-
261305
for (unsigned j = 0; j < cluster.m_processes.size(); j++) {
262306
atrt_process& proc = *cluster.m_processes[j];
263307

264-
if (out) {
265-
switch (proc.m_type) {
266-
case atrt_process::AP_NDB_MGMD:
267-
printfile(out, proc.m_options.m_generated,
268-
"[cluster_config.ndb_mgmd.%d%s]", proc.m_index,
269-
proc.m_cluster->m_name.c_str());
270-
break;
271-
case atrt_process::AP_NDBD:
272-
printfile(out, proc.m_options.m_generated,
273-
"[cluster_config.ndbd.%d%s]", proc.m_index,
274-
proc.m_cluster->m_name.c_str());
275-
break;
276-
case atrt_process::AP_MYSQLD:
277-
printfile(out, proc.m_options.m_generated, "[mysqld.%d%s]",
278-
proc.m_index, proc.m_cluster->m_name.c_str());
279-
break;
280-
case atrt_process::AP_NDB_API:
281-
break;
282-
case atrt_process::AP_CLIENT:
283-
printfile(out, proc.m_options.m_generated, "[client.%d%s]",
284-
proc.m_index, proc.m_cluster->m_name.c_str());
285-
break;
286-
case atrt_process::AP_CUSTOM:
287-
printfile(out, proc.m_options.m_generated, "[%s.%d%s]",
288-
proc.m_name.c_str(), proc.m_index,
289-
proc.m_cluster->m_name.c_str());
290-
break;
291-
case atrt_process::AP_ALL:
292-
case atrt_process::AP_CLUSTER:
293-
abort();
294-
}
295-
}
296-
297308
/**
298309
* Create env.sh
299310
*/
@@ -306,8 +317,7 @@ bool setup_files(atrt_config& config, int setup, int sshx) {
306317
FILE* fenv = fopen(tmp.c_str(), "w+");
307318
if (fenv == 0) {
308319
g_logger.error("Failed to open %s for writing", tmp.c_str());
309-
retval = false;
310-
goto end;
320+
return false;
311321
}
312322
for (size_t k = 0; env[k]; k++) {
313323
tmp = env[k];
@@ -364,8 +374,7 @@ bool setup_files(atrt_config& config, int setup, int sshx) {
364374
FILE* fenv = fopen(tmp.c_str(), "w+");
365375
if (fenv == 0) {
366376
g_logger.error("Failed to open %s for writing", tmp.c_str());
367-
retval = false;
368-
goto end;
377+
return false;
369378
}
370379
fprintf(fenv, "#!/bin/sh\n");
371380
fprintf(fenv, "cd %s\n", proc.m_proc.m_cwd.c_str());
@@ -379,12 +388,7 @@ bool setup_files(atrt_config& config, int setup, int sshx) {
379388
}
380389
}
381390

382-
end:
383-
if (out) {
384-
fclose(out);
385-
}
386-
387-
return retval;
391+
return true;
388392
}
389393

390394
static bool create_directory(const char* path) {

0 commit comments

Comments
 (0)