1 22 package org.jboss.ha.hasessionstate.server; 23 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 27 import org.jboss.ha.framework.interfaces.SubPartitionInfo; 28 import org.jboss.ha.framework.interfaces.SubPartitionsInfo; 29 30 39 40 public class HASessionStateTopologyComputerImpl implements HASessionStateTopologyComputer 41 { 42 43 protected long nodesPerSubPartition = 0; 44 protected String sessionStateIdentifier = null; 45 46 47 public HASessionStateTopologyComputerImpl () 48 { 49 } 50 51 public void init (String sessionStateName, long nodesPerSubPartition) 52 { 53 this.sessionStateIdentifier = sessionStateName; 54 this.nodesPerSubPartition = nodesPerSubPartition; 55 } 56 57 public void start () {} 58 59 public SubPartitionsInfo computeNewTopology (SubPartitionsInfo currentTopology, ArrayList newReplicants) 60 { 61 if (newReplicants.size () < 1) 62 currentTopology.partitions = null; 63 else if (newReplicants.size () == 1) 64 { 65 if (currentTopology.partitions != null) 68 currentTopology = computeCompatibleComposition (currentTopology, newReplicants); 69 else 70 { 71 SubPartitionInfo aPartition = new SubPartitionInfo (); 72 aPartition.subPartitionName = getSubPartitionName (currentTopology); 73 aPartition.memberNodeNames.add (newReplicants.get (0)); 74 SubPartitionInfo[] thePartition = 75 { aPartition }; 76 currentTopology.partitions = thePartition; 77 } 78 } 79 else if (currentTopology == null || currentTopology.partitions == null) 80 currentTopology = computerFirstComposition (currentTopology, newReplicants); 83 else 84 currentTopology = computeCompatibleComposition (currentTopology, newReplicants); 88 89 return currentTopology; 90 91 } 92 93 protected SubPartitionsInfo computerFirstComposition (SubPartitionsInfo splitingInfo, ArrayList replicants) 94 { 95 int i=0; 96 String rep = null; 97 ArrayList newConfig = new ArrayList (); 98 SubPartitionInfo aPartition = null; 99 int grpNumber = 0; 100 101 for (Iterator reps = replicants.iterator (); reps.hasNext (); i++) 104 { 105 rep = (String )reps.next (); 106 if ( (i%nodesPerSubPartition) == 0 ) 107 { 108 grpNumber++; 109 aPartition = new SubPartitionInfo (); 110 aPartition.subPartitionName = getSubPartitionName (splitingInfo); 111 newConfig.add (aPartition); 112 } 113 aPartition.memberNodeNames.add (rep); 114 } 115 116 if (aPartition.memberNodeNames.size () == 1) 119 { 120 rep = (String ) aPartition.memberNodeNames.get (0); newConfig.remove (grpNumber-1); aPartition = (SubPartitionInfo)(newConfig.get (grpNumber-1)); aPartition.memberNodeNames.add (rep); } 125 126 SubPartitionInfo[] newSpliting = new SubPartitionInfo[1]; 127 newSpliting = (SubPartitionInfo[]) newConfig.toArray (newSpliting); 128 splitingInfo.partitions = newSpliting; 129 130 return splitingInfo; 131 } 132 133 protected SubPartitionsInfo computeCompatibleComposition (SubPartitionsInfo splitingInfo, ArrayList replicants) 134 { 135 SubPartitionInfo[] newSpliting = null; 138 ArrayList newSubParts = new ArrayList (); 139 140 for (int i=0; i<splitingInfo.partitions.length; i++) 141 { 142 SubPartitionInfo currentSubPart = splitingInfo.partitions[i]; 143 SubPartitionInfo newCurrent = null; 144 Iterator iter = currentSubPart.memberNodeNames.iterator (); 145 while (iter.hasNext ()) 146 { 147 String node = (String )iter.next (); 148 if (replicants.contains (node)) 149 { 150 if (newCurrent == null) 151 { 152 newCurrent = (SubPartitionInfo)currentSubPart.clone (); 153 newCurrent.memberNodeNames.clear (); 154 } 155 newCurrent.memberNodeNames.add (node); 156 } 157 } 158 if (newCurrent != null) 159 newSubParts.add (newCurrent); 160 } 161 162 Iterator iter = replicants.iterator (); 165 ArrayList newMembersNotInAGroup = new ArrayList (); 166 while (iter.hasNext ()) 167 { 168 boolean found = false; 169 String aMember = (String )iter.next (); 170 Iterator iterNewSubPart = newSubParts.iterator (); 171 while (iterNewSubPart.hasNext () && !found) 172 if (((SubPartitionInfo)iterNewSubPart.next ()).memberNodeNames.contains (aMember)) 173 found = true; 174 if (!found) 175 newMembersNotInAGroup.add (aMember); 176 } 177 iter = null; 178 179 184 ArrayList smallerGroups = new ArrayList (); 187 ArrayList correctlySizedGroups = new ArrayList (); 188 ArrayList biggerGroups = new ArrayList (); 189 190 for (int i=0; i<newSubParts.size (); i++) 191 { 192 int groupSize = ((SubPartitionInfo)newSubParts.get (i)).memberNodeNames.size (); 193 if (groupSize < this.nodesPerSubPartition) 194 smallerGroups.add (newSubParts.get (i)); 195 else if (groupSize > this.nodesPerSubPartition) 196 biggerGroups.add (newSubParts.get (i)); 197 else 198 correctlySizedGroups.add (newSubParts.get (i)); 199 } 200 201 java.util.Collections.sort (smallerGroups); 204 205 210 iter = newMembersNotInAGroup.iterator (); 214 while (iter.hasNext ()) 215 { 216 String member = (String )iter.next (); 217 SubPartitionInfo target = null; 218 if (smallerGroups.size () > 0) 219 { 220 target = (SubPartitionInfo)smallerGroups.get (0); target.memberNodeNames.add (member); 222 if (target.memberNodeNames.size () == this.nodesPerSubPartition) 223 { 224 smallerGroups.remove (0); 227 correctlySizedGroups.add (target); 228 } 229 } 230 else 231 { 232 target = new SubPartitionInfo (); 235 target.setIsNewGroup (); 236 target.subPartitionName = getSubPartitionName (splitingInfo); 237 target.memberNodeNames.add (member); 238 smallerGroups.add (target); 239 java.util.Collections.sort (smallerGroups); 240 } 241 } 242 243 iter = biggerGroups.iterator (); 249 while (iter.hasNext ()) 250 { 251 SubPartitionInfo big = (SubPartitionInfo)iter.next (); 252 if (smallerGroups.size () > 0) 253 { 254 String member = (String )big.memberNodeNames.get (big.memberNodeNames.size ()-1); SubPartitionInfo target = null; 256 target = (SubPartitionInfo)smallerGroups.get (0); target.memberNodeNames.add (member); 258 big.memberNodeNames.remove (big.memberNodeNames.size () -1); 259 if (target.memberNodeNames.size () == this.nodesPerSubPartition) 260 { 261 smallerGroups.remove (0); 264 correctlySizedGroups.add (target); 265 } 266 } 267 } 268 correctlySizedGroups.addAll (biggerGroups); 271 272 boolean thirdStepFinished = (smallerGroups.size () == 0); 276 while (!thirdStepFinished) 277 { 278 SubPartitionInfo current = (SubPartitionInfo)smallerGroups.get (smallerGroups.size ()-1); 280 for (int i = smallerGroups.size ()-2; i >= 0; i--) 281 { 282 SubPartitionInfo merger = (SubPartitionInfo)smallerGroups.get (i); 285 if ((merger.memberNodeNames.size () + current.memberNodeNames.size ()) <= this.nodesPerSubPartition) 286 { 287 current.merge (merger); 290 smallerGroups.remove (i); 291 292 } 293 if (current.memberNodeNames.size () == this.nodesPerSubPartition) 296 break; 297 298 } 299 if (current.memberNodeNames.size () > 1) 300 { 301 smallerGroups.remove (smallerGroups.size ()-1); 304 correctlySizedGroups.add (current); 305 } 306 307 thirdStepFinished = ( (smallerGroups.size () == 0) || 308 ((smallerGroups.size () == 1) && ( ((SubPartitionInfo)smallerGroups.get (0)).memberNodeNames.size () == 1)) ); 309 310 } 311 312 if (smallerGroups.size () > 0) 316 { 317 if (correctlySizedGroups.size ()>0) 318 { 319 java.util.Collections.sort (correctlySizedGroups); 320 SubPartitionInfo merger = (SubPartitionInfo)smallerGroups.get (0); 321 SubPartitionInfo master = (SubPartitionInfo)correctlySizedGroups.get (0); 322 master.merge (merger); 323 } 324 else 325 { 326 correctlySizedGroups.add (smallerGroups.get (0)); 329 } 330 } 331 332 newSpliting = new SubPartitionInfo[1]; 336 newSpliting = (SubPartitionInfo[])correctlySizedGroups.toArray (newSpliting); 337 splitingInfo.partitions = newSpliting; 338 339 return splitingInfo; 340 } 341 342 protected String getSubPartitionName (SubPartitionsInfo manager) 343 { 344 return this.sessionStateIdentifier + "-Group-" + manager.getNextGroupId (); 345 } 346 347 392 } 393 | Popular Tags |