Skip to content

Commit

Permalink
Switch snapshot format to use unix timestamp
Browse files Browse the repository at this point in the history
  • Loading branch information
armon committed Oct 15, 2014
1 parent 35f5fa0 commit 29b20eb
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
16 changes: 15 additions & 1 deletion file_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"time"
)
Expand Down Expand Up @@ -116,7 +117,9 @@ func (f *FileSnapshotStore) testPermissions() error {

// snapshotName generate s name for the snapshot
func snapshotName(term, index uint64) string {
return fmt.Sprintf("%d-%d-%s", term, index, time.Now().Format(time.RFC3339Nano))
now := time.Now()
msec := now.UnixNano() / int64(time.Millisecond)
return fmt.Sprintf("%d-%d-%d", term, index, msec)
}

// parseName parse the components of a name
Expand All @@ -126,6 +129,17 @@ func parseName(name string) (uint64, uint64, time.Time, error) {
if _, err := fmt.Sscanf(name, "%d-%d-%s", &term, &index, &timeString); err != nil {
return 0, 0, time.Time{}, err
}

// Due to a format change, the timeString can either be unix time in
// milliseconds, OR it could be RFC3339Nano format
unixMsec, err := strconv.ParseInt(timeString, 10, 64)
if err == nil {
unixNano := unixMsec * int64(time.Millisecond)
when := time.Unix(0, unixNano)
return term, index, when, nil
}

// Try for the RFC3339Nano format
when, err := time.Parse(time.RFC3339Nano, timeString)
if err != nil {
return 0, 0, time.Time{}, err
Expand Down
17 changes: 17 additions & 0 deletions file_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,23 @@ func TestFileSS_Names(t *testing.T) {
if time.Now().Sub(when) > time.Millisecond {
t.Fatalf("bad: %v", when)
}

// Ensure old-style name works
name = "50-100-2006-01-02T15:04:05.999999999Z"
term, index, when, err = parseName(name)
if err != nil {
t.Fatalf("err: %v", err)
}
if term != 50 {
t.Fatalf("bad: %v", index)
}
if index != 100 {
t.Fatalf("bad: %v", index)
}
expect := time.Date(2006, 01, 02, 15, 04, 05, 0, time.UTC)
if diff := when.Sub(expect); diff > time.Second {
t.Fatalf("bad: %v %v", when, diff)
}
}

func TestFileSS_Ordering(t *testing.T) {
Expand Down

0 comments on commit 29b20eb

Please sign in to comment.