-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement scaling latency metrics through logical clock
Signed-off-by: Oleg Vasilev <[email protected]>
- Loading branch information
Showing
15 changed files
with
371 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package logicclock | ||
|
||
import ( | ||
"errors" | ||
"time" | ||
|
||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
vmv1 "github.com/neondatabase/autoscaling/neonvm/apis/neonvm/v1" | ||
) | ||
|
||
type ClockSource struct { | ||
cb func(time.Duration) | ||
ts []time.Time | ||
offset int64 | ||
Now func() v1.Time | ||
} | ||
|
||
func NewClockSource(cb func(time.Duration)) *ClockSource { | ||
return &ClockSource{ | ||
cb: cb, | ||
ts: nil, | ||
offset: 0, | ||
Now: v1.Now, | ||
} | ||
} | ||
|
||
func (c *ClockSource) Next() *vmv1.LogicalTime { | ||
ret := vmv1.LogicalTime{ | ||
Value: c.offset + int64(len(c.ts)), | ||
UpdatedAt: c.Now(), | ||
} | ||
c.ts = append(c.ts, ret.UpdatedAt.Time) | ||
return &ret | ||
} | ||
|
||
func (c *ClockSource) Observe(clock *vmv1.LogicalTime) error { | ||
if clock == nil { | ||
return nil | ||
} | ||
if clock.Value < c.offset { | ||
return nil | ||
} | ||
|
||
idx := clock.Value - c.offset | ||
if idx > int64(len(c.ts)) { | ||
return errors.New("clock value is in the future") | ||
} | ||
|
||
diff := clock.UpdatedAt.Time.Sub(c.ts[idx]) | ||
|
||
c.cb(diff) | ||
|
||
c.offset = clock.Value + 1 | ||
c.ts = c.ts[idx+1:] | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package logicclock_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
v1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
|
||
vmv1 "github.com/neondatabase/autoscaling/neonvm/apis/neonvm/v1" | ||
"github.com/neondatabase/autoscaling/pkg/agent/core/logicclock" | ||
) | ||
|
||
type testClockMetric struct { | ||
*logicclock.ClockSource | ||
t *testing.T | ||
now v1.Time | ||
result *time.Duration | ||
} | ||
|
||
func (tcm *testClockMetric) advance(d time.Duration) { | ||
tcm.now = v1.NewTime(tcm.now.Add(d)) | ||
} | ||
|
||
func (tcm *testClockMetric) assertResult(d time.Duration) { | ||
require.NotNil(tcm.t, tcm.result) | ||
assert.Equal(tcm.t, d, *tcm.result) | ||
tcm.result = nil | ||
} | ||
|
||
func newTestClockMetric(t *testing.T) *testClockMetric { | ||
tcm := &testClockMetric{ | ||
ClockSource: nil, | ||
t: t, | ||
now: v1.NewTime(time.Now()), | ||
result: nil, | ||
} | ||
|
||
cb := func(d time.Duration) { | ||
tcm.result = &d | ||
} | ||
tcm.ClockSource = logicclock.NewClockSource(cb) | ||
tcm.ClockSource.Now = func() v1.Time { | ||
return tcm.now | ||
} | ||
|
||
return tcm | ||
} | ||
|
||
func TestClockMetric(t *testing.T) { | ||
tcm := newTestClockMetric(t) | ||
|
||
// Generate new clock | ||
cl := tcm.Next() | ||
assert.Equal(t, int64(0), cl.Value) | ||
|
||
// Observe it coming back in 5 seconds | ||
tcm.advance(5 * time.Second) | ||
err := tcm.Observe(&vmv1.LogicalTime{ | ||
Value: 0, | ||
UpdatedAt: tcm.now, | ||
}) | ||
assert.NoError(t, err) | ||
tcm.assertResult(5 * time.Second) | ||
} | ||
|
||
func TestClockMetricSkip(t *testing.T) { | ||
tcm := newTestClockMetric(t) | ||
|
||
// Generate new clock | ||
cl := tcm.Next() | ||
assert.Equal(t, int64(0), cl.Value) | ||
|
||
// Generate another one | ||
tcm.advance(5 * time.Second) | ||
cl = tcm.Next() | ||
assert.Equal(t, int64(1), cl.Value) | ||
|
||
// Observe the first one | ||
tcm.advance(5 * time.Second) | ||
err := tcm.Observe(&vmv1.LogicalTime{ | ||
Value: 0, | ||
UpdatedAt: tcm.now, | ||
}) | ||
assert.NoError(t, err) | ||
tcm.assertResult(10 * time.Second) | ||
|
||
// Observe the second one | ||
tcm.advance(2 * time.Second) | ||
err = tcm.Observe(&vmv1.LogicalTime{ | ||
Value: 1, | ||
UpdatedAt: tcm.now, | ||
}) | ||
assert.NoError(t, err) | ||
tcm.assertResult(7 * time.Second) | ||
} | ||
|
||
func TestClockMetricStale(t *testing.T) { | ||
tcm := newTestClockMetric(t) | ||
|
||
// Generate new clock | ||
cl := tcm.Next() | ||
assert.Equal(t, int64(0), cl.Value) | ||
|
||
// Observe it coming back in 5 seconds | ||
tcm.advance(5 * time.Second) | ||
err := tcm.Observe(&vmv1.LogicalTime{ | ||
Value: 0, | ||
UpdatedAt: tcm.now, | ||
}) | ||
assert.NoError(t, err) | ||
tcm.assertResult(5 * time.Second) | ||
|
||
// Observe it coming back again | ||
tcm.advance(5 * time.Second) | ||
err = tcm.Observe(&vmv1.LogicalTime{ | ||
Value: 0, | ||
UpdatedAt: tcm.now, | ||
}) | ||
// No error, but no result either | ||
assert.NoError(t, err) | ||
assert.Nil(t, tcm.result) | ||
} |
Oops, something went wrong.