generated from TBD54566975/tbd-project-template
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Automatically join the raft cluster (#3895)
Changes `Join` to take a cluster control endpoint to send the add member request when joining as a new member. Changes the raft tester to support joining. Still, it seems that recovering from crashed nodes is not working properly, fixing that as a followup.
- Loading branch information
Showing
6 changed files
with
295 additions
and
136 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/binary" | ||
"fmt" | ||
"io" | ||
|
||
"github.com/block/ftl/internal/raft" | ||
) | ||
|
||
type IntStateMachine struct { | ||
sum int64 | ||
} | ||
|
||
type IntEvent int64 | ||
|
||
func (i *IntEvent) UnmarshalBinary(data []byte) error { //nolint:unparam | ||
*i = IntEvent(binary.BigEndian.Uint64(data)) | ||
return nil | ||
} | ||
|
||
func (i IntEvent) MarshalBinary() ([]byte, error) { //nolint:unparam | ||
return binary.BigEndian.AppendUint64([]byte{}, uint64(i)), nil | ||
} | ||
|
||
var _ raft.StateMachine[int64, int64, IntEvent, *IntEvent] = &IntStateMachine{} | ||
|
||
func (s IntStateMachine) Lookup(key int64) (int64, error) { | ||
return s.sum, nil | ||
} | ||
|
||
func (s *IntStateMachine) Update(msg IntEvent) error { | ||
s.sum += int64(msg) | ||
return nil | ||
} | ||
|
||
func (s IntStateMachine) Close() error { | ||
return nil | ||
} | ||
|
||
func (s IntStateMachine) Recover(reader io.Reader) error { | ||
err := binary.Read(reader, binary.BigEndian, &s.sum) | ||
if err != nil { | ||
return fmt.Errorf("failed to recover from snapshot: %w", err) | ||
} | ||
return nil | ||
} | ||
|
||
func (s IntStateMachine) Save(writer io.Writer) error { | ||
err := binary.Write(writer, binary.BigEndian, s.sum) | ||
if err != nil { | ||
return fmt.Errorf("failed to save snapshot: %w", err) | ||
} | ||
return nil | ||
} |
Oops, something went wrong.