summaryrefslogtreecommitdiff
path: root/jstests/slowWeekly/indexbg_dur.js
blob: 5fbe0e706daba0a2f1961e2a7a6f6a5f11cc309d (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
/**
 * Kill mongod during a background index build and ensure that the bad index
 * can be dropped on restart.
 */

function countFields( x ) {
    var count = 0;
    for( var i in x ) {
        ++count;
    }
    return count;
}

size = 100000;
while( 1 ) {
    print( "size: " + size );

    var testname = "index_build";
    var path = "/data/db/" + testname+"_dur";
    conn = startMongodEmpty("--port", 30001, "--dbpath", path, "--dur", "--smallfiles", "--durOptions", 8);
    t = conn.getDB( testname ).getCollection( testname );

    for( var i = 0; i < size; ++i ) {
        t.save( {i:i} );
    }
    t.getDB().getLastError();
    x = startMongoProgramNoConnect( "mongo", "--eval", "db.getSisterDB( '" + testname + "' )." + testname + ".ensureIndex( {i:1}, {background:true} );", conn.host );
    sleep( 1000 );
    stopMongod( 30001, /* signal */ 9 );
    waitProgram( x );
    
    conn = startMongodNoReset("--port", 30001, "--dbpath", path, "--dur", "--smallfiles", "--durOptions", 8);
    t = conn.getDB( testname ).getCollection( testname );
    
    var statsSize = countFields( t.stats().indexSizes );
    var nsSize = conn.getDB( testname ).system.indexes.count( {ns:testname+'.'+testname} );
    
    // If index build completed before the kill, try again with more data.
    if ( !( statsSize == 1 && nsSize == 2 ) ) {
        print( "statsSize: " + statsSize + ", nsSize: " + nsSize + ", retrying with more data" );
        stopMongod( 30001 );
        size *= 2;
        continue;
    }
    
    assert.eq( "index not found", t.dropIndex( "i_1" ).errmsg );
    
    var statsSize = countFields( t.stats().indexSizes );
    var nsSize = conn.getDB( testname ).system.indexes.count( {ns:testname+'.'+testname} );

    assert.eq( statsSize, nsSize );
    assert( t.validate().valid );
    // TODO check that index namespace is cleaned up as well once that is implemented
    
    t.ensureIndex( {i:1} );
    var statsSize = countFields( t.stats().indexSizes );
    var nsSize = conn.getDB( testname ).system.indexes.count( {ns:testname+'.'+testname} );

    assert.eq( 2, statsSize );
    assert.eq( 2, nsSize );
    
    exp = t.find( {i:20} ).explain();
    assert.eq( 1, exp.n );
    assert.eq( 'BtreeCursor i_1', exp.cursor );
    
    break;
}