aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/vespa/storage/tools/storage-cmd.cpp
blob: bc932fcf6fd297ac2e64b4f5b1dc03ce8596729c (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fnet/frt/rpcrequest.h>
#include <vespa/fnet/frt/supervisor.h>
#include <vespa/fnet/frt/target.h>
#include <vespa/slobrok/sbmirror.h>
#include <vespa/vespalib/util/signalhandler.h>
#include <vespa/vespalib/locale/c.h>
#include <vespa/vespalib/util/time.h>
#include <thread>

#include <vespa/log/log.h>
LOG_SETUP("vespa-storage-cmd");

class RPCClient
{
private:
    static bool addArg(FRT_RPCRequest *req, const char *param) {
        int len = strlen(param);
        if (len < 2 || param[1] != ':') {
            return false;
        }
        const char *value = param + 2;
        switch (param[0]) {
        case 'b':
            req->GetParams()->AddInt8(strtoll(value, nullptr, 0));
            break;
        case 'h':
            req->GetParams()->AddInt16(strtoll(value, nullptr, 0));
            break;
        case 'i':
            req->GetParams()->AddInt32(strtoll(value, nullptr, 0));
            break;
        case 'l':
            req->GetParams()->AddInt64(strtoll(value, nullptr, 0));
            break;
        case 'f':
            req->GetParams()->AddFloat(vespalib::locale::c::strtod(value, nullptr));
            break;
        case 'd':
            req->GetParams()->AddDouble(vespalib::locale::c::strtod(value, nullptr));
            break;
        case 's':
            req->GetParams()->AddString(value);
            break;
        default:
            return false;
        }
        return true;
    }

public:
    int main(int argc, char **argv) {
        if (argc < 3) {
            fprintf(stderr, "usage: vespa-storage-cmd <connectspec> <method> [args]\n");
            fprintf(stderr, "Calls RPC method on a storage/distributor process\n");
            fprintf(stderr, "Call frt.rpc.getMethodList to get available RPC methods\n");
            fprintf(stderr, "    each arg must be on the form <type>:<value>\n");
            fprintf(stderr, "    supported types: {'b','h','i','l','f','d','s'}\n");
            return 1;
        }
        int retCode = 0;
        fnet::frt::StandaloneFRT supervisor;

        slobrok::ConfiguratorFactory sbcfg(config::ConfigUri("client"));
        slobrok::api::MirrorAPI mirror(supervisor.supervisor(), sbcfg);

        while (!mirror.ready()) {
            std::this_thread::sleep_for(10ms);
        }

        slobrok::api::MirrorAPI::SpecList list = mirror.lookup(argv[1]);

        if (list.size() == 0) {
            fprintf(stderr, "No servers found matching %s\n", argv[1]);
        }

        for (size_t j = 0; j < list.size(); j++) {
            FRT_Target *target = supervisor.supervisor().GetTarget(list[j].second.c_str());

            // If not fleet controller, need to connect first.
            if (strstr(argv[1], "fleetcontroller") == nullptr) {
                FRT_RPCRequest *req = supervisor.supervisor().AllocRPCRequest();
                req->SetMethodName("vespa.storage.connect");
                req->GetParams()->AddString(argv[1]);
                target->InvokeSync(req, 10.0);

                if (req->GetErrorCode() != FRTE_NO_ERROR) {
                    fprintf(stderr, "error(%d): %s\n",
                        req->GetErrorCode(),
                        req->GetErrorMessage());
                    continue;
                }
                req->internal_subref();
            }

            FRT_RPCRequest *req = supervisor.supervisor().AllocRPCRequest();
            req->SetMethodName(argv[2]);

            for (int i = 3; i < argc; ++i) {
                if (!addArg(req, argv[i])) {
                    fprintf(stderr, "could not parse parameter: '%s'\n", argv[i]);
                    retCode = 2;
                    break;
                }
            }
            if (retCode == 0) {
                target->InvokeSync(req, 10.0);
                if (req->GetErrorCode() == FRTE_NO_ERROR) {
                    fprintf(stdout, "RETURN VALUES FOR %s:\n", list[j].first.c_str());
                    req->GetReturn()->Print();
                    retCode = 3;
                } else {
                    fprintf(stderr, "error(%d): %s\n",
                        req->GetErrorCode(),
                        req->GetErrorMessage());
                }
            }
            req->internal_subref();
            target->internal_subref();
        }
        return retCode;
    }
};

int main(int argc, char **argv) {
    vespalib::SignalHandler::PIPE.ignore();
    RPCClient myapp;
    return myapp.main(argc, argv);
}