diff --git a/spec/1_design_goals.md b/spec/1_design_goals.md
index a34438707..422917cd8 100644
--- a/spec/1_design_goals.md
+++ b/spec/1_design_goals.md
@@ -4,12 +4,17 @@
arbitrary sequences of bytes.
- Chronicle Map optionally persists to a *single file* via the memory-mapping facility, present in
Windows and POSIX-compatible operation systems (`mmap`). The file shouldn't necessarily materialize
- on disk. It's OK for Chronicle Map if the file resides a memory-mounted FS. The whole Chronicle Map state
- is contained in the file contents i. e. it is possible to move, copy or send the file to another
- machine, access it and observe exactly the same Chronicle Map state. Chronicle Map doesn't use the file
- metadata.
+ on disk. It's OK for Chronicle Map if the file resides a memory-mounted FS. The whole Chronicle Map
+ state is contained in the file contents i. e. it is possible to move, copy or send the file to
+ another machine, access it and observe exactly the same Chronicle Map state. Chronicle Map doesn't
+ use the file metadata.
- Chronicle Map supports fully-featured concurrent access from multiple processes, mapping the same
file.
+ - Chronicle Map supports *isolated* multi-key operations, either on the single key-value domain
+ (a single Chronicle Map) or several ones. But the multi-key updates to persisted Chronicle Map(s)
+ are *not* atomic (basically isolated and atomic in ACID sense here; see the detailed definition
+ below in [Guarantees](#Guarantees) section).
+
- Chronicle Map runs only on little-endian architectures.
- Chronicle Map is *not* distributed: the whole data structure is placed in memory of the single
machine (in a single file, if persisted).
@@ -25,43 +30,61 @@
operation on the same 4-byte block, one thread succeeds and all the rest fail.
- Aligned 32-bit or 64-bit writes should be atomic, that means either all 4(8) bytes are written to
memory, or nothing is written.
+
+The two above points are true for CPUs with x86 and x86_64 architectures.
+
- If some values Xi are written at addresses Ai, then 32- or 64-bit value Y
is written at address B, there should be a way to ensure some memory order. In particular, if a
concurrent thread reads value Y from address B, then it should read Xii addresses (but not the old values, stored at these addresses).
+ > In Java Memory model terms, this assumption means "writes to Ai addresses
+ > *happens-before* the write of Y value at address B, which *synchronizes-with* read of this value
+ > from a concurrent thread". E. g. in Java there are some trivial ways to ensure this memory
+ > ordering: volatile write or compare-and-swap at address B with volatile reads of value Y
+ > from B in a concurrent thread.
+
#### Memory-mapping
-Chronicle Map assumes that the memory-mapping implementation does not corrupt the mapped file, even
-if operation system execution was interrupted in any way (black out, virtual machine crash, etc.)
-It means Chronicle Map expects some memory might be "stale", i. e. the bits written shortly before
-operation system failure might not be persisted, but it doesn't expect to read the bit values that
-have never been written to the memory.
+ - The memory-mapping implementation shouldn't corrupt the mapped file, even if operation system
+ execution was interrupted in any way (black out, virtual machine crash, etc.) It means Chronicle
+ Map expects some memory might be "stale", i. e. the bits written shortly before operation system
+ failure might not be persisted, but it doesn't expect to read the bit values that have never been
+ written to the memory.
+ - Writes to disk should be at least 4 or 8 bytes atomic, i. e. either aligned 4- or 8-byte blocks
+ are fully written to the disk, or not written at all.
+
+ > Modern non-volatile drives (HDDs and SSDs) support atomic sector writes, which are usually at
+ > least 512 bytes in size.
On the other hand, it doesn't assume anything specific about how memory if flushed to disk:
- The order in which memory is flushed (up or down by addresses, random, concurrent, etc.)
- - The minimum persistence atomicity i.e. Chronicle Map doesn't assume memory blocks of any size
- should be either fully written to disk or not written at all, down to a single bit.
- - The maximum timeout dirty memory could not be flushed to disk. i.e. it assumes some memory could remain
- dirty forever.
- - The maximum fraction of dirty memory not written to disk i.e. it assumes that all mapped memory could be
- dirty.
+ - The maximum timeout dirty memory could not be flushed to disk. i.e. it assumes some memory could
+ remain dirty forever.
+ - The maximum fraction of dirty memory not written to disk i.e. it assumes that all mapped memory
+ could be dirty.
### Guarantees
-If the above assumptions are met, Chronicle Map aims to satisfy the following guarantees:
+If the above assumptions are met, Chronicle Map aims to satisfy the following guarantee:
- - Under any conditions (concurrent access, inter-process access, access after operation system
- failures) and using any type of access (querying the entry by the key or iteration), only entries that
- were ever stored in the Chronicle Map could be observed. Reading corrupted or half written values
- by some keys, or observing some keys which were never stored during the iteration is disallowed.
- - If Chronicle Map is persisted to a file and the operation system fails to flush all dirty memory to
- disk due to a power-off or any other failure, when the file is mapped to memory and accessed again,
- some values associated with the keys shortly before the failure could be lost.
- - If some entry is successfully put into the Chronicle Map, and, if the Chronicle Map is persisted
- to disk, all memory that is dirty by the end of this operation is successfully flushed, this entry
- couldn't disappear from the Chronicle Map unless explicitly removed from the key-value store.
+ - Single-key and multi-key accesses and updates to Chronicle Map (multi-key updates could span
+ several Chronicle Maps) are concurrently isolated, i. e. accesses involving a certain key are
+ totally ordered across accessing threads and processes. All updates made to the entry during the
+ previous update are visible during the subsequent access. After a multi-key update, a subsequent
+ multi-key query involving a subset of the updated keys (and possibly some more keys) views all
+ updates made to all entries corresponding to the keys from that subset.
+ - Under any conditions (concurrent access, inter-process access) and using any type of access
+ (querying the entry by the key or iteration), only entries that were ever stored in the Chronicle
+ Map could be observed. Reading corrupted or half written values by some keys, or observing some
+ keys which were never stored during the iteration is disallowed.
+
+If Chronicle Map is persisted to a file and the operation system fails to flush all dirty memory to
+disk due to a power-off or any other failure, when the file is mapped to memory and accessed again,
+it might be needed to perform a special recovery procedure on the store first, which identifies and
+purges corrupted entries from the Chronicle Map. Therefore, some entries updated shortly before the
+failure could be lost.
## Goals
@@ -84,25 +107,26 @@ Chronicle Map targets the goals outlined above under the following assumptions:
- It runs on AMD64 (aka x86_64) architecture
- It runs on Linux
- The total volume of memory, used by Chronicle Map fits the available physical memory. In other
- words, the memory accessed by Chronicle Map should never be swapped to disk.
+ words, the memory accessed by Chronicle Map should never be evicted to disk.
-If these assumptions are not held, Chronicle Map must remain operational, but it shouldn't be
-expected to be very efficient.
+If these assumptions are not held, Chronicle Map must still operate correctly and providing the same
+guarantees, but it shouldn't be expected to be very efficient.
## Non-goals
- Alphabetical key ordering (sorted keys).
- Durability. There are ways to make Chronicle Map look like a durable key-value store, e. g.
- performing `msync` after each operation. However, if required, it would be better use a database designed for durability.
+ performing `msync` after each operation. However, if required, it would be better use a database
+ designed for durability.
The following items are non-goals but might become goals in the future, at least under certain
conditions or with additional assumptions:
- - Entry ordering by insertion, update or access.
- - Hard latency guarantees. e.g. Chronicle Map doesn't specify "all operations complete in less than one
- second." Persistence via memory-mapping implies that any memory write could trigger flushing
+ - Entry ordering by insertion, update or access times.
+ - Hard latency guarantees. e.g. Chronicle Map doesn't specify "all operations complete in less than
+ one second." Persistence via memory-mapping implies that any memory write could trigger flushing
the whole file to disk, that could take long time.
- - Entry expirations. This could be emulated by storing the last update (access) time in the value bytes
- and checking it on each access to Chronicle Map. This could be implemented, for example, by running a background thread that
- traverses the Chronicle Map and cleans expired entries. However it is not guaranteed that Chronicle
- Map would perform better than a cache with "native" expirations.
+ - Entry expirations. This could be emulated by storing the last update (access) time in the value
+ bytes, checking it on each access to Chronicle Map and, for example, running a background thread
+ that traverses the Chronicle Map and cleans expired entries. However it is not guaranteed that
+ Chronicle Map would perform better than a cache with expirations implemented as core functionality.
diff --git a/spec/2_design_overview.md b/spec/2_design_overview.md
new file mode 100644
index 000000000..d5f43f5a2
--- /dev/null
+++ b/spec/2_design_overview.md
@@ -0,0 +1,70 @@
+# Chronicle Map Design Overview
+
+## Logic
+
+Chronicle Map is split into N *completely independent*, ordered segments. The number of segments is
+chosen during Chronicle Map creation and is never changed. Each segment has an associated 3-level
+lock (read, *update* and write levels). Typical steps of performing a query to a Chronicle Map:
+
+ 1. Compute the hash code of the queried key
+ 2. Choose the segment that should hold the key, by the hash code
+ 3. Acquire the segment lock on the needed level on
+ 4. Search for the entry with the queried key in the segment
+ 5. Perform the query operation on the entry (read the value, update the value, etc.)
+ 6. Release the segment lock
+
+For multi-key queries,
+
+ 1. All segments in which the involved keys should be held are identified
+ 2. Acquire the locks of all involved segments. Within each involved Chronicle Map segment locks
+ are acquired in the order of their segments.
+
+ > Acquiring segment locks in the predictable order is needed to avoid dead-locks, e. g. when first
+ > thread acquires lock of the segment #1 and then tries to acquire the lock of the segment #2, and
+ > the second thread does the opposite: locks the segment #2, then tries to lock the segment #1.
+
+ 3. Perform the query operations
+ 4. Release the locks of all involved segment in reverse order
+
+Bulk operations (iterations, background tasks) visit each segment once in any order. For each
+segment, they acquire the segment lock, then visit each entry stored in the segment, then release
+the segment lock.
+
+### Lock levels
+
+**Read level** is shared, several threads and processes could hold the segment lock on read level at
+the same time. This level is used for read queries on a single or multiple keys, like simple value
+reading by the key. This level couldn't be upgraded to update or write level.
+
+> Read level couldn't upgrade to more exclusive levels, because this is dead-lock prone. Two threads
+> could acquire the same lock on read level, then both try upgrade to write level - and blocked by
+> each other. (Upgrading to write level through update level wouldn't eliminate this issue too.)
+
+**Update level** is partially exclusive: a thread could hold the segment lock on update level while
+some other threads or processes are holding the same lock on read level, but not update or write
+level. This level is used for "typically read" queries, that occasionally should be able to write
+hence upgrade to write level without releasing the lock, for example reading value by the key, if
+the entry is present or storing a new value, if the entry is absent. Also, bulk operations should
+acquire segment locks at least on update level, even if they never perform writes. Lock on update
+level could be upgraded to write level.
+
+**Write level** is fully exclusive: when a thread holds the segment lock on write level, no other
+thread or process could hold the same lock on any level. Write level is used for write queries.
+
+## Lock implementation
+
+Lock is represented by two 32-bit words in Chronicle Map memory. *Count word* holds the the counts
+of threads holding the lock at the moment on read, update and write levels. *Wait word* holds the
+number of pending threads, trying to acquire the lock on write level, at the moment. These two words
+are updated only via compare-and-swap operations. In addition to the checks essential for keeping
+exclusiveness invariants, described in the previous section, when a thread tries to acquire the lock
+on read level, it should check there are no threads pending for write access, to prevent starvation
+of the latter.
+
+## The segment data structure
+
+Under normal conditions, a segment is represented by a single *tier*.
+
+Segment tier basically consists of two parts: *hash lookup*, a flat, power-of-two sized open
+addressing hash table
+
diff --git a/spec/README.md b/spec/README.md
index f338d7254..0712f4f7a 100644
--- a/spec/README.md
+++ b/spec/README.md
@@ -5,4 +5,5 @@ This directory contains the Chronicle Map specification.
This is currently work-in-progress.
- [Rational behind Chronicle Map Specification](0_purposes.md)
- - [# Chronicle Map Design Goals, Assumptions and Guarantees](1_design_goals.md)
+ - [Chronicle Map Design Goals, Assumptions and Guarantees](1_design_goals.md)
+ - [Design overview](2_design_overview.md)