Skip to content

Commit

Permalink
revised SQL query returning next pool metadata to fetch
Browse files Browse the repository at this point in the history
  There are two fundamentals change here:

  a) This now does a LEFT JOIN on the fetch attempts table on BOTH the metadata_hash and
  metadata_url. Before, we would discard metadata based solely on their metadata_hash. Now,
  we select on the joined table directly and keep all pool metadata with either no fetch attempts,
  or a fetch_after that is prior to the current datetime.

  b) It now _sorts_ the result based on their fetch_after date, the most "urgent" first. This is to cope
  with the arbitrary batch limit of 100 pools and make sure that all metadata are eventually fetched.
  Before, we could potentially retry fetching the same metadata over and over again until they expire.
  • Loading branch information
KtorZ committed Aug 3, 2020
1 parent d51b74d commit fccb250
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions lib/core/src/Cardano/Pool/DB/Sqlite.hs
Expand Up @@ -268,22 +268,32 @@ newDBLayer trace fp timeInterpreter = do
let fetchAttempts = tableName (DBField PoolFetchAttemptsMetadataHash)
let metadata = tableName (DBField PoolMetadataHash)
let query = T.unwords
[ "SELECT"
, metadataUrl, ",", metadataHash
, "FROM", registrations
[ "SELECT", "a." <> metadataUrl, ",", "a." <> metadataHash
, "FROM", registrations, "AS a"
, "LEFT JOIN", fetchAttempts, "AS b"
, "ON"
, "a." <> metadataUrl, "=", "b." <> metadataUrl, "AND"
, "a." <> metadataHash, "=", "b." <> metadataHash
, "WHERE"
, metadataHash, "NOT", "IN" -- Successfully fetched metadata
-- Successfully fetched metadata
, "a." <> metadataHash, "NOT", "IN"
, "("
, "SELECT", metadataHash
, "FROM", metadata
, ")"
, "AND"
, metadataHash, "NOT", "IN" -- Recently failed urls
-- Discard recent failed attempts
, "("
, "SELECT", metadataHash
, "FROM", fetchAttempts
, "WHERE", retryAfter, ">=", "datetime('now')"
, retryAfter, "<", "datetime('now')"
, "OR"
, retryAfter, "IS NULL"
, ")"
-- Important, since we have a limit, we order all results by
-- earlist "retry_after", so that we are sure that all
-- metadata gets _eventually_ processed.
--
-- Note that `NULL` is smaller than everything.
, "ORDER BY", retryAfter, "ASC"
, "LIMIT", nLimit
, ";"
]
Expand Down

0 comments on commit fccb250

Please sign in to comment.