forked from asonawalla/gazette
/
key_space.go
65 lines (56 loc) · 2.34 KB
/
key_space.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package broker
import (
"github.com/LiveRamp/gazette/v2/pkg/allocator"
"github.com/LiveRamp/gazette/v2/pkg/keyspace"
pb "github.com/LiveRamp/gazette/v2/pkg/protocol"
"github.com/coreos/etcd/mvcc/mvccpb"
)
// NewKeySpace returns a KeySpace suitable for use with an Allocator.
// It decodes allocator Items as JournalSpec messages, Members as BrokerSpecs,
// and Assignments as Routes.
func NewKeySpace(prefix string) *keyspace.KeySpace {
return allocator.NewAllocatorKeySpace(prefix, decoder{})
}
// decoder is an instance of allocator.Decoder. It strictly enforces that
// JournalSpec.Name and BrokerSpec.ID match those derived from the Spec's
// Etcd key. Conceptually, the Etcd key is source-of-truth regarding entity
// naming, but it's helpful to carry these identifiers within the specs
// themselves, making them a stand-alone representation and allowing for
// content-addressing of a spec into its appropriate Etcd key. This decoder
// behavior provides an assertion that these identifiers never diverge.
type decoder struct{}
func (d decoder) DecodeItem(id string, raw *mvccpb.KeyValue) (allocator.ItemValue, error) {
var s = new(pb.JournalSpec)
if err := s.Unmarshal(raw.Value); err != nil {
return nil, err
} else if err = s.Validate(); err != nil {
return nil, err
} else if s.Name.String() != id {
return nil, pb.NewValidationError("JournalSpec Name doesn't match Item ID (%+v vs %+v)", s.Name, id)
}
return s, nil
}
func (d decoder) DecodeMember(zone, suffix string, raw *mvccpb.KeyValue) (allocator.MemberValue, error) {
var s = new(pb.BrokerSpec)
if err := s.Unmarshal(raw.Value); err != nil {
return nil, err
} else if err = s.Validate(); err != nil {
return nil, err
} else if s.Id.Zone != zone {
return nil, pb.NewValidationError("BrokerSpec Zone doesn't match Member Zone (%+v vs %+v)", s.Id.Zone, zone)
} else if s.Id.Suffix != suffix {
return nil, pb.NewValidationError("BrokerSpec Suffix doesn't match Member Suffix (%+v vs %+v)", s.Id.Suffix, suffix)
}
return s, nil
}
func (d decoder) DecodeAssignment(itemID, memberZone, memberSuffix string, slot int, raw *mvccpb.KeyValue) (allocator.AssignmentValue, error) {
var s = new(pb.Route)
if len(raw.Value) == 0 {
s.Init(nil)
} else if err := s.Unmarshal(raw.Value); err != nil {
return nil, err
} else if err = s.Validate(); err != nil {
return nil, err
}
return s, nil
}