Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

stability: retry truncating sst files upon failure #484

Merged
merged 11 commits into from
May 27, 2019
71 changes: 44 additions & 27 deletions tests/pkg/ops/tikv.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,17 @@
package ops

import (
"strconv"
"strings"
"time"

"github.com/golang/glog"
"github.com/pingcap/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const retryLimit = 15

type TruncateOptions struct {
Namespace string
Cluster string
Expand Down Expand Up @@ -53,41 +57,54 @@ func (ops *TiKVOps) TruncateSSTFile(opts TruncateOptions) error {
})
}

stdout, stderr, err := exec("find", "/var/lib/tikv/db", "-name", "*.sst", "-o", "-name", "*.save")
if err != nil {
glog.Errorf("list sst files: stderr=%s err=%s", stderr, err.Error())
return errors.Annotate(err, "list sst files")
}
retryCount := 0
for ; retryCount < retryLimit; retryCount++ {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for ; retryCount < retryLimit; retryCount++ {
for ; retryCount < retryLimit; retryCount++ {
time.Sleep(10*time.Second)

if retryCount > 0 {
time.Sleep(10 * time.Second)
}
stdout, stderr, err := exec("find", "/var/lib/tikv/db", "-name", "*.sst", "-o", "-name", "*.save")
if err != nil {
glog.Warningf("list sst files: stderr=%s err=%s", stderr, err.Error())
continue
}

sstCandidates := make(map[string]bool)
sstCandidates := make(map[string]bool)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete the blanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the one more indent because it's in retry loop now.


for _, f := range strings.Split(stdout, "\n") {
f = strings.TrimSpace(f)
if len(f) > 0 {
sstCandidates[f] = true
for _, f := range strings.Split(stdout, "\n") {
f = strings.TrimSpace(f)
if len(f) > 0 {
sstCandidates[f] = true
}
}
}

sst := ""
for k := range sstCandidates {
if strings.HasSuffix(k, ".sst") && !sstCandidates[k+".save"] {
sst = k
sst := ""
for k := range sstCandidates {
if strings.HasSuffix(k, ".sst") && !sstCandidates[k+".save"] {
sst = k
}
}
if len(sst) == 0 {
glog.Warning("cannot find a sst file")
continue
}
}
if len(sst) == 0 {
return errors.New("cannot find a sst file")
}

_, stderr, err = exec("cp", sst, sst+".save")
if err != nil {
glog.Errorf("backup sst file: stderr=%s err=%s", stderr, err.Error())
return errors.Annotate(err, "backup sst file")
_, stderr, err = exec("cp", sst, sst+".save")
if err != nil {
glog.Warningf("backup sst file: stderr=%s err=%s", stderr, err.Error())
continue
}

_, stderr, err = exec("truncate", "-s", "0", sst)
if err != nil {
glog.Warningf("truncate sst file: stderr=%s err=%s", stderr, err.Error())
continue
}

break
}

_, stderr, err = exec("truncate", "-s", "0", sst)
if err != nil {
glog.Errorf("truncate sst file: stderr=%s err=%s", stderr, err.Error())
return errors.Annotate(err, "truncate sst file")
if retryCount == retryLimit {
return errors.New("failed to truncate sst file after " + strconv.Itoa(retryLimit) + " trials")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the error log needs opts.Namespace and opts.Cluster field

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added annotations to log methods. however, it's caller who pass the opts arg, that is, the caller must know these info, so I don't think there is a need for adding these fields to returned error.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but the caller has not added these fields to error log too
ref: https://github.com/pingcap/tidb-operator/blob/master/tests/failover.go#L74-L81

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added some error logs to the case func. If you think every error should be annotated with additional fields, it would be better to create an another PR to do it, because most of methods of operatorActions failed to do so.

}

return nil
Expand Down