summaryrefslogtreecommitdiff
path: root/s/writeback_listener.h
blob: 73359999b13de563526f01a8f59e4b690a5804b3 (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
// @file writeback_listener.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 "../client/connpool.h"
#include "../util/background.h"
#include "../db/client.h"

namespace mongo {

    /*
     * The writeback listener takes back write attempts that were made against a wrong shard.
     * (Wrong here in the sense that the target chunk moved before this mongos had a chance to
     * learn so.) It is responsible for reapplying these writes to the correct shard.
     *
     * Currently, there is one listener per shard.
     */
    class WriteBackListener : public BackgroundJob {
    public:
        static void init( DBClientBase& conn );
        static void init( const string& host );

        static BSONObj waitFor( ConnectionId connectionId, const OID& oid );

    protected:
        WriteBackListener( const string& addr );

        string name() const { return "WriteBackListener"; }
        void run();

    private:
        string _addr;
        
        static mongo::mutex _cacheLock; // protects _cache
        static map<string,WriteBackListener*> _cache; // server to listener
        static set<string> _seenSets; // cache of set urls we've seen - note this is ever expanding for order, case, changes

        struct WBStatus {
            OID id;
            BSONObj gle;
        };

        static mongo::mutex _seenWritebacksLock;  // protects _seenWritbacks
        static map<ConnectionId,WBStatus> _seenWritebacks; // connectionId -> last write back GLE
    };

    void waitForWriteback( const OID& oid );

}  // namespace mongo