From 953a416a0ac102d8c3aef595b92755746c03a67d Mon Sep 17 00:00:00 2001 From: Alex Aizman Date: Sat, 1 Feb 2025 16:29:24 -0500 Subject: [PATCH] remove redundant make; consolidate call-result; cleanup Signed-off-by: Alex Aizman --- ais/htcommon.go | 70 +++++++++++++++++++++++++++++++++++++++++++++ ais/htrun.go | 75 +------------------------------------------------ cmn/http.go | 14 --------- 3 files changed, 71 insertions(+), 88 deletions(-) diff --git a/ais/htcommon.go b/ais/htcommon.go index e8f8bc78b1e..e8f43581b34 100644 --- a/ais/htcommon.go +++ b/ais/htcommon.go @@ -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) } @@ -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 // //////////////// diff --git a/ais/htrun.go b/ais/htrun.go index 7e8f88657d4..26dec60b8aa 100644 --- a/ais/htrun.go +++ b/ais/htrun.go @@ -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 @@ -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") @@ -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 } @@ -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() -} diff --git a/cmn/http.go b/cmn/http.go index 57e60dab26c..2bdc9e09407 100644 --- a/cmn/http.go +++ b/cmn/http.go @@ -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 {