Remove event GenericSource, and address other code review comments. Also added deprecation info in comments.

This commit is contained in:
Preetha Appan 2017-11-03 09:34:30 -05:00
parent 1a864dd7e8
commit 0eaef09675
4 changed files with 103 additions and 87 deletions

View file

@ -152,7 +152,7 @@ func TestAllocRunner_DeploymentHealth_Unhealthy_BadStart(t *testing.T) {
assert.NotNil(state) assert.NotNil(state)
assert.NotEmpty(state.Events) assert.NotEmpty(state.Events)
last := state.Events[len(state.Events)-1] last := state.Events[len(state.Events)-1]
assert.Equal(allocHealthEventSource, last.GenericSource) assert.Equal(allocHealthEventSource, last.Type)
assert.Contains(last.Message, "failed task") assert.Contains(last.Message, "failed task")
} }
@ -202,7 +202,7 @@ func TestAllocRunner_DeploymentHealth_Unhealthy_Deadline(t *testing.T) {
assert.NotNil(state) assert.NotNil(state)
assert.NotEmpty(state.Events) assert.NotEmpty(state.Events)
last := state.Events[len(state.Events)-1] last := state.Events[len(state.Events)-1]
assert.Equal(allocHealthEventSource, last.GenericSource) assert.Equal(allocHealthEventSource, last.Type)
assert.Contains(last.Message, "not running by deadline") assert.Contains(last.Message, "not running by deadline")
} }
@ -412,7 +412,7 @@ func TestAllocRunner_DeploymentHealth_Unhealthy_Checks(t *testing.T) {
assert.NotNil(state) assert.NotNil(state)
assert.NotEmpty(state.Events) assert.NotEmpty(state.Events)
last := state.Events[len(state.Events)-1] last := state.Events[len(state.Events)-1]
assert.Equal(allocHealthEventSource, last.GenericSource) assert.Equal(allocHealthEventSource, last.Type)
assert.Contains(last.Message, "Services not healthy by deadline") assert.Contains(last.Message, "Services not healthy by deadline")
} }

View file

