Bug #71157 Expire time discrepancy
Submitted: 16 Dec 2013 21:11 Modified: 2 Jan 2014 19:48
Reporter: Aaron Morand Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: Memcached Severity:S2 (Serious)
Version:5.6.14 OS:Linux (CentOS 5.6 Ubuntu 12.04)
Assigned to: CPU Architecture:Any
Tags: expiry, memcached plugin

[16 Dec 2013 21:11] Aaron Morand
Description:
When setting a key with a specific expire time, the value stored in MySQL does not match what was set.

Memcached appears to correctly expire the key, so the problem seems to be related to how MySQL stores this value.

How to repeat:
This assumes the Memcached plugin is already setup and working using the database memcached_test and the table memstash.
CREATE DATABASE `memcached_test`;
CREATE TABLE `memstash` (
  `k` varchar(255) NOT NULL,
  `v` longtext,
  `flags` int(11) DEFAULT NULL,
  `cas_column` bigint(20) unsigned DEFAULT NULL,
  `expires` bigint(20) unsigned DEFAULT NULL,
  PRIMARY KEY (`k`),
  KEY `v` (`v`(255))
 );

Create a test key, add it via Memcached, execute date +'%s', query MySQL for the expire time of the added key and compare the time difference :

# echo "Y" > testkey
# memcp --verbose --debug --servers=localhost --expire=120 ; date +'%s'
op: set
source file: testkey
length: 1
key: testkey
flags: 0
expires: 288
1387226603
# mysql memcached_test -e "SELECT expires FROM memstash WHERE k = 'testkey'"
+------------+
| expires    |
+------------+
| 1387226891 |
+------------+

The erroneous expire time is more evident when used with a unix timestamp.

# memcp --verbose --debug --servers=localhost --expire=1390671296 testkey ; date +'%s'
op: set
source file: testkey
length: 1
key: testkey
flags: 0
expires: 84027052694
1387226782
# mysql memcached_ttfb -e "SELECT expires FROM memstash WHERE k = 'testkey'"
+------------+
| expires    |
+------------+
| 2422674070 |
+------------+

Suggested fix:
Have the saved value reflect the actual unix timestamp.
[2 Jan 2014 19:48] Aaron Morand
This is a bug with memcp and not MySQL.  I have verified this with a perl script using Memcached::libmemcached (by Tim Bunce) which is essentially the C library in perl form and I do not have this issue there.