From 98994e34af0b3dac710e65b32587df33cfbe7e2e Mon Sep 17 00:00:00 2001
From: Lei Xu <x.rayyle@gmail.com>
Date: Tue, 1 Dec 2015 19:01:41 -0600
Subject: [PATCH] fix concurrency flaws in LoadBalancer Module

---
 .../loadbalancer/LoadBalancer.java            | 26 ++++++++++++++-----
 1 file changed, 20 insertions(+), 6 deletions(-)

diff --git a/src/main/java/net/floodlightcontroller/loadbalancer/LoadBalancer.java b/src/main/java/net/floodlightcontroller/loadbalancer/LoadBalancer.java
index f77336b62..2a20c8ace 100644
--- a/src/main/java/net/floodlightcontroller/loadbalancer/LoadBalancer.java
+++ b/src/main/java/net/floodlightcontroller/loadbalancer/LoadBalancer.java
@@ -226,13 +226,13 @@ public class LoadBalancer implements IFloodlightModule,
                     }
                     
                     LBVip vip = vips.get(vipIpToId.get(destIpAddress));
-                    if (vip == null)			// fix deference violations           
+                    if (vip == null)			// fix dereference violations           
                     	return Command.CONTINUE;
                     LBPool pool = pools.get(vip.pickPool(client));
-                    if (pool == null)			// fix deference violations
+                    if (pool == null)			// fix dereference violations
                     	return Command.CONTINUE;
                     LBMember member = members.get(pool.pickMember(client));
-                    if(member == null)			//fix deference violations
+                    if(member == null)			//fix dereference violations
                     	return Command.CONTINUE;
                     
                     // for chosen member, check device manager and find and push routes, in both directions                    
@@ -511,6 +511,7 @@ public class LoadBalancer implements IFloodlightModule,
                fmb.setCookie(U64.of(0));  
                fmb.setPriority(FlowModUtils.PRIORITY_MAX);
                
+               
                if (inBound) {
                    entryName = "inbound-vip-"+ member.vipId+"-client-"+client.ipAddress
                 		   +"-srcport-"+client.srcPort+"-dstport-"+client.targetPort
@@ -543,7 +544,13 @@ public class LoadBalancer implements IFloodlightModule,
                     	   actions.add(pinSwitch.getOFFactory().actions().output(path.get(i+1).getPortId(), Integer.MAX_VALUE));
                        }
                    } else {
-                	   actions.add(switchService.getSwitch(path.get(i+1).getNodeId()).getOFFactory().actions().output(path.get(i+1).getPortId(), Integer.MAX_VALUE));
+                	   //fix concurrency errors
+                	   try{
+                		   actions.add(switchService.getSwitch(path.get(i+1).getNodeId()).getOFFactory().actions().output(path.get(i+1).getPortId(), Integer.MAX_VALUE));
+                	   }
+                	   catch(NullPointerException e){
+                		   log.error("Fail to install loadbalancer flow rules to offline switch {}.", path.get(i+1).getNodeId());
+                	   }
                    }
                } else {
                    entryName = "outbound-vip-"+ member.vipId+"-client-"+client.ipAddress
@@ -576,11 +583,18 @@ public class LoadBalancer implements IFloodlightModule,
                     	   actions.add(pinSwitch.getOFFactory().actions().output(path.get(i+1).getPortId(), Integer.MAX_VALUE));
                        }
                    } else {
-                	   actions.add(switchService.getSwitch(path.get(i+1).getNodeId()).getOFFactory().actions().output(path.get(i+1).getPortId(), Integer.MAX_VALUE));
+                	   //fix concurrency errors
+                	   try{
+                		   actions.add(switchService.getSwitch(path.get(i+1).getNodeId()).getOFFactory().actions().output(path.get(i+1).getPortId(), Integer.MAX_VALUE));
+                	   }
+                	   catch(NullPointerException e){
+                		   log.error("Fail to install loadbalancer flow rules to offline switches {}.", path.get(i+1).getNodeId());
+                	   }
                    }
                    
                }
                
+               
                fmb.setActions(actions);
                fmb.setPriority(U16.t(LB_PRIORITY));
                fmb.setMatch(mb.build());
@@ -670,7 +684,7 @@ public class LoadBalancer implements IFloodlightModule,
         LBPool pool;
         if (pools != null) {
             pool = pools.get(poolId);
-            if (pool == null)	// fix deference violations
+            if (pool == null)	// fix dereference violations
             	return -1;
             if (pool.vipId != null)
                 vips.get(pool.vipId).pools.remove(poolId);
-- 
GitLab