github.com/anth0d/nomad@v0.0.0-20221214183521-ae3a0a2cad06/client/structs/structs.go (about)

     1  package structs
     2  
     3  //go:generate codecgen -c github.com/hashicorp/go-msgpack/codec -st codec -d 102 -t codegen_generated -o structs.generated.go structs.go
     4  
     5  import (
     6  	"errors"
     7  	"time"
     8  
     9  	"github.com/hashicorp/nomad/client/stats"
    10  	"github.com/hashicorp/nomad/nomad/structs"
    11  	"github.com/hashicorp/nomad/plugins/device"
    12  )
    13  
    14  // RpcError is used for serializing errors with a potential error code
    15  type RpcError struct {
    16  	Message string
    17  	Code    *int64
    18  }
    19  
    20  func NewRpcError(err error, code *int64) *RpcError {
    21  	return &RpcError{
    22  		Message: err.Error(),
    23  		Code:    code,
    24  	}
    25  }
    26  
    27  func (r *RpcError) Error() string {
    28  	return r.Message
    29  }
    30  
    31  // ClientStatsResponse is used to return statistics about a node.
    32  type ClientStatsResponse struct {
    33  	HostStats *stats.HostStats
    34  	structs.QueryMeta
    35  }
    36  
    37  // MonitorRequest is used to request and stream logs from a client node.
    38  type MonitorRequest struct {
    39  	// LogLevel is the log level filter we want to stream logs on
    40  	LogLevel string
    41  
    42  	// LogJSON specifies if log format should be unstructured or json
    43  	LogJSON bool
    44  
    45  	// NodeID is the node we want to track the logs of
    46  	NodeID string
    47  
    48  	// ServerID is the server we want to track the logs of
    49  	ServerID string
    50  
    51  	// PlainText disables base64 encoding.
    52  	PlainText bool
    53  
    54  	structs.QueryOptions
    55  }
    56  
    57  // AllocFileInfo holds information about a file inside the AllocDir
    58  type AllocFileInfo struct {
    59  	Name        string
    60  	IsDir       bool
    61  	Size        int64
    62  	FileMode    string
    63  	ModTime     time.Time
    64  	ContentType string `json:",omitempty"`
    65  }
    66  
    67  // FsListRequest is used to list an allocation's directory.
    68  type FsListRequest struct {
    69  	// AllocID is the allocation to list from
    70  	AllocID string
    71  
    72  	// Path is the path to list
    73  	Path string
    74  
    75  	structs.QueryOptions
    76  }
    77  
    78  // FsListResponse is used to return the listings of an allocation's directory.
    79  type FsListResponse struct {
    80  	// Files are the result of listing a directory.
    81  	Files []*AllocFileInfo
    82  
    83  	structs.QueryMeta
    84  }
    85  
    86  // FsStatRequest is used to stat a file
    87  type FsStatRequest struct {
    88  	// AllocID is the allocation to stat the file in
    89  	AllocID string
    90  
    91  	// Path is the path to list
    92  	Path string
    93  
    94  	structs.QueryOptions
    95  }
    96  
    97  // FsStatResponse is used to return the stat results of a file
    98  type FsStatResponse struct {
    99  	// Info is the result of stating a file
   100  	Info *AllocFileInfo
   101  
   102  	structs.QueryMeta
   103  }
   104  
   105  // FsStreamRequest is the initial request for streaming the content of a file.
   106  type FsStreamRequest struct {
   107  	// AllocID is the allocation to stream logs from
   108  	AllocID string
   109  
   110  	// Path is the path to the file to stream
   111  	Path string
   112  
   113  	// Offset is the offset to start streaming data at.
   114  	Offset int64
   115  
   116  	// Origin can either be "start" or "end" and determines where the offset is
   117  	// applied.
   118  	Origin string
   119  
   120  	// PlainText disables base64 encoding.
   121  	PlainText bool
   122  
   123  	// Limit is the number of bytes to read
   124  	Limit int64
   125  
   126  	// Follow follows the file.
   127  	Follow bool
   128  
   129  	structs.QueryOptions
   130  }
   131  
   132  // FsLogsRequest is the initial request for accessing allocation logs.
   133  type FsLogsRequest struct {
   134  	// AllocID is the allocation to stream logs from
   135  	AllocID string
   136  
   137  	// Task is the task to stream logs from
   138  	Task string
   139  
   140  	// LogType indicates whether "stderr" or "stdout" should be streamed
   141  	LogType string
   142  
   143  	// Offset is the offset to start streaming data at.
   144  	Offset int64
   145  
   146  	// Origin can either be "start" or "end" and determines where the offset is
   147  	// applied.
   148  	Origin string
   149  
   150  	// PlainText disables base64 encoding.
   151  	PlainText bool
   152  
   153  	// Follow follows logs.
   154  	Follow bool
   155  
   156  	structs.QueryOptions
   157  }
   158  
   159  // StreamErrWrapper is used to serialize output of a stream of a file or logs.
   160  type StreamErrWrapper struct {
   161  	// Error stores any error that may have occurred.
   162  	Error *RpcError
   163  
   164  	// Payload is the payload
   165  	Payload []byte
   166  }
   167  
   168  // AllocExecRequest is the initial request for execing into an Alloc task
   169  type AllocExecRequest struct {
   170  	// AllocID is the allocation to stream logs from
   171  	AllocID string
   172  
   173  	// Task is the task to stream logs from
   174  	Task string
   175  
   176  	// Tty indicates whether to allocate a pseudo-TTY
   177  	Tty bool
   178  
   179  	// Cmd is the command to be executed
   180  	Cmd []string
   181  
   182  	structs.QueryOptions
   183  }
   184  
   185  // AllocChecksRequest is used to request the latest nomad service discovery
   186  // check status information of a given allocation.
   187  type AllocChecksRequest struct {
   188  	structs.QueryOptions
   189  	AllocID string
   190  }
   191  
   192  // AllocChecksResponse is used to return the latest nomad service discovery
   193  // check status information of a given allocation.
   194  type AllocChecksResponse struct {
   195  	structs.QueryMeta
   196  	Results map[structs.CheckID]*structs.CheckQueryResult
   197  }
   198  
   199  // AllocStatsRequest is used to request the resource usage of a given
   200  // allocation, potentially filtering by task
   201  type AllocStatsRequest struct {
   202  	// AllocID is the allocation to retrieves stats for
   203  	AllocID string
   204  
   205  	// Task is an optional filter to only request stats for the task.
   206  	Task string
   207  
   208  	structs.QueryOptions
   209  }
   210  
   211  // AllocStatsResponse is used to return the resource usage of a given
   212  // allocation.
   213  type AllocStatsResponse struct {
   214  	Stats *AllocResourceUsage
   215  	structs.QueryMeta
   216  }
   217  
   218  // MemoryStats holds memory usage related stats
   219  type MemoryStats struct {
   220  	RSS            uint64
   221  	Cache          uint64
   222  	Swap           uint64
   223  	MappedFile     uint64
   224  	Usage          uint64
   225  	MaxUsage       uint64
   226  	KernelUsage    uint64
   227  	KernelMaxUsage uint64
   228  
   229  	// A list of fields whose values were actually sampled
   230  	Measured []string
   231  }
   232  
   233  func (ms *MemoryStats) Add(other *MemoryStats) {
   234  	if other == nil {
   235  		return
   236  	}
   237  
   238  	ms.RSS += other.RSS
   239  	ms.Cache += other.Cache
   240  	ms.Swap += other.Swap
   241  	ms.MappedFile += other.MappedFile
   242  	ms.Usage += other.Usage
   243  	ms.MaxUsage += other.MaxUsage
   244  	ms.KernelUsage += other.KernelUsage
   245  	ms.KernelMaxUsage += other.KernelMaxUsage
   246  	ms.Measured = joinStringSet(ms.Measured, other.Measured)
   247  }
   248  
   249  // CpuStats holds cpu usage related stats
   250  type CpuStats struct {
   251  	SystemMode       float64
   252  	UserMode         float64
   253  	TotalTicks       float64
   254  	ThrottledPeriods uint64
   255  	ThrottledTime    uint64
   256  	Percent          float64
   257  
   258  	// A list of fields whose values were actually sampled
   259  	Measured []string
   260  }
   261  
   262  func (cs *CpuStats) Add(other *CpuStats) {
   263  	if other == nil {
   264  		return
   265  	}
   266  
   267  	cs.SystemMode += other.SystemMode
   268  	cs.UserMode += other.UserMode
   269  	cs.TotalTicks += other.TotalTicks
   270  	cs.ThrottledPeriods += other.ThrottledPeriods
   271  	cs.ThrottledTime += other.ThrottledTime
   272  	cs.Percent += other.Percent
   273  	cs.Measured = joinStringSet(cs.Measured, other.Measured)
   274  }
   275  
   276  // ResourceUsage holds information related to cpu and memory stats
   277  type ResourceUsage struct {
   278  	MemoryStats *MemoryStats
   279  	CpuStats    *CpuStats
   280  	DeviceStats []*device.DeviceGroupStats
   281  }
   282  
   283  func (ru *ResourceUsage) Add(other *ResourceUsage) {
   284  	ru.MemoryStats.Add(other.MemoryStats)
   285  	ru.CpuStats.Add(other.CpuStats)
   286  	ru.DeviceStats = append(ru.DeviceStats, other.DeviceStats...)
   287  }
   288  
   289  // TaskResourceUsage holds aggregated resource usage of all processes in a Task
   290  // and the resource usage of the individual pids
   291  type TaskResourceUsage struct {
   292  	ResourceUsage *ResourceUsage
   293  	Timestamp     int64 // UnixNano
   294  	Pids          map[string]*ResourceUsage
   295  }
   296  
   297  // AllocResourceUsage holds the aggregated task resource usage of the
   298  // allocation.
   299  type AllocResourceUsage struct {
   300  	// ResourceUsage is the summation of the task resources
   301  	ResourceUsage *ResourceUsage
   302  
   303  	// Tasks contains the resource usage of each task
   304  	Tasks map[string]*TaskResourceUsage
   305  
   306  	// The max timestamp of all the Tasks
   307  	Timestamp int64
   308  }
   309  
   310  // joinStringSet takes two slices of strings and joins them
   311  func joinStringSet(s1, s2 []string) []string {
   312  	lookup := make(map[string]struct{}, len(s1))
   313  	j := make([]string, 0, len(s1))
   314  	for _, s := range s1 {
   315  		j = append(j, s)
   316  		lookup[s] = struct{}{}
   317  	}
   318  
   319  	for _, s := range s2 {
   320  		if _, ok := lookup[s]; !ok {
   321  			j = append(j, s)
   322  		}
   323  	}
   324  
   325  	return j
   326  }
   327  
   328  // HealthCheckRequest is the request type for a type that fulfils the Health
   329  // Check interface
   330  type HealthCheckRequest struct{}
   331  
   332  // HealthCheckResponse is the response type for a type that fulfills the Health
   333  // Check interface
   334  type HealthCheckResponse struct {
   335  	// Drivers is a map of driver names to current driver information
   336  	Drivers map[string]*structs.DriverInfo
   337  }
   338  
   339  type HealthCheckIntervalRequest struct{}
   340  type HealthCheckIntervalResponse struct {
   341  	Eligible bool
   342  	Period   time.Duration
   343  }
   344  
   345  // AddDriverInfo adds information about a driver to the fingerprint response.
   346  // If the Drivers field has not yet been initialized, it does so here.
   347  func (h *HealthCheckResponse) AddDriverInfo(name string, driverInfo *structs.DriverInfo) {
   348  	// initialize Drivers if it has not been already
   349  	if h.Drivers == nil {
   350  		h.Drivers = make(map[string]*structs.DriverInfo)
   351  	}
   352  
   353  	h.Drivers[name] = driverInfo
   354  }
   355  
   356  // CheckBufSize is the size of the buffer that is used for job output
   357  const CheckBufSize = 4 * 1024
   358  
   359  // DriverStatsNotImplemented is the error to be returned if a driver doesn't
   360  // implement stats.
   361  var DriverStatsNotImplemented = errors.New("stats not implemented for driver")