-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Flesh out replica/workload to test with Antithesis
- Loading branch information
Showing
7 changed files
with
180 additions
and
96 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
toy-raft/cmd/replica/replica | ||
toy-raft/cmd/workload/workload |
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 |
---|---|---|
@@ -1,76 +1,65 @@ | ||
package main | ||
|
||
import ( | ||
"errors" | ||
"flag" | ||
"fmt" | ||
"log" | ||
"os" | ||
"slices" | ||
"strings" | ||
"time" | ||
|
||
"github.com/nats-io/nats.go" | ||
|
||
"toy-raft/network" | ||
"toy-raft/raft" | ||
"toy-raft/server" | ||
"toy-raft/state" | ||
|
||
"github.com/nats-io/nats.go" | ||
) | ||
|
||
const n = 20 | ||
|
||
func main() { | ||
var ( | ||
replicaId string | ||
groupId string | ||
natsUrl string | ||
peerString string | ||
) | ||
flag.StringVar(&replicaId, "replicaId", "", "unique id of replica") | ||
flag.StringVar(&groupId, "groupId", "", "raft group id") | ||
flag.StringVar(&natsUrl, "natsUrl", "", "nats url") | ||
flag.StringVar(&replicaId, "replica-id", "", "unique id of replica") | ||
flag.StringVar(&groupId, "group-id", "", "raft group id") | ||
flag.StringVar(&natsUrl, "nats-url", nats.DefaultURL, "nats url") | ||
flag.StringVar(&peerString, "peers", "", "comma separated list of peer ids (including self)") | ||
flag.Parse() | ||
|
||
fatalErr := func(err error) { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
if replicaId == "" { | ||
panic("requires a valid replica id") | ||
fatalErr(fmt.Errorf("missing required argument: replica-id")) | ||
} | ||
|
||
if groupId == "" { | ||
panic("requires a valid raft group id") | ||
fatalErr(fmt.Errorf("missing required argument: group-id")) | ||
} | ||
|
||
if peerString == "" { | ||
panic("requires a valid peer list") | ||
fatalErr(fmt.Errorf("missing required argument: peers")) | ||
} | ||
peers := strings.Split(peerString, ",") | ||
|
||
if natsUrl == "" { | ||
log.Printf("no natsUrl was provided, using default: %s", nats.DefaultURL) | ||
natsUrl = nats.DefaultURL | ||
if !slices.Contains(peers, replicaId) { | ||
fatalErr(fmt.Errorf("list of peers does not include this replica")) | ||
} | ||
|
||
network := network.NewNatsNetwork(groupId, natsUrl) | ||
|
||
sm := state.NewKeepLastBlocksStateMachine(replicaId, n) | ||
raftNode := raft.NewRaftNodeImpl(replicaId, sm, raft.NewInMemoryStorage(), network, peers) | ||
network.RegisterNode(replicaId, raftNode) | ||
server := server.NewServer(replicaId, raftNode, sm) | ||
server.Start() | ||
|
||
proposalCount := 0 | ||
natsNetwork, err := network.NewNatsNetwork(groupId, natsUrl) | ||
if err != nil { | ||
fatalErr(fmt.Errorf("failed to initialize network: %w", err)) | ||
} | ||
|
||
for { | ||
select { | ||
case <-time.After(1 * time.Second): | ||
if err := server.Propose([]byte{}); err != nil { | ||
if errors.Is(err, raft.ErrNotLeader) { | ||
continue | ||
} | ||
panic(err) | ||
} | ||
proposalCount++ | ||
sm := state.NewKeepLastBlocksStateMachine(replicaId, 10) | ||
raftNode := raft.NewRaftNodeImpl(replicaId, sm, raft.NewInMemoryStorage(), natsNetwork, peers) | ||
natsNetwork.RegisterNode(replicaId, raftNode) | ||
srv := server.NewServer(replicaId, raftNode, sm) | ||
srv.Start() | ||
|
||
case <-time.After(10 * time.Second): | ||
fmt.Printf("proposalCount: %d\n", proposalCount) | ||
} | ||
} | ||
// Block forever | ||
select {} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,51 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"os" | ||
"time" | ||
|
||
"github.com/antithesishq/antithesis-sdk-go/lifecycle" | ||
"github.com/nats-io/nats.go" | ||
) | ||
|
||
func main() { | ||
var ( | ||
groupId string | ||
natsUrl string | ||
) | ||
flag.StringVar(&groupId, "group-id", "", "raft group id") | ||
flag.StringVar(&natsUrl, "nats-url", nats.DefaultURL, "nats url") | ||
flag.Parse() | ||
|
||
fatalErr := func(err error) { | ||
fmt.Println(err) | ||
os.Exit(1) | ||
} | ||
|
||
if groupId == "" { | ||
fatalErr(fmt.Errorf("missing required argument: group-id")) | ||
} | ||
|
||
nc, err := nats.Connect(natsUrl) | ||
if err != nil { | ||
fatalErr(fmt.Errorf("failed to connect: %w", err)) | ||
} | ||
defer nc.Close() | ||
|
||
// TODO wait for RAFT group to be established -- not sure how. | ||
// For now, do the dumb thing: | ||
time.Sleep(3 * time.Second) | ||
|
||
// If running in antithesis, signal setup is complete | ||
lifecycle.SetupComplete(nil) | ||
|
||
// Block forever | ||
for { | ||
select { | ||
case <-time.After(5 * time.Second): | ||
fmt.Printf("Idle...\n") | ||
} | ||
} | ||
} |
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
Oops, something went wrong.