package weed_server import ( "encoding/json" "testing" hashicorpRaft "github.com/hashicorp/raft" "github.com/seaweedfs/seaweedfs/weed/sequence" "github.com/seaweedfs/seaweedfs/weed/topology" ) // The file ids recorded as handed out have to outlive the master that // recorded them: a master that recovers the raft state, from a snapshot or // from the log, starts its sequence past them. func TestRaftStateKeepsTheFileIdCeiling(t *testing.T) { newTopology := func() *topology.Topology { return topology.NewTopology("test", sequence.NewMemorySequencer(), 32*1024, 5, false) } recorded := newTopology() recorded.RaiseFileIdCeiling(5000, "") snapshot, err := StateMachine{topo: recorded}.Save() if err != nil { t.Fatalf("save the raft state: %v", err) } fromSnapshot := newTopology() if err := (StateMachine{topo: fromSnapshot}).Recovery(snapshot); err != nil { t.Fatalf("recover the raft state: %v", err) } if got := fromSnapshot.FileIdCeiling(); got != 5000 { t.Fatalf("ceiling %d recovered from the snapshot, want 5000", got) } if id := fromSnapshot.Sequence.NextFileId(1); id != 5000 { t.Fatalf("the recovered master hands out file id %d first, want 5000: the ids below may be in use", id) } fromLog := newTopology() entry, _ := json.Marshal(&topology.MaxVolumeIdCommand{FileIdCeiling: 7000}) if res := (&StateMachine{topo: fromLog}).Apply(&hashicorpRaft.Log{Data: entry}); res != nil { t.Fatalf("apply the log entry: %v", res) } if id := fromLog.Sequence.NextFileId(1); id != 7000 { t.Fatalf("a master that replayed the log hands out file id %d first, want 7000", id) } // an entry written before the ceiling existed changes nothing old, _ := json.Marshal(&topology.MaxVolumeIdCommand{MaxVolumeId: 3}) (&StateMachine{topo: fromLog}).Apply(&hashicorpRaft.Log{Data: old}) if got := fromLog.FileIdCeiling(); got != 7000 { t.Fatalf("ceiling %d after an entry without one, want 7000", got) } }