more iops cheese testing - sort by downloaded_at not host id
This commit is contained in:
parent
a30fe104a3
commit
9266c1417d
3 changed files with 62 additions and 22 deletions
|
|
@ -21,7 +21,7 @@ SET work_mem = '2GB';
|
||||||
\timing on
|
\timing on
|
||||||
|
|
||||||
CREATE TEMP TABLE best_icons AS
|
CREATE TEMP TABLE best_icons AS
|
||||||
SELECT DISTINCT ON (host_id) host_id, icon_hash
|
SELECT DISTINCT ON (host_id) host_id, icon_hash, downloaded_at
|
||||||
FROM icons
|
FROM icons
|
||||||
WHERE scan_state = 'completed'
|
WHERE scan_state = 'completed'
|
||||||
AND icon_hash IS NOT NULL
|
AND icon_hash IS NOT NULL
|
||||||
|
|
@ -50,7 +50,7 @@ ORDER BY host_id,
|
||||||
|
|
||||||
\echo 'Step 2: Updating hosts...'
|
\echo 'Step 2: Updating hosts...'
|
||||||
|
|
||||||
UPDATE hosts h SET best_icon_hash = b.icon_hash
|
UPDATE hosts h SET best_icon_hash = b.icon_hash, icon_downloaded_at = b.downloaded_at
|
||||||
FROM best_icons b WHERE h.id = b.host_id;
|
FROM best_icons b WHERE h.id = b.host_id;
|
||||||
|
|
||||||
\timing off
|
\timing off
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,9 @@ package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v5"
|
||||||
"github.com/jackc/pgx/v5/pgxpool"
|
"github.com/jackc/pgx/v5/pgxpool"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -13,19 +15,43 @@ type HostRow struct {
|
||||||
HtmlTitle string
|
HtmlTitle string
|
||||||
IframeAllowed bool
|
IframeAllowed bool
|
||||||
BestIconHash string
|
BestIconHash string
|
||||||
|
IconDownloadedAt *time.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
// fetchHostsPage gets a page of hosts with titles, ordered by id for disk locality.
|
// fetchHostsPage gets a page of hosts with titles, ordered by icon_downloaded_at for disk locality.
|
||||||
// Icons were downloaded roughly in host-ID order, so reading by ID approximates
|
// Icons written to disk at similar times are physically adjacent on the EBS volume —
|
||||||
// the physical write order on disk — improves EBS readahead cache hits.
|
// reading in write order maximizes OS readahead cache hits.
|
||||||
func fetchHostsPage(ctx context.Context, pool *pgxpool.Pool, lastID int64, limit int) ([]HostRow, error) {
|
func fetchHostsPage(ctx context.Context, pool *pgxpool.Pool, lastTime *time.Time, lastID int64, limit int) ([]HostRow, error) {
|
||||||
rows, err := pool.Query(ctx, `
|
// Two-phase: first hosts with icons (ordered by download time), then hosts without
|
||||||
SELECT id, hostname, protocol, html_title, COALESCE(iframe_allowed, true), COALESCE(best_icon_hash, '')
|
var rows pgx.Rows
|
||||||
|
var err error
|
||||||
|
if lastTime != nil {
|
||||||
|
rows, err = pool.Query(ctx, `
|
||||||
|
SELECT id, hostname, protocol, html_title, COALESCE(iframe_allowed, true), COALESCE(best_icon_hash, ''), icon_downloaded_at
|
||||||
FROM hosts
|
FROM hosts
|
||||||
WHERE html_title IS NOT NULL AND id > $1
|
WHERE html_title IS NOT NULL AND icon_downloaded_at > $1
|
||||||
|
ORDER BY icon_downloaded_at
|
||||||
|
LIMIT $2
|
||||||
|
`, lastTime, limit)
|
||||||
|
} else if lastID > 0 {
|
||||||
|
// No more timestamped hosts — fetch remaining (no icon) by id
|
||||||
|
rows, err = pool.Query(ctx, `
|
||||||
|
SELECT id, hostname, protocol, html_title, COALESCE(iframe_allowed, true), '', NULL::timestamptz
|
||||||
|
FROM hosts
|
||||||
|
WHERE html_title IS NOT NULL AND icon_downloaded_at IS NULL AND id > $1
|
||||||
ORDER BY id
|
ORDER BY id
|
||||||
LIMIT $2
|
LIMIT $2
|
||||||
`, lastID, limit)
|
`, lastID, limit)
|
||||||
|
} else {
|
||||||
|
// Start: fetch timestamped hosts first
|
||||||
|
rows, err = pool.Query(ctx, `
|
||||||
|
SELECT id, hostname, protocol, html_title, COALESCE(iframe_allowed, true), COALESCE(best_icon_hash, ''), icon_downloaded_at
|
||||||
|
FROM hosts
|
||||||
|
WHERE html_title IS NOT NULL AND icon_downloaded_at IS NOT NULL
|
||||||
|
ORDER BY icon_downloaded_at
|
||||||
|
LIMIT $1
|
||||||
|
`, limit)
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
@ -34,7 +60,7 @@ func fetchHostsPage(ctx context.Context, pool *pgxpool.Pool, lastID int64, limit
|
||||||
var hosts []HostRow
|
var hosts []HostRow
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
var h HostRow
|
var h HostRow
|
||||||
if err := rows.Scan(&h.ID, &h.Hostname, &h.Protocol, &h.HtmlTitle, &h.IframeAllowed, &h.BestIconHash); err != nil {
|
if err := rows.Scan(&h.ID, &h.Hostname, &h.Protocol, &h.HtmlTitle, &h.IframeAllowed, &h.BestIconHash, &h.IconDownloadedAt); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
hosts = append(hosts, h)
|
hosts = append(hosts, h)
|
||||||
|
|
|
||||||
|
|
@ -128,7 +128,9 @@ func main() {
|
||||||
// Stage 1: DB fetcher — continuously fetches pages into hostCh
|
// Stage 1: DB fetcher — continuously fetches pages into hostCh
|
||||||
go func() {
|
go func() {
|
||||||
defer close(hostCh)
|
defer close(hostCh)
|
||||||
|
var lastTime *time.Time
|
||||||
var lastID int64
|
var lastID int64
|
||||||
|
iconPhase := true // first: hosts with icons (by download time), then: hosts without
|
||||||
pageSize := 50000
|
pageSize := 50000
|
||||||
fetched := 0
|
fetched := 0
|
||||||
for {
|
for {
|
||||||
|
|
@ -143,14 +145,26 @@ func main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fetchStart := time.Now()
|
fetchStart := time.Now()
|
||||||
hosts, err := fetchHostsPage(ctx, pool, lastID, limit)
|
hosts, err := fetchHostsPage(ctx, pool, lastTime, lastID, limit)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("Failed to fetch hosts: %v", err)
|
log.Fatalf("Failed to fetch hosts: %v", err)
|
||||||
}
|
}
|
||||||
if len(hosts) == 0 {
|
if len(hosts) == 0 {
|
||||||
|
if iconPhase {
|
||||||
|
// Switch to no-icon hosts
|
||||||
|
iconPhase = false
|
||||||
|
lastTime = nil
|
||||||
|
lastID = 0
|
||||||
|
continue
|
||||||
|
}
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
lastID = hosts[len(hosts)-1].ID
|
last := hosts[len(hosts)-1]
|
||||||
|
if last.IconDownloadedAt != nil {
|
||||||
|
lastTime = last.IconDownloadedAt
|
||||||
|
} else {
|
||||||
|
lastID = last.ID
|
||||||
|
}
|
||||||
fmt.Printf("[fetcher] %d hosts in %dms (hostCh: %d/%d)\n",
|
fmt.Printf("[fetcher] %d hosts in %dms (hostCh: %d/%d)\n",
|
||||||
len(hosts), time.Since(fetchStart).Milliseconds(), len(hostCh), cap(hostCh))
|
len(hosts), time.Since(fetchStart).Milliseconds(), len(hostCh), cap(hostCh))
|
||||||
for _, h := range hosts {
|
for _, h := range hosts {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue