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,
13 type Volume interface {
14 Get(loc string) ([]byte, error)
15 Put(loc string, block []byte) error
16 Index(prefix string) string
17 Status() *VolumeStatus
21 // MockVolumes are Volumes used to test the Keep front end.
23 // If the Bad field is true, this volume should return an error
24 // on all writes and puts.
26 type MockVolume struct {
27 Store map[string][]byte
31 func CreateMockVolume() *MockVolume {
32 return &MockVolume{make(map[string][]byte), false}
35 func (v *MockVolume) Get(loc string) ([]byte, error) {
37 return nil, errors.New("Bad volume")
38 } else if block, ok := v.Store[loc]; ok {
41 return nil, errors.New("not found")
44 func (v *MockVolume) Put(loc string, block []byte) error {
46 return errors.New("Bad volume")
52 func (v *MockVolume) Index(prefix string) string {
54 for loc, block := range v.Store {
55 if IsValidLocator(loc) && strings.HasPrefix(loc, prefix) {
56 result = result + fmt.Sprintf("%s+%d %d\n",
57 loc, len(block), 123456789)
63 func (v *MockVolume) Status() *VolumeStatus {
65 for _, block := range v.Store {
66 used = used + uint64(len(block))
68 return &VolumeStatus{"/bogo", 123, 1000000 - used, used}
71 func (v *MockVolume) String() string {
75 // A VolumeManager manages a collection of volumes.
77 // - Volumes is a slice of available Volumes.
78 // - Choose() returns a Volume suitable for writing to.
79 // - Quit() instructs the VolumeManager to shut down gracefully.
81 type VolumeManager interface {
87 type RRVolumeManager struct {
93 func MakeRRVolumeManager(vols []Volume) *RRVolumeManager {
94 // Create a new VolumeManager struct with the specified volumes,
95 // and with new Nextwrite and Quit channels.
96 // The Quit channel is buffered with a capacity of 1 so that
97 // another routine may write to it without blocking.
98 vm := &RRVolumeManager{vols, make(chan Volume), make(chan int, 1)}
100 // This goroutine implements round-robin volume selection.
101 // It sends each available Volume in turn to the Nextwrite
102 // channel, until receiving a notification on the Quit channel
103 // that it should terminate.
110 case vm.nextwrite <- vm.volumes[i]:
111 i = (i + 1) % len(vm.volumes)
119 func (vm *RRVolumeManager) Volumes() []Volume {
123 func (vm *RRVolumeManager) Choose() Volume {
124 return <-vm.nextwrite
127 func (vm *RRVolumeManager) Quit() {