aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/concurrent/maintenance/JobMetrics.java
blob: 73a5dc77743638a4b7e18586a67767befd031aa6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.concurrent.maintenance;

import java.util.concurrent.ConcurrentHashMap;

/**
 * Tracks and forwards maintenance job metrics.
 *
 * @author mpolden
 */
public abstract class JobMetrics {

    private final ConcurrentHashMap<String, Long> incompleteRuns = new ConcurrentHashMap<>();

    /** Record starting of a run of a job */
    public void starting(String job) {
        incompleteRuns.merge(job, 1L, Long::sum);
    }

    /** Record completion of given job */
    public void recordCompletionOf(String job) {
        incompleteRuns.put(job, 0L);
    }

    /**
     * Records completion of a run of a job.
     * This is guaranteed to always be called once whenever starting has been called.
     */
    public void completed(String job, double successFactor) {
        Long incompleteRuns = this.incompleteRuns.get(job);
        if (incompleteRuns != null) {
            recordCompletion(job, incompleteRuns, successFactor);
        }
    }

    protected abstract void recordCompletion(String job, Long incompleteRuns, double successFactor);

}