-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Bifrost] Move TailState to restate-types
This allows TailState to be used in log-server as well
- Loading branch information
1 parent
02d825c
commit 9190929
Showing
14 changed files
with
118 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
// Copyright (c) 2024 - Restate Software, Inc., Restate GmbH. | ||
// All rights reserved. | ||
// | ||
// Use of this software is governed by the Business Source License | ||
// included in the LICENSE file. | ||
// | ||
// As of the Change Date specified in that file, in accordance with | ||
// the Business Source License, use of this software will be governed | ||
// by the Apache License, Version 2.0. | ||
/// Represents the state of the tail of the loglet. | ||
use super::{Lsn, SequenceNumber}; | ||
|
||
#[derive(Clone, Debug)] | ||
pub enum TailState<Offset = Lsn> { | ||
/// Loglet is open for appends | ||
Open(Offset), | ||
/// Loglet is sealed. This offset if the durable tail. | ||
Sealed(Offset), | ||
} | ||
|
||
impl<Offset: SequenceNumber> TailState<Offset> { | ||
pub fn new(sealed: bool, offset: Offset) -> Self { | ||
if sealed { | ||
TailState::Sealed(offset) | ||
} else { | ||
TailState::Open(offset) | ||
} | ||
} | ||
|
||
/// Combines two TailStates together | ||
/// | ||
/// Only applies updates to the value according to the following rules: | ||
/// - Offsets can only move forward. | ||
/// - Tail cannot be unsealed once sealed. | ||
/// | ||
/// Returns true if the state was updated | ||
pub fn combine(&mut self, sealed: bool, offset: Offset) -> bool { | ||
let old_offset = self.offset(); | ||
let is_already_sealed = self.is_sealed(); | ||
|
||
let new_offset = std::cmp::max(self.offset(), offset); | ||
let new_sealed = self.is_sealed() || sealed; | ||
if new_sealed != is_already_sealed || new_offset > old_offset { | ||
*self = TailState::new(new_sealed, new_offset); | ||
true | ||
} else { | ||
false | ||
} | ||
} | ||
|
||
/// Applies a seal on the tail state without changing the tail offset | ||
/// Returns true if the state was updated | ||
pub fn seal(&mut self) -> bool { | ||
if self.is_sealed() { | ||
false | ||
} else { | ||
*self = TailState::new(true, self.offset()); | ||
true | ||
} | ||
} | ||
} | ||
|
||
impl<Offset: SequenceNumber> TailState<Offset> { | ||
pub fn map<F, T>(self, f: F) -> TailState<T> | ||
where | ||
F: FnOnce(Offset) -> T, | ||
{ | ||
match self { | ||
TailState::Open(offset) => TailState::Open(f(offset)), | ||
TailState::Sealed(offset) => TailState::Sealed(f(offset)), | ||
} | ||
} | ||
|
||
#[inline(always)] | ||
pub fn is_sealed(&self) -> bool { | ||
matches!(self, TailState::Sealed(_)) | ||
} | ||
|
||
#[inline(always)] | ||
pub fn offset(&self) -> Offset { | ||
match self { | ||
TailState::Open(offset) | TailState::Sealed(offset) => *offset, | ||
} | ||
} | ||
} |