github.com/bigcommerce/nomad@v0.9.3-bc/client/structs/structs.go (about)

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