Change TSM file naming to generation-sequence.tsm

pull/5003/head
Jason Wilder 2015-12-04 11:51:33 -07:00
parent 479469994a
commit 52bec1f7f6
4 changed files with 31 additions and 25 deletions

View File

@ -104,7 +104,7 @@ type Compactor struct {
MaxFileSize int MaxFileSize int
FileStore interface { FileStore interface {
NextID() int NextGeneration() int
} }
} }
@ -163,10 +163,10 @@ func (c *Compactor) writeNewFiles(iter KeyIterator) ([]string, error) {
var files []string var files []string
for { for {
currentID := c.FileStore.NextID() currentID := c.FileStore.NextGeneration()
// New TSM files are written to a temp file and renamed when fully completed. // New TSM files are written to a temp file and renamed when fully completed.
fileName := filepath.Join(c.Dir, fmt.Sprintf("%07d.%s.tmp", currentID, "tsm1dev")) fileName := filepath.Join(c.Dir, fmt.Sprintf("%09d-%09d.%s.tmp", currentID, 1, "tsm1dev"))
// Write as much as possible to this file // Write as much as possible to this file
err := c.write(fileName, iter) err := c.write(fileName, iter)

View File

@ -519,6 +519,6 @@ func (w *fakeFileStore) Stats() []tsm1.FileStat {
return w.PathsFn() return w.PathsFn()
} }
func (w *fakeFileStore) NextID() int { func (w *fakeFileStore) NextGeneration() int {
return 1 return 1
} }

View File

@ -69,8 +69,8 @@ type TSMFile interface {
type FileStore struct { type FileStore struct {
mu sync.RWMutex mu sync.RWMutex
currentFileID int currentGeneration int
dir string dir string
files []TSMFile files []TSMFile
} }
@ -109,19 +109,19 @@ func (f *FileStore) Count() int {
return len(f.files) return len(f.files)
} }
// CurrentID returns the max file ID + 1 // CurrentGeneration returns the max file ID + 1
func (f *FileStore) CurrentID() int { func (f *FileStore) CurrentGeneration() int {
f.mu.RLock() f.mu.RLock()
defer f.mu.RUnlock() defer f.mu.RUnlock()
return f.currentFileID return f.currentGeneration
} }
// NextID returns the max file ID + 1 // NextGeneration returns the max file ID + 1
func (f *FileStore) NextID() int { func (f *FileStore) NextGeneration() int {
f.mu.Lock() f.mu.Lock()
defer f.mu.Unlock() defer f.mu.Unlock()
f.currentFileID++ f.currentGeneration++
return f.currentFileID return f.currentGeneration
} }
func (f *FileStore) Add(files ...TSMFile) { func (f *FileStore) Add(files ...TSMFile) {
@ -213,13 +213,13 @@ func (f *FileStore) Open() error {
for _, fn := range files { for _, fn := range files {
// Keep track of the latest ID // Keep track of the latest ID
id, err := f.idFromFileName(fn) generation, _, err := f.idFromFileName(fn)
if err != nil { if err != nil {
return err return err
} }
if id >= f.currentFileID { if generation >= f.currentGeneration {
f.currentFileID = id + 1 f.currentGeneration = generation + 1
} }
file, err := os.OpenFile(fn, os.O_RDONLY, 0666) file, err := os.OpenFile(fn, os.O_RDONLY, 0666)
@ -366,15 +366,21 @@ func (f *FileStore) Replace(oldFiles, newFiles []string) error {
} }
// idFromFileName parses the segment file ID from its name // idFromFileName parses the segment file ID from its name
func (f *FileStore) idFromFileName(name string) (int, error) { func (f *FileStore) idFromFileName(name string) (int, int, error) {
parts := strings.Split(filepath.Base(name), ".") parts := strings.Split(filepath.Base(name), ".")
if len(parts) != 2 { if len(parts) != 2 {
return 0, fmt.Errorf("file %s is named incorrectly", name) return 0, 0, fmt.Errorf("file %s is named incorrectly", name)
} }
id, err := strconv.ParseUint(parts[0], 10, 32) parts = strings.Split(parts[0], "-")
if len(parts) != 2 {
return 0, 0, fmt.Errorf("file %s is named incorrectly", name)
}
return int(id), err generation, err := strconv.ParseUint(parts[0], 10, 32)
sequence, err := strconv.ParseUint(parts[1], 10, 32)
return int(generation), int(sequence), err
} }
type KeyCursor struct { type KeyCursor struct {

View File

@ -361,7 +361,7 @@ func TestFileStore_Open(t *testing.T) {
t.Fatalf("file count mismatch: got %v, exp %v", got, exp) t.Fatalf("file count mismatch: got %v, exp %v", got, exp)
} }
if got, exp := fs.CurrentID(), 4; got != exp { if got, exp := fs.CurrentGeneration(), 4; got != exp {
t.Fatalf("current ID mismatch: got %v, exp %v", got, exp) t.Fatalf("current ID mismatch: got %v, exp %v", got, exp)
} }
} }
@ -392,7 +392,7 @@ func TestFileStore_Remove(t *testing.T) {
t.Fatalf("file count mismatch: got %v, exp %v", got, exp) t.Fatalf("file count mismatch: got %v, exp %v", got, exp)
} }
if got, exp := fs.CurrentID(), 4; got != exp { if got, exp := fs.CurrentGeneration(), 4; got != exp {
t.Fatalf("current ID mismatch: got %v, exp %v", got, exp) t.Fatalf("current ID mismatch: got %v, exp %v", got, exp)
} }
@ -402,7 +402,7 @@ func TestFileStore_Remove(t *testing.T) {
t.Fatalf("file count mismatch: got %v, exp %v", got, exp) t.Fatalf("file count mismatch: got %v, exp %v", got, exp)
} }
if got, exp := fs.CurrentID(), 4; got != exp { if got, exp := fs.CurrentGeneration(), 4; got != exp {
t.Fatalf("current ID mismatch: got %v, exp %v", got, exp) t.Fatalf("current ID mismatch: got %v, exp %v", got, exp)
} }
} }
@ -563,9 +563,9 @@ func MustTempFile(dir string) *os.File {
} }
func fatal(t *testing.T, msg string, err error) { func fatal(t *testing.T, msg string, err error) {
t.Fatalf("unexpected error %s: %v", msg, err) t.Fatalf("unexpected error %v: %v", msg, err)
} }
func tsmFileName(id int) string { func tsmFileName(id int) string {
return fmt.Sprintf("%07d.tsm1dev", id) return fmt.Sprintf("%09d-%09d.tsm1dev", id, 1)
} }