Skip to content

Commit

Permalink
Backup: safe compressor/decompressor closure (#13668)
Browse files Browse the repository at this point in the history
Signed-off-by: Shlomi Noach <[email protected]>
  • Loading branch information
shlomi-noach authored Aug 7, 2023
1 parent a30e514 commit 8a78949
Show file tree
Hide file tree
Showing 8 changed files with 797 additions and 677 deletions.
56 changes: 56 additions & 0 deletions go/ioutil/timeout_closer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2023 The Vitess Authors.
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,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package ioutil

import (
"context"
"io"
"time"
)

// TimeoutCloser is an io.Closer that has a timeout for executing the Close() function.
type TimeoutCloser struct {
ctx context.Context
closer io.Closer
timeout time.Duration
}

func NewTimeoutCloser(ctx context.Context, closer io.Closer, timeout time.Duration) *TimeoutCloser {
return &TimeoutCloser{
ctx: ctx,
closer: closer,
timeout: timeout,
}
}

func (c *TimeoutCloser) Close() error {
done := make(chan error)

ctx, cancel := context.WithTimeout(c.ctx, c.timeout)
defer cancel()

go func() {
defer close(done)
done <- c.closer.Close()
}()
select {
case err := <-done:
return err
case <-ctx.Done():
return ctx.Err()
}
}
53 changes: 53 additions & 0 deletions go/ioutil/timeout_closer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
Copyright 2023 The Vitess Authors.
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,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package ioutil

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

type hangCloser struct {
hang bool
}

func (c hangCloser) Close() error {
if c.hang {
ch := make(chan bool)
ch <- true // hang forever
}
return nil
}

func TestTimeoutCloser(t *testing.T) {
ctx := context.Background()
{
closer := NewTimeoutCloser(ctx, &hangCloser{hang: false}, time.Second)
err := closer.Close()
require.NoError(t, err)
}
{
closer := NewTimeoutCloser(ctx, &hangCloser{hang: true}, time.Second)
err := closer.Close()
require.Error(t, err)
assert.ErrorIs(t, err, context.DeadlineExceeded)
}
}
5 changes: 5 additions & 0 deletions go/vt/mysqlctl/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ const (
RestoreState = "restore_in_progress"
// BackupTimestampFormat is the format in which we save BackupTime and FinishedTime
BackupTimestampFormat = "2006-01-02.150405"

// closeTimeout is the timeout for closing backup files after writing.
// The value is a bit arbitrary. How long does it make sense to wait for a Close()? With a cloud-based implementation,
// network might be an issue. _Seconds_ are probably too short. The whereabouts of a minute us a reasonable value.
closeTimeout = 1 * time.Minute
)

const (
Expand Down
Loading

0 comments on commit 8a78949

Please sign in to comment.