@@ -4381,6 +4381,101 @@ func TestShardWithoutQuorumCanBeRestoreByImportingSnapshot(t *testing.T) {
43814381 runNodeHostTestDC (t , tf , true , fs )
43824382}
43834383
4384+ func TestSnapshotMembershipCanBeImported (t * testing.T ) {
4385+ const target uint64 = 1
4386+ addr := "localhost:26001"
4387+ voting := func () pb.Membership {
4388+ return pb.Membership {Addresses : map [uint64 ]string {target : addr }}
4389+ }
4390+ nonVoting := func () pb.Membership {
4391+ return pb.Membership {NonVotings : map [uint64 ]string {target : addr }}
4392+ }
4393+ witness := func () pb.Membership {
4394+ return pb.Membership {Witnesses : map [uint64 ]string {target : addr }}
4395+ }
4396+ removed := func () pb.Membership {
4397+ return pb.Membership {
4398+ Addresses : map [uint64 ]string {target : addr },
4399+ Removed : map [uint64 ]bool {target : false },
4400+ }
4401+ }
4402+ duplicated := func () pb.Membership {
4403+ return pb.Membership {
4404+ Addresses : map [uint64 ]string {target : addr },
4405+ NonVotings : map [uint64 ]string {target : addr },
4406+ }
4407+ }
4408+ tests := []struct {
4409+ name string
4410+ imported pb.Membership
4411+ current pb.Membership
4412+ ok bool
4413+ }{
4414+ {
4415+ name : "voting imports voting" ,
4416+ imported : voting (),
4417+ current : voting (),
4418+ ok : true ,
4419+ },
4420+ {
4421+ name : "voting rejects non-voting" ,
4422+ imported : nonVoting (),
4423+ current : voting (),
4424+ },
4425+ {
4426+ name : "voting rejects witness" ,
4427+ imported : witness (),
4428+ current : voting (),
4429+ },
4430+ {
4431+ name : "non-voting imports non-voting" ,
4432+ imported : nonVoting (),
4433+ current : nonVoting (),
4434+ ok : true ,
4435+ },
4436+ {
4437+ name : "non-voting imports promoted voting" ,
4438+ imported : voting (),
4439+ current : nonVoting (),
4440+ ok : true ,
4441+ },
4442+ {
4443+ name : "witness imports witness" ,
4444+ imported : witness (),
4445+ current : witness (),
4446+ ok : true ,
4447+ },
4448+ {
4449+ name : "witness rejects voting" ,
4450+ imported : voting (),
4451+ current : witness (),
4452+ },
4453+ {
4454+ name : "removed target rejected" ,
4455+ imported : removed (),
4456+ current : voting (),
4457+ },
4458+ {
4459+ name : "duplicated target role rejected" ,
4460+ imported : duplicated (),
4461+ current : voting (),
4462+ },
4463+ {
4464+ name : "missing target rejected" ,
4465+ imported : pb.Membership {Addresses : map [uint64 ]string {2 : "localhost:26002" }},
4466+ current : voting (),
4467+ },
4468+ }
4469+ for _ , tt := range tests {
4470+ t .Run (tt .name , func (t * testing.T ) {
4471+ if got := snapshotMembershipCanBeImported (
4472+ tt .imported , tt .current , target ); got != tt .ok {
4473+ t .Fatalf ("got %t, want %t" , got , tt .ok )
4474+ }
4475+ })
4476+ }
4477+ }
4478+
43844479func TestRequestImportSnapshotAndQueryLog (t * testing.T ) {
43854480 if vfs .GetTestFS () != vfs .DefaultFS {
43864481 t .Skip ("not using the default fs" )
@@ -4474,11 +4569,85 @@ func TestRequestImportSnapshotAndQueryLog(t *testing.T) {
44744569 }
44754570 snapshotDir := fmt .Sprintf ("snapshot-%016X" , index )
44764571 dir := fs .PathJoin (sspath , snapshotDir )
4572+ srcSnapshot , err := tools .GetSnapshotRecord (dir , server .MetadataFilename , fs )
4573+ if err != nil {
4574+ t .Fatalf ("failed to get source snapshot record %v" , err )
4575+ }
4576+ assertInvalidSnapshot := func (ss pb.Snapshot ) {
4577+ t .Helper ()
4578+ if err := fileutil .CreateFlagFile (dir , server .MetadataFilename ,
4579+ & ss , fs ); err != nil {
4580+ t .Fatalf ("failed to update source snapshot record %v" , err )
4581+ }
4582+ ctx , cancel = context .WithTimeout (context .Background (), 3 * time .Second )
4583+ err = nh .SyncRequestImportSnapshot (ctx , 1 , 1 , dir )
4584+ cancel ()
4585+ if ! errors .Is (err , tools .ErrInvalidMembers ) {
4586+ t .Fatalf ("got %v, want %v" , err , tools .ErrInvalidMembers )
4587+ }
4588+ }
4589+ missingReplica := srcSnapshot
4590+ missingReplica .Membership = pb.Membership {
4591+ ConfigChangeId : srcSnapshot .Membership .ConfigChangeId ,
4592+ Addresses : map [uint64 ]string {2 : "noidea:8080" },
4593+ }
4594+ assertInvalidSnapshot (missingReplica )
4595+ targetAddr , ok := srcSnapshot .Membership .Addresses [1 ]
4596+ if ! ok {
4597+ t .Fatalf ("source snapshot does not contain target replica" )
4598+ }
4599+ mismatchedReplica := srcSnapshot
4600+ mismatchedReplica .Membership = pb.Membership {
4601+ ConfigChangeId : srcSnapshot .Membership .ConfigChangeId ,
4602+ Addresses : map [uint64 ]string {2 : "noidea:8080" },
4603+ }
4604+ if err := fileutil .CreateFlagFile (dir , server .MetadataFilename ,
4605+ & mismatchedReplica , fs ); err != nil {
4606+ t .Fatalf ("failed to update source snapshot record %v" , err )
4607+ }
4608+ ctx , cancel = context .WithTimeout (context .Background (), 3 * time .Second )
4609+ err = nh .SyncRequestImportSnapshot (ctx , 1 , 2 , dir )
4610+ cancel ()
4611+ if ! errors .Is (err , ErrShardNotFound ) {
4612+ t .Fatalf ("got %v, want %v" , err , ErrShardNotFound )
4613+ }
4614+ removedReplica := srcSnapshot
4615+ removedReplica .Membership = pb.Membership {
4616+ ConfigChangeId : srcSnapshot .Membership .ConfigChangeId ,
4617+ Addresses : map [uint64 ]string {1 : targetAddr },
4618+ Removed : map [uint64 ]bool {1 : false },
4619+ }
4620+ assertInvalidSnapshot (removedReplica )
4621+ nonVotingReplica := srcSnapshot
4622+ nonVotingReplica .Membership = pb.Membership {
4623+ ConfigChangeId : srcSnapshot .Membership .ConfigChangeId ,
4624+ Addresses : map [uint64 ]string {2 : "noidea:8080" },
4625+ NonVotings : map [uint64 ]string {1 : targetAddr },
4626+ }
4627+ assertInvalidSnapshot (nonVotingReplica )
4628+ witnessReplica := srcSnapshot
4629+ witnessReplica .Membership = pb.Membership {
4630+ ConfigChangeId : srcSnapshot .Membership .ConfigChangeId ,
4631+ Addresses : map [uint64 ]string {2 : "noidea:8080" },
4632+ Witnesses : map [uint64 ]string {1 : targetAddr },
4633+ }
4634+ assertInvalidSnapshot (witnessReplica )
4635+ if err := fileutil .CreateFlagFile (dir , server .MetadataFilename ,
4636+ & srcSnapshot , fs ); err != nil {
4637+ t .Fatalf ("failed to restore source snapshot record %v" , err )
4638+ }
44774639 ctx , cancel = context .WithTimeout (context .Background (), 3 * time .Second )
44784640 if err := nh .SyncRequestImportSnapshot (ctx , 1 , 1 , dir ); err != nil {
44794641 t .Fatalf ("failed to import snapshot %v" , err )
44804642 }
44814643 cancel ()
4644+ logReader , err := nh .GetLogReader (1 )
4645+ if err != nil {
4646+ t .Fatalf ("failed to get log reader %v" , err )
4647+ }
4648+ imported := logReader .Snapshot ()
4649+ assert .Equal (t , srcSnapshot .Membership , imported .Membership )
4650+ assert .NotEqual (t , index , imported .Membership .ConfigChangeId )
44824651
44834652 // SyncRequestImportSnapshot imports the snapshot synchronously, but compact log entries
44844653 // asynchronously, this is ok for application because we can accept less compaction.
0 commit comments