From 68858f950497e50caa9f4ab27915fb066a6f4c3b Mon Sep 17 00:00:00 2001 From: Alex Aizman Date: Sun, 8 Sep 2024 15:33:34 -0400 Subject: [PATCH] add write-xid (micro-optimizations) Signed-off-by: Alex Aizman --- ais/htcommon.go | 8 ++++++++ ais/proxy.go | 17 +++++++++-------- ais/prxbsumm.go | 4 +--- ais/prxclu.go | 14 ++++---------- ais/target.go | 4 ++-- ais/tgtetl.go | 4 +--- ais/tgtxact.go | 6 ++---- ext/dsort/handler.go | 3 ++- 8 files changed, 29 insertions(+), 31 deletions(-) diff --git a/ais/htcommon.go b/ais/htcommon.go index 01055d70dc3..b2168750738 100644 --- a/ais/htcommon.go +++ b/ais/htcommon.go @@ -17,6 +17,7 @@ import ( "net/url" "os" rdebug "runtime/debug" + "strconv" "strings" "sync" "syscall" @@ -831,6 +832,13 @@ func apiReqFree(a *apiRequest) { // misc helpers // +// http response: xaction ID most of the time but may be any string +func writeXid(w http.ResponseWriter, xid string) { + debug.Assert(xid != "") + w.Header().Set(cos.HdrContentLength, strconv.Itoa(len(xid))) + w.Write(cos.UnsafeB(xid)) +} + func newBckFromQ(bckName string, query url.Values, dpq *dpq) (*meta.Bck, error) { bck := _bckFromQ(bckName, query, dpq) normp, err := cmn.NormalizeProvider(bck.Provider) diff --git a/ais/proxy.go b/ais/proxy.go index c979fd16520..3179d5189b3 100644 --- a/ais/proxy.go +++ b/ais/proxy.go @@ -954,8 +954,7 @@ func (p *proxy) httpbckdelete(w http.ResponseWriter, r *http.Request, apireq *ap p.writeErr(w, r, err) return } - w.Header().Set(cos.HdrContentLength, strconv.Itoa(len(xid))) - w.Write([]byte(xid)) + writeXid(w, xid) default: p.writeErrAct(w, r, msg.Action) } @@ -1182,8 +1181,7 @@ func (p *proxy) httpbckput(w http.ResponseWriter, r *http.Request) { } xid, err := p.createArchMultiObj(bckFrom, bckTo, msg) if err == nil { - w.Header().Set(cos.HdrContentLength, strconv.Itoa(len(xid))) - w.Write([]byte(xid)) + writeXid(w, xid) } else { p.writeErr(w, r, err) } @@ -1449,8 +1447,7 @@ func (p *proxy) _bckpost(w http.ResponseWriter, r *http.Request, msg *apc.ActMsg } debug.Assertf(xact.IsValidUUID(xid) || strings.IndexByte(xid, ',') > 0, "%q: %q", msg.Action, xid) - w.Header().Set(cos.HdrContentLength, strconv.Itoa(len(xid))) - w.Write([]byte(xid)) + writeXid(w, xid) } // init existing or create remote @@ -1840,7 +1837,9 @@ func (p *proxy) httpobjpost(w http.ResponseWriter, r *http.Request, apireq *apiR p.writeErr(w, r, err) return } - w.Write([]byte(xid)) + if xid != "" { + writeXid(w, xid) + } case apc.ActBlobDl: if err := p.checkAccess(w, r, bck, apc.AccessRW); err != nil { return @@ -2051,7 +2050,9 @@ func (p *proxy) httpbckpatch(w http.ResponseWriter, r *http.Request, apireq *api p.writeErr(w, r, err) return } - w.Write([]byte(xid)) + if xid != "" { + writeXid(w, xid) + } } // HEAD /v1/objects/bucket-name/object-name diff --git a/ais/prxbsumm.go b/ais/prxbsumm.go index f78ff29dfa6..6eb85164c98 100644 --- a/ais/prxbsumm.go +++ b/ais/prxbsumm.go @@ -7,7 +7,6 @@ package ais import ( "net/http" "net/url" - "strconv" "github.com/NVIDIA/aistore/api/apc" "github.com/NVIDIA/aistore/cmn" @@ -32,8 +31,7 @@ func (p *proxy) bsummact(w http.ResponseWriter, r *http.Request, qbck *cmn.Query p.writeErr(w, r, err) } else { w.WriteHeader(http.StatusAccepted) - w.Header().Set(cos.HdrContentLength, strconv.Itoa(len(msg.UUID))) - w.Write([]byte(msg.UUID)) + writeXid(w, msg.UUID) } return } diff --git a/ais/prxclu.go b/ais/prxclu.go index 7bb2b4f990f..63ed9c5e454 100644 --- a/ais/prxclu.go +++ b/ais/prxclu.go @@ -1264,9 +1264,7 @@ func (p *proxy) xstart(w http.ResponseWriter, r *http.Request, msg *apc.ActMsg) smap := p.owner.smap.get() nl := xact.NewXactNL(xargs.ID, xargs.Kind, &smap.Smap, nil) p.ic.registerEqual(regIC{smap: smap, nl: nl}) - - w.Header().Set(cos.HdrContentLength, strconv.Itoa(len(xargs.ID))) - w.Write([]byte(xargs.ID)) + writeXid(w, xargs.ID) } } @@ -1362,9 +1360,7 @@ func (p *proxy) rebalanceCluster(w http.ResponseWriter, r *http.Request, msg *ap p.writeErr(w, r, err) return } - debug.Assert(rmdCtx.rebID != "") - w.Header().Set(cos.HdrContentLength, strconv.Itoa(len(rmdCtx.rebID))) - w.Write([]byte(rmdCtx.rebID)) + writeXid(w, rmdCtx.rebID) } func (p *proxy) sendOwnTbl(w http.ResponseWriter, r *http.Request, msg *apc.ActMsg) { @@ -1509,8 +1505,7 @@ func (p *proxy) rmNode(w http.ResponseWriter, r *http.Request, msg *apc.ActMsg) return } if rebID != "" { - w.Header().Set(cos.HdrContentLength, strconv.Itoa(len(rebID))) - w.Write(cos.UnsafeB(rebID)) + writeXid(w, rebID) } } } @@ -1651,8 +1646,7 @@ func (p *proxy) stopMaintenance(w http.ResponseWriter, r *http.Request, msg *apc return } if rebID != "" { - w.Header().Set(cos.HdrContentLength, strconv.Itoa(len(rebID))) - w.Write(cos.UnsafeB(rebID)) + writeXid(w, rebID) } } diff --git a/ais/target.go b/ais/target.go index 5d51b8e609f..014c5df78d4 100644 --- a/ais/target.go +++ b/ais/target.go @@ -998,8 +998,8 @@ func (t *target) httpobjpost(w http.ResponseWriter, r *http.Request, apireq *api } if xid, _, err = t.blobdl(args, nil /*oa*/); xid != "" { debug.AssertNoErr(err) - w.Header().Set(cos.HdrContentLength, strconv.Itoa(len(xid))) - w.Write([]byte(xid)) + writeXid(w, xid) + // lom is eventually freed by x-blob } default: diff --git a/ais/tgtetl.go b/ais/tgtetl.go index eb62f6cc20c..46f216b5822 100644 --- a/ais/tgtetl.go +++ b/ais/tgtetl.go @@ -8,7 +8,6 @@ import ( "fmt" "net/http" "net/url" - "strconv" "github.com/NVIDIA/aistore/api/apc" "github.com/NVIDIA/aistore/cmn" @@ -196,8 +195,7 @@ func (t *target) healthETL(w http.ResponseWriter, r *http.Request, etlName strin } return } - w.Header().Set(cos.HdrContentLength, strconv.Itoa(len(health))) - w.Write([]byte(health)) + writeXid(w, health) } func (t *target) metricsETL(w http.ResponseWriter, r *http.Request, etlName string) { diff --git a/ais/tgtxact.go b/ais/tgtxact.go index e201432d686..c274eabe970 100644 --- a/ais/tgtxact.go +++ b/ais/tgtxact.go @@ -7,7 +7,6 @@ package ais import ( "fmt" "net/http" - "strconv" "sync" "github.com/NVIDIA/aistore/api/apc" @@ -132,9 +131,8 @@ func (t *target) httpxput(w http.ResponseWriter, r *http.Request) { t.writeErr(w, r, err) return } - if l := len(xid); l > 0 { - w.Header().Set(cos.HdrContentLength, strconv.Itoa(l)) - w.Write([]byte(xid)) + if xid != "" { + writeXid(w, xid) } case apc.ActXactStop: debug.Assert(xact.IsValidKind(xargs.Kind) || xact.IsValidUUID(xargs.ID), xargs.String()) diff --git a/ext/dsort/handler.go b/ext/dsort/handler.go index 1ce45cb240c..f91f34ea4fb 100644 --- a/ext/dsort/handler.go +++ b/ext/dsort/handler.go @@ -102,7 +102,8 @@ func PstartHandler(w http.ResponseWriter, r *http.Request, parsc *ParsedReq) { return } - w.Write([]byte(managerUUID)) + w.Header().Set(cos.HdrContentLength, strconv.Itoa(len(managerUUID))) + w.Write(cos.UnsafeB(managerUUID)) } func _handleResp(w http.ResponseWriter, r *http.Request, smap *meta.Smap, managerUUID string, responses []response) error {