Skip to content

Commit 4efda75

Browse files
committed
CSHARP-1160: Enforce the distinction between Standalone and Direct.
1 parent 0fb4a83 commit 4efda75

File tree

4 files changed

+101
-74
lines changed

4 files changed

+101
-74
lines changed

src/MongoDB.Driver.Core/Core/Clusters/ClusterFactory.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,21 +50,22 @@ public ICluster CreateCluster()
5050
{
5151
connectionMode = ClusterConnectionMode.ReplicaSet;
5252
}
53-
else if (_settings.EndPoints.Count == 1)
54-
{
55-
connectionMode = ClusterConnectionMode.Direct;
56-
}
5753
}
5854

5955
var settings = _settings.With(connectionMode: connectionMode);
6056

6157
switch (connectionMode)
6258
{
59+
case ClusterConnectionMode.Automatic:
60+
return settings.EndPoints.Count == 1 ? (ICluster)CreateSingleServerCluster(settings) : CreateMultiServerCluster(settings);
6361
case ClusterConnectionMode.Direct:
6462
case ClusterConnectionMode.Standalone:
6563
return CreateSingleServerCluster(settings);
66-
default:
64+
case ClusterConnectionMode.ReplicaSet:
65+
case ClusterConnectionMode.Sharded:
6766
return CreateMultiServerCluster(settings);
67+
default:
68+
throw new MongoInternalException(string.Format("Invalid connection mode: {0}.", connectionMode));
6869
}
6970
}
7071

src/MongoDB.Driver.Core/Core/Clusters/MultiServerCluster.cs

Lines changed: 49 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,31 @@ public override void Initialize()
132132
}
133133
}
134134

