Skip to content

Commit

Permalink
Add election test for background routine handling
Browse files Browse the repository at this point in the history
  • Loading branch information
krapie committed Jul 8, 2023
1 parent 4811338 commit 23e9f1d
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 10 deletions.
2 changes: 1 addition & 1 deletion server/backend/database/mongo/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1467,7 +1467,7 @@ func (c *Client) RenewLeaderLease(
// FindLeader returns the leader hostname for the given leaseLockName.
func (c *Client) FindLeader(ctx context.Context, leaseLockName string) (*string, error) {
electionInfo := &struct {
ElectionId string `bson:"election_id"`
ElectionID string `bson:"election_id"`
LeaderID string `bson:"leader_id"`
LeaseExpireAt gotime.Time `bson:"lease_expire_at"`
}{}
Expand Down
2 changes: 2 additions & 0 deletions server/backend/database/mongo/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
package mongo_test

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"

Expand Down
10 changes: 9 additions & 1 deletion server/backend/election/mongo/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package mongo

import (
"context"
"sync"
"time"

"github.com/yorkie-team/yorkie/server/backend/database"
Expand All @@ -33,6 +34,8 @@ type Elector struct {

ctx context.Context
cancelFunc context.CancelFunc

wg sync.WaitGroup
}

// NewElector creates a new elector instance.
Expand Down Expand Up @@ -70,6 +73,7 @@ func (e *Elector) StartElection(
// Stop stops all leader elections.
func (e *Elector) Stop() error {
e.cancelFunc()
e.wg.Wait()

return nil
}
Expand All @@ -91,7 +95,11 @@ func (e *Elector) run(
}

if acquired {
go onStartLeading(ctx)
go func() {
e.wg.Add(1)
onStartLeading(ctx)
e.wg.Done()
}()
logging.From(ctx).Infof(
"leader elected: %s", e.hostname,
)
Expand Down
42 changes: 34 additions & 8 deletions server/backend/election/mongo/election_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package mongo_test

import (
"context"
"testing"
"time"

"github.com/stretchr/testify/assert"

"github.com/yorkie-team/yorkie/server/backend/database/mongo"
mongoelection "github.com/yorkie-team/yorkie/server/backend/election/mongo"
"github.com/yorkie-team/yorkie/test/helper"
"testing"
"time"
)

var (
Expand Down Expand Up @@ -41,8 +43,11 @@ func TestElection(t *testing.T) {
electorC := mongoelection.NewElector("C", db)

assert.NoError(t, electorA.StartElection(leaseLockName, helper.LeaseDuration, normalTask, stopTask))
time.Sleep(helper.LeaseDuration)

assert.NoError(t, electorB.StartElection(leaseLockName, helper.LeaseDuration, normalTask, stopTask))
assert.NoError(t, electorC.StartElection(leaseLockName, helper.LeaseDuration, normalTask, stopTask))
time.Sleep(helper.LeaseDuration)

// elector A will be the leader because it is the first to start the election.
leader, err := db.FindLeader(context.Background(), leaseLockName)
Expand Down Expand Up @@ -71,18 +76,20 @@ func TestElection(t *testing.T) {
t.Run("lease renewal while handling a a long task test", func(t *testing.T) {
leaseLockName := t.Name()
longTask := func(ctx context.Context) {
time.Sleep(helper.LeaseDuration * 2)
time.Sleep(helper.LeaseDuration * 4)
}

electorA := mongoelection.NewElector("A", db)
electorB := mongoelection.NewElector("B", db)
electorC := mongoelection.NewElector("C", db)

assert.NoError(t, electorA.StartElection(leaseLockName, helper.LeaseDuration, longTask, stopTask))
assert.NoError(t, electorB.StartElection(leaseLockName, helper.LeaseDuration, normalTask, stopTask))
assert.NoError(t, electorC.StartElection(leaseLockName, helper.LeaseDuration, normalTask, stopTask))
time.Sleep(helper.LeaseDuration)

assert.NoError(t, electorB.StartElection(leaseLockName, helper.LeaseDuration, longTask, stopTask))
assert.NoError(t, electorC.StartElection(leaseLockName, helper.LeaseDuration, longTask, stopTask))

// check if elector A is still the leader
// wait for lease expiration and check if elector A is still the leader while handling a long task
time.Sleep(helper.LeaseDuration)

leader, err := db.FindLeader(context.Background(), leaseLockName)
Expand All @@ -92,7 +99,26 @@ func TestElection(t *testing.T) {
})

t.Run("handle background routines when shutting down the server test", func(t *testing.T) {
// TODO(krapie): find the way to gradually close election routines
t.Skip()
shutdownCh := make(chan struct{})

isTaskDone := false
longTask := func(ctx context.Context) {
close(shutdownCh)
time.Sleep(helper.LeaseDuration)
isTaskDone = true
}

elector := mongoelection.NewElector("A", db)
assert.NoError(t, elector.StartElection(t.Name(), helper.LeaseDuration, longTask, stopTask))

// if receive shutdown signal, stop elector
select {
case <-shutdownCh:
assert.NoError(t, elector.Stop())
}

// check if the task is done
// this means that the background routine is handled properly after server(elector) is stopped
assert.Equal(t, true, isTaskDone)
})
}

1 comment on commit 23e9f1d

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Go Benchmark

Benchmark suite Current: 23e9f1d Previous: 070b6bf Ratio
BenchmarkDocument/constructor_test - ns/op 1259 ns/op 1275 ns/op 0.99
BenchmarkDocument/constructor_test - B/op 752 B/op 752 B/op 1
BenchmarkDocument/constructor_test - allocs/op 12 allocs/op 12 allocs/op 1
BenchmarkDocument/status_test - ns/op 642.2 ns/op 946.9 ns/op 0.68
BenchmarkDocument/status_test - B/op 720 B/op 720 B/op 1
BenchmarkDocument/status_test - allocs/op 10 allocs/op 10 allocs/op 1
BenchmarkDocument/equals_test - ns/op 10410 ns/op 7299 ns/op 1.43
BenchmarkDocument/equals_test - B/op 5072 B/op 5072 B/op 1
BenchmarkDocument/equals_test - allocs/op 85 allocs/op 85 allocs/op 1
BenchmarkDocument/nested_update_test - ns/op 21086 ns/op 21177 ns/op 1.00
BenchmarkDocument/nested_update_test - B/op 11033 B/op 11033 B/op 1
BenchmarkDocument/nested_update_test - allocs/op 235 allocs/op 235 allocs/op 1
BenchmarkDocument/delete_test - ns/op 27717 ns/op 28530 ns/op 0.97
BenchmarkDocument/delete_test - B/op 14162 B/op 14162 B/op 1
BenchmarkDocument/delete_test - allocs/op 310 allocs/op 310 allocs/op 1
BenchmarkDocument/object_test - ns/op 9816 ns/op 9671 ns/op 1.01
BenchmarkDocument/object_test - B/op 5793 B/op 5792 B/op 1.00
BenchmarkDocument/object_test - allocs/op 97 allocs/op 97 allocs/op 1
BenchmarkDocument/array_test - ns/op 34751 ns/op 34819 ns/op 1.00
BenchmarkDocument/array_test - B/op 10889 B/op 10889 B/op 1
BenchmarkDocument/array_test - allocs/op 251 allocs/op 251 allocs/op 1
BenchmarkDocument/text_test - ns/op 37330 ns/op 37792 ns/op 0.99
BenchmarkDocument/text_test - B/op 14058 B/op 14058 B/op 1
BenchmarkDocument/text_test - allocs/op 456 allocs/op 456 allocs/op 1
BenchmarkDocument/text_composition_test - ns/op 38102 ns/op 38249 ns/op 1.00
BenchmarkDocument/text_composition_test - B/op 17538 B/op 17538 B/op 1
BenchmarkDocument/text_composition_test - allocs/op 461 allocs/op 461 allocs/op 1
BenchmarkDocument/rich_text_test - ns/op 99238 ns/op 102928 ns/op 0.96
BenchmarkDocument/rich_text_test - B/op 36007 B/op 36015 B/op 1.00
BenchmarkDocument/rich_text_test - allocs/op 1108 allocs/op 1108 allocs/op 1
BenchmarkDocument/counter_test - ns/op 19725 ns/op 19762 ns/op 1.00
BenchmarkDocument/counter_test - B/op 9058 B/op 9057 B/op 1.00
BenchmarkDocument/counter_test - allocs/op 212 allocs/op 212 allocs/op 1
BenchmarkDocument/text_edit_gc_100 - ns/op 4031999 ns/op 4040516 ns/op 1.00
BenchmarkDocument/text_edit_gc_100 - B/op 1552534 B/op 1552537 B/op 1.00
BenchmarkDocument/text_edit_gc_100 - allocs/op 17147 allocs/op 17147 allocs/op 1
BenchmarkDocument/text_edit_gc_1000 - ns/op 314080618 ns/op 316295025 ns/op 0.99
BenchmarkDocument/text_edit_gc_1000 - B/op 136642192 B/op 136642972 B/op 1.00
BenchmarkDocument/text_edit_gc_1000 - allocs/op 210737 allocs/op 210741 allocs/op 1.00
BenchmarkDocument/text_split_gc_100 - ns/op 4625619 ns/op 4596780 ns/op 1.01
BenchmarkDocument/text_split_gc_100 - B/op 2217349 B/op 2216867 B/op 1.00
BenchmarkDocument/text_split_gc_100 - allocs/op 16576 allocs/op 16572 allocs/op 1.00
BenchmarkDocument/text_split_gc_1000 - ns/op 373884041 ns/op 368857070 ns/op 1.01
BenchmarkDocument/text_split_gc_1000 - B/op 214897512 B/op 214861816 B/op 1.00
BenchmarkDocument/text_split_gc_1000 - allocs/op 211608 allocs/op 211441 allocs/op 1.00
BenchmarkDocument/text_delete_all_10000 - ns/op 15745173 ns/op 21021034 ns/op 0.75
BenchmarkDocument/text_delete_all_10000 - B/op 5903089 B/op 5904710 B/op 1.00
BenchmarkDocument/text_delete_all_10000 - allocs/op 41117 allocs/op 41123 allocs/op 1.00
BenchmarkDocument/text_delete_all_100000 - ns/op 194394746 ns/op 280500940 ns/op 0.69
BenchmarkDocument/text_delete_all_100000 - B/op 53851506 B/op 53838868 B/op 1.00
BenchmarkDocument/text_delete_all_100000 - allocs/op 416026 allocs/op 415985 allocs/op 1.00
BenchmarkDocument/text_100 - ns/op 303529 ns/op 326751 ns/op 0.93
BenchmarkDocument/text_100 - B/op 117746 B/op 117749 B/op 1.00
BenchmarkDocument/text_100 - allocs/op 5064 allocs/op 5064 allocs/op 1
BenchmarkDocument/text_1000 - ns/op 3304295 ns/op 3531932 ns/op 0.94
BenchmarkDocument/text_1000 - B/op 1152349 B/op 1152360 B/op 1.00
BenchmarkDocument/text_1000 - allocs/op 50068 allocs/op 50068 allocs/op 1
BenchmarkDocument/array_1000 - ns/op 1712472 ns/op 1783562 ns/op 0.96
BenchmarkDocument/array_1000 - B/op 1102045 B/op 1101945 B/op 1.00
BenchmarkDocument/array_1000 - allocs/op 11854 allocs/op 11854 allocs/op 1
BenchmarkDocument/array_10000 - ns/op 18544280 ns/op 20886913 ns/op 0.89
BenchmarkDocument/array_10000 - B/op 9905058 B/op 9905648 B/op 1.00
BenchmarkDocument/array_10000 - allocs/op 120701 allocs/op 120704 allocs/op 1.00
BenchmarkDocument/array_gc_100 - ns/op 177738 ns/op 182314 ns/op 0.97
BenchmarkDocument/array_gc_100 - B/op 97426 B/op 97418 B/op 1.00
BenchmarkDocument/array_gc_100 - allocs/op 1227 allocs/op 1226 allocs/op 1.00
BenchmarkDocument/array_gc_1000 - ns/op 2014559 ns/op 2023966 ns/op 1.00
BenchmarkDocument/array_gc_1000 - B/op 1169549 B/op 1169557 B/op 1.00
BenchmarkDocument/array_gc_1000 - allocs/op 12889 allocs/op 12889 allocs/op 1
BenchmarkDocument/counter_1000 - ns/op 271286 ns/op 291234 ns/op 0.93
BenchmarkDocument/counter_1000 - B/op 197876 B/op 197876 B/op 1
BenchmarkDocument/counter_1000 - allocs/op 6490 allocs/op 6490 allocs/op 1
BenchmarkDocument/counter_10000 - ns/op 2905572 ns/op 3160261 ns/op 0.92
BenchmarkDocument/counter_10000 - B/op 2164796 B/op 2164821 B/op 1.00
BenchmarkDocument/counter_10000 - allocs/op 69497 allocs/op 69497 allocs/op 1
BenchmarkDocument/object_1000 - ns/op 1905094 ns/op 1916070 ns/op 0.99
BenchmarkDocument/object_1000 - B/op 1450758 B/op 1450515 B/op 1.00
BenchmarkDocument/object_1000 - allocs/op 9902 allocs/op 9901 allocs/op 1.00
BenchmarkDocument/object_10000 - ns/op 21157482 ns/op 24081969 ns/op 0.88
BenchmarkDocument/object_10000 - B/op 12368547 B/op 12370050 B/op 1.00
BenchmarkDocument/object_10000 - allocs/op 101204 allocs/op 101208 allocs/op 1.00
BenchmarkRPC/client_to_server - ns/op 404743863 ns/op 454195849 ns/op 0.89
BenchmarkRPC/client_to_server - B/op 18922306 B/op 18895277 B/op 1.00
BenchmarkRPC/client_to_server - allocs/op 308338 allocs/op 307856 allocs/op 1.00
BenchmarkRPC/client_to_client_via_server - ns/op 667713884 ns/op 795188382 ns/op 0.84
BenchmarkRPC/client_to_client_via_server - B/op 34933448 B/op 34158392 B/op 1.02
BenchmarkRPC/client_to_client_via_server - allocs/op 572706 allocs/op 557902 allocs/op 1.03
BenchmarkRPC/attach_large_document - ns/op 1536364639 ns/op 1613558357 ns/op 0.95
BenchmarkRPC/attach_large_document - B/op 2164301824 B/op 2156784120 B/op 1.00
BenchmarkRPC/attach_large_document - allocs/op 11112 allocs/op 10070 allocs/op 1.10
BenchmarkRPC/adminCli_to_server - ns/op 535208160 ns/op 566988205 ns/op 0.94
BenchmarkRPC/adminCli_to_server - B/op 20392576 B/op 20392732 B/op 1.00
BenchmarkRPC/adminCli_to_server - allocs/op 321594 allocs/op 321623 allocs/op 1.00
BenchmarkLocker - ns/op 123.6 ns/op 124.5 ns/op 0.99
BenchmarkLocker - B/op 16 B/op 16 B/op 1
BenchmarkLocker - allocs/op 1 allocs/op 1 allocs/op 1
BenchmarkLockerParallel - ns/op 114.9 ns/op 135.9 ns/op 0.85
BenchmarkLockerParallel - B/op 0 B/op 0 B/op NaN
BenchmarkLockerParallel - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkLockerMoreKeys - ns/op 374.9 ns/op 319.9 ns/op 1.17
BenchmarkLockerMoreKeys - B/op 14 B/op 14 B/op 1
BenchmarkLockerMoreKeys - allocs/op 0 allocs/op 0 allocs/op NaN
BenchmarkSync/memory_sync_10_test - ns/op 7261 ns/op 7623 ns/op 0.95
BenchmarkSync/memory_sync_10_test - B/op 1340 B/op 1338 B/op 1.00
BenchmarkSync/memory_sync_10_test - allocs/op 39 allocs/op 39 allocs/op 1
BenchmarkSync/memory_sync_100_test - ns/op 61642 ns/op 67864 ns/op 0.91
BenchmarkSync/memory_sync_100_test - B/op 9038 B/op 8876 B/op 1.02
BenchmarkSync/memory_sync_100_test - allocs/op 295 allocs/op 285 allocs/op 1.04
BenchmarkSync/memory_sync_1000_test - ns/op 634504 ns/op 675295 ns/op 0.94
BenchmarkSync/memory_sync_1000_test - B/op 84078 B/op 81640 B/op 1.03
BenchmarkSync/memory_sync_1000_test - allocs/op 2727 allocs/op 2574 allocs/op 1.06
BenchmarkSync/memory_sync_10000_test - ns/op 6573878 ns/op 7464279 ns/op 0.88
BenchmarkSync/memory_sync_10000_test - B/op 858531 B/op 851108 B/op 1.01
BenchmarkSync/memory_sync_10000_test - allocs/op 27951 allocs/op 26725 allocs/op 1.05
BenchmarkTextEditing - ns/op 24716840477 ns/op 27333456807 ns/op 0.90
BenchmarkTextEditing - B/op 8436115952 B/op 8436351072 B/op 1.00
BenchmarkTextEditing - allocs/op 19835031 allocs/op 19836085 allocs/op 1.00

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.