Skip to content

Commit

Permalink
redesign: PSUPCLPL-17350 added error handling from the curator and ad…
Browse files Browse the repository at this point in the history
…ded a return 404 error in case if the backup copy is not found.
  • Loading branch information
fibu0125 committed Jan 30, 2025
1 parent 8b27452 commit db0e378
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 9 deletions.
40 changes: 40 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
linters:
disable-all: true
enable:
- errcheck
- gosimple
- govet
- ineffassign
- staticcheck
- typecheck
- unused
- bodyclose
- gofmt
- exportloopref
- prealloc
- gocritic
linters-settings:
errcheck:
check-type-assertions: true
check-blank: true
govet:
check-shadowing: true
enable:
- fieldalignment
- httpresponse
- defers
- ifaceassert
- slog
- waitgroup
gocritic:
enabled-tags:
- diagnostic
- experimental
- opinionated
- performance
- style

run:
timeout: 10m
go: '1.18'
modules-download-mode: readonly
60 changes: 51 additions & 9 deletions backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ type Curator struct {
client *http.Client
}

var ErrBackupNotFound = errors.New("backup not found")
var ErrCuratorUnavailable = errors.New("curator return internal server error")

type BackupProvider struct {
client common.Client
indexNames *common.IndexAdapter
Expand Down Expand Up @@ -156,7 +159,12 @@ func (bp BackupProvider) CollectBackupHandler() func(w http.ResponseWriter, r *h
_, _ = w.Write([]byte(err.Error()))
return
}
defer r.Body.Close()
defer func(Body io.ReadCloser) {
err = Body.Close()
if err != nil {
logger.ErrorContext(ctx, "failed to close http response body", slog.Any("error", err.Error()))
}
}(r.Body)

backupID, err := bp.CollectBackup(databases, ctx)
if err != nil {
Expand All @@ -165,7 +173,18 @@ func (bp BackupProvider) CollectBackupHandler() func(w http.ResponseWriter, r *h
_, _ = w.Write([]byte(err.Error()))
return
}
response := bp.TrackBackup(backupID, ctx)

response, err := bp.TrackBackup(backupID, ctx)
if err != nil {
if errors.Is(err, ErrBackupNotFound) {
w.WriteHeader(http.StatusNotFound)
_, err = w.Write([]byte(err.Error()))
if err != nil {
logger.ErrorContext(ctx, "failed to write bytes to writer output channel", slog.String("error", err.Error()))
}
return
}
}
responseBody, err := json.Marshal(response)
if err != nil {
logger.ErrorContext(ctx, "Failed to marshal response to JSON", slog.Any("error", err))
Expand Down Expand Up @@ -205,7 +224,18 @@ func (bp BackupProvider) TrackBackupHandler() func(w http.ResponseWriter, r *htt
logger.InfoContext(ctx, fmt.Sprintf("Request to track backup in '%s' is received", r.URL.Path))
vars := mux.Vars(r)
trackID := vars["backupID"]
response := bp.TrackBackup(trackID, ctx)
response, err := bp.TrackBackup(trackID, ctx)
if err != nil {
if errors.Is(err, ErrBackupNotFound) {
w.WriteHeader(http.StatusNotFound)
_, err = w.Write([]byte(err.Error()))
if err != nil {
logger.ErrorContext(ctx, "failed to write bytes to writer output channel", slog.String("error", err.Error()))
}
return
}
}

responseBody, err := json.Marshal(response)
if err != nil {
logger.ErrorContext(ctx, "Failed to marshal response to JSON", slog.Any("error", err))
Expand Down Expand Up @@ -386,16 +416,16 @@ func (bp BackupProvider) CollectBackup(dbs []string, ctx context.Context) (strin
return string(responseBody), nil
}

func (bp BackupProvider) TrackBackup(backupID string, ctx context.Context) ActionTrack {
func (bp BackupProvider) TrackBackup(backupID string, ctx context.Context) (ActionTrack, error) {
logger.DebugContext(ctx, fmt.Sprintf("Request to track '%s' backup is requested",
backupID))
jobStatus, err := bp.getJobStatus(backupID, ctx)
if err != nil {
logger.ErrorContext(ctx, "Failed to find snapshot", slog.Any("error", err))
return backupTrack(backupID, "FAIL")
return backupTrack(backupID, "FAIL"), err
}
logger.DebugContext(ctx, fmt.Sprintf("'%s' backup status is %s", backupID, jobStatus))
return backupTrack(backupID, jobStatus)
return backupTrack(backupID, jobStatus), nil
}

func (bp BackupProvider) DeleteBackup(backupID string, ctx context.Context) *http.Response {
Expand Down Expand Up @@ -691,7 +721,7 @@ func (bp BackupProvider) prepareRestoreRequest(ctx context.Context, url string,

func (bp BackupProvider) getJobStatus(snapshotName string, ctx context.Context) (string, error) {
url := fmt.Sprintf("%s/%s/%s", bp.Curator.url, "jobstatus", snapshotName)
request, err := http.NewRequest(http.MethodGet, url, nil)
request, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
logger.ErrorContext(ctx, "Failed to prepare request to track backup", slog.Any("error", err))
return "FAIL", err
Expand All @@ -703,13 +733,25 @@ func (bp BackupProvider) getJobStatus(snapshotName string, ctx context.Context)
logger.ErrorContext(ctx, "Failed to process request by curator", slog.Any("error", err))
return "FAIL", err
}
defer response.Body.Close()

defer func(Body io.ReadCloser) {
err = Body.Close()
if err != nil {
logger.ErrorContext(ctx, "Failed to properly close the response body ")
}
}(response.Body)

if response.StatusCode == 404 {
return "FAIL", ErrBackupNotFound
}

var jobStatus JobStatus
err = json.NewDecoder(response.Body).Decode(&jobStatus)
if err != nil {
logger.ErrorContext(ctx, "Failed to decode response from JSON", slog.Any("error", err))
return "FAIL", err
return "FAIL", fmt.Errorf("failed to decode response from JSON: %w", err)
}

var status string
switch state := jobStatus.State; state {
case "Failed":
Expand Down
4 changes: 4 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,20 @@ require (
github.com/hashicorp/go-sockaddr v1.0.2 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/vault/api v1.14.0 // indirect
github.com/mdempsky/maligned v0.0.0-20220203220013-d7cd9a96ae47 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/ryanuber/go-glob v1.0.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/crypto v0.23.0 // indirect
golang.org/x/mod v0.13.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.20.0 // indirect
golang.org/x/text v0.15.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.13.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/utils v0.0.0-20230406110748-d93618cff8a2 // indirect
Expand Down
26 changes: 26 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mdempsky/maligned v0.0.0-20220203220013-d7cd9a96ae47 h1:CD59WK1zO7eDo8FlF7Y2pme3YT60sC+4QmmSYzRHSg4=
github.com/mdempsky/maligned v0.0.0-20220203220013-d7cd9a96ae47/go.mod h1:fG2WnxTTx4KnybDzFl+PgKtpuU2cfCltyicf9cXVxls=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
Expand All @@ -85,29 +87,53 @@ github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI=
golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8=
golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro=
golang.org/x/mod v0.13.0 h1:I/DsJXRlw/8l/0c24sM9yb0T4z9liZTduXvdAWYiysY=
golang.org/x/mod v0.13.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20211015210444-4f30a5c0130f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac=
golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220128215802-99c3d69c2c27/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y=
golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk=
golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU=
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
Expand Down

0 comments on commit db0e378

Please sign in to comment.