135+
private bool IsServerValidForCluster(ClusterType clusterType, ClusterConnectionMode connectionMode, ServerType serverType)
136+
{
137+
switch (clusterType)
138+
{
139+
case ClusterType.ReplicaSet:
140+
return serverType.IsReplicaSetMember();
141+
142+
case ClusterType.Sharded:
143+
return serverType == ServerType.ShardRouter;
144+
145+
case ClusterType.Unknown:
146+
switch (connectionMode)
147+
{
148+
case ClusterConnectionMode.Automatic:
149+
return serverType.IsReplicaSetMember() || serverType == ServerType.ShardRouter;
150+
151+
default:
152+
throw new MongoInternalException("Unexpected connection mode.");
153+
}
154+
155+
default:
156+
throw new MongoInternalException("Unexpected cluster type.");
157+
}
158+
}
159+
135160
protected override void RequestHeartbeat()
136161
{
137162
List<IClusterableServer> servers;
@@ -173,45 +198,44 @@ private void ServerDescriptionChangedHandler(object sender, ServerDescriptionCha
173198

174199
private void ProcessServerDescriptionChanged(ServerDescriptionChangedEventArgs args)
175200
{
176-
var currentClusterDescription = Description;
177-
var currentServerDescription = args.OldServerDescription;
178201
var newServerDescription = args.NewServerDescription;
202+
var newClusterDescription = Description;
179203

180-
var currentServer = _servers.SingleOrDefault(x => EndPointHelper.Equals(x.EndPoint, newServerDescription.EndPoint));
181-
if (currentServer == null)
204+
if (!_servers.Any(x => EndPointHelper.Equals(x.EndPoint, newServerDescription.EndPoint)))
182205
{
183206
return;
184207
}
185208

186-
ClusterDescription newClusterDescription;
187209
if (newServerDescription.State == ServerState.Disconnected)
188210
{
189-
newClusterDescription = currentClusterDescription.WithServerDescription(args.NewServerDescription);
190-
}
191-
else if (newServerDescription.Type == ServerType.Standalone)
192-
{
193-
newClusterDescription = currentClusterDescription.WithoutServerDescription(args.NewServerDescription.EndPoint);
211+
newClusterDescription = newClusterDescription.WithServerDescription(newServerDescription);
194212
}
195213
else
196214
{
197-
if (currentClusterDescription.Type == ClusterType.Unknown)
215+
if (IsServerValidForCluster(newClusterDescription.Type, Settings.ConnectionMode, newServerDescription.Type))
198216
{
199-
currentClusterDescription = currentClusterDescription.WithType(args.NewServerDescription.Type.ToClusterType());
200-
}
217+
if (newClusterDescription.Type == ClusterType.Unknown)
218+
{
219+
newClusterDescription = newClusterDescription.WithType(newServerDescription.Type.ToClusterType());
220+
}
201221

202-
switch (currentClusterDescription.Type)
222+
switch (newClusterDescription.Type)
223+
{
224+
case ClusterType.ReplicaSet:
225+
newClusterDescription = ProcessReplicaSetChange(newClusterDescription, args);
226+
break;
227+
228+
case ClusterType.Sharded:
229+
newClusterDescription = ProcessShardedChange(newClusterDescription, args);
230+
break;
231+
232+
default:
233+
throw new MongoInternalException("Unexpected cluster type.");
234+
}
235+
}
236+
else
203237
{
204-
case ClusterType.ReplicaSet:
205-
newClusterDescription = ProcessReplicaSetChange(currentClusterDescription, args);
206-
break;
207-
case ClusterType.Sharded:
208-
newClusterDescription = ProcessShardedChange(currentClusterDescription, args);
209-
break;
210-
case ClusterType.Standalone:
211-
throw new MongoInternalException("MultiServerCluster does not support a standalone state.");
212-
default:
213-
newClusterDescription = currentClusterDescription.WithServerDescription(newServerDescription);
214-
break;
238+
newClusterDescription = newClusterDescription.WithoutServerDescription(newServerDescription.EndPoint);
215239
}
216240
}
217241

src/MongoDB.Driver.Core/Core/Clusters/SingleServerCluster.cs

Lines changed: 42 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,6 @@ internal SingleServerCluster(ClusterSettings settings, IClusterableServerFactory
4242
}
4343

4444
// methods
45-
private ClusterType DetermineClusterType(ServerDescription serverDescription)
46-
{
47-
switch (serverDescription.Type)
48-
{
49-
case ServerType.ReplicaSetArbiter:
50-
case ServerType.ReplicaSetGhost:
51-
case ServerType.ReplicaSetOther:
52-
case ServerType.ReplicaSetPassive:
53-
case ServerType.ReplicaSetPrimary:
54-
case ServerType.ReplicaSetSecondary:
55-
return ClusterType.ReplicaSet;
56-
57-
case ServerType.ShardRouter:
58-
return ClusterType.Sharded;
59-
60-
case ServerType.Standalone:
61-
return ClusterType.Standalone;
62-
63-
case ServerType.Unknown:
64-
return ClusterType.Unknown;
65-
66-
default:
67-
throw new MongoException("Unexpected ServerTypes.");
68-
}
69-
}
70-
7145
protected override void Dispose(bool disposing)
7246
{
7347
if (_state.TryChange(State.Disposed))
@@ -121,40 +95,65 @@ public override void Initialize()
12195
}
12296
}
12397

98+
private bool IsServerValidForCluster(ClusterType clusterType, ClusterConnectionMode connectionMode, ServerType serverType)
99+
{
100+
switch (clusterType)
101+
{
102+
case ClusterType.ReplicaSet:
103+
return serverType.IsReplicaSetMember();
104+
105+
case ClusterType.Sharded:
106+
return serverType == ServerType.ShardRouter;
107+
108+
case ClusterType.Standalone:
109+
return serverType == ServerType.Standalone;
110+
111+
case ClusterType.Unknown:
112+
switch (connectionMode)
113+
{
114+
case ClusterConnectionMode.Automatic:
115+
return serverType == ServerType.Standalone || serverType == ServerType.ShardRouter;
116+
117+
case ClusterConnectionMode.Direct:
118+
return true;
119+
120+
default:
121+
throw new MongoInternalException("Unexpected connection mode.");
122+
}
123+
124+
default:
125+
throw new MongoInternalException("Unexpected cluster type.");
126+
}
127+
}
128+
124129
protected override void RequestHeartbeat()
125130
{
126131
_server.RequestHeartbeat();
127132
}
128133

129134
private void ServerDescriptionChanged(object sender, ServerDescriptionChangedEventArgs args)
130135
{
131-
var oldClusterDescription = Description;
132-
ClusterDescription newClusterDescription = oldClusterDescription;
133-
134136
var newServerDescription = args.NewServerDescription;
137+
var newClusterDescription = Description;
138+
135139
if (newServerDescription.State == ServerState.Disconnected)
136140
{
137-
newClusterDescription = Description
138-
.WithServerDescription(newServerDescription);
141+
newClusterDescription = newClusterDescription.WithServerDescription(newServerDescription);
139142
}
140143
else
141144
{
142-
var determinedClusterType = DetermineClusterType(newServerDescription);
143-
if (oldClusterDescription.Type == ClusterType.Unknown)
144-
{
145-
newClusterDescription = newClusterDescription
146-
.WithType(determinedClusterType)
147-
.WithServerDescription(newServerDescription);
148-
}
149-
else if (determinedClusterType != oldClusterDescription.Type)
145+
if (IsServerValidForCluster(newClusterDescription.Type, Settings.ConnectionMode, newServerDescription.Type))
150146
{
151-
newClusterDescription = newClusterDescription
152-
.WithoutServerDescription(newServerDescription.EndPoint);
147+
if (newClusterDescription.Type == ClusterType.Unknown)
148+
{
149+
newClusterDescription = newClusterDescription.WithType(newServerDescription.Type.ToClusterType());
150+
}
151+
152+
newClusterDescription = newClusterDescription.WithServerDescription(newServerDescription);
153153
}
154154
else
155155
{
156-
newClusterDescription = newClusterDescription
157-
.WithServerDescription(newServerDescription);
156+
newClusterDescription = newClusterDescription.WithoutServerDescription(newServerDescription.EndPoint);
158157
}
159158
}
160159

src/MongoDB.Driver.Core/Core/Servers/ServerType.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,11 @@ public static ClusterType ToClusterType(this ServerType serverType)
7171
return ClusterType.Sharded;
7272
case ServerType.Standalone:
7373
return ClusterType.Standalone;
74-
default:
74+
case ServerType.Unknown:
7575
return ClusterType.Unknown;
76+
default:
77+
var message = string.Format("Invalid server type: {0}.", serverType);
78+
throw new ArgumentException(message, "serverType");
7679
}
7780
}
7881
}

0 commit comments

Comments
 (0)