Merge "On ARPHandler restart Threads are left dangling, very visible in the IT tests"
[controller.git] / opendaylight / clustering / services_implementation / src / main / java / org / opendaylight / controller / clustering / services_implementation / internal / ClusterManager.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.opendaylight.controller.clustering.services_implementation.internal;
11
12 import java.net.InetAddress;
13 import java.net.NetworkInterface;
14 import java.net.SocketException;
15 import java.net.UnknownHostException;
16 import java.util.ArrayList;
17 import java.util.EnumSet;
18 import java.util.Enumeration;
19 import java.util.HashSet;
20 import java.util.List;
21 import java.util.Properties;
22 import java.util.Set;
23 import java.util.StringTokenizer;
24 import java.util.concurrent.ConcurrentMap;
25
26 import javax.transaction.HeuristicMixedException;
27 import javax.transaction.HeuristicRollbackException;
28 import javax.transaction.NotSupportedException;
29 import javax.transaction.RollbackException;
30 import javax.transaction.SystemException;
31 import javax.transaction.Transaction;
32 import javax.transaction.TransactionManager;
33
34 import org.infinispan.Cache;
35 import org.infinispan.configuration.cache.Configuration;
36 import org.infinispan.manager.DefaultCacheManager;
37 import org.infinispan.manager.EmbeddedCacheManager;
38 import org.infinispan.notifications.Listener;
39 import org.infinispan.notifications.cachemanagerlistener.annotation.ViewChanged;
40 import org.infinispan.notifications.cachemanagerlistener.event.ViewChangedEvent;
41 import org.infinispan.remoting.transport.Address;
42 import org.infinispan.remoting.transport.Transport;
43 import org.infinispan.remoting.transport.jgroups.JGroupsAddress;
44 import org.infinispan.remoting.transport.jgroups.JGroupsTransport;
45 import org.jgroups.Channel;
46 import org.jgroups.Event;
47 import org.jgroups.stack.GossipRouter;
48 import org.opendaylight.controller.clustering.services.CacheConfigException;
49 import org.opendaylight.controller.clustering.services.CacheExistException;
50 import org.opendaylight.controller.clustering.services.CacheListenerAddException;
51 import org.opendaylight.controller.clustering.services.IClusterServices;
52 import org.opendaylight.controller.clustering.services.IGetUpdates;
53 import org.opendaylight.controller.clustering.services.IListenRoleChange;
54 import org.opendaylight.controller.clustering.services.ListenRoleChangeAddException;
55 import org.opendaylight.controller.sal.core.IContainerAware;
56 import org.slf4j.Logger;
57 import org.slf4j.LoggerFactory;
58
59 public class ClusterManager implements IClusterServices, IContainerAware {
60     protected static final Logger logger = LoggerFactory
61             .getLogger(ClusterManager.class);
62     private DefaultCacheManager cm;
63     GossipRouter gossiper;
64     private HashSet<IListenRoleChange> roleChangeListeners;
65     private ViewChangedListener cacheManagerListener;
66
67     private static String loopbackAddress = "127.0.0.1";
68
69     /**
70      * Start a JGroups GossipRouter if we are a supernode. The
71      * GosispRouter is nothing more than a simple
72      * rendevouz-pointer. All the nodes that wants to join the cluster
73      * will come to any of the rendevouz point and they introduce the
74      * nodes to all the others. Once the meet and greet phase if over,
75      * the nodes will open a full-mesh with the remaining n-1 nodes,
76      * so even if the GossipRouter goes down nothing is lost.
77      * NOTE: This function has the side effect to set some of the
78      * JGROUPS configurations, this because in this function already
79      * we try to retrieve some of the network capabilities of the
80      * device and so it's better not to do that again
81      *
82      *
83      * @return GossipRouter
84      */
85     private GossipRouter startGossiper() {
86         boolean amIGossipRouter = false;
87         Integer gossipRouterPortDefault = 12001;
88         Integer gossipRouterPort = gossipRouterPortDefault;
89         InetAddress gossipRouterAddress = null;
90         String supernodes_list = System.getProperty("supernodes",
91                 loopbackAddress);
92         StringBuffer sanitized_supernodes_list = new StringBuffer();
93         List<InetAddress> myAddresses = new ArrayList<InetAddress>();
94
95         StringTokenizer supernodes = new StringTokenizer(supernodes_list, ":");
96         if (supernodes.hasMoreTokens()) {
97             // Populate the list of my addresses
98             try {
99                 Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
100                 while (e.hasMoreElements()) {
101                     NetworkInterface n = e.nextElement();
102                     Enumeration<InetAddress> ee = n.getInetAddresses();
103                     while (ee.hasMoreElements()) {
104                         InetAddress i = ee.nextElement();
105                         myAddresses.add(i);
106                     }
107                 }
108             } catch (SocketException se) {
109                 logger.error("Cannot get the list of network interfaces");
110                 return null;
111             }
112         }
113         while (supernodes.hasMoreTokens()) {
114             String curr_supernode = supernodes.nextToken();
115             logger.debug("Examining supernode {}", curr_supernode);
116             StringTokenizer host_port = new StringTokenizer(curr_supernode,
117                     "[]");
118             String host;
119             String port;
120             Integer port_num = gossipRouterPortDefault;
121             if (host_port.countTokens() > 2) {
122                 logger.error("Error parsing supernode {} proceed to the next one",
123                         curr_supernode);
124                 continue;
125             }
126             host = host_port.nextToken();
127             InetAddress hostAddr;
128             try {
129                 hostAddr = InetAddress.getByName(host);
130             } catch (UnknownHostException ue) {
131                 logger.error("Host not known");
132                 continue;
133             }
134             if (host_port.hasMoreTokens()) {
135                 port = host_port.nextToken();
136                 try {
137                     port_num = Integer.valueOf(port);
138                 } catch (NumberFormatException ne) {
139                     logger
140                             .error("Supplied supernode gossiepr port is not recognized, using standard gossipport");
141                     port_num = gossipRouterPortDefault;
142                 }
143                 if ((port_num > 65535) || (port_num < 0)) {
144                     logger
145                             .error("Supplied supernode gossip port is outside a valid TCP port range");
146                     port_num = gossipRouterPortDefault;
147                 }
148             }
149             if (!amIGossipRouter) {
150                 if (host != null) {
151                     for (InetAddress myAddr : myAddresses) {
152                         if (myAddr.equals(hostAddr)) {
153                             amIGossipRouter = true;
154                             gossipRouterAddress = hostAddr;
155                             gossipRouterPort = port_num;
156                             break;
157                         }
158                     }
159                 }
160             }
161             if (!sanitized_supernodes_list.toString().equals("")) {
162                 sanitized_supernodes_list.append(",");
163             }
164             sanitized_supernodes_list.append(hostAddr.getHostAddress() + "["
165                     + port_num + "]");
166         }
167
168         if (amIGossipRouter) {
169             // Set the Jgroups binding interface to the one we got
170             // from the supernodes attribute
171             if (gossipRouterAddress != null) {
172                 System.setProperty("jgroups.tcp.address", gossipRouterAddress
173                         .getHostAddress());
174             }
175         } else {
176             // Set the Jgroup binding interface to the one we are well
177             // known outside or else to the first with non-local
178             // scope.
179             try {
180                 String myBind = InetAddress.getLocalHost().getHostAddress();
181                 if (myBind == null
182                         || InetAddress.getLocalHost().isLoopbackAddress()) {
183                     for (InetAddress myAddr : myAddresses) {
184                         if (myAddr.isLoopbackAddress()
185                                 || myAddr.isLinkLocalAddress()) {
186                             logger.debug("Skipping local address {}",
187                                          myAddr.getHostAddress());
188                             continue;
189                         } else {
190                             // First non-local address
191                             myBind = myAddr.getHostAddress();
192                             logger.debug("First non-local address {}", myBind);
193                             break;
194                         }
195                     }
196                 }
197                 String jgroupAddress = System
198                         .getProperty("jgroups.tcp.address");
199                 if (jgroupAddress == null) {
200                     if (myBind != null) {
201                         logger.debug("Set bind address to be {}", myBind);
202                         System.setProperty("jgroups.tcp.address", myBind);
203                     } else {
204                         logger
205                                 .debug("Set bind address to be LOCALHOST=127.0.0.1");
206                         System.setProperty("jgroups.tcp.address", "127.0.0.1");
207                     }
208                 } else {
209                     logger.debug("jgroup.tcp.address already set to be {}",
210                             jgroupAddress);
211                 }
212             } catch (UnknownHostException uhe) {
213                 logger
214                         .error("Met UnknownHostException while trying to get binding address for jgroups");
215             }
216         }
217
218         // The supernodes list constitute also the tcpgossip initial
219         // host list
220         System.setProperty("jgroups.tcpgossip.initial_hosts",
221                 sanitized_supernodes_list.toString());
222         logger.debug("jgroups.tcp.address set to {}",
223                 System.getProperty("jgroups.tcp.address"));
224         logger.debug("jgroups.tcpgossip.initial_hosts set to {}",
225                 System.getProperty("jgroups.tcpgossip.initial_hosts"));
226         GossipRouter res = null;
227         if (amIGossipRouter) {
228             logger.info("I'm a GossipRouter will listen on port {}",
229                     gossipRouterPort);
230             // Start a GossipRouter with JMX support
231             res = new GossipRouter(gossipRouterPort, null, true);
232         }
233         return res;
234     }
235
236     public void start() {
237         this.gossiper = startGossiper();
238         if (this.gossiper != null) {
239             logger.debug("Trying to start Gossiper");
240             try {
241                 this.gossiper.start();
242                 logger.info("Started GossipRouter");
243             } catch (Exception e) {
244                 logger.error("GossipRouter didn't start. Exception Stack Trace",
245                              e);
246             }
247         }
248         logger.info("Starting the ClusterManager");
249         try {
250             //FIXME keeps throwing FileNotFoundException
251             this.cm = new DefaultCacheManager("config/infinispan-config.xml");
252             logger.debug("Allocated ClusterManager");
253             if (this.cm != null) {
254                 this.cm.start();
255                 this.cm.startCache();
256                 logger.debug("Started the ClusterManager");
257             }
258         } catch (Exception ioe) {
259             logger.error("Cannot configure infinispan .. bailing out ");
260             logger.error("Stack Trace that raised th exception");
261             logger.error("",ioe);
262             this.cm = null;
263             this.stop();
264         }
265         logger.debug("Cache Manager has value {}", this.cm);
266     }
267
268     public void stop() {
269         logger.info("Stopping the ClusterManager");
270         if (this.cm != null) {
271             logger.info("Found a valid ClusterManager, now let it be stopped");
272             this.cm.stop();
273             this.cm = null;
274         }
275         if (this.gossiper != null) {
276             this.gossiper.stop();
277             this.gossiper = null;
278         }
279     }
280
281     @Override
282     public ConcurrentMap<?, ?> createCache(String containerName,
283             String cacheName, Set<cacheMode> cMode) throws CacheExistException,
284             CacheConfigException {
285         EmbeddedCacheManager manager = this.cm;
286         Cache<Object,Object> c;
287         String realCacheName = "{" + containerName + "}_{" + cacheName + "}";
288         if (manager == null) {
289             return null;
290         }
291
292         if (manager.cacheExists(realCacheName)) {
293             throw new CacheExistException();
294         }
295
296         // Sanity check to avoid contrasting parameters
297         if (cMode.containsAll(EnumSet.of(
298                 IClusterServices.cacheMode.NON_TRANSACTIONAL,
299                 IClusterServices.cacheMode.TRANSACTIONAL))) {
300             throw new CacheConfigException();
301         }
302
303         if (cMode.contains(IClusterServices.cacheMode.NON_TRANSACTIONAL)) {
304             c = manager.getCache(realCacheName);
305             return c;
306         } else if (cMode.contains(IClusterServices.cacheMode.TRANSACTIONAL)) {
307             Configuration rc = manager
308                     .getCacheConfiguration("transactional-type");
309             manager.defineConfiguration(realCacheName, rc);
310             c = manager.getCache(realCacheName);
311             return c;
312         }
313         return null;
314     }
315
316     @Override
317     public ConcurrentMap<?, ?> getCache(String containerName, String cacheName) {
318         EmbeddedCacheManager manager = this.cm;
319         Cache<Object,Object> c;
320         String realCacheName = "{" + containerName + "}_{" + cacheName + "}";
321         if (manager == null) {
322             return null;
323         }
324
325         if (manager.cacheExists(realCacheName)) {
326             c = manager.getCache(realCacheName);
327             return c;
328         }
329         return null;
330     }
331
332     @Override
333     public void destroyCache(String containerName, String cacheName) {
334         EmbeddedCacheManager manager = this.cm;
335         String realCacheName = "{" + containerName + "}_{" + cacheName + "}";
336         if (manager == null) {
337             return;
338         }
339         if (manager.cacheExists(realCacheName)) {
340             manager.removeCache(realCacheName);
341         }
342     }
343
344     @Override
345     public boolean existCache(String containerName, String cacheName) {
346         EmbeddedCacheManager manager = this.cm;
347         String realCacheName = "{" + containerName + "}_{" + cacheName + "}";
348         if (manager == null) {
349             return false;
350         }
351         return manager.cacheExists(realCacheName);
352     }
353
354     @Override
355     public Set<String> getCacheList(String containerName) {
356         Set<String> perContainerCaches = new HashSet<String>();
357         EmbeddedCacheManager manager = this.cm;
358         if (manager == null) {
359             return null;
360         }
361         for (String cacheName : manager.getCacheNames()) {
362             if (cacheName.startsWith("{" + containerName + "}_")) {
363                 String[] res = cacheName.split("[{}]");
364                 if (res.length >= 4 && res[1].equals(containerName)
365                         && res[2].equals("_")) {
366                     perContainerCaches.add(res[3]);
367                 }
368             }
369         }
370
371         return (perContainerCaches);
372     }
373
374     @Override
375     public Properties getCacheProperties(String containerName, String cacheName) {
376         EmbeddedCacheManager manager = this.cm;
377         if (manager == null) {
378             return null;
379         }
380         String realCacheName = "{" + containerName + "}_{" + cacheName + "}";
381         if (!manager.cacheExists(realCacheName)) {
382             return null;
383         }
384         Configuration conf = manager.getCache(realCacheName).getAdvancedCache()
385                 .getCacheConfiguration();
386         Properties p = new Properties();
387         p.setProperty(IClusterServices.cacheProps.TRANSACTION_PROP.toString(),
388                 conf.transaction().toString());
389         p.setProperty(IClusterServices.cacheProps.CLUSTERING_PROP.toString(),
390                 conf.clustering().toString());
391         p.setProperty(IClusterServices.cacheProps.LOCKING_PROP.toString(), conf
392                 .locking().toString());
393         return p;
394     }
395
396     @Override
397     public void addListener(String containerName, String cacheName,
398             IGetUpdates<?, ?> u) throws CacheListenerAddException {
399         EmbeddedCacheManager manager = this.cm;
400         Cache<Object,Object> c;
401         String realCacheName = "{" + containerName + "}_{" + cacheName + "}";
402         if (manager == null) {
403             return;
404         }
405
406         if (!manager.cacheExists(realCacheName)) {
407             throw new CacheListenerAddException();
408         }
409         c = manager.getCache(realCacheName);
410         CacheListenerContainer cl = new CacheListenerContainer(u,
411                 containerName, cacheName);
412         c.addListener(cl);
413     }
414
415     @Override
416     public Set<IGetUpdates<?, ?>> getListeners(String containerName,
417             String cacheName) {
418         EmbeddedCacheManager manager = this.cm;
419         Cache<Object,Object> c;
420         String realCacheName = "{" + containerName + "}_{" + cacheName + "}";
421         if (manager == null) {
422             return null;
423         }
424
425         if (!manager.cacheExists(realCacheName)) {
426             return null;
427         }
428         c = manager.getCache(realCacheName);
429
430         Set<IGetUpdates<?, ?>> res = new HashSet<IGetUpdates<?, ?>>();
431         Set<Object> listeners = c.getListeners();
432         for (Object listener : listeners) {
433             if (listener instanceof CacheListenerContainer) {
434                 CacheListenerContainer cl = (CacheListenerContainer) listener;
435                 res.add(cl.whichListener());
436             }
437         }
438
439         return res;
440     }
441
442     @Override
443     public void removeListener(String containerName, String cacheName,
444             IGetUpdates<?, ?> u) {
445         EmbeddedCacheManager manager = this.cm;
446         Cache<Object,Object> c;
447         String realCacheName = "{" + containerName + "}_{" + cacheName + "}";
448         if (manager == null) {
449             return;
450         }
451
452         if (!manager.cacheExists(realCacheName)) {
453             return;
454         }
455         c = manager.getCache(realCacheName);
456
457         Set<Object> listeners = c.getListeners();
458         for (Object listener : listeners) {
459             if (listener instanceof CacheListenerContainer) {
460                 CacheListenerContainer cl = (CacheListenerContainer) listener;
461                 if (cl.whichListener() == u) {
462                     c.removeListener(listener);
463                     return;
464                 }
465             }
466         }
467     }
468
469     @Override
470     public void tbegin() throws NotSupportedException, SystemException {
471         EmbeddedCacheManager manager = this.cm;
472         if (manager == null) {
473             throw new IllegalStateException();
474         }
475         TransactionManager tm = manager.getCache("transactional-type")
476                 .getAdvancedCache().getTransactionManager();
477         if (tm == null) {
478             throw new IllegalStateException();
479         }
480         tm.begin();
481     }
482
483     @Override
484     public void tcommit() throws RollbackException, HeuristicMixedException,
485             HeuristicRollbackException, java.lang.SecurityException,
486             java.lang.IllegalStateException, SystemException {
487         EmbeddedCacheManager manager = this.cm;
488         if (manager == null) {
489             throw new IllegalStateException();
490         }
491         TransactionManager tm = manager.getCache("transactional-type")
492                 .getAdvancedCache().getTransactionManager();
493         if (tm == null) {
494             throw new IllegalStateException();
495         }
496         tm.commit();
497     }
498
499     @Override
500     public void trollback() throws java.lang.IllegalStateException,
501             java.lang.SecurityException, SystemException {
502         EmbeddedCacheManager manager = this.cm;
503         if (manager == null) {
504             throw new IllegalStateException();
505         }
506         TransactionManager tm = manager.getCache("transactional-type")
507                 .getAdvancedCache().getTransactionManager();
508         if (tm == null) {
509             throw new IllegalStateException();
510         }
511         tm.rollback();
512     }
513
514     @Override
515     public Transaction tgetTransaction() throws SystemException {
516         EmbeddedCacheManager manager = this.cm;
517         if (manager == null) {
518             throw new IllegalStateException();
519         }
520         TransactionManager tm = manager.getCache("transactional-type")
521                 .getAdvancedCache().getTransactionManager();
522         if (tm == null) {
523             return null;
524         }
525         return tm.getTransaction();
526     }
527
528     @Override
529     public boolean amIStandby() {
530         EmbeddedCacheManager manager = this.cm;
531         if (manager == null) {
532             // In case we cannot fetch the information, lets assume we
533             // are standby, so to have less responsibility.
534             return true;
535         }
536         return (!manager.isCoordinator());
537     }
538
539     private InetAddress addressToInetAddress(Address a) {
540         EmbeddedCacheManager manager = this.cm;
541         if ((manager == null) || (a == null)) {
542             // In case we cannot fetch the information, lets assume we
543             // are standby, so to have less responsibility.
544             return null;
545         }
546         Transport t = manager.getTransport();
547         if (t instanceof JGroupsTransport) {
548             JGroupsTransport jt = (JGroupsTransport) t;
549             Channel c = jt.getChannel();
550             if (a instanceof JGroupsAddress) {
551                 JGroupsAddress ja = (JGroupsAddress) a;
552                 org.jgroups.Address phys = (org.jgroups.Address) c
553                         .down(new Event(Event.GET_PHYSICAL_ADDRESS, ja
554                                 .getJGroupsAddress()));
555                 if (phys instanceof org.jgroups.stack.IpAddress) {
556                     InetAddress bindAddress = ((org.jgroups.stack.IpAddress) phys)
557                             .getIpAddress();
558                     return bindAddress;
559                 }
560             }
561         }
562         return null;
563     }
564
565     @Override
566     public List<InetAddress> getClusteredControllers() {
567         EmbeddedCacheManager manager = this.cm;
568         if (manager == null) {
569             return null;
570         }
571         List<Address> controllers = manager.getMembers();
572         if ((controllers == null) || controllers.size() == 0) {
573             return null;
574         }
575
576         List<InetAddress> clusteredControllers = new ArrayList<InetAddress>();
577         for (Address a : controllers) {
578             InetAddress inetAddress = addressToInetAddress(a);
579             if (inetAddress != null
580                     && !inetAddress.getHostAddress().equals(loopbackAddress)) {
581                 clusteredControllers.add(inetAddress);
582             }
583         }
584         return clusteredControllers;
585     }
586
587     @Override
588     public InetAddress getMyAddress() {
589         EmbeddedCacheManager manager = this.cm;
590         if (manager == null) {
591             return null;
592         }
593         return addressToInetAddress(manager.getAddress());
594     }
595
596     @Override
597     public InetAddress getActiveAddress() {
598         EmbeddedCacheManager manager = this.cm;
599         if (manager == null) {
600             // In case we cannot fetch the information, lets assume we
601             // are standby, so to have less responsibility.
602             return null;
603         }
604
605         return addressToInetAddress(manager.getCoordinator());
606     }
607
608     @Override
609     public void listenRoleChange(IListenRoleChange i)
610             throws ListenRoleChangeAddException {
611         EmbeddedCacheManager manager = this.cm;
612         if (manager == null) {
613             // In case we cannot fetch the information, lets assume we
614             // are standby, so to have less responsibility.
615             throw new ListenRoleChangeAddException();
616         }
617
618         if (this.roleChangeListeners == null) {
619             this.roleChangeListeners = new HashSet<IListenRoleChange>();
620             this.cacheManagerListener = new ViewChangedListener(
621                     this.roleChangeListeners);
622             manager.addListener(this.cacheManagerListener);
623         }
624
625         if (this.roleChangeListeners != null) {
626             this.roleChangeListeners.add(i);
627         }
628     }
629
630     @Override
631     public void unlistenRoleChange(IListenRoleChange i) {
632         EmbeddedCacheManager manager = this.cm;
633         if (manager == null) {
634             // In case we cannot fetch the information, lets assume we
635             // are standby, so to have less responsibility.
636             return;
637         }
638
639         if (this.roleChangeListeners != null) {
640             this.roleChangeListeners.remove(i);
641         }
642
643         if ((this.roleChangeListeners != null && this.roleChangeListeners
644                 .isEmpty())
645                 && (this.cacheManagerListener != null)) {
646             manager.removeListener(this.cacheManagerListener);
647             this.cacheManagerListener = null;
648             this.roleChangeListeners = null;
649         }
650     }
651
652     @Listener
653     public class ViewChangedListener {
654         Set<IListenRoleChange> roleListeners;
655
656         public ViewChangedListener(Set<IListenRoleChange> s) {
657             this.roleListeners = s;
658         }
659
660         @ViewChanged
661         public void viewChanged(ViewChangedEvent e) {
662             for (IListenRoleChange i : this.roleListeners) {
663                 i.newActiveAvailable();
664             }
665         }
666     }
667
668     private void removeContainerCaches(String containerName) {
669         logger.info("Destroying caches for container {}", containerName);
670         for (String cacheName : this.getCacheList(containerName)) {
671             this.destroyCache(containerName, cacheName);
672         }
673     }
674
675     @Override
676     public void containerCreate(String arg0) {
677         // no op
678     }
679
680     @Override
681     public void containerDestroy(String container) {
682         removeContainerCaches(container);
683     }
684 }