未加星标

A Tale of Two Nows

字体大小 | |
[数据库(综合) 所属分类 数据库(综合) | 发布者 店小二04 | 时间 2016 | 作者 红领巾 ] 0人收藏点击收藏

I recently endeavored to use an analytics database called pipelindb to build a leaderboard type feature for a client. Pipelinedb is a fork-and-enhance project built on postgresql and most the of magic is presented to the developer in the form of a CONTINUOUS VIEW construct, which is similar to a regular relational table/view but has the ability to efficiently do sliding window queries on big-data data sets.

The sliding window query I needed was along the lines of "show me the players on team X with the highest score in the last day". I'm changing the subject matter here to make it generic, but my requirements were basically that. I had to keep track of daily, weekly, and all-time player scores and be able to find the top N players in combination with some WHERE clauses like WHERE team_id = 42 .

This was my first exposure to pipelinedb and working with my team and the docs, I was able to build the feature based on the following structures in pipelinedb:

a stream where rows are inserted as players score INSERT INTO score_stream (player_id, team_id, timestamp) VALUES (23, 47, '2016-10-13 20:08:17.505233+00'); a continuous view built upon that stream: CREATE CONTINUOUS VIEW score_by_team_1d AS SELECT player.player_id, team.team_id, count(*) AS score FROM score_stream ss JOIN players ON ss.player_id::integer = players.player_id JOIN players_teams pt ON players.player_id = pt.player_id WHERE ss.timestamp > current_timestamp - interval '1 day' GROUP BY pt.term_id, pt.player_id;

We got it built and deployed and all seemed to be well. But after a while, we started to have some doubts about the resulting data we were getting. The daily high score players weren't changing as much as we would have intuitively expected.

I dug into this a bit using some ad-hoc tooling, dumping results into spreadsheets, and comparing and I also started to see patterns that looked like instead of our sliding windows tracking scores over specific periods like daily and weekly, it looked like they were just accumulating total scores. But it was particularly weird because it wasn't all-time total scores, just totals since some particular date.

With this hypothesis in mind, I went back to the pipelinedb docs and did the classic slow, long facepalm when I found this clause:

PipelineDB exposes the current_date, current_time, and current_timestamp values to use within queries, but by design these don’t work with sliding-window queries because they remain constant within a transaction and thus don’t necessarily represent the current moment in time.

When we had prototyped our queries, they were based upon a function called clock_timestamp() which works properly with pipelindb continuous views. As we finalized the code and switched from proof-of-concept structures to our real structures, I changed that to current_timestamp based on my reading of the postgresql documentation. I thought that was the more appropriate function.

But it turns out I had this wrong. The current_timestamp had only ever evaluated to a single moment in time, specifically when we first launched the feature and ran the CREATE CONTINUOUS VIEW statement. Thus it wasn't computing daily scores, it was computing scores since that specific, fixed moment in the past. Our sliding windows weren't sliding. The query's notion of "now" was not always being re-evaluated to the current moment in time.

The proper fix involved "just" dropping the errant CONTINUOUS VIEW definition and recreating it with the only change being to use clock_timestamp() instead of current_timestamp . This also discarded our current data for 1 day and 1 week intervals, meaning we'd have to wait a week before we could rely on that data again.

However, the realities of production data meant this fix out would take the better part of 2 weeks. Compounding the problem was the fact that a feature such as this that requires careful testing of data results over relatively long periods of time (days, weeks) meant that a severe but easy-to-miss bug such as this could get through dev, QA, and product management and escape detection. It's also a challenge to write automated tests for this. It's not impossible, it's just a lot more involved that testing basic CRUD functionality.

本文数据库(综合)相关术语:系统安全软件

主题: TI
分页:12
转载请注明
本文标题:A Tale of Two Nows
本站链接:http://www.codesec.net/view/483999.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(综合) | 评论(0) | 阅读(30)