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

feat: Compress data backup using zstd #264

Merged
merged 1 commit into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
github.com/golang/glog v1.0.0
github.com/golang/protobuf v1.5.2
github.com/jteeuwen/go-bindata v3.0.7+incompatible
github.com/klauspost/compress v1.16.5
github.com/nsf/jsondiff v0.0.0-20190712045011-8443391ee9b6
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.14.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,8 @@ github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7V
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.16.5 h1:IFV2oUNUzZaz+XyusxpLzpzS8Pt5rh0Z16For/djlyI=
github.com/klauspost/compress v1.16.5/go.mod h1:ntbaceVETuRiXiv4DpjP66DpAtAGkEQskQzEyD//IeE=
github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
Expand Down
21 changes: 18 additions & 3 deletions pkg/sloop/webserver/webserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"syscall"
"time"

"github.com/klauspost/compress/zstd"
"github.com/salesforce/sloop/pkg/sloop/common"
"github.com/spf13/afero"

Expand Down Expand Up @@ -85,7 +86,7 @@
logWebError(nil, "Not allowed", r, w)
return
}
data, err := readWebfile(fixedUrl, &afero.Afero{afero.NewOsFs()})

Check failure on line 89 in pkg/sloop/webserver/webserver.go

View workflow job for this annotation

GitHub Actions / test

github.com/spf13/afero.Afero composite literal uses unkeyed fields
if err != nil {
logWebError(err, "Error reading web file: "+fixedUrl, r, w)
return
Expand Down Expand Up @@ -116,11 +117,25 @@
return
}

w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=sloop-%s-%d.bak", currentContext, since))
w.Header().Set("Content-Type", "application/octet-stream")
w.Header().Set("Content-Disposition", fmt.Sprintf("attachment; filename=sloop-%s-%d.bak.zst", currentContext, since))
// The 'Content-Length' header is not set, because we do not know the size of the backup before we write it to the body.
w.Header().Set("Content-Encoding", "zstd")
w.Header().Set("Content-Type", "application/zstd")
w.Header().Set("Transfer-Encoding", "chunked")

_, err = db.Backup(w, since)
zw, err := zstd.NewWriter(w)
if err != nil {
logWebError(err, "Error configuring compression", r, w)
return
}

_, err = db.Backup(zw, since)
if err != nil {
logWebError(err, "Error writing backup", r, w)
return
}

err = zw.Close()
if err != nil {
logWebError(err, "Error writing backup", r, w)
return
Expand Down
18 changes: 18 additions & 0 deletions pkg/sloop/webserver/webserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"net/http/httptest"
"testing"

badger "github.com/dgraph-io/badger/v2"
"github.com/salesforce/sloop/pkg/sloop/store/untyped/badgerwrap"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -55,3 +57,19 @@ func TestWebFileHandler(t *testing.T) {
assert.Equal(t, http.StatusOK, rr.Code)
assert.NotNil(t, rr.Body.String())
}

func TestBackupHandler(t *testing.T) {
req, err := http.NewRequest("GET", "/clusterContext/data/backup", nil)
assert.Nil(t, err)

db, err := (&badgerwrap.MockFactory{}).Open(badger.DefaultOptions(""))
assert.Nil(t, err)

// Create a ResponseRecorder (which satisfies http.ResponseWriter) to record the response.
rr := httptest.NewRecorder()
handler := http.HandlerFunc(backupHandler(db, "clusterContext"))
handler.ServeHTTP(rr, req)

assert.Equal(t, http.StatusOK, rr.Code)
assert.NotNil(t, rr.Body.String())
}
Loading