Skip to content

DecodeableRpcResult解码参数丢失#6590

Merged
AlbumenJ merged 1 commit intoapache:masterfrom
XiaoWeiKIN:fix-rpcResult
Mar 14, 2021
Merged

DecodeableRpcResult解码参数丢失#6590
AlbumenJ merged 1 commit intoapache:masterfrom
XiaoWeiKIN:fix-rpcResult

Conversation

@XiaoWeiKIN
Copy link
Copy Markdown
Contributor

  • Dubbo version: 2.7.8
public class DecodeableRpcResult{

public void setObjectAttachments(Map<String, Object> map) {
        this.attachments = map == null ? new HashMap<>() : map;
    }
}

DecodeableRpcResult解码的时候使用setObjectAttachments会丢失已经在IO线程解码的部分参数,例如input,output。

该方法应该替换为

  public void addObjectAttachments(Map<String, Object> map) {
        if (map == null) {
            return;
        }
        if (this.attachments == null) {
            this.attachments = new HashMap<>();
        }
        this.attachments.putAll(map);
    }

@chickenlj
Copy link
Copy Markdown
Contributor

I think this is the first and only place trying to update Attachments, can u list the place that Attachments is possibly changed in the IO thread?

@XiaoWeiKIN
Copy link
Copy Markdown
Contributor Author

public class DubboCountCodec{
public Object decode(Channel channel, ChannelBuffer buffer) throws IOException {
        int save = buffer.readerIndex();
        MultiMessage result = MultiMessage.create();
        do {
            Object obj = codec.decode(channel, buffer);
            if (Codec2.DecodeResult.NEED_MORE_INPUT == obj) {
                buffer.readerIndex(save);
                break;
            } else {
                result.addMessage(obj);
                logMessageLength(obj, buffer.readerIndex() - save);
                save = buffer.readerIndex();
            }
        } while (true);
        if (result.isEmpty()) {
            return Codec2.DecodeResult.NEED_MORE_INPUT;
        }
        if (result.size() == 1) {
            return result.get(0);
        }
        return result;
    }

    private void logMessageLength(Object result, int bytes) {
        if (bytes <= 0) {
            return;
        }
        if (result instanceof Request) {
            try {
                ((RpcInvocation) ((Request) result).getData()).setAttachment(INPUT_KEY, String.valueOf(bytes));
            } catch (Throwable e) {
                /* ignore */
            }
        } else if (result instanceof Response) {
            try {
                ((AppResponse) ((Response) result).getResult()).setAttachment(OUTPUT_KEY, String.valueOf(bytes));
            } catch (Throwable e) {
                /* ignore */
            }
        }
    }
}

IO线程解码会添加参数,业务线程解码会导致覆盖

@XiaoWeiKIN
Copy link
Copy Markdown
Contributor Author

@chickenlj

@XiaoWeiKIN
Copy link
Copy Markdown
Contributor Author

INPUT_KEY 和OUTPUT_KEY 在IO线程解码后,DecodeableRpcResult在业务线程解码body导致INPUT_KEY 和OUTPUT_KEY参数丢失

Copy link
Copy Markdown
Member

@AlbumenJ AlbumenJ left a comment

Choose a reason for hiding this comment

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

LGTM.

@AlbumenJ AlbumenJ merged commit 6104ef1 into apache:master Mar 14, 2021
AlbumenJ added a commit to AlbumenJ/dubbo that referenced this pull request May 27, 2021
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.

3 participants