Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrmiskiewicz committed Nov 13, 2024
1 parent 7acc041 commit ef316d9
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 17 deletions.
26 changes: 26 additions & 0 deletions cmd/broker/binding_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,32 @@ func TestBinding(t *testing.T) {
assert.Equal(t, http.StatusOK, resp.StatusCode)
})

t.Run("should return 200 when creating a second binding with the same id and params as an existing one", func(t *testing.T) {
bid = uuid.New().String()
resp = suite.CallAPI(http.MethodPut, fmt.Sprintf("oauth/v2/service_instances/%s/service_bindings/%s", iid, bid),
`{
"service_id": "47c9dcbf-ff30-448e-ab36-d3bad66ba281",
"plan_id": "361c511f-f939-4621-b228-d0fb79a1fe15",
"parameters": {
"expiration_seconds": 1
}
}`)

time.Sleep(2 * time.Second)

resp = suite.CallAPI(http.MethodPut, fmt.Sprintf("oauth/v2/service_instances/%s/service_bindings/%s", iid, bid),
`{
"service_id": "47c9dcbf-ff30-448e-ab36-d3bad66ba281",
"plan_id": "361c511f-f939-4621-b228-d0fb79a1fe15",
"parameters": {
"expiration_seconds": 1
}
}`)
assert.Equal(t, http.StatusOK, resp.StatusCode)
r, _ := io.ReadAll(resp.Body)
fmt.Printf("%s", r)
})

t.Run("should return 409 when creating a second binding with the same id as an existing one but different params", func(t *testing.T) {
bid = uuid.New().String()
resp = suite.CallAPI(http.MethodPut, fmt.Sprintf("oauth/v2/service_instances/%s/service_bindings/%s", iid, bid),
Expand Down
4 changes: 2 additions & 2 deletions cmd/broker/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -930,9 +930,9 @@ func fixConfig() *Config {
Binding: broker.BindingConfig{
Enabled: true,
BindablePlans: []string{"aws", "azure"},
ExpirationSeconds: 600,
ExpirationSeconds: 1,
MaxExpirationSeconds: 7200,
MinExpirationSeconds: 600,
MinExpirationSeconds: 1,
MaxBindingsCount: 10,
CreateBindingTimeout: 15 * time.Second,
},
Expand Down
9 changes: 5 additions & 4 deletions internal/metricsv2/bind.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ type BindingStatitics struct {
logger logrus.FieldLogger

sync sync.Mutex
poolingInterval time.Duration
pollingInterval time.Duration
MinutesSinceEarliestExpirationMetric prometheus.Gauge
}

Expand All @@ -109,12 +109,13 @@ func NewBindingStatsCollector(db storage.Bindings, poolingInterval time.Duration
return &BindingStatitics{
db: db,
logger: logger,
poolingInterval: poolingInterval,
pollingInterval: poolingInterval,
MinutesSinceEarliestExpirationMetric: prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: prometheusNamespacev2,
Subsystem: prometheusSubsystemv2,
Name: "minutes_since_earliest_binding_expiration",
Help: "Specifies the time in minutes since the earliest binding expiration.",
Help: "Specifies the time in minutes since the earliest binding expiration. " +
"The value should not be greater than the binding cleaning job interval. The metric is created to detect problems with the job.",
}),
}
}
Expand All @@ -135,7 +136,7 @@ func (c *BindingStatitics) Job(ctx context.Context) {
c.logger.Error("failed to update metrics metrics", err)
}

ticker := time.NewTicker(c.poolingInterval)
ticker := time.NewTicker(c.pollingInterval)
for {
select {
case <-ticker.C:
Expand Down
2 changes: 1 addition & 1 deletion internal/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ type ERSContextStats struct {
}

type BindingStats struct {
MinutesSinceEarliestExpiration float64
MinutesSinceEarliestExpiration float64 `db:"minutes_since_earliest_expiration"`
}

// NewProvisioningOperation creates a fresh (just starting) instance of the ProvisioningOperation
Expand Down
4 changes: 2 additions & 2 deletions internal/storage/dbmodel/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ type BindingDTO struct {
CreatedBy string
}

type BindingStats struct {
MaxExpirationTimeInSeconds *float64 `db:"max_expiration_time_in_seconds"`
type BindingStatsDTO struct {
SecondsSinceEarliestExpiration *float64 `db:"seconds_since_earliest_expiration"`
}
2 changes: 1 addition & 1 deletion internal/storage/driver/postsql/binding.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (s *Binding) GetStatistics() (internal.BindingStats, error) {
if err != nil {
return internal.BindingStats{}, err
}
return internal.BindingStats{MinutesSinceEarliestExpiration: *dto.MaxExpirationTimeInSeconds / 60}, nil
return internal.BindingStats{MinutesSinceEarliestExpiration: *dto.SecondsSinceEarliestExpiration / 60}, nil
}

func (s *Binding) toBindingDTO(binding *internal.Binding) (dbmodel.BindingDTO, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/storage/postsql/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type ReadSession interface {
GetBinding(instanceID string, bindingID string) (dbmodel.BindingDTO, dberr.Error)
ListBindings(instanceID string) ([]dbmodel.BindingDTO, error)
ListExpiredBindings() ([]dbmodel.BindingDTO, error)
GetBindingsStatistics() (dbmodel.BindingStats, error)
GetBindingsStatistics() (dbmodel.BindingStatsDTO, error)
}

//go:generate mockery --name=WriteSession
Expand Down
12 changes: 6 additions & 6 deletions internal/storage/postsql/read.go
Original file line number Diff line number Diff line change
Expand Up @@ -1053,16 +1053,16 @@ func (r readSession) ListInstancesArchived(filter dbmodel.InstanceFilter) ([]dbm
return instancesArchived, len(instancesArchived), totalCount, nil
}

func (r readSession) GetBindingsStatistics() (dbmodel.BindingStats, error) {
dto := dbmodel.BindingStats{}
statement := r.session.Select("max(extract(epoch from AGE(now(), expires_at))) as max_expiration_time_in_seconds").From(BindingsTableName)
func (r readSession) GetBindingsStatistics() (dbmodel.BindingStatsDTO, error) {
dto := dbmodel.BindingStatsDTO{}
statement := r.session.Select("max(extract(epoch from AGE(now(), expires_at))) as seconds_since_earliest_expiration").From(BindingsTableName)

err := statement.LoadOne(&dto)
if err != nil {
return dbmodel.BindingStats{}, err
return dbmodel.BindingStatsDTO{}, err
}
if dto.MaxExpirationTimeInSeconds == nil {
dto.MaxExpirationTimeInSeconds = new(float64)
if dto.SecondsSinceEarliestExpiration == nil {
dto.SecondsSinceEarliestExpiration = new(float64)
}
return dto, nil
}
Expand Down

0 comments on commit ef316d9

Please sign in to comment.