forked from kubernetes-retired/heapster
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathin_memory.go
134 lines (123 loc) · 3.53 KB
/
in_memory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package store
import (
"container/list"
"fmt"
"sync"
"time"
"github.com/golang/glog"
)
// TODO: Consider using cadvisor's in memory storage instead.
type timeStore struct {
// A list that will contain all the timeStore entries.
// This list is in reverse chronological order.
// E.x. [4, 3, 1]
// list.Front refers to the most recent entry and list.Back refers to the oldest entry.
buffer *list.List
rwLock sync.RWMutex
}
func (ts *timeStore) Put(tp TimePoint) error {
if tp.Value == nil {
return fmt.Errorf("cannot store TimePoint with nil data")
}
if (tp.Timestamp == time.Time{}) {
return fmt.Errorf("cannot store TimePoint with zero timestamp")
}
ts.rwLock.Lock()
defer ts.rwLock.Unlock()
if ts.buffer.Len() == 0 {
glog.V(5).Infof("put pushfront: %v, %v", tp.Timestamp, tp.Value)
ts.buffer.PushFront(tp)
return nil
}
for elem := ts.buffer.Front(); elem != nil; elem = elem.Next() {
if tp.Timestamp.After(elem.Value.(TimePoint).Timestamp) {
glog.V(5).Infof("put insert before: %v, %v, %v", elem, tp.Timestamp, tp.Value)
ts.buffer.InsertBefore(tp, elem)
return nil
}
}
glog.V(5).Infof("put pushback: %v, %v", tp.Timestamp, tp.Value)
ts.buffer.PushBack(tp)
return nil
}
// Returns true if 't1' is equal to or before 't2'
func timeEqualOrBefore(t1, t2 time.Time) bool {
if t1.Equal(t2) || t1.Before(t2) {
return true
}
return false
}
// Returns true if 't1' is equal to or after 't2'
func timeEqualOrAfter(t1, t2 time.Time) bool {
if t1.Equal(t2) || t1.After(t2) {
return true
}
return false
}
func (ts *timeStore) Get(start, end time.Time) []TimePoint {
ts.rwLock.RLock()
defer ts.rwLock.RUnlock()
if ts.buffer.Len() == 0 {
return nil
}
zeroTime := time.Time{}
result := []TimePoint{}
for elem := ts.buffer.Front(); elem != nil; elem = elem.Next() {
entry := elem.Value.(TimePoint)
// Break the loop if we encounter a timestamp that is before 'start'
if entry.Timestamp.Before(start) {
break
}
// Add all entries whose timestamp is before end.
if end != zeroTime && entry.Timestamp.After(end) {
continue
}
result = append(result, entry)
}
return result
}
func (ts *timeStore) Delete(start, end time.Time) error {
ts.rwLock.Lock()
defer ts.rwLock.Unlock()
if ts.buffer.Len() == 0 {
return nil
}
if (end != time.Time{}) && !end.After(start) {
return fmt.Errorf("end time %v is not after start time %v", end, start)
}
// Assuming that deletes will happen more frequently for older data.
elem := ts.buffer.Back()
for elem != nil {
entry := elem.Value.(TimePoint)
if (end != time.Time{}) && entry.Timestamp.After(end) {
// If we have reached an entry which is more recent than 'end' stop iterating.
break
}
oldElem := elem
elem = elem.Prev()
// Skip entried which are before start.
if !entry.Timestamp.Before(start) {
ts.buffer.Remove(oldElem)
}
}
return nil
}
func NewTimeStore() TimeStore {
return &timeStore{
rwLock: sync.RWMutex{},
buffer: list.New(),
}
}