@@ -5,14 +5,13 @@ import java.net.{InetAddress, InetSocketAddress}
5
5
6
6
import com .datastax .oss .driver .api .core .CqlSession
7
7
import com .datastax .oss .driver .api .core .loadbalancing .NodeDistance
8
- import com .datastax .spark .connector .cql .CassandraConnectorConf .CassandraSSLConf
9
8
import com .datastax .spark .connector .types .TypeAdapters .{ValueByNameAdapter , ValuesSeqAdapter }
10
9
import com .datastax .spark .connector .types .{NullableTypeConverter , TypeConverter }
11
10
import com .datastax .spark .connector .util .ConfigCheck .ConnectorConfigurationException
12
11
import com .datastax .spark .connector .util .DriverUtil .toAddress
13
- import com .datastax .spark .connector .util .{DriverUtil , Logging , SerialShutdownHooks }
12
+ import com .datastax .spark .connector .util .{Logging , SerialShutdownHooks }
14
13
import org .apache .spark .sql .catalyst .expressions .GenericRowWithSchema
15
- import org .apache .spark .{SparkConf , SparkContext , SparkFiles }
14
+ import org .apache .spark .{SparkConf , SparkContext }
16
15
17
16
import scala .collection .JavaConverters ._
18
17
import scala .language .reflectiveCalls
@@ -65,14 +64,8 @@ class CassandraConnector(val conf: CassandraConnectorConf)
65
64
def hosts : Set [InetSocketAddress ] =
66
65
// wrapped in a session, so we get full lists of hosts,
67
66
// not only those explicitly passed in the conf
68
- withSessionDo {
69
- _.getMetadata
70
- .getNodes
71
- .values
72
- .asScala
73
- .filter(_.getDistance == NodeDistance .LOCAL )
74
- .flatMap(DriverUtil .toAddress)
75
- .toSet
67
+ withSessionDo { session =>
68
+ dataCenterNodes(_config, session)
76
69
}
77
70
78
71
private [connector] def hostAddresses : Set [InetAddress ] = hosts.map(_.getAddress)
@@ -187,14 +180,14 @@ object CassandraConnector extends Logging {
187
180
logInfo(s " Disconnected from Cassandra cluster. " )
188
181
}
189
182
190
- private def dataCenterNodes (conf : CassandraConnectorConf , ipConf : IpBasedContactInfo , session : CqlSession ): Set [InetSocketAddress ] = {
183
+ // LocalNodeFirstLoadBalancingPolicy assigns LOCAL or REMOTE (i.e. non-IGNORED) distance to local DC nodes
184
+ private def dataCenterNodes (conf : CassandraConnectorConf , session : CqlSession ): Set [InetSocketAddress ] = {
191
185
val allNodes = session.getMetadata.getNodes.asScala.values.toSet
192
- val dcToUse = conf.localDC.getOrElse(LocalNodeFirstLoadBalancingPolicy .determineDataCenter(ipConf.hosts, allNodes))
193
186
val nodes = allNodes
194
- .collect { case n if n.getDatacenter == dcToUse => toAddress(n) }
195
- .flatten
187
+ .filter(_.getDistance != NodeDistance . IGNORED )
188
+ .flatMap(toAddress)
196
189
if (nodes.isEmpty) {
197
- throw new ConnectorConfigurationException (s " Could not determine suitable nodes for DC: $dcToUse and known nodes: " +
190
+ throw new ConnectorConfigurationException (s " Could not determine suitable nodes in local DC for known nodes: " +
198
191
s " ${allNodes.map(n => (n.getHostId, toAddress(n))).mkString(" , " )}" )
199
192
}
200
193
nodes
@@ -204,7 +197,7 @@ object CassandraConnector extends Logging {
204
197
private def alternativeConnectionConfigs (conf : CassandraConnectorConf , session : CqlSession ): Set [CassandraConnectorConf ] = {
205
198
conf.contactInfo match {
206
199
case ipConf : IpBasedContactInfo =>
207
- val nodes = dataCenterNodes(conf, ipConf, session)
200
+ val nodes = dataCenterNodes(conf, session)
208
201
nodes.map(n => conf.copy(contactInfo = ipConf.copy(hosts = Set (n)))) + conf.copy(contactInfo = ipConf.copy(hosts = nodes))
209
202
case _ => Set .empty
210
203
}
0 commit comments