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

Profile base to dev #352

Merged
merged 50 commits into from
Dec 26, 2023
Merged

Profile base to dev #352

merged 50 commits into from
Dec 26, 2023

Conversation

snail007
Copy link
Collaborator

@snail007 snail007 commented Dec 6, 2023

No description provided.

snail007 and others added 30 commits November 28, 2023 15:02
* Profile base bytesbuffer (#326)

* add WriteString to BytesBuffer

* Encode WriteString

* Profile base object reuse (#327)

* reuse bytesbuffer & stream

* Optimise StringMap Range (#328)

* Profile base object reuse (#332)

* reuse bytesbuffer & stream
* Profile base add metric (#338)
* Optimise filter addMetric
* Profile base findcluster (#340)
* Profile base findcluster (#337)
* Profile base do acccesslog (#339)
* bugfix: BytesBuffer.Reset (#334)
* Access log (#336)
* Profile base do accesslog (#343)
 Accesslogv2 (#342)
* Profile base preview escape (#345)
* optimise Escape
* Profile base timenow (#344) (#347)
* Profile base do accesslog (#343)
* update: log/bytesbuffer (#341)
* bytesBuffer release
* Accesslogv2 (#346)
* update: log/bytesbuffer
* memory copy & bytesBuffer resue
agent.go Outdated
@@ -437,7 +442,7 @@ func (a *Agent) initHTTPClusters() {
}
httpCluster := cluster.NewHTTPCluster(url, true, a.Context, a.extFactory)
if httpCluster == nil {
vlog.Errorf("Create http cluster %s failed", id)
vlog.Errorf("Create http cluster %s failed", id)
Copy link
Contributor

Choose a reason for hiding this comment

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

agent.go Outdated
@@ -490,7 +490,7 @@ func (a *Agent) reloadClusters(ctx *motan.Context) {
}

service := url.Path
mapKey := getClusterKey(url.Group, url.GetStringParamsWithDefault(motan.VersionKey, "0.1"), url.Protocol, url.Path)
mapKey := getClusterKey(url.Group, url.GetStringParamsWithDefault(motan.VersionKey, "1.0"), url.Protocol, url.Path)
Copy link
Contributor

Choose a reason for hiding this comment

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

默认版本号改为全局常量(包含其他相关处)

if motanReq, ok := mreq.(*motan.MotanRequest); ok {
motan.ReleaseMotanRequest(motanReq)
}
if motanResp, ok := mres.(*motan.MotanResponse); ok {
Copy link
Contributor

Choose a reason for hiding this comment

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

对mres的HttpMotanResponse类型判断放在else中,不需要重复判断两次

agent_test.go Outdated
{"test", "g1", "http", "1.3", "No refers for request"},
{"test", "g1", "http", "1.2", "less condition to select cluster"},
{"test", "b", "c", "d", "no cluster matches the request"},
// 同一个service有多个cluster可以匹配,但是group没有传
Copy link
Contributor

Choose a reason for hiding this comment

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

改成英文注释吧

core/bytes.go Outdated
if bb.buf == nil {
bb.buf = make([]byte, initSize)
}
if bb.temp == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

默认属性(temp)的初始化都放到池构造func中,不用对每个buf都做判空了。buf是因为涉及到初始大小的问题,与此方法的入参相关,可以放在这里

@@ -433,23 +448,27 @@ func MotanRequestToFasthttpRequest(motanRequest core.Request, fasthttpRequest *f
motanRequest.GetAttachments().Range(func(k, v string) bool {
// ignore some specified key
for _, attachmentKey := range httpProxySpecifiedAttachments {
if strings.EqualFold(k, attachmentKey) {
if k == attachmentKey {
Copy link
Contributor

Choose a reason for hiding this comment

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

优化匹配逻辑,命中概率大的放在前面快速终止流程。比如InnerAttachmentsConvertMap的匹配

@@ -501,12 +520,13 @@ func FasthttpResponseToMotanResponse(motanResponse core.Response, fasthttpRespon
fasthttpResponse.Header.VisitAll(func(k, v []byte) {
motanResponse.SetAttachment(string(k), string(v))
})
if resp, ok := motanResponse.(*core.MotanResponse); ok {
if resp, ok := motanResponse.(*core.HttpMotanResponse); ok {
httpResponseBody := fasthttpResponse.Body()
if httpResponseBody != nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

没有必要判空

log/bytes.go Outdated

func acquireBytesBuffer() *innerBytesBuffer {
b := innerBytesBufferPool.Get()
if b == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

不会为nil

log/log.go Outdated
@@ -390,12 +391,13 @@ func (d *defaultLogger) doAccessLog(logObject *AccessLogEntity) {
zap.String("exception", logObject.Exception),
zap.String("upstreamCode", logObject.UpstreamCode))
} else {
var buffer bytes.Buffer
buffer := newInnerBytesBuffer()
Copy link
Contributor

Choose a reason for hiding this comment

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

改用acquireBytesBuffer,显示使用池化buffer

log/log.go Outdated

func AcquireAccessLogEntity() *AccessLogEntity {
v := accessLogEntityPool.Get()
if v == nil {
Copy link
Contributor

Choose a reason for hiding this comment

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

无效分支

s.channel.heartbeatLock.Unlock()
} else {
s.channel.streamLock.Lock()
delete(s.channel.streams, s.streamId)
if _, exist = s.channel.heartbeats[s.streamId]; exist {
Copy link
Contributor

Choose a reason for hiding this comment

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

remove的不对

default:
}
//use atomic.Swap avoid data race between goroutines(Channel.Call and Stream.notify) when ReleaseStream
atomic.SwapPointer((*unsafe.Pointer)(unsafe.Pointer(&s.channel)), nil)
Copy link
Contributor

Choose a reason for hiding this comment

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

channel重置了多次;
res需要重置,避免长时间持有大对象;

select {
case <-s.recvNotifyCh:
msg := s.res
if msg == nil {
return nil, errors.New("recv err: recvMsg is nil")
}
return msg, nil
case <-timer.C:
case <-s.timer.C:
s.release = false
Copy link
Contributor

Choose a reason for hiding this comment

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

需要在注释解释stream的release机制,几个关键点:stream重置后初值默认进行回收;Channel.Call是唯一回收点;当超时等特殊情况,回收状态需要由remove重新决定

// DecodeWithTime the parameter rs is a slice pointer, so the
// extension of *rs will affect the slice outside in order to
// reuse the readSlice which is used by reading header info from buf
func DecodeWithTime(buf *bufio.Reader, rs *[]byte, maxContentLength int) (msg *Message, start time.Time, err error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

rs参数命名调整。tempBuf之类的

metabuf := motan.NewBytesBuffer(256)
// EncodeWithoutBody encode message expect Message.Body
// unexpected if call Message.GetEncodedBytes after rewrite result(*motan.BytesBuffer)
func (msg *Message) EncodeWithoutBody() *motan.BytesBuffer {
Copy link
Contributor

Choose a reason for hiding this comment

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

不用和Encode方法进行区分,不用显示表明body处理方式,由Encode方法不再返回结果,只能使用GetEncodedBytes方法强制限定使用姿势

msg.Metadata.Range(func(k, v string) bool {
if k == "" || v == "" {
return true
if VerifyMeta(k, v) {
Copy link
Contributor

Choose a reason for hiding this comment

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

为啥拆分开?

func DecodeWithTime(buf *bufio.Reader, maxContentLength int) (msg *Message, start time.Time, err error) {
temp := make([]byte, HeaderLength, HeaderLength)

func DecodeWithoutBody(buf *bufio.Reader, rs *[]byte, maxContentLength int) (msg *Message, bodySize int, err error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

不需要单独拆分方法,这个方法没有复用的场景,也不会进行内联

@@ -660,11 +740,15 @@ func ConvertToResMessage(response motan.Response, serialize motan.Serialization)
res.Body = b
} else {
vlog.Warningf("convert response value fail! serialized value not []byte. res:%+v", response)
res.SetAlreadySend()
Copy link
Contributor

Choose a reason for hiding this comment

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

alreadySend改为canRelease

@@ -140,6 +140,20 @@ func (h *HTTPProvider) SetContext(context *motan.Context) {
h.gctx = context
}

// Rewrite do rewrite
func (h *HTTPProvider) Rewrite(httpReq *fasthttp.Request, request motan.Request) (string, error) {
Copy link
Contributor

Choose a reason for hiding this comment

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

改为私有方法

return resp
}
resp.RequestID = request.GetRequestID()
Copy link
Contributor

Choose a reason for hiding this comment

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

给response赋值requestid的语句呢?

//do rewrite
rewritePath := request.GetMethod()
var err error
if h.enableRewrite {
Copy link
Contributor

Choose a reason for hiding this comment

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

f h.enableRewrite 的判定可以放到Rewrite方法中

err := mhttp.MotanRequestToFasthttpRequest(request, httpReq, h.defaultHTTPMethod)
if err != nil {
fillExceptionWithCode(resp, http.StatusBadRequest, t, err)
if h.enableHttpException {
Copy link
Contributor

Choose a reason for hiding this comment

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

判定条件合并。下同

@snail007 snail007 merged commit ecfa6a0 into dev Dec 26, 2023
@rayzhang0603 rayzhang0603 deleted the profile_base branch April 7, 2024 03:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants