2121import java .util .HashMap ;
2222import java .util .List ;
2323import java .util .Map ;
24- import java .util .Map .Entry ;
2524import java .util .Properties ;
2625import java .util .Random ;
27- import java .util .concurrent .Callable ;
26+ import java .util .concurrent .locks . ReentrantLock ;
2827import java .util .regex .Matcher ;
2928import java .util .regex .Pattern ;
3029import java .util .stream .Collectors ;
@@ -46,6 +45,8 @@ public class WeightedRandomHostSelector implements HostSelector {
4645 private String cachedHostWeightMapString ;
4746 private Random random ;
4847
48+ private final ReentrantLock lock = new ReentrantLock ();
49+
4950 public WeightedRandomHostSelector () {
5051 this (new Random ());
5152 }
@@ -108,43 +109,48 @@ public HostSpec getHost(
108109 }
109110
110111 private Map <String , Integer > getHostWeightPairMap (final String hostWeightMapString ) throws SQLException {
111- if (this .cachedHostWeightMapString != null
112- && this .cachedHostWeightMapString .trim ().equals (hostWeightMapString .trim ())
113- && this .cachedHostWeightMap != null
114- && !this .cachedHostWeightMap .isEmpty ()) {
115- return this .cachedHostWeightMap ;
116- }
117-
118- final Map <String , Integer > hostWeightMap = new HashMap <>();
119- if (hostWeightMapString == null || hostWeightMapString .trim ().isEmpty ()) {
120- return hostWeightMap ;
121- }
122- final String [] hostWeightPairs = hostWeightMapString .split ("," );
123- for (final String hostWeightPair : hostWeightPairs ) {
124- final Matcher matcher = HOST_WEIGHT_PAIRS_PATTERN .matcher (hostWeightPair );
125- if (!matcher .matches ()) {
126- throw new SQLException (Messages .get ("HostSelector.weightedRandomInvalidHostWeightPairs" ));
112+ try {
113+ lock .lock ();
114+ if (this .cachedHostWeightMapString != null
115+ && this .cachedHostWeightMapString .trim ().equals (hostWeightMapString .trim ())
116+ && this .cachedHostWeightMap != null
117+ && !this .cachedHostWeightMap .isEmpty ()) {
118+ return this .cachedHostWeightMap ;
127119 }
128120
129- final String hostName = matcher .group ("host" ).trim ();
130- final String hostWeight = matcher .group ("weight" ).trim ();
131- if (hostName .isEmpty () || hostWeight .isEmpty ()) {
132- throw new SQLException (Messages .get ("HostSelector.weightedRandomInvalidHostWeightPairs" ));
121+ final Map <String , Integer > hostWeightMap = new HashMap <>();
122+ if (hostWeightMapString == null || hostWeightMapString .trim ().isEmpty ()) {
123+ return hostWeightMap ;
133124 }
125+ final String [] hostWeightPairs = hostWeightMapString .split ("," );
126+ for (final String hostWeightPair : hostWeightPairs ) {
127+ final Matcher matcher = HOST_WEIGHT_PAIRS_PATTERN .matcher (hostWeightPair );
128+ if (!matcher .matches ()) {
129+ throw new SQLException (Messages .get ("HostSelector.weightedRandomInvalidHostWeightPairs" ));
130+ }
134131
135- try {
136- final int weight = Integer . parseInt ( hostWeight );
137- if (weight < DEFAULT_WEIGHT ) {
132+ final String hostName = matcher . group ( "host" ). trim ();
133+ final String hostWeight = matcher . group ( "weight" ). trim ( );
134+ if (hostName . isEmpty () || hostWeight . isEmpty () ) {
138135 throw new SQLException (Messages .get ("HostSelector.weightedRandomInvalidHostWeightPairs" ));
139136 }
140- hostWeightMap .put (hostName , weight );
141- } catch (NumberFormatException e ) {
142- throw new SQLException (Messages .get ("HostSelector.roundRobinInvalidHostWeightPairs" ));
137+
138+ try {
139+ final int weight = Integer .parseInt (hostWeight );
140+ if (weight < DEFAULT_WEIGHT ) {
141+ throw new SQLException (Messages .get ("HostSelector.weightedRandomInvalidHostWeightPairs" ));
142+ }
143+ hostWeightMap .put (hostName , weight );
144+ } catch (NumberFormatException e ) {
145+ throw new SQLException (Messages .get ("HostSelector.roundRobinInvalidHostWeightPairs" ));
146+ }
143147 }
148+ this .cachedHostWeightMap = hostWeightMap ;
149+ this .cachedHostWeightMapString = hostWeightMapString ;
150+ return hostWeightMap ;
151+ } finally {
152+ lock .unlock ();
144153 }
145- this .cachedHostWeightMap = hostWeightMap ;
146- this .cachedHostWeightMapString = hostWeightMapString ;
147- return hostWeightMap ;
148154 }
149155
150156 private static class NumberRange {
0 commit comments