@@ -7379,6 +7379,83 @@ func TestFileStoreCheckSkipFirstBlockNotLoadOldBlocks(t *testing.T) {
73797379 require_Equal (t , loaded , 1 )
73807380}
73817381
7382+ func TestFileStoreSyncCompressOnlyIfDirty (t * testing.T ) {
7383+ sd := t .TempDir ()
7384+ fs , err := newFileStore (
7385+ FileStoreConfig {StoreDir : sd , BlockSize : 256 , SyncInterval : 250 * time .Millisecond },
7386+ StreamConfig {Name : "zzz" , Subjects : []string {"foo.*" }, Storage : FileStorage })
7387+ require_NoError (t , err )
7388+ defer fs .Stop ()
7389+
7390+ msg := []byte ("hello" )
7391+
7392+ // 6 msgs per block.
7393+ // Fill 2 blocks.
7394+ for i := 0 ; i < 12 ; i ++ {
7395+ fs .StoreMsg ("foo.BB" , nil , msg )
7396+ }
7397+ // Create third block with just one message in it.
7398+ fs .StoreMsg ("foo.BB" , nil , msg )
7399+
7400+ // Should have created 3 blocks.
7401+ require_Equal (t , fs .numMsgBlocks (), 3 )
7402+
7403+ // Now delete a bunch that will will fill up 3 block with tombstones.
7404+ for _ , seq := range []uint64 {2 , 3 , 4 , 5 , 8 , 9 , 10 , 11 } {
7405+ _ , err = fs .RemoveMsg (seq )
7406+ require_NoError (t , err )
7407+ }
7408+ // Now make sure we add 4th block so syncBlocks will try to compress.
7409+ for i := 0 ; i < 6 ; i ++ {
7410+ fs .StoreMsg ("foo.BB" , nil , msg )
7411+ }
7412+ require_Equal (t , fs .numMsgBlocks (), 4 )
7413+
7414+ // All should have compact set.
7415+ fs .mu .Lock ()
7416+ // Only check first 3 blocks.
7417+ for i := 0 ; i < 3 ; i ++ {
7418+ mb := fs .blks [i ]
7419+ mb .mu .Lock ()
7420+ shouldCompact := mb .shouldCompactSync ()
7421+ mb .mu .Unlock ()
7422+ if ! shouldCompact {
7423+ fs .mu .Unlock ()
7424+ t .Fatalf ("Expected should compact to be true for %d, got false" , mb .getIndex ())
7425+ }
7426+ }
7427+ fs .mu .Unlock ()
7428+
7429+ // Let sync run.
7430+ time .Sleep (300 * time .Millisecond )
7431+
7432+ // We want to make sure the last block, which is filled with tombstones and is not compactable, returns false now.
7433+ fs .mu .Lock ()
7434+ for _ , mb := range fs .blks {
7435+ mb .mu .Lock ()
7436+ shouldCompact := mb .shouldCompactSync ()
7437+ mb .mu .Unlock ()
7438+ if shouldCompact {
7439+ fs .mu .Unlock ()
7440+ t .Fatalf ("Expected should compact to be false for %d, got true" , mb .getIndex ())
7441+ }
7442+ }
7443+ fs .mu .Unlock ()
7444+
7445+ // Now remove some from block 3 and verify that compact is not suppressed.
7446+ _ , err = fs .RemoveMsg (13 )
7447+ require_NoError (t , err )
7448+
7449+ fs .mu .Lock ()
7450+ mb := fs .blks [2 ] // block 3.
7451+ mb .mu .Lock ()
7452+ noCompact := mb .noCompact
7453+ mb .mu .Unlock ()
7454+ fs .mu .Unlock ()
7455+ // Verify that since we deleted a message we should be considered for compaction again in syncBlocks().
7456+ require_False (t , noCompact )
7457+ }
7458+
73827459///////////////////////////////////////////////////////////////////////////
73837460// Benchmarks
73847461///////////////////////////////////////////////////////////////////////////
0 commit comments