3636
3737import com .cloud .cluster .agentlb .HostTransferMapVO ;
3838import com .cloud .cluster .agentlb .dao .HostTransferMapDao ;
39- import com .cloud .cluster .agentlb .dao .HostTransferMapDaoImpl ;
4039import com .cloud .dc .ClusterVO ;
41- import com .cloud .dc .HostPodVO ;
4240import com .cloud .dc .dao .ClusterDao ;
43- import com .cloud .dc .dao .ClusterDaoImpl ;
4441import com .cloud .host .Host ;
4542import com .cloud .host .Host .Type ;
4643import com .cloud .host .HostTagVO ;
@@ -120,12 +117,12 @@ public class HostDaoImpl extends GenericDaoBase<HostVO, Long> implements HostDao
120117 protected GenericSearchBuilder <HostVO , Long > ClustersForHostsNotOwnedByAnyMSSearch ;
121118 protected GenericSearchBuilder <ClusterVO , Long > AllClustersSearch ;
122119 protected SearchBuilder <HostVO > HostsInClusterSearch ;
123-
120+
124121 protected Attribute _statusAttr ;
125122 protected Attribute _resourceStateAttr ;
126123 protected Attribute _msIdAttr ;
127124 protected Attribute _pingTimeAttr ;
128-
125+
129126 @ Inject protected HostDetailsDao _detailsDao ;
130127 @ Inject protected HostTagsDao _hostTagsDao ;
131128 @ Inject protected HostTransferMapDao _hostTransferDao ;
@@ -163,7 +160,7 @@ public void init() {
163160 TypeDcSearch .and ("type" , TypeDcSearch .entity ().getType (), SearchCriteria .Op .EQ );
164161 TypeDcSearch .and ("dc" , TypeDcSearch .entity ().getDataCenterId (), SearchCriteria .Op .EQ );
165162 TypeDcSearch .done ();
166-
163+
167164 SecondaryStorageVMSearch = createSearchBuilder ();
168165 SecondaryStorageVMSearch .and ("type" , SecondaryStorageVMSearch .entity ().getType (), SearchCriteria .Op .EQ );
169166 SecondaryStorageVMSearch .and ("dc" , SecondaryStorageVMSearch .entity ().getDataCenterId (), SearchCriteria .Op .EQ );
@@ -236,7 +233,7 @@ public void init() {
236233 StatusSearch = createSearchBuilder ();
237234 StatusSearch .and ("status" , StatusSearch .entity ().getStatus (), SearchCriteria .Op .IN );
238235 StatusSearch .done ();
239-
236+
240237 ResourceStateSearch = createSearchBuilder ();
241238 ResourceStateSearch .and ("resourceState" , ResourceStateSearch .entity ().getResourceState (), SearchCriteria .Op .IN );
242239 ResourceStateSearch .done ();
@@ -284,7 +281,7 @@ public void init() {
284281 ClusterManagedSearch .and ("managed" , ClusterManagedSearch .entity ().getManagedState (), SearchCriteria .Op .EQ );
285282 UnmanagedDirectConnectSearch .join ("ClusterManagedSearch" , ClusterManagedSearch , ClusterManagedSearch .entity ().getId (), UnmanagedDirectConnectSearch .entity ().getClusterId (), JoinType .INNER );
286283 UnmanagedDirectConnectSearch .done ();
287-
284+
288285
289286 DirectConnectSearch = createSearchBuilder ();
290287 DirectConnectSearch .and ("resource" , DirectConnectSearch .entity ().getResource (), SearchCriteria .Op .NNULL );
@@ -334,7 +331,7 @@ public void init() {
334331 ManagedRoutingServersSearch .and ("server" , ManagedRoutingServersSearch .entity ().getManagementServerId (), SearchCriteria .Op .NNULL );
335332 ManagedRoutingServersSearch .and ("type" , ManagedRoutingServersSearch .entity ().getType (), SearchCriteria .Op .EQ );
336333 ManagedRoutingServersSearch .done ();
337-
334+
338335 RoutingSearch = createSearchBuilder ();
339336 RoutingSearch .and ("type" , RoutingSearch .entity ().getType (), SearchCriteria .Op .EQ );
340337 RoutingSearch .done ();
@@ -582,20 +579,20 @@ public List<HostVO> findAndUpdateDirectAgentToLoad(long lastPingSecondsAfter, Lo
582579 @ Override @ DB
583580 public List <HostVO > findAndUpdateApplianceToLoad (long lastPingSecondsAfter , long managementServerId ) {
584581 Transaction txn = Transaction .currentTxn ();
585-
582+
586583 txn .start ();
587584 SearchCriteria <HostVO > sc = UnmanagedApplianceSearch .create ();
588585 sc .setParameters ("lastPinged" , lastPingSecondsAfter );
589586 sc .setParameters ("types" , Type .ExternalDhcp , Type .ExternalFirewall , Type .ExternalLoadBalancer , Type .BaremetalDhcp , Type .BaremetalPxe , Type .TrafficMonitor , Type .L2Networking );
590587 List <HostVO > hosts = lockRows (sc , null , true );
591-
588+
592589 for (HostVO host : hosts ) {
593590 host .setManagementServerId (managementServerId );
594591 update (host .getId (), host );
595592 }
596-
593+
597594 txn .commit ();
598-
595+
599596 return hosts ;
600597 }
601598
@@ -622,7 +619,7 @@ public void markHostsAsDisconnected(long msId, long lastPing) {
622619 ub = getUpdateBuilder (host );
623620 update (ub , sc , null );
624621 }
625-
622+
626623 @ Override
627624 public List <HostVO > listByHostTag (Host .Type type , Long clusterId , Long podId , long dcId , String hostTag ) {
628625
@@ -655,8 +652,8 @@ public List<HostVO> listByHostTag(Host.Type type, Long clusterId, Long podId, lo
655652
656653 return listBy (sc );
657654 }
658-
659-
655+
656+
660657 @ Override
661658 public List <HostVO > listAllUpAndEnabledNonHAHosts (Type type , Long clusterId , Long podId , long dcId , String haTag ) {
662659 SearchBuilder <HostTagVO > hostTagSearch = null ;
@@ -666,42 +663,42 @@ public List<HostVO> listAllUpAndEnabledNonHAHosts(Type type, Long clusterId, Lon
666663 hostTagSearch .or ("tagNull" , hostTagSearch .entity ().getTag (), SearchCriteria .Op .NULL );
667664 hostTagSearch .cp ();
668665 }
669-
666+
670667 SearchBuilder <HostVO > hostSearch = createSearchBuilder ();
671-
668+
672669 hostSearch .and ("type" , hostSearch .entity ().getType (), SearchCriteria .Op .EQ );
673670 hostSearch .and ("clusterId" , hostSearch .entity ().getClusterId (), SearchCriteria .Op .EQ );
674671 hostSearch .and ("podId" , hostSearch .entity ().getPodId (), SearchCriteria .Op .EQ );
675672 hostSearch .and ("zoneId" , hostSearch .entity ().getDataCenterId (), SearchCriteria .Op .EQ );
676673 hostSearch .and ("status" , hostSearch .entity ().getStatus (), SearchCriteria .Op .EQ );
677674 hostSearch .and ("resourceState" , hostSearch .entity ().getResourceState (), SearchCriteria .Op .EQ );
678-
675+
679676 if (haTag != null && !haTag .isEmpty ()) {
680677 hostSearch .join ("hostTagSearch" , hostTagSearch , hostSearch .entity ().getId (), hostTagSearch .entity ().getHostId (), JoinBuilder .JoinType .LEFTOUTER );
681678 }
682679
683680 SearchCriteria <HostVO > sc = hostSearch .create ();
684-
681+
685682 if (haTag != null && !haTag .isEmpty ()) {
686683 sc .setJoinParameters ("hostTagSearch" , "tag" , haTag );
687684 }
688-
685+
689686 if (type != null ) {
690687 sc .setParameters ("type" , type );
691688 }
692-
689+
693690 if (clusterId != null ) {
694691 sc .setParameters ("clusterId" , clusterId );
695692 }
696-
693+
697694 if (podId != null ) {
698695 sc .setParameters ("podId" , podId );
699696 }
700-
697+
701698 sc .setParameters ("zoneId" , dcId );
702699 sc .setParameters ("status" , Status .Up );
703700 sc .setParameters ("resourceState" , ResourceState .Enabled );
704-
701+
705702 return listBy (sc );
706703 }
707704
@@ -748,7 +745,7 @@ public List<HostVO> findLostHosts(long timeout) {
748745 }
749746 return result ;
750747 }
751-
748+
752749 @ Override
753750 public void saveDetails (HostVO host ) {
754751 Map <String , String > details = host .getDetails ();
@@ -873,6 +870,15 @@ public long countRoutingHostsByDataCenter(long dcId) {
873870 @ Override
874871 public boolean updateState (Status oldStatus , Event event , Status newStatus , Host vo , Object data ) {
875872 HostVO host = findById (vo .getId ());
873+ if (host == null ){
874+ if (event == Event .Remove && newStatus == Status .Removed ){
875+ host = findByIdIncludingRemoved (vo .getId ());
876+ }
877+ }
878+
879+ if (host == null ){
880+ return false ;
881+ }
876882 long oldPingTime = host .getLastPinged ();
877883
878884 SearchBuilder <HostVO > sb = createSearchBuilder ();
@@ -941,28 +947,28 @@ public boolean updateState(Status oldStatus, Event event, Status newStatus, Host
941947 msg .append ("; new update count = " + newUpdateCount + "]" );
942948 status_logger .debug (msg .toString ());
943949 }
944-
950+
945951 return result > 0 ;
946952 }
947-
953+
948954 @ Override
949955 public boolean updateResourceState (ResourceState oldState , ResourceState .Event event , ResourceState newState , Host vo ) {
950956 HostVO host = (HostVO )vo ;
951957 SearchBuilder <HostVO > sb = createSearchBuilder ();
952958 sb .and ("resource_state" , sb .entity ().getResourceState (), SearchCriteria .Op .EQ );
953959 sb .and ("id" , sb .entity ().getId (), SearchCriteria .Op .EQ );
954960 sb .done ();
955-
961+
956962 SearchCriteria <HostVO > sc = sb .create ();
957963
958964 sc .setParameters ("resource_state" , oldState );
959965 sc .setParameters ("id" , host .getId ());
960-
966+
961967 UpdateBuilder ub = getUpdateBuilder (host );
962968 ub .set (host , _resourceStateAttr , newState );
963969 int result = update (ub , sc , null );
964970 assert result <= 1 : "How can this update " + result + " rows? " ;
965-
971+
966972 if (state_logger .isDebugEnabled () && result == 0 ) {
967973 HostVO ho = findById (host .getId ());
968974 assert ho != null : "How how how? : " + host .getId ();
@@ -983,10 +989,10 @@ public boolean updateResourceState(ResourceState oldState, ResourceState.Event e
983989 msg .append ("; new state = " + newState + "]" );
984990 state_logger .debug (msg .toString ());
985991 }
986-
992+
987993 return result > 0 ;
988994 }
989-
995+
990996 @ Override
991997 public HostVO findByTypeNameAndZoneId (long zoneId , String name , Host .Type type ) {
992998 SearchCriteria <HostVO > sc = TypeNameZoneSearch .create ();
0 commit comments