event_history.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2015 The etcd Authors
  2. //
  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. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package store
  15. import (
  16. "fmt"
  17. "path"
  18. "strings"
  19. "sync"
  20. etcdErr "github.com/coreos/etcd/error"
  21. )
  22. type EventHistory struct {
  23. Queue eventQueue
  24. StartIndex uint64
  25. LastIndex uint64
  26. rwl sync.RWMutex
  27. }
  28. func newEventHistory(capacity int) *EventHistory {
  29. return &EventHistory{
  30. Queue: eventQueue{
  31. Capacity: capacity,
  32. Events: make([]*Event, capacity),
  33. },
  34. }
  35. }
  36. // addEvent function adds event into the eventHistory
  37. func (eh *EventHistory) addEvent(e *Event) *Event {
  38. eh.rwl.Lock()
  39. defer eh.rwl.Unlock()
  40. eh.Queue.insert(e)
  41. eh.LastIndex = e.Index()
  42. eh.StartIndex = eh.Queue.Events[eh.Queue.Front].Index()
  43. return e
  44. }
  45. // scan enumerates events from the index history and stops at the first point
  46. // where the key matches.
  47. func (eh *EventHistory) scan(key string, recursive bool, index uint64) (*Event, *etcdErr.Error) {
  48. eh.rwl.RLock()
  49. defer eh.rwl.RUnlock()
  50. // index should be after the event history's StartIndex
  51. if index < eh.StartIndex {
  52. return nil,
  53. etcdErr.NewError(etcdErr.EcodeEventIndexCleared,
  54. fmt.Sprintf("the requested history has been cleared [%v/%v]",
  55. eh.StartIndex, index), 0)
  56. }
  57. // the index should come before the size of the queue minus the duplicate count
  58. if index > eh.LastIndex { // future index
  59. return nil, nil
  60. }
  61. offset := index - eh.StartIndex
  62. i := (eh.Queue.Front + int(offset)) % eh.Queue.Capacity
  63. for {
  64. e := eh.Queue.Events[i]
  65. if !e.Refresh {
  66. ok := (e.Node.Key == key)
  67. if recursive {
  68. // add tailing slash
  69. nkey := path.Clean(key)
  70. if nkey[len(nkey)-1] != '/' {
  71. nkey = nkey + "/"
  72. }
  73. ok = ok || strings.HasPrefix(e.Node.Key, nkey)
  74. }
  75. if (e.Action == Delete || e.Action == Expire) && e.PrevNode != nil && e.PrevNode.Dir {
  76. ok = ok || strings.HasPrefix(key, e.PrevNode.Key)
  77. }
  78. if ok {
  79. return e, nil
  80. }
  81. }
  82. i = (i + 1) % eh.Queue.Capacity
  83. if i == eh.Queue.Back {
  84. return nil, nil
  85. }
  86. }
  87. }
  88. // clone will be protected by a stop-world lock
  89. // do not need to obtain internal lock
  90. func (eh *EventHistory) clone() *EventHistory {
  91. clonedQueue := eventQueue{
  92. Capacity: eh.Queue.Capacity,
  93. Events: make([]*Event, eh.Queue.Capacity),
  94. Size: eh.Queue.Size,
  95. Front: eh.Queue.Front,
  96. Back: eh.Queue.Back,
  97. }
  98. copy(clonedQueue.Events, eh.Queue.Events)
  99. return &EventHistory{
  100. StartIndex: eh.StartIndex,
  101. Queue: clonedQueue,
  102. LastIndex: eh.LastIndex,
  103. }
  104. }