summaryrefslogtreecommitdiff
path: root/s/d_logic.h
blob: a000f6ba830f0f60728fb23e191478c55c8f733d (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
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
// d_logic.h
/*
 *    Copyright (C) 2010 10gen Inc.
 *
 *    This program is free software: you can redistribute it and/or  modify
 *    it under the terms of the GNU Affero General Public License, version 3,
 *    as published by the Free Software Foundation.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU Affero General Public License for more details.
 *
 *    You should have received a copy of the GNU Affero General Public License
 *    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


#pragma once

#include "../pch.h"
#include "../db/jsobj.h"
#include "util.h"

namespace mongo {
    
    class ShardingState;
    
    typedef ShardChunkVersion ConfigVersion;
    typedef map<string,ConfigVersion> NSVersionMap;

    // -----------

    class ChunkMatcher {
        typedef map<BSONObj,pair<BSONObj,BSONObj>,BSONObjCmp> MyMap;
    public:
        
        bool belongsToMe( const BSONObj& key , const DiskLoc& loc ) const;

    private:
        ChunkMatcher( ConfigVersion version );
        
        void gotRange( const BSONObj& min , const BSONObj& max );
        
        ConfigVersion _version;
        BSONObj _key;
        MyMap _map;
        
        friend class ShardingState;
    };

    typedef shared_ptr<ChunkMatcher> ChunkMatcherPtr;
    
    // --------------
    // --- global state ---
    // --------------

    class ShardingState {
    public:
        ShardingState();
        
        bool enabled() const { return _enabled; }
        const string& getConfigServer() const { return _configServer; }
        void enable( const string& server );

        void gotShardName( const string& name );
        void gotShardHost( const string& host );
        
        bool hasVersion( const string& ns );
        bool hasVersion( const string& ns , ConfigVersion& version );
        ConfigVersion& getVersion( const string& ns ); // TODO: this is dangeroues
        void setVersion( const string& ns , const ConfigVersion& version );
        
        void appendInfo( BSONObjBuilder& b );
        
        ChunkMatcherPtr getChunkMatcher( const string& ns );
        
        bool inCriticalMigrateSection();
    private:
        
        bool _enabled;
        
        string _configServer;
        
        string _shardName;
        string _shardHost;

        mongo::mutex _mutex;
        NSVersionMap _versions;
        map<string,ChunkMatcherPtr> _chunks;
    };
    
    extern ShardingState shardingState;

    // --------------
    // --- per connection ---
    // --------------
    
    class ShardedConnectionInfo {
    public:
        ShardedConnectionInfo();
        
        const OID& getID() const { return _id; }
        bool hasID() const { return _id.isSet(); }
        void setID( const OID& id );
        
        ConfigVersion& getVersion( const string& ns ); // TODO: this is dangeroues
        void setVersion( const string& ns , const ConfigVersion& version );
        
        static ShardedConnectionInfo* get( bool create );
        static void reset();
        
        bool inForceMode() const { 
            return _forceMode;
        }
        
        void enterForceMode(){ _forceMode = true; }
        void leaveForceMode(){ _forceMode = false; }

    private:
        
        OID _id;
        NSVersionMap _versions;
        bool _forceMode;

        static boost::thread_specific_ptr<ShardedConnectionInfo> _tl;
    };

    struct ShardForceModeBlock {
        ShardForceModeBlock(){
            info = ShardedConnectionInfo::get( false );
            if ( info )
                info->enterForceMode();
        }
        ~ShardForceModeBlock(){
            if ( info )
                info->leaveForceMode();
        }

        ShardedConnectionInfo * info;
    };
    
    // -----------------
    // --- core ---
    // -----------------

    unsigned long long extractVersion( BSONElement e , string& errmsg );

    
    /**
     * @return true if we have any shard info for the ns
     */
    bool haveLocalShardingInfo( const string& ns );
    
    /**
     * @return true if the current threads shard version is ok, or not in sharded version
     */
    bool shardVersionOk( const string& ns , bool write , string& errmsg );

    /**
     * @return true if we took care of the message and nothing else should be done
     */
    bool handlePossibleShardedMessage( Message &m, DbResponse * dbresponse );

    void logOpForSharding( const char * opstr , const char * ns , const BSONObj& obj , BSONObj * patt );

    // -----------------
    // --- writeback ---
    // -----------------

    /* queue a write back on a remote server for a failed write */
    void queueWriteBack( const string& remote , const BSONObj& o );

}