aboutsummaryrefslogtreecommitdiffstats
path: root/searchcore/src/vespa/searchcore/proton/reprocessing/reprocessingrunner.cpp
blob: a76f29656aa9eb676559342c3032994d1bd505ad (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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include "reprocessingrunner.h"
#include "i_reprocessing_task.h"
#include <mutex>

namespace proton {

ReprocessingRunner::ReprocessingRunner()
    : _lock(),
      _tasks(),
      _state(NOT_STARTED)
{
}


void
ReprocessingRunner::addTasks(const ReprocessingTasks &tasks)
{
    std::lock_guard<std::mutex> guard(_lock);
    for (auto task : tasks) {
        _tasks.push_back(task);
    }
}


void
ReprocessingRunner::run()
{
    {
        std::lock_guard<std::mutex> guard(_lock);
        _state = RUNNING;
    }
    for (auto &task : _tasks) {
        task->run();
    }
    std::lock_guard<std::mutex> guard(_lock);
    _tasks.clear();
    _state = DONE;
    
}


void
ReprocessingRunner::reset()
{
    std::lock_guard<std::mutex> guard(_lock);
    _tasks.clear();
    _state = NOT_STARTED;
}


bool
ReprocessingRunner::empty() const
{
    std::lock_guard<std::mutex> guard(_lock);
    return _tasks.empty();
}


double
ReprocessingRunner::getProgress() const
{
    std::lock_guard<std::mutex> guard(_lock);
    switch (_state) {
    case State::NOT_STARTED:
        return 0.0;
    case State::DONE:
        return 1.0;
    default:
        ;
    }
    double weightedProgress = 0.0;
    double weight = 0.0;
    for (auto task : _tasks) {
        IReprocessingTask::Progress progress = task->getProgress();
        weightedProgress += progress._progress * progress._weight;
        weight += progress._weight;
    }
    if (weight == 0.0)
        return 1.0;
    return weightedProgress / weight;
}

} // namespace proton