Every once in a while you really want to cleanup the token table of the Keystone database. A couple of weeks ago while backuping my cloud controller I noticed that the backup of the Keystone database was longer than the other databases. After that, I checked the size of the dump (compressed) 60MB. Hummm but there is almost nothing in the Keystone database: users, tenants… wait.. could it be TOKENS?!
The token validity is manage via the following options in keystone.conf:
[token]
driver = keystone.token.backends.sql.Token
# Amount of time a token should remain valid (in seconds)
expiration = 86400
One option could be to use different backend to store the tokens:
The keystone.token.backends.memcache, Memcached storage backend
The keystone.token.backends.kvs, Key Value storage backend
I will prefer another backend to store the tokens in order to make database dump shorter and smaller. I’m not quite sure if memcache is a good candidat though. This could make things harder for some reasons like:
Does the token remain forever in memcache?
Cache consistency, if a server crash
Makes the setup more complex, try to achieve a replicated memcache
Note: I volontary let a retention of 2 days in the command since I work with days and not with hours. It’s not always day per day, thus some token could overlap and be valid. So 2 days are fine.
#!/bin/bash# Purpose of the script# Everytime a service wants to be do 'something' it has to retrieve an autentication token# Nova/Glance/Cinder services are manage by Pacemaker and monitor functions (from the RA) ask for a token every 10 sec# There is no cleanup procedure nor periodical task running to delete expire tokenmysql_user=keystone
mysql_password=********
mysql_host=mysql=$(which mysql)logger -t keystone-cleaner "Starting Keystone 'token' table cleanup"logger -t keystone-cleaner "Starting token cleanup"mysql -u${mysql_user} -p${mysql_password} -h${mysql_host} -e 'USE keystone ; DELETE FROM token WHERE NOT DATE_SUB(CURDATE(),INTERVAL 2 DAY) <= expires;'valid_token=$($mysql -u${mysql_user} -p${mysql_password} -h${mysql_host} -e 'USE keystone ; SELECT * FROM token;' | wc -l)logger -t keystone-cleaner "Finishing token cleanup, there is still $valid_token valid tokens..."exit 0
$ sudo telnet 127.0.0.1 11211
Trying 127.0.0.1...
Connected to 127.0.0.1.
Escape character is '^]'.
stats items
STAT items:9:number 4
STAT items:9:age 314
STAT items:9:evicted 0
STAT items:9:evicted_nonzero 0
STAT items:9:evicted_time 0
STAT items:9:outofmemory 0
STAT items:9:tailrepairs 0
STAT items:9:reclaimed 0
STAT items:9:expired_unfetched 0
STAT items:9:evicted_unfetched 0
STAT items:10:number 1310
STAT items:10:age 511
STAT items:10:evicted 0
STAT items:10:evicted_nonzero 0
STAT items:10:evicted_time 0
STAT items:10:outofmemory 0
STAT items:10:tailrepairs 0
STAT items:10:reclaimed 0
STAT items:10:expired_unfetched 0
STAT items:10:evicted_unfetched 0
END
quit
Connection closed by foreign host.
You should see the STAT items:10:number growing and growing.