Skip to content

Commit

Permalink
fix(metrics): concurrent map read and map write (openGemini#805)
Browse files Browse the repository at this point in the history
Signed-off-by: Young Xu <[email protected]>
  • Loading branch information
xuthus5 authored Nov 29, 2024
1 parent c12bda1 commit 6be5081
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/util/lifted/influx/httpd/handler_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"net/http"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"

Expand Down Expand Up @@ -52,6 +53,7 @@ func init() {
}

type OpenGeminiCollector struct {
mux sync.RWMutex
indexMap map[string][]*metrics.ModuleIndex
}

Expand All @@ -66,7 +68,9 @@ func (c *OpenGeminiCollector) Describe(chan<- *prometheus.Desc) {}

func (c *OpenGeminiCollector) Collect(ch chan<- prometheus.Metric) {
for _, moduleName := range metricMsts {
c.mux.RLock()
metricSlice, ok := c.indexMap[moduleName]
c.mux.RUnlock()
if !ok {
continue
}
Expand Down Expand Up @@ -103,7 +107,9 @@ func (h *Handler) serveMetrics(w http.ResponseWriter, r *http.Request, user meta
if err != nil {
continue
}
openGeminiCollector.mux.Lock()
openGeminiCollector.indexMap[moduleName] = moduleIndex
openGeminiCollector.mux.Unlock()
}

promhttp.Handler().ServeHTTP(w, r)
Expand Down Expand Up @@ -211,6 +217,9 @@ func getMetrics(h *Handler, r *http.Request, user meta.User, tableName string) (
}

resp := h.getStmtResult(stmtID2Result)
if len(resp.Results) == 0 {
return []*metrics.ModuleIndex{}, resp.Err
}
if resp.Results[0].Err != nil {
return nil, resp.Results[0].Err
}
Expand Down

0 comments on commit 6be5081

Please sign in to comment.