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,
14 type Volume interface {
15 Get(loc string) ([]byte, error)
16 Put(loc string, block []byte) error
17 Index(prefix string) string
18 Status() *VolumeStatus
22 // MockVolumes are Volumes used to test the Keep front end.
24 // If the Bad field is true, this volume should return an error
25 // on all writes and puts.
27 type MockVolume struct {
28 Store map[string][]byte
32 func CreateMockVolume() *MockVolume {
33 return &MockVolume{make(map[string][]byte), false}
36 func (v *MockVolume) Get(loc string) ([]byte, error) {
38 return nil, errors.New("Bad volume")
39 } else if block, ok := v.Store[loc]; ok {
42 return nil, os.ErrNotExist
45 func (v *MockVolume) Put(loc string, block []byte) error {
47 return errors.New("Bad volume")
53 func (v *MockVolume) Index(prefix string) string {
55 for loc, block := range v.Store {
56 if IsValidLocator(loc) && strings.HasPrefix(loc, prefix) {
57 result = result + fmt.Sprintf("%s+%d %d\n",
58 loc, len(block), 123456789)
64 func (v *MockVolume) Status() *VolumeStatus {
66 for _, block := range v.Store {
67 used = used + uint64(len(block))
69 return &VolumeStatus{"/bogo", 123, 1000000 - used, used}
72 func (v *MockVolume) String() string {
76 // A VolumeManager manages a collection of volumes.
78 // - Volumes is a slice of available Volumes.
79 // - Choose() returns a Volume suitable for writing to.
80 // - Quit() instructs the VolumeManager to shut down gracefully.
82 type VolumeManager interface {
88 type RRVolumeManager struct {
94 func MakeRRVolumeManager(vols []Volume) *RRVolumeManager {
95 // Create a new VolumeManager struct with the specified volumes,
96 // and with new Nextwrite and Quit channels.
97 // The Quit channel is buffered with a capacity of 1 so that
98 // another routine may write to it without blocking.
99 vm := &RRVolumeManager{vols, make(chan Volume), make(chan int, 1)}
101 // This goroutine implements round-robin volume selection.
102 // It sends each available Volume in turn to the Nextwrite
103 // channel, until receiving a notification on the Quit channel
104 // that it should terminate.
111 case vm.nextwrite <- vm.volumes[i]:
112 i = (i + 1) % len(vm.volumes)
120 func (vm *RRVolumeManager) Volumes() []Volume {
124 func (vm *RRVolumeManager) Choose() Volume {
125 return <-vm.nextwrite
128 func (vm *RRVolumeManager) Quit() {