Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions lib/records/P_RecCore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
#include "P_RecMessage.h"
#include "P_RecCore.h"

#include <fstream>

RecModeT g_mode_type = RECM_NULL;

//-------------------------------------------------------------------------
Expand Down Expand Up @@ -491,6 +493,27 @@ RecSetRecordCounter(const char *name, RecCounter rec_counter, RecSourceT source,
return RecSetRecord(RECT_NULL, name, RECD_COUNTER, &data, nullptr, source, lock, inc_version);
}

// check the version of the snap file to remove records.snap or not
static void
CheckSnapFileVersion(const char *path)
{
std::ifstream f(path, std::ios::binary);
if (f.good()) {
// get version, compare and remove
char data[VERSION_HDR_SIZE];
if (!f.read(data, VERSION_HDR_SIZE)) {
return;
}
if (data[0] != 'V' || data[1] != PACKAGE_VERSION[0] || data[2] != PACKAGE_VERSION[2] || data[3] != PACKAGE_VERSION[4] ||
data[4] != '\0') {
// not the right version found
if (remove(path) != 0) {
ink_warning("unable to remove incompatible snap file '%s'", path);
}
}
}
}

//-------------------------------------------------------------------------
// RecReadStatsFile
//-------------------------------------------------------------------------
Expand All @@ -506,6 +529,8 @@ RecReadStatsFile()
// lock our hash table
ink_rwlock_wrlock(&g_records_rwlock);

CheckSnapFileVersion(snap_fpath);

if ((m = RecMessageReadFromDisk(snap_fpath)) != nullptr) {
if (RecMessageUnmarshalFirst(m, &itr, &r) != REC_ERR_FAIL) {
do {
Expand Down
3 changes: 3 additions & 0 deletions lib/records/P_RecFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#define REC_HANDLE_INVALID -1
typedef int RecHandle;

static constexpr unsigned VERSION_HDR_SIZE = 5;
//-------------------------------------------------------------------------
// RecFile
//-------------------------------------------------------------------------
Expand All @@ -38,7 +39,9 @@ RecHandle RecFileOpenR(const char *file);
RecHandle RecFileOpenW(const char *file);
int RecFileClose(RecHandle h_file);
int RecFileRead(RecHandle h_file, char *buf, int size, int *bytes_read);
int RecSnapFileRead(RecHandle h_file, char *buf, int size, int *bytes_read);
int RecFileWrite(RecHandle h_file, char *buf, int size, int *bytes_written);
int RecSnapFileWrite(RecHandle h_file, char *buf, int size, int *bytes_written);
int RecFileGetSize(RecHandle h_file);
int RecFileExists(const char *file);
int RecFileSync(RecHandle h_file);
2 changes: 1 addition & 1 deletion lib/records/RecCore.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ register_record(RecT rec_type, const char *name, RecDataT data_type, RecData dat
RecRecord *r = nullptr;

// Metrics are restored from persistence before they are registered. In this case, when the registration arrives, we
// might find that they yave changed. For example, a metric might change it's type due to a software upgrade. Records
// might find that they have changed. For example, a metric might change it's type due to a software upgrade. Records
// must not flip between config and metrics, but changing within those classes is OK.
if (ink_hash_table_lookup(g_records_ht, name, (void **)&r)) {
if (REC_TYPE_IS_STAT(rec_type)) {
Expand Down
36 changes: 36 additions & 0 deletions lib/records/RecFile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
#include "P_RecDefs.h"
#include "P_RecUtils.h"

#include <array>

//-------------------------------------------------------------------------
// RecFileOpenR
//-------------------------------------------------------------------------
Expand Down Expand Up @@ -74,6 +76,20 @@ RecFileClose(RecHandle h_file)
return (close(h_file) == 0) ? REC_ERR_OKAY : REC_ERR_FAIL;
}

//-------------------------------------------------------------------------
// RecSnapFileRead
//-------------------------------------------------------------------------

int
RecSnapFileRead(RecHandle h_file, char *buf, int size, int *bytes_read)
{
if ((*bytes_read = ::pread(h_file, buf, size, VERSION_HDR_SIZE)) <= 0) {
*bytes_read = 0;
return REC_ERR_FAIL;
}
return REC_ERR_OKAY;
}

//-------------------------------------------------------------------------
// RecFileRead
//-------------------------------------------------------------------------
Expand All @@ -88,6 +104,26 @@ RecFileRead(RecHandle h_file, char *buf, int size, int *bytes_read)
return REC_ERR_OKAY;
}

//-------------------------------------------------------------------------
// RecSnapFileWrite
//-------------------------------------------------------------------------

int
RecSnapFileWrite(RecHandle h_file, char *buf, int size, int *bytes_written)
{
// First write the version byes for snap file
std::array<char, VERSION_HDR_SIZE> VERSION_HDR{{'V', PACKAGE_VERSION[0], PACKAGE_VERSION[2], PACKAGE_VERSION[4], '\0'}};
if (::write(h_file, VERSION_HDR.data(), VERSION_HDR_SIZE) < 0) {
return REC_ERR_FAIL;
}

if ((*bytes_written = ::pwrite(h_file, buf, size, VERSION_HDR_SIZE)) < 0) {
*bytes_written = 0;
return REC_ERR_FAIL;
}
return REC_ERR_OKAY;
}

//-------------------------------------------------------------------------
// RecFileWrite
//-------------------------------------------------------------------------
Expand Down
6 changes: 3 additions & 3 deletions lib/records/RecMessage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,12 @@ RecMessageReadFromDisk(const char *fpath)
if ((h_file = RecFileOpenR(fpath)) == REC_HANDLE_INVALID) {
goto Lerror;
}
if (RecFileRead(h_file, (char *)(&msg_hdr), sizeof(RecMessageHdr), &bytes_read) == REC_ERR_FAIL) {
if (RecSnapFileRead(h_file, (char *)(&msg_hdr), sizeof(RecMessageHdr), &bytes_read) == REC_ERR_FAIL) {
goto Lerror;
}
msg = (RecMessage *)ats_malloc((msg_hdr.o_end - msg_hdr.o_start) + sizeof(RecMessageHdr));
memcpy(msg, &msg_hdr, sizeof(RecMessageHdr));
if (RecFileRead(h_file, (char *)(msg) + msg_hdr.o_start, msg_hdr.o_end - msg_hdr.o_start, &bytes_read) == REC_ERR_FAIL) {
if (RecSnapFileRead(h_file, (char *)(msg) + msg_hdr.o_start, msg_hdr.o_end - msg_hdr.o_start, &bytes_read) == REC_ERR_FAIL) {
goto Lerror;
}

Expand Down Expand Up @@ -307,7 +307,7 @@ RecMessageWriteToDisk(RecMessage *msg, const char *fpath)

msg_size = sizeof(RecMessageHdr) + (msg->o_write - msg->o_start);
if ((h_file = RecFileOpenW(fpath)) != REC_HANDLE_INVALID) {
if (RecFileWrite(h_file, (char *)msg, msg_size, &bytes_written) == REC_ERR_FAIL) {
if (RecSnapFileWrite(h_file, (char *)msg, msg_size, &bytes_written) == REC_ERR_FAIL) {
RecFileClose(h_file);
return REC_ERR_FAIL;
}
Expand Down