Skip to content
This repository was archived by the owner on Aug 23, 2023. It is now read-only.

Commit 3f91288

Browse files
committed
Use atomic operations and a read lock instead of a write lock
1 parent f9c9cd3 commit 3f91288

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

idx/memory/memory.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"sort"
88
"strings"
99
"sync"
10+
"sync/atomic"
1011
"time"
1112

1213
"github.com/grafana/metrictank/errors"
@@ -213,20 +214,20 @@ func (m *MemoryIdx) Stop() {
213214
func (m *MemoryIdx) Update(point schema.MetricPoint, partition int32) (idx.Archive, int32, bool) {
214215
pre := time.Now()
215216

216-
m.Lock()
217-
defer m.Unlock()
217+
m.RLock()
218+
defer m.RUnlock()
218219

219220
existing, ok := m.defById[point.MKey]
220221
if ok {
221-
oldPart := existing.Partition
222222
if LogLevel < 2 {
223223
log.Debug("metricDef with id %v already in index", point.MKey)
224224
}
225225

226226
if existing.LastUpdate < int64(point.Time) {
227-
existing.LastUpdate = int64(point.Time)
227+
atomic.SwapInt64(&existing.LastUpdate, int64(point.Time))
228228
}
229-
existing.Partition = partition
229+
230+
oldPart := atomic.SwapInt32(&existing.Partition, partition)
230231
statUpdate.Inc()
231232
statUpdateDuration.Value(time.Since(pre))
232233
return *existing, oldPart, true

0 commit comments

Comments
 (0)