summaryrefslogtreecommitdiff
path: root/cpp/src/IceGrid/NodeCache.h
blob: 639ff988ce10e70ae89ae72cea11ade816a7e82e (plain)
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
//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

#ifndef ICE_GRID_NODECACHE_H
#define ICE_GRID_NODECACHE_H

#include <IceGrid/Cache.h>
#include <IceGrid/Internal.h>

namespace IceGrid
{

class NodeCache;
class NodeSessionI;
class ReplicaCache;
class ServerEntry;
class SessionI;

using ServerEntrySeq = std::vector<std::shared_ptr<ServerEntry>>;

class NodeEntry final
{
public:

    NodeEntry(NodeCache&, const std::string&);

    void addDescriptor(const std::string&, const NodeDescriptor&);
    void removeDescriptor(const std::string&);

    void addServer(const std::shared_ptr<ServerEntry>&);
    void removeServer(const std::shared_ptr<ServerEntry>&);
    void setSession(const std::shared_ptr<NodeSessionI>&);

    std::shared_ptr<NodePrx> getProxy() const;
    std::shared_ptr<InternalNodeInfo> getInfo() const;
    ServerEntrySeq getServers() const;
    LoadInfo getLoadInfoAndLoadFactor(const std::string&, float&) const;
    std::shared_ptr<NodeSessionI> getSession() const;

    std::shared_ptr<Ice::ObjectPrx> getAdminProxy() const;

    bool canRemove();

    void loadServer(const std::shared_ptr<ServerEntry>&, const ServerInfo&, const std::shared_ptr<SessionI>&,
                    std::chrono::seconds, bool);
    void destroyServer(const std::shared_ptr<ServerEntry>&, const ServerInfo&, std::chrono::seconds, bool);

    ServerInfo getServerInfo(const ServerInfo&, const std::shared_ptr<SessionI>&);
    std::shared_ptr<InternalServerDescriptor> getInternalServerDescriptor(const ServerInfo&, const std::shared_ptr<SessionI>&);

    void checkSession(std::unique_lock<std::mutex>&) const;
    void setProxy(const std::shared_ptr<NodePrx>&);
    void finishedRegistration();
    void finishedRegistration(std::exception_ptr);

private:

    std::shared_ptr<NodeEntry> selfRemovingPtr() const;

    std::shared_ptr<ServerDescriptor> getServerDescriptor(const ServerInfo&, const std::shared_ptr<SessionI>&);
    std::shared_ptr<InternalServerDescriptor> getInternalServerDescriptor(const ServerInfo&) const;

    NodeCache& _cache;
    const std::string _name;
    std::shared_ptr<NodeSessionI> _session;
    std::map<std::string, std::shared_ptr<ServerEntry>> _servers;
    std::map<std::string, NodeDescriptor> _descriptors;

    mutable bool _registering;
    mutable std::shared_ptr<NodePrx> _proxy;

    mutable std::mutex _mutex;
    mutable std::condition_variable _condVar;

    // A self removing shared_ptr of 'this' which removes itself from the NodeCache upon destruction
    std::weak_ptr<NodeEntry> _selfRemovingPtr;

    // The number of self removing shared_ptr deleters left to run.
    // Always accessed with the cache mutex locked
    int _selfRemovingRefCount;

    friend NodeCache;
};

class NodeCache : public CacheByString<NodeEntry>
{
public:

    using ValueType = NodeEntry*;

    NodeCache(const std::shared_ptr<Ice::Communicator>&, ReplicaCache&, const std::string&);

    std::shared_ptr<NodeEntry> get(const std::string&, bool = false) const;

    const std::shared_ptr<Ice::Communicator>& getCommunicator() const { return _communicator; }
    const std::string& getReplicaName() const { return _replicaName; }
    ReplicaCache& getReplicaCache() const { return _replicaCache; }

private:

    const std::shared_ptr<Ice::Communicator> _communicator;
    const std::string _replicaName;
    ReplicaCache& _replicaCache;
};

};

#endif