2019-07-31 08:12:43 -07:00
|
|
|
// Copyright 2019 The Prometheus Authors
|
|
|
|
// Licensed 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 promql
|
|
|
|
|
|
|
|
import (
|
2020-01-27 14:29:44 -08:00
|
|
|
"context"
|
2019-07-31 08:12:43 -07:00
|
|
|
"encoding/json"
|
2022-02-27 04:49:33 -08:00
|
|
|
"io"
|
2019-07-31 08:12:43 -07:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
"unicode/utf8"
|
2019-08-23 18:38:48 -07:00
|
|
|
|
|
|
|
"github.com/edsrzf/mmap-go"
|
2021-06-11 09:17:59 -07:00
|
|
|
"github.com/go-kit/log"
|
|
|
|
"github.com/go-kit/log/level"
|
2019-07-31 08:12:43 -07:00
|
|
|
)
|
|
|
|
|
|
|
|
type ActiveQueryTracker struct {
|
2020-01-28 12:38:49 -08:00
|
|
|
mmapedFile []byte
|
|
|
|
getNextIndex chan int
|
|
|
|
logger log.Logger
|
2022-02-27 04:49:33 -08:00
|
|
|
closer io.Closer
|
2020-01-28 12:38:49 -08:00
|
|
|
maxConcurrent int
|
2019-07-31 08:12:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
type Entry struct {
|
|
|
|
Query string `json:"query"`
|
|
|
|
Timestamp int64 `json:"timestamp_sec"`
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
entrySize int = 1000
|
|
|
|
)
|
|
|
|
|
2020-08-02 01:48:57 -07:00
|
|
|
func parseBrokenJSON(brokenJSON []byte) (string, bool) {
|
2020-01-09 13:11:39 -08:00
|
|
|
queries := strings.ReplaceAll(string(brokenJSON), "\x00", "")
|
2019-10-17 16:21:58 -07:00
|
|
|
if len(queries) > 0 {
|
|
|
|
queries = queries[:len(queries)-1] + "]"
|
|
|
|
}
|
2019-07-31 08:12:43 -07:00
|
|
|
|
|
|
|
// Conditional because of implementation detail: len() = 1 implies file consisted of a single char: '['.
|
2019-10-17 16:21:58 -07:00
|
|
|
if len(queries) <= 1 {
|
2020-08-02 01:48:57 -07:00
|
|
|
return "[]", false
|
2019-07-31 08:12:43 -07:00
|
|
|
}
|
|
|
|
|
2020-08-02 01:48:57 -07:00
|
|
|
return queries, true
|
2019-07-31 08:12:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func logUnfinishedQueries(filename string, filesize int, logger log.Logger) {
|
|
|
|
if _, err := os.Stat(filename); err == nil {
|
|
|
|
fd, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
level.Error(logger).Log("msg", "Failed to open query log file", "err", err)
|
|
|
|
return
|
|
|
|
}
|
2022-08-17 02:57:27 -07:00
|
|
|
defer fd.Close()
|
2019-07-31 08:12:43 -07:00
|
|
|
|
2020-01-09 13:11:39 -08:00
|
|
|
brokenJSON := make([]byte, filesize)
|
|
|
|
_, err = fd.Read(brokenJSON)
|
2019-07-31 08:12:43 -07:00
|
|
|
if err != nil {
|
|
|
|
level.Error(logger).Log("msg", "Failed to read query log file", "err", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-02 01:48:57 -07:00
|
|
|
queries, queriesExist := parseBrokenJSON(brokenJSON)
|
2019-07-31 08:12:43 -07:00
|
|
|
if !queriesExist {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
level.Info(logger).Log("msg", "These queries didn't finish in prometheus' last run:", "queries", queries)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-27 04:49:33 -08:00
|
|
|
func getMMapedFile(filename string, filesize int, logger log.Logger) ([]byte, io.Closer, error) {
|
2021-10-22 01:06:44 -07:00
|
|
|
file, err := os.OpenFile(filename, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0o666)
|
2019-07-31 08:12:43 -07:00
|
|
|
if err != nil {
|
2022-02-16 08:43:15 -08:00
|
|
|
absPath, pathErr := filepath.Abs(filename)
|
|
|
|
if pathErr != nil {
|
|
|
|
absPath = filename
|
|
|
|
}
|
|
|
|
level.Error(logger).Log("msg", "Error opening query log file", "file", absPath, "err", err)
|
2022-02-27 04:49:33 -08:00
|
|
|
return nil, nil, err
|
2019-07-31 08:12:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
err = file.Truncate(int64(filesize))
|
|
|
|
if err != nil {
|
|
|
|
level.Error(logger).Log("msg", "Error setting filesize.", "filesize", filesize, "err", err)
|
2022-02-27 04:49:33 -08:00
|
|
|
return nil, nil, err
|
2019-07-31 08:12:43 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
fileAsBytes, err := mmap.Map(file, mmap.RDWR, 0)
|
|
|
|
if err != nil {
|
2019-08-07 07:02:53 -07:00
|
|
|
level.Error(logger).Log("msg", "Failed to mmap", "file", filename, "Attempted size", filesize, "err", err)
|
2022-02-27 04:49:33 -08:00
|
|
|
return nil, nil, err
|
2019-07-31 08:12:43 -07:00
|
|
|
}
|
|
|
|
|
2022-02-27 04:49:33 -08:00
|
|
|
return fileAsBytes, file, err
|
2019-07-31 08:12:43 -07:00
|
|
|
}
|
|
|
|
|
2020-01-28 12:38:49 -08:00
|
|
|
func NewActiveQueryTracker(localStoragePath string, maxConcurrent int, logger log.Logger) *ActiveQueryTracker {
|
2021-10-22 01:06:44 -07:00
|
|
|
err := os.MkdirAll(localStoragePath, 0o777)
|
2019-07-31 08:12:43 -07:00
|
|
|
if err != nil {
|
|
|
|
level.Error(logger).Log("msg", "Failed to create directory for logging active queries")
|
|
|
|
}
|
|
|
|
|
2020-01-28 12:38:49 -08:00
|
|
|
filename, filesize := filepath.Join(localStoragePath, "queries.active"), 1+maxConcurrent*entrySize
|
2019-07-31 08:12:43 -07:00
|
|
|
logUnfinishedQueries(filename, filesize, logger)
|
|
|
|
|
2022-02-27 04:49:33 -08:00
|
|
|
fileAsBytes, closer, err := getMMapedFile(filename, filesize, logger)
|
2019-07-31 08:12:43 -07:00
|
|
|
if err != nil {
|
|
|
|
panic("Unable to create mmap-ed active query log")
|
|
|
|
}
|
|
|
|
|
|
|
|
copy(fileAsBytes, "[")
|
|
|
|
activeQueryTracker := ActiveQueryTracker{
|
2020-01-28 12:38:49 -08:00
|
|
|
mmapedFile: fileAsBytes,
|
2022-02-27 04:49:33 -08:00
|
|
|
closer: closer,
|
2020-01-28 12:38:49 -08:00
|
|
|
getNextIndex: make(chan int, maxConcurrent),
|
|
|
|
logger: logger,
|
|
|
|
maxConcurrent: maxConcurrent,
|
2019-07-31 08:12:43 -07:00
|
|
|
}
|
|
|
|
|
2020-01-28 12:38:49 -08:00
|
|
|
activeQueryTracker.generateIndices(maxConcurrent)
|
2019-07-31 08:12:43 -07:00
|
|
|
|
|
|
|
return &activeQueryTracker
|
|
|
|
}
|
|
|
|
|
|
|
|
func trimStringByBytes(str string, size int) string {
|
|
|
|
bytesStr := []byte(str)
|
|
|
|
|
|
|
|
trimIndex := len(bytesStr)
|
|
|
|
if size < len(bytesStr) {
|
|
|
|
for !utf8.RuneStart(bytesStr[size]) {
|
2019-08-23 18:38:48 -07:00
|
|
|
size--
|
2019-07-31 08:12:43 -07:00
|
|
|
}
|
|
|
|
trimIndex = size
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(bytesStr[:trimIndex])
|
|
|
|
}
|
|
|
|
|
2020-01-09 13:11:39 -08:00
|
|
|
func _newJSONEntry(query string, timestamp int64, logger log.Logger) []byte {
|
2019-07-31 08:12:43 -07:00
|
|
|
entry := Entry{query, timestamp}
|
|
|
|
jsonEntry, err := json.Marshal(entry)
|
|
|
|
if err != nil {
|
|
|
|
level.Error(logger).Log("msg", "Cannot create json of query", "query", query)
|
|
|
|
return []byte{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return jsonEntry
|
|
|
|
}
|
|
|
|
|
2020-01-09 13:11:39 -08:00
|
|
|
func newJSONEntry(query string, logger log.Logger) []byte {
|
2019-07-31 08:12:43 -07:00
|
|
|
timestamp := time.Now().Unix()
|
2020-01-09 13:11:39 -08:00
|
|
|
minEntryJSON := _newJSONEntry("", timestamp, logger)
|
2019-07-31 08:12:43 -07:00
|
|
|
|
2020-01-09 13:11:39 -08:00
|
|
|
query = trimStringByBytes(query, entrySize-(len(minEntryJSON)+1))
|
|
|
|
jsonEntry := _newJSONEntry(query, timestamp, logger)
|
2019-07-31 08:12:43 -07:00
|
|
|
|
|
|
|
return jsonEntry
|
|
|
|
}
|
|
|
|
|
2020-01-28 12:38:49 -08:00
|
|
|
func (tracker ActiveQueryTracker) generateIndices(maxConcurrent int) {
|
|
|
|
for i := 0; i < maxConcurrent; i++ {
|
2019-07-31 08:12:43 -07:00
|
|
|
tracker.getNextIndex <- 1 + (i * entrySize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-28 12:38:49 -08:00
|
|
|
func (tracker ActiveQueryTracker) GetMaxConcurrent() int {
|
|
|
|
return tracker.maxConcurrent
|
|
|
|
}
|
|
|
|
|
2019-07-31 08:12:43 -07:00
|
|
|
func (tracker ActiveQueryTracker) Delete(insertIndex int) {
|
|
|
|
copy(tracker.mmapedFile[insertIndex:], strings.Repeat("\x00", entrySize))
|
|
|
|
tracker.getNextIndex <- insertIndex
|
|
|
|
}
|
|
|
|
|
2020-01-27 14:29:44 -08:00
|
|
|
func (tracker ActiveQueryTracker) Insert(ctx context.Context, query string) (int, error) {
|
|
|
|
select {
|
|
|
|
case i := <-tracker.getNextIndex:
|
|
|
|
fileBytes := tracker.mmapedFile
|
|
|
|
entry := newJSONEntry(query, tracker.logger)
|
|
|
|
start, end := i, i+entrySize
|
|
|
|
|
|
|
|
copy(fileBytes[start:], entry)
|
|
|
|
copy(fileBytes[end-1:], ",")
|
|
|
|
return i, nil
|
|
|
|
case <-ctx.Done():
|
|
|
|
return 0, ctx.Err()
|
|
|
|
}
|
2019-07-31 08:12:43 -07:00
|
|
|
}
|
2022-02-27 04:49:33 -08:00
|
|
|
|
|
|
|
func (tracker *ActiveQueryTracker) Close() {
|
|
|
|
if tracker == nil || tracker.closer == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tracker.closer.Close()
|
|
|
|
}
|