Skip to content

Conversation

@beliefer
Copy link
Contributor

@beliefer beliefer commented Jan 5, 2024

What changes were proposed in this pull request?

This PR propose to remove ThreadLocal by replace SimpleDateFormat with DateTimeFormatter.

Why are the changes needed?

SimpleDateFormat is not thread safe, so we wrap it with ThreadLocal.
DateTimeFormatter is thread safe, we can use it instead.
According to the javadoc of SimpleDateFormat, it recommended to use DateTimeFormatter too.
1

In addition, DateTimeFormatter have better performance than SimpleDateFormat too.

Does this PR introduce any user-facing change?

'No'.

How was this patch tested?

GA tests.

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

'No'.

@github-actions github-actions bot added the WEB UI label Jan 5, 2024
@beliefer beliefer changed the title [WIP][CORE] Remove ThreadLocal by replace SimpleDateFormat with DateTimeFormatter [CORE] Remove ThreadLocal by replace SimpleDateFormat with DateTimeFormatter Jan 5, 2024
@beliefer beliefer changed the title [CORE] Remove ThreadLocal by replace SimpleDateFormat with DateTimeFormatter [WIP][CORE] Remove ThreadLocal by replace SimpleDateFormat with DateTimeFormatter Jan 5, 2024
Copy link
Contributor

Choose a reason for hiding this comment

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

How about

private val dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss", Locale.US)
    .withZone(java.time.ZoneId.systemDefault())

def formatDate(date: Date): String = dateTimeFormatter.format(date.toInstant)
def formatDate(timestamp: Long): String = dateTimeFormatter.format(Instant.ofEpochMilli(timestamp))

Copy link
Contributor

Choose a reason for hiding this comment

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

This is even better than my suggestion, thanks @LuciferYang !

Copy link
Contributor

@mridulm mridulm left a comment

Choose a reason for hiding this comment

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

This is a nice change.
There are other usages of SimpleDateFormat which are candidates for this pattern.

@beliefer
Copy link
Contributor Author

beliefer commented Jan 6, 2024

There are other usages of SimpleDateFormat which are candidates for this pattern.

I will check.

@beliefer beliefer changed the title [WIP][CORE] Remove ThreadLocal by replace SimpleDateFormat with DateTimeFormatter [SPARK-46611][CORE] Remove ThreadLocal by replace SimpleDateFormat with DateTimeFormatter Jan 6, 2024
@beliefer
Copy link
Contributor Author

beliefer commented Jan 7, 2024

ping @dongjoon-hyun @srowen cc @yaooqinn

@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@MaxGekk
Copy link
Member

MaxGekk commented Jan 7, 2024

I sent info about the spam from @llkj1 to GitHub support.

@srowen
Copy link
Member

srowen commented Jan 7, 2024

I tried to block the user and have been deleting the spam

@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@apache apache deleted a comment Jan 7, 2024
@LuciferYang
Copy link
Contributor

is this one ready to go?

Copy link
Contributor

@LuciferYang LuciferYang left a comment

Choose a reason for hiding this comment

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

+1, LGTM

@LuciferYang
Copy link
Contributor

LuciferYang commented Jan 8, 2024

Merged into master. Thanks @beliefer @srowen @mridulm

batchTimeFormat.get.setTimeZone(timezone)
batchTimeFormatWithMilliseconds.get.setTimeZone(timezone)
val zoneId = timezone.toZoneId
batchTimeFormat.withZone(zoneId)
Copy link
Member

Choose a reason for hiding this comment

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

This looks like a regression - see #44619

@beliefer
Copy link
Contributor Author

beliefer commented Jan 8, 2024

@LuciferYang @srowen @mridulm Thank you!

HyukjinKwon pushed a commit that referenced this pull request Jan 8, 2024
…one in UIUtils

### What changes were proposed in this pull request?

Simplify code and fix new use of DateTimeFormat.withZone introduced in #44613 ; need to use the new object copy this creates.

### Why are the changes needed?

Describing on mailing list from Janda Martin:

```
DateTimeFormatter is thread-safe and immutable according to JavaDoc so method DateTimeFormatter::withZone returns new instance when zone is changed.

Following code has no effect:
  val oldTimezones = (batchTimeFormat.getZone, batchTimeFormatWithMilliseconds.getZone)
  if (timezone != null) {
      val zoneId = timezone.toZoneId
      batchTimeFormat.withZone(zoneId)
      batchTimeFormatWithMilliseconds.withZone(zoneId)
    }

Suggested fix:
introduce local variables for "batchTimeFormat" and "batchTimeFormatWithMilliseconds" and remove "oldTimezones" and "finally" block.
```

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

Unlikely, the path in question is apparently test-only

### How was this patch tested?

Existing tests

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

No

Closes #44619 from srowen/SPARK-46611.2.

Authored-by: Sean Owen <srowen@gmail.com>
Signed-off-by: Hyukjin Kwon <gurwls223@apache.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants