aboutsummaryrefslogtreecommitdiffstats
path: root/filedistribution/src/apps/status/status-filedistribution.cpp
blob: f51454d438bc1b7ab19c442631dd29e01471e87c (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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <vespa/filedistribution/model/zkfacade.h>
#include <vespa/filedistribution/model/filedistributionmodel.h>
#include <vespa/filedistribution/model/filedistributionmodelimpl.h>
#include <zookeeper/zookeeper.h>
#include <boost/program_options.hpp>

#include <vespa/log/log.h>
#include <iostream>
#include <map>
#include <thread>

LOG_SETUP("status-filedistribution");

using namespace filedistribution;
using namespace std::literals;
namespace po = boost::program_options;

std::string
plural(size_t size)
{
    if (size == 1)
        return "";
    else
        return "s";
}

template <class CONT>
std::string
plural(const CONT& cont)
{
    size_t size = cont.size();
    return plural(size);
}

typedef FileDBModel::HostStatus HostStatus;
typedef std::map<std::string, HostStatus> StatusByHostName;

void
printWaitingForHosts(const StatusByHostName& notFinishedHosts)
{
    std::cout <<"Waiting for the following host" <<plural(notFinishedHosts) <<":" <<std::endl;
    for (const StatusByHostName::value_type & hostNameAndStatus : notFinishedHosts) {
        std::cout <<hostNameAndStatus.first <<"  (";

        const HostStatus& hostStatus = hostNameAndStatus.second;
        if (hostStatus._state == HostStatus::notStarted) {
            std::cout <<"Not started";
        } else {
            std::cout <<"Downloading, "
                      <<hostStatus._numFilesFinished <<"/" <<hostStatus._numFilesToDownload
                      <<" file" <<plural(hostStatus._numFilesToDownload) <<" completed";
        }
        std::cout <<")" <<std::endl;
    }
}

//TODO:refactor
int printStatus(const std::string& zkservers)
{
    std::shared_ptr<ZKFacade> zk(new ZKFacade(zkservers, true));

    std::shared_ptr<FileDBModel> model(new ZKFileDBModel(zk));

    std::vector<std::string> hosts = model->getHosts();

    StatusByHostName notFinishedHosts;
    StatusByHostName finishedHosts;
    bool hasStarted = false;

    for (const std::string & host : hosts) {
        HostStatus hostStatus = model->getHostStatus(host);
        switch (hostStatus._state) {
          case HostStatus::finished:
            hasStarted = true;
            finishedHosts[host] = hostStatus;
            break;
          case HostStatus::inProgress:
            hasStarted = true;
            [[fallthrough]];
          case HostStatus::notStarted:
            notFinishedHosts[host] = hostStatus;
            break;
        }
    }

    if (notFinishedHosts.empty()) {
        std::cout <<"Finished distributing files to all hosts." <<std::endl;
        return 0;
    } else if (!hasStarted) {
        std::cout <<"File distribution has not yet started." <<std::endl;
        return 0;
    } else {
        printWaitingForHosts(notFinishedHosts);
        return 5;
    }
}

int
printStatusRetryIfZKProblem(const std::string& zkservers, const std::string& zkLogFile)
{
    FILE* file = fopen(zkLogFile.c_str(), "w");
    if (file == NULL) {
         std::cerr <<"Could not open file " <<zkLogFile <<std::endl;
    } else {
         zoo_set_log_stream(file);
    }
    zoo_set_debug_level(ZOO_LOG_LEVEL_ERROR);

    for (int i = 0; i < 5; ++i) {
        try {
            return printStatus(zkservers);
        } catch (ZKNodeDoesNotExistsException& e) {
            LOG(debug, "Node does not exists, assuming concurrent update. %s", e.what());

        } catch (ZKSessionExpired& e) {
            LOG(debug, "Session expired.");
        }
        std::this_thread::sleep_for(500ms);
    }
    return 4;
}


//TODO: move to common
struct ProgramOptionException {
    std::string _msg;
    ProgramOptionException(const std::string & msg)
        : _msg(msg)
    {}
};

bool exists(const std::string& optionName, const po::variables_map& map) {
    return map.find(optionName) != map.end();
}

void ensureExists(const std::string& optionName, const po::variables_map& map) {
    if (!exists(optionName, map)) {
        throw ProgramOptionException("Error: Missing option " + optionName);
    }
}
//END: move to common


int
main(int argc, char** argv) {
    const char
        *zkstring = "zkstring",
        *zkLogFile = "zkLogFile",
        *help = "help";

    po::options_description description;
    description.add_options()
        (zkstring, po::value<std::string > (), "The zookeeper servers to connect to, separated by comma")
        (zkLogFile, po::value<std::string >() -> default_value("/dev/null"), "Zookeeper log file")
        (help, "help");

    try {
        po::variables_map values;
        po::store(po::parse_command_line(argc, argv, description), values);

        if (exists(help, values)) {
            std::cout <<description;
            return 0;
        }

        ensureExists(zkstring, values);

        return printStatusRetryIfZKProblem(
                values[zkstring] .as<std::string>(),
                values[zkLogFile].as<std::string>());
    } catch (const ProgramOptionException& e) {
        std::cerr <<e._msg <<std::endl;
        return 3;
    } catch(const std::exception& e) {
        std::cerr <<"Error: " <<e.what() <<std::endl;
        return 3;
    }
}