-
Notifications
You must be signed in to change notification settings - Fork 2
/
message_binding_registry.go
102 lines (86 loc) · 2.62 KB
/
message_binding_registry.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package bristle
import (
"fmt"
"github.com/rs/zerolog/log"
v1 "github.com/uplol/bristle/proto/v1"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/reflect/protoreflect"
)
type messageBindingRegistry map[string]*MessageTableBinding
func newMessageBindingRegistry() messageBindingRegistry {
return make(messageBindingRegistry)
}
func (m messageBindingRegistry) BindFromClusters(clusters []*ClickhouseCluster, protoRegistry *ProtoRegistry) error {
for _, cluster := range clusters {
for _, table := range cluster.tables {
for _, messageTypeName := range table.config.Messages {
messageType := protoRegistry.MessageTypes[protoreflect.FullName(messageTypeName)]
if messageType == nil {
return fmt.Errorf("message type '%v' is not registered", messageTypeName)
}
err := m.bind(cluster, messageType, table.Name)
if err != nil {
return err
}
}
}
}
return nil
}
func (m messageBindingRegistry) BindFromProtos(clusters []*ClickhouseCluster, protoRegistry *ProtoRegistry) error {
for name, messageType := range protoRegistry.MessageTypes {
messageOpts := messageType.Descriptor().Options()
if !proto.HasExtension(messageOpts, v1.E_BristleTable) {
log.Trace().
Str("name", string(name)).
Msg("server: skipping auto-registering message missing bristle_table option")
continue
}
fullTableName := ParseFullTableName(
proto.GetExtension(messageOpts, v1.E_BristleTable).(string),
)
matched := false
for _, cluster := range clusters {
err := m.bind(cluster, messageType, fullTableName)
if err != nil {
if err == ErrNoSuchTable {
continue
}
return err
}
matched = true
break
}
if !matched {
return fmt.Errorf("failed to find table %v for message %v", fullTableName, name)
}
}
return nil
}
func (m messageBindingRegistry) bind(cluster *ClickhouseCluster, messageType protoreflect.MessageType, fullTableName FullTableName) error {
typeName := string(messageType.Descriptor().FullName())
table, err := cluster.GetTable(fullTableName)
if err != nil {
log.Error().
Err(err).
Str("message", typeName).
Str("table", string(fullTableName)).
Msg("server: failed to find table in cluster")
return err
}
binding, err := table.BindMessage(messageType, table.config.MessageInstancePoolSize)
if err != nil {
log.Error().
Err(err).
Str("message", typeName).
Str("table", string(fullTableName)).
Msg("server: failed to bind message to table")
return err
}
m[typeName] = binding
log.Info().
Str("message", typeName).
Str("table", string(fullTableName)).
Msg("server: successfully bound message")
return nil
}