Skip to content

Commit b65ec6d

Browse files
committed
WIP: New wsrep-lib informative assert
Example: 2023-08-31 8:26:32 1 [ERROR] WSREP: Assertion: '(0 && (state() == s_preparing || (is_xa() && state() == s_replaying) || (ret && (state() == s_must_abort || state() == s_must_replay || state() == s_cert_failed || state() == s_aborted))))' failed in: /home/jan/work/mariadb/10.4/wsrep-lib/src/transaction.cpp:387 Info: ::before_prepare_leave : ret=0 error=success state=preparing
1 parent 173693f commit b65ec6d

File tree

4 files changed

+167
-4
lines changed

4 files changed

+167
-4
lines changed

include/wsrep/assert.hpp

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright (C) 2023 Codership Oy <[email protected]>
3+
*
4+
* This file is part of wsrep-lib.
5+
*
6+
* Wsrep-lib is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* Wsrep-lib is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with wsrep-lib. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef WSREP_ASSERT_HPP
21+
#define WSREP_ASSERT_HPP
22+
23+
/** @file assert.hpp
24+
*
25+
* Wsrep library informative assert.
26+
*/
27+
28+
#include <assert.h>
29+
30+
/**
31+
* Function to do informative assert with actual file name,
32+
* line and formatted info text.
33+
*
34+
* @param expr Assertion expression string
35+
* @param file File name string
36+
* @param line File line number
37+
* @param info Formatted info text
38+
*
39+
*/
40+
void wsrep_info_assert(const char* expr, const char* file, const int line, const char* info);
41+
42+
/**
43+
* Function to create informative text from format
44+
* and variable number of parameters
45+
*
46+
* @param format strict
47+
* @param ... variable number of parameters
48+
*
49+
* @retval Formated informative text
50+
*
51+
*/
52+
char* wsrep_info_assert_text(const char* format, ...);
53+
54+
/**
55+
* Macro for informative assert. First actual expression
56+
* is printed and then informative text provided by
57+
* developer.
58+
*
59+
* @param exp assertion expression
60+
* @param p Informative text format and parameters
61+
*
62+
* usage : WSREP_INFO_ASSERT((EXPR), (format, format_param,...))
63+
* example : WSREP_INFO_ASSERT(0, ("This is always false=%d", 0));
64+
*
65+
*/
66+
#ifdef NDEBUG
67+
#define WSREP_INFO_ASSERT(exp, p) (__ASSERT_VOID_CAST (0))
68+
#else
69+
#define WSREP_INFO_ASSERT(exp, p) do { \
70+
if (__builtin_expect((!(exp)), false)) \
71+
{ \
72+
wsrep_info_assert(#exp, __FILE__, __LINE__, \
73+
wsrep_info_assert_text p); \
74+
assert(exp); \
75+
} \
76+
} while (0)
77+
#endif // NDEBUG
78+
79+
#endif // WSREP_ASSERT_HPP

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
add_library(wsrep-lib
66
allowlist_service_v1.cpp
7+
assert.cpp
78
client_state.cpp
89
config_service_v1.cpp
910
event_service_v1.cpp

src/assert.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (C) 2023 Codership Oy <[email protected]>
3+
*
4+
* This file is part of wsrep-lib.
5+
*
6+
* Wsrep-lib is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU General Public License as published by
8+
* the Free Software Foundation, either version 2 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* Wsrep-lib is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU General Public License
17+
* along with wsrep-lib. If not, see <https://www.gnu.org/licenses/>.
18+
*/
19+
20+
/** @file assert.cpp
21+
*
22+
* Wsrep library informative assert.
23+
*/
24+
25+
#include <cassert>
26+
#include <cstring>
27+
#include <stdarg.h>
28+
#include <stdio.h>
29+
#include "wsrep/logger.hpp"
30+
31+
/**
32+
* Function to create informative text from format
33+
* and variable number of parameters
34+
*
35+
* @param format text format strict
36+
* @param ... variable number of parameters
37+
*
38+
* @retval Formated informative text
39+
*
40+
*/
41+
#define WSREP_MAX_INFO_BUF 1024
42+
43+
char* wsrep_info_assert_text(const char* format, ...)
44+
{
45+
static char wsrep_buf[WSREP_MAX_INFO_BUF];
46+
va_list ap;
47+
48+
va_start(ap, format);
49+
int n= vsnprintf(wsrep_buf, WSREP_MAX_INFO_BUF,(char *)format, ap);
50+
if (n >= WSREP_MAX_INFO_BUF)
51+
{
52+
wsrep::log_debug() << "wsrep_info_assert_text too long and truncated"
53+
<< " used length: " << WSREP_MAX_INFO_BUF
54+
<< " needed lenght: " << n;
55+
}
56+
va_end(ap);
57+
return wsrep_buf;
58+
}
59+
60+
/**
61+
* Function to do informative assert with actual file name,
62+
* line and formatted info text. Function will not return.
63+
*
64+
* @param file File name string
65+
* @param line File line number
66+
* @param info Formatted info text
67+
*
68+
*/
69+
void wsrep_info_assert(const char* expr, const char* file, const int line, const char* info)
70+
{
71+
wsrep::log_error() << "Assertion: '" << expr << "' failed in: " << file << ":" << line
72+
<< " Info: " << info;
73+
}

src/transaction.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "wsrep/compiler.hpp"
2828
#include "wsrep/server_service.hpp"
2929
#include "wsrep/client_service.hpp"
30+
#include "wsrep/assert.hpp"
3031

3132
#include <cassert>
3233
#include <sstream>
@@ -346,10 +347,14 @@ int wsrep::transaction::before_prepare(
346347
ret = certify_commit(lock);
347348
}
348349

349-
assert((ret == 0 && state() == s_preparing) ||
350+
WSREP_INFO_ASSERT(((ret == 0 && state() == s_preparing) ||
350351
(state() == s_must_abort ||
351352
state() == s_must_replay ||
352-
state() == s_cert_failed));
353+
state() == s_cert_failed)),
354+
("::before_prepare : ret=%d error=%s state=%s\n",
355+
ret,
356+
to_c_string(client_state_.current_error()),
357+
to_c_string(state())));
353358

354359
if (ret)
355360
{
@@ -379,12 +384,17 @@ int wsrep::transaction::before_prepare(
379384
break;
380385
}
381386

382-
assert(state() == s_preparing ||
387+
WSREP_INFO_ASSERT((state() == s_preparing ||
383388
(is_xa() && state() == s_replaying) ||
384389
(ret && (state() == s_must_abort ||
385390
state() == s_must_replay ||
386391
state() == s_cert_failed ||
387-
state() == s_aborted)));
392+
state() == s_aborted))),
393+
("::before_prepare_leave : ret=%d error=%s state=%s\n",
394+
ret,
395+
to_c_string(client_state_.current_error()),
396+
to_c_string(state())));
397+
388398
debug_log_state("before_prepare_leave");
389399
return ret;
390400
}

0 commit comments

Comments
 (0)