6
6
import org .zstack .core .cloudbus .CloudBusCallBack ;
7
7
import org .zstack .core .cloudbus .CloudBusListCallBack ;
8
8
import org .zstack .core .componentloader .PluginRegistry ;
9
+ import org .zstack .core .db .Q ;
9
10
import org .zstack .core .db .SimpleQuery ;
10
11
import org .zstack .core .db .SimpleQuery .Op ;
11
12
import org .zstack .core .db .UpdateQuery ;
36
37
import org .zstack .header .storage .snapshot .VolumeSnapshotInventory ;
37
38
import org .zstack .header .storage .snapshot .VolumeSnapshotVO ;
38
39
import org .zstack .header .storage .snapshot .VolumeSnapshotVO_ ;
39
- import org .zstack .header .volume .VolumeConstant ;
40
- import org .zstack .header .volume .VolumeInventory ;
41
- import org .zstack .header .volume .VolumeVO ;
42
- import org .zstack .header .volume .VolumeVO_ ;
40
+ import org .zstack .header .volume .*;
43
41
import org .zstack .storage .primary .PrimaryStorageBase ;
44
42
import org .zstack .storage .primary .PrimaryStorageCapacityUpdater ;
45
43
import org .zstack .storage .primary .PrimaryStoragePhysicalCapacityManager ;
@@ -1057,36 +1055,32 @@ public void fail(ErrorCode errorCode) {
1057
1055
});
1058
1056
}
1059
1057
1060
- @ Transactional
1061
1058
private void handle (RemoveHostFromLocalStorageMsg msg ) {
1062
- String sqlLocalStorageHostRefVO = "select ref" +
1063
- " from LocalStorageHostRefVO ref" +
1064
- " where hostUuid = :hostUuid" +
1065
- " and primaryStorageUuid = :primaryStorageUuid" ;
1066
- TypedQuery <LocalStorageHostRefVO > query = dbf .getEntityManager ().
1067
- createQuery (sqlLocalStorageHostRefVO , LocalStorageHostRefVO .class );
1068
- query .setParameter ("hostUuid" , msg .getHostUuid ());
1069
- query .setParameter ("primaryStorageUuid" , msg .getPrimaryStorageUuid ());
1070
- LocalStorageHostRefVO ref = query .getSingleResult ();
1071
- if (ref == null ) {
1072
- return ;
1059
+ LocalStorageHostRefVO ref = Q .New (LocalStorageHostRefVO .class )
1060
+ .eq (LocalStorageHostRefVO_ .hostUuid , msg .getHostUuid ())
1061
+ .eq (LocalStorageHostRefVO_ .primaryStorageUuid , self .getUuid ())
1062
+ .find ();
1063
+
1064
+ if (ref != null ) {
1065
+ // on remove, subtract the capacity from every capacity
1066
+ decreaseCapacity (ref .getTotalCapacity (),
1067
+ ref .getAvailableCapacity (),
1068
+ ref .getTotalPhysicalCapacity (),
1069
+ ref .getAvailablePhysicalCapacity (),
1070
+ ref .getSystemUsedCapacity ());
1071
+
1072
+ dbf .remove (ref );
1073
1073
}
1074
- dbf .remove (ref );
1075
1074
1076
1075
deleteResourceRef (msg .getHostUuid ());
1077
1076
1078
- // on remove, subtract the capacity from every capacity
1079
- decreaseCapacity (ref .getTotalCapacity (),
1080
- ref .getAvailableCapacity (),
1081
- ref .getTotalPhysicalCapacity (),
1082
- ref .getAvailablePhysicalCapacity (),
1083
- ref .getSystemUsedCapacity ());
1084
1077
bus .reply (msg , new RemoveHostFromLocalStorageReply ());
1085
1078
}
1086
1079
1087
1080
void deleteResourceRef (String hostUuid ) {
1088
1081
SimpleQuery <LocalStorageResourceRefVO > rq = dbf .createQuery (LocalStorageResourceRefVO .class );
1089
1082
rq .add (LocalStorageResourceRefVO_ .hostUuid , Op .EQ , hostUuid );
1083
+ rq .add (LocalStorageResourceRefVO_ .primaryStorageUuid , Op .EQ , self .getUuid ());
1090
1084
List <LocalStorageResourceRefVO > refs = rq .list ();
1091
1085
if (refs .isEmpty ()) {
1092
1086
return ;
@@ -1111,6 +1105,20 @@ void deleteResourceRef(String hostUuid) {
1111
1105
1112
1106
// delete volumes
1113
1107
if (!volumes .isEmpty ()) {
1108
+ new Runnable () {
1109
+ @ Override
1110
+ @ Transactional
1111
+ public void run () {
1112
+ // delete VMs on the host
1113
+ String sql = "delete from VmInstanceVO vm where vm.rootVolumeUuid in (select vol.uuid from VolumeVO vol" +
1114
+ " where vol.uuid in (:volUuids) and vol.type = :volType)" ;
1115
+ Query q = dbf .getEntityManager ().createQuery (sql );
1116
+ q .setParameter ("volType" , VolumeType .Root );
1117
+ q .setParameter ("volUuids" , volumes );
1118
+ q .executeUpdate ();
1119
+ }
1120
+ }.run ();
1121
+
1114
1122
uq = UpdateQuery .New ();
1115
1123
uq .entity (VolumeVO .class );
1116
1124
uq .condAnd (VolumeVO_ .uuid , Op .IN , volumes );
@@ -1136,19 +1144,15 @@ protected void handle(final InitPrimaryStorageOnHostConnectedMsg msg) {
1136
1144
final InitPrimaryStorageOnHostConnectedReply reply = new InitPrimaryStorageOnHostConnectedReply ();
1137
1145
LocalStorageHypervisorFactory f = getHypervisorBackendFactoryByHostUuid (msg .getHostUuid ());
1138
1146
final LocalStorageHypervisorBackend bkd = f .getHypervisorBackend (self );
1147
+
1139
1148
bkd .handle (msg , new ReturnValueCompletion <PhysicalCapacityUsage >(msg ) {
1140
1149
@ Override
1141
- @ Transactional
1142
1150
public void success (PhysicalCapacityUsage c ) {
1143
- String sqlLocalStorageHostRefVO = "select ref" +
1144
- " from LocalStorageHostRefVO ref" +
1145
- " where hostUuid = :hostUuid" +
1146
- " and primaryStorageUuid = :primaryStorageUuid" ;
1147
- TypedQuery <LocalStorageHostRefVO > query = dbf .getEntityManager ().
1148
- createQuery (sqlLocalStorageHostRefVO , LocalStorageHostRefVO .class );
1149
- query .setParameter ("hostUuid" , msg .getHostUuid ());
1150
- query .setParameter ("primaryStorageUuid" , msg .getPrimaryStorageUuid ());
1151
- List <LocalStorageHostRefVO > refs = query .getResultList ();
1151
+ List <LocalStorageHostRefVO > refs = Q .New (LocalStorageHostRefVO .class )
1152
+ .eq (LocalStorageHostRefVO_ .hostUuid , msg .getHostUuid ())
1153
+ .eq (LocalStorageHostRefVO_ .primaryStorageUuid , self .getUuid ())
1154
+ .list ();
1155
+
1152
1156
LocalStorageHostRefVO ref ;
1153
1157
if (refs == null || refs .isEmpty ()) {
1154
1158
ref = new LocalStorageHostRefVO ();
0 commit comments