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
|
// from server 2326 - make sure that sharding only works with unique indices
s = new ShardingTest( "shard_index", 2, 50, 1 )
// Regenerate fully because of SERVER-2782
for ( var i = 0; i < 10; i++ ) {
var coll = s.admin._mongo.getDB( "test" ).getCollection( "foo" + i )
coll.drop()
for ( var j = 0; j < 300; j++ ) {
coll.insert( { num : j, x : 1 } )
}
if(i == 0) s.adminCommand( { enablesharding : "" + coll._db } );
print("\n\n\n\n\nTest # " + i)
if ( i == 0 ) {
// Unique index exists, but not the right one.
coll.ensureIndex( { num : 1 }, { unique : true } )
coll.ensureIndex( { x : 1 } )
passed = false
try {
s.adminCommand( { shardcollection : "" + coll, key : { x : 1 } } )
passed = true
} catch (e) {
print( e )
}
assert( !passed, "Should not shard collection when another unique index exists!")
}
if ( i == 1 ) {
// Unique index exists as prefix, also index exists
coll.ensureIndex( { x : 1 } )
coll.ensureIndex( { x : 1, num : 1 }, { unique : true } )
try{
s.adminCommand({ shardcollection : "" + coll, key : { x : 1 } })
}
catch(e){
print(e)
assert( false, "Should be able to shard non-unique index without unique option.")
}
}
if ( i == 2 ) {
if (false) { // SERVER-3718
// Non-unique index exists as prefix, also index exists. No unique index.
coll.ensureIndex( { x : 1 } )
coll.ensureIndex( { x : 1, num : 1 } )
passed = false;
try{
s.adminCommand({ shardcollection : "" + coll, key : { x : 1 } })
passed = true;
}
catch( e ){
print(e)
}
assert( !passed, "Should not shard collection with no unique index.")
}
}
if ( i == 3 ) {
// Unique index exists as prefix, also unique index exists
coll.ensureIndex( { num : 1 }, { unique : true })
coll.ensureIndex( { num : 1 , x : 1 }, { unique : true } )
try{
s.adminCommand({ shardcollection : "" + coll, key : { num : 1 }, unique : true })
}
catch( e ){
print(e)
assert( false, "Should be able to shard collection with unique prefix index.")
}
}
if ( i == 4 ) {
// Unique index exists as id, also unique prefix index exists
coll.ensureIndex( { _id : 1, num : 1 }, { unique : true } )
try{
s.adminCommand({ shardcollection : "" + coll, key : { _id : 1 }, unique : true })
}
catch( e ){
print(e)
assert( false, "Should be able to shard collection with unique id index.")
}
}
if ( i == 5 ) {
// Unique index exists as id, also unique prefix index exists
coll.ensureIndex( { _id : 1, num : 1 }, { unique : true } )
try{
s.adminCommand({ shardcollection : "" + coll, key : { _id : 1, num : 1 }, unique : true })
}
catch( e ){
print(e)
assert( false, "Should be able to shard collection with unique combination id index.")
}
}
if ( i == 6 ) {
coll.remove()
// Unique index does not exist, also unique prefix index exists
coll.ensureIndex( { num : 1, _id : 1 }, { unique : true } )
try{
s.adminCommand({ shardcollection : "" + coll, key : { num : 1 }, unique : true })
}
catch( e ){
print(e)
assert( false, "Should be able to shard collection with no unique index but with a unique prefix index.")
}
}
if ( i == 7 ) {
coll.remove()
// No index exists
try{
s.adminCommand({ shardcollection : "" + coll, key : { num : 1 } })
}
catch( e ){
print(e)
assert( !passed, "Should be able to shard collection with no index on shard key.")
}
}
if ( i == 8 ) {
if (false) { // SERVER-3718
coll.remove()
// No index exists
passed = false
try{
s.adminCommand({ shardcollection : "" + coll, key : { num : 1 }, unique : true })
passed = true
}
catch( e ){
print(e)
}
assert( !passed, "Should not shard collection with unique flag but with no unique index on shard key.")
}
}
if ( i == 9 ) {
// Unique index exists on a different field as well
coll.ensureIndex( { num : 1 }, { unique : true } )
coll.ensureIndex( { x : 1 }, { unique : true} )
passed = false
try {
s.adminCommand( { shardcollection : "" + coll, key : { x : 1 } } )
passed = true
} catch (e) {
print( e )
}
assert( !passed, "Should not shard collection when another unique index exists!" )
}
}
s.stop();
|