#![deny(missing_docs)]
mod capability;
pub mod checkpoint;
mod checkpoint_artifact;
mod contract;
mod error;
mod ha_contract;
mod progress;
mod timer;
mod timer_snapshot;
mod wal;
pub use capability::AdminStore;
pub use capability::DerivedRecordSink;
pub use capability::MessageAppender;
pub use capability::MessageReader;
pub use capability::OffsetIndex;
pub use capability::ReleaseCheckpointCreateOutcome;
pub use capability::ReleaseCheckpointCreateRejection;
pub use capability::ReleaseCheckpointRestoreOutcome;
pub use capability::ReleaseCheckpointRestoreRejection;
pub use capability::ReleaseCheckpointStore;
pub use capability::ReplicationControl;
pub use capability::StoreHealth;
pub use capability::StoreLifecycle;
pub use checkpoint::CheckpointArtifact;
pub use checkpoint::CheckpointBackend;
pub use checkpoint::CheckpointManifest;
pub use checkpoint::CheckpointOffsets;
pub use checkpoint::CheckpointRequest;
pub use checkpoint::CheckpointRestoreVerification;
pub use checkpoint::CheckpointStorageIdentity;
pub use checkpoint::CHECKPOINT_SCHEMA_VERSION;
pub use checkpoint_artifact::file_uri_to_path;
pub use checkpoint_artifact::hash_checkpoint_directory;
pub use checkpoint_artifact::path_to_file_uri;
pub use checkpoint_artifact::CheckpointDirectoryDigest;
pub use checkpoint_artifact::RELEASE_CHECKPOINT_MANIFEST_FILE;
pub use contract::StoreContractViolation;
pub use error::StoreComponent;
pub use error::StoreError;
pub use error::StoreOperation;
pub use ha_contract::decide_replication;
pub use ha_contract::AckPolicy;
pub use ha_contract::HaRejectReason;
pub use ha_contract::MasterEpoch;
pub use ha_contract::ReplicaAck;
pub use ha_contract::ReplicaCount;
pub use ha_contract::ReplicationAcknowledgement;
pub use ha_contract::ReplicationDecision;
pub use ha_contract::ReplicationObservation;
pub use ha_contract::SyncStateSet;
pub use ha_contract::SyncStateSetEpoch;
pub use ha_contract::WriteAuthority;
pub use ha_contract::WriteLeaseToken;
pub use progress::CursorAdvance;
pub use progress::CursorAdvanceDisposition;
pub use progress::DerivedCheckpoint;
pub use progress::DerivedCursor;
pub use progress::DerivedEngine;
pub use progress::DerivedRecordId;
pub use progress::LegacyDerivedCursorV0;
pub use progress::DERIVED_CHECKPOINT_ENCODED_LEN;
pub use progress::DERIVED_CHECKPOINT_FORMAT_VERSION;
pub use timer::PersistedTimerRoute;
pub use timer::TimerEngineEpoch;
pub use timer::TimerEngineId;
pub use timer::TimerGeneration;
pub use timer::TimerId;
pub use timer::TimerPayloadLocator;
pub use timer::TimerPayloadStoreLocator;
pub use timer::TimerRecallRequest;
pub use timer::TimerRecallStatus;
pub use timer::TimerSourceCqOffset;
pub use timer::TimerStoreMode;
pub use timer::TimerTimelineCursor;
pub use timer::EXTENDED_TIMELINE_FORMAT_VERSION;
pub use timer::JAVA_COMPAT_TIMER_FORMAT_VERSION;
pub use timer_snapshot::TimerSnapshotFile;
pub use timer_snapshot::TimerSnapshotManifest;
pub use timer_snapshot::TimerTimelineIndexKind;
pub use timer_snapshot::TIMER_SNAPSHOT_SCHEMA_VERSION;
pub use wal::WalPort;
use std::fmt;
use std::ops::Range;
use bytes::Bytes;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum Durability {
#[default]
Memory,
Local,
Replicated,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub enum AppendStatus {
#[default]
PutOk,
FlushDiskTimeout,
FlushReplicaTimeout,
ReplicaUnavailable,
ServiceUnavailable,
StorageUnavailable,
InvalidMessage,
PropertiesTooLarge,
PageCacheBusy,
Unknown,
InsufficientReplicas,
RemoteAppendFailed,
QueueLimitExceeded,
ScheduleFlowControl,
ScheduleMessageIllegal,
ScheduleDisabled,
}
impl AppendStatus {
pub const fn is_accepted(self) -> bool {
matches!(
self,
Self::PutOk | Self::FlushDiskTimeout | Self::FlushReplicaTimeout | Self::ReplicaUnavailable
)
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AppendReceipt {
status: AppendStatus,
appended_range: Option<Range<i64>>,
appended_watermark: i64,
durable_watermark: i64,
durability: Durability,
}
impl AppendReceipt {
pub fn try_new(
status: AppendStatus,
appended_range: Range<i64>,
appended_watermark: i64,
durable_watermark: i64,
durability: Durability,
) -> Result<Self, StoreContractViolation> {
if appended_range.start == appended_range.end {
return Err(StoreContractViolation::AppendReceiptEmptyRange);
}
if appended_range.start > appended_range.end {
return Err(StoreContractViolation::AppendReceiptReversedRange);
}
if !status.is_accepted() {
return Err(StoreContractViolation::AppendReceiptRejectedStatusWithRange);
}
if appended_watermark < appended_range.end {
return Err(StoreContractViolation::AppendReceiptAppendedWatermarkBehindRange);
}
if durable_watermark > appended_watermark {
return Err(StoreContractViolation::AppendReceiptDurableWatermarkAheadOfAppended);
}
if durability == Durability::Replicated {
return Err(StoreContractViolation::AppendReceiptReplicatedDurabilityRequiresDecision);
}
match durability {
Durability::Memory if durable_watermark >= appended_range.end => {
return Err(StoreContractViolation::AppendReceiptMemoryDurabilityAlreadyCovered);
}
Durability::Local | Durability::Replicated if durable_watermark < appended_range.end => {
return Err(StoreContractViolation::AppendReceiptDurableWatermarkBehindRange);
}
Durability::Memory | Durability::Local | Durability::Replicated => {}
}
Ok(Self {
status,
appended_range: Some(appended_range),
appended_watermark,
durable_watermark,
durability,
})
}
pub fn try_new_with_replication(
status: AppendStatus,
appended_range: Range<i64>,
appended_watermark: i64,
durable_watermark: i64,
acknowledgement: ReplicationAcknowledgement,
) -> Result<Self, StoreContractViolation> {
if acknowledgement.acknowledged_offset() < appended_range.end {
return Err(StoreContractViolation::AppendReceiptReplicationDecisionBehindRange);
}
let mut receipt = Self::try_new(
status,
appended_range,
appended_watermark,
durable_watermark,
Durability::Local,
)?;
receipt.durability = acknowledgement.durability();
Ok(receipt)
}
pub const fn try_rejected(
status: AppendStatus,
appended_watermark: i64,
durable_watermark: i64,
) -> Result<Self, StoreContractViolation> {
if status.is_accepted() {
return Err(StoreContractViolation::AppendReceiptAcceptedStatusWithoutRange);
}
if durable_watermark > appended_watermark {
return Err(StoreContractViolation::AppendReceiptDurableWatermarkAheadOfAppended);
}
Ok(Self {
status,
appended_range: None,
appended_watermark,
durable_watermark,
durability: Durability::Memory,
})
}
pub const fn status(&self) -> AppendStatus {
self.status
}
pub fn appended_range(&self) -> Option<Range<i64>> {
self.appended_range.clone()
}
pub fn first_appended_offset(&self) -> Option<i64> {
self.appended_range.as_ref().map(|range| range.start)
}
pub fn last_appended_offset(&self) -> Option<i64> {
self.appended_range.as_ref().map(|range| range.end - 1)
}
pub const fn appended_watermark(&self) -> i64 {
self.appended_watermark
}
pub const fn durable_watermark(&self) -> i64 {
self.durable_watermark
}
pub const fn durability(&self) -> Durability {
self.durability
}
pub fn is_accepted(&self) -> bool {
self.status.is_accepted() && self.appended_range.is_some()
}
pub fn is_durable(&self) -> bool {
self.is_accepted()
&& self.durability != Durability::Memory
&& self
.appended_range
.as_ref()
.is_some_and(|range| self.durable_watermark >= range.end)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct DerivedProgress {
source_watermark: i64,
derived_watermark: i64,
}
impl DerivedProgress {
pub const fn new(source_watermark: i64, derived_watermark: i64) -> Self {
Self {
source_watermark,
derived_watermark,
}
}
pub const fn source_watermark(self) -> i64 {
self.source_watermark
}
pub const fn derived_watermark(self) -> i64 {
self.derived_watermark
}
pub const fn acknowledges_primary_append(self) -> bool {
false
}
pub const fn satisfies_primary_durability(self) -> bool {
false
}
}
pub struct LeasedBytes<L> {
bytes: Bytes,
lease: L,
}
impl<L> LeasedBytes<L> {
pub const fn new(bytes: Bytes, lease: L) -> Self {
Self { bytes, lease }
}
pub const fn bytes(&self) -> &Bytes {
&self.bytes
}
pub fn into_bytes(self) -> Bytes {
self.bytes
}
}
impl<L: fmt::Debug> fmt::Debug for LeasedBytes<L> {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
formatter
.debug_struct("LeasedBytes")
.field("bytes", &self.bytes)
.field("lease", &self.lease)
.finish()
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ReadCacheState {
#[default]
Unknown,
Hot,
Cold,
}
#[derive(Debug)]
pub struct SelectResult<L> {
start_offset: u64,
data: LeasedBytes<L>,
cache_state: ReadCacheState,
}
impl<L> SelectResult<L> {
pub const fn new(start_offset: u64, data: LeasedBytes<L>, cache_state: ReadCacheState) -> Self {
Self {
start_offset,
data,
cache_state,
}
}
pub const fn start_offset(&self) -> u64 {
self.start_offset
}
pub fn size(&self) -> usize {
self.data.bytes().len()
}
pub const fn data(&self) -> &LeasedBytes<L> {
&self.data
}
pub fn into_data(self) -> LeasedBytes<L> {
self.data
}
pub const fn cache_state(&self) -> ReadCacheState {
self.cache_state
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum GetStatus {
#[default]
Found,
NoMatchedMessage,
MessageWasRemoving,
OffsetFoundNull,
OffsetOverflowBadly,
OffsetOverflowOne,
OffsetTooSmall,
NoMatchedLogicQueue,
NoMessageInQueue,
OffsetReset,
}
#[derive(Debug)]
pub struct GetResult<L> {
pub records: Vec<SelectResult<L>>,
pub queue_offsets: Vec<u64>,
pub status: Option<GetStatus>,
pub next_begin_offset: i64,
pub min_offset: i64,
pub max_offset: i64,
pub buffer_total_size: i32,
pub message_count: i32,
pub suggest_pulling_from_replica: bool,
pub commercial_message_count: i32,
pub commercial_size_per_message: i32,
pub cold_data_sum: i64,
}
impl<L> Default for GetResult<L> {
fn default() -> Self {
Self {
records: Vec::new(),
queue_offsets: Vec::new(),
status: None,
next_begin_offset: 0,
min_offset: 0,
max_offset: 0,
buffer_total_size: 0,
message_count: 0,
suggest_pulling_from_replica: false,
commercial_message_count: 0,
commercial_size_per_message: 4 * 1024,
cold_data_sum: 0,
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct ReadOutcome<T> {
status: GetStatus,
next_begin_offset: i64,
min_offset: i64,
max_offset: i64,
records: Option<Vec<T>>,
}
impl<T> ReadOutcome<T> {
pub fn new<R>(status: GetStatus, next_begin_offset: i64, min_offset: i64, max_offset: i64, records: R) -> Self
where
R: Into<Option<Vec<T>>>,
{
Self {
status,
next_begin_offset,
min_offset,
max_offset,
records: records.into(),
}
}
pub const fn status(&self) -> GetStatus {
self.status
}
pub const fn next_begin_offset(&self) -> i64 {
self.next_begin_offset
}
pub const fn min_offset(&self) -> i64 {
self.min_offset
}
pub const fn max_offset(&self) -> i64 {
self.max_offset
}
pub fn records(&self) -> Option<&[T]> {
self.records.as_deref()
}
pub fn into_records(self) -> Option<Vec<T>> {
self.records
}
}
#[derive(Debug)]
pub struct QueryResult<L> {
pub records: Vec<SelectResult<L>>,
pub index_last_update_timestamp: i64,
pub index_last_update_physical_offset: i64,
pub buffer_total_size: i32,
pub index_query_safe: bool,
pub index_safe_physical_offset: i64,
pub index_confirm_physical_offset: i64,
}
impl<L> Default for QueryResult<L> {
fn default() -> Self {
Self {
records: Vec::new(),
index_last_update_timestamp: 0,
index_last_update_physical_offset: 0,
buffer_total_size: 0,
index_query_safe: true,
index_safe_physical_offset: 0,
index_confirm_physical_offset: 0,
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct FlushBacklog {
pub queue_depth: u64,
pub oldest_wait_millis: u64,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct StoreHealthSnapshot {
pub writable: bool,
pub last_error: Option<&'static rocketmq_error::ErrorDescriptor>,
pub page_cache_busy: bool,
pub transient_pool_deficient: bool,
pub flush_backlog: FlushBacklog,
pub dispatch_behind_bytes: i64,
pub shutdown: bool,
pub replication_pending_count: u64,
pub replication_oldest_wait_millis: u64,
pub appended_watermark: i64,
pub durable_watermark: i64,
}
impl Default for StoreHealthSnapshot {
fn default() -> Self {
Self {
writable: true,
last_error: None,
page_cache_busy: false,
transient_pool_deficient: false,
flush_backlog: FlushBacklog::default(),
dispatch_behind_bytes: 0,
shutdown: false,
replication_pending_count: 0,
replication_oldest_wait_millis: 0,
appended_watermark: 0,
durable_watermark: 0,
}
}
}
impl StoreHealthSnapshot {
pub const fn writable(&self) -> bool {
self.writable
}
pub const fn last_error(&self) -> Option<&'static rocketmq_error::ErrorDescriptor> {
self.last_error
}
pub const fn appended_watermark(&self) -> i64 {
self.appended_watermark
}
pub const fn durable_watermark(&self) -> i64 {
self.durable_watermark
}
}