Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
New version of MAHEVE plus corrections.
[mapping.git] / src / and / Mapping / Cluster.java
1 package and.Mapping ;
2
3
4 import java.io.Serializable;
5 import java.util.ArrayList;
6
7
8 /**
9  * Class representing a computing nodes cluster
10  * @author Sébastien Miquée
11  *
12  */
13 public class Cluster implements Serializable
14 {
15         private static final long serialVersionUID = 1L;
16         
17         private String name ;
18         private ArrayList<GNode> nodes ;
19         private ArrayList<GNode> freenodes ;
20         private String site ;
21         private int moreNode ;
22         
23         
24         /**
25          * Default constructor.
26          */
27         public Cluster()
28         {
29                 name = "" ;
30                 nodes = new ArrayList<GNode>() ;
31                 freenodes = new ArrayList<GNode>() ;
32                 site = "" ;
33                 moreNode = 0 ;
34         }
35         
36         
37         /**
38          * Set the name of the cluster.
39          * @param _name Cluster's name
40          */
41         public void setName( String _name )
42         {
43                 name = _name ;
44         }
45         
46         
47         /**
48          * Adding a computing node to the cluster.
49          * @param _n Node to be add
50          */
51         public void addGNode( GNode _n )
52         {
53                 if( _n != null && _n.getClusterName().equalsIgnoreCase( name ) )
54                 {
55                         _n.setInCluster( true ) ;
56                         nodes.add( _n ) ;
57
58                         if( ! _n.getMapped() )
59                         {
60                                 freenodes.add( _n ) ;
61                         }
62                 }
63         }
64         
65         
66         /**
67          * Return the list of computing nodes which are in the cluster.
68          * @return The list of nodes
69          */
70         public ArrayList<GNode> getGNodes()
71         {
72                 return nodes ;
73         }
74         
75         
76         /**
77          * Return the list of free computing nodes which are in the cluster.
78          * @return The list of free nodes
79          */
80         public ArrayList<GNode> getFreeGNodes()
81         {
82                 return freenodes ;
83         }
84         
85         
86         /**
87          * Return cluster's name.
88          * @return Cluster's name
89          */
90         public String getName()
91         {
92                 return name ;
93         }
94         
95         
96         /**
97          * Return the amount of computing nodes in the cluster.
98          * @return The amount of nodes
99          */
100         public int getNbGNode()
101         {
102                 return nodes.size() ;
103         }
104         
105         
106         /**
107          * Set the site in which the cluster is.
108          * @param _site Site's name
109          */
110         public void setSite( String _site )
111         {
112                 site = _site ;
113         }
114         
115         
116         /**
117          * Return the site's name in which the cluster is.
118          * @return The site's name
119          */
120         public String getSite()
121         {
122                 return site ;
123         }
124
125         
126         /**
127          * Test if a computing node is in the cluster, and return its position (if
128          * it exists).
129          * @param _g The node to be tested
130          * @return The position of the node
131          */
132         public int isIn( GNode _g )
133         {
134                 int pos = -1 ;
135                 
136                 if( _g != null )
137                 {
138                         for( int i = 0 ; i < nodes.size() ; i ++ )
139                         {
140                                 if( nodes.get( i ).getId() == _g.getId() )
141                                 {
142                                         pos = i ;
143                                         break ;
144                                 }                               
145                         }
146                 }
147                 
148                 return pos ;
149         }
150         
151         
152         /**
153          * Test if a computing node is in the cluster, and return it (if
154          * it exists).
155          * @param _g The node to be tested
156          * @return The position of the node
157          */
158         public GNode exists( GNode _g )
159         {               
160                 if( _g != null )
161                 {
162                         for( int i = 0 ; i < nodes.size() ; i ++ )
163                         {
164                                 if( nodes.get( i ).getId() == _g.getId() )
165                                 {
166                                         return nodes.get( i ) ;
167                                 }                       
168                         }
169                 }
170                 
171                 return null ;
172         }
173         
174         /**
175          * Return the next available computing node in the cluster.
176          * @return The next node in the cluster
177          */
178         public GNode nextGNode() 
179         {
180                 GNode ret = null ;
181                 
182                 if( freenodes.size() > 0 )
183                 {
184                         ret = freenodes.get( 0 ) ;
185                 }
186                 
187                 return ret ;
188         }
189         
190         
191         /**
192          * Return the next available computing node in the cluster,
193          * according to the moreNode iterator.
194          * @return The next node in the cluster
195          */
196         public GNode moreGNode()
197         {
198                 GNode ret = null ;
199                 
200                 if( freenodes.size() > 0 )
201                 {
202                         if( moreNode >= freenodes.size() )
203                         {
204                                 System.err.println( "No more free node available" );
205                         } else {
206                                 ret = freenodes.get( moreNode ) ;
207                                 moreNode++ ;
208                         }
209                 }
210                 
211                 return ret ;
212         }
213         
214         
215         /**
216          * (Re-)Initialize the moreNode counter. 
217          */
218         public void initMoreGNode()
219         {
220                 moreNode = 0 ;
221         }
222
223
224         /**
225          * Remove a failed node from the cluster.
226          * @param _dead The failed node
227          */
228         public void removeGNode( GNode _dead ) 
229         {
230                 if( _dead != null )
231                 {
232                         if( _dead.getClusterName().equalsIgnoreCase( name ) 
233                                         && _dead.getSiteName().equalsIgnoreCase( site )  )
234                         {
235                                 int i = 0 ;
236                                 for( i = 0 ; i < nodes.size() ; i++ )
237                                 {
238                                         if( _dead.getId() == nodes.get( i ).getId() )
239                                         {
240                                                 nodes.remove( i ) ;
241
242                                                 int j = 0 ;
243                                                 for( j = 0 ; j < freenodes.size() ; j++ )
244                                                 {
245                                                         if( freenodes.get(j).getId() == _dead.getId() )
246                                                         {
247                                                                 freenodes.remove( j ) ;
248                                                                 break ;
249                                                         }
250                                                 }
251
252                                                 break ;
253                                         }
254                                 }
255                                 
256                                 if( i > nodes.size() )
257                                 {
258                                         System.err.println( "(Cluster) The GNode was not found in the list!" );
259                                 }
260                         } else {
261                                 System.err.println( "(Cluster) The GNode to be deleted is not mine!" ) ;
262                         }
263                 } else {
264                         System.err.println( "(Cluster) The GNode to be deleted is null!" ) ;
265                 }
266         }
267         
268         
269         /**
270          * Set the mapped status of a node of the cluster.
271          * @param _g The mapped node
272          * @param _status The status of the node
273          */
274         public void setGNodeStatus( GNode _g, boolean _status )
275         {
276                 if( _g != null )
277                 {
278                         for( int i = 0 ; i < nodes.size() ; i++ )
279                         {
280                                 if( nodes.get( i ).getId() == _g.getId() )
281                                 {
282                                         nodes.get( i ).setMapped( _status ) ;
283                                         
284                                         if( _status ) 
285                                         {
286                                                 for( int j = 0 ; j < freenodes.size() ; j++ )
287                                                 {
288                                                         if( freenodes.get(j).getId() == nodes.get(i).getId() )
289                                                         {
290                                                                 freenodes.remove( j ) ;
291                                                                 break ;
292                                                         }
293                                                 }
294
295                                         } else {
296                                                 if( ! freenodes.contains( nodes.get( i ) ) )
297                                                         freenodes.add( nodes.get( i ) ) ;
298                                         }
299                                 }
300                         }
301                 }
302         }
303         
304         
305         /**
306          * Return the amount of available free nodes in the cluster.
307          * @return The amount of free nodes
308          */
309         public int getNbFreeNodes()
310         {
311                 return freenodes.size() ;
312         }
313         
314         
315         /**
316          * Compute and return the available computing power of the cluster.
317          * @return The available computing power
318          */
319         public double getAvailablePower()
320         {
321                 double ret = 0 ;
322                 
323                 /** If there is some available nodes **/
324                 if( freenodes.size() > 0 )
325                 {                                               
326                         for( int i = 0 ; i < freenodes.size() ; i++ )
327                         {
328                                 ret += freenodes.get( i ).getPower() ;
329                         }
330                 }
331                 
332                 return ret ;
333         }
334         
335         
336         /**
337          * Compute and return the average available computing power of the cluster.
338          * @return The available available computing power
339          */
340         public double getAvgAvailablePower()
341         {
342                 double ret = 0 ;
343                 
344                 /** If there is some available nodes **/
345                 if( freenodes.size() > 0 )
346                 {                                               
347                         for( int i = 0 ; i < freenodes.size() ; i++ )
348                         {
349                                 ret += freenodes.get( i ).getPower() ;
350                         }
351                         
352                         ret = ret / freenodes.size() ;
353                 }
354                 
355                 return ret ;
356         }
357
358         
359         public double getAvgPower()
360         {
361                 double ret = 0 ;
362                 
363                 /** If there is some available nodes **/
364                 if( nodes.size() > 0 )
365                 {                                               
366                         for( int i = 0 ; i < nodes.size() ; i++ )
367                         {
368                                 ret += nodes.get( i ).getPower() ;
369                         }
370                         
371                         ret = ret / nodes.size() ;
372                 }
373                 
374                 return ret ;
375         }
376         
377         
378         public double getPower()
379         {
380                 double ret = 0 ;
381                 
382                 /** If there is some available nodes **/
383                 if( nodes.size() > 0 )
384                 {                                               
385                         for( int i = 0 ; i < nodes.size() ; i++ )
386                         {
387                                 ret += nodes.get( i ).getPower() ;
388                         }
389                 }
390                 
391                 return ret ;
392         }
393
394         /**
395          * Initialization of computing nodes in this cluster. Set all
396          * of these nodes to be not mapped on.
397          */
398         public void initGNodes() 
399         {
400                 /** Initialization of local nodes            **/
401                 /** and reconstruction of the freenodes list **/
402                 freenodes = null ;
403                 freenodes = new ArrayList<GNode>() ;
404                 
405                 for( int i = 0 ; i < nodes.size() ; i++ )
406                 {
407                         nodes.get( i ).setMapped( false ) ;
408                         
409                         freenodes.add( nodes.get( i ) ) ;
410                 }
411         }
412         
413         
414         /**
415          * Replace a node in the cluster (in case of a reconnection for example).
416          * @param _g The node to be replaced
417          */
418         public void replaceGNode( GNode _g )
419         {
420                 if( _g != null )
421                 {
422                         removeGNode( _g ) ;
423                         addGNode( _g ) ;
424                 }
425         }
426
427
428         /**
429          * Search and return the better (most powerful) available node 
430          * of the cluster.
431          * @return The best available node
432          */
433         public GNode getBetterFreeGNode() 
434         {
435                 GNode ret = null ;
436                 
437                 if( freenodes.size() > 0 )
438                 {
439                         ret = freenodes.get( 0 ) ;
440                 }
441                 
442                 for( int i = 1 ; i < freenodes.size() ; i++ )
443                 {
444                         if( freenodes.get( i ).getPower() > ret.getPower() )
445                         {
446                                 ret = freenodes.get( i ) ;
447                         }
448                 }
449                 
450                 return ret ;
451         }
452         
453         
454         /**
455          * Construct and return a copy of the current Cluster.
456          * @return A copy of the cluster
457          */
458         public Cluster clone()
459         {
460                 Cluster copy = new Cluster() ;
461                 
462                 copy.setName( name ) ;
463                 copy.setSite( site ) ;
464                 
465                 for( int i = 0 ; i < nodes.size() ; i++ )
466                 {
467                         GNode newgn = (GNode) nodes.get( i ).clone() ;
468                         newgn.setCluster( copy ) ;
469                         copy.addGNode( newgn ) ;
470                 }
471                 
472                 return copy ;
473         }
474         
475 }
476
477 /** La programmation est un art, respectons ceux qui la pratiquent !! **/