aboutsummaryrefslogtreecommitdiffstats
path: root/searchlib/src/vespa/searchlib/transactionlog/translogserver.h
blob: 3f9459773862ad6008e5a9a488d56f3a3f9f4a84 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#pragma once

#include "domain.h"
#include <vespa/vespalib/util/document_runnable.h>
#include <vespa/vespalib/util/threadstackexecutor.h>
#include <vespa/document/util/queue.h>
#include <vespa/fnet/frt/invokable.h>
#include <mutex>


class FRT_Supervisor;
class FNET_Transport;

namespace search::common { class FileHeaderContext; }

namespace search::transactionlog {

class TransLogServerExplorer;

class TransLogServer : public document::Runnable, private FRT_Invokable, public Writer
{
public:
    friend class TransLogServerExplorer;
    typedef std::unique_ptr<TransLogServer> UP;
    typedef std::shared_ptr<TransLogServer> SP;

    TransLogServer(const vespalib::string &name, int listenPort, const vespalib::string &baseDir,
                   const common::FileHeaderContext &fileHeaderContext, const DomainConfig & cfg, size_t maxThreads);
    TransLogServer(const vespalib::string &name, int listenPort, const vespalib::string &baseDir,
                   const common::FileHeaderContext &fileHeaderContext, const DomainConfig & cfg);
    TransLogServer(const vespalib::string &name, int listenPort, const vespalib::string &baseDir,
                   const common::FileHeaderContext &fileHeaderContext);
    ~TransLogServer() override;
    DomainStats getDomainStats() const;
    void commit(const vespalib::string & domainName, const Packet & packet, DoneCallback done) override;
    TransLogServer & setDomainConfig(const DomainConfig & cfg);

    class Session
    {
        bool _down;
    public:
        typedef std::shared_ptr<Session> SP;

        Session() : _down(false) { }
        bool getDown() const { return _down; }
        void setDown() { _down = true; }
    };

private:
    bool onStop() override;
    void run() override;
    void exportRPC(FRT_Supervisor & supervisor);
    void relayToThreadRPC(FRT_RPCRequest *req);

    void createDomain(FRT_RPCRequest *req);
    void deleteDomain(FRT_RPCRequest *req);
    void openDomain(FRT_RPCRequest *req);
    void listDomains(FRT_RPCRequest *req);

    void domainStatus(FRT_RPCRequest *req);
    void domainCommit(FRT_RPCRequest *req);
    void domainSessionRun(FRT_RPCRequest *req);
    void domainPrune(FRT_RPCRequest *req);
    void domainVisit(FRT_RPCRequest *req);
    void domainSessionClose(FRT_RPCRequest *req);
    void domainSync(FRT_RPCRequest *req);

    void initSession(FRT_RPCRequest *req);
    void finiSession(FRT_RPCRequest *req);
    void downSession(FRT_RPCRequest *req);

    std::vector<vespalib::string> getDomainNames();
    Domain::SP findDomain(vespalib::stringref name);
    vespalib::string dir()        const { return _baseDir + "/" + _name; }
    vespalib::string domainList() const { return dir() + "/" + _name + ".domains"; }

    static const Session::SP & getSession(FRT_RPCRequest *req);

    using DomainList = std::map<vespalib::string, Domain::SP >;

    vespalib::string                    _name;
    vespalib::string                    _baseDir;
    DomainConfig                        _domainConfig;
    vespalib::ThreadStackExecutor       _commitExecutor;
    vespalib::ThreadStackExecutor       _sessionExecutor;
    std::unique_ptr<FastOS_ThreadPool>  _threadPool;
    std::unique_ptr<FNET_Transport>     _transport;
    std::unique_ptr<FRT_Supervisor>     _supervisor;
    DomainList                          _domains;
    mutable std::mutex                  _lock;          // Protects _domains
    std::mutex                          _fileLock;      // Protects the creating and deleting domains including file system operations.
    document::Queue<FRT_RPCRequest *>   _reqQ;
    const common::FileHeaderContext    &_fileHeaderContext;
    using Guard = std::lock_guard<std::mutex>;
};

}