-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1936 from keboola/michaljurecko-PSGO-598-close-sync
feat: Add closesync pkg to sync between source and coordinators nodes
- Loading branch information
Showing
14 changed files
with
517 additions
and
45 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
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
33 changes: 33 additions & 0 deletions
33
...l/pkg/service/stream/storage/level/local/diskwriter/network/router/closesync/closesync.go
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,33 @@ | ||
// Package closesync provides synchronization between source and coordinator nodes regarding closing slices | ||
// The coordinator nodes are waiting for slice pipeline to finish, the router nodes notify about closed slices. | ||
package closesync | ||
|
||
import ( | ||
etcd "go.etcd.io/etcd/client/v3" | ||
|
||
"github.com/keboola/keboola-as-code/internal/pkg/log" | ||
"github.com/keboola/keboola-as-code/internal/pkg/service/common/etcdop" | ||
"github.com/keboola/keboola-as-code/internal/pkg/service/common/etcdop/serde" | ||
"github.com/keboola/keboola-as-code/internal/pkg/service/common/servicectx" | ||
) | ||
|
||
type dependencies interface { | ||
Logger() log.Logger | ||
Process() *servicectx.Process | ||
EtcdClient() *etcd.Client | ||
EtcdSerde() *serde.Serde | ||
} | ||
|
||
type schema struct { | ||
prefix etcdop.PrefixT[int64] | ||
} | ||
|
||
func newSchema(s *serde.Serde) schema { | ||
return schema{ | ||
prefix: etcdop.NewTypedPrefix[int64]("runtime/closesync/source/node", s), | ||
} | ||
} | ||
|
||
func (s schema) SourceNode(sourceNodeID string) etcdop.KeyT[int64] { | ||
return s.prefix.Key(sourceNodeID) | ||
} |
170 changes: 170 additions & 0 deletions
170
.../service/stream/storage/level/local/diskwriter/network/router/closesync/closesync_test.go
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,170 @@ | ||
package closesync_test | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/keboola/keboola-as-code/internal/pkg/service/stream/dependencies" | ||
"github.com/keboola/keboola-as-code/internal/pkg/service/stream/storage/level/local/diskwriter/network/router/closesync" | ||
"github.com/keboola/keboola-as-code/internal/pkg/utils/errors" | ||
"github.com/keboola/keboola-as-code/internal/pkg/utils/etcdhelper" | ||
) | ||
|
||
func TestSourceAndCoordinatorNodes(t *testing.T) { | ||
t.Parallel() | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
defer cancel() | ||
|
||
d, mock := dependencies.NewMockedServiceScope(t) | ||
client := mock.TestEtcdClient() | ||
|
||
// Create 3 source nodes and 1 coordinator node | ||
coordinator, err := closesync.NewCoordinatorNode(d) | ||
require.NoError(t, err) | ||
assert.Equal(t, closesync.NoSourceNode, coordinator.MinRevInUse()) | ||
s1, err := closesync.NewSourceNode(d, "source-node-1") | ||
require.NoError(t, err) | ||
s2, err := closesync.NewSourceNode(d, "source-node-2") | ||
require.NoError(t, err) | ||
s3, err := closesync.NewSourceNode(d, "source-node-3") | ||
require.NoError(t, err) | ||
|
||
// Helper | ||
waitForMinRevInUse := func(t *testing.T, r int64) { | ||
t.Helper() | ||
assert.EventuallyWithT(t, func(c *assert.CollectT) { | ||
assert.Equal(c, r, coordinator.MinRevInUse()) | ||
}, 10*time.Second, 10*time.Millisecond) | ||
} | ||
waitForEtcdState := func(t *testing.T, expected string) { | ||
t.Helper() | ||
assert.EventuallyWithT(t, func(c *assert.CollectT) { | ||
etcdhelper.AssertKVsString(c, client, expected) | ||
}, 10*time.Second, 10*time.Millisecond) | ||
} | ||
|
||
// Check initial etcd state | ||
waitForEtcdState(t, ` | ||
<<<<< | ||
runtime/closesync/source/node/source-node-1 (lease) | ||
----- | ||
0 | ||
>>>>> | ||
<<<<< | ||
runtime/closesync/source/node/source-node-2 (lease) | ||
----- | ||
0 | ||
>>>>> | ||
<<<<< | ||
runtime/closesync/source/node/source-node-3 (lease) | ||
----- | ||
0 | ||
>>>>> | ||
`) | ||
assert.Equal(t, int64(0), coordinator.MinRevInUse()) | ||
|
||
// The progress of individual source nodes is different | ||
assert.NoError(t, s1.Notify(ctx, 100)) | ||
assert.NoError(t, s2.Notify(ctx, 101)) | ||
assert.NoError(t, s3.Notify(ctx, 102)) | ||
waitForMinRevInUse(t, 100) | ||
waitForEtcdState(t, ` | ||
<<<<< | ||
runtime/closesync/source/node/source-node-1 (lease) | ||
----- | ||
100 | ||
>>>>> | ||
<<<<< | ||
runtime/closesync/source/node/source-node-2 (lease) | ||
----- | ||
101 | ||
>>>>> | ||
<<<<< | ||
runtime/closesync/source/node/source-node-3 (lease) | ||
----- | ||
102 | ||
>>>>> | ||
`) | ||
|
||
// Revisions <= 100 are unblocked | ||
select { | ||
case <-coordinator.WaitForRevisionChan(99): | ||
default: | ||
assert.Fail(t, "channel should be closed") | ||
} | ||
select { | ||
case <-coordinator.WaitForRevisionChan(100): | ||
default: | ||
assert.Fail(t, "channel should be closed") | ||
} | ||
|
||
// Revisions > 100 are blocked | ||
wait101 := coordinator.WaitForRevisionChan(101) | ||
wait102 := coordinator.WaitForRevisionChan(102) | ||
wait103 := coordinator.WaitForRevisionChan(103) | ||
select { | ||
case <-wait101: | ||
assert.Fail(t, "channel shouldn't be closed") | ||
case <-wait102: | ||
assert.Fail(t, "channel shouldn't be closed") | ||
case <-wait103: | ||
assert.Fail(t, "channel shouldn't be closed") | ||
default: | ||
} | ||
|
||
// Unblock 101 | ||
require.NoError(t, s1.Notify(ctx, 200)) | ||
waitForMinRevInUse(t, 101) | ||
select { | ||
case <-wait101: | ||
default: | ||
assert.Fail(t, "channel should be closed") | ||
} | ||
select { | ||
case <-wait102: | ||
assert.Fail(t, "channel shouldn't be closed") | ||
case <-wait103: | ||
assert.Fail(t, "channel shouldn't be closed") | ||
default: | ||
} | ||
|
||
// Unblock 102 | ||
require.NoError(t, s2.Notify(ctx, 200)) | ||
waitForMinRevInUse(t, 102) | ||
select { | ||
case <-wait102: | ||
default: | ||
assert.Fail(t, "channel should be closed") | ||
} | ||
select { | ||
case <-wait103: | ||
assert.Fail(t, "channel shouldn't be closed") | ||
default: | ||
} | ||
|
||
// Unblock 103 | ||
require.NoError(t, s3.Notify(ctx, 200)) | ||
waitForMinRevInUse(t, 200) | ||
select { | ||
case <-wait103: | ||
default: | ||
assert.Fail(t, "channel should be closed") | ||
} | ||
|
||
// Shutdown | ||
d.Process().Shutdown(ctx, errors.New("bye bye")) | ||
d.Process().WaitForShutdown() | ||
waitForEtcdState(t, ``) | ||
|
||
// No error is logged | ||
mock.DebugLogger().AssertJSONMessages(t, "") | ||
} |
Oops, something went wrong.