|
|
|
@ -16,6 +16,7 @@ package promql
|
|
|
|
|
import ( |
|
|
|
|
"context" |
|
|
|
|
"encoding/json" |
|
|
|
|
"io" |
|
|
|
|
"os" |
|
|
|
|
"path/filepath" |
|
|
|
|
"strings" |
|
|
|
@ -31,6 +32,7 @@ type ActiveQueryTracker struct {
|
|
|
|
|
mmapedFile []byte |
|
|
|
|
getNextIndex chan int |
|
|
|
|
logger log.Logger |
|
|
|
|
closer io.Closer |
|
|
|
|
maxConcurrent int |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -81,7 +83,7 @@ func logUnfinishedQueries(filename string, filesize int, logger log.Logger) {
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func getMMapedFile(filename string, filesize int, logger log.Logger) ([]byte, error) { |
|
|
|
|
func getMMapedFile(filename string, filesize int, logger log.Logger) ([]byte, io.Closer, error) { |
|
|
|
|
file, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0o666) |
|
|
|
|
if err != nil { |
|
|
|
|
absPath, pathErr := filepath.Abs(filename) |
|
|
|
@ -89,22 +91,22 @@ func getMMapedFile(filename string, filesize int, logger log.Logger) ([]byte, er
|
|
|
|
|
absPath = filename |
|
|
|
|
} |
|
|
|
|
level.Error(logger).Log("msg", "Error opening query log file", "file", absPath, "err", err) |
|
|
|
|
return nil, err |
|
|
|
|
return nil, nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
err = file.Truncate(int64(filesize)) |
|
|
|
|
if err != nil { |
|
|
|
|
level.Error(logger).Log("msg", "Error setting filesize.", "filesize", filesize, "err", err) |
|
|
|
|
return nil, err |
|
|
|
|
return nil, nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fileAsBytes, err := mmap.Map(file, mmap.RDWR, 0) |
|
|
|
|
if err != nil { |
|
|
|
|
level.Error(logger).Log("msg", "Failed to mmap", "file", filename, "Attempted size", filesize, "err", err) |
|
|
|
|
return nil, err |
|
|
|
|
return nil, nil, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return fileAsBytes, err |
|
|
|
|
return fileAsBytes, file, err |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func NewActiveQueryTracker(localStoragePath string, maxConcurrent int, logger log.Logger) *ActiveQueryTracker { |
|
|
|
@ -116,7 +118,7 @@ func NewActiveQueryTracker(localStoragePath string, maxConcurrent int, logger lo
|
|
|
|
|
filename, filesize := filepath.Join(localStoragePath, "queries.active"), 1+maxConcurrent*entrySize |
|
|
|
|
logUnfinishedQueries(filename, filesize, logger) |
|
|
|
|
|
|
|
|
|
fileAsBytes, err := getMMapedFile(filename, filesize, logger) |
|
|
|
|
fileAsBytes, closer, err := getMMapedFile(filename, filesize, logger) |
|
|
|
|
if err != nil { |
|
|
|
|
panic("Unable to create mmap-ed active query log") |
|
|
|
|
} |
|
|
|
@ -124,6 +126,7 @@ func NewActiveQueryTracker(localStoragePath string, maxConcurrent int, logger lo
|
|
|
|
|
copy(fileAsBytes, "[") |
|
|
|
|
activeQueryTracker := ActiveQueryTracker{ |
|
|
|
|
mmapedFile: fileAsBytes, |
|
|
|
|
closer: closer, |
|
|
|
|
getNextIndex: make(chan int, maxConcurrent), |
|
|
|
|
logger: logger, |
|
|
|
|
maxConcurrent: maxConcurrent, |
|
|
|
@ -198,3 +201,10 @@ func (tracker ActiveQueryTracker) Insert(ctx context.Context, query string) (int
|
|
|
|
|
return 0, ctx.Err() |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
func (tracker *ActiveQueryTracker) Close() { |
|
|
|
|
if tracker == nil || tracker.closer == nil { |
|
|
|
|
return |
|
|
|
|
} |
|
|
|
|
tracker.closer.Close() |
|
|
|
|
} |
|
|
|
|