@@ -21,11 +21,21 @@ import (
2121
2222 "github.com/lf-edge/ekuiper/internal/conf"
2323 "github.com/lf-edge/ekuiper/internal/pkg/store"
24+ "github.com/lf-edge/ekuiper/metrics"
2425 "github.com/lf-edge/ekuiper/pkg/api"
2526 "github.com/lf-edge/ekuiper/pkg/infra"
2627 "github.com/lf-edge/ekuiper/pkg/kv"
2728)
2829
30+ const (
31+ addLbl = "add"
32+ sendLbl = "send"
33+ delLbl = "del"
34+ ackLbl = "ack"
35+ loadLbl = "load"
36+ flushLbl = "flush"
37+ )
38+
2939// page Rotates storage for in memory cache
3040// Not thread safe!
3141type page struct {
@@ -92,6 +102,8 @@ func (p *page) reset() {
92102}
93103
94104type SyncCache struct {
105+ ruleID string
106+ opID string
95107 // The input data to the cache
96108 in <- chan []map [string ]interface {}
97109 Out chan []map [string ]interface {}
@@ -119,6 +131,8 @@ type SyncCache struct {
119131func NewSyncCacheWithExitChanel (ctx api.StreamContext , in <- chan []map [string ]interface {}, errCh chan <- error , cacheConf * conf.SinkConf , bufferLength int , exitCh chan <- struct {}) * SyncCache {
120132 c := NewSyncCache (ctx , in , errCh , cacheConf , bufferLength )
121133 c .exitCh = exitCh
134+ c .ruleID = ctx .GetRuleId ()
135+ c .opID = ctx .GetOpId ()
122136 return c
123137}
124138
@@ -171,6 +185,7 @@ func (c *SyncCache) run(ctx api.StreamContext) {
171185 c .send (ctx )
172186 }
173187 case isSuccess := <- c .Ack :
188+ metrics .SyncCacheOpCnter .WithLabelValues (ackLbl , c .ruleID , c .opID ).Inc ()
174189 // only send the next sink after receiving an ack
175190 ctx .GetLogger ().Debugf ("cache ack" )
176191 if isSuccess {
@@ -194,6 +209,7 @@ func (c *SyncCache) run(ctx api.StreamContext) {
194209}
195210
196211func (c * SyncCache ) send (ctx api.StreamContext ) {
212+ metrics .SyncCacheOpCnter .WithLabelValues (sendLbl , c .ruleID , c .opID ).Inc ()
197213 if c .CacheLength > 1 && c .cacheConf .ResendInterval > 0 {
198214 time .Sleep (time .Duration (c .cacheConf .ResendInterval ) * time .Millisecond )
199215 }
@@ -215,6 +231,7 @@ func (c *SyncCache) send(ctx api.StreamContext) {
215231
216232// addCache not thread safe!
217233func (c * SyncCache ) addCache (ctx api.StreamContext , item []map [string ]interface {}) {
234+ metrics .SyncCacheOpCnter .WithLabelValues (addLbl , c .ruleID , c .opID ).Inc ()
218235 isNotFull := c .appendMemCache (item )
219236 if ! isNotFull {
220237 if c .diskBufferPage == nil {
@@ -227,6 +244,10 @@ func (c *SyncCache) addCache(ctx api.StreamContext, item []map[string]interface{
227244 c .loadFromDisk (ctx )
228245 ctx .GetLogger ().Debug ("disk full, remove the last page" )
229246 }
247+ start := time .Now ()
248+ defer func () {
249+ metrics .SyncCacheDurationHist .WithLabelValues (flushLbl , c .ruleID , c .opID ).Observe (float64 (time .Since (start ).Microseconds ()))
250+ }()
230251 err := c .store .Set (strconv .Itoa (c .diskPageTail ), c .diskBufferPage )
231252 if err != nil {
232253 ctx .GetLogger ().Errorf ("fail to store disk cache %v" , err )
@@ -258,6 +279,7 @@ func (c *SyncCache) addCache(ctx api.StreamContext, item []map[string]interface{
258279
259280// deleteCache not thread safe!
260281func (c * SyncCache ) deleteCache (ctx api.StreamContext ) {
282+ metrics .SyncCacheOpCnter .WithLabelValues (delLbl , c .ruleID , c .opID ).Inc ()
261283 ctx .GetLogger ().Debugf ("deleting cache. CacheLength: %d, diskSize: %d" , c .CacheLength , c .diskSize )
262284 if len (c .memCache ) == 0 {
263285 ctx .GetLogger ().Debug ("mem cache is empty" )
@@ -282,6 +304,11 @@ func (c *SyncCache) deleteCache(ctx api.StreamContext) {
282304}
283305
284306func (c * SyncCache ) loadFromDisk (ctx api.StreamContext ) {
307+ metrics .SyncCacheOpCnter .WithLabelValues (loadLbl , c .ruleID , c .opID ).Inc ()
308+ start := time .Now ()
309+ defer func () {
310+ metrics .SyncCacheDurationHist .WithLabelValues (loadLbl , c .ruleID , c .opID ).Observe (float64 (time .Since (start ).Microseconds ()))
311+ }()
285312 // load page from the disk
286313 ctx .GetLogger ().Debugf ("loading from disk %d. CacheLength: %d, diskSize: %d" , c .diskPageTail , c .CacheLength , c .diskSize )
287314 hotPage := newPage (c .cacheConf .BufferPageSize )
0 commit comments