1 // A Volume is an interface representing a Keep back-end storage unit:
2 // for example, a single mounted disk, a RAID array, an Amazon S3 volume,
15 type Volume interface {
16 Get(loc string) ([]byte, error)
17 Put(loc string, block []byte) error
18 Touch(loc string) error
19 Mtime(loc string) (time.Time, error)
20 Index(prefix string) string
21 Delete(loc string) error
22 Status() *VolumeStatus
26 // MockVolumes are Volumes used to test the Keep front end.
28 // If the Bad field is true, this volume should return an error
29 // on all writes and puts.
31 type MockVolume struct {
32 Store map[string][]byte
33 Timestamps map[string]time.Time
37 func CreateMockVolume() *MockVolume {
39 make(map[string][]byte),
40 make(map[string]time.Time),
45 func (v *MockVolume) Get(loc string) ([]byte, error) {
47 return nil, errors.New("Bad volume")
48 } else if block, ok := v.Store[loc]; ok {
51 return nil, os.ErrNotExist
54 func (v *MockVolume) Put(loc string, block []byte) error {
56 return errors.New("Bad volume")
62 func (v *MockVolume) Touch(loc string) error {
64 return errors.New("Bad volume")
66 v.Timestamps[loc] = time.Now()
70 func (v *MockVolume) Mtime(loc string) (time.Time, error) {
74 err = errors.New("Bad volume")
75 } else if t, ok := v.Timestamps[loc]; ok {
83 func (v *MockVolume) Index(prefix string) string {
85 for loc, block := range v.Store {
86 if IsValidLocator(loc) && strings.HasPrefix(loc, prefix) {
87 result = result + fmt.Sprintf("%s+%d %d\n",
88 loc, len(block), 123456789)
94 func (v *MockVolume) Delete(loc string) error {
95 if _, ok := v.Store[loc]; ok {
96 if time.Since(v.Timestamps[loc]) < permission_ttl {
102 return os.ErrNotExist
105 func (v *MockVolume) Status() *VolumeStatus {
107 for _, block := range v.Store {
108 used = used + uint64(len(block))
110 return &VolumeStatus{"/bogo", 123, 1000000 - used, used}
113 func (v *MockVolume) String() string {
114 return "[MockVolume]"
117 // A VolumeManager manages a collection of volumes.
119 // - Volumes is a slice of available Volumes.
120 // - Choose() returns a Volume suitable for writing to.
121 // - Quit() instructs the VolumeManager to shut down gracefully.
123 type VolumeManager interface {
129 type RRVolumeManager struct {
131 nextwrite chan Volume
135 func MakeRRVolumeManager(vols []Volume) *RRVolumeManager {
136 // Create a new VolumeManager struct with the specified volumes,
137 // and with new Nextwrite and Quit channels.
138 // The Quit channel is buffered with a capacity of 1 so that
139 // another routine may write to it without blocking.
140 vm := &RRVolumeManager{vols, make(chan Volume), make(chan int, 1)}
142 // This goroutine implements round-robin volume selection.
143 // It sends each available Volume in turn to the Nextwrite
144 // channel, until receiving a notification on the Quit channel
145 // that it should terminate.
152 case vm.nextwrite <- vm.volumes[i]:
153 i = (i + 1) % len(vm.volumes)
161 func (vm *RRVolumeManager) Volumes() []Volume {
165 func (vm *RRVolumeManager) Choose() Volume {
166 return <-vm.nextwrite
169 func (vm *RRVolumeManager) Quit() {