Skip to content

Commit

Permalink
Refactor hooks: composition over inheritance
Browse files Browse the repository at this point in the history
Add JavaDoc.
Use terminology of "insert" and "call" as per [common terminology].
Repackage hooks.

[common terminology]: https://en.wikipedia.org/wiki/Hooking
  • Loading branch information
dagguh committed Jan 22, 2021
1 parent 26bdabb commit 2742b75
Show file tree
Hide file tree
Showing 43 changed files with 248 additions and 221 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.atlassian.performance.tools.infrastructure.api.database

import com.atlassian.performance.tools.infrastructure.api.Sed
import com.atlassian.performance.tools.infrastructure.api.jira.hook.PostInstallHooks
import com.atlassian.performance.tools.infrastructure.api.jira.hook.install.PostInstallHooks
import com.atlassian.performance.tools.infrastructure.api.jira.hook.install.InstalledJira
import com.atlassian.performance.tools.infrastructure.api.jira.hook.install.PostInstallHook
import com.atlassian.performance.tools.ssh.api.SshConnection
Expand All @@ -10,7 +10,7 @@ class DatabaseIpConfig(
private val databaseIp: String
) : PostInstallHook {

override fun run(
override fun call(
ssh: SshConnection,
jira: InstalledJira,
hooks: PostInstallHooks
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.atlassian.performance.tools.infrastructure.api.database

import com.atlassian.performance.tools.infrastructure.api.jira.hook.PostInstallHooks
import com.atlassian.performance.tools.infrastructure.api.jira.hook.install.PostInstallHooks
import com.atlassian.performance.tools.infrastructure.api.jira.hook.install.InstalledJira
import com.atlassian.performance.tools.infrastructure.api.jira.hook.install.PostInstallHook
import com.atlassian.performance.tools.jvmtasks.api.Backoff
Expand All @@ -10,7 +10,7 @@ import java.time.Duration

class MysqlConnector : PostInstallHook {

override fun run(
override fun call(
ssh: SshConnection,
jira: InstalledJira,
hooks: PostInstallHooks
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,23 @@ package com.atlassian.performance.tools.infrastructure.api.jira.hook

import com.atlassian.performance.tools.infrastructure.api.jira.JiraNodeConfig
import com.atlassian.performance.tools.infrastructure.api.jira.hook.install.DefaultPostInstallHook
import com.atlassian.performance.tools.infrastructure.api.jira.hook.install.PreInstallHooks
import com.atlassian.performance.tools.infrastructure.api.jira.hook.start.DefaultPostStartHook
import net.jcip.annotations.ThreadSafe

@ThreadSafe
class JiraNodeHooks private constructor() : PreInstallHooks() {
class JiraNodeHooks {

val preInstall = PreInstallHooks()
val postInstall = preInstall.postInstall
val preStart = postInstall.preStart
val postStart = preStart.postStart
val reports = postStart.reports

companion object {
fun default(): JiraNodeHooks = JiraNodeHooks()
.apply { hook(DefaultPostStartHook()) }
.apply { hook(DefaultPostInstallHook(JiraNodeConfig.Builder().build())) }
.apply { postStart.insert(DefaultPostStartHook()) }
.apply { postInstall.insert(DefaultPostInstallHook(JiraNodeConfig.Builder().build())) }

fun empty(): JiraNodeHooks = JiraNodeHooks()
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package com.atlassian.performance.tools.infrastructure.api.jira.hook.server
package com.atlassian.performance.tools.infrastructure.api.jira.hook.install

import com.atlassian.performance.tools.infrastructure.api.jira.hook.PostStartHooks
import com.atlassian.performance.tools.infrastructure.api.jira.hook.TcpServer
import com.atlassian.performance.tools.infrastructure.api.jira.hook.PreInstallHooks
import com.atlassian.performance.tools.infrastructure.api.jira.hook.start.PostStartHooks
import com.atlassian.performance.tools.infrastructure.api.jira.hook.report.Report
import com.atlassian.performance.tools.infrastructure.api.jira.hook.start.PostStartHook
import com.atlassian.performance.tools.ssh.api.SshConnection
import java.net.URI

class AsyncProfilerHook : PreInstallHook {

override fun run(
override fun call(
ssh: SshConnection,
server: TcpServer,
hooks: PreInstallHooks
Expand All @@ -25,22 +23,22 @@ class AsyncProfilerHook : PreInstallHook {
ssh.execute("sudo sh -c 'echo 0 > /proc/sys/kernel/kptr_restrict'")
val profilerPath = "./$directory/profiler.sh"
val profiler = InstalledAsyncProfiler(profilerPath)
hooks.hook(profiler)
hooks.postStart.insert(profiler)
}
}

private class InstalledAsyncProfiler(
private val profilerPath: String
) : PostStartHook {

override fun run(
override fun call(
ssh: SshConnection,
jira: StartedJira,
hooks: PostStartHooks
) {
ssh.execute("$profilerPath -b 20000000 start ${jira.pid}")
val profiler = StartedAsyncProfiler(jira.pid, profilerPath)
hooks.addReport(profiler)
hooks.reports.add(profiler)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.atlassian.performance.tools.infrastructure.api.jira.hook.install

import com.atlassian.performance.tools.infrastructure.api.jira.SharedHome
import com.atlassian.performance.tools.infrastructure.api.jira.hook.PostInstallHooks
import com.atlassian.performance.tools.ssh.api.SshConnection

class DataCenterHook(
private val nodeId: String,
private val sharedHome: SharedHome
) : PostInstallHook {

override fun run(
override fun call(
ssh: SshConnection,
jira: InstalledJira,
hooks: PostInstallHooks
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.atlassian.performance.tools.infrastructure.api.jira.hook.install

import com.atlassian.performance.tools.infrastructure.api.jira.JiraNodeConfig
import com.atlassian.performance.tools.infrastructure.api.jira.hook.PostInstallHooks
import com.atlassian.performance.tools.infrastructure.api.jira.hook.server.LateUbuntuSysstat
import com.atlassian.performance.tools.infrastructure.jira.hook.install.ProfilerHook
import com.atlassian.performance.tools.infrastructure.jira.hook.install.SplunkForwarderHook
import com.atlassian.performance.tools.ssh.api.SshConnection
Expand All @@ -11,7 +9,7 @@ class DefaultPostInstallHook(
private val config: JiraNodeConfig
) : PostInstallHook {

override fun run(
override fun call(
ssh: SshConnection,
jira: InstalledJira,
hooks: PostInstallHooks
Expand All @@ -24,6 +22,6 @@ class DefaultPostInstallHook(
SplunkForwarderHook(config.splunkForwarder),
JiraLogs(),
LateUbuntuSysstat()
).forEach { it.run(ssh, jira, hooks) }
).forEach { it.call(ssh, jira, hooks) }
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.atlassian.performance.tools.infrastructure.api.jira.hook.install

import com.atlassian.performance.tools.infrastructure.api.jira.hook.PostInstallHooks
import com.atlassian.performance.tools.ssh.api.SshConnection

class DisabledAutoBackup : PostInstallHook {

override fun run(
override fun call(
ssh: SshConnection,
jira: InstalledJira,
hooks: PostInstallHooks
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
package com.atlassian.performance.tools.infrastructure.api.jira.hook.install

import com.atlassian.performance.tools.infrastructure.api.jira.hook.JiraNodeHooks
import com.atlassian.performance.tools.infrastructure.api.jira.hook.TcpServer
import com.atlassian.performance.tools.ssh.api.SshConnection

class HookedJiraInstallation(
private val installation: JiraInstallation
private val installation: JiraInstallation,
private val hooks: PreInstallHooks
) : JiraInstallation {

override fun install(
ssh: SshConnection,
server: TcpServer,
hooks: JiraNodeHooks
server: TcpServer
): InstalledJira {
hooks.runPreInstallHooks(ssh, server)
val installed = installation.install(ssh, server, hooks)
hooks.runPostInstallHooks(ssh, installed)
hooks.call(ssh, server)
val installed = installation.install(ssh, server)
hooks.postInstall.call(ssh, installed)
return installed
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.atlassian.performance.tools.infrastructure.api.jira.hook.install

import com.atlassian.performance.tools.infrastructure.api.jira.hook.TcpServer
import com.atlassian.performance.tools.infrastructure.api.jvm.JavaDevelopmentKit

class InstalledJira(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package com.atlassian.performance.tools.infrastructure.api.jira.hook.install

import com.atlassian.performance.tools.infrastructure.api.jira.hook.PostInstallHooks
import com.atlassian.performance.tools.ssh.api.SshConnection

class JiraHomeProperty : PostInstallHook {

override fun run(
override fun call(
ssh: SshConnection,
jira: InstalledJira,
hooks: PostInstallHooks
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package com.atlassian.performance.tools.infrastructure.api.jira.hook.install

import com.atlassian.performance.tools.infrastructure.api.jira.hook.JiraNodeHooks
import com.atlassian.performance.tools.infrastructure.api.jira.hook.TcpServer
import com.atlassian.performance.tools.ssh.api.SshConnection
import net.jcip.annotations.ThreadSafe

Expand All @@ -10,7 +8,6 @@ interface JiraInstallation {

fun install(
ssh: SshConnection,
server: TcpServer,
hooks: JiraNodeHooks
server: TcpServer
): InstalledJira
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package com.atlassian.performance.tools.infrastructure.api.jira.hook.install

import com.atlassian.performance.tools.infrastructure.api.jira.hook.PostInstallHooks
import com.atlassian.performance.tools.infrastructure.api.jira.hook.report.StaticReport
import com.atlassian.performance.tools.ssh.api.SshConnection
import java.nio.file.Path
import java.nio.file.Paths

class JiraLogs : PostInstallHook {

override fun run(ssh: SshConnection, jira: InstalledJira, hooks: PostInstallHooks) {
override fun call(ssh: SshConnection, jira: InstalledJira, hooks: PostInstallHooks) {
listOf(
"${jira.home}/log/atlassian-jira.log",
"${jira.installation}/logs/catalina.out"
)
.onEach { ensureFile(Paths.get(it), ssh) }
.map { StaticReport(it) }
.forEach { hooks.addReport(it) }
.forEach { hooks.reports.add(it) }
}

private fun ensureFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ package com.atlassian.performance.tools.infrastructure.api.jira.hook.install
import com.atlassian.performance.tools.infrastructure.api.jira.JiraGcLog
import com.atlassian.performance.tools.infrastructure.api.jira.JiraNodeConfig
import com.atlassian.performance.tools.infrastructure.api.jira.SetenvSh
import com.atlassian.performance.tools.infrastructure.api.jira.hook.PostInstallHooks
import com.atlassian.performance.tools.infrastructure.api.jira.hook.report.FileListing
import com.atlassian.performance.tools.ssh.api.SshConnection

class JvmConfig(
private val config: JiraNodeConfig
) : PostInstallHook {

override fun run(
override fun call(
ssh: SshConnection,
jira: InstalledJira,
hooks: PostInstallHooks
Expand All @@ -24,6 +23,6 @@ class JvmConfig(
jiraIp = jira.server.ip
)
val report = FileListing(gcLog.path("*"))
hooks.addReport(report)
hooks.reports.add(report)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
package com.atlassian.performance.tools.infrastructure.api.jira.hook.server
package com.atlassian.performance.tools.infrastructure.api.jira.hook.install

import com.atlassian.performance.tools.infrastructure.Iostat
import com.atlassian.performance.tools.infrastructure.api.jira.hook.PostInstallHooks
import com.atlassian.performance.tools.infrastructure.api.jira.hook.PostStartHooks
import com.atlassian.performance.tools.infrastructure.api.jira.hook.install.InstalledJira
import com.atlassian.performance.tools.infrastructure.api.jira.hook.install.PostInstallHook
import com.atlassian.performance.tools.infrastructure.api.jira.hook.start.PostStartHooks
import com.atlassian.performance.tools.infrastructure.api.jira.hook.start.PostStartHook
import com.atlassian.performance.tools.infrastructure.api.os.OsMetric
import com.atlassian.performance.tools.infrastructure.api.os.Ubuntu
Expand All @@ -13,7 +10,7 @@ import com.atlassian.performance.tools.infrastructure.jira.hook.RemoteMonitoring
import com.atlassian.performance.tools.ssh.api.SshConnection

class LateUbuntuSysstat : PostInstallHook {
override fun run(
override fun call(
ssh: SshConnection,
jira: InstalledJira,
hooks: PostInstallHooks
Expand All @@ -22,15 +19,15 @@ class LateUbuntuSysstat : PostInstallHook {
ubuntu.install(ssh, listOf("sysstat"))
listOf(Vmstat(), Iostat())
.map { PostStartOsMetric(it) }
.forEach { hooks.hook(it) }
.forEach { hooks.postStart.insert(it) }
}
}

internal class PostStartOsMetric(
private val metric: OsMetric
) : PostStartHook {
override fun run(ssh: SshConnection, jira: StartedJira, hooks: PostStartHooks) {
override fun call(ssh: SshConnection, jira: StartedJira, hooks: PostStartHooks) {
val process = metric.start(ssh)
hooks.addReport(RemoteMonitoringProcessReport(process))
hooks.reports.add(RemoteMonitoringProcessReport(process))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package com.atlassian.performance.tools.infrastructure.api.jira.hook.install
import com.atlassian.performance.tools.concurrency.api.submitWithLogContext
import com.atlassian.performance.tools.infrastructure.api.distribution.ProductDistribution
import com.atlassian.performance.tools.infrastructure.api.jira.JiraHomeSource
import com.atlassian.performance.tools.infrastructure.api.jira.hook.JiraNodeHooks
import com.atlassian.performance.tools.infrastructure.api.jira.hook.TcpServer
import com.atlassian.performance.tools.infrastructure.api.jvm.JavaDevelopmentKit
import com.atlassian.performance.tools.ssh.api.SshConnection
import java.util.concurrent.Executors
Expand All @@ -17,8 +15,7 @@ class ParallelInstallation(

override fun install(
ssh: SshConnection,
server: TcpServer,
hooks: JiraNodeHooks
server: TcpServer
): InstalledJira {
val pool = Executors.newCachedThreadPool { runnable ->
Thread(runnable, "jira-installation-${runnable.hashCode()}")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package com.atlassian.performance.tools.infrastructure.api.jira.hook.install

import com.atlassian.performance.tools.infrastructure.api.jira.hook.PostInstallHooks
import com.atlassian.performance.tools.ssh.api.SshConnection

/**
* Intercepts a call after Jira is installed.
*/
interface PostInstallHook {

fun run(
/**
* @param [ssh] connects to the [jira]
* @param [jira] points to the installed Jira
* @param [hooks] inserts future hooks and reports
*/
fun call(
ssh: SshConnection,
jira: InstalledJira,
hooks: PostInstallHooks
Expand Down
Loading

0 comments on commit 2742b75

Please sign in to comment.