7286: Use ex_customdata to put the ping URL on the node instead of
[arvados.git] / services / keepstore / volume.go
1 package main
2
3 import (
4         "io"
5         "sync/atomic"
6         "time"
7 )
8
9 // A Volume is an interface representing a Keep back-end storage unit:
10 // for example, a single mounted disk, a RAID array, an Amazon S3 volume,
11 // etc.
12 type Volume interface {
13         // Get a block. IFF the returned error is nil, the caller must
14         // put the returned slice back into the buffer pool when it's
15         // finished with it. (Otherwise, the buffer pool will be
16         // depleted and eventually -- when all available buffers are
17         // used and not returned -- operations will reach deadlock.)
18         //
19         // loc is guaranteed to consist of 32 or more lowercase hex
20         // digits.
21         //
22         // Get should not verify the integrity of the returned data:
23         // it should just return whatever was found in its backing
24         // store. (Integrity checking is the caller's responsibility.)
25         //
26         // If an error is encountered that prevents it from
27         // retrieving the data, that error should be returned so the
28         // caller can log (and send to the client) a more useful
29         // message.
30         //
31         // If the error is "not found", and there's no particular
32         // reason to expect the block to be found (other than that a
33         // caller is asking for it), the returned error should satisfy
34         // os.IsNotExist(err): this is a normal condition and will not
35         // be logged as an error (except that a 404 will appear in the
36         // access log if the block is not found on any other volumes
37         // either).
38         //
39         // If the data in the backing store is bigger than BlockSize,
40         // Get is permitted to return an error without reading any of
41         // the data.
42         Get(loc string) ([]byte, error)
43
44         // Compare the given data with the stored data (i.e., what Get
45         // would return). If equal, return nil. If not, return
46         // CollisionError or DiskHashError (depending on whether the
47         // data on disk matches the expected hash), or whatever error
48         // was encountered opening/reading the stored data.
49         Compare(loc string, data []byte) error
50
51         // Put writes a block to an underlying storage device.
52         //
53         // loc is as described in Get.
54         //
55         // len(block) is guaranteed to be between 0 and BlockSize.
56         //
57         // If a block is already stored under the same name (loc) with
58         // different content, Put must either overwrite the existing
59         // data with the new data or return a non-nil error. When
60         // overwriting existing data, it must never leave the storage
61         // device in an inconsistent state: a subsequent call to Get
62         // must return either the entire old block, the entire new
63         // block, or an error. (An implementation that cannot peform
64         // atomic updates must leave the old data alone and return an
65         // error.)
66         //
67         // Put also sets the timestamp for the given locator to the
68         // current time.
69         //
70         // Put must return a non-nil error unless it can guarantee
71         // that the entire block has been written and flushed to
72         // persistent storage, and that its timestamp is current. Of
73         // course, this guarantee is only as good as the underlying
74         // storage device, but it is Put's responsibility to at least
75         // get whatever guarantee is offered by the storage device.
76         //
77         // Put should not verify that loc==hash(block): this is the
78         // caller's responsibility.
79         Put(loc string, block []byte) error
80
81         // Touch sets the timestamp for the given locator to the
82         // current time.
83         //
84         // loc is as described in Get.
85         //
86         // If invoked at time t0, Touch must guarantee that a
87         // subsequent call to Mtime will return a timestamp no older
88         // than {t0 minus one second}. For example, if Touch is called
89         // at 2015-07-07T01:23:45.67890123Z, it is acceptable for a
90         // subsequent Mtime to return any of the following:
91         //
92         //   - 2015-07-07T01:23:45.00000000Z
93         //   - 2015-07-07T01:23:45.67890123Z
94         //   - 2015-07-07T01:23:46.67890123Z
95         //   - 2015-07-08T00:00:00.00000000Z
96         //
97         // It is not acceptable for a subsequente Mtime to return
98         // either of the following:
99         //
100         //   - 2015-07-07T00:00:00.00000000Z -- ERROR
101         //   - 2015-07-07T01:23:44.00000000Z -- ERROR
102         //
103         // Touch must return a non-nil error if the timestamp cannot
104         // be updated.
105         Touch(loc string) error
106
107         // Mtime returns the stored timestamp for the given locator.
108         //
109         // loc is as described in Get.
110         //
111         // Mtime must return a non-nil error if the given block is not
112         // found or the timestamp could not be retrieved.
113         Mtime(loc string) (time.Time, error)
114
115         // IndexTo writes a complete list of locators with the given
116         // prefix for which Get() can retrieve data.
117         //
118         // prefix consists of zero or more lowercase hexadecimal
119         // digits.
120         //
121         // Each locator must be written to the given writer using the
122         // following format:
123         //
124         //   loc "+" size " " timestamp "\n"
125         //
126         // where:
127         //
128         //   - size is the number of bytes of content, given as a
129         //     decimal number with one or more digits
130         //
131         //   - timestamp is the timestamp stored for the locator,
132         //     given as a decimal number of seconds after January 1,
133         //     1970 UTC.
134         //
135         // IndexTo must not write any other data to writer: for
136         // example, it must not write any blank lines.
137         //
138         // If an error makes it impossible to provide a complete
139         // index, IndexTo must return a non-nil error. It is
140         // acceptable to return a non-nil error after writing a
141         // partial index to writer.
142         //
143         // The resulting index is not expected to be sorted in any
144         // particular order.
145         IndexTo(prefix string, writer io.Writer) error
146
147         // Delete deletes the block data from the underlying storage
148         // device.
149         //
150         // loc is as described in Get.
151         //
152         // If the timestamp for the given locator is newer than
153         // blobSignatureTTL, Delete must not delete the data.
154         //
155         // If a Delete operation overlaps with any Touch or Put
156         // operations on the same locator, the implementation must
157         // ensure one of the following outcomes:
158         //
159         //   - Touch and Put return a non-nil error, or
160         //   - Delete does not delete the block, or
161         //   - Both of the above.
162         //
163         // If it is possible for the storage device to be accessed by
164         // a different process or host, the synchronization mechanism
165         // should also guard against races with other processes and
166         // hosts. If such a mechanism is not available, there must be
167         // a mechanism for detecting unsafe configurations, alerting
168         // the operator, and aborting or falling back to a read-only
169         // state. In other words, running multiple keepstore processes
170         // with the same underlying storage device must either work
171         // reliably or fail outright.
172         //
173         // Corollary: A successful Touch or Put guarantees a block
174         // will not be deleted for at least blobSignatureTTL
175         // seconds.
176         Delete(loc string) error
177
178         // Status returns a *VolumeStatus representing the current
179         // in-use and available storage capacity and an
180         // implementation-specific volume identifier (e.g., "mount
181         // point" for a UnixVolume).
182         Status() *VolumeStatus
183
184         // String returns an identifying label for this volume,
185         // suitable for including in log messages. It should contain
186         // enough information to uniquely identify the underlying
187         // storage device, but should not contain any credentials or
188         // secrets.
189         String() string
190
191         // Writable returns false if all future Put, Mtime, and Delete
192         // calls are expected to fail.
193         //
194         // If the volume is only temporarily unwritable -- or if Put
195         // will fail because it is full, but Mtime or Delete can
196         // succeed -- then Writable should return false.
197         Writable() bool
198 }
199
200 // A VolumeManager tells callers which volumes can read, which volumes
201 // can write, and on which volume the next write should be attempted.
202 type VolumeManager interface {
203         // AllReadable returns all volumes.
204         AllReadable() []Volume
205
206         // AllWritable returns all volumes that aren't known to be in
207         // a read-only state. (There is no guarantee that a write to
208         // one will succeed, though.)
209         AllWritable() []Volume
210
211         // NextWritable returns the volume where the next new block
212         // should be written. A VolumeManager can select a volume in
213         // order to distribute activity across spindles, fill up disks
214         // with more free space, etc.
215         NextWritable() Volume
216
217         // Close shuts down the volume manager cleanly.
218         Close()
219 }
220
221 // RRVolumeManager is a round-robin VolumeManager: the Nth call to
222 // NextWritable returns the (N % len(writables))th writable Volume
223 // (where writables are all Volumes v where v.Writable()==true).
224 type RRVolumeManager struct {
225         readables []Volume
226         writables []Volume
227         counter   uint32
228 }
229
230 // MakeRRVolumeManager initializes RRVolumeManager
231 func MakeRRVolumeManager(volumes []Volume) *RRVolumeManager {
232         vm := &RRVolumeManager{}
233         for _, v := range volumes {
234                 vm.readables = append(vm.readables, v)
235                 if v.Writable() {
236                         vm.writables = append(vm.writables, v)
237                 }
238         }
239         return vm
240 }
241
242 // AllReadable returns an array of all readable volumes
243 func (vm *RRVolumeManager) AllReadable() []Volume {
244         return vm.readables
245 }
246
247 // AllWritable returns an array of all writable volumes
248 func (vm *RRVolumeManager) AllWritable() []Volume {
249         return vm.writables
250 }
251
252 // NextWritable returns the next writable
253 func (vm *RRVolumeManager) NextWritable() Volume {
254         if len(vm.writables) == 0 {
255                 return nil
256         }
257         i := atomic.AddUint32(&vm.counter, 1)
258         return vm.writables[i%uint32(len(vm.writables))]
259 }
260
261 // Close the RRVolumeManager
262 func (vm *RRVolumeManager) Close() {
263 }
264
265 // VolumeStatus provides status information of the volume consisting of:
266 //   * mount_point
267 //   * device_num (an integer identifying the underlying storage system)
268 //   * bytes_free
269 //   * bytes_used
270 type VolumeStatus struct {
271         MountPoint string `json:"mount_point"`
272         DeviceNum  uint64 `json:"device_num"`
273         BytesFree  uint64 `json:"bytes_free"`
274         BytesUsed  uint64 `json:"bytes_used"`
275 }