Skip to content

Commit

Permalink
Getting syncdbAfterWrite working (#1857)
Browse files Browse the repository at this point in the history
Co-authored-by: John Stilley <1831479+john-science@users.noreply.github.com>
  • Loading branch information
zachmprince and john-science authored Sep 12, 2024
1 parent 55d098d commit 57dc6fe
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
11 changes: 11 additions & 0 deletions armi/bookkeeping/db/database3.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"""
import collections
import copy
import gc
import io
import itertools
import os
Expand Down Expand Up @@ -683,8 +684,18 @@ def syncToSharedFolder(self):
"""
runLog.extra("Copying DB to shared working directory.")
self.h5db.flush()

# Close the h5 file so it can be copied
self.h5db.close()
self.h5db = None
shutil.copy(self._fullPath, self._fileName)

# Garbage collect so we don't have multiple databases hanging around in memory
gc.collect()

# Reload the file in append mode and continue on our merry way
self.h5db = h5py.File(self._fullPath, "r+")

def load(
self,
cycle,
Expand Down
49 changes: 49 additions & 0 deletions armi/bookkeeping/db/tests/test_databaseInterface.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,55 @@ def test_timeNodeLoop_tightCoupling(self):
self.o._timeNodeLoop(0, 0)
self.assertTrue(self.dbi._db.hasTimeStep(0, 0))

def test_syncDbAfterWrite(self):
"""
Test to ensure that the fast-path database is copied to working
directory at every time node when ``syncDbAfterWrite`` is ``True``.
"""
r = self.r

self.o.cs["syncDbAfterWrite"] = True
self.o.cs["burnSteps"] = 2 # make test insensitive to burn steps

self.dbi.interactBOL()
self.assertFalse(os.path.exists(self.dbi.database.fileName))

# Go through a few time nodes to ensure appending is working
for timeNode in range(self.o.cs["burnSteps"]):
r.p.cycle = 0
r.p.timeNode = timeNode
self.dbi.interactEveryNode(r.p.cycle, r.p.timeNode)

# The file should have been copied to working directory
self.assertTrue(os.path.exists(self.dbi.database.fileName))

# The copied file should have the newest time node
with Database3(self.dbi.database.fileName, "r") as db:
for tn in range(timeNode + 1):
self.assertTrue(db.hasTimeStep(r.p.cycle, tn))

# The in-memory database should have been reloaded properly
for tn in range(timeNode + 1):
self.assertTrue(self.dbi.database.hasTimeStep(r.p.cycle, tn))

# Make sure EOL runs smoothly
self.dbi.interactEOL()
self.assertTrue(os.path.exists(self.dbi.database.fileName))

def test_noSyncDbAfterWrite(self):
"""
Test to ensure that the fast-path database is NOT copied to working
directory at every time node when ``syncDbAfterWrite`` is ``False``.
"""
self.o.cs["syncDbAfterWrite"] = False

self.dbi.interactBOL()
self.assertFalse(os.path.exists(self.dbi.database.fileName))
self.dbi.interactEveryNode(0, 0)
self.assertFalse(os.path.exists(self.dbi.database.fileName))
self.dbi.interactEOL()
self.assertTrue(os.path.exists(self.dbi.database.fileName))


class TestDatabaseWriter(unittest.TestCase):
def setUp(self):
Expand Down
1 change: 1 addition & 0 deletions doc/release/0.4.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Bug Fixes
---------
#. Fixed ``DerivedShape.getArea`` for ``cold=True``. (`PR#1831 <https://github.com/terrapower/armi/pull/1831>`_)
#. Fixed error parsing command line integers in ``ReportsEntryPoint``. (`PR#1824 <https://github.com/terrapower/armi/pull/1824>`_)
#. Fixed ``PermissionError`` when using ``syncDbAfterWrite``. (`PR#1857 <https://github.com/terrapower/armi/pull/1857>`_)
#. Fixed ``MpiDirectoryChanger``. (`PR#1853 <https://github.com/terrapower/armi/pull/1853>`_)
#. Changed data type of ``thKernel`` setting from ``bool`` to ``str`` in ``ThermalHydraulicsPlugin``. (`PR#1855 <https://github.com/terrapower/armi/pull/1855>`_)
#. TBD
Expand Down

0 comments on commit 57dc6fe

Please sign in to comment.