Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor(store): use protobuf for notify/listen signal messages (#18776)

p0ny9cd92e3f2891
diff --git a/backend/api/v1/plan_service.go b/backend/api/v1/plan_service.go
index d3332a02f8..f12dca74ad 100644
--- a/backend/api/v1/plan_service.go
+++ b/backend/api/v1/plan_service.go
@@ -500,7 +500,7 @@ func (s *PlanService) CancelPlanCheckRun(ctx context.Context, request *connect.R
}
// Broadcast cancel signal to all replicas for HA.
- if err := s.store.SendSignal(ctx, store.SignalTypeCancelPlanCheckRun, planCheckRun.UID); err != nil {
+ if err := s.store.SendSignal(ctx, storepb.Signal_CANCEL_PLAN_CHECK_RUN, int32(planCheckRun.UID)); err != nil {
slog.Warn("failed to send cancel signal", log.BBError(err))
}
diff --git a/backend/api/v1/rollout_service.go b/backend/api/v1/rollout_service.go
index 2df3ba5d89..c981aea5d3 100644
--- a/backend/api/v1/rollout_service.go
+++ b/backend/api/v1/rollout_service.go
@@ -933,7 +933,7 @@ func (s *RolloutService) BatchCancelTaskRuns(ctx context.Context, req *connect.R
cancelFunc.(context.CancelFunc)()
}
// Broadcast cancel signal to all replicas for HA.
- if err := s.store.SendSignal(ctx, store.SignalTypeCancelTaskRun, taskRun.ID); err != nil {
+ if err := s.store.SendSignal(ctx, storepb.Signal_CANCEL_TASK_RUN, int32(taskRun.ID)); err != nil {
slog.Warn("failed to send cancel signal", log.BBError(err))
}
}
diff --git a/backend/generated-go/store/signal.pb.go b/backend/generated-go/store/signal.pb.go
new file mode 100644
index 0000000000..bb9ef77209
--- /dev/null
+++ b/backend/generated-go/store/signal.pb.go
@@ -0,0 +1,191 @@
+// Code generated by protoc-gen-go. DO NOT EDIT.
+// versions:
+// protoc-gen-go v1.36.11
+// protoc (unknown)
+// source: store/signal.proto
+
+package store
+
+import (
+ protoreflect "google.golang.org/protobuf/reflect/protoreflect"
+ protoimpl "google.golang.org/protobuf/runtime/protoimpl"
+ reflect "reflect"
+ sync "sync"
+ unsafe "unsafe"
+)
+
+const (
+ // Verify that this generated code is sufficiently up-to-date.
+ _ = protoimpl.EnforceVersion(20 - protoimpl.MinVersion)
+ // Verify that runtime/protoimpl is sufficiently up-to-date.
+ _ = protoimpl.EnforceVersion(protoimpl.MaxVersion - 20)
+)
+
+// Type represents the type of signal.
+type Signal_Type int32
+
+const (
+ Signal_TYPE_UNSPECIFIED Signal_Type = 0
+ Signal_CANCEL_PLAN_CHECK_RUN Signal_Type = 1
+ Signal_CANCEL_TASK_RUN Signal_Type = 2
+)
+
+// Enum value maps for Signal_Type.
+var (
+ Signal_Type_name = map[int32]string{
+ 0: "TYPE_UNSPECIFIED",
+ 1: "CANCEL_PLAN_CHECK_RUN",
+ 2: "CANCEL_TASK_RUN",
+ }
+ Signal_Type_value = map[string]int32{
+ "TYPE_UNSPECIFIED": 0,
+ "CANCEL_PLAN_CHECK_RUN": 1,
+ "CANCEL_TASK_RUN": 2,
+ }
+)
+
+func (x Signal_Type) Enum() *Signal_Type {
+ p := new(Signal_Type)
+ *p = x
+ return p
+}
+
+func (x Signal_Type) String() string {
+ return protoimpl.X.EnumStringOf(x.Descriptor(), protoreflect.EnumNumber(x))
+}
+
+func (Signal_Type) Descriptor() protoreflect.EnumDescriptor {
+ return file_store_signal_proto_enumTypes[0].Descriptor()
+}
+
+func (Signal_Type) Type() protoreflect.EnumType {
+ return &file_store_signal_proto_enumTypes[0]
+}
+
+func (x Signal_Type) Number() protoreflect.EnumNumber {
+ return protoreflect.EnumNumber(x)
+}
+
+// Deprecated: Use Signal_Type.Descriptor instead.
+func (Signal_Type) EnumDescriptor() ([]byte, []int) {
+ return file_store_signal_proto_rawDescGZIP(), []int{0, 0}
+}
+
+// Signal represents a notification payload sent via PostgreSQL NOTIFY for HA coordination.
+type Signal struct {
+ state protoimpl.MessageState `protogen:"open.v1"`
+ Type Signal_Type `protobuf:"varint,1,opt,name=type,proto3,enum=bytebase.store.Signal_Type" json:"type,omitempty"`
+ Uid int32 `protobuf:"varint,2,opt,name=uid,proto3" json:"uid,omitempty"`
+ unknownFields protoimpl.UnknownFields
+ sizeCache protoimpl.SizeCache
+}
+
+func (x *Signal) Reset() {
+ *x = Signal{}
+ mi := &file_store_signal_proto_msgTypes[0]
+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+ ms.StoreMessageInfo(mi)
+}
+
+func (x *Signal) String() string {
+ return protoimpl.X.MessageStringOf(x)
+}
+
+func (*Signal) ProtoMessage() {}
+
+func (x *Signal) ProtoReflect() protoreflect.Message {
+ mi := &file_store_signal_proto_msgTypes[0]
+ if x != nil {
+ ms := protoimpl.X.MessageStateOf(protoimpl.Pointer(x))
+ if ms.LoadMessageInfo() == nil {
+ ms.StoreMessageInfo(mi)
+ }
+ return ms
+ }
+ return mi.MessageOf(x)
+}
+
+// Deprecated: Use Signal.ProtoReflect.Descriptor instead.
+func (*Signal) Descriptor() ([]byte, []int) {
+ return file_store_signal_proto_rawDescGZIP(), []int{0}
+}
+
+func (x *Signal) GetType() Signal_Type {
+ if x != nil {
+ return x.Type
+ }
+ return Signal_TYPE_UNSPECIFIED
+}
+
+func (x *Signal) GetUid() int32 {
+ if x != nil {
+ return x.Uid
+ }
+ return 0
+}
+
+var File_store_signal_proto protoreflect.FileDescriptor
+
+const file_store_signal_proto_rawDesc = "" +
+ "\n" +
+ "\x12store/signal.proto\x12\x0ebytebase.store\"\x99\x01\n" +
+ "\x06Signal\x12/\n" +
+ "\x04type\x18\x01 \x01(\x0e2\x1b.bytebase.store.Signal.TypeR\x04type\x12\x10\n" +
+ "\x03uid\x18\x02 \x01(\x05R\x03uid\"L\n" +
+ "\x04Type\x12\x14\n" +
+ "\x10TYPE_UNSPECIFIED\x10\x00\x12\x19\n" +
+ "\x15CANCEL_PLAN_CHECK_RUN\x10\x01\x12\x13\n" +
+ "\x0fCANCEL_TASK_RUN\x10\x02B\x8e\x01\n" +
+ "\x12com.bytebase.storeB\vSignalProtoP\x01Z\x12generated-go/store\xa2\x02\x03BSX\xaa\x02\x0eBytebase.Store\xca\x02\x0eBytebase\\Store\xe2\x02\x1aBytebase\\Store\\GPBMetadata\xea\x02\x0fBytebase::Storeb\x06proto3"
+
+var (
+ file_store_signal_proto_rawDescOnce sync.Once
+ file_store_signal_proto_rawDescData []byte
+)
+
+func file_store_signal_proto_rawDescGZIP() []byte {
+ file_store_signal_proto_rawDescOnce.Do(func() {
+ file_store_signal_proto_rawDescData = protoimpl.X.CompressGZIP(unsafe.Slice(unsafe.StringData(file_store_signal_proto_rawDesc), len(file_store_signal_proto_rawDesc)))
+ })
+ return file_store_signal_proto_rawDescData
+}
+
+var file_store_signal_proto_enumTypes = make([]protoimpl.EnumInfo, 1)
+var file_store_signal_proto_msgTypes = make([]protoimpl.MessageInfo, 1)
+var file_store_signal_proto_goTypes = []any{
+ (Signal_Type)(0), // 0: bytebase.store.Signal.Type
+ (*Signal)(nil), // 1: bytebase.store.Signal
+}
+var file_store_signal_proto_depIdxs = []int32{
+ 0, // 0: bytebase.store.Signal.type:type_name -> bytebase.store.Signal.Type
+ 1, // [1:1] is the sub-list for method output_type
+ 1, // [1:1] is the sub-list for method input_type
+ 1, // [1:1] is the sub-list for extension type_name
+ 1, // [1:1] is the sub-list for extension extendee
+ 0, // [0:1] is the sub-list for field type_name
+}
+
+func init() { file_store_signal_proto_init() }
+func file_store_signal_proto_init() {
+ if File_store_signal_proto != nil {
+ return
+ }
+ type x struct{}
+ out := protoimpl.TypeBuilder{
+ File: protoimpl.DescBuilder{
+ GoPackagePath: reflect.TypeOf(x{}).PkgPath(),
+ RawDescriptor: unsafe.Slice(unsafe.StringData(file_store_signal_proto_rawDesc), len(file_store_signal_proto_rawDesc)),
+ NumEnums: 1,
+ NumMessages: 1,
+ NumExtensions: 0,
+ NumServices: 0,
+ },
+ GoTypes: file_store_signal_proto_goTypes,
+ DependencyIndexes: file_store_signal_proto_depIdxs,
+ EnumInfos: file_store_signal_proto_enumTypes,
+ MessageInfos: file_store_signal_proto_msgTypes,
+ }.Build()
+ File_store_signal_proto = out.File
+ file_store_signal_proto_goTypes = nil
+ file_store_signal_proto_depIdxs = nil
+}
diff --git a/backend/generated-go/store/signal_equal.pb.go b/backend/generated-go/store/signal_equal.pb.go
new file mode 100644
index 0000000000..f7f45497d0
--- /dev/null
+++ b/backend/generated-go/store/signal_equal.pb.go
@@ -0,0 +1,20 @@
+// Code generated by protoc-gen-equal-go. DO NOT EDIT.
+// source: store/signal.proto
+
+package store
+
+func (x *Signal) Equal(y *Signal) bool {
+ if x == y {
+ return true
+ }
+ if x == nil || y == nil {
+ return x == nil && y == nil
+ }
+ if x.Type != y.Type {
+ return false
+ }
+ if x.Uid != y.Uid {
+ return false
+ }
+ return true
+}
diff --git a/backend/runner/notifylistener/listener.go b/backend/runner/notifylistener/listener.go
index 05815d9f16..9c3c1619d7 100644
--- a/backend/runner/notifylistener/listener.go
+++ b/backend/runner/notifylistener/listener.go
@@ -4,15 +4,16 @@ package notifylistener
import (
"context"
"database/sql"
- "encoding/json"
"log/slog"
"sync"
"time"
"github.com/jackc/pgx/v5/stdlib"
+ "github.com/bytebase/bytebase/backend/common"
"github.com/bytebase/bytebase/backend/common/log"
"github.com/bytebase/bytebase/backend/component/bus"
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
"github.com/bytebase/bytebase/backend/store"
)
@@ -81,19 +82,19 @@ func (l *Listener) listen(ctx context.Context) error {
}
func (l *Listener) handleNotification(payload string) {
- var signal store.Signal
- if err := json.Unmarshal([]byte(payload), &signal); err != nil {
+ var signal storepb.Signal
+ if err := common.ProtojsonUnmarshaler.Unmarshal([]byte(payload), &signal); err != nil {
slog.Warn("invalid signal payload", "payload", payload, log.BBError(err))
return
}
switch signal.Type {
- case store.SignalTypeCancelPlanCheckRun:
- if cancel, ok := l.bus.RunningPlanCheckRunsCancelFunc.Load(signal.UID); ok {
+ case storepb.Signal_CANCEL_PLAN_CHECK_RUN:
+ if cancel, ok := l.bus.RunningPlanCheckRunsCancelFunc.Load(int(signal.Uid)); ok {
cancel.(context.CancelFunc)()
}
- case store.SignalTypeCancelTaskRun:
- if cancel, ok := l.bus.RunningTaskRunsCancelFunc.Load(signal.UID); ok {
+ case storepb.Signal_CANCEL_TASK_RUN:
+ if cancel, ok := l.bus.RunningTaskRunsCancelFunc.Load(int(signal.Uid)); ok {
cancel.(context.CancelFunc)()
}
default:
diff --git a/backend/store/signal.go b/backend/store/signal.go
index 0336f5693a..5e1ed9b7e0 100644
--- a/backend/store/signal.go
+++ b/backend/store/signal.go
@@ -2,31 +2,21 @@ package store
import (
"context"
- "encoding/json"
"github.com/pkg/errors"
+ "google.golang.org/protobuf/encoding/protojson"
+
+ storepb "github.com/bytebase/bytebase/backend/generated-go/store"
)
// SignalChannel is the PostgreSQL NOTIFY channel for HA coordination.
const SignalChannel = "bytebase_signal"
-// Signal represents a notification payload sent via PostgreSQL NOTIFY.
-type Signal struct {
- Type string `json:"type"`
- UID int `json:"uid"`
-}
-
-// Signal types.
-const (
- SignalTypeCancelPlanCheckRun = "cancel_plan_check_run"
- SignalTypeCancelTaskRun = "cancel_task_run"
-)
-
// SendSignal sends a notification to the bytebase_signal channel.
-func (s *Store) SendSignal(ctx context.Context, signalType string, uid int) error {
- payload, err := json.Marshal(&Signal{
+func (s *Store) SendSignal(ctx context.Context, signalType storepb.Signal_Type, uid int32) error {
+ payload, err := protojson.Marshal(&storepb.Signal{
Type: signalType,
- UID: uid,
+ Uid: uid,
})
if err != nil {
return errors.Wrap(err, "failed to marshal signal payload")
diff --git a/proto/gen/grpc-doc/store/README.md b/proto/gen/grpc-doc/store/README.md
index b39255b05e..e9bf3c78fd 100644
--- a/proto/gen/grpc-doc/store/README.md
+++ b/proto/gen/grpc-doc/store/README.md
@@ -275,6 +275,11 @@
- [WorkspaceProfileSetting.Announcement.AlertLevel](#bytebase-store-WorkspaceProfileSetting-Announcement-AlertLevel)
- [WorkspaceProfileSetting.DatabaseChangeMode](#bytebase-store-WorkspaceProfileSetting-DatabaseChangeMode)
+- [store/signal.proto](#store_signal-proto)
+ - [Signal](#bytebase-store-Signal)
+
+ - [Signal.Type](#bytebase-store-Signal-Type)
+
- [store/task.proto](#store_task-proto)
- [Task](#bytebase-store-Task)
- [Task.FlagsEntry](#bytebase-store-Task-FlagsEntry)
@@ -4578,6 +4583,51 @@ We support three levels of AlertLevel: INFO, WARNING, and ERROR.
+<a name="store_signal-proto"></a>
+<p align="right"><a href="#top">Top</a></p>
+
+## store/signal.proto
+
+
+
+<a name="bytebase-store-Signal"></a>
+
+### Signal
+Signal represents a notification payload sent via PostgreSQL NOTIFY for HA coordination.
+
+
+| Field | Type | Label | Description |
+| ----- | ---- | ----- | ----------- |
+| type | [Signal.Type](#bytebase-store-Signal-Type) | | |
+| uid | [int32](#int32) | | |
+
+
+
+
+
+
+
+
+<a name="bytebase-store-Signal-Type"></a>
+
+### Signal.Type
+Type represents the type of signal.
+
+| Name | Number | Description |
+| ---- | ------ | ----------- |
+| TYPE_UNSPECIFIED | 0 | |
+| CANCEL_PLAN_CHECK_RUN | 1 | |
+| CANCEL_TASK_RUN | 2 | |
+
+
+
+
+
+
+
+
+
+
<a name="store_task-proto"></a>
<p align="right"><a href="#top">Top</a></p>
diff --git a/proto/gen/grpc-doc/store/index.html b/proto/gen/grpc-doc/store/index.html
index 5b318f3566..0c75d2927a 100644
--- a/proto/gen/grpc-doc/store/index.html
+++ b/proto/gen/grpc-doc/store/index.html
@@ -1274,6 +1274,25 @@
</li>
+ <li>
+ <a href="#store%2fsignal.proto">store/signal.proto</a>
+ <ul>
+
+ <li>
+ <a href="#bytebase.store.Signal"><span class="badge">M</span>Signal</a>
+ </li>
+
+
+ <li>
+ <a href="#bytebase.store.Signal.Type"><span class="badge">E</span>Signal.Type</a>
+ </li>
+
+
+
+ </ul>
+ </li>
+
+
<li>
<a href="#store%2ftask.proto">store/task.proto</a>
<ul>
@@ -12483,6 +12502,80 @@ Default to this mode.</p></td>
+ <div class="file-heading">
+ <h2 id="store/signal.proto">store/signal.proto</h2><a href="#title">Top</a>
+ </div>
+ <p></p>
+
+
+ <h3 id="bytebase.store.Signal">Signal</h3>
+ <p>Signal represents a notification payload sent via PostgreSQL NOTIFY for HA coordination.</p>
+
+
+ <table class="field-table">
+ <thead>
+ <tr><td>Field</td><td>Type</td><td>Label</td><td>Description</td></tr>
+ </thead>
+ <tbody>
+
+ <tr>
+ <td>type</td>
+ <td><a href="#bytebase.store.Signal.Type">Signal.Type</a></td>
+ <td></td>
+ <td><p> </p></td>
+ </tr>
+
+ <tr>
+ <td>uid</td>
+ <td><a href="#int32">int32</a></td>
+ <td></td>
+ <td><p> </p></td>
+ </tr>
+
+ </tbody>
+ </table>
+
+
+
+
+
+
+
+ <h3 id="bytebase.store.Signal.Type">Signal.Type</h3>
+ <p>Type represents the type of signal.</p>
+ <table class="enum-table">
+ <thead>
+ <tr><td>Name</td><td>Number</td><td>Description</td></tr>
+ </thead>
+ <tbody>
+
+ <tr>
+ <td>TYPE_UNSPECIFIED</td>
+ <td>0</td>
+ <td><p></p></td>
+ </tr>
+
+ <tr>
+ <td>CANCEL_PLAN_CHECK_RUN</td>
+ <td>1</td>
+ <td><p></p></td>
+ </tr>
+
+ <tr>
+ <td>CANCEL_TASK_RUN</td>
+ <td>2</td>
+ <td><p></p></td>
+ </tr>
+
+ </tbody>
+ </table>
+
+
+
+
+
+
+
<div class="file-heading">
<h2 id="store/task.proto">store/task.proto</h2><a href="#title">Top</a>
</div>
diff --git a/proto/store/store/signal.proto b/proto/store/store/signal.proto
new file mode 100644
index 0000000000..903db0faa1
--- /dev/null
+++ b/proto/store/store/signal.proto
@@ -0,0 +1,18 @@
+syntax = "proto3";
+
+package bytebase.store;
+
+option go_package = "generated-go/store";
+
+// Signal represents a notification payload sent via PostgreSQL NOTIFY for HA coordination.
+message Signal {
+ // Type represents the type of signal.
+ enum Type {
+ TYPE_UNSPECIFIED = 0;
+ CANCEL_PLAN_CHECK_RUN = 1;
+ CANCEL_TASK_RUN = 2;
+ }
+
+ Type type = 1;
+ int32 uid = 2;
+}