Send Unix timestamps to database in pgjsonb

The pgjsonb returner wants a database with a timestamp column using
the type TIMESTAMP WITH TIME ZONE. Despite this, it's inserted its
timestamps as a date-time-zone string. This means that the minion has
needed to waste time formatting the timetamp, and then PostgreSQL has
wasted even more time parsing it back into a Unix timestamp for
storage.

Also, the code has been buggy on Python 2, which doesn't properly
support the %z format specifier to strftime() and has therefore sent
timestamps using the wrong time zone.

Solve both these problems by having the minion retrieve a Unix
timestamp by calling time.time(), sending it unmolested to PostgreSQL,
and using the PostgreSQL function to_timestamp() to store it.

Closes #44544.
This commit is contained in:
Karl-Johan Karlsson 2017-11-16 10:14:12 +01:00
parent 91d46d4cfc
commit 15c445e6b9
No known key found for this signature in database
GPG key ID: 1EAA4E8619CB6AC1

View file

@ -254,14 +254,14 @@ def returner(ret):
with _get_serv(ret, commit=True) as cur:
sql = '''INSERT INTO salt_returns
(fun, jid, return, id, success, full_ret, alter_time)
VALUES (%s, %s, %s, %s, %s, %s, %s)'''
VALUES (%s, %s, %s, %s, %s, %s, to_timestamp(%s))'''
cur.execute(sql, (ret['fun'], ret['jid'],
psycopg2.extras.Json(ret['return']),
ret['id'],
ret.get('success', False),
psycopg2.extras.Json(ret),
time.strftime('%Y-%m-%d %H:%M:%S %z', time.localtime())))
time.time()))
except salt.exceptions.SaltMasterError:
log.critical('Could not store return with pgjsonb returner. PostgreSQL server unavailable.')
@ -278,9 +278,9 @@ def event_return(events):
tag = event.get('tag', '')
data = event.get('data', '')
sql = '''INSERT INTO salt_events (tag, data, master_id, alter_time)
VALUES (%s, %s, %s, %s)'''
VALUES (%s, %s, %s, to_timestamp(%s))'''
cur.execute(sql, (tag, psycopg2.extras.Json(data),
__opts__['id'], time.strftime('%Y-%m-%d %H:%M:%S %z', time.localtime())))
__opts__['id'], time.time()))
def save_load(jid, load, minions=None):