Skip to content

Commit

Permalink
migrate tsdb
Browse files Browse the repository at this point in the history
  • Loading branch information
BeryJu committed May 31, 2023
1 parent ab64c78 commit 5ce76a0
Show file tree
Hide file tree
Showing 6 changed files with 185 additions and 17 deletions.
2 changes: 1 addition & 1 deletion pkg/roles/tsdb/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
type Role struct {
log *zap.Logger
i roles.Instance
cfg *RoleConfig
cfg *types.TSDBRoleConfig
ctx context.Context
m map[string]types.Metric
ms sync.RWMutex
Expand Down
22 changes: 9 additions & 13 deletions pkg/roles/tsdb/role_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/json"

instanceTypes "beryju.io/gravity/pkg/instance/types"
"beryju.io/gravity/pkg/roles/tsdb/types"
"beryju.io/gravity/pkg/storage"
"go.uber.org/zap"

"github.com/swaggest/usecase"
Expand All @@ -15,35 +17,29 @@ const (
KeyRole = "tsdb"
)

type RoleConfig struct {
Enabled bool `json:"enabled"`
Expire int64 `json:"expire"`
Scrape int64 `json:"scrape"`
}

func (r *Role) decodeRoleConfig(raw []byte) *RoleConfig {
def := RoleConfig{
func (r *Role) decodeRoleConfig(raw []byte) *types.TSDBRoleConfig {
def := types.TSDBRoleConfig{
Enabled: true,
Expire: 60 * 30,
Scrape: 30,
}
if len(raw) < 1 {
return &def
}
err := json.Unmarshal(raw, &def)
conf, err := storage.Parse(raw, &def)
if err != nil {
r.log.Warn("failed to decode role config", zap.Error(err))
}
return &def
return conf
}

type APIRoleConfigOutput struct {
Config RoleConfig `json:"config" required:"true"`
Config *types.TSDBRoleConfig `json:"config" required:"true"`
}

func (r *Role) APIRoleConfigGet() usecase.Interactor {
u := usecase.NewInteractor(func(ctx context.Context, input struct{}, output *APIRoleConfigOutput) error {
output.Config = *r.cfg
output.Config = r.cfg
return nil
})
u.SetName("tsdb.get_role_config")
Expand All @@ -53,7 +49,7 @@ func (r *Role) APIRoleConfigGet() usecase.Interactor {
}

type APIRoleConfigInput struct {
Config RoleConfig `json:"config" required:"true"`
Config *types.TSDBRoleConfig `json:"config" required:"true"`
}

func (r *Role) APIRoleConfigPut() usecase.Interactor {
Expand Down
3 changes: 2 additions & 1 deletion pkg/roles/tsdb/role_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"beryju.io/gravity/pkg/instance"
"beryju.io/gravity/pkg/roles/tsdb"
"beryju.io/gravity/pkg/roles/tsdb/types"
"beryju.io/gravity/pkg/tests"
"github.com/stretchr/testify/assert"
)
Expand All @@ -31,7 +32,7 @@ func TestAPIRoleConfigPut(t *testing.T) {
defer role.Stop()

assert.NoError(t, role.APIRoleConfigPut().Interact(ctx, tsdb.APIRoleConfigInput{
Config: tsdb.RoleConfig{
Config: &types.TSDBRoleConfig{
Enabled: false,
},
}, &struct{}{}))
Expand Down
4 changes: 2 additions & 2 deletions pkg/roles/tsdb/role_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func TestRoleStartNotEnabled(t *testing.T) {
inst := rootInst.ForRole("tsdb", ctx)
role := tsdb.New(inst)
assert.NotNil(t, role)
assert.Error(t, roles.ErrRoleNotConfigured, role.Start(ctx, []byte(tests.MustJSON(tsdb.RoleConfig{
assert.Error(t, roles.ErrRoleNotConfigured, role.Start(ctx, []byte(tests.MustPB(&types.TSDBRoleConfig{
Enabled: false,
}))))
defer role.Stop()
Expand All @@ -60,7 +60,7 @@ func TestRoleWrite(t *testing.T) {
ctx := tests.Context()
inst := rootInst.ForRole("tsdb", ctx)
role := tsdb.New(inst)
assert.NoError(t, role.Start(ctx, []byte(tests.MustJSON(tsdb.RoleConfig{
assert.NoError(t, role.Start(ctx, []byte(tests.MustPB(&types.TSDBRoleConfig{
Enabled: true,
Scrape: 0,
}))))
Expand Down
162 changes: 162 additions & 0 deletions pkg/roles/tsdb/types/role_tsdb_config.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions protobuf/role_tsdb_config.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
syntax = "proto3";

option go_package = "pkg/roles/tsdb/types";

message TSDBRoleConfig {
bool enabled = 1;
int64 expire = 2;
int64 scrape = 3;
}

0 comments on commit 5ce76a0

Please sign in to comment.