Skip to content

Commit d50e53e

Browse files
authored
test: add unit test for sds_check (#1)
+ minor adjustments to error/warning messages Signed-off-by: Oksana Salyk <oksana.salyk@hpe.com>
1 parent 1f1f203 commit d50e53e

35 files changed

Lines changed: 569 additions & 43 deletions

src/common/pmemcommon.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-License-Identifier: BSD-3-Clause
22
# Copyright 2017-2023, Intel Corporation
3+
# Copyright 2025, Hewlett Packard Enterprise Development LP
34
#
45
# src/pmemcommon.inc -- common SOURCE definitions for PMDK libraries
56
#
@@ -21,6 +22,7 @@ SOURCE +=\
2122
$(COMMON)/rand.c\
2223
$(COMMON)/set.c\
2324
$(COMMON)/shutdown_state.c\
25+
$(COMMON)/shutdown_state_helper.c\
2426
$(COMMON)/uuid.c\
2527
$(COMMON)/uuid_linux.c\
2628
$(PMEM2)/pmem2_utils.c\

src/common/shutdown_state.c

Lines changed: 27 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// SPDX-License-Identifier: BSD-3-Clause
22
/* Copyright 2017-2024, Intel Corporation */
3+
/* Copyright 2025, Hewlett Packard Enterprise Development LP */
34

45
/*
56
* shutdown_state.c -- unsafe shudown detection
@@ -17,13 +18,10 @@
1718
#include "bad_blocks.h"
1819
#include "../libpmem2/pmem2_utils.h"
1920

20-
#define FLUSH_SDS(sds, rep) \
21-
if ((rep) != NULL) os_part_deep_common(rep, 0, sds, sizeof(*(sds)), 1)
22-
2321
/*
24-
* shutdown_state_checksum -- (internal) counts SDS checksum and flush it
22+
* shutdown_state_checksum -- counts SDS checksum and flush it
2523
*/
26-
static void
24+
void
2725
shutdown_state_checksum(struct shutdown_state *sds, struct pool_replica *rep)
2826
{
2927
LOG(3, "sds %p", sds);
@@ -159,24 +157,6 @@ shutdown_state_clear_dirty(struct shutdown_state *sds, struct pool_replica *rep)
159157
shutdown_state_checksum(sds, rep);
160158
}
161159

162-
/*
163-
* shutdown_state_reinit -- (internal) reinitializes shutdown_state struct
164-
*/
165-
static void
166-
shutdown_state_reinit(struct shutdown_state *curr_sds,
167-
struct shutdown_state *pool_sds, struct pool_replica *rep)
168-
{
169-
LOG(3, "curr_sds %p, pool_sds %p", curr_sds, pool_sds);
170-
shutdown_state_init(pool_sds, rep);
171-
pool_sds->uuid = htole64(curr_sds->uuid);
172-
pool_sds->usc = htole64(curr_sds->usc);
173-
pool_sds->dirty = 0;
174-
175-
FLUSH_SDS(pool_sds, rep);
176-
177-
shutdown_state_checksum(pool_sds, rep);
178-
}
179-
180160
/*
181161
* shutdown_state_check -- compares and fixes shutdown state
182162
*/
@@ -186,15 +166,26 @@ shutdown_state_check(struct shutdown_state *curr_sds,
186166
{
187167
LOG(3, "curr_sds %p, pool_sds %p", curr_sds, pool_sds);
188168

169+
/*
170+
* This is likely to occur only when the pool is being opened for
171+
* the first time after the SHUTDOWN_STATE feature has been enabled on
172+
* the pool, for example, via (lib)pmempool.
173+
* Please do not confuse this with establishing SDS during creation.
174+
*/
189175
if (util_is_zeroed(pool_sds, sizeof(*pool_sds)) &&
190176
!util_is_zeroed(curr_sds, sizeof(*curr_sds))) {
177+
CORE_LOG_WARNING(
178+
"Enabling ADR failure detection, assuming pool consistency up to this point.");
191179
shutdown_state_reinit(curr_sds, pool_sds, rep);
192180
return 0;
193181
}
194182

183+
bool is_uuid_correct =
184+
le64toh(pool_sds->uuid) == le64toh(curr_sds->uuid);
185+
195186
bool is_uuid_usc_correct =
196187
le64toh(pool_sds->usc) == le64toh(curr_sds->usc) &&
197-
le64toh(pool_sds->uuid) == le64toh(curr_sds->uuid);
188+
is_uuid_correct;
198189

199190
bool is_checksum_correct = util_checksum(pool_sds,
200191
sizeof(*pool_sds), &pool_sds->checksum, 0, 0);
@@ -204,7 +195,7 @@ shutdown_state_check(struct shutdown_state *curr_sds,
204195
if (!is_checksum_correct) {
205196
/* the program was killed during opening or closing the pool */
206197
CORE_LOG_WARNING(
207-
"incorrect checksum - SDS will be reinitialized");
198+
"The pool was not opened/closed properly - reinitializing ADR failure detection.");
208199
shutdown_state_reinit(curr_sds, pool_sds, rep);
209200
return 0;
210201
}
@@ -217,19 +208,23 @@ shutdown_state_check(struct shutdown_state *curr_sds,
217208
* but there wasn't an ADR failure
218209
*/
219210
CORE_LOG_WARNING(
220-
"the pool was not closed - SDS will be reinitialized");
211+
"The ADR failure was detected but the pool was closed properly - reinitializing ADR failure detection.");
221212
shutdown_state_reinit(curr_sds, pool_sds, rep);
222213
return 0;
223214
}
224215
if (dirty == 0) {
225-
/* an ADR failure but the pool was closed */
226-
CORE_LOG_WARNING(
227-
"an ADR failure was detected but the pool was closed - SDS will be reinitialized");
216+
if (is_uuid_correct)
217+
CORE_LOG_WARNING(
218+
"The ADR failure was detected but the pool was closed properly - reinitializing ADR failure detection.");
219+
else
220+
CORE_LOG_HARK(
221+
"The pool has moved to a new location but it was closed properly - reinitializing ADR failure detection.");
228222
shutdown_state_reinit(curr_sds, pool_sds, rep);
229223
return 0;
230224
}
231-
/* an ADR failure - the pool might be corrupted */
232-
ERR_WO_ERRNO(
233-
"an ADR failure was detected, the pool might be corrupted");
225+
226+
ERR_WO_ERRNO("%s, the pool might be corrupted.", is_uuid_correct ?
227+
"The ADR failure was detected" :
228+
"The pool has moved to a new location while it was not closed properly");
234229
return 1;
235230
}

src/common/shutdown_state.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/* SPDX-License-Identifier: BSD-3-Clause */
22
/* Copyright 2017-2020, Intel Corporation */
3+
/* Copyright 2025, Hewlett Packard Enterprise Development LP */
34

45
/*
56
* shutdown_state.h -- unsafe shudown detection
@@ -23,6 +24,9 @@ struct shutdown_state {
2324
uint64_t checksum;
2425
};
2526

27+
#define FLUSH_SDS(sds, rep) \
28+
if ((rep) != NULL) os_part_deep_common(rep, 0, sds, sizeof(*(sds)), 1)
29+
2630
int shutdown_state_init(struct shutdown_state *sds, struct pool_replica *rep);
2731
int shutdown_state_add_part(struct shutdown_state *sds, int fd,
2832
struct pool_replica *rep);
@@ -34,6 +38,12 @@ void shutdown_state_clear_dirty(struct shutdown_state *sds,
3438
int shutdown_state_check(struct shutdown_state *curr_sds,
3539
struct shutdown_state *pool_sds, struct pool_replica *rep);
3640

41+
void shutdown_state_reinit(struct shutdown_state *curr_sds,
42+
struct shutdown_state *pool_sds, struct pool_replica *rep);
43+
44+
void shutdown_state_checksum(struct shutdown_state *sds,
45+
struct pool_replica *rep);
46+
3747
#ifdef __cplusplus
3848
}
3949
#endif

src/common/shutdown_state_helper.c

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
/* Copyright 2025, Hewlett Packard Enterprise Development LP */
3+
4+
/*
5+
* shutdown_state_helper.c -- implementation of shutdown_state_reinit
6+
*/
7+
8+
#include "out.h"
9+
#include "os_deep.h"
10+
#include "set.h"
11+
12+
/*
13+
* shutdown_state_reinit -- reinitializes shutdown_state struct
14+
*/
15+
void
16+
shutdown_state_reinit(struct shutdown_state *curr_sds,
17+
struct shutdown_state *pool_sds, struct pool_replica *rep)
18+
{
19+
LOG(3, "curr_sds %p, pool_sds %p", curr_sds, pool_sds);
20+
shutdown_state_init(pool_sds, rep);
21+
pool_sds->uuid = htole64(curr_sds->uuid);
22+
pool_sds->usc = htole64(curr_sds->usc);
23+
pool_sds->dirty = 0;
24+
25+
FLUSH_SDS(pool_sds, rep);
26+
27+
shutdown_state_checksum(pool_sds, rep);
28+
}

src/test/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ OTHER_TESTS = \
148148
util_poolset_size\
149149
util_ravl\
150150
util_sds\
151+
util_sds_check\
151152
util_uuid_generate\
152153
util_vec\
153154
util_vecq\

src/test/Makefile.inc

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ OBJS +=\
246246
$(TOP)/src/nondebug/common/pool_hdr.o\
247247
$(TOP)/src/nondebug/common/set.o\
248248
$(TOP)/src/nondebug/common/shutdown_state.o\
249+
$(TOP)/src/nondebug/common/shutdown_state_helper.o\
249250
$(TOP)/src/nondebug/common/util.o\
250251
$(TOP)/src/nondebug/common/util_posix.o\
251252
$(TOP)/src/nondebug/common/uuid.o\
@@ -287,6 +288,7 @@ OBJS +=\
287288
$(TOP)/src/debug/common/pool_hdr.o\
288289
$(TOP)/src/debug/common/set.o\
289290
$(TOP)/src/debug/common/shutdown_state.o\
291+
$(TOP)/src/debug/common/shutdown_state_helper.o\
290292
$(TOP)/src/debug/common/uuid.o\
291293
$(TOP)/src/debug/common/uuid_linux.o\
292294
$(TOP)/src/debug/libpmem2/pmem2_utils.o\

src/test/core_log_max/call_all.c.generated

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,13 @@ call_all_ERR_WO_ERRNO(void)
181181
// src/common/set_badblocks.c
182182
ERR_WO_ERRNO("part file contains bad blocks -- '%s'", _s);
183183
// src/common/shutdown_state.c
184-
ERR_WO_ERRNO("cannot read uuid of %d", _d);
185-
// src/common/shutdown_state.c
186-
ERR_WO_ERRNO("an ADR failure was detected, the pool might be corrupted");
184+
ERR_WO_ERRNO("%s, the pool might be corrupted.", _s);
187185
// src/common/shutdown_state.c
188186
ERR_WO_ERRNO("Cannot read unsafe shutdown count. For more information please check https://github.com/pmem/pmdk/issues/4207");
189187
// src/common/shutdown_state.c
190188
ERR_WO_ERRNO("cannot read uuid of %d", _d);
189+
// src/common/shutdown_state.c
190+
ERR_WO_ERRNO("cannot read uuid of %d", _d);
191191
// src/libpmem/libpmem.c
192192
ERR_WO_ERRNO("libpmem major version mismatch (need %u, found %u)", _u, _u);
193193
// src/libpmem/libpmem.c
@@ -462,11 +462,13 @@ call_all_CORE_LOG_WARNING(void)
462462
// src/common/set.c
463463
CORE_LOG_WARNING("file permissions changed during pool initialization, file: %s (%o)", _s, _u);
464464
// src/common/shutdown_state.c
465-
CORE_LOG_WARNING("incorrect checksum - SDS will be reinitialized");
465+
CORE_LOG_WARNING("Enabling ADR failure detection, assuming pool consistency up to this point.");
466+
// src/common/shutdown_state.c
467+
CORE_LOG_WARNING("The pool was not opened/closed properly - reinitializing ADR failure detection.");
466468
// src/common/shutdown_state.c
467-
CORE_LOG_WARNING("the pool was not closed - SDS will be reinitialized");
469+
CORE_LOG_WARNING("The ADR failure was detected but the pool was closed properly - reinitializing ADR failure detection.");
468470
// src/common/shutdown_state.c
469-
CORE_LOG_WARNING("an ADR failure was detected but the pool was closed - SDS will be reinitialized");
471+
CORE_LOG_WARNING("The ADR failure was detected but the pool was closed properly - reinitializing ADR failure detection.");
470472
// src/libpmemobj/heap.c
471473
CORE_LOG_WARNING("failed to allocate memory block runtime tracking info");
472474
// src/libpmemobj/heap.c

src/test/core_log_max/core_log_max.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// SPDX-License-Identifier: BSD-3-Clause
22
/* Copyright 2024, Intel Corporation */
3+
/* Copyright 2025, Hewlett Packard Enterprise Development LP */
34

45
/*
56
* core_log_max.c -- unit test to verify max size of log buffers
@@ -119,7 +120,7 @@ test_ERR_W_ERRNO(const struct test_case *tc, int argc, char *argv[])
119120
return NO_ARGS_CONSUMED;
120121
}
121122

122-
#define TOTAL_MESSAGE_NUM_EXPECTED 213
123+
#define TOTAL_MESSAGE_NUM_EXPECTED 214
123124
static int Max_message_len = 0;
124125
static int Total_message_num = 0;
125126
static char The_longest_message[BIG_BUF_SIZE];

src/test/util_sds/Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# SPDX-License-Identifier: BSD-3-Clause
22
# Copyright 2015-2020, Intel Corporation
3+
# Copyright 2025, Hewlett Packard Enterprise Development LP
34

45
#
56
# src/test/util_sds/Makefile -- shutdown_tests unit test
@@ -11,6 +12,7 @@ vpath %.c $(TOP)/src/common/
1112
TARGET = util_sds
1213
OBJS = util_sds.o\
1314
shutdown_state.o\
15+
shutdown_state_helper.o\
1416
ut_pmem2_config.o\
1517
ut_pmem2_source.o\
1618
ut_pmem2_utils.o

src/test/util_sds/grep1.log.match

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<libpmem2>: <2> [shutdown_state.c:$(N) shutdown_state_check] Enabling ADR failure detection, assuming pool consistency up to this point.

0 commit comments

Comments
 (0)