This is an automated email from the ASF dual-hosted git repository.
sunnianjun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 01329b1239c Add StandardDateTimeFormatter (#29200)
01329b1239c is described below
commit 01329b1239cad66a5e38701dfca94775071a2106
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Nov 25 11:36:03 2023 +0800
Add StandardDateTimeFormatter (#29200)
---
.../query/text/MySQLTextResultSetRowPacket.java | 6 ++--
.../util/datetime/StandardDateTimeFormatter.java | 37 ++++++++++++++++++++++
.../datetime/StandardDateTimeFormatterTest.java | 31 ++++++++++++++++++
.../execute/AbstractPipelineLifecycleRunnable.java | 6 ++--
.../service/PipelineJobConfigurationManager.java | 6 ++--
.../core/job/service/PipelineJobManager.java | 6 ++--
.../data/pipeline/cdc/api/impl/CDCJobAPI.java | 6 ++--
7 files changed, 78 insertions(+), 20 deletions(-)
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 fec7bed350b..81fca057930 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.Getter;
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<Object> data;
public MySQLTextResultSetRowPacket(final MySQLPacketPayload payload, final
int columnCount) {
@@ -72,7 +70,7 @@ public final class MySQLTextResultSetRowPacket extends
MySQLPacket {
} 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 00000000000..96e1a5d7121
--- /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 00000000000..d4730283978
--- /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 01121824d0f..2b5d85fd594 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 @@ import java.util.concurrent.atomic.AtomicReference;
@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<Boolean> running = new
AtomicReference<>(null);
private volatile long startTimeMillis;
@@ -65,7 +63,7 @@ public abstract class AbstractPipelineLifecycleRunnable
implements PipelineLifec
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 3e5d3577660..898c162eeaa 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.config.job.PipelineJobConf
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 @@ import java.util.Collections;
@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 final class PipelineJobConfigurationManager {
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 80890cc2d46..82325787f0e 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.data.pipeline.core.job.PipelineJobIdUtils;
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 @@ import java.util.stream.Collectors;
@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 @@ public final class PipelineJobManager {
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 416d4ee4204..06ac2e6bbd6 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.exception.core.ShardingSpherePrecondition
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
org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigur
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 @@ import java.util.stream.Collectors;
@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 final class CDCJobAPI implements TransmissionJobAPI {
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()));