2020import java .net .URI ;
2121import java .net .URISyntaxException ;
2222import java .net .UnknownHostException ;
23+ import java .sql .PreparedStatement ;
24+ import java .sql .ResultSet ;
2325import java .util .ArrayList ;
2426import java .util .Collection ;
2527import java .util .Collections ;
4547import com .cloud .agent .AgentManager ;
4648import com .cloud .agent .api .Answer ;
4749import com .cloud .agent .api .BackupSnapshotCommand ;
50+ import com .cloud .agent .api .CleanupSnapshotBackupCommand ;
4851import com .cloud .agent .api .Command ;
4952import com .cloud .agent .api .CreateStoragePoolCommand ;
5053import com .cloud .agent .api .CreateVolumeFromSnapshotAnswer ;
@@ -1102,7 +1105,9 @@ public String getName() {
11021105 @ Override
11031106 public boolean start () {
11041107 if (_storageCleanupEnabled ) {
1105- _executor .scheduleWithFixedDelay (new StorageGarbageCollector (), _storageCleanupInterval , _storageCleanupInterval , TimeUnit .SECONDS );
1108+ Random generator = new Random ();
1109+ int initialDelay = generator .nextInt (_storageCleanupInterval );
1110+ _executor .scheduleWithFixedDelay (new StorageGarbageCollector (), initialDelay , _storageCleanupInterval , TimeUnit .SECONDS );
11061111 } else {
11071112 s_logger .debug ("Storage cleanup is not enabled, so the storage cleanup thread is not being scheduled." );
11081113 }
@@ -1978,46 +1983,8 @@ public void cleanupStorage(boolean recurring) {
19781983 }
19791984 }
19801985 }
1981-
1982- // Cleanup secondary storage hosts
1983- List <HostVO > secondaryStorageHosts = _ssvmMgr .listSecondaryStorageHostsInAllZones ();
1984- for (HostVO secondaryStorageHost : secondaryStorageHosts ) {
1985- try {
1986- long hostId = secondaryStorageHost .getId ();
1987- List <VMTemplateHostVO > destroyedTemplateHostVOs = _vmTemplateHostDao .listDestroyed (hostId );
1988- s_logger .debug ("Secondary storage garbage collector found " + destroyedTemplateHostVOs .size () + " templates to cleanup on secondary storage host: "
1989- + secondaryStorageHost .getName ());
1990- for (VMTemplateHostVO destroyedTemplateHostVO : destroyedTemplateHostVOs ) {
1991- if (!_tmpltMgr .templateIsDeleteable (destroyedTemplateHostVO )) {
1992- if (s_logger .isDebugEnabled ()) {
1993- s_logger .debug ("Not deleting template at: " + destroyedTemplateHostVO );
1994- }
1995- continue ;
1996- }
1997-
1998- if (s_logger .isDebugEnabled ()) {
1999- s_logger .debug ("Deleting template host: " + destroyedTemplateHostVO );
2000- }
2001-
2002- String installPath = destroyedTemplateHostVO .getInstallPath ();
2003-
2004- if (installPath != null ) {
2005- Answer answer = _agentMgr .sendToSecStorage (secondaryStorageHost , new DeleteTemplateCommand (secondaryStorageHost .getStorageUrl (),destroyedTemplateHostVO .getInstallPath ()));
2006-
2007- if (answer == null || !answer .getResult ()) {
2008- s_logger .debug ("Failed to delete " + destroyedTemplateHostVO + " due to " + ((answer == null ) ? "answer is null" : answer .getDetails ()));
2009- } else {
2010- _vmTemplateHostDao .remove (destroyedTemplateHostVO .getId ());
2011- s_logger .debug ("Deleted template at: " + destroyedTemplateHostVO .getInstallPath ());
2012- }
2013- } else {
2014- _vmTemplateHostDao .remove (destroyedTemplateHostVO .getId ());
2015- }
2016- }
2017- } catch (Exception e ) {
2018- s_logger .warn ("problem cleaning up secondary storage " + secondaryStorageHost , e );
2019- }
2020- }
1986+
1987+ cleanupSecondaryStorage (recurring );
20211988
20221989 List <VolumeVO > vols = _volsDao .listVolumesToBeDestroyed ();
20231990 for (VolumeVO vol : vols ) {
@@ -2035,6 +2002,142 @@ public void cleanupStorage(boolean recurring) {
20352002 scanLock .releaseRef ();
20362003 }
20372004 }
2005+
2006+ @ DB
2007+ List <Long > findAllVolumeIdInSnapshotTable (Long hostId ) {
2008+ String sql = "SELECT volume_id from snapshots WHERE sechost_id=? GROUP BY volume_id" ;
2009+ List <Long > list = new ArrayList <Long >();
2010+ try {
2011+ Transaction txn = Transaction .currentTxn ();
2012+ ResultSet rs = null ;
2013+ PreparedStatement pstmt = null ;
2014+ pstmt = txn .prepareAutoCloseStatement (sql );
2015+ pstmt .setLong (1 , hostId );
2016+ rs = pstmt .executeQuery ();
2017+ while (rs .next ()) {
2018+ list .add (rs .getLong (1 ));
2019+ }
2020+ return list ;
2021+ } catch (Exception e ) {
2022+ s_logger .debug ("failed to get all volumes who has snapshots in secondary storage " + hostId + " due to " + e .getMessage ());
2023+ return null ;
2024+ }
2025+
2026+ }
2027+
2028+
2029+ List <String > findAllSnapshotForVolume (Long volumeId ) {
2030+ String sql = "SELECT backup_snap_id FROM snapshots WHERE volume_id=? and backup_snap_id is not NULL" ;
2031+ try {
2032+ Transaction txn = Transaction .currentTxn ();
2033+ ResultSet rs = null ;
2034+ PreparedStatement pstmt = null ;
2035+ pstmt = txn .prepareAutoCloseStatement (sql );
2036+ pstmt .setLong (1 , volumeId );
2037+ rs = pstmt .executeQuery ();
2038+ List <String > list = new ArrayList <String >();
2039+ while (rs .next ()) {
2040+ list .add (rs .getString (1 ));
2041+ }
2042+ return list ;
2043+ } catch (Exception e ) {
2044+ s_logger .debug ("failed to get all snapshots for a volume " + volumeId + " due to " + e .getMessage ());
2045+ return null ;
2046+ }
2047+ }
2048+
2049+ @ Override
2050+ @ DB
2051+ public void cleanupSecondaryStorage (boolean recurring ) {
2052+ try {
2053+ // Cleanup templates in secondary storage hosts
2054+ List <HostVO > secondaryStorageHosts = _hostDao .listSecondaryStorageHosts ();
2055+ for (HostVO secondaryStorageHost : secondaryStorageHosts ) {
2056+ try {
2057+ long hostId = secondaryStorageHost .getId ();
2058+ List <VMTemplateHostVO > destroyedTemplateHostVOs = _vmTemplateHostDao .listDestroyed (hostId );
2059+ s_logger .debug ("Secondary storage garbage collector found " + destroyedTemplateHostVOs .size () + " templates to cleanup on secondary storage host: "
2060+ + secondaryStorageHost .getName ());
2061+ for (VMTemplateHostVO destroyedTemplateHostVO : destroyedTemplateHostVOs ) {
2062+ if (!_tmpltMgr .templateIsDeleteable (destroyedTemplateHostVO )) {
2063+ if (s_logger .isDebugEnabled ()) {
2064+ s_logger .debug ("Not deleting template at: " + destroyedTemplateHostVO );
2065+ }
2066+ continue ;
2067+ }
2068+
2069+ if (s_logger .isDebugEnabled ()) {
2070+ s_logger .debug ("Deleting template host: " + destroyedTemplateHostVO );
2071+ }
2072+
2073+ String installPath = destroyedTemplateHostVO .getInstallPath ();
2074+
2075+ if (installPath != null ) {
2076+ Answer answer = _agentMgr .sendToSecStorage (secondaryStorageHost , new DeleteTemplateCommand (secondaryStorageHost .getStorageUrl (), destroyedTemplateHostVO .getInstallPath ()));
2077+
2078+ if (answer == null || !answer .getResult ()) {
2079+ s_logger .debug ("Failed to delete " + destroyedTemplateHostVO + " due to " + ((answer == null ) ? "answer is null" : answer .getDetails ()));
2080+ } else {
2081+ _vmTemplateHostDao .remove (destroyedTemplateHostVO .getId ());
2082+ s_logger .debug ("Deleted template at: " + destroyedTemplateHostVO .getInstallPath ());
2083+ }
2084+ } else {
2085+ _vmTemplateHostDao .remove (destroyedTemplateHostVO .getId ());
2086+ }
2087+ }
2088+ } catch (Exception e ) {
2089+ s_logger .warn ("problem cleaning up templates in secondary storage " + secondaryStorageHost , e );
2090+ }
2091+ }
2092+
2093+ // Cleanup snapshot in secondary storage hosts
2094+ for (HostVO secondaryStorageHost : secondaryStorageHosts ) {
2095+ try {
2096+ long hostId = secondaryStorageHost .getId ();
2097+ List <Long > vIDs = findAllVolumeIdInSnapshotTable (hostId );
2098+ if (vIDs == null ) {
2099+ continue ;
2100+ }
2101+ for (Long volumeId : vIDs ) {
2102+ boolean lock = false ;
2103+ try {
2104+ VolumeVO volume = _volsDao .findByIdIncludingRemoved (volumeId );
2105+ if (volume .getRemoved () == null ) {
2106+ volume = _volsDao .acquireInLockTable (volumeId , 10 );
2107+ if (volume == null ) {
2108+ continue ;
2109+ }
2110+ lock = true ;
2111+ }
2112+ List <String > snapshots = findAllSnapshotForVolume (volumeId );
2113+ if (snapshots == null ) {
2114+ continue ;
2115+ }
2116+ CleanupSnapshotBackupCommand cmd = new CleanupSnapshotBackupCommand (secondaryStorageHost .getStorageUrl (), secondaryStorageHost .getDataCenterId (), volume .getAccountId (),
2117+ volumeId , snapshots );
2118+
2119+ Answer answer = _agentMgr .sendToSecStorage (secondaryStorageHost , cmd );
2120+ if ((answer == null ) || !answer .getResult ()) {
2121+ String details = "Failed to cleanup snapshots for volume " + volumeId + " due to " + (answer == null ? "null" : answer .getDetails ());
2122+ s_logger .warn (details );
2123+ }
2124+ } catch (Exception e1 ) {
2125+ s_logger .warn ("problem cleaning up snapshots in secondary storage " + secondaryStorageHost , e1 );
2126+ } finally {
2127+ if (lock ) {
2128+ _volsDao .releaseFromLockTable (volumeId );
2129+ }
2130+ }
2131+ }
2132+ } catch (Exception e2 ) {
2133+ s_logger .warn ("problem cleaning up snapshots in secondary storage " + secondaryStorageHost , e2 );
2134+ }
2135+ }
2136+
2137+ } catch (Exception e3 ) {
2138+ s_logger .warn ("problem cleaning up secondary storage " , e3 );
2139+ }
2140+ }
20382141
20392142 @ Override
20402143 public String getPrimaryStorageNameLabel (VolumeVO volume ) {
@@ -3087,6 +3190,7 @@ public void run() {
30873190 }
30883191 }
30893192 }
3193+
30903194
30913195 @ Override
30923196 public void onManagementNodeJoined (List <ManagementServerHostVO > nodeList , long selfNodeId ) {
0 commit comments