Skip to content

Conversation

@gzlicanyi
Copy link
Contributor

Fix

  • Add a unit test to verify that the fix works.
  • Explain briefly why the bug exists and how to fix it.
    When the value of a JSON config in Kafka is a non-string type, such as {"batch.size":32768}, an error will be thrown when setting properties with the generated map.
ERROR 2023-05-31 14:56:57.847 SkywalkingAgent-13-org.apache.skywalking.apm.dependencies.kafkaProducerInitThread-0 KafkaProducerManager : unexpected exception. 
java.lang.ClassCastException: java.lang.Double cannot be cast to java.lang.String
        at java.util.Map.forEach(Map.java:630)
        at org.apache.skywalking.apm.agent.core.kafka.KafkaProducerManager.run(KafkaProducerManager.java:111)

Because this code does not deserialize the value of JSON into a String type. However, the value required by Properties is of String type.

Map<String, String> config = (Map<String, String>) gson.fromJson(Kafka.PRODUCER_CONFIG_JSON, Map.class);
  • If this pull request closes/resolves/fixes an existing issue, replace the issue number. Closes #.
  • Update the CHANGES log.

@wu-sheng
Copy link
Member

I think this is duplicated with #540?

@gzlicanyi
Copy link
Contributor Author

#540 hasn't fixed this bug.

@wu-sheng
Copy link
Member

Didn't you have fixed? #538 I am confused.

@gzlicanyi
Copy link
Contributor Author

Didn't you have fixed? #538 I am confused.

It's not the same issue. #538 fixed the Gson dependency issue, while #542 fixed the Gson deserialization problem, but both addressed the Kafka JSON config issue.

@gzlicanyi
Copy link
Contributor Author

Didn't you have fixed? #538 I am confused.

When the value of a JSON config in Kafka is a non-string type, such as {"batch.size":32768}, an error will be thrown when setting properties with the generated map.

But such as {"batch.size":"32768"}, it is ok.

@wu-sheng wu-sheng reopened this May 31, 2023
@wu-sheng wu-sheng removed the rejected label May 31, 2023
@wu-sheng
Copy link
Member

Could you provide more details? I am not familiar with this kind of Kafka configuration.

@wu-sheng wu-sheng added the bug Something isn't working label May 31, 2023
@gzlicanyi
Copy link
Contributor Author

Could you provide more details? I am not familiar with this kind of Kafka configuration.

Kafka's batch.size configuration refers to the size of messages sent by the producer in one batch, i.e., the batch size. By default, the batch.size is 16KB. If the size of a message is smaller than batch.size, the producer will wait for a certain amount of time to try to aggregate more messages before sending them together, in order to improve producer efficiency.

Typically, the batch size can be adjusted based on the size of messages sent by the producer to achieve optimal performance. If the messages are small, the batch size can be decreased, and if the messages are large, the batch size can be increased. However, it should be noted that if the batch size is set too large, it can increase the time it takes for messages to be transmitted over the network and increase the risk of message loss.

batch.size is usually configured as a numeric value.

Comment on lines +58 to +68
@Test
public void testSetPropertiesFromJsonConfig() {
KafkaProducerManager kafkaProducerManager = new KafkaProducerManager();
Properties properties = new Properties();

KafkaReporterPluginConfig.Plugin.Kafka.PRODUCER_CONFIG_JSON = "{\"batch.size\":32768}";
kafkaProducerManager.setPropertiesFromJsonConfig(properties);

assertEquals(properties.get("batch.size"), "32768");
}

Copy link
Member

Choose a reason for hiding this comment

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

What does this UT prove? The codes are not shaded ever in the UT scope, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Show an example of how a JSON configuration can be correctly set to properties.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What does this UT prove? The codes are not shaded ever in the UT scope, right?

right.

Copy link
Member

Choose a reason for hiding this comment

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

So, the point of this PR is TypeToken, which cast the type accordingly.

But still import com.google.common.reflect.TypeToken; seems not from gson package. Why?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So, the point of this PR is TypeToken, which cast the type accordingly.

But still import com.google.common.reflect.TypeToken; seems not from gson package. Why?

I have used the wrong TypeToken out of two.

Comment on lines 84 to 86
<pattern>com.google.gson</pattern>
<shadedPattern>${shade.package}.com.google.common</shadedPattern>
</relocation>
Copy link
Member

Choose a reason for hiding this comment

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

com.google.gson -> com.google.common?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed

@wu-sheng
Copy link
Member

Please fix the conflicts as another PR gets merged.

@wu-sheng wu-sheng added this to the 8.16.0 milestone May 31, 2023
@wu-sheng wu-sheng merged commit dc602b7 into apache:main May 31, 2023
yangyulely pushed a commit to yangyulely/skywalking-java that referenced this pull request May 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants