Skip to content

Commit

Permalink
Move the metrics update logic to jicoco. (#1134)
Browse files Browse the repository at this point in the history
* Move the metrics update logic to jicoco.

* Use Metrics updater.

* Update jicoco.
  • Loading branch information
bgrozev authored Feb 20, 2024
1 parent 9ed14e2 commit be8a115
Show file tree
Hide file tree
Showing 8 changed files with 14 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,66 +21,18 @@ import org.jitsi.config.JitsiConfig
import org.jitsi.jicofo.TaskPools
import org.jitsi.metaconfig.config
import org.jitsi.metrics.MetricsContainer
import org.jitsi.utils.logging2.createLogger
import org.jitsi.metrics.MetricsUpdater
import java.time.Duration
import java.util.concurrent.CopyOnWriteArrayList
import java.util.concurrent.ScheduledFuture
import java.util.concurrent.TimeUnit

class JicofoMetricsContainer private constructor() : MetricsContainer(namespace = "jitsi_jicofo") {
private val logger = createLogger()
private val subtasks: MutableList<() -> Unit> = CopyOnWriteArrayList()
val metricsUpdater = MetricsUpdater(TaskPools.scheduledPool, updateInterval)

private var updateTask: ScheduledFuture<*>? = null

// Allow updates to be disabled for tests
var disablePeriodicUpdates = false

fun addUpdateTask(subtask: () -> Unit) {
if (disablePeriodicUpdates) {
logger.warn("Periodic updates are disabled, will not execute update task.")
return
}

subtasks.add(subtask)
synchronized(this) {
if (updateTask == null) {
logger.info("Scheduling metrics update task with interval $updateInterval.")
updateTask = TaskPools.scheduledPool.scheduleAtFixedRate(
{ updateMetrics() },
0,
updateInterval.toMillis(),
TimeUnit.MILLISECONDS
)
}
}
}

fun updateMetrics() {
synchronized(this) {
logger.debug("Running ${subtasks.size} subtasks.")
subtasks.forEach {
try {
it.invoke()
} catch (e: Exception) {
logger.warn("Exception while running subtask", e)
}
}
companion object {
private val updateInterval: Duration by config {
"jicofo.metrics.update-interval".from(JitsiConfig.newConfig)
}
}

fun stop() = synchronized(this) {
updateTask?.cancel(false)
updateTask = null
subtasks.clear()
}

companion object {
@JvmStatic
val instance = JicofoMetricsContainer()

val updateInterval: Duration by config {
"jicofo.metrics.update-interval".from(JitsiConfig.newConfig)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class BridgeSelector @JvmOverloads constructor(
private val bridges: MutableMap<Jid, Bridge> = mutableMapOf()

init {
JicofoMetricsContainer.instance.addUpdateTask { updateMetrics() }
JicofoMetricsContainer.instance.metricsUpdater.addUpdateTask { updateMetrics() }
}

val operationalBridgeCount: Int
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ class JicofoSelectorKotestProjectConfig : AbstractProjectConfig() {
MetaconfigSettings.cacheEnabled = false

JicofoMetricsContainer.instance.checkForNameConflicts = false
JicofoMetricsContainer.instance.disablePeriodicUpdates = true
JicofoMetricsContainer.instance.metricsUpdater.disablePeriodicUpdates = true
}
}
2 changes: 1 addition & 1 deletion jicofo/src/main/java/org/jitsi/jicofo/rest/Statistics.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public String getStats()
JicofoServices jicofoServices
= Objects.requireNonNull(JicofoServices.getJicofoServicesSingleton(), "jicofoServices");
// Update the metrics that are usually updated periodically so we read the current values.
JicofoMetricsContainer.getInstance().updateMetrics();
JicofoMetricsContainer.getInstance().getMetricsUpdater().updateMetrics();
return jicofoServices.getStats().toJSONString();
}
}
2 changes: 1 addition & 1 deletion jicofo/src/main/kotlin/org/jitsi/jicofo/FocusManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ class FocusManager(
private val pinnedConferences: MutableMap<EntityBareJid, PinnedConference> = HashMap()

fun start() {
metricsContainer.addUpdateTask { updateMetrics() }
metricsContainer.metricsUpdater.addUpdateTask { updateMetrics() }
}

/**
Expand Down
6 changes: 3 additions & 3 deletions jicofo/src/main/kotlin/org/jitsi/jicofo/JicofoServices.kt
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ class JicofoServices {

init {
if (jibriDetector != null || sipJibriDetector != null) {
JicofoMetricsContainer.instance.addUpdateTask {
JicofoMetricsContainer.instance.metricsUpdater.addUpdateTask {
JibriDetectorMetrics.updateMetrics(jibriDetector = jibriDetector, sipJibriDetector = sipJibriDetector)
}
}
Expand Down Expand Up @@ -167,7 +167,7 @@ class JicofoServices {

init {
logger.info("Registering GlobalMetrics periodic updates.")
JicofoMetricsContainer.instance.addUpdateTask { GlobalMetrics.update() }
JicofoMetricsContainer.instance.metricsUpdater.addUpdateTask { GlobalMetrics.update() }
}

fun shutdown() {
Expand All @@ -176,7 +176,7 @@ class JicofoServices {
it.shutdown()
}
healthChecker?.shutdown()
JicofoMetricsContainer.instance.stop()
JicofoMetricsContainer.instance.metricsUpdater.stop()
jettyServer?.stop()
jvbDoctor?.let {
bridgeSelector.removeHandler(it)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ class XmppServices(
breweryJid
).apply {
init()
JicofoMetricsContainer.instance.addUpdateTask { updateMetrics() }
JicofoMetricsContainer.instance.metricsUpdater.addUpdateTask { updateMetrics() }
}
} ?: run {
logger.info("No Jigasi detector configured.")
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
<slf4j.version>1.7.32</slf4j.version>
<jersey.version>3.0.10</jersey.version>
<jitsi.utils.version>1.0-127-g6c65524</jitsi.utils.version>
<jicoco.version>1.1-127-gf49982f</jicoco.version>
<jicoco.version>1.1-132-g906f995</jicoco.version>
<ktlint-maven-plugin.version>3.0.0</ktlint-maven-plugin.version>
<spotbugs.version>4.6.0</spotbugs.version>
<junit.version>5.10.0</junit.version>
Expand Down

0 comments on commit be8a115

Please sign in to comment.