-
Notifications
You must be signed in to change notification settings - Fork 182
Add peer-to-peer metrics tracker #1333
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
djeebus
wants to merge
20
commits into
main
Choose a base branch
from
joint-metrics
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+651
−82
Open
Changes from 14 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f598bf8
create a strongly typed sandboxes map
djeebus 4402908
rename struct, add subscribers
djeebus fa22f6a
add locking to the subs list
djeebus 80ed359
collect/publish metrics across peers
djeebus 2fba68d
use the errgroup.Group to run the metrics tracker
djeebus 5bababb
split up the limiter from the tracker
djeebus 753a5d5
make tidy
djeebus 8d31aa5
shrink the tracker a bit
djeebus 51fd9e5
Merge remote-tracking branch 'origin/main' into joint-metrics
djeebus e8db442
rename some variables and fields
djeebus ec9c0d2
remove obsolete error
djeebus c37153c
Merge branch 'main' into joint-metrics
djeebus 94882c5
clean up error
djeebus fb54ef7
fix error message
djeebus 21cc7a8
Merge remote-tracking branch 'origin/main' into joint-metrics
djeebus d5ab234
bring back new error message
djeebus f7d02fc
linting
djeebus 196a06c
Merge branch 'main' into joint-metrics
djeebus 39a2b94
rename metrics tracker to shared state manager
djeebus 835ee09
protect against nils
djeebus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,248 @@ | ||
| package metrics | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "errors" | ||
| "fmt" | ||
| "os" | ||
| "path/filepath" | ||
| "strconv" | ||
| "strings" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/fsnotify/fsnotify" | ||
| "go.uber.org/zap" | ||
|
|
||
| "github.com/e2b-dev/infra/packages/orchestrator/internal/sandbox" | ||
| "github.com/e2b-dev/infra/packages/shared/pkg/smap" | ||
| ) | ||
|
|
||
| type Tracker struct { | ||
| selfSandboxResources *smap.Map[sandbox.Config] | ||
| selfWriteInterval time.Duration | ||
| otherMetrics map[int]Allocations | ||
| otherLock sync.RWMutex | ||
| } | ||
|
|
||
| func (t *Tracker) OnInsert(sandbox *sandbox.Sandbox) { | ||
| t.selfSandboxResources.Insert(sandbox.Metadata.Runtime.SandboxID, sandbox.Config) | ||
| } | ||
|
|
||
| func (t *Tracker) OnRemove(sandboxID string) { | ||
| t.selfSandboxResources.Remove(sandboxID) | ||
| } | ||
|
|
||
| func NewTracker(selfWriteInterval time.Duration) (*Tracker, error) { | ||
| return &Tracker{ | ||
| otherMetrics: map[int]Allocations{}, | ||
|
|
||
| selfWriteInterval: selfWriteInterval, | ||
| selfSandboxResources: smap.New[sandbox.Config](), | ||
| }, nil | ||
| } | ||
|
|
||
| func (t *Tracker) TotalRunningCount() int { | ||
| count := t.selfSandboxResources.Count() | ||
|
|
||
| t.otherLock.RLock() | ||
| for _, item := range t.otherMetrics { | ||
| count += int(item.Sandboxes) | ||
| } | ||
| t.otherLock.RUnlock() | ||
|
|
||
| return count | ||
| } | ||
|
|
||
| func (t *Tracker) getSelfAllocated() Allocations { | ||
| var allocated Allocations | ||
| for _, item := range t.selfSandboxResources.Items() { | ||
| allocated.VCPUs += uint32(item.Vcpu) | ||
| allocated.MemoryBytes += uint64(item.RamMB) * 1024 * 1024 | ||
| allocated.DiskBytes += uint64(item.TotalDiskSizeMB) * 1024 * 1024 | ||
| allocated.Sandboxes++ | ||
| } | ||
| return allocated | ||
| } | ||
|
|
||
| func (t *Tracker) removeSelfFile(path string) { | ||
| if err := os.Remove(path); err != nil { | ||
| zap.L().Error("Failed to remove self file", zap.Error(err), zap.String("path", path)) | ||
| } | ||
| } | ||
|
|
||
| func (t *Tracker) makeSelfPath(directory string) string { | ||
| filename := fmt.Sprintf("%d.json", os.Getpid()) | ||
| selfPath := filepath.Join(directory, filename) | ||
| return selfPath | ||
| } | ||
|
|
||
| func (t *Tracker) Run(ctx context.Context, directory string) error { | ||
| if err := os.MkdirAll(directory, 0o777); err != nil { | ||
| return fmt.Errorf("failed to create directory: %w", err) | ||
| } | ||
|
|
||
| // set up the self file | ||
| selfPath := t.makeSelfPath(directory) | ||
| defer t.removeSelfFile(selfPath) | ||
| writeTicks := time.Tick(t.selfWriteInterval) | ||
|
|
||
| // set up the file watcher | ||
| watcher, err := fsnotify.NewWatcher() | ||
| if err != nil { | ||
| return fmt.Errorf("failed to create watcher: %w", err) | ||
| } | ||
| if err = watcher.Add(directory); err != nil { | ||
| if err2 := watcher.Close(); err2 != nil { | ||
| err = errors.Join(err, fmt.Errorf("failed to close watcher: %w", err2)) | ||
| } | ||
| return fmt.Errorf("failed to watch %q: %w", directory, err) | ||
| } | ||
|
|
||
| // read existing files | ||
| fullPaths, err := filepath.Glob(filepath.Join(directory, "*.json")) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read directory: %w", err) | ||
| } | ||
| for _, fullPath := range fullPaths { | ||
| // fullPath := filepath.Join(directory, fullPath) | ||
| if err = t.handleOtherWrite(fullPath); err != nil { | ||
| zap.L().Error("Failed to handle other write", | ||
| zap.Error(err), | ||
| zap.String("path", fullPath)) | ||
| } | ||
| } | ||
|
|
||
| // main loop | ||
| // 1. read allocations from other processes | ||
| // 2. write our allocations to a file | ||
| // 3. return when context is canceled | ||
| for { | ||
| select { | ||
| case <-writeTicks: | ||
| if err := t.handleWriteSelf(selfPath); err != nil { | ||
| zap.L().Error("Failed to write allocations", | ||
| zap.Error(err), | ||
| zap.String("path", selfPath)) | ||
| } | ||
| case <-ctx.Done(): | ||
| err := ctx.Err() | ||
| if err2 := watcher.Close(); err2 != nil { | ||
| err = errors.Join(err, fmt.Errorf("failed to close watcher: %w", err2)) | ||
| } | ||
| return err | ||
| case event := <-watcher.Events: | ||
| switch { | ||
| default: | ||
| zap.L().Warn("Unknown event", zap.Any("event", event)) | ||
| case event.Name == selfPath: | ||
| // ignore our writes | ||
| case event.Has(fsnotify.Write), event.Has(fsnotify.Create): | ||
| if err := t.handleOtherWrite(event.Name); err != nil { | ||
| zap.L().Error("Failed to handle other write", | ||
| zap.Error(err), | ||
| zap.String("path", event.Name)) | ||
| } | ||
| case event.Has(fsnotify.Remove): | ||
| if err := t.handleOtherRemove(event.Name); err != nil { | ||
| zap.L().Error("Failed to handle other remove", | ||
| zap.Error(err), | ||
| zap.String("path", event.Name)) | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func getPIDFromFilename(path string) (int, bool) { | ||
| basePath := filepath.Base(path) | ||
| dotIndex := strings.Index(basePath, ".") | ||
| if dotIndex == -1 { | ||
| zap.L().Warn("Ignoring file without extension", zap.String("file", path)) | ||
| return 0, false | ||
| } | ||
|
|
||
| pidStr := basePath[:dotIndex] | ||
| pid, err := strconv.Atoi(pidStr) | ||
| if err != nil { | ||
| zap.L().Error("Filename is not a number", zap.String("path", path), zap.Error(err)) | ||
| return 0, false | ||
| } | ||
|
|
||
| return pid, true | ||
| } | ||
|
|
||
| func (t *Tracker) handleOtherRemove(name string) error { | ||
| pid, ok := getPIDFromFilename(name) | ||
| if !ok { | ||
| return errInvalidMetricsFilename | ||
| } | ||
|
|
||
| t.otherLock.Lock() | ||
| defer t.otherLock.Unlock() | ||
|
|
||
| delete(t.otherMetrics, pid) | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| var errInvalidMetricsFilename = errors.New("invalid metrics filename") | ||
|
|
||
| func (t *Tracker) handleOtherWrite(name string) error { | ||
| pid, ok := getPIDFromFilename(name) | ||
| if !ok { | ||
| return errInvalidMetricsFilename | ||
| } | ||
|
|
||
| data, err := os.ReadFile(name) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to read file: %w", err) | ||
| } | ||
|
|
||
| var allocations Allocations | ||
| if err := json.Unmarshal(data, &allocations); err != nil { | ||
| return fmt.Errorf("failed to unmarshal file: %w", err) | ||
| } | ||
|
|
||
| t.otherLock.Lock() | ||
| defer t.otherLock.Unlock() | ||
|
|
||
| t.otherMetrics[pid] = allocations | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| type Allocations struct { | ||
| DiskBytes uint64 `json:"disk_bytes"` | ||
| MemoryBytes uint64 `json:"memory_bytes"` | ||
| Sandboxes uint32 `json:"sandboxes"` | ||
| VCPUs uint32 `json:"vcpus"` | ||
| } | ||
|
|
||
| func (t *Tracker) TotalAllocated() Allocations { | ||
| allocated := t.getSelfAllocated() | ||
|
|
||
| t.otherLock.RLock() | ||
| for _, item := range t.otherMetrics { | ||
| allocated.VCPUs += item.VCPUs | ||
| allocated.MemoryBytes += item.MemoryBytes | ||
| allocated.DiskBytes += item.DiskBytes | ||
| allocated.Sandboxes += item.Sandboxes | ||
| } | ||
| t.otherLock.RUnlock() | ||
|
|
||
| return allocated | ||
| } | ||
|
|
||
| func (t *Tracker) handleWriteSelf(selfPath string) error { | ||
| selfAllocated := t.getSelfAllocated() | ||
| data, err := json.Marshal(selfAllocated) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to marshal allocations: %w", err) | ||
| } | ||
| if err := os.WriteFile(selfPath, data, 0o644); err != nil { | ||
| return fmt.Errorf("failed to write allocations: %w", err) | ||
| } | ||
| return nil | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.