diff --git a/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/text/MySQLTextResultSetRowPacket.java b/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/text/MySQLTextResultSetRowPacket.java index fec7bed350bfb..81fca057930a3 100644 --- a/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/text/MySQLTextResultSetRowPacket.java +++ b/db-protocol/mysql/src/main/java/org/apache/shardingsphere/db/protocol/mysql/packet/command/query/text/MySQLTextResultSetRowPacket.java @@ -21,11 +21,11 @@ import lombok.RequiredArgsConstructor; import org.apache.shardingsphere.db.protocol.mysql.packet.MySQLPacket; import org.apache.shardingsphere.db.protocol.mysql.payload.MySQLPacketPayload; +import org.apache.shardingsphere.infra.util.datetime.StandardDateTimeFormatter; import java.math.BigDecimal; import java.sql.Timestamp; import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Collection; @@ -40,8 +40,6 @@ public final class MySQLTextResultSetRowPacket extends MySQLPacket { private static final int NULL = 0xfb; - private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); - private final Collection data; public MySQLTextResultSetRowPacket(final MySQLPacketPayload payload, final int columnCount) { @@ -72,7 +70,7 @@ private void writeDataIntoPayload(final MySQLPacketPayload payload, final Object } else if (data instanceof Boolean) { payload.writeBytesLenenc((boolean) data ? new byte[]{1} : new byte[]{0}); } else if (data instanceof LocalDateTime) { - payload.writeStringLenenc(DATE_TIME_FORMATTER.format((LocalDateTime) data)); + payload.writeStringLenenc(StandardDateTimeFormatter.get().format((LocalDateTime) data)); } else { payload.writeStringLenenc(data.toString()); } diff --git a/infra/util/src/main/java/org/apache/shardingsphere/infra/util/datetime/StandardDateTimeFormatter.java b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/datetime/StandardDateTimeFormatter.java new file mode 100644 index 0000000000000..96e1a5d7121aa --- /dev/null +++ b/infra/util/src/main/java/org/apache/shardingsphere/infra/util/datetime/StandardDateTimeFormatter.java @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.infra.util.datetime; + +import java.time.format.DateTimeFormatter; + +/** + * Standard date time formatter. + */ +public final class StandardDateTimeFormatter { + + private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); + + /** + * Get standard date time formatter. + * + * @return standard date time formatter + */ + public static DateTimeFormatter get() { + return DATE_TIME_FORMATTER; + } +} diff --git a/infra/util/src/test/java/org/apache/shardingsphere/infra/util/datetime/StandardDateTimeFormatterTest.java b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/datetime/StandardDateTimeFormatterTest.java new file mode 100644 index 0000000000000..d4730283978a7 --- /dev/null +++ b/infra/util/src/test/java/org/apache/shardingsphere/infra/util/datetime/StandardDateTimeFormatterTest.java @@ -0,0 +1,31 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.shardingsphere.infra.util.datetime; + +import org.junit.jupiter.api.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; + +class StandardDateTimeFormatterTest { + + @Test + void assertGet() { + assertThat(StandardDateTimeFormatter.get().parse("1970-01-01 00:00:00").toString(), is("{},ISO resolved to 1970-01-01T00:00")); + } +} diff --git a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/common/execute/AbstractPipelineLifecycleRunnable.java b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/common/execute/AbstractPipelineLifecycleRunnable.java index 01121824d0f96..2b5d85fd5946b 100644 --- a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/common/execute/AbstractPipelineLifecycleRunnable.java +++ b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/common/execute/AbstractPipelineLifecycleRunnable.java @@ -18,12 +18,12 @@ package org.apache.shardingsphere.data.pipeline.common.execute; import lombok.extern.slf4j.Slf4j; +import org.apache.shardingsphere.infra.util.datetime.StandardDateTimeFormatter; import java.sql.SQLException; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneId; -import java.time.format.DateTimeFormatter; import java.util.concurrent.atomic.AtomicReference; /** @@ -32,8 +32,6 @@ @Slf4j public abstract class AbstractPipelineLifecycleRunnable implements PipelineLifecycleRunnable { - private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); - private final AtomicReference running = new AtomicReference<>(null); private volatile long startTimeMillis; @@ -65,7 +63,7 @@ public final void stop() { return; } LocalDateTime startTime = LocalDateTime.ofInstant(Instant.ofEpochMilli(startTimeMillis), ZoneId.systemDefault()); - log.info("stop lifecycle executor {}, startTime={}, cost {} ms", this, startTime.format(DATE_TIME_FORMATTER), System.currentTimeMillis() - startTimeMillis); + log.info("stop lifecycle executor {}, startTime={}, cost {} ms", this, startTime.format(StandardDateTimeFormatter.get()), System.currentTimeMillis() - startTimeMillis); try { doStop(); // CHECKSTYLE:OFF diff --git a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/service/PipelineJobConfigurationManager.java b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/service/PipelineJobConfigurationManager.java index 3e5d357766058..898c162eeaa2e 100644 --- a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/service/PipelineJobConfigurationManager.java +++ b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/service/PipelineJobConfigurationManager.java @@ -22,10 +22,10 @@ import org.apache.shardingsphere.data.pipeline.common.listener.PipelineElasticJobListener; import org.apache.shardingsphere.data.pipeline.core.job.PipelineJobIdUtils; import org.apache.shardingsphere.elasticjob.infra.pojo.JobConfigurationPOJO; +import org.apache.shardingsphere.infra.util.datetime.StandardDateTimeFormatter; import org.apache.shardingsphere.infra.util.yaml.YamlEngine; import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; import java.util.Collections; /** @@ -34,8 +34,6 @@ @RequiredArgsConstructor public final class PipelineJobConfigurationManager { - private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); - private final PipelineJobAPI jobAPI; /** @@ -62,7 +60,7 @@ public JobConfigurationPOJO convertToJobConfigurationPOJO(final PipelineJobConfi int shardingTotalCount = jobAPI.isForceNoShardingWhenConvertToJobConfigurationPOJO() ? 1 : jobConfig.getJobShardingCount(); result.setShardingTotalCount(shardingTotalCount); result.setJobParameter(YamlEngine.marshal(jobAPI.getYamlJobConfigurationSwapper().swapToYamlConfiguration(jobConfig))); - String createTimeFormat = LocalDateTime.now().format(DATE_TIME_FORMATTER); + String createTimeFormat = LocalDateTime.now().format(StandardDateTimeFormatter.get()); result.getProps().setProperty("create_time", createTimeFormat); result.getProps().setProperty("start_time_millis", String.valueOf(System.currentTimeMillis())); result.getProps().setProperty("run_count", "1"); diff --git a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/service/PipelineJobManager.java b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/service/PipelineJobManager.java index 80890cc2d46d4..82325787f0e89 100644 --- a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/service/PipelineJobManager.java +++ b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/service/PipelineJobManager.java @@ -33,9 +33,9 @@ import org.apache.shardingsphere.elasticjob.infra.pojo.JobConfigurationPOJO; import org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions; import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader; +import org.apache.shardingsphere.infra.util.datetime.StandardDateTimeFormatter; import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; import java.util.Collections; import java.util.List; import java.util.Optional; @@ -49,8 +49,6 @@ @Slf4j public final class PipelineJobManager { - private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); - private final PipelineJobAPI jobAPI; /** @@ -148,7 +146,7 @@ private void stopCurrentJob(final String jobId) { return; } jobConfigPOJO.setDisabled(true); - jobConfigPOJO.getProps().setProperty("stop_time", LocalDateTime.now().format(DATE_TIME_FORMATTER)); + jobConfigPOJO.getProps().setProperty("stop_time", LocalDateTime.now().format(StandardDateTimeFormatter.get())); jobConfigPOJO.getProps().setProperty("stop_time_millis", String.valueOf(System.currentTimeMillis())); String barrierPath = PipelineMetaDataNode.getJobBarrierDisablePath(jobId); pipelineDistributedBarrier.register(barrierPath, jobConfigPOJO.getShardingTotalCount()); diff --git a/kernel/data-pipeline/scenario/cdc/core/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/api/impl/CDCJobAPI.java b/kernel/data-pipeline/scenario/cdc/core/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/api/impl/CDCJobAPI.java index 416d4ee4204ba..06ac2e6bbd688 100644 --- a/kernel/data-pipeline/scenario/cdc/core/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/api/impl/CDCJobAPI.java +++ b/kernel/data-pipeline/scenario/cdc/core/src/main/java/org/apache/shardingsphere/data/pipeline/cdc/api/impl/CDCJobAPI.java @@ -79,6 +79,7 @@ import org.apache.shardingsphere.infra.instance.metadata.InstanceType; import org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase; import org.apache.shardingsphere.infra.metadata.database.resource.unit.StorageUnit; +import org.apache.shardingsphere.infra.util.datetime.StandardDateTimeFormatter; import org.apache.shardingsphere.infra.util.yaml.YamlEngine; import org.apache.shardingsphere.infra.yaml.config.pojo.YamlRootConfiguration; import org.apache.shardingsphere.infra.yaml.config.swapper.resource.YamlDataSourceConfigurationSwapper; @@ -86,7 +87,6 @@ import java.sql.SQLException; import java.time.LocalDateTime; -import java.time.format.DateTimeFormatter; import java.util.Collection; import java.util.HashMap; import java.util.List; @@ -102,8 +102,6 @@ @Slf4j public final class CDCJobAPI implements TransmissionJobAPI { - private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); - private final YamlDataSourceConfigurationSwapper dataSourceConfigSwapper = new YamlDataSourceConfigurationSwapper(); private final YamlRuleConfigurationSwapperEngine ruleConfigSwapperEngine = new YamlRuleConfigurationSwapperEngine(); @@ -226,7 +224,7 @@ public void updateJobConfigurationDisabled(final String jobId, final boolean dis JobConfigurationPOJO jobConfigPOJO = PipelineJobIdUtils.getElasticJobConfigurationPOJO(jobId); jobConfigPOJO.setDisabled(disabled); if (disabled) { - jobConfigPOJO.getProps().setProperty("stop_time", LocalDateTime.now().format(DATE_TIME_FORMATTER)); + jobConfigPOJO.getProps().setProperty("stop_time", LocalDateTime.now().format(StandardDateTimeFormatter.get())); jobConfigPOJO.getProps().setProperty("stop_time_millis", String.valueOf(System.currentTimeMillis())); } else { jobConfigPOJO.getProps().setProperty("start_time_millis", String.valueOf(System.currentTimeMillis()));