From 2c31bd2fe97701336afc60a08eb3b5fc6ccb396f Mon Sep 17 00:00:00 2001 From: yisaer Date: Mon, 6 Dec 2021 12:02:59 +0800 Subject: [PATCH 1/2] refine logic Signed-off-by: yisaer --- server/plan_replayer.go | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/server/plan_replayer.go b/server/plan_replayer.go index a363781f0b8c9..25ed6ec1de4bf 100644 --- a/server/plan_replayer.go +++ b/server/plan_replayer.go @@ -16,7 +16,7 @@ package server import ( "fmt" - "io" + "io/ioutil" "net/http" "os" "path/filepath" @@ -69,15 +69,18 @@ func handleDownloadFile(handler downloadFileHandler, w http.ResponseWriter, req params := mux.Vars(req) name := params[pFileName] path := handler.filePath - if isExists(path) { - w.Header().Set("Content-Type", "application/zip") - w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.zip\"", handler.downloadedFilename)) + exist, err := isExists(path) + if err != nil { + writeError(w, err) + return + } + if exist { file, err := os.Open(path) if err != nil { writeError(w, err) return } - _, err = io.Copy(w, file) + content, err := ioutil.ReadAll(file) if err != nil { writeError(w, err) return @@ -92,7 +95,9 @@ func handleDownloadFile(handler downloadFileHandler, w http.ResponseWriter, req writeError(w, err) return } - w.WriteHeader(http.StatusOK) + w.Header().Set("Content-Type", "application/zip") + w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.zip\"", handler.downloadedFilename)) + w.Write(content) return } if handler.infoGetter == nil { @@ -126,10 +131,7 @@ func handleDownloadFile(handler downloadFileHandler, w http.ResponseWriter, req if resp.StatusCode != http.StatusOK { continue } - // find dump file in one remote tidb-server, return file directly - w.Header().Set("Content-Type", "application/zip") - w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.zip\"", handler.downloadedFilename)) - _, err = io.Copy(w, resp.Body) + content, err := ioutil.ReadAll(resp.Body) if err != nil { writeError(w, err) return @@ -139,11 +141,14 @@ func handleDownloadFile(handler downloadFileHandler, w http.ResponseWriter, req writeError(w, err) return } - w.WriteHeader(http.StatusOK) + // find dump file in one remote tidb-server, return file directly + w.Header().Set("Content-Type", "application/zip") + w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.zip\"", handler.downloadedFilename)) + w.Write(content) return } // we can't find dump file in any tidb-server, return 404 directly - logutil.BgLogger().Info("can't find dump file in any remote server", zap.String("filename", name)) + logutil.BgLogger().Error("can't find dump file in any remote server", zap.String("filename", name)) w.WriteHeader(http.StatusNotFound) } @@ -157,10 +162,13 @@ type downloadFileHandler struct { downloadedFilename string } -func isExists(path string) bool { +func isExists(path string) (bool, error) { _, err := os.Stat(path) - if err != nil && !os.IsExist(err) { - return false + if err != nil { + if os.IsNotExist(err) { + return false, nil + } + return false, err } - return true + return true, nil } From a6531679095da18c9fb4be11f746239cf7b72a41 Mon Sep 17 00:00:00 2001 From: yisaer Date: Mon, 6 Dec 2021 12:08:45 +0800 Subject: [PATCH 2/2] fix Signed-off-by: yisaer --- server/plan_replayer.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/server/plan_replayer.go b/server/plan_replayer.go index 25ed6ec1de4bf..1d9a6a957f06e 100644 --- a/server/plan_replayer.go +++ b/server/plan_replayer.go @@ -95,9 +95,13 @@ func handleDownloadFile(handler downloadFileHandler, w http.ResponseWriter, req writeError(w, err) return } + _, err = w.Write(content) + if err != nil { + writeError(w, err) + return + } w.Header().Set("Content-Type", "application/zip") w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.zip\"", handler.downloadedFilename)) - w.Write(content) return } if handler.infoGetter == nil { @@ -141,10 +145,14 @@ func handleDownloadFile(handler downloadFileHandler, w http.ResponseWriter, req writeError(w, err) return } + _, err = w.Write(content) + if err != nil { + writeError(w, err) + return + } // find dump file in one remote tidb-server, return file directly w.Header().Set("Content-Type", "application/zip") w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=\"%s.zip\"", handler.downloadedFilename)) - w.Write(content) return } // we can't find dump file in any tidb-server, return 404 directly