-
Notifications
You must be signed in to change notification settings - Fork 73
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #14924: Cleanup unreferenced software #2231
Merged
fanf
merged 4 commits into
Normation:branches/rudder/5.0
from
ncharles:bug_14924/cleanup_unreferenced_software
Jun 12, 2019
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
...ventory-repository/src/main/scala/com/normation/inventory/ldap/core/SoftwareService.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.normation.inventory.ldap.core | ||
|
||
|
||
import com.normation.inventory.services.core.{ReadOnlySoftwareDAO, WriteOnlySoftwareDAO} | ||
import net.liftweb.common.Loggable | ||
import net.liftweb.common._ | ||
|
||
|
||
trait SoftwareService { | ||
def deleteUnreferencedSoftware() : Box[Seq[String]] | ||
} | ||
|
||
class SoftwareServiceImpl( | ||
readOnlySoftware : ReadOnlySoftwareDAO | ||
, writeOnlySoftware : WriteOnlySoftwareDAO) | ||
extends SoftwareService with Loggable { | ||
|
||
/** Delete all unreferenced softwares | ||
* First search in software, and then in nodes, so that if a node arrives in between (new inventory) | ||
* its software wont be deleted | ||
*/ | ||
def deleteUnreferencedSoftware() : Box[Seq[String]] = { | ||
val t1 = System.currentTimeMillis | ||
for { | ||
allSoftwares <- readOnlySoftware.getAllSoftwareIds() | ||
t2 = System.currentTimeMillis() | ||
_ = logger.debug(s"All softwares id in ou=software fetched: ${allSoftwares.size} softwares id in ${t2-t1}ms") | ||
|
||
allNodesSoftwares <- readOnlySoftware.getSoftwaresForAllNodes() | ||
t3 = System.currentTimeMillis() | ||
_ = logger.debug(s"All softwares id in nodes fetched: ${allNodesSoftwares.size} softwares id in ${t3-t2}ms") | ||
|
||
|
||
extraSoftware = allSoftwares -- allNodesSoftwares | ||
_ = logger.debug(s"Found ${extraSoftware.size} unreferenced software in ou=software, going to delete them") | ||
|
||
deletedSoftware <- writeOnlySoftware.deleteSoftwares(extraSoftware.toSeq) | ||
t4 = System.currentTimeMillis() | ||
_ = logger.debug(s"Deleted ${deletedSoftware.size} software in ${t4-t3}ms") | ||
|
||
|
||
} yield { | ||
deletedSoftware.map(x => x.toString).toSeq | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
...er/rudder-core/src/main/scala/com/normation/rudder/batch/PurgeUnreferencedSoftwares.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
************************************************************************************* | ||
* Copyright 2017 Normation SAS | ||
************************************************************************************* | ||
* | ||
* This file is part of Rudder. | ||
* | ||
* Rudder is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* In accordance with the terms of section 7 (7. Additional Terms.) of | ||
* the GNU General Public License version 3, the copyright holders add | ||
* the following Additional permissions: | ||
* Notwithstanding to the terms of section 5 (5. Conveying Modified Source | ||
* Versions) and 6 (6. Conveying Non-Source Forms.) of the GNU General | ||
* Public License version 3, when you create a Related Module, this | ||
* Related Module is not considered as a part of the work and may be | ||
* distributed under the license agreement of your choice. | ||
* A "Related Module" means a set of sources files including their | ||
* documentation that, without modification of the Source Code, enables | ||
* supplementary functions or services in addition to those offered by | ||
* the Software. | ||
* | ||
* Rudder is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Rudder. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
* | ||
************************************************************************************* | ||
*/ | ||
|
||
package com.normation.rudder.batch | ||
|
||
|
||
import com.normation.rudder.domain.logger.ScheduledJobLogger | ||
import com.normation.inventory.ldap.core.SoftwareService | ||
import monix.execution.Scheduler.{global => scheduler} | ||
import net.liftweb.common._ | ||
|
||
import scala.concurrent.duration._ | ||
|
||
|
||
/** | ||
* A naive scheduler which checks every updateInterval if software needs to be deleted | ||
*/ | ||
class PurgeUnreferencedSoftwares( | ||
softwareService : SoftwareService | ||
, updateInterval : FiniteDuration | ||
) { | ||
|
||
val logger = ScheduledJobLogger | ||
|
||
|
||
if (updateInterval < 1.hour) { | ||
logger.info(s"Disable automatic purge of unreferenced softwares (update interval cannot be less than 1 hour)") | ||
} else { | ||
logger.debug(s"***** starting batch that purge unreferenced softwares, every ${updateInterval.toString()} *****") | ||
scheduler.scheduleWithFixedDelay(1.hour, updateInterval) { | ||
softwareService.deleteUnreferencedSoftware() match { | ||
case Full(softwares) => | ||
logger.info(s"Purged ${softwares.length} unreferenced softwares") | ||
if (logger.isDebugEnabled && softwares.length > 0) | ||
logger.debug(s"Purged following software: ${softwares.mkString(",")}") | ||
case e: EmptyBox => | ||
val error = (e ?~! s"Error when deleting unreferenced softwares") | ||
logger.error(error.messageChain) | ||
error.rootExceptionCause.foreach(ex => | ||
logger.error("Exception was:", ex) | ||
) | ||
} | ||
} | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this ain't pretty, yet its more efficient
[2019-05-23 13:00:06] DEBUG com.normation.inventory.ldap.core.SoftwareServiceImpl - All softwares id in nodes fetched: 37953 softwares id in 10029ms, compared to what is in the TODO below
[2019-05-23 12:55:17] DEBUG com.normation.inventory.ldap.core.SoftwareServiceImpl - All softwares id in nodes fetched: 37953 softwares id in 15197ms
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this code is surely non idiomatic, and i fail to improve it