zhijiangW commented on a change in pull request #7368: [FLINK-10742][network] Let Netty use Flink's buffers directly in credit-based mode URL: https://github.com/apache/flink/pull/7368#discussion_r379993355
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/io/network/netty/DefaultMessageParser.java ########## @@ -0,0 +1,75 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.flink.runtime.io.network.netty; + +import org.apache.flink.shaded.netty4.io.netty.buffer.ByteBuf; +import org.apache.flink.shaded.netty4.io.netty.buffer.ByteBufAllocator; + +import java.net.ProtocolException; + +/** + * The parser for messages without specific parser. It receives the whole + * messages and then delegate the parsing to the targeted messages. + */ +public class DefaultMessageParser implements NettyMessageParser { + + /** The initial size of the message header cumulator buffer. */ + private static final int INITIAL_MESSAGE_HEADER_BUFFER_LENGTH = 128; + + /** The cumulator of message header. */ + private ByteBufCumulator messageCumulator; + + /** The type of messages under processing. */ + private int msgId = -1; + + /** The length of messages under processing. */ + private int messageLength; + + @Override + public void onChannelActive(ByteBufAllocator alloc) { + messageCumulator = new ByteBufCumulator(alloc, INITIAL_MESSAGE_HEADER_BUFFER_LENGTH); + } + + @Override + public void startParsingMessage(int msgId, int messageLength) { + this.msgId = msgId; + this.messageLength = messageLength; + } + + @Override + public ParseResult onData(ByteBuf data) throws Exception { + ByteBuf toDecode = messageCumulator.cumulate(data, messageLength); + + if (toDecode == null) { + return ParseResult.notFinished(); + } + + switch (msgId) { + case NettyMessage.ErrorResponse.ID: Review comment: I guess it is not necessary to judge the message id here, otherwise we also need to touch this place when introducing new message type future. If removing this, we can also simplify the `startParsingMessage` and reduce the class-level `msgId` field. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services