nats-io / nats-server

High-Performance server for NATS.io, the cloud and edge native messaging system.
https://nats.io
Apache License 2.0
15.49k stars 1.38k forks source link

Purging subjects using file storage doesn't update the first seq correctly #4041

Closed MauriceVanVeen closed 1 year ago

MauriceVanVeen commented 1 year ago

Defect

Make sure that these boxes are checked before submitting your issue -- thank you!

[1] 2023/04/12 08:26:57.409824 [INF] Starting nats-server
[1] 2023/04/12 08:26:57.409898 [INF]   Version:  2.9.15
[1] 2023/04/12 08:26:57.409900 [INF]   Git:      [b91fa85]
[1] 2023/04/12 08:26:57.409901 [INF]   Name:     NDPQFSNXRAAYAREAM4CLTMZAKMLPMI4GHOS4XPQEU46NEBPZEPS4QSZD
[1] 2023/04/12 08:26:57.409905 [INF]   Node:     gpgKsVwa
[1] 2023/04/12 08:26:57.409907 [INF]   ID:       NDPQFSNXRAAYAREAM4CLTMZAKMLPMI4GHOS4XPQEU46NEBPZEPS4QSZD
[1] 2023/04/12 08:26:57.412407 [INF] Starting JetStream
[1] 2023/04/12 08:26:57.412692 [INF]     _ ___ _____ ___ _____ ___ ___   _   __  __
[1] 2023/04/12 08:26:57.412696 [INF]  _ | | __|_   _/ __|_   _| _ \ __| /_\ |  \/  |
[1] 2023/04/12 08:26:57.412697 [INF] | || | _|  | | \__ \ | | |   / _| / _ \| |\/| |
[1] 2023/04/12 08:26:57.412698 [INF]  \__/|___| |_| |___/ |_| |_|_\___/_/ \_\_|  |_|
[1] 2023/04/12 08:26:57.412699 [INF] 
[1] 2023/04/12 08:26:57.412700 [INF]          https://docs.nats.io/jetstream
[1] 2023/04/12 08:26:57.412701 [INF] 
[1] 2023/04/12 08:26:57.412702 [INF] ---------------- JETSTREAM ----------------
[1] 2023/04/12 08:26:57.412705 [INF]   Max Memory:      11.25 GB
[1] 2023/04/12 08:26:57.412706 [INF]   Max Storage:     125.81 GB
[1] 2023/04/12 08:26:57.412707 [INF]   Store Directory: "/tmp/nats/jetstream"
[1] 2023/04/12 08:26:57.412708 [INF] -------------------------------------------
[1] 2023/04/12 08:26:57.413328 [INF] Listening for client connections on 0.0.0.0:4222
[1] 2023/04/12 08:26:57.413648 [INF] Server is ready

Versions of nats-server and affected client libraries used:

nats-server 2.9.15 natsio/nats-box latest

OS/Container environment:

Using docker:

docker run --network host nats -js
docker run --rm --network host -it --name nats-cli natsio/nats-box

Steps or code to reproduce the issue:

  1. Create an OBJ bucket: nats obj add bucket --storage=file (to demonstrate when having many messages spanning multiple message blocks)

  2. Create a file with random data: head -c 100000000 </dev/urandom > bucket_data

  3. Put the data into the bucket: nats obj put bucket bucket_data -f

  4. nats str info -a OBJ_bucket Expected: state.FirstSeq = 1, state.LastSeq = 764, state.NumDeleted = 0 Actual: equals ^

  5. Put the data into the bucket again, to start purging previous data: nats obj put bucket bucket_data -f

  6. nats str info -a OBJ_bucket Expected: state.FirstSeq = 765, state.LastSeq = 1528, state.NumDeleted = 0 Actual: state.FirstSeq = 63, state.LastSeq = 1528, state.NumDeleted = 702

Expected result:

A stream's first seq should be correct after purging from a subject.

Only occurs when using file storage, memory storage works as expected.

Actual result:

A stream's first seq stops being updated.

derekcollison commented 1 year ago

What is message 63? nats str get OBJ_bucket 63

MauriceVanVeen commented 1 year ago

The fix is already merged, but for completeness:

State:

             Messages: 764
                Bytes: 95 MiB
             FirstSeq: 63 @ 2023-04-12T19:14:20 UTC
              LastSeq: 1,528 @ 2023-04-12T19:14:34 UTC
     Deleted Messages: 702
     Active Consumers: 0
   Number of Subjects: 2

:~# nats str get -a
? Select a Stream OBJ_bucket
? Message Sequence to retrieve 63
nats: error: could not retrieve OBJ_bucket#63: no message found (10037)

After purging the subject there is no message there any more, the FirstSeq was not being updated if multiple blocks were removed.