-
Notifications
You must be signed in to change notification settings - Fork 2
Add multi-VM runbook and fixed Raft bootstrap members #326
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
bootjp
wants to merge
12
commits into
main
Choose a base branch
from
feature/prd-workload
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,893
−322
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8e3e9cc
Add deployment docs and improve test coverage
bootjp 5371c32
Refactor tests for parallel execution and clarity
bootjp 9ac59fa
Add error handling and context timeouts
bootjp 8a9cd9c
Merge branch 'main' into feature/prd-workload
bootjp 5cb4a44
Update test and retry logic; pin grpcurl image version
bootjp 70f113e
Merge branch 'feature/prd-workload' of github.com:bootjp/elastickv in…
bootjp 9e45c74
Add RPC timeout and refactor E2E listeners
bootjp 5095592
Handle join retry on context cancellation
bootjp 90f9689
Add validation to replaceNames function
bootjp f5b2706
Refactor runtime server startup logic
bootjp 7644d6d
Refactor DynamoDB attribute name validation logic
bootjp 3cbeaa3
Refactor bootstrap logic for raft initialization
bootjp File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,330 @@ | ||
| package adapter | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "context" | ||
| "errors" | ||
| "net" | ||
| "strconv" | ||
| "sync" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/Jille/raft-grpc-leader-rpc/leaderhealth" | ||
| "github.com/Jille/raftadmin" | ||
| raftadminpb "github.com/Jille/raftadmin/proto" | ||
| "github.com/bootjp/elastickv/kv" | ||
| pb "github.com/bootjp/elastickv/proto" | ||
| "github.com/bootjp/elastickv/store" | ||
| "github.com/hashicorp/raft" | ||
| "github.com/stretchr/testify/require" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/credentials/insecure" | ||
| ) | ||
|
|
||
| func TestAddVoterJoinPath_RegistersMemberAndServesAdapterTraffic(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| const ( | ||
| waitTimeout = 12 * time.Second | ||
| waitInterval = 100 * time.Millisecond | ||
| rpcTimeout = 2 * time.Second | ||
| ) | ||
|
|
||
| ctx := context.Background() | ||
| nodes, servers := setupAddVoterJoinPathNodes(t, ctx) | ||
| t.Cleanup(func() { | ||
| shutdown(nodes) | ||
| servers.AwaitNoError(t, waitTimeout) | ||
| }) | ||
|
|
||
| waitForNodeListeners(t, ctx, nodes, waitTimeout, waitInterval) | ||
| require.Eventually(t, func() bool { | ||
| return nodes[0].raft.State() == raft.Leader | ||
| }, waitTimeout, waitInterval) | ||
|
|
||
| adminConn, err := grpc.NewClient(nodes[0].grpcAddress, grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
bootjp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| require.NoError(t, err) | ||
| t.Cleanup(func() { _ = adminConn.Close() }) | ||
| admin := raftadminpb.NewRaftAdminClient(adminConn) | ||
|
|
||
| addVotersAndAwait(t, ctx, rpcTimeout, admin, nodes, []int{1, 2}) | ||
|
|
||
| expectedCfg := expectedVoterConfig(nodes) | ||
| waitForConfigReplication(t, expectedCfg, nodes, waitTimeout, waitInterval) | ||
| waitForRaftReadiness(t, nodes, waitTimeout, waitInterval) | ||
|
|
||
| followerConn, err := grpc.NewClient(nodes[1].grpcAddress, | ||
| grpc.WithTransportCredentials(insecure.NewCredentials()), | ||
| ) | ||
bootjp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| require.NoError(t, err) | ||
| t.Cleanup(func() { _ = followerConn.Close() }) | ||
| followerRaw := pb.NewRawKVClient(followerConn) | ||
|
|
||
| leaderConn, err := grpc.NewClient(nodes[0].grpcAddress, | ||
| grpc.WithTransportCredentials(insecure.NewCredentials()), | ||
| ) | ||
bootjp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| require.NoError(t, err) | ||
| t.Cleanup(func() { _ = leaderConn.Close() }) | ||
| leaderRaw := pb.NewRawKVClient(leaderConn) | ||
bootjp marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| putAndWaitForRead(t, ctx, rpcTimeout, followerRaw, leaderRaw, []byte("addvoter-key"), []byte("ok"), waitTimeout, waitInterval) | ||
|
|
||
| // Simulate a partition-like failure by isolating node2's raft transport. | ||
| require.NoError(t, nodes[2].tm.Close()) | ||
| nodes[2].tm = nil | ||
|
|
||
| putAndWaitForRead(t, ctx, rpcTimeout, followerRaw, leaderRaw, []byte("partition-survive-key"), []byte("ok2"), waitTimeout, waitInterval) | ||
|
|
||
| // Force leader change while one node is isolated, then confirm write/read path. | ||
| require.NoError(t, nodes[0].raft.LeadershipTransferToServer(raft.ServerID("1"), raft.ServerAddress(nodes[1].raftAddress)).Error()) | ||
| require.Eventually(t, func() bool { | ||
| return nodes[1].raft.State() == raft.Leader | ||
| }, waitTimeout, waitInterval) | ||
|
|
||
| putAndWaitForRead(t, ctx, rpcTimeout, leaderRaw, followerRaw, []byte("leader-transfer-key"), []byte("ok3"), waitTimeout, waitInterval) | ||
| } | ||
|
|
||
| func setupAddVoterJoinPathNodes(t *testing.T, ctx context.Context) ([]Node, *serverWorkers) { | ||
| t.Helper() | ||
|
|
||
| ports, lis := reserveAddVoterJoinListeners(t, ctx, 3) | ||
|
|
||
| // AddVoter address must point to the node's shared gRPC endpoint where | ||
| // raft transport and adapter services are served. | ||
| require.Equal(t, ports[1].raftAddress, ports[1].grpcAddress) | ||
| require.Equal(t, ports[2].raftAddress, ports[2].grpcAddress) | ||
|
|
||
| leaderRedisMap := map[raft.ServerAddress]string{ | ||
| raft.ServerAddress(ports[0].raftAddress): ports[0].redisAddress, | ||
| raft.ServerAddress(ports[1].raftAddress): ports[1].redisAddress, | ||
| raft.ServerAddress(ports[2].raftAddress): ports[2].redisAddress, | ||
| } | ||
| bootstrapCfg := raft.Configuration{ | ||
| Servers: []raft.Server{ | ||
| { | ||
| Suffrage: raft.Voter, | ||
| ID: raft.ServerID("0"), | ||
| Address: raft.ServerAddress(ports[0].raftAddress), | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| workers := newServerWorkers(len(ports) * 3) | ||
| nodes := make([]Node, 0, len(ports)) | ||
| for i := range ports { | ||
| nodes = append(nodes, startAddVoterJoinNode(t, workers, i, ports[i], lis[i], bootstrapCfg, leaderRedisMap)) | ||
| } | ||
| return nodes, workers | ||
| } | ||
|
|
||
| func reserveAddVoterJoinListeners(t *testing.T, ctx context.Context, n int) ([]portsAdress, []listeners) { | ||
| t.Helper() | ||
|
|
||
| var lc net.ListenConfig | ||
| ports := assignPorts(n) | ||
| lis := make([]listeners, 0, n) | ||
| for i := range ports { | ||
| for { | ||
| bound, ls, retry, err := bindListeners(ctx, &lc, ports[i]) | ||
| require.NoError(t, err) | ||
| if !retry { | ||
| ports[i] = bound | ||
| lis = append(lis, ls) | ||
| break | ||
| } | ||
| ports[i] = assignPorts(1)[0] | ||
| } | ||
| } | ||
| return ports, lis | ||
| } | ||
|
|
||
| func startAddVoterJoinNode( | ||
| t *testing.T, | ||
| workers *serverWorkers, | ||
| idx int, | ||
| port portsAdress, | ||
| lis listeners, | ||
| bootstrapCfg raft.Configuration, | ||
| leaderRedisMap map[raft.ServerAddress]string, | ||
| ) Node { | ||
| t.Helper() | ||
|
|
||
| st := store.NewMVCCStore() | ||
| fsm := kv.NewKvFSM(st) | ||
|
|
||
| electionTimeout := leaderElectionTimeout | ||
| if idx != 0 { | ||
| electionTimeout = followerElectionTimeout | ||
| } | ||
|
|
||
| r, tm, err := newRaft(strconv.Itoa(idx), port.raftAddress, fsm, idx == 0, bootstrapCfg, electionTimeout) | ||
| require.NoError(t, err) | ||
|
|
||
| s := grpc.NewServer() | ||
| trx := kv.NewTransaction(r) | ||
| coordinator := kv.NewCoordinator(trx, r) | ||
| routedStore := kv.NewLeaderRoutedStore(st, coordinator) | ||
| gs := NewGRPCServer(routedStore, coordinator, WithCloseStore()) | ||
| tm.Register(s) | ||
| pb.RegisterRawKVServer(s, gs) | ||
| pb.RegisterTransactionalKVServer(s, gs) | ||
| pb.RegisterInternalServer(s, NewInternal(trx, r, coordinator.Clock())) | ||
| leaderhealth.Setup(r, s, []string{"RawKV"}) | ||
| raftadmin.Register(s, r) | ||
|
|
||
| workers.Go(func() error { | ||
| err := s.Serve(lis.grpc) | ||
| if errors.Is(err, grpc.ErrServerStopped) || errors.Is(err, net.ErrClosed) { | ||
| return nil | ||
| } | ||
| return err | ||
| }) | ||
|
|
||
| rd := NewRedisServer(lis.redis, st, coordinator, leaderRedisMap) | ||
| workers.Go(func() error { | ||
| err := rd.Run() | ||
| if errors.Is(err, net.ErrClosed) { | ||
| return nil | ||
| } | ||
| return err | ||
| }) | ||
|
|
||
| ds := NewDynamoDBServer(lis.dynamo, st, coordinator) | ||
| workers.Go(func() error { | ||
| err := ds.Run() | ||
| if errors.Is(err, net.ErrClosed) { | ||
| return nil | ||
| } | ||
| return err | ||
| }) | ||
|
|
||
| return newNode( | ||
| port.grpcAddress, | ||
| port.raftAddress, | ||
| port.redisAddress, | ||
| port.dynamoAddress, | ||
| r, | ||
| tm, | ||
| s, | ||
| gs, | ||
| rd, | ||
| ds, | ||
| ) | ||
| } | ||
|
|
||
| func addVotersAndAwait( | ||
| t *testing.T, | ||
| ctx context.Context, | ||
| rpcTimeout time.Duration, | ||
| admin raftadminpb.RaftAdminClient, | ||
| nodes []Node, | ||
| targets []int, | ||
| ) { | ||
| t.Helper() | ||
|
|
||
| for _, target := range targets { | ||
| addCtx, cancelAdd := context.WithTimeout(ctx, rpcTimeout) | ||
| future, err := admin.AddVoter(addCtx, &raftadminpb.AddVoterRequest{ | ||
| Id: strconv.Itoa(target), | ||
| Address: nodes[target].grpcAddress, | ||
| PreviousIndex: 0, | ||
| }) | ||
| cancelAdd() | ||
| require.NoError(t, err) | ||
|
|
||
| awaitCtx, cancelAwait := context.WithTimeout(ctx, rpcTimeout) | ||
| await, err := admin.Await(awaitCtx, future) | ||
| cancelAwait() | ||
| require.NoError(t, err) | ||
| require.Empty(t, await.GetError()) | ||
| require.Greater(t, await.GetIndex(), uint64(0)) | ||
| } | ||
| } | ||
|
|
||
| func expectedVoterConfig(nodes []Node) raft.Configuration { | ||
| servers := make([]raft.Server, 0, len(nodes)) | ||
| for i, n := range nodes { | ||
| servers = append(servers, raft.Server{ | ||
| Suffrage: raft.Voter, | ||
| ID: raft.ServerID(strconv.Itoa(i)), | ||
| Address: raft.ServerAddress(n.raftAddress), | ||
| }) | ||
| } | ||
| return raft.Configuration{Servers: servers} | ||
| } | ||
|
|
||
| func putAndWaitForRead( | ||
| t *testing.T, | ||
| ctx context.Context, | ||
| rpcTimeout time.Duration, | ||
| writer pb.RawKVClient, | ||
| reader pb.RawKVClient, | ||
| key []byte, | ||
| value []byte, | ||
| waitTimeout time.Duration, | ||
| waitInterval time.Duration, | ||
| ) { | ||
| t.Helper() | ||
|
|
||
| putCtx, cancelPut := context.WithTimeout(ctx, rpcTimeout) | ||
| _, err := writer.RawPut(putCtx, &pb.RawPutRequest{Key: key, Value: value}) | ||
| cancelPut() | ||
| require.NoError(t, err) | ||
|
|
||
| require.Eventually(t, func() bool { | ||
| getCtx, cancelGet := context.WithTimeout(ctx, rpcTimeout) | ||
| resp, getErr := reader.RawGet(getCtx, &pb.RawGetRequest{Key: key}) | ||
| cancelGet() | ||
| if getErr != nil { | ||
| return false | ||
| } | ||
| return resp.Exists && bytes.Equal(resp.Value, value) | ||
| }, waitTimeout, waitInterval) | ||
| } | ||
|
|
||
| type serverWorkers struct { | ||
| wg sync.WaitGroup | ||
| errCh chan error | ||
| } | ||
|
|
||
| func newServerWorkers(buffer int) *serverWorkers { | ||
| return &serverWorkers{errCh: make(chan error, buffer)} | ||
| } | ||
|
|
||
| func (w *serverWorkers) Go(run func() error) { | ||
| if w == nil || run == nil { | ||
| return | ||
| } | ||
| w.wg.Add(1) | ||
| go func() { | ||
| defer w.wg.Done() | ||
| if err := run(); err != nil { | ||
| w.errCh <- err | ||
| } | ||
| }() | ||
| } | ||
|
|
||
| func (w *serverWorkers) AwaitNoError(t *testing.T, timeout time.Duration) { | ||
| t.Helper() | ||
| if w == nil { | ||
| return | ||
| } | ||
|
|
||
| done := make(chan struct{}) | ||
| go func() { | ||
| w.wg.Wait() | ||
| close(done) | ||
| }() | ||
|
|
||
| select { | ||
| case <-done: | ||
| case <-time.After(timeout): | ||
| require.FailNow(t, "server goroutines did not finish in time") | ||
| } | ||
|
|
||
| close(w.errCh) | ||
| for err := range w.errCh { | ||
| require.NoError(t, err) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.