-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: resouce manager * feat: add resource manager flags * feat: add resource manager to dowmloader service * feat: add resource manager to challenge service * feat: add resource manager to task node service * chore: rich log info for rcmgr * feat: init global resource manager * fix: integration testing bugs * feat: return preparations error to client during put object * chore: fix cr comments
- Loading branch information
1 parent
c6ebd0b
commit 90b5039
Showing
21 changed files
with
1,507 additions
and
22 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
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
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,68 @@ | ||
package rcmgr | ||
|
||
import ( | ||
"errors" | ||
) | ||
|
||
// ErrResourceLimitExceeded is returned when attempting to perform an operation that would | ||
// exceed system resource limits. | ||
var ErrResourceLimitExceeded = errors.New("resource limit exceeded") | ||
|
||
// ErrResourceScopeClosed is returned when attempting to reserve resources in a closed resource | ||
// scope. | ||
var ErrResourceScopeClosed = errors.New("resource scope closed") | ||
|
||
type ErrMemoryLimitExceeded struct { | ||
current, attempted, limit int64 | ||
priority uint8 | ||
err error | ||
} | ||
|
||
func (e *ErrMemoryLimitExceeded) Error() string { return e.err.Error() } | ||
func (e *ErrMemoryLimitExceeded) Unwrap() error { return e.err } | ||
|
||
// edge may be empty if this is not an edge error | ||
func logValuesMemoryLimit(scope, edge string, stat ScopeStat, err error) []interface{} { | ||
logValues := make([]interface{}, 0, 2*8) | ||
logValues = append(logValues, "scope", scope) | ||
if edge != "" { | ||
logValues = append(logValues, "edge", edge) | ||
} | ||
var e *ErrMemoryLimitExceeded | ||
if errors.As(err, &e) { | ||
logValues = append(logValues, | ||
"current", e.current, | ||
"attempted", e.attempted, | ||
"priority", e.priority, | ||
"limit", e.limit, | ||
) | ||
} | ||
return append(logValues, "stat", stat, "error", err) | ||
} | ||
|
||
type ErrConnLimitExceeded struct { | ||
current, attempted, limit int | ||
err error | ||
} | ||
|
||
func (e *ErrConnLimitExceeded) Error() string { return e.err.Error() } | ||
func (e *ErrConnLimitExceeded) Unwrap() error { return e.err } | ||
|
||
// edge may be empty if this is not an edge error | ||
func logValuesConnLimit(scope, edge string, dir Direction, stat ScopeStat, err error) []interface{} { | ||
logValues := make([]interface{}, 0, 2*9) | ||
logValues = append(logValues, "scope", scope) | ||
if edge != "" { | ||
logValues = append(logValues, "edge", edge) | ||
} | ||
logValues = append(logValues, "direction", dir) | ||
var e *ErrConnLimitExceeded | ||
if errors.As(err, &e) { | ||
logValues = append(logValues, | ||
"current", e.current, | ||
"attempted", e.attempted, | ||
"limit", e.limit, | ||
) | ||
} | ||
return append(logValues, "stat", stat, "error", err) | ||
} |
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,106 @@ | ||
package rcmgr | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
|
||
"github.com/shirou/gopsutil/mem" | ||
|
||
"github.com/bnb-chain/greenfield-storage-provider/pkg/log" | ||
) | ||
|
||
const ( | ||
LimitFactor = 0.85 | ||
DefaultMemorySize uint64 = 8 * 1024 * 1024 | ||
) | ||
|
||
// Limit is an interface that that specifies basic resource limits. | ||
type Limit interface { | ||
// GetMemoryLimit returns the (current) memory limit. | ||
GetMemoryLimit() int64 | ||
// GetConnLimit returns the connection limit, for inbound or outbound connections. | ||
GetConnLimit(Direction) int | ||
// GetConnTotalLimit returns the total connection limit | ||
GetConnTotalLimit() int | ||
// GetFDLimit returns the file descriptor limit. | ||
GetFDLimit() int | ||
// String returns the Limit state string | ||
String() string | ||
} | ||
|
||
// Limiter is an interface for providing limits to the resource manager. | ||
type Limiter interface { | ||
GetSystemLimits() Limit | ||
GetTransientLimits() Limit | ||
GetServiceLimits(svc string) Limit | ||
String() string | ||
} | ||
|
||
var _ Limit = &BaseLimit{} | ||
|
||
// BaseLimit is a mixin type for basic resource limits. | ||
type BaseLimit struct { | ||
Conns int `json:",omitempty"` | ||
ConnsInbound int `json:",omitempty"` | ||
ConnsOutbound int `json:",omitempty"` | ||
FD int `json:",omitempty"` | ||
Memory int64 `json:",omitempty"` | ||
} | ||
|
||
// GetMemoryLimit returns the (current) memory limit. | ||
func (limit *BaseLimit) GetMemoryLimit() int64 { | ||
return limit.Memory | ||
} | ||
|
||
// GetConnLimit returns the connection limit, for inbound or outbound connections. | ||
func (limit *BaseLimit) GetConnLimit(direction Direction) int { | ||
if direction == DirInbound { | ||
return limit.ConnsInbound | ||
} | ||
return limit.ConnsOutbound | ||
} | ||
|
||
// GetConnTotalLimit returns the total connection limit | ||
func (limit *BaseLimit) GetConnTotalLimit() int { | ||
return limit.Conns | ||
} | ||
|
||
// GetFDLimit returns the file descriptor limit. | ||
func (limit *BaseLimit) GetFDLimit() int { | ||
return limit.FD | ||
} | ||
|
||
// String returns the Limit state string | ||
// TODO:: supports connection and fd field | ||
func (limit *BaseLimit) String() string { | ||
return fmt.Sprintf("memory limits %d", limit.Memory) | ||
} | ||
|
||
// InfiniteBaseLimit are a limiter configuration that uses unlimited limits, thus effectively not limiting anything. | ||
// Keep in mind that the operating system limits the number of file descriptors that an application can use. | ||
var InfiniteBaseLimit = BaseLimit{ | ||
Conns: math.MaxInt, | ||
ConnsInbound: math.MaxInt, | ||
ConnsOutbound: math.MaxInt, | ||
FD: math.MaxInt, | ||
Memory: math.MaxInt64, | ||
} | ||
|
||
// DynamicLimits generate limits by os resource | ||
func DynamicLimits() *BaseLimit { | ||
availableMem := DefaultMemorySize | ||
virtualMem, err := mem.VirtualMemory() | ||
if err != nil { | ||
log.Errorw("failed to get os memory states", "error", err) | ||
} else { | ||
availableMem = virtualMem.Available | ||
} | ||
limits := &BaseLimit{} | ||
limits.Memory = int64(float64(availableMem) * LimitFactor) | ||
// TODO:: get from os and compatible with a variety of os | ||
limits.FD = math.MaxInt | ||
limits.Conns = math.MaxInt | ||
limits.ConnsInbound = math.MaxInt | ||
limits.ConnsOutbound = math.MaxInt | ||
return limits | ||
} |
Oops, something went wrong.