Skip to content

Commit

Permalink
HPCC-33142 Distinguish between id and sequency number in SysInfoLogger
Browse files Browse the repository at this point in the history
Use id or msgid for unique SysInfoLogger message identifier
Use SeqNum for the sequence number used to the generated ensure id/msgid is unique

Signed-off-by: Shamser Ahmed <[email protected]>
  • Loading branch information
shamser committed Dec 23, 2024
1 parent 803672b commit 968121e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 26 deletions.
61 changes: 35 additions & 26 deletions dali/base/sysinfologger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,42 +23,49 @@
#define SYS_INFO_VERSION "1.0"

#define SYS_INFO_ROOT "/SysLogs"
#define ATTR_NEXTID "@nextId"
#define ATTR_NEXTSEQN "@nextSeqNum"
#define ATTR_VERSION "@version"
#define MSG_NODE "msg"
#define ATTR_ID "@id"
#define ATTR_SEQNUM "@seqNum"
#define ATTR_TIMESTAMP "@ts"
#define ATTR_AUDIENCE "@audience"
#define ATTR_CLASS "@class"
#define ATTR_CODE "@code"
#define ATTR_HIDDEN "@hidden"
#define ATTR_SOURCE "@source"

static constexpr unsigned __int64 nonSysInfoLogMsgMask = 0x80000000;

static void extractDate(timestamp_type ts, unsigned & year, unsigned & month, unsigned & day)
{
CDateTime timeStamp;
timeStamp.setTimeStamp(ts);
timeStamp.getDate(year, month, day);
}

static unsigned __int64 makeMessageId(unsigned year, unsigned month, unsigned day, unsigned id)
unsigned __int64 makeMessageId(unsigned year, unsigned month, unsigned day, unsigned seqN, bool nonSysInfoLogMsg)
{
return id<<21 | year<<9 | month<<5 | day;
// bits 0-4 = day, bits 5-8 = month, bits 9-22 = year
// bit 23-30 unused
// bit 31 = (flag) unset means SysInfoLogger managed message, set means message managed elsewhere
// bits 32-63 = sequence number
return seqN<<32 | (nonSysInfoLogMsg?nonSysInfoLogMsgMask:0) | year<<9 | month<<5 | day;
}

static unsigned __int64 makeMessageId(unsigned __int64 ts, unsigned id)
unsigned __int64 makeMessageId(unsigned __int64 ts, unsigned seqN, bool nonSysInfoLogMsg)
{
unsigned year, month, day;
extractDate(ts, year, month, day);
return makeMessageId(year, month, day, id);
return makeMessageId(year, month, day, seqN, nonSysInfoLogMsg);
}

static void decodeMessageId(unsigned __int64 msgId, unsigned & year, unsigned & month, unsigned & day, unsigned & id)
static bool decodeMessageId(unsigned __int64 msgId, unsigned & year, unsigned & month, unsigned & day, unsigned & seqn)
{
day = msgId & 0x1F;
month = (msgId>>5) & 0x0F;
year = (msgId>>9) & 0xFFF;
id = (msgId>>21);
year = (msgId>>9) & 0x3FFF;
seqn = msgId>>32;
return msgId & nonSysInfoLogMsgMask;
}

