Skip to content

Commit 1b4acb2

Browse files
committed
add cookie interface for log checkpoint manager
1 parent 62d18b3 commit 1b4acb2

File tree

5 files changed

+37
-33
lines changed

5 files changed

+37
-33
lines changed

libs/cluster/Server/Replication/ReplicationLogCheckpointManager.cs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -85,27 +85,6 @@ private unsafe byte[] CreateCookie()
8585
return cookie;
8686
}
8787

88-
private unsafe int GetCookieData(HybridLogRecoveryInfo hlri, out long checkpointCoveredAddress, out string primaryReplId)
89-
{
90-
checkpointCoveredAddress = -1;
91-
primaryReplId = null;
92-
93-
var bytesRead = sizeof(int);
94-
fixed (byte* ptr = hlri.cookie)
95-
{
96-
if (hlri.cookie.Length < 4) throw new Exception($"invalid metadata length: {hlri.cookie.Length} < 4");
97-
var cookieSize = *(int*)ptr;
98-
bytesRead += cookieSize;
99-
100-
if (hlri.cookie.Length < 12) throw new Exception($"invalid metadata length: {hlri.cookie.Length} < 12");
101-
checkpointCoveredAddress = *(long*)(ptr + 4);
102-
103-
if (hlri.cookie.Length < 52) throw new Exception($"invalid metadata length: {hlri.cookie.Length} < 52");
104-
primaryReplId = Encoding.ASCII.GetString(ptr + 12, 40);
105-
}
106-
return bytesRead;
107-
}
108-
10988
private HybridLogRecoveryInfo ConverMetadata(byte[] checkpointMetadata)
11089
{
11190
var success = true;
@@ -200,7 +179,7 @@ public void CommiLogCheckpointSendFromPrimary(Guid logToken, byte[] checkpointMe
200179
/// <param name="hlri"></param>
201180
public override unsafe void CommitLogCheckpoint(Guid logToken, HybridLogRecoveryInfo hlri)
202181
{
203-
CommitCookie = CreateCookie();
182+
AddCookie(CreateCookie());
204183
base.CommitLogCheckpoint(logToken, hlri);
205184
}
206185

@@ -212,7 +191,7 @@ public override unsafe void CommitLogCheckpoint(Guid logToken, HybridLogRecovery
212191
/// <param name="deltaLog"></param>
213192
public override unsafe void CommitLogIncrementalCheckpoint(Guid logToken, HybridLogRecoveryInfo hlri, DeltaLog deltaLog)
214193
{
215-
CommitCookie = CreateCookie();
194+
AddCookie(CreateCookie());
216195
base.CommitLogIncrementalCheckpoint(logToken, hlri, deltaLog);
217196
}
218197

@@ -223,6 +202,27 @@ public unsafe (long, string) GetCheckpointCookieMetadata(Guid logToken, DeltaLog
223202
var bytesRead = GetCookieData(hlri, out var RecoveredSafeAofAddress, out var RecoveredReplicationId);
224203
Debug.Assert(bytesRead == 52);
225204
return (RecoveredSafeAofAddress, RecoveredReplicationId);
205+
206+
unsafe int GetCookieData(HybridLogRecoveryInfo hlri, out long checkpointCoveredAddress, out string primaryReplId)
207+
{
208+
checkpointCoveredAddress = -1;
209+
primaryReplId = null;
210+
211+
var bytesRead = sizeof(int);
212+
fixed (byte* ptr = hlri.cookie)
213+
{
214+
if (hlri.cookie.Length < 4) throw new Exception($"invalid metadata length: {hlri.cookie.Length} < 4");
215+
var cookieSize = *(int*)ptr;
216+
bytesRead += cookieSize;
217+
218+
if (hlri.cookie.Length < 12) throw new Exception($"invalid metadata length: {hlri.cookie.Length} < 12");
219+
checkpointCoveredAddress = *(long*)(ptr + 4);
220+
221+
if (hlri.cookie.Length < 52) throw new Exception($"invalid metadata length: {hlri.cookie.Length} < 52");
222+
primaryReplId = Encoding.ASCII.GetString(ptr + 12, 40);
223+
}
224+
return bytesRead;
225+
}
226226
}
227227

228228
public override void GetLogCheckpointMetadataInfo(ref HybridLogRecoveryInfo hlri, Guid logToken, DeltaLog deltaLog, bool scanDelta, long recoverTo)

libs/storage/Tsavorite/cs/src/core/Index/CheckpointManagement/DeviceLogCommitCheckpointManager.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,10 @@ public class DeviceLogCommitCheckpointManager : ILogCommitManager, ICheckpointMa
4646
readonly int fastCommitThrottleFreq;
4747
int commitCount;
4848

49-
public byte[] CommitCookie { get; set; }
49+
/// <summary>
50+
/// Any additional (user specified) metadata to write out with commit
51+
/// </summary>
52+
protected byte[] CommitCookie { get; private set; }
5053

5154
/// <summary>
5255
/// Create new instance of log commit manager
@@ -169,6 +172,9 @@ public byte[] GetCommitMetadata(long commitNum)
169172

170173
#region ICheckpointManager
171174

175+
/// <inheritdoc />
176+
public void AddCookie(byte[] cookie) => CommitCookie = cookie;
177+
172178
/// <inheritdoc />
173179
public unsafe void CommitIndexCheckpoint(Guid indexToken, byte[] commitMetadata)
174180
{

libs/storage/Tsavorite/cs/src/core/Index/Checkpointing/TsavoriteStateMachineProperties.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,10 @@ public partial class TsavoriteKV<TKey, TValue, TStoreFunctions, TAllocator> : Ts
1010
internal long lastVersion;
1111

1212
/// <summary>
13-
/// Any additional (user specified) metadata to write out with commit
13+
/// Add cookie to the metadata before commit
1414
/// </summary>
15-
public byte[] CommitCookie
16-
{
17-
get => checkpointManager.CommitCookie;
18-
set => checkpointManager.CommitCookie = value;
19-
}
15+
/// <param name="cookie"></param>
16+
public void AddCookie(byte[] cookie) => checkpointManager.AddCookie(cookie);
2017

2118
private byte[] recoveredCommitCookie;
2219
/// <summary>

libs/storage/Tsavorite/cs/src/core/Index/Recovery/ICheckpointManager.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ namespace Tsavorite.core
3131
public interface ICheckpointManager : IDisposable
3232
{
3333
/// <summary>
34-
/// Any additional (user specified) metadata to write out with commit
34+
/// Add cookie to the metadata before commit
3535
/// </summary>
36-
public byte[] CommitCookie { get; set; }
36+
/// <param name="cookie"></param>
37+
void AddCookie(byte[] cookie);
3738

3839
/// <summary>
3940
/// Initialize index checkpoint

libs/storage/Tsavorite/cs/test/SimpleRecoveryTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ private async ValueTask SimpleRecoveryTest1_Worker(CheckpointType checkpointType
149149
}
150150

151151
if (testCommitCookie)
152-
store1.CommitCookie = commitCookie;
152+
store1.AddCookie(commitCookie);
153153
_ = store1.TryInitiateFullCheckpoint(out Guid token, checkpointType);
154154
if (completionSyncMode == CompletionSyncMode.Sync)
155155
store1.CompleteCheckpointAsync().AsTask().GetAwaiter().GetResult();

0 commit comments

Comments
 (0)