summaryrefslogtreecommitdiff
path: root/jstests/replsets/fastsync.js
blob: 1c9c2152ebb83978031be26c18151886bb44cecd (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/*
 * 1. insert 100000 objects
 * 2. export to two dbpaths
 * 3. add one node w/fastsync
 * 4. check that we never get "errmsg" : "initial sync cloning db: whatever"
 * 5. check writes are replicated
 */

var w = 0;
var wait = function(f) {
    w++;
    var n = 0;
    while (!f()) {
        if( n % 4 == 0 )
            print("toostale.js waiting " + w);
        if (++n == 4) {
            print("" + f);
        }
        assert(n < 200, 'tried 200 times, giving up');
        sleep(1000);
    }
}

var reconnect = function(a) {
  wait(function() {
      try {
        a.getDB("foo").bar.stats();
        return true;
      } catch(e) {
        print(e);
        return false;
      }
    });
};

ports = allocatePorts( 4 );

var basename = "jstests_fastsync";
var basePath = "/data/db/" + basename;
var hostname = getHostName();

var pargs = new MongodRunner( ports[ 0 ], basePath + "-p", false, false,
                              ["--replSet", basename, "--oplogSize", 2],
                              {no_bind : true} );
p = pargs.start();

var admin = p.getDB("admin");
var foo = p.getDB("foo");
var local = p.getDB("local");

var config = {_id : basename, members : [{_id : 0, host : hostname+":"+ports[0], priority:2}]};
printjson(config);
var result = admin.runCommand({replSetInitiate : config});
print("result:");
printjson(result);

var count = 0;
while (count < 10 && result.ok != 1) {
  count++;
  sleep(2000);
  result = admin.runCommand({replSetInitiate : config});
}

assert(result.ok, tojson(result));
assert.soon(function() { result = false;
        try {
            result = admin.runCommand({isMaster:1}).ismaster;
        }
        catch(e) {
            print(e);
            return false;
        }
        return result;
    });

print("1");
for (var i=0; i<100000; i++) {
  foo.bar.insert({date : new Date(), x : i, str : "all the talk on the market"});
}
print("total in foo: "+foo.bar.count());


print("2");
admin.runCommand( {fsync:1,lock:1} );
copyDbpath( basePath + "-p", basePath + "-s"+1 );
admin.$cmd.sys.unlock.findOne();

print("3");
var startSlave = function(n) {
    var sargs = new MongodRunner( ports[ n ], basePath + "-s"+n, false, false,
                              ["--replSet", basename, "--fastsync",
                               "--oplogSize", 2], {no_bind : true} );
    var reuseData = true;
    var conn = sargs.start(reuseData);

    config = local.system.replset.findOne();
    config.version++;
    config.members.push({_id:n, host:hostname+":"+ports[n]});

    result = admin.runCommand({replSetReconfig : config});
    printjson(result);
    assert(result.ok, "reconfig worked");
    reconnect(p);

    print("4");
    var status = admin.runCommand({replSetGetStatus : 1});
    var count = 0;
    while (status.members[n].state != 2 && count < 200) {
        print("not a secondary yet");
        if (count % 10 == 0) {
            printjson(status);
        }
        assert(!status.members[n].errmsg || !status.members[n].errmsg.match("^initial sync cloning db"));

        sleep(1000);

        // disconnection could happen here
        try {
            status = admin.runCommand({replSetGetStatus : 1});
        }
        catch (e) {
            print(e);
        }
        count++;
    }

    assert.eq(status.members[n].state, 2);

    assert.soon(function() {
        return admin.runCommand({isMaster : 1}).ismaster;
    });

    admin.foo.insert({x:1});
    assert.soon(function() {
        var last = local.oplog.rs.find().sort({$natural:-1}).limit(1).next();
        var cur = conn.getDB("local").oplog.rs.find().sort({$natural:-1}).limit(1).next();
        print("last: "+tojson(last)+" cur: "+tojson(cur));
        return cur != null && last != null && cur.ts.t == last.ts.t && cur.ts.i == last.ts.i;
    });

    return conn;
};

var s1 = startSlave(1);

var me1 = s1.getDB("local").me.findOne();

print("me: " +me1._id);
assert(me1._id != null);

print("5");
s1.getDB("admin").runCommand( {fsync:1,lock:1} );
copyDbpath( basePath + "-s1", basePath + "-s2" );
s1.getDB("admin").$cmd.sys.unlock.findOne();

var s2 = startSlave(2);

var me2 = s2.getDB("local").me.findOne();

print("me: " +me2._id);
assert(me1._id != me2._id);

print("restart member with a different port and make it a new set");
try {
  p.getDB("admin").runCommand({shutdown:1});
}
catch(e) {
  print("good, shutting down: " +e);
}
sleep(10000);

pargs = new MongodRunner( ports[ 3 ], basePath + "-p", false, false,
                          ["--replSet", basename, "--oplogSize", 2],
                          {no_bind : true} );
p = pargs.start(true);

printjson(p.getDB("admin").runCommand({replSetGetStatus:1}));

p.getDB("admin").runCommand({replSetReconfig : {
      _id : basename,
      members : [{_id:0, host : hostname+":"+ports[3]}]
        }, force : true});

print("start waiting for primary...");
assert.soon(function() {
    try {
      return p.getDB("admin").runCommand({isMaster : 1}).ismaster;
    }
    catch(e) {
      print(e);
    }
    return false;
  }, "waiting for master", 60000);