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

[SPARK-48744][Core] Log entry should be constructed only once #47135

Closed
wants to merge 2 commits into from

Conversation

gengliangwang
Copy link
Member

@gengliangwang gengliangwang commented Jun 27, 2024

What changes were proposed in this pull request?

In the current implementation:

class LogEntry(messageWithContext: => MessageWithContext) {

  def message: String = messageWithContext.message

  def context: java.util.HashMap[String, String] = messageWithContext.context
}

def logInfo(entry: LogEntry): Unit = {
  if (log.isInfoEnabled) {
    withLogContext(entry.context) {
      log.info(entry.message)
    }
  }
}

The field messageWithContext is constructed twice, one from entry.context and another one from entry.message.

This PR is to improve this and ensure a log entry is constructed only once.

Why are the changes needed?

Improve the performance of logging

Does this PR introduce any user-facing change?

No

How was this patch tested?

New UT

Was this patch authored or co-authored using generative AI tooling?

No

@gengliangwang
Copy link
Member Author

cc @panbingkun

@@ -228,6 +228,37 @@ trait LoggingSuiteBase
verifyMsgWithConcat(level, logOutput)
}
}

test("LogEntry should construct MessageWithContext only once") {
Copy link
Contributor

Choose a reason for hiding this comment

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

Perhaps we should put the above testing logic into MDCSuite or a new UT (eg: MessageWithContextSuite), as it seems unrelated to the classification of structured and pattern logs, so it will be executed twice in GA?

Copy link
Member Author

Choose a reason for hiding this comment

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

A test takes 200ms, running twice is ok. Also, we need to ensure the log entry is constructed only once when structured logging is disabled, too.

@panbingkun
Copy link
Contributor

This is really a great improvement!

@gengliangwang
Copy link
Member Author

Merging to master

asl3 pushed a commit to asl3/spark that referenced this pull request Jul 1, 2024
### What changes were proposed in this pull request?

In the current implementation:

```
class LogEntry(messageWithContext: => MessageWithContext) {

  def message: String = messageWithContext.message

  def context: java.util.HashMap[String, String] = messageWithContext.context
}

def logInfo(entry: LogEntry): Unit = {
  if (log.isInfoEnabled) {
    withLogContext(entry.context) {
      log.info(entry.message)
    }
  }
}
```

The field `messageWithContext` is constructed twice, one from `entry.context` and another one from `entry.message`.

This PR is to improve this and ensure a log entry is constructed only once.

### Why are the changes needed?

Improve the performance of logging

### Does this PR introduce _any_ user-facing change?

No
### How was this patch tested?

New UT
### Was this patch authored or co-authored using generative AI tooling?

No

Closes apache#47135 from gengliangwang/addCache.

Authored-by: Gengliang Wang <[email protected]>
Signed-off-by: Gengliang Wang <[email protected]>
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.

5 participants