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

Implement objectSizer for minio object and timingReadCloser #4687

Closed
wants to merge 2 commits into from
Closed
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
24 changes: 22 additions & 2 deletions pkg/objstore/objstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ import (
"strings"
"time"

"github.com/go-kit/log"
"github.com/go-kit/log/level"
"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
"github.com/minio/minio-go/v7"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
Expand Down Expand Up @@ -145,6 +146,12 @@ func TryToGetSize(r io.Reader) (int64, error) {
return int64(f.Len()), nil
case *strings.Reader:
return f.Size(), nil
case *minio.Object:
Copy link
Member

Choose a reason for hiding this comment

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

💪🏻 Could add a unit test?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@hanjm Seems it is hard to add one for minio.Object as the objectInfo field is not exposed. I feel this is not a blocker for this pr. WDYT?

Copy link
Member

@hanjm hanjm Feb 22, 2022

Choose a reason for hiding this comment

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

@yeya24 pkg/objstore is a abstract package for all of objectstore, i think it should not assert to a certain implement. another way to solve the replicate issue is implement objstore.ObjectSizer for this . like the PR #4687.
"pkg/objstore"

	// Add size info into reader for pass it to Upload function.
	r := objectSizerReadCloser{ReadCloser: resp.Body, size: resp.ContentLength}
	return r, nil
}

type objectSizerReadCloser struct {
	io.ReadCloser
	size int64
}

// ObjectSize implement objstore.ObjectSizer.
func (o objectSizerReadCloser) ObjectSize() (int64, error) {
	return o.size, nil
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. Thank you!

objectInfo, err := f.Stat()
if err != nil {
return 0, errors.Wrap(err, "minio.Object.Stat()")
}
return objectInfo.Size, nil
case ObjectSizer:
return f.ObjectSize()
}
Expand Down Expand Up @@ -500,9 +507,16 @@ type timingReadCloser struct {
duration *prometheus.HistogramVec
failed *prometheus.CounterVec
isFailureExpected IsOpFailureExpectedFunc

objSize int64
objSizeErr error
}

func newTimingReadCloser(rc io.ReadCloser, op string, dur *prometheus.HistogramVec, failed *prometheus.CounterVec, isFailureExpected IsOpFailureExpectedFunc) *timingReadCloser {
// Since TryToGetSize can only reliably return size before doing any read calls,
// we call during "construction" and remember the results.
objSize, objSizeErr := TryToGetSize(rc)

// Initialize the metrics with 0.
dur.WithLabelValues(op)
failed.WithLabelValues(op)
Expand All @@ -513,6 +527,8 @@ func newTimingReadCloser(rc io.ReadCloser, op string, dur *prometheus.HistogramV
duration: dur,
failed: failed,
isFailureExpected: isFailureExpected,
objSize: objSize,
objSizeErr: objSizeErr,
}
}

Expand All @@ -539,3 +555,7 @@ func (rc *timingReadCloser) Read(b []byte) (n int, err error) {
}
return n, err
}

func (rc *timingReadCloser) ObjectSize() (int64, error) {
return rc.objSize, rc.objSizeErr
}
74 changes: 74 additions & 0 deletions pkg/objstore/objstore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@ package objstore
import (
"bytes"
"io"
"os"
"path"
"strings"
"testing"

"github.com/pkg/errors"
promtest "github.com/prometheus/client_golang/prometheus/testutil"

"github.com/thanos-io/thanos/pkg/testutil"
Expand Down Expand Up @@ -86,3 +90,73 @@ func TestTracingReader(t *testing.T) {
testutil.Ok(t, err)
testutil.Equals(t, int64(11), size)
}

func TestTryToGetSize(t *testing.T) {
tmpFile, err := os.OpenFile(path.Join(os.TempDir(), "test_try_get_size"), os.O_CREATE|os.O_RDWR, os.ModePerm)
testutil.Ok(t, err)
defer tmpFile.Close()
_, err = tmpFile.WriteString("abc")
testutil.Ok(t, err)

buf := make([]byte, 5)
bbuff := bytes.NewBuffer(buf)

sreader := strings.NewReader("hello world")

buf2 := make([]byte, 100)
breader := bytes.NewReader(buf2)

r := bytes.NewBuffer(make([]byte, 20))
trc := newTimingReadCloser(NopCloserWithSize(r), "test", nil, nil, nil)
for _, tcase := range []struct {
name string
reader io.Reader
expSize int64
expErr error
}{
{
name: "local file",
reader: tmpFile,
expSize: 3,
},
{
name: "bytes buffer",
reader: bbuff,
expSize: 5,
},
{
name: "bytes reader",
reader: breader,
expSize: 100,
},
{
name: "string reader",
reader: sreader,
expSize: 11,
},
{
name: "timing read closer",
reader: trc,
expSize: 20,
},
{
name: "other io.Reader implementation",
reader: &testReader{},
expErr: errors.Errorf("unsupported type of io.Reader: %T", &testReader{}),
},
} {
t.Run(tcase.name, func(t *testing.T) {
size, err := TryToGetSize(tcase.reader)
if tcase.expErr == nil {
testutil.Ok(t, err)
testutil.Equals(t, tcase.expSize, size)
} else {
testutil.Equals(t, tcase.expErr.Error(), err.Error())
}
})
}
}

type testReader struct{}

func (r *testReader) Read([]byte) (n int, err error) { return 0, nil }