| OLD | NEW |
| 1 // Copyright 2016 The LUCI Authors. All rights reserved. | 1 // Copyright 2016 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 package bigtable | 5 package bigtable |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | 8 "bytes" |
| 9 "encoding/binary" | 9 "encoding/binary" |
| 10 "time" | 10 "time" |
| 11 | 11 |
| 12 "github.com/luci/luci-go/common/config" | |
| 13 log "github.com/luci/luci-go/common/logging" | 12 log "github.com/luci/luci-go/common/logging" |
| 14 "github.com/luci/luci-go/logdog/common/storage/caching" | 13 "github.com/luci/luci-go/logdog/common/storage/caching" |
| 15 "github.com/luci/luci-go/logdog/common/types" | 14 "github.com/luci/luci-go/logdog/common/types" |
| 15 "github.com/luci/luci-go/luci_config/common/cfgtypes" |
| 16 | 16 |
| 17 "golang.org/x/net/context" | 17 "golang.org/x/net/context" |
| 18 ) | 18 ) |
| 19 | 19 |
| 20 // cacheSchema represents the cache schema used by this version of the tail | 20 // cacheSchema represents the cache schema used by this version of the tail |
| 21 // cache. If the underlying data format changes, this value must also be | 21 // cache. If the underlying data format changes, this value must also be |
| 22 // updated. | 22 // updated. |
| 23 const cacheSchema = "v1" | 23 const cacheSchema = "v1" |
| 24 | 24 |
| 25 // lastTailIndexCacheDuration is the amount of time that the last tail index | 25 // lastTailIndexCacheDuration is the amount of time that the last tail index |
| 26 // should be cached. | 26 // should be cached. |
| 27 const lastTailIndexCacheDuration = 1 * time.Hour | 27 const lastTailIndexCacheDuration = 1 * time.Hour |
| 28 | 28 |
| 29 // getLastTailIndex will return the cached last tail index of a given stream. | 29 // getLastTailIndex will return the cached last tail index of a given stream. |
| 30 // | 30 // |
| 31 // If there was an error, or if the item was not cached, 0 (first index) will be | 31 // If there was an error, or if the item was not cached, 0 (first index) will be |
| 32 // returned. | 32 // returned. |
| 33 func getLastTailIndex(c context.Context, cache caching.Cache, project config.Pro
jectName, path types.StreamPath) int64 { | 33 func getLastTailIndex(c context.Context, cache caching.Cache, project cfgtypes.P
rojectName, path types.StreamPath) int64 { |
| 34 itm := mkLastTailItem(project, path) | 34 itm := mkLastTailItem(project, path) |
| 35 cache.Get(c, itm) | 35 cache.Get(c, itm) |
| 36 if itm.Data == nil { | 36 if itm.Data == nil { |
| 37 return 0 | 37 return 0 |
| 38 } | 38 } |
| 39 | 39 |
| 40 v, err := binary.ReadVarint(bytes.NewReader(itm.Data)) | 40 v, err := binary.ReadVarint(bytes.NewReader(itm.Data)) |
| 41 if err != nil { | 41 if err != nil { |
| 42 log.Fields{ | 42 log.Fields{ |
| 43 log.ErrorKey: err, | 43 log.ErrorKey: err, |
| 44 "project": project, | 44 "project": project, |
| 45 "path": path, | 45 "path": path, |
| 46 }.Warningf(c, "Could not decode last tail cache.") | 46 }.Warningf(c, "Could not decode last tail cache.") |
| 47 return 0 | 47 return 0 |
| 48 } | 48 } |
| 49 | 49 |
| 50 log.Fields{ | 50 log.Fields{ |
| 51 "index": v, | 51 "index": v, |
| 52 }.Infof(c, "Using cached tail index.") | 52 }.Infof(c, "Using cached tail index.") |
| 53 return v | 53 return v |
| 54 } | 54 } |
| 55 | 55 |
| 56 func putLastTailIndex(c context.Context, cache caching.Cache, project config.Pro
jectName, path types.StreamPath, v int64) { | 56 func putLastTailIndex(c context.Context, cache caching.Cache, project cfgtypes.P
rojectName, path types.StreamPath, v int64) { |
| 57 buf := make([]byte, binary.MaxVarintLen64) | 57 buf := make([]byte, binary.MaxVarintLen64) |
| 58 buf = buf[:binary.PutVarint(buf, v)] | 58 buf = buf[:binary.PutVarint(buf, v)] |
| 59 | 59 |
| 60 itm := mkLastTailItem(project, path) | 60 itm := mkLastTailItem(project, path) |
| 61 itm.Data = buf | 61 itm.Data = buf |
| 62 cache.Put(c, lastTailIndexCacheDuration, itm) | 62 cache.Put(c, lastTailIndexCacheDuration, itm) |
| 63 } | 63 } |
| 64 | 64 |
| 65 func mkLastTailItem(project config.ProjectName, path types.StreamPath) *caching.
Item { | 65 func mkLastTailItem(project cfgtypes.ProjectName, path types.StreamPath) *cachin
g.Item { |
| 66 return &caching.Item{ | 66 return &caching.Item{ |
| 67 Schema: cacheSchema, | 67 Schema: cacheSchema, |
| 68 Type: "bt_tail_idx", | 68 Type: "bt_tail_idx", |
| 69 Key: caching.HashKey(string(project), string(path)), | 69 Key: caching.HashKey(string(project), string(path)), |
| 70 } | 70 } |
| 71 } | 71 } |
| OLD | NEW |