1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package com.allanbank.mongodb.client.connection.rs;
22
23 import java.io.IOException;
24 import java.util.List;
25
26 import com.allanbank.mongodb.MongoClientConfiguration;
27 import com.allanbank.mongodb.MongoDbException;
28 import com.allanbank.mongodb.client.Message;
29 import com.allanbank.mongodb.client.connection.Connection;
30 import com.allanbank.mongodb.client.connection.proxy.AbstractProxyMultipleConnection;
31 import com.allanbank.mongodb.client.connection.proxy.ConnectionInfo;
32 import com.allanbank.mongodb.client.connection.proxy.ProxiedConnectionFactory;
33 import com.allanbank.mongodb.client.state.Cluster;
34 import com.allanbank.mongodb.client.state.Server;
35 import com.allanbank.mongodb.util.log.Log;
36 import com.allanbank.mongodb.util.log.LogFactory;
37
38
39
40
41
42
43
44
45
46 public class ReplicaSetConnection extends
47 AbstractProxyMultipleConnection<Server> {
48
49
50 private static final Log LOG = LogFactory
51 .getLog(ReplicaSetConnection.class);
52
53
54 private volatile ReplicaSetReconnectStrategy myReconnectStrategy;
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72 public ReplicaSetConnection(final Connection proxiedConnection,
73 final Server server, final Cluster cluster,
74 final ProxiedConnectionFactory factory,
75 final MongoClientConfiguration config,
76 final ReplicaSetReconnectStrategy strategy) {
77 super(proxiedConnection, server, cluster, factory, config);
78
79 myReconnectStrategy = strategy;
80 }
81
82
83
84
85
86
87
88 @Override
89 public String getServerName() {
90 if (myMainKey != null) {
91 return myMainKey.getCanonicalName();
92 }
93 return "UNKNOWN";
94 }
95
96
97
98
99
100
101
102 @Override
103 protected Connection connect(final Server server) {
104 Connection conn = null;
105 try {
106 conn = myFactory.connect(server, myConfig);
107
108 conn = cacheConnection(server, conn);
109 }
110 catch (final IOException e) {
111 LOG.info("Could not connect to the server '"
112 + server.getCanonicalName() + "': " + e.getMessage());
113 }
114 return conn;
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131 @Override
132 protected List<Server> findPotentialKeys(final Message message1,
133 final Message message2) throws MongoDbException {
134 List<Server> servers = myCluster.findServers(message1, message2);
135
136 if (servers.isEmpty()) {
137
138
139
140 if (myMainKey == null) {
141
142 final ConnectionInfo<Server> newConnInfo = reconnectMain();
143 if (newConnInfo != null) {
144 updateMain(newConnInfo);
145 servers = myCluster.findServers(message1, message2);
146 }
147 }
148
149 if (servers.isEmpty()) {
150 throw createReconnectFailure(message1, message2);
151 }
152 }
153
154 return servers;
155 }
156
157
158
159
160
161
162
163 @Override
164 protected String getConnectionType() {
165 return "ReplicaSet";
166 }
167
168
169
170
171
172
173
174 @Override
175 protected ConnectionInfo<Server> reconnectMain() {
176 return myReconnectStrategy.reconnectPrimary();
177 }
178
179 }