-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Gyuho Lee <[email protected]>
- Loading branch information
Showing
8 changed files
with
1,100 additions
and
28 deletions.
There are no files selected for viewing
This file contains 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 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,98 @@ | ||
package fuse | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
"time" | ||
|
||
fuse_id "github.com/leptonai/gpud/components/fuse/id" | ||
"github.com/leptonai/gpud/components/fuse/metrics" | ||
"github.com/leptonai/gpud/components/fuse/state" | ||
components_metrics "github.com/leptonai/gpud/components/metrics" | ||
"github.com/leptonai/gpud/components/query" | ||
"github.com/leptonai/gpud/pkg/fuse" | ||
) | ||
|
||
type Output struct { | ||
ConnectionInfos []fuse.ConnectionInfo `json:"connection_infos"` | ||
} | ||
|
||
var ( | ||
defaultPollerOnce sync.Once | ||
defaultPoller query.Poller | ||
) | ||
|
||
// only set once since it relies on the kube client and specific port | ||
func setDefaultPoller(cfg Config) { | ||
defaultPollerOnce.Do(func() { | ||
defaultPoller = query.New( | ||
fuse_id.Name, | ||
cfg.Query, | ||
CreateGet(cfg), | ||
nil, | ||
) | ||
}) | ||
} | ||
|
||
func getDefaultPoller() query.Poller { | ||
return defaultPoller | ||
} | ||
|
||
func CreateGet(cfg Config) query.GetFunc { | ||
return func(ctx context.Context) (_ any, e error) { | ||
defer func() { | ||
if e != nil { | ||
components_metrics.SetGetFailed(fuse_id.Name) | ||
} else { | ||
components_metrics.SetGetSuccess(fuse_id.Name) | ||
} | ||
}() | ||
|
||
infos, err := fuse.ListConnections() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
now := time.Now().UTC() | ||
nowUTC := float64(now.Unix()) | ||
metrics.SetLastUpdateUnixSeconds(nowUTC) | ||
|
||
foundDev := make(map[string]fuse.ConnectionInfo) | ||
for _, info := range infos { | ||
// to dedup fuse connection stats by device name | ||
if _, ok := foundDev[info.DeviceName]; ok { | ||
continue | ||
} | ||
|
||
prev, err := state.FindEvent(ctx, cfg.Query.State.DB, now.Unix(), info.DeviceName) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if prev == nil { | ||
continue | ||
} | ||
|
||
if err := state.InsertEvent(ctx, cfg.Query.State.DB, state.Event{ | ||
UnixSeconds: now.Unix(), | ||
DeviceName: info.DeviceName, | ||
CongestedPercentAgainstThreshold: info.CongestedPercent, | ||
MaxBackgroundPercentAgainstThreshold: info.MaxBackgroundPercent, | ||
}); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := metrics.SetConnectionsCongestedPercent(ctx, info.DeviceName, info.CongestedPercent, now); err != nil { | ||
return nil, err | ||
} | ||
if err := metrics.SetConnectionsMaxBackgroundPercent(ctx, info.DeviceName, info.MaxBackgroundPercent, now); err != nil { | ||
return nil, err | ||
} | ||
|
||
foundDev[info.DeviceName] = info | ||
} | ||
|
||
return &Output{ | ||
ConnectionInfos: infos, | ||
}, nil | ||
} | ||
} |
This file contains 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,51 @@ | ||
package fuse | ||
|
||
import ( | ||
"database/sql" | ||
"encoding/json" | ||
|
||
query_config "github.com/leptonai/gpud/components/query/config" | ||
) | ||
|
||
type Config struct { | ||
Query query_config.Config `json:"query"` | ||
|
||
// CongestedPercentAgainstThreshold is the percentage of the FUSE connections waiting | ||
// at which we consider the system to be congested. | ||
CongestedPercentAgainstThreshold float64 `json:"congested_percent_against_threshold"` | ||
|
||
// MaxBackgroundPercentAgainstThreshold is the percentage of the FUSE connections waiting | ||
// at which we consider the system to be congested. | ||
MaxBackgroundPercentAgainstThreshold float64 `json:"max_background_percent_against_threshold"` | ||
} | ||
|
||
func ParseConfig(b any, db *sql.DB) (*Config, error) { | ||
raw, err := json.Marshal(b) | ||
if err != nil { | ||
return nil, err | ||
} | ||
cfg := new(Config) | ||
err = json.Unmarshal(raw, cfg) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if cfg.Query.State != nil { | ||
cfg.Query.State.DB = db | ||
} | ||
return cfg, nil | ||
} | ||
|
||
const ( | ||
DefaultCongestedPercentAgainstThreshold = float64(90) | ||
DefaultMaxBackgroundPercentAgainstThreshold = float64(80) | ||
) | ||
|
||
func (cfg *Config) Validate() error { | ||
if cfg.CongestedPercentAgainstThreshold == 0 { | ||
cfg.CongestedPercentAgainstThreshold = DefaultCongestedPercentAgainstThreshold | ||
} | ||
if cfg.MaxBackgroundPercentAgainstThreshold == 0 { | ||
cfg.MaxBackgroundPercentAgainstThreshold = DefaultMaxBackgroundPercentAgainstThreshold | ||
} | ||
return nil | ||
} |
Oops, something went wrong.