metadata.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*
  2. Copyright 2017 Google Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. // Package metadata describes metadata paths stored in the cache per target.
  14. package metadata
  15. import (
  16. "errors"
  17. "sync"
  18. )
  19. const (
  20. // Root node where metadata is cached.
  21. Root = "meta"
  22. // Per-target metadata
  23. // Sync is a boolean that reports whether all target state is cached.
  24. Sync = "sync"
  25. // Connected is a boolean that reports whether updates are being received.
  26. Connected = "connected"
  27. // AddCount is the total number of leaves that have been added.
  28. AddCount = "targetLeavesAdded"
  29. // DelCount is the total number of leaves that have been deleted.
  30. DelCount = "targetLeavesDeleted"
  31. // LeafCount is the current total leaves stored in the cache.
  32. LeafCount = "targetLeaves"
  33. // UpdateCount is the total number of leaf updates received.
  34. UpdateCount = "targetLeavesUpdated"
  35. // LatencyAvg is the average latency between target timestamp and cache
  36. // reception.
  37. LatencyAvg = "latencyAvg"
  38. // LatencyMax is the maximum latency between target timestamp and cache
  39. // reception.
  40. LatencyMax = "latencyMax"
  41. // LatencyMin is the minimum latency between target timestamp and cache
  42. // reception.
  43. LatencyMin = "latencyMin"
  44. // Size is the total number of bytes used to store all values. This count
  45. // excludes all indexing overhead.
  46. Size = "targetSize"
  47. // LatestTimestamp is the latest timestamp for any update received for the
  48. // target.
  49. LatestTimestamp = "latestTimestamp"
  50. )
  51. var (
  52. // TargetBoolValues is the list of all bool metadata fields.
  53. TargetBoolValues = map[string]bool{
  54. Sync: true,
  55. Connected: true,
  56. }
  57. // TargetIntValues is the list of all int64 metadata fields.
  58. TargetIntValues = map[string]bool{
  59. AddCount: true,
  60. DelCount: true,
  61. LeafCount: true,
  62. UpdateCount: true,
  63. LatencyAvg: true,
  64. LatencyMax: true,
  65. LatencyMin: true,
  66. Size: true,
  67. LatestTimestamp: true,
  68. }
  69. )
  70. // Metadata is the container for all target specific metadata.
  71. type Metadata struct {
  72. mu sync.Mutex
  73. valuesInt map[string]int64
  74. valuesBool map[string]bool
  75. }
  76. // Path is a convenience function that will return the full metadata path for
  77. // any valid metadata value. Only metadata values registered above in
  78. // TargetBoolValues and TargetIntValues will return a path. An invalid metadata
  79. // value will return nil.
  80. func Path(value string) []string {
  81. if TargetBoolValues[value] || TargetIntValues[value] {
  82. return []string{Root, value}
  83. }
  84. return nil
  85. }
  86. // New returns an initialized Metadata structure. Integer values are
  87. // initialized to 0 and boolean values are initialized to false.
  88. func New() *Metadata {
  89. m := Metadata{
  90. valuesInt: make(map[string]int64, len(TargetIntValues)),
  91. valuesBool: make(map[string]bool, len(TargetBoolValues)),
  92. }
  93. m.Clear()
  94. return &m
  95. }
  96. // ErrInvalidValue is returned when a metadata operation is attempted on a value
  97. // that does not exist.
  98. var ErrInvalidValue = errors.New("invalid metadata value")
  99. func validInt(value string) error {
  100. if valid := TargetIntValues[value]; !valid {
  101. return ErrInvalidValue
  102. }
  103. return nil
  104. }
  105. func validBool(value string) error {
  106. if valid := TargetBoolValues[value]; !valid {
  107. return ErrInvalidValue
  108. }
  109. return nil
  110. }
  111. // Clear sets all metadata values to zero values.
  112. func (m *Metadata) Clear() {
  113. m.mu.Lock()
  114. defer m.mu.Unlock()
  115. for k := range TargetBoolValues {
  116. m.valuesBool[k] = false
  117. }
  118. for k := range TargetIntValues {
  119. m.valuesInt[k] = 0
  120. }
  121. }
  122. // AddInt atomically increments the metadata value specified by i.
  123. func (m *Metadata) AddInt(value string, i int64) error {
  124. if err := validInt(value); err != nil {
  125. return err
  126. }
  127. m.mu.Lock()
  128. m.valuesInt[value] += i
  129. m.mu.Unlock()
  130. return nil
  131. }
  132. // SetInt atomically sets the metadata value specified to v.
  133. func (m *Metadata) SetInt(value string, v int64) error {
  134. if err := validInt(value); err != nil {
  135. return err
  136. }
  137. m.mu.Lock()
  138. m.valuesInt[value] = v
  139. m.mu.Unlock()
  140. return nil
  141. }
  142. // ErrUnsetValue is returned when a metadata Get is attempted on a value that
  143. // has not been Set (or Added).
  144. var ErrUnsetValue = errors.New("unset value")
  145. // GetInt atomically retrieves the metadata value specified.
  146. func (m *Metadata) GetInt(value string) (int64, error) {
  147. if err := validInt(value); err != nil {
  148. return 0, err
  149. }
  150. m.mu.Lock()
  151. v, ok := m.valuesInt[value]
  152. m.mu.Unlock()
  153. if !ok {
  154. return 0, ErrUnsetValue
  155. }
  156. return v, nil
  157. }
  158. // SetBool atomically sets the metadata value specified to v.
  159. func (m *Metadata) SetBool(value string, v bool) error {
  160. if err := validBool(value); err != nil {
  161. return err
  162. }
  163. m.mu.Lock()
  164. m.valuesBool[value] = v
  165. m.mu.Unlock()
  166. return nil
  167. }
  168. // GetBool atomically retrieves the metadata value specified.
  169. func (m *Metadata) GetBool(value string) (bool, error) {
  170. if err := validBool(value); err != nil {
  171. return false, err
  172. }
  173. m.mu.Lock()
  174. v, ok := m.valuesBool[value]
  175. m.mu.Unlock()
  176. if !ok {
  177. return false, ErrUnsetValue
  178. }
  179. return v, nil
  180. }