From 84166e192bb17293cf74f447a2d6f7c644732521 Mon Sep 17 00:00:00 2001 From: Maximilian Pass <22845248+mpass99@users.noreply.github.com> Date: Tue, 23 Jul 2024 13:24:35 +0200 Subject: [PATCH] Refine updateFileSystem logging messages to be able to differentiate between the different errors that occur. --- internal/api/runners.go | 26 +++++++++++++++++++++----- internal/nomad/api_querier.go | 7 ++++++- 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/internal/api/runners.go b/internal/api/runners.go index e6b85129..b5851081 100644 --- a/internal/api/runners.go +++ b/internal/api/runners.go @@ -12,7 +12,9 @@ import ( "github.com/google/uuid" "github.com/gorilla/mux" + nomadApi "github.com/hashicorp/nomad/api" "github.com/openHPI/poseidon/internal/config" + "github.com/openHPI/poseidon/internal/nomad" "github.com/openHPI/poseidon/internal/runner" "github.com/openHPI/poseidon/pkg/dto" "github.com/openHPI/poseidon/pkg/logging" @@ -137,13 +139,27 @@ func (r *RunnerController) updateFileSystem(writer http.ResponseWriter, request logging.StartSpan(request.Context(), "api.fs.update", "Update File System", func(ctx context.Context) { err = targetRunner.UpdateFileSystem(ctx, fileCopyRequest) }) - if err != nil { - log.WithContext(request.Context()).WithError(err).Error("Could not perform the requested updateFileSystem.") + + entry := log.WithContext(request.Context()).WithError(err) + switch { + case err == nil: + writer.WriteHeader(http.StatusNoContent) + case errors.Is(err, nomadApi.NodeDownErr): + entry.Debug("Nomad Node Down while updateFileSystem") + writeInternalServerError(request.Context(), writer, err, dto.ErrorNomadInternalServerError) + case errors.Is(err, io.ErrUnexpectedEOF): + entry.Warn("Unexpected EOF while updateFileSystem") + writeInternalServerError(request.Context(), writer, err, dto.ErrorUnknown) + case errors.Is(err, nomad.ErrNoAllocationFound): + entry.Warn("No allocation found while updateFileSystem") + writeInternalServerError(request.Context(), writer, err, dto.ErrorUnknown) + case errors.Is(err, nomad.ErrNomadUnknownAllocation): + entry.Warn("Unknown allocation while updateFileSystem") + writeInternalServerError(request.Context(), writer, err, dto.ErrorUnknown) + default: + entry.Error("Could not perform the requested updateFileSystem.") writeInternalServerError(request.Context(), writer, err, dto.ErrorUnknown) - return } - - writer.WriteHeader(http.StatusNoContent) } func (r *RunnerController) fileContent(writer http.ResponseWriter, request *http.Request) { diff --git a/internal/nomad/api_querier.go b/internal/nomad/api_querier.go index 313fda69..8a1a5475 100644 --- a/internal/nomad/api_querier.go +++ b/internal/nomad/api_querier.go @@ -14,7 +14,10 @@ import ( "github.com/openHPI/poseidon/pkg/logging" ) -var ErrNoAllocationFound = errors.New("no allocation found") +var ( + ErrNoAllocationFound = errors.New("no allocation found") + ErrNomadUnknownAllocation = errors.New("unknown allocation") +) // apiQuerier provides access to the Nomad functionality. type apiQuerier interface { @@ -135,6 +138,8 @@ func (nc *nomadAPIClient) Execute(ctx context.Context, runnerID string, cmd stri case errors.Is(err, context.Canceled): log.WithContext(ctx).Debug("Execution canceled by context") return 0, nil + case strings.Contains(err.Error(), "Unknown allocation"): + return 1, ErrNomadUnknownAllocation default: return 1, fmt.Errorf("error executing command in allocation: %w", err) }