github.com/iqoqo/nomad@v0.11.3-0.20200911112621-d7021c74d101/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  // AllocStatsRequest is used to request the resource usage of a given
   186  // allocation, potentially filtering by task
   187  type AllocStatsRequest struct {
   188  	// AllocID is the allocation to retrieves stats for
   189  	AllocID string
   190  
   191  	// Task is an optional filter to only request stats for the task.
   192  	Task string
   193  
   194  	structs.QueryOptions
   195  }
   196  
   197  // AllocStatsResponse is used to return the resource usage of a given
   198  // allocation.
   199  type AllocStatsResponse struct {
   200  	Stats *AllocResourceUsage
   201  	structs.QueryMeta
   202  }
   203  
   204  // MemoryStats holds memory usage related stats
   205  type MemoryStats struct {
   206  	RSS            uint64
   207  	Cache          uint64
   208  	Swap           uint64
   209  	Usage          uint64
   210  	MaxUsage       uint64
   211  	KernelUsage    uint64
   212  	KernelMaxUsage uint64
   213  
   214  	// A list of fields whose values were actually sampled
   215  	Measured []string
   216  }
   217  
   218  func (ms *MemoryStats) Add(other *MemoryStats) {
   219  	if other == nil {
   220  		return
   221  	}
   222  
   223  	ms.RSS += other.RSS
   224  	ms.Cache += other.Cache
   225  	ms.Swap += other.Swap
   226  	ms.Usage += other.Usage
   227  	ms.MaxUsage += other.MaxUsage
   228  	ms.KernelUsage += other.KernelUsage
   229  	ms.KernelMaxUsage += other.KernelMaxUsage
   230  	ms.Measured = joinStringSet(ms.Measured, other.Measured)
   231  }
   232  
   233  // CpuStats holds cpu usage related stats
   234  type CpuStats struct {
   235  	SystemMode       float64
   236  	UserMode         float64
   237  	TotalTicks       float64
   238  	ThrottledPeriods uint64
   239  	ThrottledTime    uint64
   240  	Percent          float64
   241  
   242  	// A list of fields whose values were actually sampled
   243  	Measured []string
   244  }
   245  
   246  func (cs *CpuStats) Add(other *CpuStats) {
   247  	if other == nil {
   248  		return
   249  	}
   250  
   251  	cs.SystemMode += other.SystemMode
   252  	cs.UserMode += other.UserMode
   253  	cs.TotalTicks += other.TotalTicks
   254  	cs.ThrottledPeriods += other.ThrottledPeriods
   255  	cs.ThrottledTime += other.ThrottledTime
   256  	cs.Percent += other.Percent
   257  	cs.Measured = joinStringSet(cs.Measured, other.Measured)
   258  }
   259  
   260  // ResourceUsage holds information related to cpu and memory stats
   261  type ResourceUsage struct {
   262  	MemoryStats *MemoryStats
   263  	CpuStats    *CpuStats
   264  	DeviceStats []*device.DeviceGroupStats
   265  }
   266  
   267  func (ru *ResourceUsage) Add(other *ResourceUsage) {
   268  	ru.MemoryStats.Add(other.MemoryStats)
   269  	ru.CpuStats.Add(other.CpuStats)
   270  	ru.DeviceStats = append(ru.DeviceStats, other.DeviceStats...)
   271  }
   272  
   273  // TaskResourceUsage holds aggregated resource usage of all processes in a Task
   274  // and the resource usage of the individual pids
   275  type TaskResourceUsage struct {
   276  	ResourceUsage *ResourceUsage
   277  	Timestamp     int64 // UnixNano
   278  	Pids          map[string]*ResourceUsage
   279  }
   280  
   281  // AllocResourceUsage holds the aggregated task resource usage of the
   282  // allocation.
   283  type AllocResourceUsage struct {
   284  	// ResourceUsage is the summation of the task resources
   285  	ResourceUsage *ResourceUsage
   286  
   287  	// Tasks contains the resource usage of each task
   288  	Tasks map[string]*TaskResourceUsage
   289  
   290  	// The max timestamp of all the Tasks
   291  	Timestamp int64
   292  }
   293  
   294  // joinStringSet takes two slices of strings and joins them
   295  func joinStringSet(s1, s2 []string) []string {
   296  	lookup := make(map[string]struct{}, len(s1))
   297  	j := make([]string, 0, len(s1))
   298  	for _, s := range s1 {
   299  		j = append(j, s)
   300  		lookup[s] = struct{}{}
   301  	}
   302  
   303  	for _, s := range s2 {
   304  		if _, ok := lookup[s]; !ok {
   305  			j = append(j, s)
   306  		}
   307  	}
   308  
   309  	return j
   310  }
   311  
   312  // HealthCheckRequest is the request type for a type that fulfils the Health
   313  // Check interface
   314  type HealthCheckRequest struct{}
   315  
   316  // HealthCheckResponse is the response type for a type that fulfills the Health
   317  // Check interface
   318  type HealthCheckResponse struct {
   319  	// Drivers is a map of driver names to current driver information
   320  	Drivers map[string]*structs.DriverInfo
   321  }
   322  
   323  type HealthCheckIntervalRequest struct{}
   324  type HealthCheckIntervalResponse struct {
   325  	Eligible bool
   326  	Period   time.Duration
   327  }
   328  
   329  // AddDriverInfo adds information about a driver to the fingerprint response.
   330  // If the Drivers field has not yet been initialized, it does so here.
   331  func (h *HealthCheckResponse) AddDriverInfo(name string, driverInfo *structs.DriverInfo) {
   332  	// initialize Drivers if it has not been already
   333  	if h.Drivers == nil {
   334  		h.Drivers = make(map[string]*structs.DriverInfo, 0)
   335  	}
   336  
   337  	h.Drivers[name] = driverInfo
   338  }
   339  
   340  // CheckBufSize is the size of the buffer that is used for job output
   341  const CheckBufSize = 4 * 1024
   342  
   343  // DriverStatsNotImplemented is the error to be returned if a driver doesn't
   344  // implement stats.
   345  var DriverStatsNotImplemented = errors.New("stats not implemented for driver")