From 9bf22531d0ed5b8e3173c3742a1f5fd78514271b Mon Sep 17 00:00:00 2001 From: Haibin Xie Date: Sun, 28 Apr 2019 11:48:16 +0800 Subject: [PATCH] statistics: fix panic when move analyze jobs to history (#10286) --- statistics/analyze_jobs.go | 2 +- statistics/analyze_jobs_test.go | 34 +++++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 statistics/analyze_jobs_test.go diff --git a/statistics/analyze_jobs.go b/statistics/analyze_jobs.go index b6910aa4a77f0..ff1fedfcf6464 100644 --- a/statistics/analyze_jobs.go +++ b/statistics/analyze_jobs.go @@ -63,7 +63,7 @@ func MoveToHistory(job *AnalyzeJob) { analyzeStatus.Lock() delete(analyzeStatus.jobs, job) analyzeStatus.history = append(analyzeStatus.history, job) - numJobs := len(analyzeStatus.jobs) + numJobs := len(analyzeStatus.history) if numJobs > numMaxHistoryJobs { analyzeStatus.history = analyzeStatus.history[numJobs-numMaxHistoryJobs:] } diff --git a/statistics/analyze_jobs_test.go b/statistics/analyze_jobs_test.go new file mode 100644 index 0000000000000..0c91672ea9bc6 --- /dev/null +++ b/statistics/analyze_jobs_test.go @@ -0,0 +1,34 @@ +// Copyright 2019 PingCAP, Inc. +// +// 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, +// See the License for the specific language governing permissions and +// limitations under the License. + +package statistics + +import ( + . "github.com/pingcap/check" +) + +func (s *testStatisticsSuite) TestMoveToHistory(c *C) { + numJobs := numMaxHistoryJobs*2 + 1 + jobs := make([]*AnalyzeJob, 0, numJobs) + for i := 0; i < numJobs; i++ { + job := &AnalyzeJob{} + AddNewAnalyzeJob(job) + jobs = append(jobs, job) + } + MoveToHistory(jobs[0]) + c.Assert(len(GetAllAnalyzeJobs()), Equals, numJobs) + for i := 1; i < numJobs; i++ { + MoveToHistory(jobs[i]) + } + c.Assert(len(GetAllAnalyzeJobs()), Equals, numMaxHistoryJobs) +}