|
| 1 | +// Copyright (C) MongoDB, Inc. 2017-present. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | +// not use this file except in compliance with the License. You may obtain |
| 5 | +// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | + |
| 7 | +package mongo |
| 8 | + |
| 9 | +import ( |
| 10 | + "context" |
| 11 | + "os" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/stretchr/testify/require" |
| 15 | + "go.mongodb.org/mongo-driver/bson" |
| 16 | + "go.mongodb.org/mongo-driver/event" |
| 17 | + "go.mongodb.org/mongo-driver/internal/testutil" |
| 18 | + "go.mongodb.org/mongo-driver/mongo/options" |
| 19 | + "go.mongodb.org/mongo-driver/mongo/readpref" |
| 20 | + "go.mongodb.org/mongo-driver/mongo/writeconcern" |
| 21 | +) |
| 22 | + |
| 23 | +type serverStatus struct { |
| 24 | + Host string |
| 25 | + Connections struct { |
| 26 | + TotalCreated int32 `bson:"totalCreated"` |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +var poolChan = make(chan *event.PoolEvent, 100) |
| 31 | + |
| 32 | +var poolMonitor = event.PoolMonitor{func(event *event.PoolEvent) { poolChan <- event }} |
| 33 | + |
| 34 | +func isPoolCleared() bool { |
| 35 | + for len(poolChan) > 0 { |
| 36 | + curr := <-poolChan |
| 37 | + if curr.Type == event.PoolCleared { |
| 38 | + return true |
| 39 | + } |
| 40 | + } |
| 41 | + return false |
| 42 | +} |
| 43 | + |
| 44 | +func TestConnectionsSurvivePrimaryStepDown(t *testing.T) { |
| 45 | + if os.Getenv("TOPOLOGY") != "replica_set" { |
| 46 | + t.Skip("Needs to run on a replica set") |
| 47 | + } |
| 48 | + |
| 49 | + ctx := context.Background() |
| 50 | + mongodbURI := testutil.ConnString(t) |
| 51 | + client, err := Connect(ctx, options.Client().ApplyURI(mongodbURI.String()).SetRetryWrites(false). |
| 52 | + SetPoolMonitor(&poolMonitor)) |
| 53 | + require.NoError(t, err) |
| 54 | + db := client.Database("step-down", options.Database().SetWriteConcern(writeconcern.New(writeconcern.WMajority()))) |
| 55 | + collName := "step-down" |
| 56 | + err = db.Collection(collName).Drop(ctx) |
| 57 | + require.NoError(t, err) |
| 58 | + |
| 59 | + err = db.RunCommand( |
| 60 | + context.Background(), |
| 61 | + bson.D{{"create", collName}}, |
| 62 | + ).Err() |
| 63 | + require.NoError(t, err) |
| 64 | + coll := db.Collection(collName) |
| 65 | + |
| 66 | + serverVersion, err := getServerVersion(db) |
| 67 | + require.NoError(t, err) |
| 68 | + adminDB := client.Database("admin") |
| 69 | + |
| 70 | + for len(poolChan) > 0 { |
| 71 | + <-poolChan |
| 72 | + } |
| 73 | + |
| 74 | + t.Run("getMore_iteration", func(t *testing.T) { |
| 75 | + if compareVersions(t, serverVersion, "4.2") < 0 { |
| 76 | + t.Skip("Needs server version >= 4.2") |
| 77 | + } |
| 78 | + initCollection(t, coll) |
| 79 | + cur, err := coll.Find(ctx, bson.D{}, options.Find().SetBatchSize(2)) |
| 80 | + ok := cur.Next(ctx) |
| 81 | + require.True(t, ok) |
| 82 | + |
| 83 | + err = adminDB.RunCommand( |
| 84 | + context.Background(), |
| 85 | + bson.D{{"replSetStepDown", 5}, {"force", true}}, |
| 86 | + options.RunCmd().SetReadPreference(readpref.Primary()), |
| 87 | + ).Err() |
| 88 | + require.NoError(t, err) |
| 89 | + |
| 90 | + ok = cur.Next(ctx) |
| 91 | + require.True(t, ok) |
| 92 | + |
| 93 | + require.False(t, isPoolCleared()) |
| 94 | + }) |
| 95 | + t.Run("notMaster_keep_pool", func(t *testing.T) { |
| 96 | + if compareVersions(t, serverVersion, "4.2") < 0 { |
| 97 | + t.Skip("Needs server version >= 4.2") |
| 98 | + } |
| 99 | + |
| 100 | + err = adminDB.RunCommand( |
| 101 | + ctx, |
| 102 | + bson.D{{"configureFailPoint", "failCommand"}, |
| 103 | + {"mode", bson.D{{"times", 1}}}, |
| 104 | + {"data", bson.D{{"failCommands", bson.A{"insert"}}, {"errorCode", 10107}}}}, |
| 105 | + ).Err() |
| 106 | + require.NoError(t, err) |
| 107 | + defer func() { |
| 108 | + require.NoError(t, adminDB.RunCommand(ctx, bson.D{ |
| 109 | + {"configureFailPoint", "failCommand"}, |
| 110 | + {"mode", "off"}, |
| 111 | + }).Err()) |
| 112 | + }() |
| 113 | + |
| 114 | + _, err = coll.InsertOne(ctx, bson.D{{"test", 1}}) |
| 115 | + require.Error(t, err) |
| 116 | + |
| 117 | + cerr, ok := err.(CommandError) |
| 118 | + require.True(t, ok) |
| 119 | + require.Equal(t, int32(10107), cerr.Code) |
| 120 | + |
| 121 | + _, err = coll.InsertOne(ctx, bson.D{{"test", 1}}) |
| 122 | + require.NoError(t, err) |
| 123 | + |
| 124 | + require.False(t, isPoolCleared()) |
| 125 | + }) |
| 126 | + t.Run("notMaster_reset_pool", func(t *testing.T) { |
| 127 | + if compareVersions(t, serverVersion, "4.0") != 0 { |
| 128 | + t.Skip("Needs server version 4.0") |
| 129 | + } |
| 130 | + |
| 131 | + err = adminDB.RunCommand( |
| 132 | + ctx, |
| 133 | + bson.D{{"configureFailPoint", "failCommand"}, |
| 134 | + {"mode", bson.D{{"times", 1}}}, |
| 135 | + {"data", bson.D{{"failCommands", bson.A{"insert"}}, {"errorCode", 10107}}}}, |
| 136 | + ).Err() |
| 137 | + require.NoError(t, err) |
| 138 | + defer func() { |
| 139 | + require.NoError(t, adminDB.RunCommand(ctx, bson.D{ |
| 140 | + {"configureFailPoint", "failCommand"}, |
| 141 | + {"mode", "off"}, |
| 142 | + }).Err()) |
| 143 | + }() |
| 144 | + _, err = coll.InsertOne(ctx, bson.D{{"test", 1}}) |
| 145 | + require.Error(t, err) |
| 146 | + |
| 147 | + cerr, ok := err.(CommandError) |
| 148 | + require.True(t, ok) |
| 149 | + require.Equal(t, int32(10107), cerr.Code) |
| 150 | + |
| 151 | + require.True(t, isPoolCleared()) |
| 152 | + }) |
| 153 | + t.Run("shutdownInProgress_reset_pool", func(t *testing.T) { |
| 154 | + if compareVersions(t, serverVersion, "4.0") < 0 { |
| 155 | + t.Skip("Needs server version >= 4.0") |
| 156 | + } |
| 157 | + |
| 158 | + err = adminDB.RunCommand( |
| 159 | + ctx, |
| 160 | + bson.D{{"configureFailPoint", "failCommand"}, |
| 161 | + {"mode", bson.D{{"times", 1}}}, |
| 162 | + {"data", bson.D{{"failCommands", bson.A{"insert"}}, {"errorCode", 91}}}}, |
| 163 | + ).Err() |
| 164 | + require.NoError(t, err) |
| 165 | + defer func() { |
| 166 | + require.NoError(t, adminDB.RunCommand(ctx, bson.D{ |
| 167 | + {"configureFailPoint", "failCommand"}, |
| 168 | + {"mode", "off"}, |
| 169 | + }).Err()) |
| 170 | + }() |
| 171 | + |
| 172 | + _, err = coll.InsertOne(ctx, bson.D{{"test", 1}}) |
| 173 | + require.Error(t, err) |
| 174 | + |
| 175 | + cerr, ok := err.(CommandError) |
| 176 | + require.True(t, ok) |
| 177 | + require.Equal(t, int32(91), cerr.Code) |
| 178 | + |
| 179 | + require.True(t, isPoolCleared()) |
| 180 | + }) |
| 181 | + t.Run("interruptedAtShutdown_reset_pool", func(t *testing.T) { |
| 182 | + if compareVersions(t, serverVersion, "4.0") < 0 { |
| 183 | + t.Skip("Needs server version >= 4.0") |
| 184 | + } |
| 185 | + |
| 186 | + err = adminDB.RunCommand( |
| 187 | + ctx, |
| 188 | + bson.D{{"configureFailPoint", "failCommand"}, |
| 189 | + {"mode", bson.D{{"times", 1}}}, |
| 190 | + {"data", bson.D{{"failCommands", bson.A{"insert"}}, {"errorCode", 11600}}}}, |
| 191 | + ).Err() |
| 192 | + require.NoError(t, err) |
| 193 | + defer func() { |
| 194 | + require.NoError(t, adminDB.RunCommand(ctx, bson.D{ |
| 195 | + {"configureFailPoint", "failCommand"}, |
| 196 | + {"mode", "off"}, |
| 197 | + }).Err()) |
| 198 | + }() |
| 199 | + |
| 200 | + _, err = coll.InsertOne(ctx, bson.D{{"test", 1}}) |
| 201 | + require.Error(t, err) |
| 202 | + |
| 203 | + cerr, ok := err.(CommandError) |
| 204 | + require.True(t, ok) |
| 205 | + require.Equal(t, int32(11600), cerr.Code) |
| 206 | + |
| 207 | + require.True(t, isPoolCleared()) |
| 208 | + }) |
| 209 | +} |
0 commit comments