Skip to content

Commit 14e4731

Browse files
committed
Fix mongodb restore deadlock
1 parent 3268b10 commit 14e4731

File tree

17 files changed

+262
-777
lines changed

17 files changed

+262
-777
lines changed

.idea/dictionaries/project.xml

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/.vitepress/config.mts

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { defineConfig } from 'vitepress'
1+
import {defineConfig} from 'vitepress'
22

33
// https://vitepress.dev/reference/site-config
44
export default defineConfig({
@@ -29,25 +29,17 @@ export default defineConfig({
2929
text: 'Concepts',
3030
items: [
3131
{ text: 'Events', link: '/concepts/events' },
32-
{ text: 'Event Catalog', link: '/concepts/event-catalog' },
33-
{ text: 'Event Stream Processing', link: '/concepts/event-stream-processing' },
34-
{ text: 'Event Sources', link: '/concepts/event-sources' },
35-
{ text: 'Event Sourcing Strategies', link: '/concepts/event-sourcing-strategies' },
36-
{ text: 'Event Models', link: '/concepts/event-models' },
37-
{ text: 'Checkpointing', link: '/concepts/checkpointing' }
32+
{text: 'Checkpointing', link: '/concepts/checkpointing'},
33+
{text: 'Models', link: '/concepts/models'}
3834
]
3935
},
4036
{
4137
text: 'Guide',
4238
items: [
4339
{ text: 'Installation', link: '/guide/installation' },
44-
{ text: 'Getting Started', link: '/guide/getting-started' },
45-
{ text: 'Creating Event Models', link: '/guide/creating-event-models' },
46-
{ text: 'Working with Event Sources', link: '/guide/working-with-event-sources' }
4740
]
4841
}
4942
],
50-
5143
socialLinks: [
5244
{ icon: 'github', link: 'https://github.com/helightdev/krescent' }
5345
]

docs/concepts/checkpointing.md

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,32 @@
22
title: Checkpointing
33
---
44

5-
Checkpointing in Krescent is a crucial optimization mechanism for event models. Its primary purpose is to periodically save the current state of an event model, along with the position (e.g., `StreamingToken`) of the last event processed to reach that state. This avoids the need to reprocess the entire event stream from the beginning every time the model is initialized or restarted.
5+
Checkpointing in Krescent is a crucial optimization mechanism for event models. Its primary purpose is to periodically
6+
save the current state of an event model, along with the position (e.g., `StreamingToken`) of the last event processed
7+
to reach that state. This avoids the need to reprocess the entire event stream from the beginning every time the model
8+
is initialized or restarted.
69

710
## `CheckpointStrategy`
811

912
A `CheckpointStrategy` determines *when* a checkpoint should be taken. Krescent offers several built-in strategies:
1013

11-
- **`FixedEventRateCheckpointStrategy`**: Triggers a checkpoint after a fixed number of events have been processed. For example, checkpoint every 1000 events.
12-
- **`FixedTimeRateCheckpointStrategy`**: Triggers a checkpoint after a fixed amount of time has elapsed since the last checkpoint. For example, checkpoint every 5 minutes.
13-
- **`ManualCheckpointStrategy`**: Allows the application to trigger checkpoints programmatically based on custom logic or external signals. This provides the most flexibility but requires explicit management.
14+
- **`FixedEventRateCheckpointStrategy`**: Triggers a checkpoint after a fixed number of events have been processed. For
15+
example, checkpoint every 1000 events.
16+
- **`FixedTimeRateCheckpointStrategy`**: Triggers a checkpoint after a fixed amount of time has elapsed since the last
17+
checkpoint. For example, checkpoint every 5 minutes.
18+
- **`AlwaysCheckpointStrategy`**: Trigger a checkpoint after every event processed. This is either useful if creating
19+
checkpoints is very inexpensive or the projection of a read model represents the latest snapshot with the read model
20+
being able to handle transactions and rollbacks.
21+
- **`ManualCheckpointStrategy`**: Allows the application to trigger checkpoints programmatically based on custom logic
22+
or external signals. This provides the most flexibility but requires explicit management.
1423

1524
## `CheckpointStorage`
1625

17-
The `CheckpointStorage` interface defines *how* and *where* checkpoints are saved and loaded. Implementations of this interface are responsible for serializing the model's state and the associated `StreamingToken`, persisting them to a durable store (like a file system, database, or cloud storage), and retrieving them when needed.
26+
The `CheckpointStorage` interface defines *how* and *where* checkpoints are saved and loaded. Implementations of this
27+
interface are responsible for serializing the model's state and the associated `StreamingToken`, persisting them to a
28+
durable store (like a file system, database, or cloud storage), and retrieving them when needed.
1829

1930
## `CheckpointSupport`
2031

21-
`CheckpointSupport` is an interface that must be implemented by components (typically event models or parts of them) that can be checkpointed. It defines methods for:
22-
23-
- **`captureCheckpoint()`**: Returns the current state of the component to be saved.
24-
- **`restoreCheckpoint(state: Any)`**: Restores the component's state from a previously saved checkpoint.
25-
26-
## `CheckpointingEventSourceConsumer`
27-
28-
The `CheckpointingEventSourceConsumer` is the component that orchestrates the checkpointing process for an event model. It works in conjunction with a `CheckpointStrategy` and `CheckpointStorage`. As it consumes events for the model, it monitors the chosen strategy. When the strategy indicates that a checkpoint is due, the consumer:
29-
30-
1. Pauses event processing temporarily.
31-
2. Instructs the event model (which implements `CheckpointSupport`) to capture its current state.
32-
3. Retrieves the `StreamingToken` of the last processed event.
33-
4. Uses the `CheckpointStorage` to save the model's state and the token.
34-
5. Resumes event processing.
35-
36-
When the model is restarted, the `CheckpointingEventSourceConsumer` first attempts to load the latest checkpoint from `CheckpointStorage`. If successful, the model's state is restored, and event processing resumes from the token stored in the checkpoint, significantly reducing startup time.
32+
`CheckpointSupport` is an interface that must be implemented by components (typically event models or parts of them)
33+
that can be checkpointed.

docs/concepts/event-catalog.md

Lines changed: 0 additions & 23 deletions
This file was deleted.

docs/concepts/event-models.md

Lines changed: 0 additions & 35 deletions
This file was deleted.

docs/concepts/event-sources.md

Lines changed: 0 additions & 29 deletions
This file was deleted.

docs/concepts/event-sourcing-strategies.md

Lines changed: 0 additions & 17 deletions
This file was deleted.

docs/concepts/event-stream-processing.md

Lines changed: 0 additions & 26 deletions
This file was deleted.

docs/concepts/events.md

Lines changed: 53 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,68 @@
22
title: Events
33
---
44

5-
Events in Krescent are immutable facts that represent something that has happened in the system. They are the primary building blocks of Krescent's event-driven architecture.
5+
Events in Krescent are immutable facts that represent something that has happened in the system. They are the primary
6+
building blocks of Krescent's event-driven architecture.
67

7-
## Base `Event` Class
8+
## Physical vs Virtual Events
89

9-
All events in Krescent inherit from a base `Event` class. This class provides common functionality and a consistent structure for all events.
10+
In Krescent, events can be categorized into three types:
1011

11-
## `EventMetadata`
12+
- **Physical Events**: These are events that are sourced from an event stream and have a position, id and timestamp.
13+
All events in this category must be strictly serializable and are typically defined as Kotlin data classes.
14+
- **Virtual Events**: These are events created by the framework, a virtual event stream or model extensions and
15+
are either derived from physical events or related to the event processing pipelines state.
16+
- **System Events**: These are just a special case of virtual events which are only emitted by the framework and usually
17+
represent internal state changes, notifications or stream state changes.
1218

13-
Every event carries metadata, which is encapsulated in the `EventMetadata` class. This metadata includes:
19+
#### Example Event Definition
1420

15-
- **id**: A unique identifier for the event.
16-
- **type**: The type of the event.
17-
- **timestamp**: The time at which the event occurred.
18-
- **position**: The position of the event in the event stream.
21+
```kotlin
22+
@Serializable
23+
data class BookAddedEvent(
24+
override val bookId: String,
25+
val title: String,
26+
val author: String,
27+
val price: Double,
28+
val copies: Int,
29+
) : Event(), BookEvent
1930

20-
## Physical vs. Virtual Events
31+
@Serializable
32+
data class BookPriceChangedEvent(
33+
override val bookId: String,
34+
val price: Double,
35+
) : Event(), BookEvent
2136

22-
Krescent distinguishes between two main types of events:
37+
interface BookEvent {
38+
val bookId: String
39+
}
40+
```
2341

24-
- **Physical Events**: These represent actual occurrences in the system, such as a user action or a sensor reading.
25-
- **`VirtualEvent`**: These are events that are derived or generated by the system itself, often as a result of processing physical events.
42+
## Event Catalog
2643

27-
## `SystemEvent`
44+
Physical events must be registered in one or more `EventCatalog` instances. The `EventCatalog` is responsible for
45+
defining and maneging known event types, their serialization and deserialization for the event processing pipeline.
2846

29-
`SystemEvent`s are a special category of events used by Krescent for internal purposes. Some examples include:
47+
The catalog also defines the type identifier for each event. Naming usually follows domain-based `domain.event`
48+
naming conventions where the type starts with the namespace of the domain and then ends with the event name.
3049

31-
- **`SystemStreamHeadEvent`**: Indicates the beginning of an event stream.
32-
- **`SystemStreamTailEvent`**: Indicates the end of an event stream.
33-
- **`SystemHintCommitTransactionEvent`**: Provides a hint to commit a transaction, often used in scenarios involving transactional event processing.
50+
```kotlin
51+
val bookstoreEventCatalog = buildEventCatalog(1) {
52+
event<BookAddedEvent>("book.added")
53+
event<BookPriceChangedEvent>("book.price_changed")
54+
event<BookRemovedEvent>("book.removed")
55+
event<BookLentEvent>("book.lent")
56+
event<BookReturnedEvent>("book.returned")
57+
event<BookCopyAddedEvent>("book.copy_added")
58+
event<BookCopyRemovedEvent>("book.copy_removed")
59+
}
60+
```
61+
62+
All event catalogs are **versioned** using a revision number. You should increment the revision number **whenever
63+
breaking changes** are made to the events in the catalog that would **affect serialization or deserialization** or
64+
**change the meaning** of the events.
65+
66+
> [!IMPORTANT]
67+
> A change in this revision number will cause all models that use this catalog to discard their checkpoints the next
68+
> time they are loaded.
3469
35-
These system events help manage and coordinate the flow of events within Krescent.

0 commit comments

Comments
 (0)