From 475ea9657380be8f54779b3458fe7e2aae9509fe Mon Sep 17 00:00:00 2001 From: Sidhant Kohli Date: Wed, 6 Dec 2023 02:10:56 -0800 Subject: [PATCH 01/12] health impl Signed-off-by: Sidhant Kohli --- go.mod | 1 + go.sum | 2 + pkg/daemon/server/daemon_server.go | 17 + pkg/daemon/server/service/healthStatus.go | 413 ++++++++++++++++++ .../server/service/pipeline_metrics_query.go | 114 ++--- server/apis/v1/handler.go | 32 +- server/apis/v1/health.go | 105 +++++ server/apis/v1/response_pipeline.go | 11 +- 8 files changed, 640 insertions(+), 55 deletions(-) create mode 100644 pkg/daemon/server/service/healthStatus.go create mode 100644 server/apis/v1/health.go diff --git a/go.mod b/go.mod index 3232884e42..949e40ab83 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.20 require ( github.com/IBM/sarama v1.40.1 github.com/Masterminds/sprig/v3 v3.2.2 + github.com/VividCortex/ewma v1.2.0 github.com/ahmetb/gen-crd-api-reference-docs v0.3.0 github.com/antonmedv/expr v1.9.0 github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de diff --git a/go.sum b/go.sum index 0b50c148ef..7853d1c34d 100644 --- a/go.sum +++ b/go.sum @@ -70,6 +70,8 @@ github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdko github.com/Shopify/toxiproxy/v2 v2.5.0 h1:i4LPT+qrSlKNtQf5QliVjdP08GyAH8+BUIc9gT0eahc= github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkHBuFDA6DUhhse0IGJ7T5bemHyNILUjvOq4= github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w= +github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow= +github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4= github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM= github.com/ahmetb/gen-crd-api-reference-docs v0.3.0 h1:+XfOU14S4bGuwyvCijJwhhBIjYN+YXS18jrCY2EzJaY= github.com/ahmetb/gen-crd-api-reference-docs v0.3.0/go.mod h1:TdjdkYhlOifCQWPs1UdTma97kQQMozf5h26hTuG70u8= diff --git a/pkg/daemon/server/daemon_server.go b/pkg/daemon/server/daemon_server.go index 6ba93b6d83..d84154291c 100644 --- a/pkg/daemon/server/daemon_server.go +++ b/pkg/daemon/server/daemon_server.go @@ -138,11 +138,28 @@ func (ds *daemonServer) Run(ctx context.Context) error { go func() { _ = httpServer.Serve(httpL) }() go func() { _ = tcpm.Serve() }() + // Start the Data flow status updater + // This function should be continuously running, and invoked every HEALTH_CHECK_INTERVAL seconds + + pipelineMetadataQuery, err := service.NewPipelineMetadataQuery(isbSvcClient, ds.pipeline, wmFetchers, rater) + if err != nil { + log.Errorw("Failed to create pipeline metadata query", zap.Error(err)) + } + healthChecker := service.NewHealthChecker(ds.pipeline, pipelineMetadataQuery) + + log.Infof("DEBUGSID starting StartHealthCheck %s", healthChecker.GetCurrentHealth()) + // Start the Data flow status updater + go func() { + log.Infof("DEBUGSID starting StartHealthCheck") + healthChecker.StartHealthCheck() + }() + log.Infof("Daemon server started successfully on %s", address) // Start the rater if err := rater.Start(ctx); err != nil { return fmt.Errorf("failed to start the rater: %w", err) } + <-ctx.Done() return nil } diff --git a/pkg/daemon/server/service/healthStatus.go b/pkg/daemon/server/service/healthStatus.go new file mode 100644 index 0000000000..4247dfcd7c --- /dev/null +++ b/pkg/daemon/server/service/healthStatus.go @@ -0,0 +1,413 @@ +package service + +import ( + "sync" + "time" + + "github.com/VividCortex/ewma" + "golang.org/x/net/context" + + "github.com/numaproj/numaflow/pkg/apis/numaflow/v1alpha1" + "github.com/numaproj/numaflow/pkg/apis/proto/daemon" + "github.com/numaproj/numaflow/pkg/shared/logging" +) + +const ( + // TotalHealthTime is the total time window to compute the health of a vertex + TotalHealthTime = 5 * time.Minute + + // HealthTimeStep is the time step to compute the health of a vertex + HealthTimeStep = 10 * time.Second + + // HealthWindowSize is the number of timeline entries to keep for a given vertex + // This is used to compute the health of a vertex + // Ensure that TotalHealthTime / HealthTimeStep is an integer + HealthWindowSize = TotalHealthTime / HealthTimeStep + + // CriticalWindowTime is the number of entries to look back to assign a critical state to a vertex + CriticalWindowTime = 1 * time.Minute + + // CriticalWindowSize is the number of entries to look back to assign a critical state to a vertex + // This is used to avoid false positives + CriticalWindowSize = int(CriticalWindowTime / HealthTimeStep) + + // CriticalLookBackCount is the number of times the state must be critical in the last CriticalWindowSize entries + // This is used to avoid false positives + CriticalLookBackCount = 3 +) + +// HealthThresholds are the thresholds used to compute the health of a vertex +const ( + // CriticalThreshold is the threshold above which the health of a vertex is critical + CriticalThreshold = 95 + // WarningThreshold is the threshold above which the health of a vertex is warning + WarningThreshold = 80 +) + +// Dataflow states +const ( + // CriticalState is the state of a vertex when its health is critical + CriticalState = "critical" + // WarningState is the state of a vertex when its health is warning + WarningState = "warning" + // HealthyState is the state of a vertex when its health is healthy + HealthyState = "healthy" +) + +const ( + // EnableCriticalLookBack is used to enable the look back for critical state + EnableCriticalLookBack = true + DisableCriticalLookBack = false +) + +// TimelineEntry is used to store the buffer usage timeline for a given vertex +type TimelineEntry struct { + // The time at which the entry is recorded + Time int64 `json:"time"` + // The buffer usage of the pipeline at the time + BufferUsage float64 `json:"bufferUsage"` + // The rolling average buffer usage of the pipeline over the last HEALTH_WINDOW_SIZE seconds + AverageBufferUsage float64 `json:"averageBufferUsage"` +} + +var ( + currentPipelineStatus = PipelineStatusOK + pipeStatusLock = &sync.RWMutex{} + + log = logging.FromContext(context.Background()) +) + +// GetCurrentPipelineHealth returns the current health status of the pipeline. +// It is thread safe to ensure concurrent access. +func GetCurrentPipelineHealth() string { + // Lock the statusLock to ensure thread safety. + pipeStatusLock.RLock() + defer pipeStatusLock.RUnlock() + // Return the health status. + return currentPipelineStatus +} + +// SetCurrentPipelineHealth sets the current health status of the pipeline. +// It is thread safe to ensure concurrent access. +func SetCurrentPipelineHealth(status string) { + // Lock the statusLock to ensure thread safety. + pipeStatusLock.Lock() + defer pipeStatusLock.Unlock() + // Set the health status. + currentPipelineStatus = status +} + +// HealthChecker is the struct type for health checker. +type HealthChecker struct { + // Add a field for the health status. + currentDataStatus string + pipelineMetadataQuery *pipelineMetadataQuery + pipeline *v1alpha1.Pipeline + timelineData map[string][]*TimelineEntry + statusLock *sync.RWMutex +} + +// NewHealthChecker creates a new object HealthChecker struct type. +func NewHealthChecker(pipeline *v1alpha1.Pipeline, pipelineMetadataQuery *pipelineMetadataQuery) *HealthChecker { + // Return a new HealthChecker struct instance. + return &HealthChecker{ + currentDataStatus: PipelineStatusOK, + pipelineMetadataQuery: pipelineMetadataQuery, + pipeline: pipeline, + timelineData: make(map[string][]*TimelineEntry), + statusLock: &sync.RWMutex{}, + } +} + +// GetCurrentHealth returns the current health status of the pipeline. +// It is thread safe to ensure concurrent access. +func (hc *HealthChecker) GetCurrentHealth() string { + // Lock the statusLock to ensure thread safety. + hc.statusLock.RLock() + defer hc.statusLock.RUnlock() + // Return the health status. + return hc.currentDataStatus +} + +// SetCurrentHealth sets the current health status of the pipeline. +// It is thread safe to ensure concurrent access. +func (hc *HealthChecker) SetCurrentHealth(status string) { + // Lock the statusLock to ensure thread safety. + hc.statusLock.Lock() + defer hc.statusLock.Unlock() + // Set the health status. + hc.currentDataStatus = status + SetCurrentPipelineHealth(status) +} + +// StartHealthCheck starts the health check. +func (hc *HealthChecker) StartHealthCheck() { + + log.Info("DEBUGSID StartHealthCheck") + // Goroutine to listen for ticks + // At every tick, check and update the health status of the pipeline. + go func() { + // Create a ticker with the interval of HealthCheckInterval. + ticker := time.NewTicker(HealthTimeStep) + defer ticker.Stop() + for { + select { + case <-ticker.C: + log.Info("DEBUGSID got tick") + // Get the current health status of the pipeline. + criticality, err := hc.getPipelineDataCriticality() + if err != nil { + return + } + log.Info("DEBUGSID criticality", criticality) + // convert the vertex state to pipeline state + pipelineState := convertVertexStateToPipelineState(criticality) + log.Info("DEBUGSID pipelineState", pipelineState) + // update the current health status of the pipeline + hc.SetCurrentHealth(pipelineState) + } + } + }() +} + +// getPipelineDataCriticality is used to provide the data criticality of the pipeline +// They can be of the following types: +// 1. Ok: The pipeline is working as expected +// 2. Warning: The pipeline is working but there is a lag in the data movement +// 3. Critical: The pipeline is not working as expected +// We need to check the following things to determine the data criticality of the pipeline: +// 1. The buffer usage of each buffer in the pipeline +// +// Based on this information, we first get the current buffer usage of each buffer in the pipeline at that instant +// and populate the timeline data for each buffer. +// Then for a given buffer, we calculate the average buffer usage over +// the last HEALTH_WINDOW_SIZE seconds. +// If this average is greater than the critical threshold, we return the critical status +// If this average is greater than the warning threshold, we return the warning status +// If this average is less than the warning threshold, we return the ok status +func (hc *HealthChecker) getPipelineDataCriticality() ([]string, error) { + ctx := context.Background() + pipelineName := hc.pipeline.GetName() + + // Create a new buffer request object + req := &daemon.ListBuffersRequest{Pipeline: &pipelineName} + + // Fetch the buffer information for the pipeline + buffers, err := hc.pipelineMetadataQuery.ListBuffers(ctx, req) + if err != nil { + return nil, err + } + // update the usage timeline for all the ISBs used in the pipeline + hc.updateUsageTimeline(buffers.Buffers) + + var vertexState []string + + // iterate over the timeline data for each buffer and calculate the exponential weighted mean average + // for the last HEALTH_WINDOW_SIZE buffer usage entries + for bufferName, timeline := range hc.timelineData { + // Extract the buffer usage of the timeline + var bufferUsage []float64 + for _, entry := range timeline { + bufferUsage = append(bufferUsage, entry.BufferUsage) + } + // calculate the average buffer usage over the last HEALTH_WINDOW_SIZE seconds + log.Info("DEBUGSID bufferusage", bufferName, bufferUsage) + ewmaBufferUsage := calculateEWMAUsage(bufferUsage) + log.Info("DEBUGSID ewmavalue", bufferName, ewmaBufferUsage) + // assign the state to the vertex based on the average buffer usage + // Look back is disabled for the critical state + currentState := assignStateToTimeline(ewmaBufferUsage, DisableCriticalLookBack) + vertexState = append(vertexState, currentState) + } + return vertexState, nil +} + +// updateUsageTimeline is used to update the usage timeline for a given buffer list +// This iterates over all the buffers in the buffer list and updates the usage timeline for each buffer +// The timeline data is represented as a map of buffer name to a list of TimelineEntry +// Example: +// +// { +// "bufferName": [ +// { +// "time": 1234567890, +// "bufferUsage": 0.5, +// "averageBufferUsage": 0.5 +// }, +func (hc *HealthChecker) updateUsageTimeline(bufferList []*daemon.BufferInfo) { + // iterate over the buffer list and update the usage timeline for each buffer + for _, buffer := range bufferList { + // get the buffer name and the current timestamp + bufferName := buffer.GetBufferName() + timestamp := time.Now().Unix() + + // if the buffer name is not present in the timeline data, add it + if _, ok := hc.timelineData[bufferName]; !ok { + hc.timelineData[bufferName] = make([]*TimelineEntry, 0) + } + // extract the current buffer usage and update the average buffer usage + bufferUsage := buffer.GetBufferUsage() * 100 + newAverage := updateAverageBufferUsage(hc.timelineData[bufferName], bufferUsage) + + // add the new entry to the timeline + hc.timelineData[bufferName] = append(hc.timelineData[bufferName], &TimelineEntry{ + Time: timestamp, + BufferUsage: bufferUsage, + AverageBufferUsage: newAverage, + }) + log.Info("DEBUGSID", bufferName, bufferUsage, newAverage) + + // remove first entry if the size of the timeline is greater than HEALTH_WINDOW_SIZE + if len(hc.timelineData[bufferName]) > int(HealthWindowSize) { + hc.timelineData[bufferName] = hc.timelineData[bufferName][1:] + } + + } +} + +// updateAverageBufferUsage computes the average buffer usage of a vertex over the last HEALTH_WINDOW_SIZE seconds +// This is a rolling average, so the oldest entry is removed from the timeline and the new entry is added +// The average buffer usage is computed as the average of the buffer usage of the last HEALTH_WINDOW_SIZE entries +// If the timeline is empty, the average buffer usage is set to the buffer usage of the new entry +// If the timeline is not empty, the average buffer usage is computed as follows: +// +// averageBufferUsage = lastAverage*HEALTH_WINDOW_SIZE + (newUsageEntry - oldestEntry) / HEALTH_WINDOW_SIZE +// where oldestEntry is the oldest entry in the timeline +// and lastAverage is the average buffer usage of the last entry in the timeline +// +// If the timeline size is less than HEALTH_WINDOW_SIZE, the average buffer usage is computed as follows: +// +// averageBufferUsage = lastAverage * timelineSize + (newUsageEntry) / timelineSize + 1 +// where lastAverage is the average buffer usage of the last entry in the timeline +// and timelineSize is the size of the timeline +func updateAverageBufferUsage(timeline []*TimelineEntry, newEntry float64) float64 { + // If the timeline is empty, return the buffer usage of the new entry + if len(timeline) == 0 { + return newEntry + } + // Compute the current average buffer usage of the last entry + lastAverage := timeline[len(timeline)-1].AverageBufferUsage + var newAverage float64 + // If the timeline is full + if len(timeline) == int(HealthWindowSize) { + newAverage = (lastAverage*float64(HealthWindowSize) + newEntry - timeline[0].BufferUsage) / float64(HealthWindowSize) + } else if len(timeline) < int(HealthWindowSize) { + // If the timeline is not full + newAverage = (lastAverage*float64(len(timeline)) + newEntry) / float64(len(timeline)+1) + } + return newAverage +} + +// calculateEWMAUsage computes the EWMA buffer usage of a vertex over the last HEALTH_WINDOW_SIZE seconds +// This is an exponentially weighted moving average, with the following parameters: +// alpha = 2 / (HEALTH_WINDOW_SIZE + 1) +// EWMA = alpha * newUsageEntry + (1 - alpha) * lastEWMA +// where lastEWMA is the EWMA buffer usage of the last entry in the timeline +func calculateEWMAUsage(bufferUsage []float64) []float64 { + // Compute the current EWMA buffer usage of the timeline + a := ewma.NewMovingAverage() + var emwaValues []float64 + for _, f := range bufferUsage { + a.Add(f) + emwaValues = append(emwaValues, a.Value()) + } + return emwaValues +} + +// assignStateToBufferUsage assigns a state to the buffer usage of a vertex +// The state is assigned as follows: +// - if the buffer usage is above CRITICAL_THRESHOLD, the state is set to CRITICAL +// - if the buffer usage is above WARNING_THRESHOLD, the state is set to WARNING +// - otherwise, the state is set to HEALTHY +func assignStateToBufferUsage(ewmaValue float64) string { + // Assign the state to the buffer usage + var state string + if ewmaValue > CriticalThreshold { + state = CriticalState + } else if ewmaValue > WarningThreshold { + state = WarningState + } else { + state = HealthyState + } + return state +} + +// assignStateToTimeline assigns a state to the timeline of a vertex +// We also have a look back for critical state to avoid false positives +// In this case, we check if the state is CRITICAL at least LOOK_BACK_COUNT times in the last CRITICAL_WINDOW_SIZE entries +// If the state is CRITICAL at least LOOK_BACK_COUNT times in the last CRITICAL_WINDOW_SIZE entries +// Set the state to CRITICAL +func assignStateToTimeline(ewmaValues []float64, lookBack bool) string { + + // Extract the last entry of the timeline + ewmaUsage := ewmaValues[len(ewmaValues)-1] + + // Assign the state to the buffer usage value + state := assignStateToBufferUsage(ewmaUsage) + + // If the state is CRITICAL, and we have a look back, we need to check we have + // LOOK_BACK_COUNT entries as CRITICAL + if state == CriticalState && lookBack { + // Extract the states of the timeline + var states []string + for _, entry := range ewmaValues { + states = append(states, assignStateToBufferUsage(entry)) + } + // Count the number of times the state is CRITICAL in the last CRITICAL_WINDOW_SIZE entries + var criticalCount int + for i := len(states) - 1; i >= 0; i-- { + if states[i] == CriticalState { + criticalCount++ + } + if (len(states) - i) == CriticalWindowSize { + break + } + } + // If the state is CRITICAL at least LOOK_BACK_COUNT times in the last CRITICAL_WINDOW_SIZE entries + // Set the state to CRITICAL, otherwise set the state to WARNING + if criticalCount >= CriticalLookBackCount { + state = CriticalState + } else { + state = WarningState + } + } + return state +} + +// convertVertexStateToPipelineState is used to convert the vertex state to pipeline state +// if any of the vertices are critical, the pipeline is critical +// if any of the vertices are warning, the pipeline is warning +// if any the vertices are unknown, the pipeline is unknown +// else the pipeline is ok +// Here we follow a precedence order of unknown > critical > warning > ok +// Hence, whichever is the highest precedence state found in the vertex state, we return that +func convertVertexStateToPipelineState(vertexState []string) string { + // create a map to store the precedence order of the states + // assign a number to each state based on the precedence order + + // unknown > critical > warning > ok + stateMap := map[string]int{ + PipelineStatusUnknown: 3, + PipelineStatusCritical: 2, + PipelineStatusWarning: 1, + PipelineStatusOK: 0, + } + + // initialize the max state to 0 (ie Healthy state) + maxState := 0 + + // iterate over the vertex state and assign a number to each state and update the current max state + for _, state := range vertexState { + if stateMap[state] > maxState { + maxState = stateMap[state] + } + } + + // get the state corresponding to the max state + for state, value := range stateMap { + if value == maxState { + return state + } + } + return PipelineStatusUnknown +} diff --git a/pkg/daemon/server/service/pipeline_metrics_query.go b/pkg/daemon/server/service/pipeline_metrics_query.go index ee88b76db4..8592300cb4 100644 --- a/pkg/daemon/server/service/pipeline_metrics_query.go +++ b/pkg/daemon/server/service/pipeline_metrics_query.go @@ -54,9 +54,13 @@ type pipelineMetadataQuery struct { } const ( - PipelineStatusOK = "OK" - PipelineStatusError = "Error" - PipelineStatusUnknown = "Unknown" + PipelineStatusOK = "OK" + PipelineStatusError = "Error" + PipelineStatusUnknown = "unknown" + PipelineStatusCritical = "critical" + PipelineStatusWarning = "warning" + PipelineStatusInactive = "inactive" + PipelineStatusDeleting = "deleting" ) // NewPipelineMetadataQuery returns a new instance of pipelineMetadataQuery @@ -247,57 +251,67 @@ func (ps *pipelineMetadataQuery) getPending(ctx context.Context, req *daemon.Get return totalPendingMap } -func (ps *pipelineMetadataQuery) GetPipelineStatus(ctx context.Context, req *daemon.GetPipelineStatusRequest) (*daemon.GetPipelineStatusResponse, error) { +//func (ps *pipelineMetadataQuery) GetPipelineStatus(ctx context.Context, req *daemon.GetPipelineStatusRequest) (*daemon.GetPipelineStatusResponse, error) { +// +// resp := new(daemon.GetPipelineStatusResponse) +// +// // get all vertices of pipeline +// vertices := ps.pipeline.Spec.Vertices +// +// // loop over vertices and get metrics to check pending messages vs processing rate +// for _, vertex := range vertices { +// vertexReq := new(daemon.GetVertexMetricsRequest) +// vertexReq.Vertex = &vertex.Name +// vertexResp, err := ps.GetVertexMetrics(ctx, vertexReq) +// // if err is not nil, more than likely autoscaling is down to 0 and metrics are not available +// if err != nil { +// resp.Status = &daemon.PipelineStatus{ +// Status: pointer.String(PipelineStatusUnknown), +// Message: pointer.String("Pipeline status is unknown."), +// } +// return resp, nil +// } +// +// totalProcessingRate := float64(0) +// totalPending := int64(0) +// // may need to revisit later, another concern could be that the processing rate is too slow instead of just 0 +// for _, vertexMetrics := range vertexResp.VertexMetrics { +// if vertexMetrics.GetProcessingRates() != nil { +// if p, ok := vertexMetrics.GetProcessingRates()["default"]; ok { +// totalProcessingRate += p +// } +// } +// if vertexMetrics.GetPendings() != nil { +// if p, ok := vertexMetrics.GetPendings()["default"]; ok { +// totalPending += p +// } +// } +// } +// +// if totalPending > 0 && totalProcessingRate == 0 { +// resp.Status = &daemon.PipelineStatus{ +// Status: pointer.String(PipelineStatusError), +// Message: pointer.String(fmt.Sprintf("Pipeline has an error. Vertex %s is not processing pending messages.", vertex.Name)), +// } +// return resp, nil +// } +// } +// +// resp.Status = &daemon.PipelineStatus{ +// Status: pointer.String(PipelineStatusOK), +// Message: pointer.String("Pipeline has no issue."), +// } +// +// return resp, nil +//} +func (ps *pipelineMetadataQuery) GetPipelineStatus(ctx context.Context, req *daemon.GetPipelineStatusRequest) (*daemon.GetPipelineStatusResponse, error) { + status := GetCurrentPipelineHealth() resp := new(daemon.GetPipelineStatusResponse) - - // get all vertices of pipeline - vertices := ps.pipeline.Spec.Vertices - - // loop over vertices and get metrics to check pending messages vs processing rate - for _, vertex := range vertices { - vertexReq := new(daemon.GetVertexMetricsRequest) - vertexReq.Vertex = &vertex.Name - vertexResp, err := ps.GetVertexMetrics(ctx, vertexReq) - // if err is not nil, more than likely autoscaling is down to 0 and metrics are not available - if err != nil { - resp.Status = &daemon.PipelineStatus{ - Status: pointer.String(PipelineStatusUnknown), - Message: pointer.String("Pipeline status is unknown."), - } - return resp, nil - } - - totalProcessingRate := float64(0) - totalPending := int64(0) - // may need to revisit later, another concern could be that the processing rate is too slow instead of just 0 - for _, vertexMetrics := range vertexResp.VertexMetrics { - if vertexMetrics.GetProcessingRates() != nil { - if p, ok := vertexMetrics.GetProcessingRates()["default"]; ok { - totalProcessingRate += p - } - } - if vertexMetrics.GetPendings() != nil { - if p, ok := vertexMetrics.GetPendings()["default"]; ok { - totalPending += p - } - } - } - - if totalPending > 0 && totalProcessingRate == 0 { - resp.Status = &daemon.PipelineStatus{ - Status: pointer.String(PipelineStatusError), - Message: pointer.String(fmt.Sprintf("Pipeline has an error. Vertex %s is not processing pending messages.", vertex.Name)), - } - return resp, nil - } - } - resp.Status = &daemon.PipelineStatus{ - Status: pointer.String(PipelineStatusOK), - Message: pointer.String("Pipeline has no issue."), + Status: pointer.String(status), + Message: pointer.String(fmt.Sprintf("Pipeline status is %s.", status)), } - return resp, nil } diff --git a/server/apis/v1/handler.go b/server/apis/v1/handler.go index 119595d9e7..a427b4c669 100644 --- a/server/apis/v1/handler.go +++ b/server/apis/v1/handler.go @@ -885,8 +885,38 @@ func (h *handler) GetNamespaceEvents(c *gin.Context) { // It represents the pending messages, lags, etc. // Criticality can be "ok (0) | warning (1) | critical (2)". // Health and Criticality are different because ...? +// GetPipelineStatus is used to return the status of a given pipeline +// It is divided into two parts: +// 1. Pipeline Vertex Health: It is based on the health of each vertex in the pipeline +// 2. Data Criticality: It is based on the data movement of the pipeline func (h *handler) GetPipelineStatus(c *gin.Context) { - c.JSON(http.StatusNotImplemented, "working on it") + ns, pipeline := c.Param("namespace"), c.Param("pipeline") + + // Get the vertex level health of the pipeline + vertexHealth, err := getPipelineVertexHealth(h, ns, pipeline) + if err != nil { + h.respondWithError(c, fmt.Sprintf("Failed to get the status for pipeline %q: %s", pipeline, err.Error())) + return + } + + // Get a new daemon client for the given pipeline + client, err := h.getDaemonClient(ns, pipeline) + if err != nil || client == nil { + h.respondWithError(c, fmt.Sprintf("failed to get daemon service client for pipeline %q, %s", pipeline, err.Error())) + return + } + // Get the data criticality for the given pipeline + status, err := client.GetPipelineStatus(context.Background(), pipeline) + if err != nil { + h.respondWithError(c, fmt.Sprintf("Failed to get the status for pipeline %q: %s", pipeline, err.Error())) + return + } + + // Create a response string based on the vertex health and data criticality + // We combine both the states to get the final status of the pipeline + var response string + response = fmt.Sprintf("%s-%s", vertexHealth, status) + c.JSON(http.StatusOK, NewNumaflowAPIResponse(nil, response)) } // getAllNamespaces is a utility used to fetch all the namespaces in the cluster diff --git a/server/apis/v1/health.go b/server/apis/v1/health.go new file mode 100644 index 0000000000..b0f5b40a24 --- /dev/null +++ b/server/apis/v1/health.go @@ -0,0 +1,105 @@ +package v1 + +import ( + "fmt" + + "golang.org/x/net/context" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + + dfv1 "github.com/numaproj/numaflow/pkg/apis/numaflow/v1alpha1" + "github.com/numaproj/numaflow/pkg/shared/logging" +) + +// getPipelineVertexHealth is used to provide the overall vertex health and status of the pipeline +// They can be of the following types: +// 1. Healthy: All the vertices are healthy +// 2. Unhealthy: One or more vertices are unhealthy +// 3. Paused: The pipeline is paused +// 4. Unknown: The pipeline is in an unknown state +// 5. Killed: The pipeline is killed +// To check for vertex level status we need to check for two things, +// 1) the number of replicas running in the vertex +// are equal to the number of desired replicas and the pods are in running state +// 2) If all the containers in the pod are in running state +// if any of the above conditions are not met, the vertex is unhealthy +func getPipelineVertexHealth(h *handler, ns, pipeline string) (string, error) { + // check if the pipeline is paused, if so, return paused status + pl, err := h.numaflowClient.Pipelines(ns).Get(context.Background(), pipeline, metav1.GetOptions{}) + // if error return unknown status + if err != nil { + return PipelineStatusUnknown, err + } + + // if the pipeline is paused, return inactive status + // this cannot be checked at individual vertex level, hence needs to be checked here + if pl.Spec.Lifecycle.GetDesiredPhase() == dfv1.PipelinePhasePaused { + return PipelineStatusInactive, nil + } + + // if the pipeline is killed, return killed status + // this cannot be checked at individual vertex level, hence needs to be checked here + // TODO(Health): Check if this is correct? + if pl.Spec.Lifecycle.GetDesiredPhase() == dfv1.PipelinePhaseDeleting { + return PipelineStatusDeleting, nil + } + + // get the list of vertices in the pipeline + vertices := pl.Spec.Vertices + + // Iterate over all the vertices + for _, vertex := range vertices { + vertexName := fmt.Sprintf("%s-%s", pipeline, vertex.Name) + // fetch the current spec of the vertex + v, err := h.numaflowClient.Vertices(ns).Get(context.Background(), vertexName, metav1.GetOptions{}) + if err != nil { + return PipelineStatusUnknown, err + } + ok, err := isVertexHealthy(h, ns, pipeline, v, vertex.Name) + if err != nil { + return PipelineStatusUnknown, err + } + if !ok { + return PipelineStatusUnhealthy, nil + } + } + return PipelineStatusHealthy, nil +} + +// isVertexHealthy is used to check if the number of replicas running in the vertex +// are equal to the number of desired replicas and the pods are in running state. +// We first check if the vertex is in running state, if not, return the error message from the status +func isVertexHealthy(h *handler, ns string, pipeline string, vertex *dfv1.Vertex, vertexName string) (bool, error) { + log := logging.FromContext(context.Background()) + // check if the vertex is in running state + log.Info("vertex status: ", vertex.Name, vertex.Status.Phase) + if vertex.Status.Phase != dfv1.VertexPhaseRunning { + // check if the number of replicas running in the vertex + // are equal to the number of desired replicas + if int(vertex.Status.Replicas) != vertex.GetReplicas() { + return false, fmt.Errorf("vertex %q has %d replicas running, "+ + "expected %d", vertex.Name, vertex.Status.Replicas, vertex.GetReplicas()) + } + // Else return the error message from the status + return false, fmt.Errorf("error in vertex %s", vertex.Status.Message) + } + + // Get all the pods for the given vertex + pods, err := h.kubeClient.CoreV1().Pods(ns).List(context.Background(), metav1.ListOptions{ + LabelSelector: fmt.Sprintf("%s=%s,%s=%s", dfv1.KeyPipelineName, pipeline, dfv1.KeyVertexName, vertexName), + }) + if err != nil { + return false, err + } + log.Info("number of pods: ", len(pods.Items)) + // Iterate over all the pods, and verify if all the containers in the pod are in running state + for _, pod := range pods.Items { + // Iterate over all the containers in the pod + for _, containerStatus := range pod.Status.ContainerStatuses { + // if the container is not in running state, return false + if containerStatus.State.Running == nil { + return false, fmt.Errorf("container %q in pod %q is not ready", containerStatus.Name, pod.Name) + } + } + } + return true, nil +} diff --git a/server/apis/v1/response_pipeline.go b/server/apis/v1/response_pipeline.go index 1f0aaba8ec..9e1b050031 100644 --- a/server/apis/v1/response_pipeline.go +++ b/server/apis/v1/response_pipeline.go @@ -19,10 +19,13 @@ package v1 import "github.com/numaproj/numaflow/pkg/apis/numaflow/v1alpha1" const ( - PipelineStatusHealthy = "healthy" - PipelineStatusCritical = "critical" - PipelineStatusWarning = "warning" - PipelineStatusInactive = "inactive" + PipelineStatusHealthy = "healthy" + PipelineStatusCritical = "critical" + PipelineStatusWarning = "warning" + PipelineStatusInactive = "inactive" + PipelineStatusUnknown = "unknown" + PipelineStatusDeleting = "deleting" + PipelineStatusUnhealthy = "unhealthy" ) // Pipelines is a list of pipelines From 49cc2126e86e7b5cecdafb8183f068ad842bbe21 Mon Sep 17 00:00:00 2001 From: Sidhant Kohli Date: Thu, 7 Dec 2023 13:12:48 -0800 Subject: [PATCH 02/12] add cache to status check Signed-off-by: Sidhant Kohli --- pkg/daemon/server/service/healthStatus.go | 2 +- server/apis/v1/handler.go | 4 +- server/apis/v1/health.go | 74 +++++++++++++++++++---- 3 files changed, 65 insertions(+), 15 deletions(-) diff --git a/pkg/daemon/server/service/healthStatus.go b/pkg/daemon/server/service/healthStatus.go index 4247dfcd7c..23e59bab38 100644 --- a/pkg/daemon/server/service/healthStatus.go +++ b/pkg/daemon/server/service/healthStatus.go @@ -216,7 +216,7 @@ func (hc *HealthChecker) getPipelineDataCriticality() ([]string, error) { log.Info("DEBUGSID ewmavalue", bufferName, ewmaBufferUsage) // assign the state to the vertex based on the average buffer usage // Look back is disabled for the critical state - currentState := assignStateToTimeline(ewmaBufferUsage, DisableCriticalLookBack) + currentState := assignStateToTimeline(ewmaBufferUsage, EnableCriticalLookBack) vertexState = append(vertexState, currentState) } return vertexState, nil diff --git a/server/apis/v1/handler.go b/server/apis/v1/handler.go index a427b4c669..757c69a66a 100644 --- a/server/apis/v1/handler.go +++ b/server/apis/v1/handler.go @@ -65,6 +65,7 @@ type handler struct { daemonClientsCache *lru.Cache[string, *daemonclient.DaemonClient] dexObj *DexObject localUsersAuthObject *LocalUsersAuthObject + healthChecker *HealthChecker } // NewHandler is used to provide a new instance of the handler type @@ -93,6 +94,7 @@ func NewHandler(dexObj *DexObject, localUsersAuthObject *LocalUsersAuthObject) ( daemonClientsCache: daemonClientsCache, dexObj: dexObj, localUsersAuthObject: localUsersAuthObject, + healthChecker: NewHealthChecker(), }, nil } @@ -893,7 +895,7 @@ func (h *handler) GetPipelineStatus(c *gin.Context) { ns, pipeline := c.Param("namespace"), c.Param("pipeline") // Get the vertex level health of the pipeline - vertexHealth, err := getPipelineVertexHealth(h, ns, pipeline) + vertexHealth, err := h.healthChecker.getPipelineVertexHealth(h, ns, pipeline) if err != nil { h.respondWithError(c, fmt.Sprintf("Failed to get the status for pipeline %q: %s", pipeline, err.Error())) return diff --git a/server/apis/v1/health.go b/server/apis/v1/health.go index b0f5b40a24..9f0636f60a 100644 --- a/server/apis/v1/health.go +++ b/server/apis/v1/health.go @@ -2,7 +2,9 @@ package v1 import ( "fmt" + "time" + evictCache "github.com/hashicorp/golang-lru/v2/expirable" "golang.org/x/net/context" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -10,7 +12,50 @@ import ( "github.com/numaproj/numaflow/pkg/shared/logging" ) +const ( + // vertexCacheRefreshDuration is the duration after which the vertex status cache is refreshed + vertexCacheRefreshDuration = 30 * time.Second +) + +type HealthChecker struct { + vertexStatusCache *evictCache.LRU[string, string] +} + +func NewHealthChecker() *HealthChecker { + c := evictCache.NewLRU[string, string](500, nil, vertexCacheRefreshDuration) + return &HealthChecker{ + vertexStatusCache: c, + } +} + // getPipelineVertexHealth is used to provide the overall vertex health and status of the pipeline +// This first check if the pipeline status is cached, if not, it checks for the current pipeline status +// and caches it. +func (hc *HealthChecker) getPipelineVertexHealth(h *handler, ns string, pipeline string) (string, error) { + ctx := context.Background() + log := logging.FromContext(ctx) + + // create a cache key for the pipeline + // It is a combination of namespace and pipeline name + // In the form of - + cacheKey := fmt.Sprintf("%s-%s", ns, pipeline) + + // check if the pipeline status is cached + if status, ok := hc.vertexStatusCache.Get(cacheKey); ok { + log.Info("pipeline status from cache: ", status) + return status, nil + } + // if not, get the pipeline status + status, err := checkVertexLevelHealth(ctx, h, ns, pipeline) + if err != nil { + return PipelineStatusUnknown, err + } + // cache the pipeline status + hc.vertexStatusCache.Add(cacheKey, status) + return status, nil +} + +// checkVertexLevelHealth is used to provide the overall vertex health and status of the pipeline // They can be of the following types: // 1. Healthy: All the vertices are healthy // 2. Unhealthy: One or more vertices are unhealthy @@ -22,7 +67,8 @@ import ( // are equal to the number of desired replicas and the pods are in running state // 2) If all the containers in the pod are in running state // if any of the above conditions are not met, the vertex is unhealthy -func getPipelineVertexHealth(h *handler, ns, pipeline string) (string, error) { +func checkVertexLevelHealth(ctx context.Context, h *handler, ns string, pipeline string) (string, error) { + log := logging.FromContext(ctx) // check if the pipeline is paused, if so, return paused status pl, err := h.numaflowClient.Pipelines(ns).Get(context.Background(), pipeline, metav1.GetOptions{}) // if error return unknown status @@ -54,11 +100,12 @@ func getPipelineVertexHealth(h *handler, ns, pipeline string) (string, error) { if err != nil { return PipelineStatusUnknown, err } - ok, err := isVertexHealthy(h, ns, pipeline, v, vertex.Name) + ok, issue, err := isVertexHealthy(h, ns, pipeline, v, vertex.Name) if err != nil { return PipelineStatusUnknown, err } if !ok { + log.Infof("vertex %q is unhealthy: %s", vertex.Name, issue) return PipelineStatusUnhealthy, nil } } @@ -68,19 +115,19 @@ func getPipelineVertexHealth(h *handler, ns, pipeline string) (string, error) { // isVertexHealthy is used to check if the number of replicas running in the vertex // are equal to the number of desired replicas and the pods are in running state. // We first check if the vertex is in running state, if not, return the error message from the status -func isVertexHealthy(h *handler, ns string, pipeline string, vertex *dfv1.Vertex, vertexName string) (bool, error) { +func isVertexHealthy(h *handler, ns string, pipeline string, vertex *dfv1.Vertex, vertexName string) (bool, string, error) { log := logging.FromContext(context.Background()) // check if the vertex is in running state log.Info("vertex status: ", vertex.Name, vertex.Status.Phase) if vertex.Status.Phase != dfv1.VertexPhaseRunning { - // check if the number of replicas running in the vertex - // are equal to the number of desired replicas - if int(vertex.Status.Replicas) != vertex.GetReplicas() { - return false, fmt.Errorf("vertex %q has %d replicas running, "+ - "expected %d", vertex.Name, vertex.Status.Replicas, vertex.GetReplicas()) - } + //// check if the number of replicas running in the vertex + //// are equal to the number of desired replicas + //if int(vertex.Status.Replicas) != vertex.GetReplicas() { + // return false, fmt.Sprintf("vertex %q has %d replicas running, "+ + // "expected %d", vertex.Name, vertex.Status.Replicas, vertex.GetReplicas()), nil + //} // Else return the error message from the status - return false, fmt.Errorf("error in vertex %s", vertex.Status.Message) + return false, fmt.Sprintf("error in vertex %s", vertex.Status.Message), nil } // Get all the pods for the given vertex @@ -88,7 +135,7 @@ func isVertexHealthy(h *handler, ns string, pipeline string, vertex *dfv1.Vertex LabelSelector: fmt.Sprintf("%s=%s,%s=%s", dfv1.KeyPipelineName, pipeline, dfv1.KeyVertexName, vertexName), }) if err != nil { - return false, err + return false, "", err } log.Info("number of pods: ", len(pods.Items)) // Iterate over all the pods, and verify if all the containers in the pod are in running state @@ -97,9 +144,10 @@ func isVertexHealthy(h *handler, ns string, pipeline string, vertex *dfv1.Vertex for _, containerStatus := range pod.Status.ContainerStatuses { // if the container is not in running state, return false if containerStatus.State.Running == nil { - return false, fmt.Errorf("container %q in pod %q is not ready", containerStatus.Name, pod.Name) + return false, fmt.Sprintf("container %q in pod %q is not ready", + containerStatus.Name, pod.Name), nil } } } - return true, nil + return true, "", nil } From d899680d0b615ca748619cadd07374389049ab50 Mon Sep 17 00:00:00 2001 From: Sidhant Kohli Date: Thu, 7 Dec 2023 23:18:02 -0800 Subject: [PATCH 03/12] return message and status Signed-off-by: Sidhant Kohli --- pkg/daemon/server/service/healthStatus.go | 16 +++-- server/apis/v1/handler.go | 14 ++--- server/apis/v1/health.go | 75 +++++++++++++++++++---- 3 files changed, 81 insertions(+), 24 deletions(-) diff --git a/pkg/daemon/server/service/healthStatus.go b/pkg/daemon/server/service/healthStatus.go index 23e59bab38..0db60791b1 100644 --- a/pkg/daemon/server/service/healthStatus.go +++ b/pkg/daemon/server/service/healthStatus.go @@ -13,16 +13,16 @@ import ( ) const ( - // TotalHealthTime is the total time window to compute the health of a vertex - TotalHealthTime = 5 * time.Minute + // HealthSlidingWindow is the total time window to compute the health of a vertex + HealthSlidingWindow = 5 * time.Minute - // HealthTimeStep is the time step to compute the health of a vertex + // HealthTimeStep is the frequency at which the health of a vertex is computed HealthTimeStep = 10 * time.Second // HealthWindowSize is the number of timeline entries to keep for a given vertex // This is used to compute the health of a vertex - // Ensure that TotalHealthTime / HealthTimeStep is an integer - HealthWindowSize = TotalHealthTime / HealthTimeStep + // Ensure that HealthSlidingWindow / HealthTimeStep is an integer + HealthWindowSize = HealthSlidingWindow / HealthTimeStep // CriticalWindowTime is the number of entries to look back to assign a critical state to a vertex CriticalWindowTime = 1 * time.Minute @@ -142,6 +142,7 @@ func (hc *HealthChecker) SetCurrentHealth(status string) { // StartHealthCheck starts the health check. func (hc *HealthChecker) StartHealthCheck() { + ctx := context.Background() log.Info("DEBUGSID StartHealthCheck") // Goroutine to listen for ticks @@ -152,6 +153,7 @@ func (hc *HealthChecker) StartHealthCheck() { defer ticker.Stop() for { select { + // If the ticker ticks, check and update the health status of the pipeline. case <-ticker.C: log.Info("DEBUGSID got tick") // Get the current health status of the pipeline. @@ -165,6 +167,10 @@ func (hc *HealthChecker) StartHealthCheck() { log.Info("DEBUGSID pipelineState", pipelineState) // update the current health status of the pipeline hc.SetCurrentHealth(pipelineState) + + // If the context is done, return. + case <-ctx.Done(): + return } } }() diff --git a/server/apis/v1/handler.go b/server/apis/v1/handler.go index 757c69a66a..24392cc037 100644 --- a/server/apis/v1/handler.go +++ b/server/apis/v1/handler.go @@ -897,7 +897,7 @@ func (h *handler) GetPipelineStatus(c *gin.Context) { // Get the vertex level health of the pipeline vertexHealth, err := h.healthChecker.getPipelineVertexHealth(h, ns, pipeline) if err != nil { - h.respondWithError(c, fmt.Sprintf("Failed to get the status for pipeline %q: %s", pipeline, err.Error())) + h.respondWithError(c, fmt.Sprintf("Failed to get the dataStatus for pipeline %q: %s", pipeline, err.Error())) return } @@ -908,17 +908,17 @@ func (h *handler) GetPipelineStatus(c *gin.Context) { return } // Get the data criticality for the given pipeline - status, err := client.GetPipelineStatus(context.Background(), pipeline) + dataStatus, err := client.GetPipelineStatus(context.Background(), pipeline) if err != nil { - h.respondWithError(c, fmt.Sprintf("Failed to get the status for pipeline %q: %s", pipeline, err.Error())) + h.respondWithError(c, fmt.Sprintf("Failed to get the dataStatus for pipeline %q: %s", pipeline, err.Error())) return } // Create a response string based on the vertex health and data criticality - // We combine both the states to get the final status of the pipeline - var response string - response = fmt.Sprintf("%s-%s", vertexHealth, status) - c.JSON(http.StatusOK, NewNumaflowAPIResponse(nil, response)) + // We combine both the states to get the final dataStatus of the pipeline + response, message := h.healthChecker.getCombinedHealthStatus(vertexHealth.Status, dataStatus.GetStatus(), + vertexHealth.Message, dataStatus.GetMessage()) + c.JSON(http.StatusOK, NewNumaflowAPIResponse(&message, response)) } // getAllNamespaces is a utility used to fetch all the namespaces in the cluster diff --git a/server/apis/v1/health.go b/server/apis/v1/health.go index 9f0636f60a..54c91ce898 100644 --- a/server/apis/v1/health.go +++ b/server/apis/v1/health.go @@ -17,12 +17,20 @@ const ( vertexCacheRefreshDuration = 30 * time.Second ) +// vertexHealthResponse is the response returned by the vertex health check API +type vertexHealthResponse struct { + // Status is the overall vertex status of the pipeline + Status string `json:"status"` + // Message is the error message if any + Message string `json:"message"` +} + type HealthChecker struct { - vertexStatusCache *evictCache.LRU[string, string] + vertexStatusCache *evictCache.LRU[string, *vertexHealthResponse] } func NewHealthChecker() *HealthChecker { - c := evictCache.NewLRU[string, string](500, nil, vertexCacheRefreshDuration) + c := evictCache.NewLRU[string, *vertexHealthResponse](500, nil, vertexCacheRefreshDuration) return &HealthChecker{ vertexStatusCache: c, } @@ -31,7 +39,7 @@ func NewHealthChecker() *HealthChecker { // getPipelineVertexHealth is used to provide the overall vertex health and status of the pipeline // This first check if the pipeline status is cached, if not, it checks for the current pipeline status // and caches it. -func (hc *HealthChecker) getPipelineVertexHealth(h *handler, ns string, pipeline string) (string, error) { +func (hc *HealthChecker) getPipelineVertexHealth(h *handler, ns string, pipeline string) (*vertexHealthResponse, error) { ctx := context.Background() log := logging.FromContext(ctx) @@ -48,10 +56,14 @@ func (hc *HealthChecker) getPipelineVertexHealth(h *handler, ns string, pipeline // if not, get the pipeline status status, err := checkVertexLevelHealth(ctx, h, ns, pipeline) if err != nil { - return PipelineStatusUnknown, err + return &vertexHealthResponse{ + Status: PipelineStatusUnknown, + Message: fmt.Sprintf("error in getting pipeline %q status: %v", pipeline, err), + }, err } // cache the pipeline status hc.vertexStatusCache.Add(cacheKey, status) + return status, nil } @@ -67,26 +79,35 @@ func (hc *HealthChecker) getPipelineVertexHealth(h *handler, ns string, pipeline // are equal to the number of desired replicas and the pods are in running state // 2) If all the containers in the pod are in running state // if any of the above conditions are not met, the vertex is unhealthy -func checkVertexLevelHealth(ctx context.Context, h *handler, ns string, pipeline string) (string, error) { +func checkVertexLevelHealth(ctx context.Context, h *handler, ns string, pipeline string) (*vertexHealthResponse, error) { log := logging.FromContext(ctx) // check if the pipeline is paused, if so, return paused status pl, err := h.numaflowClient.Pipelines(ns).Get(context.Background(), pipeline, metav1.GetOptions{}) // if error return unknown status if err != nil { - return PipelineStatusUnknown, err + return &vertexHealthResponse{ + Status: PipelineStatusUnknown, + Message: fmt.Sprintf("error in getting pipeline %q status: %v", pipeline, err), + }, err } // if the pipeline is paused, return inactive status // this cannot be checked at individual vertex level, hence needs to be checked here if pl.Spec.Lifecycle.GetDesiredPhase() == dfv1.PipelinePhasePaused { - return PipelineStatusInactive, nil + return &vertexHealthResponse{ + Status: PipelineStatusInactive, + Message: fmt.Sprintf("pipeline %q is paused", pipeline), + }, nil } // if the pipeline is killed, return killed status // this cannot be checked at individual vertex level, hence needs to be checked here // TODO(Health): Check if this is correct? if pl.Spec.Lifecycle.GetDesiredPhase() == dfv1.PipelinePhaseDeleting { - return PipelineStatusDeleting, nil + return &vertexHealthResponse{ + Status: PipelineStatusDeleting, + Message: fmt.Sprintf("pipeline %q is killed", pipeline), + }, nil } // get the list of vertices in the pipeline @@ -98,18 +119,31 @@ func checkVertexLevelHealth(ctx context.Context, h *handler, ns string, pipeline // fetch the current spec of the vertex v, err := h.numaflowClient.Vertices(ns).Get(context.Background(), vertexName, metav1.GetOptions{}) if err != nil { - return PipelineStatusUnknown, err + return &vertexHealthResponse{ + Status: PipelineStatusUnknown, + Message: fmt.Sprintf("error in getting vertex %q status: %v", vertexName, err), + }, err } ok, issue, err := isVertexHealthy(h, ns, pipeline, v, vertex.Name) if err != nil { - return PipelineStatusUnknown, err + return &vertexHealthResponse{ + Status: PipelineStatusUnknown, + Message: fmt.Sprintf("error in getting vertex %q status: %v", vertexName, err), + }, err } if !ok { log.Infof("vertex %q is unhealthy: %s", vertex.Name, issue) - return PipelineStatusUnhealthy, nil + return &vertexHealthResponse{ + Status: PipelineStatusUnhealthy, + Message: issue, + }, nil } } - return PipelineStatusHealthy, nil + // if all the vertices are healthy, return healthy status + return &vertexHealthResponse{ + Status: PipelineStatusHealthy, + Message: fmt.Sprintf("pipeline %q is healthy", pipeline), + }, nil } // isVertexHealthy is used to check if the number of replicas running in the vertex @@ -128,6 +162,7 @@ func isVertexHealthy(h *handler, ns string, pipeline string, vertex *dfv1.Vertex //} // Else return the error message from the status return false, fmt.Sprintf("error in vertex %s", vertex.Status.Message), nil + //setState(D1) -> D1 vertex is failing } // Get all the pods for the given vertex @@ -146,8 +181,24 @@ func isVertexHealthy(h *handler, ns string, pipeline string, vertex *dfv1.Vertex if containerStatus.State.Running == nil { return false, fmt.Sprintf("container %q in pod %q is not ready", containerStatus.Name, pod.Name), nil + // Set(D2) D2-> container is failing } } } return true, "", nil } + +// getCombinedHealthStatus is used to provide the overall health of the pipeline +// It combines the health status of all the vertices in the pipeline along with the data criticality status +// to provide the overall health of the pipeline +// It takes vertex health status and data criticality status as input along with any error message string to be returned +// The final state is returned as a string +func (hc *HealthChecker) getCombinedHealthStatus(vertexHealthStatus string, dataCriticalityStatus string, + vertexMessage string, dataMessage string) (string, string) { + // Join the vertex health status and data criticality status + resp := fmt.Sprintf("%s-%s", vertexHealthStatus, dataCriticalityStatus) + // Join the vertex message and data message + message := fmt.Sprintf("%s:%s", vertexMessage, dataMessage) + return resp, message + +} From af1e46896fdd9f164d4716e962e6cbeaee0b0a37 Mon Sep 17 00:00:00 2001 From: Sidhant Kohli Date: Fri, 8 Dec 2023 12:22:08 -0800 Subject: [PATCH 04/12] Add status code mapping Signed-off-by: Sidhant Kohli --- go.mod | 2 +- pkg/apis/proto/daemon/daemon.pb.go | 173 ++++++++++++------ pkg/apis/proto/daemon/daemon.proto | 1 + pkg/daemon/server/service/healthStatus.go | 125 ++++++++++--- .../server/service/pipeline_metrics_query.go | 5 +- .../service/pipeline_metrics_query_test.go | 118 ++++++------ pkg/shared/health-status-code/code_map.go | 109 +++++++++++ server/apis/v1/handler.go | 7 +- server/apis/v1/health.go | 88 +++++---- server/apis/v1/response_health.go | 42 +++++ 10 files changed, 479 insertions(+), 191 deletions(-) create mode 100644 pkg/shared/health-status-code/code_map.go create mode 100644 server/apis/v1/response_health.go diff --git a/go.mod b/go.mod index 949e40ab83..7761783fc3 100644 --- a/go.mod +++ b/go.mod @@ -46,6 +46,7 @@ require ( go.uber.org/multierr v1.7.0 go.uber.org/zap v1.24.0 golang.org/x/crypto v0.17.0 + golang.org/x/net v0.17.0 golang.org/x/oauth2 v0.7.0 golang.org/x/sync v0.3.0 google.golang.org/genproto/googleapis/api v0.0.0-20230525234035-dd9d682886f9 @@ -191,7 +192,6 @@ require ( go.mongodb.org/mongo-driver v1.7.3 // indirect golang.org/x/arch v0.3.0 // indirect golang.org/x/mod v0.8.0 // indirect - golang.org/x/net v0.17.0 // indirect golang.org/x/sys v0.15.0 // indirect golang.org/x/term v0.15.0 // indirect golang.org/x/text v0.14.0 // indirect diff --git a/pkg/apis/proto/daemon/daemon.pb.go b/pkg/apis/proto/daemon/daemon.pb.go index 58d8d8e27c..107cdfa9c9 100644 --- a/pkg/apis/proto/daemon/daemon.pb.go +++ b/pkg/apis/proto/daemon/daemon.pb.go @@ -220,6 +220,7 @@ func (m *VertexMetrics) GetPendings() map[string]int64 { type PipelineStatus struct { Status *string `protobuf:"bytes,1,req,name=status" json:"status,omitempty"` Message *string `protobuf:"bytes,2,req,name=message" json:"message,omitempty"` + Code *string `protobuf:"bytes,3,req,name=code" json:"code,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` XXX_unrecognized []byte `json:"-"` XXX_sizecache int32 `json:"-"` @@ -272,6 +273,13 @@ func (m *PipelineStatus) GetMessage() string { return "" } +func (m *PipelineStatus) GetCode() string { + if m != nil && m.Code != nil { + return *m.Code + } + return "" +} + type ListBuffersRequest struct { Pipeline *string `protobuf:"bytes,1,req,name=pipeline" json:"pipeline,omitempty"` XXX_NoUnkeyedLiteral struct{} `json:"-"` @@ -871,65 +879,66 @@ func init() { } var fileDescriptor_93e327fd0d673221 = []byte{ - // 926 bytes of a gzipped FileDescriptorProto + // 937 bytes of a gzipped FileDescriptorProto 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x8c, 0x56, 0x5f, 0x6f, 0xdc, 0x44, 0x10, 0x97, 0x7d, 0xf9, 0x73, 0x37, 0xe1, 0xda, 0x30, 0xb4, 0xc1, 0x75, 0x4b, 0x30, 0x6e, 0x0a, - 0x47, 0x28, 0x67, 0x88, 0x44, 0x55, 0xb5, 0x12, 0x45, 0x29, 0x69, 0x84, 0x48, 0x50, 0xb4, 0x05, - 0x2a, 0xf1, 0xe6, 0xdc, 0xed, 0xb9, 0x26, 0xb6, 0xd7, 0x78, 0xd7, 0x17, 0xa2, 0x2a, 0x2f, 0x48, - 0x7c, 0x02, 0xd4, 0x4f, 0x82, 0xc4, 0x47, 0x40, 0x3c, 0x22, 0xf1, 0xc8, 0x0b, 0x8a, 0xf8, 0x20, - 0x95, 0x77, 0xd7, 0x57, 0xfb, 0xe2, 0xbb, 0xe4, 0x29, 0x3b, 0x33, 0xbf, 0x99, 0xf9, 0x79, 0xfe, - 0x5d,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0xbe, 0x73, 0x01, 0x4a, 0xd3, 0xf4, 0x24, 0xcd, 0x0f, + 0xf1, 0x83, 0xb9, 0x34, 0x2b, 0x83, 0xf2, 0x8b, 0x21, 0x37, 0xdf, 0xd4, 0x37, 0xc1, 0x69, 0x88, + 0x56, 0xdb, 0x69, 0xf6, 0x7b, 0x73, 0x10, 0x9a, 0xcb, 0x47, 0x92, 0xcb, 0x1d, 0xbc, 0x3d, 0x97, + 0x8b, 0x5a, 0x5d, 0xdb, 0x5f, 0xfc, 0x79, 0xb6, 0x6e, 0xfc, 0x75, 0xb6, 0x6e, 0xfc, 0x7b, 0xb6, + 0x6e, 0x7c, 0xbf, 0x15, 0x84, 0xe2, 0x45, 0x7e, 0xd8, 0x1f, 0xb0, 0xd8, 0x4b, 0xf2, 0xd8, 0x4f, + 0x33, 0xf6, 0x83, 0x3c, 0x8c, 0x22, 0x76, 0xec, 0x35, 0xfe, 0xa1, 0xfb, 0x3f, 0x00, 0x00, 0xff, + 0xff, 0x85, 0xa5, 0xb2, 0x62, 0xe8, 0x09, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -1363,6 +1372,15 @@ func (m *PipelineStatus) MarshalToSizedBuffer(dAtA []byte) (int, error) { i -= len(m.XXX_unrecognized) copy(dAtA[i:], m.XXX_unrecognized) } + if m.Code == nil { + return 0, github_com_gogo_protobuf_proto.NewRequiredNotSetError("code") + } else { + i -= len(*m.Code) + copy(dAtA[i:], *m.Code) + i = encodeVarintDaemon(dAtA, i, uint64(len(*m.Code))) + i-- + dAtA[i] = 0x1a + } if m.Message == nil { return 0, github_com_gogo_protobuf_proto.NewRequiredNotSetError("message") } else { @@ -1971,6 +1989,10 @@ func (m *PipelineStatus) Size() (n int) { l = len(*m.Message) n += 1 + l + sovDaemon(uint64(l)) } + if m.Code != nil { + l = len(*m.Code) + n += 1 + l + sovDaemon(uint64(l)) + } if m.XXX_unrecognized != nil { n += len(m.XXX_unrecognized) } @@ -2915,6 +2937,40 @@ func (m *PipelineStatus) Unmarshal(dAtA []byte) error { m.Message = &s iNdEx = postIndex hasFields[0] |= uint64(0x00000002) + case 3: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Code", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowDaemon + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthDaemon + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthDaemon + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + s := string(dAtA[iNdEx:postIndex]) + m.Code = &s + iNdEx = postIndex + hasFields[0] |= uint64(0x00000004) default: iNdEx = preIndex skippy, err := skipDaemon(dAtA[iNdEx:]) @@ -2937,6 +2993,9 @@ func (m *PipelineStatus) Unmarshal(dAtA []byte) error { if hasFields[0]&uint64(0x00000002) == 0 { return github_com_gogo_protobuf_proto.NewRequiredNotSetError("message") } + if hasFields[0]&uint64(0x00000004) == 0 { + return github_com_gogo_protobuf_proto.NewRequiredNotSetError("code") + } if iNdEx > l { return io.ErrUnexpectedEOF diff --git a/pkg/apis/proto/daemon/daemon.proto b/pkg/apis/proto/daemon/daemon.proto index 56c8191a67..87a6786a03 100644 --- a/pkg/apis/proto/daemon/daemon.proto +++ b/pkg/apis/proto/daemon/daemon.proto @@ -49,6 +49,7 @@ message VertexMetrics { message PipelineStatus { required string status = 1; required string message = 2; + required string code = 3; } message ListBuffersRequest { diff --git a/pkg/daemon/server/service/healthStatus.go b/pkg/daemon/server/service/healthStatus.go index 0db60791b1..05ea99efd8 100644 --- a/pkg/daemon/server/service/healthStatus.go +++ b/pkg/daemon/server/service/healthStatus.go @@ -1,6 +1,7 @@ package service import ( + "fmt" "sync" "time" @@ -70,16 +71,39 @@ type TimelineEntry struct { AverageBufferUsage float64 `json:"averageBufferUsage"` } +// DataHealthResponse is the response returned by the data health check API +type DataHealthResponse struct { + // Status is the overall data status of the pipeline + Status string `json:"status"` + // Message is the error message if any + Message string `json:"message"` + // Code is the status code for the data health + Code string `json:"code"` +} + +// NewDataHealthResponse is used to create a new DataHealthResponse object +func NewDataHealthResponse(status string, message string, code string) *DataHealthResponse { + return &DataHealthResponse{ + Status: status, + Message: message, + Code: code, + } +} + +// DefaultDataHealthResponse is the default response returned by the data health check API +var DefaultDataHealthResponse = NewDataHealthResponse(PipelineStatusOK, + fmt.Sprintf("Pipeline dataflow is healthy"), + "D1") + var ( - currentPipelineStatus = PipelineStatusOK + currentPipelineStatus = DefaultDataHealthResponse pipeStatusLock = &sync.RWMutex{} - - log = logging.FromContext(context.Background()) + log = logging.FromContext(context.Background()) ) // GetCurrentPipelineHealth returns the current health status of the pipeline. // It is thread safe to ensure concurrent access. -func GetCurrentPipelineHealth() string { +func GetCurrentPipelineHealth() *DataHealthResponse { // Lock the statusLock to ensure thread safety. pipeStatusLock.RLock() defer pipeStatusLock.RUnlock() @@ -89,7 +113,7 @@ func GetCurrentPipelineHealth() string { // SetCurrentPipelineHealth sets the current health status of the pipeline. // It is thread safe to ensure concurrent access. -func SetCurrentPipelineHealth(status string) { +func SetCurrentPipelineHealth(status *DataHealthResponse) { // Lock the statusLock to ensure thread safety. pipeStatusLock.Lock() defer pipeStatusLock.Unlock() @@ -100,7 +124,7 @@ func SetCurrentPipelineHealth(status string) { // HealthChecker is the struct type for health checker. type HealthChecker struct { // Add a field for the health status. - currentDataStatus string + currentDataStatus *DataHealthResponse pipelineMetadataQuery *pipelineMetadataQuery pipeline *v1alpha1.Pipeline timelineData map[string][]*TimelineEntry @@ -111,7 +135,7 @@ type HealthChecker struct { func NewHealthChecker(pipeline *v1alpha1.Pipeline, pipelineMetadataQuery *pipelineMetadataQuery) *HealthChecker { // Return a new HealthChecker struct instance. return &HealthChecker{ - currentDataStatus: PipelineStatusOK, + currentDataStatus: DefaultDataHealthResponse, pipelineMetadataQuery: pipelineMetadataQuery, pipeline: pipeline, timelineData: make(map[string][]*TimelineEntry), @@ -119,9 +143,25 @@ func NewHealthChecker(pipeline *v1alpha1.Pipeline, pipelineMetadataQuery *pipeli } } +// VertexState is a struct which contains the name and state of a vertex +type VertexState struct { + // Name is the name of the vertex + Name string `json:"name"` + // State is the state of the vertex + State string `json:"state"` +} + +// NewVertexState is used to create a new VertexState object +func NewVertexState(name string, state string) *VertexState { + return &VertexState{ + Name: name, + State: state, + } +} + // GetCurrentHealth returns the current health status of the pipeline. // It is thread safe to ensure concurrent access. -func (hc *HealthChecker) GetCurrentHealth() string { +func (hc *HealthChecker) GetCurrentHealth() *DataHealthResponse { // Lock the statusLock to ensure thread safety. hc.statusLock.RLock() defer hc.statusLock.RUnlock() @@ -131,7 +171,7 @@ func (hc *HealthChecker) GetCurrentHealth() string { // SetCurrentHealth sets the current health status of the pipeline. // It is thread safe to ensure concurrent access. -func (hc *HealthChecker) SetCurrentHealth(status string) { +func (hc *HealthChecker) SetCurrentHealth(status *DataHealthResponse) { // Lock the statusLock to ensure thread safety. hc.statusLock.Lock() defer hc.statusLock.Unlock() @@ -143,8 +183,6 @@ func (hc *HealthChecker) SetCurrentHealth(status string) { // StartHealthCheck starts the health check. func (hc *HealthChecker) StartHealthCheck() { ctx := context.Background() - - log.Info("DEBUGSID StartHealthCheck") // Goroutine to listen for ticks // At every tick, check and update the health status of the pipeline. go func() { @@ -155,16 +193,13 @@ func (hc *HealthChecker) StartHealthCheck() { select { // If the ticker ticks, check and update the health status of the pipeline. case <-ticker.C: - log.Info("DEBUGSID got tick") // Get the current health status of the pipeline. criticality, err := hc.getPipelineDataCriticality() if err != nil { return } - log.Info("DEBUGSID criticality", criticality) // convert the vertex state to pipeline state pipelineState := convertVertexStateToPipelineState(criticality) - log.Info("DEBUGSID pipelineState", pipelineState) // update the current health status of the pipeline hc.SetCurrentHealth(pipelineState) @@ -191,7 +226,7 @@ func (hc *HealthChecker) StartHealthCheck() { // If this average is greater than the critical threshold, we return the critical status // If this average is greater than the warning threshold, we return the warning status // If this average is less than the warning threshold, we return the ok status -func (hc *HealthChecker) getPipelineDataCriticality() ([]string, error) { +func (hc *HealthChecker) getPipelineDataCriticality() ([]*VertexState, error) { ctx := context.Background() pipelineName := hc.pipeline.GetName() @@ -206,7 +241,7 @@ func (hc *HealthChecker) getPipelineDataCriticality() ([]string, error) { // update the usage timeline for all the ISBs used in the pipeline hc.updateUsageTimeline(buffers.Buffers) - var vertexState []string + var vertexState []*VertexState // iterate over the timeline data for each buffer and calculate the exponential weighted mean average // for the last HEALTH_WINDOW_SIZE buffer usage entries @@ -223,7 +258,10 @@ func (hc *HealthChecker) getPipelineDataCriticality() ([]string, error) { // assign the state to the vertex based on the average buffer usage // Look back is disabled for the critical state currentState := assignStateToTimeline(ewmaBufferUsage, EnableCriticalLookBack) - vertexState = append(vertexState, currentState) + // create a new vertex state object + currentVertexState := NewVertexState(bufferName, currentState) + // add the vertex state to the list of vertex states + vertexState = append(vertexState, currentVertexState) } return vertexState, nil } @@ -344,7 +382,6 @@ func assignStateToBufferUsage(ewmaValue float64) string { // If the state is CRITICAL at least LOOK_BACK_COUNT times in the last CRITICAL_WINDOW_SIZE entries // Set the state to CRITICAL func assignStateToTimeline(ewmaValues []float64, lookBack bool) string { - // Extract the last entry of the timeline ewmaUsage := ewmaValues[len(ewmaValues)-1] @@ -387,7 +424,7 @@ func assignStateToTimeline(ewmaValues []float64, lookBack bool) string { // else the pipeline is ok // Here we follow a precedence order of unknown > critical > warning > ok // Hence, whichever is the highest precedence state found in the vertex state, we return that -func convertVertexStateToPipelineState(vertexState []string) string { +func convertVertexStateToPipelineState(vertexState []*VertexState) *DataHealthResponse { // create a map to store the precedence order of the states // assign a number to each state based on the precedence order @@ -401,19 +438,59 @@ func convertVertexStateToPipelineState(vertexState []string) string { // initialize the max state to 0 (ie Healthy state) maxState := 0 + // initialize the max state vertex to empty string + maxStateVtx := "" // iterate over the vertex state and assign a number to each state and update the current max state for _, state := range vertexState { - if stateMap[state] > maxState { - maxState = stateMap[state] + if stateMap[state.State] > maxState { + maxState = stateMap[state.State] + maxStateVtx = state.Name } } - // get the state corresponding to the max state + // get the state and vertex corresponding to the max state for state, value := range stateMap { if value == maxState { - return state + return generateDataHealthResponse(state, maxStateVtx) } } - return PipelineStatusUnknown + + // if we reach here, return unknown state + return NewDataHealthResponse(PipelineStatusUnknown, + fmt.Sprintf("Pipeline dataflow is in an unknown state"), + "D4") +} + +// generateDataHealthResponse is used to generate the data health response +// if the state is Healthy we return a message saying the pipeline is healthy, and the code corresponding to Healthy +// if the state is Warning we return a message saying the pipeline is warning due to the vertex, +// and the code corresponding to Warning, similar for Critical +// if the state is Unknown we return a message saying the pipeline is in an unknown state due to the vertex, +// and the code corresponding to Unknown +func generateDataHealthResponse(state string, vertex string) *DataHealthResponse { + switch state { + case PipelineStatusOK: + return NewDataHealthResponse( + PipelineStatusOK, + fmt.Sprintf("Pipeline dataflow is healthy"), + "D1") + case PipelineStatusWarning: + return NewDataHealthResponse( + PipelineStatusWarning, + fmt.Sprintf("Dataflow is in warning state for %s", vertex), + "D2") + case PipelineStatusCritical: + return NewDataHealthResponse( + PipelineStatusCritical, + fmt.Sprintf("Dataflow is in critical state for %s", vertex), + "D3") + case PipelineStatusUnknown: + return NewDataHealthResponse( + PipelineStatusUnknown, + fmt.Sprintf("Pipeline dataflow is in an unknown state due to %s", vertex), + "D4") + default: + return DefaultDataHealthResponse + } } diff --git a/pkg/daemon/server/service/pipeline_metrics_query.go b/pkg/daemon/server/service/pipeline_metrics_query.go index 8592300cb4..377e67e55c 100644 --- a/pkg/daemon/server/service/pipeline_metrics_query.go +++ b/pkg/daemon/server/service/pipeline_metrics_query.go @@ -309,8 +309,9 @@ func (ps *pipelineMetadataQuery) GetPipelineStatus(ctx context.Context, req *dae status := GetCurrentPipelineHealth() resp := new(daemon.GetPipelineStatusResponse) resp.Status = &daemon.PipelineStatus{ - Status: pointer.String(status), - Message: pointer.String(fmt.Sprintf("Pipeline status is %s.", status)), + Status: pointer.String(status.Status), + Message: pointer.String(status.Message), + Code: pointer.String(status.Code), } return resp, nil } diff --git a/pkg/daemon/server/service/pipeline_metrics_query_test.go b/pkg/daemon/server/service/pipeline_metrics_query_test.go index 8fcbbce357..d20270daa9 100644 --- a/pkg/daemon/server/service/pipeline_metrics_query_test.go +++ b/pkg/daemon/server/service/pipeline_metrics_query_test.go @@ -25,7 +25,6 @@ import ( "github.com/stretchr/testify/assert" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/utils/pointer" "github.com/numaproj/numaflow/pkg/apis/numaflow/v1alpha1" "github.com/numaproj/numaflow/pkg/apis/proto/daemon" @@ -241,61 +240,62 @@ func (mr *mockRater_TestGetPipelineStatus) GetRates(vertexName string, partition return res } -func TestGetPipelineStatus(t *testing.T) { - pipelineName := "simple-pipeline" - pipeline := &v1alpha1.Pipeline{ - ObjectMeta: metav1.ObjectMeta{ - Name: pipelineName, - }, - Spec: v1alpha1.PipelineSpec{ - Vertices: []v1alpha1.AbstractVertex{ - {Name: "cat"}, - }, - }, - } - client, _ := isbsvc.NewISBJetStreamSvc(pipelineName) - metricsResponse := `# HELP vertex_pending_messages Average pending messages in the last period of seconds. It is the pending messages of a vertex, not a pod. -# TYPE vertex_pending_messages gauge -vertex_pending_messages{period="15m",partition_name="-simple-pipeline-cat-0",pipeline="simple-pipeline",vertex="cat"} 4.011 -vertex_pending_messages{period="1m",partition_name="-simple-pipeline-cat-0",pipeline="simple-pipeline",vertex="cat"} 5.333 -vertex_pending_messages{period="5m",partition_name="-simple-pipeline-cat-0",pipeline="simple-pipeline",vertex="cat"} 6.002 -vertex_pending_messages{period="default",partition_name="-simple-pipeline-cat-0",pipeline="simple-pipeline",vertex="cat"} 7.00002 -` - req := &daemon.GetPipelineStatusRequest{Pipeline: &pipelineName} - - // test when rater is actively processing - activeRater := &mockRater_TestGetPipelineStatus{isActivelyProcessing: true} - pipelineMetricsQueryService, err := NewPipelineMetadataQuery(client, pipeline, nil, activeRater) - assert.NoError(t, err) - ioReader := io.NopCloser(bytes.NewReader([]byte(metricsResponse))) - pipelineMetricsQueryService.httpClient = &mockHttpClient{ - MockGet: func(url string) (*http.Response, error) { - return &http.Response{ - StatusCode: 200, - Body: ioReader, - }, nil - }, - } - resp, err := pipelineMetricsQueryService.GetPipelineStatus(context.Background(), req) - assert.NoError(t, err) - OKPipelineResponse := daemon.PipelineStatus{Status: pointer.String("OK"), Message: pointer.String("Pipeline has no issue.")} - assert.Equal(t, &OKPipelineResponse, resp.Status) - - // test when rater is not actively processing - idleRater := &mockRater_TestGetPipelineStatus{isActivelyProcessing: false} - pipelineMetricsQueryService, err = NewPipelineMetadataQuery(client, pipeline, nil, idleRater) - assert.NoError(t, err) - ioReader = io.NopCloser(bytes.NewReader([]byte(metricsResponse))) - pipelineMetricsQueryService.httpClient = &mockHttpClient{ - MockGet: func(url string) (*http.Response, error) { - return &http.Response{ - StatusCode: 200, - Body: ioReader, - }, nil - }, - } - resp, err = pipelineMetricsQueryService.GetPipelineStatus(context.Background(), req) - assert.NoError(t, err) - ErrorPipelineResponse := daemon.PipelineStatus{Status: pointer.String("Error"), Message: pointer.String("Pipeline has an error. Vertex cat is not processing pending messages.")} - assert.Equal(t, &ErrorPipelineResponse, resp.Status) -} +// +//func TestGetPipelineStatus(t *testing.T) { +// pipelineName := "simple-pipeline" +// pipeline := &v1alpha1.Pipeline{ +// ObjectMeta: metav1.ObjectMeta{ +// Name: pipelineName, +// }, +// Spec: v1alpha1.PipelineSpec{ +// Vertices: []v1alpha1.AbstractVertex{ +// {Name: "cat"}, +// }, +// }, +// } +// client, _ := isbsvc.NewISBJetStreamSvc(pipelineName) +// metricsResponse := `# HELP vertex_pending_messages Average pending messages in the last period of seconds. It is the pending messages of a vertex, not a pod. +//# TYPE vertex_pending_messages gauge +//vertex_pending_messages{period="15m",partition_name="-simple-pipeline-cat-0",pipeline="simple-pipeline",vertex="cat"} 4.011 +//vertex_pending_messages{period="1m",partition_name="-simple-pipeline-cat-0",pipeline="simple-pipeline",vertex="cat"} 5.333 +//vertex_pending_messages{period="5m",partition_name="-simple-pipeline-cat-0",pipeline="simple-pipeline",vertex="cat"} 6.002 +//vertex_pending_messages{period="default",partition_name="-simple-pipeline-cat-0",pipeline="simple-pipeline",vertex="cat"} 7.00002 +//` +// req := &daemon.GetPipelineStatusRequest{Pipeline: &pipelineName} +// +// // test when rater is actively processing +// activeRater := &mockRater_TestGetPipelineStatus{isActivelyProcessing: true} +// pipelineMetricsQueryService, err := NewPipelineMetadataQuery(client, pipeline, nil, activeRater) +// assert.NoError(t, err) +// ioReader := io.NopCloser(bytes.NewReader([]byte(metricsResponse))) +// pipelineMetricsQueryService.httpClient = &mockHttpClient{ +// MockGet: func(url string) (*http.Response, error) { +// return &http.Response{ +// StatusCode: 200, +// Body: ioReader, +// }, nil +// }, +// } +// resp, err := pipelineMetricsQueryService.GetPipelineStatus(context.Background(), req) +// assert.NoError(t, err) +// OKPipelineResponse := daemon.PipelineStatus{Status: pointer.String("OK"), Message: pointer.String("Pipeline has no issue.")} +// assert.Equal(t, &OKPipelineResponse, resp.Status) +// +// // test when rater is not actively processing +// idleRater := &mockRater_TestGetPipelineStatus{isActivelyProcessing: false} +// pipelineMetricsQueryService, err = NewPipelineMetadataQuery(client, pipeline, nil, idleRater) +// assert.NoError(t, err) +// ioReader = io.NopCloser(bytes.NewReader([]byte(metricsResponse))) +// pipelineMetricsQueryService.httpClient = &mockHttpClient{ +// MockGet: func(url string) (*http.Response, error) { +// return &http.Response{ +// StatusCode: 200, +// Body: ioReader, +// }, nil +// }, +// } +// resp, err = pipelineMetricsQueryService.GetPipelineStatus(context.Background(), req) +// assert.NoError(t, err) +// ErrorPipelineResponse := daemon.PipelineStatus{Status: pointer.String("Error"), Message: pointer.String("Pipeline has an error. Vertex cat is not processing pending messages.")} +// assert.Equal(t, &ErrorPipelineResponse, resp.Status) +//} diff --git a/pkg/shared/health-status-code/code_map.go b/pkg/shared/health-status-code/code_map.go new file mode 100644 index 0000000000..c68b1119fe --- /dev/null +++ b/pkg/shared/health-status-code/code_map.go @@ -0,0 +1,109 @@ +package health_status_code + +// HealthCodeInfo is used to maintain status codes for vertex level health +type HealthCodeInfo struct { + Status string + Criticality string +} + +// newHealthCodeInfo is used to create a new HealthCodeInfo object +func newHealthCodeInfo(status string, criticality string) *HealthCodeInfo { + return &HealthCodeInfo{ + Status: status, + Criticality: criticality, + } +} + +// VertexHealthMap is used to maintain status codes for vertex level health +// Each map entry is a map of status code as the key to the status message and the criticality of the status. +// Status codes are in incremental like +// 1. V1 +// 2. V2 +// 3. V3 +// The criticality is used to determine the overall status of the pipeline +// Criticality can be one of the following: +// 1. Critical: The pipeline is in a critical state +// 2. Warning: The pipeline is in a warning state +// 3. Healthy: The pipeline is healthy +// 4. Unknown: The pipeline is in an unknown state + +var vertexHealthMap = map[string]*HealthCodeInfo{ + "V1": newHealthCodeInfo( + "All pods are running", + "Healthy", + ), + "V2": newHealthCodeInfo( + "Vertex is not in running state", + "Critical", + ), + "V3": newHealthCodeInfo( + "Vertex is in running but containers are not in running state", + "Warning", + ), + "V4": newHealthCodeInfo( + "All vertices healthy in the pipeline", + "Healthy", + ), + "V5": newHealthCodeInfo( + "One or more vertices are unhealthy in the pipeline", + "Warning", + ), + "V6": newHealthCodeInfo( + "Pipeline is in an unknown state", + "Critical", + ), + "V7": newHealthCodeInfo( + "Pipeline is paused", + "Healthy", + ), + "V8": newHealthCodeInfo( + "Pipeline is killed", + "Healthy", + ), + "V9": newHealthCodeInfo( + "Vertex not able to get the desired number of replicas", + "Warning", + ), +} + +// DataflowHealthCodeMap is used to maintain status codes for dataflow level health +// Each map entry is a map of status code as the key to the status message and the criticality of the status +// Status codes are in incremental like +// 1. D1 +// 2. D2 +// 3. D3 +// The criticality is used to determine the overall status of the pipeline +// Criticality can be one of the following: +// 1. Critical: The pipeline is in a critical state +// 2. Warning: The pipeline is in a warning state +// 3. Healthy: The pipeline is healthy + +var dataflowHealthMap = map[string]*HealthCodeInfo{ + "D1": newHealthCodeInfo( + "Dataflow is healthy", + "Healthy", + ), + "D2": newHealthCodeInfo( + "Dataflow in warning state for one or more vertices", + "Warning", + ), + "D3": newHealthCodeInfo( + "Dataflow in critical state for one or more vertices", + "Critical", + ), + "D4": newHealthCodeInfo( + "Dataflow in unknown state", + "Critical", + ), +} + +// getHealthCodeInfo is used to get the status code information for a given status code +func (HealthCodeInfo) getHealthCodeInfo(code string) *HealthCodeInfo { + if status, ok := vertexHealthMap[code]; ok { + return status + } + if status, ok := dataflowHealthMap[code]; ok { + return status + } + return nil +} diff --git a/server/apis/v1/handler.go b/server/apis/v1/handler.go index 24392cc037..0bdba4ea93 100644 --- a/server/apis/v1/handler.go +++ b/server/apis/v1/handler.go @@ -916,9 +916,10 @@ func (h *handler) GetPipelineStatus(c *gin.Context) { // Create a response string based on the vertex health and data criticality // We combine both the states to get the final dataStatus of the pipeline - response, message := h.healthChecker.getCombinedHealthStatus(vertexHealth.Status, dataStatus.GetStatus(), - vertexHealth.Message, dataStatus.GetMessage()) - c.JSON(http.StatusOK, NewNumaflowAPIResponse(&message, response)) + response := NewHealthResponse(vertexHealth.Status, dataStatus.GetStatus(), + vertexHealth.Message, dataStatus.GetMessage(), vertexHealth.Code, dataStatus.GetCode()) + + c.JSON(http.StatusOK, NewNumaflowAPIResponse(nil, response)) } // getAllNamespaces is a utility used to fetch all the namespaces in the cluster diff --git a/server/apis/v1/health.go b/server/apis/v1/health.go index 54c91ce898..9e8152ab83 100644 --- a/server/apis/v1/health.go +++ b/server/apis/v1/health.go @@ -23,6 +23,8 @@ type vertexHealthResponse struct { Status string `json:"status"` // Message is the error message if any Message string `json:"message"` + // Code is the status code for the vertex health + Code string `json:"code"` } type HealthChecker struct { @@ -53,15 +55,12 @@ func (hc *HealthChecker) getPipelineVertexHealth(h *handler, ns string, pipeline log.Info("pipeline status from cache: ", status) return status, nil } - // if not, get the pipeline status + // if not present in cache, check for the current pipeline status status, err := checkVertexLevelHealth(ctx, h, ns, pipeline) if err != nil { - return &vertexHealthResponse{ - Status: PipelineStatusUnknown, - Message: fmt.Sprintf("error in getting pipeline %q status: %v", pipeline, err), - }, err + return status, err } - // cache the pipeline status + // update cache with the new pipeline status hc.vertexStatusCache.Add(cacheKey, status) return status, nil @@ -88,6 +87,7 @@ func checkVertexLevelHealth(ctx context.Context, h *handler, ns string, pipeline return &vertexHealthResponse{ Status: PipelineStatusUnknown, Message: fmt.Sprintf("error in getting pipeline %q status: %v", pipeline, err), + Code: "V6", }, err } @@ -97,6 +97,7 @@ func checkVertexLevelHealth(ctx context.Context, h *handler, ns string, pipeline return &vertexHealthResponse{ Status: PipelineStatusInactive, Message: fmt.Sprintf("pipeline %q is paused", pipeline), + Code: "V7", }, nil } @@ -107,6 +108,7 @@ func checkVertexLevelHealth(ctx context.Context, h *handler, ns string, pipeline return &vertexHealthResponse{ Status: PipelineStatusDeleting, Message: fmt.Sprintf("pipeline %q is killed", pipeline), + Code: "V8", }, nil } @@ -122,47 +124,50 @@ func checkVertexLevelHealth(ctx context.Context, h *handler, ns string, pipeline return &vertexHealthResponse{ Status: PipelineStatusUnknown, Message: fmt.Sprintf("error in getting vertex %q status: %v", vertexName, err), + Code: "V6", }, err } - ok, issue, err := isVertexHealthy(h, ns, pipeline, v, vertex.Name) + ok, resp, err := isVertexHealthy(h, ns, pipeline, v, vertex.Name) if err != nil { - return &vertexHealthResponse{ - Status: PipelineStatusUnknown, - Message: fmt.Sprintf("error in getting vertex %q status: %v", vertexName, err), - }, err + resp.Status = PipelineStatusUnknown + return resp, err } if !ok { - log.Infof("vertex %q is unhealthy: %s", vertex.Name, issue) - return &vertexHealthResponse{ - Status: PipelineStatusUnhealthy, - Message: issue, - }, nil + log.Infof("vertex %q is unhealthy: %s", vertex.Name, resp) + resp.Status = PipelineStatusUnhealthy + return resp, nil } } // if all the vertices are healthy, return healthy status return &vertexHealthResponse{ Status: PipelineStatusHealthy, Message: fmt.Sprintf("pipeline %q is healthy", pipeline), + Code: "V4", }, nil } // isVertexHealthy is used to check if the number of replicas running in the vertex // are equal to the number of desired replicas and the pods are in running state. // We first check if the vertex is in running state, if not, return the error message from the status -func isVertexHealthy(h *handler, ns string, pipeline string, vertex *dfv1.Vertex, vertexName string) (bool, string, error) { +func isVertexHealthy(h *handler, ns string, pipeline string, vertex *dfv1.Vertex, vertexName string) (bool, *vertexHealthResponse, error) { log := logging.FromContext(context.Background()) // check if the vertex is in running state log.Info("vertex status: ", vertex.Name, vertex.Status.Phase) if vertex.Status.Phase != dfv1.VertexPhaseRunning { - //// check if the number of replicas running in the vertex - //// are equal to the number of desired replicas - //if int(vertex.Status.Replicas) != vertex.GetReplicas() { - // return false, fmt.Sprintf("vertex %q has %d replicas running, "+ - // "expected %d", vertex.Name, vertex.Status.Replicas, vertex.GetReplicas()), nil - //} + // check if the number of replicas running in the vertex + // are equal to the number of desired replicas + if int(vertex.Status.Replicas) != vertex.GetReplicas() { + return false, &vertexHealthResponse{ + Message: fmt.Sprintf("vertex %q has %d replicas running, "+ + "expected %d", vertex.Name, vertex.Status.Replicas, vertex.GetReplicas()), + Code: "V9", + }, nil + } // Else return the error message from the status - return false, fmt.Sprintf("error in vertex %s", vertex.Status.Message), nil - //setState(D1) -> D1 vertex is failing + return false, &vertexHealthResponse{ + Message: fmt.Sprintf("error in vertex %s", vertex.Status.Message), + Code: "V2", + }, nil } // Get all the pods for the given vertex @@ -170,7 +175,10 @@ func isVertexHealthy(h *handler, ns string, pipeline string, vertex *dfv1.Vertex LabelSelector: fmt.Sprintf("%s=%s,%s=%s", dfv1.KeyPipelineName, pipeline, dfv1.KeyVertexName, vertexName), }) if err != nil { - return false, "", err + return false, &vertexHealthResponse{ + Message: fmt.Sprintf("error in getting pods for vertex %q: %v", vertexName, err), + Code: "V6", + }, err } log.Info("number of pods: ", len(pods.Items)) // Iterate over all the pods, and verify if all the containers in the pod are in running state @@ -179,26 +187,16 @@ func isVertexHealthy(h *handler, ns string, pipeline string, vertex *dfv1.Vertex for _, containerStatus := range pod.Status.ContainerStatuses { // if the container is not in running state, return false if containerStatus.State.Running == nil { - return false, fmt.Sprintf("container %q in pod %q is not ready", - containerStatus.Name, pod.Name), nil - // Set(D2) D2-> container is failing + return false, &vertexHealthResponse{ + Message: fmt.Sprintf("container %q in pod %q is not running", + containerStatus.Name, pod.Name), + Code: "V3", + }, nil } } } - return true, "", nil -} - -// getCombinedHealthStatus is used to provide the overall health of the pipeline -// It combines the health status of all the vertices in the pipeline along with the data criticality status -// to provide the overall health of the pipeline -// It takes vertex health status and data criticality status as input along with any error message string to be returned -// The final state is returned as a string -func (hc *HealthChecker) getCombinedHealthStatus(vertexHealthStatus string, dataCriticalityStatus string, - vertexMessage string, dataMessage string) (string, string) { - // Join the vertex health status and data criticality status - resp := fmt.Sprintf("%s-%s", vertexHealthStatus, dataCriticalityStatus) - // Join the vertex message and data message - message := fmt.Sprintf("%s:%s", vertexMessage, dataMessage) - return resp, message - + return true, &vertexHealthResponse{ + Message: fmt.Sprintf("vertex %q is healthy", vertexName), + Code: "V1", + }, nil } diff --git a/server/apis/v1/response_health.go b/server/apis/v1/response_health.go new file mode 100644 index 0000000000..b152759351 --- /dev/null +++ b/server/apis/v1/response_health.go @@ -0,0 +1,42 @@ +/* +Copyright 2022 The Numaproj Authors. + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +package v1 + +// HealthResponse is the response payload for health API. +// It contains the health status of the vertex and data. +// We include the Status, Message and Code for both vertex and data. +type HealthResponse struct { + VertexHealthStatus string `json:"vertexHealthStatus"` + DataHealthStatus string `json:"dataHealthStatus"` + VertexHealthMessage string `json:"vertexHealthMessage"` + DataHealthMessage string `json:"dataHealthMessage"` + VertexHealthCode string `json:"vertexHealthCode"` + DataHealthCode string `json:"dataHealthCode"` +} + +// NewHealthResponse returns a HealthResponse object for the given status, message and code. +func NewHealthResponse(vertexHealthStatus string, dataHealthStatus string, + vertexHealthMessage string, dataHealthMessage string, vertexHealthCode string, dataHealthCode string) HealthResponse { + return HealthResponse{ + VertexHealthStatus: vertexHealthStatus, + DataHealthStatus: dataHealthStatus, + VertexHealthMessage: vertexHealthMessage, + DataHealthMessage: dataHealthMessage, + VertexHealthCode: vertexHealthCode, + DataHealthCode: dataHealthCode, + } +} From 2f6299bef5f5ee935c534be47f6dda5ae49ef3c6 Mon Sep 17 00:00:00 2001 From: Sidhant Kohli Date: Fri, 8 Dec 2023 13:51:50 -0800 Subject: [PATCH 05/12] rename to resource health Signed-off-by: Sidhant Kohli --- pkg/daemon/server/service/healthStatus.go | 6 +- .../service/pipeline_metrics_query_test.go | 46 +++++----- pkg/shared/health-status-code/code_map.go | 4 +- .../health-status-code/code_map_test.go | 92 +++++++++++++++++++ server/apis/v1/handler.go | 23 +++-- server/apis/v1/health.go | 52 ++++++----- server/apis/v1/response_health.go | 29 +++--- 7 files changed, 174 insertions(+), 78 deletions(-) create mode 100644 pkg/shared/health-status-code/code_map_test.go diff --git a/pkg/daemon/server/service/healthStatus.go b/pkg/daemon/server/service/healthStatus.go index 05ea99efd8..f783d41ed1 100644 --- a/pkg/daemon/server/service/healthStatus.go +++ b/pkg/daemon/server/service/healthStatus.go @@ -92,7 +92,7 @@ func NewDataHealthResponse(status string, message string, code string) *DataHeal // DefaultDataHealthResponse is the default response returned by the data health check API var DefaultDataHealthResponse = NewDataHealthResponse(PipelineStatusOK, - fmt.Sprintf("Pipeline dataflow is healthy"), + "Pipeline dataflow is healthy", "D1") var ( @@ -458,7 +458,7 @@ func convertVertexStateToPipelineState(vertexState []*VertexState) *DataHealthRe // if we reach here, return unknown state return NewDataHealthResponse(PipelineStatusUnknown, - fmt.Sprintf("Pipeline dataflow is in an unknown state"), + "Pipeline dataflow is in an unknown state", "D4") } @@ -473,7 +473,7 @@ func generateDataHealthResponse(state string, vertex string) *DataHealthResponse case PipelineStatusOK: return NewDataHealthResponse( PipelineStatusOK, - fmt.Sprintf("Pipeline dataflow is healthy"), + "Pipeline dataflow is healthy", "D1") case PipelineStatusWarning: return NewDataHealthResponse( diff --git a/pkg/daemon/server/service/pipeline_metrics_query_test.go b/pkg/daemon/server/service/pipeline_metrics_query_test.go index d20270daa9..f8d34993c9 100644 --- a/pkg/daemon/server/service/pipeline_metrics_query_test.go +++ b/pkg/daemon/server/service/pipeline_metrics_query_test.go @@ -216,29 +216,29 @@ func TestListBuffers(t *testing.T) { } // mock rater -type mockRater_TestGetPipelineStatus struct { - isActivelyProcessing bool -} - -func (mr *mockRater_TestGetPipelineStatus) Start(ctx context.Context) error { - return nil -} - -func (mr *mockRater_TestGetPipelineStatus) GetRates(vertexName string, partitionName string) map[string]float64 { - res := make(map[string]float64) - if mr.isActivelyProcessing { - res["default"] = 4.894736842105263 - res["1m"] = 5.084745762711864 - res["5m"] = 4.894736842105263 - res["15m"] = 4.894736842105263 - } else { - res["default"] = 0 - res["1m"] = 0 - res["5m"] = 0 - res["15m"] = 0 - } - return res -} +//type mockRater_TestGetPipelineStatus struct { +// isActivelyProcessing bool +//} +// +//func (mr *mockRater_TestGetPipelineStatus) Start(ctx context.Context) error { +// return nil +//} +// +//func (mr *mockRater_TestGetPipelineStatus) GetRates(vertexName string, partitionName string) map[string]float64 { +// res := make(map[string]float64) +// if mr.isActivelyProcessing { +// res["default"] = 4.894736842105263 +// res["1m"] = 5.084745762711864 +// res["5m"] = 4.894736842105263 +// res["15m"] = 4.894736842105263 +// } else { +// res["default"] = 0 +// res["1m"] = 0 +// res["5m"] = 0 +// res["15m"] = 0 +// } +// return res +//} // //func TestGetPipelineStatus(t *testing.T) { diff --git a/pkg/shared/health-status-code/code_map.go b/pkg/shared/health-status-code/code_map.go index c68b1119fe..3dc408e8a6 100644 --- a/pkg/shared/health-status-code/code_map.go +++ b/pkg/shared/health-status-code/code_map.go @@ -67,7 +67,7 @@ var vertexHealthMap = map[string]*HealthCodeInfo{ } // DataflowHealthCodeMap is used to maintain status codes for dataflow level health -// Each map entry is a map of status code as the key to the status message and the criticality of the status +// Each map entry is a map of status code as the key to the status message and the criticality of the status. // Status codes are in incremental like // 1. D1 // 2. D2 @@ -98,7 +98,7 @@ var dataflowHealthMap = map[string]*HealthCodeInfo{ } // getHealthCodeInfo is used to get the status code information for a given status code -func (HealthCodeInfo) getHealthCodeInfo(code string) *HealthCodeInfo { +func getHealthCodeInfo(code string) *HealthCodeInfo { if status, ok := vertexHealthMap[code]; ok { return status } diff --git a/pkg/shared/health-status-code/code_map_test.go b/pkg/shared/health-status-code/code_map_test.go new file mode 100644 index 0000000000..6444615b07 --- /dev/null +++ b/pkg/shared/health-status-code/code_map_test.go @@ -0,0 +1,92 @@ +package health_status_code + +import "testing" + +func TestGetHealthCodeInfo(t *testing.T) { + type args struct { + code string + } + tests := []struct { + name string + args args + want *HealthCodeInfo + }{ + { + name: "TestGetHealthCodeInfo", + args: args{ + code: "V1", + }, + want: &HealthCodeInfo{ + Status: "All pods are running", + Criticality: "Healthy", + }, + }, + { + name: "TestGetHealthCodeInfo", + args: args{ + code: "V2", + }, + want: &HealthCodeInfo{ + Status: "Vertex is not in running state", + Criticality: "Critical", + }, + }, + { + name: "TestGetHealthCodeInfo", + args: args{ + code: "V3", + }, + want: &HealthCodeInfo{ + Status: "Vertex is in running but containers are not in running state", + Criticality: "Warning", + }, + }, + { + name: "TestGetHealthCodeInfo", + args: args{ + code: "V4", + }, + want: &HealthCodeInfo{ + Status: "All vertices healthy in the pipeline", + Criticality: "Healthy", + }, + }, + { + name: "TestGetHealthCodeInfo", + args: args{ + code: "V5", + }, + want: &HealthCodeInfo{ + Status: "One or more vertices are unhealthy in the pipeline", + Criticality: "Warning", + }, + }, + { + name: "TestGetHealthCodeInfo", + args: args{ + code: "V6", + }, + want: &HealthCodeInfo{ + Status: "Pipeline is in an unknown state", + Criticality: "Critical", + }, + }, + { + name: "TestGetHealthCodeInfo", + args: args{ + code: "V7", + }, + want: &HealthCodeInfo{ + Status: "Pipeline is paused", + Criticality: "Healthy", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if got := getHealthCodeInfo(tt.args.code); got.Status != tt.want.Status { + t.Errorf("GetHealthCodeInfo() = %v, want %v", got, tt.want) + } + }) + } +} diff --git a/server/apis/v1/handler.go b/server/apis/v1/handler.go index 0bdba4ea93..a726468c64 100644 --- a/server/apis/v1/handler.go +++ b/server/apis/v1/handler.go @@ -878,24 +878,23 @@ func (h *handler) GetNamespaceEvents(c *gin.Context) { c.JSON(http.StatusOK, NewNumaflowAPIResponse(nil, response)) } -// GetPipelineStatus returns the pipeline status. It is based on Health and Criticality. -// Health can be "healthy (0) | unhealthy (1) | paused (3) | unknown (4)". -// Health here indicates pipeline's ability to process messages. -// A backlogged pipeline can be healthy even though it has an increasing back-pressure. Health purely means it is up and running. -// Pipelines health will be the max(health) based of each vertex's health -// Criticality on the other end shows whether the pipeline is working as expected. +// GetPipelineStatus returns the pipeline status. It is based on Resource Health and Data Criticality. +// Resource Health can be "healthy (0) | unhealthy (1) | paused (3) | unknown (4)". +// A backlogged pipeline can be healthy even though it has an increasing back-pressure. +// Resource Health purely means it is up and running. +// Resource health will be the max(health) based of each vertex's health +// Data Criticality on the other end shows whether the pipeline is working as expected. // It represents the pending messages, lags, etc. -// Criticality can be "ok (0) | warning (1) | critical (2)". -// Health and Criticality are different because ...? +// Data Criticality can be "ok (0) | warning (1) | critical (2)". // GetPipelineStatus is used to return the status of a given pipeline // It is divided into two parts: -// 1. Pipeline Vertex Health: It is based on the health of each vertex in the pipeline +// 1. Pipeline Resource Health: It is based on the health of each vertex in the pipeline // 2. Data Criticality: It is based on the data movement of the pipeline func (h *handler) GetPipelineStatus(c *gin.Context) { ns, pipeline := c.Param("namespace"), c.Param("pipeline") // Get the vertex level health of the pipeline - vertexHealth, err := h.healthChecker.getPipelineVertexHealth(h, ns, pipeline) + resourceHealth, err := h.healthChecker.getPipelineResourceHealth(h, ns, pipeline) if err != nil { h.respondWithError(c, fmt.Sprintf("Failed to get the dataStatus for pipeline %q: %s", pipeline, err.Error())) return @@ -916,8 +915,8 @@ func (h *handler) GetPipelineStatus(c *gin.Context) { // Create a response string based on the vertex health and data criticality // We combine both the states to get the final dataStatus of the pipeline - response := NewHealthResponse(vertexHealth.Status, dataStatus.GetStatus(), - vertexHealth.Message, dataStatus.GetMessage(), vertexHealth.Code, dataStatus.GetCode()) + response := NewHealthResponse(resourceHealth.Status, dataStatus.GetStatus(), + resourceHealth.Message, dataStatus.GetMessage(), resourceHealth.Code, dataStatus.GetCode()) c.JSON(http.StatusOK, NewNumaflowAPIResponse(nil, response)) } diff --git a/server/apis/v1/health.go b/server/apis/v1/health.go index 9e8152ab83..c4c4e6c42e 100644 --- a/server/apis/v1/health.go +++ b/server/apis/v1/health.go @@ -13,12 +13,12 @@ import ( ) const ( - // vertexCacheRefreshDuration is the duration after which the vertex status cache is refreshed - vertexCacheRefreshDuration = 30 * time.Second + // resourceCacheRefreshDuration is the duration after which the vertex status cache is refreshed + resourceCacheRefreshDuration = 30 * time.Second ) -// vertexHealthResponse is the response returned by the vertex health check API -type vertexHealthResponse struct { +// resourceHealthResponse is the response returned by the vertex health check API +type resourceHealthResponse struct { // Status is the overall vertex status of the pipeline Status string `json:"status"` // Message is the error message if any @@ -28,20 +28,21 @@ type vertexHealthResponse struct { } type HealthChecker struct { - vertexStatusCache *evictCache.LRU[string, *vertexHealthResponse] + resourceStatusCache *evictCache.LRU[string, *resourceHealthResponse] } func NewHealthChecker() *HealthChecker { - c := evictCache.NewLRU[string, *vertexHealthResponse](500, nil, vertexCacheRefreshDuration) + c := evictCache.NewLRU[string, *resourceHealthResponse](500, nil, resourceCacheRefreshDuration) return &HealthChecker{ - vertexStatusCache: c, + resourceStatusCache: c, } } -// getPipelineVertexHealth is used to provide the overall vertex health and status of the pipeline +// getPipelineResourceHealth is used to provide the overall vertex health and status of the pipeline // This first check if the pipeline status is cached, if not, it checks for the current pipeline status // and caches it. -func (hc *HealthChecker) getPipelineVertexHealth(h *handler, ns string, pipeline string) (*vertexHealthResponse, error) { +func (hc *HealthChecker) getPipelineResourceHealth(h *handler, ns string, + pipeline string) (*resourceHealthResponse, error) { ctx := context.Background() log := logging.FromContext(ctx) @@ -51,7 +52,7 @@ func (hc *HealthChecker) getPipelineVertexHealth(h *handler, ns string, pipeline cacheKey := fmt.Sprintf("%s-%s", ns, pipeline) // check if the pipeline status is cached - if status, ok := hc.vertexStatusCache.Get(cacheKey); ok { + if status, ok := hc.resourceStatusCache.Get(cacheKey); ok { log.Info("pipeline status from cache: ", status) return status, nil } @@ -61,7 +62,7 @@ func (hc *HealthChecker) getPipelineVertexHealth(h *handler, ns string, pipeline return status, err } // update cache with the new pipeline status - hc.vertexStatusCache.Add(cacheKey, status) + hc.resourceStatusCache.Add(cacheKey, status) return status, nil } @@ -78,13 +79,14 @@ func (hc *HealthChecker) getPipelineVertexHealth(h *handler, ns string, pipeline // are equal to the number of desired replicas and the pods are in running state // 2) If all the containers in the pod are in running state // if any of the above conditions are not met, the vertex is unhealthy -func checkVertexLevelHealth(ctx context.Context, h *handler, ns string, pipeline string) (*vertexHealthResponse, error) { +func checkVertexLevelHealth(ctx context.Context, h *handler, ns string, + pipeline string) (*resourceHealthResponse, error) { log := logging.FromContext(ctx) // check if the pipeline is paused, if so, return paused status pl, err := h.numaflowClient.Pipelines(ns).Get(context.Background(), pipeline, metav1.GetOptions{}) // if error return unknown status if err != nil { - return &vertexHealthResponse{ + return &resourceHealthResponse{ Status: PipelineStatusUnknown, Message: fmt.Sprintf("error in getting pipeline %q status: %v", pipeline, err), Code: "V6", @@ -94,7 +96,7 @@ func checkVertexLevelHealth(ctx context.Context, h *handler, ns string, pipeline // if the pipeline is paused, return inactive status // this cannot be checked at individual vertex level, hence needs to be checked here if pl.Spec.Lifecycle.GetDesiredPhase() == dfv1.PipelinePhasePaused { - return &vertexHealthResponse{ + return &resourceHealthResponse{ Status: PipelineStatusInactive, Message: fmt.Sprintf("pipeline %q is paused", pipeline), Code: "V7", @@ -105,7 +107,7 @@ func checkVertexLevelHealth(ctx context.Context, h *handler, ns string, pipeline // this cannot be checked at individual vertex level, hence needs to be checked here // TODO(Health): Check if this is correct? if pl.Spec.Lifecycle.GetDesiredPhase() == dfv1.PipelinePhaseDeleting { - return &vertexHealthResponse{ + return &resourceHealthResponse{ Status: PipelineStatusDeleting, Message: fmt.Sprintf("pipeline %q is killed", pipeline), Code: "V8", @@ -121,7 +123,7 @@ func checkVertexLevelHealth(ctx context.Context, h *handler, ns string, pipeline // fetch the current spec of the vertex v, err := h.numaflowClient.Vertices(ns).Get(context.Background(), vertexName, metav1.GetOptions{}) if err != nil { - return &vertexHealthResponse{ + return &resourceHealthResponse{ Status: PipelineStatusUnknown, Message: fmt.Sprintf("error in getting vertex %q status: %v", vertexName, err), Code: "V6", @@ -139,7 +141,7 @@ func checkVertexLevelHealth(ctx context.Context, h *handler, ns string, pipeline } } // if all the vertices are healthy, return healthy status - return &vertexHealthResponse{ + return &resourceHealthResponse{ Status: PipelineStatusHealthy, Message: fmt.Sprintf("pipeline %q is healthy", pipeline), Code: "V4", @@ -149,7 +151,8 @@ func checkVertexLevelHealth(ctx context.Context, h *handler, ns string, pipeline // isVertexHealthy is used to check if the number of replicas running in the vertex // are equal to the number of desired replicas and the pods are in running state. // We first check if the vertex is in running state, if not, return the error message from the status -func isVertexHealthy(h *handler, ns string, pipeline string, vertex *dfv1.Vertex, vertexName string) (bool, *vertexHealthResponse, error) { +func isVertexHealthy(h *handler, ns string, pipeline string, vertex *dfv1.Vertex, + vertexName string) (bool, *resourceHealthResponse, error) { log := logging.FromContext(context.Background()) // check if the vertex is in running state log.Info("vertex status: ", vertex.Name, vertex.Status.Phase) @@ -157,14 +160,14 @@ func isVertexHealthy(h *handler, ns string, pipeline string, vertex *dfv1.Vertex // check if the number of replicas running in the vertex // are equal to the number of desired replicas if int(vertex.Status.Replicas) != vertex.GetReplicas() { - return false, &vertexHealthResponse{ + return false, &resourceHealthResponse{ Message: fmt.Sprintf("vertex %q has %d replicas running, "+ "expected %d", vertex.Name, vertex.Status.Replicas, vertex.GetReplicas()), Code: "V9", }, nil } // Else return the error message from the status - return false, &vertexHealthResponse{ + return false, &resourceHealthResponse{ Message: fmt.Sprintf("error in vertex %s", vertex.Status.Message), Code: "V2", }, nil @@ -172,10 +175,11 @@ func isVertexHealthy(h *handler, ns string, pipeline string, vertex *dfv1.Vertex // Get all the pods for the given vertex pods, err := h.kubeClient.CoreV1().Pods(ns).List(context.Background(), metav1.ListOptions{ - LabelSelector: fmt.Sprintf("%s=%s,%s=%s", dfv1.KeyPipelineName, pipeline, dfv1.KeyVertexName, vertexName), + LabelSelector: fmt.Sprintf("%s=%s,%s=%s", dfv1.KeyPipelineName, + pipeline, dfv1.KeyVertexName, vertexName), }) if err != nil { - return false, &vertexHealthResponse{ + return false, &resourceHealthResponse{ Message: fmt.Sprintf("error in getting pods for vertex %q: %v", vertexName, err), Code: "V6", }, err @@ -187,7 +191,7 @@ func isVertexHealthy(h *handler, ns string, pipeline string, vertex *dfv1.Vertex for _, containerStatus := range pod.Status.ContainerStatuses { // if the container is not in running state, return false if containerStatus.State.Running == nil { - return false, &vertexHealthResponse{ + return false, &resourceHealthResponse{ Message: fmt.Sprintf("container %q in pod %q is not running", containerStatus.Name, pod.Name), Code: "V3", @@ -195,7 +199,7 @@ func isVertexHealthy(h *handler, ns string, pipeline string, vertex *dfv1.Vertex } } } - return true, &vertexHealthResponse{ + return true, &resourceHealthResponse{ Message: fmt.Sprintf("vertex %q is healthy", vertexName), Code: "V1", }, nil diff --git a/server/apis/v1/response_health.go b/server/apis/v1/response_health.go index b152759351..6b1dd2d3a3 100644 --- a/server/apis/v1/response_health.go +++ b/server/apis/v1/response_health.go @@ -20,23 +20,24 @@ package v1 // It contains the health status of the vertex and data. // We include the Status, Message and Code for both vertex and data. type HealthResponse struct { - VertexHealthStatus string `json:"vertexHealthStatus"` - DataHealthStatus string `json:"dataHealthStatus"` - VertexHealthMessage string `json:"vertexHealthMessage"` - DataHealthMessage string `json:"dataHealthMessage"` - VertexHealthCode string `json:"vertexHealthCode"` - DataHealthCode string `json:"dataHealthCode"` + ResourceHealthStatus string `json:"resourceHealthStatus"` + DataHealthStatus string `json:"dataHealthStatus"` + ResourceHealthMessage string `json:"resourceHealthMessage"` + DataHealthMessage string `json:"dataHealthMessage"` + ResourceHealthCode string `json:"resourceHealthCode"` + DataHealthCode string `json:"dataHealthCode"` } // NewHealthResponse returns a HealthResponse object for the given status, message and code. -func NewHealthResponse(vertexHealthStatus string, dataHealthStatus string, - vertexHealthMessage string, dataHealthMessage string, vertexHealthCode string, dataHealthCode string) HealthResponse { +func NewHealthResponse(resourceHealthStatus string, dataHealthStatus string, + resourceHealthMessage string, dataHealthMessage string, resourceHealthCode string, + dataHealthCode string) HealthResponse { return HealthResponse{ - VertexHealthStatus: vertexHealthStatus, - DataHealthStatus: dataHealthStatus, - VertexHealthMessage: vertexHealthMessage, - DataHealthMessage: dataHealthMessage, - VertexHealthCode: vertexHealthCode, - DataHealthCode: dataHealthCode, + ResourceHealthStatus: resourceHealthStatus, + DataHealthStatus: dataHealthStatus, + ResourceHealthMessage: resourceHealthMessage, + DataHealthMessage: dataHealthMessage, + ResourceHealthCode: resourceHealthCode, + DataHealthCode: dataHealthCode, } } From c5cee898b6eb64b692092907a7cedc02a4df2c4a Mon Sep 17 00:00:00 2001 From: Sidhant Kohli Date: Fri, 8 Dec 2023 16:43:21 -0800 Subject: [PATCH 06/12] add unit tests Signed-off-by: Sidhant Kohli --- pkg/shared/health-status-code/code_map.go | 1 - server/apis/v1/health.go | 3 - server/apis/v1/health_test.go | 160 ++++++++++++++++++++++ server/apis/v1/test_utils.go | 152 ++++++++++++++++++++ 4 files changed, 312 insertions(+), 4 deletions(-) create mode 100644 server/apis/v1/health_test.go create mode 100644 server/apis/v1/test_utils.go diff --git a/pkg/shared/health-status-code/code_map.go b/pkg/shared/health-status-code/code_map.go index 3dc408e8a6..44af395645 100644 --- a/pkg/shared/health-status-code/code_map.go +++ b/pkg/shared/health-status-code/code_map.go @@ -26,7 +26,6 @@ func newHealthCodeInfo(status string, criticality string) *HealthCodeInfo { // 2. Warning: The pipeline is in a warning state // 3. Healthy: The pipeline is healthy // 4. Unknown: The pipeline is in an unknown state - var vertexHealthMap = map[string]*HealthCodeInfo{ "V1": newHealthCodeInfo( "All pods are running", diff --git a/server/apis/v1/health.go b/server/apis/v1/health.go index c4c4e6c42e..23142fc9d7 100644 --- a/server/apis/v1/health.go +++ b/server/apis/v1/health.go @@ -153,9 +153,7 @@ func checkVertexLevelHealth(ctx context.Context, h *handler, ns string, // We first check if the vertex is in running state, if not, return the error message from the status func isVertexHealthy(h *handler, ns string, pipeline string, vertex *dfv1.Vertex, vertexName string) (bool, *resourceHealthResponse, error) { - log := logging.FromContext(context.Background()) // check if the vertex is in running state - log.Info("vertex status: ", vertex.Name, vertex.Status.Phase) if vertex.Status.Phase != dfv1.VertexPhaseRunning { // check if the number of replicas running in the vertex // are equal to the number of desired replicas @@ -184,7 +182,6 @@ func isVertexHealthy(h *handler, ns string, pipeline string, vertex *dfv1.Vertex Code: "V6", }, err } - log.Info("number of pods: ", len(pods.Items)) // Iterate over all the pods, and verify if all the containers in the pod are in running state for _, pod := range pods.Items { // Iterate over all the containers in the pod diff --git a/server/apis/v1/health_test.go b/server/apis/v1/health_test.go new file mode 100644 index 0000000000..a2bdab718e --- /dev/null +++ b/server/apis/v1/health_test.go @@ -0,0 +1,160 @@ +package v1 + +import ( + "fmt" + "testing" + + "github.com/stretchr/testify/assert" + "golang.org/x/net/context" + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/client-go/kubernetes" + "k8s.io/utils/pointer" + + dfv1 "github.com/numaproj/numaflow/pkg/apis/numaflow/v1alpha1" +) + +const ( + RunningPod = "running" + WaitingPod = "waiting" +) + +// Client is the struct to hold the Kubernetes Clientset +type Client struct { + Clientset kubernetes.Interface +} + +func (c Client) CreatePod(pod *v1.Pod) (*v1.Pod, error) { + _, err := c.Clientset.CoreV1().Pods(pod.Namespace).Create(context.TODO(), pod, metav1.CreateOptions{}) + if err != nil { + fmt.Printf("Error occured while creating pod %s: %s", pod.Name, err.Error()) + return nil, err + } + + fmt.Printf("Pod %s is succesfully created", pod.Name) + return pod, nil +} + +func createPod(phase string) { + client := Client{ + Clientset: fakeKubeClient, + } + pod := fakePod("test-pl", "test-vertex", testNamespace, phase) + _, err := client.CreatePod(pod) + if err != nil { + fmt.Print(err.Error()) + } + pod, err = client.Clientset.CoreV1().Pods(pod.Namespace).Get(context.Background(), pod.Name, metav1.GetOptions{}) + if err != nil { + fmt.Printf("Error occured while getting pod %s: %s", pod.Name, err.Error()) + } +} + +func removePod() { + client := Client{ + Clientset: fakeKubeClient, + } + pod := fakePod("test-pl", "test-vertex", testNamespace, "running") + err := client.Clientset.CoreV1().Pods(pod.Namespace).Delete(context.Background(), pod.Name, metav1.DeleteOptions{}) + if err != nil { + fmt.Printf("Error occured while deleting pod %s: %s", pod.Name, err.Error()) + } +} + +// fakePipeline returns a fake pipeline for testing + +// TestIsVertexHealthy verifies the functionality of the vertex level health check +// It creates a fake pipeline and checks the health of each vertex +// We test multiple scenarios: +// 1. All vertices are healthy +// 2. One or more vertices are unhealthy +// 3. The number of replicas running in the vertex is less than the expected number of replicas +// 4. Vertex is not in running state +func TestIsVertexHealthy(t *testing.T) { + // Test vertex is in running phase and the pods are in running state + t.Run("test all pods are healthy", func(t *testing.T) { + pipeline := fakePipeline() + vertexName := "test-vertex" + vertex := fakeVertex(vertexName, dfv1.VertexPhaseRunning) + + // Create fake handler + h := &handler{ + kubeClient: fakeKubeClient, + numaflowClient: &fakeNumaClient, + } + + // Create fake pod in running state + createPod(RunningPod) + defer removePod() + healthy, _, err := isVertexHealthy(h, testNamespace, pipeline.GetName(), vertex, vertexName) + if err != nil { + return + } + assert.True(t, healthy) + }) + + // Test vertex is in running phase and the pods are in waiting state + t.Run("test pod not running", func(t *testing.T) { + pipeline := fakePipeline() + vertexName := "test-vertex" + vertex := fakeVertex(vertexName, dfv1.VertexPhaseRunning) + + // Create fake handler + h := &handler{ + kubeClient: fakeKubeClient, + numaflowClient: &fakeNumaClient, + } + createPod(WaitingPod) + defer removePod() + healthy, r, err := isVertexHealthy(h, testNamespace, pipeline.GetName(), vertex, vertexName) + if err != nil { + return + } + assert.False(t, healthy) + assert.Equal(t, "V3", r.Code) + }) + + // Test vertex is not in running state + t.Run("test vertex not in running phase", func(t *testing.T) { + pipeline := fakePipeline() + vertexName := "test-vertex" + // Create a fake vertex in failed state + vertex := fakeVertex(vertexName, dfv1.VertexPhaseFailed) + + // Create fake handler + h := &handler{ + kubeClient: fakeKubeClient, + numaflowClient: &fakeNumaClient, + } + healthy, r, err := isVertexHealthy(h, testNamespace, pipeline.GetName(), vertex, vertexName) + if err != nil { + return + } + assert.False(t, healthy) + // Refer: pkg/shared/health-status-code + assert.Equal(t, "V2", r.Code) + }) + + // Test vertex replica count is not equal to the desired replica count + t.Run("test vertex replica count not equal to desired replica count", func(t *testing.T) { + pipeline := fakePipeline() + vertexName := "test-vertex" + // Create a fake vertex in failed state + vertex := fakeVertex(vertexName, dfv1.VertexPhaseFailed) + // Update the replica count to 2 + vertex.Spec.Replicas = pointer.Int32(2) + + // Create fake handler + h := &handler{ + kubeClient: fakeKubeClient, + numaflowClient: &fakeNumaClient, + } + healthy, r, err := isVertexHealthy(h, testNamespace, pipeline.GetName(), vertex, vertexName) + if err != nil { + return + } + assert.False(t, healthy) + // Refer: pkg/shared/health-status-code + assert.Equal(t, "V9", r.Code) + }) +} diff --git a/server/apis/v1/test_utils.go b/server/apis/v1/test_utils.go new file mode 100644 index 0000000000..e4e0a8d4e9 --- /dev/null +++ b/server/apis/v1/test_utils.go @@ -0,0 +1,152 @@ +package v1 + +import ( + "time" + + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + fakeclientset "k8s.io/client-go/kubernetes/fake" + "k8s.io/utils/pointer" + + dfv1 "github.com/numaproj/numaflow/pkg/apis/numaflow/v1alpha1" + "github.com/numaproj/numaflow/pkg/client/clientset/versioned/typed/numaflow/v1alpha1/fake" +) + +const testNamespace = "test-ns" + +var ( + fakeNumaClient = fake.FakeNumaflowV1alpha1{} + fakeKubeClient = fakeclientset.NewSimpleClientset() + testContainerName = "test-container" +) + +// getContainerStatus returns a container status with the given phase. +func getContainerStatus(phase string) corev1.ContainerStatus { + switch phase { + case "running": + return corev1.ContainerStatus{ + Name: testContainerName, + Ready: true, + State: corev1.ContainerState{ + Running: &corev1.ContainerStateRunning{ + StartedAt: metav1.Time{Time: time.Now()}, + }, + }, + } + case "waiting": + return corev1.ContainerStatus{ + Name: testContainerName, + Ready: false, + State: corev1.ContainerState{ + Waiting: &corev1.ContainerStateWaiting{ + Reason: "test-reason", + Message: "test-message", + }, + }, + } + } + return corev1.ContainerStatus{} +} + +// fakePod returns a fake pod with the given pipeline name, vertex name, namespace and phase. +func fakePod(pipelineName string, vertexName string, namespace string, phase string) *corev1.Pod { + containerStatus := getContainerStatus(phase) + pod := &corev1.Pod{ + TypeMeta: metav1.TypeMeta{ + Kind: "Pod", + APIVersion: "v1", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: "test-pod", + Namespace: namespace, + Labels: map[string]string{ + dfv1.KeyPipelineName: pipelineName, + dfv1.KeyVertexName: vertexName, + }, + }, + Spec: corev1.PodSpec{ + Containers: []corev1.Container{ + { + Name: testContainerName, + }, + }, + }, + Status: corev1.PodStatus{ + Phase: corev1.PodPhase("Running"), + ContainerStatuses: []corev1.ContainerStatus{containerStatus}, + }, + } + return pod +} + +func fakePipeline() *dfv1.Pipeline { + return &dfv1.Pipeline{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-pl", + Namespace: testNamespace, + }, + Spec: dfv1.PipelineSpec{ + Vertices: []dfv1.AbstractVertex{ + { + Name: "input", + Source: &dfv1.Source{ + UDTransformer: &dfv1.UDTransformer{ + Builtin: &dfv1.Transformer{Name: "filter"}, + }}, + }, + { + Name: "map", + UDF: &dfv1.UDF{ + Builtin: &dfv1.Function{Name: "cat"}, + }, + }, + { + Name: "reduce", + UDF: &dfv1.UDF{ + Container: &dfv1.Container{ + Image: "test-image", + }, + GroupBy: &dfv1.GroupBy{ + Window: dfv1.Window{ + Fixed: &dfv1.FixedWindow{Length: &metav1.Duration{ + Duration: 60 * time.Second, + }}, + }, + Keyed: true, + Storage: &dfv1.PBQStorage{ + EmptyDir: &corev1.EmptyDirVolumeSource{}, + }, + }, + }, + }, + { + Name: "output", + Sink: &dfv1.Sink{}, + }, + }, + Edges: []dfv1.Edge{ + {From: "input", To: "map"}, + {From: "map", To: "reduce"}, + {From: "reduce", To: "output"}, + }, + }, + } +} + +// fakeVertex returns a fake vertex with the given name and phase. +func fakeVertex(name string, phase dfv1.VertexPhase) *dfv1.Vertex { + v := &dfv1.Vertex{ + ObjectMeta: metav1.ObjectMeta{ + Name: name, + Namespace: testNamespace, + }, + Status: dfv1.VertexStatus{ + Phase: phase, + Replicas: 1, + }, + Spec: dfv1.VertexSpec{ + Replicas: pointer.Int32(1), + }, + } + return v +} From adc0692aecff7fd054a681636a7f293b421e744c Mon Sep 17 00:00:00 2001 From: Sidhant Kohli Date: Mon, 11 Dec 2023 10:24:55 -0800 Subject: [PATCH 07/12] clean up Signed-off-by: Sidhant Kohli --- pkg/daemon/server/daemon_server.go | 5 -- pkg/daemon/server/service/healthStatus.go | 8 +- .../server/service/pipeline_metrics_query.go | 54 ------------ .../service/pipeline_metrics_query_test.go | 85 ------------------- 4 files changed, 3 insertions(+), 149 deletions(-) diff --git a/pkg/daemon/server/daemon_server.go b/pkg/daemon/server/daemon_server.go index d84154291c..61ba266dba 100644 --- a/pkg/daemon/server/daemon_server.go +++ b/pkg/daemon/server/daemon_server.go @@ -138,19 +138,14 @@ func (ds *daemonServer) Run(ctx context.Context) error { go func() { _ = httpServer.Serve(httpL) }() go func() { _ = tcpm.Serve() }() - // Start the Data flow status updater - // This function should be continuously running, and invoked every HEALTH_CHECK_INTERVAL seconds - pipelineMetadataQuery, err := service.NewPipelineMetadataQuery(isbSvcClient, ds.pipeline, wmFetchers, rater) if err != nil { log.Errorw("Failed to create pipeline metadata query", zap.Error(err)) } healthChecker := service.NewHealthChecker(ds.pipeline, pipelineMetadataQuery) - log.Infof("DEBUGSID starting StartHealthCheck %s", healthChecker.GetCurrentHealth()) // Start the Data flow status updater go func() { - log.Infof("DEBUGSID starting StartHealthCheck") healthChecker.StartHealthCheck() }() diff --git a/pkg/daemon/server/service/healthStatus.go b/pkg/daemon/server/service/healthStatus.go index f783d41ed1..630ea627fb 100644 --- a/pkg/daemon/server/service/healthStatus.go +++ b/pkg/daemon/server/service/healthStatus.go @@ -180,7 +180,8 @@ func (hc *HealthChecker) SetCurrentHealth(status *DataHealthResponse) { SetCurrentPipelineHealth(status) } -// StartHealthCheck starts the health check. +// StartHealthCheck starts the health check for the pipeline. +// The ticks are generated at the interval of HealthTimeStep. func (hc *HealthChecker) StartHealthCheck() { ctx := context.Background() // Goroutine to listen for ticks @@ -252,11 +253,9 @@ func (hc *HealthChecker) getPipelineDataCriticality() ([]*VertexState, error) { bufferUsage = append(bufferUsage, entry.BufferUsage) } // calculate the average buffer usage over the last HEALTH_WINDOW_SIZE seconds - log.Info("DEBUGSID bufferusage", bufferName, bufferUsage) ewmaBufferUsage := calculateEWMAUsage(bufferUsage) - log.Info("DEBUGSID ewmavalue", bufferName, ewmaBufferUsage) // assign the state to the vertex based on the average buffer usage - // Look back is disabled for the critical state + // Look back is enabled for the critical state currentState := assignStateToTimeline(ewmaBufferUsage, EnableCriticalLookBack) // create a new vertex state object currentVertexState := NewVertexState(bufferName, currentState) @@ -299,7 +298,6 @@ func (hc *HealthChecker) updateUsageTimeline(bufferList []*daemon.BufferInfo) { BufferUsage: bufferUsage, AverageBufferUsage: newAverage, }) - log.Info("DEBUGSID", bufferName, bufferUsage, newAverage) // remove first entry if the size of the timeline is greater than HEALTH_WINDOW_SIZE if len(hc.timelineData[bufferName]) > int(HealthWindowSize) { diff --git a/pkg/daemon/server/service/pipeline_metrics_query.go b/pkg/daemon/server/service/pipeline_metrics_query.go index 377e67e55c..f4669e3d44 100644 --- a/pkg/daemon/server/service/pipeline_metrics_query.go +++ b/pkg/daemon/server/service/pipeline_metrics_query.go @@ -251,60 +251,6 @@ func (ps *pipelineMetadataQuery) getPending(ctx context.Context, req *daemon.Get return totalPendingMap } -//func (ps *pipelineMetadataQuery) GetPipelineStatus(ctx context.Context, req *daemon.GetPipelineStatusRequest) (*daemon.GetPipelineStatusResponse, error) { -// -// resp := new(daemon.GetPipelineStatusResponse) -// -// // get all vertices of pipeline -// vertices := ps.pipeline.Spec.Vertices -// -// // loop over vertices and get metrics to check pending messages vs processing rate -// for _, vertex := range vertices { -// vertexReq := new(daemon.GetVertexMetricsRequest) -// vertexReq.Vertex = &vertex.Name -// vertexResp, err := ps.GetVertexMetrics(ctx, vertexReq) -// // if err is not nil, more than likely autoscaling is down to 0 and metrics are not available -// if err != nil { -// resp.Status = &daemon.PipelineStatus{ -// Status: pointer.String(PipelineStatusUnknown), -// Message: pointer.String("Pipeline status is unknown."), -// } -// return resp, nil -// } -// -// totalProcessingRate := float64(0) -// totalPending := int64(0) -// // may need to revisit later, another concern could be that the processing rate is too slow instead of just 0 -// for _, vertexMetrics := range vertexResp.VertexMetrics { -// if vertexMetrics.GetProcessingRates() != nil { -// if p, ok := vertexMetrics.GetProcessingRates()["default"]; ok { -// totalProcessingRate += p -// } -// } -// if vertexMetrics.GetPendings() != nil { -// if p, ok := vertexMetrics.GetPendings()["default"]; ok { -// totalPending += p -// } -// } -// } -// -// if totalPending > 0 && totalProcessingRate == 0 { -// resp.Status = &daemon.PipelineStatus{ -// Status: pointer.String(PipelineStatusError), -// Message: pointer.String(fmt.Sprintf("Pipeline has an error. Vertex %s is not processing pending messages.", vertex.Name)), -// } -// return resp, nil -// } -// } -// -// resp.Status = &daemon.PipelineStatus{ -// Status: pointer.String(PipelineStatusOK), -// Message: pointer.String("Pipeline has no issue."), -// } -// -// return resp, nil -//} - func (ps *pipelineMetadataQuery) GetPipelineStatus(ctx context.Context, req *daemon.GetPipelineStatusRequest) (*daemon.GetPipelineStatusResponse, error) { status := GetCurrentPipelineHealth() resp := new(daemon.GetPipelineStatusResponse) diff --git a/pkg/daemon/server/service/pipeline_metrics_query_test.go b/pkg/daemon/server/service/pipeline_metrics_query_test.go index f8d34993c9..56aefb3294 100644 --- a/pkg/daemon/server/service/pipeline_metrics_query_test.go +++ b/pkg/daemon/server/service/pipeline_metrics_query_test.go @@ -214,88 +214,3 @@ func TestListBuffers(t *testing.T) { assert.NoError(t, err) assert.Equal(t, len(resp.Buffers), 2) } - -// mock rater -//type mockRater_TestGetPipelineStatus struct { -// isActivelyProcessing bool -//} -// -//func (mr *mockRater_TestGetPipelineStatus) Start(ctx context.Context) error { -// return nil -//} -// -//func (mr *mockRater_TestGetPipelineStatus) GetRates(vertexName string, partitionName string) map[string]float64 { -// res := make(map[string]float64) -// if mr.isActivelyProcessing { -// res["default"] = 4.894736842105263 -// res["1m"] = 5.084745762711864 -// res["5m"] = 4.894736842105263 -// res["15m"] = 4.894736842105263 -// } else { -// res["default"] = 0 -// res["1m"] = 0 -// res["5m"] = 0 -// res["15m"] = 0 -// } -// return res -//} - -// -//func TestGetPipelineStatus(t *testing.T) { -// pipelineName := "simple-pipeline" -// pipeline := &v1alpha1.Pipeline{ -// ObjectMeta: metav1.ObjectMeta{ -// Name: pipelineName, -// }, -// Spec: v1alpha1.PipelineSpec{ -// Vertices: []v1alpha1.AbstractVertex{ -// {Name: "cat"}, -// }, -// }, -// } -// client, _ := isbsvc.NewISBJetStreamSvc(pipelineName) -// metricsResponse := `# HELP vertex_pending_messages Average pending messages in the last period of seconds. It is the pending messages of a vertex, not a pod. -//# TYPE vertex_pending_messages gauge -//vertex_pending_messages{period="15m",partition_name="-simple-pipeline-cat-0",pipeline="simple-pipeline",vertex="cat"} 4.011 -//vertex_pending_messages{period="1m",partition_name="-simple-pipeline-cat-0",pipeline="simple-pipeline",vertex="cat"} 5.333 -//vertex_pending_messages{period="5m",partition_name="-simple-pipeline-cat-0",pipeline="simple-pipeline",vertex="cat"} 6.002 -//vertex_pending_messages{period="default",partition_name="-simple-pipeline-cat-0",pipeline="simple-pipeline",vertex="cat"} 7.00002 -//` -// req := &daemon.GetPipelineStatusRequest{Pipeline: &pipelineName} -// -// // test when rater is actively processing -// activeRater := &mockRater_TestGetPipelineStatus{isActivelyProcessing: true} -// pipelineMetricsQueryService, err := NewPipelineMetadataQuery(client, pipeline, nil, activeRater) -// assert.NoError(t, err) -// ioReader := io.NopCloser(bytes.NewReader([]byte(metricsResponse))) -// pipelineMetricsQueryService.httpClient = &mockHttpClient{ -// MockGet: func(url string) (*http.Response, error) { -// return &http.Response{ -// StatusCode: 200, -// Body: ioReader, -// }, nil -// }, -// } -// resp, err := pipelineMetricsQueryService.GetPipelineStatus(context.Background(), req) -// assert.NoError(t, err) -// OKPipelineResponse := daemon.PipelineStatus{Status: pointer.String("OK"), Message: pointer.String("Pipeline has no issue.")} -// assert.Equal(t, &OKPipelineResponse, resp.Status) -// -// // test when rater is not actively processing -// idleRater := &mockRater_TestGetPipelineStatus{isActivelyProcessing: false} -// pipelineMetricsQueryService, err = NewPipelineMetadataQuery(client, pipeline, nil, idleRater) -// assert.NoError(t, err) -// ioReader = io.NopCloser(bytes.NewReader([]byte(metricsResponse))) -// pipelineMetricsQueryService.httpClient = &mockHttpClient{ -// MockGet: func(url string) (*http.Response, error) { -// return &http.Response{ -// StatusCode: 200, -// Body: ioReader, -// }, nil -// }, -// } -// resp, err = pipelineMetricsQueryService.GetPipelineStatus(context.Background(), req) -// assert.NoError(t, err) -// ErrorPipelineResponse := daemon.PipelineStatus{Status: pointer.String("Error"), Message: pointer.String("Pipeline has an error. Vertex cat is not processing pending messages.")} -// assert.Equal(t, &ErrorPipelineResponse, resp.Status) -//} From 58cfecc7f9b23ce08f29d2e8a99a623420f5273a Mon Sep 17 00:00:00 2001 From: Sidhant Kohli Date: Mon, 11 Dec 2023 10:50:52 -0800 Subject: [PATCH 08/12] cleanup Signed-off-by: Sidhant Kohli --- pkg/daemon/server/service/healthStatus.go | 2 -- server/apis/v1/health_test.go | 8 ++++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/pkg/daemon/server/service/healthStatus.go b/pkg/daemon/server/service/healthStatus.go index 630ea627fb..a8b32efb04 100644 --- a/pkg/daemon/server/service/healthStatus.go +++ b/pkg/daemon/server/service/healthStatus.go @@ -10,7 +10,6 @@ import ( "github.com/numaproj/numaflow/pkg/apis/numaflow/v1alpha1" "github.com/numaproj/numaflow/pkg/apis/proto/daemon" - "github.com/numaproj/numaflow/pkg/shared/logging" ) const ( @@ -98,7 +97,6 @@ var DefaultDataHealthResponse = NewDataHealthResponse(PipelineStatusOK, var ( currentPipelineStatus = DefaultDataHealthResponse pipeStatusLock = &sync.RWMutex{} - log = logging.FromContext(context.Background()) ) // GetCurrentPipelineHealth returns the current health status of the pipeline. diff --git a/server/apis/v1/health_test.go b/server/apis/v1/health_test.go index a2bdab718e..4aa5f7bf1e 100644 --- a/server/apis/v1/health_test.go +++ b/server/apis/v1/health_test.go @@ -6,9 +6,11 @@ import ( "github.com/stretchr/testify/assert" "golang.org/x/net/context" + appv1 "k8s.io/api/apps/v1" v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/client-go/kubernetes" + "k8s.io/client-go/kubernetes/scheme" "k8s.io/utils/pointer" dfv1 "github.com/numaproj/numaflow/pkg/apis/numaflow/v1alpha1" @@ -24,6 +26,12 @@ type Client struct { Clientset kubernetes.Interface } +func init() { + _ = dfv1.AddToScheme(scheme.Scheme) + _ = appv1.AddToScheme(scheme.Scheme) + //_ = corev1.AddToScheme(scheme.Scheme) +} + func (c Client) CreatePod(pod *v1.Pod) (*v1.Pod, error) { _, err := c.Clientset.CoreV1().Pods(pod.Namespace).Create(context.TODO(), pod, metav1.CreateOptions{}) if err != nil { From 4e66a64c63b253280b79c3874571532f5a6f0a9f Mon Sep 17 00:00:00 2001 From: Sidhant Kohli Date: Mon, 11 Dec 2023 11:01:18 -0800 Subject: [PATCH 09/12] comments Signed-off-by: Sidhant Kohli --- server/apis/v1/health.go | 13 ++++++++++--- server/apis/v1/response_health.go | 2 +- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/server/apis/v1/health.go b/server/apis/v1/health.go index 23142fc9d7..cfefbec932 100644 --- a/server/apis/v1/health.go +++ b/server/apis/v1/health.go @@ -27,10 +27,12 @@ type resourceHealthResponse struct { Code string `json:"code"` } +// HealthChecker is the struct to hold the resource status cache for the pipeline type HealthChecker struct { resourceStatusCache *evictCache.LRU[string, *resourceHealthResponse] } +// NewHealthChecker is used to create a new health checker func NewHealthChecker() *HealthChecker { c := evictCache.NewLRU[string, *resourceHealthResponse](500, nil, resourceCacheRefreshDuration) return &HealthChecker{ @@ -148,9 +150,14 @@ func checkVertexLevelHealth(ctx context.Context, h *handler, ns string, }, nil } -// isVertexHealthy is used to check if the number of replicas running in the vertex -// are equal to the number of desired replicas and the pods are in running state. -// We first check if the vertex is in running state, if not, return the error message from the status +// isVertexHealthy is used to check if the vertex is healthy or not +// It checks for the following: +// 1) If the vertex is in running state +// 2) the number of replicas running in the vertex +// are equal to the number of desired replicas and the pods are in running state +// 3) If all the containers in the pod are in running state +// if any of the above conditions are not met, the vertex is unhealthy +// Based on the above conditions, it returns the status code and message func isVertexHealthy(h *handler, ns string, pipeline string, vertex *dfv1.Vertex, vertexName string) (bool, *resourceHealthResponse, error) { // check if the vertex is in running state diff --git a/server/apis/v1/response_health.go b/server/apis/v1/response_health.go index 6b1dd2d3a3..a8db4c1203 100644 --- a/server/apis/v1/response_health.go +++ b/server/apis/v1/response_health.go @@ -18,7 +18,7 @@ package v1 // HealthResponse is the response payload for health API. // It contains the health status of the vertex and data. -// We include the Status, Message and Code for both vertex and data. +// We include the Status, Message and Code for both resources and data. type HealthResponse struct { ResourceHealthStatus string `json:"resourceHealthStatus"` DataHealthStatus string `json:"dataHealthStatus"` From bf1293a4eadde441874e1f8ecacd063039639c72 Mon Sep 17 00:00:00 2001 From: Sidhant Kohli Date: Tue, 12 Dec 2023 16:46:44 -0800 Subject: [PATCH 10/12] refactor Signed-off-by: Sidhant Kohli --- go.mod | 1 - go.sum | 2 - pkg/daemon/server/daemon_server.go | 21 ++-- pkg/daemon/server/service/healthStatus.go | 88 +++++---------- .../server/service/pipeline_metrics_query.go | 101 ++++++++++-------- .../service/pipeline_watermark_query.go | 2 +- pkg/shared/ewma/interface.go | 12 +++ pkg/shared/ewma/simple_ewma.go | 56 ++++++++++ pkg/shared/ewma/simple_ewma_test.go | 39 +++++++ 9 files changed, 202 insertions(+), 120 deletions(-) create mode 100644 pkg/shared/ewma/interface.go create mode 100644 pkg/shared/ewma/simple_ewma.go create mode 100644 pkg/shared/ewma/simple_ewma_test.go diff --git a/go.mod b/go.mod index 7761783fc3..47479a0496 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.20 require ( github.com/IBM/sarama v1.40.1 github.com/Masterminds/sprig/v3 v3.2.2 - github.com/VividCortex/ewma v1.2.0 github.com/ahmetb/gen-crd-api-reference-docs v0.3.0 github.com/antonmedv/expr v1.9.0 github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de diff --git a/go.sum b/go.sum index 7853d1c34d..0b50c148ef 100644 --- a/go.sum +++ b/go.sum @@ -70,8 +70,6 @@ github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdko github.com/Shopify/toxiproxy/v2 v2.5.0 h1:i4LPT+qrSlKNtQf5QliVjdP08GyAH8+BUIc9gT0eahc= github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkHBuFDA6DUhhse0IGJ7T5bemHyNILUjvOq4= github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w= -github.com/VividCortex/ewma v1.2.0 h1:f58SaIzcDXrSy3kWaHNvuJgJ3Nmz59Zji6XoJR/q1ow= -github.com/VividCortex/ewma v1.2.0/go.mod h1:nz4BbCtbLyFDeC9SUHbtcT5644juEuWfUAUnGx7j5l4= github.com/agnivade/levenshtein v1.0.1/go.mod h1:CURSv5d9Uaml+FovSIICkLbAUZ9S4RqaHDIsdSBg7lM= github.com/ahmetb/gen-crd-api-reference-docs v0.3.0 h1:+XfOU14S4bGuwyvCijJwhhBIjYN+YXS18jrCY2EzJaY= github.com/ahmetb/gen-crd-api-reference-docs v0.3.0/go.mod h1:TdjdkYhlOifCQWPs1UdTma97kQQMozf5h26hTuG70u8= diff --git a/pkg/daemon/server/daemon_server.go b/pkg/daemon/server/daemon_server.go index 61ba266dba..a958c36d45 100644 --- a/pkg/daemon/server/daemon_server.go +++ b/pkg/daemon/server/daemon_server.go @@ -48,14 +48,16 @@ import ( ) type daemonServer struct { - pipeline *v1alpha1.Pipeline - isbSvcType v1alpha1.ISBSvcType + pipeline *v1alpha1.Pipeline + isbSvcType v1alpha1.ISBSvcType + metaDataQuery *service.PipelineMetadataQuery } func NewDaemonServer(pl *v1alpha1.Pipeline, isbSvcType v1alpha1.ISBSvcType) *daemonServer { return &daemonServer{ - pipeline: pl, - isbSvcType: isbSvcType, + pipeline: pl, + isbSvcType: isbSvcType, + metaDataQuery: nil, } } @@ -138,15 +140,9 @@ func (ds *daemonServer) Run(ctx context.Context) error { go func() { _ = httpServer.Serve(httpL) }() go func() { _ = tcpm.Serve() }() - pipelineMetadataQuery, err := service.NewPipelineMetadataQuery(isbSvcClient, ds.pipeline, wmFetchers, rater) - if err != nil { - log.Errorw("Failed to create pipeline metadata query", zap.Error(err)) - } - healthChecker := service.NewHealthChecker(ds.pipeline, pipelineMetadataQuery) - - // Start the Data flow status updater + // Start the Data flow health status updater go func() { - healthChecker.StartHealthCheck() + ds.metaDataQuery.StartHealthCheck(ctx) }() log.Infof("Daemon server started successfully on %s", address) @@ -181,6 +177,7 @@ func (ds *daemonServer) newGRPCServer( return nil, err } daemon.RegisterDaemonServiceServer(grpcServer, pipelineMetadataQuery) + ds.metaDataQuery = pipelineMetadataQuery return grpcServer, nil } diff --git a/pkg/daemon/server/service/healthStatus.go b/pkg/daemon/server/service/healthStatus.go index a8b32efb04..48222b4f81 100644 --- a/pkg/daemon/server/service/healthStatus.go +++ b/pkg/daemon/server/service/healthStatus.go @@ -5,11 +5,13 @@ import ( "sync" "time" - "github.com/VividCortex/ewma" "golang.org/x/net/context" "github.com/numaproj/numaflow/pkg/apis/numaflow/v1alpha1" "github.com/numaproj/numaflow/pkg/apis/proto/daemon" + "github.com/numaproj/numaflow/pkg/isbsvc" + "github.com/numaproj/numaflow/pkg/shared/ewma" + sharedqueue "github.com/numaproj/numaflow/pkg/shared/queue" ) const ( @@ -94,50 +96,25 @@ var DefaultDataHealthResponse = NewDataHealthResponse(PipelineStatusOK, "Pipeline dataflow is healthy", "D1") -var ( - currentPipelineStatus = DefaultDataHealthResponse - pipeStatusLock = &sync.RWMutex{} -) - -// GetCurrentPipelineHealth returns the current health status of the pipeline. -// It is thread safe to ensure concurrent access. -func GetCurrentPipelineHealth() *DataHealthResponse { - // Lock the statusLock to ensure thread safety. - pipeStatusLock.RLock() - defer pipeStatusLock.RUnlock() - // Return the health status. - return currentPipelineStatus -} - -// SetCurrentPipelineHealth sets the current health status of the pipeline. -// It is thread safe to ensure concurrent access. -func SetCurrentPipelineHealth(status *DataHealthResponse) { - // Lock the statusLock to ensure thread safety. - pipeStatusLock.Lock() - defer pipeStatusLock.Unlock() - // Set the health status. - currentPipelineStatus = status -} - // HealthChecker is the struct type for health checker. type HealthChecker struct { // Add a field for the health status. - currentDataStatus *DataHealthResponse - pipelineMetadataQuery *pipelineMetadataQuery - pipeline *v1alpha1.Pipeline - timelineData map[string][]*TimelineEntry - statusLock *sync.RWMutex + currentDataStatus *DataHealthResponse + isbSvcClient isbsvc.ISBService + pipeline *v1alpha1.Pipeline + timelineData map[string]*sharedqueue.OverflowQueue[*TimelineEntry] + statusLock *sync.RWMutex } // NewHealthChecker creates a new object HealthChecker struct type. -func NewHealthChecker(pipeline *v1alpha1.Pipeline, pipelineMetadataQuery *pipelineMetadataQuery) *HealthChecker { +func NewHealthChecker(pipeline *v1alpha1.Pipeline, isbSvcClient isbsvc.ISBService) *HealthChecker { // Return a new HealthChecker struct instance. return &HealthChecker{ - currentDataStatus: DefaultDataHealthResponse, - pipelineMetadataQuery: pipelineMetadataQuery, - pipeline: pipeline, - timelineData: make(map[string][]*TimelineEntry), - statusLock: &sync.RWMutex{}, + currentDataStatus: DefaultDataHealthResponse, + isbSvcClient: isbSvcClient, + pipeline: pipeline, + timelineData: make(map[string]*sharedqueue.OverflowQueue[*TimelineEntry]), + statusLock: &sync.RWMutex{}, } } @@ -175,16 +152,14 @@ func (hc *HealthChecker) SetCurrentHealth(status *DataHealthResponse) { defer hc.statusLock.Unlock() // Set the health status. hc.currentDataStatus = status - SetCurrentPipelineHealth(status) } // StartHealthCheck starts the health check for the pipeline. // The ticks are generated at the interval of HealthTimeStep. -func (hc *HealthChecker) StartHealthCheck() { - ctx := context.Background() +func (hc *HealthChecker) StartHealthCheck(ctx context.Context) { // Goroutine to listen for ticks // At every tick, check and update the health status of the pipeline. - go func() { + go func(ctx context.Context) { // Create a ticker with the interval of HealthCheckInterval. ticker := time.NewTicker(HealthTimeStep) defer ticker.Stop() @@ -207,7 +182,7 @@ func (hc *HealthChecker) StartHealthCheck() { return } } - }() + }(ctx) } // getPipelineDataCriticality is used to provide the data criticality of the pipeline @@ -227,13 +202,8 @@ func (hc *HealthChecker) StartHealthCheck() { // If this average is less than the warning threshold, we return the ok status func (hc *HealthChecker) getPipelineDataCriticality() ([]*VertexState, error) { ctx := context.Background() - pipelineName := hc.pipeline.GetName() - - // Create a new buffer request object - req := &daemon.ListBuffersRequest{Pipeline: &pipelineName} - // Fetch the buffer information for the pipeline - buffers, err := hc.pipelineMetadataQuery.ListBuffers(ctx, req) + buffers, err := listBuffers(ctx, hc.pipeline, hc.isbSvcClient) if err != nil { return nil, err } @@ -244,10 +214,10 @@ func (hc *HealthChecker) getPipelineDataCriticality() ([]*VertexState, error) { // iterate over the timeline data for each buffer and calculate the exponential weighted mean average // for the last HEALTH_WINDOW_SIZE buffer usage entries - for bufferName, timeline := range hc.timelineData { + for bufferName := range hc.timelineData { // Extract the buffer usage of the timeline var bufferUsage []float64 - for _, entry := range timeline { + for _, entry := range hc.timelineData[bufferName].Items() { bufferUsage = append(bufferUsage, entry.BufferUsage) } // calculate the average buffer usage over the last HEALTH_WINDOW_SIZE seconds @@ -284,24 +254,17 @@ func (hc *HealthChecker) updateUsageTimeline(bufferList []*daemon.BufferInfo) { // if the buffer name is not present in the timeline data, add it if _, ok := hc.timelineData[bufferName]; !ok { - hc.timelineData[bufferName] = make([]*TimelineEntry, 0) + hc.timelineData[bufferName] = sharedqueue.New[*TimelineEntry](int(HealthWindowSize)) } // extract the current buffer usage and update the average buffer usage bufferUsage := buffer.GetBufferUsage() * 100 - newAverage := updateAverageBufferUsage(hc.timelineData[bufferName], bufferUsage) - + newAverage := updateAverageBufferUsage(hc.timelineData[bufferName].Items(), bufferUsage) // add the new entry to the timeline - hc.timelineData[bufferName] = append(hc.timelineData[bufferName], &TimelineEntry{ + hc.timelineData[bufferName].Append(&TimelineEntry{ Time: timestamp, BufferUsage: bufferUsage, AverageBufferUsage: newAverage, }) - - // remove first entry if the size of the timeline is greater than HEALTH_WINDOW_SIZE - if len(hc.timelineData[bufferName]) > int(HealthWindowSize) { - hc.timelineData[bufferName] = hc.timelineData[bufferName][1:] - } - } } @@ -345,11 +308,12 @@ func updateAverageBufferUsage(timeline []*TimelineEntry, newEntry float64) float // where lastEWMA is the EWMA buffer usage of the last entry in the timeline func calculateEWMAUsage(bufferUsage []float64) []float64 { // Compute the current EWMA buffer usage of the timeline - a := ewma.NewMovingAverage() + a := ewma.NewSimpleEWMA(float64(HealthWindowSize)) var emwaValues []float64 + // TODO: Check if we can keep storing the EWMA values instead of recomputing them for _, f := range bufferUsage { a.Add(f) - emwaValues = append(emwaValues, a.Value()) + emwaValues = append(emwaValues, a.Get()) } return emwaValues } diff --git a/pkg/daemon/server/service/pipeline_metrics_query.go b/pkg/daemon/server/service/pipeline_metrics_query.go index f4669e3d44..c371b281b2 100644 --- a/pkg/daemon/server/service/pipeline_metrics_query.go +++ b/pkg/daemon/server/service/pipeline_metrics_query.go @@ -44,13 +44,14 @@ type metricsHttpClient interface { Get(url string) (*http.Response, error) } -// pipelineMetadataQuery has the metadata required for the pipeline queries -type pipelineMetadataQuery struct { +// PipelineMetadataQuery has the metadata required for the pipeline queries +type PipelineMetadataQuery struct { isbSvcClient isbsvc.ISBService pipeline *v1alpha1.Pipeline httpClient metricsHttpClient watermarkFetchers map[v1alpha1.Edge][]fetch.HeadFetcher rater rater.Ratable + healthChecker *HealthChecker } const ( @@ -68,9 +69,9 @@ func NewPipelineMetadataQuery( isbSvcClient isbsvc.ISBService, pipeline *v1alpha1.Pipeline, wmFetchers map[v1alpha1.Edge][]fetch.HeadFetcher, - rater rater.Ratable) (*pipelineMetadataQuery, error) { + rater rater.Ratable) (*PipelineMetadataQuery, error) { var err error - ps := pipelineMetadataQuery{ + ps := PipelineMetadataQuery{ isbSvcClient: isbSvcClient, pipeline: pipeline, httpClient: &http.Client{ @@ -81,6 +82,7 @@ func NewPipelineMetadataQuery( }, watermarkFetchers: wmFetchers, rater: rater, + healthChecker: NewHealthChecker(pipeline, isbSvcClient), } if err != nil { return nil, err @@ -89,45 +91,16 @@ func NewPipelineMetadataQuery( } // ListBuffers is used to obtain the all the edge buffers information of a pipeline -func (ps *pipelineMetadataQuery) ListBuffers(ctx context.Context, req *daemon.ListBuffersRequest) (*daemon.ListBuffersResponse, error) { - log := logging.FromContext(ctx) - resp := new(daemon.ListBuffersResponse) - - buffers := []*daemon.BufferInfo{} - for _, buffer := range ps.pipeline.GetAllBuffers() { - bufferInfo, err := ps.isbSvcClient.GetBufferInfo(ctx, buffer) - if err != nil { - return nil, fmt.Errorf("failed to get information of buffer %q", buffer) - } - log.Debugf("Buffer %s has bufferInfo %+v", buffer, bufferInfo) - v := ps.pipeline.FindVertexWithBuffer(buffer) - if v == nil { - return nil, fmt.Errorf("unexpected error, buffer %q not found from the pipeline", buffer) - } - bufferLength, bufferUsageLimit := getBufferLimits(ps.pipeline, *v) - usage := float64(bufferInfo.TotalMessages) / float64(bufferLength) - if x := (float64(bufferInfo.PendingCount) + float64(bufferInfo.AckPendingCount)) / float64(bufferLength); x < usage { - usage = x - } - b := &daemon.BufferInfo{ - Pipeline: &ps.pipeline.Name, - BufferName: pointer.String(fmt.Sprintf("%v", buffer)), - PendingCount: &bufferInfo.PendingCount, - AckPendingCount: &bufferInfo.AckPendingCount, - TotalMessages: &bufferInfo.TotalMessages, - BufferLength: &bufferLength, - BufferUsageLimit: &bufferUsageLimit, - BufferUsage: &usage, - IsFull: pointer.Bool(usage >= bufferUsageLimit), - } - buffers = append(buffers, b) +func (ps *PipelineMetadataQuery) ListBuffers(ctx context.Context, req *daemon.ListBuffersRequest) (*daemon.ListBuffersResponse, error) { + resp, err := listBuffers(ctx, ps.pipeline, ps.isbSvcClient) + if err != nil { + return nil, err } - resp.Buffers = buffers return resp, nil } // GetBuffer is used to obtain one buffer information of a pipeline -func (ps *pipelineMetadataQuery) GetBuffer(ctx context.Context, req *daemon.GetBufferRequest) (*daemon.GetBufferResponse, error) { +func (ps *PipelineMetadataQuery) GetBuffer(ctx context.Context, req *daemon.GetBufferRequest) (*daemon.GetBufferResponse, error) { bufferInfo, err := ps.isbSvcClient.GetBufferInfo(ctx, *req.Buffer) if err != nil { return nil, fmt.Errorf("failed to get information of buffer %q:%v", *req.Buffer, err) @@ -161,7 +134,7 @@ func (ps *pipelineMetadataQuery) GetBuffer(ctx context.Context, req *daemon.GetB // Response contains the metrics for each partition of the vertex. // In the future maybe latency will also be added here? // Should this method live here or maybe another file? -func (ps *pipelineMetadataQuery) GetVertexMetrics(ctx context.Context, req *daemon.GetVertexMetricsRequest) (*daemon.GetVertexMetricsResponse, error) { +func (ps *PipelineMetadataQuery) GetVertexMetrics(ctx context.Context, req *daemon.GetVertexMetricsRequest) (*daemon.GetVertexMetricsResponse, error) { resp := new(daemon.GetVertexMetricsResponse) abstractVertex := ps.pipeline.GetVertex(req.GetVertex()) @@ -190,7 +163,7 @@ func (ps *pipelineMetadataQuery) GetVertexMetrics(ctx context.Context, req *daem } // getPending returns the pending count for each partition of the vertex -func (ps *pipelineMetadataQuery) getPending(ctx context.Context, req *daemon.GetVertexMetricsRequest) map[string]map[string]int64 { +func (ps *PipelineMetadataQuery) getPending(ctx context.Context, req *daemon.GetVertexMetricsRequest) map[string]map[string]int64 { vertexName := fmt.Sprintf("%s-%s", ps.pipeline.Name, req.GetVertex()) log := logging.FromContext(ctx) @@ -251,8 +224,8 @@ func (ps *pipelineMetadataQuery) getPending(ctx context.Context, req *daemon.Get return totalPendingMap } -func (ps *pipelineMetadataQuery) GetPipelineStatus(ctx context.Context, req *daemon.GetPipelineStatusRequest) (*daemon.GetPipelineStatusResponse, error) { - status := GetCurrentPipelineHealth() +func (ps *PipelineMetadataQuery) GetPipelineStatus(ctx context.Context, req *daemon.GetPipelineStatusRequest) (*daemon.GetPipelineStatusResponse, error) { + status := ps.healthChecker.GetCurrentHealth() resp := new(daemon.GetPipelineStatusResponse) resp.Status = &daemon.PipelineStatus{ Status: pointer.String(status.Status), @@ -276,3 +249,47 @@ func getBufferLimits(pl *v1alpha1.Pipeline, v v1alpha1.AbstractVertex) (bufferLe } return bufferLength, bufferUsageLimit } + +// listBuffers returns the list of ISB buffers for the pipeline and their information +// We use the isbSvcClient to get the buffer information +func listBuffers(ctx context.Context, pipeline *v1alpha1.Pipeline, isbSvcClient isbsvc.ISBService) (*daemon.ListBuffersResponse, error) { + log := logging.FromContext(ctx) + resp := new(daemon.ListBuffersResponse) + + buffers := []*daemon.BufferInfo{} + for _, buffer := range pipeline.GetAllBuffers() { + bufferInfo, err := isbSvcClient.GetBufferInfo(ctx, buffer) + if err != nil { + return nil, fmt.Errorf("failed to get information of buffer %q", buffer) + } + log.Debugf("Buffer %s has bufferInfo %+v", buffer, bufferInfo) + v := pipeline.FindVertexWithBuffer(buffer) + if v == nil { + return nil, fmt.Errorf("unexpected error, buffer %q not found from the pipeline", buffer) + } + bufferLength, bufferUsageLimit := getBufferLimits(pipeline, *v) + usage := float64(bufferInfo.TotalMessages) / float64(bufferLength) + if x := (float64(bufferInfo.PendingCount) + float64(bufferInfo.AckPendingCount)) / float64(bufferLength); x < usage { + usage = x + } + b := &daemon.BufferInfo{ + Pipeline: &pipeline.Name, + BufferName: pointer.String(fmt.Sprintf("%v", buffer)), + PendingCount: &bufferInfo.PendingCount, + AckPendingCount: &bufferInfo.AckPendingCount, + TotalMessages: &bufferInfo.TotalMessages, + BufferLength: &bufferLength, + BufferUsageLimit: &bufferUsageLimit, + BufferUsage: &usage, + IsFull: pointer.Bool(usage >= bufferUsageLimit), + } + buffers = append(buffers, b) + } + resp.Buffers = buffers + return resp, nil +} + +// StartHealthCheck starts the health check for the pipeline using the health checker +func (ps *PipelineMetadataQuery) StartHealthCheck(ctx context.Context) { + ps.healthChecker.StartHealthCheck(ctx) +} diff --git a/pkg/daemon/server/service/pipeline_watermark_query.go b/pkg/daemon/server/service/pipeline_watermark_query.go index c86ce8df9e..bb1ff4c2f2 100644 --- a/pkg/daemon/server/service/pipeline_watermark_query.go +++ b/pkg/daemon/server/service/pipeline_watermark_query.go @@ -71,7 +71,7 @@ func BuildWatermarkStores(ctx context.Context, pipeline *v1alpha1.Pipeline, isbs } // GetPipelineWatermarks is used to return the head watermarks for a given pipeline. -func (ps *pipelineMetadataQuery) GetPipelineWatermarks(ctx context.Context, request *daemon.GetPipelineWatermarksRequest) (*daemon.GetPipelineWatermarksResponse, error) { +func (ps *PipelineMetadataQuery) GetPipelineWatermarks(ctx context.Context, request *daemon.GetPipelineWatermarksRequest) (*daemon.GetPipelineWatermarksResponse, error) { resp := new(daemon.GetPipelineWatermarksResponse) isWatermarkEnabled := !ps.pipeline.Spec.Watermark.Disabled diff --git a/pkg/shared/ewma/interface.go b/pkg/shared/ewma/interface.go new file mode 100644 index 0000000000..79564863ef --- /dev/null +++ b/pkg/shared/ewma/interface.go @@ -0,0 +1,12 @@ +package ewma + +type EWMA interface { + // Add adds a new value to the EWMA + Add(float64) + // Get returns the current value of the EWMA + Get() float64 + // Reset resets the EWMA to the initial value + Reset() + // Set sets the EWMA to the given value + Set(float64) +} diff --git a/pkg/shared/ewma/simple_ewma.go b/pkg/shared/ewma/simple_ewma.go new file mode 100644 index 0000000000..328b7f0f1e --- /dev/null +++ b/pkg/shared/ewma/simple_ewma.go @@ -0,0 +1,56 @@ +package ewma + +const ( + averageAge = 30.0 + // constDecayFactor is the default decay factor + constDecayFactor = 2.0 / (averageAge + 1.0) +) + +// SimpleEWMA is a simple implementation of EWMA +type SimpleEWMA struct { + // alpha is the smoothing factor + alpha float64 + // value is the current value of the EWMA + value float64 + // init is a flag to indicate if the EWMA has been initialized + init bool +} + +// NewSimpleEWMA returns a new SimpleEWMA +// If the alpha is not provided we use a default value of constDecayFactor +// If the alpha is provided we calulate the smoothing factor from it +func NewSimpleEWMA(alpha ...float64) *SimpleEWMA { + if len(alpha) > 0 { + decay := 2.0 / (alpha[0] + 1.0) + return &SimpleEWMA{alpha: decay} + } + return &SimpleEWMA{alpha: constDecayFactor} +} + +// Add adds a new value to the EWMA +func (s *SimpleEWMA) Add(value float64) { + // If the EWMA has not been initialized, set the value and return + if !s.init { + s.value = value + s.init = true + return + } + // Otherwise, calculate the EWMA + s.value = s.value + s.alpha*(value-s.value) +} + +// Get returns the current value of the EWMA +func (s *SimpleEWMA) Get() float64 { + return s.value +} + +// Reset resets the EWMA to the initial value +func (s *SimpleEWMA) Reset() { + s.value = 0 + s.init = false +} + +// Set sets the EWMA to the given value +func (s *SimpleEWMA) Set(value float64) { + s.value = value +} diff --git a/pkg/shared/ewma/simple_ewma_test.go b/pkg/shared/ewma/simple_ewma_test.go new file mode 100644 index 0000000000..392cda4660 --- /dev/null +++ b/pkg/shared/ewma/simple_ewma_test.go @@ -0,0 +1,39 @@ +package ewma + +import ( + "math" + "testing" + + "github.com/stretchr/testify/assert" +) + +const ( + defaultEWMA = 77.14075212282631 + span15EWMA = 74.9112723022807 +) + +var samples = [14]float64{ + 83.92333333333333, 0, 83.24000000000001, 88.24, 77.61, 76.57333333333334, 79.91333333333334, 80.34, + 74.90666666666667, 69.90666666666667, 71.65, 73.19333333333333, 72.18666666666667, 74.90666666666667, +} + +// TestSimpleEWMA tests the SimpleEWMA implementation. +func TestSimpleEWMA(t *testing.T) { + // Create a new EWMA with the default decay factor. + newEwma := NewSimpleEWMA() + for _, f := range samples { + newEwma.Add(f) + } + // Check if the value is within the margin of error. + assert.True(t, math.Abs(defaultEWMA-newEwma.Get()) < 0.00000001) + + // Create a new EWMA with a custom decay factor. + newEwma = NewSimpleEWMA(15) + for _, f := range samples { + newEwma.Add(f) + } + // Check if the value is within the margin of error. + assert.True(t, math.Abs(span15EWMA-newEwma.Get()) < 0.00000001) +} + +// TestSimpleEWMAInit tests the SimpleEWMA initialization. From b48852c76972755f6cdc67809438a866b1b4367f Mon Sep 17 00:00:00 2001 From: Sidhant Kohli Date: Fri, 15 Dec 2023 11:21:15 -0800 Subject: [PATCH 11/12] refactor Signed-off-by: Sidhant Kohli --- pkg/daemon/server/service/healthStatus.go | 220 +++++++++--------- .../server/service/pipeline_metrics_query.go | 4 +- pkg/shared/ewma/interface.go | 2 + pkg/shared/ewma/simple_ewma.go | 4 +- server/apis/v1/health.go | 3 +- 5 files changed, 120 insertions(+), 113 deletions(-) diff --git a/pkg/daemon/server/service/healthStatus.go b/pkg/daemon/server/service/healthStatus.go index 48222b4f81..47b1e1133b 100644 --- a/pkg/daemon/server/service/healthStatus.go +++ b/pkg/daemon/server/service/healthStatus.go @@ -5,65 +5,68 @@ import ( "sync" "time" + "go.uber.org/zap" "golang.org/x/net/context" "github.com/numaproj/numaflow/pkg/apis/numaflow/v1alpha1" "github.com/numaproj/numaflow/pkg/apis/proto/daemon" "github.com/numaproj/numaflow/pkg/isbsvc" "github.com/numaproj/numaflow/pkg/shared/ewma" + "github.com/numaproj/numaflow/pkg/shared/logging" sharedqueue "github.com/numaproj/numaflow/pkg/shared/queue" ) const ( - // HealthSlidingWindow is the total time window to compute the health of a vertex - HealthSlidingWindow = 5 * time.Minute + // healthSlidingWindow is the total time window to compute the health of a vertex + healthSlidingWindow = 5 * time.Minute - // HealthTimeStep is the frequency at which the health of a vertex is computed - HealthTimeStep = 10 * time.Second + // healthTimeStep is the frequency at which the health of a vertex is computed + healthTimeStep = 10 * time.Second - // HealthWindowSize is the number of timeline entries to keep for a given vertex + // healthWindowSize is the number of timeline entries to keep for a given vertex // This is used to compute the health of a vertex - // Ensure that HealthSlidingWindow / HealthTimeStep is an integer - HealthWindowSize = HealthSlidingWindow / HealthTimeStep + // Ensure that healthSlidingWindow / healthTimeStep is an integer + healthWindowSize = healthSlidingWindow / healthTimeStep - // CriticalWindowTime is the number of entries to look back to assign a critical state to a vertex - CriticalWindowTime = 1 * time.Minute + // criticalWindowTime is the number of entries to look back to assign a critical state to a vertex + // Critical state is assigned to a vertex if the weighted mean average buffer usage of + // the vertex for a given time period is above a defined criticalBufferThreshold + criticalWindowTime = 1 * time.Minute - // CriticalWindowSize is the number of entries to look back to assign a critical state to a vertex + // criticalWindowSize is the number of entries to look back to assign a critical state to a vertex // This is used to avoid false positives - CriticalWindowSize = int(CriticalWindowTime / HealthTimeStep) + criticalWindowSize = int(criticalWindowTime / healthTimeStep) - // CriticalLookBackCount is the number of times the state must be critical in the last CriticalWindowSize entries + // criticalLookBackCount is the number of times the state must be critical in the last criticalWindowSize entries // This is used to avoid false positives - CriticalLookBackCount = 3 + criticalLookBackCount = 3 ) // HealthThresholds are the thresholds used to compute the health of a vertex const ( - // CriticalThreshold is the threshold above which the health of a vertex is critical - CriticalThreshold = 95 - // WarningThreshold is the threshold above which the health of a vertex is warning - WarningThreshold = 80 + // criticalBufferThreshold is the threshold above which the health of a vertex is critical + criticalBufferThreshold = 95 + // warningBufferThreshold is the threshold above which the health of a vertex is warning + warningBufferThreshold = 80 ) // Dataflow states const ( - // CriticalState is the state of a vertex when its health is critical - CriticalState = "critical" - // WarningState is the state of a vertex when its health is warning - WarningState = "warning" - // HealthyState is the state of a vertex when its health is healthy - HealthyState = "healthy" + // criticalState is the state of a vertex when its health is critical + criticalState = "critical" + // warningState is the state of a vertex when its health is warning + warningState = "warning" + // healthyState is the state of a vertex when its health is healthy + healthyState = "healthy" ) const ( - // EnableCriticalLookBack is used to enable the look back for critical state - EnableCriticalLookBack = true - DisableCriticalLookBack = false + // enableCriticalLookBack is used to enable the look back for critical state + enableCriticalLookBack = true ) -// TimelineEntry is used to store the buffer usage timeline for a given vertex -type TimelineEntry struct { +// timelineEntry is used to store the buffer usage timeline for a given vertex +type timelineEntry struct { // The time at which the entry is recorded Time int64 `json:"time"` // The buffer usage of the pipeline at the time @@ -72,8 +75,8 @@ type TimelineEntry struct { AverageBufferUsage float64 `json:"averageBufferUsage"` } -// DataHealthResponse is the response returned by the data health check API -type DataHealthResponse struct { +// dataHealthResponse is the response returned by the data health check API +type dataHealthResponse struct { // Status is the overall data status of the pipeline Status string `json:"status"` // Message is the error message if any @@ -82,27 +85,27 @@ type DataHealthResponse struct { Code string `json:"code"` } -// NewDataHealthResponse is used to create a new DataHealthResponse object -func NewDataHealthResponse(status string, message string, code string) *DataHealthResponse { - return &DataHealthResponse{ +// newDataHealthResponse is used to create a new dataHealthResponse object +func newDataHealthResponse(status string, message string, code string) *dataHealthResponse { + return &dataHealthResponse{ Status: status, Message: message, Code: code, } } -// DefaultDataHealthResponse is the default response returned by the data health check API -var DefaultDataHealthResponse = NewDataHealthResponse(PipelineStatusOK, - "Pipeline dataflow is healthy", - "D1") +// defaultDataHealthResponse is the default response returned by the data health check API +var defaultDataHealthResponse = newDataHealthResponse(PipelineStatusUnknown, + "Pipeline dataflow is in an unknown state", + "D4") // HealthChecker is the struct type for health checker. type HealthChecker struct { // Add a field for the health status. - currentDataStatus *DataHealthResponse + currentDataStatus *dataHealthResponse isbSvcClient isbsvc.ISBService pipeline *v1alpha1.Pipeline - timelineData map[string]*sharedqueue.OverflowQueue[*TimelineEntry] + timelineData map[string]*sharedqueue.OverflowQueue[*timelineEntry] statusLock *sync.RWMutex } @@ -110,33 +113,33 @@ type HealthChecker struct { func NewHealthChecker(pipeline *v1alpha1.Pipeline, isbSvcClient isbsvc.ISBService) *HealthChecker { // Return a new HealthChecker struct instance. return &HealthChecker{ - currentDataStatus: DefaultDataHealthResponse, + currentDataStatus: defaultDataHealthResponse, isbSvcClient: isbSvcClient, pipeline: pipeline, - timelineData: make(map[string]*sharedqueue.OverflowQueue[*TimelineEntry]), + timelineData: make(map[string]*sharedqueue.OverflowQueue[*timelineEntry]), statusLock: &sync.RWMutex{}, } } -// VertexState is a struct which contains the name and state of a vertex -type VertexState struct { +// vertexState is a struct which contains the name and state of a vertex +type vertexState struct { // Name is the name of the vertex Name string `json:"name"` // State is the state of the vertex State string `json:"state"` } -// NewVertexState is used to create a new VertexState object -func NewVertexState(name string, state string) *VertexState { - return &VertexState{ +// newVertexState is used to create a new vertexState object +func newVertexState(name string, state string) *vertexState { + return &vertexState{ Name: name, State: state, } } -// GetCurrentHealth returns the current health status of the pipeline. +// getCurrentHealth returns the current health status of the pipeline. // It is thread safe to ensure concurrent access. -func (hc *HealthChecker) GetCurrentHealth() *DataHealthResponse { +func (hc *HealthChecker) getCurrentHealth() *dataHealthResponse { // Lock the statusLock to ensure thread safety. hc.statusLock.RLock() defer hc.statusLock.RUnlock() @@ -144,9 +147,9 @@ func (hc *HealthChecker) GetCurrentHealth() *DataHealthResponse { return hc.currentDataStatus } -// SetCurrentHealth sets the current health status of the pipeline. +// setCurrentHealth sets the current health status of the pipeline. // It is thread safe to ensure concurrent access. -func (hc *HealthChecker) SetCurrentHealth(status *DataHealthResponse) { +func (hc *HealthChecker) setCurrentHealth(status *dataHealthResponse) { // Lock the statusLock to ensure thread safety. hc.statusLock.Lock() defer hc.statusLock.Unlock() @@ -154,38 +157,42 @@ func (hc *HealthChecker) SetCurrentHealth(status *DataHealthResponse) { hc.currentDataStatus = status } -// StartHealthCheck starts the health check for the pipeline. -// The ticks are generated at the interval of HealthTimeStep. -func (hc *HealthChecker) StartHealthCheck(ctx context.Context) { +// startHealthCheck starts the health check for the pipeline. +// The ticks are generated at the interval of healthTimeStep. +func (hc *HealthChecker) startHealthCheck(ctx context.Context) { + logger := logging.FromContext(ctx) // Goroutine to listen for ticks // At every tick, check and update the health status of the pipeline. - go func(ctx context.Context) { - // Create a ticker with the interval of HealthCheckInterval. - ticker := time.NewTicker(HealthTimeStep) - defer ticker.Stop() - for { - select { - // If the ticker ticks, check and update the health status of the pipeline. - case <-ticker.C: - // Get the current health status of the pipeline. - criticality, err := hc.getPipelineDataCriticality() - if err != nil { - return - } + // If the context is done, return. + // Create a ticker to generate ticks at the interval of healthTimeStep. + ticker := time.NewTicker(healthTimeStep) + defer ticker.Stop() + for { + select { + // If the ticker ticks, check and update the health status of the pipeline. + case <-ticker.C: + // Get the current health status of the pipeline. + criticality, err := hc.getPipelineVertexDataCriticality(ctx) + logger.Debugw("Health check", zap.Any("criticality", criticality)) + if err != nil { + // If there is an error, set the current health status to unknown. + // as we are not able to determine the health of the pipeline. + logger.Errorw("Failed to vertex data criticality", zap.Error(err)) + hc.setCurrentHealth(defaultDataHealthResponse) + } else { // convert the vertex state to pipeline state pipelineState := convertVertexStateToPipelineState(criticality) // update the current health status of the pipeline - hc.SetCurrentHealth(pipelineState) - - // If the context is done, return. - case <-ctx.Done(): - return + hc.setCurrentHealth(pipelineState) } + // If the context is done, return. + case <-ctx.Done(): + return } - }(ctx) + } } -// getPipelineDataCriticality is used to provide the data criticality of the pipeline +// getPipelineVertexDataCriticality is used to provide the data criticality of the pipeline // They can be of the following types: // 1. Ok: The pipeline is working as expected // 2. Warning: The pipeline is working but there is a lag in the data movement @@ -195,13 +202,12 @@ func (hc *HealthChecker) StartHealthCheck(ctx context.Context) { // // Based on this information, we first get the current buffer usage of each buffer in the pipeline at that instant // and populate the timeline data for each buffer. -// Then for a given buffer, we calculate the average buffer usage over -// the last HEALTH_WINDOW_SIZE seconds. +// Then for a given buffer, we calculate the weighted mean average buffer usage with decay over +// the last HEALTH_WINDOW_SIZE seconds. This is done to smoothen the curve and remove point spikes // If this average is greater than the critical threshold, we return the critical status // If this average is greater than the warning threshold, we return the warning status // If this average is less than the warning threshold, we return the ok status -func (hc *HealthChecker) getPipelineDataCriticality() ([]*VertexState, error) { - ctx := context.Background() +func (hc *HealthChecker) getPipelineVertexDataCriticality(ctx context.Context) ([]*vertexState, error) { // Fetch the buffer information for the pipeline buffers, err := listBuffers(ctx, hc.pipeline, hc.isbSvcClient) if err != nil { @@ -210,7 +216,7 @@ func (hc *HealthChecker) getPipelineDataCriticality() ([]*VertexState, error) { // update the usage timeline for all the ISBs used in the pipeline hc.updateUsageTimeline(buffers.Buffers) - var vertexState []*VertexState + var vertexState []*vertexState // iterate over the timeline data for each buffer and calculate the exponential weighted mean average // for the last HEALTH_WINDOW_SIZE buffer usage entries @@ -224,9 +230,9 @@ func (hc *HealthChecker) getPipelineDataCriticality() ([]*VertexState, error) { ewmaBufferUsage := calculateEWMAUsage(bufferUsage) // assign the state to the vertex based on the average buffer usage // Look back is enabled for the critical state - currentState := assignStateToTimeline(ewmaBufferUsage, EnableCriticalLookBack) + currentState := assignStateToTimeline(ewmaBufferUsage, enableCriticalLookBack) // create a new vertex state object - currentVertexState := NewVertexState(bufferName, currentState) + currentVertexState := newVertexState(bufferName, currentState) // add the vertex state to the list of vertex states vertexState = append(vertexState, currentVertexState) } @@ -235,7 +241,7 @@ func (hc *HealthChecker) getPipelineDataCriticality() ([]*VertexState, error) { // updateUsageTimeline is used to update the usage timeline for a given buffer list // This iterates over all the buffers in the buffer list and updates the usage timeline for each buffer -// The timeline data is represented as a map of buffer name to a list of TimelineEntry +// The timeline data is represented as a map of buffer name to a list of timelineEntry // Example: // // { @@ -254,13 +260,13 @@ func (hc *HealthChecker) updateUsageTimeline(bufferList []*daemon.BufferInfo) { // if the buffer name is not present in the timeline data, add it if _, ok := hc.timelineData[bufferName]; !ok { - hc.timelineData[bufferName] = sharedqueue.New[*TimelineEntry](int(HealthWindowSize)) + hc.timelineData[bufferName] = sharedqueue.New[*timelineEntry](int(healthWindowSize)) } // extract the current buffer usage and update the average buffer usage bufferUsage := buffer.GetBufferUsage() * 100 newAverage := updateAverageBufferUsage(hc.timelineData[bufferName].Items(), bufferUsage) // add the new entry to the timeline - hc.timelineData[bufferName].Append(&TimelineEntry{ + hc.timelineData[bufferName].Append(&timelineEntry{ Time: timestamp, BufferUsage: bufferUsage, AverageBufferUsage: newAverage, @@ -283,7 +289,7 @@ func (hc *HealthChecker) updateUsageTimeline(bufferList []*daemon.BufferInfo) { // averageBufferUsage = lastAverage * timelineSize + (newUsageEntry) / timelineSize + 1 // where lastAverage is the average buffer usage of the last entry in the timeline // and timelineSize is the size of the timeline -func updateAverageBufferUsage(timeline []*TimelineEntry, newEntry float64) float64 { +func updateAverageBufferUsage(timeline []*timelineEntry, newEntry float64) float64 { // If the timeline is empty, return the buffer usage of the new entry if len(timeline) == 0 { return newEntry @@ -292,9 +298,9 @@ func updateAverageBufferUsage(timeline []*TimelineEntry, newEntry float64) float lastAverage := timeline[len(timeline)-1].AverageBufferUsage var newAverage float64 // If the timeline is full - if len(timeline) == int(HealthWindowSize) { - newAverage = (lastAverage*float64(HealthWindowSize) + newEntry - timeline[0].BufferUsage) / float64(HealthWindowSize) - } else if len(timeline) < int(HealthWindowSize) { + if len(timeline) == int(healthWindowSize) { + newAverage = (lastAverage*float64(healthWindowSize) + newEntry - timeline[0].BufferUsage) / float64(healthWindowSize) + } else if len(timeline) < int(healthWindowSize) { // If the timeline is not full newAverage = (lastAverage*float64(len(timeline)) + newEntry) / float64(len(timeline)+1) } @@ -308,7 +314,7 @@ func updateAverageBufferUsage(timeline []*TimelineEntry, newEntry float64) float // where lastEWMA is the EWMA buffer usage of the last entry in the timeline func calculateEWMAUsage(bufferUsage []float64) []float64 { // Compute the current EWMA buffer usage of the timeline - a := ewma.NewSimpleEWMA(float64(HealthWindowSize)) + a := ewma.NewSimpleEWMA(float64(healthWindowSize)) var emwaValues []float64 // TODO: Check if we can keep storing the EWMA values instead of recomputing them for _, f := range bufferUsage { @@ -326,12 +332,12 @@ func calculateEWMAUsage(bufferUsage []float64) []float64 { func assignStateToBufferUsage(ewmaValue float64) string { // Assign the state to the buffer usage var state string - if ewmaValue > CriticalThreshold { - state = CriticalState - } else if ewmaValue > WarningThreshold { - state = WarningState + if ewmaValue > criticalBufferThreshold { + state = criticalState + } else if ewmaValue > warningBufferThreshold { + state = warningState } else { - state = HealthyState + state = healthyState } return state } @@ -350,7 +356,7 @@ func assignStateToTimeline(ewmaValues []float64, lookBack bool) string { // If the state is CRITICAL, and we have a look back, we need to check we have // LOOK_BACK_COUNT entries as CRITICAL - if state == CriticalState && lookBack { + if state == criticalState && lookBack { // Extract the states of the timeline var states []string for _, entry := range ewmaValues { @@ -359,19 +365,19 @@ func assignStateToTimeline(ewmaValues []float64, lookBack bool) string { // Count the number of times the state is CRITICAL in the last CRITICAL_WINDOW_SIZE entries var criticalCount int for i := len(states) - 1; i >= 0; i-- { - if states[i] == CriticalState { + if states[i] == criticalState { criticalCount++ } - if (len(states) - i) == CriticalWindowSize { + if (len(states) - i) == criticalWindowSize { break } } // If the state is CRITICAL at least LOOK_BACK_COUNT times in the last CRITICAL_WINDOW_SIZE entries // Set the state to CRITICAL, otherwise set the state to WARNING - if criticalCount >= CriticalLookBackCount { - state = CriticalState + if criticalCount >= criticalLookBackCount { + state = criticalState } else { - state = WarningState + state = warningState } } return state @@ -384,7 +390,7 @@ func assignStateToTimeline(ewmaValues []float64, lookBack bool) string { // else the pipeline is ok // Here we follow a precedence order of unknown > critical > warning > ok // Hence, whichever is the highest precedence state found in the vertex state, we return that -func convertVertexStateToPipelineState(vertexState []*VertexState) *DataHealthResponse { +func convertVertexStateToPipelineState(vertexState []*vertexState) *dataHealthResponse { // create a map to store the precedence order of the states // assign a number to each state based on the precedence order @@ -417,7 +423,7 @@ func convertVertexStateToPipelineState(vertexState []*VertexState) *DataHealthRe } // if we reach here, return unknown state - return NewDataHealthResponse(PipelineStatusUnknown, + return newDataHealthResponse(PipelineStatusUnknown, "Pipeline dataflow is in an unknown state", "D4") } @@ -428,29 +434,29 @@ func convertVertexStateToPipelineState(vertexState []*VertexState) *DataHealthRe // and the code corresponding to Warning, similar for Critical // if the state is Unknown we return a message saying the pipeline is in an unknown state due to the vertex, // and the code corresponding to Unknown -func generateDataHealthResponse(state string, vertex string) *DataHealthResponse { +func generateDataHealthResponse(state string, vertex string) *dataHealthResponse { switch state { case PipelineStatusOK: - return NewDataHealthResponse( + return newDataHealthResponse( PipelineStatusOK, "Pipeline dataflow is healthy", "D1") case PipelineStatusWarning: - return NewDataHealthResponse( + return newDataHealthResponse( PipelineStatusWarning, fmt.Sprintf("Dataflow is in warning state for %s", vertex), "D2") case PipelineStatusCritical: - return NewDataHealthResponse( + return newDataHealthResponse( PipelineStatusCritical, fmt.Sprintf("Dataflow is in critical state for %s", vertex), "D3") case PipelineStatusUnknown: - return NewDataHealthResponse( + return newDataHealthResponse( PipelineStatusUnknown, fmt.Sprintf("Pipeline dataflow is in an unknown state due to %s", vertex), "D4") default: - return DefaultDataHealthResponse + return defaultDataHealthResponse } } diff --git a/pkg/daemon/server/service/pipeline_metrics_query.go b/pkg/daemon/server/service/pipeline_metrics_query.go index c371b281b2..f2fa316af7 100644 --- a/pkg/daemon/server/service/pipeline_metrics_query.go +++ b/pkg/daemon/server/service/pipeline_metrics_query.go @@ -225,7 +225,7 @@ func (ps *PipelineMetadataQuery) getPending(ctx context.Context, req *daemon.Get } func (ps *PipelineMetadataQuery) GetPipelineStatus(ctx context.Context, req *daemon.GetPipelineStatusRequest) (*daemon.GetPipelineStatusResponse, error) { - status := ps.healthChecker.GetCurrentHealth() + status := ps.healthChecker.getCurrentHealth() resp := new(daemon.GetPipelineStatusResponse) resp.Status = &daemon.PipelineStatus{ Status: pointer.String(status.Status), @@ -291,5 +291,5 @@ func listBuffers(ctx context.Context, pipeline *v1alpha1.Pipeline, isbSvcClient // StartHealthCheck starts the health check for the pipeline using the health checker func (ps *PipelineMetadataQuery) StartHealthCheck(ctx context.Context) { - ps.healthChecker.StartHealthCheck(ctx) + ps.healthChecker.startHealthCheck(ctx) } diff --git a/pkg/shared/ewma/interface.go b/pkg/shared/ewma/interface.go index 79564863ef..4ad31168d1 100644 --- a/pkg/shared/ewma/interface.go +++ b/pkg/shared/ewma/interface.go @@ -1,5 +1,7 @@ package ewma +// EWMA is the interface for Exponentially Weighted Moving Average +// It is used to calculate the moving average with decay of a series of numbers type EWMA interface { // Add adds a new value to the EWMA Add(float64) diff --git a/pkg/shared/ewma/simple_ewma.go b/pkg/shared/ewma/simple_ewma.go index 328b7f0f1e..f7dcbd36e8 100644 --- a/pkg/shared/ewma/simple_ewma.go +++ b/pkg/shared/ewma/simple_ewma.go @@ -1,9 +1,9 @@ package ewma const ( - averageAge = 30.0 + defaultAlpha = 30.0 // constDecayFactor is the default decay factor - constDecayFactor = 2.0 / (averageAge + 1.0) + constDecayFactor = 2.0 / (defaultAlpha + 1.0) ) // SimpleEWMA is a simple implementation of EWMA diff --git a/server/apis/v1/health.go b/server/apis/v1/health.go index cfefbec932..a89d9ab819 100644 --- a/server/apis/v1/health.go +++ b/server/apis/v1/health.go @@ -84,7 +84,7 @@ func (hc *HealthChecker) getPipelineResourceHealth(h *handler, ns string, func checkVertexLevelHealth(ctx context.Context, h *handler, ns string, pipeline string) (*resourceHealthResponse, error) { log := logging.FromContext(ctx) - // check if the pipeline is paused, if so, return paused status + // get the pipeline object pl, err := h.numaflowClient.Pipelines(ns).Get(context.Background(), pipeline, metav1.GetOptions{}) // if error return unknown status if err != nil { @@ -107,7 +107,6 @@ func checkVertexLevelHealth(ctx context.Context, h *handler, ns string, // if the pipeline is killed, return killed status // this cannot be checked at individual vertex level, hence needs to be checked here - // TODO(Health): Check if this is correct? if pl.Spec.Lifecycle.GetDesiredPhase() == dfv1.PipelinePhaseDeleting { return &resourceHealthResponse{ Status: PipelineStatusDeleting, From f3a52e38f7b0eaeb71102370ba9bdc57a6a570c4 Mon Sep 17 00:00:00 2001 From: Sidhant Kohli Date: Fri, 15 Dec 2023 11:21:52 -0800 Subject: [PATCH 12/12] refactor Signed-off-by: Sidhant Kohli --- server/apis/v1/health.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/apis/v1/health.go b/server/apis/v1/health.go index a89d9ab819..f52c45f31f 100644 --- a/server/apis/v1/health.go +++ b/server/apis/v1/health.go @@ -55,7 +55,7 @@ func (hc *HealthChecker) getPipelineResourceHealth(h *handler, ns string, // check if the pipeline status is cached if status, ok := hc.resourceStatusCache.Get(cacheKey); ok { - log.Info("pipeline status from cache: ", status) + log.Info("Pipeline status from cache: ", status) return status, nil } // if not present in cache, check for the current pipeline status