|
| 1 | +// Copyright The Linux Foundation and each contributor to LFX. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package sync |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "flag" |
| 9 | + "fmt" |
| 10 | + "log/slog" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/linuxfoundation/lfx-v2-committee-service/cmd/committee-cli/commands" |
| 14 | + "github.com/linuxfoundation/lfx-v2-committee-service/internal/domain/model" |
| 15 | + "github.com/linuxfoundation/lfx-v2-committee-service/pkg/constants" |
| 16 | + "github.com/linuxfoundation/lfx-v2-committee-service/pkg/errors" |
| 17 | +) |
| 18 | + |
| 19 | +// membersByUsernameIndexSubcommand backfills the secondary index |
| 20 | +// "lookup/committee-members-by-username/<username_hash>.<member_uid>" for all existing members |
| 21 | +// that carry a username. Members without a username are skipped. Idempotent: re-running is safe. |
| 22 | +type membersByUsernameIndexSubcommand struct{} |
| 23 | + |
| 24 | +func (s *membersByUsernameIndexSubcommand) Name() string { return "members-by-username-index" } |
| 25 | + |
| 26 | +func (s *membersByUsernameIndexSubcommand) Help() string { |
| 27 | + return "backfill the username→member secondary index for existing members (LFXV2-2645)" |
| 28 | +} |
| 29 | + |
| 30 | +func (s *membersByUsernameIndexSubcommand) Run(ctx context.Context, rc commands.RunContext) error { |
| 31 | + slog.DebugContext(ctx, "starting subcommand", "subcommand", s.Name(), "args", rc.Args) |
| 32 | + |
| 33 | + fs := flag.NewFlagSet("members-by-username-index", flag.ContinueOnError) |
| 34 | + fs.Usage = func() { |
| 35 | + _, _ = fmt.Fprintf(fs.Output(), "usage: committee-cli sync members-by-username-index [flags]\n\nflags:\n") |
| 36 | + fs.PrintDefaults() |
| 37 | + } |
| 38 | + sleep := fs.Duration("sleep", 0, "wait between each member write (e.g. 200ms, 1s)") |
| 39 | + dryRun := fs.Bool("dry-run", true, "compute what would be written without actually writing (default true; pass --dry-run=false to write)") |
| 40 | + if err := fs.Parse(rc.Args); err != nil { |
| 41 | + if err == flag.ErrHelp { |
| 42 | + return nil |
| 43 | + } |
| 44 | + return err |
| 45 | + } |
| 46 | + |
| 47 | + rc.DryRun = *dryRun |
| 48 | + |
| 49 | + if rc.CommitteeReader == nil { |
| 50 | + return errors.NewUnexpected("CommitteeReader is not wired in RunContext") |
| 51 | + } |
| 52 | + if rc.CommitteeMemberWriter == nil { |
| 53 | + return errors.NewUnexpected("CommitteeMemberWriter is not wired in RunContext") |
| 54 | + } |
| 55 | + |
| 56 | + ctx = context.WithValue(ctx, constants.AuthorizationContextID, "Bearer lfx-v2-committee-service") |
| 57 | + |
| 58 | + stats := commands.NewStats() |
| 59 | + stats.DryRun = rc.DryRun |
| 60 | + |
| 61 | + errEach := rc.CommitteeReader.EachMember(ctx, func(member *model.CommitteeMember) error { |
| 62 | + stats.Total++ |
| 63 | + |
| 64 | + if member.BuildUsernameIndexKey(ctx) == "" { |
| 65 | + stats.Skipped++ |
| 66 | + return nil |
| 67 | + } |
| 68 | + |
| 69 | + if rc.DryRun { |
| 70 | + slog.DebugContext(ctx, "dry-run: would write username member index", |
| 71 | + "member_uid", member.UID, |
| 72 | + ) |
| 73 | + stats.Updated++ |
| 74 | + return nil |
| 75 | + } |
| 76 | + |
| 77 | + key, errIdx := rc.CommitteeMemberWriter.IndexMemberByUsername(ctx, member) |
| 78 | + if errIdx != nil { |
| 79 | + slog.WarnContext(ctx, "failed to index member by username", |
| 80 | + "error", errIdx, |
| 81 | + "member_uid", member.UID, |
| 82 | + ) |
| 83 | + stats.Failed++ |
| 84 | + return nil |
| 85 | + } |
| 86 | + if key == "" { |
| 87 | + stats.Skipped++ |
| 88 | + return nil |
| 89 | + } |
| 90 | + |
| 91 | + slog.DebugContext(ctx, "indexed member by username", |
| 92 | + "member_uid", member.UID, |
| 93 | + ) |
| 94 | + stats.Updated++ |
| 95 | + |
| 96 | + if *sleep > 0 { |
| 97 | + timer := time.NewTimer(*sleep) |
| 98 | + select { |
| 99 | + case <-ctx.Done(): |
| 100 | + if !timer.Stop() { |
| 101 | + <-timer.C |
| 102 | + } |
| 103 | + return ctx.Err() |
| 104 | + case <-timer.C: |
| 105 | + } |
| 106 | + } |
| 107 | + return nil |
| 108 | + }) |
| 109 | + if errEach != nil { |
| 110 | + return fmt.Errorf("failed to stream members: %w", errEach) |
| 111 | + } |
| 112 | + |
| 113 | + stats.Log(ctx, "sync members-by-username-index") |
| 114 | + |
| 115 | + if stats.Failed > 0 { |
| 116 | + return fmt.Errorf("%d member(s) failed to index", stats.Failed) |
| 117 | + } |
| 118 | + return nil |
| 119 | +} |
0 commit comments