class CSysInfoLoggerMsg : implements ISysInfoLoggerMsg
Expand All @@ -77,10 +84,10 @@ class CSysInfoLoggerMsg : implements ISysInfoLoggerMsg
{
msgPtree.setown(createPTree(MSG_NODE));
}
CSysInfoLoggerMsg(unsigned id, const LogMsgCategory & cat, LogMsgCode code, const char * source, const char * msg, timestamp_type ts, bool hidden)
CSysInfoLoggerMsg(unsigned seqn, const LogMsgCategory & cat, LogMsgCode code, const char * source, const char * msg, timestamp_type ts, bool hidden)
{
msgPtree.setown(createPTree(MSG_NODE));
msgPtree->setPropInt64(ATTR_ID, id);
msgPtree->setPropInt64(ATTR_SEQNUM, seqn);
msgPtree->setPropBool(ATTR_HIDDEN, hidden);
msgPtree->setPropInt64(ATTR_TIMESTAMP, ts);
msgPtree->setPropInt(ATTR_CODE, code);
Expand Down Expand Up @@ -130,7 +137,7 @@ class CSysInfoLoggerMsg : implements ISysInfoLoggerMsg
}
virtual unsigned __int64 queryLogMsgId() const override
{
return makeMessageId(queryTimeStamp(), msgPtree->getPropInt64(ATTR_ID, 0));
return makeMessageId(queryTimeStamp(), msgPtree->getPropInt64(ATTR_SEQNUM, 0));
}
virtual const char * queryMsg() const override
{
Expand All @@ -146,8 +153,8 @@ class CSysInfoLoggerMsg : implements ISysInfoLoggerMsg
{
unsigned year, month, day;
extractDate(queryTimeStamp(), year, month, day);
unsigned __int64 id = msgPtree->getPropInt64(ATTR_ID, 0);
xpath.appendf("m%04u%02u/d%02u/" MSG_NODE "[" ATTR_ID "='%" I64F "u']", year, month, day, id);
unsigned __int64 seqn = msgPtree->getPropInt64(ATTR_SEQNUM, 0);
xpath.appendf("m%04u%02u/d%02u/" MSG_NODE "[" ATTR_SEQNUM "='%" I64F "u']", year, month, day, seqn);
return xpath;
}
IPropertyTree * getTree()
Expand Down Expand Up @@ -220,14 +227,16 @@ class CSysInfoLoggerMsgFilter : public CSimpleInterfaceOf<ISysInfoLoggerMsgFilte
}
virtual void setMatchMsgId(unsigned __int64 msgId) override
{
unsigned year, month, day, id;
decodeMessageId(msgId, year, month, day, id);
if (year==0 || month==0 || day==0 || id==0)
throw makeStringExceptionV(-1,"ISysInfoLoggerMsgFilter::setMatchMsgId invalid argument: %" I64F "u", msgId);
unsigned year, month, day, seqn;
bool nonSysInfoLogMsg = decodeMessageId(msgId, year, month, day, seqn);
if (nonSysInfoLogMsg)
throw makeStringExceptionV(-1, "Message id: %" I64F "u cannot be processed by SysInfoLogger", msgId);
if (year==0 || month==0 || day==0 || seqn==0)
throw makeStringExceptionV(-1,"ISysInfoLoggerMsgFilter::setMatchMsgId invalid message id: %" I64F "u", msgId);
matchEndYear = matchStartYear = year;
matchEndMonth = matchStartMonth = month;
matchEndDay = matchStartDay = day;
matchId = id;
matchId = seqn;
haveDateRange = false;
}
virtual void setDateRange(unsigned startYear, unsigned startMonth, unsigned startDay, unsigned endYear, unsigned endMonth, unsigned endDay) override
Expand Down Expand Up @@ -394,7 +403,7 @@ class CSysInfoLoggerMsgFilter : public CSimpleInterfaceOf<ISysInfoLoggerMsgFilte
if (matchClass!=MSGCLS_all)
xpath.appendf("[" ATTR_CLASS "='%s']", LogMsgClassToFixString(matchClass));
if (matchId)
xpath.appendf("[" ATTR_ID "='%u']", matchId);
xpath.appendf("[" ATTR_SEQNUM "='%u']", matchId);
if (matchTimeStamp)
xpath.appendf("[" ATTR_TIMESTAMP "='%" I64F "u']", matchTimeStamp);
return xpath;
Expand Down Expand Up @@ -501,10 +510,10 @@ unsigned __int64 logSysInfoError(const LogMsgCategory & cat, LogMsgCode code, co
throw makeStringExceptionV(-1, "logSysInfoLogger: unable to create connection to '%s'", SYS_INFO_ROOT);

IPropertyTree * root = conn->queryRoot();
unsigned id = root->getPropInt(ATTR_NEXTID, 1);
if (id==UINT_MAX) // wrap id to reuse id numbers (shouldn't wrap but no harm in doing this for safety)
id=1;
root->setPropInt(ATTR_NEXTID, id+1);
unsigned seqn = root->getPropInt(ATTR_NEXTSEQN, 1);
if (seqn==UINT_MAX) // wrap id to reuse id numbers (shouldn't wrap but no harm in doing this for safety)
seqn=1;
root->setPropInt(ATTR_NEXTSEQN, seqn+1);

StringBuffer xpath;
unsigned year, month, day;
Expand All @@ -515,10 +524,10 @@ unsigned __int64 logSysInfoError(const LogMsgCategory & cat, LogMsgCode code, co
throw makeStringExceptionV(-1, "logSysInfoLogger: unable to create connection to '%s'", xpath.str());
IPropertyTree * msgPT = connMsgRoot->queryRoot();

CSysInfoLoggerMsg sysInfoMsg(id, cat, code, source, msg, ts, false);
CSysInfoLoggerMsg sysInfoMsg(seqn, cat, code, source, msg, ts, false);
msgPT->setPropTree(nullptr, sysInfoMsg.getTree());
msgPT->setProp(".", msg); // previous setPropTree doesn't set the node value
return makeMessageId(ts, id);
return makeMessageId(ts, seqn);
}

unsigned updateMessage(IConstSysInfoLoggerMsgFilter * msgFilter, std::function<void (CSysInfoLoggerMsg &)> updateOp)
Expand Down
4 changes: 4 additions & 0 deletions dali/base/sysinfologger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,8 @@ SYSINFO_API unsigned deleteLogSysInfoMsg(IConstSysInfoLoggerMsgFilter * msgFilte
SYSINFO_API bool deleteLogSysInfoMsg(unsigned __int64 msgId, const char *source=nullptr);
SYSINFO_API unsigned deleteOlderThanLogSysInfoMsg(bool visibleOnly, bool hiddenOnly, unsigned year, unsigned month, unsigned day, const char *source=nullptr);

/* makeMessageId - Create a message id from date, sequence number and nonSysInfoLogMsg flag to uniquely identify a message
- nonSysInfoLogMsg flag is used to identify whether or not the message is managed by SysInfoLogger */
SYSINFO_API unsigned __int64 makeMessageId(unsigned __int64 ts, unsigned seqN, bool nonSysInfoLogMsg=false);
SYSINFO_API unsigned __int64 makeMessageId(unsigned year, unsigned month, unsigned day, unsigned seqN, bool nonSysInfoLogMsg=false);
#endif

0 comments on commit 968121e

Please sign in to comment.