util

package
v0.3.52 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 24, 2023 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

View Source
const MaxMessageLength = 2048

Variables

This section is empty.

Functions

func Batch

func Batch(elements []string, batchSize int) [][]string

func CloseToDeadline

func CloseToDeadline(ctx context.Context, tolerance time.Duration) bool

func Concat added in v0.3.49

func Concat[T any](slices ...[]T) []T

func ContainsString

func ContainsString(list []string, val string) bool

func DeepCopy

func DeepCopy(a map[string]string) map[string]string

func DeepCopyListUint32

func DeepCopyListUint32(list []uint32) []uint32

func Equal

func Equal(a map[string]string, b map[string]string) bool

func Filter

func Filter[S ~[]E, E any](s S, predicate func(e E) bool) S

func FilterKeys

func FilterKeys(a map[string]string, keys []string) map[string]string

func FormatBinarySI

func FormatBinarySI(q int64) string

func GetClusterAvailableCapacity

func GetClusterAvailableCapacity(report *api.ClusterUsageReport) armadaresource.ComputeResources

GetClusterAvailableCapacity returns the total resource to be shared amongst queues. This is the total capacity available to armada on schedulable nodes + the capacity currently in use on unschedulable nodes.

func GetClusterCapacity

func GetClusterCapacity(report *api.ClusterUsageReport) armadaresource.ComputeResources

GetClusterCapacity returns the total capacity on all nodes on a cluster, even if they are unschedulable.

func GetOrDefault

func GetOrDefault(m map[string]float64, key string, def float64) float64

func GetQueueReports

func GetQueueReports(report *api.ClusterUsageReport) []*api.QueueReport

func Hash added in v0.3.50

func Hash(v interface{}, format Format, opts *HashOptions) (uint64, error)

Hash returns the hash value of an arbitrary value.

If opts is nil, then default options will be used. See HashOptions for the default values. The same *HashOptions value cannot be used concurrently. None of the values within a *HashOptions struct are safe to read/write while hashing is being done.

The "format" is required and must be one of the format values defined by this library. You should probably just use "FormatV2". This allows generated hashes uses alternate logic to maintain compatibility with older versions.

Notes on the value:

  • Unexported fields on structs are ignored and do not affect the hash value.

  • Adding an exported field to a struct with the zero value will change the hash value.

For structs, the hashing can be controlled using tags. For example:

struct {
    Name string
    UUID string `hash:"ignore"`
}

The available tag values are:

  • "ignore" or "-" - The field will be ignored and not affect the hash code.

  • "set" - The field will be treated as a set, where ordering doesn't affect the hash code. This only works for slices.

  • "string" - The field will be hashed as a string, only works when the field implements fmt.Stringer

func InverseMap

func InverseMap[K comparable, V comparable](a map[K]V) map[V]K

InverseMap creates a new map where each key: value pair is swapped If the same value is present multiple times, a random one will be selected (depending on map key-value iteration)

func Map added in v0.3.50

func Map[T any, U any](list []T, fn func(val T) U) []U

func MergeMaps

func MergeMaps(a map[string]string, b map[string]string) map[string]string

func Min

func Min(a, b int) int

func NewThreadsafeRand

func NewThreadsafeRand(seed int64) *rand.Rand

NewThreadsafeRand Returns a *rand.Rand that is safe to share across multiple goroutines

func NewULID

func NewULID() string

NewULID returns a new ULID for the current time generated from a global RNG. The ULID is returned as a string converted to lower-case to ensure it is a valid DNS subdomain name; see https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names

func PodSpecFromJob

func PodSpecFromJob(job *api.Job) *v1.PodSpec

func RemoveNullsFromJson

func RemoveNullsFromJson(json []byte) []byte

func RemoveNullsFromString

func RemoveNullsFromString(s string) string

func StringFromUlid

func StringFromUlid(id ulid.ULID) string

StringFromUlid returns a string representation of a proto UUID. Because Kubernetes requires ids to be valid DNS subdomain names, the string is returned in lower-case; see https://kubernetes.io/docs/concepts/overview/working-with-objects/names/#dns-subdomain-names

func StringListToSet

func StringListToSet(list []string) map[string]bool

func SubtractStringList

func SubtractStringList(a []string, b []string) []string

func SumReportClusterCapacity

func SumReportClusterCapacity(reports map[string]*api.ClusterUsageReport) armadaresource.ComputeResources

func Truncate

func Truncate(s string, max int) string

func ULID

func ULID() ulid.ULID

ULID returns a new ULID for the current time generated from a global RNG.

Types

type Clock

type Clock interface {
	Now() time.Time
}

type DefaultClock

type DefaultClock struct{}

func (*DefaultClock) Now

func (c *DefaultClock) Now() time.Time

type DummyClock

type DummyClock struct {
	T time.Time
}

func (*DummyClock) Now

func (c *DummyClock) Now() time.Time

type Format added in v0.3.50

type Format uint

Format specifies the hashing process used. Different formats typically generate different hashes for the same value and have different properties.

const (

	// FormatV1 is the format used in v1.x of this library. This has the
	// downsides noted in issue #18 but allows simultaneous v1/v2 usage.
	FormatV1 Format

	// FormatV2 is the current recommended format and fixes the issues
	// noted in FormatV1.
	FormatV2
)

type HashOptions added in v0.3.50

type HashOptions struct {
	// Hasher is the hash function to use. If this isn't set, it will
	// default to FNV.
	Hasher hash.Hash64

	// TagName is the struct tag to look at when hashing the structure.
	// By default this is "hash".
	TagName string

	// ZeroNil is flag determining if nil pointer should be treated equal
	// to a zero value of pointed type. By default this is false.
	ZeroNil bool

	// IgnoreZeroValue is determining if zero value fields should be
	// ignored for hash calculation.
	IgnoreZeroValue bool

	// SlicesAsSets assumes that a `set` tag is always present for slices.
	// Default is false (in which case the tag is used instead)
	SlicesAsSets bool

	// UseStringer will attempt to use fmt.Stringer always. If the struct
	// doesn't implement fmt.Stringer, it'll fall back to trying usual tricks.
	// If this is true, and the "string" tag is also set, the tag takes
	// precedence (meaning that if the type doesn't implement fmt.Stringer, we
	// panic)
	UseStringer bool
}

HashOptions are options that are available for hashing.

type LockedSource

type LockedSource struct {
	// contains filtered or unexported fields
}

LockedSource is a random source that is uses a mutex to ensure it is threadsafe

func (*LockedSource) Int63

func (r *LockedSource) Int63() (n int64)

func (*LockedSource) Seed

func (r *LockedSource) Seed(seed int64)

type StringInterner added in v0.3.47

type StringInterner struct {
	// contains filtered or unexported fields
}

StringInterner allows strings with equal values but different backing arrays to be deduplicated This is useful in Armada where common strings are often used across jobs (e.g. jobset, queue) and so deduplication can help save memory. The Interner is backed by an LRU so that only the most recently interned strings are kept. Note that this probably isn't the most efficient implementation (eg see https://github.com/josharian/intern which abuses sync.pool) but this should be reliable and performance more than good enough for Armada use cases

func NewStringInterner added in v0.3.47

func NewStringInterner(cacheSize uint32) (*StringInterner, error)

NewStringInterner will allocate an Interner backed by an LRU limited to the provided size

func (*StringInterner) Intern added in v0.3.47

func (i *StringInterner) Intern(s string) string

Intern ensures the string is cached and returns the cached string

type UTCClock

type UTCClock struct{}

func (*UTCClock) Now

func (c *UTCClock) Now() time.Time

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL