Skip to content

Commit

Permalink
Refactor: rename doXXX() (#1878)
Browse files Browse the repository at this point in the history
* rename pool/doGet to pool/getHelthyConn

Signed-off-by: kevindiu <kevindiujp@gmail.com>

* rename doXXX()

Signed-off-by: kevindiu <kevindiujp@gmail.com>

* rename writeFileSync to writeFileCtx

Signed-off-by: kevindiu <kevindiujp@gmail.com>

* rename writeFileCtx to writeFileWithContext

Signed-off-by: kevindiu <kevindiujp@gmail.com>

Signed-off-by: kevindiu <kevindiujp@gmail.com>
Co-authored-by: Yusuke Kato <kpango@vdaas.org>
  • Loading branch information
kevindiu and kpango authored Dec 5, 2022
1 parent 9bb7508 commit 92682d6
Show file tree
Hide file tree
Showing 10 changed files with 70 additions and 155 deletions.
8 changes: 4 additions & 4 deletions hack/benchmark/assets/x1b/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ type ivecs struct {
*file
}

func doOpen(fname string, elementSize int) (f *file, err error) {
func openFile(fname string, elementSize int) (f *file, err error) {
fp, err := os.Open(fname)
if err != nil {
return nil, err
Expand Down Expand Up @@ -164,23 +164,23 @@ func (iv *ivecs) Load(i int) (interface{}, error) {
}

func NewUint8Vectors(fname string) (Uint8Vectors, error) {
f, err := doOpen(fname, 1)
f, err := openFile(fname, 1)
if err != nil {
return nil, err
}
return &bvecs{f}, nil
}

func NewFloatVectors(fname string) (FloatVectors, error) {
f, err := doOpen(fname, 4)
f, err := openFile(fname, 4)
if err != nil {
return nil, err
}
return &fvecs{f}, nil
}

func NewInt32Vectors(fname string) (Int32Vectors, error) {
f, err := doOpen(fname, 4)
f, err := openFile(fname, 4)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions hack/benchmark/assets/x1b/loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import (
"github.com/vdaas/vald/internal/test/goleak"
)

func Test_doOpen(t *testing.T) {
func Test_openFile(t *testing.T) {
t.Parallel()
type args struct {
fname string
Expand Down Expand Up @@ -94,7 +94,7 @@ func Test_doOpen(t *testing.T) {
checkFunc = defaultCheckFunc
}

gotF, err := doOpen(test.args.fname, test.args.elementSize)
gotF, err := openFile(test.args.fname, test.args.elementSize)
if err := checkFunc(test.want, gotF, err); err != nil {
tt.Errorf("error = %v", err)
}
Expand Down
59 changes: 27 additions & 32 deletions internal/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func doMoveDir(ctx context.Context, src, dst string, rollback bool) (err error)
if len(src) == 0 || len(dst) == 0 || src == dst {
return nil
}
exits, fi, err := doExists(src)
exits, fi, err := ExistsWithDetail(src)
if !exits || fi == nil || !fi.IsDir() || err != nil {
return errors.ErrDirectoryNotFound(err, src, fi)
}
Expand All @@ -105,7 +105,7 @@ func doMoveDir(ctx context.Context, src, dst string, rollback bool) (err error)
if err != nil {
log.Debug(errors.ErrFailedToRenameDir(err, src, dst, nil, nil))
var tmpPath string
exits, fi, err := doExists(dst)
exits, fi, err := ExistsWithDetail(dst)
if exits && fi.IsDir() && err == nil {
tmpPath = Join(filepath.Dir(dst), "tmp-"+strconv.FormatInt(fastime.UnixNanoNow(), 10))
_ = os.RemoveAll(tmpPath)
Expand All @@ -131,7 +131,7 @@ func doMoveDir(ctx context.Context, src, dst string, rollback bool) (err error)
}
log.Debugf("directory %s successfully moved to tmp location %s", dst, tmpPath)
}
exits, fi, err = doExists(src)
exits, fi, err = ExistsWithDetail(src)
if exits && fi != nil && fi.IsDir() && err == nil {
err = os.Rename(src, dst)
if err != nil {
Expand Down Expand Up @@ -223,7 +223,7 @@ func CopyFileWithPerm(ctx context.Context, src, dst string, perm fs.FileMode) (n
}
}()

exist, fi, err := doExists(src)
exist, fi, err := ExistsWithDetail(src)
switch {
case !exist, fi == nil, fi.Size() == 0, fi.IsDir():
return 0, errors.Wrap(err, errors.ErrFileNotFound(src).Error())
Expand Down Expand Up @@ -258,23 +258,23 @@ func CopyFileWithPerm(ctx context.Context, src, dst string, perm fs.FileMode) (n
}

func WriteFile(ctx context.Context, target string, r io.Reader, perm fs.FileMode) (n int64, err error) {
return doWriteFile(ctx, target, r, os.O_CREATE|os.O_WRONLY|os.O_SYNC, perm)
return writeFileWithContext(ctx, target, r, os.O_CREATE|os.O_WRONLY|os.O_SYNC, perm)
}

func OverWriteFile(ctx context.Context, target string, r io.Reader, perm fs.FileMode) (n int64, err error) {
return doWriteFile(ctx, target, r, os.O_CREATE|os.O_TRUNC|os.O_WRONLY|os.O_SYNC, perm)
return writeFileWithContext(ctx, target, r, os.O_CREATE|os.O_TRUNC|os.O_WRONLY|os.O_SYNC, perm)
}

func AppendFile(ctx context.Context, target string, r io.Reader, perm fs.FileMode) (n int64, err error) {
return doWriteFile(ctx, target, r, os.O_CREATE|os.O_APPEND|os.O_RDWR|os.O_SYNC, perm)
return writeFileWithContext(ctx, target, r, os.O_CREATE|os.O_APPEND|os.O_RDWR|os.O_SYNC, perm)
}

func doWriteFile(ctx context.Context, target string, r io.Reader, flg int, perm fs.FileMode) (n int64, err error) {
func writeFileWithContext(ctx context.Context, target string, r io.Reader, flg int, perm fs.FileMode) (n int64, err error) {
if len(target) == 0 || r == nil {
return 0, nil
}

exist, fi, err := doExists(target)
exist, fi, err := ExistsWithDetail(target)
switch {
case err == nil, exist, fi != nil && fi.Size() != 0, fi != nil && fi.IsDir():
err = errors.ErrFileAlreadyExists(target)
Expand Down Expand Up @@ -359,13 +359,23 @@ func ReadFile(path string) (n []byte, err error) {

// Exists returns file existence
func Exists(path string) (e bool) {
e, _, _ = doExists(path)
e, _, _ = ExistsWithDetail(path)
return e
}

// ExistsWithDetail returns file existence
// ExistsWithDetail returns file existence with detailed information
func ExistsWithDetail(path string) (e bool, fi fs.FileInfo, err error) {
return doExists(path)
fi, err = os.Stat(path)
if err != nil {
if os.IsExist(err) {
return true, fi, nil
}
if os.IsNotExist(err) {
return false, fi, err
}
return false, fi, err
}
return true, fi, nil
}

// MkdirAll creates directory like mkdir -p
Expand All @@ -375,7 +385,7 @@ func MkdirAll(path string, perm fs.FileMode) (err error) {
fi fs.FileInfo
merr, rerr error
)
exist, fi, err = doExists(path)
exist, fi, err = ExistsWithDetail(path)
if exist {
if err == nil && fi != nil && fi.IsDir() {
return nil
Expand Down Expand Up @@ -447,24 +457,9 @@ func CreateTemp(baseDir string) (f *os.File, err error) {
return nil, errors.ErrFailedToCreateFile(err, path, nil)
}

// doExists returns file existence with detailed information
func doExists(path string) (exists bool, fi fs.FileInfo, err error) {
fi, err = os.Stat(path)
if err != nil {
if os.IsExist(err) {
return true, fi, nil
}
if os.IsNotExist(err) {
return false, fi, err
}
return false, fi, err
}
return true, fi, nil
}

// ListInDir returns file list in directory
func ListInDir(path string) ([]string, error) {
exists, fi, err := doExists(path)
exists, fi, err := ExistsWithDetail(path)
if !exists {
return nil, err
}
Expand All @@ -484,7 +479,7 @@ func Join(paths ...string) (path string) {
return ""
}
if len(paths) > 1 {
path = doJoin(paths...)
path = joinFilePaths(paths...)
} else {
path = replacer.Replace(paths[0])
}
Expand All @@ -498,7 +493,7 @@ func Join(paths ...string) (path string) {
log.Warn(err)
return filepath.Clean(path)
}
return filepath.Clean(doJoin(root, path))
return filepath.Clean(joinFilePaths(root, path))
}

var replacer = strings.NewReplacer(
Expand All @@ -508,7 +503,7 @@ var replacer = strings.NewReplacer(
string(os.PathSeparator),
)

func doJoin(paths ...string) (path string) {
func joinFilePaths(paths ...string) (path string) {
for i, path := range paths {
if path != "" {
return replacer.Replace(strings.Join(paths[i:], string(os.PathSeparator)))
Expand Down
88 changes: 4 additions & 84 deletions internal/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -484,86 +484,6 @@ func TestExistsWithDetail(t *testing.T) {
}
}

func Test_doExists(t *testing.T) {
type args struct {
path string
}
type want struct {
wantExists bool
wantFi fs.FileInfo
err error
}
type test struct {
name string
args args
want want
checkFunc func(want, bool, fs.FileInfo, error) error
beforeFunc func(args)
afterFunc func(args)
}
defaultCheckFunc := func(w want, gotExists bool, gotFi fs.FileInfo, err error) error {
if !errors.Is(err, w.err) {
return errors.Errorf("got_error: \"%#v\",\n\t\t\t\twant: \"%#v\"", err, w.err)
}
if !reflect.DeepEqual(gotExists, w.wantExists) {
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotExists, w.wantExists)
}
if !reflect.DeepEqual(gotFi, w.wantFi) {
return errors.Errorf("got: \"%#v\",\n\t\t\t\twant: \"%#v\"", gotFi, w.wantFi)
}
return nil
}
tests := []test{
// TODO test cases
/*
{
name: "test_case_1",
args: args {
path: "",
},
want: want{},
checkFunc: defaultCheckFunc,
},
*/

// TODO test cases
/*
func() test {
return test {
name: "test_case_2",
args: args {
path: "",
},
want: want{},
checkFunc: defaultCheckFunc,
}
}(),
*/
}

for _, tc := range tests {
test := tc
t.Run(test.name, func(tt *testing.T) {
tt.Parallel()
defer goleak.VerifyNone(tt, goleak.IgnoreCurrent())
if test.beforeFunc != nil {
test.beforeFunc(test.args)
}
if test.afterFunc != nil {
defer test.afterFunc(test.args)
}
if test.checkFunc == nil {
test.checkFunc = defaultCheckFunc
}

gotExists, gotFi, err := doExists(test.args.path)
if err := test.checkFunc(test.want, gotExists, gotFi, err); err != nil {
tt.Errorf("error = %v", err)
}
})
}
}

func TestListInDir(t *testing.T) {
type args struct {
path string
Expand Down Expand Up @@ -1122,7 +1042,7 @@ func TestJoin(t *testing.T) {
}
}

func Test_doJoin(t *testing.T) {
func Test_joinFilePaths(t *testing.T) {
type args struct {
paths []string
}
Expand Down Expand Up @@ -1187,7 +1107,7 @@ func Test_doJoin(t *testing.T) {
checkFunc = defaultCheckFunc
}

gotPath := doJoin(test.args.paths...)
gotPath := joinFilePaths(test.args.paths...)
if err := checkFunc(test.want, gotPath); err != nil {
tt.Errorf("error = %v", err)
}
Expand Down Expand Up @@ -1606,7 +1526,7 @@ func TestAppendFile(t *testing.T) {
}
}

func Test_doWriteFile(t *testing.T) {
func Test_writeFileWithContext(t *testing.T) {
type args struct {
ctx context.Context
target string
Expand Down Expand Up @@ -1687,7 +1607,7 @@ func Test_doWriteFile(t *testing.T) {
checkFunc = defaultCheckFunc
}

gotN, err := doWriteFile(test.args.ctx, test.args.target, test.args.r, test.args.flg, test.args.perm)
gotN, err := writeFileWithContext(test.args.ctx, test.args.target, test.args.r, test.args.flg, test.args.perm)
if err := checkFunc(test.want, gotN, err); err != nil {
tt.Errorf("error = %v", err)
}
Expand Down
14 changes: 7 additions & 7 deletions internal/net/grpc/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ func (g *gRPCClient) Range(ctx context.Context,
case <-ctx.Done():
return false
default:
g.doConnect(ssctx, p, addr, true, func(ictx context.Context,
g.connectWithBackoff(ssctx, p, addr, true, func(ictx context.Context,
conn *ClientConn, copts ...CallOption,
) (interface{}, error) {
return nil, f(ictx, addr, conn, copts...)
Expand Down Expand Up @@ -360,7 +360,7 @@ func (g *gRPCClient) RangeConcurrent(ctx context.Context,
case <-egctx.Done():
return nil
default:
g.doConnect(ssctx, p, addr, true, func(ictx context.Context,
g.connectWithBackoff(ssctx, p, addr, true, func(ictx context.Context,
conn *ClientConn, copts ...CallOption,
) (interface{}, error) {
return nil, f(ictx, addr, conn, copts...)
Expand Down Expand Up @@ -403,7 +403,7 @@ func (g *gRPCClient) OrderedRange(ctx context.Context,
span.End()
}
}()
g.doConnect(ssctx, p, addr, true, func(ictx context.Context,
g.connectWithBackoff(ssctx, p, addr, true, func(ictx context.Context,
conn *ClientConn, copts ...CallOption,
) (interface{}, error) {
return nil, f(ictx, addr, conn, copts...)
Expand Down Expand Up @@ -447,7 +447,7 @@ func (g *gRPCClient) OrderedRangeConcurrent(ctx context.Context,
case <-egctx.Done():
return nil
default:
g.doConnect(ssctx, p, addr, true, func(ictx context.Context,
g.connectWithBackoff(ssctx, p, addr, true, func(ictx context.Context,
conn *ClientConn, copts ...CallOption,
) (interface{}, error) {
return nil, f(ictx, addr, conn, copts...)
Expand Down Expand Up @@ -475,7 +475,7 @@ func (g *gRPCClient) RoundRobin(ctx context.Context, f func(ctx context.Context,
sctx = backoff.WithBackoffName(ctx, boName)
}
do := func(ctx context.Context, p pool.Conn, addr string, f func(ctx context.Context, conn *ClientConn, copts ...CallOption) (interface{}, error)) (r interface{}, ret bool, err error) {
r, err = g.doConnect(ctx, p, addr, false, f)
r, err = g.connectWithBackoff(ctx, p, addr, false, f)
if err != nil {
st, ok := status.FromError(err)
if !ok || st == nil {
Expand Down Expand Up @@ -555,10 +555,10 @@ func (g *gRPCClient) Do(ctx context.Context, addr string,
log.Warnf("gRPCClient.Do operation failed, grpc pool connection for %s is invalid,\terror: %v", addr, err)
return nil, err
}
return g.doConnect(sctx, p, addr, true, f)
return g.connectWithBackoff(sctx, p, addr, true, f)
}

func (g *gRPCClient) doConnect(ctx context.Context, p pool.Conn, addr string, enableBackoff bool,
func (g *gRPCClient) connectWithBackoff(ctx context.Context, p pool.Conn, addr string, enableBackoff bool,
f func(ctx context.Context,
conn *ClientConn, copts ...CallOption) (interface{}, error),
) (data interface{}, err error) {
Expand Down
Loading

0 comments on commit 92682d6

Please sign in to comment.