Skip to content

Commit

Permalink
Allow round-robin across multiple URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
AlCutter committed May 23, 2024
1 parent 303ae53 commit 25280eb
Showing 1 changed file with 56 additions and 15 deletions.
71 changes: 56 additions & 15 deletions hammer/hammer.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"net/url"
"os"
"strings"
"sync"
"time"

"github.com/gdamore/tcell/v2"
Expand All @@ -37,8 +38,13 @@ import (
"k8s.io/klog/v2"
)

func init() {
flag.Var(&logURL, "log_url", "Log storage root URL (can be specified multiple times), e.g. https://log.server/and/path/")
}

var (
logURL = flag.String("log_url", "", "Log storage root URL, e.g. https://log.server/and/path/")
logURL multiStringFlag

bearerToken = flag.String("bearer_token", "", "The bearer token for auth. For GCP this is the result of `gcloud auth print-identity-token`")
logPubKeyFile = flag.String("log_public_key", "", "Location of log public key file. If unset, uses the contents of the SERVERLESS_LOG_PUBLIC_KEY environment variable")
origin = flag.String("origin", "", "Expected first line of checkpoints from log")
Expand All @@ -65,6 +71,26 @@ var (
}
)

type roundRobinFetcher struct {
sync.Mutex
idx int
f []client.Fetcher
}

func (rr *roundRobinFetcher) Add(f client.Fetcher) {
rr.f = append(rr.f, f)
}

func (rr *roundRobinFetcher) Fetch(ctx context.Context, path string) ([]byte, error) {
rr.Lock()
defer rr.Unlock()

klog.V(2).Infof("Using fetcher [%d] to fetch %q", rr.idx, path)
f := rr.f[rr.idx]
rr.idx = (rr.idx + 1) % len(rr.f)
return f(ctx, path)
}

func main() {
klog.InitFlags(nil)
flag.Parse()
Expand All @@ -76,25 +102,29 @@ func main() {
klog.Exitf("failed to read log public key: %v", err)
}

u := *logURL
if len(u) == 0 {
if len(logURL) == 0 {
klog.Exitf("--log_url must be provided")
}
// url must reference a directory, by definition
if !strings.HasSuffix(u, "/") {
u += "/"
}

rootURL, err := url.Parse(u)
if err != nil {
klog.Exitf("Invalid log URL: %v", err)
}
f := roundRobinFetcher{}
var rootURL *url.URL
for _, s := range logURL {
// url must reference a directory, by definition
if !strings.HasSuffix(s, "/") {
s += "/"
}

rootURL, err = url.Parse(s)
if err != nil {
klog.Exitf("Invalid log URL: %v", err)
}
f.Add(newFetcher(rootURL))

}
var cpRaw []byte
f := newFetcher(rootURL)
cons := client.UnilateralConsensus(f)
cons := client.UnilateralConsensus(f.Fetch)
hasher := rfc6962.DefaultHasher
tracker, err := client.NewLogStateTracker(ctx, f, hasher, cpRaw, logSigV, *origin, cons)
tracker, err := client.NewLogStateTracker(ctx, f.Fetch, hasher, cpRaw, logSigV, *origin, cons)
if err != nil {
klog.Exitf("Failed to create LogStateTracker: %v", err)
}
Expand All @@ -108,7 +138,7 @@ func main() {
if err != nil {
klog.Exitf("Failed to create add URL: %v", err)
}
hammer := NewHammer(&tracker, f, addURL)
hammer := NewHammer(&tracker, f.Fetch, addURL)
hammer.Run(ctx)

if *showUI {
Expand Down Expand Up @@ -443,3 +473,14 @@ func readHTTP(ctx context.Context, u *url.URL) ([]byte, error) {
}
return body, nil
}

type multiStringFlag []string

func (ms *multiStringFlag) String() string {
return strings.Join(*ms, ",")
}

func (ms *multiStringFlag) Set(w string) error {
*ms = append(*ms, w)
return nil
}

0 comments on commit 25280eb

Please sign in to comment.