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,
12 type Volume interface {
13 // Get a block: copy the block data into buf, and return the
14 // number of bytes copied.
16 // loc is guaranteed to consist of 32 or more lowercase hex
19 // Get should not verify the integrity of the data: it should
20 // just return whatever was found in its backing
21 // store. (Integrity checking is the caller's responsibility.)
23 // If an error is encountered that prevents it from
24 // retrieving the data, that error should be returned so the
25 // caller can log (and send to the client) a more useful
28 // If the error is "not found", and there's no particular
29 // reason to expect the block to be found (other than that a
30 // caller is asking for it), the returned error should satisfy
31 // os.IsNotExist(err): this is a normal condition and will not
32 // be logged as an error (except that a 404 will appear in the
33 // access log if the block is not found on any other volumes
36 // If the data in the backing store is bigger than len(buf),
37 // then Get is permitted to return an error without reading
40 // len(buf) will not exceed BlockSize.
41 Get(loc string, buf []byte) (int, error)
43 // Compare the given data with the stored data (i.e., what Get
44 // would return). If equal, return nil. If not, return
45 // CollisionError or DiskHashError (depending on whether the
46 // data on disk matches the expected hash), or whatever error
47 // was encountered opening/reading the stored data.
48 Compare(loc string, data []byte) error
50 // Put writes a block to an underlying storage device.
52 // loc is as described in Get.
54 // len(block) is guaranteed to be between 0 and BlockSize.
56 // If a block is already stored under the same name (loc) with
57 // different content, Put must either overwrite the existing
58 // data with the new data or return a non-nil error. When
59 // overwriting existing data, it must never leave the storage
60 // device in an inconsistent state: a subsequent call to Get
61 // must return either the entire old block, the entire new
62 // block, or an error. (An implementation that cannot peform
63 // atomic updates must leave the old data alone and return an
66 // Put also sets the timestamp for the given locator to the
69 // Put must return a non-nil error unless it can guarantee
70 // that the entire block has been written and flushed to
71 // persistent storage, and that its timestamp is current. Of
72 // course, this guarantee is only as good as the underlying
73 // storage device, but it is Put's responsibility to at least
74 // get whatever guarantee is offered by the storage device.
76 // Put should not verify that loc==hash(block): this is the
77 // caller's responsibility.
78 Put(loc string, block []byte) error
80 // Touch sets the timestamp for the given locator to the
83 // loc is as described in Get.
85 // If invoked at time t0, Touch must guarantee that a
86 // subsequent call to Mtime will return a timestamp no older
87 // than {t0 minus one second}. For example, if Touch is called
88 // at 2015-07-07T01:23:45.67890123Z, it is acceptable for a
89 // subsequent Mtime to return any of the following:
91 // - 2015-07-07T01:23:45.00000000Z
92 // - 2015-07-07T01:23:45.67890123Z
93 // - 2015-07-07T01:23:46.67890123Z
94 // - 2015-07-08T00:00:00.00000000Z
96 // It is not acceptable for a subsequente Mtime to return
97 // either of the following:
99 // - 2015-07-07T00:00:00.00000000Z -- ERROR
100 // - 2015-07-07T01:23:44.00000000Z -- ERROR
102 // Touch must return a non-nil error if the timestamp cannot
104 Touch(loc string) error
106 // Mtime returns the stored timestamp for the given locator.
108 // loc is as described in Get.
110 // Mtime must return a non-nil error if the given block is not
111 // found or the timestamp could not be retrieved.
112 Mtime(loc string) (time.Time, error)
114 // IndexTo writes a complete list of locators with the given
115 // prefix for which Get() can retrieve data.
117 // prefix consists of zero or more lowercase hexadecimal
120 // Each locator must be written to the given writer using the
123 // loc "+" size " " timestamp "\n"
127 // - size is the number of bytes of content, given as a
128 // decimal number with one or more digits
130 // - timestamp is the timestamp stored for the locator,
131 // given as a decimal number of seconds after January 1,
134 // IndexTo must not write any other data to writer: for
135 // example, it must not write any blank lines.
137 // If an error makes it impossible to provide a complete
138 // index, IndexTo must return a non-nil error. It is
139 // acceptable to return a non-nil error after writing a
140 // partial index to writer.
142 // The resulting index is not expected to be sorted in any
144 IndexTo(prefix string, writer io.Writer) error
146 // Trash moves the block data from the underlying storage
147 // device to trash area. The block then stays in trash for
148 // -trash-lifetime interval before it is actually deleted.
150 // loc is as described in Get.
152 // If the timestamp for the given locator is newer than
153 // blobSignatureTTL, Trash must not trash the data.
155 // If a Trash operation overlaps with any Touch or Put
156 // operations on the same locator, the implementation must
157 // ensure one of the following outcomes:
159 // - Touch and Put return a non-nil error, or
160 // - Trash does not trash the block, or
161 // - Both of the above.
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.
173 // Corollary: A successful Touch or Put guarantees a block
174 // will not be trashed for at least blobSignatureTTL
176 Trash(loc string) error
178 // Untrash moves block from trash back into store
179 Untrash(loc string) error
181 // Status returns a *VolumeStatus representing the current
182 // in-use and available storage capacity and an
183 // implementation-specific volume identifier (e.g., "mount
184 // point" for a UnixVolume).
185 Status() *VolumeStatus
187 // String returns an identifying label for this volume,
188 // suitable for including in log messages. It should contain
189 // enough information to uniquely identify the underlying
190 // storage device, but should not contain any credentials or
194 // Writable returns false if all future Put, Mtime, and Delete
195 // calls are expected to fail.
197 // If the volume is only temporarily unwritable -- or if Put
198 // will fail because it is full, but Mtime or Delete can
199 // succeed -- then Writable should return false.
202 // Replication returns the storage redundancy of the
203 // underlying device. It will be passed on to clients in
204 // responses to PUT requests.
207 // EmptyTrash looks for trashed blocks that exceeded trashLifetime
208 // and deletes them from the volume.
212 // A VolumeManager tells callers which volumes can read, which volumes
213 // can write, and on which volume the next write should be attempted.
214 type VolumeManager interface {
215 // AllReadable returns all volumes.
216 AllReadable() []Volume
218 // AllWritable returns all volumes that aren't known to be in
219 // a read-only state. (There is no guarantee that a write to
220 // one will succeed, though.)
221 AllWritable() []Volume
223 // NextWritable returns the volume where the next new block
224 // should be written. A VolumeManager can select a volume in
225 // order to distribute activity across spindles, fill up disks
226 // with more free space, etc.
227 NextWritable() Volume
229 // Close shuts down the volume manager cleanly.
233 // RRVolumeManager is a round-robin VolumeManager: the Nth call to
234 // NextWritable returns the (N % len(writables))th writable Volume
235 // (where writables are all Volumes v where v.Writable()==true).
236 type RRVolumeManager struct {
242 // MakeRRVolumeManager initializes RRVolumeManager
243 func MakeRRVolumeManager(volumes []Volume) *RRVolumeManager {
244 vm := &RRVolumeManager{}
245 for _, v := range volumes {
246 vm.readables = append(vm.readables, v)
248 vm.writables = append(vm.writables, v)
254 // AllReadable returns an array of all readable volumes
255 func (vm *RRVolumeManager) AllReadable() []Volume {
259 // AllWritable returns an array of all writable volumes
260 func (vm *RRVolumeManager) AllWritable() []Volume {
264 // NextWritable returns the next writable
265 func (vm *RRVolumeManager) NextWritable() Volume {
266 if len(vm.writables) == 0 {
269 i := atomic.AddUint32(&vm.counter, 1)
270 return vm.writables[i%uint32(len(vm.writables))]
273 // Close the RRVolumeManager
274 func (vm *RRVolumeManager) Close() {
277 // VolumeStatus provides status information of the volume consisting of:
279 // * device_num (an integer identifying the underlying storage system)
282 type VolumeStatus struct {
283 MountPoint string `json:"mount_point"`
284 DeviceNum uint64 `json:"device_num"`
285 BytesFree uint64 `json:"bytes_free"`
286 BytesUsed uint64 `json:"bytes_used"`