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

Commit 5904a9a

Browse files
committed
when inserting we want to adjust the ttl to make it relative to now
1 parent 55de017 commit 5904a9a

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

store/cassandra/cassandra.go

+16-1
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,21 @@ func (c *CassandraStore) insertChunk(key string, t0, ttl uint32, data []byte) er
355355
return nil
356356
}
357357

358+
// we calculate ttl like this:
359+
// - the chunk's t0 plus maxChunkSpan is the ts of last possible datapoint in the chunk
360+
// - the timestamp of the last datapoint + ttl is the timestamp until when we want to keep this chunk
361+
// - then we subtract the current time stamp to get the difference relative to now
362+
// - the result is the ttl in seconds relative to now
363+
relativeTtl := int64(t0+mdata.MaxChunkSpan()+ttl) - time.Now().Unix()
364+
365+
// if the ttl relative to now is <=0 then we can omit the insert
366+
if relativeTtl <= 0 {
367+
if log.IsLevelEnabled(log.DebugLevel) {
368+
log.Debugf("Omitting insert of chunk with ttl %d: %s, %d", relativeTtl, key, t0)
369+
}
370+
return nil
371+
}
372+
358373
table, ok := c.TTLTables[ttl]
359374
if !ok {
360375
return errTableNotFound
@@ -363,7 +378,7 @@ func (c *CassandraStore) insertChunk(key string, t0, ttl uint32, data []byte) er
363378
row_key := fmt.Sprintf("%s_%d", key, t0/Month_sec) // "month number" based on unix timestamp (rounded down)
364379
pre := time.Now()
365380
ctx, cancel := context.WithTimeout(context.Background(), c.timeout)
366-
ret := c.Session.Query(table.QueryWrite, row_key, t0, data, ttl).WithContext(ctx).Exec()
381+
ret := c.Session.Query(table.QueryWrite, row_key, t0, data, uint32(relativeTtl)).WithContext(ctx).Exec()
367382
cancel()
368383
cassPutExecDuration.Value(time.Now().Sub(pre))
369384
return ret

0 commit comments

Comments
 (0)