Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
anishathalye committed Apr 6, 2017
1 parent a1013cb commit 3de6e85
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/kvraft/config.go
Expand Up @@ -67,6 +67,18 @@ func (cfg *config) LogSize() int {
return logsize
}

// Maximum snapshot size across all servers
func (cfg *config) SnapshotSize() int {
snapshotsize := 0
for i := 0; i < cfg.n; i++ {
n := cfg.saved[i].SnapshotSize()
if n > snapshotsize {
snapshotsize = n
}
}
return snapshotsize
}

// attach server i to servers listed in to
// caller must hold cfg.mu
func (cfg *config) connectUnlocked(i int, to []int) {
Expand Down
33 changes: 33 additions & 0 deletions src/kvraft/test_test.go
Expand Up @@ -453,6 +453,39 @@ func TestSnapshotRPC(t *testing.T) {
fmt.Printf(" ... Passed\n")
}

// are the snapshots not too huge? 500 bytes is a generous bound for the
// operations we're doing here.
func TestSnapshotSize(t *testing.T) {
const nservers = 3
maxraftstate := 1000
maxsnapshotstate := 500
cfg := make_config(t, "snapshotsize", nservers, false, maxraftstate)
defer cfg.cleanup()

ck := cfg.makeClient(cfg.All())

fmt.Printf("Test: snapshot size is reasonable ...\n")

for i := 0; i < 200; i++ {
ck.Put("x", "0")
check(t, ck, "x", "0")
ck.Put("x", "1")
check(t, ck, "x", "1")
}

// check that servers have thrown away most of their log entries
if cfg.LogSize() > 2*maxraftstate {
t.Fatalf("logs were not trimmed (%v > 2*%v)", cfg.LogSize(), maxraftstate)
}

// check that the snapshots are not unreasonably large
if cfg.SnapshotSize() > maxsnapshotstate {
t.Fatalf("snapshot too large (%v > %v)", cfg.SnapshotSize(), maxsnapshotstate)
}

fmt.Printf(" ... Passed\n")
}

func TestSnapshotRecover(t *testing.T) {
fmt.Printf("Test: persistence with one client and snapshots ...\n")
GenericTest(t, "snapshot", 1, false, true, false, 1000)
Expand Down
6 changes: 6 additions & 0 deletions src/raft/persister.go
Expand Up @@ -59,3 +59,9 @@ func (ps *Persister) ReadSnapshot() []byte {
defer ps.mu.Unlock()
return ps.snapshot
}

func (ps *Persister) SnapshotSize() int {
ps.mu.Lock()
defer ps.mu.Unlock()
return len(ps.snapshot)
}

0 comments on commit 3de6e85

Please sign in to comment.