Page MenuHomePhabricator

24 hour period per log entry calculated incorrectly
Closed, ResolvedPublicBUG REPORT

Description

What is the problem?

When we calculate the timestamp to check that a log hasn't been created in the last 24 hours, I think the calculation we are doing is incorrect.

It can lead to quite inconsistent results. In some cases we are recording more than one log entry per 24 hour period. It might also be possible that we might see less than 1 log entry per 24 hour period (although I have not seen this yet).

https://gerrit.wikimedia.org/r/plugins/gitiles/mediawiki/extensions/IPInfo/+/02f7561b060ab0f97e37e78e85ae9162ca9262f9/src/Logging/Logger.php#206:

$timestamp = (int)wfTimestampNow() - $this->delay;

(int)wfTimestampNow() returns a MW timestamp YYYYMMDDhhmmss, e.g. 20220304102515.

$this->delay is in seconds, e.g. 86400.

I guess we need to convert (int)wfTimestampNow() to seconds first. Or perhaps we have a MW way of doing this.

Steps to reproduce problem

If you have access to the database query logs:

  1. Lookup IPInfo for any IP
  2. Look in the logs and find a line that starts [DBQuery] Wikimedia\Rdbms\Database::selectRowCount
  3. On that line is a section of the SQL query like (log_timestamp > '20220304015702')

Expected behavior: The timestamp should be exactly 24 hours ago.
Observed behavior: The timestamp isn't exactly 24 hours ago.

Environment

Wiki(s): local docker IP Info 0.0.0 (02f7561) 14:01, 3 March 2022.

Event Timeline

If you want a unix timestamp (to subtract the delay) use (int)wfTimestamp() or (int)wfTimestamp( TS_UNIX ).

But the code around the timestamp handling seems to have more problems or get improvements - https://gerrit.wikimedia.org/g/mediawiki/extensions/IPInfo/+/02f7561b060ab0f97e37e78e85ae9162ca9262f9/src/Logging/Logger.php#206

It acquire an actor id for read. If the function has to acquire a new id (because there is no) then the next select on the logging table would not find anything, because there is nothing, findActorId is enough here.

There is no need to select the whole and exact COUNT(*) to only find there is nothing (code compares with 0). Just check if here is a match for one row and treat no match as false (Using selectRow with '1' - https://codesearch.wmcloud.org/search/?q=selectRow%5Cs*%5C(%5B%5E%2C%5D%2B%2C%5Cs*%271%27&i=nope&files=%5C.php&excludeFiles=&repos=).
LIKE queries with % at the begin are not good from performance view. Is the check for the level necessary? It seems that log entry always has the level set. This is not a big deal, because there are so much fields in the WHERE this does not make performance worse.

Tchanders added subscribers: • Niharika, Tchanders.

Working on this now, since it should probably be fixed before testwiki deployment.

(Letting @Niharika know too.)

Thanks @Umherirrender - I've moved the non-timestamp suggestions to T303047

Change 768052 had a related patch set uploaded (by Tchanders; author: Tchanders):

[mediawiki/extensions/IPInfo@master] Get the current timestamp in seconds, in Logger::debouncedLog

https://gerrit.wikimedia.org/r/768052

Change 768052 merged by jenkins-bot:

[mediawiki/extensions/IPInfo@master] Get the current timestamp in seconds, in Logger::debouncedLog

https://gerrit.wikimedia.org/r/768052

I looked up a load of revisions using the IPInfo API. I checked that only one log entry was recorded per IP.

I then looked at the query the IPInfo Logger class was making in the debug logs. I checked that the timestamp in the query (log_timestamp > '<timestamp>') was roughly 24 hours ago. This was the case for all the 20-30 queries I looked at.

The testing was done on my local docker, which uses sqlite. I also tested another local install which uses mariadb. The timestamp in the queries it was making were also roughly 24 hours ago (although I only looked at one).

I didn't test on beta because a) I don't know how to look at the queries the application is making on beta and b) beta is having a few reliability issues at the moment (T302699).

Test environment: local docker IP Info 0.0.0 (0f63a8b) 07:11, 7 March 2022.