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

[ISSUE #4701]Fix use tcp protocol client send message, it throw a DecoderException #4702

Merged
merged 7 commits into from
Jan 10, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
optimize code
  • Loading branch information
mxsm committed Jan 7, 2024
commit 413f44bda302e2c1942a3a60731da21179f9afb2
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class Codec {

Check warning on line 45 in eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java#L45

Added line #L45 was not covered by tests

private static final int FRAME_MAX_LENGTH = 1024 * 1024 * 4;

Expand Down Expand Up @@ -104,7 +104,7 @@
public static class Decoder extends LengthFieldBasedFrameDecoder {

public Decoder() {
super(FRAME_MAX_LENGTH, 13, 4, -9, 0);
super(FRAME_MAX_LENGTH, PREFIX_LENGTH, Integer.BYTES, -9, 0);
Copy link
Member

Choose a reason for hiding this comment

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

I believe Integer.BYTES and -9 are still not clear enough for developers. Integer.BYTES: The lengths of version field, package length field, and header length field are all Integer.BYTES. -9: Hard-coded.

If you disagree with replacing them with expressions composed of more descriptive constant or variable names, as mentioned earlier, you can provide detailed comments to explain the usage of Integer.BYTES and -9 here.

我认为Integer.BYTES-9对开发者来说依旧不够清晰。Integer.BYTES:Version字段, package length字段, header length字段的长度都是Integer.BYTES-9:硬编码。
如果您不赞同将它们换成有助理解的常量名或变量名组成的表达式,正如我之前所说,您可以用详细的注释来说明这里的Integer.BYTES-9

Copy link
Member

Choose a reason for hiding this comment

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

@mxsm I'm curious to understand the thought processes behind choosing a hard-coded value over a dynamic value. I'm wondering about potential future flexibility. Have we considered scenarios where the header structure might evolve, and how a dynamic approach might handle those cases?

Copy link
Member Author

Choose a reason for hiding this comment

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

@mxsm I'm curious to understand the thought processes behind choosing a hard-coded value over a dynamic value. I'm wondering about potential future flexibility. Have we considered scenarios where the header structure might evolve, and how a dynamic approach might handle those cases?

@harshithasudhakar For a custom protocol, once it is determined, it will not undergo significant changes for a considerable period of time. This is because any changes to the protocol structure may introduce compatibility issues, potentially requiring existing users to perform a comprehensive upgrade to resolve them. In such cases, a major version upgrade, such as upgrading to version 2.x, may be necessary to undertake protocol modifications. Once the protocol is modified, it will be necessary to reprocess the encoding and decoding of the protocol. Therefore, I believe there is limited need for dynamic handling of these issues. Dynamically setting values increases the complexity of the code.

Copy link
Member

Choose a reason for hiding this comment

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

Got it, thanks for clarifying!

}

@Override
Expand All @@ -115,7 +115,7 @@
try {
target = (ByteBuf) super.decode(ctx, in);
if (null == target) {
return null;

Check warning on line 118 in eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java#L118

Added line #L118 was not covered by tests
}
byte[] flagBytes = parseFlag(target);
byte[] versionBytes = parseVersion(target);
Expand All @@ -130,16 +130,16 @@
Package pkg = new Package(header, body);
return pkg;

} catch (Exception ex) {
log.error("decode error", ex);
ctx.channel().close();

Check warning on line 135 in eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java#L133-L135

Added lines #L133 - L135 were not covered by tests
} finally {
if (target != null) {
target.release();
}
}

return null;

Check warning on line 142 in eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java#L142

Added line #L142 was not covered by tests
}

private byte[] parseFlag(ByteBuf in) {
Expand All @@ -161,7 +161,7 @@
final byte[] headerData = new byte[headerLength];
in.readBytes(headerData);
if (log.isDebugEnabled()) {
Copy link
Member

Choose a reason for hiding this comment

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

Regarding these modifications to the log printing, you don't have to address them. PR #4698 is currently handling these issues uniformly.

关于日志打印的这些修改,您可以不用处理,PR #4698正在统一处理这些问题。

log.debug("Decode headerJson={}", deserializeBytes(headerData));

Check warning on line 164 in eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java#L164

Added line #L164 was not covered by tests
}
return JsonUtils.parseObject(headerData, Header.class);
}
Expand All @@ -173,7 +173,7 @@
final byte[] bodyData = new byte[bodyLength];
in.readBytes(bodyData);
if (log.isDebugEnabled()) {
log.debug("Decode bodyJson={}", deserializeBytes(bodyData));

Check warning on line 176 in eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java#L176

Added line #L176 was not covered by tests
}
return deserializeBody(deserializeBytes(bodyData), header);
}
Expand Down Expand Up @@ -227,7 +227,7 @@
return JsonUtils.parseObject(bodyJsonString, RedirectInfo.class);
default:
if (log.isWarnEnabled()) {
log.warn("Invalidate TCP command: {}", command);

Check warning on line 230 in eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java

View check run for this annotation

Codecov / codecov/patch

eventmesh-common/src/main/java/org/apache/eventmesh/common/protocol/tcp/codec/Codec.java#L230

Added line #L230 was not covered by tests
Copy link
Member

Choose a reason for hiding this comment

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

The same situation as mentioned earlier: #4702 (comment)

和前面同样的情况:#4702 (comment)

}
return null;
}
Expand Down
Loading