From d186b4199302e681f869dc8cd9371f971e123117 Mon Sep 17 00:00:00 2001 From: liyao Date: Thu, 16 Nov 2023 11:00:02 +0800 Subject: [PATCH] refactor: optimize backup metadata time format (#464) * feat: add time utils for formatting Signed-off-by: mlycore * refactor: using string and unified format for time Signed-off-by: mlycore * feat: rename mytime to atime Signed-off-by: mlycore * refactor: refactor time string Signed-off-by: mlycore * chore: update test with timeutil Signed-off-by: mlycore --------- Signed-off-by: mlycore --- pitr/cli/internal/cmd/backup.go | 15 +++--- pitr/cli/internal/cmd/show_test.go | 13 ++--- pitr/cli/internal/pkg/local-storage_test.go | 13 ++--- pitr/cli/internal/pkg/model/ls_backup.go | 8 +-- pitr/cli/pkg/timeutil/time.go | 57 +++++++++++++++++++++ 5 files changed, 83 insertions(+), 23 deletions(-) create mode 100644 pitr/cli/pkg/timeutil/time.go diff --git a/pitr/cli/internal/cmd/backup.go b/pitr/cli/internal/cmd/backup.go index 3f2a7a71..649cfcaf 100644 --- a/pitr/cli/internal/cmd/backup.go +++ b/pitr/cli/internal/cmd/backup.go @@ -27,6 +27,7 @@ import ( "github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/xerr" "github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/logging" "github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/prettyoutput" + "github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/timeutil" "github.com/google/uuid" "github.com/jedib0t/go-pretty/v6/progress" @@ -225,8 +226,8 @@ func exportData(proxy pkg.IShardingSphereProxy, ls pkg.ILocalStorage) (lsBackup Info: &model.BackupMetaInfo{ ID: uuid.New().String(), // generate uuid for this backup CSN: csn, - StartTime: time.Now().Unix(), - EndTime: 0, + StartTime: timeutil.Now().String(), + EndTime: timeutil.Init(), BackupMode: BackupMode, }, SsBackup: &model.SsBackup{ @@ -299,8 +300,8 @@ func _execBackup(as pkg.IAgentServer, node *model.StorageNode, dnCh chan *model. Port: node.Port, Status: model.SsBackupStatusRunning, BackupID: backupID, - StartTime: time.Now().Unix(), - EndTime: 0, + StartTime: timeutil.Now().String(), + EndTime: timeutil.Init(), } dnCh <- dn return nil @@ -363,7 +364,7 @@ func checkBackupStatus(lsBackup *model.LsBackup) model.BackupStatus { lsBackup.DnList = dnResult lsBackup.SsBackup.Status = backupFinalStatus - lsBackup.Info.EndTime = time.Now().Unix() + lsBackup.Info.EndTime = timeutil.Now().String() return backupFinalStatus } @@ -388,14 +389,14 @@ func checkStatus(as pkg.IAgentServer, sn *model.StorageNode, dn *model.DataNode, if err != nil { tracker.MarkAsErrored() dn.Status = status - dn.EndTime = time.Now().Unix() + dn.EndTime = timeutil.Now().String() dnCh <- dn done <- struct{}{} } if status == model.SsBackupStatusCompleted || status == model.SsBackupStatusFailed { tracker.MarkAsDone() dn.Status = status - dn.EndTime = time.Now().Unix() + dn.EndTime = timeutil.Now().String() dnCh <- dn done <- struct{}{} } diff --git a/pitr/cli/internal/cmd/show_test.go b/pitr/cli/internal/cmd/show_test.go index d4f9519b..ef2f427b 100644 --- a/pitr/cli/internal/cmd/show_test.go +++ b/pitr/cli/internal/cmd/show_test.go @@ -25,6 +25,7 @@ import ( "github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg" mock_pkg "github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/mocks" "github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/model" + "github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/timeutil" "github.com/golang/mock/gomock" . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" @@ -37,8 +38,8 @@ var _ = Describe("Show", func() { Info: &model.BackupMetaInfo{ ID: "back-id", CSN: "csn", - StartTime: time.Now().Unix(), - EndTime: time.Now().Add(time.Second * 10).Unix(), + StartTime: timeutil.Now().String(), + EndTime: timeutil.Now().Add(time.Second * 10).String(), }, DnList: []*model.DataNode{ { @@ -46,16 +47,16 @@ var _ = Describe("Show", func() { Port: 3306, Status: "Running", BackupID: "back-id", - StartTime: time.Now().Unix(), - EndTime: time.Now().Add(time.Second * 10).Unix(), + StartTime: timeutil.Now().String(), + EndTime: timeutil.Now().Add(time.Second * 10).String(), }, { IP: "127.0.0.2", Port: 3306, Status: "Completed", BackupID: "back-id", - StartTime: time.Now().Unix(), - EndTime: time.Now().Add(time.Second * 10).Unix(), + StartTime: timeutil.Now().String(), + EndTime: timeutil.Now().Add(time.Second * 10).String(), }, }, SsBackup: &model.SsBackup{ diff --git a/pitr/cli/internal/pkg/local-storage_test.go b/pitr/cli/internal/pkg/local-storage_test.go index ae9e4ecd..91976b63 100644 --- a/pitr/cli/internal/pkg/local-storage_test.go +++ b/pitr/cli/internal/pkg/local-storage_test.go @@ -23,6 +23,7 @@ import ( "time" "github.com/apache/shardingsphere-on-cloud/pitr/cli/internal/pkg/model" + "github.com/apache/shardingsphere-on-cloud/pitr/cli/pkg/timeutil" "github.com/google/uuid" . "github.com/onsi/ginkgo/v2" @@ -90,8 +91,8 @@ var _ = Describe("ILocalStorage", func() { Info: &model.BackupMetaInfo{ ID: backupID, CSN: uuid.New().String(), - StartTime: time.Now().Unix(), - EndTime: time.Now().Add(time.Minute).Unix(), + StartTime: timeutil.Now().String(), + EndTime: timeutil.Now().Add(time.Minute).String(), }, DnList: []*model.DataNode{ { @@ -99,16 +100,16 @@ var _ = Describe("ILocalStorage", func() { Port: 5432, Status: "Completed", BackupID: "SK08DAK1", - StartTime: time.Now().Unix(), - EndTime: time.Now().Unix(), + StartTime: timeutil.Now().String(), + EndTime: timeutil.Now().String(), }, { IP: "1.1.1.2", Port: 5432, Status: "Completed", BackupID: "SK08DAK2", - StartTime: time.Now().Unix(), - EndTime: time.Now().Unix(), + StartTime: timeutil.Now().String(), + EndTime: timeutil.Now().String(), }, }, SsBackup: &model.SsBackup{ diff --git a/pitr/cli/internal/pkg/model/ls_backup.go b/pitr/cli/internal/pkg/model/ls_backup.go index 8b5a4282..e1e59da4 100644 --- a/pitr/cli/internal/pkg/model/ls_backup.go +++ b/pitr/cli/internal/pkg/model/ls_backup.go @@ -29,8 +29,8 @@ type ( ID string `json:"id"` CSN string `json:"csn"` BackupMode DBBackupMode `json:"backup_mode"` - StartTime int64 `json:"start_time"` // Unix time - EndTime int64 `json:"end_time"` // Unix time + StartTime string `json:"start_time"` + EndTime string `json:"end_time"` FileName string } @@ -39,8 +39,8 @@ type ( Port uint16 `json:"port"` Status BackupStatus `json:"status"` BackupID string `json:"backup_id"` - StartTime int64 `json:"start_time"` // Unix time - EndTime int64 `json:"end_time"` // Unix time + StartTime string `json:"start_time"` + EndTime string `json:"end_time"` } ) diff --git a/pitr/cli/pkg/timeutil/time.go b/pitr/cli/pkg/timeutil/time.go new file mode 100644 index 00000000..e1e7297c --- /dev/null +++ b/pitr/cli/pkg/timeutil/time.go @@ -0,0 +1,57 @@ +/* + * 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 timeutil + +import "time" + +const ( + unifiedTimeFormat = "2006-01-02 15:04:05" +) + +type atime struct { + time.Time +} + +func Now() atime { + return atime{time.Now()} +} + +func (t atime) Add(d time.Duration) atime { + _ = t.Time.Add(d) + return t +} + +func (t atime) String() string { + return UnifiedTimeFormat(t.Time) +} + +func (t atime) Unit() int64 { + return UnixTimestampFormat(t.Time) +} + +func Init() string { + return "" +} + +func UnifiedTimeFormat(t time.Time) string { + return t.Format(unifiedTimeFormat) +} + +func UnixTimestampFormat(t time.Time) int64 { + return t.Unix() +}