Skip to main content

Error with Geolocation plugin

2 weeks 6 days ago #100000474 by earmaster
-- url of the page with the problem -- : www.earmaster.com
-- HikaShop version -- : 6.6.0
-- Joomla version -- : 5.4.9 Stable
-- PHP version -- : 8.5.12
-- Browser(s) name and version -- : Chrome 156.0
-- Error-message(debug-mod must be tuned on) -- : Lock wait timeout exceeded; try restarting transaction

We regularly got this unhandled exception on ordinary front-end pages, often non-shop pages visited by new visitors:
Code:
Joomla\Database\Exception\ExecutionFailureException Lock wait timeout exceeded; try restarting transaction #2 plugins/system/hikashopgeolocation/hikashopgeolocation.php(327): JFalangDatabase->execute() #3 plugins/system/hikashopgeolocation/hikashopgeolocation.php(185): plgSystemHikashopgeolocation->getZone() #4 plugins/system/hikashopgeolocation/hikashopgeolocation.php(86): plgSystemHikashopgeolocation->process()

Each occurrence means a visitor waits about 50 seconds and then gets an error page.

Cause

Line 327 is the cleanup query at the end of getZone():
Code:
DELETE FROM #__hikashop_geolocation WHERE geolocation_type = 'visit' AND geolocation_created < (now - 1 year)

This runs for every visitor without an existing session: every new visitor, and every bot or client that doesn't keep cookies. The table only has an index on (geolocation_type, geolocation_ref_id). So the DELETE has to scan and lock every "visit" row, not just the expired ones. On our site that's about 1.87 million rows. The IP lookup in the same function has no index on geolocation_ip either. When several new visitors arrive at the same time, these queries queue behind each other until one times out.

Workaround we applied

Adding two indexes solved it for us:
Code:
ALTER TABLE #__hikashop_geolocation   ADD INDEX idx_type_created (geolocation_type, geolocation_created),   ADD INDEX idx_ip (geolocation_ip(45)),   ALGORITHM=INPLACE, LOCK=NONE;

EXPLAIN confirms the DELETE now uses idx_type_created.

Suggestions
  1. Add these indexes (or equivalent ones) to the table definition and the update script, so they survive updates and other sites get them too.
  2. Run the purge occasionally, for example once a day or through a scheduled task, instead of on every new visitor's first page view.
  3. Rows with geolocation_type = 'bot' are never purged. We have about 245,000 of them after a year, and the number keeps growing.
  4. Possible typo: a few lines above the DELETE in getZone(), if(empty($zones)) looks like it should be if(!empty($zones)). In practice the fallback query below it usually finds the zone anyway, but it's worth checking.

Please Log in or Create an account to join the conversation.

2 weeks 6 days ago #100000477 by nicolas
Hi,

Thank you for the detailed analysis: it is correct, and we were able to reproduce the exact error on a test site.

The geolocation plugin looked up the visitor's IP address and purged the old entries of its table on the first page of each new visitor, and both queries had to read the whole table because it had no suitable index. The purge locks every row it reads, so as soon as another request held a lock on any row of that table, the next new visitor waited until MySQL gave up after 50 seconds with "Lock wait timeout exceeded".

This is fixed in the latest build of HikaShop 6.7.1, available now through Joomla's updater or from your account on our website, to install over your current HikaShop (your settings are kept):

- the update adds an index on the IP address and one on the type and creation date of the geolocation table, and the database check of HikaShop adds them too if they are missing;

- the purge now runs at most once a day instead of on every new visitor's first page, and it also removes the old "bot" entries, which were never purged before;

- if the purge ever fails, the visitor gets the page normally and the purge is simply tried again the next day;

- the "if(empty($zones))" you spotted is corrected to "if(!empty($zones))".

Since the update creates its own indexes (named geolocation_type_created and geolocation_ip), the two you added by hand become duplicates that only slow down the writes to that table. Once the update is installed, you can remove yours with: ALTER TABLE #__hikashop_geolocation DROP INDEX idx_type_created, DROP INDEX idx_ip; (replace #__ with your table prefix).

The first purge after the update will remove all the bot entries older than the retention period at once (about a year by default), so that one may take a moment on your table; the following ones only remove a day's worth of entries.
The following user(s) said Thank You: earmaster

Please Log in or Create an account to join the conversation.

Time to create page: 0.058 seconds
Powered by Kunena Forum