@ -543,6 +543,7 @@ func (r *TaskRunner) DestroyState() error {
// setState is used to update the state of the task runner // setState is used to update the state of the task runner
func (r *TaskRunner) setState(state string, event *structs.TaskEvent, lazySync bool) { func (r *TaskRunner) setState(state string, event *structs.TaskEvent, lazySync bool) {
event.PopulateEventDisplayMessage() event.PopulateEventDisplayMessage()
// Persist our state to disk. // Persist our state to disk.
if err := r.SaveState(); err != nil { if err := r.SaveState(); err != nil {
r.logger.Printf("[ERR] client: failed to save state of Task Runner for task %q: %v", r.task.Name, err) r.logger.Printf("[ERR] client: failed to save state of Task Runner for task %q: %v", r.task.Name, err)
@ -1752,8 +1753,8 @@ func (r *TaskRunner) Kill(source, reason string, fail bool) {
} }
func (r *TaskRunner) EmitEvent(source, message string) { func (r *TaskRunner) EmitEvent(source, message string) {
event := structs.NewTaskEvent(structs.TaskGenericMessage). event := structs.NewTaskEvent(source).
SetGenericSource(source).SetMessage(message) SetMessage(message)
r.setState("", event, false) r.setState("", event, false)
r.logger.Printf("[DEBUG] client: event from %q for task %q in alloc %q: %v", r.logger.Printf("[DEBUG] client: event from %q for task %q in alloc %q: %v",
source, r.task.Name, r.alloc.ID, message) source, r.task.Name, r.alloc.ID, message)

View file

@ -325,20 +325,18 @@ func (c *AllocStatusCommand) outputTaskStatus(state *api.TaskState) {
size := len(state.Events) size := len(state.Events)
for i, event := range state.Events { for i, event := range state.Events {
if event.DisplayMessage != "" { msg := event.DisplayMessage
formattedTime := formatUnixNanoTime(event.Time) if msg == "" {
formattedDisplayMsg := fmt.Sprintf("%s|%s|%s", formattedTime, event.Type, event.DisplayMessage) msg = buildDisplayMessage(event)
events[size-i] = formattedDisplayMsg
} else {
events[size-i] = buildDisplayMessage(event)
} }
formattedTime := formatUnixNanoTime(event.Time)
events[size-i] = fmt.Sprintf("%s|%s|%s", formattedTime, event.Type, msg)
// Reverse order so we are sorted by time
} }
c.Ui.Output(formatList(events)) c.Ui.Output(formatList(events))
} }
func buildDisplayMessage(event *api.TaskEvent) string { func buildDisplayMessage(event *api.TaskEvent) string {
formattedTime := formatUnixNanoTime(event.Time)
// Build up the description based on the event type. // Build up the description based on the event type.
var desc string var desc string
switch event.Type { switch event.Type {
@ -447,8 +445,7 @@ func buildDisplayMessage(event *api.TaskEvent) string {
desc = event.Message desc = event.Message
} }
// Reverse order so we are sorted by time return desc
return fmt.Sprintf("%s|%s|%s", formattedTime, event.Type, desc)
} }
// outputTaskResources prints the task resources for the passed task and if // outputTaskResources prints the task resources for the passed task and if

View file

@ -73,9 +73,6 @@ const (
ACLTokenUpsertRequestType ACLTokenUpsertRequestType
ACLTokenDeleteRequestType ACLTokenDeleteRequestType
ACLTokenBootstrapRequestType ACLTokenBootstrapRequestType
// Constant for restart events that are within policy
ReasonWithinPolicy = "Restart within policy"
) )
const ( const (
@ -2487,6 +2484,9 @@ const (
// RestartPolicyMinInterval is the minimum interval that is accepted for a // RestartPolicyMinInterval is the minimum interval that is accepted for a
// restart policy. // restart policy.
RestartPolicyMinInterval = 5 * time.Second RestartPolicyMinInterval = 5 * time.Second
// ReasonWithinPolicy describes restart events that are within policy
ReasonWithinPolicy = "Restart within policy"
) )
// RestartPolicy configures how Tasks are restarted when they crash or fail. // RestartPolicy configures how Tasks are restarted when they crash or fail.
@ -3753,9 +3753,6 @@ const (
// TaskLeaderDead indicates that the leader task within the has finished. // TaskLeaderDead indicates that the leader task within the has finished.
TaskLeaderDead = "Leader Task Dead" TaskLeaderDead = "Leader Task Dead"
// TaskGenericMessage is used by various subsystems to emit a message.
TaskGenericMessage = "Generic"
) )
// TaskEvent is an event that effects the state of a task and contains meta-data // TaskEvent is an event that effects the state of a task and contains meta-data
@ -3764,75 +3761,105 @@ type TaskEvent struct {
Type string Type string
Time int64 // Unix Nanosecond timestamp Time int64 // Unix Nanosecond timestamp
// FailsTask marks whether this event fails the task Message string // A possible message explaining the termination of the task.
FailsTask bool
// Restart fields.
RestartReason string
// Setup Failure fields.
SetupError string
// Driver Failure fields.
DriverError string // A driver error occurred while starting the task.
// Task Terminated Fields.
ExitCode int // The exit code of the task.
Signal int // The signal that terminated the task.
Message string // A possible message explaining the termination of the task.
// Killing fields
KillTimeout time.Duration
// Task Killed Fields.
KillError string // Error killing the task.
// KillReason is the reason the task was killed
KillReason string
// TaskRestarting fields.
StartDelay int64 // The sleep period before restarting the task in unix nanoseconds.
// Artifact Download fields
DownloadError string // Error downloading artifacts
// Validation fields
ValidationError string // Validation error
// The maximum allowed task disk size.
DiskLimit int64
// Name of the sibling task that caused termination of the task that
// the TaskEvent refers to.
FailedSibling string
// VaultError is the error from token renewal
VaultError string
// TaskSignalReason indicates the reason the task is being signalled.
TaskSignalReason string
// TaskSignal is the signal that was sent to the task
TaskSignal string
// DriverMessage indicates a driver action being taken.
DriverMessage string
// GenericSource is the source of a message.
GenericSource string
// DisplayMessage is a human friendly message about the event // DisplayMessage is a human friendly message about the event
DisplayMessage string DisplayMessage string
// Details is a map with annotated info about the event // Details is a map with annotated info about the event
Details map[string]string Details map[string]string
// DEPRECATION NOTICE: The following fields are deprecated and will be removed
// in a future release. Field values are available in the Details map.
// FailsTask marks whether this event fails the task.
// Deprecated, use Details["fails_task"] to access this.
FailsTask bool
// Restart fields.
// Deprecated, use Details["restart_reason"] to access this.
RestartReason string
// Setup Failure fields.
// Deprecated, use Details["setup_error"] to access this.
SetupError string
// Driver Failure fields.
// Deprecated, use Details["driver_error"] to access this.
DriverError string // A driver error occurred while starting the task.
// Task Terminated Fields.
// Deprecated, use Details["exit_code"] to access this.
ExitCode int // The exit code of the task.
// Deprecated, use Details["signal"] to access this.
Signal int // The signal that terminated the task.
// Killing fields
// Deprecated, use Details["kill_timeout"] to access this.
KillTimeout time.Duration
// Task Killed Fields.
// Deprecated, use Details["kill_error"] to access this.
KillError string // Error killing the task.
// KillReason is the reason the task was killed
// Deprecated, use Details["kill_reason"] to access this.
KillReason string
// TaskRestarting fields.
// Deprecated, use Details["start_delay"] to access this.
StartDelay int64 // The sleep period before restarting the task in unix nanoseconds.
// Artifact Download fields
// Deprecated, use Details["download_error"] to access this.
DownloadError string // Error downloading artifacts
// Validation fields
// Deprecated, use Details["validation_error"] to access this.
ValidationError string // Validation error
// The maximum allowed task disk size.
// Deprecated, use Details["disk_limit"] to access this.
DiskLimit int64
// Name of the sibling task that caused termination of the task that
// the TaskEvent refers to.
// Deprecated, use Details["failed_sibling"] to access this.
FailedSibling string
// VaultError is the error from token renewal
// Deprecated, use Details["vault_renewal_error"] to access this.
VaultError string
// TaskSignalReason indicates the reason the task is being signalled.
// Deprecated, use Details["task_signal_reason"] to access this.
TaskSignalReason string
// TaskSignal is the signal that was sent to the task
// Deprecated, use Details["task_signal"] to access this.
TaskSignal string
// DriverMessage indicates a driver action being taken.
// Deprecated, use Details["driver_message"] to access this.
DriverMessage string
// GenericSource is the source of a message.
// Deprecated, is redundant with event type.
GenericSource string
} }
func (event *TaskEvent) PopulateEventDisplayMessage() { func (event *TaskEvent) PopulateEventDisplayMessage() {
// Build up the description based on the event type. // Build up the description based on the event type.
if event == nil { //TODO PA needs investigation alloc_runner's Run method sends a nil event when sigterming nomad. Why? if event == nil { //TODO(preetha) needs investigation alloc_runner's Run method sends a nil event when sigterming nomad. Why?
return return
} }
if event.DisplayMessage != "" {
return
}
var desc string var desc string
switch event.Type { switch event.Type {
case TaskSetup: case TaskSetup:
@ -3935,9 +3962,6 @@ func (event *TaskEvent) PopulateEventDisplayMessage() {
desc = event.DriverMessage desc = event.DriverMessage
case TaskLeaderDead: case TaskLeaderDead:
desc = "Leader Task in Group dead" desc = "Leader Task in Group dead"
case TaskGenericMessage:
event.Type = event.GenericSource
desc = event.Message
default: default:
desc = "" desc = ""
} }
@ -4103,12 +4127,6 @@ func (e *TaskEvent) SetDriverMessage(m string) *TaskEvent {
return e return e
} }
func (e *TaskEvent) SetGenericSource(s string) *TaskEvent {
e.GenericSource = s
e.Details["generic_source"] = s
return e
}
// TaskArtifact is an artifact to download before running the task. // TaskArtifact is an artifact to download before running the task.
type TaskArtifact struct { type TaskArtifact struct {
// GetterSource is the source to download an artifact using go-getter // GetterSource is the source to download an artifact using go-getter