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