diff --git a/docs/modules/ROOT/pages/how-to/databases-and-queries.adoc b/docs/modules/ROOT/pages/how-to/databases-and-queries.adoc index 62f845c..26fd833 100644 --- a/docs/modules/ROOT/pages/how-to/databases-and-queries.adoc +++ b/docs/modules/ROOT/pages/how-to/databases-and-queries.adoc @@ -93,7 +93,7 @@ Run it twice: the first run creates the database and prints `1`, the second open * `read_only=True` never creates. A path that holds no database raises `ArcadeDBError`, with the same message as `open_database`, and nothing is written. * The path is a directory that holds no database (empty, or with other files in it): a database is created inside it and the other files stay untouched, as with `create_database`. The path is a file: it raises `ArcadeDBError` ("Not a directory") and changes nothing. * Another process holds the database open: it raises `ArcadeDBError` with the engine's "locked by another process" message and does not wait. See <>. The same process cannot hold one path twice either (the engine says "already in use"): keep the first handle and share it between threads. -* Two processes call it on the same new path at the same moment: one creates the database. The other finds that the database exists now and opens it, so it never fails with "already exists" or "does not exist". It raises the "locked by another process" error while the first process keeps the database open, and works once that one closes it. +* Two processes call it on the same new path at the same moment: one creates the database. The other opens it, or raises the "locked by another process" error while the first process still has it open, also while that one is still creating it. It does not fail with "already exists", "does not exist" or "Cannot create directory", and it works once the first process closes the database. === Read-only open diff --git a/src/arcadedb/_open_or_create.py b/src/arcadedb/_open_or_create.py index 781f5ec..d74b407 100644 --- a/src/arcadedb/_open_or_create.py +++ b/src/arcadedb/_open_or_create.py @@ -15,7 +15,7 @@ # One lock for the process, so two threads that open or create the same path do not both see "missing" and both create. _LOCK = threading.Lock() -# After a create that failed, how long to wait for another process to finish creating the same database, and how often to look. +# After the second create failed, how long to wait for another process to finish creating the same database, and how often to look. _LOST_RACE_WAIT_S = 0.25 _LOST_RACE_STEP_S = 0.01 @@ -26,9 +26,9 @@ def open_or_create(factory, read_only: bool = False): ``read_only=True`` never creates: there is nothing to open read-only in a path that holds no database, so a missing database raises the same ``ArcadeDBError`` as ``open_database``. - Two processes that call this on the same new path at the same time: one of them creates the database, the other one's create - fails, it sees that the database exists now and opens it. The engine allows one process per database, so that open raises the - engine's "locked by another process" ``ArcadeDBError`` for as long as the first process keeps the database open. + Two processes that call this on the same new path at the same time: one of them creates the database. The other one opens it, + or, because the engine allows one process per database, raises the engine's "locked by another process" ``ArcadeDBError`` while + the first process still has the database open, which includes the time in which it is still creating it. """ with _LOCK: if read_only: @@ -38,7 +38,15 @@ def open_or_create(factory, read_only: bool = False): try: return factory.create() except ArcadeDBError: - # The database may have appeared between exists() and create() (another process won): then open it. + # Create once more (#492). The engine makes the database directory with exists() and then mkdirs(), so a process that + # another one overtakes between the two gets "Cannot create directory" for a directory that is there now, and the other + # process may need longer than any short wait to write the database. The second create finds the directory, and the + # engine's database lock decides: it creates, or it raises "locked by another process" or "already exists". + pass + try: + return factory.create() + except ArcadeDBError: + # The database may have appeared in the meantime (another process won): then open it. # Any other failure (a path that is a file, no permission) is still missing a database: raise the create error. deadline = time.monotonic() + _LOST_RACE_WAIT_S while not factory.exists(): diff --git a/src/arcadedb/core.py b/src/arcadedb/core.py index 2ada1ef..2bf4254 100644 --- a/src/arcadedb/core.py +++ b/src/arcadedb/core.py @@ -2938,10 +2938,10 @@ def open_or_create_database( wait. The same process cannot open a path twice either: close the first handle, or share it between threads. * Two processes call it on the same new path at once. One creates - the database. The other one's create fails, it finds the - database and opens it, so it never raises "already exists"; it - raises the "locked" error of the case above for as long as the - first process keeps the database open. + the database. The other one opens it, or raises the "locked" + error of the case above while the first process still has the + database open, also while it is still creating it. It does not + raise "already exists" or "Cannot create directory". Example: >>> with arcadedb.open_or_create_database("./memory") as db: diff --git a/tests/test_open_or_create.py b/tests/test_open_or_create.py index b70f571..0e38d47 100644 --- a/tests/test_open_or_create.py +++ b/tests/test_open_or_create.py @@ -13,6 +13,7 @@ import pytest import arcadedb +from arcadedb._open_or_create import open_or_create from arcadedb.exceptions import ArcadeDBError ACCEPTED_RACE_OUTCOMES = ("OK", "LOCKED") @@ -192,9 +193,9 @@ def test_locked_by_another_process_raises_the_engine_error_and_does_not_wait(tmp def test_two_processes_racing_on_a_new_path(tmp_path, round_number): """Both processes call the function at the same moment on a path that holds no database. - Neither may fail with "already exists" or "does not exist". The only other outcome is the engine's lock error, when one process - still holds the database as the other one looks (the engine allows one process per database). At least one process gets the - database, and the directory it made opens afterwards. + Neither may fail with "already exists", "does not exist" or "Cannot create directory" (#492). The only other outcome is the + engine's lock error, when one process still holds the database as the other one looks (the engine allows one process per + database). At least one process gets the database, and the directory it made opens afterwards. """ path = str(tmp_path / "db") go = str(tmp_path / "go") @@ -220,3 +221,75 @@ def test_two_processes_racing_on_a_new_path(tmp_path, round_number): assert arcadedb.database_exists(path) with arcadedb.open_or_create_database(path) as db: assert db.is_open() + + +class _OvertakenFactory: + """The factory of a process that another one overtakes on a new path: ``create()`` raises ``errors`` in turn and then succeeds. + + ``exists()`` stays False until ``create()`` ran ``visible_after`` times, because the other process has made the directory and has + not written the database yet (it is False for ever with no ``visible_after``). + """ + + def __init__(self, *errors, visible_after=None): + self._errors = list(errors) + self._visible_after = visible_after + self.creates = 0 + + def exists(self): + return self._visible_after is not None and self.creates >= self._visible_after + + def create(self): + self.creates += 1 + if self._errors: + raise self._errors.pop(0) + return "created" + + def open(self, read_only=False): + assert self.exists(), "open() of a path that holds no database" + return "opened" + + +def _cannot_create_directory(): + # the text of the failure in ArcadeData/arcadedb-python#492: the engine checks that the directory is missing and then calls + # mkdirs(), which returns False when another process made the directory between the two + return ArcadeDBError( + "Failed to create database: com.arcadedb.exception.DatabaseOperationException: Cannot create directory 'db'" + ) + + +def _locked(): + return ArcadeDBError( + "Failed to create database: Database 'db' is locked by another process" + ) + + +def test_losing_the_directory_to_a_process_that_still_creates_raises_the_lock_error(): + locked = _locked() + factory = _OvertakenFactory(_cannot_create_directory(), locked) + with pytest.raises(ArcadeDBError) as raised: + open_or_create(factory) + assert raised.value is locked + assert factory.creates == 2 + + +def test_losing_the_directory_alone_does_not_fail_the_call(): + factory = _OvertakenFactory(_cannot_create_directory()) + assert open_or_create(factory) == "created" + assert factory.creates == 2 + + +def test_losing_the_directory_opens_the_database_the_other_process_finished(): + exists = ArcadeDBError("Failed to create database: Database 'db' already exists") + factory = _OvertakenFactory(_cannot_create_directory(), exists, visible_after=2) + assert open_or_create(factory) == "opened" + + +def test_a_directory_that_cannot_be_made_still_raises_after_a_second_create(): + again = _cannot_create_directory() + factory = _OvertakenFactory( + _cannot_create_directory(), again, _cannot_create_directory() + ) + with pytest.raises(ArcadeDBError, match="Cannot create directory") as raised: + open_or_create(factory) + assert raised.value is again + assert factory.creates == 2