feat(apply): support staged strategy groups
Co-authored-by: GPT-6 <gpt-6@openai.com>
This commit is contained in:
1 parent
468532dc8f
commit
2d4e4878ae
9 files changed
+776
-71
No files matched your search
@@ -55,33 +55,35 @@ func managedDependencies(player playerRunner) managedDeps {
|
||||
}
|
||||
|
||||
func managedArguments(args []string) (string, []int64, error) {
|
||||
if len(args) == 1 && (args[0] == "init" || args[0] == "status") {
|
||||
invalid := fmt.Errorf("managed accepts init, status, active-ids, or apply with one or both candidate flags (full desired set); no path flags")
|
||||
if len(args) == 1 && (args[0] == "init" || args[0] == "status" || args[0] == "active-ids") {
|
||||
return args[0], nil, nil
|
||||
}
|
||||
if len(args) == 5 && args[0] == "apply" {
|
||||
ids := make([]int64, 2)
|
||||
seen := map[string]bool{}
|
||||
for i := 1; i < len(args); i += 2 {
|
||||
key := args[i]
|
||||
if seen[key] || (key != "--frontend-candidate" && key != "--googlevideo-candidate") || args[i+1] == "" || strings.Trim(args[i+1], "0123456789") != "" {
|
||||
break
|
||||
}
|
||||
seen[key] = true
|
||||
id, err := strconv.ParseInt(args[i+1], 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
break
|
||||
}
|
||||
index := 0
|
||||
if key == "--googlevideo-candidate" {
|
||||
index = 1
|
||||
}
|
||||
ids[index] = id
|
||||
if (len(args) != 3 && len(args) != 5) || args[0] != "apply" {
|
||||
return "", nil, invalid
|
||||
}
|
||||
selected := map[string]int64{}
|
||||
for i := 1; i < len(args); i += 2 {
|
||||
key := args[i]
|
||||
if selected[key] != 0 || (key != "--frontend-candidate" && key != "--googlevideo-candidate") || args[i+1] == "" || strings.Trim(args[i+1], "0123456789") != "" {
|
||||
return "", nil, invalid
|
||||
}
|
||||
if ids[0] > 0 && ids[1] > 0 && ids[0] != ids[1] {
|
||||
return "apply", ids, nil
|
||||
id, err := strconv.ParseInt(args[i+1], 10, 64)
|
||||
if err != nil || id <= 0 {
|
||||
return "", nil, invalid
|
||||
}
|
||||
selected[key] = id
|
||||
}
|
||||
var ids []int64
|
||||
for _, key := range []string{"--frontend-candidate", "--googlevideo-candidate"} {
|
||||
if id := selected[key]; id != 0 {
|
||||
ids = append(ids, id)
|
||||
}
|
||||
}
|
||||
return "", nil, fmt.Errorf("managed accepts init, status, or apply --frontend-candidate ID --googlevideo-candidate ID (positive distinct IDs); no path flags")
|
||||
if len(ids) == 2 && ids[0] == ids[1] {
|
||||
return "", nil, invalid
|
||||
}
|
||||
return "apply", ids, nil
|
||||
}
|
||||
|
||||
// Provisioning supplies the directory. Never create it or follow aliases.
|
||||
@@ -193,27 +195,79 @@ func managedCheckIdentity(m *managedTransaction) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func managedStatus(m *managedTransaction, out io.Writer) error {
|
||||
// Status and supervisor reads share the same locked, DB-validated snapshot.
|
||||
func managedReport(m *managedTransaction, out io.Writer, openDB func() (*sql.DB, error), activeOnly bool) error {
|
||||
return m.locked(func() error {
|
||||
j, err := m.load()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
b, err := m.read("config")
|
||||
b, err := m.read(m.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if j.State != "committed" {
|
||||
return fmt.Errorf("pending transaction; managed apply must reconcile before activation")
|
||||
if j.State != "committed" && activeOnly {
|
||||
return fmt.Errorf("pending transaction; reconcile before reading active IDs")
|
||||
}
|
||||
if managedHash(b) != j.Hash {
|
||||
if managedHash(b) != j.Hash && (j.State != "prepared" || managedHash(b) != j.Next) {
|
||||
return errManagedDrift
|
||||
}
|
||||
groups := "none (disabled)"
|
||||
if strings.Contains(string(b), "\nNFQWS2_ENABLE=1\n") {
|
||||
groups = "frontend,googlevideo"
|
||||
groups := map[string]bool{}
|
||||
known := j.State == "committed" && j.Version == 2 && string(b) == managedBaseline && len(j.Candidates) == 0
|
||||
if j.State == "committed" && j.Version == 2 && len(j.Candidates) != 0 {
|
||||
db, err := openDB()
|
||||
if db != nil {
|
||||
defer db.Close()
|
||||
}
|
||||
if err == nil && db != nil {
|
||||
rendered, err := renderManagedConfig(db, j.Candidates)
|
||||
if err == nil && managedHash(rendered) == j.Hash {
|
||||
known = true
|
||||
for _, id := range j.Candidates {
|
||||
c, err := readStrategyCandidate(db, id)
|
||||
if err != nil || !c.Qualified {
|
||||
known = false
|
||||
break
|
||||
}
|
||||
groups[c.Group] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_, err = fmt.Fprintf(out, "managed-test generation=%d hash=%s groups=%s\nLive health: NOT CHECKED (Player API is not playback)\n", j.Generation, j.Hash, groups)
|
||||
if !known {
|
||||
groups = map[string]bool{}
|
||||
// Unvalidated journal references are not active recovery candidates.
|
||||
j.Candidates = nil
|
||||
}
|
||||
var active, missing []string
|
||||
for _, group := range []string{"frontend", "googlevideo"} {
|
||||
if groups[group] {
|
||||
active = append(active, group)
|
||||
} else {
|
||||
missing = append(missing, group)
|
||||
}
|
||||
}
|
||||
if activeOnly {
|
||||
if !known {
|
||||
return fmt.Errorf("active candidate IDs unresolved; deployment preserved by hash")
|
||||
}
|
||||
return json.NewEncoder(out).Encode(struct {
|
||||
IDs []int64 `json:"active_candidate_ids"`
|
||||
Missing []string `json:"unresolved_groups"`
|
||||
}{append([]int64{}, j.Candidates...), missing})
|
||||
}
|
||||
groupText := strings.Join(active, ",")
|
||||
if groupText == "" {
|
||||
groupText = "none (disabled)"
|
||||
if !known {
|
||||
groupText = "unknown"
|
||||
}
|
||||
}
|
||||
var reportIDs []int64
|
||||
if known {
|
||||
reportIDs = j.Candidates
|
||||
}
|
||||
_, err = fmt.Fprintf(out, "managed-test generation=%d hash=%s state=%s groups=%s active_candidate_ids=%v unresolved_groups=%s\nLive health: NOT CHECKED (Player API is not GGC/video playback)\n", j.Generation, j.Hash, j.State, groupText, reportIDs, strings.Join(missing, ","))
|
||||
return err
|
||||
})
|
||||
}
|
||||
@@ -269,25 +323,35 @@ func managedCLI(args []string, stdout, stderr io.Writer, d managedDeps) int {
|
||||
if command == "init" {
|
||||
err = managedInitialize(m)
|
||||
} else if err = managedCheckIdentity(m); err == nil {
|
||||
if command == "status" {
|
||||
err = managedStatus(m, stdout)
|
||||
if command == "status" || command == "active-ids" {
|
||||
err = managedReport(m, stdout, d.openDB, command == "active-ids")
|
||||
} else {
|
||||
err = managedActivate(m, d, ids)
|
||||
expected := map[int64]string{}
|
||||
for i := 1; i < len(args); i += 2 {
|
||||
id, _ := strconv.ParseInt(args[i+1], 10, 64)
|
||||
expected[id] = strings.TrimSuffix(strings.TrimPrefix(args[i], "--"), "-candidate")
|
||||
}
|
||||
err = managedActivate(m, d, ids, expected)
|
||||
}
|
||||
}
|
||||
if err != nil {
|
||||
fmt.Fprintln(stderr, "error:", err)
|
||||
return 1
|
||||
}
|
||||
if command != "status" {
|
||||
if command == "init" || command == "apply" {
|
||||
if _, err := fmt.Fprintln(stdout, "managed-test "+command+": OK"); err != nil {
|
||||
return 1
|
||||
}
|
||||
if command == "apply" {
|
||||
if _, err := fmt.Fprintln(stdout, "Player API only; not full-profile/GGC/playback health. Use managed status for unresolved groups."); err != nil {
|
||||
return 1
|
||||
}
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func managedActivate(m *managedTransaction, d managedDeps, ids []int64) error {
|
||||
func managedActivate(m *managedTransaction, d managedDeps, ids []int64, expected map[int64]string) error {
|
||||
info, err := os.Stat(d.script)
|
||||
if err != nil || !info.Mode().IsRegular() || info.Mode().Perm()&0111 == 0 {
|
||||
return fmt.Errorf("target zapret daemon init executable unavailable")
|
||||
@@ -322,8 +386,9 @@ func managedActivate(m *managedTransaction, d managedDeps, ids []int64) error {
|
||||
return fmt.Errorf("candidate database missing")
|
||||
}
|
||||
defer db.Close()
|
||||
for i, group := range []string{"frontend", "googlevideo"} {
|
||||
c, err := readStrategyCandidate(db, ids[i])
|
||||
for _, id := range ids {
|
||||
group := expected[id]
|
||||
c, err := readStrategyCandidate(db, id)
|
||||
if err != nil || c.Group != group {
|
||||
return fmt.Errorf("invalid %s candidate ID", group)
|
||||
}
|
||||
@@ -331,7 +396,7 @@ func managedActivate(m *managedTransaction, d managedDeps, ids []int64) error {
|
||||
if _, err := renderManagedConfig(db, ids); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := m.Reconcile(); err != nil {
|
||||
if err := m.Reconcile(db); err != nil {
|
||||
return err
|
||||
}
|
||||
return m.Apply(db, ids)
|
||||
|
||||
Reference in new issue
Block a user