summaryrefslogtreecommitdiff
path: root/cpp/src/IceGrid/AdapterCache.h
blob: be7203955debc4b16141c356933aa7ce81a291bc (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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// **********************************************************************
//
// Copyright (c) 2003-2006 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************

#ifndef ICE_GRID_ADAPTERCACHE_H
#define ICE_GRID_ADAPTERCACHE_H

#include <IceUtil/Mutex.h>
#include <IceUtil/Shared.h>
#include <IceGrid/Cache.h>
#include <IceGrid/Query.h>
#include <IceGrid/Internal.h>
#include <IceGrid/Allocatable.h>

namespace IceGrid
{

class AdapterCache;

class ServerEntry;
typedef IceUtil::Handle<ServerEntry> ServerEntryPtr;
typedef std::vector<ServerEntryPtr> ServerEntrySeq;

class AdapterEntry;
typedef IceUtil::Handle<AdapterEntry> AdapterEntryPtr;

class AdapterEntry : virtual public IceUtil::Shared, public IceUtil::Mutex
{
public:
    
    AdapterEntry(AdapterCache&, const std::string&);

    virtual std::vector<std::pair<std::string, AdapterPrx> > getProxies(int&, const SessionIPtr&) = 0;
    virtual float getLeastLoadedNodeLoad(LoadSample) const = 0;
    virtual std::string getApplication() const = 0;
    virtual AdapterInfoSeq getAdapterInfo() const = 0;

    virtual bool canRemove();
    
protected:

    AdapterCache& _cache;
    const std::string _id;
};
typedef IceUtil::Handle<AdapterEntry> AdapterEntryPtr;

class ServerAdapterEntry : public AdapterEntry, public Allocatable
{
public:

    ServerAdapterEntry(AdapterCache&, const std::string&, const std::string&, bool, const ServerEntryPtr&);

    virtual std::vector<std::pair<std::string, AdapterPrx> > getProxies(int&, const SessionIPtr&);
    virtual float getLeastLoadedNodeLoad(LoadSample) const;
    virtual std::string getApplication() const;
    virtual AdapterInfoSeq getAdapterInfo() const;
    virtual const std::string& getReplicaGroupId() const { return _replicaGroupId; }

    AdapterPrx getProxy(const std::string& = std::string()) const;

    virtual bool allocated(const SessionIPtr&);
    virtual void released(const SessionIPtr&);

private:
    
    ServerEntryPtr getServer() const;

    const std::string _replicaGroupId;
    const ServerEntryPtr _server;
};
typedef IceUtil::Handle<ServerAdapterEntry> ServerAdapterEntryPtr;

class ReplicaGroupEntry : public AdapterEntry
{
public:

    ReplicaGroupEntry(AdapterCache&, const std::string&, const std::string&, const LoadBalancingPolicyPtr&);

    virtual std::vector<std::pair<std::string, AdapterPrx> > getProxies(int&, const SessionIPtr&);
    virtual float getLeastLoadedNodeLoad(LoadSample) const;
    virtual std::string getApplication() const;
    virtual AdapterInfoSeq getAdapterInfo() const;

    void addReplica(const std::string&, const ServerAdapterEntryPtr&);
    void removeReplica(const std::string&);

    void update(const LoadBalancingPolicyPtr&);

    typedef std::vector<std::pair<std::string, ServerAdapterEntryPtr> > ReplicaSeq;

private:

    const std::string _application;

    LoadBalancingPolicyPtr _loadBalancing;
    int _loadBalancingNReplicas;
    LoadSample _loadSample;
    ReplicaSeq _replicas;
    int _lastReplica;

    static std::pointer_to_unary_function<int, int> _rand;
};
typedef IceUtil::Handle<ReplicaGroupEntry> ReplicaGroupEntryPtr;

class AdapterCache : public CacheByString<AdapterEntry>
{
public:

    ServerAdapterEntryPtr addServerAdapter(const std::string&, const std::string&, bool, const ServerEntryPtr&);
    ReplicaGroupEntryPtr addReplicaGroup(const std::string&, const std::string&, const LoadBalancingPolicyPtr&);

    AdapterEntryPtr get(const std::string&) const;
    ServerAdapterEntryPtr getServerAdapter(const std::string&) const;
    ReplicaGroupEntryPtr getReplicaGroup(const std::string&) const;
    
    void removeServerAdapter(const std::string&);
    void removeReplicaGroup(const std::string&);

protected:
    
    virtual AdapterEntryPtr addImpl(const std::string&, const AdapterEntryPtr&);
    virtual AdapterEntryPtr removeImpl(const std::string&);

};

};

#endif