feat(recovery): retry saved Googlevideo scenarios first
Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
1 parent
6220eb0b31
commit
aaa8f751c6
7 files changed
+345
-7
No files matched your search
@@ -32,6 +32,14 @@ type cacheScan func(context.Context, string, string, io.Writer, io.Writer) (stri
|
||||
type cacheCheck func(context.Context, string, string, string, string, io.Writer, io.Writer) error
|
||||
|
||||
func tuneGooglevideo(flags []string, stdout, stderr io.Writer, discover playerRunner, scan cacheScan, check cacheCheck) int {
|
||||
return googlevideoProposal(flags, stdout, stderr, discover, scan, check, false)
|
||||
}
|
||||
|
||||
func recoverGooglevideo(flags []string, stdout, stderr io.Writer, discover playerRunner, scan cacheScan, check cacheCheck) int {
|
||||
return googlevideoProposal(flags, stdout, stderr, discover, scan, check, true)
|
||||
}
|
||||
|
||||
func googlevideoProposal(flags []string, stdout, stderr io.Writer, discover playerRunner, scan cacheScan, check cacheCheck, recoverSaved bool) int {
|
||||
if _, _, err := discoveryFlags(flags); err != nil {
|
||||
fmt.Fprintln(stderr, "error: tune googlevideo", err)
|
||||
return 2
|
||||
@@ -61,7 +69,14 @@ func tuneGooglevideo(flags []string, stdout, stderr io.Writer, discover playerRu
|
||||
host = result.Hostnames[0]
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
if err == nil && recoverSaved {
|
||||
fmt.Fprintln(stderr, "Revalidating qualified saved googlevideo scenarios newest-first; PROPOSED, NOT ACTIVE; no configuration applied.")
|
||||
args, report, err = revalidateGooglevideo(ctx, db, host, stderr, check)
|
||||
if err == nil && args != "" {
|
||||
output, err = googlevideoOutput(report, args, host)
|
||||
}
|
||||
}
|
||||
if err == nil && args == "" {
|
||||
fmt.Fprintln(stderr, "Testing exact googlevideo endpoint:", host, "(proposal only; no configuration applied).")
|
||||
fmt.Fprintln(stderr, "TLS1.2+TLS1.3 scan; real-data shortlist --take 10 --passes 3; exact combined check 3/3 on both protocols. No Selectel ranking or playback claim.")
|
||||
var raw bytes.Buffer
|
||||
@@ -96,6 +111,9 @@ func tuneGooglevideo(flags []string, stdout, stderr io.Writer, discover playerRu
|
||||
err = ctx.Err()
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
err = ctx.Err()
|
||||
}
|
||||
// Do not store or print arbitrary runner errors (they might contain signed URLs).
|
||||
var safeError error
|
||||
if err != nil {
|
||||
|
||||
@@ -18,6 +18,8 @@ Commands:
|
||||
tune youtube Test youtube.com frontend only; print options without applying
|
||||
tune googlevideo --video-id ID --proxy socks5://HOST:PORT
|
||||
Discover and verify one exact cache host; proposal only
|
||||
recover googlevideo --video-id ID --proxy socks5://HOST:PORT
|
||||
Recheck saved scenarios before scanning; proposal, not active
|
||||
render <json-file> Select normalized offline results and print NFQWS2_OPT
|
||||
`
|
||||
|
||||
@@ -26,6 +28,9 @@ func main() {
|
||||
}
|
||||
|
||||
func run(args []string, stdout, stderr io.Writer) int {
|
||||
if len(args) >= 2 && args[0] == "recover" && args[1] == "googlevideo" {
|
||||
return recoverGooglevideo(args[2:], stdout, stderr, runPlayerCurl, runBlockcheck, runCombinedBlockcheckForHost)
|
||||
}
|
||||
if len(args) >= 2 && args[0] == "tune" && args[1] == "googlevideo" {
|
||||
return tuneGooglevideo(args[2:], stdout, stderr, runPlayerCurl, runBlockcheck, runCombinedBlockcheckForHost)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,80 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
)
|
||||
|
||||
// Only complete, well-formed non-passing measurements permit scan fallback.
|
||||
var errCandidateNonpass = errors.New("saved scenario did not pass both TLS protocols")
|
||||
|
||||
func revalidateGooglevideo(ctx context.Context, db *sql.DB, host string, stderr io.Writer, check cacheCheck) (string, []byte, error) {
|
||||
rows, err := db.Query(`SELECT id FROM strategy_candidates WHERE strategy_group = 'googlevideo' ORDER BY id DESC`)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
var ids []int64
|
||||
for rows.Next() {
|
||||
var id int64
|
||||
if err := rows.Scan(&id); err != nil {
|
||||
rows.Close()
|
||||
return "", nil, err
|
||||
}
|
||||
ids = append(ids, id)
|
||||
}
|
||||
err = rows.Err()
|
||||
closeErr := rows.Close() // Release the store's single connection before candidate reads.
|
||||
if err == nil {
|
||||
err = closeErr
|
||||
}
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
for _, id := range ids {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
c, err := readStrategyCandidate(db, id)
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
if !c.Qualified {
|
||||
continue
|
||||
}
|
||||
artifact, err := os.CreateTemp("", "blockcheckw-recover-*.txt")
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
path := artifact.Name()
|
||||
if err := artifact.Close(); err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
fmt.Fprintln(stderr, "Rechecking saved candidate:", id, "exact endpoint:", host, "Blockcheck artifacts:", path)
|
||||
var raw bytes.Buffer
|
||||
err = check(ctx, "blockcheckw", path, c.Args, host, &raw, &raw)
|
||||
if e := os.WriteFile(path+".stdout", raw.Bytes(), 0600); e != nil {
|
||||
err = e
|
||||
}
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
if err := ctx.Err(); err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
report, err := os.ReadFile(path + ".combined.json")
|
||||
if err != nil {
|
||||
return "", nil, err
|
||||
}
|
||||
_, err = selectBlockcheckEvidence(bytes.NewReader(report), c.Args, "googlevideo", host, true)
|
||||
if errors.Is(err, errCandidateNonpass) {
|
||||
continue
|
||||
}
|
||||
return c.Args, report, err
|
||||
}
|
||||
return "", nil, ctx.Err()
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestRecoverGooglevideo(t *testing.T) {
|
||||
for _, mode := range []string{"newest-pass", "older-pass", "no-candidates", "unqualified", "all-failed", "runner-error", "cancelled", "malformed", "missing-report", "incomplete", "wrong-host", "wrong-args", "incomplete-passes", "discovery-error", "database-open", "database-read", "database-save"} {
|
||||
t.Run(mode, func(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
t.Setenv("TMPDIR", dir)
|
||||
t.Setenv("ZAPRET_AUTOTUNE_DB", filepath.Join(dir, "autotune.db"))
|
||||
config := filepath.Join(dir, "active.conf")
|
||||
if err := os.WriteFile(config, []byte("ACTIVE UNCHANGED"), 0600); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
db, err := openStore(databasePath())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
oldArgs, newArgs := "--lua-desync=fake", "--lua-desync=multisplit"
|
||||
seeded := 0
|
||||
if mode != "no-candidates" && mode != "unqualified" {
|
||||
for _, args := range []string{oldArgs, newArgs} {
|
||||
if err := saveGooglevideoRun(db, time.Now(), "old.googlevideo.com", args, []byte(combinedReportFixture(args)), nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
seeded++
|
||||
}
|
||||
}
|
||||
if mode != "no-candidates" {
|
||||
// Newest incomplete endpoint × TLS evidence must never be eligible.
|
||||
tx, err := db.Begin()
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := insertStrategyCandidate(tx, "googlevideo", []string{"old.googlevideo.com", "other.googlevideo.com"}, "--lua-desync=disorder", nil); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if mode == "database-read" {
|
||||
if _, err := db.Exec(`DROP TABLE candidate_evidence`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
if mode == "database-save" {
|
||||
if _, err := db.Exec(`CREATE TRIGGER reject_recovery BEFORE INSERT ON candidate_evidence BEGIN SELECT RAISE(ABORT, 'injected failure'); END`); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
db.Close()
|
||||
if mode == "database-open" {
|
||||
t.Setenv("ZAPRET_AUTOTUNE_DB", dir)
|
||||
}
|
||||
var order, checked []string
|
||||
player := cachePlayer(t, &order)
|
||||
if mode == "discovery-error" {
|
||||
player = func(context.Context, []string, io.Writer, io.Writer) error {
|
||||
order = append(order, "discover")
|
||||
return fmt.Errorf("SECRET discovery failure")
|
||||
}
|
||||
}
|
||||
scans := 0
|
||||
scan := func(_ context.Context, _, host string, _, _ io.Writer) (string, error) {
|
||||
scans++
|
||||
order = append(order, "scan")
|
||||
if host != cacheTestHost {
|
||||
t.Fatal(host)
|
||||
}
|
||||
path := filepath.Join(dir, "scan")
|
||||
return path, os.WriteFile(path+".check.json", []byte(reportFixture(t)), 0600)
|
||||
}
|
||||
check := func(_ context.Context, binary, path, args, host string, _, _ io.Writer) error {
|
||||
if binary != "blockcheckw" || host != cacheTestHost {
|
||||
t.Fatalf("wrong fresh scope: %s %s", binary, host)
|
||||
}
|
||||
order = append(order, "check")
|
||||
if scans == 0 {
|
||||
checked = append(checked, args)
|
||||
}
|
||||
report := combinedReportFixture(args)
|
||||
if scans == 0 {
|
||||
switch mode {
|
||||
case "runner-error":
|
||||
return fmt.Errorf("SECRET runner failure")
|
||||
case "cancelled":
|
||||
return context.Canceled
|
||||
case "missing-report":
|
||||
return nil
|
||||
case "malformed":
|
||||
report = `{"strategies":null}`
|
||||
case "incomplete":
|
||||
report = strings.Replace(report, "HTTPS/TLS1.3", "HTTPS/TLS1.2", 1)
|
||||
case "wrong-host":
|
||||
report = strings.ReplaceAll(report, `"protocol":`, `"domains":["old.googlevideo.com"],"protocol":`)
|
||||
case "wrong-args":
|
||||
report = combinedReportFixture(oldArgs)
|
||||
case "incomplete-passes":
|
||||
report = strings.ReplaceAll(report, `"passes_total":3`, `"passes_total":4`)
|
||||
case "older-pass", "all-failed":
|
||||
if mode == "all-failed" || args == newArgs {
|
||||
report = strings.Replace(report, `"passes_ok":3`, `"passes_ok":2`, 1)
|
||||
}
|
||||
}
|
||||
}
|
||||
return os.WriteFile(path+".combined.json", []byte(report), 0600)
|
||||
}
|
||||
var stdout, stderr bytes.Buffer
|
||||
code := recoverGooglevideo(cacheTestFlags, &stdout, &stderr, player, scan, check)
|
||||
fallback := mode == "no-candidates" || mode == "unqualified" || mode == "all-failed"
|
||||
success := fallback || mode == "newest-pass" || mode == "older-pass"
|
||||
if (code == 0) != success || strings.Contains(stdout.String()+stderr.String(), "SECRET") {
|
||||
t.Fatalf("code %d output %s diagnostics %s", code, stdout.String(), stderr.String())
|
||||
}
|
||||
wantScans := 0
|
||||
if fallback {
|
||||
wantScans = 1
|
||||
}
|
||||
if scans != wantScans {
|
||||
t.Fatalf("scans %d want %d", scans, wantScans)
|
||||
}
|
||||
if mode != "database-open" && (len(order) == 0 || order[0] != "discover" || strings.Count(strings.Join(order, ","), "discover") != 1) {
|
||||
t.Fatal(order)
|
||||
}
|
||||
if len(checked) > 0 && checked[0] != newArgs {
|
||||
t.Fatal(checked)
|
||||
}
|
||||
if mode == "older-pass" || mode == "all-failed" {
|
||||
if strings.Join(checked, ",") != newArgs+","+oldArgs {
|
||||
t.Fatal(checked)
|
||||
}
|
||||
} else if mode == "newest-pass" && len(checked) != 1 {
|
||||
t.Fatal(checked)
|
||||
}
|
||||
if !success && stdout.Len() != 0 {
|
||||
t.Fatal("failed recovery emitted proposal")
|
||||
}
|
||||
if success && (!strings.Contains(stdout.String(), "--hostlist-domains=^"+cacheTestHost+" ") || !strings.Contains(stderr.String(), "NOT ACTIVE")) {
|
||||
t.Fatal(stdout.String(), stderr.String())
|
||||
}
|
||||
active, err := os.ReadFile(config)
|
||||
if err != nil || string(active) != "ACTIVE UNCHANGED" {
|
||||
t.Fatalf("active config changed: %s %v", active, err)
|
||||
}
|
||||
if mode == "database-open" || mode == "database-read" {
|
||||
return
|
||||
}
|
||||
db, err = openStore(databasePath())
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer db.Close()
|
||||
var legacy, proposals int
|
||||
if err := db.QueryRow(`SELECT count(*) FROM tune_runs`).Scan(&legacy); err != nil || legacy != 0 {
|
||||
t.Fatalf("frontend changed: %d %v", legacy, err)
|
||||
}
|
||||
if err := db.QueryRow(`SELECT count(*) FROM googlevideo_tune_runs WHERE state = 'PROPOSED'`).Scan(&proposals); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
want := seeded
|
||||
if success {
|
||||
want++
|
||||
}
|
||||
if proposals != want {
|
||||
t.Fatalf("proposals %d want %d", proposals, want)
|
||||
}
|
||||
if success {
|
||||
var id int64
|
||||
if err := db.QueryRow(`SELECT candidate_id FROM googlevideo_tune_runs ORDER BY id DESC LIMIT 1`).Scan(&id); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
c, err := readStrategyCandidate(db, id)
|
||||
if err != nil || !c.Qualified || len(c.Endpoints) != 1 || c.Endpoints[0] != cacheTestHost {
|
||||
t.Fatalf("fresh candidate: %+v %v", c, err)
|
||||
}
|
||||
if mode == "newest-pass" && c.Args != newArgs || mode == "older-pass" && c.Args != oldArgs {
|
||||
t.Fatal(c.Args)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -110,6 +110,10 @@ func selectBlockcheckArgs(r io.Reader, expected string) (string, error) {
|
||||
}
|
||||
|
||||
func selectBlockcheckArgsForTarget(r io.Reader, expected, group, host string) (string, error) {
|
||||
return selectBlockcheckEvidence(r, expected, group, host, false)
|
||||
}
|
||||
|
||||
func selectBlockcheckEvidence(r io.Reader, expected, group, host string, recovery bool) (string, error) {
|
||||
d := json.NewDecoder(r)
|
||||
var report struct {
|
||||
Strategies []json.RawMessage `json:"strategies"`
|
||||
@@ -125,6 +129,7 @@ func selectBlockcheckArgsForTarget(r io.Reader, expected, group, host string) (s
|
||||
}
|
||||
best := make(map[string]candidate)
|
||||
seen := make(map[string]bool)
|
||||
nonpass := false
|
||||
for i, record := range report.Strategies {
|
||||
var fields map[string]json.RawMessage
|
||||
if err := json.Unmarshal(record, &fields); err != nil {
|
||||
@@ -165,7 +170,15 @@ func selectBlockcheckArgsForTarget(r io.Reader, expected, group, host string) (s
|
||||
return "", fmt.Errorf("result %d: duplicate args/protocol", i)
|
||||
}
|
||||
seen[key] = true
|
||||
if expected != "" && (row.Args != expected || !fullyPassing(row.candidate) || row.PassesTotal != 3) {
|
||||
if recovery {
|
||||
if row.Args != expected || row.PassesTotal != 3 {
|
||||
return "", fmt.Errorf("unexpected saved args or incomplete check")
|
||||
}
|
||||
if err := row.validateArgs(); err != nil {
|
||||
return "", err
|
||||
}
|
||||
nonpass = nonpass || !fullyPassing(row.candidate)
|
||||
} else if expected != "" && (row.Args != expected || !fullyPassing(row.candidate) || row.PassesTotal != 3) {
|
||||
return "", fmt.Errorf("result %d: combined args must pass exactly 3/3 for both protocols", i)
|
||||
}
|
||||
// Valid check evidence may describe args our inert output cannot represent.
|
||||
@@ -180,6 +193,15 @@ func selectBlockcheckArgsForTarget(r io.Reader, expected, group, host string) (s
|
||||
best[protocol] = row.candidate
|
||||
}
|
||||
}
|
||||
if recovery {
|
||||
if len(seen) != 2 || !seen["tls12\x00"+expected] || !seen["tls13\x00"+expected] {
|
||||
return "", fmt.Errorf("incomplete combined TLS evidence")
|
||||
}
|
||||
if nonpass {
|
||||
return "", errCandidateNonpass
|
||||
}
|
||||
return expected, nil
|
||||
}
|
||||
var args []string
|
||||
payloads := make(map[string]bool)
|
||||
for _, protocol := range []string{"tls12", "tls13"} {
|
||||
|
||||
Reference in new issue
Block a user