github.com/smithx10/nomad@v0.9.1-rc1/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  // AllocStatsRequest is used to request the resource usage of a given
   148  // allocation, potentially filtering by task
   149  type AllocStatsRequest struct {
   150  	// AllocID is the allocation to retrieves stats for
   151  	AllocID string
   152  
   153  	// Task is an optional filter to only request stats for the task.
   154  	Task string
   155  
   156  	structs.QueryOptions
   157  }
   158  
   159  // AllocStatsResponse is used to return the resource usage of a given
   160  // allocation.
   161  type AllocStatsResponse struct {
   162  	Stats *AllocResourceUsage
   163  	structs.QueryMeta
   164  }
   165  
   166  // MemoryStats holds memory usage related stats
   167  type MemoryStats struct {
   168  	RSS            uint64
   169  	Cache          uint64
   170  	Swap           uint64
   171  	Usage          uint64
   172  	MaxUsage       uint64
   173  	KernelUsage    uint64
   174  	KernelMaxUsage uint64
   175  
   176  	// A list of fields whose values were actually sampled
   177  	Measured []string
   178  }
   179  
   180  func (ms *MemoryStats) Add(other *MemoryStats) {
   181  	if other == nil {
   182  		return
   183  	}
   184  
   185  	ms.RSS += other.RSS
   186  	ms.Cache += other.Cache
   187  	ms.Swap += other.Swap
   188  	ms.Usage += other.Usage
   189  	ms.MaxUsage += other.MaxUsage
   190  	ms.KernelUsage += other.KernelUsage
   191  	ms.KernelMaxUsage += other.KernelMaxUsage
   192  	ms.Measured = joinStringSet(ms.Measured, other.Measured)
   193  }
   194  
   195  // CpuStats holds cpu usage related stats
   196  type CpuStats struct {
   197  	SystemMode       float64
   198  	UserMode         float64
   199  	TotalTicks       float64
   200  	ThrottledPeriods uint64
   201  	ThrottledTime    uint64
   202  	Percent          float64
   203  
   204  	// A list of fields whose values were actually sampled
   205  	Measured []string
   206  }
   207  
   208  func (cs *CpuStats) Add(other *CpuStats) {
   209  	if other == nil {
   210  		return
   211  	}
   212  
   213  	cs.SystemMode += other.SystemMode
   214  	cs.UserMode += other.UserMode
   215  	cs.TotalTicks += other.TotalTicks
   216  	cs.ThrottledPeriods += other.ThrottledPeriods
   217  	cs.ThrottledTime += other.ThrottledTime
   218  	cs.Percent += other.Percent
   219  	cs.Measured = joinStringSet(cs.Measured, other.Measured)
   220  }
   221  
   222  // ResourceUsage holds information related to cpu and memory stats
   223  type ResourceUsage struct {
   224  	MemoryStats *MemoryStats
   225  	CpuStats    *CpuStats
   226  	DeviceStats []*device.DeviceGroupStats
   227  }
   228  
   229  func (ru *ResourceUsage) Add(other *ResourceUsage) {
   230  	ru.MemoryStats.Add(other.MemoryStats)
   231  	ru.CpuStats.Add(other.CpuStats)
   232  	ru.DeviceStats = append(ru.DeviceStats, other.DeviceStats...)
   233  }
   234  
   235  // TaskResourceUsage holds aggregated resource usage of all processes in a Task
   236  // and the resource usage of the individual pids
   237  type TaskResourceUsage struct {
   238  	ResourceUsage *ResourceUsage
   239  	Timestamp     int64 // UnixNano
   240  	Pids          map[string]*ResourceUsage
   241  }
   242  
   243  // AllocResourceUsage holds the aggregated task resource usage of the
   244  // allocation.
   245  type AllocResourceUsage struct {
   246  	// ResourceUsage is the summation of the task resources
   247  	ResourceUsage *ResourceUsage
   248  
   249  	// Tasks contains the resource usage of each task
   250  	Tasks map[string]*TaskResourceUsage
   251  
   252  	// The max timestamp of all the Tasks
   253  	Timestamp int64
   254  }
   255  
   256  // joinStringSet takes two slices of strings and joins them
   257  func joinStringSet(s1, s2 []string) []string {
   258  	lookup := make(map[string]struct{}, len(s1))
   259  	j := make([]string, 0, len(s1))
   260  	for _, s := range s1 {
   261  		j = append(j, s)
   262  		lookup[s] = struct{}{}
   263  	}
   264  
   265  	for _, s := range s2 {
   266  		if _, ok := lookup[s]; !ok {
   267  			j = append(j, s)
   268  		}
   269  	}
   270  
   271  	return j
   272  }
   273  
   274  // HealthCheckRequest is the request type for a type that fulfils the Health
   275  // Check interface
   276  type HealthCheckRequest struct{}
   277  
   278  // HealthCheckResponse is the response type for a type that fulfills the Health
   279  // Check interface
   280  type HealthCheckResponse struct {
   281  	// Drivers is a map of driver names to current driver information
   282  	Drivers map[string]*structs.DriverInfo
   283  }
   284  
   285  type HealthCheckIntervalRequest struct{}
   286  type HealthCheckIntervalResponse struct {
   287  	Eligible bool
   288  	Period   time.Duration
   289  }
   290  
   291  // AddDriverInfo adds information about a driver to the fingerprint response.
   292  // If the Drivers field has not yet been initialized, it does so here.
   293  func (h *HealthCheckResponse) AddDriverInfo(name string, driverInfo *structs.DriverInfo) {
   294  	// initialize Drivers if it has not been already
   295  	if h.Drivers == nil {
   296  		h.Drivers = make(map[string]*structs.DriverInfo, 0)
   297  	}
   298  
   299  	h.Drivers[name] = driverInfo
   300  }
   301  
   302  // CheckBufSize is the size of the buffer that is used for job output
   303  const CheckBufSize = 4 * 1024
   304  
   305  // DriverStatsNotImplemented is the error to be returned if a driver doesn't
   306  // implement stats.
   307  var DriverStatsNotImplemented = errors.New("stats not implemented for driver")