-
Notifications
You must be signed in to change notification settings - Fork 3.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: do not rename files on mmap failure #25340
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -176,9 +176,8 @@ type FileStore struct { | |
currentGeneration int | ||
dir string | ||
|
||
files []TSMFile | ||
tsmMMAPWillNeed bool // If true then the kernel will be advised MMAP_WILLNEED for TSM files. | ||
openLimiter limiter.Fixed // limit the number of concurrent opening TSM files. | ||
files []TSMFile | ||
openLimiter limiter.Fixed // limit the number of concurrent opening TSM files. | ||
|
||
logger *zap.Logger // Logger to be used for important messages | ||
traceLogger *zap.Logger // Logger to be used when trace-logging is on. | ||
|
@@ -198,6 +197,8 @@ type FileStore struct { | |
// newReaderBlockCount keeps track of the current new reader block requests. | ||
// If non-zero, no new TSMReader objects may be created. | ||
newReaderBlockCount int | ||
|
||
readerOptions []tsmReaderOption | ||
} | ||
|
||
// FileStat holds information about a TSM file on disk. | ||
|
@@ -234,7 +235,7 @@ func (f FileStat) ContainsKey(key []byte) bool { | |
} | ||
|
||
// NewFileStore returns a new instance of FileStore based on the given directory. | ||
func NewFileStore(dir string, tags tsdb.EngineTags) *FileStore { | ||
func NewFileStore(dir string, tags tsdb.EngineTags, options ...tsmReaderOption) *FileStore { | ||
logger := zap.NewNop() | ||
fs := &FileStore{ | ||
dir: dir, | ||
|
@@ -250,6 +251,7 @@ func NewFileStore(dir string, tags tsdb.EngineTags) *FileStore { | |
obs: noFileStoreObserver{}, | ||
parseFileName: DefaultParseFileName, | ||
copyFiles: runtime.GOOS == "windows", | ||
readerOptions: options, | ||
} | ||
fs.purger.fileStore = fs | ||
return fs | ||
|
@@ -616,28 +618,37 @@ func (f *FileStore) Open(ctx context.Context) error { | |
defer f.openLimiter.Release() | ||
|
||
start := time.Now() | ||
df, err := NewTSMReader(file, WithMadviseWillNeed(f.tsmMMAPWillNeed)) | ||
df, err := NewTSMReader(file, f.readerOptions...) | ||
f.logger.Info("Opened file", | ||
zap.String("path", file.Name()), | ||
zap.Int("id", idx), | ||
zap.Duration("duration", time.Since(start))) | ||
|
||
// If we are unable to read a TSM file then log the error, rename | ||
// the file, and continue loading the shard without it. | ||
// If we are unable to read a TSM file then log the error. | ||
if err != nil { | ||
if cerr := file.Close(); cerr != nil { | ||
f.logger.Error("Error closing TSM file after error", zap.String("path", file.Name()), zap.Int("id", idx), zap.Error(cerr)) | ||
} | ||
// If the file is corrupt, rename it and | ||
// continue loading the shard without it. | ||
f.logger.Error("Cannot read corrupt tsm file, renaming", zap.String("path", file.Name()), zap.Int("id", idx), zap.Error(err)) | ||
if e := os.Rename(file.Name(), file.Name()+"."+BadTSMFileExtension); e != nil { | ||
f.logger.Error("Cannot rename corrupt tsm file", zap.String("path", file.Name()), zap.Int("id", idx), zap.Error(e)) | ||
readerC <- &res{r: df, err: fmt.Errorf("cannot rename corrupt file %s: %w", file.Name(), e)} | ||
if errors.Is(err, MmapError{}) { | ||
// An MmapError may indicate we have insufficient | ||
// handles for the mmap call, in which case the file should | ||
// be left untouched, and the vm.max_map_count be raised. | ||
f.logger.Error("Cannot read TSM file, system limit for vm.max_map_count may be too low", | ||
zap.String("path", file.Name()), zap.Int("id", idx), zap.Error(err)) | ||
readerC <- &res{r: df, err: fmt.Errorf("cannot read file %s, system limit for vm.max_map_count may be too low: %v", file.Name(), err)} | ||
return | ||
} else { | ||
// If the file is corrupt, rename it and | ||
// continue loading the shard without it. | ||
f.logger.Error("Cannot read corrupt tsm file, renaming", zap.String("path", file.Name()), zap.Int("id", idx), zap.Error(err)) | ||
if e := os.Rename(file.Name(), file.Name()+"."+BadTSMFileExtension); e != nil { | ||
f.logger.Error("Cannot rename corrupt tsm file", zap.String("path", file.Name()), zap.Int("id", idx), zap.Error(e)) | ||
readerC <- &res{r: df, err: fmt.Errorf("cannot rename corrupt file %s: %v", file.Name(), e)} | ||
return | ||
} | ||
readerC <- &res{r: df, err: fmt.Errorf("cannot read corrupt file %s: %v", file.Name(), err)} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return | ||
} | ||
readerC <- &res{r: df, err: fmt.Errorf("cannot read corrupt file %s: %w", file.Name(), err)} | ||
return | ||
} | ||
df.WithObserver(f.obs) | ||
readerC <- &res{r: df} | ||
|
@@ -920,7 +931,7 @@ func (f *FileStore) replace(oldFiles, newFiles []string, updatedFn func(r []TSMF | |
} | ||
} | ||
|
||
tsm, err := NewTSMReader(fd, WithMadviseWillNeed(f.tsmMMAPWillNeed)) | ||
tsm, err := NewTSMReader(fd, f.readerOptions...) | ||
if err != nil { | ||
if newName != oldName { | ||
if err1 := os.Rename(newName, oldName); err1 != nil { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
package tsm1 | ||
|
||
import ( | ||
"github.com/influxdata/influxdb/v2/tsdb" | ||
) | ||
|
||
var TestMmapInitFailOption = func(err error) tsmReaderOption { | ||
return func(r *TSMReader) { | ||
r.accessor = &badBlockAccessor{error: err} | ||
} | ||
} | ||
|
||
type badBlockAccessor struct { | ||
error | ||
initCalled bool | ||
} | ||
|
||
func (b *badBlockAccessor) init() (*indirectIndex, error) { | ||
b.initCalled = true | ||
return nil, b.error | ||
} | ||
|
||
func (b *badBlockAccessor) read(key []byte, timestamp int64) ([]Value, error) { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (b *badBlockAccessor) readAll(key []byte) ([]Value, error) { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (b *badBlockAccessor) readBlock(entry *IndexEntry, values []Value) ([]Value, error) { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (b *badBlockAccessor) readFloatBlock(entry *IndexEntry, values *[]FloatValue) ([]FloatValue, error) { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (b *badBlockAccessor) readFloatArrayBlock(entry *IndexEntry, values *tsdb.FloatArray) error { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (b *badBlockAccessor) readIntegerBlock(entry *IndexEntry, values *[]IntegerValue) ([]IntegerValue, error) { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (b *badBlockAccessor) readIntegerArrayBlock(entry *IndexEntry, values *tsdb.IntegerArray) error { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (b *badBlockAccessor) readUnsignedBlock(entry *IndexEntry, values *[]UnsignedValue) ([]UnsignedValue, error) { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (b *badBlockAccessor) readUnsignedArrayBlock(entry *IndexEntry, values *tsdb.UnsignedArray) error { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (b *badBlockAccessor) readStringBlock(entry *IndexEntry, values *[]StringValue) ([]StringValue, error) { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (b *badBlockAccessor) readStringArrayBlock(entry *IndexEntry, values *tsdb.StringArray) error { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (b *badBlockAccessor) readBooleanBlock(entry *IndexEntry, values *[]BooleanValue) ([]BooleanValue, error) { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (b *badBlockAccessor) readBooleanArrayBlock(entry *IndexEntry, values *tsdb.BooleanArray) error { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (b *badBlockAccessor) readBytes(entry *IndexEntry, buf []byte) (uint32, []byte, error) { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (b *badBlockAccessor) rename(path string) error { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (b *badBlockAccessor) path() string { | ||
//TODO implement me | ||
panic("implement me") | ||
} | ||
|
||
func (b *badBlockAccessor) close() error { | ||
if !b.initCalled { | ||
panic("close called without an init call") | ||
} | ||
b.initCalled = false | ||
return nil | ||
} | ||
|
||
func (b *badBlockAccessor) free() error { | ||
//TODO implement me | ||
panic("implement me") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would use a
%w
here instead of%v
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
master-1.x
code uses %v also. My preference would be to get this PR merged and backported to2.7
, then do a PR to fix this inmaster-1.x
and propagate it through the branches.