luohaiyang created KAFKA-21685:
----------------------------------

             Summary: Values formats and parses temporal values in the JVM 
default time zone but labels them as UTC
                 Key: KAFKA-21685
                 URL: https://issues.apache.org/jira/browse/KAFKA-21685
             Project: Kafka
          Issue Type: Bug
          Components: connect
            Reporter: luohaiyang


h3. Problem
{{Values.dateFormatFor()}} and {{Values.parseAsTemporalType()}} create 
{{SimpleDateFormat}} instances without setting a time zone, so they use the JVM 
default time zone. The patterns end with a literal {{'Z'}}, which marks the 
output as UTC even when it is not. As a result, when the Connect worker does 
not run in UTC:
* Timestamps are converted to strings in local time but still carry the {{Z}} 
suffix.
* Dates can shift by one day. This happens in time zones west of UTC.
* A Date written by {{SimpleHeaderConverter}} and then read back is no longer 
midnight UTC, so {{Date.fromLogical()}} rejects it.

This affects {{SimpleHeaderConverter}} (the default {{header.converter}}), the 
{{Cast}} SMT when casting to string, and any caller of 
{{Values.convertToString()}} / {{Values.parseString()}}. The code has been 
unchanged since 1.1.0 (KAFKA-5142).

h3. Reproduction
Run with jshell and connect-api on the classpath, using 
{{-R-Duser.timezone=<tz>}}. Tested on trunk (c3cbf35565).
{code:java}
import org.apache.kafka.connect.data.*;
import org.apache.kafka.connect.storage.SimpleHeaderConverter;
// 2023-11-14T22:13:20Z
System.out.println(Values.convertToString(Timestamp.SCHEMA, new 
java.util.Date(1700000000000L)));
// 2022-01-08, a valid Connect Date (midnight UTC)
java.util.Date date = new java.util.Date(19000L * 86400000L);
System.out.println(Values.convertToString(org.apache.kafka.connect.data.Date.SCHEMA,
 date));
SimpleHeaderConverter c = new SimpleHeaderConverter();
SchemaAndValue back = c.toConnectHeader("t", "h", c.fromConnectHeader("t", "h", 
org.apache.kafka.connect.data.Date.SCHEMA, date));
org.apache.kafka.connect.data.Date.fromLogical(back.schema(), (java.util.Date) 
back.value());
{code}

||user.timezone||Timestamp||Date||Header round trip + Date.fromLogical||
|UTC|2023-11-14T22:13:20.000Z|2022-01-08|OK|
|Asia/Shanghai|2023-11-15T06:13:20.000Z|2022-01-08|DataException: Kafka Connect 
Date type should not have any time fields set to non-zero values.|
|America/Los_Angeles|2023-11-14T14:13:20.000Z|2022-01-07|same DataException|

The {{Cast}} SMT ({{spec=ts:string}}) produces the same Timestamp strings as 
above.

h3. Expected behavior
The output should not depend on the JVM time zone. UTC appears to be the 
intended behavior:
* In 
[KIP-145|https://cwiki.apache.org/confluence/display/KAFKA/KIP-145+-+Expose+Record+Headers+in+Kafka+Connect],
 the SimpleHeaderConverter table describes TIME and TIMESTAMP with the {{'Z'}} 
formats and gives the examples {{16:31:05.387UTC}} and 
{{2017-05-21T16:31:05.387UTC}}.
* {{TimestampConverter}} and {{TimestampRouter}} both call {{setTimeZone(UTC)}} 
on their {{SimpleDateFormat}}.
* {{Values}} itself uses {{Calendar.getInstance(UTC)}} when converting a 
Timestamp to a Time.

h3. Possible fixes
* A. Call {{setTimeZone(UTC)}} when formatting and parsing.
* B. Make the time zone configurable, keeping the current behavior as the 
default.
* C. Output the actual offset (e.g. {{+08:00}}) instead of a literal {{Z}}.

I'd prefer A, since it matches KIP-145 and the existing transforms.

h3. Compatibility
* On non-UTC workers, the string output changes.
* Strings written by older versions on non-UTC workers would be parsed with a 
shift after the fix.
* {{Values.dateFormatFor()}} is a public method.
* {{ValuesTest#shouldConvertDateValues}} currently asserts local-time behavior 
(KAFKA-18585, #18611, #20520).

Does this need a KIP, or is a note in the upgrade docs enough?



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to