Skip to content

Commit

Permalink
refactor: optimize backup metadata time format (#464)
Browse files Browse the repository at this point in the history
* feat: add time utils for formatting

Signed-off-by: mlycore <[email protected]>

* refactor: using string and unified format for time

Signed-off-by: mlycore <[email protected]>

* feat: rename mytime to atime

Signed-off-by: mlycore <[email protected]>

* refactor: refactor time string

Signed-off-by: mlycore <[email protected]>

* chore: update test with timeutil

Signed-off-by: mlycore <[email protected]>

---------

Signed-off-by: mlycore <[email protected]>
  • Loading branch information
mlycore authored Nov 16, 2023
1 parent e2a5864 commit d186b41
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 23 deletions.
15 changes: 8 additions & 7 deletions pitr/cli/internal/cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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{
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}

Expand All @@ -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{}{}
}
Expand Down
13 changes: 7 additions & 6 deletions pitr/cli/internal/cmd/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -37,25 +38,25 @@ 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{
{
IP: "127.0.0.1",
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{
Expand Down
13 changes: 7 additions & 6 deletions pitr/cli/internal/pkg/local-storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -90,25 +91,25 @@ 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{
{
IP: "1.1.1.1",
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{
Expand Down
8 changes: 4 additions & 4 deletions pitr/cli/internal/pkg/model/ls_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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"`
}
)

Expand Down
57 changes: 57 additions & 0 deletions pitr/cli/pkg/timeutil/time.go
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit d186b41

Please sign in to comment.