wp
This commit is contained in:
151
worker/metric.go
151
worker/metric.go
@@ -16,18 +16,14 @@ import (
|
||||
var ErrNoValueBug = errors.New("has timestamp but no value")
|
||||
|
||||
type CapturedState struct {
|
||||
LastTimestamp uint32
|
||||
LastValue float64
|
||||
LastValue float64
|
||||
}
|
||||
|
||||
type Metric struct {
|
||||
metricType qb.MetricType
|
||||
fracDigits byte
|
||||
lastPageNo uint32
|
||||
//SinceValue float64
|
||||
//Since uint32
|
||||
lastValue float64
|
||||
//Until uint32
|
||||
metricType qb.MetricType
|
||||
fracDigits byte
|
||||
lastPageNo uint32
|
||||
lastValue float64
|
||||
buffer []byte
|
||||
timestamps qb.TimestampCompressor
|
||||
values qb.ValueCompressor
|
||||
@@ -117,14 +113,42 @@ func (s *Metric) LastValue() float64 {
|
||||
return s.lastValue
|
||||
}
|
||||
|
||||
func (s *Metric) LastTimestamp() uint32 {
|
||||
if s.capturedState != nil {
|
||||
return s.capturedState.LastTimestamp
|
||||
// REQUESTS
|
||||
|
||||
func (s *Metric) ReleaseRLock() {
|
||||
s.rLocks--
|
||||
if s.rLocks == 0 {
|
||||
if len(s.waitQueue) > 0 {
|
||||
s.ProcessQueue()
|
||||
}
|
||||
}
|
||||
return s.timestamps.LastTimestamp()
|
||||
}
|
||||
|
||||
// REQUESTS
|
||||
// суть у тому що треба запускати запити, пока не зустріну XLock
|
||||
func (s *Metric) ProcessQueue(metricID uint32, tmp []byte) {
|
||||
if len(s.waitQueue) == 0 {
|
||||
return
|
||||
}
|
||||
for _, untyped := range s.waitQueue {
|
||||
switch req := untyped.(type) {
|
||||
case RangeScanReq:
|
||||
s.StartRangeScan(req)
|
||||
case FullScanReq:
|
||||
s.StartFullScan(req)
|
||||
case GetMetricReq:
|
||||
s.GetMetric(req)
|
||||
case AppendMeasuresReq:
|
||||
metric.AppendMeasures(req, tmp, s.storageInbox)
|
||||
case DeleteMetricReq:
|
||||
s.DeleteMetric(req)
|
||||
case DeleteMeasuresReq:
|
||||
metric.DeleteMeasures(req)
|
||||
default:
|
||||
qb.Abort(qb.UnknownMetricWaitQueueItemBug,
|
||||
fmt.Errorf("bug: unknown metric wait queue item type %T", req))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (s *Metric) AppendMeasures(req AppendMeasuresReq, tmp []byte, storageInbox *inbox.Inbox) {
|
||||
if s.xLock || s.capturedState != nil {
|
||||
@@ -151,15 +175,14 @@ func (s *Metric) AppendMeasures(req AppendMeasuresReq, tmp []byte, storageInbox
|
||||
)
|
||||
|
||||
s.capturedState = &CapturedState{
|
||||
LastTimestamp: timestamps.LastTimestamp(),
|
||||
LastValue: s.lastValue,
|
||||
LastValue: s.lastValue,
|
||||
}
|
||||
|
||||
s.timestamps.CaptureState()
|
||||
s.values.CaptureState()
|
||||
|
||||
for idx, measure := range req.Measures {
|
||||
if measure.Timestamp <= s.timestamps.LastTimestamp() {
|
||||
if measure.Timestamp <= s.timestamps.Until() {
|
||||
resultCode = ExpiredMeasure
|
||||
break
|
||||
}
|
||||
@@ -270,15 +293,15 @@ func (s *Metric) AppendMeasures(req AppendMeasuresReq, tmp []byte, storageInbox
|
||||
}
|
||||
|
||||
func (s *Metric) DeleteMeasures(req DeleteMeasuresReq) {
|
||||
if s.xLock || s.capturedState != nil {
|
||||
s.waitQueue = append(s.waitQueue, req)
|
||||
return
|
||||
}
|
||||
since := s.timestamps.FirstTimestamp()
|
||||
until := s.timestamps.LastTimestamp()
|
||||
if since == 0 || (req.Since > 0 && until < req.Since) {
|
||||
req.ResultCh <- NoMeasuresToDelete
|
||||
}
|
||||
// if s.xLock || s.capturedState != nil {
|
||||
// s.waitQueue = append(s.waitQueue, req)
|
||||
// return
|
||||
// }
|
||||
// since := s.timestamps.FirstTimestamp()
|
||||
// until := s.timestamps.LastTimestamp()
|
||||
// if since == 0 || (req.Since > 0 && until < req.Since) {
|
||||
// req.ResultCh <- NoMeasuresToDelete
|
||||
// }
|
||||
// if s.RootPageNo > 0 {
|
||||
// req.ResultCh <- tryDeleteMeasuresResult{
|
||||
// ResultCode: DeleteFromAtreeRequired,
|
||||
@@ -296,36 +319,47 @@ func (s *Metric) StartRangeScan(req RangeScanReq) {
|
||||
s.waitQueue = append(s.waitQueue, req)
|
||||
return
|
||||
}
|
||||
// if s.timestamps.CommitedSize() == 0 {
|
||||
// req.ResultCh <- RangeScanResult{
|
||||
// ResultCode: QueryDone,
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
|
||||
// if req.Since > s.timestamps.LastTimestamp() {
|
||||
// req.ResultCh <- RangeScanResult{
|
||||
// ResultCode: QueryDone,
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
|
||||
// if req.Until < s.Since {
|
||||
// if s.RootPageNo > 0 {
|
||||
// req.ResultCh <- RangeScanResult{
|
||||
// ResultCode: UntilNotFound,
|
||||
// RootPageNo: s.RootPageNo,
|
||||
// FracDigits: s.fracDigits,
|
||||
// }
|
||||
// s.rLocks++
|
||||
// return
|
||||
// } else {
|
||||
// req.ResultCh <- RangeScanResult{
|
||||
// ResultCode: QueryDone,
|
||||
// }
|
||||
// return
|
||||
// }
|
||||
// }
|
||||
since := s.timestamps.CommitedSince()
|
||||
if since == 0 {
|
||||
req.ResultCh <- RangeScanResult{
|
||||
ResultCode: QueryDone,
|
||||
}
|
||||
return
|
||||
}
|
||||
// range after
|
||||
if req.Since > s.timestamps.CommitedUntil() {
|
||||
req.ResultCh <- RangeScanResult{
|
||||
ResultCode: QueryDone,
|
||||
}
|
||||
return
|
||||
}
|
||||
// range before
|
||||
if req.Until < since {
|
||||
if len(s.indexLevelTails) > 0 {
|
||||
pageNo, isDataPage := storage.FindPageOnIndexLevelTails(s.indexLevelTails, req.Until)
|
||||
if pageNo > 0 {
|
||||
req.ResultCh <- RangeScanResult{
|
||||
ResultCode: UntilNotFound,
|
||||
FracDigits: s.fracDigits,
|
||||
LastPageNo: pageNo,
|
||||
IsDataPage: isDataPage,
|
||||
}
|
||||
s.rLocks++
|
||||
return
|
||||
} else {
|
||||
// range until before 1st measure timestamp
|
||||
req.ResultCh <- RangeScanResult{
|
||||
ResultCode: QueryDone,
|
||||
}
|
||||
return
|
||||
}
|
||||
} else {
|
||||
req.ResultCh <- RangeScanResult{
|
||||
ResultCode: QueryDone,
|
||||
}
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
timestampDecompressor := s.timestamps.CreateDecompressor()
|
||||
valueDecompressor := s.values.CreateDecompressor(s.metricType, s.fracDigits)
|
||||
@@ -443,11 +477,6 @@ func (s *Metric) OnMeasuresDeleteCommited(rec storage.MeasuresDeleteCommited) {
|
||||
s.xLock = false
|
||||
// s.Timestamps.Renew()
|
||||
// s.Values.Renew()
|
||||
|
||||
// s.LastPageNo = 0
|
||||
// s.Since = 0
|
||||
// s.SinceValue = 0
|
||||
// s.Until = 0
|
||||
s.indexLevelTails = nil
|
||||
s.lastPageNo = 0
|
||||
s.lastValue = 0
|
||||
|
||||
@@ -5,7 +5,6 @@ import (
|
||||
"sync"
|
||||
|
||||
qb "gordenko.dev/dima/qb"
|
||||
"gordenko.dev/dima/qb/atree"
|
||||
"gordenko.dev/dima/qb/enc"
|
||||
"gordenko.dev/dima/qb/inbox"
|
||||
"gordenko.dev/dima/qb/proto"
|
||||
@@ -86,15 +85,8 @@ func New(opt Options) *Worker {
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Worker) ReleaseRLock(metricID uint32) {
|
||||
// s.mutex.Lock()
|
||||
// //s.rLocksToRelease = append(s.rLocksToRelease, metricID)
|
||||
// s.mutex.Unlock()
|
||||
|
||||
// select {
|
||||
// case s.signalCh <- struct{}{}:
|
||||
// default:
|
||||
// }
|
||||
type ReleaseRLock struct {
|
||||
MetricID uint32
|
||||
}
|
||||
|
||||
func (s *Worker) Run() {
|
||||
@@ -114,39 +106,12 @@ func (s *Worker) Run() {
|
||||
func (s *Worker) doWork() {
|
||||
queue := s.inbox.Drain()
|
||||
|
||||
//rLocksToRelease := s.rLocksToRelease
|
||||
//s.rLocksToRelease = nil
|
||||
|
||||
// for _, metricID := range rLocksToRelease {
|
||||
// metric, ok := s.metrics[metricID]
|
||||
// if !ok {
|
||||
// qb.Abort(qb.NoMetricBug,
|
||||
// fmt.Errorf("drainQueues: metric %d not found", metricID))
|
||||
// }
|
||||
|
||||
// if metric.XLock {
|
||||
// qb.Abort(qb.XLockBug,
|
||||
// fmt.Errorf("drainQueues: xlock is set for the metric %d",
|
||||
// metricID))
|
||||
// }
|
||||
|
||||
// if metric.RLocks <= 0 {
|
||||
// qb.Abort(qb.NoRLockBug,
|
||||
// fmt.Errorf("drainQueues: rlock not set for the metric %d",
|
||||
// metricID))
|
||||
// }
|
||||
|
||||
// metric.RLocks--
|
||||
|
||||
// if len(metric.WaitQueue) > 0 {
|
||||
// s.processMetricQueue(metricID, metric)
|
||||
// }
|
||||
// }
|
||||
|
||||
for _, untyped := range queue {
|
||||
switch req := untyped.(type) {
|
||||
case AppendMeasuresReq:
|
||||
s.AppendMeasures(req)
|
||||
case ReleaseRLock:
|
||||
s.releaseRLock(req.MetricID)
|
||||
case storage.Changes:
|
||||
s.applyCommits(req) // all metrics only
|
||||
case ListCurrentValuesReq:
|
||||
@@ -170,29 +135,10 @@ func (s *Worker) doWork() {
|
||||
}
|
||||
}
|
||||
|
||||
// суть у тому що треба запускати запити, пока не зустріну XLock
|
||||
func (s *Worker) processMetricQueue(metricID uint32, metric *Metric, tmp []byte) {
|
||||
if len(metric.waitQueue) == 0 {
|
||||
return
|
||||
}
|
||||
for _, untyped := range metric.waitQueue {
|
||||
switch req := untyped.(type) {
|
||||
case RangeScanReq:
|
||||
metric.StartRangeScan(req)
|
||||
case FullScanReq:
|
||||
metric.StartFullScan(req)
|
||||
case GetMetricReq:
|
||||
s.GetMetric(req)
|
||||
case AppendMeasuresReq:
|
||||
metric.AppendMeasures(req, tmp, s.storageInbox)
|
||||
case DeleteMetricReq:
|
||||
s.DeleteMetric(req)
|
||||
case DeleteMeasuresReq:
|
||||
metric.DeleteMeasures(req)
|
||||
default:
|
||||
qb.Abort(qb.UnknownMetricWaitQueueItemBug,
|
||||
fmt.Errorf("bug: unknown metric wait queue item type %T", req))
|
||||
}
|
||||
func (s *Worker) releaseRLock(metricID uint32) {
|
||||
metric, ok := s.metrics[metricID]
|
||||
if ok {
|
||||
metric.ReleaseRLock()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -318,8 +264,8 @@ func (s *Worker) AppendMeasures(req AppendMeasuresReq) {
|
||||
type RangeScanResult struct {
|
||||
ResultCode byte
|
||||
FracDigits byte
|
||||
RootPageNo uint32
|
||||
LastPageNo uint32
|
||||
IsDataPage bool // for UntilNotFound only
|
||||
}
|
||||
|
||||
type RangeScanReq struct {
|
||||
@@ -327,7 +273,7 @@ type RangeScanReq struct {
|
||||
Since uint32
|
||||
Until uint32
|
||||
MetricType qb.MetricType
|
||||
ResponseWriter atree.WorkerMeasureConsumer
|
||||
ResponseWriter qb.WorkerMeasureConsumer
|
||||
ResultCh chan RangeScanResult
|
||||
}
|
||||
|
||||
@@ -361,7 +307,7 @@ type FullScanResult struct {
|
||||
type FullScanReq struct {
|
||||
MetricID uint32
|
||||
MetricType qb.MetricType
|
||||
ResponseWriter atree.WorkerMeasureConsumer
|
||||
ResponseWriter qb.WorkerMeasureConsumer
|
||||
ResultCh chan FullScanResult
|
||||
}
|
||||
|
||||
@@ -398,7 +344,7 @@ func (s *Worker) ListCurrentValues(req ListCurrentValuesReq) {
|
||||
if ok {
|
||||
req.ResponseWriter.BufferValue(transform.CurrentValue{
|
||||
MetricID: metricID,
|
||||
Timestamp: metric.LastTimestamp(),
|
||||
Timestamp: metric.timestamps.CommitedUntil(),
|
||||
Value: metric.LastValue(),
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user