Skip to content

Commit 0881994

Browse files
authored
instance: protobuf migration v2 (#1315)
* instance: protobuf migration v2 * fix tests
1 parent 0f9f425 commit 0881994

7 files changed

Lines changed: 191 additions & 9 deletions

File tree

Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,12 @@ gen-build:
8787
DEBUG=true go run ${GO_FLAGS} ${PWD} generateSchema ${SCHEMA_FILE}
8888
git add ${SCHEMA_FILE}
8989

90+
gen-proto:
91+
protoc \
92+
--proto_path . \
93+
--go_out . \
94+
protobuf/**
95+
9096
gen-clean:
9197
rm -rf ${PWD}/gen-ts-api/
9298
rm -rf ${PWD}/api/api/

pkg/roles/tsdb/api.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111
"github.com/swaggest/usecase/status"
1212
clientv3 "go.etcd.io/etcd/client/v3"
1313
"go.uber.org/zap"
14+
"google.golang.org/protobuf/proto"
1415
)
1516

1617
func (r *Role) APIMetrics() usecase.Interactor {
@@ -45,16 +46,21 @@ func (r *Role) APIMetrics() usecase.Interactor {
4546
if input.Node != "" && input.Node != node {
4647
continue
4748
}
48-
value, err := strconv.ParseInt(string(kv.Value), 10, 0)
49+
v := types.MetricsRecord{}
50+
err = proto.Unmarshal(kv.Value, &v)
4951
if err != nil {
50-
r.log.Warn("failed to parse metric value", zap.Error(err))
51-
continue
52+
value, err := strconv.ParseInt(string(kv.Value), 10, 0)
53+
if err != nil {
54+
r.log.Warn("failed to parse metric value", zap.Error(err))
55+
continue
56+
}
57+
v.Value = value
5258
}
5359
output.Records = append(output.Records, types.APIMetricsRecord{
5460
Keys: keyParts[:2],
5561
Time: time.Unix(int64(ts), 0),
5662
Node: node,
57-
Value: value,
63+
Value: v.Value,
5864
})
5965
}
6066
return nil

pkg/roles/tsdb/role.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/swaggest/rest/web"
2121
clientv3 "go.etcd.io/etcd/client/v3"
2222
"go.uber.org/zap"
23+
"google.golang.org/protobuf/proto"
2324
)
2425

2526
type Role struct {
@@ -165,10 +166,17 @@ func (r *Role) write(ctx context.Context) {
165166
extconfig.Get().Instance.Identifier,
166167
strconv.FormatInt(time.Now().Unix(), 10),
167168
).String()
168-
_, err := r.i.KV().Put(
169+
v := types.MetricsRecord{
170+
Value: int64(value.Value),
171+
}
172+
rv, err := proto.Marshal(&v)
173+
if err != nil {
174+
r.log.Warn("failed to marshal message", zap.Error(err))
175+
}
176+
_, err = r.i.KV().Put(
169177
ks.Context(),
170178
key,
171-
strconv.Itoa(value.Value),
179+
string(rv),
172180
clientv3.WithLease(lease.ID),
173181
)
174182
if err != nil {

pkg/roles/tsdb/role_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ func TestRoleWrite(t *testing.T) {
101101
types.KeyRole, nameBeforeWrite,
102102
extconfig.Get().Instance.Identifier,
103103
).Prefix(true),
104-
42,
104+
&types.MetricsRecord{Value: 42},
105105
)
106106
tests.AssertEtcd(
107107
t,
@@ -110,7 +110,7 @@ func TestRoleWrite(t *testing.T) {
110110
types.KeyRole, nameSet,
111111
extconfig.Get().Instance.Identifier,
112112
).Prefix(true),
113-
43,
113+
&types.MetricsRecord{Value: 43},
114114
)
115115
tests.AssertEtcd(
116116
t,
@@ -119,6 +119,6 @@ func TestRoleWrite(t *testing.T) {
119119
types.KeyRole, nameInc,
120120
extconfig.Get().Instance.Identifier,
121121
).Prefix(true),
122-
1,
122+
&types.MetricsRecord{Value: 1},
123123
)
124124
}

pkg/roles/tsdb/types/role_tsdb_record.pb.go

Lines changed: 143 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/tests/utils.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ import (
1717
"github.com/google/uuid"
1818
"github.com/stretchr/testify/assert"
1919
clientv3 "go.etcd.io/etcd/client/v3"
20+
"google.golang.org/protobuf/proto"
21+
"google.golang.org/protobuf/reflect/protoreflect"
2022
)
2123

2224
var (
@@ -40,6 +42,14 @@ func MustJSON(in interface{}) string {
4042
return string(j)
4143
}
4244

45+
func MustProto(in protoreflect.ProtoMessage) []byte {
46+
r, err := proto.Marshal(in)
47+
if err != nil {
48+
panic(err)
49+
}
50+
return r
51+
}
52+
4353
func MustParseNetIP(t *testing.T, r string) netip.Addr {
4454
i, err := netip.ParseAddr(r)
4555
assert.NoError(t, err)
@@ -72,6 +82,8 @@ func AssertEtcd(t *testing.T, c *storage.Client, key *storage.Key, expected ...i
7282
assert.Equal(t, rb, values.Kvs[idx].Value)
7383
} else if rb, ok := res.(string); ok {
7484
assert.Equal(t, rb, string(values.Kvs[idx].Value))
85+
} else if rb, ok := res.(protoreflect.ProtoMessage); ok {
86+
assert.Equal(t, MustProto(rb), values.Kvs[idx].Value)
7587
} else {
7688
assert.Equal(t, MustJSON(res), string(values.Kvs[idx].Value))
7789
}

protobuf/role_tsdb_record.proto

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
syntax = "proto3";
2+
3+
option go_package = "pkg/roles/tsdb/types";
4+
5+
message MetricsRecord {
6+
int64 value = 1;
7+
}

0 commit comments

Comments
 (0)