/
test_oplog_manager_sharded.py
410 lines (322 loc) · 14.2 KB
/
test_oplog_manager_sharded.py
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# Copyright 20134-2014 MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
Module with code to setup cluster and test oplog_manager functions.
This is the main tester method. All the functions can be called by
finaltests.py
"""
import os
import sys
import inspect
import socket
sys.path[0:0] = [""]
import time
if sys.version_info[:2] == (2, 6):
import unittest2 as unittest
else:
import unittest
import re
try:
from pymongo import MongoClient as Connection
except ImportError:
from pymongo import Connection
from mongo_connector.doc_managers.doc_manager_simulator import DocManager
from mongo_connector.locking_dict import LockingDict
from tests.setup_cluster import (kill_mongo_proc,
start_mongo_proc,
start_cluster,
kill_all)
from pymongo.errors import OperationFailure
from os import path
from mongo_connector.oplog_manager import OplogThread
from mongo_connector.util import (long_to_bson_ts,
bson_ts_to_long,
retry_until_ok)
from bson.objectid import ObjectId
PORTS_ONE = {"PRIMARY": "27117", "SECONDARY": "27118", "ARBITER": "27119",
"CONFIG": "27220", "MONGOS": "27217"}
PORTS_TWO = {"PRIMARY": "27317", "SECONDARY": "27318", "ARBITER": "27319",
"CONFIG": "27220", "MONGOS": "27217"}
SETUP_DIR = path.expanduser("~/mongo_connector")
DEMO_SERVER_DATA = SETUP_DIR + "/data"
DEMO_SERVER_LOG = SETUP_DIR + "/logs"
MONGOD_KSTR = " --dbpath " + DEMO_SERVER_DATA
MONGOS_KSTR = "mongos --port " + PORTS_ONE["MONGOS"]
AUTH_FILE = os.environ.get('AUTH_FILE', None)
AUTH_USERNAME = os.environ.get('AUTH_USERNAME', None)
HOSTNAME = os.environ.get('HOSTNAME', socket.gethostname())
TEMP_CONFIG = os.environ.get('TEMP_CONFIG', "temp_config.txt")
CONFIG = os.environ.get('CONFIG', "config.txt")
def safe_mongo_op(func, arg1, arg2=None):
"""Performs the given operation with the safe argument
"""
count = 0
while True:
time.sleep(1)
count += 1
if (count > 60):
self.fail('Call %s failed too many times in safe_mongo_op' % (func))
try:
if arg2:
func(arg1, arg2, safe=True)
else:
func(arg1, safe=True)
break
except OperationFailure:
pass
class TestOplogManagerSharded(unittest.TestCase):
"""Defines all the testing methods for a sharded cluster
"""
def runTest(self):
""" Runs the tests
"""
unittest.TestCase.__init__(self)
@classmethod
def setUpClass(cls):
""" Initializes the cluster
"""
os.system('rm %s; touch %s' % (CONFIG, CONFIG))
cls.AUTH_KEY = None
cls.flag = True
if AUTH_FILE:
# We want to get the credentials from this file
try:
key = (open(AUTH_FILE)).read()
re.sub(r'\s', '', key)
cls.AUTH_KEY = key
except IOError:
print('Could not parse authentication file!')
cls.flag = False
cls.err_msg = "Could not read key file!"
if not start_cluster(sharded=True, key_file=cls.AUTH_KEY):
cls.flag = False
cls.err_msg = "Shards cannot be added to mongos"
def setUp(self):
""" Fails if we can't read the key file or if the
cluster cannot be created.
"""
if not self.flag:
self.fail(self.err_msg)
@classmethod
def tearDownClass(cls):
""" Kills cluster instance
"""
kill_all()
@classmethod
def get_oplog_thread(cls):
""" Set up connection with mongo.
Returns oplog, the connection and oplog collection.
This function clears the oplog.
"""
primary_conn = Connection(HOSTNAME,int(PORTS_ONE["PRIMARY"]))
if primary_conn['admin'].command("isMaster")['ismaster'] is False:
primary_conn = Connection(HOSTNAME, int(PORTS_ONE["SECONDARY"]))
mongos_addr = "%s:%s" % (HOSTNAME, PORTS_ONE["MONGOS"])
mongos = Connection(mongos_addr)
mongos['alpha']['foo'].drop()
oplog_coll = primary_conn['local']['oplog.rs']
oplog_coll.drop() # reset the oplog
primary_conn['local'].create_collection('oplog.rs', capped=True,
size=1000000)
namespace_set = ['test.test', 'alpha.foo']
doc_manager = DocManager()
oplog = OplogThread(primary_conn, mongos_addr, oplog_coll, True,
doc_manager, LockingDict(), namespace_set,
cls.AUTH_KEY, AUTH_USERNAME)
return (oplog, primary_conn, oplog_coll, mongos)
@classmethod
def get_new_oplog(cls):
""" Set up connection with mongo.
Returns oplog, the connection and oplog collection
This function does not clear the oplog
"""
primary_conn = Connection(HOSTNAME, int(PORTS_ONE["PRIMARY"]))
if primary_conn['admin'].command("isMaster")['ismaster'] is False:
primary_conn = Connection(HOSTNAME, int(PORTS_ONE["SECONDARY"]))
mongos = "%s:%s" % (HOSTNAME, PORTS_ONE["MONGOS"])
oplog_coll = primary_conn['local']['oplog.rs']
namespace_set = ['test.test', 'alpha.foo']
doc_manager = DocManager()
oplog = OplogThread(primary_conn, mongos, oplog_coll, True,
doc_manager, LockingDict(), namespace_set,
cls.AUTH_KEY, AUTH_USERNAME)
return (oplog, primary_conn, oplog_coll, oplog.main_connection)
def test_retrieve_doc(self):
"""Test retrieve_doc in oplog_manager.
Assertion failure if it doesn't pass
"""
test_oplog, oplog_cursor, oplog_coll, mongos = self.get_oplog_thread()
# testing for entry as none type
entry = None
assert (test_oplog.retrieve_doc(entry) is None)
oplog_cursor = oplog_coll.find({}, tailable=True, await_data=True)
assert (oplog_cursor.count() == 0)
safe_mongo_op(mongos['alpha']['foo'].insert, {'name': 'paulie'})
last_oplog_entry = next(oplog_cursor)
target_entry = mongos['alpha']['foo'].find_one()
# testing for search after inserting a document
assert (test_oplog.retrieve_doc(last_oplog_entry) == target_entry)
safe_mongo_op(mongos['alpha']['foo'].update, {'name': 'paulie'},
{"$set": {'name': 'paul'}})
last_oplog_entry = next(oplog_cursor)
target_entry = mongos['alpha']['foo'].find_one()
# testing for search after updating a document
assert (test_oplog.retrieve_doc(last_oplog_entry) == target_entry)
safe_mongo_op(mongos['alpha']['foo'].remove, {'name': 'paul'})
last_oplog_entry = next(oplog_cursor)
# testing for search after deleting a document
assert (test_oplog.retrieve_doc(last_oplog_entry) is None)
last_oplog_entry['o']['_id'] = 'badID'
# testing for bad doc id as input
assert (test_oplog.retrieve_doc(last_oplog_entry) is None)
# test_oplog.stop()
def test_get_oplog_cursor(self):
"""Test get_oplog_cursor in oplog_manager.
Assertion failure if it doesn't pass
"""
test_oplog, timestamp, cursor, mongos = self.get_oplog_thread()
# test None cursor
assert (test_oplog.get_oplog_cursor(None) is None)
# test with one document
safe_mongo_op(mongos['alpha']['foo'].insert, {'name': 'paulie'})
timestamp = test_oplog.get_last_oplog_timestamp()
cursor = test_oplog.get_oplog_cursor(timestamp)
assert (cursor.count() == 1)
# test with two documents, one after the ts
safe_mongo_op(mongos['alpha']['foo'].insert, {'name': 'paul'})
cursor = test_oplog.get_oplog_cursor(timestamp)
assert (cursor.count() == 2)
def test_get_last_oplog_timestamp(self):
"""Test get_last_oplog_timestamp in oplog_manager.
Assertion failure if it doesn't pass
"""
# test empty oplog
test_oplog, oplog_cursor, oplog_coll, mongos = self.get_oplog_thread()
assert (test_oplog.get_last_oplog_timestamp() is None)
# test non-empty oplog
oplog_cursor = oplog_coll.find({}, tailable=True, await_data=True)
safe_mongo_op(mongos['alpha']['foo'].insert, {'name': 'paulie'})
last_oplog_entry = next(oplog_cursor)
last_ts = last_oplog_entry['ts']
assert (test_oplog.get_last_oplog_timestamp() == last_ts)
# test_oplog.stop()
def test_dump_collection(self):
"""Test dump_collection in oplog_manager.
Assertion failure if it doesn't pass
"""
test_oplog, search_ts, solr, mongos = self.get_oplog_thread()
# with documents
safe_mongo_op(mongos['alpha']['foo'].insert, {'name': 'paulie'})
search_ts = test_oplog.get_last_oplog_timestamp()
test_oplog.dump_collection()
docman = test_oplog.doc_managers[0]
docman.commit()
solr_results = docman._search()
assert (len(solr_results) == 1)
solr_doc = solr_results[0]
assert (long_to_bson_ts(solr_doc['_ts']) == search_ts)
assert (solr_doc['name'] == 'paulie')
assert (solr_doc['ns'] == 'alpha.foo')
def test_init_cursor(self):
"""Test init_cursor in oplog_manager.
Assertion failure if it doesn't pass
"""
test_oplog, search_ts, cursor, mongos = self.get_oplog_thread()
test_oplog.checkpoint = None # needed for these tests
# initial tests with no config file and empty oplog
assert (test_oplog.init_cursor() is None)
# no config, single oplog entry
safe_mongo_op(mongos['alpha']['foo'].insert, {'name': 'paulie'})
search_ts = test_oplog.get_last_oplog_timestamp()
cursor = test_oplog.init_cursor()
assert (cursor.count() == 1)
assert (test_oplog.checkpoint == search_ts)
# with config file, assert that size != 0
os.system('touch %s' % (TEMP_CONFIG))
cursor = test_oplog.init_cursor()
oplog_dict = test_oplog.oplog_progress.get_dict()
assert(cursor.count() == 1)
self.assertTrue(str(test_oplog.oplog) in oplog_dict)
commit_ts = test_oplog.checkpoint
self.assertTrue(oplog_dict[str(test_oplog.oplog)] == commit_ts)
os.system('rm %s' % (TEMP_CONFIG))
def test_rollback(self):
"""Test rollback in oplog_manager. Assertion failure if it doesn't pass
We force a rollback by inserting a doc, killing primary, inserting
another doc, killing the new primary, and then restarting both
servers.
"""
os.system('rm %s; touch %s' % (CONFIG, CONFIG))
if not start_cluster(sharded=True):
self.fail("Shards cannot be added to mongos")
test_oplog, primary_conn, solr, mongos = self.get_new_oplog()
solr = test_oplog.doc_managers[0]
solr._delete() # equivalent to solr.delete(q='*:*')
safe_mongo_op(mongos['alpha']['foo'].remove, {})
safe_mongo_op(mongos['alpha']['foo'].insert,
{'_id': ObjectId('4ff74db3f646462b38000001'),
'name': 'paulie'})
cutoff_ts = test_oplog.get_last_oplog_timestamp()
obj2 = ObjectId('4ff74db3f646462b38000002')
first_doc = {'name': 'paulie', '_ts': bson_ts_to_long(cutoff_ts),
'ns': 'alpha.foo',
'_id': ObjectId('4ff74db3f646462b38000001')}
# try kill one, try restarting
kill_mongo_proc(primary_conn.host, PORTS_ONE['PRIMARY'])
new_primary_conn = Connection(HOSTNAME, int(PORTS_ONE['SECONDARY']))
admin_db = new_primary_conn['admin']
while admin_db.command("isMaster")['ismaster'] is False:
time.sleep(1)
time.sleep(5)
count = 0
while True:
try:
mongos['alpha']['foo'].insert({'_id': obj2, 'name': 'paul'})
break
except OperationFailure:
time.sleep(1)
count += 1
if count > 60:
self.fail('Insert failed too many times in rollback')
continue
kill_mongo_proc(primary_conn.host, PORTS_ONE['SECONDARY'])
start_mongo_proc(PORTS_ONE['PRIMARY'], "demo-repl", "/replset1a",
"/replset1a.log", None)
# wait for master to be established
while primary_conn['admin'].command("isMaster")['ismaster'] is False:
time.sleep(1)
start_mongo_proc(PORTS_ONE['SECONDARY'], "demo-repl", "/replset1b",
"/replset1b.log", None)
# wait for secondary to be established
admin_db = new_primary_conn['admin']
while admin_db.command("replSetGetStatus")['myState'] != 2:
time.sleep(1)
while retry_until_ok(mongos['alpha']['foo'].find().count) != 1:
time.sleep(1)
self.assertEqual(str(new_primary_conn.port), PORTS_ONE['SECONDARY'])
self.assertEqual(str(primary_conn.port), PORTS_ONE['PRIMARY'])
last_ts = test_oplog.get_last_oplog_timestamp()
second_doc = {'name': 'paul', '_ts': bson_ts_to_long(last_ts),
'ns': 'alpha.foo', '_id': obj2}
solr.upsert(first_doc)
solr.upsert(second_doc)
test_oplog.rollback()
solr.commit()
results = solr._search()
self.assertEqual(len(results), 1)
results_doc = results[0]
self.assertEqual(results_doc['name'], 'paulie')
self.assertTrue(results_doc['_ts'] <= bson_ts_to_long(cutoff_ts))
if __name__ == '__main__':
unittest.main()