2023-10-19 12:19:10 +02:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
|
|
|
|
_ "embed"
|
|
|
|
|
"errors"
|
|
|
|
|
"time"
|
|
|
|
|
|
2025-05-27 17:13:17 +02:00
|
|
|
"github.com/shopspring/decimal"
|
|
|
|
|
|
2023-10-19 12:19:10 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
2023-12-08 16:30:55 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/zerrors"
|
2023-10-19 12:19:10 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type state struct {
|
|
|
|
|
instanceID string
|
2025-05-27 17:13:17 +02:00
|
|
|
position decimal.Decimal
|
2023-10-19 12:19:10 +02:00
|
|
|
eventTimestamp time.Time
|
|
|
|
|
aggregateType eventstore.AggregateType
|
|
|
|
|
aggregateID string
|
|
|
|
|
sequence uint64
|
2023-12-21 11:40:51 +01:00
|
|
|
offset uint32
|
2023-10-19 12:19:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
//go:embed state_get.sql
|
|
|
|
|
currentStateStmt string
|
|
|
|
|
//go:embed state_set.sql
|
|
|
|
|
updateStateStmt string
|
|
|
|
|
)
|
|
|
|
|
|
fix(projections): overhaul the event projection system (#10560)
This PR overhauls our event projection system to make it more robust and
prevent skipped events under high load. The core change replaces our
custom, transaction-based locking with standard PostgreSQL advisory
locks. We also introduce a worker pool to manage concurrency and prevent
database connection exhaustion.
### Key Changes
* **Advisory Locks for Projections:** Replaces exclusive row locks and
inspection of `pg_stat_activity` with PostgreSQL advisory locks for
managing projection state. This is a more reliable and standard approach
to distributed locking.
* **Simplified Await Logic:** Removes the complex logic for awaiting
open transactions, simplifying it to a more straightforward time-based
filtering of events.
* **Projection Worker Pool:** Implements a worker pool to limit
concurrent projection triggers, preventing connection exhaustion and
improving stability under load. A new `MaxParallelTriggers`
configuration option is introduced.
### Problem Solved
Under high throughput, a race condition could cause projections to miss
events from the eventstore. This led to inconsistent data in projection
tables (e.g., a user grant might be missing). This PR fixes the
underlying locking and concurrency issues to ensure all events are
processed reliably.
### How it Works
1. **Event Writing:** When writing events, a *shared* advisory lock is
taken. This signals that a write is in progress.
2. **Event Handling (Projections):**
* A projection worker attempts to acquire an *exclusive* advisory lock
for that specific projection. If the lock is already held, it means
another worker is on the job, so the current one backs off.
* Once the lock is acquired, the worker briefly acquires and releases
the same *shared* lock used by event writers. This acts as a barrier,
ensuring it waits for any in-flight writes to complete.
* Finally, it processes all events that occurred before its transaction
began.
### Additional Information
* ZITADEL no longer modifies the `application_name` PostgreSQL variable
during event writes.
* The lock on the `current_states` table is now `FOR NO KEY UPDATE`.
* Fixes https://github.com/zitadel/zitadel/issues/8509
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Tim Möhlmann <tim+github@zitadel.com>
(cherry picked from commit 0575f67e942c3192b36e39fd6ae06b1502bc0f5f)
2025-09-03 17:29:00 +02:00
|
|
|
func (h *Handler) currentState(ctx context.Context, tx *sql.Tx) (currentState *state, err error) {
|
2023-10-19 12:19:10 +02:00
|
|
|
currentState = &state{
|
|
|
|
|
instanceID: authz.GetInstance(ctx).InstanceID(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var (
|
|
|
|
|
aggregateID = new(sql.NullString)
|
|
|
|
|
aggregateType = new(sql.NullString)
|
|
|
|
|
sequence = new(sql.NullInt64)
|
|
|
|
|
timestamp = new(sql.NullTime)
|
2025-05-27 17:13:17 +02:00
|
|
|
position = new(decimal.NullDecimal)
|
2023-12-21 11:40:51 +01:00
|
|
|
offset = new(sql.NullInt64)
|
2023-10-19 12:19:10 +02:00
|
|
|
)
|
|
|
|
|
|
fix(projections): overhaul the event projection system (#10560)
This PR overhauls our event projection system to make it more robust and
prevent skipped events under high load. The core change replaces our
custom, transaction-based locking with standard PostgreSQL advisory
locks. We also introduce a worker pool to manage concurrency and prevent
database connection exhaustion.
### Key Changes
* **Advisory Locks for Projections:** Replaces exclusive row locks and
inspection of `pg_stat_activity` with PostgreSQL advisory locks for
managing projection state. This is a more reliable and standard approach
to distributed locking.
* **Simplified Await Logic:** Removes the complex logic for awaiting
open transactions, simplifying it to a more straightforward time-based
filtering of events.
* **Projection Worker Pool:** Implements a worker pool to limit
concurrent projection triggers, preventing connection exhaustion and
improving stability under load. A new `MaxParallelTriggers`
configuration option is introduced.
### Problem Solved
Under high throughput, a race condition could cause projections to miss
events from the eventstore. This led to inconsistent data in projection
tables (e.g., a user grant might be missing). This PR fixes the
underlying locking and concurrency issues to ensure all events are
processed reliably.
### How it Works
1. **Event Writing:** When writing events, a *shared* advisory lock is
taken. This signals that a write is in progress.
2. **Event Handling (Projections):**
* A projection worker attempts to acquire an *exclusive* advisory lock
for that specific projection. If the lock is already held, it means
another worker is on the job, so the current one backs off.
* Once the lock is acquired, the worker briefly acquires and releases
the same *shared* lock used by event writers. This acts as a barrier,
ensuring it waits for any in-flight writes to complete.
* Finally, it processes all events that occurred before its transaction
began.
### Additional Information
* ZITADEL no longer modifies the `application_name` PostgreSQL variable
during event writes.
* The lock on the `current_states` table is now `FOR NO KEY UPDATE`.
* Fixes https://github.com/zitadel/zitadel/issues/8509
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Tim Möhlmann <tim+github@zitadel.com>
(cherry picked from commit 0575f67e942c3192b36e39fd6ae06b1502bc0f5f)
2025-09-03 17:29:00 +02:00
|
|
|
row := tx.QueryRow(currentStateStmt, currentState.instanceID, h.projection.Name())
|
2023-10-19 12:19:10 +02:00
|
|
|
err = row.Scan(
|
|
|
|
|
aggregateID,
|
|
|
|
|
aggregateType,
|
|
|
|
|
sequence,
|
|
|
|
|
timestamp,
|
|
|
|
|
position,
|
2023-12-01 13:25:41 +01:00
|
|
|
offset,
|
2023-10-19 12:19:10 +02:00
|
|
|
)
|
fix(projections): overhaul the event projection system (#10560)
This PR overhauls our event projection system to make it more robust and
prevent skipped events under high load. The core change replaces our
custom, transaction-based locking with standard PostgreSQL advisory
locks. We also introduce a worker pool to manage concurrency and prevent
database connection exhaustion.
### Key Changes
* **Advisory Locks for Projections:** Replaces exclusive row locks and
inspection of `pg_stat_activity` with PostgreSQL advisory locks for
managing projection state. This is a more reliable and standard approach
to distributed locking.
* **Simplified Await Logic:** Removes the complex logic for awaiting
open transactions, simplifying it to a more straightforward time-based
filtering of events.
* **Projection Worker Pool:** Implements a worker pool to limit
concurrent projection triggers, preventing connection exhaustion and
improving stability under load. A new `MaxParallelTriggers`
configuration option is introduced.
### Problem Solved
Under high throughput, a race condition could cause projections to miss
events from the eventstore. This led to inconsistent data in projection
tables (e.g., a user grant might be missing). This PR fixes the
underlying locking and concurrency issues to ensure all events are
processed reliably.
### How it Works
1. **Event Writing:** When writing events, a *shared* advisory lock is
taken. This signals that a write is in progress.
2. **Event Handling (Projections):**
* A projection worker attempts to acquire an *exclusive* advisory lock
for that specific projection. If the lock is already held, it means
another worker is on the job, so the current one backs off.
* Once the lock is acquired, the worker briefly acquires and releases
the same *shared* lock used by event writers. This acts as a barrier,
ensuring it waits for any in-flight writes to complete.
* Finally, it processes all events that occurred before its transaction
began.
### Additional Information
* ZITADEL no longer modifies the `application_name` PostgreSQL variable
during event writes.
* The lock on the `current_states` table is now `FOR NO KEY UPDATE`.
* Fixes https://github.com/zitadel/zitadel/issues/8509
---------
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Tim Möhlmann <tim+github@zitadel.com>
(cherry picked from commit 0575f67e942c3192b36e39fd6ae06b1502bc0f5f)
2025-09-03 17:29:00 +02:00
|
|
|
if err != nil && !errors.Is(err, sql.ErrNoRows) {
|
2023-10-19 12:19:10 +02:00
|
|
|
h.log().WithError(err).Debug("unable to query current state")
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentState.aggregateID = aggregateID.String
|
|
|
|
|
currentState.aggregateType = eventstore.AggregateType(aggregateType.String)
|
|
|
|
|
currentState.sequence = uint64(sequence.Int64)
|
|
|
|
|
currentState.eventTimestamp = timestamp.Time
|
2025-05-27 17:13:17 +02:00
|
|
|
currentState.position = position.Decimal
|
2023-12-21 11:40:51 +01:00
|
|
|
// psql does not provide unsigned numbers so we work around it
|
|
|
|
|
currentState.offset = uint32(offset.Int64)
|
2023-10-19 12:19:10 +02:00
|
|
|
return currentState, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (h *Handler) setState(tx *sql.Tx, updatedState *state) error {
|
|
|
|
|
res, err := tx.Exec(updateStateStmt,
|
|
|
|
|
h.projection.Name(),
|
|
|
|
|
updatedState.instanceID,
|
|
|
|
|
updatedState.aggregateID,
|
|
|
|
|
updatedState.aggregateType,
|
|
|
|
|
updatedState.sequence,
|
|
|
|
|
updatedState.eventTimestamp,
|
|
|
|
|
updatedState.position,
|
2023-12-01 13:25:41 +01:00
|
|
|
updatedState.offset,
|
2023-10-19 12:19:10 +02:00
|
|
|
)
|
|
|
|
|
if err != nil {
|
2024-01-31 11:25:28 +01:00
|
|
|
h.log().WithError(err).Warn("unable to update state")
|
|
|
|
|
return zerrors.ThrowInternal(err, "V2-WF23g2", "unable to update state")
|
2023-10-19 12:19:10 +02:00
|
|
|
}
|
|
|
|
|
if affected, err := res.RowsAffected(); affected == 0 {
|
|
|
|
|
h.log().OnError(err).Error("unable to check if states are updated")
|
2023-12-08 16:30:55 +02:00
|
|
|
return zerrors.ThrowInternal(err, "V2-FGEKi", "unable to update state")
|
2023-10-19 12:19:10 +02:00
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|