Skip to content

Commit

Permalink
Add StandardDateTimeFormatter (#29200)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Nov 25, 2023
1 parent 93fc8a5 commit 01329b1
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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) {
Expand Down Expand Up @@ -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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -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"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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<Boolean> running = new AtomicReference<>(null);

private volatile long startTimeMillis;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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;

/**
Expand All @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,14 @@
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;
import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapperEngine;

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;
Expand All @@ -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();
Expand Down Expand Up @@ -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()));
Expand Down

0 comments on commit 01329b1

Please sign in to comment.