Skip to content

Commit

Permalink
remove redundant make; consolidate call-result; cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Alex Aizman <[email protected]>
  • Loading branch information
alex-aizman committed Feb 1, 2025
1 parent 3583494 commit 953a416
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 88 deletions.
70 changes: 70 additions & 0 deletions ais/htcommon.go
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,10 @@ func freeBcastRes(results sliceResults) {
resultsPool.Put(&results)
}

////////////////
// callResult //
////////////////

func (res *callResult) read(body io.Reader, size int64) {
res.bytes, res.err = cos.ReadAllN(body, size)
}
Expand All @@ -409,6 +413,72 @@ func (res *callResult) mread(body io.Reader) {
slab.Free(buf)
}

//
// error helpers for intra-cluster calls
//

func (res *callResult) unwrap() (err error) {
err = errors.Unwrap(res.err)
if err == nil {
err = res.err
}
return
}

func (res *callResult) toErr() error {
if res.err == nil {
return nil
}
// is cmn.ErrHTTP
if herr := cmn.Err2HTTPErr(res.err); herr != nil {
// add status, details
if res.status >= http.StatusBadRequest {
herr.Status = res.status
}
if herr.Message == "" {
herr.Message = res.details
}
return herr
}
// res => cmn.ErrHTTP
if res.status >= http.StatusBadRequest {
var detail string
if res.details != "" {
detail = "[" + res.details + "]"
}
return res.herr(nil, fmt.Sprintf("%v%s", res.err, detail))
}
if res.details == "" {
return res.err
}
return cmn.NewErrFailedTo(nil, "call "+res.si.StringEx(), res.details, res.err)
}

func (res *callResult) herr(r *http.Request, msg string) *cmn.ErrHTTP {
orig := &cmn.ErrHTTP{}
if e := jsoniter.Unmarshal([]byte(msg), orig); e == nil {
return orig
}
nherr := cmn.NewErrHTTP(r, errors.New(msg), res.status)
if res.si != nil {
nherr.Node = res.si.StringEx()
}
return nherr
}

func (res *callResult) errorf(format string, a ...any) error {
debug.Assert(res.err != nil)
// add formatted
msg := fmt.Sprintf(format, a...)
if herr := cmn.Err2HTTPErr(res.err); herr != nil {
herr.Message = msg + ": " + herr.Message
res.err = herr
} else {
res.err = errors.New(msg + ": " + res.err.Error())
}
return res.toErr()
}

////////////////
// nlogWriter //
////////////////
Expand Down
75 changes: 1 addition & 74 deletions ais/htrun.go
Original file line number Diff line number Diff line change
Expand Up @@ -629,8 +629,6 @@ func (h *htrun) _call(si *meta.Snode, bargs *bcastArgs, results *bcastResults) {
freeCargs(cargs)
}

const lenhdr = 5

func (h *htrun) call(args *callArgs, smap *smapX) (res *callResult) {
var (
req *http.Request
Expand Down Expand Up @@ -690,9 +688,6 @@ func (h *htrun) call(args *callArgs, smap *smapX) (res *callResult) {
}

// req header
if args.req.Header == nil {
args.req.Header = make(http.Header, lenhdr)
}
if smap.vstr != "" {
if smap.IsPrimary(h.si) {
req.Header.Set(apc.HdrCallerIsPrimary, "true")
Expand Down Expand Up @@ -2046,7 +2041,7 @@ func (h *htrun) fastKalive(smap *smapX, timeout time.Duration, ecActive bool) (s
}
if ecActive {
// (target => primary)
hdr := make(http.Header, lenhdr)
hdr := make(http.Header, 1)
hdr.Set(apc.HdrActiveEC, "true")
cargs.req.Header = hdr
}
Expand Down Expand Up @@ -2417,71 +2412,3 @@ func (h *htrun) _status(smap *smapX) (daeStatus string) {
}
return
}

////////////////
// callResult //
////////////////

// error helpers for intra-cluster calls

func (res *callResult) unwrap() (err error) {
err = errors.Unwrap(res.err)
if err == nil {
err = res.err
}
return
}

func (res *callResult) toErr() error {
if res.err == nil {
return nil
}
// is cmn.ErrHTTP
if herr := cmn.Err2HTTPErr(res.err); herr != nil {
// add status, details
if res.status >= http.StatusBadRequest {
herr.Status = res.status
}
if herr.Message == "" {
herr.Message = res.details
}
return herr
}
// res => cmn.ErrHTTP
if res.status >= http.StatusBadRequest {
var detail string
if res.details != "" {
detail = "[" + res.details + "]"
}
return res.herr(nil, fmt.Sprintf("%v%s", res.err, detail))
}
if res.details == "" {
return res.err
}
return cmn.NewErrFailedTo(nil, "call "+res.si.StringEx(), res.details, res.err)
}

func (res *callResult) herr(r *http.Request, msg string) *cmn.ErrHTTP {
orig := &cmn.ErrHTTP{}
if e := jsoniter.Unmarshal([]byte(msg), orig); e == nil {
return orig
}
nherr := cmn.NewErrHTTP(r, errors.New(msg), res.status)
if res.si != nil {
nherr.Node = res.si.StringEx()
}
return nherr
}

func (res *callResult) errorf(format string, a ...any) error {
debug.Assert(res.err != nil)
// add formatted
msg := fmt.Sprintf(format, a...)
if herr := cmn.Err2HTTPErr(res.err); herr != nil {
herr.Message = msg + ": " + herr.Message
res.err = herr
} else {
res.err = errors.New(msg + ": " + res.err.Error())
}
return res.toErr()
}
14 changes: 0 additions & 14 deletions cmn/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,20 +324,6 @@ func (u *HreqArgs) Req() (*http.Request, error) {
return req, nil
}

// ReqWithCancel creates request with ability to cancel it.
func (u *HreqArgs) ReqWithCancel() (*http.Request, context.Context, context.CancelFunc, error) {
req, err := u.Req()
if err != nil {
return nil, nil, nil, err
}
if u.Method == http.MethodPost || u.Method == http.MethodPut {
req.Header.Set(cos.HdrContentType, cos.ContentJSON)
}
ctx, cancel := context.WithCancel(context.Background())
req = req.WithContext(ctx)
return req, ctx, cancel, nil
}

func (u *HreqArgs) ReqWithTimeout(timeout time.Duration) (*http.Request, context.Context, context.CancelFunc, error) {
req, err := u.Req()
if err != nil {
Expand Down

0 comments on commit 953a416

Please sign in to comment.