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,
13 type Volume interface {
14 // Volume type as specified in config file. Examples: "S3",
18 // Do whatever private setup tasks and configuration checks
19 // are needed. Return non-nil if the volume is unusable (e.g.,
23 // Get a block: copy the block data into buf, and return the
24 // number of bytes copied.
26 // loc is guaranteed to consist of 32 or more lowercase hex
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.)
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
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
46 // If the data in the backing store is bigger than len(buf),
47 // then Get is permitted to return an error without reading
50 // len(buf) will not exceed BlockSize.
51 Get(ctx context.Context, loc string, buf []byte) (int, error)
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(ctx context.Context, loc string, data []byte) error
60 // Put writes a block to an underlying storage device.
62 // loc is as described in Get.
64 // len(block) is guaranteed to be between 0 and BlockSize.
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
76 // Put also sets the timestamp for the given locator to the
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.
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
90 // Touch sets the timestamp for the given locator to the
93 // loc is as described in Get.
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:
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
106 // It is not acceptable for a subsequente Mtime to return
107 // either of the following:
109 // - 2015-07-07T00:00:00.00000000Z -- ERROR
110 // - 2015-07-07T01:23:44.00000000Z -- ERROR
112 // Touch must return a non-nil error if the timestamp cannot
114 Touch(loc string) error
116 // Mtime returns the stored timestamp for the given locator.
118 // loc is as described in Get.
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)
124 // IndexTo writes a complete list of locators with the given
125 // prefix for which Get() can retrieve data.
127 // prefix consists of zero or more lowercase hexadecimal
130 // Each locator must be written to the given writer using the
133 // loc "+" size " " timestamp "\n"
137 // - size is the number of bytes of content, given as a
138 // decimal number with one or more digits
140 // - timestamp is the timestamp stored for the locator,
141 // given as a decimal number of seconds after January 1,
144 // IndexTo must not write any other data to writer: for
145 // example, it must not write any blank lines.
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.
152 // The resulting index is not expected to be sorted in any
154 IndexTo(prefix string, writer io.Writer) error
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.
160 // loc is as described in Get.
162 // If the timestamp for the given locator is newer than
163 // BlobSignatureTTL, Trash must not trash the data.
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:
169 // - Touch and Put return a non-nil error, or
170 // - Trash does not trash the block, or
171 // - Both of the above.
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.
183 // Corollary: A successful Touch or Put guarantees a block
184 // will not be trashed for at least BlobSignatureTTL
186 Trash(loc string) error
188 // Untrash moves block from trash back into store
189 Untrash(loc string) error
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
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
204 // Writable returns false if all future Put, Mtime, and Delete
205 // calls are expected to fail.
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.
212 // Replication returns the storage redundancy of the
213 // underlying device. It will be passed on to clients in
214 // responses to PUT requests.
217 // EmptyTrash looks for trashed blocks that exceeded TrashLifetime
218 // and deletes them from the volume.
222 // A VolumeWithExamples provides example configs to display in the
224 type VolumeWithExamples interface {
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
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
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
246 // VolumeStats returns the ioStats used for tracking stats for
248 VolumeStats(Volume) *ioStats
250 // Close shuts down the volume manager cleanly.
254 // RRVolumeManager is a round-robin VolumeManager: the Nth call to
255 // NextWritable returns the (N % len(writables))th writable Volume
256 // (where writables are all Volumes v where v.Writable()==true).
257 type RRVolumeManager struct {
261 iostats map[Volume]*ioStats
264 // MakeRRVolumeManager initializes RRVolumeManager
265 func MakeRRVolumeManager(volumes []Volume) *RRVolumeManager {
266 vm := &RRVolumeManager{
267 iostats: make(map[Volume]*ioStats),
269 for _, v := range volumes {
270 vm.iostats[v] = &ioStats{}
271 vm.readables = append(vm.readables, v)
273 vm.writables = append(vm.writables, v)
279 // AllReadable returns an array of all readable volumes
280 func (vm *RRVolumeManager) AllReadable() []Volume {
284 // AllWritable returns an array of all writable volumes
285 func (vm *RRVolumeManager) AllWritable() []Volume {
289 // NextWritable returns the next writable
290 func (vm *RRVolumeManager) NextWritable() Volume {
291 if len(vm.writables) == 0 {
294 i := atomic.AddUint32(&vm.counter, 1)
295 return vm.writables[i%uint32(len(vm.writables))]
298 // VolumeStats returns an ioStats for the given volume.
299 func (vm *RRVolumeManager) VolumeStats(v Volume) *ioStats {
303 // Close the RRVolumeManager
304 func (vm *RRVolumeManager) Close() {
307 // VolumeStatus describes the current condition of a volume
308 type VolumeStatus struct {
315 // ioStats tracks I/O statistics for a volume or server
316 type ioStats struct {
327 type InternalStatser interface {
328 InternalStats() interface{}