Skip to content

Commit b44e68f

Browse files
committed
Speed up node initialization
1 parent 9feba10 commit b44e68f

File tree

2 files changed

+52
-17
lines changed

2 files changed

+52
-17
lines changed

testgres/testgres.py

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@
5050

5151
registered_nodes = []
5252
util_threads = []
53+
tmp_dirs = []
5354
last_assigned_port = int(random.random() * 16384) + 49152
5455
pg_config_data = {}
56+
base_data_dir = None
5557

5658

5759
class ClusterException(Exception):
@@ -208,6 +210,7 @@ def __init__(self, name, port, base_dir=None, use_logging=False):
208210
self.port = port
209211
if base_dir is None:
210212
self.base_dir = tempfile.mkdtemp()
213+
tmp_dirs.append(self.base_dir)
211214
else:
212215
self.base_dir = base_dir
213216
if not os.path.exists(self.logs_dir):
@@ -246,6 +249,31 @@ def get_bin_path(self, filename):
246249
else:
247250
return os.path.join(pg_config.get("BINDIR"), filename)
248251

252+
def initdb(self, directory, initdb_params=[]):
253+
initdb = self.get_bin_path("initdb")
254+
initdb_logfile = os.path.join(self.logs_dir, "initdb.log")
255+
256+
with open(initdb_logfile, 'a') as file_out:
257+
ret = subprocess.call(
258+
[initdb, directory, "-N"] + initdb_params,
259+
stdout=file_out,
260+
stderr=subprocess.STDOUT)
261+
262+
if ret:
263+
raise ClusterException("Cluster initialization failed. You"
264+
" can find additional information at '%s'" % initdb_logfile)
265+
266+
def _setup_data_dir(self, data_dir):
267+
global base_data_dir
268+
269+
if base_data_dir is None:
270+
base_data_dir = tempfile.mkdtemp()
271+
tmp_dirs.append(base_data_dir)
272+
self.initdb(base_data_dir)
273+
274+
shutil.copytree(base_data_dir, data_dir)
275+
276+
249277
def init(self, allows_streaming=False, initdb_params=[]):
250278
""" Performs initdb """
251279

@@ -258,19 +286,13 @@ def init(self, allows_streaming=False, initdb_params=[]):
258286
return self
259287

260288
# initialize cluster
261-
os.makedirs(self.data_dir)
262-
initdb = self.get_bin_path("initdb")
263-
with open(os.path.join(self.logs_dir, "initdb.log"), "a") as file_out:
264-
ret = subprocess.call(
265-
[initdb, self.data_dir, "-N"] + initdb_params,
266-
stdout=file_out,
267-
stderr=subprocess.STDOUT
268-
)
269-
if ret:
270-
raise ClusterException("Cluster initialization failed")
289+
if initdb_params:
290+
self.initdb(self.data_dir, initdb_params)
291+
else:
292+
self._setup_data_dir(self.data_dir)
271293

272294
# add parameters to config file
273-
with open(postgres_conf, "a") as conf:
295+
with open(postgres_conf, "w") as conf:
274296
conf.write(
275297
"fsync = off\n"
276298
"log_statement = all\n"
@@ -286,7 +308,7 @@ def init(self, allows_streaming=False, initdb_params=[]):
286308
"max_wal_senders = 5\n"
287309
"wal_keep_segments = 20\n"
288310
"max_wal_size = 128MB\n"
289-
"shared_buffers = 1MB\n"
311+
"shared_buffers = 128MB\n"
290312
"wal_log_hints = on\n"
291313
"hot_standby = on\n"
292314
"max_connections = 10\n")
@@ -369,7 +391,9 @@ def pg_ctl(self, command, params={}, command_options=[]):
369391

370392
if res > 0:
371393
with open(self.error_filename, "r") as errfile:
372-
raise ClusterException(errfile.readlines()[-1])
394+
text = errfile.readlines()[-1]
395+
text += 'Logs at: %s' % self.logs_dir
396+
raise ClusterException(text)
373397

374398
def start(self, params={}):
375399
""" Starts cluster """
@@ -486,8 +510,7 @@ def cleanup(self):
486510
pass
487511

488512
# remove data directory
489-
shutil.rmtree(self.data_dir)
490-
513+
shutil.rmtree(self.data_dir, ignore_errors=True)
491514
return self
492515

493516
def psql(self, dbname, query=None, filename=None, username=None):
@@ -723,7 +746,16 @@ def clean_all():
723746
global registered_nodes
724747
for node in registered_nodes:
725748
node.cleanup()
749+
750+
for cat in tmp_dirs:
751+
if os.path.exists():
752+
shutil.rmtree(cat, ignore_errors=True)
753+
754+
if cat == base_data_dir:
755+
base_data_dir = None
756+
726757
registered_nodes = []
758+
tmp_dirs = []
727759

728760

729761
def stop_all():

testgres/tests/test_simple.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
import unittest
2+
import six
3+
24
from testgres import get_new_node, stop_all
35

46

57
class SimpleTest(unittest.TestCase):
68

79
def teardown(self):
8-
# clean_all()
10+
clean_all()
911
stop_all()
1012

1113
@unittest.skip("demo")
@@ -73,7 +75,8 @@ def test_users(self):
7375
node.init().start()
7476
node.psql('postgres', 'create role test_user login')
7577
value = node.safe_psql('postgres', 'select 1', username='test_user')
76-
self.assertEqual(value, '1\n')
78+
self.assertEqual(value, six.b('1\n'))
79+
node.stop()
7780

7881

7982
if __name__ == '__main__':

0 commit comments

Comments
 (0)