Improve journal file error logging

When the monitor loads the server state journal, it now correctly logs all
OS errors.
This commit is contained in:
Markus Mäkelä
2017-03-17 12:31:19 +02:00
parent 85b8a963bd
commit 40b5e627a2

View File

@ -62,7 +62,8 @@ static bool rename_tmp_file(const char *src)
if (rename(src, dest) == -1) if (rename(src, dest) == -1)
{ {
rval = false; rval = false;
MXS_ERROR("Failed to rename '%s' to '%s'", src, dest); MXS_ERROR("Failed to rename journal file '%s' to '%s': %d, %s",
src, dest, errno, mxs_strerror(errno));
} }
return rval; return rval;
@ -86,7 +87,11 @@ static FILE* open_tmp_file(MXS_MONITOR *monitor, char *path)
if (nbytes < PATH_MAX - sizeof(filename) && mxs_mkdir_all(path, 0744)) if (nbytes < PATH_MAX - sizeof(filename) && mxs_mkdir_all(path, 0744))
{ {
strcat(path, filename); strcat(path, filename);
rval = fopen(path, "wb");
if ((rval = fopen(path, "wb")) == NULL)
{
MXS_ERROR("Failed to open file '%s': %d, %s", path, errno, mxs_strerror(errno));
}
} }
return rval; return rval;
@ -173,8 +178,7 @@ static FILE* open_data_file(MXS_MONITOR *monitor, char *path)
{ {
if ((rval = fopen(path, "rb")) == NULL && errno != ENOENT) if ((rval = fopen(path, "rb")) == NULL && errno != ENOENT)
{ {
MXS_ERROR("Failed to open persisted server states: %d, %s", MXS_ERROR("Failed to open journal file: %d, %s", errno, mxs_strerror(errno));
errno, mxs_strerror(errno));
} }
} }
@ -267,7 +271,7 @@ static bool process_data_file(MXS_MONITOR *monitor, const char *data, const char
/** All values contain a null terminated string */ /** All values contain a null terminated string */
if (!has_null_terminator(ptr, crc_ptr)) if (!has_null_terminator(ptr, crc_ptr))
{ {
MXS_ERROR("Possible corrupted data file (no null terminator found), ignoring persisted states."); MXS_ERROR("Possible corrupted journal file (no null terminator found). Ignoring.");
return false; return false;
} }
@ -285,7 +289,7 @@ static bool process_data_file(MXS_MONITOR *monitor, const char *data, const char
break; break;
default: default:
MXS_ERROR("Possible corrupted data file (unknown stored value), ignoring persisted states."); MXS_ERROR("Possible corrupted journal file (unknown stored value). Ignoring.");
return false; return false;
} }
ss_dassert(prevptr != ptr); ss_dassert(prevptr != ptr);
@ -340,7 +344,7 @@ void store_server_backup(MXS_MONITOR *monitor)
} }
else else
{ {
MXS_ERROR("Failed to write backup data to disk: %d, %s", MXS_ERROR("Failed to write journal data to disk: %d, %s",
errno, mxs_strerror(errno)); errno, mxs_strerror(errno));
} }
fclose(file); fclose(file);
@ -357,9 +361,10 @@ void load_server_backup(MXS_MONITOR *monitor)
if (file) if (file)
{ {
uint32_t size = 0; uint32_t size = 0;
size_t bytes = fread(&size, 1, MMB_LEN_BYTES, file);
ss_dassert(sizeof(size) == MMB_LEN_BYTES); ss_dassert(sizeof(size) == MMB_LEN_BYTES);
if (fread(&size, 1, MMB_LEN_BYTES, file) == MMB_LEN_BYTES) if (bytes == MMB_LEN_BYTES)
{ {
/** Payload contents: /** Payload contents:
* *
@ -369,7 +374,7 @@ void load_server_backup(MXS_MONITOR *monitor)
*/ */
char *data = (char*)MXS_MALLOC(size); char *data = (char*)MXS_MALLOC(size);
if (fread(data, 1, size, file) == size) if (data && (bytes = fread(data, 1, size, file)) == size)
{ {
if (*data == MMB_SCHEMA_VERSION) if (*data == MMB_SCHEMA_VERSION)
{ {
@ -379,26 +384,46 @@ void load_server_backup(MXS_MONITOR *monitor)
if (process_data_file(monitor, data + MMB_LEN_SCHEMA_VERSION, if (process_data_file(monitor, data + MMB_LEN_SCHEMA_VERSION,
data + size - MMB_LEN_CRC32)) data + size - MMB_LEN_CRC32))
{ {
MXS_NOTICE("Loaded server states from backup file: %s", path); MXS_NOTICE("Loaded server states from journal file: %s", path);
} }
} }
else else
{ {
MXS_ERROR("CRC32 mismatch in persisted server state file. " MXS_ERROR("CRC32 mismatch in journal file. Ignoring.");
"Ignoring persisted server states.");
} }
} }
else else
{ {
MXS_ERROR("Unknown backup schema version: %d", (int)*data); MXS_ERROR("Unknown journal schema version: %d", (int)*data);
} }
} }
else if (data)
{
if (ferror(file))
{
MXS_ERROR("Failed to read journal file: %d, %s", errno, mxs_strerror(errno));
}
else else
{ {
MXS_ERROR("Failed to read persisted server states."); MXS_ERROR("Failed to read journal file: Expected %u bytes, "
"read %lu bytes.", size, bytes);
}
} }
MXS_FREE(data); MXS_FREE(data);
} }
else
{
if (ferror(file))
{
MXS_ERROR("Failed to read journal file length: %d, %s",
errno, mxs_strerror(errno));
}
else
{
MXS_ERROR("Failed to read journal file length: Expected %d bytes, "
"read %lu bytes.", MMB_LEN_BYTES, bytes);
}
}
fclose(file); fclose(file);
} }
@ -414,6 +439,6 @@ void remove_server_backup(MXS_MONITOR *monitor)
} }
else else
{ {
MXS_ERROR("Path to monitor data directory is too long."); MXS_ERROR("Path to monitor journal directory is too long.");
} }
} }