diff --git a/.github/workflows/pushes.yaml b/.github/workflows/pushes.yaml index cbfee69e..510ec6ef 100644 --- a/.github/workflows/pushes.yaml +++ b/.github/workflows/pushes.yaml @@ -41,16 +41,16 @@ jobs: # The canonical entry is the only one where we run vet/lint/style checks. # `experimental: true` entries do not cause the tests to fail. include: - - go: '1.17.x' + - go: '1.19.x' os: ubuntu-latest canonical: true - - go: '1.16.x' + - go: '1.18.x' os: ubuntu-latest canonical: false - - go: '1.16.x' + - go: '1.19.x' os: windows-latest canonical: false - - go: '1.17.x' + - go: '1.19.x' os: macos-latest canonical: false diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index 23170af7..59a04550 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -41,7 +41,7 @@ jobs: - name: Set up Go uses: actions/setup-go@v2 with: - go-version: 1.17 + go-version: 1.18 check-latest: true - name: Install cosign diff --git a/cmd/importaccount_test.go b/cmd/importaccount_test.go index 43ac3a4f..78b89038 100644 --- a/cmd/importaccount_test.go +++ b/cmd/importaccount_test.go @@ -95,6 +95,7 @@ func Test_ImportDecoratedAccount(t *testing.T) { require.NoError(t, err) normal := filepath.Join(ts.Dir, "a.jwt") err = Write(normal, a) + require.NoError(t, err) // save a decorated jwt decorated := filepath.Join(ts.Dir, "decorated_a.jwt") diff --git a/go.mod b/go.mod index ba8406bc..23e54877 100644 --- a/go.mod +++ b/go.mod @@ -9,11 +9,11 @@ require ( github.com/inconshreveable/mousetrap v1.0.0 // indirect github.com/mitchellh/go-homedir v1.1.0 github.com/nats-io/cliprompts/v2 v2.0.0-20191226174129-372d79b36768 - github.com/nats-io/jsm.go v0.0.30 + github.com/nats-io/jsm.go v0.0.33 github.com/nats-io/jwt v1.2.2 - github.com/nats-io/jwt/v2 v2.2.1-0.20220509180118-3bcd719cc7d0 - github.com/nats-io/nats-server/v2 v2.7.5-0.20220309212130-5c0d1999ff72 - github.com/nats-io/nats.go v1.13.1-0.20220308171302-2f2f6968e98d + github.com/nats-io/jwt/v2 v2.3.0 + github.com/nats-io/nats-server/v2 v2.8.4 + github.com/nats-io/nats.go v1.16.0 github.com/nats-io/nkeys v0.3.0 github.com/nats-io/nuid v1.0.1 github.com/onsi/gomega v1.4.3 // indirect diff --git a/go.sum b/go.sum index d7fd9711..88151087 100644 --- a/go.sum +++ b/go.sum @@ -30,6 +30,7 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.7 h1:81/ik6ipDQS2aGcBfIN5dHDB36BwrStyeAQquSYCV4o= github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE= +github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/go-github/v30 v30.1.0 h1:VLDx+UolQICEOKu2m4uAoMti1SxuEBAl7RSEG16L+Oo= github.com/google/go-github/v30 v30.1.0/go.mod h1:n8jBpHl45a/rlBUtRJMOG4GhNADUQFEufcolZ95JfU8= github.com/google/go-querystring v1.0.0 h1:Xkwi/a1rcvNg1PPYe5vI8GbeBY/jrVuDX5ASuANWTrk= @@ -45,6 +46,8 @@ github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNU github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8= github.com/klauspost/compress v1.14.4 h1:eijASRJcobkVtSt81Olfh7JX43osYLwy5krOJo6YEu4= github.com/klauspost/compress v1.14.4/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= +github.com/klauspost/compress v1.15.5 h1:qyCLMz2JCrKADihKOh9FxnW3houKeNsp2h5OEz0QSEA= +github.com/klauspost/compress v1.15.5/go.mod h1:PhcZ0MbTNciWF3rruxRgKxI5NkcHHrHUDtV4Yw2GlzU= github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= @@ -70,15 +73,25 @@ github.com/nats-io/cliprompts/v2 v2.0.0-20191226174129-372d79b36768 h1:sdr8zfPeN github.com/nats-io/cliprompts/v2 v2.0.0-20191226174129-372d79b36768/go.mod h1:oweZn7AeaVJYKlNHfCIhznJVsdySLSng55vfuINE/d0= github.com/nats-io/jsm.go v0.0.30 h1:0PR9TuJV1FMBJMyZmH0hhwb+ASn0YAjBE6QqnSUrx6o= github.com/nats-io/jsm.go v0.0.30/go.mod h1:EKSYvbvWAoh0hIfuZ+ieWm8u0VOTRTeDfuQvNPKRqEg= +github.com/nats-io/jsm.go v0.0.33 h1:mNxlZEnSiHo9BwAFpjZYuopVvtwVUdtoAana2ovyWOU= +github.com/nats-io/jsm.go v0.0.33/go.mod h1:1ySvWrDbPo/Rs1v0Ccoy7QjZKBGfVhvmolfJRBX+fCg= github.com/nats-io/jwt v1.2.2 h1:w3GMTO969dFg+UOKTmmyuu7IGdusK+7Ytlt//OYH/uU= github.com/nats-io/jwt v1.2.2/go.mod h1:/xX356yQA6LuXI9xWW7mZNpxgF2mBmGecH+Fj34sP5Q= github.com/nats-io/jwt/v2 v2.2.1-0.20220113022732-58e87895b296/go.mod h1:0tqz9Hlu6bCBFLWAASKhE5vUA4c24L9KPUUgvwumE/k= +github.com/nats-io/jwt/v2 v2.2.1-0.20220330180145-442af02fd36a/go.mod h1:0tqz9Hlu6bCBFLWAASKhE5vUA4c24L9KPUUgvwumE/k= github.com/nats-io/jwt/v2 v2.2.1-0.20220509180118-3bcd719cc7d0 h1:R2KQtvAFeZ7AbMYZ90qWE26yrg/PxhZYRsC93ROw6o4= github.com/nats-io/jwt/v2 v2.2.1-0.20220509180118-3bcd719cc7d0/go.mod h1:0tqz9Hlu6bCBFLWAASKhE5vUA4c24L9KPUUgvwumE/k= +github.com/nats-io/jwt/v2 v2.3.0 h1:z2mA1a7tIf5ShggOFlR1oBPgd6hGqcDYsISxZByUzdI= +github.com/nats-io/jwt/v2 v2.3.0/go.mod h1:0tqz9Hlu6bCBFLWAASKhE5vUA4c24L9KPUUgvwumE/k= github.com/nats-io/nats-server/v2 v2.7.5-0.20220309212130-5c0d1999ff72 h1:Moe/K4fo/5FCNpE/TYrMt7sEPUuldBVJ0D4g/SWFkd0= github.com/nats-io/nats-server/v2 v2.7.5-0.20220309212130-5c0d1999ff72/go.mod h1:1vZ2Nijh8tcyNe8BDVyTviCd9NYzRbubQYiEHsvOQWc= +github.com/nats-io/nats-server/v2 v2.8.4 h1:0jQzze1T9mECg8YZEl8+WYUXb9JKluJfCBriPUtluB4= +github.com/nats-io/nats-server/v2 v2.8.4/go.mod h1:8zZa+Al3WsESfmgSs98Fi06dRWLH5Bnq90m5bKD/eT4= github.com/nats-io/nats.go v1.13.1-0.20220308171302-2f2f6968e98d h1:zJf4l8Kp67RIZhoVeniSLZs69SHNgjLHz0aNsqPPlx8= github.com/nats-io/nats.go v1.13.1-0.20220308171302-2f2f6968e98d/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w= +github.com/nats-io/nats.go v1.15.0/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w= +github.com/nats-io/nats.go v1.16.0 h1:zvLE7fGBQYW6MWaFaRdsgm9qT39PJDQoju+DS8KsO1g= +github.com/nats-io/nats.go v1.16.0/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w= github.com/nats-io/nkeys v0.2.0/go.mod h1:XdZpAbhgyyODYqjTawOnIOI7VlbKSarI9Gfy1tqEu/s= github.com/nats-io/nkeys v0.3.0 h1:cgM5tL53EvYRU+2YLXIK0G2mJtK12Ft9oeooSZMA2G8= github.com/nats-io/nkeys v0.3.0/go.mod h1:gvUNGjVcM2IPr5rCsRsC6Wb3Hr2CQAm08dsxtV6A5y4= @@ -115,6 +128,8 @@ golang.org/x/crypto v0.0.0-20200323165209-0ec3e9974c59/go.mod h1:LzIPMQfyMNhhGPh golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce h1:Roh6XWxHFKrPgC/EQhVubSAGQ6Ozk6IdxHSzt1mR0EI= golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd h1:XcWmESyNjXJMLahc3mqVQJcgSTDxFxhETVlfk9uGc38= +golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= diff --git a/vendor/github.com/klauspost/compress/s2/README.md b/vendor/github.com/klauspost/compress/s2/README.md index 11979345..73c0c462 100644 --- a/vendor/github.com/klauspost/compress/s2/README.md +++ b/vendor/github.com/klauspost/compress/s2/README.md @@ -19,6 +19,7 @@ This is important, so you don't have to worry about spending CPU cycles on alrea * Adjustable compression (3 levels) * Concurrent stream compression * Faster decompression, even for Snappy compatible content +* Concurrent Snappy/S2 stream decompression * Ability to quickly skip forward in compressed stream * Random seeking with indexes * Compatible with reading Snappy compressed content @@ -415,6 +416,25 @@ Without assembly decompression is also very fast; single goroutine decompression Even though S2 typically compresses better than Snappy, decompression speed is always better. +### Concurrent Stream Decompression + +For full stream decompression S2 offers a [DecodeConcurrent](https://pkg.go.dev/github.com/klauspost/compress/s2#Reader.DecodeConcurrent) +that will decode a full stream using multiple goroutines. + +Example scaling, AMD Ryzen 3950X, 16 cores, decompression using `s2d -bench=3 `, best of 3: + +| Input | `-cpu=1` | `-cpu=2` | `-cpu=4` | `-cpu=8` | `-cpu=16` | +|-------------------------------------------|------------|------------|------------|------------|-------------| +| enwik10.snappy | 1098.6MB/s | 1819.8MB/s | 3625.6MB/s | 6910.6MB/s | 10818.2MB/s | +| enwik10.s2 | 1303.5MB/s | 2606.1MB/s | 4847.9MB/s | 8878.4MB/s | 9592.1MB/s | +| sofia-air-quality-dataset.tar.snappy | 1302.0MB/s | 2165.0MB/s | 4244.5MB/s | 8241.0MB/s | 12920.5MB/s | +| sofia-air-quality-dataset.tar.s2 | 1399.2MB/s | 2463.2MB/s | 5196.5MB/s | 9639.8MB/s | 11439.5MB/s | +| sofia-air-quality-dataset.tar.s2 (no asm) | 837.5MB/s | 1652.6MB/s | 3183.6MB/s | 5945.0MB/s | 9620.7MB/s | + +Scaling can be expected to be pretty linear until memory bandwidth is saturated. + +For now the DecodeConcurrent can only be used for full streams without seeking or combining with regular reads. + ## Block compression @@ -873,7 +893,7 @@ for each entry { } // Uncompressed uses previous offset and adds EstBlockSize - entry[entryNum].UncompressedOffset = entry[entryNum-1].UncompressedOffset + EstBlockSize + entry[entryNum].UncompressedOffset = entry[entryNum-1].UncompressedOffset + EstBlockSize + uOff } @@ -901,6 +921,14 @@ for each entry { } ``` +To decode from any given uncompressed offset `(wantOffset)`: + +* Iterate entries until `entry[n].UncompressedOffset > wantOffset`. +* Start decoding from `entry[n-1].CompressedOffset`. +* Discard `entry[n-1].UncompressedOffset - wantOffset` bytes from the decoded stream. + +See [using indexes](https://github.com/klauspost/compress/tree/master/s2#using-indexes) for functions that perform the operations with a simpler interface. + # Format Extensions * Frame [Stream identifier](https://github.com/google/snappy/blob/master/framing_format.txt#L68) changed from `sNaPpY` to `S2sTwO`. diff --git a/vendor/github.com/klauspost/compress/s2/decode.go b/vendor/github.com/klauspost/compress/s2/decode.go index 9e7fce88..e2c1b16e 100644 --- a/vendor/github.com/klauspost/compress/s2/decode.go +++ b/vendor/github.com/klauspost/compress/s2/decode.go @@ -11,6 +11,8 @@ import ( "fmt" "io" "io/ioutil" + "runtime" + "sync" ) var ( @@ -196,13 +198,13 @@ type Reader struct { // ensureBufferSize will ensure that the buffer can take at least n bytes. // If false is returned the buffer exceeds maximum allowed size. func (r *Reader) ensureBufferSize(n int) bool { - if len(r.buf) >= n { - return true - } if n > r.maxBufSize { r.err = ErrCorrupt return false } + if cap(r.buf) >= n { + return true + } // Realloc buffer. r.buf = make([]byte, n) return true @@ -220,6 +222,7 @@ func (r *Reader) Reset(reader io.Reader) { r.err = nil r.i = 0 r.j = 0 + r.blockStart = 0 r.readHeader = r.ignoreStreamID } @@ -435,6 +438,259 @@ func (r *Reader) Read(p []byte) (int, error) { } } +// DecodeConcurrent will decode the full stream to w. +// This function should not be combined with reading, seeking or other operations. +// Up to 'concurrent' goroutines will be used. +// If <= 0, runtime.NumCPU will be used. +// On success the number of bytes decompressed nil and is returned. +// This is mainly intended for bigger streams. +func (r *Reader) DecodeConcurrent(w io.Writer, concurrent int) (written int64, err error) { + if r.i > 0 || r.j > 0 || r.blockStart > 0 { + return 0, errors.New("DecodeConcurrent called after ") + } + if concurrent <= 0 { + concurrent = runtime.NumCPU() + } + + // Write to output + var errMu sync.Mutex + var aErr error + setErr := func(e error) (ok bool) { + errMu.Lock() + defer errMu.Unlock() + if e == nil { + return aErr == nil + } + if aErr == nil { + aErr = e + } + return false + } + hasErr := func() (ok bool) { + errMu.Lock() + v := aErr != nil + errMu.Unlock() + return v + } + + var aWritten int64 + toRead := make(chan []byte, concurrent) + writtenBlocks := make(chan []byte, concurrent) + queue := make(chan chan []byte, concurrent) + reUse := make(chan chan []byte, concurrent) + for i := 0; i < concurrent; i++ { + toRead <- make([]byte, 0, r.maxBufSize) + writtenBlocks <- make([]byte, 0, r.maxBufSize) + reUse <- make(chan []byte, 1) + } + // Writer + var wg sync.WaitGroup + wg.Add(1) + go func() { + defer wg.Done() + for toWrite := range queue { + entry := <-toWrite + reUse <- toWrite + if hasErr() { + writtenBlocks <- entry + continue + } + n, err := w.Write(entry) + want := len(entry) + writtenBlocks <- entry + if err != nil { + setErr(err) + continue + } + if n != want { + setErr(io.ErrShortWrite) + continue + } + aWritten += int64(n) + } + }() + + // Reader + defer func() { + close(queue) + if r.err != nil { + err = r.err + setErr(r.err) + } + wg.Wait() + if err == nil { + err = aErr + } + written = aWritten + }() + + for !hasErr() { + if !r.readFull(r.buf[:4], true) { + if r.err == io.EOF { + r.err = nil + } + return 0, r.err + } + chunkType := r.buf[0] + if !r.readHeader { + if chunkType != chunkTypeStreamIdentifier { + r.err = ErrCorrupt + return 0, r.err + } + r.readHeader = true + } + chunkLen := int(r.buf[1]) | int(r.buf[2])<<8 | int(r.buf[3])<<16 + + // The chunk types are specified at + // https://github.com/google/snappy/blob/master/framing_format.txt + switch chunkType { + case chunkTypeCompressedData: + r.blockStart += int64(r.j) + // Section 4.2. Compressed data (chunk type 0x00). + if chunkLen < checksumSize { + r.err = ErrCorrupt + return 0, r.err + } + if chunkLen > r.maxBufSize { + r.err = ErrCorrupt + return 0, r.err + } + orgBuf := <-toRead + buf := orgBuf[:chunkLen] + + if !r.readFull(buf, false) { + return 0, r.err + } + + checksum := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24 + buf = buf[checksumSize:] + + n, err := DecodedLen(buf) + if err != nil { + r.err = err + return 0, r.err + } + if r.snappyFrame && n > maxSnappyBlockSize { + r.err = ErrCorrupt + return 0, r.err + } + + if n > r.maxBlock { + r.err = ErrCorrupt + return 0, r.err + } + wg.Add(1) + + decoded := <-writtenBlocks + entry := <-reUse + queue <- entry + go func() { + defer wg.Done() + decoded = decoded[:n] + _, err := Decode(decoded, buf) + toRead <- orgBuf + if err != nil { + writtenBlocks <- decoded + setErr(err) + return + } + if crc(decoded) != checksum { + writtenBlocks <- decoded + setErr(ErrCRC) + return + } + entry <- decoded + }() + continue + + case chunkTypeUncompressedData: + + // Section 4.3. Uncompressed data (chunk type 0x01). + if chunkLen < checksumSize { + r.err = ErrCorrupt + return 0, r.err + } + if chunkLen > r.maxBufSize { + r.err = ErrCorrupt + return 0, r.err + } + // Grab write buffer + orgBuf := <-writtenBlocks + buf := orgBuf[:checksumSize] + if !r.readFull(buf, false) { + return 0, r.err + } + checksum := uint32(buf[0]) | uint32(buf[1])<<8 | uint32(buf[2])<<16 | uint32(buf[3])<<24 + // Read content. + n := chunkLen - checksumSize + + if r.snappyFrame && n > maxSnappyBlockSize { + r.err = ErrCorrupt + return 0, r.err + } + if n > r.maxBlock { + r.err = ErrCorrupt + return 0, r.err + } + // Read uncompressed + buf = orgBuf[:n] + if !r.readFull(buf, false) { + return 0, r.err + } + + if crc(buf) != checksum { + r.err = ErrCRC + return 0, r.err + } + entry := <-reUse + queue <- entry + entry <- buf + continue + + case chunkTypeStreamIdentifier: + // Section 4.1. Stream identifier (chunk type 0xff). + if chunkLen != len(magicBody) { + r.err = ErrCorrupt + return 0, r.err + } + if !r.readFull(r.buf[:len(magicBody)], false) { + return 0, r.err + } + if string(r.buf[:len(magicBody)]) != magicBody { + if string(r.buf[:len(magicBody)]) != magicBodySnappy { + r.err = ErrCorrupt + return 0, r.err + } else { + r.snappyFrame = true + } + } else { + r.snappyFrame = false + } + continue + } + + if chunkType <= 0x7f { + // Section 4.5. Reserved unskippable chunks (chunk types 0x02-0x7f). + // fmt.Printf("ERR chunktype: 0x%x\n", chunkType) + r.err = ErrUnsupported + return 0, r.err + } + // Section 4.4 Padding (chunk type 0xfe). + // Section 4.6. Reserved skippable chunks (chunk types 0x80-0xfd). + if chunkLen > maxChunkSize { + // fmt.Printf("ERR chunkLen: 0x%x\n", chunkLen) + r.err = ErrUnsupported + return 0, r.err + } + + // fmt.Printf("skippable: ID: 0x%x, len: 0x%x\n", chunkType, chunkLen) + if !r.skippable(r.buf, chunkLen, false, chunkType) { + return 0, r.err + } + } + return 0, r.err +} + // Skip will skip n bytes forward in the decompressed output. // For larger skips this consumes less CPU and is faster than reading output and discarding it. // CRC is not checked on skipped blocks. @@ -699,8 +955,16 @@ func (r *ReadSeeker) Seek(offset int64, whence int) (int64, error) { case io.SeekCurrent: offset += r.blockStart + int64(r.i) case io.SeekEnd: - offset = -offset + if offset > 0 { + return 0, errors.New("seek after end of file") + } + offset = r.index.TotalUncompressed + offset } + + if offset < 0 { + return 0, errors.New("seek before start of file") + } + c, u, err := r.index.Find(offset) if err != nil { return r.blockStart + int64(r.i), err @@ -712,10 +976,6 @@ func (r *ReadSeeker) Seek(offset int64, whence int) (int64, error) { return 0, err } - if offset < 0 { - offset = r.index.TotalUncompressed + offset - } - r.i = r.j // Remove rest of current block. if u < offset { // Forward inside block diff --git a/vendor/github.com/klauspost/compress/s2/encodeblock_amd64.go b/vendor/github.com/klauspost/compress/s2/encodeblock_amd64.go index d9312e5b..88f27c09 100644 --- a/vendor/github.com/klauspost/compress/s2/encodeblock_amd64.go +++ b/vendor/github.com/klauspost/compress/s2/encodeblock_amd64.go @@ -5,6 +5,8 @@ package s2 +func _dummy_() + // encodeBlockAsm encodes a non-empty src to a guaranteed-large-enough dst. // Maximum input 4294967295 bytes. // It assumes that the varint-encoded length of the decompressed bytes has already been written. diff --git a/vendor/github.com/klauspost/compress/s2/encodeblock_amd64.s b/vendor/github.com/klauspost/compress/s2/encodeblock_amd64.s index 729dbf53..337a73ca 100644 --- a/vendor/github.com/klauspost/compress/s2/encodeblock_amd64.s +++ b/vendor/github.com/klauspost/compress/s2/encodeblock_amd64.s @@ -5,6 +5,15 @@ #include "textflag.h" +// func _dummy_() +TEXT ·_dummy_(SB), $0 +#ifdef GOAMD64_v4 +#ifndef GOAMD64_v3 +#define GOAMD64_v3 +#endif +#endif + RET + // func encodeBlockAsm(dst []byte, src []byte) int // Requires: BMI, SSE2 TEXT ·encodeBlockAsm(SB), $65560-56 @@ -253,17 +262,6 @@ matchlen_loopback_repeat_extend_encodeBlockAsm: #ifdef GOAMD64_v3 TZCNTQ R11, R11 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R11, R11 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R11, R11 @@ -791,17 +789,6 @@ matchlen_loopback_match_nolit_encodeBlockAsm: #ifdef GOAMD64_v3 TZCNTQ R9, R9 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R9, R9 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R9, R9 @@ -1134,17 +1121,36 @@ memmove_emit_remainder_encodeBlockAsm: MOVL SI, BX // genMemMoveShort + CMPQ BX, $0x03 + JB emit_lit_memmove_emit_remainder_encodeBlockAsm_memmove_move_1or2 + JE emit_lit_memmove_emit_remainder_encodeBlockAsm_memmove_move_3 CMPQ BX, $0x08 - JLE emit_lit_memmove_emit_remainder_encodeBlockAsm_memmove_move_8 + JB emit_lit_memmove_emit_remainder_encodeBlockAsm_memmove_move_4through7 CMPQ BX, $0x10 JBE emit_lit_memmove_emit_remainder_encodeBlockAsm_memmove_move_8through16 CMPQ BX, $0x20 JBE emit_lit_memmove_emit_remainder_encodeBlockAsm_memmove_move_17through32 JMP emit_lit_memmove_emit_remainder_encodeBlockAsm_memmove_move_33through64 -emit_lit_memmove_emit_remainder_encodeBlockAsm_memmove_move_8: - MOVQ (CX), SI - MOVQ SI, (AX) +emit_lit_memmove_emit_remainder_encodeBlockAsm_memmove_move_1or2: + MOVB (CX), SI + MOVB -1(CX)(BX*1), CL + MOVB SI, (AX) + MOVB CL, -1(AX)(BX*1) + JMP memmove_end_copy_emit_remainder_encodeBlockAsm + +emit_lit_memmove_emit_remainder_encodeBlockAsm_memmove_move_3: + MOVW (CX), SI + MOVB 2(CX), CL + MOVW SI, (AX) + MOVB CL, 2(AX) + JMP memmove_end_copy_emit_remainder_encodeBlockAsm + +emit_lit_memmove_emit_remainder_encodeBlockAsm_memmove_move_4through7: + MOVL (CX), SI + MOVL -4(CX)(BX*1), CX + MOVL SI, (AX) + MOVL CX, -4(AX)(BX*1) JMP memmove_end_copy_emit_remainder_encodeBlockAsm emit_lit_memmove_emit_remainder_encodeBlockAsm_memmove_move_8through16: @@ -1466,17 +1472,6 @@ matchlen_loopback_repeat_extend_encodeBlockAsm4MB: #ifdef GOAMD64_v3 TZCNTQ R11, R11 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R11, R11 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R11, R11 @@ -1963,17 +1958,6 @@ matchlen_loopback_match_nolit_encodeBlockAsm4MB: #ifdef GOAMD64_v3 TZCNTQ R9, R9 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R9, R9 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R9, R9 @@ -2276,17 +2260,36 @@ memmove_emit_remainder_encodeBlockAsm4MB: MOVL SI, BX // genMemMoveShort + CMPQ BX, $0x03 + JB emit_lit_memmove_emit_remainder_encodeBlockAsm4MB_memmove_move_1or2 + JE emit_lit_memmove_emit_remainder_encodeBlockAsm4MB_memmove_move_3 CMPQ BX, $0x08 - JLE emit_lit_memmove_emit_remainder_encodeBlockAsm4MB_memmove_move_8 + JB emit_lit_memmove_emit_remainder_encodeBlockAsm4MB_memmove_move_4through7 CMPQ BX, $0x10 JBE emit_lit_memmove_emit_remainder_encodeBlockAsm4MB_memmove_move_8through16 CMPQ BX, $0x20 JBE emit_lit_memmove_emit_remainder_encodeBlockAsm4MB_memmove_move_17through32 JMP emit_lit_memmove_emit_remainder_encodeBlockAsm4MB_memmove_move_33through64 -emit_lit_memmove_emit_remainder_encodeBlockAsm4MB_memmove_move_8: - MOVQ (CX), SI - MOVQ SI, (AX) +emit_lit_memmove_emit_remainder_encodeBlockAsm4MB_memmove_move_1or2: + MOVB (CX), SI + MOVB -1(CX)(BX*1), CL + MOVB SI, (AX) + MOVB CL, -1(AX)(BX*1) + JMP memmove_end_copy_emit_remainder_encodeBlockAsm4MB + +emit_lit_memmove_emit_remainder_encodeBlockAsm4MB_memmove_move_3: + MOVW (CX), SI + MOVB 2(CX), CL + MOVW SI, (AX) + MOVB CL, 2(AX) + JMP memmove_end_copy_emit_remainder_encodeBlockAsm4MB + +emit_lit_memmove_emit_remainder_encodeBlockAsm4MB_memmove_move_4through7: + MOVL (CX), SI + MOVL -4(CX)(BX*1), CX + MOVL SI, (AX) + MOVL CX, -4(AX)(BX*1) JMP memmove_end_copy_emit_remainder_encodeBlockAsm4MB emit_lit_memmove_emit_remainder_encodeBlockAsm4MB_memmove_move_8through16: @@ -2597,17 +2600,6 @@ matchlen_loopback_repeat_extend_encodeBlockAsm12B: #ifdef GOAMD64_v3 TZCNTQ R11, R11 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R11, R11 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R11, R11 @@ -2979,17 +2971,6 @@ matchlen_loopback_match_nolit_encodeBlockAsm12B: #ifdef GOAMD64_v3 TZCNTQ R9, R9 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R9, R9 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R9, R9 @@ -3189,17 +3170,36 @@ memmove_emit_remainder_encodeBlockAsm12B: MOVL SI, BX // genMemMoveShort + CMPQ BX, $0x03 + JB emit_lit_memmove_emit_remainder_encodeBlockAsm12B_memmove_move_1or2 + JE emit_lit_memmove_emit_remainder_encodeBlockAsm12B_memmove_move_3 CMPQ BX, $0x08 - JLE emit_lit_memmove_emit_remainder_encodeBlockAsm12B_memmove_move_8 + JB emit_lit_memmove_emit_remainder_encodeBlockAsm12B_memmove_move_4through7 CMPQ BX, $0x10 JBE emit_lit_memmove_emit_remainder_encodeBlockAsm12B_memmove_move_8through16 CMPQ BX, $0x20 JBE emit_lit_memmove_emit_remainder_encodeBlockAsm12B_memmove_move_17through32 JMP emit_lit_memmove_emit_remainder_encodeBlockAsm12B_memmove_move_33through64 -emit_lit_memmove_emit_remainder_encodeBlockAsm12B_memmove_move_8: - MOVQ (CX), SI - MOVQ SI, (AX) +emit_lit_memmove_emit_remainder_encodeBlockAsm12B_memmove_move_1or2: + MOVB (CX), SI + MOVB -1(CX)(BX*1), CL + MOVB SI, (AX) + MOVB CL, -1(AX)(BX*1) + JMP memmove_end_copy_emit_remainder_encodeBlockAsm12B + +emit_lit_memmove_emit_remainder_encodeBlockAsm12B_memmove_move_3: + MOVW (CX), SI + MOVB 2(CX), CL + MOVW SI, (AX) + MOVB CL, 2(AX) + JMP memmove_end_copy_emit_remainder_encodeBlockAsm12B + +emit_lit_memmove_emit_remainder_encodeBlockAsm12B_memmove_move_4through7: + MOVL (CX), SI + MOVL -4(CX)(BX*1), CX + MOVL SI, (AX) + MOVL CX, -4(AX)(BX*1) JMP memmove_end_copy_emit_remainder_encodeBlockAsm12B emit_lit_memmove_emit_remainder_encodeBlockAsm12B_memmove_move_8through16: @@ -3510,17 +3510,6 @@ matchlen_loopback_repeat_extend_encodeBlockAsm10B: #ifdef GOAMD64_v3 TZCNTQ R11, R11 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R11, R11 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R11, R11 @@ -3892,17 +3881,6 @@ matchlen_loopback_match_nolit_encodeBlockAsm10B: #ifdef GOAMD64_v3 TZCNTQ R9, R9 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R9, R9 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R9, R9 @@ -4102,17 +4080,36 @@ memmove_emit_remainder_encodeBlockAsm10B: MOVL SI, BX // genMemMoveShort + CMPQ BX, $0x03 + JB emit_lit_memmove_emit_remainder_encodeBlockAsm10B_memmove_move_1or2 + JE emit_lit_memmove_emit_remainder_encodeBlockAsm10B_memmove_move_3 CMPQ BX, $0x08 - JLE emit_lit_memmove_emit_remainder_encodeBlockAsm10B_memmove_move_8 + JB emit_lit_memmove_emit_remainder_encodeBlockAsm10B_memmove_move_4through7 CMPQ BX, $0x10 JBE emit_lit_memmove_emit_remainder_encodeBlockAsm10B_memmove_move_8through16 CMPQ BX, $0x20 JBE emit_lit_memmove_emit_remainder_encodeBlockAsm10B_memmove_move_17through32 JMP emit_lit_memmove_emit_remainder_encodeBlockAsm10B_memmove_move_33through64 -emit_lit_memmove_emit_remainder_encodeBlockAsm10B_memmove_move_8: - MOVQ (CX), SI - MOVQ SI, (AX) +emit_lit_memmove_emit_remainder_encodeBlockAsm10B_memmove_move_1or2: + MOVB (CX), SI + MOVB -1(CX)(BX*1), CL + MOVB SI, (AX) + MOVB CL, -1(AX)(BX*1) + JMP memmove_end_copy_emit_remainder_encodeBlockAsm10B + +emit_lit_memmove_emit_remainder_encodeBlockAsm10B_memmove_move_3: + MOVW (CX), SI + MOVB 2(CX), CL + MOVW SI, (AX) + MOVB CL, 2(AX) + JMP memmove_end_copy_emit_remainder_encodeBlockAsm10B + +emit_lit_memmove_emit_remainder_encodeBlockAsm10B_memmove_move_4through7: + MOVL (CX), SI + MOVL -4(CX)(BX*1), CX + MOVL SI, (AX) + MOVL CX, -4(AX)(BX*1) JMP memmove_end_copy_emit_remainder_encodeBlockAsm10B emit_lit_memmove_emit_remainder_encodeBlockAsm10B_memmove_move_8through16: @@ -4423,17 +4420,6 @@ matchlen_loopback_repeat_extend_encodeBlockAsm8B: #ifdef GOAMD64_v3 TZCNTQ R11, R11 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R11, R11 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R11, R11 @@ -4795,17 +4781,6 @@ matchlen_loopback_match_nolit_encodeBlockAsm8B: #ifdef GOAMD64_v3 TZCNTQ R9, R9 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R9, R9 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R9, R9 @@ -4999,17 +4974,36 @@ memmove_emit_remainder_encodeBlockAsm8B: MOVL SI, BX // genMemMoveShort + CMPQ BX, $0x03 + JB emit_lit_memmove_emit_remainder_encodeBlockAsm8B_memmove_move_1or2 + JE emit_lit_memmove_emit_remainder_encodeBlockAsm8B_memmove_move_3 CMPQ BX, $0x08 - JLE emit_lit_memmove_emit_remainder_encodeBlockAsm8B_memmove_move_8 + JB emit_lit_memmove_emit_remainder_encodeBlockAsm8B_memmove_move_4through7 CMPQ BX, $0x10 JBE emit_lit_memmove_emit_remainder_encodeBlockAsm8B_memmove_move_8through16 CMPQ BX, $0x20 JBE emit_lit_memmove_emit_remainder_encodeBlockAsm8B_memmove_move_17through32 JMP emit_lit_memmove_emit_remainder_encodeBlockAsm8B_memmove_move_33through64 -emit_lit_memmove_emit_remainder_encodeBlockAsm8B_memmove_move_8: - MOVQ (CX), SI - MOVQ SI, (AX) +emit_lit_memmove_emit_remainder_encodeBlockAsm8B_memmove_move_1or2: + MOVB (CX), SI + MOVB -1(CX)(BX*1), CL + MOVB SI, (AX) + MOVB CL, -1(AX)(BX*1) + JMP memmove_end_copy_emit_remainder_encodeBlockAsm8B + +emit_lit_memmove_emit_remainder_encodeBlockAsm8B_memmove_move_3: + MOVW (CX), SI + MOVB 2(CX), CL + MOVW SI, (AX) + MOVB CL, 2(AX) + JMP memmove_end_copy_emit_remainder_encodeBlockAsm8B + +emit_lit_memmove_emit_remainder_encodeBlockAsm8B_memmove_move_4through7: + MOVL (CX), SI + MOVL -4(CX)(BX*1), CX + MOVL SI, (AX) + MOVL CX, -4(AX)(BX*1) JMP memmove_end_copy_emit_remainder_encodeBlockAsm8B emit_lit_memmove_emit_remainder_encodeBlockAsm8B_memmove_move_8through16: @@ -5225,17 +5219,6 @@ matchlen_loopback_match_nolit_encodeBetterBlockAsm: #ifdef GOAMD64_v3 TZCNTQ R11, R11 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R11, R11 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R11, R11 @@ -5979,8 +5962,9 @@ memmove_emit_remainder_encodeBetterBlockAsm: MOVL SI, BX // genMemMoveShort - CMPQ BX, $0x04 - JLE emit_lit_memmove_emit_remainder_encodeBetterBlockAsm_memmove_move_4 + CMPQ BX, $0x03 + JB emit_lit_memmove_emit_remainder_encodeBetterBlockAsm_memmove_move_1or2 + JE emit_lit_memmove_emit_remainder_encodeBetterBlockAsm_memmove_move_3 CMPQ BX, $0x08 JB emit_lit_memmove_emit_remainder_encodeBetterBlockAsm_memmove_move_4through7 CMPQ BX, $0x10 @@ -5989,9 +5973,18 @@ memmove_emit_remainder_encodeBetterBlockAsm: JBE emit_lit_memmove_emit_remainder_encodeBetterBlockAsm_memmove_move_17through32 JMP emit_lit_memmove_emit_remainder_encodeBetterBlockAsm_memmove_move_33through64 -emit_lit_memmove_emit_remainder_encodeBetterBlockAsm_memmove_move_4: - MOVL (CX), SI - MOVL SI, (AX) +emit_lit_memmove_emit_remainder_encodeBetterBlockAsm_memmove_move_1or2: + MOVB (CX), SI + MOVB -1(CX)(BX*1), CL + MOVB SI, (AX) + MOVB CL, -1(AX)(BX*1) + JMP memmove_end_copy_emit_remainder_encodeBetterBlockAsm + +emit_lit_memmove_emit_remainder_encodeBetterBlockAsm_memmove_move_3: + MOVW (CX), SI + MOVB 2(CX), CL + MOVW SI, (AX) + MOVB CL, 2(AX) JMP memmove_end_copy_emit_remainder_encodeBetterBlockAsm emit_lit_memmove_emit_remainder_encodeBetterBlockAsm_memmove_move_4through7: @@ -6214,17 +6207,6 @@ matchlen_loopback_match_nolit_encodeBetterBlockAsm4MB: #ifdef GOAMD64_v3 TZCNTQ R11, R11 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R11, R11 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R11, R11 @@ -6911,8 +6893,9 @@ memmove_emit_remainder_encodeBetterBlockAsm4MB: MOVL SI, BX // genMemMoveShort - CMPQ BX, $0x04 - JLE emit_lit_memmove_emit_remainder_encodeBetterBlockAsm4MB_memmove_move_4 + CMPQ BX, $0x03 + JB emit_lit_memmove_emit_remainder_encodeBetterBlockAsm4MB_memmove_move_1or2 + JE emit_lit_memmove_emit_remainder_encodeBetterBlockAsm4MB_memmove_move_3 CMPQ BX, $0x08 JB emit_lit_memmove_emit_remainder_encodeBetterBlockAsm4MB_memmove_move_4through7 CMPQ BX, $0x10 @@ -6921,9 +6904,18 @@ memmove_emit_remainder_encodeBetterBlockAsm4MB: JBE emit_lit_memmove_emit_remainder_encodeBetterBlockAsm4MB_memmove_move_17through32 JMP emit_lit_memmove_emit_remainder_encodeBetterBlockAsm4MB_memmove_move_33through64 -emit_lit_memmove_emit_remainder_encodeBetterBlockAsm4MB_memmove_move_4: - MOVL (CX), SI - MOVL SI, (AX) +emit_lit_memmove_emit_remainder_encodeBetterBlockAsm4MB_memmove_move_1or2: + MOVB (CX), SI + MOVB -1(CX)(BX*1), CL + MOVB SI, (AX) + MOVB CL, -1(AX)(BX*1) + JMP memmove_end_copy_emit_remainder_encodeBetterBlockAsm4MB + +emit_lit_memmove_emit_remainder_encodeBetterBlockAsm4MB_memmove_move_3: + MOVW (CX), SI + MOVB 2(CX), CL + MOVW SI, (AX) + MOVB CL, 2(AX) JMP memmove_end_copy_emit_remainder_encodeBetterBlockAsm4MB emit_lit_memmove_emit_remainder_encodeBetterBlockAsm4MB_memmove_move_4through7: @@ -7138,17 +7130,6 @@ matchlen_loopback_match_nolit_encodeBetterBlockAsm12B: #ifdef GOAMD64_v3 TZCNTQ R11, R11 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R11, R11 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R11, R11 @@ -7689,8 +7670,9 @@ memmove_emit_remainder_encodeBetterBlockAsm12B: MOVL SI, BX // genMemMoveShort - CMPQ BX, $0x04 - JLE emit_lit_memmove_emit_remainder_encodeBetterBlockAsm12B_memmove_move_4 + CMPQ BX, $0x03 + JB emit_lit_memmove_emit_remainder_encodeBetterBlockAsm12B_memmove_move_1or2 + JE emit_lit_memmove_emit_remainder_encodeBetterBlockAsm12B_memmove_move_3 CMPQ BX, $0x08 JB emit_lit_memmove_emit_remainder_encodeBetterBlockAsm12B_memmove_move_4through7 CMPQ BX, $0x10 @@ -7699,9 +7681,18 @@ memmove_emit_remainder_encodeBetterBlockAsm12B: JBE emit_lit_memmove_emit_remainder_encodeBetterBlockAsm12B_memmove_move_17through32 JMP emit_lit_memmove_emit_remainder_encodeBetterBlockAsm12B_memmove_move_33through64 -emit_lit_memmove_emit_remainder_encodeBetterBlockAsm12B_memmove_move_4: - MOVL (CX), SI - MOVL SI, (AX) +emit_lit_memmove_emit_remainder_encodeBetterBlockAsm12B_memmove_move_1or2: + MOVB (CX), SI + MOVB -1(CX)(BX*1), CL + MOVB SI, (AX) + MOVB CL, -1(AX)(BX*1) + JMP memmove_end_copy_emit_remainder_encodeBetterBlockAsm12B + +emit_lit_memmove_emit_remainder_encodeBetterBlockAsm12B_memmove_move_3: + MOVW (CX), SI + MOVB 2(CX), CL + MOVW SI, (AX) + MOVB CL, 2(AX) JMP memmove_end_copy_emit_remainder_encodeBetterBlockAsm12B emit_lit_memmove_emit_remainder_encodeBetterBlockAsm12B_memmove_move_4through7: @@ -7916,17 +7907,6 @@ matchlen_loopback_match_nolit_encodeBetterBlockAsm10B: #ifdef GOAMD64_v3 TZCNTQ R11, R11 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R11, R11 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R11, R11 @@ -8467,8 +8447,9 @@ memmove_emit_remainder_encodeBetterBlockAsm10B: MOVL SI, BX // genMemMoveShort - CMPQ BX, $0x04 - JLE emit_lit_memmove_emit_remainder_encodeBetterBlockAsm10B_memmove_move_4 + CMPQ BX, $0x03 + JB emit_lit_memmove_emit_remainder_encodeBetterBlockAsm10B_memmove_move_1or2 + JE emit_lit_memmove_emit_remainder_encodeBetterBlockAsm10B_memmove_move_3 CMPQ BX, $0x08 JB emit_lit_memmove_emit_remainder_encodeBetterBlockAsm10B_memmove_move_4through7 CMPQ BX, $0x10 @@ -8477,9 +8458,18 @@ memmove_emit_remainder_encodeBetterBlockAsm10B: JBE emit_lit_memmove_emit_remainder_encodeBetterBlockAsm10B_memmove_move_17through32 JMP emit_lit_memmove_emit_remainder_encodeBetterBlockAsm10B_memmove_move_33through64 -emit_lit_memmove_emit_remainder_encodeBetterBlockAsm10B_memmove_move_4: - MOVL (CX), SI - MOVL SI, (AX) +emit_lit_memmove_emit_remainder_encodeBetterBlockAsm10B_memmove_move_1or2: + MOVB (CX), SI + MOVB -1(CX)(BX*1), CL + MOVB SI, (AX) + MOVB CL, -1(AX)(BX*1) + JMP memmove_end_copy_emit_remainder_encodeBetterBlockAsm10B + +emit_lit_memmove_emit_remainder_encodeBetterBlockAsm10B_memmove_move_3: + MOVW (CX), SI + MOVB 2(CX), CL + MOVW SI, (AX) + MOVB CL, 2(AX) JMP memmove_end_copy_emit_remainder_encodeBetterBlockAsm10B emit_lit_memmove_emit_remainder_encodeBetterBlockAsm10B_memmove_move_4through7: @@ -8694,17 +8684,6 @@ matchlen_loopback_match_nolit_encodeBetterBlockAsm8B: #ifdef GOAMD64_v3 TZCNTQ R11, R11 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R11, R11 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R11, R11 @@ -9235,8 +9214,9 @@ memmove_emit_remainder_encodeBetterBlockAsm8B: MOVL SI, BX // genMemMoveShort - CMPQ BX, $0x04 - JLE emit_lit_memmove_emit_remainder_encodeBetterBlockAsm8B_memmove_move_4 + CMPQ BX, $0x03 + JB emit_lit_memmove_emit_remainder_encodeBetterBlockAsm8B_memmove_move_1or2 + JE emit_lit_memmove_emit_remainder_encodeBetterBlockAsm8B_memmove_move_3 CMPQ BX, $0x08 JB emit_lit_memmove_emit_remainder_encodeBetterBlockAsm8B_memmove_move_4through7 CMPQ BX, $0x10 @@ -9245,9 +9225,18 @@ memmove_emit_remainder_encodeBetterBlockAsm8B: JBE emit_lit_memmove_emit_remainder_encodeBetterBlockAsm8B_memmove_move_17through32 JMP emit_lit_memmove_emit_remainder_encodeBetterBlockAsm8B_memmove_move_33through64 -emit_lit_memmove_emit_remainder_encodeBetterBlockAsm8B_memmove_move_4: - MOVL (CX), SI - MOVL SI, (AX) +emit_lit_memmove_emit_remainder_encodeBetterBlockAsm8B_memmove_move_1or2: + MOVB (CX), SI + MOVB -1(CX)(BX*1), CL + MOVB SI, (AX) + MOVB CL, -1(AX)(BX*1) + JMP memmove_end_copy_emit_remainder_encodeBetterBlockAsm8B + +emit_lit_memmove_emit_remainder_encodeBetterBlockAsm8B_memmove_move_3: + MOVW (CX), SI + MOVB 2(CX), CL + MOVW SI, (AX) + MOVB CL, 2(AX) JMP memmove_end_copy_emit_remainder_encodeBetterBlockAsm8B emit_lit_memmove_emit_remainder_encodeBetterBlockAsm8B_memmove_move_4through7: @@ -9584,17 +9573,6 @@ matchlen_loopback_repeat_extend_encodeSnappyBlockAsm: #ifdef GOAMD64_v3 TZCNTQ R10, R10 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R10, R10 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R10, R10 @@ -9918,17 +9896,6 @@ matchlen_loopback_match_nolit_encodeSnappyBlockAsm: #ifdef GOAMD64_v3 TZCNTQ R9, R9 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R9, R9 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R9, R9 @@ -10127,17 +10094,36 @@ memmove_emit_remainder_encodeSnappyBlockAsm: MOVL SI, BX // genMemMoveShort + CMPQ BX, $0x03 + JB emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm_memmove_move_1or2 + JE emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm_memmove_move_3 CMPQ BX, $0x08 - JLE emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm_memmove_move_8 + JB emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm_memmove_move_4through7 CMPQ BX, $0x10 JBE emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm_memmove_move_8through16 CMPQ BX, $0x20 JBE emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm_memmove_move_17through32 JMP emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm_memmove_move_33through64 -emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm_memmove_move_8: - MOVQ (CX), SI - MOVQ SI, (AX) +emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm_memmove_move_1or2: + MOVB (CX), SI + MOVB -1(CX)(BX*1), CL + MOVB SI, (AX) + MOVB CL, -1(AX)(BX*1) + JMP memmove_end_copy_emit_remainder_encodeSnappyBlockAsm + +emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm_memmove_move_3: + MOVW (CX), SI + MOVB 2(CX), CL + MOVW SI, (AX) + MOVB CL, 2(AX) + JMP memmove_end_copy_emit_remainder_encodeSnappyBlockAsm + +emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm_memmove_move_4through7: + MOVL (CX), SI + MOVL -4(CX)(BX*1), CX + MOVL SI, (AX) + MOVL CX, -4(AX)(BX*1) JMP memmove_end_copy_emit_remainder_encodeSnappyBlockAsm emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm_memmove_move_8through16: @@ -10448,17 +10434,6 @@ matchlen_loopback_repeat_extend_encodeSnappyBlockAsm64K: #ifdef GOAMD64_v3 TZCNTQ R10, R10 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R10, R10 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R10, R10 @@ -10739,17 +10714,6 @@ matchlen_loopback_match_nolit_encodeSnappyBlockAsm64K: #ifdef GOAMD64_v3 TZCNTQ R9, R9 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R9, R9 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R9, R9 @@ -10905,17 +10869,36 @@ memmove_emit_remainder_encodeSnappyBlockAsm64K: MOVL SI, BX // genMemMoveShort + CMPQ BX, $0x03 + JB emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm64K_memmove_move_1or2 + JE emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm64K_memmove_move_3 CMPQ BX, $0x08 - JLE emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm64K_memmove_move_8 + JB emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm64K_memmove_move_4through7 CMPQ BX, $0x10 JBE emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm64K_memmove_move_8through16 CMPQ BX, $0x20 JBE emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm64K_memmove_move_17through32 JMP emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm64K_memmove_move_33through64 -emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm64K_memmove_move_8: - MOVQ (CX), SI - MOVQ SI, (AX) +emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm64K_memmove_move_1or2: + MOVB (CX), SI + MOVB -1(CX)(BX*1), CL + MOVB SI, (AX) + MOVB CL, -1(AX)(BX*1) + JMP memmove_end_copy_emit_remainder_encodeSnappyBlockAsm64K + +emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm64K_memmove_move_3: + MOVW (CX), SI + MOVB 2(CX), CL + MOVW SI, (AX) + MOVB CL, 2(AX) + JMP memmove_end_copy_emit_remainder_encodeSnappyBlockAsm64K + +emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm64K_memmove_move_4through7: + MOVL (CX), SI + MOVL -4(CX)(BX*1), CX + MOVL SI, (AX) + MOVL CX, -4(AX)(BX*1) JMP memmove_end_copy_emit_remainder_encodeSnappyBlockAsm64K emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm64K_memmove_move_8through16: @@ -11226,17 +11209,6 @@ matchlen_loopback_repeat_extend_encodeSnappyBlockAsm12B: #ifdef GOAMD64_v3 TZCNTQ R10, R10 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R10, R10 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R10, R10 @@ -11517,17 +11489,6 @@ matchlen_loopback_match_nolit_encodeSnappyBlockAsm12B: #ifdef GOAMD64_v3 TZCNTQ R9, R9 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R9, R9 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R9, R9 @@ -11683,17 +11644,36 @@ memmove_emit_remainder_encodeSnappyBlockAsm12B: MOVL SI, BX // genMemMoveShort + CMPQ BX, $0x03 + JB emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm12B_memmove_move_1or2 + JE emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm12B_memmove_move_3 CMPQ BX, $0x08 - JLE emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm12B_memmove_move_8 + JB emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm12B_memmove_move_4through7 CMPQ BX, $0x10 JBE emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm12B_memmove_move_8through16 CMPQ BX, $0x20 JBE emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm12B_memmove_move_17through32 JMP emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm12B_memmove_move_33through64 -emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm12B_memmove_move_8: - MOVQ (CX), SI - MOVQ SI, (AX) +emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm12B_memmove_move_1or2: + MOVB (CX), SI + MOVB -1(CX)(BX*1), CL + MOVB SI, (AX) + MOVB CL, -1(AX)(BX*1) + JMP memmove_end_copy_emit_remainder_encodeSnappyBlockAsm12B + +emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm12B_memmove_move_3: + MOVW (CX), SI + MOVB 2(CX), CL + MOVW SI, (AX) + MOVB CL, 2(AX) + JMP memmove_end_copy_emit_remainder_encodeSnappyBlockAsm12B + +emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm12B_memmove_move_4through7: + MOVL (CX), SI + MOVL -4(CX)(BX*1), CX + MOVL SI, (AX) + MOVL CX, -4(AX)(BX*1) JMP memmove_end_copy_emit_remainder_encodeSnappyBlockAsm12B emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm12B_memmove_move_8through16: @@ -12004,17 +11984,6 @@ matchlen_loopback_repeat_extend_encodeSnappyBlockAsm10B: #ifdef GOAMD64_v3 TZCNTQ R10, R10 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R10, R10 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R10, R10 @@ -12295,17 +12264,6 @@ matchlen_loopback_match_nolit_encodeSnappyBlockAsm10B: #ifdef GOAMD64_v3 TZCNTQ R9, R9 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R9, R9 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R9, R9 @@ -12461,17 +12419,36 @@ memmove_emit_remainder_encodeSnappyBlockAsm10B: MOVL SI, BX // genMemMoveShort + CMPQ BX, $0x03 + JB emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm10B_memmove_move_1or2 + JE emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm10B_memmove_move_3 CMPQ BX, $0x08 - JLE emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm10B_memmove_move_8 + JB emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm10B_memmove_move_4through7 CMPQ BX, $0x10 JBE emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm10B_memmove_move_8through16 CMPQ BX, $0x20 JBE emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm10B_memmove_move_17through32 JMP emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm10B_memmove_move_33through64 -emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm10B_memmove_move_8: - MOVQ (CX), SI - MOVQ SI, (AX) +emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm10B_memmove_move_1or2: + MOVB (CX), SI + MOVB -1(CX)(BX*1), CL + MOVB SI, (AX) + MOVB CL, -1(AX)(BX*1) + JMP memmove_end_copy_emit_remainder_encodeSnappyBlockAsm10B + +emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm10B_memmove_move_3: + MOVW (CX), SI + MOVB 2(CX), CL + MOVW SI, (AX) + MOVB CL, 2(AX) + JMP memmove_end_copy_emit_remainder_encodeSnappyBlockAsm10B + +emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm10B_memmove_move_4through7: + MOVL (CX), SI + MOVL -4(CX)(BX*1), CX + MOVL SI, (AX) + MOVL CX, -4(AX)(BX*1) JMP memmove_end_copy_emit_remainder_encodeSnappyBlockAsm10B emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm10B_memmove_move_8through16: @@ -12782,17 +12759,6 @@ matchlen_loopback_repeat_extend_encodeSnappyBlockAsm8B: #ifdef GOAMD64_v3 TZCNTQ R10, R10 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R10, R10 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R10, R10 @@ -13071,17 +13037,6 @@ matchlen_loopback_match_nolit_encodeSnappyBlockAsm8B: #ifdef GOAMD64_v3 TZCNTQ R9, R9 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R9, R9 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R9, R9 @@ -13235,17 +13190,36 @@ memmove_emit_remainder_encodeSnappyBlockAsm8B: MOVL SI, BX // genMemMoveShort + CMPQ BX, $0x03 + JB emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm8B_memmove_move_1or2 + JE emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm8B_memmove_move_3 CMPQ BX, $0x08 - JLE emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm8B_memmove_move_8 + JB emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm8B_memmove_move_4through7 CMPQ BX, $0x10 JBE emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm8B_memmove_move_8through16 CMPQ BX, $0x20 JBE emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm8B_memmove_move_17through32 JMP emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm8B_memmove_move_33through64 -emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm8B_memmove_move_8: - MOVQ (CX), SI - MOVQ SI, (AX) +emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm8B_memmove_move_1or2: + MOVB (CX), SI + MOVB -1(CX)(BX*1), CL + MOVB SI, (AX) + MOVB CL, -1(AX)(BX*1) + JMP memmove_end_copy_emit_remainder_encodeSnappyBlockAsm8B + +emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm8B_memmove_move_3: + MOVW (CX), SI + MOVB 2(CX), CL + MOVW SI, (AX) + MOVB CL, 2(AX) + JMP memmove_end_copy_emit_remainder_encodeSnappyBlockAsm8B + +emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm8B_memmove_move_4through7: + MOVL (CX), SI + MOVL -4(CX)(BX*1), CX + MOVL SI, (AX) + MOVL CX, -4(AX)(BX*1) JMP memmove_end_copy_emit_remainder_encodeSnappyBlockAsm8B emit_lit_memmove_emit_remainder_encodeSnappyBlockAsm8B_memmove_move_8through16: @@ -13461,17 +13435,6 @@ matchlen_loopback_match_nolit_encodeSnappyBetterBlockAsm: #ifdef GOAMD64_v3 TZCNTQ R11, R11 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R11, R11 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R11, R11 @@ -13850,17 +13813,36 @@ memmove_emit_remainder_encodeSnappyBetterBlockAsm: MOVL SI, BX // genMemMoveShort + CMPQ BX, $0x03 + JB emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm_memmove_move_1or2 + JE emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm_memmove_move_3 CMPQ BX, $0x08 - JLE emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm_memmove_move_8 + JB emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm_memmove_move_4through7 CMPQ BX, $0x10 JBE emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm_memmove_move_8through16 CMPQ BX, $0x20 JBE emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm_memmove_move_17through32 JMP emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm_memmove_move_33through64 -emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm_memmove_move_8: - MOVQ (CX), SI - MOVQ SI, (AX) +emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm_memmove_move_1or2: + MOVB (CX), SI + MOVB -1(CX)(BX*1), CL + MOVB SI, (AX) + MOVB CL, -1(AX)(BX*1) + JMP memmove_end_copy_emit_remainder_encodeSnappyBetterBlockAsm + +emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm_memmove_move_3: + MOVW (CX), SI + MOVB 2(CX), CL + MOVW SI, (AX) + MOVB CL, 2(AX) + JMP memmove_end_copy_emit_remainder_encodeSnappyBetterBlockAsm + +emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm_memmove_move_4through7: + MOVL (CX), SI + MOVL -4(CX)(BX*1), CX + MOVL SI, (AX) + MOVL CX, -4(AX)(BX*1) JMP memmove_end_copy_emit_remainder_encodeSnappyBetterBlockAsm emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm_memmove_move_8through16: @@ -14068,17 +14050,6 @@ matchlen_loopback_match_nolit_encodeSnappyBetterBlockAsm64K: #ifdef GOAMD64_v3 TZCNTQ R11, R11 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R11, R11 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R11, R11 @@ -14386,17 +14357,36 @@ memmove_emit_remainder_encodeSnappyBetterBlockAsm64K: MOVL SI, BX // genMemMoveShort + CMPQ BX, $0x03 + JB emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm64K_memmove_move_1or2 + JE emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm64K_memmove_move_3 CMPQ BX, $0x08 - JLE emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm64K_memmove_move_8 + JB emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm64K_memmove_move_4through7 CMPQ BX, $0x10 JBE emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm64K_memmove_move_8through16 CMPQ BX, $0x20 JBE emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm64K_memmove_move_17through32 JMP emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm64K_memmove_move_33through64 -emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm64K_memmove_move_8: - MOVQ (CX), SI - MOVQ SI, (AX) +emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm64K_memmove_move_1or2: + MOVB (CX), SI + MOVB -1(CX)(BX*1), CL + MOVB SI, (AX) + MOVB CL, -1(AX)(BX*1) + JMP memmove_end_copy_emit_remainder_encodeSnappyBetterBlockAsm64K + +emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm64K_memmove_move_3: + MOVW (CX), SI + MOVB 2(CX), CL + MOVW SI, (AX) + MOVB CL, 2(AX) + JMP memmove_end_copy_emit_remainder_encodeSnappyBetterBlockAsm64K + +emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm64K_memmove_move_4through7: + MOVL (CX), SI + MOVL -4(CX)(BX*1), CX + MOVL SI, (AX) + MOVL CX, -4(AX)(BX*1) JMP memmove_end_copy_emit_remainder_encodeSnappyBetterBlockAsm64K emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm64K_memmove_move_8through16: @@ -14604,17 +14594,6 @@ matchlen_loopback_match_nolit_encodeSnappyBetterBlockAsm12B: #ifdef GOAMD64_v3 TZCNTQ R11, R11 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R11, R11 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R11, R11 @@ -14922,17 +14901,36 @@ memmove_emit_remainder_encodeSnappyBetterBlockAsm12B: MOVL SI, BX // genMemMoveShort + CMPQ BX, $0x03 + JB emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm12B_memmove_move_1or2 + JE emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm12B_memmove_move_3 CMPQ BX, $0x08 - JLE emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm12B_memmove_move_8 + JB emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm12B_memmove_move_4through7 CMPQ BX, $0x10 JBE emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm12B_memmove_move_8through16 CMPQ BX, $0x20 JBE emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm12B_memmove_move_17through32 JMP emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm12B_memmove_move_33through64 -emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm12B_memmove_move_8: - MOVQ (CX), SI - MOVQ SI, (AX) +emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm12B_memmove_move_1or2: + MOVB (CX), SI + MOVB -1(CX)(BX*1), CL + MOVB SI, (AX) + MOVB CL, -1(AX)(BX*1) + JMP memmove_end_copy_emit_remainder_encodeSnappyBetterBlockAsm12B + +emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm12B_memmove_move_3: + MOVW (CX), SI + MOVB 2(CX), CL + MOVW SI, (AX) + MOVB CL, 2(AX) + JMP memmove_end_copy_emit_remainder_encodeSnappyBetterBlockAsm12B + +emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm12B_memmove_move_4through7: + MOVL (CX), SI + MOVL -4(CX)(BX*1), CX + MOVL SI, (AX) + MOVL CX, -4(AX)(BX*1) JMP memmove_end_copy_emit_remainder_encodeSnappyBetterBlockAsm12B emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm12B_memmove_move_8through16: @@ -15140,17 +15138,6 @@ matchlen_loopback_match_nolit_encodeSnappyBetterBlockAsm10B: #ifdef GOAMD64_v3 TZCNTQ R11, R11 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R11, R11 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R11, R11 @@ -15458,17 +15445,36 @@ memmove_emit_remainder_encodeSnappyBetterBlockAsm10B: MOVL SI, BX // genMemMoveShort + CMPQ BX, $0x03 + JB emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm10B_memmove_move_1or2 + JE emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm10B_memmove_move_3 CMPQ BX, $0x08 - JLE emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm10B_memmove_move_8 + JB emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm10B_memmove_move_4through7 CMPQ BX, $0x10 JBE emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm10B_memmove_move_8through16 CMPQ BX, $0x20 JBE emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm10B_memmove_move_17through32 JMP emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm10B_memmove_move_33through64 -emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm10B_memmove_move_8: - MOVQ (CX), SI - MOVQ SI, (AX) +emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm10B_memmove_move_1or2: + MOVB (CX), SI + MOVB -1(CX)(BX*1), CL + MOVB SI, (AX) + MOVB CL, -1(AX)(BX*1) + JMP memmove_end_copy_emit_remainder_encodeSnappyBetterBlockAsm10B + +emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm10B_memmove_move_3: + MOVW (CX), SI + MOVB 2(CX), CL + MOVW SI, (AX) + MOVB CL, 2(AX) + JMP memmove_end_copy_emit_remainder_encodeSnappyBetterBlockAsm10B + +emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm10B_memmove_move_4through7: + MOVL (CX), SI + MOVL -4(CX)(BX*1), CX + MOVL SI, (AX) + MOVL CX, -4(AX)(BX*1) JMP memmove_end_copy_emit_remainder_encodeSnappyBetterBlockAsm10B emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm10B_memmove_move_8through16: @@ -15676,17 +15682,6 @@ matchlen_loopback_match_nolit_encodeSnappyBetterBlockAsm8B: #ifdef GOAMD64_v3 TZCNTQ R11, R11 -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ R11, R11 - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ R11, R11 @@ -15992,17 +15987,36 @@ memmove_emit_remainder_encodeSnappyBetterBlockAsm8B: MOVL SI, BX // genMemMoveShort + CMPQ BX, $0x03 + JB emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm8B_memmove_move_1or2 + JE emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm8B_memmove_move_3 CMPQ BX, $0x08 - JLE emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm8B_memmove_move_8 + JB emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm8B_memmove_move_4through7 CMPQ BX, $0x10 JBE emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm8B_memmove_move_8through16 CMPQ BX, $0x20 JBE emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm8B_memmove_move_17through32 JMP emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm8B_memmove_move_33through64 -emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm8B_memmove_move_8: - MOVQ (CX), SI - MOVQ SI, (AX) +emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm8B_memmove_move_1or2: + MOVB (CX), SI + MOVB -1(CX)(BX*1), CL + MOVB SI, (AX) + MOVB CL, -1(AX)(BX*1) + JMP memmove_end_copy_emit_remainder_encodeSnappyBetterBlockAsm8B + +emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm8B_memmove_move_3: + MOVW (CX), SI + MOVB 2(CX), CL + MOVW SI, (AX) + MOVB CL, 2(AX) + JMP memmove_end_copy_emit_remainder_encodeSnappyBetterBlockAsm8B + +emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm8B_memmove_move_4through7: + MOVL (CX), SI + MOVL -4(CX)(BX*1), CX + MOVL SI, (AX) + MOVL CX, -4(AX)(BX*1) JMP memmove_end_copy_emit_remainder_encodeSnappyBetterBlockAsm8B emit_lit_memmove_emit_remainder_encodeSnappyBetterBlockAsm8B_memmove_move_8through16: @@ -16644,17 +16658,6 @@ matchlen_loopback_standalone: #ifdef GOAMD64_v3 TZCNTQ BX, BX -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef GOAMD64_v4 - TZCNTQ BX, BX - -#define TZCNTQ_EMITTED 1 -#endif - -#ifdef TZCNTQ_EMITTED -#undef TZCNTQ_EMITTED #else BSFQ BX, BX diff --git a/vendor/github.com/klauspost/compress/s2/index.go b/vendor/github.com/klauspost/compress/s2/index.go index fd857682..7b24a006 100644 --- a/vendor/github.com/klauspost/compress/s2/index.go +++ b/vendor/github.com/klauspost/compress/s2/index.go @@ -10,6 +10,7 @@ import ( "encoding/json" "fmt" "io" + "sort" ) const ( @@ -100,6 +101,15 @@ func (i *Index) Find(offset int64) (compressedOff, uncompressedOff int64, err er if offset > i.TotalUncompressed { return 0, 0, io.ErrUnexpectedEOF } + if len(i.info) > 200 { + n := sort.Search(len(i.info), func(n int) bool { + return i.info[n].uncompressedOffset > offset + }) + if n == 0 { + n = 1 + } + return i.info[n-1].compressedOffset, i.info[n-1].uncompressedOffset, nil + } for _, info := range i.info { if info.uncompressedOffset > offset { break diff --git a/vendor/github.com/nats-io/jsm.go/natscontext/context.go b/vendor/github.com/nats-io/jsm.go/natscontext/context.go index a9a72fe0..42a3d113 100644 --- a/vendor/github.com/nats-io/jsm.go/natscontext/context.go +++ b/vendor/github.com/nats-io/jsm.go/natscontext/context.go @@ -87,6 +87,33 @@ func New(name string, load bool, opts ...Option) (*Context, error) { } } + c.configureNewContext(opts...) + + return c, nil +} + +// NewFromFile loads a new configuration context from the given filename. +// +// When opts is supplied those settings will override what was loaded or supply +// values for an empty context +func NewFromFile(filename string, opts ...Option) (*Context, error) { + c := &Context{ + Name: strings.TrimSuffix(filepath.Base(filename), filepath.Ext(filename)), + config: &settings{}, + path: filename, + } + + err := c.loadActiveContext() + if err != nil { + return nil, err + } + + c.configureNewContext(opts...) + + return c, nil +} + +func (c *Context) configureNewContext(opts ...Option) { // apply supplied overrides for _, opt := range opts { opt(c.config) @@ -95,8 +122,6 @@ func New(name string, load bool, opts ...Option) (*Context, error) { if c.config.NSCLookup == "" && c.config.URL == "" && c.config.nscUrl == "" { c.config.URL = nats.DefaultURL } - - return c, nil } // Connect connects to the NATS server configured by the named context, empty name connects to selected context @@ -291,28 +316,31 @@ func (c *Context) NATSOptions(opts ...nats.Option) ([]nats.Option, error) { } func (c *Context) loadActiveContext() error { - parent, err := parentDir() - if err != nil { - return err - } + if c.path == "" { + parent, err := parentDir() + if err != nil { + return err + } - // none given, lets try to find it via the fs - if c.Name == "" { - c.Name = SelectedContext() + // none given, lets try to find it via the fs if c.Name == "" { - return nil + c.Name = SelectedContext() + if c.Name == "" { + return nil + } } - } - if !validName(c.Name) { - return fmt.Errorf("invalid context name %s", c.Name) - } + if !validName(c.Name) { + return fmt.Errorf("invalid context name %s", c.Name) + } + + if !knownContext(parent, c.Name) { + return fmt.Errorf("unknown context %q", c.Name) + } - if !knownContext(parent, c.Name) { - return fmt.Errorf("unknown context %q", c.Name) + c.path = filepath.Join(parent, "nats", "context", c.Name+".json") } - c.path = filepath.Join(parent, "nats", "context", c.Name+".json") ctxContent, err := ioutil.ReadFile(c.path) if err != nil { return err diff --git a/vendor/github.com/nats-io/jwt/v2/header.go b/vendor/github.com/nats-io/jwt/v2/header.go index 198bf306..eadd4eaa 100644 --- a/vendor/github.com/nats-io/jwt/v2/header.go +++ b/vendor/github.com/nats-io/jwt/v2/header.go @@ -23,7 +23,7 @@ import ( const ( // Version is semantic version. - Version = "2.2.0" + Version = "2.3.0" // TokenTypeJwt is the JWT token type supported JWT tokens // encoded and decoded by this library diff --git a/vendor/github.com/nats-io/nats-server/v2/server/README.md b/vendor/github.com/nats-io/nats-server/v2/server/README.md new file mode 100644 index 00000000..3184eeda --- /dev/null +++ b/vendor/github.com/nats-io/nats-server/v2/server/README.md @@ -0,0 +1,17 @@ +# Tests + +Tests that run on Travis have been split into jobs that run in their own VM in parallel. This reduces the overall running time but also is allowing recycling of a job when we get a flapper as opposed to have to recycle the whole test suite. + +## JetStream Tests + +For JetStream tests, we need to observe a naming convention so that no tests are omitted when running on Travis. + +The script `runTestsOnTravis.sh` will run a given job based on the definition found in "`.travis.yml`". + +As for the naming convention: + +- All JetStream tests name should start with `TestJetStream` +- Cluster tests should go into `jetstream_cluster_test.go` and start with `TestJetStreamCluster` +- Super-cluster tests should go into `jetstream_super_cluster_test.go` and start with `TestJetStreamSuperCluster` + +Not following this convention means that some tests may not be executed on Travis. diff --git a/vendor/github.com/nats-io/nats-server/v2/server/accounts.go b/vendor/github.com/nats-io/nats-server/v2/server/accounts.go index 36de78d2..d1333955 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/accounts.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/accounts.go @@ -18,6 +18,7 @@ import ( "encoding/hex" "errors" "fmt" + "hash/fnv" "hash/maphash" "io/ioutil" "math" @@ -25,6 +26,7 @@ import ( "net/http" "net/textproto" "reflect" + "regexp" "sort" "strconv" "strings" @@ -74,7 +76,7 @@ type Account struct { imports importMap exports exportMap js *jsAccount - jsLimits *JetStreamAccountLimits + jsLimits map[string]JetStreamAccountLimits limits expired bool incomplete bool @@ -161,6 +163,10 @@ const ( Chunked ) +var commaSeparatorRegEx = regexp.MustCompile(`,\s*`) +var partitionMappingFunctionRegEx = regexp.MustCompile(`{{\s*partition\s*\((.*)\)\s*}}`) +var wildcardMappingFunctionRegEx = regexp.MustCompile(`{{\s*wildcard\s*\((.*)\)\s*}}`) + // String helper. func (rt ServiceRespType) String() string { switch rt { @@ -1700,8 +1706,7 @@ func (a *Account) checkForReverseEntry(reply string, si *serviceImport, checkInt return } - sres := a.imports.rrMap[reply] - if sres == nil { + if sres := a.imports.rrMap[reply]; sres == nil { a.mu.RUnlock() return } @@ -1722,9 +1727,11 @@ func (a *Account) checkForReverseEntry(reply string, si *serviceImport, checkInt // Delete the appropriate entries here based on optional si. a.mu.Lock() + // We need a new lookup here because we have released the lock. + sres := a.imports.rrMap[reply] if si == nil { delete(a.imports.rrMap, reply) - } else { + } else if sres != nil { // Find the one we are looking for.. for i, sre := range sres { if sre.msub == si.from { @@ -1743,6 +1750,8 @@ func (a *Account) checkForReverseEntry(reply string, si *serviceImport, checkInt // If we are here we no longer have interest and we have // response entries that we should clean up. if si == nil { + // sres is now known to have been removed from a.imports.rrMap, so we + // can safely (data race wise) iterate through. for _, sre := range sres { acc := sre.acc var trackingCleanup bool @@ -3030,9 +3039,6 @@ func (s *Server) updateAccountClaimsWithRefresh(a *Account, ac *jwt.AccountClaim } jsEnabled := s.JetStreamEnabled() - if jsEnabled && a == s.SystemAccount() { - s.checkJetStreamExports() - } streamTokenExpirationChanged := false serviceTokenExpirationChanged := false @@ -3268,15 +3274,41 @@ func (s *Server) updateAccountClaimsWithRefresh(a *Account, ac *jwt.AccountClaim a.srv = s } - // Setup js limits regardless of whether this server has jsEnabled. - if ac.Limits.JetStreamLimits.DiskStorage != 0 || ac.Limits.JetStreamLimits.MemoryStorage != 0 { - // JetStreamAccountLimits and jwt.JetStreamLimits use same value for unlimited - a.jsLimits = &JetStreamAccountLimits{ - MaxMemory: ac.Limits.JetStreamLimits.MemoryStorage, - MaxStore: ac.Limits.JetStreamLimits.DiskStorage, - MaxStreams: int(ac.Limits.JetStreamLimits.Streams), - MaxConsumers: int(ac.Limits.JetStreamLimits.Consumer), - MaxBytesRequired: ac.Limits.JetStreamLimits.MaxBytesRequired, + if ac.Limits.IsJSEnabled() { + toUnlimited := func(value int64) int64 { + if value > 0 { + return value + } + return -1 + } + if ac.Limits.JetStreamLimits.DiskStorage != 0 || ac.Limits.JetStreamLimits.MemoryStorage != 0 { + // JetStreamAccountLimits and jwt.JetStreamLimits use same value for unlimited + a.jsLimits = map[string]JetStreamAccountLimits{ + _EMPTY_: { + MaxMemory: ac.Limits.JetStreamLimits.MemoryStorage, + MaxStore: ac.Limits.JetStreamLimits.DiskStorage, + MaxStreams: int(ac.Limits.JetStreamLimits.Streams), + MaxConsumers: int(ac.Limits.JetStreamLimits.Consumer), + MemoryMaxStreamBytes: toUnlimited(ac.Limits.JetStreamLimits.MemoryMaxStreamBytes), + StoreMaxStreamBytes: toUnlimited(ac.Limits.JetStreamLimits.DiskMaxStreamBytes), + MaxBytesRequired: ac.Limits.JetStreamLimits.MaxBytesRequired, + MaxAckPending: int(toUnlimited(ac.Limits.JetStreamLimits.MaxAckPending)), + }, + } + } else { + a.jsLimits = map[string]JetStreamAccountLimits{} + for t, l := range ac.Limits.JetStreamTieredLimits { + a.jsLimits[t] = JetStreamAccountLimits{ + MaxMemory: l.MemoryStorage, + MaxStore: l.DiskStorage, + MaxStreams: int(l.Streams), + MaxConsumers: int(l.Consumer), + MemoryMaxStreamBytes: toUnlimited(l.MemoryMaxStreamBytes), + StoreMaxStreamBytes: toUnlimited(l.DiskMaxStreamBytes), + MaxBytesRequired: l.MaxBytesRequired, + MaxAckPending: int(toUnlimited(l.MaxAckPending)), + } + } } } else if a.jsLimits != nil { // covers failed update followed by disable @@ -4132,18 +4164,65 @@ type transform struct { src, dest string dtoks []string stoks []string - dtpi []int8 + dtpi [][]int // destination token position indexes + dtpinp []int32 // destination token position index number of partitions +} + +func getMappingFunctionArgs(functionRegEx *regexp.Regexp, token string) []string { + commandStrings := functionRegEx.FindStringSubmatch(token) + if len(commandStrings) > 1 { + return commaSeparatorRegEx.Split(commandStrings[1], -1) + } + return nil } -// Helper to pull raw place holder index. Returns -1 if not a place holder. -func placeHolderIndex(token string) int { - if len(token) > 1 && token[0] == '$' { - var tp int - if n, err := fmt.Sscanf(token, "$%d", &tp); err == nil && n == 1 { - return tp +// Helper to pull raw place holder indexes and number of partitions. Returns -1 if not a place holder. +func placeHolderIndex(token string) ([]int, int32, error) { + if len(token) > 1 { + // old $1, $2, etc... mapping format still supported to maintain backwards compatibility + if token[0] == '$' { // simple non-partition mapping + tp, err := strconv.Atoi(token[1:]) + if err != nil { + return []int{-1}, -1, nil + } + return []int{tp}, -1, nil + } + + // New 'moustache' style mapping + // wildcard(wildcard token index) (equivalent to $) + args := getMappingFunctionArgs(wildcardMappingFunctionRegEx, token) + if args != nil { + if len(args) == 1 { + tp, err := strconv.Atoi(strings.Trim(args[0], " ")) + if err != nil { + return []int{}, -1, err + } + return []int{tp}, -1, nil + } + } + + // partition(number of partitions, token1, token2, ...) + args = getMappingFunctionArgs(partitionMappingFunctionRegEx, token) + if args != nil { + if len(args) >= 2 { + tphnp, err := strconv.Atoi(strings.Trim(args[0], " ")) + if err != nil { + return []int{}, -1, err + } + var numPositions = len(args[1:]) + tps := make([]int, numPositions) + for ti, t := range args[1:] { + i, err := strconv.Atoi(strings.Trim(t, " ")) + if err != nil { + return []int{}, -1, err + } + tps[ti] = i + } + return tps, int32(tphnp), nil + } } } - return -1 + return []int{-1}, -1, nil } // newTransform will create a new transform checking the src and dest subjects for accuracy. @@ -4157,7 +4236,8 @@ func newTransform(src, dest string) (*transform, error) { return nil, ErrBadSubject } - var dtpi []int8 + var dtpi [][]int + var dtpinb []int32 // If the src has partial wildcards then the dest needs to have the token place markers. if npwcs > 0 || hasFwc { @@ -4171,25 +4251,33 @@ func newTransform(src, dest string) (*transform, error) { nphs := 0 for _, token := range dtokens { - tp := placeHolderIndex(token) - if tp >= 0 { - if tp > npwcs { - return nil, ErrBadSubject - } + tp, nb, err := placeHolderIndex(token) + if err != nil { + return nil, ErrBadSubjectMappingDestination + } + if tp[0] >= 0 { nphs++ // Now build up our runtime mapping from dest to source tokens. - dtpi = append(dtpi, int8(sti[tp])) + var stis []int + for _, position := range tp { + if position > npwcs { + return nil, ErrBadSubjectMappingDestination + } + stis = append(stis, sti[position]) + } + dtpi = append(dtpi, stis) + dtpinb = append(dtpinb, nb) } else { - dtpi = append(dtpi, -1) + dtpi = append(dtpi, []int{-1}) + dtpinb = append(dtpinb, -1) } } - - if nphs != npwcs { - return nil, ErrBadSubject + if nphs < npwcs { + return nil, ErrBadSubjectMappingDestination } } - return &transform{src: src, dest: dest, dtoks: dtokens, stoks: stokens, dtpi: dtpi}, nil + return &transform{src: src, dest: dest, dtoks: dtokens, stoks: stokens, dtpi: dtpi, dtpinp: dtpinb}, nil } // match will take a literal published subject that is associated with a client and will match and transform @@ -4233,6 +4321,13 @@ func (tr *transform) transformSubject(subject string) (string, error) { return tr.transform(tts) } +func (tr *transform) getHashPartition(key []byte, numBuckets int) string { + h := fnv.New32a() + h.Write(key) + + return strconv.Itoa(int(h.Sum32() % uint32(numBuckets))) +} + // Do a transform on the subject to the dest subject. func (tr *transform) transform(tokens []string) (string, error) { if len(tr.dtpi) == 0 { @@ -4248,7 +4343,7 @@ func (tr *transform) transform(tokens []string) (string, error) { li := len(tr.dtpi) - 1 for i, index := range tr.dtpi { // <0 means use destination token. - if index < 0 { + if index[0] < 0 { token = tr.dtoks[i] // Break if fwc if len(token) == 1 && token[0] == fwc { @@ -4256,7 +4351,18 @@ func (tr *transform) transform(tokens []string) (string, error) { } } else { // >= 0 means use source map index to figure out which source token to pull. - token = tokens[index] + if tr.dtpinp[i] > 0 { // there is a valid (i.e. not -1) value for number of partitions, this is a partition transform token + var ( + _buffer [64]byte + keyForHashing = _buffer[:0] + ) + for _, sourceToken := range tr.dtpi[i] { + keyForHashing = append(keyForHashing, []byte(tokens[sourceToken])...) + } + token = tr.getHashPartition(keyForHashing, int(tr.dtpinp[i])) + } else { // back to normal substitution + token = tokens[tr.dtpi[i][0]] + } } b.WriteString(token) if i < li { diff --git a/vendor/github.com/nats-io/nats-server/v2/server/client.go b/vendor/github.com/nats-io/nats-server/v2/server/client.go index 5d372cd4..d7a8da09 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/client.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/client.go @@ -201,6 +201,8 @@ const ( ClusterNameConflict DuplicateRemoteLeafnodeConnection DuplicateClientID + DuplicateServerName + MinimumVersionRequired ) // Some flags passed to processMsgResults @@ -1225,6 +1227,15 @@ func (c *client) readLoop(pre []byte) { // to process messages, etc. for i := 0; i < len(bufs); i++ { if err := c.parse(bufs[i]); err != nil { + if err == ErrMinimumVersionRequired { + // Special case here, currently only for leaf node connections. + // When process the CONNECT protocol, if the minimum version + // required was not met, an error was printed and sent back to + // the remote, and connection was closed after a certain delay + // (to avoid "rapid" reconnection from the remote). + // We don't need to do any of the things below, simply return. + return + } if dur := time.Since(start); dur >= readLoopReportThreshold { c.Warnf("Readloop processing time: %v", dur) } @@ -2430,7 +2441,7 @@ func (c *client) processSubEx(subject, queue, bsid []byte, cb msgHandler, noForw // allow = ["foo", "foo v1"] -> can subscribe to 'foo' but can only queue subscribe to 'foo v1' // if sub.queue != nil { - if !c.canQueueSubscribe(string(sub.subject), string(sub.queue)) { + if !c.canSubscribe(string(sub.subject), string(sub.queue)) { c.mu.Unlock() c.subPermissionViolation(sub) return nil, ErrSubscribePermissionViolation @@ -2643,7 +2654,7 @@ func (c *client) addShadowSub(sub *subscription, ime *ime) (*subscription, error nsub := *sub // copy nsub.im = im - if !im.usePub && ime.dyn { + if !im.usePub && ime.dyn && im.tr != nil { if im.rtr == nil { im.rtr = im.tr.reverse() } @@ -2656,7 +2667,7 @@ func (c *client) addShadowSub(sub *subscription, ime *ime) (*subscription, error return nil, err } nsub.subject = []byte(subj) - } else if !im.usePub || !ime.dyn { + } else if !im.usePub || (im.usePub && ime.overlapSubj != _EMPTY_) || !ime.dyn { if ime.overlapSubj != _EMPTY_ { nsub.subject = []byte(ime.overlapSubj) } else { @@ -2680,17 +2691,27 @@ func (c *client) addShadowSub(sub *subscription, ime *ime) (*subscription, error // canSubscribe determines if the client is authorized to subscribe to the // given subject. Assumes caller is holding lock. -func (c *client) canSubscribe(subject string) bool { +func (c *client) canSubscribe(subject string, optQueue ...string) bool { if c.perms == nil { return true } allowed := true + // Optional queue group. + var queue string + if len(optQueue) > 0 { + queue = optQueue[0] + } + // Check allow list. If no allow list that means all are allowed. Deny can overrule. if c.perms.sub.allow != nil { r := c.perms.sub.allow.Match(subject) - allowed = len(r.psubs) != 0 + allowed = len(r.psubs) > 0 + if queue != _EMPTY_ && len(r.qsubs) > 0 { + // If the queue appears in the allow list, then DO allow. + allowed = queueMatches(queue, r.qsubs) + } // Leafnodes operate slightly differently in that they allow broader scoped subjects. // They will prune based on publish perms before sending to a leafnode client. if !allowed && c.kind == LEAF && subjectHasWildcard(subject) { @@ -2703,6 +2724,11 @@ func (c *client) canSubscribe(subject string) bool { r := c.perms.sub.deny.Match(subject) allowed = len(r.psubs) == 0 + if queue != _EMPTY_ && len(r.qsubs) > 0 { + // If the queue appears in the deny list, then DO NOT allow. + allowed = !queueMatches(queue, r.qsubs) + } + // We use the actual subscription to signal us to spin up the deny mperms // and cache. We check if the subject is a wildcard that contains any of // the deny clauses. @@ -2738,42 +2764,6 @@ func queueMatches(queue string, qsubs [][]*subscription) bool { return false } -func (c *client) canQueueSubscribe(subject, queue string) bool { - if c.perms == nil { - return true - } - - allowed := true - - if c.perms.sub.allow != nil { - r := c.perms.sub.allow.Match(subject) - - // If perms DO NOT have queue name, then psubs will be greater than - // zero. If perms DO have queue name, then qsubs will be greater than - // zero. - allowed = len(r.psubs) > 0 - if len(r.qsubs) > 0 { - // If the queue appears in the allow list, then DO allow. - allowed = queueMatches(queue, r.qsubs) - } - } - - if allowed && c.perms.sub.deny != nil { - r := c.perms.sub.deny.Match(subject) - - // If perms DO NOT have queue name, then psubs will be greater than - // zero. If perms DO have queue name, then qsubs will be greater than - // zero. - allowed = len(r.psubs) == 0 - if len(r.qsubs) > 0 { - // If the queue appears in the deny list, then DO NOT allow. - allowed = !queueMatches(queue, r.qsubs) - } - } - - return allowed -} - // Low level unsubscribe for a given client. func (c *client) unsubscribe(acc *Account, sub *subscription, force, remove bool) { c.mu.Lock() @@ -2921,21 +2911,26 @@ func (c *client) checkDenySub(subject string) bool { // Create a message header for routes or leafnodes. Header and origin cluster aware. func (c *client) msgHeaderForRouteOrLeaf(subj, reply []byte, rt *routeTarget, acc *Account) []byte { hasHeader := c.pa.hdr > 0 - canReceiveHeader := rt.sub.client.headers + subclient := rt.sub.client + canReceiveHeader := subclient.headers mh := c.msgb[:msgHeadProtoLen] - kind := rt.sub.client.kind + kind := subclient.kind var lnoc bool if kind == ROUTER { // If we are coming from a leaf with an origin cluster we need to handle differently // if we can. We will send a route based LMSG which has origin cluster and headers // by default. - if c.kind == LEAF && c.remoteCluster() != _EMPTY_ && rt.sub.client.route.lnoc { + if c.kind == LEAF && c.remoteCluster() != _EMPTY_ { + subclient.mu.Lock() + lnoc = subclient.route.lnoc + subclient.mu.Unlock() + } + if lnoc { mh[0] = 'L' mh = append(mh, c.remoteCluster()...) mh = append(mh, ' ') - lnoc = true } else { // Router (and Gateway) nodes are RMSG. Set here since leafnodes may rewrite. mh[0] = 'R' @@ -3477,8 +3472,11 @@ func isReservedReply(reply []byte) bool { if isServiceReply(reply) { return true } + rLen := len(reply) // Faster to check with string([:]) than byte-by-byte - if len(reply) > gwReplyPrefixLen && string(reply[:gwReplyPrefixLen]) == gwReplyPrefix { + if rLen > jsAckPreLen && string(reply[:jsAckPreLen]) == jsAckPre { + return true + } else if rLen > gwReplyPrefixLen && string(reply[:gwReplyPrefixLen]) == gwReplyPrefix { return true } return false @@ -4087,11 +4085,13 @@ func (c *client) processMsgResults(acc *Account, r *SublistResult, msg, deliver, // Check for JetStream encoded reply subjects. // For now these will only be on $JS.ACK prefixed reply subjects. + var remapped bool if len(creply) > 0 && c.kind != CLIENT && c.kind != SYSTEM && c.kind != JETSTREAM && c.kind != ACCOUNT && bytes.HasPrefix(creply, []byte(jsAckPre)) { // We need to rewrite the subject and the reply. if li := bytes.LastIndex(creply, []byte("@")); li != -1 && li < len(creply)-1 { + remapped = true subj, creply = creply[li+1:], creply[:li] } } @@ -4138,13 +4138,18 @@ func (c *client) processMsgResults(acc *Account, r *SublistResult, msg, deliver, continue } if sub.im.tr != nil { - to, _ := sub.im.tr.transformSubject(string(dsubj)) + to, _ := sub.im.tr.transformSubject(string(subject)) dsubj = append(_dsubj[:0], to...) } else if sub.im.usePub { dsubj = append(_dsubj[:0], subj...) } else { dsubj = append(_dsubj[:0], sub.im.to...) } + + // Make sure deliver is set if inbound from a route. + if remapped && (c.kind == GATEWAY || c.kind == ROUTER || c.kind == LEAF) { + deliver = subj + } // If we are mapping for a deliver subject we will reverse roles. // The original subj we set from above is correct for the msg header, // but we need to transform the deliver subject to properly route. @@ -4274,13 +4279,23 @@ func (c *client) processMsgResults(acc *Account, r *SublistResult, msg, deliver, continue } if sub.im.tr != nil { - to, _ := sub.im.tr.transformSubject(string(subj)) + to, _ := sub.im.tr.transformSubject(string(subject)) dsubj = append(_dsubj[:0], to...) } else if sub.im.usePub { dsubj = append(_dsubj[:0], subj...) } else { dsubj = append(_dsubj[:0], sub.im.to...) } + // Make sure deliver is set if inbound from a route. + if remapped && (c.kind == GATEWAY || c.kind == ROUTER || c.kind == LEAF) { + deliver = subj + } + // If we are mapping for a deliver subject we will reverse roles. + // The original subj we set from above is correct for the msg header, + // but we need to transform the deliver subject to properly route. + if len(deliver) > 0 { + dsubj, subj = subj, dsubj + } } mh := c.msgHeader(dsubj, creply, sub) @@ -4648,7 +4663,7 @@ func (c *client) processSubsOnConfigReload(awcsti map[string]struct{}) { // Just checking to rebuild mperms under the lock, will collect removed though here. // Only collect under subs array of canSubscribe and checkAcc true. canSub := c.canSubscribe(string(sub.subject)) - canQSub := sub.queue != nil && c.canQueueSubscribe(string(sub.subject), string(sub.queue)) + canQSub := sub.queue != nil && c.canSubscribe(string(sub.subject), string(sub.queue)) if !canSub && !canQSub { removed = append(removed, sub) @@ -5319,6 +5334,15 @@ func (c *client) Warnf(format string, v ...interface{}) { c.srv.Warnf(format, v...) } +func (c *client) RateLimitWarnf(format string, v ...interface{}) { + // Do the check before adding the client info to the format... + statement := fmt.Sprintf(format, v...) + if _, loaded := c.srv.rateLimitLogging.LoadOrStore(statement, time.Now()); loaded { + return + } + c.Warnf("%s", statement) +} + // Set the very first PING to a lower interval to capture the initial RTT. // After that the PING interval will be set to the user defined value. // Client lock should be held. diff --git a/vendor/github.com/nats-io/nats-server/v2/server/const.go b/vendor/github.com/nats-io/nats-server/v2/server/const.go index 396b900b..12902b1b 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/const.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/const.go @@ -41,7 +41,7 @@ var ( const ( // VERSION is the current version for the server. - VERSION = "2.7.5-beta" + VERSION = "2.8.4" // PROTO is the currently supported protocol. // 0 was the original diff --git a/vendor/github.com/nats-io/nats-server/v2/server/consumer.go b/vendor/github.com/nats-io/nats-server/v2/server/consumer.go index 73bd2e42..9fce5e65 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/consumer.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/consumer.go @@ -25,6 +25,7 @@ import ( "strconv" "strings" "sync" + "sync/atomic" "time" "github.com/nats-io/nuid" @@ -67,8 +68,9 @@ type ConsumerConfig struct { HeadersOnly bool `json:"headers_only,omitempty"` // Pull based options. - MaxRequestBatch int `json:"max_batch,omitempty"` - MaxRequestExpires time.Duration `json:"max_expires,omitempty"` + MaxRequestBatch int `json:"max_batch,omitempty"` + MaxRequestExpires time.Duration `json:"max_expires,omitempty"` + MaxRequestMaxBytes int `json:"max_bytes,omitempty"` // Push based consumers. DeliverSubject string `json:"deliver_subject,omitempty"` @@ -77,6 +79,11 @@ type ConsumerConfig struct { // Ephemeral inactivity threshold. InactiveThreshold time.Duration `json:"inactive_threshold,omitempty"` + // Generally inherited by parent stream and other markers, now can be configured directly. + Replicas int `json:"num_replicas"` + // Force memory storage. + MemoryStorage bool `json:"mem_storage,omitempty"` + // Don't add to general clients. Direct bool `json:"direct,omitempty"` } @@ -198,6 +205,10 @@ var ( // Consumer is a jetstream consumer. type consumer struct { + // Atomic used to notify that we want to process an ack. + // This will be checked in checkPending to abort processing + // and let ack be processed in priority. + awl int64 mu sync.RWMutex js *jetStream mset *stream @@ -212,8 +223,8 @@ type consumer struct { dseq uint64 adflr uint64 asflr uint64 - sgap uint64 - lsgap uint64 + npc uint64 + npcm uint64 dsubj string qgroup string lss *lastSeqSkipList @@ -252,6 +263,7 @@ type consumer struct { inch chan bool sfreq int32 ackEventT string + nakEventT string deliveryExcEventT string created time.Time ldt time.Time @@ -259,17 +271,22 @@ type consumer struct { closed bool // Clustered. - ca *consumerAssignment - node RaftNode - infoSub *subscription - lqsent time.Time - prm map[string]struct{} - prOk bool + ca *consumerAssignment + node RaftNode + infoSub *subscription + lqsent time.Time + prm map[string]struct{} + prOk bool + uch chan struct{} + retention RetentionPolicy // R>1 proposals pch chan struct{} phead *proposal ptail *proposal + + // Ack queue + ackMsgs *ipQueue } type proposal struct { @@ -285,13 +302,13 @@ const ( JsDeleteWaitTimeDefault = 5 * time.Second // JsFlowControlMaxPending specifies default pending bytes during flow control that can be // outstanding. - JsFlowControlMaxPending = 1 * 1024 * 1024 + JsFlowControlMaxPending = 32 * 1024 * 1024 // JsDefaultMaxAckPending is set for consumers with explicit ack that do not set the max ack pending. - JsDefaultMaxAckPending = 20_000 + JsDefaultMaxAckPending = 1000 ) // Helper function to set consumer config defaults from above. -func setConsumerConfigDefaults(config *ConsumerConfig) { +func setConsumerConfigDefaults(config *ConsumerConfig, lim *JSLimitOpts, accLim *JetStreamAccountLimits) { // Set to default if not specified. if config.DeliverSubject == _EMPTY_ && config.MaxWaiting == 0 { config.MaxWaiting = JSWaitQueueDefaultMax @@ -310,106 +327,118 @@ func setConsumerConfigDefaults(config *ConsumerConfig) { } // Set proper default for max ack pending if we are ack explicit and none has been set. if (config.AckPolicy == AckExplicit || config.AckPolicy == AckAll) && config.MaxAckPending == 0 { - config.MaxAckPending = JsDefaultMaxAckPending + accPending := JsDefaultMaxAckPending + if lim.MaxAckPending > 0 && lim.MaxAckPending < accPending { + accPending = lim.MaxAckPending + } + if accLim.MaxAckPending > 0 && accLim.MaxAckPending < accPending { + accPending = accLim.MaxAckPending + } + config.MaxAckPending = accPending + } + // if applicable set max request batch size + if config.DeliverSubject == _EMPTY_ && config.MaxRequestBatch == 0 && lim.MaxRequestBatch > 0 { + config.MaxRequestBatch = lim.MaxRequestBatch } } func (mset *stream) addConsumer(config *ConsumerConfig) (*consumer, error) { - return mset.addConsumerWithAssignment(config, _EMPTY_, nil) + return mset.addConsumerWithAssignment(config, _EMPTY_, nil, false) } -func (mset *stream) addConsumerWithAssignment(config *ConsumerConfig, oname string, ca *consumerAssignment) (*consumer, error) { - mset.mu.RLock() - s, jsa := mset.srv, mset.jsa - mset.mu.RUnlock() +// Check the consumer config. If we are recovering don't check filter subjects. +func checkConsumerCfg( + config *ConsumerConfig, + srvLim *JSLimitOpts, + cfg *StreamConfig, + acc *Account, + accLim *JetStreamAccountLimits, + isRecovering bool, +) *ApiError { - // If we do not have the consumer currently assigned to us in cluster mode we will proceed but warn. - // This can happen on startup with restored state where on meta replay we still do not have - // the assignment. Running in single server mode this always returns true. - if oname != _EMPTY_ && !jsa.consumerAssigned(mset.name(), oname) { - s.Debugf("Consumer %q > %q does not seem to be assigned to this server", mset.name(), oname) - } - - if config == nil { - return nil, NewJSConsumerConfigRequiredError() + // Check if replicas is defined but exceeds parent stream. + if config.Replicas > 0 && config.Replicas > cfg.Replicas { + return NewJSConsumerReplicasExceedsStreamError() } - // Make sure we have sane defaults. - setConsumerConfigDefaults(config) - // Check if we have a BackOff defined that MaxDeliver is within range etc. if lbo := len(config.BackOff); lbo > 0 && config.MaxDeliver <= lbo { - return nil, NewJSConsumerMaxDeliverBackoffError() + return NewJSConsumerMaxDeliverBackoffError() } if len(config.Description) > JSMaxDescriptionLen { - return nil, NewJSConsumerDescriptionTooLongError(JSMaxDescriptionLen) + return NewJSConsumerDescriptionTooLongError(JSMaxDescriptionLen) } - var err error // For now expect a literal subject if its not empty. Empty means work queue mode (pull mode). if config.DeliverSubject != _EMPTY_ { if !subjectIsLiteral(config.DeliverSubject) { - return nil, NewJSConsumerDeliverToWildcardsError() + return NewJSConsumerDeliverToWildcardsError() } if !IsValidSubject(config.DeliverSubject) { - return nil, NewJSConsumerInvalidDeliverSubjectError() + return NewJSConsumerInvalidDeliverSubjectError() } - if mset.deliveryFormsCycle(config.DeliverSubject) { - return nil, NewJSConsumerDeliverCycleError() + if deliveryFormsCycle(cfg, config.DeliverSubject) { + return NewJSConsumerDeliverCycleError() } if config.MaxWaiting != 0 { - return nil, NewJSConsumerPushMaxWaitingError() + return NewJSConsumerPushMaxWaitingError() } if config.MaxAckPending > 0 && config.AckPolicy == AckNone { - return nil, NewJSConsumerMaxPendingAckPolicyRequiredError() + return NewJSConsumerMaxPendingAckPolicyRequiredError() } if config.Heartbeat > 0 && config.Heartbeat < 100*time.Millisecond { - return nil, NewJSConsumerSmallHeartbeatError() + return NewJSConsumerSmallHeartbeatError() } } else { // Pull mode / work queue mode require explicit ack. if config.AckPolicy == AckNone { - return nil, NewJSConsumerPullRequiresAckError() + return NewJSConsumerPullRequiresAckError() } if config.RateLimit > 0 { - return nil, NewJSConsumerPullWithRateLimitError() + return NewJSConsumerPullWithRateLimitError() } if config.MaxWaiting < 0 { - return nil, NewJSConsumerMaxWaitingNegativeError() + return NewJSConsumerMaxWaitingNegativeError() } if config.Heartbeat > 0 { - return nil, NewJSConsumerHBRequiresPushError() + return NewJSConsumerHBRequiresPushError() } if config.FlowControl { - return nil, NewJSConsumerFCRequiresPushError() + return NewJSConsumerFCRequiresPushError() } if config.MaxRequestBatch < 0 { - return nil, NewJSConsumerMaxRequestBatchNegativeError() + return NewJSConsumerMaxRequestBatchNegativeError() } if config.MaxRequestExpires != 0 && config.MaxRequestExpires < time.Millisecond { - return nil, NewJSConsumerMaxRequestExpiresToSmallError() + return NewJSConsumerMaxRequestExpiresToSmallError() + } + if srvLim.MaxRequestBatch > 0 && config.MaxRequestBatch > srvLim.MaxRequestBatch { + return NewJSConsumerMaxRequestBatchExceededError(srvLim.MaxRequestBatch) } } + if srvLim.MaxAckPending > 0 && config.MaxAckPending > srvLim.MaxAckPending { + return NewJSConsumerMaxPendingAckExcessError(srvLim.MaxAckPending) + } + if accLim.MaxAckPending > 0 && config.MaxAckPending > accLim.MaxAckPending { + return NewJSConsumerMaxPendingAckExcessError(accLim.MaxAckPending) + } // Direct need to be non-mapped ephemerals. if config.Direct { if config.DeliverSubject == _EMPTY_ { - return nil, NewJSConsumerDirectRequiresPushError() + return NewJSConsumerDirectRequiresPushError() } if isDurableConsumer(config) { - return nil, NewJSConsumerDirectRequiresEphemeralError() - } - if ca != nil { - return nil, NewJSConsumerOnMappedError() + return NewJSConsumerDirectRequiresEphemeralError() } } // As best we can make sure the filtered subject is valid. - if config.FilterSubject != _EMPTY_ { - subjects, hasExt := mset.allSubjects() + if config.FilterSubject != _EMPTY_ && !isRecovering { + subjects, hasExt := allSubjects(cfg, acc) if !validFilteredSubject(config.FilterSubject, subjects) && !hasExt { - return nil, NewJSConsumerFilterNotSubsetError() + return NewJSConsumerFilterNotSubsetError() } } @@ -425,67 +454,115 @@ func (mset *stream) addConsumerWithAssignment(config *ConsumerConfig, oname stri switch config.DeliverPolicy { case DeliverAll: if config.OptStartSeq > 0 { - return nil, NewJSConsumerInvalidPolicyError(badStart("all", "sequence")) + return NewJSConsumerInvalidPolicyError(badStart("all", "sequence")) } if config.OptStartTime != nil { - return nil, NewJSConsumerInvalidPolicyError(badStart("all", "time")) + return NewJSConsumerInvalidPolicyError(badStart("all", "time")) } case DeliverLast: if config.OptStartSeq > 0 { - return nil, NewJSConsumerInvalidPolicyError(badStart("last", "sequence")) + return NewJSConsumerInvalidPolicyError(badStart("last", "sequence")) } if config.OptStartTime != nil { - return nil, NewJSConsumerInvalidPolicyError(badStart("last", "time")) + return NewJSConsumerInvalidPolicyError(badStart("last", "time")) } case DeliverLastPerSubject: if config.OptStartSeq > 0 { - return nil, NewJSConsumerInvalidPolicyError(badStart("last per subject", "sequence")) + return NewJSConsumerInvalidPolicyError(badStart("last per subject", "sequence")) } if config.OptStartTime != nil { - return nil, NewJSConsumerInvalidPolicyError(badStart("last per subject", "time")) + return NewJSConsumerInvalidPolicyError(badStart("last per subject", "time")) } if config.FilterSubject == _EMPTY_ { - return nil, NewJSConsumerInvalidPolicyError(notSet("last per subject", "filter subject")) + return NewJSConsumerInvalidPolicyError(notSet("last per subject", "filter subject")) } case DeliverNew: if config.OptStartSeq > 0 { - return nil, NewJSConsumerInvalidPolicyError(badStart("new", "sequence")) + return NewJSConsumerInvalidPolicyError(badStart("new", "sequence")) } if config.OptStartTime != nil { - return nil, NewJSConsumerInvalidPolicyError(badStart("new", "time")) + return NewJSConsumerInvalidPolicyError(badStart("new", "time")) } case DeliverByStartSequence: if config.OptStartSeq == 0 { - return nil, NewJSConsumerInvalidPolicyError(notSet("by start sequence", "start sequence")) + return NewJSConsumerInvalidPolicyError(notSet("by start sequence", "start sequence")) } if config.OptStartTime != nil { - return nil, NewJSConsumerInvalidPolicyError(badStart("by start sequence", "time")) + return NewJSConsumerInvalidPolicyError(badStart("by start sequence", "time")) } case DeliverByStartTime: if config.OptStartTime == nil { - return nil, NewJSConsumerInvalidPolicyError(notSet("by start time", "start time")) + return NewJSConsumerInvalidPolicyError(notSet("by start time", "start time")) } if config.OptStartSeq != 0 { - return nil, NewJSConsumerInvalidPolicyError(badStart("by start time", "start sequence")) + return NewJSConsumerInvalidPolicyError(badStart("by start time", "start sequence")) } } - sampleFreq := 0 if config.SampleFrequency != _EMPTY_ { s := strings.TrimSuffix(config.SampleFrequency, "%") - sampleFreq, err = strconv.Atoi(s) - if err != nil { - return nil, NewJSConsumerInvalidSamplingError(err) + if sampleFreq, err := strconv.Atoi(s); err != nil || sampleFreq < 0 { + return NewJSConsumerInvalidSamplingError(err) } } + // We reject if flow control is set without heartbeats. + if config.FlowControl && config.Heartbeat == 0 { + return NewJSConsumerWithFlowControlNeedsHeartbeatsError() + } + + return nil +} + +func (mset *stream) addConsumerWithAssignment(config *ConsumerConfig, oname string, ca *consumerAssignment, isRecovering bool) (*consumer, error) { + mset.mu.RLock() + s, jsa, tierName, cfg, acc := mset.srv, mset.jsa, mset.tier, mset.cfg, mset.acc + retention := cfg.Retention + mset.mu.RUnlock() + + // If we do not have the consumer currently assigned to us in cluster mode we will proceed but warn. + // This can happen on startup with restored state where on meta replay we still do not have + // the assignment. Running in single server mode this always returns true. + if oname != _EMPTY_ && !jsa.consumerAssigned(mset.name(), oname) { + s.Debugf("Consumer %q > %q does not seem to be assigned to this server", mset.name(), oname) + } + + if config == nil { + return nil, NewJSConsumerConfigRequiredError() + } + + jsa.usageMu.RLock() + selectedLimits, limitsFound := jsa.limits[tierName] + jsa.usageMu.RUnlock() + if !limitsFound { + return nil, NewJSNoLimitsError() + } + + srvLim := &s.getOpts().JetStreamLimits + // Make sure we have sane defaults. + setConsumerConfigDefaults(config, srvLim, &selectedLimits) + + if err := checkConsumerCfg(config, srvLim, &cfg, acc, &selectedLimits, isRecovering); err != nil { + return nil, err + } + + sampleFreq := 0 + if config.SampleFrequency != _EMPTY_ { + // Can't fail as checkConsumerCfg checks correct format + sampleFreq, _ = strconv.Atoi(strings.TrimSuffix(config.SampleFrequency, "%")) + } + // Grab the client, account and server reference. c := mset.client if c == nil { return nil, NewJSStreamInvalidError() } + var accName string c.mu.Lock() s, a := c.srv, c.acc + if a != nil { + accName = a.Name + } c.mu.Unlock() // Hold mset lock here. @@ -512,8 +589,8 @@ func (mset *stream) addConsumerWithAssignment(config *ConsumerConfig, oname stri // than stream config we prefer the account limits to handle cases where account limits are // updated during the lifecycle of the stream maxc := mset.cfg.MaxConsumers - if maxc <= 0 || (mset.jsa.limits.MaxConsumers > 0 && mset.jsa.limits.MaxConsumers < maxc) { - maxc = mset.jsa.limits.MaxConsumers + if maxc <= 0 || (selectedLimits.MaxConsumers > 0 && selectedLimits.MaxConsumers < maxc) { + maxc = selectedLimits.MaxConsumers } if maxc > 0 && mset.numPublicConsumers() >= maxc { mset.mu.Unlock() @@ -546,22 +623,24 @@ func (mset *stream) addConsumerWithAssignment(config *ConsumerConfig, oname stri // Set name, which will be durable name if set, otherwise we create one at random. o := &consumer{ - mset: mset, - js: s.getJetStream(), - acc: a, - srv: s, - client: s.createInternalJetStreamClient(), - sysc: s.createInternalJetStreamClient(), - cfg: *config, - dsubj: config.DeliverSubject, - outq: mset.outq, - active: true, - qch: make(chan struct{}), - mch: make(chan struct{}, 1), - sfreq: int32(sampleFreq), - maxdc: uint64(config.MaxDeliver), - maxp: config.MaxAckPending, - created: time.Now().UTC(), + mset: mset, + js: s.getJetStream(), + acc: a, + srv: s, + client: s.createInternalJetStreamClient(), + sysc: s.createInternalJetStreamClient(), + cfg: *config, + dsubj: config.DeliverSubject, + outq: mset.outq, + active: true, + qch: make(chan struct{}), + uch: make(chan struct{}, 1), + mch: make(chan struct{}, 1), + sfreq: int32(sampleFreq), + maxdc: uint64(config.MaxDeliver), + maxp: config.MaxAckPending, + retention: retention, + created: time.Now().UTC(), } // Bind internal client to the user account. @@ -586,6 +665,9 @@ func (mset *stream) addConsumerWithAssignment(config *ConsumerConfig, oname stri } } } + // Create ackMsgs queue now that we have a consumer name + o.ackMsgs = s.newIPQueue(fmt.Sprintf("[ACC:%s] consumer '%s' on stream '%s' ackMsgs", accName, o.name, mset.cfg.Name)) + // Create our request waiting queue. if o.isPullMode() { o.waiting = newWaitQueue(config.MaxWaiting) @@ -599,6 +681,7 @@ func (mset *stream) addConsumerWithAssignment(config *ConsumerConfig, oname stri // already under lock, mset.Name() would deadlock o.stream = mset.cfg.Name o.ackEventT = JSMetricConsumerAckPre + "." + o.stream + "." + o.name + o.nakEventT = JSAdvisoryConsumerMsgNakPre + "." + o.stream + "." + o.name o.deliveryExcEventT = JSAdvisoryConsumerMaxDeliveryExceedPre + "." + o.stream + "." + o.name if !isValidName(o.name) { @@ -607,9 +690,7 @@ func (mset *stream) addConsumerWithAssignment(config *ConsumerConfig, oname stri return nil, NewJSConsumerBadDurableNameError() } - // Select starting sequence number - o.selectStartingSeqNo() - + // Setup our storage if not a direct consumer. if !config.Direct { store, err := mset.store.ConsumerStore(o.name, config) if err != nil { @@ -620,6 +701,14 @@ func (mset *stream) addConsumerWithAssignment(config *ConsumerConfig, oname stri o.store = store } + if o.store != nil && o.store.HasState() { + // Restore our saved state. + o.readStoredState(0) + } else { + // Select starting sequence number + o.selectStartingSeqNo() + } + // Now register with mset and create the ack subscription. // Check if we already have this one registered. if eo, ok := mset.consumers[o.name]; ok { @@ -721,13 +810,27 @@ func (o *consumer) consumerAssignment() *consumerAssignment { func (o *consumer) setConsumerAssignment(ca *consumerAssignment) { o.mu.Lock() defer o.mu.Unlock() + o.ca = ca + if ca == nil { + return + } // Set our node. - if ca != nil { - o.node = ca.Group.node + o.node = ca.Group.node + + // Trigger update chan. + select { + case o.uch <- struct{}{}: + default: } } +func (o *consumer) updateC() <-chan struct{} { + o.mu.RLock() + defer o.mu.RUnlock() + return o.uch +} + // checkQueueInterest will check on our interest's queue group status. // Lock should be held. func (o *consumer) checkQueueInterest() { @@ -780,12 +883,27 @@ func (o *consumer) setLeader(isLeader bool) { } mset.mu.RLock() - s, jsa, stream := mset.srv, mset.jsa, mset.cfg.Name + s, jsa, stream, lseq := mset.srv, mset.jsa, mset.cfg.Name, mset.lseq mset.mu.RUnlock() o.mu.Lock() + o.rdq, o.rdqi = nil, nil + // Restore our saved state. During non-leader status we just update our underlying store. - o.readStoredState() + o.readStoredState(lseq) + + // Setup initial num pending. + o.streamNumPending() + + // Cleanup lss when we take over in clustered mode. + if o.hasSkipListPending() && o.sseq >= o.lss.resume { + o.lss = nil + } + + // Update the group on the our starting sequence if we are starting but we skipped some in the stream. + if o.dseq == 1 && o.sseq > 1 { + o.updateSkipped() + } // Do info sub. if o.infoSub == nil && jsa != nil { @@ -795,7 +913,7 @@ func (o *consumer) setLeader(isLeader bool) { } var err error - if o.ackSub, err = o.subscribeInternal(o.ackSubj, o.processAck); err != nil { + if o.ackSub, err = o.subscribeInternal(o.ackSubj, o.pushAck); err != nil { o.mu.Unlock() o.deleteWithoutAdvisory() return @@ -820,9 +938,6 @@ func (o *consumer) setLeader(isLeader bool) { } } - // Setup initial pending and proper start sequence. - o.setInitialPendingAndStart() - // If push mode, register for notifications on interest. if o.isPushMode() { o.inch = make(chan bool, 8) @@ -866,6 +981,9 @@ func (o *consumer) setLeader(isLeader bool) { // Now start up Go routine to deliver msgs. go o.loopAndGatherMsgs(qch) + // Now start up Go routine to process acks. + go o.processInboundAcks(qch) + // If we are R>1 spin up our proposal loop. if node != nil { // Determine if we can send pending requests info to the group. @@ -878,6 +996,14 @@ func (o *consumer) setLeader(isLeader bool) { } else { // Shutdown the go routines and the subscriptions. o.mu.Lock() + if o.qch != nil { + close(o.qch) + o.qch = nil + } + // Make sure to clear out any re delivery queues + stopAndClearTimer(&o.ptmr) + o.rdq, o.rdqi = nil, nil + o.pending = nil // ok if they are nil, we protect inside unsubscribe() o.unsubscribe(o.ackSub) o.unsubscribe(o.reqSub) @@ -887,13 +1013,14 @@ func (o *consumer) setLeader(isLeader bool) { o.srv.sysUnsubscribe(o.infoSub) o.infoSub = nil } - if o.qch != nil { - close(o.qch) - o.qch = nil - } // Reset waiting if we are in pull mode. if o.isPullMode() { o.waiting = newWaitQueue(o.cfg.MaxWaiting) + if !o.isDurable() { + stopAndClearTimer(&o.dtmr) + } + } else if o.srv.gateway.enabled { + stopAndClearTimer(&o.gwdtmr) } o.mu.Unlock() } @@ -1335,6 +1462,13 @@ func (o *consumer) updateConfig(cfg *ConsumerConfig) error { // We need both locks here so do in Go routine. go o.setRateLimitNeedsLocks() } + if cfg.SampleFrequency != o.cfg.SampleFrequency { + s := strings.TrimSuffix(cfg.SampleFrequency, "%") + // String has been already verified for validity up in the stack, so no + // need to check for error here. + sampleFreq, _ := strconv.Atoi(s) + o.sfreq = int32(sampleFreq) + } // Record new config for others that do not need special handling. // Allowed but considered no-op, [Description, MaxDeliver, SampleFrequency, MaxWaiting, HeadersOnly] @@ -1384,9 +1518,55 @@ func (o *consumer) sendAckReply(subj string) { o.sendAdvisory(subj, nil) } -// Process a message for the ack reply subject delivered with a message. -func (o *consumer) processAck(_ *subscription, c *client, acc *Account, subject, reply string, rmsg []byte) { - _, msg := c.msgParts(rmsg) +type jsAckMsg struct { + subject string + reply string + hdr int + msg []byte +} + +var jsAckMsgPool sync.Pool + +func newJSAckMsg(subj, reply string, hdr int, msg []byte) *jsAckMsg { + var m *jsAckMsg + am := jsAckMsgPool.Get() + if am != nil { + m = am.(*jsAckMsg) + } else { + m = &jsAckMsg{} + } + // When getting something from a pool it is criticical that all fields are + // initialized. Doing this way guarantees that if someone adds a field to + // the structure, the compiler will fail the build if this line is not updated. + (*m) = jsAckMsg{subj, reply, hdr, msg} + return m +} + +func (am *jsAckMsg) returnToPool() { + if am == nil { + return + } + am.subject, am.reply, am.hdr, am.msg = _EMPTY_, _EMPTY_, -1, nil + jsAckMsgPool.Put(am) +} + +// Push the ack message to the consumer's ackMsgs queue +func (o *consumer) pushAck(_ *subscription, c *client, _ *Account, subject, reply string, rmsg []byte) { + atomic.AddInt64(&o.awl, 1) + o.ackMsgs.push(newJSAckMsg(subject, reply, c.pa.hdr, copyBytes(rmsg))) +} + +// Processes a message for the ack reply subject delivered with a message. +func (o *consumer) processAck(subject, reply string, hdr int, rmsg []byte) { + defer atomic.AddInt64(&o.awl, -1) + + var msg []byte + if hdr > 0 { + msg = rmsg[hdr:] + } else { + msg = rmsg + } + sseq, dseq, dc := ackReplyInfo(subject) skipAckReply := sseq == 0 @@ -1396,16 +1576,7 @@ func (o *consumer) processAck(_ *subscription, c *client, acc *Account, subject, o.processAckMsg(sseq, dseq, dc, true) case bytes.HasPrefix(msg, AckNext): o.processAckMsg(sseq, dseq, dc, true) - // processNextMsgReq can be invoked from an internal subscription or from here. - // Therefore, it has to call msgParts(), so we can't simply pass msg[len(AckNext):] - // with current c.pa.hdr because it would cause a panic. We will save the current - // c.pa.hdr value and disable headers before calling processNextMsgReq and then - // restore so that we don't mess with the calling stack in case it is used - // somewhere else. - phdr := c.pa.hdr - c.pa.hdr = -1 - o.processNextMsgReq(nil, c, acc, subject, reply, msg[len(AckNext):]) - c.pa.hdr = phdr + o.processNextMsgRequest(reply, msg[len(AckNext):]) skipAckReply = true case bytes.HasPrefix(msg, AckNak): o.processNak(sseq, dseq, dc, msg) @@ -1469,7 +1640,7 @@ func (o *consumer) loopAndForwardProposals(qch chan struct{}) { sz += len(proposal.data) if sz > maxBatch { node.ProposeDirect(entries) - // We need to re-craete `entries` because there is a reference + // We need to re-create `entries` because there is a reference // to it in the node's pae map. sz, entries = 0, nil } @@ -1644,6 +1815,29 @@ func (o *consumer) processNak(sseq, dseq, dc uint64, nak []byte) { return } } + + // Deliver an advisory + e := JSConsumerDeliveryNakAdvisory{ + TypedEvent: TypedEvent{ + Type: JSConsumerDeliveryNakAdvisoryType, + ID: nuid.Next(), + Time: time.Now().UTC(), + }, + Stream: o.stream, + Consumer: o.name, + ConsumerSeq: dseq, + StreamSeq: sseq, + Deliveries: dc, + Domain: o.srv.getOpts().JetStreamDomain, + } + + j, err := json.Marshal(e) + if err != nil { + return + } + + o.sendAdvisory(o.nakEventT, j) + // Check to see if we have delays attached. if len(nak) > len(AckNak) { arg := bytes.TrimSpace(nak[len(AckNak):]) @@ -1735,10 +1929,10 @@ func (o *consumer) ackWait(next time.Duration) time.Duration { } // Due to bug in calculation of sequences on restoring redelivered let's do quick sanity check. -func (o *consumer) checkRedelivered() { +func (o *consumer) checkRedelivered(slseq uint64) { var lseq uint64 if mset := o.mset; mset != nil { - lseq = mset.lastSeq() + lseq = slseq } var shouldUpdateState bool for sseq := range o.rdc { @@ -1755,15 +1949,15 @@ func (o *consumer) checkRedelivered() { // This will restore the state from disk. // Lock should be held. -func (o *consumer) readStoredState() error { +func (o *consumer) readStoredState(slseq uint64) error { if o.store == nil { return nil } state, err := o.store.State() - if err == nil && state != nil && state.Delivered.Consumer != 0 { + if err == nil { o.applyState(state) if len(o.rdc) > 0 { - o.checkRedelivered() + o.checkRedelivered(slseq) } } return err @@ -1775,8 +1969,12 @@ func (o *consumer) applyState(state *ConsumerState) { return } + // If o.sseq is greater don't update. Don't go backwards on o.sseq. + if o.sseq <= state.Delivered.Stream { + o.sseq = state.Delivered.Stream + 1 + } o.dseq = state.Delivered.Consumer + 1 - o.sseq = state.Delivered.Stream + 1 + o.adflr = state.AckFloor.Consumer o.asflr = state.AckFloor.Stream o.pending = state.Pending @@ -1795,16 +1993,6 @@ func (o *consumer) applyState(state *ConsumerState) { } } -func (o *consumer) readStoreState() *ConsumerState { - o.mu.RLock() - defer o.mu.RUnlock() - if o.store == nil { - return nil - } - state, _ := o.store.State() - return state -} - // Sets our store state from another source. Used in clustered mode on snapshot restore. func (o *consumer) setStoreState(state *ConsumerState) error { if state == nil || o.store == nil { @@ -1827,7 +2015,6 @@ func (o *consumer) writeStoreStateUnlocked() error { if o.store == nil { return nil } - state := ConsumerState{ Delivered: SequencePair{ Consumer: o.dseq - 1, @@ -1904,7 +2091,7 @@ func (o *consumer) infoWithSnap(snap bool) *ConsumerInfo { }, NumAckPending: len(o.pending), NumRedelivered: len(o.rdc), - NumPending: o.adjustedPending(), + NumPending: o.streamNumPending(), PushBound: o.isPushMode() && o.active, Cluster: ci, } @@ -1920,7 +2107,7 @@ func (o *consumer) infoWithSnap(snap bool) *ConsumerInfo { // If we are a pull mode consumer, report on number of waiting requests. if o.isPullMode() { - o.processWaiting() + o.processWaiting(false) info.NumWaiting = o.waiting.len() } // If we were asked to snapshot do so here. @@ -2062,7 +2249,7 @@ func (o *consumer) processAckMsg(sseq, dseq, dc uint64, doSample bool) { } } - // If we had max ack pending set and were at limit we need to unblock folks. + // If we had max ack pending set and were at limit we need to unblock ourselves. if needSignal { o.signalNewMessages() } @@ -2097,8 +2284,9 @@ func (o *consumer) needAck(sseq uint64) bool { // Check first if we are filtered, and if so check if this is even applicable to us. if o.isFiltered() && o.mset != nil { - subj, _, _, _, err := o.mset.store.LoadMsg(sseq) - if err != nil || !o.isFilteredMatch(subj) { + var svp StoreMsg + sm, err := o.mset.store.LoadMsg(sseq, &svp) + if err != nil || !o.isFilteredMatch(sm.subj) { o.mu.RUnlock() return false } @@ -2143,36 +2331,36 @@ func (o *consumer) needAck(sseq uint64) bool { } // Helper for the next message requests. -func nextReqFromMsg(msg []byte) (time.Time, int, bool, time.Duration, time.Time, error) { +func nextReqFromMsg(msg []byte) (time.Time, int, int, bool, time.Duration, time.Time, error) { req := bytes.TrimSpace(msg) switch { case len(req) == 0: - return time.Time{}, 1, false, 0, time.Time{}, nil + return time.Time{}, 1, 0, false, 0, time.Time{}, nil case req[0] == '{': var cr JSApiConsumerGetNextRequest if err := json.Unmarshal(req, &cr); err != nil { - return time.Time{}, -1, false, 0, time.Time{}, err + return time.Time{}, -1, 0, false, 0, time.Time{}, err } var hbt time.Time if cr.Heartbeat > 0 { if cr.Heartbeat*2 > cr.Expires { - return time.Time{}, 1, false, 0, time.Time{}, errors.New("heartbeat value too large") + return time.Time{}, 1, 0, false, 0, time.Time{}, errors.New("heartbeat value too large") } hbt = time.Now().Add(cr.Heartbeat) } if cr.Expires == time.Duration(0) { - return time.Time{}, cr.Batch, cr.NoWait, cr.Heartbeat, hbt, nil + return time.Time{}, cr.Batch, cr.MaxBytes, cr.NoWait, cr.Heartbeat, hbt, nil } - return time.Now().Add(cr.Expires), cr.Batch, cr.NoWait, cr.Heartbeat, hbt, nil + return time.Now().Add(cr.Expires), cr.Batch, cr.MaxBytes, cr.NoWait, cr.Heartbeat, hbt, nil default: if n, err := strconv.Atoi(string(req)); err == nil { - return time.Time{}, n, false, 0, time.Time{}, nil + return time.Time{}, n, 0, false, 0, time.Time{}, nil } } - return time.Time{}, 1, false, 0, time.Time{}, nil + return time.Time{}, 1, 0, false, 0, time.Time{}, nil } // Represents a request that is on the internal waiting queue @@ -2182,6 +2370,7 @@ type waitingRequest struct { reply string n int // For batching d int + b int // For max bytes tracking. expires time.Time received time.Time hb time.Duration @@ -2215,9 +2404,9 @@ func (wr *waitingRequest) recycle() { // waiting queue for requests that are waiting for new messages to arrive. type waitQueue struct { - rp, wp int - last time.Time - reqs []*waitingRequest + rp, wp, n int + last time.Time + reqs []*waitingRequest } // Create a new ring buffer with at most max items. @@ -2248,11 +2437,12 @@ func (wq *waitQueue) add(wr *waitingRequest) error { } // Track last active via when we receive a request. wq.last = wr.received + wq.n++ return nil } func (wq *waitQueue) isFull() bool { - return wq.rp == wq.wp + return wq.n == cap(wq.reqs) } func (wq *waitQueue) isEmpty() bool { @@ -2260,13 +2450,10 @@ func (wq *waitQueue) isEmpty() bool { } func (wq *waitQueue) len() int { - if wq == nil || wq.rp < 0 { + if wq == nil { return 0 } - if wq.rp < wq.wp { - return wq.wp - wq.rp - } - return cap(wq.reqs) - wq.rp + wq.wp + return wq.n } // Peek will return the next request waiting or nil if empty. @@ -2301,8 +2488,9 @@ func (wq *waitQueue) removeCurrent() { } wq.reqs[wq.rp] = nil wq.rp = (wq.rp + 1) % cap(wq.reqs) + wq.n-- // Check if we are empty. - if wq.rp == wq.wp { + if wq.n == 0 { wq.rp, wq.wp = -1, 0 } } @@ -2313,14 +2501,15 @@ func (wq *waitQueue) compact() { return } nreqs, i := make([]*waitingRequest, cap(wq.reqs)), 0 - for rp := wq.rp; rp != wq.wp; rp = (rp + 1) % cap(wq.reqs) { + for j, rp := 0, wq.rp; j < wq.n; j++ { if wr := wq.reqs[rp]; wr != nil { nreqs[i] = wr i++ } + rp = (rp + 1) % cap(wq.reqs) } // Reset here. - wq.rp, wq.wp, wq.reqs = 0, i, nreqs + wq.rp, wq.wp, wq.n, wq.reqs = 0, i, i, nreqs } // Return the replies for our pending requests. @@ -2332,10 +2521,11 @@ func (o *consumer) pendingRequestReplies() []string { return nil } wq, m := o.waiting, make(map[string]struct{}) - for rp := o.waiting.rp; o.waiting.rp >= 0 && rp != wq.wp; rp = (rp + 1) % cap(wq.reqs) { + for i, rp := 0, o.waiting.rp; i < wq.n; i++ { if wr := wq.reqs[rp]; wr != nil { m[wr.reply] = struct{}{} } + rp = (rp + 1) % cap(wq.reqs) } var replies []string for reply := range m { @@ -2347,23 +2537,52 @@ func (o *consumer) pendingRequestReplies() []string { // Return next waiting request. This will check for expirations but not noWait or interest. // That will be handled by processWaiting. // Lock should be held. -func (o *consumer) nextWaiting() *waitingRequest { +func (o *consumer) nextWaiting(sz int) *waitingRequest { if o.waiting == nil || o.waiting.isEmpty() { return nil } for wr := o.waiting.peek(); !o.waiting.isEmpty(); wr = o.waiting.peek() { - if wr == nil || wr.expires.IsZero() || time.Now().Before(wr.expires) { + if wr == nil { + break + } + // Check if we have max bytes set. + if wr.b > 0 { + if sz <= wr.b { + wr.b -= sz + // If we are right now at zero, set batch to 1 to deliver this one but stop after. + if wr.b == 0 { + wr.n = 1 + } + } else { + // If we have not delivered anything to the requestor let them know. + if wr.d == 0 { + hdr := []byte("NATS/1.0 408 Message Size Exceeds MaxBytes\r\n\r\n") + o.outq.send(newJSPubMsg(wr.reply, _EMPTY_, _EMPTY_, hdr, nil, nil, 0)) + } + // Remove the current one, no longer valid due to max bytes limit. + o.waiting.removeCurrent() + if o.node != nil { + o.removeClusterPendingRequest(wr.reply) + } + wr.recycle() + continue + } + } + + if wr.expires.IsZero() || time.Now().Before(wr.expires) { rr := wr.acc.sl.Match(wr.interest) if len(rr.psubs)+len(rr.qsubs) > 0 { return o.waiting.pop() - } else if o.srv.gateway.enabled { - if o.srv.hasGatewayInterest(wr.acc.Name, wr.interest) || time.Since(wr.received) < defaultGatewayRecentSubExpiration { - return o.waiting.pop() - } + } else if time.Since(wr.received) < defaultGatewayRecentSubExpiration && (o.srv.leafNodeEnabled || o.srv.gateway.enabled) { + return o.waiting.pop() + } else if o.srv.gateway.enabled && o.srv.hasGatewayInterest(wr.acc.Name, wr.interest) { + return o.waiting.pop() } } - hdr := []byte("NATS/1.0 408 Request Timeout\r\n\r\n") - o.outq.send(newJSPubMsg(wr.reply, _EMPTY_, _EMPTY_, hdr, nil, nil, 0)) + if wr.interest != wr.reply { + hdr := []byte("NATS/1.0 408 Interest Expired\r\n\r\n") + o.outq.send(newJSPubMsg(wr.reply, _EMPTY_, _EMPTY_, hdr, nil, nil, 0)) + } // Remove the current one, no longer valid. o.waiting.removeCurrent() if o.node != nil { @@ -2382,7 +2601,10 @@ func (o *consumer) processNextMsgReq(_ *subscription, c *client, _ *Account, _, return } _, msg = c.msgParts(msg) + o.processNextMsgRequest(reply, msg) +} +func (o *consumer) processNextMsgRequest(reply string, msg []byte) { o.mu.Lock() defer o.mu.Unlock() @@ -2402,7 +2624,7 @@ func (o *consumer) processNextMsgReq(_ *subscription, c *client, _ *Account, _, } // Check payload here to see if they sent in batch size or a formal request. - expires, batchSize, noWait, hb, hbt, err := nextReqFromMsg(msg) + expires, batchSize, maxBytes, noWait, hb, hbt, err := nextReqFromMsg(msg) if err != nil { sendErr(400, fmt.Sprintf("Bad Request - %v", err)) return @@ -2419,18 +2641,20 @@ func (o *consumer) processNextMsgReq(_ *subscription, c *client, _ *Account, _, return } + if maxBytes > 0 && o.cfg.MaxRequestMaxBytes > 0 && maxBytes > o.cfg.MaxRequestMaxBytes { + sendErr(409, fmt.Sprintf("Exceeded MaxRequestMaxBytes of %v", o.cfg.MaxRequestMaxBytes)) + return + } + // If we have the max number of requests already pending try to expire. if o.waiting.isFull() { // Try to expire some of the requests. - if expired, _, _, _ := o.processWaiting(); expired == 0 { - // Force expiration if needed. - o.forceExpireFirstWaiting() - } + o.processWaiting(false) } // If the request is for noWait and we have pending requests already, check if we have room. if noWait { - msgsPending := o.adjustedPending() + uint64(len(o.rdq)) + msgsPending := o.numPending() + uint64(len(o.rdq)) // If no pending at all, decide what to do with request. // If no expires was set then fail. if msgsPending == 0 && expires.IsZero() { @@ -2438,7 +2662,7 @@ func (o *consumer) processNextMsgReq(_ *subscription, c *client, _ *Account, _, return } if msgsPending > 0 { - _, _, batchPending, _ := o.processWaiting() + _, _, batchPending, _ := o.processWaiting(false) if msgsPending < uint64(batchPending) { sendErr(408, "Requests Pending") return @@ -2449,18 +2673,12 @@ func (o *consumer) processNextMsgReq(_ *subscription, c *client, _ *Account, _, } // If we receive this request though an account export, we need to track that interest subject and account. - acc, interest := o.acc, reply - for strings.HasPrefix(interest, replyPrefix) && acc.exports.responses != nil { - if si := acc.exports.responses[interest]; si != nil { - acc, interest = si.acc, si.to - } else { - break - } - } + acc, interest := trackDownAccountAndInterest(o.acc, reply) - // In case we have to queue up this request. + // Create a waiting request. wr := wrPool.Get().(*waitingRequest) wr.acc, wr.interest, wr.reply, wr.n, wr.d, wr.noWait, wr.expires, wr.hb, wr.hbt = acc, interest, reply, batchSize, 0, noWait, expires, hb, hbt + wr.b = maxBytes wr.received = time.Now() if err := o.waiting.add(wr); err != nil { @@ -2474,6 +2692,25 @@ func (o *consumer) processNextMsgReq(_ *subscription, c *client, _ *Account, _, } } +func trackDownAccountAndInterest(acc *Account, interest string) (*Account, string) { + for strings.HasPrefix(interest, replyPrefix) { + oa := acc + oa.mu.RLock() + if oa.exports.responses == nil { + oa.mu.RUnlock() + break + } + si := oa.exports.responses[interest] + if si == nil { + oa.mu.RUnlock() + break + } + acc, interest = si.acc, si.to + oa.mu.RUnlock() + } + return acc, interest +} + // Increase the delivery count for this message. // ONLY used on redelivery semantics. // Lock should be held. @@ -2532,9 +2769,9 @@ var ( // Get next available message from underlying store. // Is partition aware and redeliver aware. // Lock should be held. -func (o *consumer) getNextMsg() (subj string, hdr, msg []byte, sseq uint64, dc uint64, ts int64, err error) { +func (o *consumer) getNextMsg() (*jsPubMsg, uint64, error) { if o.mset == nil || o.mset.store == nil { - return _EMPTY_, nil, nil, 0, 0, 0, errBadConsumer + return nil, 0, errBadConsumer } seq, dc := o.sseq, uint64(1) if o.hasSkipListPending() { @@ -2559,8 +2796,13 @@ func (o *consumer) getNextMsg() (subj string, hdr, msg []byte, sseq uint64, dc u continue } if seq > 0 { - subj, hdr, msg, ts, err = o.mset.store.LoadMsg(seq) - return subj, hdr, msg, seq, dc, ts, err + pmsg := getJSPubMsgFromPool() + sm, err := o.mset.store.LoadMsg(seq, &pmsg.StoreMsg) + if sm == nil || err != nil { + pmsg.returnToPool() + pmsg, dc = nil, 0 + } + return pmsg, dc, err } } // Fallback if all redeliveries are gone. @@ -2571,11 +2813,12 @@ func (o *consumer) getNextMsg() (subj string, hdr, msg []byte, sseq uint64, dc u if o.maxp > 0 && len(o.pending) >= o.maxp { // maxp only set when ack policy != AckNone and user set MaxAckPending // Stall if we have hit max pending. - return _EMPTY_, nil, nil, 0, 0, 0, errMaxAckPending + return nil, 0, errMaxAckPending } // Grab next message applicable to us. - subj, sseq, hdr, msg, ts, err = o.mset.store.LoadNextMsg(o.cfg.FilterSubject, o.filterWC, seq) + pmsg := getJSPubMsgFromPool() + sm, sseq, err := o.mset.store.LoadNextMsg(o.cfg.FilterSubject, o.filterWC, seq, &pmsg.StoreMsg) if sseq >= o.sseq { o.sseq = sseq + 1 @@ -2584,33 +2827,17 @@ func (o *consumer) getNextMsg() (subj string, hdr, msg []byte, sseq uint64, dc u } } - return subj, hdr, msg, sseq, dc, ts, err -} - -// forceExpireFirstWaiting will force expire the first waiting. -// Lock should be held. -func (o *consumer) forceExpireFirstWaiting() { - // FIXME(dlc) - Should we do advisory here as well? - wr := o.waiting.peek() - if wr == nil { - return - } - // If we are expiring this and we think there is still interest, alert. - if rr := wr.acc.sl.Match(wr.interest); len(rr.psubs)+len(rr.qsubs) > 0 && o.mset != nil { - // We still appear to have interest, so send alert as courtesy. - hdr := []byte("NATS/1.0 408 Request Canceled\r\n\r\n") - o.outq.send(newJSPubMsg(wr.reply, _EMPTY_, _EMPTY_, hdr, nil, nil, 0)) - } - o.waiting.removeCurrent() - if o.node != nil { - o.removeClusterPendingRequest(wr.reply) + if sm == nil { + pmsg.returnToPool() + return nil, 0, err } - wr.recycle() + + return pmsg, dc, err } // Will check for expiration and lack of interest on waiting requests. // Will also do any heartbeats and return the next expiration or HB interval. -func (o *consumer) processWaiting() (int, int, int, time.Time) { +func (o *consumer) processWaiting(eos bool) (int, int, int, time.Time) { var fexp time.Time if o.srv == nil || o.waiting.isEmpty() { return 0, 0, 0, fexp @@ -2636,23 +2863,25 @@ func (o *consumer) processWaiting() (int, int, int, time.Time) { } wq := o.waiting - - for rp := o.waiting.rp; o.waiting.rp >= 0 && rp != wq.wp; rp = (rp + 1) % cap(wq.reqs) { + for i, rp, n := 0, wq.rp, wq.n; i < n; rp = (rp + 1) % cap(wq.reqs) { wr := wq.reqs[rp] // Check expiration. - if (wr.noWait && wr.d > 0) || (!wr.expires.IsZero() && now.After(wr.expires)) { + if (eos && wr.noWait && wr.d > 0) || (!wr.expires.IsZero() && now.After(wr.expires)) { hdr := []byte("NATS/1.0 408 Request Timeout\r\n\r\n") o.outq.send(newJSPubMsg(wr.reply, _EMPTY_, _EMPTY_, hdr, nil, nil, 0)) remove(wr, rp) + i++ continue } // Now check interest. rr := wr.acc.sl.Match(wr.interest) interest := len(rr.psubs)+len(rr.qsubs) > 0 - if !interest && s.gateway.enabled { - // If we are here check on gateways. + if !interest && (s.leafNodeEnabled || s.gateway.enabled) { + // If we are here check on gateways and leaf nodes (as they can mask gateways on the other end). // If we have interest or the request is too young break and do not expire. - if s.hasGatewayInterest(wr.acc.Name, wr.interest) || time.Since(wr.received) < defaultGatewayRecentSubExpiration { + if time.Since(wr.received) < defaultGatewayRecentSubExpiration { + interest = true + } else if s.gateway.enabled && s.hasGatewayInterest(wr.acc.Name, wr.interest) { interest = true } } @@ -2673,10 +2902,12 @@ func (o *consumer) processWaiting() (int, int, int, time.Time) { if !wr.expires.IsZero() && (fexp.IsZero() || wr.expires.Before(fexp)) { fexp = wr.expires } + i++ continue } // No more interest here so go ahead and remove this one from our list. remove(wr, rp) + i++ } // If we have interior deletes from out of order invalidation, compact the waiting queue. @@ -2684,12 +2915,12 @@ func (o *consumer) processWaiting() (int, int, int, time.Time) { o.waiting.compact() } - return expired, o.waiting.len(), brp, fexp + return expired, wq.len(), brp, fexp } // Will check to make sure those waiting still have registered interest. func (o *consumer) checkWaitingForInterest() bool { - o.processWaiting() + o.processWaiting(true) return o.waiting.len() > 0 } @@ -2701,6 +2932,30 @@ func (o *consumer) hbTimer() (time.Duration, *time.Timer) { return o.cfg.Heartbeat, time.NewTimer(o.cfg.Heartbeat) } +func (o *consumer) processInboundAcks(qch chan struct{}) { + // Grab the server lock to watch for server quit. + o.mu.RLock() + s := o.srv + o.mu.RUnlock() + + for { + select { + case <-o.ackMsgs.ch: + acks := o.ackMsgs.pop() + for _, acki := range acks { + ack := acki.(*jsAckMsg) + o.processAck(ack.subject, ack.reply, ack.hdr, ack.msg) + ack.returnToPool() + } + o.ackMsgs.recycle(&acks) + case <-qch: + return + case <-s.quitCh: + return + } + } +} + func (o *consumer) loopAndGatherMsgs(qch chan struct{}) { // On startup check to see if we are in a a reply situation where replay policy is not instant. var ( @@ -2708,15 +2963,24 @@ func (o *consumer) loopAndGatherMsgs(qch chan struct{}) { lseq uint64 ) + o.mu.RLock() + mset := o.mset + getLSeq := o.replay + o.mu.RUnlock() + // consumer is closed when mset is set to nil. + if mset == nil { + return + } + if getLSeq { + lseq = mset.state().LastSeq + } + o.mu.Lock() s := o.srv - if o.replay { - // consumer is closed when mset is set to nil. - if o.mset == nil { - o.mu.Unlock() - return - } - lseq = o.mset.state().LastSeq + // need to check again if consumer is closed + if o.mset == nil { + o.mu.Unlock() + return } // For idle heartbeat support. var hbc <-chan time.Time @@ -2728,18 +2992,21 @@ func (o *consumer) loopAndGatherMsgs(qch chan struct{}) { inch := o.inch o.mu.Unlock() + // Grab the stream's retention policy + mset.mu.RLock() + rp := mset.cfg.Retention + mset.mu.RUnlock() + + var err error + // Deliver all the msgs we have now, once done or on a condition, we wait for new ones. for { var ( - seq, dc uint64 - subj, dsubj string - hdr []byte - msg []byte - err error - ts int64 - delay time.Duration + pmsg *jsPubMsg + dc uint64 + dsubj string + delay time.Duration ) - o.mu.Lock() // consumer is closed when mset is set to nil. if o.mset == nil { @@ -2747,6 +3014,9 @@ func (o *consumer) loopAndGatherMsgs(qch chan struct{}) { return } + // Clear last error. + err = nil + // If we are in push mode and not active or under flowcontrol let's stop sending. if o.isPushMode() { if !o.active || (o.maxpb > 0 && o.pbytes > o.maxpb) { @@ -2757,10 +3027,11 @@ func (o *consumer) loopAndGatherMsgs(qch chan struct{}) { goto waitForMsgs } - subj, hdr, msg, seq, dc, ts, err = o.getNextMsg() + // Grab our next msg. + pmsg, dc, err = o.getNextMsg() // On error either wait or return. - if err != nil { + if err != nil || pmsg == nil { if err == ErrStoreMsgNotFound || err == ErrStoreEOF || err == errMaxAckPending || err == errPartialCache { goto waitForMsgs } else { @@ -2771,7 +3042,7 @@ func (o *consumer) loopAndGatherMsgs(qch chan struct{}) { if o.isPushMode() { dsubj = o.dsubj - } else if wr := o.nextWaiting(); wr != nil { + } else if wr := o.nextWaiting(len(pmsg.hdr) + len(pmsg.msg)); wr != nil { dsubj = wr.reply if done := wr.recycleIfDone(); done && o.node != nil { o.removeClusterPendingRequest(dsubj) @@ -2781,15 +3052,17 @@ func (o *consumer) loopAndGatherMsgs(qch chan struct{}) { } else { // We will redo this one. o.sseq-- + pmsg.returnToPool() goto waitForMsgs } // If we are in a replay scenario and have not caught up check if we need to delay here. if o.replay && lts > 0 { - if delay = time.Duration(ts - lts); delay > time.Millisecond { + if delay = time.Duration(pmsg.ts - lts); delay > time.Millisecond { o.mu.Unlock() select { case <-qch: + pmsg.returnToPool() return case <-time.After(delay): } @@ -2798,17 +3071,18 @@ func (o *consumer) loopAndGatherMsgs(qch chan struct{}) { } // Track this regardless. - lts = ts + lts = pmsg.ts // If we have a rate limit set make sure we check that here. if o.rlimit != nil { - now := time.Now() - r := o.rlimit.ReserveN(now, len(msg)+len(hdr)+len(subj)+len(dsubj)+len(o.ackReplyT)) + now, sm := time.Now(), &pmsg.StoreMsg + r := o.rlimit.ReserveN(now, len(sm.msg)+len(sm.hdr)+len(sm.subj)+len(dsubj)+len(o.ackReplyT)) delay := r.DelayFrom(now) if delay > 0 { o.mu.Unlock() select { case <-qch: + pmsg.returnToPool() return case <-time.After(delay): } @@ -2817,7 +3091,7 @@ func (o *consumer) loopAndGatherMsgs(qch chan struct{}) { } // Do actual delivery. - o.deliverMsg(dsubj, subj, hdr, msg, seq, dc, ts) + o.deliverMsg(dsubj, pmsg, dc, rp) // Reset our idle heartbeat timer if set. if hb != nil { @@ -2836,7 +3110,8 @@ func (o *consumer) loopAndGatherMsgs(qch chan struct{}) { // Make sure to process any expired requests that are pending. var wrExp <-chan time.Time if o.isPullMode() { - _, _, _, fexp := o.processWaiting() + // Dont expire oneshots if we are here because of max ack pending limit. + _, _, _, fexp := o.processWaiting(err != errMaxAckPending) if !fexp.IsZero() { expires := time.Until(fexp) if expires <= 0 { @@ -2851,17 +3126,17 @@ func (o *consumer) loopAndGatherMsgs(qch chan struct{}) { o.mu.Unlock() select { + case <-mch: + // Messages are waiting. case interest := <-inch: // inch can be nil on pull-based, but then this will // just block and not fire. o.updateDeliveryInterest(interest) case <-qch: return - case <-mch: - // Messages are waiting. case <-wrExp: o.mu.Lock() - o.processWaiting() + o.processWaiting(true) o.mu.Unlock() case <-hbc: if o.isActive() { @@ -2901,27 +3176,52 @@ func (o *consumer) setMaxPendingBytes(limit int) { } } -// We have the case where a consumer can become greedy and pick up a messages before the stream has incremented our pending(sgap). -// Instead of trying to slow things down and synchronize we will allow this to wrap and go negative (biggest uint64) for a short time. -// This functions checks for that and returns 0. // Lock should be held. -func (o *consumer) adjustedPending() uint64 { - if o.sgap&(1<<63) != 0 { +func (o *consumer) numPending() uint64 { + if o.npcm == 0 { + o.streamNumPending() + } + // This can wrap based on possibly having a dec before the inc. Account for that here. + if o.npc&(1<<63) != 0 { return 0 } - return o.sgap + return o.npc +} + +// Will force a set from the stream store of num pending. +// Depends on delivery policy, for last per subject we calculate differently. +// Lock should be held. +func (o *consumer) streamNumPending() uint64 { + if o.mset == nil || o.mset.store == nil { + o.npc, o.npcm = 0, 0 + } else if o.cfg.DeliverPolicy == DeliverLastPerSubject { + o.npc, o.npcm = 0, 0 + for _, ss := range o.mset.store.SubjectsState(o.cfg.FilterSubject) { + if o.sseq <= ss.Last { + o.npc++ + if ss.Last > o.npcm { + o.npcm = ss.Last + } + } + } + } else { + ss := o.mset.store.FilteredState(o.sseq, o.cfg.FilterSubject) + o.npc, o.npcm = ss.Msgs, ss.Last + } + return o.npc } // Deliver a msg to the consumer. // Lock should be held and o.mset validated to be non-nil. -func (o *consumer) deliverMsg(dsubj, subj string, hdr, msg []byte, seq, dc uint64, ts int64) { +func (o *consumer) deliverMsg(dsubj string, pmsg *jsPubMsg, dc uint64, rp RetentionPolicy) { if o.mset == nil { + pmsg.returnToPool() return } - // Update pending on first attempt. This can go upside down for a short bit, that is ok. - // See adjustedPending(). - if dc == 1 { - o.sgap-- + + // Update our cached num pending. + if dc == 1 && o.npcm > 0 { + o.npc-- } dseq := o.dseq @@ -2930,6 +3230,7 @@ func (o *consumer) deliverMsg(dsubj, subj string, hdr, msg []byte, seq, dc uint6 // If headers only do not send msg payload. // Add in msg size itself as header. if o.cfg.HeadersOnly { + hdr, msg := pmsg.hdr, pmsg.msg var bb bytes.Buffer if len(hdr) == 0 { bb.WriteString(hdrLine) @@ -2942,12 +3243,17 @@ func (o *consumer) deliverMsg(dsubj, subj string, hdr, msg []byte, seq, dc uint6 bb.WriteString(strconv.FormatInt(int64(len(msg)), 10)) bb.WriteString(CR_LF) bb.WriteString(CR_LF) - hdr = bb.Bytes() + // Replace underlying buf which we can use directly when we send. + // TODO(dlc) - Probably just use directly when forming bytes.Buffer? + pmsg.buf = pmsg.buf[:0] + pmsg.buf = append(pmsg.buf, bb.Bytes()...) + // Replace with new header. + pmsg.hdr = pmsg.buf // Cancel msg payload - msg = nil + pmsg.msg = nil } - pmsg := newJSPubMsg(dsubj, subj, o.ackReply(seq, dseq, dc, ts, o.adjustedPending()), hdr, msg, o, seq) + pmsg.dsubj, pmsg.reply, pmsg.o = dsubj, o.ackReply(pmsg.seq, dseq, dc, pmsg.ts, o.numPending()), o psz := pmsg.size() if o.maxpb > 0 { @@ -2957,6 +3263,8 @@ func (o *consumer) deliverMsg(dsubj, subj string, hdr, msg []byte, seq, dc uint6 mset := o.mset ap := o.cfg.AckPolicy + // Cant touch pmsg after this sending so capture what we need. + seq, ts := pmsg.seq, pmsg.ts // Send message. o.outq.send(pmsg) @@ -2976,7 +3284,7 @@ func (o *consumer) deliverMsg(dsubj, subj string, hdr, msg []byte, seq, dc uint6 o.updateDelivered(dseq, seq, dc, ts) // If we are ack none and mset is interest only we should make sure stream removes interest. - if ap == AckNone && mset.cfg.Retention != LimitsPolicy { + if ap == AckNone && rp != LimitsPolicy { if o.node == nil || o.cfg.Direct { mset.ackq.push(seq) } else { @@ -3174,13 +3482,19 @@ func (o *consumer) checkPending() { defer o.mu.Unlock() mset := o.mset - if mset == nil { + // On stop, mset and timer will be nil. + if mset == nil || o.ptmr == nil { return } now := time.Now().UnixNano() ttl := int64(o.cfg.AckWait) next := int64(o.ackWait(0)) + // However, if there is backoff, initializes with the largest backoff. + // It will be adjusted as needed. + if l := len(o.cfg.BackOff); l > 0 { + next = int64(o.cfg.BackOff[l-1]) + } var shouldUpdateState bool var state StreamState @@ -3190,7 +3504,12 @@ func (o *consumer) checkPending() { // Since we can update timestamps, we have to review all pending. // We may want to unlock here or warn if list is big. var expired []uint64 + check := len(o.pending) > 1024 for seq, p := range o.pending { + if check && atomic.LoadInt64(&o.awl) > 0 { + o.ptmr.Reset(100 * time.Millisecond) + return + } // Check if these are no longer valid. if seq < fseq { delete(o.pending, seq) @@ -3200,12 +3519,20 @@ func (o *consumer) checkPending() { continue } elapsed, deadline := now-p.Timestamp, ttl - if len(o.cfg.BackOff) > 0 && o.rdc != nil { + if len(o.cfg.BackOff) > 0 { + // This is ok even if o.rdc is nil, we would get dc == 0, which is what we want. dc := int(o.rdc[seq]) - if dc >= len(o.cfg.BackOff) { + // This will be the index for the next backoff, will set to last element if needed. + nbi := dc + 1 + if dc+1 >= len(o.cfg.BackOff) { dc = len(o.cfg.BackOff) - 1 + nbi = dc } deadline = int64(o.cfg.BackOff[dc]) + // Set `next` to the next backoff (if smaller than current `next` value). + if nextBackoff := int64(o.cfg.BackOff[nbi]); nextBackoff < next { + next = nextBackoff + } } if elapsed >= deadline { if !o.onRedeliverQueue(seq) { @@ -3379,6 +3706,7 @@ func (o *consumer) selectStartingSeqNo() { // TODO(dlc) - Once clustered can't rely on this. o.sseq = o.mset.store.GetSeqFromTime(*o.cfg.OptStartTime) } else { + // DeliverNew o.sseq = state.LastSeq + 1 } } else { @@ -3400,6 +3728,11 @@ func (o *consumer) selectStartingSeqNo() { o.adflr = o.dseq - 1 // Set ack store floor to store-1 o.asflr = o.sseq - 1 + + // Set our starting sequence state. + if o.store != nil && o.sseq > 0 { + o.store.SetStarting(o.sseq - 1) + } } // Test whether a config represents a durable subscriber. @@ -3466,14 +3799,14 @@ func (o *consumer) hasNoLocalInterest() bool { // This is when the underlying stream has been purged. // sseq is the new first seq for the stream after purge. // Lock should be held. -func (o *consumer) purge(sseq uint64) { +func (o *consumer) purge(sseq uint64, slseq uint64) { // Do not update our state unless we know we are the leader. if !o.isLeader() { return } // Signals all have been purged for this consumer. if sseq == 0 { - sseq = o.mset.lastSeq() + 1 + sseq = slseq + 1 } o.mu.Lock() @@ -3487,7 +3820,6 @@ func (o *consumer) purge(sseq uint64) { o.adflr = o.dseq - 1 } } - o.sgap = 0 o.pending = nil // We need to remove all those being queued for redelivery under o.rdq @@ -3583,6 +3915,7 @@ func (o *consumer) stopWithFlags(dflag, sdflag, doSignal, advisory bool) error { } n := o.node qgroup := o.cfg.DeliverGroup + o.ackMsgs.unregister() o.mu.Unlock() if c != nil { @@ -3608,7 +3941,7 @@ func (o *consumer) stopWithFlags(dflag, sdflag, doSignal, advisory bool) error { stop := mset.lastSeq() o.mu.Lock() if !o.isLeader() { - o.readStoredState() + o.readStoredState(stop) } start := o.asflr o.mu.Unlock() @@ -3655,11 +3988,8 @@ func (o *consumer) stopWithFlags(dflag, sdflag, doSignal, advisory bool) error { // Check that we do not form a cycle by delivering to a delivery subject // that is part of the interest group. -func (mset *stream) deliveryFormsCycle(deliverySubject string) bool { - mset.mu.RLock() - defer mset.mu.RUnlock() - - for _, subject := range mset.cfg.Subjects { +func deliveryFormsCycle(cfg *StreamConfig, deliverySubject string) bool { + for _, subject := range cfg.Subjects { if subjectIsSubsetMatch(deliverySubject, subject) { return true } @@ -3737,65 +4067,11 @@ func (o *consumer) requestNextMsgSubject() string { return o.nextMsgSubj } -// Will set the initial pending and start sequence. -// mset lock should be held. -func (o *consumer) setInitialPendingAndStart() { - mset := o.mset - if mset == nil || mset.store == nil { - return - } - - // !filtered means we want all messages. - filtered, dp := o.cfg.FilterSubject != _EMPTY_, o.cfg.DeliverPolicy - if filtered { - // Check to see if we directly match the configured stream. - // Many clients will always send a filtered subject. - cfg := &mset.cfg - if len(cfg.Subjects) == 1 && cfg.Subjects[0] == o.cfg.FilterSubject { - filtered = false - } - } - - if !filtered && dp != DeliverLastPerSubject { - var state StreamState - mset.store.FastState(&state) - if state.Msgs > 0 { - o.sgap = state.Msgs - (o.sseq - state.FirstSeq) - o.lsgap = state.LastSeq - } - } else { - // Here we are filtered. - if dp == DeliverLastPerSubject && o.hasSkipListPending() && o.sseq < o.lss.resume { - ss := mset.store.FilteredState(o.lss.resume+1, o.cfg.FilterSubject) - o.sseq = o.lss.seqs[0] - o.sgap = ss.Msgs + uint64(len(o.lss.seqs)) - o.lsgap = ss.Last - } else if ss := mset.store.FilteredState(o.sseq, o.cfg.FilterSubject); ss.Msgs > 0 { - o.sgap = ss.Msgs - o.lsgap = ss.Last - // See if we should update our starting sequence. - if dp == DeliverLast || dp == DeliverLastPerSubject { - o.sseq = ss.Last - } else if dp == DeliverNew { - o.sseq = ss.Last + 1 - } else { - // DeliverAll, DeliverByStartSequence, DeliverByStartTime - o.sseq = ss.First - } - // Cleanup lss when we take over in clustered mode. - if dp == DeliverLastPerSubject && o.hasSkipListPending() && o.sseq >= o.lss.resume { - o.lss = nil - } - } - o.updateSkipped() - } -} - func (o *consumer) decStreamPending(sseq uint64, subj string) { o.mu.Lock() - // Ignore if we have already seen this one. - if sseq >= o.sseq && o.sgap > 0 && o.isFilteredMatch(subj) { - o.sgap-- + // Update our cached num pending. Only do so if we think deliverMsg has not done so. + if sseq > o.npcm && sseq >= o.sseq && o.isFilteredMatch(subj) { + o.npc-- } // Check if this message was pending. p, wasPending := o.pending[sseq] diff --git a/vendor/github.com/nats-io/nats-server/v2/server/dirstore.go b/vendor/github.com/nats-io/nats-server/v2/server/dirstore.go index b0d82ea6..9bb499fd 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/dirstore.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/dirstore.go @@ -28,6 +28,8 @@ import ( "sync" "time" + "github.com/nats-io/nkeys" + "github.com/nats-io/jwt/v2" // only used to decode, not for storage ) @@ -321,6 +323,9 @@ func (store *DirJWTStore) Merge(pack string) error { return fmt.Errorf("line in package didn't contain 2 entries: %q", line) } pubKey := split[0] + if !nkeys.IsValidPublicAccountKey(pubKey) { + return fmt.Errorf("key to merge is not a valid public account key") + } if err := store.saveIfNewer(pubKey, split[1]); err != nil { return err } @@ -370,6 +375,9 @@ func (store *DirJWTStore) pathForKey(publicKey string) string { if len(publicKey) < 2 { return _EMPTY_ } + if !nkeys.IsValidPublicKey(publicKey) { + return _EMPTY_ + } fileName := fmt.Sprintf("%s%s", publicKey, fileExtension) if store.shard { last := publicKey[len(publicKey)-2:] @@ -488,7 +496,7 @@ func (store *DirJWTStore) save(publicKey string, theJWT string) error { } // Assumes the lock is NOT held, and only updates if the jwt is new, or the one on disk is older -// returns true when the jwt changed +// When changed, invokes jwt changed callback func (store *DirJWTStore) saveIfNewer(publicKey string, theJWT string) error { if store.readonly { return fmt.Errorf("store is read-only") @@ -505,7 +513,7 @@ func (store *DirJWTStore) saveIfNewer(publicKey string, theJWT string) error { } if _, err := os.Stat(path); err == nil { if newJWT, err := jwt.DecodeGeneric(theJWT); err != nil { - // skip if it can't be decoded + return err } else if existing, err := ioutil.ReadFile(path); err != nil { return err } else if existingJWT, err := jwt.DecodeGeneric(string(existing)); err != nil { @@ -514,6 +522,10 @@ func (store *DirJWTStore) saveIfNewer(publicKey string, theJWT string) error { return nil } else if existingJWT.IssuedAt > newJWT.IssuedAt { return nil + } else if newJWT.Subject != publicKey { + return fmt.Errorf("jwt subject nkey and provided nkey do not match") + } else if existingJWT.Subject != newJWT.Subject { + return fmt.Errorf("subject of existing and new jwt do not match") } } store.Lock() diff --git a/vendor/github.com/nats-io/nats-server/v2/server/errors.go b/vendor/github.com/nats-io/nats-server/v2/server/errors.go index 1dcbe8d9..56b5af3f 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/errors.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/errors.go @@ -46,6 +46,9 @@ var ( // ErrBadSubject represents an error condition for an invalid subject. ErrBadSubject = errors.New("invalid subject") + // ErrBadSubjectMappingDestination is used to error on a bad transform destination mapping + ErrBadSubjectMappingDestination = errors.New("invalid subject mapping destination") + // ErrBadQualifier is used to error on a bad qualifier for a transform. ErrBadQualifier = errors.New("bad qualifier") @@ -181,6 +184,13 @@ var ( // ErrCertNotPinned is returned when pinned certs are set and the certificate is not in it ErrCertNotPinned = errors.New("certificate not pinned") + + // ErrDuplicateServerName is returned when processing a server remote connection and + // the server reports that this server name is already used in the cluster. + ErrDuplicateServerName = errors.New("duplicate server name") + + // ErrMinimumVersionRequired is returned when a connection is not at the minimum version required. + ErrMinimumVersionRequired = errors.New("minimum version required") ) // configErr is a configuration error. diff --git a/vendor/github.com/nats-io/nats-server/v2/server/errors.json b/vendor/github.com/nats-io/nats-server/v2/server/errors.json index b520415c..1192377b 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/errors.json +++ b/vendor/github.com/nats-io/nats-server/v2/server/errors.json @@ -51,7 +51,7 @@ }, { "constant": "JSStreamSubjectOverlapErr", - "code": 500, + "code": 400, "error_code": 10065, "description": "subjects overlap with an existing stream", "comment": "", @@ -293,7 +293,7 @@ "constant": "JSMirrorWithSubjectsErr", "code": 400, "error_code": 10034, - "description": "stream mirrors can not also contain subjects", + "description": "stream mirrors can not contain subjects", "comment": "", "help": "", "url": "", @@ -333,7 +333,7 @@ "constant": "JSStreamMirrorNotUpdatableErr", "code": 400, "error_code": 10055, - "description": "Mirror configuration can not be updated", + "description": "stream mirror configuration can not be updated", "comment": "", "help": "", "url": "", @@ -404,7 +404,7 @@ "code": 500, "error_code": 10029, "description": "{err}", - "comment": "Generic mirror consumer setup failure string", + "comment": "generic mirror consumer setup failure string", "help": "", "url": "", "deprecates": "" @@ -1158,5 +1158,95 @@ "help": "", "url": "", "deprecates": "" + }, + { + "constant": "JSStreamOfflineErr", + "code": 500, + "error_code": 10118, + "description": "stream is offline", + "comment": "", + "help": "", + "url": "", + "deprecates": "" + }, + { + "constant": "JSConsumerOfflineErr", + "code": 500, + "error_code": 10119, + "description": "consumer is offline", + "comment": "", + "help": "", + "url": "", + "deprecates": "" + }, + { + "constant": "JSNoLimitsErr", + "code": 400, + "error_code": 10120, + "description": "no JetStream default or applicable tiered limit present", + "comment": "", + "help": "", + "url": "", + "deprecates": "" + }, + { + "constant": "JSConsumerMaxPendingAckExcessErrF", + "code": 400, + "error_code": 10121, + "description": "consumer max ack pending exceeds system limit of {limit}", + "comment": "", + "help": "", + "url": "", + "deprecates": "" + }, + { + "constant": "JSStreamMaxStreamBytesExceeded", + "code": 400, + "error_code": 10122, + "description": "stream max bytes exceeds account limit max stream bytes", + "comment": "", + "help": "", + "url": "", + "deprecates": "" + }, + { + "constant": "JSStreamMoveAndScaleErr", + "code": 400, + "error_code": 10123, + "description": "can not move and scale a stream in a single update", + "comment": "", + "help": "", + "url": "", + "deprecates": "" + }, + { + "constant": "JSStreamMoveInProgress", + "code": 400, + "error_code": 10124, + "description": "stream move already in progress", + "comment": "", + "help": "", + "url": "", + "deprecates": "" + }, + { + "constant": "JSConsumerMaxRequestBatchExceededF", + "code": 400, + "error_code": 10125, + "description": "consumer max request batch exceeds server limit of {limit}", + "comment": "", + "help": "", + "url": "", + "deprecates": "" + }, + { + "constant": "JSConsumerReplicasExceedsStream", + "code": 400, + "error_code": 10126, + "description": "consumer config replica count exceeds parent stream", + "comment": "", + "help": "", + "url": "", + "deprecates": "" } -] +] \ No newline at end of file diff --git a/vendor/github.com/nats-io/nats-server/v2/server/events.go b/vendor/github.com/nats-io/nats-server/v2/server/events.go index e519b1e7..bec38180 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/events.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/events.go @@ -433,6 +433,7 @@ RESET: // there is a chance that the process will exit before the // writeLoop has a chance to send it. c.flushClients(time.Second) + sendq.recycle(&msgs) return } pm.returnToPool() @@ -690,6 +691,9 @@ func (s *Server) sendStatsz(subj string) { ni := v.(nodeInfo) ni.stats = jStat.Stats ni.cfg = jStat.Config + s.optsMu.RLock() + ni.tags = copyStrings(s.opts.Tags) + s.optsMu.RUnlock() s.nodeToInfo.Store(ourNode, ni) } // Metagroup info. @@ -1009,8 +1013,8 @@ func (s *Server) addSystemAccountExports(sacc *Account) { if err := sacc.AddServiceExport(accSubsSubj, nil); err != nil { s.Errorf("Error adding system service export for %q: %v", accSubsSubj, err) } - - if s.JetStreamEnabled() { + // in case of a mixed mode setup, enable js exports anyway + if s.JetStreamEnabled() || !s.standAloneMode() { s.checkJetStreamExports() } } @@ -1573,12 +1577,12 @@ func (s *Server) registerSystemImports(a *Account) { // Add in this to the account in 2 places. // "$SYS.REQ.SERVER.PING.CONNZ" and "$SYS.REQ.ACCOUNT.PING.CONNZ" - if _, ok := a.imports.services[connzSubj]; !ok { + if !a.serviceImportExists(connzSubj) { if err := a.AddServiceImport(sacc, connzSubj, mappedSubj); err != nil { s.Errorf("Error setting up system service imports for account: %v", err) } } - if _, ok := a.imports.services[accConnzReqSubj]; !ok { + if !a.serviceImportExists(accConnzReqSubj) { if err := a.AddServiceImport(sacc, accConnzReqSubj, mappedSubj); err != nil { s.Errorf("Error setting up system service imports for account: %v", err) } diff --git a/vendor/github.com/nats-io/nats-server/v2/server/filestore.go b/vendor/github.com/nats-io/nats-server/v2/server/filestore.go index d23cdf59..f3a30662 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/filestore.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/filestore.go @@ -32,6 +32,7 @@ import ( "path/filepath" "runtime" "sort" + "strings" "sync" "sync/atomic" "time" @@ -92,7 +93,7 @@ type fileStore struct { psmc map[string]uint64 hh hash.Hash64 qch chan struct{} - cfs []*consumerFileStore + cfs []ConsumerStore sips int closed bool fip bool @@ -120,6 +121,7 @@ type msgBlock struct { rbytes uint64 // Total bytes (raw) including deleted. Used for rolling to new blk. msgs uint64 // User visible message count. fss map[string]*SimpleState + sfilter string // Single subject filter sfn string kfn string lwits int64 @@ -150,6 +152,7 @@ type cache struct { idx []uint32 lrl uint32 fseq uint64 + nra bool } type msgId struct { @@ -157,16 +160,6 @@ type msgId struct { ts int64 } -type fileStoredMsg struct { - subj string - hdr []byte - msg []byte - seq uint64 - ts int64 // nanoseconds - mb *msgBlock - off int64 // offset into block file -} - const ( // Magic is used to identify the file store files. magic = uint8(22) @@ -225,22 +218,28 @@ const ( blkKeySize = 72 // Default stream block size. - defaultStreamBlockSize = 16 * 1024 * 1024 // 16MB + defaultLargeBlockSize = 8 * 1024 * 1024 // 8MB // Default for workqueue or interest based. - defaultOtherBlockSize = 8 * 1024 * 1024 // 8MB + defaultMediumBlockSize = 4 * 1024 * 1024 // 4MB + // For smaller reuse buffers. Usually being generated during contention on the lead write buffer. + // E.g. mirrors/sources etc. + defaultSmallBlockSize = 1 * 1024 * 1024 // 1MB // Default for KV based - defaultKVBlockSize = 8 * 1024 * 1024 // 8MB + defaultKVBlockSize = defaultMediumBlockSize // max block size for now. - maxBlockSize = defaultStreamBlockSize + maxBlockSize = defaultLargeBlockSize // Compact minimum threshold. compactMinimum = 2 * 1024 * 1024 // 2MB // FileStoreMinBlkSize is minimum size we will do for a blk size. FileStoreMinBlkSize = 32 * 1000 // 32kib // FileStoreMaxBlkSize is maximum size we will do for a blk size. FileStoreMaxBlkSize = maxBlockSize - // Check for bad record length value due to corrupt data. rlBadThresh = 32 * 1024 * 1024 + // Time threshold to write index info. + wiThresh = int64(2 * time.Second) + // Time threshold to write index info for non FIFO cases + winfThresh = int64(500 * time.Millisecond) ) func newFileStore(fcfg FileStoreConfig, cfg StreamConfig) (*fileStore, error) { @@ -376,6 +375,18 @@ func (fs *fileStore) UpdateConfig(cfg *StreamConfig) error { fs.ageChk.Stop() fs.ageChk = nil } + + // Update our sfilter for the last block. + if lmb := fs.lmb; lmb != nil { + lmb.mu.Lock() + if len(fs.cfg.Subjects) == 1 { + lmb.sfilter = fs.cfg.Subjects[0] + } else { + lmb.sfilter = _EMPTY_ + } + lmb.mu.Unlock() + } + fs.mu.Unlock() if cfg.MaxAge != 0 { @@ -391,21 +402,22 @@ func dynBlkSize(retention RetentionPolicy, maxBytes int64) uint64 { if m := blkSize % 100; m != 0 { blkSize += 100 - m } - if blkSize < FileStoreMinBlkSize { + if blkSize <= FileStoreMinBlkSize { blkSize = FileStoreMinBlkSize - } - if blkSize > FileStoreMaxBlkSize { + } else if blkSize >= FileStoreMaxBlkSize { blkSize = FileStoreMaxBlkSize + } else { + blkSize = defaultMediumBlockSize } return uint64(blkSize) } if retention == LimitsPolicy { // TODO(dlc) - Make the blocksize relative to this if set. - return defaultStreamBlockSize + return defaultLargeBlockSize } else { // TODO(dlc) - Make the blocksize relative to this if set. - return defaultOtherBlockSize + return defaultMediumBlockSize } } @@ -490,6 +502,57 @@ func (fs *fileStore) writeStreamMeta() error { return nil } +// Pools to recycle the blocks to help with memory pressure. +var blkPoolBig sync.Pool // 16MB +var blkPoolMedium sync.Pool // 8MB +var blkPoolSmall sync.Pool // 2MB + +// Get a new msg block based on sz estimate. +func getMsgBlockBuf(sz int) (buf []byte) { + var pb interface{} + if sz <= defaultSmallBlockSize { + pb = blkPoolSmall.Get() + } else if sz <= defaultMediumBlockSize { + pb = blkPoolMedium.Get() + } else { + pb = blkPoolBig.Get() + } + if pb != nil { + buf = *(pb.(*[]byte)) + } else { + // Here we need to make a new blk. + // If small leave as is.. + if sz > defaultSmallBlockSize && sz <= defaultMediumBlockSize { + sz = defaultMediumBlockSize + } else if sz > defaultMediumBlockSize { + sz = defaultLargeBlockSize + } + buf = make([]byte, sz) + } + return buf[:0] +} + +// Recycle the msg block. +func recycleMsgBlockBuf(buf []byte) { + if buf == nil || cap(buf) < defaultSmallBlockSize { + return + } + // Make sure to reset before placing back into pool. + buf = buf[:0] + + // We need to make sure the load code gets a block that can fit the maximum for a size block. + // E.g. 8, 16 etc. otherwise we thrash and actually make things worse by pulling it out, and putting + // it right back in and making a new []byte. + // From above we know its already >= defaultSmallBlockSize + if sz := cap(buf); sz < defaultMediumBlockSize { + blkPoolSmall.Put(&buf) + } else if sz < defaultLargeBlockSize { + blkPoolMedium.Put(&buf) + } else { + blkPoolBig.Put(&buf) + } +} + const ( msgHdrSize = 22 checksumSize = 8 @@ -499,6 +562,7 @@ const ( // This is the max room needed for index header. const indexHdrSize = 7*binary.MaxVarintLen64 + hdrLen + checksumSize +// Lock held on entry func (fs *fileStore) recoverMsgBlock(fi os.FileInfo, index uint64) (*msgBlock, error) { mb := &msgBlock{fs: fs, index: index, cexp: fs.fcfg.CacheExpire} @@ -599,20 +663,28 @@ func (fs *fileStore) recoverMsgBlock(fi os.FileInfo, index uint64) (*msgBlock, e } // Grab last checksum from main block file. var lchk [8]byte - file.ReadAt(lchk[:], fi.Size()-8) + if mb.rbytes >= checksumSize { + file.ReadAt(lchk[:], fi.Size()-checksumSize) + } file.Close() // Read our index file. Use this as source of truth if possible. if err := mb.readIndexInfo(); err == nil { // Quick sanity check here. - // Note this only checks that the message blk file is not newer then this file. - if bytes.Equal(lchk[:], mb.lchk[:]) { + // Note this only checks that the message blk file is not newer then this file, or is empty and we expect empty. + if (mb.rbytes == 0 && mb.msgs == 0) || bytes.Equal(lchk[:], mb.lchk[:]) { if fs.tms { if err = mb.readPerSubjectInfo(); err != nil { return nil, err } } fs.blks = append(fs.blks, mb) + // If we only have one subject registered we can optimize filtered lookups here. + if len(mb.fss) == 1 { + for sfilter := range mb.fss { + mb.sfilter = sfilter + } + } return mb, nil } } @@ -784,7 +856,10 @@ func (mb *msgBlock) rebuildStateLocked() (*LostStreamData, error) { // This is an old erased message, or a new one that we can track. if seq == 0 || seq&ebit != 0 || seq < mb.first.seq { seq = seq &^ ebit - addToDmap(seq) + // Only add to dmap if past recorded first seq and non-zero. + if seq != 0 && seq >= mb.first.seq { + addToDmap(seq) + } index += rl mb.last.seq = seq mb.last.ts = ts @@ -834,14 +909,16 @@ func (mb *msgBlock) rebuildStateLocked() (*LostStreamData, error) { mb.bytes += uint64(rl) // Do per subject info. - if mb.fss != nil { - if subj := string(data[:slen]); len(subj) > 0 { - if ss := mb.fss[subj]; ss != nil { - ss.Msgs++ - ss.Last = seq - } else { - mb.fss[subj] = &SimpleState{Msgs: 1, First: seq, Last: seq} - } + if slen > 0 && mb.fss != nil { + // For the lookup, we cast the byte slice and there won't be any copy + if ss := mb.fss[string(data[:slen])]; ss != nil { + ss.Msgs++ + ss.Last = seq + } else { + // This will either use a subject from the config, or make a copy + // so we don't reference the underlying buffer. + subj := mb.subjString(data[:slen]) + mb.fss[subj] = &SimpleState{Msgs: 1, First: seq, Last: seq} } } } @@ -854,6 +931,13 @@ func (mb *msgBlock) rebuildStateLocked() (*LostStreamData, error) { mb.last.seq = mb.first.seq - 1 } + // If we only have one subject registered we can optimize filtered lookups here. + if len(mb.fss) == 1 { + for sfilter := range mb.fss { + mb.sfilter = sfilter + } + } + return nil, nil } @@ -905,6 +989,12 @@ func (fs *fileStore) recoverMsgs() error { if len(fs.blks) > 0 { sort.Slice(fs.blks, func(i, j int) bool { return fs.blks[i].index < fs.blks[j].index }) fs.lmb = fs.blks[len(fs.blks)-1] + // Update our sfilter for the last block since we could have only see one subject during recovery. + if len(fs.cfg.Subjects) == 1 { + fs.lmb.sfilter = fs.cfg.Subjects[0] + } else { + fs.lmb.sfilter = _EMPTY_ + } } else { _, err = fs.newMsgBlockForWrite() } @@ -993,9 +1083,11 @@ func (fs *fileStore) expireMsgsOnRecover() { break } + var smv StoreMsg + // Walk messages and remove if expired. for seq := mb.first.seq; seq <= mb.last.seq; seq++ { - sm, err := mb.cacheLookup(seq) + sm, err := mb.cacheLookup(seq, &smv) // Process interior deleted msgs. if err == errDeletedMsg { // Update dmap. @@ -1005,10 +1097,14 @@ func (fs *fileStore) expireMsgsOnRecover() { mb.dmap = nil } } + // Keep this update just in case since we are removing dmap entries. + mb.first.seq = seq continue } // Break on other errors. if err != nil || sm == nil { + // Keep this update just in case since we could have removed dmap entries. + mb.first.seq = seq break } @@ -1023,14 +1119,16 @@ func (fs *fileStore) expireMsgsOnRecover() { } // Delete the message here. - sz := fileStoreMsgSize(sm.subj, sm.hdr, sm.msg) - mb.bytes -= sz - bytes += sz - mb.msgs-- - purged++ + if mb.msgs > 0 { + sz := fileStoreMsgSize(sm.subj, sm.hdr, sm.msg) + mb.bytes -= sz + bytes += sz + mb.msgs-- + purged++ + } // Update fss fs.removePerSubject(sm.subj) - mb.removeSeqPerSubject(sm.subj, seq) + mb.removeSeqPerSubject(sm.subj, seq, &smv) } // Check if empty after processing, could happen if tail of messages are all deleted. @@ -1106,10 +1204,12 @@ func (fs *fileStore) GetSeqFromTime(t time.Time) uint64 { lseq := mb.last.seq mb.mu.RUnlock() + var smv StoreMsg + // Linear search, hence the dumb part.. ts := t.UnixNano() for seq := fseq; seq <= lseq; seq++ { - sm, _, _ := mb.fetchMsg(seq) + sm, _, _ := mb.fetchMsg(seq, &smv) if sm != nil && sm.ts >= ts { return sm.seq } @@ -1118,32 +1218,40 @@ func (fs *fileStore) GetSeqFromTime(t time.Time) uint64 { } // Find the first matching message. -func (mb *msgBlock) firstMatching(filter string, wc bool, start uint64) (*fileStoredMsg, bool, error) { +func (mb *msgBlock) firstMatching(filter string, wc bool, start uint64, sm *StoreMsg) (*StoreMsg, bool, error) { mb.mu.Lock() defer mb.mu.Unlock() - isAll, subs := filter == _EMPTY_ || filter == fwcs, []string{filter} - // If we have a wildcard match against all tracked subjects we know about. - if wc || isAll { - subs = subs[:0] - for subj := range mb.fss { - if isAll || subjectIsSubsetMatch(subj, filter) { - subs = append(subs, subj) + fseq, isAll, subs := start, filter == _EMPTY_ || filter == mb.sfilter || filter == fwcs, []string{filter} + + // Skip scan of mb.fss is number of messages in the block are less than + // 1/2 the number of subjects in mb.fss. + doLinearScan := isAll || 2*int(mb.last.seq-start) < len(mb.fss) + + if !doLinearScan { + // If we have a wildcard match against all tracked subjects we know about. + if wc { + subs = subs[:0] + for subj := range mb.fss { + if subjectIsSubsetMatch(subj, filter) { + subs = append(subs, subj) + } } } - } - fseq := mb.last.seq + 1 - for _, subj := range subs { - ss := mb.fss[subj] - if ss == nil || start > ss.Last || ss.First >= fseq { - continue - } - if ss.First < start { - fseq = start - } else { - fseq = ss.First + fseq = mb.last.seq + 1 + for _, subj := range subs { + ss := mb.fss[subj] + if ss == nil || start > ss.Last || ss.First >= fseq { + continue + } + if ss.First < start { + fseq = start + } else { + fseq = ss.First + } } } + if fseq > mb.last.seq { return nil, false, ErrStoreMsgNotFound } @@ -1154,19 +1262,31 @@ func (mb *msgBlock) firstMatching(filter string, wc bool, start uint64) (*fileSt } } + if sm == nil { + sm = new(StoreMsg) + } + for seq := fseq; seq <= mb.last.seq; seq++ { llseq := mb.llseq - sm, err := mb.cacheLookup(seq) + fsm, err := mb.cacheLookup(seq, sm) if err != nil { continue } - expireOk := seq == mb.last.seq && mb.llseq == seq-1 - if len(subs) == 1 && sm.subj == subs[0] { - return sm, expireOk, nil - } - for _, subj := range subs { - if sm.subj == subj { - return sm, expireOk, nil + expireOk := seq == mb.last.seq && mb.llseq == seq + if doLinearScan { + if isAll { + return fsm, expireOk, nil + } + if wc && subjectIsSubsetMatch(fsm.subj, filter) { + return fsm, expireOk, nil + } else if !wc && fsm.subj == filter { + return fsm, expireOk, nil + } + } else { + for _, subj := range subs { + if fsm.subj == subj { + return fsm, expireOk, nil + } } } // If we are here we did not match, so put the llseq back. @@ -1258,8 +1378,10 @@ func (mb *msgBlock) filteredPendingLocked(filter string, wc bool, seq uint64) (t } numScanIn, numScanOut := lseq-seq, seq-mb.first.seq + var smv StoreMsg + isMatch := func(seq uint64) bool { - if sm, _ := mb.cacheLookup(seq); sm != nil { + if sm, _ := mb.cacheLookup(seq, &smv); sm != nil { if len(subs) == 1 && sm.subj == subs[0] { return true } @@ -1361,9 +1483,10 @@ func (fs *fileStore) FilteredState(sseq uint64, subj string) SimpleState { } } else { // Fallback to linear scan. + var smv StoreMsg eq := compareFn(subj) for seq := sseq; seq <= lseq; seq++ { - if sm, _ := fs.msgForSeq(seq); sm != nil && eq(sm.subj, subj) { + if sm, _ := fs.msgForSeq(seq, &smv); sm != nil && eq(sm.subj, subj) { ss.Msgs++ if ss.First == 0 { ss.First = seq @@ -1449,6 +1572,7 @@ func (mb *msgBlock) setupWriteCache(buf []byte) { if mb.cache != nil { return } + // Setup simple cache. mb.cache = &cache{buf: buf} // Make sure we set the proper cache offset if we have existing data. @@ -1484,14 +1608,7 @@ func (fs *fileStore) newMsgBlockForWrite() (*msgBlock, error) { lmb.closeFDsLocked() if lmb.cache != nil { // Reset write timestamp and see if we can expire this cache. - lwts, buf, llts := lmb.lwts, lmb.cache.buf, lmb.llts - lmb.lwts = 0 - lmb.expireCacheLocked() - lmb.lwts = lwts - // We could check for a certain time since last load, but to be safe just reuse if no loads at all. - if llts == 0 && (lmb.cache == nil || lmb.cache.buf == nil) { - rbuf = buf[:0] - } + rbuf = lmb.tryExpireWriteCache() } lmb.mu.Unlock() } @@ -1499,6 +1616,11 @@ func (fs *fileStore) newMsgBlockForWrite() (*msgBlock, error) { mb := &msgBlock{fs: fs, index: index, cexp: fs.fcfg.CacheExpire} + // If we only have one subject registered we can optimize filtered lookups here. + if len(fs.cfg.Subjects) == 1 { + mb.sfilter = fs.cfg.Subjects[0] + } + // Lock should be held to quiet race detector. mb.mu.Lock() mb.setupWriteCache(rbuf) @@ -1594,11 +1716,13 @@ func (fs *fileStore) storeRawMsg(subj string, hdr, msg []byte, seq uint64, ts in return ErrStoreClosed } + var pscheck bool + var asl bool // Check if we are discarding new messages when we reach the limit. if fs.cfg.Discard == DiscardNew { - var asl bool var fseq uint64 if fs.cfg.MaxMsgsPer > 0 && len(subj) > 0 { + pscheck = true var msgs uint64 if msgs, fseq, _ = fs.perSubjectState(subj); msgs >= uint64(fs.cfg.MaxMsgsPer) { asl = true @@ -1649,7 +1773,9 @@ func (fs *fileStore) storeRawMsg(subj string, hdr, msg []byte, seq uint64, ts in // Enforce per message limits. if fs.cfg.MaxMsgsPer > 0 && len(subj) > 0 { - fs.enforcePerSubjectLimit(subj) + if !pscheck || asl { + fs.enforcePerSubjectLimit(subj) + } } // Limits checks and enforcement. @@ -1708,26 +1834,30 @@ func (mb *msgBlock) skipMsg(seq uint64, now time.Time) { } var needsRecord bool + nowts := now.UnixNano() + mb.mu.Lock() // If we are empty can just do meta. if mb.msgs == 0 { mb.last.seq = seq - mb.last.ts = now.UnixNano() + mb.last.ts = nowts mb.first.seq = seq + 1 - mb.first.ts = now.UnixNano() + mb.first.ts = nowts + // Take care of index if needed. + if nowts-mb.lwits > wiThresh { + mb.writeIndexInfoLocked() + } } else { needsRecord = true if mb.dmap == nil { mb.dmap = make(map[uint64]struct{}) } mb.dmap[seq] = struct{}{} - mb.msgs-- - mb.bytes -= emptyRecordLen } mb.mu.Unlock() if needsRecord { - mb.writeMsgRecord(emptyRecordLen, seq|ebit, _EMPTY_, nil, nil, now.UnixNano(), true) + mb.writeMsgRecord(emptyRecordLen, seq|ebit, _EMPTY_, nil, nil, nowts, true) } else { mb.kickFlusher() } @@ -1738,18 +1868,14 @@ func (fs *fileStore) SkipMsg() uint64 { fs.mu.Lock() defer fs.mu.Unlock() - // Grab time. - now := time.Now().UTC() - seq := fs.state.LastSeq + 1 - fs.state.LastSeq = seq - fs.state.LastTime = now + // Grab time and last seq. + now, seq := time.Now().UTC(), fs.state.LastSeq+1 + fs.state.LastSeq, fs.state.LastTime = seq, now if fs.state.Msgs == 0 { - fs.state.FirstSeq = seq - fs.state.FirstTime = now + fs.state.FirstSeq, fs.state.FirstTime = seq, now } if seq == fs.state.FirstSeq { - fs.state.FirstSeq = seq + 1 - fs.state.FirstTime = now + fs.state.FirstSeq, fs.state.FirstTime = seq+1, now } fs.lmb.skipMsg(seq, now) @@ -1881,7 +2007,7 @@ func (fs *fileStore) removeMsg(seq uint64, secure, needFSLock bool) (bool, error mb.mu.Lock() - // See if the sequence numbers is still relevant. + // See if the sequence number is still relevant. if seq < mb.first.seq { mb.mu.Unlock() fsUnlock() @@ -1908,7 +2034,8 @@ func (fs *fileStore) removeMsg(seq uint64, secure, needFSLock bool) (bool, error } } - sm, err := mb.cacheLookup(seq) + var smv StoreMsg + sm, err := mb.cacheLookup(seq, &smv) if err != nil { mb.mu.Unlock() fsUnlock() @@ -1930,9 +2057,7 @@ func (fs *fileStore) removeMsg(seq uint64, secure, needFSLock bool) (bool, error // If we are tracking multiple subjects here make sure we update that accounting. fs.removePerSubject(sm.subj) - mb.removeSeqPerSubject(sm.subj, seq) - - var shouldWriteIndex, firstSeqNeedsUpdate bool + mb.removeSeqPerSubject(sm.subj, seq, &smv) if secure { // Grab record info. @@ -1940,39 +2065,48 @@ func (fs *fileStore) removeMsg(seq uint64, secure, needFSLock bool) (bool, error mb.eraseMsg(seq, int(ri), int(rl)) } - // Optimize for FIFO case. fifo := seq == mb.first.seq + isLastBlock := mb == fs.lmb + isEmpty := mb.msgs == 0 + shouldWriteIndex := !isEmpty + if fifo { mb.selectNextFirst() - if mb.isEmpty() { - fs.removeMsgBlock(mb) - firstSeqNeedsUpdate = seq == fs.state.FirstSeq - } else { - shouldWriteIndex = true + if !isEmpty { + // Can update this one in place. if seq == fs.state.FirstSeq { fs.state.FirstSeq = mb.first.seq // new one. fs.state.FirstTime = time.Unix(0, mb.first.ts).UTC() } } - } else { - // Check if we are empty first, as long as not the last message block. - if notLast := mb != fs.lmb; notLast && mb.msgs == 0 { - fs.removeMsgBlock(mb) - firstSeqNeedsUpdate = seq == fs.state.FirstSeq - } else { - // Out of order delete. - shouldWriteIndex = true - if mb.dmap == nil { - mb.dmap = make(map[uint64]struct{}) - } - mb.dmap[seq] = struct{}{} - // Check if <25% utilization and minimum size met. - if notLast && mb.rbytes > compactMinimum && mb.rbytes>>2 > mb.bytes { + } else if !isEmpty { + // Out of order delete. + if mb.dmap == nil { + mb.dmap = make(map[uint64]struct{}) + } + mb.dmap[seq] = struct{}{} + // Check if <25% utilization and minimum size met. + if mb.rbytes > compactMinimum && !isLastBlock { + // Remove the interior delete records + rbytes := mb.rbytes - uint64(len(mb.dmap)*emptyRecordLen) + if rbytes>>2 > mb.bytes { mb.compact() } } } + var firstSeqNeedsUpdate bool + + // Decide how we want to clean this up. If last block we will hold into index. + if isEmpty { + if isLastBlock { + mb.closeAndKeepIndex() + } else { + fs.removeMsgBlock(mb) + } + firstSeqNeedsUpdate = seq == fs.state.FirstSeq + } + var qch, fch chan struct{} if shouldWriteIndex { qch, fch = mb.qch, mb.fch @@ -1987,22 +2121,26 @@ func (fs *fileStore) removeMsg(seq uint64, secure, needFSLock bool) (bool, error // Check if we need to write the index file and we are flush in place (fip). if shouldWriteIndex && fs.fip { // Check if this is the first message, common during expirations etc. - if !fifo || time.Now().UnixNano()-mb.lwits > int64(2*time.Second) { + threshold := wiThresh + if !fifo { + // For out-of-order deletes, we will have a shorter threshold, but + // still won't write the index for every single delete. + threshold = winfThresh + } + if time.Now().UnixNano()-mb.lwits > threshold { mb.writeIndexInfoLocked() } } mb.mu.Unlock() // Kick outside of lock. - if shouldWriteIndex { - if !fs.fip { - if qch == nil { - mb.spinUpFlushLoop() - } - select { - case fch <- struct{}{}: - default: - } + if !fs.fip && shouldWriteIndex { + if qch == nil { + mb.spinUpFlushLoop() + } + select { + case fch <- struct{}{}: + default: } } @@ -2011,7 +2149,11 @@ func (fs *fileStore) removeMsg(seq uint64, secure, needFSLock bool) (bool, error // we don't lose track of the first sequence. if firstSeqNeedsUpdate { fs.selectNextFirst() - fs.lmb.writeIndexInfo() + // Write out the new first message block if we have one. + if len(fs.blks) > 0 { + fmb := fs.blks[0] + fmb.writeIndexInfo() + } } fs.mu.Unlock() @@ -2037,7 +2179,8 @@ func (fs *fileStore) removeMsg(seq uint64, secure, needFSLock bool) (bool, error // writing new messages. We will silently bail on any issues with the underlying block and let someone else detect. // Write lock needs to be held. func (mb *msgBlock) compact() { - if mb.cacheNotLoaded() { + wasLoaded := mb.cacheAlreadyLoaded() + if !wasLoaded { if err := mb.loadMsgsWithLock(); err != nil { return } @@ -2119,11 +2262,12 @@ func (mb *msgBlock) compact() { // We will write to a new file and mv/rename it in case of failure. mfn := filepath.Join(filepath.Join(mb.fs.fcfg.StoreDir, msgDir), fmt.Sprintf(newScan, mb.index)) - defer os.Remove(mfn) if err := ioutil.WriteFile(mfn, nbuf, defaultFilePerms); err != nil { + os.Remove(mfn) return } if err := os.Rename(mfn, mb.mfn); err != nil { + os.Remove(mfn) return } @@ -2132,7 +2276,11 @@ func (mb *msgBlock) compact() { mb.removeIndexFileLocked() mb.deleteDmap() mb.rebuildStateLocked() - mb.loadMsgsWithLock() + + // If we entered with the msgs loaded make sure to reload them. + if wasLoaded { + mb.loadMsgsWithLock() + } } // Nil out our dmap. @@ -2335,7 +2483,7 @@ func (mb *msgBlock) eraseMsg(seq uint64, ri, rl int) error { } // Truncate this message block to the storedMsg. -func (mb *msgBlock) truncate(sm *fileStoredMsg) (nmsgs, nbytes uint64, err error) { +func (mb *msgBlock) truncate(sm *StoreMsg) (nmsgs, nbytes uint64, err error) { // Make sure we are loaded to process messages etc. if err := mb.loadMsgs(); err != nil { return 0, 0, err @@ -2352,23 +2500,34 @@ func (mb *msgBlock) truncate(sm *fileStoredMsg) (nmsgs, nbytes uint64, err error var purged, bytes uint64 mb.mu.Lock() + checkDmap := len(mb.dmap) > 0 + var smv StoreMsg + for seq := mb.last.seq; seq > sm.seq; seq-- { if checkDmap { if _, ok := mb.dmap[seq]; ok { // Delete and skip to next. delete(mb.dmap, seq) + if len(mb.dmap) == 0 { + mb.dmap = nil + checkDmap = false + } continue } } // We should have a valid msg to calculate removal stats. - _, rl, _, err := mb.slotInfo(int(seq - mb.cache.fseq)) - if err != nil { - mb.mu.Unlock() - return 0, 0, err + if m, err := mb.cacheLookup(seq, &smv); err == nil { + if mb.msgs > 0 { + rl := fileStoreMsgSize(m.subj, m.hdr, m.msg) + mb.msgs-- + mb.bytes -= rl + mb.rbytes -= rl + // For return accounting. + purged++ + bytes += uint64(rl) + } } - purged++ - bytes += uint64(rl) } // Truncate our msgs and close file. @@ -2384,11 +2543,6 @@ func (mb *msgBlock) truncate(sm *fileStoredMsg) (nmsgs, nbytes uint64, err error return 0, 0, fmt.Errorf("failed to truncate msg block %d, file not open", mb.index) } - // Do local mb stat updates. - mb.msgs -= purged - mb.bytes -= bytes - mb.rbytes -= bytes - // Update our last msg. mb.last.seq = sm.seq mb.last.ts = sm.ts @@ -2432,11 +2586,12 @@ func (mb *msgBlock) selectNextFirst() { // Need to get the timestamp. // We will try the cache direct and fallback if needed. - sm, _ := mb.cacheLookup(seq) + var smv StoreMsg + sm, _ := mb.cacheLookup(seq, &smv) if sm == nil { // Slow path, need to unlock. mb.mu.Unlock() - sm, _, _ = mb.fetchMsg(seq) + sm, _, _ = mb.fetchMsg(seq, &smv) mb.mu.Lock() } if sm != nil { @@ -2500,6 +2655,7 @@ func (mb *msgBlock) clearCache() { return } + buf := mb.cache.buf if mb.cache.off == 0 { mb.cache = nil } else { @@ -2508,6 +2664,7 @@ func (mb *msgBlock) clearCache() { mb.cache.idx = nil mb.cache.wp = 0 } + recycleMsgBlockBuf(buf) } // Called to possibly expire a message block cache. @@ -2531,6 +2688,29 @@ func (mb *msgBlock) tryForceExpireCacheLocked() { mb.llts = llts } +// This is for expiration of the write cache, which will be partial with fip. +// So we want to bypass the Pools here. +// Lock should be held. +func (mb *msgBlock) tryExpireWriteCache() []byte { + if mb.cache == nil { + return nil + } + lwts, buf, llts, nra := mb.lwts, mb.cache.buf, mb.llts, mb.cache.nra + mb.lwts, mb.cache.nra = 0, true + mb.expireCacheLocked() + mb.lwts = lwts + if mb.cache != nil { + mb.cache.nra = nra + } + // We could check for a certain time since last load, but to be safe just reuse if no loads at all. + if llts == 0 && (mb.cache == nil || mb.cache.buf == nil) { + // Clear last write time since we now are about to move on to a new lmb. + mb.lwts = 0 + return buf[:0] + } + return nil +} + // Lock should be held. func (mb *msgBlock) expireCacheLocked() { if mb.cache == nil { @@ -2565,6 +2745,9 @@ func (mb *msgBlock) expireCacheLocked() { // If we are here we will at least expire the core msg buffer. // We need to capture offset in case we do a write next before a full load. mb.cache.off += len(mb.cache.buf) + if !mb.cache.nra { + recycleMsgBlockBuf(mb.cache.buf) + } mb.cache.buf = nil mb.cache.wp = 0 @@ -2608,9 +2791,12 @@ func (fs *fileStore) cancelAgeChk() { func (fs *fileStore) expireMsgs() { // We need to delete one by one here and can not optimize for the time being. // Reason is that we need more information to adjust ack pending in consumers. - var sm *fileStoredMsg + var smv StoreMsg + var sm *StoreMsg + fs.mu.RLock() minAge := time.Now().UnixNano() - int64(fs.cfg.MaxAge) - for sm, _ = fs.msgForSeq(0); sm != nil && sm.ts <= minAge; sm, _ = fs.msgForSeq(0) { + fs.mu.RUnlock() + for sm, _ = fs.msgForSeq(0, &smv); sm != nil && sm.ts <= minAge; sm, _ = fs.msgForSeq(0, &smv) { fs.removeMsg(sm.seq, false, true) } @@ -2758,10 +2944,10 @@ func (mb *msgBlock) writeMsgRecord(rl, seq uint64, subj string, mhdr, msg []byte // Set cache timestamp for last store. mb.lwts = ts // Decide if we write index info if flushing in place. - writeIndex := ts-mb.lwits > int64(2*time.Second) + writeIndex := ts-mb.lwits > wiThresh // Accounting - mb.updateAccounting(seq&^ebit, ts, rl) + mb.updateAccounting(seq, ts, rl) // Check if we are tracking per subject for our simple state. if len(subj) > 0 && mb.fss != nil { @@ -2822,6 +3008,11 @@ func (mb *msgBlock) closeFDsLocked() error { if buf, _ := mb.bytesPending(); len(buf) > 0 { return errPendingData } + mb.closeFDsLockedNoCheck() + return nil +} + +func (mb *msgBlock) closeFDsLockedNoCheck() { if mb.mfd != nil { mb.mfd.Close() mb.mfd = nil @@ -2830,7 +3021,6 @@ func (mb *msgBlock) closeFDsLocked() error { mb.ifd.Close() mb.ifd = nil } - return nil } // bytesPending returns the buffer to be used for writing to the underlying file. @@ -2864,6 +3054,11 @@ func (mb *msgBlock) blkSize() uint64 { // Update accounting on a write msg. // Lock should be held. func (mb *msgBlock) updateAccounting(seq uint64, ts int64, rl uint64) { + isDeleted := seq&ebit != 0 + if isDeleted { + seq = seq &^ ebit + } + if mb.first.seq == 0 || mb.first.ts == 0 { mb.first.seq = seq mb.first.ts = ts @@ -2871,9 +3066,12 @@ func (mb *msgBlock) updateAccounting(seq uint64, ts int64, rl uint64) { // Need atomics here for selectMsgBlock speed. atomic.StoreUint64(&mb.last.seq, seq) mb.last.ts = ts - mb.bytes += rl mb.rbytes += rl - mb.msgs++ + // Only update this accounting if message is not a deleted message. + if !isDeleted { + mb.bytes += rl + mb.msgs++ + } } // Lock should be held. @@ -2887,7 +3085,7 @@ func (fs *fileStore) writeMsgRecord(seq uint64, ts int64, subj string, hdr, msg } // Grab our current last message block. mb := fs.lmb - if mb == nil || mb.blkSize()+rl > fs.fcfg.BlockSize { + if mb == nil || mb.msgs > 0 && mb.blkSize()+rl > fs.fcfg.BlockSize { if mb, err = fs.newMsgBlockForWrite(); err != nil { return 0, err } @@ -3158,6 +3356,7 @@ func (mb *msgBlock) flushPendingMsgsLocked() (*LostStreamData, error) { if cap(mb.cache.buf) <= maxBufReuse { buf = mb.cache.buf[:0] } else { + recycleMsgBlockBuf(mb.cache.buf) buf = nil } if moreBytes > 0 { @@ -3227,6 +3426,15 @@ func (mb *msgBlock) loadBlock(buf []byte) ([]byte, error) { } } + if buf == nil { + buf = getMsgBlockBuf(sz) + if sz > cap(buf) { + // We know we will make a new one so just recycle for now. + recycleMsgBlockBuf(buf) + buf = nil + } + } + if sz > cap(buf) { buf = make([]byte, sz) } else { @@ -3325,7 +3533,7 @@ checkCache: // Fetch a message from this block, possibly reading in and caching the messages. // We assume the block was selected and is correct, so we do not do range checks. -func (mb *msgBlock) fetchMsg(seq uint64) (*fileStoredMsg, bool, error) { +func (mb *msgBlock) fetchMsg(seq uint64, sm *StoreMsg) (*StoreMsg, bool, error) { mb.mu.Lock() defer mb.mu.Unlock() @@ -3334,12 +3542,12 @@ func (mb *msgBlock) fetchMsg(seq uint64) (*fileStoredMsg, bool, error) { return nil, false, err } } - sm, err := mb.cacheLookup(seq) + fsm, err := mb.cacheLookup(seq, sm) if err != nil { return nil, false, err } - expireOk := seq == mb.last.seq && mb.llseq == seq-1 - return sm, expireOk, err + expireOk := seq == mb.last.seq && mb.llseq == seq + return fsm, expireOk, err } var ( @@ -3366,7 +3574,7 @@ const ebit = 1 << 63 // Will do a lookup from cache. // Lock should be held. -func (mb *msgBlock) cacheLookup(seq uint64) (*fileStoredMsg, error) { +func (mb *msgBlock) cacheLookup(seq uint64, sm *StoreMsg) (*StoreMsg, error) { if seq < mb.first.seq || seq > mb.last.seq { return nil, ErrStoreMsgNotFound } @@ -3412,13 +3620,20 @@ func (mb *msgBlock) cacheLookup(seq uint64) (*fileStoredMsg, error) { } // Parse from the raw buffer. - subj, hdr, msg, mseq, ts, err := msgFromBuf(buf, hh) - if err != nil { + fsm, err := mb.msgFromBuf(buf, sm, hh) + if err != nil || fsm == nil { return nil, err } - if seq != mseq { + + // Deleted messages that are decoded return a 0 for seqeunce. + if fsm.seq == 0 { + return nil, errDeletedMsg + } + + if seq != fsm.seq { + recycleMsgBlockBuf(mb.cache.buf) mb.cache.buf = nil - return nil, fmt.Errorf("sequence numbers for cache load did not match, %d vs %d", seq, mseq) + return nil, fmt.Errorf("sequence numbers for cache load did not match, %d vs %d", seq, fsm.seq) } // Clear the check bit here after we know all is good. @@ -3426,15 +3641,16 @@ func (mb *msgBlock) cacheLookup(seq uint64) (*fileStoredMsg, error) { mb.cache.idx[seq-mb.cache.fseq] = (bi | hbit) } - return &fileStoredMsg{subj, hdr, msg, seq, ts, mb, int64(bi)}, nil + return fsm, nil } // Used when we are checking if discarding a message due to max msgs per subject will give us // enough room for a max bytes condition. // Lock should be already held. func (fs *fileStore) sizeForSeq(seq uint64) int { + var smv StoreMsg if mb := fs.selectMsgBlock(seq); mb != nil { - if sm, _, _ := mb.fetchMsg(seq); sm != nil { + if sm, _, _ := mb.fetchMsg(seq, &smv); sm != nil { return int(fileStoreMsgSize(sm.subj, sm.hdr, sm.msg)) } } @@ -3442,7 +3658,7 @@ func (fs *fileStore) sizeForSeq(seq uint64) int { } // Will return message for the given sequence number. -func (fs *fileStore) msgForSeq(seq uint64) (*fileStoredMsg, error) { +func (fs *fileStore) msgForSeq(seq uint64, sm *StoreMsg) (*StoreMsg, error) { // TODO(dlc) - Since Store, Remove, Skip all hold the write lock on fs this will // be stalled. Need another lock if want to happen in parallel. fs.mu.RLock() @@ -3466,7 +3682,7 @@ func (fs *fileStore) msgForSeq(seq uint64) (*fileStoredMsg, error) { return nil, err } - fsm, expireOk, err := mb.fetchMsg(seq) + fsm, expireOk, err := mb.fetchMsg(seq, sm) if err != nil { return nil, err } @@ -3481,9 +3697,10 @@ func (fs *fileStore) msgForSeq(seq uint64) (*fileStoredMsg, error) { } // Internal function to return msg parts from a raw buffer. -func msgFromBuf(buf []byte, hh hash.Hash64) (string, []byte, []byte, uint64, int64, error) { +// Lock should be held. +func (mb *msgBlock) msgFromBuf(buf []byte, sm *StoreMsg, hh hash.Hash64) (*StoreMsg, error) { if len(buf) < emptyRecordLen { - return _EMPTY_, nil, nil, 0, 0, errBadMsg + return nil, errBadMsg } var le = binary.LittleEndian @@ -3495,7 +3712,7 @@ func msgFromBuf(buf []byte, hh hash.Hash64) (string, []byte, []byte, uint64, int slen := int(le.Uint16(hdr[20:])) // Simple sanity check. if dlen < 0 || slen > dlen || int(rl) > len(buf) { - return _EMPTY_, nil, nil, 0, 0, errBadMsg + return nil, errBadMsg } data := buf[msgHdrSize : msgHdrSize+dlen] // Do checksum tests here if requested. @@ -3509,7 +3726,7 @@ func msgFromBuf(buf []byte, hh hash.Hash64) (string, []byte, []byte, uint64, int hh.Write(data[slen : dlen-8]) } if !bytes.Equal(hh.Sum(nil), data[len(data)-8:]) { - return _EMPTY_, nil, nil, 0, 0, errBadMsg + return nil, errBadMsg } } seq := le.Uint64(hdr[4:]) @@ -3517,55 +3734,93 @@ func msgFromBuf(buf []byte, hh hash.Hash64) (string, []byte, []byte, uint64, int seq = 0 } ts := int64(le.Uint64(hdr[12:])) - // FIXME(dlc) - We need to not allow appends to the underlying buffer, so we will - // fix the capacity. This will cause a copy though in stream:internalSendLoop when - // we append CRLF but this was causing a race. Need to rethink more to avoid this copy. + + // Create a StoreMsg if needed. + if sm == nil { + sm = new(StoreMsg) + } else { + sm.clear() + } + // To recycle the large blocks we can never pass back a reference, so need to copy for the upper + // layers and for us to be safe to expire, and recycle, the large msgBlocks. end := dlen - 8 - var mhdr, msg []byte + if hasHeaders { hl := le.Uint32(data[slen:]) bi := slen + 4 li := bi + int(hl) - mhdr = data[bi:li:li] - msg = data[li:end:end] + sm.buf = append(sm.buf, data[bi:end]...) + li, end = li-bi, end-bi + sm.hdr = sm.buf[0:li:li] + sm.msg = sm.buf[li:end] } else { - msg = data[slen:end:end] + sm.buf = append(sm.buf, data[slen:end]...) + sm.msg = sm.buf[0 : end-slen] + } + sm.seq, sm.ts = seq, ts + // Treat subject a bit different to not reference underlying buf. + if slen > 0 { + sm.subj = mb.subjString(data[:slen]) } - return string(data[:slen]), mhdr, msg, seq, ts, nil + + return sm, nil } -// LoadMsg will lookup the message by sequence number and return it if found. -func (fs *fileStore) LoadMsg(seq uint64) (string, []byte, []byte, int64, error) { - sm, err := fs.msgForSeq(seq) - if sm != nil { - return sm.subj, sm.hdr, sm.msg, sm.ts, nil +// Given the `key` byte slice, this function will return the subject +// as a copy of `key` or a configured subject as to minimize memory allocations. +// Lock should be held. +func (mb *msgBlock) subjString(key []byte) string { + if len(key) == 0 { + return _EMPTY_ } - return _EMPTY_, nil, nil, 0, err + + if lsubjs := len(mb.fs.cfg.Subjects); lsubjs > 0 { + if lsubjs == 1 { + // The cast for the comparison does not make a copy + if string(key) == mb.fs.cfg.Subjects[0] { + return mb.fs.cfg.Subjects[0] + } + } else { + for _, subj := range mb.fs.cfg.Subjects { + if string(key) == subj { + return subj + } + } + } + } + // Copy here to not reference underlying buffer. + var sb strings.Builder + sb.Write(key) + return sb.String() +} + +// LoadMsg will lookup the message by sequence number and return it if found. +func (fs *fileStore) LoadMsg(seq uint64, sm *StoreMsg) (*StoreMsg, error) { + return fs.msgForSeq(seq, sm) } // LoadLastMsg will return the last message we have that matches a given subject. // The subject can be a wildcard. -func (fs *fileStore) LoadLastMsg(subject string) (subj string, seq uint64, hdr, msg []byte, ts int64, err error) { - var sm *fileStoredMsg +func (fs *fileStore) LoadLastMsg(subject string, sm *StoreMsg) (*StoreMsg, error) { if subject == _EMPTY_ || subject == fwcs { - sm, _ = fs.msgForSeq(fs.lastSeq()) + sm, _ = fs.msgForSeq(fs.lastSeq(), sm) } else if ss := fs.FilteredState(1, subject); ss.Msgs > 0 { - sm, _ = fs.msgForSeq(ss.Last) + sm, _ = fs.msgForSeq(ss.Last, sm) + } else { + sm = nil } if sm == nil { - return _EMPTY_, 0, nil, nil, 0, ErrStoreMsgNotFound + return nil, ErrStoreMsgNotFound } - return sm.subj, sm.seq, sm.hdr, sm.msg, sm.ts, nil + return sm, nil } -// LoadNextMsg will find the next message matching the filter subject starting at the start sequence. -// The filter subject can be a wildcard. -func (fs *fileStore) LoadNextMsg(filter string, wc bool, start uint64) (subj string, seq uint64, hdr, msg []byte, ts int64, err error) { +func (fs *fileStore) LoadNextMsg(filter string, wc bool, start uint64, sm *StoreMsg) (*StoreMsg, uint64, error) { fs.mu.RLock() defer fs.mu.RUnlock() if fs.closed { - return _EMPTY_, 0, nil, nil, 0, ErrStoreClosed + return nil, 0, ErrStoreClosed } if start < fs.state.FirstSeq { start = fs.state.FirstSeq @@ -3576,17 +3831,17 @@ func (fs *fileStore) LoadNextMsg(filter string, wc bool, start uint64) (subj str if start > atomic.LoadUint64(&mb.last.seq) { continue } - if sm, expireOk, err := mb.firstMatching(filter, wc, start); err == nil { + if sm, expireOk, err := mb.firstMatching(filter, wc, start, sm); err == nil { if expireOk && mb != fs.lmb { mb.tryForceExpireCache() } - return sm.subj, sm.seq, sm.hdr, sm.msg, sm.ts, nil + return sm, sm.seq, nil } else if err != ErrStoreMsgNotFound { - return _EMPTY_, 0, nil, nil, 0, err + return nil, 0, err } } - return _EMPTY_, fs.state.LastSeq, nil, nil, 0, ErrStoreEOF + return nil, fs.state.LastSeq, ErrStoreEOF } // Type returns the type of the underlying store. @@ -3812,10 +4067,16 @@ func (mb *msgBlock) readIndexInfo() error { // Check if this is a short write index file. if bi < 0 || bi+checksumSize > len(buf) { - defer os.Remove(mb.ifn) + os.Remove(mb.ifn) return fmt.Errorf("short index file") } + // Check for consistency if accounting. If something is off bail and we will rebuild. + if mb.msgs != (mb.last.seq-mb.first.seq+1)-dmapLen { + os.Remove(mb.ifn) + return fmt.Errorf("accounting inconsistent") + } + // Checksum copy(mb.lchk[0:], buf[bi:bi+checksumSize]) bi += checksumSize @@ -3923,6 +4184,10 @@ func compareFn(subject string) func(string, string) bool { // PurgeEx will remove messages based on subject filters, sequence and number of messages to keep. // Will return the number of purged messages. func (fs *fileStore) PurgeEx(subject string, sequence, keep uint64) (purged uint64, err error) { + if sequence > 1 && keep > 0 { + return 0, ErrPurgeArgMismatch + } + if subject == _EMPTY_ || subject == fwcs { if keep == 0 && (sequence == 0 || sequence == 1) { return fs.Purge() @@ -3954,6 +4219,8 @@ func (fs *fileStore) PurgeEx(subject string, sequence, keep uint64) (purged uint maxp = ss.Msgs - keep } + var smv StoreMsg + fs.mu.Lock() for _, mb := range fs.blks { mb.mu.Lock() @@ -3968,22 +4235,25 @@ func (fs *fileStore) PurgeEx(subject string, sequence, keep uint64) (purged uint mb.loadMsgsWithLock() shouldExpire = true } - if sequence > 0 && sequence <= l { + if sequence > 1 && sequence <= l { l = sequence - 1 } for seq := f; seq <= l; seq++ { - if sm, _ := mb.cacheLookup(seq); sm != nil && eq(sm.subj, subject) { + if sm, _ := mb.cacheLookup(seq, &smv); sm != nil && eq(sm.subj, subject) { rl := fileStoreMsgSize(sm.subj, sm.hdr, sm.msg) // Do fast in place remove. // Stats - fs.state.Msgs-- - fs.state.Bytes -= rl - mb.msgs-- - mb.bytes -= rl + if mb.msgs > 0 { + fs.state.Msgs-- + fs.state.Bytes -= rl + mb.msgs-- + mb.bytes -= rl + purged++ + } // FSS updates. fs.removePerSubject(sm.subj) - mb.removeSeqPerSubject(sm.subj, seq) + mb.removeSeqPerSubject(sm.subj, seq, &smv) // Check for first message. if seq == mb.first.seq { mb.selectNextFirst() @@ -4001,7 +4271,7 @@ func (fs *fileStore) PurgeEx(subject string, sequence, keep uint64) (purged uint } mb.dmap[seq] = struct{}{} } - purged++ + if maxp > 0 && purged >= maxp { break } @@ -4085,6 +4355,7 @@ func (fs *fileStore) purge(fseq uint64) (uint64, error) { } fs.lmb.first.seq = fs.state.FirstSeq fs.lmb.last.seq = fs.state.LastSeq + fs.lmb.last.ts = fs.state.LastTime.UnixNano() fs.lmb.writeIndexInfo() @@ -4137,9 +4408,19 @@ func (fs *fileStore) Compact(seq uint64) (uint64, error) { deleted++ } + var smv StoreMsg + var err error + var isEmpty bool + smb.mu.Lock() + // Since we loaded before we acquired our lock, double check here under lock that we have the messages loaded. + if smb.cacheNotLoaded() { + if err = smb.loadMsgsWithLock(); err != nil { + goto SKIP + } + } for mseq := smb.first.seq; mseq < seq; mseq++ { - sm, err := smb.cacheLookup(mseq) + sm, err := smb.cacheLookup(mseq, &smv) if err == errDeletedMsg { // Update dmap. if len(smb.dmap) > 0 { @@ -4150,18 +4431,20 @@ func (fs *fileStore) Compact(seq uint64) (uint64, error) { } } else if sm != nil { sz := fileStoreMsgSize(sm.subj, sm.hdr, sm.msg) - smb.bytes -= sz - bytes += sz - smb.msgs-- - purged++ + if smb.msgs > 0 { + smb.bytes -= sz + bytes += sz + smb.msgs-- + purged++ + } // Update fss fs.removePerSubject(sm.subj) - smb.removeSeqPerSubject(sm.subj, mseq) + smb.removeSeqPerSubject(sm.subj, mseq, &smv) } } // Check if empty after processing, could happen if tail of messages are all deleted. - isEmpty := smb.msgs == 0 + isEmpty = smb.msgs == 0 if isEmpty { smb.dirtyCloseWithRemove(true) // Update fs first here as well. @@ -4174,7 +4457,41 @@ func (fs *fileStore) Compact(seq uint64) (uint64, error) { smb.selectNextFirst() fs.state.FirstSeq = smb.first.seq fs.state.FirstTime = time.Unix(0, smb.first.ts).UTC() + + // Check if we should reclaim the head space from this block. + // This will be optimistic only, so don't continue if we encounter any errors here. + if smb.bytes*2 < smb.rbytes { + var moff uint32 + moff, _, _, err = smb.slotInfo(int(smb.first.seq - smb.cache.fseq)) + if err != nil || moff >= uint32(len(smb.cache.buf)) { + goto SKIP + } + buf := smb.cache.buf[moff:] + // Don't reuse, copy to new recycled buf. + nbuf := getMsgBlockBuf(len(buf)) + nbuf = append(nbuf, buf...) + smb.closeFDsLockedNoCheck() + // Check for encryption. + if smb.bek != nil && len(nbuf) > 0 { + // Recreate to reset counter. + rbek, err := chacha20.NewUnauthenticatedCipher(smb.seed, smb.nonce) + if err != nil { + goto SKIP + } + cbuf := make([]byte, len(nbuf)) + rbek.XORKeyStream(cbuf, nbuf) + if err = ioutil.WriteFile(smb.mfn, cbuf, defaultFilePerms); err != nil { + goto SKIP + } + } else if err = ioutil.WriteFile(smb.mfn, nbuf, defaultFilePerms); err != nil { + goto SKIP + } + smb.clearCacheAndOffset() + smb.rbytes = uint64(len(nbuf)) + } } + +SKIP: smb.mu.Unlock() if !isEmpty { @@ -4198,7 +4515,7 @@ func (fs *fileStore) Compact(seq uint64) (uint64, error) { cb(-int64(purged), -int64(bytes), 0, _EMPTY_) } - return purged, nil + return purged, err } // Truncate will truncate a stream store up to and including seq. Sequence needs to be valid. @@ -4219,7 +4536,7 @@ func (fs *fileStore) Truncate(seq uint64) error { fs.mu.Unlock() return ErrInvalidSequence } - lsm, _, _ := nlmb.fetchMsg(seq) + lsm, _, _ := nlmb.fetchMsg(seq, nil) if lsm == nil { fs.mu.Unlock() return ErrInvalidSequence @@ -4323,6 +4640,27 @@ func (fs *fileStore) removeMsgBlock(mb *msgBlock) { } } +// When we have an empty block but want to keep the index for timestamp info etc. +// Lock should be held. +func (mb *msgBlock) closeAndKeepIndex() { + // We will leave a 0 length blk marker. + if mb.mfd != nil { + mb.mfd.Truncate(0) + } else { + // We were closed, so just write out an empty file. + ioutil.WriteFile(mb.mfn, nil, defaultFilePerms) + } + // Close + mb.dirtyCloseWithRemove(false) + // Make sure to write the index file so we can remember last seq and ts. + mb.writeIndexInfoLocked() + + // Clear any fss. + if mb.sfn != _EMPTY_ { + os.Remove(mb.sfn) + } +} + // Called by purge to simply get rid of the cache and close our fds. // Lock should not be held. func (mb *msgBlock) dirtyClose() { @@ -4372,7 +4710,7 @@ func (mb *msgBlock) dirtyCloseWithRemove(remove bool) { // Remove a seq from the fss and select new first. // Lock should be held. -func (mb *msgBlock) removeSeqPerSubject(subj string, seq uint64) { +func (mb *msgBlock) removeSeqPerSubject(subj string, seq uint64, smp *StoreMsg) { ss := mb.fss[subj] if ss == nil { return @@ -4395,8 +4733,12 @@ func (mb *msgBlock) removeSeqPerSubject(subj string, seq uint64) { } // TODO(dlc) - Might want to optimize this. + var smv StoreMsg + if smp == nil { + smp = &smv + } for tseq := seq + 1; tseq <= ss.Last; tseq++ { - if sm, _ := mb.cacheLookup(tseq); sm != nil { + if sm, _ := mb.cacheLookup(tseq, smp); sm != nil { if sm.subj == subj { ss.First = tseq return @@ -4410,6 +4752,15 @@ func (mb *msgBlock) generatePerSubjectInfo() error { mb.mu.Lock() defer mb.mu.Unlock() + if mb.fss == nil { + mb.fss = make(map[string]*SimpleState) + } + + // Check if this mb is empty. This can happen when its the last one and we are holding onto it for seq and timestamp info. + if mb.msgs == 0 { + return nil + } + var shouldExpire bool if mb.cacheNotLoaded() { if err := mb.loadMsgsWithLock(); err != nil { @@ -4417,13 +4768,11 @@ func (mb *msgBlock) generatePerSubjectInfo() error { } shouldExpire = true } - if mb.fss == nil { - mb.fss = make(map[string]*SimpleState) - } + var smv StoreMsg fseq, lseq := mb.first.seq, mb.last.seq for seq := fseq; seq <= lseq; seq++ { - sm, err := mb.cacheLookup(seq) + sm, err := mb.cacheLookup(seq, &smv) if err != nil { // Since we are walking by sequence we can ignore some errors that are benign to rebuilding our state. if err == ErrStoreMsgNotFound || err == errDeletedMsg { @@ -4479,8 +4828,6 @@ func (mb *msgBlock) readPerSubjectInfo() error { return mb.generatePerSubjectInfo() } - fss := make(map[string]*SimpleState) - bi := hdrLen readU64 := func() uint64 { if bi < 0 { @@ -4495,14 +4842,18 @@ func (mb *msgBlock) readPerSubjectInfo() error { return num } - for i, numEntries := uint64(0), readU64(); i < numEntries; i++ { + numEntries := readU64() + fss := make(map[string]*SimpleState, numEntries) + + mb.mu.Lock() + for i := uint64(0); i < numEntries; i++ { lsubj := readU64() - subj := buf[bi : bi+int(lsubj)] + // Make a copy or use a configured subject (to avoid mem allocation) + subj := mb.subjString(buf[bi : bi+int(lsubj)]) bi += int(lsubj) msgs, first, last := readU64(), readU64(), readU64() - fss[string(subj)] = &SimpleState{Msgs: msgs, First: first, Last: last} + fss[subj] = &SimpleState{Msgs: msgs, First: first, Last: last} } - mb.mu.Lock() mb.fss = fss mb.mu.Unlock() return nil @@ -4588,6 +4939,8 @@ func (fs *fileStore) closeAllMsgBlocks(sync bool) { func (fs *fileStore) Delete() error { if fs.isClosed() { + // Always attempt to remove since we could have been closed beforehand. + os.RemoveAll(fs.fcfg.StoreDir) return ErrStoreClosed } fs.Purge() @@ -4640,7 +4993,7 @@ func (fs *fileStore) Stop() error { fs.cancelSyncTimer() fs.cancelAgeChk() - var _cfs [256]*consumerFileStore + var _cfs [256]ConsumerStore cfs := append(_cfs[:0], fs.cfs...) fs.cfs = nil fs.mu.Unlock() @@ -4794,7 +5147,11 @@ func (fs *fileStore) streamSnapshot(w io.WriteCloser, state *StreamState, includ cfs := fs.cfs fs.mu.Unlock() - for _, o := range cfs { + for _, cs := range cfs { + o, ok := cs.(*consumerFileStore) + if !ok { + continue + } o.mu.Lock() // Grab our general meta data. // We do this now instead of pulling from files since they could be encrypted. @@ -4908,11 +5265,20 @@ func (fs *fileStore) ConsumerStore(name string, cfg *ConsumerConfig) (ConsumerSt if cfg == nil || name == _EMPTY_ { return nil, fmt.Errorf("bad consumer config") } + + // We now allow overrides from a stream being a filestore type and forcing a consumer to be memory store. + if cfg.MemoryStorage { + // Create directly here. + o := &consumerMemStore{ms: fs, cfg: *cfg} + fs.AddConsumer(o) + return o, nil + } + odir := filepath.Join(fs.fcfg.StoreDir, consumerDir, name) if err := os.MkdirAll(odir, defaultDirPerms); err != nil { return nil, fmt.Errorf("could not create consumer directory - %v", err) } - csi := &FileConsumerInfo{ConsumerConfig: *cfg} + csi := &FileConsumerInfo{Name: name, Created: time.Now().UTC(), ConsumerConfig: *cfg} o := &consumerFileStore{ fs: fs, cfg: csi, @@ -4984,9 +5350,7 @@ func (fs *fileStore) ConsumerStore(name string, cfg *ConsumerConfig) (ConsumerSt o.qch = make(chan struct{}) go o.flushLoop() - fs.mu.Lock() - fs.cfs = append(fs.cfs, o) - fs.mu.Unlock() + fs.AddConsumer(o) return o, nil } @@ -5082,6 +5446,26 @@ func (o *consumerFileStore) flushLoop() { } } +// SetStarting sets our starting stream sequence. +func (o *consumerFileStore) SetStarting(sseq uint64) error { + o.mu.Lock() + o.state.Delivered.Stream = sseq + buf, err := o.encodeState() + o.mu.Unlock() + if err != nil { + return err + } + return o.writeState(buf) +} + +// HasState returns if this store has a recorded state. +func (o *consumerFileStore) HasState() bool { + o.mu.Lock() + _, err := os.Stat(o.ifn) + o.mu.Unlock() + return err == nil +} + // UpdateDelivered is called whenever a new message has been delivered. func (o *consumerFileStore) UpdateDelivered(dseq, sseq, dc uint64, ts int64) error { o.mu.Lock() @@ -5212,73 +5596,22 @@ const seqsHdrSize = 6*binary.MaxVarintLen64 + hdrLen // Encode our consumer state, version 2. // Lock should be held. -func (o *consumerFileStore) encodeState() ([]byte, error) { + +func (o *consumerFileStore) EncodedState() ([]byte, error) { + o.mu.Lock() + defer o.mu.Unlock() + if o.closed { return nil, ErrStoreClosed } return encodeConsumerState(&o.state), nil } -func encodeConsumerState(state *ConsumerState) []byte { - var hdr [seqsHdrSize]byte - var buf []byte - - maxSize := seqsHdrSize - if lp := len(state.Pending); lp > 0 { - maxSize += lp*(3*binary.MaxVarintLen64) + binary.MaxVarintLen64 - } - if lr := len(state.Redelivered); lr > 0 { - maxSize += lr*(2*binary.MaxVarintLen64) + binary.MaxVarintLen64 - } - if maxSize == seqsHdrSize { - buf = hdr[:seqsHdrSize] - } else { - buf = make([]byte, maxSize) - } - - // Write header - buf[0] = magic - buf[1] = 2 - - n := hdrLen - n += binary.PutUvarint(buf[n:], state.AckFloor.Consumer) - n += binary.PutUvarint(buf[n:], state.AckFloor.Stream) - n += binary.PutUvarint(buf[n:], state.Delivered.Consumer) - n += binary.PutUvarint(buf[n:], state.Delivered.Stream) - n += binary.PutUvarint(buf[n:], uint64(len(state.Pending))) - - asflr := state.AckFloor.Stream - adflr := state.AckFloor.Consumer - - // These are optional, but always write len. This is to avoid a truncate inline. - if len(state.Pending) > 0 { - // To save space we will use now rounded to seconds to be base timestamp. - mints := time.Now().Round(time.Second).Unix() - // Write minimum timestamp we found from above. - n += binary.PutVarint(buf[n:], mints) - - for k, v := range state.Pending { - n += binary.PutUvarint(buf[n:], k-asflr) - n += binary.PutUvarint(buf[n:], v.Sequence-adflr) - // Downsample to seconds to save on space. - // Subsecond resolution not needed for recovery etc. - ts := v.Timestamp / 1_000_000_000 - n += binary.PutVarint(buf[n:], mints-ts) - } - } - - // We always write the redelivered len. - n += binary.PutUvarint(buf[n:], uint64(len(state.Redelivered))) - - // We expect these to be small. - if len(state.Redelivered) > 0 { - for k, v := range state.Redelivered { - n += binary.PutUvarint(buf[n:], k-asflr) - n += binary.PutUvarint(buf[n:], v) - } +func (o *consumerFileStore) encodeState() ([]byte, error) { + if o.closed { + return nil, ErrStoreClosed } - - return buf[:n] + return encodeConsumerState(&o.state), nil } func (o *consumerFileStore) UpdateConfig(cfg *ConsumerConfig) error { @@ -5508,10 +5841,14 @@ func (o *consumerFileStore) State() (*ConsumerState, error) { o.mu.Lock() defer o.mu.Unlock() + if o.closed { + return nil, ErrStoreClosed + } + state := &ConsumerState{} // See if we have a running state or if we need to read in from disk. - if o.state.Delivered.Consumer != 0 { + if o.state.Delivered.Consumer != 0 || o.state.Delivered.Stream != 0 { state.Delivered = o.state.Delivered state.AckFloor = o.state.AckFloor if len(o.state.Pending) > 0 { @@ -5699,7 +6036,7 @@ func (o *consumerFileStore) Stop() error { ifn, fs := o.ifn, o.fs o.mu.Unlock() - fs.removeConsumer(o) + fs.RemoveConsumer(o) if len(buf) > 0 { o.waitOnFlusher() @@ -5759,21 +6096,29 @@ func (o *consumerFileStore) delete(streamDeleted bool) error { } if !streamDeleted { - fs.removeConsumer(o) + fs.RemoveConsumer(o) } return err } -func (fs *fileStore) removeConsumer(cfs *consumerFileStore) { +func (fs *fileStore) AddConsumer(o ConsumerStore) error { fs.mu.Lock() defer fs.mu.Unlock() - for i, o := range fs.cfs { + fs.cfs = append(fs.cfs, o) + return nil +} + +func (fs *fileStore) RemoveConsumer(o ConsumerStore) error { + fs.mu.Lock() + defer fs.mu.Unlock() + for i, cfs := range fs.cfs { if o == cfs { fs.cfs = append(fs.cfs[:i], fs.cfs[i+1:]...) break } } + return nil } //////////////////////////////////////////////////////////////////////////////// diff --git a/vendor/github.com/nats-io/nats-server/v2/server/gateway.go b/vendor/github.com/nats-io/nats-server/v2/server/gateway.go index ce5b7e6d..8e4939d8 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/gateway.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/gateway.go @@ -1013,6 +1013,13 @@ func (c *client) processGatewayInfo(info *Info) { return } + // Check for duplicate server name with servers in our cluster + if s.isDuplicateServerName(info.Name) { + c.Errorf("Remote server has a duplicate name: %q", info.Name) + c.closeConnection(DuplicateServerName) + return + } + // Possibly add URLs that we get from the INFO protocol. if len(info.GatewayURLs) > 0 { cfg.updateURLs(info.GatewayURLs) @@ -1084,6 +1091,13 @@ func (c *client) processGatewayInfo(info *Info) { } else if isFirstINFO { // This is the first INFO of an inbound connection... + // Check for duplicate server name with servers in our cluster + if s.isDuplicateServerName(info.Name) { + c.Errorf("Remote server has a duplicate name: %q", info.Name) + c.closeConnection(DuplicateServerName) + return + } + s.registerInboundGatewayConnection(cid, c) c.Noticef("Inbound gateway connection from %q (%s) registered", info.Gateway, info.ID) @@ -2810,7 +2824,7 @@ func (c *client) handleGatewayReply(msg []byte) (processed bool) { // If route is nil, we will process the incoming message locally. if route == nil { // Check if this is a service reply subject (_R_) - isServiceReply := len(acc.imports.services) > 0 && isServiceReply(c.pa.subject) + isServiceReply := isServiceReply(c.pa.subject) var queues [][]byte if len(r.psubs)+len(r.qsubs) > 0 { diff --git a/vendor/github.com/nats-io/nats-server/v2/server/ipqueue.go b/vendor/github.com/nats-io/nats-server/v2/server/ipqueue.go index 2e514513..4f288e82 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/ipqueue.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/ipqueue.go @@ -15,35 +15,26 @@ package server import ( "sync" + "sync/atomic" ) const ipQueueDefaultMaxRecycleSize = 4 * 1024 -const ipQueueDefaultWarnThreshold = 32 * 1024 - -type ipQueueLogger interface { - // The ipQueue will invoke this function with the queue's name and the number - // of pending elements. This call CANNOT block. It is ok to drop the logging - // if desired, but not block. - log(name string, pending int) -} // This is a generic intra-process queue. type ipQueue struct { + inprogress int64 sync.RWMutex - ch chan struct{} - elts []interface{} - pos int - pool *sync.Pool - mrs int - name string - logger ipQueueLogger - lt int + ch chan struct{} + elts []interface{} + pos int + pool *sync.Pool + mrs int + name string + m *sync.Map } type ipQueueOpts struct { maxRecycleSize int - name string - logger ipQueueLogger } type ipQueueOpt func(*ipQueueOpts) @@ -56,27 +47,19 @@ func ipQueue_MaxRecycleSize(max int) ipQueueOpt { } } -// This option provides the logger to be used by this queue to log -// when the number of pending elements reaches a certain threshold. -func ipQueue_Logger(name string, l ipQueueLogger) ipQueueOpt { - return func(o *ipQueueOpts) { - o.name, o.logger = name, l - } -} - -func newIPQueue(opts ...ipQueueOpt) *ipQueue { +func (s *Server) newIPQueue(name string, opts ...ipQueueOpt) *ipQueue { qo := ipQueueOpts{maxRecycleSize: ipQueueDefaultMaxRecycleSize} for _, o := range opts { o(&qo) } q := &ipQueue{ - ch: make(chan struct{}, 1), - mrs: qo.maxRecycleSize, - pool: &sync.Pool{}, - name: qo.name, - logger: qo.logger, - lt: ipQueueDefaultWarnThreshold, + ch: make(chan struct{}, 1), + mrs: qo.maxRecycleSize, + pool: &sync.Pool{}, + name: name, + m: &s.ipQueues, } + s.ipQueues.Store(name, q) return q } @@ -101,9 +84,6 @@ func (q *ipQueue) push(e interface{}) int { } q.elts = append(q.elts, e) l++ - if l >= q.lt && q.logger != nil && (l <= q.lt+10 || q.lt%10000 == 0) { - q.logger.log(q.name, l) - } q.Unlock() if signal { select { @@ -132,6 +112,7 @@ func (q *ipQueue) pop() []interface{} { elts = q.elts[q.pos:] } q.elts, q.pos = nil, 0 + atomic.AddInt64(&q.inprogress, int64(len(elts))) q.Unlock() return elts } @@ -174,13 +155,24 @@ func (q *ipQueue) popOne() interface{} { // After a pop(), the slice can be recycled for the next push() when // a first element is added to the queue. +// This will also decrement the "in progress" count with the length +// of the slice. // Reason we use pointer to slice instead of slice is explained // here: https://staticcheck.io/docs/checks#SA6002 func (q *ipQueue) recycle(elts *[]interface{}) { - // If invoked with an nil list, don't recyle. + // If invoked with a nil list, nothing to do. + if elts == nil || *elts == nil { + return + } + // Update the in progress count. + if len(*elts) > 0 { + if atomic.AddInt64(&q.inprogress, int64(-(len(*elts)))) < 0 { + atomic.StoreInt64(&q.inprogress, 0) + } + } // We also don't want to recycle huge slices, so check against the max. // q.mrs is normally immutable but can be changed, in a safe way, in some tests. - if elts == nil || *elts == nil || cap(*elts) > q.mrs { + if cap(*elts) > q.mrs { return } q.resetAndReturnToPool(elts) @@ -199,6 +191,9 @@ func (q *ipQueue) len() int { // notified that there is something in the queue (reading from queue's `ch`) // may then get nothing if `drain()` is invoked before the `pop()` or `popOne()`. func (q *ipQueue) drain() { + if q == nil { + return + } q.Lock() if q.elts != nil { q.resetAndReturnToPool(&q.elts) @@ -212,3 +207,21 @@ func (q *ipQueue) drain() { } q.Unlock() } + +// Since the length of the queue goes to 0 after a pop(), it is good to +// have an insight on how many elements are yet to be processed after a pop(). +// For that reason, the queue maintains a count of elements returned through +// the pop() API. When the caller will call q.recycle(), this count will +// be reduced by the size of the slice returned by pop(). +func (q *ipQueue) inProgress() int64 { + return atomic.LoadInt64(&q.inprogress) +} + +// Remove this queue from the server's map of ipQueues. +// All ipQueue operations (such as push/pop/etc..) are still possible. +func (q *ipQueue) unregister() { + if q == nil { + return + } + q.m.Delete(q.name) +} diff --git a/vendor/github.com/nats-io/nats-server/v2/server/jetstream.go b/vendor/github.com/nats-io/nats-server/v2/server/jetstream.go index 6b13ac7a..e9f10ce2 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/jetstream.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/jetstream.go @@ -59,24 +59,32 @@ type JetStreamStats struct { } type JetStreamAccountLimits struct { - MaxMemory int64 `json:"max_memory"` - MaxStore int64 `json:"max_storage"` - MaxStreams int `json:"max_streams"` - MaxConsumers int `json:"max_consumers"` - MaxBytesRequired bool `json:"max_bytes_required"` + MaxMemory int64 `json:"max_memory"` + MaxStore int64 `json:"max_storage"` + MaxStreams int `json:"max_streams"` + MaxConsumers int `json:"max_consumers"` + MaxAckPending int `json:"max_ack_pending"` + MemoryMaxStreamBytes int64 `json:"memory_max_stream_bytes"` + StoreMaxStreamBytes int64 `json:"storage_max_stream_bytes"` + MaxBytesRequired bool `json:"max_bytes_required"` } -// JetStreamAccountStats returns current statistics about the account's JetStream usage. -type JetStreamAccountStats struct { +type JetStreamTier struct { Memory uint64 `json:"memory"` Store uint64 `json:"storage"` Streams int `json:"streams"` Consumers int `json:"consumers"` - Domain string `json:"domain,omitempty"` - API JetStreamAPIStats `json:"api"` Limits JetStreamAccountLimits `json:"limits"` } +// JetStreamAccountStats returns current statistics about the account's JetStream usage. +type JetStreamAccountStats struct { + JetStreamTier // in case tiers are used, reflects totals with limits not set + Domain string `json:"domain,omitempty"` + API JetStreamAPIStats `json:"api"` + Tiers map[string]JetStreamTier `json:"tiers,omitempty"` // indexed by tier name +} + type JetStreamAPIStats struct { Total uint64 `json:"total"` Errors uint64 `json:"errors"` @@ -93,6 +101,7 @@ type jetStream struct { storeReserved int64 memUsed int64 storeUsed int64 + clustered int32 mu sync.RWMutex srv *Server config JetStreamConfig @@ -104,44 +113,53 @@ type jetStream struct { oos bool } +type remoteUsage struct { + tiers map[string]*jsaUsage // indexed by tier name + api uint64 + err uint64 +} + +type jsaStorage struct { + total jsaUsage + local jsaUsage +} + // This represents a jetstream enabled account. // Worth noting that we include the jetstream pointer, this is because // in general we want to be very efficient when receiving messages on // an internal sub for a stream, so we will direct link to the stream // and walk backwards as needed vs multiple hash lookups and locks, etc. type jsAccount struct { - mu sync.RWMutex - js *jetStream - account *Account - limits JetStreamAccountLimits - memReserved int64 - storeReserved int64 - memTotal int64 - storeTotal int64 - apiTotal uint64 - apiErrors uint64 - usage jsaUsage - rusage map[string]*jsaUsage - storeDir string - streams map[string]*stream - templates map[string]*streamTemplate - store TemplateStore - - // Cluster support + mu sync.RWMutex + js *jetStream + account *Account + storeDir string + streams map[string]*stream + templates map[string]*streamTemplate + store TemplateStore + + // From server + sendq *ipQueue // of *pubMsg + + // Usage/limits related fields that will be protected by usageMu + usageMu sync.RWMutex + limits map[string]JetStreamAccountLimits // indexed by tierName + usage map[string]*jsaStorage // indexed by tierName + rusage map[string]*remoteUsage // indexed by node id + apiTotal uint64 + apiErrors uint64 + usageApi uint64 + usageErr uint64 updatesPub string updatesSub *subscription - // From server - sendq *ipQueue // of *pubMsg - lupdate time.Time - utimer *time.Timer + lupdate time.Time + utimer *time.Timer } // Track general usage for this account. type jsaUsage struct { mem int64 store int64 - api uint64 - err uint64 } // EnableJetStream will enable JetStream support on this server with the given configuration. @@ -553,8 +571,8 @@ func (s *Server) enableJetStreamAccounts() error { if s.globalAccountOnly() { gacc := s.GlobalAccount() gacc.mu.Lock() - if gacc.jsLimits == nil { - gacc.jsLimits = dynamicJSAccountLimits + if len(gacc.jsLimits) == 0 { + gacc.jsLimits = defaultJSAccountTiers } gacc.mu.Unlock() if err := s.configJetStream(gacc); err != nil { @@ -617,17 +635,20 @@ func (s *Server) configJetStream(acc *Account) error { if acc == nil { return nil } - if acc.jsLimits != nil { + acc.mu.RLock() + jsLimits := acc.jsLimits + acc.mu.RUnlock() + if jsLimits != nil { // Check if already enabled. This can be during a reload. if acc.JetStreamEnabled() { if err := acc.enableAllJetStreamServiceImportsAndMappings(); err != nil { return err } - if err := acc.UpdateJetStreamLimits(acc.jsLimits); err != nil { + if err := acc.UpdateJetStreamLimits(jsLimits); err != nil { return err } } else { - if err := acc.EnableJetStream(acc.jsLimits); err != nil { + if err := acc.EnableJetStream(jsLimits); err != nil { return err } if s.gateway.enabled { @@ -759,6 +780,10 @@ func (s *Server) migrateEphemerals() { var consumers []*consumerAssignment js.mu.Lock() + if cc.meta == nil { + js.mu.Unlock() + return + } ourID := cc.meta.ID() for _, asa := range cc.streams { for _, sa := range asa { @@ -797,7 +822,7 @@ func (s *Server) migrateEphemerals() { } o.mu.Unlock() } - state := o.readStoreState() + state, _ := o.store.State() o.deleteWithoutAdvisory() js.mu.Lock() // Delete old one. @@ -832,6 +857,13 @@ func (s *Server) shutdownJetStream() { s.Noticef("Initiating JetStream Shutdown...") defer s.Noticef("JetStream Shutdown") + // If we have folks blocked on sync requests, unblock. + // Send 1 is enough, but use select in case they were all present. + select { + case s.syncOutSem <- struct{}{}: + default: + } + var _a [512]*Account accounts := _a[:0] @@ -917,7 +949,7 @@ func (s *Server) getJetStream() *jetStream { return js } -func (a *Account) assignJetStreamLimits(limits *JetStreamAccountLimits) { +func (a *Account) assignJetStreamLimits(limits map[string]JetStreamAccountLimits) { a.mu.Lock() a.jsLimits = limits a.mu.Unlock() @@ -925,7 +957,7 @@ func (a *Account) assignJetStreamLimits(limits *JetStreamAccountLimits) { // EnableJetStream will enable JetStream on this account with the defined limits. // This is a helper for JetStreamEnableAccount. -func (a *Account) EnableJetStream(limits *JetStreamAccountLimits) error { +func (a *Account) EnableJetStream(limits map[string]JetStreamAccountLimits) error { a.mu.RLock() s := a.srv a.mu.RUnlock() @@ -944,8 +976,8 @@ func (a *Account) EnableJetStream(limits *JetStreamAccountLimits) error { // No limits means we dynamically set up limits. // We also place limits here so we know that the account is configured for JetStream. - if limits == nil { - limits = dynamicJSAccountLimits + if len(limits) == 0 { + limits = defaultJSAccountTiers } a.assignJetStreamLimits(limits) @@ -967,20 +999,20 @@ func (a *Account) EnableJetStream(limits *JetStreamAccountLimits) error { return err } - jsa := &jsAccount{js: js, account: a, limits: *limits, streams: make(map[string]*stream), sendq: sendq} - jsa.utimer = time.AfterFunc(usageTick, jsa.sendClusterUsageUpdateTimer) - jsa.storeDir = filepath.Join(js.config.StoreDir, a.Name) - - js.accounts[a.Name] = jsa - js.mu.Unlock() - sysNode := s.Node() + jsa := &jsAccount{js: js, account: a, limits: limits, streams: make(map[string]*stream), sendq: sendq, usage: make(map[string]*jsaStorage)} + jsa.storeDir = filepath.Join(js.config.StoreDir, a.Name) + + jsa.usageMu.Lock() + jsa.utimer = time.AfterFunc(usageTick, jsa.sendClusterUsageUpdateTimer) // Cluster mode updates to resource usage, but we always will turn on. System internal prevents echos. - jsa.mu.Lock() jsa.updatesPub = fmt.Sprintf(jsaUpdatesPubT, a.Name, sysNode) jsa.updatesSub, _ = s.sysSubscribe(fmt.Sprintf(jsaUpdatesSubT, a.Name), jsa.remoteUpdateUsage) - jsa.mu.Unlock() + jsa.usageMu.Unlock() + + js.accounts[a.Name] = jsa + js.mu.Unlock() // Stamp inside account as well. a.mu.Lock() @@ -993,8 +1025,16 @@ func (a *Account) EnableJetStream(limits *JetStreamAccountLimits) error { } s.Debugf("Enabled JetStream for account %q", a.Name) - s.Debugf(" Max Memory: %s", friendlyBytes(limits.MaxMemory)) - s.Debugf(" Max Storage: %s", friendlyBytes(limits.MaxStore)) + if l, ok := limits[_EMPTY_]; ok { + s.Debugf(" Max Memory: %s", friendlyBytes(l.MaxMemory)) + s.Debugf(" Max Storage: %s", friendlyBytes(l.MaxStore)) + } else { + for t, l := range limits { + s.Debugf(" Tier: %s", t) + s.Debugf(" Max Memory: %s", friendlyBytes(l.MaxMemory)) + s.Debugf(" Max Storage: %s", friendlyBytes(l.MaxStore)) + } + } // Clean up any old snapshots that were orphaned while staging. os.RemoveAll(filepath.Join(js.config.StoreDir, snapStagingDir)) @@ -1084,11 +1124,11 @@ func (a *Account) EnableJetStream(limits *JetStreamAccountLimits) error { } buf, err := ioutil.ReadFile(metafile) if err != nil { - s.Warnf(" Error reading metafile %q: %v", metasum, err) + s.Warnf(" Error reading metafile %q: %v", metafile, err) continue } if _, err := os.Stat(metasum); os.IsNotExist(err) { - s.Warnf(" Missing stream checksum for %q", metasum) + s.Warnf(" Missing stream checksum file %q", metasum) continue } sum, err := ioutil.ReadFile(metasum) @@ -1099,7 +1139,7 @@ func (a *Account) EnableJetStream(limits *JetStreamAccountLimits) error { hh.Write(buf) checksum := hex.EncodeToString(hh.Sum(nil)) if checksum != string(sum) { - s.Warnf(" Stream metafile checksums do not match %q vs %q", sum, checksum) + s.Warnf(" Stream metafile %q: checksums do not match %q vs %q", metafile, sum, checksum) continue } @@ -1119,7 +1159,7 @@ func (a *Account) EnableJetStream(limits *JetStreamAccountLimits) error { var cfg FileStreamInfo if err := json.Unmarshal(buf, &cfg); err != nil { - s.Warnf(" Error unmarshalling stream metafile: %v", err) + s.Warnf(" Error unmarshalling stream metafile %q: %v", metafile, err) continue } @@ -1129,6 +1169,9 @@ func (a *Account) EnableJetStream(limits *JetStreamAccountLimits) error { } } + // We had a bug that set a default de dupe window on mirror, despite that being not a valid config + fixCfgMirrorWithDedupWindow(&cfg.StreamConfig) + // We had a bug that could allow subjects in that had prefix or suffix spaces. We check for that here // and will patch them on the fly for now. We will warn about them. var hadSubjErr bool @@ -1187,7 +1230,7 @@ func (a *Account) EnableJetStream(limits *JetStreamAccountLimits) error { } buf, err := ioutil.ReadFile(metafile) if err != nil { - s.Warnf(" Error reading consumer metafile %q: %v", metasum, err) + s.Warnf(" Error reading consumer metafile %q: %v", metafile, err) continue } if _, err := os.Stat(metasum); os.IsNotExist(err) { @@ -1207,7 +1250,7 @@ func (a *Account) EnableJetStream(limits *JetStreamAccountLimits) error { var cfg FileConsumerInfo if err := json.Unmarshal(buf, &cfg); err != nil { - s.Warnf(" Error unmarshalling consumer metafile: %v", err) + s.Warnf(" Error unmarshalling consumer metafile %q: %v", metafile, err) continue } isEphemeral := !isDurableConsumer(&cfg.ConsumerConfig) @@ -1216,9 +1259,9 @@ func (a *Account) EnableJetStream(limits *JetStreamAccountLimits) error { // the consumer can reconnect. We will create it as a durable and switch it. cfg.ConsumerConfig.Durable = ofi.Name() } - obs, err := e.mset.addConsumer(&cfg.ConsumerConfig) + obs, err := e.mset.addConsumerWithAssignment(&cfg.ConsumerConfig, _EMPTY_, nil, true) if err != nil { - s.Warnf(" Error adding consumer: %v", err) + s.Warnf(" Error adding consumer %q: %v", cfg.Name, err) continue } if isEphemeral { @@ -1227,11 +1270,12 @@ func (a *Account) EnableJetStream(limits *JetStreamAccountLimits) error { if !cfg.Created.IsZero() { obs.setCreatedTime(cfg.Created) } + lseq := e.mset.lastSeq() obs.mu.Lock() - err = obs.readStoredState() + err = obs.readStoredState(lseq) obs.mu.Unlock() if err != nil { - s.Warnf(" Error restoring consumer state: %v", err) + s.Warnf(" Error restoring consumer %q state: %v", cfg.Name, err) } } } @@ -1244,16 +1288,28 @@ func (a *Account) EnableJetStream(limits *JetStreamAccountLimits) error { return nil } -// Return whether or not we require MaxBytes to be set. -func (a *Account) maxBytesRequired() bool { +// Return whether we require MaxBytes to be set and if > 0 an upper limit for stream size exists +// Both limits are independent of each other. +func (a *Account) maxBytesLimits(cfg *StreamConfig) (bool, int64) { a.mu.RLock() - defer a.mu.RUnlock() - jsa := a.js + a.mu.RUnlock() if jsa == nil { - return false + return false, 0 + } + jsa.usageMu.RLock() + selectedLimits, _, ok := jsa.selectLimits(cfg) + jsa.usageMu.RUnlock() + if !ok { + return false, 0 } - return jsa.limits.MaxBytesRequired + maxStreamBytes := int64(0) + if cfg.Storage == MemoryStorage { + maxStreamBytes = selectedLimits.MemoryMaxStreamBytes + } else { + maxStreamBytes = selectedLimits.StoreMaxStreamBytes + } + return selectedLimits.MaxBytesRequired, maxStreamBytes } // NumStreams will return how many streams we have. @@ -1324,7 +1380,7 @@ func (a *Account) lookupStream(name string) (*stream, error) { } // UpdateJetStreamLimits will update the account limits for a JetStream enabled account. -func (a *Account) UpdateJetStreamLimits(limits *JetStreamAccountLimits) error { +func (a *Account) UpdateJetStreamLimits(limits map[string]JetStreamAccountLimits) error { a.mu.RLock() s, jsa := a.srv, a.js a.mu.RUnlock() @@ -1340,42 +1396,58 @@ func (a *Account) UpdateJetStreamLimits(limits *JetStreamAccountLimits) error { return NewJSNotEnabledForAccountError() } - if limits == nil { - limits = dynamicJSAccountLimits + if len(limits) == 0 { + limits = defaultJSAccountTiers } // Calculate the delta between what we have and what we want. - jsa.mu.Lock() - dl := diffCheckedLimits(&jsa.limits, limits) - jsa.mu.Unlock() + jsa.usageMu.RLock() + dl := diffCheckedLimits(jsa.limits, limits) + jsa.usageMu.RUnlock() js.mu.Lock() // Check the limits against existing reservations. - if err := js.sufficientResources(&dl); err != nil { + if err := js.sufficientResources(dl); err != nil { js.mu.Unlock() return err } js.mu.Unlock() // Update - jsa.mu.Lock() - jsa.limits = *limits - jsa.mu.Unlock() + jsa.usageMu.Lock() + jsa.limits = limits + jsa.usageMu.Unlock() return nil } -func diffCheckedLimits(a, b *JetStreamAccountLimits) JetStreamAccountLimits { - return JetStreamAccountLimits{ - MaxMemory: b.MaxMemory - a.MaxMemory, - MaxStore: b.MaxStore - a.MaxStore, +func diffCheckedLimits(a, b map[string]JetStreamAccountLimits) map[string]JetStreamAccountLimits { + diff := map[string]JetStreamAccountLimits{} + for t, la := range a { + // in a, not in b will return 0 + lb := b[t] + diff[t] = JetStreamAccountLimits{ + MaxMemory: lb.MaxMemory - la.MaxMemory, + MaxStore: lb.MaxStore - la.MaxStore, + } + } + for t, lb := range b { + if la, ok := a[t]; !ok { + // only in b not in a. (in a and b already covered) + diff[t] = JetStreamAccountLimits{ + MaxMemory: lb.MaxMemory - la.MaxMemory, + MaxStore: lb.MaxStore - la.MaxStore, + } + } } + return diff } // JetStreamUsage reports on JetStream usage and limits for an account. func (a *Account) JetStreamUsage() JetStreamAccountStats { a.mu.RLock() jsa, aname := a.js, a.Name + accJsLimits := a.jsLimits a.mu.RUnlock() var stats JetStreamAccountStats @@ -1383,26 +1455,80 @@ func (a *Account) JetStreamUsage() JetStreamAccountStats { js := jsa.js js.mu.RLock() jsa.mu.RLock() - stats.Memory = uint64(jsa.memTotal) - stats.Store = uint64(jsa.storeTotal) + jsa.usageMu.RLock() + stats.Memory, stats.Store = jsa.storageTotals() stats.Domain = js.config.Domain stats.API = JetStreamAPIStats{ Total: jsa.apiTotal, Errors: jsa.apiErrors, } + l, defaultTier := jsa.limits[_EMPTY_] + if defaultTier { + stats.Limits = l + } else { + skipped := 0 + stats.Tiers = make(map[string]JetStreamTier) + for t, total := range jsa.usage { + if _, ok := jsa.limits[t]; !ok && (*total) == (jsaStorage{}) { + // skip tiers not present that don't contain a count + // In case this shows an empty stream, that tier will be added when iterating over streams + skipped++ + } else { + stats.Tiers[t] = JetStreamTier{ + Memory: uint64(total.total.mem), + Store: uint64(total.total.store), + Limits: jsa.limits[t], + } + } + } + if len(accJsLimits) != len(jsa.usage)-skipped { + // insert unused limits + for t, lim := range accJsLimits { + if _, ok := stats.Tiers[t]; !ok { + stats.Tiers[t] = JetStreamTier{Limits: lim} + } + } + } + } + jsa.usageMu.RUnlock() if cc := jsa.js.cluster; cc != nil { sas := cc.streams[aname] - stats.Streams = len(sas) + if defaultTier { + stats.Streams = len(sas) + } for _, sa := range sas { stats.Consumers += len(sa.consumers) + if !defaultTier { + tier := tierName(sa.Config) + u, ok := stats.Tiers[tier] + if !ok { + u = JetStreamTier{} + } + u.Streams++ + stats.Streams++ + u.Consumers += len(sa.consumers) + stats.Tiers[tier] = u + } } } else { - stats.Streams = len(jsa.streams) + if defaultTier { + stats.Streams = len(jsa.streams) + } for _, mset := range jsa.streams { - stats.Consumers += mset.numConsumers() + consCount := mset.numConsumers() + stats.Consumers += consCount + if !defaultTier { + u, ok := stats.Tiers[mset.tier] + if !ok { + u = JetStreamTier{} + } + u.Streams++ + stats.Streams++ + u.Consumers += consCount + stats.Tiers[mset.tier] = u + } } } - stats.Limits = jsa.limits jsa.mu.RUnlock() js.mu.RUnlock() } @@ -1456,7 +1582,7 @@ func (a *Account) jetStreamConfigured() bool { } a.mu.RLock() defer a.mu.RUnlock() - return a.jsLimits != nil + return len(a.jsLimits) > 0 } // JetStreamEnabled is a helper to determine if jetstream is enabled for an account. @@ -1473,10 +1599,12 @@ func (a *Account) JetStreamEnabled() bool { func (jsa *jsAccount) remoteUpdateUsage(sub *subscription, c *client, _ *Account, subject, _ string, msg []byte) { const usageSize = 32 - jsa.mu.Lock() + // jsa.js.srv is immutable and guaranteed to no be nil, so no lock needed. s := jsa.js.srv + + jsa.usageMu.Lock() if len(msg) < usageSize { - jsa.mu.Unlock() + jsa.usageMu.Unlock() s.Warnf("Ignoring remote usage update with size too short") return } @@ -1485,56 +1613,92 @@ func (jsa *jsAccount) remoteUpdateUsage(sub *subscription, c *client, _ *Account rnode = subject[li+1:] } if rnode == _EMPTY_ { - jsa.mu.Unlock() + jsa.usageMu.Unlock() s.Warnf("Received remote usage update with no remote node") return } + rUsage, ok := jsa.rusage[rnode] + if !ok { + if jsa.rusage == nil { + jsa.rusage = make(map[string]*remoteUsage) + } + rUsage = &remoteUsage{tiers: make(map[string]*jsaUsage)} + jsa.rusage[rnode] = rUsage + } + updateTotal := func(tierName string, memUsed, storeUsed int64) { + total, ok := jsa.usage[tierName] + if !ok { + total = &jsaStorage{} + jsa.usage[tierName] = total + } + // Update the usage for this remote. + if usage := rUsage.tiers[tierName]; usage != nil { + // Decrement our old values. + total.total.mem -= usage.mem + total.total.store -= usage.store + usage.mem, usage.store = memUsed, storeUsed + } else { + rUsage.tiers[tierName] = &jsaUsage{memUsed, storeUsed} + } + total.total.mem += memUsed + total.total.store += storeUsed + } + var le = binary.LittleEndian - memUsed, storeUsed := int64(le.Uint64(msg[0:])), int64(le.Uint64(msg[8:])) apiTotal, apiErrors := le.Uint64(msg[16:]), le.Uint64(msg[24:]) + memUsed, storeUsed := int64(le.Uint64(msg[0:])), int64(le.Uint64(msg[8:])) - if jsa.rusage == nil { - jsa.rusage = make(map[string]*jsaUsage) - } - // Update the usage for this remote. - if usage := jsa.rusage[rnode]; usage != nil { - // Decrement our old values. - jsa.memTotal -= usage.mem - jsa.storeTotal -= usage.store - jsa.apiTotal -= usage.api - jsa.apiErrors -= usage.err - usage.mem, usage.store = memUsed, storeUsed - usage.api, usage.err = apiTotal, apiErrors - } else { - jsa.rusage[rnode] = &jsaUsage{memUsed, storeUsed, apiTotal, apiErrors} - } - jsa.memTotal += memUsed - jsa.storeTotal += storeUsed + // we later extended the data structure to support multiple tiers + excessRecordCnt := uint32(0) + tierName := _EMPTY_ + if len(msg) >= 44 { + excessRecordCnt = le.Uint32(msg[32:]) + length := le.Uint64(msg[36:]) + tierName = string(msg[44 : 44+length]) + msg = msg[44+length:] + } + updateTotal(tierName, memUsed, storeUsed) + for ; excessRecordCnt > 0 && len(msg) >= 24; excessRecordCnt-- { + memUsed, storeUsed := int64(le.Uint64(msg[0:])), int64(le.Uint64(msg[8:])) + length := le.Uint64(msg[16:]) + tierName = string(msg[24 : 24+length]) + msg = msg[24+length:] + updateTotal(tierName, memUsed, storeUsed) + } + jsa.apiTotal -= rUsage.api + jsa.apiErrors -= rUsage.err + rUsage.api = apiTotal + rUsage.err = apiErrors jsa.apiTotal += apiTotal jsa.apiErrors += apiErrors - jsa.mu.Unlock() + jsa.usageMu.Unlock() } // Updates accounting on in use memory and storage. This is called from locally // by the lower storage layers. -func (jsa *jsAccount) updateUsage(storeType StorageType, delta int64) { - var isClustered bool - // Ok to check jsa.js here w/o lock. +func (jsa *jsAccount) updateUsage(tierName string, storeType StorageType, delta int64) { + // jsa.js is immutable and cannot be nil, so ok w/o lock. js := jsa.js - if js != nil { - isClustered = js.isClustered() - } + // updateUsage() may be invoked under the mset's lock, so we can't get + // the js' lock to check if clustered. So use this function that make + // use of an atomic to do the check without having data race reports. + isClustered := js.isClusteredNoLock() - jsa.mu.Lock() - defer jsa.mu.Unlock() + jsa.usageMu.Lock() + defer jsa.usageMu.Unlock() + s, ok := jsa.usage[tierName] + if !ok { + s = &jsaStorage{} + jsa.usage[tierName] = s + } if storeType == MemoryStorage { - jsa.usage.mem += delta - jsa.memTotal += delta + s.local.mem += delta + s.total.mem += delta atomic.AddInt64(&js.memUsed, delta) } else { - jsa.usage.store += delta - jsa.storeTotal += delta + s.local.store += delta + s.total.store += delta atomic.AddInt64(&js.storeUsed, delta) } // Publish our local updates if in clustered mode. @@ -1546,8 +1710,8 @@ func (jsa *jsAccount) updateUsage(storeType StorageType, delta int64) { const usageTick = 1500 * time.Millisecond func (jsa *jsAccount) sendClusterUsageUpdateTimer() { - jsa.mu.Lock() - defer jsa.mu.Unlock() + jsa.usageMu.Lock() + defer jsa.usageMu.Unlock() jsa.sendClusterUsageUpdate() if jsa.utimer != nil { jsa.utimer.Reset(usageTick) @@ -1555,11 +1719,8 @@ func (jsa *jsAccount) sendClusterUsageUpdateTimer() { } // Send updates to our account usage for this server. -// Lock should be held. +// jsa.usageMu lock should be held. func (jsa *jsAccount) sendClusterUsageUpdate() { - if jsa.js == nil || jsa.js.srv == nil || jsa.sendq == nil { - return - } // These values are absolute so we can limit send rates. now := time.Now() if now.Sub(jsa.lupdate) < 250*time.Millisecond { @@ -1567,13 +1728,39 @@ func (jsa *jsAccount) sendClusterUsageUpdate() { } jsa.lupdate = now - b := make([]byte, 32) + lenUsage := len(jsa.usage) + if lenUsage == 0 { + return + } + // every base record contains mem/store/len(tier) as well as the tier name + l := 24 * lenUsage + for tier := range jsa.usage { + l += len(tier) + } + if lenUsage > 0 { + // first record contains api/usage errors as well as count for extra base records + l += 20 + } var le = binary.LittleEndian - le.PutUint64(b[0:], uint64(jsa.usage.mem)) - le.PutUint64(b[8:], uint64(jsa.usage.store)) - le.PutUint64(b[16:], uint64(jsa.usage.api)) - le.PutUint64(b[24:], uint64(jsa.usage.err)) - + b := make([]byte, l) + i := 0 + + for tier, usage := range jsa.usage { + le.PutUint64(b[i+0:], uint64(usage.local.mem)) + le.PutUint64(b[i+8:], uint64(usage.local.store)) + if i == 0 { + le.PutUint64(b[i+16:], jsa.usageApi) + le.PutUint64(b[i+24:], jsa.usageErr) + le.PutUint32(b[i+32:], uint32(len(jsa.usage)-1)) + le.PutUint64(b[i+36:], uint64(len(tier))) + copy(b[i+44:], tier) + i += 44 + len(tier) + } else { + le.PutUint64(b[i+16:], uint64(len(tier))) + copy(b[i+24:], tier) + i += 24 + len(tier) + } + } jsa.sendq.push(newPubMsg(nil, jsa.updatesPub, _EMPTY_, nil, nil, b, noCompression, false, false)) } @@ -1594,65 +1781,135 @@ func (js *jetStream) limitsExceeded(storeType StorageType) bool { return js.wouldExceedLimits(storeType, 0) } -func (jsa *jsAccount) limitsExceeded(storeType StorageType) bool { +func tierName(cfg *StreamConfig) string { + // TODO (mh) this is where we could select based off a placement tag as well "qos:tier" + return fmt.Sprintf("R%d", cfg.Replicas) +} + +func isSameTier(cfgA, cfgB *StreamConfig) bool { + // TODO (mh) this is where we could select based off a placement tag as well "qos:tier" + return cfgA.Replicas == cfgB.Replicas +} + +func (jsa *jsAccount) jetStreamAndClustered() (*jetStream, bool) { jsa.mu.RLock() - defer jsa.mu.RUnlock() + js := jsa.js + jsa.mu.RUnlock() + return js, js.isClustered() +} + +// jsa.usageMu read lock should be held. +func (jsa *jsAccount) selectLimits(cfg *StreamConfig) (JetStreamAccountLimits, string, bool) { + if selectedLimits, ok := jsa.limits[_EMPTY_]; ok { + return selectedLimits, _EMPTY_, true + } + tier := tierName(cfg) + if selectedLimits, ok := jsa.limits[tier]; ok { + return selectedLimits, tier, true + } + return JetStreamAccountLimits{}, _EMPTY_, false +} +// Lock should be held. +func (jsa *jsAccount) countStreams(tier string, cfg *StreamConfig) int { + streams := len(jsa.streams) + if tier != _EMPTY_ { + streams = 0 + for _, sa := range jsa.streams { + if isSameTier(&sa.cfg, cfg) { + streams++ + } + } + } + return streams +} + +// jsa.usageMu read lock (at least) should be held. +func (jsa *jsAccount) storageTotals() (uint64, uint64) { + mem := uint64(0) + store := uint64(0) + for _, sa := range jsa.usage { + mem += uint64(sa.total.mem) + store += uint64(sa.total.store) + } + return mem, store +} + +func (jsa *jsAccount) limitsExceeded(storeType StorageType, tierName string) (bool, *ApiError) { + jsa.usageMu.RLock() + defer jsa.usageMu.RUnlock() + + selectedLimits, ok := jsa.limits[tierName] + if !ok { + return true, NewJSNoLimitsError() + } + inUse := jsa.usage[tierName] + if inUse == nil { + // Imply totals of 0 + return false, nil + } if storeType == MemoryStorage { - if jsa.limits.MaxMemory >= 0 && jsa.memTotal > jsa.limits.MaxMemory { - return true + totalMem := inUse.total.mem + if selectedLimits.MemoryMaxStreamBytes > 0 && totalMem > selectedLimits.MemoryMaxStreamBytes { + return true, nil + } + if selectedLimits.MaxMemory >= 0 && totalMem > selectedLimits.MaxMemory { + return true, nil } } else { - if jsa.limits.MaxStore >= 0 && jsa.storeTotal > jsa.limits.MaxStore { - return true + totalStore := inUse.total.store + if selectedLimits.StoreMaxStreamBytes > 0 && totalStore > selectedLimits.StoreMaxStreamBytes { + return true, nil + } + if selectedLimits.MaxStore >= 0 && totalStore > selectedLimits.MaxStore { + return true, nil } } - return false + return false, nil } // Check account limits. -func (jsa *jsAccount) checkAccountLimits(config *StreamConfig) error { - return jsa.checkLimits(config, false) +// Read Lock should be held +func (js *jetStream) checkAccountLimits(selected *JetStreamAccountLimits, config *StreamConfig, currentRes int64) error { + return js.checkLimits(selected, config, false, currentRes, 0) } // Check account and server limits. -func (jsa *jsAccount) checkAllLimits(config *StreamConfig) error { - return jsa.checkLimits(config, true) +// Read Lock should be held +func (js *jetStream) checkAllLimits(selected *JetStreamAccountLimits, config *StreamConfig, currentRes, maxBytesOffset int64) error { + return js.checkLimits(selected, config, true, currentRes, maxBytesOffset) } // Check if a new proposed msg set while exceed our account limits. // Lock should be held. -func (jsa *jsAccount) checkLimits(config *StreamConfig, checkServer bool) error { - if jsa.limits.MaxStreams > 0 && len(jsa.streams) >= jsa.limits.MaxStreams { - return NewJSMaximumStreamsLimitError() - } +func (js *jetStream) checkLimits(selected *JetStreamAccountLimits, config *StreamConfig, checkServer bool, currentRes, maxBytesOffset int64) error { // Check MaxConsumers - if config.MaxConsumers > 0 && jsa.limits.MaxConsumers > 0 && config.MaxConsumers > jsa.limits.MaxConsumers { + if config.MaxConsumers > 0 && selected.MaxConsumers > 0 && config.MaxConsumers > selected.MaxConsumers { return NewJSMaximumConsumersLimitError() } - + // stream limit is checked separately on stream create only! // Check storage, memory or disk. - return jsa.checkBytesLimits(config.MaxBytes, config.Storage, config.Replicas, checkServer) + return js.checkBytesLimits(selected, config.MaxBytes, config.Storage, config.Replicas, checkServer, currentRes, maxBytesOffset) } // Check if additional bytes will exceed our account limits and optionally the server itself. // This should account for replicas. -// Lock should be held. -func (jsa *jsAccount) checkBytesLimits(addBytes int64, storage StorageType, replicas int, checkServer bool) error { +// Read Lock should be held. +func (js *jetStream) checkBytesLimits(selectedLimits *JetStreamAccountLimits, addBytes int64, storage StorageType, replicas int, checkServer bool, currentRes, maxBytesOffset int64) error { if replicas < 1 { replicas = 1 } if addBytes < 0 { addBytes = 1 } - js, totalBytes := jsa.js, addBytes*int64(replicas) + totalBytes := (addBytes * int64(replicas)) + maxBytesOffset switch storage { case MemoryStorage: // Account limits defined. - if jsa.limits.MaxMemory >= 0 { - if jsa.memReserved+totalBytes > jsa.limits.MaxMemory { + if selectedLimits.MaxMemory >= 0 { + if currentRes+totalBytes > selectedLimits.MaxMemory { return NewJSMemoryResourcesExceededError() } } @@ -1662,8 +1919,8 @@ func (jsa *jsAccount) checkBytesLimits(addBytes int64, storage StorageType, repl } case FileStorage: // Account limits defined. - if jsa.limits.MaxStore >= 0 { - if jsa.storeReserved+totalBytes > jsa.limits.MaxStore { + if selectedLimits.MaxStore >= 0 { + if currentRes+totalBytes > selectedLimits.MaxStore { return NewJSStorageResourcesExceededError() } } @@ -1686,16 +1943,18 @@ func (jsa *jsAccount) delete() { var ts []string jsa.mu.Lock() + // The update timer and subs need to be protected by usageMu lock + jsa.usageMu.Lock() if jsa.utimer != nil { jsa.utimer.Stop() jsa.utimer = nil } - if jsa.updatesSub != nil && jsa.js.srv != nil { s := jsa.js.srv s.sysUnsubscribe(jsa.updatesSub) jsa.updatesSub = nil } + jsa.usageMu.Unlock() for _, ms := range jsa.streams { streams = append(streams, ms) @@ -1747,7 +2006,7 @@ func (js *jetStream) usageStats() *JetStreamStats { // Check to see if we have enough system resources for this account. // Lock should be held. -func (js *jetStream) sufficientResources(limits *JetStreamAccountLimits) error { +func (js *jetStream) sufficientResources(limits map[string]JetStreamAccountLimits) error { // If we are clustered we do not really know how many resources will be ultimately available. // This needs to be handled out of band. // If we are a single server, we can make decisions here. @@ -1755,31 +2014,44 @@ func (js *jetStream) sufficientResources(limits *JetStreamAccountLimits) error { return nil } + totalMaxBytes := func(limits map[string]JetStreamAccountLimits) (int64, int64) { + totalMaxMemory := int64(0) + totalMaxStore := int64(0) + for _, l := range limits { + if l.MaxMemory > 0 { + totalMaxMemory += l.MaxMemory + } + if l.MaxStore > 0 { + totalMaxStore += l.MaxStore + } + } + return totalMaxMemory, totalMaxStore + } + + totalMaxMemory, totalMaxStore := totalMaxBytes(limits) + // Reserved is now specific to the MaxBytes for streams. - if js.memReserved+limits.MaxMemory > js.config.MaxMemory { + if js.memReserved+totalMaxMemory > js.config.MaxMemory { return NewJSMemoryResourcesExceededError() } - if js.storeReserved+limits.MaxStore > js.config.MaxStore { + if js.storeReserved+totalMaxStore > js.config.MaxStore { return NewJSStorageResourcesExceededError() } // Since we know if we are here we are single server mode, check the account reservations. var storeReserved, memReserved int64 for _, jsa := range js.accounts { - jsa.mu.RLock() - if jsa.limits.MaxMemory > 0 { - memReserved += jsa.limits.MaxMemory - } - if jsa.limits.MaxStore > 0 { - storeReserved += jsa.limits.MaxStore - } - jsa.mu.RUnlock() + jsa.usageMu.RLock() + maxMemory, maxStore := totalMaxBytes(jsa.limits) + jsa.usageMu.RUnlock() + memReserved += maxMemory + storeReserved += maxStore } - if memReserved+limits.MaxMemory > js.config.MaxMemory { + if memReserved+totalMaxMemory > js.config.MaxMemory { return NewJSMemoryResourcesExceededError() } - if storeReserved+limits.MaxStore > js.config.MaxStore { + if storeReserved+totalMaxStore > js.config.MaxStore { return NewJSStorageResourcesExceededError() } @@ -1931,9 +2203,9 @@ func (a *Account) addStreamTemplate(tc *StreamTemplateConfig) (*streamTemplate, // FIXME(dlc) - Hacky tcopy := tc.deepCopy() tcopy.Config.Name = "_" - cfg, err := checkStreamCfg(tcopy.Config) - if err != nil { - return nil, err + cfg, apiErr := s.checkStreamCfg(tcopy.Config, a) + if apiErr != nil { + return nil, apiErr } tcopy.Config = &cfg t := &streamTemplate{ @@ -2022,7 +2294,7 @@ func (t *streamTemplate) processInboundTemplateMsg(_ *subscription, pc *client, t.mu.Unlock() if atLimit { - c.Warnf("JetStream could not create stream for account %q on subject %q, at limit", acc.Name, subject) + c.RateLimitWarnf("JetStream could not create stream for account %q on subject %q, at limit", acc.Name, subject) return } @@ -2033,7 +2305,7 @@ func (t *streamTemplate) processInboundTemplateMsg(_ *subscription, pc *client, mset, err := acc.addStream(&cfg) if err != nil { acc.validateStreams(t) - c.Warnf("JetStream could not create stream for account %q on subject %q", acc.Name, subject) + c.RateLimitWarnf("JetStream could not create stream for account %q on subject %q: %v", acc.Name, subject, err) return } @@ -2253,7 +2525,7 @@ func validateJetStreamOptions(o *Options) error { } else { for _, acc := range o.Accounts { if a == acc.GetName() { - if acc.jsLimits != nil && domain != _EMPTY_ { + if len(acc.jsLimits) > 0 && domain != _EMPTY_ { return fmt.Errorf("default_js_domain contains account name %q with enabled JetStream", a) } found = true @@ -2317,3 +2589,13 @@ func validateJetStreamOptions(o *Options) error { } return nil } + +// We had a bug that set a default de dupe window on mirror, despite that being not a valid config +func fixCfgMirrorWithDedupWindow(cfg *StreamConfig) { + if cfg == nil || cfg.Mirror == nil { + return + } + if cfg.Duplicates != 0 { + cfg.Duplicates = 0 + } +} diff --git a/vendor/github.com/nats-io/nats-server/v2/server/jetstream_api.go b/vendor/github.com/nats-io/nats-server/v2/server/jetstream_api.go index 31c0de05..5a4da584 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/jetstream_api.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/jetstream_api.go @@ -183,8 +183,9 @@ const ( // jsAckT is the template for the ack message stream coming back from a consumer // when they ACK/NAK, etc a message. - jsAckT = "$JS.ACK.%s.%s" - jsAckPre = "$JS.ACK." + jsAckT = "$JS.ACK.%s.%s" + jsAckPre = "$JS.ACK." + jsAckPreLen = len(jsAckPre) // jsFlowControl is for flow control subjects. jsFlowControlPre = "$JS.FC." @@ -203,6 +204,9 @@ const ( // JSAdvisoryConsumerMaxDeliveryExceedPre is a notification published when a message exceeds its delivery threshold. JSAdvisoryConsumerMaxDeliveryExceedPre = "$JS.EVENT.ADVISORY.CONSUMER.MAX_DELIVERIES" + // JSAdvisoryConsumerMsgNakPre is a notification published when a message has been naked + JSAdvisoryConsumerMsgNakPre = "$JS.EVENT.ADVISORY.CONSUMER.MSG_NAKED" + // JSAdvisoryConsumerMsgTerminatedPre is a notification published when a message has been terminated. JSAdvisoryConsumerMsgTerminatedPre = "$JS.EVENT.ADVISORY.CONSUMER.MSG_TERMINATED" @@ -289,7 +293,8 @@ func generateJSMappingTable(domain string) map[string]string { const JSMaxDescriptionLen = 4 * 1024 // JSMaxNameLen is the maximum name lengths for streams, consumers and templates. -const JSMaxNameLen = 256 +// Picked 255 as it seems to be a widely used file name limit +const JSMaxNameLen = 255 // Responses for API calls. @@ -603,6 +608,7 @@ const JSApiConsumerListResponseType = "io.nats.jetstream.api.v1.consumer_list_re type JSApiConsumerGetNextRequest struct { Expires time.Duration `json:"expires,omitempty"` Batch int `json:"batch,omitempty"` + MaxBytes int `json:"max_bytes,omitempty"` NoWait bool `json:"no_wait,omitempty"` Heartbeat time.Duration `json:"idle_heartbeat,omitempty"` } @@ -643,16 +649,22 @@ type JSApiStreamTemplateNamesResponse struct { const JSApiStreamTemplateNamesResponseType = "io.nats.jetstream.api.v1.stream_template_names_response" -// Default max API calls outstanding. -const defaultMaxJSApiOut = int64(4096) - -// Max API calls outstanding. -var maxJSApiOut = defaultMaxJSApiOut +// Structure that holds state for a JetStream API request that is processed +// in a separate long-lived go routine. This is to avoid possibly blocking +// ROUTE and GATEWAY connections. +type jsAPIRoutedReq struct { + jsub *subscription + sub *subscription + acc *Account + subject string + reply string + msg []byte + pa pubArg +} func (js *jetStream) apiDispatch(sub *subscription, c *client, acc *Account, subject, reply string, rmsg []byte) { - js.mu.RLock() + // No lock needed, those are immutable. s, rr := js.srv, js.apiSubs.Match(subject) - js.mu.RUnlock() hdr, _ := c.msgParts(rmsg) if len(getHeader(ClientInfoHdr, hdr)) == 0 { @@ -682,36 +694,36 @@ func (js *jetStream) apiDispatch(sub *subscription, c *client, acc *Account, sub } // If we are here we have received this request over a non client connection. - // We need to make sure not to block. We will spin a Go routine per but also make - // sure we do not have too many outstanding. - if apiOut := atomic.AddInt64(&js.apiInflight, 1); apiOut > maxJSApiOut { - atomic.AddInt64(&js.apiInflight, -1) - ci, acc, _, msg, err := s.getRequestInfo(c, rmsg) - if err == nil { - resp := &ApiResponse{Type: JSApiOverloadedType, Error: NewJSInsufficientResourcesError()} - s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) - } else { - s.Warnf(badAPIRequestT, rmsg) - } - s.Warnf("JetStream API limit exceeded: %d calls outstanding", apiOut) - return - } + // We need to make sure not to block. We will send the request to a long-lived + // go routine. - // If we are here we can properly dispatch this API call. - // Copy the message and the client. Client for the pubArgs - // but note the JSAPI only uses the hdr index to piece apart - // the header from the msg body. No other references are needed. - // FIXME(dlc) - Should cleanup eventually and make sending - // and receiving internal messages more formal. - rmsg = copyBytes(rmsg) + // Copy the state. Note the JSAPI only uses the hdr index to piece apart the + // header from the msg body. No other references are needed. + s.jsAPIRoutedReqs.push(&jsAPIRoutedReq{jsub, sub, acc, subject, reply, copyBytes(rmsg), c.pa}) +} + +func (s *Server) processJSAPIRoutedRequests() { + defer s.grWG.Done() + + s.mu.Lock() + queue := s.jsAPIRoutedReqs client := &client{srv: s, kind: JETSTREAM} - client.pa = c.pa + s.mu.Unlock() - // Dispatch the API call to its own Go routine. - go func() { - jsub.icb(sub, client, acc, subject, reply, rmsg) - atomic.AddInt64(&js.apiInflight, -1) - }() + for { + select { + case <-queue.ch: + reqs := queue.pop() + for _, req := range reqs { + r := req.(*jsAPIRoutedReq) + client.pa = r.pa + r.jsub.icb(r.sub, client, r.acc, r.subject, r.reply, r.msg) + } + queue.recycle(&reqs) + case <-s.quitCh: + return + } + } } func (s *Server) setJetStreamExportSubs() error { @@ -720,6 +732,11 @@ func (s *Server) setJetStreamExportSubs() error { return NewJSNotEnabledError() } + // Start the go routine that will process API requests received by the + // subscription below when they are coming from routes, etc.. + s.jsAPIRoutedReqs = s.newIPQueue("Routed JS API Requests") + s.startGoRoutine(s.processJSAPIRoutedRequests) + // This is the catch all now for all JetStream API calls. if _, err := s.sysSubscribe(jsAllAPI, js.apiDispatch); err != nil { return err @@ -845,12 +862,12 @@ func (a *Account) trackAPI() { jsa := a.js a.mu.RUnlock() if jsa != nil { - jsa.mu.Lock() - jsa.usage.api++ + jsa.usageMu.Lock() + jsa.usageApi++ jsa.apiTotal++ jsa.sendClusterUsageUpdate() atomic.AddInt64(&jsa.js.apiTotal, 1) - jsa.mu.Unlock() + jsa.usageMu.Unlock() } } @@ -859,15 +876,15 @@ func (a *Account) trackAPIErr() { jsa := a.js a.mu.RUnlock() if jsa != nil { - jsa.mu.Lock() - jsa.usage.api++ + jsa.usageMu.Lock() + jsa.usageApi++ jsa.apiTotal++ - jsa.usage.err++ + jsa.usageErr++ jsa.apiErrors++ jsa.sendClusterUsageUpdate() atomic.AddInt64(&jsa.js.apiTotal, 1) atomic.AddInt64(&jsa.js.apiErrors, 1) - jsa.mu.Unlock() + jsa.usageMu.Unlock() } } @@ -1145,6 +1162,31 @@ func (s *Server) jsonResponse(v interface{}) string { return string(b) } +// Read lock must be held +func (jsa *jsAccount) tieredReservation(tier string, cfg *StreamConfig) int64 { + reservation := int64(0) + if tier == _EMPTY_ { + for _, sa := range jsa.streams { + if sa.cfg.MaxBytes > 0 { + if sa.cfg.Storage == cfg.Storage && sa.cfg.Name != cfg.Name { + reservation += (int64(sa.cfg.Replicas) * sa.cfg.MaxBytes) + } + } + } + } else { + for _, sa := range jsa.streams { + if sa.cfg.Replicas == cfg.Replicas { + if sa.cfg.MaxBytes > 0 { + if isSameTier(&sa.cfg, cfg) && sa.cfg.Name != cfg.Name { + reservation += (int64(sa.cfg.Replicas) * sa.cfg.MaxBytes) + } + } + } + } + } + return reservation +} + // Request to create a stream. func (s *Server) jsStreamCreateRequest(sub *subscription, c *client, _ *Account, subject, reply string, rmsg []byte) { if c == nil || !s.JetStreamEnabled() { @@ -1182,6 +1224,7 @@ func (s *Server) jsStreamCreateRequest(sub *subscription, c *client, _ *Account, } return } + var cfg StreamConfig if err := json.Unmarshal(msg, &cfg); err != nil { resp.Error = NewJSInvalidJSONError() @@ -1196,132 +1239,9 @@ func (s *Server) jsStreamCreateRequest(sub *subscription, c *client, _ *Account, return } - hasStream := func(streamName string) (bool, int32, []string) { - var exists bool - var maxMsgSize int32 - var subs []string - if s.JetStreamIsClustered() { - if js, _ := s.getJetStreamCluster(); js != nil { - js.mu.RLock() - if sa := js.streamAssignment(acc.Name, streamName); sa != nil { - maxMsgSize = sa.Config.MaxMsgSize - subs = sa.Config.Subjects - exists = true - } - js.mu.RUnlock() - } - } else if mset, err := acc.lookupStream(streamName); err == nil { - maxMsgSize = mset.cfg.MaxMsgSize - subs = mset.cfg.Subjects - exists = true - } - return exists, maxMsgSize, subs - } - - var streamSubs []string - var deliveryPrefixes []string - var apiPrefixes []string - - // Do some pre-checking for mirror config to avoid cycles in clustered mode. - if cfg.Mirror != nil { - if len(cfg.Subjects) > 0 { - resp.Error = NewJSMirrorWithSubjectsError() - s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) - return - } - if len(cfg.Sources) > 0 { - resp.Error = NewJSMirrorWithSourcesError() - s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) - return - } - if cfg.Mirror.FilterSubject != _EMPTY_ { - resp.Error = NewJSMirrorWithSubjectFiltersError() - s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) - return - } - if cfg.Mirror.OptStartSeq > 0 && cfg.Mirror.OptStartTime != nil { - resp.Error = NewJSMirrorWithStartSeqAndTimeError() - s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) - return - } - if cfg.Duplicates != time.Duration(0) { - resp.Error = &ApiError{Code: 400, Description: "stream mirrors do not make use of a de-duplication window"} - s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) - return - } - // We do not require other stream to exist anymore, but if we can see it check payloads. - exists, maxMsgSize, subs := hasStream(cfg.Mirror.Name) - if len(subs) > 0 { - streamSubs = append(streamSubs, subs...) - } - if exists && cfg.MaxMsgSize > 0 && maxMsgSize > 0 && cfg.MaxMsgSize < maxMsgSize { - resp.Error = NewJSMirrorMaxMessageSizeTooBigError() - s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) - return - } - if cfg.Mirror.External != nil { - if cfg.Mirror.External.DeliverPrefix != _EMPTY_ { - deliveryPrefixes = append(deliveryPrefixes, cfg.Mirror.External.DeliverPrefix) - } - if cfg.Mirror.External.ApiPrefix != _EMPTY_ { - apiPrefixes = append(apiPrefixes, cfg.Mirror.External.ApiPrefix) - } - } - } - if len(cfg.Sources) > 0 { - for _, src := range cfg.Sources { - if src.External == nil { - continue - } - exists, maxMsgSize, subs := hasStream(src.Name) - if len(subs) > 0 { - streamSubs = append(streamSubs, subs...) - } - if src.External.DeliverPrefix != _EMPTY_ { - deliveryPrefixes = append(deliveryPrefixes, src.External.DeliverPrefix) - } - if src.External.ApiPrefix != _EMPTY_ { - apiPrefixes = append(apiPrefixes, src.External.ApiPrefix) - } - if exists && cfg.MaxMsgSize > 0 && maxMsgSize > 0 && cfg.MaxMsgSize < maxMsgSize { - resp.Error = NewJSSourceMaxMessageSizeTooBigError() - s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) - return - } - } - } - // check prefix overlap with subjects - for _, pfx := range deliveryPrefixes { - if !IsValidPublishSubject(pfx) { - resp.Error = NewJSStreamInvalidExternalDeliverySubjError(pfx) - s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) - return - } - for _, sub := range streamSubs { - if SubjectsCollide(sub, fmt.Sprintf("%s.%s", pfx, sub)) { - resp.Error = NewJSStreamExternalDelPrefixOverlapsError(pfx, sub) - s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) - return - } - } - } - // check if api prefixes overlap - for _, apiPfx := range apiPrefixes { - if !IsValidPublishSubject(apiPfx) { - resp.Error = &ApiError{Code: 400, Description: fmt.Sprintf("stream external api prefix %q must be a valid subject without wildcards", apiPfx)} - s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) - return - } - if SubjectsCollide(apiPfx, JSApiPrefix) { - resp.Error = NewJSStreamExternalApiOverlapError(apiPfx, JSApiPrefix) - s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) - return - } - } - - // Check for MaxBytes required. - if acc.maxBytesRequired() && cfg.MaxBytes <= 0 { - resp.Error = NewJSStreamMaxBytesRequiredError() + // Can't create a stream with a sealed state. + if cfg.Sealed { + resp.Error = NewJSStreamInvalidConfigError(fmt.Errorf("stream configuration for create can not be sealed")) s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } @@ -1332,13 +1252,23 @@ func (s *Server) jsStreamCreateRequest(sub *subscription, c *client, _ *Account, return } + if err := acc.jsNonClusteredStreamLimitsCheck(&cfg); err != nil { + resp.Error = err + s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) + return + } + mset, err := acc.addStream(&cfg) if err != nil { resp.Error = NewJSStreamCreateError(err, Unless(err)) s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } - resp.StreamInfo = &StreamInfo{Created: mset.createdTime(), State: mset.state(), Config: mset.config()} + resp.StreamInfo = &StreamInfo{ + Created: mset.createdTime(), + State: mset.state(), + Config: mset.config(), + } resp.DidCreate = true s.sendAPIResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(resp)) } @@ -1388,9 +1318,9 @@ func (s *Server) jsStreamUpdateRequest(sub *subscription, c *client, _ *Account, return } - cfg, err := checkStreamCfg(&ncfg) - if err != nil { - resp.Error = NewJSStreamInvalidConfigError(err) + cfg, apiErr := s.checkStreamCfg(&ncfg, acc) + if apiErr != nil { + resp.Error = apiErr s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) return } @@ -1402,8 +1332,15 @@ func (s *Server) jsStreamUpdateRequest(sub *subscription, c *client, _ *Account, return } + // Handle clustered version here. if s.JetStreamIsClustered() { - s.jsClusteredStreamUpdateRequest(ci, acc, subject, reply, rmsg, &cfg) + // If we are inline with client, we still may need to do a callout for stream info + // during this call, so place in Go routine to not block client. + if c.kind != ROUTER && c.kind != GATEWAY { + go s.jsClusteredStreamUpdateRequest(ci, acc, subject, reply, rmsg, &cfg) + } else { + s.jsClusteredStreamUpdateRequest(ci, acc, subject, reply, rmsg, &cfg) + } return } @@ -1420,9 +1357,14 @@ func (s *Server) jsStreamUpdateRequest(sub *subscription, c *client, _ *Account, return } - js, _ := s.getJetStreamCluster() - - resp.StreamInfo = &StreamInfo{Created: mset.createdTime(), State: mset.state(), Config: mset.config(), Cluster: js.clusterInfo(mset.raftGroup())} + resp.StreamInfo = &StreamInfo{ + Created: mset.createdTime(), + State: mset.state(), + Config: mset.config(), + Domain: s.getOpts().JetStreamDomain, + Mirror: mset.mirrorInfo(), + Sources: mset.sourcesInfo(), + } s.sendAPIResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(resp)) } @@ -1609,7 +1551,7 @@ func (s *Server) jsStreamListRequest(sub *subscription, c *client, _ *Account, s // Clustered mode will invoke a scatter and gather. if s.JetStreamIsClustered() { - // Need to copy these off before sending.. + // Need to copy these off before sending.. don't move this inside startGoRoutine!!! msg = copyBytes(msg) s.startGoRoutine(func() { s.jsClusteredStreamListRequest(acc, ci, filter, offset, subject, reply, msg) }) return @@ -1634,13 +1576,15 @@ func (s *Server) jsStreamListRequest(sub *subscription, c *client, _ *Account, s } for _, mset := range msets[offset:] { + config := mset.config() resp.Streams = append(resp.Streams, &StreamInfo{ Created: mset.createdTime(), State: mset.state(), - Config: mset.config(), + Config: config, + Domain: s.getOpts().JetStreamDomain, Mirror: mset.mirrorInfo(), - Sources: mset.sourcesInfo()}, - ) + Sources: mset.sourcesInfo(), + }) if len(resp.Streams) >= JSApiListLimit { break } @@ -1684,8 +1628,10 @@ func (s *Server) jsStreamInfoRequest(sub *subscription, c *client, a *Account, s js.mu.RLock() isLeader, sa := cc.isLeader(), js.streamAssignment(acc.Name, streamName) + var offline bool if sa != nil { clusterWideConsCount = len(sa.consumers) + offline = s.allPeersOffline(sa.Group) } js.mu.RUnlock() @@ -1709,6 +1655,10 @@ func (s *Server) jsStreamInfoRequest(sub *subscription, c *client, a *Account, s s.sendDelayedAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp), nil) } return + } else if isLeader && offline { + resp.Error = NewJSStreamOfflineError() + s.sendDelayedAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp), nil) + return } // Check to see if we are a member of the group and if the group has no leader. @@ -1720,8 +1670,27 @@ func (s *Server) jsStreamInfoRequest(sub *subscription, c *client, a *Account, s resp.Error = NewJSClusterNotAvailError() // Delaying an error response gives the leader a chance to respond before us s.sendDelayedAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp), sa.Group) + return + } + + // We may be in process of electing a leader, but if this is a scale up from 1 we will still be the state leader + // while the new members work through the election and catchup process. + // Double check for that instead of exiting here and being silent. e.g. nats stream update test --replicas=3 + js.mu.RLock() + rg := sa.Group + var ourID string + if cc.meta != nil { + ourID = cc.meta.ID() + } + bail := !rg.isMember(ourID) + if !bail { + // We know we are a member here, if this group is new and we are preferred allow us to answer. + bail = rg.Preferred != ourID || time.Since(rg.node.Created()) > lostQuorumInterval + } + js.mu.RUnlock() + if bail { + return } - return } } @@ -1756,11 +1725,14 @@ func (s *Server) jsStreamInfoRequest(sub *subscription, c *client, a *Account, s js, _ := s.getJetStreamCluster() resp.StreamInfo = &StreamInfo{ - Created: mset.createdTime(), - State: mset.stateWithDetail(details), - Config: config, - Domain: s.getOpts().JetStreamDomain, - Cluster: js.clusterInfo(mset.raftGroup()), + Created: mset.createdTime(), + State: mset.stateWithDetail(details), + Config: config, + Domain: s.getOpts().JetStreamDomain, + Cluster: js.clusterInfo(mset.raftGroup()), + Mirror: mset.mirrorInfo(), + Sources: mset.sourcesInfo(), + Alternates: js.streamAlternates(ci, config.Name), } if clusterWideConsCount > 0 { resp.StreamInfo.State.Consumers = clusterWideConsCount @@ -1790,7 +1762,7 @@ func (s *Server) jsStreamInfoRequest(sub *subscription, c *client, a *Account, s } // Check for out of band catchups. if mset.hasCatchupPeers() { - mset.checkClusterInfo(resp.StreamInfo) + mset.checkClusterInfo(resp.StreamInfo.Cluster) } s.sendAPIResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(resp)) @@ -1877,6 +1849,9 @@ func (s *Server) jsStreamLeaderStepDownRequest(sub *subscription, c *client, _ * // Call actual stepdown. if mset != nil { if node := mset.raftNode(); node != nil { + mset.setLeader(false) + // TODO (mh) eventually make sure all go routines exited and all channels are cleared + time.Sleep(250 * time.Millisecond) node.StepDown() } } @@ -1978,7 +1953,12 @@ func (s *Server) jsConsumerLeaderStepDownRequest(sub *subscription, c *client, _ } // Call actual stepdown. - o.raftNode().StepDown() + if n := o.raftNode(); n != nil { + o.setLeader(false) + // TODO (mh) eventually make sure all go routines exited and all channels are cleared + time.Sleep(250 * time.Millisecond) + n.StepDown() + } resp.Success = true s.sendAPIResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(resp)) @@ -2530,16 +2510,13 @@ func (s *Server) jsMsgGetRequest(sub *subscription, c *client, _ *Account, subje return } - var subj string - var hdr []byte - var data []byte - var ts int64 - seq := req.Seq + var svp StoreMsg + var sm *StoreMsg if req.Seq > 0 { - subj, hdr, data, ts, err = mset.store.LoadMsg(req.Seq) + sm, err = mset.store.LoadMsg(req.Seq, &svp) } else { - subj, seq, hdr, data, ts, err = mset.store.LoadLastMsg(req.LastFor) + sm, err = mset.store.LoadLastMsg(req.LastFor, &svp) } if err != nil { resp.Error = NewJSNoMessageFoundError() @@ -2547,11 +2524,11 @@ func (s *Server) jsMsgGetRequest(sub *subscription, c *client, _ *Account, subje return } resp.Message = &StoredMsg{ - Subject: subj, - Sequence: seq, - Header: hdr, - Data: data, - Time: time.Unix(0, ts).UTC(), + Subject: sm.subj, + Sequence: sm.seq, + Header: sm.hdr, + Data: sm.msg, + Time: time.Unix(0, sm.ts).UTC(), } s.sendAPIResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(resp)) } @@ -2677,6 +2654,23 @@ func (s *Server) jsStreamPurgeRequest(sub *subscription, c *client, _ *Account, s.sendAPIResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(resp)) } +func (acc *Account) jsNonClusteredStreamLimitsCheck(cfg *StreamConfig) *ApiError { + selectedLimits, tier, jsa, apiErr := acc.selectLimits(cfg) + if apiErr != nil { + return apiErr + } + jsa.mu.RLock() + defer jsa.mu.RUnlock() + if selectedLimits.MaxStreams > 0 && jsa.countStreams(tier, cfg) >= selectedLimits.MaxStreams { + return NewJSMaximumStreamsLimitError() + } + reserved := jsa.tieredReservation(tier, cfg) + if err := jsa.js.checkAllLimits(selectedLimits, cfg, reserved, 0); err != nil { + return NewJSStreamLimitsError(err, Unless(err)) + } + return nil +} + // Request to restore a stream. func (s *Server) jsStreamRestoreRequest(sub *subscription, c *client, _ *Account, subject, reply string, rmsg []byte) { if c == nil || !s.JetStreamIsLeader() { @@ -2713,11 +2707,25 @@ func (s *Server) jsStreamRestoreRequest(sub *subscription, c *client, _ *Account req.Config.Name = stream } + // check stream config at the start of the restore process, not at the end + cfg, apiErr := s.checkStreamCfg(&req.Config, acc) + if apiErr != nil { + resp.Error = apiErr + s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) + return + } + if s.JetStreamIsClustered() { s.jsClusteredStreamRestoreRequest(ci, acc, &req, stream, subject, reply, rmsg) return } + if err := acc.jsNonClusteredStreamLimitsCheck(&cfg); err != nil { + resp.Error = err + s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) + return + } + if _, err := acc.lookupStream(stream); err == nil { resp.Error = NewJSStreamNameExistError() s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) @@ -2782,7 +2790,7 @@ func (s *Server) processStreamRestore(ci *ClientInfo, acc *Account, cfg *StreamC // For signaling to upper layers. resultCh := make(chan result, 1) - activeQ := newIPQueue() // of int + activeQ := s.newIPQueue(fmt.Sprintf("[ACC:%s] stream '%s' restore", acc.Name, streamName)) // of int var total int @@ -2861,6 +2869,7 @@ func (s *Server) processStreamRestore(ci *ClientInfo, acc *Account, cfg *StreamC tfile.Close() os.Remove(tfile.Name()) sub.client.processUnsub(sub.sid) + activeQ.unregister() }() const activityInterval = 5 * time.Second @@ -3057,8 +3066,8 @@ func (s *Server) jsStreamSnapshotRequest(sub *subscription, c *client, _ *Accoun } // Default chunk size for now. -const defaultSnapshotChunkSize = 256 * 1024 -const defaultSnapshotWindowSize = 32 * 1024 * 1024 // 32MB +const defaultSnapshotChunkSize = 128 * 1024 +const defaultSnapshotWindowSize = 8 * 1024 * 1024 // 8MB // streamSnapshot will stream out our snapshot to the reply subject. func (s *Server) streamSnapshot(ci *ClientInfo, acc *Account, mset *stream, sr *SnapshotResult, req *JSApiStreamSnapshotRequest) { @@ -3121,7 +3130,7 @@ func (s *Server) streamSnapshot(ci *ClientInfo, acc *Account, mset *stream, sr * } // Wait on acks for flow control if past our window size. - // Wait up to 1ms for now if no acks received. + // Wait up to 10ms for now if no acks received. if atomic.LoadInt32(&out) > defaultSnapshotWindowSize { select { case <-acks: @@ -3177,32 +3186,19 @@ func (s *Server) jsConsumerCreate(sub *subscription, c *client, a *Account, subj return } - // We reject if flow control is set without heartbeats. - if req.Config.FlowControl && req.Config.Heartbeat == 0 { - resp.Error = NewJSConsumerWithFlowControlNeedsHeartbeatsError() - s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) - return - } - - // Make sure we have sane defaults. - setConsumerConfigDefaults(&req.Config) - - // Check if we have a BackOff defined that MaxDeliver is within range etc. - if lbo := len(req.Config.BackOff); lbo > 0 && req.Config.MaxDeliver <= lbo { - resp.Error = NewJSConsumerMaxDeliverBackoffError() - s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) - return - } + var js *jetStream + isClustered := s.JetStreamIsClustered() // Determine if we should proceed here when we are in clustered mode. - if s.JetStreamIsClustered() { + if isClustered { if req.Config.Direct { // Check to see if we have this stream and are the stream leader. if !acc.JetStreamIsStreamLeader(streamName) { return } } else { - js, cc := s.getJetStreamCluster() + var cc *jetStreamCluster + js, cc = s.getJetStreamCluster() if js == nil || cc == nil { return } @@ -3262,11 +3258,18 @@ func (s *Server) jsConsumerCreate(sub *subscription, c *client, a *Account, subj } } - if s.JetStreamIsClustered() && !req.Config.Direct { + if isClustered && !req.Config.Direct { s.jsClusteredConsumerRequest(ci, acc, subject, reply, rmsg, req.Stream, &req.Config) return } + // If we are here we are single server mode. + if req.Config.Replicas > 1 { + resp.Error = NewJSStreamReplicasNotSupportedError() + s.sendAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp)) + return + } + stream, err := acc.lookupStream(req.Stream) if err != nil { resp.Error = NewJSStreamNotFoundError(Unless(err)) @@ -3461,6 +3464,7 @@ func (s *Server) jsConsumerListRequest(sub *subscription, c *client, _ *Account, // Clustered mode will invoke a scatter and gather. if s.JetStreamIsClustered() { + // Need to copy these off before sending.. don't move this inside startGoRoutine!!! msg = copyBytes(msg) s.startGoRoutine(func() { s.jsClusteredConsumerListRequest(acc, ci, offset, streamName, subject, reply, msg) @@ -3530,6 +3534,10 @@ func (s *Server) jsConsumerInfoRequest(sub *subscription, c *client, _ *Account, js.mu.RLock() isLeader, sa, ca := cc.isLeader(), js.streamAssignment(acc.Name, streamName), js.consumerAssignment(acc.Name, streamName, consumerName) ourID := cc.meta.ID() + var offline bool + if ca != nil { + offline = s.allPeersOffline(ca.Group) + } js.mu.RUnlock() if isLeader && ca == nil { @@ -3557,6 +3565,10 @@ func (s *Server) jsConsumerInfoRequest(sub *subscription, c *client, _ *Account, s.sendDelayedAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp), nil) } return + } else if isLeader && offline { + resp.Error = NewJSConsumerOfflineError() + s.sendDelayedAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp), nil) + return } // Check to see if we are a member of the group and if the group has no leader. @@ -3574,14 +3586,15 @@ func (s *Server) jsConsumerInfoRequest(sub *subscription, c *client, _ *Account, s.sendDelayedAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp), ca.Group) return } - if ca == nil { - return - } // We have a consumer assignment. js.mu.RLock() var node RaftNode + var leaderNotPartOfGroup bool if rg := ca.Group; rg != nil && rg.node != nil && rg.isMember(ourID) { node = rg.node + if gl := node.GroupLeader(); gl != _EMPTY_ && !rg.isMember(gl) { + leaderNotPartOfGroup = true + } } js.mu.RUnlock() // Check if we should ignore all together. @@ -3599,7 +3612,12 @@ func (s *Server) jsConsumerInfoRequest(sub *subscription, c *client, _ *Account, } return } + // If we are a member and we have a group leader or we had a previous leader consider bailing out. if node != nil && (node.GroupLeader() != _EMPTY_ || node.HadPreviousLeader()) { + if leaderNotPartOfGroup { + resp.Error = NewJSConsumerOfflineError() + s.sendDelayedAPIErrResponse(ci, acc, subject, reply, string(msg), s.jsonResponse(&resp), nil) + } return } // If we are here we are a member and this is just a new consumer that does not have a leader yet. diff --git a/vendor/github.com/nats-io/nats-server/v2/server/jetstream_cluster.go b/vendor/github.com/nats-io/nats-server/v2/server/jetstream_cluster.go index 584c55d3..7fc1350e 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/jetstream_cluster.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/jetstream_cluster.go @@ -350,7 +350,8 @@ func (cc *jetStreamCluster) isCurrent() bool { return cc.meta.Current() } -// isStreamCurrent will determine if this node is a participant for the stream and if its up to date. +// isStreamCurrent will determine if the stream is up to date. +// For R1 it will make sure the stream is present on this server. // Read lock should be held. func (cc *jetStreamCluster) isStreamCurrent(account, stream string) bool { if cc == nil { @@ -366,12 +367,11 @@ func (cc *jetStreamCluster) isStreamCurrent(account, stream string) bool { return false } rg := sa.Group - if rg == nil || rg.node == nil { + if rg == nil { return false } - isCurrent := rg.node.Current() - if isCurrent { + if rg.node == nil || rg.node.Current() { // Check if we are processing a snapshot and are catching up. acc, err := cc.s.LookupAccount(account) if err != nil { @@ -384,9 +384,37 @@ func (cc *jetStreamCluster) isStreamCurrent(account, stream string) bool { if mset.isCatchingUp() { return false } + // Success. + return true } - return isCurrent + return false +} + +// isConsumerCurrent will determine if the consumer is up to date. +// For R1 it will make sure the consunmer is present on this server. +// Read lock should be held. +func (cc *jetStreamCluster) isConsumerCurrent(account, stream, consumer string) bool { + if cc == nil { + // Non-clustered mode + return true + } + acc, err := cc.s.LookupAccount(account) + if err != nil { + return false + } + mset, err := acc.lookupStream(stream) + if err != nil { + return false + } + o := mset.lookupConsumer(consumer) + if o == nil { + return false + } + if n := o.raftNode(); n != nil && !n.Current() { + return false + } + return true } func (a *Account) getJetStreamFromAccount() (*Server, *jetStream, *jsAccount) { @@ -495,6 +523,14 @@ func (js *jetStream) isClustered() bool { return isClustered } +// isClusteredNoLock returns if we are clustered, but unlike isClustered() does +// not use the jetstream's lock, instead, uses an atomic operation. +// There are situations where some code wants to know if we are clustered but +// can't use js.isClustered() without causing a lock inversion. +func (js *jetStream) isClusteredNoLock() bool { + return atomic.LoadInt32(&js.clustered) == 1 +} + func (js *jetStream) setupMetaGroup() error { s := js.srv s.Noticef("Creating JetStream metadata controller") @@ -557,7 +593,7 @@ func (js *jetStream) setupMetaGroup() error { } // Start up our meta node. - n, err := s.startRaftNode(cfg) + n, err := s.startRaftNode(sysAcc.GetName(), cfg) if err != nil { s.Warnf("Could not start metadata controller: %v", err) return err @@ -579,6 +615,7 @@ func (js *jetStream) setupMetaGroup() error { s: s, c: c, } + atomic.StoreInt32(&js.clustered, 1) c.registerWithAccount(sacc) js.srv.startGoRoutine(js.monitorCluster) @@ -630,6 +667,9 @@ func (js *jetStream) isGroupLeaderless(rg *raftGroup) bool { cc := js.cluster // If we are not a member we can not say.. + if cc.meta == nil { + return false + } if !rg.isMember(cc.meta.ID()) { return false } @@ -773,6 +813,12 @@ func (cc *jetStreamCluster) isConsumerLeader(account, stream, consumer string) b return false } +// During recovery track any stream and consumer delete operations. +type recoveryRemovals struct { + streams map[string]*streamAssignment + consumers map[string]*consumerAssignment +} + func (js *jetStream) monitorCluster() { s, n := js.server(), js.getMetaGroup() qch, lch, aq := n.QuitC(), n.LeadChangeC(), n.ApplyQ() @@ -816,6 +862,11 @@ func (js *jetStream) monitorCluster() { } } + rm := &recoveryRemovals{ + streams: make(map[string]*streamAssignment), + consumers: make(map[string]*consumerAssignment), + } + for { select { case <-s.quitCh: @@ -828,12 +879,21 @@ func (js *jetStream) monitorCluster() { if cei == nil { // Signals we have replayed all of our metadata. isRecovering = false + // Process any removes that are still valid after recovery. + for _, sa := range rm.streams { + js.processStreamRemoval(sa) + } + for _, ca := range rm.consumers { + js.processConsumerRemoval(ca) + } + // Clear. + rm = nil s.Debugf("Recovered JetStream cluster metadata") continue } ce := cei.(*CommittedEntry) // FIXME(dlc) - Deal with errors. - if didSnap, didRemoval, err := js.applyMetaEntries(ce.Entries, isRecovering); err == nil { + if didSnap, didRemoval, err := js.applyMetaEntries(ce.Entries, isRecovering, rm); err == nil { _, nb := n.Applied(ce.Index) if js.hasPeerEntries(ce.Entries) || didSnap || (didRemoval && time.Since(lastSnapTime) > 2*time.Second) { // Since we received one make sure we have our own since we do not store @@ -941,6 +1001,15 @@ type writeableStreamAssignment struct { Consumers []*consumerAssignment } +func (js *jetStream) clusterStreamConfig(accName, streamName string) (StreamConfig, bool) { + js.mu.RLock() + defer js.mu.RUnlock() + if sa, ok := js.cluster.streams[accName][streamName]; ok { + return *sa.Config, true + } + return StreamConfig{}, false +} + func (js *jetStream) metaSnapshot() []byte { var streams []writeableStreamAssignment @@ -989,6 +1058,7 @@ func (js *jetStream) applyMetaSnapshot(buf []byte, isRecovering bool) error { // Build our new version here outside of js. streams := make(map[string]map[string]*streamAssignment) for _, wsa := range wsas { + fixCfgMirrorWithDedupWindow(wsa.Config) as := streams[wsa.Client.serviceAccount()] if as == nil { as = make(map[string]*streamAssignment) @@ -1219,6 +1289,56 @@ func (js *jetStream) processRemovePeer(peer string) { } } +// Remove old peers after the new peers are caught up. +// We are the old stream leader here. +func (js *jetStream) removeOldPeers(mset *stream, newPreferred string) { + // Make sure still valid. + mset.mu.Lock() + isValid := mset.qch != nil + mset.mu.Unlock() + + if !isValid { + return + } + + sa := mset.streamAssignment() + ci := js.clusterInfo(mset.raftGroup()) + + js.mu.Lock() + defer js.mu.Unlock() + + // Make sure still valid. + if js.srv == nil || !js.srv.isRunning() { + return + } + + cc, csa := js.cluster, sa.copyGroup() + numExpandedPeers := len(csa.Group.Peers) + csa.Group.Peers = csa.Group.Peers[:0] + + for _, r := range ci.Replicas { + if r.cluster != ci.Name { + csa.Group.Peers = append(csa.Group.Peers, r.peer) + } + } + + // Now do consumers actually first here, followed by the owning stream. + for _, ca := range csa.consumers { + cca := ca.copyGroup() + numPeers := len(cca.Group.Peers) + if numPeers == numExpandedPeers { + cca.Group.Peers = csa.Group.Peers + cca.Group.Preferred = _EMPTY_ + } else { + cca.Group.Peers = cca.Group.Peers[len(cca.Group.Peers)-1:] + } + cc.meta.ForwardProposal(encodeAddConsumerAssignment(cca)) + } + + csa.Group.Preferred = newPreferred + cc.meta.ForwardProposal(encodeUpdateStreamAssignment(csa)) +} + // Assumes all checks have already been done. func (js *jetStream) removePeerFromStream(sa *streamAssignment, peer string) bool { js.mu.Lock() @@ -1265,7 +1385,7 @@ func (js *jetStream) hasPeerEntries(entries []*Entry) bool { return false } -func (js *jetStream) applyMetaEntries(entries []*Entry, isRecovering bool) (bool, bool, error) { +func (js *jetStream) applyMetaEntries(entries []*Entry, isRecovering bool, rm *recoveryRemovals) (bool, bool, error) { var didSnap, didRemove bool for _, e := range entries { if e.Type == EntrySnapshot { @@ -1290,6 +1410,8 @@ func (js *jetStream) applyMetaEntries(entries []*Entry, isRecovering bool) (bool } if isRecovering { js.setStreamAssignmentRecovering(sa) + key := sa.Client.Account + ":" + sa.Config.Name + delete(rm.streams, key) } didRemove = js.processStreamAssignment(sa) case removeStreamOp: @@ -1300,9 +1422,12 @@ func (js *jetStream) applyMetaEntries(entries []*Entry, isRecovering bool) (bool } if isRecovering { js.setStreamAssignmentRecovering(sa) + key := sa.Client.Account + ":" + sa.Config.Name + rm.streams[key] = sa + } else { + js.processStreamRemoval(sa) + didRemove = true } - js.processStreamRemoval(sa) - didRemove = true case assignConsumerOp: ca, err := decodeConsumerAssignment(buf[1:]) if err != nil { @@ -1311,6 +1436,8 @@ func (js *jetStream) applyMetaEntries(entries []*Entry, isRecovering bool) (bool } if isRecovering { js.setConsumerAssignmentRecovering(ca) + key := ca.Client.Account + ":" + ca.Name + delete(rm.consumers, key) } js.processConsumerAssignment(ca) case assignCompressedConsumerOp: @@ -1321,6 +1448,8 @@ func (js *jetStream) applyMetaEntries(entries []*Entry, isRecovering bool) (bool } if isRecovering { js.setConsumerAssignmentRecovering(ca) + key := ca.Client.Account + ":" + ca.Name + delete(rm.consumers, key) } js.processConsumerAssignment(ca) case removeConsumerOp: @@ -1331,9 +1460,12 @@ func (js *jetStream) applyMetaEntries(entries []*Entry, isRecovering bool) (bool } if isRecovering { js.setConsumerAssignmentRecovering(ca) + key := ca.Client.Account + ":" + ca.Name + rm.consumers[key] = ca + } else { + js.processConsumerRemoval(ca) + didRemove = true } - js.processConsumerRemoval(ca) - didRemove = true case updateStreamOp: sa, err := decodeStreamAssignment(buf[1:]) if err != nil { @@ -1378,7 +1510,7 @@ func (rg *raftGroup) setPreferred() { } // createRaftGroup is called to spin up this raft group if needed. -func (js *jetStream) createRaftGroup(rg *raftGroup, storage StorageType) error { +func (js *jetStream) createRaftGroup(accName string, rg *raftGroup, storage StorageType) error { js.mu.Lock() defer js.mu.Unlock() s, cc := js.srv, js.cluster @@ -1411,7 +1543,7 @@ func (js *jetStream) createRaftGroup(rg *raftGroup, storage StorageType) error { var store StreamStore if storage == FileStorage { fs, err := newFileStore( - FileStoreConfig{StoreDir: storeDir, BlockSize: 4_000_000, AsyncFlush: false, SyncInterval: 5 * time.Minute}, + FileStoreConfig{StoreDir: storeDir, BlockSize: defaultMediumBlockSize, AsyncFlush: false, SyncInterval: 5 * time.Minute}, StreamConfig{Name: rg.Name, Storage: FileStorage}, ) if err != nil { @@ -1434,7 +1566,7 @@ func (js *jetStream) createRaftGroup(rg *raftGroup, storage StorageType) error { s.bootstrapRaftNode(cfg, rg.Peers, true) } - n, err := s.startRaftNode(cfg) + n, err := s.startRaftNode(accName, cfg) if err != nil || n == nil { s.Debugf("Error creating raft group: %v", err) return err @@ -1442,10 +1574,9 @@ func (js *jetStream) createRaftGroup(rg *raftGroup, storage StorageType) error { rg.node = n // See if we are preferred and should start campaign immediately. - if n.ID() == rg.Preferred { + if n.ID() == rg.Preferred && n.Term() == 0 { n.Campaign() } - return nil } @@ -1480,7 +1611,7 @@ func (mset *stream) removeNode() { } // Monitor our stream node for this stream. -func (js *jetStream) monitorStream(mset *stream, sa *streamAssignment) { +func (js *jetStream) monitorStream(mset *stream, sa *streamAssignment, sendSnapshot bool) { s, cc, n := js.server(), js.cluster, sa.Group.node defer s.grWG.Done() @@ -1489,7 +1620,7 @@ func (js *jetStream) monitorStream(mset *stream, sa *streamAssignment) { return } - qch, lch, aq := n.QuitC(), n.LeadChangeC(), n.ApplyQ() + qch, lch, aq, uch := n.QuitC(), n.LeadChangeC(), n.ApplyQ(), mset.updateC() s.Debugf("Starting stream monitor for '%s > %s' [%s]", sa.Client.serviceAccount(), sa.Config.Name, n.Group()) defer s.Debugf("Exiting stream monitor for '%s > %s' [%s]", sa.Client.serviceAccount(), sa.Config.Name, n.Group()) @@ -1508,11 +1639,13 @@ func (js *jetStream) monitorStream(mset *stream, sa *streamAssignment) { const ( compactInterval = 2 * time.Minute - compactSizeMin = 32 * 1024 * 1024 - compactNumMin = 8192 + compactSizeMin = 8 * 1024 * 1024 + compactNumMin = 65536 ) - t := time.NewTicker(compactInterval) + // Spread these out for large numbers on server restart. + rci := time.Duration(rand.Int63n(int64(time.Minute))) + t := time.NewTicker(compactInterval + rci) defer t.Stop() js.mu.RLock() @@ -1545,6 +1678,35 @@ func (js *jetStream) monitorStream(mset *stream, sa *streamAssignment) { restoreDoneCh := make(<-chan error) isRecovering := true + // For migration tracking. + var migrating bool + var peerGroup peerMigrateType + var mmt *time.Ticker + var mmtc <-chan time.Time + + startMigrationMonitoring := func() { + if mmt == nil { + mmt = time.NewTicker(1 * time.Second) + mmtc = mmt.C + } + } + + stopMigrationMonitoring := func() { + if mmt != nil { + mmt.Stop() + mmtc = nil + } + } + defer stopMigrationMonitoring() + + // This is triggered during a scale up from 1 to clustered mode. We need the new followers to catchup, + // similar to how we trigger the catchup mechanism post a backup/restore. It's ok to do here and preferred + // over waiting to be elected, this just queues it up for the new members to see first and trigger the above + // RAFT layer catchup mechanism. + if sendSnapshot && mset != nil && n != nil { + n.SendSnapshot(mset.stateSnapshot()) + } + for { select { case <-s.quitCh: @@ -1602,9 +1764,65 @@ func (js *jetStream) monitorStream(mset *stream, sa *streamAssignment) { } else if n.GroupLeader() != noLeader { js.setStreamAssignmentRecovering(sa) } + + // Process our leader change. js.processStreamLeaderChange(mset, isLeader) + + // We may receive a leader change after the stream assignment which would cancel us + // monitoring for this closely. So re-assess our state here as well. + migrating, peerGroup = mset.isMigrating() + + // Check for migrations here. We set the state on the stream assignment update below. + if isLeader && migrating { + if peerGroup == oldPeerGroup { + startMigrationMonitoring() + } else { + stopMigrationMonitoring() + } + } case <-t.C: doSnapshot() + case <-uch: + // We get this when we have a new stream assignment caused by an update. We want + // to know if we are migrating. + migrating, peerGroup = mset.isMigrating() + // If we are migrating and in the old peer group and we are leader, monitor for the + // new peers to be caught up. We could not be leader yet, so we will do same check below + // on leadership change. + if isLeader && migrating && peerGroup == oldPeerGroup { + doSnapshot() + startMigrationMonitoring() + } + case <-mmtc: + if !isLeader { + // We are no longer leader, so not our job. + stopMigrationMonitoring() + continue + } + // Check to see that we have someone caught up. + // TODO(dlc) - For now start checking after a second in order to give proper time to kick in any catchup logic needed. + // What we really need to do longer term is know if we need catchup and make sure that process has kicked off and/or completed. + ci := js.clusterInfo(mset.raftGroup()) + // The polling interval of one second allows this to be kicked in if needed. + if mset.hasCatchupPeers() { + mset.checkClusterInfo(ci) + } + // Track the new peers and check the ones that are current. + var newPeers []*PeerInfo + quorum := mset.cfg.Replicas/2 + 1 + for _, r := range ci.Replicas { + if r.cluster != ci.Name { + if r.Current { + newPeers = append(newPeers, r) + } + } + } + // If all are current we are good, or if we have some offline and we have a quorum. + if lnp := len(newPeers); lnp >= quorum { + stopMigrationMonitoring() + // Remove the old peers and transfer leadership. + time.AfterFunc(2*time.Second, func() { js.removeOldPeers(mset, newPeers[0].peer) }) + } case err := <-restoreDoneCh: // We have completed a restore from snapshot on this server. The stream assignment has // already been assigned but the replicas will need to catch up out of band. Consumers @@ -1617,9 +1835,10 @@ func (js *jetStream) monitorStream(mset *stream, sa *streamAssignment) { sa.Restore = nil // If we were successful lookup up our stream now. if err == nil { - mset, err = acc.lookupStream(sa.Config.Name) - if mset != nil { + if mset, err = acc.lookupStream(sa.Config.Name); mset != nil { mset.setStreamAssignment(sa) + // Make sure to update our updateC which would have been nil. + uch = mset.updateC() } } if err != nil { @@ -1647,7 +1866,7 @@ func (js *jetStream) monitorStream(mset *stream, sa *streamAssignment) { panic("Finished restore but not leader") } // Trigger the stream followers to catchup. - if n := mset.raftNode(); n != nil { + if n = mset.raftNode(); n != nil { n.SendSnapshot(mset.stateSnapshot()) } js.processStreamLeaderChange(mset, isLeader) @@ -1656,11 +1875,13 @@ func (js *jetStream) monitorStream(mset *stream, sa *streamAssignment) { // These are not currently assigned so we will need to do so here. if consumers := mset.getPublicConsumers(); len(consumers) > 0 { for _, o := range consumers { - rg := cc.createGroupForConsumer(sa) + name, cfg := o.String(), o.config() + rg := cc.createGroupForConsumer(&cfg, sa) // Pick a preferred leader. rg.setPreferred() - name, cfg := o.String(), o.config() + // Place our initial state here as well for assignment distribution. + state, _ := o.store.State() ca := &consumerAssignment{ Group: rg, Stream: sa.Config.Name, @@ -1668,7 +1889,7 @@ func (js *jetStream) monitorStream(mset *stream, sa *streamAssignment) { Config: &cfg, Client: sa.Client, Created: o.createdTime(), - State: o.readStoreState(), + State: state, } // We make these compressed in case state is complex. @@ -1701,11 +1922,47 @@ func (js *jetStream) monitorStream(mset *stream, sa *streamAssignment) { } } +// When we are migration denotes if we ourselves are part of the old peer set or the new one. +// Both types will be running at the same time as we scale up to extend into the new cluster. +// Once detected we will us our type to dictate our behavior. +type peerMigrateType int8 + +const ( + oldPeerGroup = peerMigrateType(iota) + newPeerGroup +) + +// Determine if we are migrating and if so if we are part of the old or new set. +func (mset *stream) isMigrating() (bool, peerMigrateType) { + mset.mu.RLock() + s, js, sa := mset.srv, mset.js, mset.sa + mset.mu.RUnlock() + + js.mu.RLock() + defer js.mu.RUnlock() + + // During migration we will always be R>1, even when we start R1. + // So if we do not have a group or node we no we are not migrating. + if sa == nil || sa.Group == nil || sa.Group.node == nil { + return false, oldPeerGroup + } + // The sign of migration is if our group peer count != configured replica count. + if sa.Config.Replicas == len(sa.Group.Peers) { + return false, oldPeerGroup + } + // So we believe we are migrating here, need to determine if we are the old set or new set. + // We can shor circuit this based on our group assigned cluster vs our own. + if sa.Group.Cluster == s.cachedClusterName() { + return true, newPeerGroup + } + return true, oldPeerGroup +} + // resetClusteredState is called when a clustered stream had a sequence mismatch and needs to be reset. func (mset *stream) resetClusteredState(err error) bool { mset.mu.RLock() s, js, jsa, sa, acc, node := mset.srv, mset.js, mset.jsa, mset.sa, mset.acc, mset.node - stype, isLeader := mset.cfg.Storage, mset.isLeader() + stype, isLeader, tierName := mset.cfg.Storage, mset.isLeader(), mset.tier mset.mu.RUnlock() // Stepdown regardless if we are the leader here. @@ -1720,7 +1977,7 @@ func (mset *stream) resetClusteredState(err error) bool { } // Account - if jsa.limitsExceeded(stype) { + if exceeded, _ := jsa.limitsExceeded(stype, tierName); exceeded { s.Warnf("stream '%s > %s' errored, account resources exceeded", acc, mset.name()) return false } @@ -1807,7 +2064,8 @@ func (js *jetStream) applyStreamEntries(mset *stream, ce *CommittedEntry, isReco // We can skip if we know this is less than what we already have. if lseq < last { - s.Debugf("Apply stream entries skipping message with sequence %d with last of %d", lseq, last) + s.Debugf("Apply stream entries for '%s > %s' skipping message with sequence %d with last of %d", + mset.account(), mset.name(), lseq, last) continue } @@ -1831,7 +2089,8 @@ func (js *jetStream) applyStreamEntries(mset *stream, ce *CommittedEntry, isReco if isClusterResetErr(err) || isOutOfSpaceErr(err) { return err } - s.Debugf("Apply stream entries error processing message: %v", err) + s.Debugf("Apply stream entries for '%s > %s' got error processing message: %v", + mset.account(), mset.name(), err) } case deleteMsgOp: md, err := decodeMsgDelete(buf[1:]) @@ -1858,8 +2117,8 @@ func (js *jetStream) applyStreamEntries(mset *stream, ce *CommittedEntry, isReco } if err != nil && !isRecovering { - s.Debugf("JetStream cluster failed to delete msg %d from stream %q for account %q: %v", - md.Seq, md.Stream, md.Client.serviceAccount(), err) + s.Debugf("JetStream cluster failed to delete stream msg %d from '%s > %s': %v", + md.Seq, md.Client.serviceAccount(), md.Stream, err) } js.mu.RLock() @@ -1928,8 +2187,10 @@ func (js *jetStream) applyStreamEntries(mset *stream, ce *CommittedEntry, isReco if err := json.Unmarshal(e.Data, &snap); err != nil { return err } - if err := mset.processSnapshot(&snap); err != nil { - return err + if !mset.IsLeader() { + if err := mset.processSnapshot(&snap); err != nil { + return err + } } } } else if e.Type == EntryRemovePeer { @@ -2204,6 +2465,7 @@ func (js *jetStream) processStreamAssignment(sa *streamAssignment) bool { js.mu.Lock() if node := sa.Group.node; node != nil { if node.Leader() { + node.UpdateKnownPeers(sa.Group.Peers) node.StepDown() } node.ProposeRemovePeer(ourID) @@ -2270,15 +2532,22 @@ func (js *jetStream) processUpdateStreamAssignment(sa *streamAssignment) { sa.consumers = osa.consumers sa.err = osa.err + // If we detect we are scaling down to 1, non-clustered, and we had a previous node, clear it here. + if sa.Config.Replicas == 1 && sa.Group.node != nil { + sa.Group.node = nil + } + // Update our state. accStreams[stream] = sa cc.streams[acc.Name] = accStreams - // Make sure we respond. + // Make sure we respond if we are a member. if isMember { sa.responded = false + } else { + // Make sure to clean up any old node in case this stream moves back here. + sa.Group.node = nil } - js.mu.Unlock() // Check if this is for us.. @@ -2286,8 +2555,11 @@ func (js *jetStream) processUpdateStreamAssignment(sa *streamAssignment) { js.processClusterUpdateStream(acc, osa, sa) } else if mset, _ := acc.lookupStream(sa.Config.Name); mset != nil { // We have one here even though we are not a member. This can happen on re-assignment. - s.Debugf("JetStream removing stream '%s > %s' from this server, re-assigned", sa.Client.serviceAccount(), sa.Config.Name) + s.Debugf("JetStream removing stream '%s > %s' from this server", sa.Client.serviceAccount(), sa.Config.Name) if node := mset.raftNode(); node != nil { + if node.Leader() { + node.StepDown(sa.Group.Preferred) + } node.ProposeRemovePeer(ourID) } mset.stop(true, false) @@ -2304,37 +2576,39 @@ func (js *jetStream) processClusterUpdateStream(acc *Account, osa, sa *streamAss js.mu.Lock() s, rg := js.srv, sa.Group client, subject, reply := sa.Client, sa.Subject, sa.Reply - alreadyRunning, numReplicas := osa.Group.node != nil, sa.Config.Replicas + alreadyRunning, numReplicas := osa.Group.node != nil, len(rg.Peers) needsNode := rg.node == nil - storage := sa.Config.Storage + storage, cfg := sa.Config.Storage, sa.Config hasResponded := sa.responded sa.responded = true js.mu.Unlock() - mset, err := acc.lookupStream(sa.Config.Name) + mset, err := acc.lookupStream(cfg.Name) if err == nil && mset != nil { var needsSetLeader bool if !alreadyRunning && numReplicas > 1 { if needsNode { - js.createRaftGroup(rg, storage) + js.createRaftGroup(acc.GetName(), rg, storage) } - s.startGoRoutine(func() { js.monitorStream(mset, sa) }) + s.startGoRoutine(func() { js.monitorStream(mset, sa, needsNode) }) } else if numReplicas == 1 && alreadyRunning { // We downgraded to R1. Make sure we cleanup the raft node and the stream monitor. mset.removeNode() // Make sure we are leader now that we are R1. needsSetLeader = true - // In case we nned to shutdown the cluster specific subs, etc. + // In case we need to shutdown the cluster specific subs, etc. mset.setLeader(false) js.mu.Lock() - sa.Group.node = nil + rg.node = nil js.mu.Unlock() } - mset.setStreamAssignment(sa) - if err = mset.update(sa.Config); err != nil { - s.Warnf("JetStream cluster error updating stream %q for account %q: %v", sa.Config.Name, acc.Name, err) - mset.setStreamAssignment(osa) + + if err = mset.update(cfg); err != nil { + s.Warnf("JetStream cluster error updating stream %q for account %q: %v", cfg.Name, acc.Name, err) } + // Set the new stream assignment. + mset.setStreamAssignment(sa) + // Make sure we are the leader now that we are R1. if needsSetLeader { mset.setLeader(true) @@ -2396,6 +2670,7 @@ func (js *jetStream) processClusterUpdateStream(acc *Account, osa, sa *streamAss Mirror: mset.mirrorInfo(), Sources: mset.sourcesInfo(), } + s.sendAPIResponse(client, acc, subject, reply, _EMPTY_, s.jsonResponse(&resp)) } @@ -2413,7 +2688,7 @@ func (js *jetStream) processClusterCreateStream(acc *Account, sa *streamAssignme js.mu.RUnlock() // Process the raft group and make sure it's running if needed. - err := js.createRaftGroup(rg, storage) + err := js.createRaftGroup(acc.GetName(), rg, storage) // If we are restoring, create the stream if we are R>1 and not the preferred who handles the // receipt of the snapshot itself. @@ -2487,7 +2762,7 @@ func (js *jetStream) processClusterCreateStream(acc *Account, sa *streamAssignme // Start our monitoring routine. if rg.node != nil { if !alreadyRunning { - s.startGoRoutine(func() { js.monitorStream(mset, sa) }) + s.startGoRoutine(func() { js.monitorStream(mset, sa, false) }) } } else { // Single replica stream, process manually here. @@ -2534,8 +2809,9 @@ func (js *jetStream) processClusterCreateStream(acc *Account, sa *streamAssignme js.mu.RUnlock() for _, o := range consumers { - rg := cc.createGroupForConsumer(sa) name, cfg := o.String(), o.config() + rg := cc.createGroupForConsumer(&cfg, sa) + // Place our initial state here as well for assignment distribution. ca := &consumerAssignment{ Group: rg, @@ -2617,36 +2893,37 @@ func (js *jetStream) processClusterDeleteStream(sa *streamAssignment, isMember, js.mu.RLock() s := js.srv hadLeader := sa.Group.node == nil || sa.Group.node.GroupLeader() != noLeader - js.mu.RUnlock() - - acc, err := s.LookupAccount(sa.Client.serviceAccount()) - if err != nil { - s.Debugf("JetStream cluster failed to lookup account %q: %v", sa.Client.serviceAccount(), err) - return + offline := s.allPeersOffline(sa.Group) + var isMetaLeader bool + if cc := js.cluster; cc != nil { + isMetaLeader = cc.isLeader() } + js.mu.RUnlock() var resp = JSApiStreamDeleteResponse{ApiResponse: ApiResponse{Type: JSApiStreamDeleteResponseType}} + var err error + var acc *Account - // Go ahead and delete the stream. - mset, err := acc.lookupStream(sa.Config.Name) - if err != nil { - resp.Error = NewJSStreamNotFoundError(Unless(err)) - } else if mset != nil { - err = mset.stop(true, wasLeader) + // Go ahead and delete the stream if we have it and the account here. + if acc, _ = s.LookupAccount(sa.Client.serviceAccount()); acc != nil { + if mset, _ := acc.lookupStream(sa.Config.Name); mset != nil { + err = mset.stop(true, wasLeader) + } } + // Always delete the node if present. if sa.Group.node != nil { sa.Group.node.Delete() } if !isMember || !wasLeader && hadLeader { - return + if !(offline && isMetaLeader) { + return + } } if err != nil { - if resp.Error == nil { - resp.Error = NewJSStreamGeneralError(err, Unless(err)) - } + resp.Error = NewJSStreamGeneralError(err, Unless(err)) s.sendAPIErrResponse(sa.Client, acc, sa.Subject, sa.Reply, _EMPTY_, s.jsonResponse(resp)) } else { resp.Success = true @@ -2658,7 +2935,7 @@ func (js *jetStream) processClusterDeleteStream(sa *streamAssignment, isMember, func (js *jetStream) processConsumerAssignment(ca *consumerAssignment) { js.mu.RLock() s, cc := js.srv, js.cluster - accName, stream, consumer := ca.Client.serviceAccount(), ca.Stream, ca.Name + accName, stream, consumerName := ca.Client.serviceAccount(), ca.Stream, ca.Name noMeta := cc == nil || cc.meta == nil var ourID string if !noMeta { @@ -2674,14 +2951,15 @@ func (js *jetStream) processConsumerAssignment(ca *consumerAssignment) { return } - if _, err := s.LookupAccount(accName); err != nil { + acc, err := s.LookupAccount(accName) + if err != nil { ll := fmt.Sprintf("Account [%s] lookup for consumer create failed: %v", accName, err) if isMember { // If we can not lookup the account and we are a member, send this result back to the metacontroller leader. result := &consumerAssignmentResult{ Account: accName, Stream: stream, - Consumer: consumer, + Consumer: consumerName, Response: &JSApiConsumerCreateResponse{ApiResponse: ApiResponse{Type: JSApiConsumerCreateResponseType}}, } result.Response.Error = NewJSNoAccountError() @@ -2699,6 +2977,9 @@ func (js *jetStream) processConsumerAssignment(ca *consumerAssignment) { return } + // Might need this below. + numReplicas := sa.Config.Replicas + // Track if this existed already. var wasExisting bool @@ -2728,17 +3009,62 @@ func (js *jetStream) processConsumerAssignment(ca *consumerAssignment) { if isMember { js.processClusterCreateConsumer(ca, state, wasExisting) } else { + // We need to be removed here, we are no longer assigned. + // Grab consumer if we have it. + var o *consumer + if mset, _ := acc.lookupStream(sa.Config.Name); mset != nil { + o = mset.lookupConsumer(ca.Name) + } + // Check if we have a raft node running, meaning we are no longer part of the group but were. js.mu.Lock() if node := ca.Group.node; node != nil { + // We have one here even though we are not a member. This can happen on re-assignment. + s.Debugf("JetStream removing consumer '%s > %s > %s' from this server", sa.Client.serviceAccount(), sa.Config.Name, ca.Name) if node.Leader() { - node.StepDown() + s.Debugf("JetStream consumer '%s > %s > %s' is being removed and was the leader, will perform stepdown", + sa.Client.serviceAccount(), sa.Config.Name, ca.Name) + + peers, cn := node.Peers(), s.cachedClusterName() + migrating := numReplicas != len(peers) + + // Select a new peer to transfer to. If we are a migrating make sure its from the new cluster. + var npeer string + for _, r := range peers { + if !r.Current { + continue + } + if !migrating { + npeer = r.ID + break + } else if sir, ok := s.nodeToInfo.Load(r.ID); ok && sir != nil { + si := sir.(nodeInfo) + if si.cluster != cn { + npeer = r.ID + break + } + } + } + // Clear the raftnode from our consumer so that a subsequent o.delete will not also issue a stepdown. + if o != nil { + o.clearRaftNode() + } + // Manually handle the stepdown and deletion of the node. + node.UpdateKnownPeers(ca.Group.Peers) + node.StepDown(npeer) + node.Delete() + } else { + node.UpdateKnownPeers(ca.Group.Peers) } - node.ProposeRemovePeer(ourID) } + // Always clear the old node. ca.Group.node = nil ca.err = nil js.mu.Unlock() + + if o != nil { + o.deleteWithoutAdvisory() + } } } @@ -2810,17 +3136,30 @@ func (js *jetStream) processClusterCreateConsumer(ca *consumerAssignment, state return } + // Check if we already have this consumer running. + o := mset.lookupConsumer(ca.Name) + if !alreadyRunning { // Process the raft group and make sure its running if needed. - js.createRaftGroup(rg, mset.config().Storage) + storage := mset.config().Storage + if ca.Config.MemoryStorage { + storage = MemoryStorage + } + js.createRaftGroup(acc.GetName(), rg, storage) + } else { + // If we are clustered update the known peers. + js.mu.RLock() + if node := rg.node; node != nil { + node.UpdateKnownPeers(ca.Group.Peers) + } + js.mu.RUnlock() } // Check if we already have this consumer running. var didCreate bool - o := mset.lookupConsumer(ca.Name) if o == nil { // Add in the consumer if needed. - o, err = mset.addConsumerWithAssignment(ca.Config, ca.Name, ca) + o, err = mset.addConsumerWithAssignment(ca.Config, ca.Name, ca, false) didCreate = true } else { if err := o.updateConfig(ca.Config); err != nil { @@ -2839,15 +3178,32 @@ func (js *jetStream) processClusterCreateConsumer(ca *consumerAssignment, state } // Check if we already had a consumer assignment and its still pending. cca, oca := ca, o.consumerAssignment() + o.mu.Lock() + leader := o.isLeader() + o.mu.Unlock() + + var sendState bool js.mu.Lock() - if oca != nil && !oca.responded { - // We can't over ride info for replying here otherwise leader once elected can not respond. - // So just update Config, leave off client and reply to the originals. - cac := *oca - cac.Config = ca.Config - cca = &cac + if oca != nil { + if !oca.responded { + // We can't override info for replying here otherwise leader once elected can not respond. + // So just update Config, leave off client and reply to the originals. + cac := *oca + cac.Config = ca.Config + cca = &cac + } + // If we look like we are scaling up, let's send our current state to the group. + sendState = len(ca.Group.Peers) > len(oca.Group.Peers) && leader } + n := rg.node js.mu.Unlock() + + if sendState && n != nil { + if snap, err := o.store.EncodedState(); err == nil { + n.SendSnapshot(snap) + } + } + // Set CA for our consumer. o.setConsumerAssignment(cca) s.Debugf("JetStream cluster, consumer was already running") @@ -2857,7 +3213,6 @@ func (js *jetStream) processClusterCreateConsumer(ca *consumerAssignment, state if state != nil && o != nil { err = o.setStoreState(state) } - if err != nil { if IsNatsErr(err, JSConsumerStoreFailedErrF) { s.Warnf("Consumer create failed for '%s > %s > %s': %v", ca.Client.serviceAccount(), ca.Stream, ca.Name, err) @@ -2917,6 +3272,10 @@ func (js *jetStream) processClusterCreateConsumer(ca *consumerAssignment, state // Start our monitoring routine. if rg.node == nil { // Single replica consumer, process manually here. + js.mu.Lock() + // to force response in case we think we have responded before. + ca.responded = false + js.mu.Unlock() js.processConsumerLeaderChange(o, true) } else { if !alreadyRunning { @@ -2942,40 +3301,39 @@ func (js *jetStream) processClusterDeleteConsumer(ca *consumerAssignment, isMemb } js.mu.RLock() s := js.srv - js.mu.RUnlock() - - acc, err := s.LookupAccount(ca.Client.serviceAccount()) - if err != nil { - s.Warnf("JetStream cluster failed to lookup account %q: %v", ca.Client.serviceAccount(), err) - return + offline := s.allPeersOffline(ca.Group) + var isMetaLeader bool + if cc := js.cluster; cc != nil { + isMetaLeader = cc.isLeader() } + js.mu.RUnlock() var resp = JSApiConsumerDeleteResponse{ApiResponse: ApiResponse{Type: JSApiConsumerDeleteResponseType}} + var err error + var acc *Account - // Go ahead and delete the consumer. - mset, err := acc.lookupStream(ca.Stream) - if err != nil { - resp.Error = NewJSStreamNotFoundError(Unless(err)) - } else if mset != nil { - if o := mset.lookupConsumer(ca.Name); o != nil { - err = o.stopWithFlags(true, false, true, wasLeader) - } else { - resp.Error = NewJSConsumerNotFoundError() + // Go ahead and delete the consumer if we have it and the account. + if acc, _ = s.LookupAccount(ca.Client.serviceAccount()); acc != nil { + if mset, _ := acc.lookupStream(ca.Stream); mset != nil { + if o := mset.lookupConsumer(ca.Name); o != nil { + err = o.stopWithFlags(true, false, true, wasLeader) + } } } + // Always delete the node if present. if ca.Group.node != nil { ca.Group.node.Delete() } if !wasLeader || ca.Reply == _EMPTY_ { - return + if !(offline && isMetaLeader) { + return + } } if err != nil { - if resp.Error == nil { - resp.Error = NewJSStreamNotFoundError(Unless(err)) - } + resp.Error = NewJSStreamNotFoundError(Unless(err)) s.sendAPIErrResponse(ca.Client, acc, ca.Subject, ca.Reply, _EMPTY_, s.jsonResponse(resp)) } else { resp.Success = true @@ -3058,6 +3416,15 @@ func (o *consumer) raftGroup() *raftGroup { return o.ca.Group } +func (o *consumer) clearRaftNode() { + if o == nil { + return + } + o.mu.Lock() + defer o.mu.Unlock() + o.node = nil +} + func (o *consumer) raftNode() RaftNode { if o == nil { return nil @@ -3076,35 +3443,45 @@ func (js *jetStream) monitorConsumer(o *consumer, ca *consumerAssignment) { return } - qch, lch, aq := n.QuitC(), n.LeadChangeC(), n.ApplyQ() + qch, lch, aq, uch := n.QuitC(), n.LeadChangeC(), n.ApplyQ(), o.updateC() s.Debugf("Starting consumer monitor for '%s > %s > %s' [%s]", o.acc.Name, ca.Stream, ca.Name, n.Group()) defer s.Debugf("Exiting consumer monitor for '%s > %s > %s' [%s]", o.acc.Name, ca.Stream, ca.Name, n.Group()) const ( compactInterval = 2 * time.Minute - compactSizeMin = 8 * 1024 * 1024 - compactNumMin = 8192 + compactSizeMin = 64 * 1024 // What is stored here is always small for consumers. + compactNumMin = 1024 + minSnapDelta = 2 * time.Second ) - t := time.NewTicker(compactInterval) + // Spread these out for large numbers on server restart. + rci := time.Duration(rand.Int63n(int64(time.Minute))) + t := time.NewTicker(compactInterval + rci) defer t.Stop() - st := o.store.Type() var lastSnap []byte + var lastSnapTime time.Time - doSnapshot := func() { - // Memory store consumers do not keep state in the store itself. - // Just compact to our applied index. - if st == MemoryStorage { - _, _, applied := n.Progress() - n.Compact(applied) - } else if state, err := o.store.State(); err == nil && state != nil { - // FileStore version. - if snap := encodeConsumerState(state); !bytes.Equal(lastSnap, snap) { - if err := n.InstallSnapshot(snap); err == nil { - lastSnap = snap - } + doSnapshot := func(force bool) { + // Bail if trying too fast and not in a forced situation. + if !force && time.Since(lastSnapTime) < minSnapDelta { + return + } + + // Check several things to see if we need a snapshot. + needSnap := force || n.NeedSnapshot() + if !needSnap { + // Check if we should compact etc. based on size of log. + ne, nb := n.Size() + needSnap = nb > 0 && ne >= compactNumMin || nb > compactSizeMin + } + + if snap, err := o.store.EncodedState(); err == nil && (!bytes.Equal(lastSnap, snap) || needSnap) { + if err := n.InstallSnapshot(snap); err == nil { + lastSnap, lastSnapTime = snap, time.Now() + } else { + s.Warnf("Failed to install snapshot for '%s > %s > %s' [%s]: %v", o.acc.Name, ca.Stream, ca.Name, n.Group(), err) } } } @@ -3126,7 +3503,7 @@ func (js *jetStream) monitorConsumer(o *consumer, ca *consumerAssignment) { if cei == nil { recovering = false if n.NeedSnapshot() { - doSnapshot() + doSnapshot(true) } continue } @@ -3135,7 +3512,7 @@ func (js *jetStream) monitorConsumer(o *consumer, ca *consumerAssignment) { ne, nb := n.Applied(ce.Index) // If we have at least min entries to compact, go ahead and snapshot/compact. if nb > 0 && ne >= compactNumMin || nb > compactSizeMin { - doSnapshot() + doSnapshot(false) } } else { s.Warnf("Error applying consumer entries to '%s > %s'", ca.Client.serviceAccount(), ca.Name) @@ -3146,9 +3523,12 @@ func (js *jetStream) monitorConsumer(o *consumer, ca *consumerAssignment) { if recovering && !isLeader { js.setConsumerAssignmentRecovering(ca) } - js.processConsumerLeaderChange(o, isLeader) + if err := js.processConsumerLeaderChange(o, isLeader); err == nil && isLeader { + doSnapshot(true) + } + case <-uch: case <-t.C: - doSnapshot() + doSnapshot(false) } } } @@ -3218,7 +3598,9 @@ func (js *jetStream) applyConsumerEntries(o *consumer, ce *CommittedEntry, isLea o.mu.Lock() if !o.isLeader() { var le = binary.LittleEndian - o.sseq = le.Uint64(buf[1:]) + if sseq := le.Uint64(buf[1:]); sseq > o.sseq { + o.sseq = sseq + } } o.mu.Unlock() case addPendingRequest: @@ -3254,7 +3636,7 @@ func (o *consumer) processReplicatedAck(dseq, sseq uint64) { o.store.UpdateAcks(dseq, sseq) mset := o.mset - if mset == nil || mset.cfg.Retention == LimitsPolicy { + if mset == nil || o.retention == LimitsPolicy { o.mu.Unlock() return } @@ -3320,10 +3702,17 @@ func decodeDeliveredUpdate(buf []byte) (dseq, sseq, dc uint64, ts int64, err err return dseq, sseq, dc, ts, nil } -func (js *jetStream) processConsumerLeaderChange(o *consumer, isLeader bool) { +func (js *jetStream) processConsumerLeaderChange(o *consumer, isLeader bool) error { + stepDownIfLeader := func() error { + if node := o.raftNode(); node != nil && isLeader { + node.StepDown() + } + return errors.New("failed to update consumer leader status") + } + ca := o.consumerAssignment() if ca == nil { - return + return stepDownIfLeader() } js.mu.Lock() s, account, err := js.srv, ca.Client.serviceAccount(), ca.err @@ -3336,7 +3725,7 @@ func (js *jetStream) processConsumerLeaderChange(o *consumer, isLeader bool) { consumerName := o.String() acc, _ := s.LookupAccount(account) if acc == nil { - return + return stepDownIfLeader() } if isLeader { @@ -3355,22 +3744,11 @@ func (js *jetStream) processConsumerLeaderChange(o *consumer, isLeader bool) { // Tell consumer to switch leader status. o.setLeader(isLeader) - // Synchronize others to our version of state. - if isLeader { - if n := o.raftNode(); n != nil { - if state, err := o.store.State(); err == nil && state != nil { - if snap := encodeConsumerState(state); len(snap) > 0 { - n.SendSnapshot(snap) - } - } - } - } - if !isLeader || hasResponded { if isLeader { o.clearInitialInfo() } - return + return nil } var resp = JSApiConsumerCreateResponse{ApiResponse: ApiResponse{Type: JSApiConsumerCreateResponseType}} @@ -3384,6 +3762,8 @@ func (js *jetStream) processConsumerLeaderChange(o *consumer, isLeader bool) { o.sendCreateAdvisory() } } + + return nil } // Determines if we should send lost quorum advisory. We throttle these after first one. @@ -3624,14 +4004,20 @@ func (js *jetStream) stopUpdatesSub() { func (js *jetStream) processLeaderChange(isLeader bool) { if isLeader { js.srv.Noticef("Self is new JetStream cluster metadata leader") - } else if node := js.getMetaGroup().GroupLeader(); node == _EMPTY_ { - js.srv.Noticef("JetStream cluster no metadata leader") - } else if srv := js.srv.serverNameForNode(node); srv == _EMPTY_ { - js.srv.Noticef("JetStream cluster new remote metadata leader") - } else if clst := js.srv.clusterNameForNode(node); clst == _EMPTY_ { - js.srv.Noticef("JetStream cluster new metadata leader: %s", srv) } else { - js.srv.Noticef("JetStream cluster new metadata leader: %s/%s", srv, clst) + var node string + if meta := js.getMetaGroup(); meta != nil { + node = meta.GroupLeader() + } + if node == _EMPTY_ { + js.srv.Noticef("JetStream cluster no metadata leader") + } else if srv := js.srv.serverNameForNode(node); srv == _EMPTY_ { + js.srv.Noticef("JetStream cluster new remote metadata leader") + } else if clst := js.srv.clusterNameForNode(node); clst == _EMPTY_ { + js.srv.Noticef("JetStream cluster new metadata leader: %s", srv) + } else { + js.srv.Noticef("JetStream cluster new metadata leader: %s/%s", srv, clst) + } } js.mu.Lock() @@ -3735,6 +4121,7 @@ func (cc *jetStreamCluster) selectPeerGroup(r int, cluster string, cfg *StreamCo } var nodes []wn + // peers is a randomized list s, peers := cc.s, cc.meta.Peers() // Map existing. @@ -3749,6 +4136,19 @@ func (cc *jetStreamCluster) selectPeerGroup(r int, cluster string, cfg *StreamCo } } + uniqueTagPrefix := s.getOpts().JetStreamUniqueTag + if uniqueTagPrefix != _EMPTY_ { + for _, tag := range tags { + if strings.HasPrefix(tag, uniqueTagPrefix) { + // disable uniqueness check of explicitly listed in tags + uniqueTagPrefix = _EMPTY_ + break + } + } + } + var uniqueTags = make(map[string]struct{}) + maxHaAssets := s.getOpts().JetStreamLimits.MaxHAAssets + for _, p := range peers { si, ok := s.nodeToInfo.Load(p.ID) if !ok || si == nil { @@ -3803,14 +4203,39 @@ func (cc *jetStreamCluster) selectPeerGroup(r int, cluster string, cfg *StreamCo // Otherwise check if we have enough room if maxBytes set. if maxBytes > 0 && maxBytes > available { + s.Warnf("%s@%s (Max Bytes: %d) exceeds available %s storage of %d bytes", + ni.name, ni.cluster, maxBytes, cfg.Storage.String(), available) + continue + } + // HAAssets contain _meta_ which we want to ignore + if maxHaAssets > 0 && ni.stats != nil && ni.stats.HAAssets > maxHaAssets { + s.Warnf("%s@%s (HA Asset Count: %d) exceeds max ha asset limit of %d for stream placement", + ni.name, ni.cluster, ni.stats.HAAssets, maxHaAssets) continue } - // Add to our list of potential nodes. - nodes = append(nodes, wn{p.ID, available}) - } - // If we could not select enough peers, fail. - if len(nodes) < (r - len(existing)) { + if uniqueTagPrefix != _EMPTY_ { + // default requires the unique prefix to be present + isUnique := false + for _, t := range ni.tags { + if strings.HasPrefix(t, uniqueTagPrefix) { + if _, ok := uniqueTags[t]; !ok { + uniqueTags[t] = struct{}{} + isUnique = true + } + break + } + } + if !isUnique { + continue + } + } + // Add to our list of potential nodes. + nodes = append(nodes, wn{p.ID, available}) + } + + // If we could not select enough peers, fail. + if len(nodes) < (r - len(existing)) { return nil } // Sort based on available from most to least. @@ -3836,13 +4261,37 @@ func groupNameForConsumer(peers []string, storage StorageType) string { } func groupName(prefix string, peers []string, storage StorageType) string { - var gns string - if len(peers) == 1 { - gns = peers[0] + gns := string(getHash(nuid.Next())) + return fmt.Sprintf("%s-R%d%s-%s", prefix, len(peers), storage.String()[:1], gns) +} + +// returns stream count for this tier as well as applicable reservation size (not including reservations for cfg) +// jetStream read lock should be held +func tieredStreamAndReservationCount(asa map[string]*streamAssignment, tier string, cfg *StreamConfig) (int, int64) { + numStreams := len(asa) + reservation := int64(0) + if tier == _EMPTY_ { + for _, sa := range asa { + if sa.Config.MaxBytes > 0 && sa.Config.Name != cfg.Name { + if sa.Config.Storage == cfg.Storage { + reservation += (int64(sa.Config.Replicas) * sa.Config.MaxBytes) + } + } + } } else { - gns = string(getHash(nuid.Next())) + numStreams = 0 + for _, sa := range asa { + if isSameTier(sa.Config, cfg) { + numStreams++ + if sa.Config.MaxBytes > 0 { + if sa.Config.Storage == cfg.Storage && sa.Config.Name != cfg.Name { + reservation += (int64(sa.Config.Replicas) * sa.Config.MaxBytes) + } + } + } + } } - return fmt.Sprintf("%s-R%d%s-%s", prefix, len(peers), storage.String()[:1], gns) + return numStreams, reservation } // createGroupForStream will create a group for assignment for the stream. @@ -3876,52 +4325,69 @@ func (js *jetStream) createGroupForStream(ci *ClientInfo, cfg *StreamConfig) *ra return nil } -func (s *Server) jsClusteredStreamRequest(ci *ClientInfo, acc *Account, subject, reply string, rmsg []byte, config *StreamConfig) { - js, cc := s.getJetStreamCluster() - if js == nil || cc == nil { - return - } - - var resp = JSApiStreamCreateResponse{ApiResponse: ApiResponse{Type: JSApiStreamCreateResponseType}} - +func (acc *Account) selectLimits(cfg *StreamConfig) (*JetStreamAccountLimits, string, *jsAccount, *ApiError) { // Grab our jetstream account info. acc.mu.RLock() jsa := acc.js acc.mu.RUnlock() if jsa == nil { - resp.Error = NewJSNotEnabledForAccountError() - s.sendAPIErrResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(&resp)) + return nil, _EMPTY_, nil, NewJSNotEnabledForAccountError() + } + + jsa.usageMu.RLock() + selectedLimits, tierName, ok := jsa.selectLimits(cfg) + jsa.usageMu.RUnlock() + + if !ok { + return nil, _EMPTY_, nil, NewJSNoLimitsError() + } + return &selectedLimits, tierName, jsa, nil +} + +// Read lock needs to be held +func (js *jetStream) jsClusteredStreamLimitsCheck(acc *Account, cfg *StreamConfig) *ApiError { + selectedLimits, tier, _, apiErr := acc.selectLimits(cfg) + if apiErr != nil { + return apiErr + } + + asa := js.cluster.streams[acc.Name] + numStreams, reservations := tieredStreamAndReservationCount(asa, tier, cfg) + + if selectedLimits.MaxStreams > 0 && numStreams >= selectedLimits.MaxStreams { + return NewJSMaximumStreamsLimitError() + } + // Check for account limits here before proposing. + if err := js.checkAccountLimits(selectedLimits, cfg, reservations); err != nil { + return NewJSStreamLimitsError(err, Unless(err)) + } + return nil +} + +func (s *Server) jsClusteredStreamRequest(ci *ClientInfo, acc *Account, subject, reply string, rmsg []byte, config *StreamConfig) { + js, cc := s.getJetStreamCluster() + if js == nil || cc == nil { return } - ccfg, err := checkStreamCfg(config) - if err != nil { - resp.Error = NewJSStreamInvalidConfigError(err, Unless(err)) + var resp = JSApiStreamCreateResponse{ApiResponse: ApiResponse{Type: JSApiStreamCreateResponseType}} + + ccfg, apiErr := s.checkStreamCfg(config, acc) + if apiErr != nil { + resp.Error = apiErr s.sendAPIErrResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(&resp)) return } cfg := &ccfg - // Check for stream limits here before proposing. These need to be tracked from meta layer, not jsa. js.mu.RLock() + apiErr = js.jsClusteredStreamLimitsCheck(acc, cfg) asa := cc.streams[acc.Name] - numStreams := len(asa) js.mu.RUnlock() - - jsa.mu.RLock() - exceeded := jsa.limits.MaxStreams > 0 && numStreams >= jsa.limits.MaxStreams - jsa.mu.RUnlock() - - if exceeded { - resp.Error = NewJSMaximumStreamsLimitError() - s.sendAPIErrResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(&resp)) - return - } - - // Check for account limits here before proposing. - if err := jsa.checkAccountLimits(cfg); err != nil { - resp.Error = NewJSStreamLimitsError(err, Unless(err)) + // Check for stream limits here before proposing. These need to be tracked from meta layer, not jsa. + if apiErr != nil { + resp.Error = apiErr s.sendAPIErrResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(&resp)) return } @@ -4006,7 +4472,10 @@ func (s *Server) jsClusteredStreamUpdateRequest(ci *ClientInfo, acc *Account, su } var newCfg *StreamConfig if jsa := js.accounts[acc.Name]; jsa != nil { - if ncfg, err := jsa.configUpdateCheck(osa.Config, cfg); err != nil { + js.mu.Unlock() + ncfg, err := jsa.configUpdateCheck(osa.Config, cfg, s) + js.mu.Lock() + if err != nil { resp.Error = NewJSStreamUpdateError(err, Unless(err)) s.sendAPIErrResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(&resp)) return @@ -4018,7 +4487,7 @@ func (s *Server) jsClusteredStreamUpdateRequest(ci *ClientInfo, acc *Account, su s.sendAPIErrResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(&resp)) return } - // Check for mirrot changes which are not allowed. + // Check for mirror changes which are not allowed. if !reflect.DeepEqual(newCfg.Mirror, osa.Config.Mirror) { resp.Error = NewJSStreamMirrorNotUpdatableError() s.sendAPIErrResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(&resp)) @@ -4041,11 +4510,34 @@ func (s *Server) jsClusteredStreamUpdateRequest(ci *ClientInfo, acc *Account, su } } + // Make copy so to not change original. + rg := osa.copyGroup().Group + + // Check for a move update. + // TODO(dlc) - Should add a resolve from Tags to cluster and check that vs reflect. + isMoveRequest := newCfg.Placement != nil && !reflect.DeepEqual(osa.Config.Placement, newCfg.Placement) + // Check for replica changes. - rg := osa.Group + isReplicaChange := newCfg.Replicas != osa.Config.Replicas + + // We stage consumer updates and do them after the stream update. var consumers []*consumerAssignment - if newCfg.Replicas != len(rg.Peers) { + // Check if this is a move request and we are already moving this stream. + if isMoveRequest && osa.Config.Replicas != len(rg.Peers) { + resp.Error = NewJSStreamMoveInProgressError() + s.sendAPIErrResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(&resp)) + return + } + + // Can not move and scale at same time. + if isMoveRequest && isReplicaChange { + resp.Error = NewJSStreamMoveAndScaleError() + s.sendAPIErrResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(&resp)) + return + } + + if isReplicaChange { // We are adding new peers here. if newCfg.Replicas > len(rg.Peers) { peers := cc.selectPeerGroup(newCfg.Replicas, rg.Cluster, newCfg, rg.Peers) @@ -4063,8 +4555,79 @@ func (s *Server) jsClusteredStreamUpdateRequest(ci *ClientInfo, acc *Account, su } rg.Peers = peers } else { - // We are deleting nodes here. - rg.Peers = rg.Peers[:newCfg.Replicas] + // We are deleting nodes here. We want to do our best to preserve the current leader. + // We have support now from above that guarantees we are in our own Go routine, so can + // ask for stream info from the stream leader to make sure we keep the leader in the new list. + var curLeader string + if !s.allPeersOffline(rg) { + // Need to release js lock. + js.mu.Unlock() + s.mu.Lock() + inbox := s.newRespInbox() + results := make(chan *StreamInfo, 1) + // Store our handler. + s.sys.replies[inbox] = func(sub *subscription, _ *client, _ *Account, subject, _ string, msg []byte) { + var si StreamInfo + if err := json.Unmarshal(msg, &si); err != nil { + s.Warnf("Error unmarshaling clustered stream info response:%v", err) + return + } + select { + case results <- &si: + default: + s.Warnf("Failed placing remote stream info result on internal channel") + } + } + s.mu.Unlock() + + isubj := fmt.Sprintf(clusterStreamInfoT, ci.serviceAccount(), cfg.Name) + s.sendInternalMsgLocked(isubj, inbox, nil, nil) + + const timeout = 2 * time.Second + notActive := time.NewTimer(timeout) + defer notActive.Stop() + + select { + case <-s.quitCh: + break + case <-notActive.C: + s.Warnf("Did not receive stream info results for '%s > %s'", acc, cfg.Name) + case si := <-results: + if si.Cluster != nil { + // The leader here is the server name, but need to convert to internal name. + curLeader = string(getHash(si.Cluster.Leader)) + } + } + // Clean up here. + s.mu.Lock() + if s.sys != nil && s.sys.replies != nil { + delete(s.sys.replies, inbox) + } + s.mu.Unlock() + // Re-acquire here. + js.mu.Lock() + } + // If we identified a leader make sure its part of the new group. + selected := make([]string, 0, newCfg.Replicas) + + if curLeader != _EMPTY_ { + selected = append(selected, curLeader) + } + for _, peer := range rg.Peers { + if len(selected) == newCfg.Replicas { + break + } + if peer == curLeader { + continue + } + if si, ok := s.nodeToInfo.Load(peer); ok && si != nil { + if si.(nodeInfo).offline { + continue + } + selected = append(selected, peer) + } + } + rg.Peers = selected } // Need to remap any consumers. @@ -4085,15 +4648,56 @@ func (s *Server) jsClusteredStreamUpdateRequest(ci *ClientInfo, acc *Account, su consumers = append(consumers, cca) } } + + } else if isMoveRequest { + nrg := js.createGroupForStream(ci, newCfg) + if nrg == nil { + resp.Error = NewJSInsufficientResourcesError() + s.sendAPIErrResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(&resp)) + return + } + // Only change if resolved clusters are different. + if rg.Cluster != nrg.Cluster { + // If we are R1, make sure original is leader during scale up for move. + if len(rg.Peers) == 1 { + rg.Preferred = rg.Peers[0] + } + // Add in new peers since we will extend the peer group to straddle both clusters. + rg.Peers = append(rg.Peers, nrg.Peers...) + rg.Cluster = nrg.Cluster + + for _, ca := range osa.consumers { + cca := ca.copyGroup() + // Ephemerals are R=1, so only auto-remap if consumer peer count == nrg peer count. + numPeers := len(ca.Group.Peers) + if numPeers == len(nrg.Peers) { + cca.Group.Peers = append(cca.Group.Peers, nrg.Peers...) + } else { + // This is an ephemeral, so R1. Just randomly pick a single peer from the new set. + pi := rand.Int31n(int32(len(nrg.Peers))) + cca.Group.Peers = append(cca.Group.Peers, nrg.Peers[pi]) + } + // Make sure to set if not already set. + if cca.Group.Preferred == _EMPTY_ { + cca.Group.Preferred = cca.Group.Peers[0] + } + // We can not propose here before the stream itself so we collect them. + consumers = append(consumers, cca) + } + } + } else { + // All other updates make sure no preferred is set. + rg.Preferred = _EMPTY_ } - sa := &streamAssignment{Group: rg, Sync: osa.Sync, Config: newCfg, Subject: subject, Reply: reply, Client: ci} + sa := &streamAssignment{Group: rg, Sync: osa.Sync, Created: osa.Created, Config: newCfg, Subject: subject, Reply: reply, Client: ci} cc.meta.Propose(encodeUpdateStreamAssignment(sa)) // Process any staged consumers. for _, ca := range consumers { cc.meta.Propose(encodeAddConsumerAssignment(ca)) } + } func (s *Server) jsClusteredStreamDeleteRequest(ci *ClientInfo, acc *Account, stream, subject, reply string, rmsg []byte) { @@ -4187,6 +4791,12 @@ func (s *Server) jsClusteredStreamRestoreRequest( cfg := &req.Config resp := JSApiStreamRestoreResponse{ApiResponse: ApiResponse{Type: JSApiStreamRestoreResponseType}} + if err := js.jsClusteredStreamLimitsCheck(acc, cfg); err != nil { + resp.Error = err + s.sendAPIErrResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(&resp)) + return + } + if sa := js.streamAssignment(ci.serviceAccount(), cfg.Name); sa != nil { resp.Error = NewJSStreamNameExistError() s.sendAPIErrResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(&resp)) @@ -4208,6 +4818,7 @@ func (s *Server) jsClusteredStreamRestoreRequest( cc.meta.Propose(encodeAddStreamAssignment(sa)) } +// Determine if all peers for this group are offline. func (s *Server) allPeersOffline(rg *raftGroup) bool { if rg == nil { return false @@ -4233,7 +4844,7 @@ func (s *Server) jsClusteredStreamListRequest(acc *Account, ci *ClientInfo, filt return } - js.mu.Lock() + js.mu.RLock() var streams []*streamAssignment for _, sa := range cc.streams[acc.Name] { @@ -4283,8 +4894,9 @@ func (s *Server) jsClusteredStreamListRequest(acc *Account, ci *ClientInfo, filt Streams: make([]*StreamInfo, 0, len(streams)), } + js.mu.RUnlock() + if len(streams) == 0 { - js.mu.Unlock() resp.Limit = JSApiListLimit resp.Offset = offset s.sendAPIResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(resp)) @@ -4322,7 +4934,9 @@ func (s *Server) jsClusteredStreamListRequest(acc *Account, ci *ClientInfo, filt var missingNames []string sent := map[string]int{} + // Send out our requests here. + js.mu.RLock() for _, sa := range streams { if s.allPeersOffline(sa.Group) { // Place offline onto our results by hand here. @@ -4336,14 +4950,14 @@ func (s *Server) jsClusteredStreamListRequest(acc *Account, ci *ClientInfo, filt } } // Don't hold lock. - js.mu.Unlock() + js.mu.RUnlock() const timeout = 4 * time.Second notActive := time.NewTimer(timeout) defer notActive.Stop() LOOP: - for { + for len(sent) > 0 { select { case <-s.quitCh: return @@ -4352,7 +4966,6 @@ LOOP: for sName := range sent { missingNames = append(missingNames, sName) } - resp.Missing = missingNames break LOOP case si := <-rc: consCount := sent[si.Config.Name] @@ -4378,6 +4991,7 @@ LOOP: resp.Total = len(resp.Streams) resp.Limit = JSApiListLimit resp.Offset = offset + resp.Missing = missingNames s.sendAPIResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(resp)) } @@ -4391,7 +5005,7 @@ func (s *Server) jsClusteredConsumerListRequest(acc *Account, ci *ClientInfo, of return } - js.mu.Lock() + js.mu.RLock() var consumers []*consumerAssignment if sas := cc.streams[acc.Name]; sas != nil { @@ -4426,8 +5040,9 @@ func (s *Server) jsClusteredConsumerListRequest(acc *Account, ci *ClientInfo, of Consumers: []*ConsumerInfo{}, } + js.mu.RUnlock() + if len(consumers) == 0 { - js.mu.Unlock() resp.Limit = JSApiListLimit resp.Offset = offset s.sendAPIResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(resp)) @@ -4465,35 +5080,38 @@ func (s *Server) jsClusteredConsumerListRequest(acc *Account, ci *ClientInfo, of var missingNames []string sent := map[string]struct{}{} + + // Send out our requests here. + js.mu.RLock() for _, ca := range consumers { if s.allPeersOffline(ca.Group) { // Place offline onto our results by hand here. ci := &ConsumerInfo{Config: ca.Config, Created: ca.Created, Cluster: js.offlineClusterInfo(ca.Group)} resp.Consumers = append(resp.Consumers, ci) - missingNames = append(missingNames, ci.Name) + missingNames = append(missingNames, ca.Name) } else { isubj := fmt.Sprintf(clusterConsumerInfoT, ca.Client.serviceAccount(), stream, ca.Name) s.sendInternalMsgLocked(isubj, inbox, nil, nil) sent[ca.Name] = struct{}{} } } - js.mu.Unlock() + // Don't hold lock. + js.mu.RUnlock() const timeout = 4 * time.Second notActive := time.NewTimer(timeout) defer notActive.Stop() LOOP: - for { + for len(sent) > 0 { select { case <-s.quitCh: return case <-notActive.C: - s.Warnf("Did not receive all consumer info results for %q", acc) + s.Warnf("Did not receive all consumer info results for '%s > %s'", acc, stream) for cName := range sent { missingNames = append(missingNames, cName) } - resp.Missing = missingNames break LOOP case ci := <-rc: delete(sent, ci.Name) @@ -4515,6 +5133,7 @@ LOOP: resp.Total = len(resp.Consumers) resp.Limit = JSApiListLimit resp.Offset = offset + resp.Missing = missingNames s.sendAPIResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(resp)) } @@ -4647,16 +5266,28 @@ func encodeDeleteStreamAssignment(sa *streamAssignment) []byte { func decodeStreamAssignment(buf []byte) (*streamAssignment, error) { var sa streamAssignment err := json.Unmarshal(buf, &sa) + if err != nil { + return nil, err + } + fixCfgMirrorWithDedupWindow(sa.Config) return &sa, err } -// createGroupForConsumer will create a new group with same peer set as the stream. -func (cc *jetStreamCluster) createGroupForConsumer(sa *streamAssignment) *raftGroup { - peers := sa.Group.Peers +// createGroupForConsumer will create a new group from same peer set as the stream. +func (cc *jetStreamCluster) createGroupForConsumer(cfg *ConsumerConfig, sa *streamAssignment) *raftGroup { + peers := copyStrings(sa.Group.Peers) if len(peers) == 0 { return nil } - return &raftGroup{Name: groupNameForConsumer(peers, sa.Config.Storage), Storage: sa.Config.Storage, Peers: peers} + if cfg.Replicas > 0 && cfg.Replicas != len(peers) { + rand.Shuffle(len(peers), func(i, j int) { peers[i], peers[j] = peers[j], peers[i] }) + peers = peers[:cfg.Replicas] + } + storage := sa.Config.Storage + if cfg.MemoryStorage { + storage = MemoryStorage + } + return &raftGroup{Name: groupNameForConsumer(peers, storage), Storage: storage, Peers: peers} } // jsClusteredConsumerRequest is first point of entry to create a consumer with R > 1. @@ -4666,11 +5297,33 @@ func (s *Server) jsClusteredConsumerRequest(ci *ClientInfo, acc *Account, subjec return } + var resp = JSApiConsumerCreateResponse{ApiResponse: ApiResponse{Type: JSApiConsumerCreateResponseType}} + + streamCfg, ok := js.clusterStreamConfig(acc.Name, stream) + if !ok { + resp.Error = NewJSStreamNotFoundError() + s.sendAPIErrResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(&resp)) + return + } + selectedLimits, _, _, apiErr := acc.selectLimits(&streamCfg) + if apiErr != nil { + resp.Error = apiErr + s.sendAPIErrResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(&resp)) + return + } + srvLim := &s.getOpts().JetStreamLimits + // Make sure we have sane defaults + setConsumerConfigDefaults(cfg, srvLim, selectedLimits) + + if err := checkConsumerCfg(cfg, srvLim, &streamCfg, acc, selectedLimits, false); err != nil { + resp.Error = err + s.sendAPIErrResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(&resp)) + return + } + js.mu.Lock() defer js.mu.Unlock() - var resp = JSApiConsumerCreateResponse{ApiResponse: ApiResponse{Type: JSApiConsumerCreateResponseType}} - // Lookup the stream assignment. sa := js.streamAssignment(acc.Name, stream) if sa == nil { @@ -4735,7 +5388,7 @@ func (s *Server) jsClusteredConsumerRequest(ci *ClientInfo, acc *Account, subjec // If this is new consumer. if ca == nil { - rg := cc.createGroupForConsumer(sa) + rg := cc.createGroupForConsumer(cfg, sa) if rg == nil { resp.Error = NewJSInsufficientResourcesError() s.sendAPIErrResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(&resp)) @@ -4744,6 +5397,9 @@ func (s *Server) jsClusteredConsumerRequest(ci *ClientInfo, acc *Account, subjec // Pick a preferred leader. rg.setPreferred() + // Inherit cluster from stream. + rg.Cluster = sa.Group.Cluster + // We need to set the ephemeral here before replicating. if !isDurableConsumer(cfg) { // We chose to have ephemerals be R=1 unless stream is interest or workqueue. @@ -4762,6 +5418,23 @@ func (s *Server) jsClusteredConsumerRequest(ci *ClientInfo, acc *Account, subjec break } } + if len(rg.Peers) > 1 { + if maxHaAssets := s.getOpts().JetStreamLimits.MaxHAAssets; maxHaAssets != 0 { + for _, peer := range rg.Peers { + if ni, ok := s.nodeToInfo.Load(peer); ok { + ni := ni.(nodeInfo) + if stats := ni.stats; stats != nil && stats.HAAssets > maxHaAssets { + resp.Error = NewJSInsufficientResourcesError() + s.sendAPIErrResponse(ci, acc, subject, reply, string(rmsg), s.jsonResponse(&resp)) + s.Warnf("%s@%s (HA Asset Count: %d) exceeds max ha asset limit of %d"+ + " for (durable) consumer %s placement on stream %s", + ni.name, ni.cluster, ni.stats.HAAssets, maxHaAssets, oname, stream) + return + } + } + } + } + } ca = &consumerAssignment{ Group: rg, Stream: stream, @@ -4962,7 +5635,7 @@ func (mset *stream) processClusteredInboundMsg(subject, reply string, hdr, msg [ mset.mu.RLock() canRespond := !mset.cfg.NoAck && len(reply) > 0 name, stype := mset.cfg.Name, mset.cfg.Storage - s, js, jsa, st, rf, outq, node := mset.srv, mset.js, mset.jsa, mset.cfg.Storage, mset.cfg.Replicas, mset.outq, mset.node + s, js, jsa, st, rf, tierName, outq, node := mset.srv, mset.js, mset.jsa, mset.cfg.Storage, mset.cfg.Replicas, mset.tier, mset.outq, mset.node maxMsgSize, lseq := int(mset.cfg.MaxMsgSize), mset.lseq mset.mu.RUnlock() @@ -4987,24 +5660,42 @@ func (mset *stream) processClusteredInboundMsg(subject, reply string, hdr, msg [ // Check here pre-emptively if we have exceeded our account limits. var exceeded bool - jsa.mu.RLock() + jsa.usageMu.Lock() + jsaLimits, ok := jsa.limits[tierName] + if !ok { + jsa.usageMu.Unlock() + err := fmt.Errorf("no JetStream resource limits found account: %q", jsa.acc().Name) + s.RateLimitWarnf(err.Error()) + if canRespond { + var resp = &JSPubAckResponse{PubAck: &PubAck{Stream: name}} + resp.Error = NewJSNoLimitsError() + response, _ = json.Marshal(resp) + outq.send(newJSPubMsg(reply, _EMPTY_, _EMPTY_, nil, response, nil, 0)) + } + return err + } + t, ok := jsa.usage[tierName] + if !ok { + t = &jsaStorage{} + jsa.usage[tierName] = t + } if st == MemoryStorage { - total := jsa.storeTotal + int64(memStoreMsgSize(subject, hdr, msg)*uint64(rf)) - if jsa.limits.MaxMemory > 0 && total > jsa.limits.MaxMemory { + total := t.total.store + int64(memStoreMsgSize(subject, hdr, msg)*uint64(rf)) + if jsaLimits.MaxMemory > 0 && total > jsaLimits.MaxMemory { exceeded = true } } else { - total := jsa.storeTotal + int64(fileStoreMsgSize(subject, hdr, msg)*uint64(rf)) - if jsa.limits.MaxStore > 0 && total > jsa.limits.MaxStore { + total := t.total.store + int64(fileStoreMsgSize(subject, hdr, msg)*uint64(rf)) + if jsaLimits.MaxStore > 0 && total > jsaLimits.MaxStore { exceeded = true } } - jsa.mu.RUnlock() + jsa.usageMu.Unlock() // If we have exceeded our account limits go ahead and return. if exceeded { err := fmt.Errorf("JetStream resource limits exceeded for account: %q", jsa.acc().Name) - s.Warnf(err.Error()) + s.RateLimitWarnf(err.Error()) if canRespond { var resp = &JSPubAckResponse{PubAck: &PubAck{Stream: name}} resp.Error = NewJSAccountResourcesExceededError() @@ -5017,7 +5708,7 @@ func (mset *stream) processClusteredInboundMsg(subject, reply string, hdr, msg [ // Check msgSize if we have a limit set there. Again this works if it goes through but better to be pre-emptive. if maxMsgSize >= 0 && (len(hdr)+len(msg)) > maxMsgSize { err := fmt.Errorf("JetStream message size exceeds limits for '%s > %s'", jsa.acc().Name, mset.cfg.Name) - s.Warnf(err.Error()) + s.RateLimitWarnf(err.Error()) if canRespond { var resp = &JSPubAckResponse{PubAck: &PubAck{Stream: name}} resp.Error = NewJSStreamMessageExceedsMaximumError() @@ -5031,7 +5722,7 @@ func (mset *stream) processClusteredInboundMsg(subject, reply string, hdr, msg [ // Again this works if it goes through but better to be pre-emptive. if len(hdr) > math.MaxUint16 { err := fmt.Errorf("JetStream header size exceeds limits for '%s > %s'", jsa.acc().Name, mset.cfg.Name) - s.Warnf(err.Error()) + s.RateLimitWarnf(err.Error()) if canRespond { var resp = &JSPubAckResponse{PubAck: &PubAck{Stream: name}} resp.Error = NewJSStreamHeaderExceedsMaximumError() @@ -5185,8 +5876,11 @@ func (mset *stream) isCatchingUp() bool { return mset.catchup } +// Maximum requests for the whole server that can be in flight. +const maxConcurrentSyncRequests = 8 + // Process a stream snapshot. -func (mset *stream) processSnapshot(snap *streamSnapshot) error { +func (mset *stream) processSnapshot(snap *streamSnapshot) (e error) { // Update any deletes, etc. mset.processSnapshotDeletes(snap) @@ -5195,8 +5889,9 @@ func (mset *stream) processSnapshot(snap *streamSnapshot) error { mset.clfs = snap.Failed mset.store.FastState(&state) sreq := mset.calculateSyncRequest(&state, snap) + s, js, subject, n := mset.srv, mset.js, mset.sa.Sync, mset.node - qname := fmt.Sprintf("Stream %q snapshot", mset.cfg.Name) + qname := fmt.Sprintf("[ACC:%s] stream '%s' snapshot", mset.acc.Name, mset.cfg.Name) mset.mu.Unlock() // Make sure our state's first sequence is <= the leader's snapshot. @@ -5215,8 +5910,20 @@ func (mset *stream) processSnapshot(snap *streamSnapshot) error { } // Pause the apply channel for our raft group while we catch up. - n.PauseApply() - defer n.ResumeApply() + if err := n.PauseApply(); err != nil { + return err + } + + // ErrStreamStopped is when a catchup is terminated due to the stream going away. + var ErrStreamStopped = errors.New("stream has been stopped") + + defer func() { + if e == ErrServerNotRunning || e == ErrStreamStopped { + // Wipe our raft state if exiting with these errors. + n.Wipe() + } + n.ResumeApply() + }() // Set our catchup state. mset.setCatchingUp() @@ -5225,7 +5932,7 @@ func (mset *stream) processSnapshot(snap *streamSnapshot) error { var sub *subscription var err error - const activityInterval = 5 * time.Second + const activityInterval = 10 * time.Second notActive := time.NewTimer(activityInterval) defer notActive.Stop() @@ -5238,21 +5945,61 @@ func (mset *stream) processSnapshot(snap *streamSnapshot) error { for _, o := range mset.consumers { o.mu.Lock() if o.isLeader() { - // This expects mset lock to be held. - o.setInitialPendingAndStart() + o.streamNumPending() } o.mu.Unlock() } mset.mu.Unlock() }() + var releaseSem bool + releaseSyncOutSem := func() { + if !releaseSem { + return + } + // Need to use select for the server shutdown case. + select { + case s.syncOutSem <- struct{}{}: + default: + } + releaseSem = false + } + // On exit, we will release our semaphore if we acquired it. + defer releaseSyncOutSem() + RETRY: + // On retry, we need to release the semaphore we got. Call will be no-op + // if releaseSem boolean has not been set to true on successfully getting + // the semaphore. + releaseSyncOutSem() + + if n.GroupLeader() == _EMPTY_ { + return fmt.Errorf("catchup for stream '%s > %s' aborted, no leader", mset.account(), mset.name()) + } + // If we have a sub clear that here. if sub != nil { s.sysUnsubscribe(sub) sub = nil } + // Block here if we have too many requests in flight. + <-s.syncOutSem + releaseSem = true + if !s.isRunning() { + return ErrServerNotRunning + } + + // We may have been blocked for a bit, so the reset need to ensure that we + // consume the already fired timer. + if !notActive.Stop() { + select { + case <-notActive.C: + default: + } + } + notActive.Reset(activityInterval) + // Grab sync request again on failures. if sreq == nil { mset.mu.Lock() @@ -5271,7 +6018,8 @@ RETRY: reply string } - msgsQ := newIPQueue(ipQueue_Logger(qname, s.ipqLog)) // of *im + msgsQ := s.newIPQueue(qname) // of *im + defer msgsQ.unregister() // Send our catchup request here. reply := syncReplySubject() @@ -5282,9 +6030,9 @@ RETRY: }) if err != nil { s.Errorf("Could not subscribe to stream catchup: %v", err) - return err + err = nil + goto RETRY } - b, _ := json.Marshal(sreq) s.sendInternalMsgLocked(subject, reply, nil, b) @@ -5305,10 +6053,12 @@ RETRY: // Check for eof signaling. if len(msg) == 0 { + msgsQ.recycle(&mrecs) return nil } if lseq, err := mset.processCatchupMsg(msg); err == nil { if lseq >= last { + msgsQ.recycle(&mrecs) return nil } } else if isOutOfSpaceErr(err) { @@ -5319,9 +6069,11 @@ RETRY: } else { s.Warnf("Catchup for stream '%s > %s' errored, account resources exceeded: %v", mset.account(), mset.name(), err) } + msgsQ.recycle(&mrecs) return err } else { s.Warnf("Catchup for stream '%s > %s' errored, will retry: %v", mset.account(), mset.name(), err) + msgsQ.recycle(&mrecs) goto RETRY } if mrec.reply != _EMPTY_ { @@ -5331,15 +6083,16 @@ RETRY: msgsQ.recycle(&mrecs) case <-notActive.C: s.Warnf("Catchup for stream '%s > %s' stalled", mset.account(), mset.name()) - notActive.Reset(activityInterval) goto RETRY case <-s.quitCh: - return nil + return ErrServerNotRunning case <-qch: - return nil + return ErrStreamStopped case isLeader := <-lch: - js.processStreamLeaderChange(mset, isLeader) - return nil + if isLeader { + n.StepDown() + goto RETRY + } } } } @@ -5357,9 +6110,15 @@ func (mset *stream) processCatchupMsg(msg []byte) (uint64, error) { mset.mu.RLock() st := mset.cfg.Storage + ddloaded := mset.ddloaded + tierName := mset.tier mset.mu.RUnlock() - if mset.js.limitsExceeded(st) || mset.jsa.limitsExceeded(st) { + if mset.js.limitsExceeded(st) { + return 0, NewJSInsufficientResourcesError() + } else if exceeded, apiErr := mset.jsa.limitsExceeded(st, tierName); apiErr != nil { + return 0, apiErr + } else if exceeded { return 0, NewJSInsufficientResourcesError() } @@ -5377,6 +6136,18 @@ func (mset *stream) processCatchupMsg(msg []byte) (uint64, error) { // Update our lseq. mset.setLastSeq(seq) + // Check for MsgId and if we have one here make sure to update our internal map. + if len(hdr) > 0 { + if msgId := getMsgId(hdr); msgId != _EMPTY_ { + if !ddloaded { + mset.mu.Lock() + mset.rebuildDedupe() + mset.mu.Unlock() + } + mset.storeMsgId(&ddentry{msgId, seq, ts}) + } + } + return seq, nil } @@ -5447,7 +6218,15 @@ func (js *jetStream) clusterInfo(rg *raftGroup) *ClusterInfo { } if sir, ok := s.nodeToInfo.Load(rp.ID); ok && sir != nil { si := sir.(nodeInfo) - pi := &PeerInfo{Name: si.name, Current: current, Offline: si.offline, Active: lastSeen, Lag: rp.Lag} + pi := &PeerInfo{ + Name: si.name, + Current: current, + Offline: si.offline, + Active: lastSeen, + Lag: rp.Lag, + cluster: si.cluster, + peer: rp.ID, + } ci.Replicas = append(ci.Replicas, pi) } } @@ -5455,8 +6234,8 @@ func (js *jetStream) clusterInfo(rg *raftGroup) *ClusterInfo { return ci } -func (mset *stream) checkClusterInfo(si *StreamInfo) { - for _, r := range si.Cluster.Replicas { +func (mset *stream) checkClusterInfo(ci *ClusterInfo) { + for _, r := range ci.Replicas { peer := string(getHash(r.Name)) if lag := mset.lagForCatchupPeer(peer); lag > 0 { r.Current = false @@ -5465,6 +6244,59 @@ func (mset *stream) checkClusterInfo(si *StreamInfo) { } } +// Return a list of alternates, ranked by preference order to the request, of stream mirrors. +// This allows clients to select or get more information about read replicas that could be a +// better option to connect to versus the original source. +func (js *jetStream) streamAlternates(ci *ClientInfo, stream string) []StreamAlternate { + if js == nil { + return nil + } + + js.mu.RLock() + defer js.mu.RUnlock() + + s, cc := js.srv, js.cluster + // Track our domain. + domain := s.getOpts().JetStreamDomain + + // No clustering just return nil. + if cc == nil { + return nil + } + acc, _ := s.LookupAccount(ci.serviceAccount()) + if acc == nil { + return nil + } + + // Collect our ordering first for clusters. + weights := make(map[string]int) + all := []string{ci.Cluster} + all = append(all, ci.Alternates...) + + for i := 0; i < len(all); i++ { + weights[all[i]] = len(all) - i + } + + var alts []StreamAlternate + for _, sa := range cc.streams[acc.Name] { + // Add in ourselves and any mirrors. + if sa.Config.Name == stream || (sa.Config.Mirror != nil && sa.Config.Mirror.Name == stream) { + alts = append(alts, StreamAlternate{Name: sa.Config.Name, Domain: domain, Cluster: sa.Group.Cluster}) + } + } + // If just us don't fill in. + if len(alts) == 1 { + return nil + } + + // Sort based on our weights that originate from the request itself. + sort.Slice(alts, func(i, j int) bool { + return weights[alts[i].Cluster] > weights[alts[j].Cluster] + }) + + return alts +} + func (mset *stream) handleClusterStreamInfoRequest(sub *subscription, c *client, _ *Account, subject, reply string, _ []byte) { mset.mu.RLock() sysc, js, sa, config := mset.sysc, mset.srv.js, mset.sa, mset.cfg @@ -5494,21 +6326,87 @@ func (mset *stream) handleClusterStreamInfoRequest(sub *subscription, c *client, // Check for out of band catchups. if mset.hasCatchupPeers() { - mset.checkClusterInfo(si) + mset.checkClusterInfo(si.Cluster) } sysc.sendInternalMsg(reply, _EMPTY_, nil, si) } +const maxTotalCatchupOutBytes = int64(128 * 1024 * 1024) // 128MB for now, for the total server. + +// Current total outstanding catchup bytes. +func (s *Server) gcbTotal() int64 { + s.gcbMu.RLock() + defer s.gcbMu.RUnlock() + return s.gcbOut +} + +// Adds `sz` to the server's total outstanding catchup bytes and to `localsz` +// under the gcbMu lock. The `localsz` points to the local outstanding catchup +// bytes of the runCatchup go routine of a given stream. +func (s *Server) gcbAdd(localsz *int64, sz int64) { + s.gcbMu.Lock() + atomic.AddInt64(localsz, sz) + s.gcbOut += sz + if s.gcbOut >= maxTotalCatchupOutBytes && s.gcbKick == nil { + s.gcbKick = make(chan struct{}) + } + s.gcbMu.Unlock() +} + +// Removes `sz` from the server's total outstanding catchup bytes and from +// `localsz`, but only if `localsz` is non 0, which would signal that gcSubLast +// has already been invoked. See that function for details. +// Must be invoked under the gcbMu lock. +func (s *Server) gcbSubLocked(localsz *int64, sz int64) { + if atomic.LoadInt64(localsz) == 0 { + return + } + atomic.AddInt64(localsz, -sz) + s.gcbOut -= sz + if s.gcbKick != nil && s.gcbOut < maxTotalCatchupOutBytes { + close(s.gcbKick) + s.gcbKick = nil + } +} + +// Locked version of gcbSubLocked() +func (s *Server) gcbSub(localsz *int64, sz int64) { + s.gcbMu.Lock() + s.gcbSubLocked(localsz, sz) + s.gcbMu.Unlock() +} + +// Similar to gcbSub() but reset `localsz` to 0 at the end under the gcbMu lock. +// This will signal further calls to gcbSub() for this `localsz` pointer that +// nothing should be done because runCatchup() has exited and any remaining +// outstanding bytes value has already been decremented. +func (s *Server) gcbSubLast(localsz *int64) { + s.gcbMu.Lock() + s.gcbSubLocked(localsz, *localsz) + *localsz = 0 + s.gcbMu.Unlock() +} + +// Returns our kick chan, or nil if it does not exist. +func (s *Server) cbKickChan() <-chan struct{} { + s.gcbMu.RLock() + defer s.gcbMu.RUnlock() + return s.gcbKick +} + func (mset *stream) runCatchup(sendSubject string, sreq *streamSyncRequest) { s := mset.srv defer s.grWG.Done() - const maxOutBytes = int64(1 * 1024 * 1024) // 1MB for now. - const maxOutMsgs = int32(16384) + const maxOutBytes = int64(32 * 1024 * 1024) // 32MB for now, these are all internal, from server to server + const maxOutMsgs = int32(128 * 1024) outb := int64(0) outm := int32(0) + // On abnormal exit make sure to update global total. + defer s.gcbSubLast(&outb) + // Flow control processing. ackReplySize := func(subj string) int64 { if li := strings.LastIndexByte(subj, btsep); li > 0 && li < len(subj) { @@ -5524,9 +6422,10 @@ func (mset *stream) runCatchup(sendSubject string, sreq *streamSyncRequest) { ackReply := syncAckSubject() ackSub, _ := s.sysSubscribe(ackReply, func(sub *subscription, c *client, _ *Account, subject, reply string, msg []byte) { sz := ackReplySize(subject) - atomic.AddInt64(&outb, -sz) + s.gcbSub(&outb, sz) atomic.AddInt32(&outm, -1) mset.updateCatchupPeer(sreq.Peer) + // Kick ourselves and anyone else who might have stalled on global state. select { case nextBatchC <- struct{}{}: default: @@ -5535,9 +6434,6 @@ func (mset *stream) runCatchup(sendSubject string, sreq *streamSyncRequest) { defer s.sysUnsubscribe(ackSub) ackReplyT := strings.ReplaceAll(ackReply, ".*", ".%d") - // EOF - defer s.sendInternalMsgLocked(sendSubject, _EMPTY_, nil, nil) - const activityInterval = 5 * time.Second notActive := time.NewTimer(activityInterval) defer notActive.Stop() @@ -5547,23 +6443,40 @@ func (mset *stream) runCatchup(sendSubject string, sreq *streamSyncRequest) { mset.setCatchupPeer(sreq.Peer, last-seq) defer mset.clearCatchupPeer(sreq.Peer) - sendNextBatch := func() { - for ; seq <= last && atomic.LoadInt64(&outb) <= maxOutBytes && atomic.LoadInt32(&outm) <= maxOutMsgs; seq++ { - subj, hdr, msg, ts, err := mset.store.LoadMsg(seq) + sendNextBatchAndContinue := func() bool { + // Update our activity timer. + notActive.Reset(activityInterval) + + var smv StoreMsg + for ; seq <= last && atomic.LoadInt64(&outb) <= maxOutBytes && atomic.LoadInt32(&outm) <= maxOutMsgs && s.gcbTotal() <= maxTotalCatchupOutBytes; seq++ { + sm, err := mset.store.LoadMsg(seq, &smv) // if this is not a deleted msg, bail out. if err != nil && err != ErrStoreMsgNotFound && err != errDeletedMsg { - // break, something changed. - seq = last + 1 - return + s.Warnf("Error loading message for catchup '%s > %s': %v", mset.account(), mset.name(), err) + return false } // S2? - em := encodeStreamMsg(subj, _EMPTY_, hdr, msg, seq, ts) + var em []byte + if sm != nil { + em = encodeStreamMsg(sm.subj, _EMPTY_, sm.hdr, sm.msg, sm.seq, sm.ts) + } else { + // Skip record for deleted msg. + em = encodeStreamMsg(_EMPTY_, _EMPTY_, nil, nil, seq, 0) + } // Place size in reply subject for flow control. - reply := fmt.Sprintf(ackReplyT, len(em)) - atomic.AddInt64(&outb, int64(len(em))) + l := int64(len(em)) + reply := fmt.Sprintf(ackReplyT, l) + s.gcbAdd(&outb, l) atomic.AddInt32(&outm, 1) s.sendInternalMsgLocked(sendSubject, reply, nil, em) + if seq == last { + s.Noticef("Catchup for stream '%s > %s' complete", mset.account(), mset.name()) + // EOF + s.sendInternalMsgLocked(sendSubject, _EMPTY_, nil, nil) + return false + } } + return true } // Grab stream quit channel. @@ -5577,6 +6490,9 @@ func (mset *stream) runCatchup(sendSubject string, sreq *streamSyncRequest) { // Run as long as we are still active and need catchup. // FIXME(dlc) - Purge event? Stream delete? for { + // Get this each time, will be non-nil if globally blocked and we will close to wake everyone up. + cbKick := s.cbKickChan() + select { case <-s.quitCh: return @@ -5586,12 +6502,11 @@ func (mset *stream) runCatchup(sendSubject string, sreq *streamSyncRequest) { s.Warnf("Catchup for stream '%s > %s' stalled", mset.account(), mset.name()) return case <-nextBatchC: - // Update our activity timer. - notActive.Reset(activityInterval) - sendNextBatch() - // Check if we are finished. - if seq > last { - s.Debugf("Done resync for stream '%s > %s'", mset.account(), mset.name()) + if !sendNextBatchAndContinue() { + return + } + case <-cbKick: + if !sendNextBatchAndContinue() { return } } diff --git a/vendor/github.com/nats-io/nats-server/v2/server/jetstream_errors_generated.go b/vendor/github.com/nats-io/nats-server/v2/server/jetstream_errors_generated.go index 3ff7d762..be507d1a 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/jetstream_errors_generated.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/jetstream_errors_generated.go @@ -107,9 +107,15 @@ const ( // JSConsumerMaxDeliverBackoffErr max deliver is required to be > length of backoff values JSConsumerMaxDeliverBackoffErr ErrorIdentifier = 10116 + // JSConsumerMaxPendingAckExcessErrF consumer max ack pending exceeds system limit of {limit} + JSConsumerMaxPendingAckExcessErrF ErrorIdentifier = 10121 + // JSConsumerMaxPendingAckPolicyRequiredErr consumer requires ack policy for max ack pending JSConsumerMaxPendingAckPolicyRequiredErr ErrorIdentifier = 10082 + // JSConsumerMaxRequestBatchExceededF consumer max request batch exceeds server limit of {limit} + JSConsumerMaxRequestBatchExceededF ErrorIdentifier = 10125 + // JSConsumerMaxRequestBatchNegativeErr consumer max request batch needs to be > 0 JSConsumerMaxRequestBatchNegativeErr ErrorIdentifier = 10114 @@ -128,6 +134,9 @@ const ( // JSConsumerNotFoundErr consumer not found JSConsumerNotFoundErr ErrorIdentifier = 10014 + // JSConsumerOfflineErr consumer is offline + JSConsumerOfflineErr ErrorIdentifier = 10119 + // JSConsumerOnMappedErr consumer direct on a mapped consumer JSConsumerOnMappedErr ErrorIdentifier = 10092 @@ -146,6 +155,9 @@ const ( // JSConsumerReplacementWithDifferentNameErr consumer replacement durable config not the same JSConsumerReplacementWithDifferentNameErr ErrorIdentifier = 10106 + // JSConsumerReplicasExceedsStream consumer config replica count exceeds parent stream + JSConsumerReplicasExceedsStream ErrorIdentifier = 10126 + // JSConsumerSmallHeartbeatErr consumer idle heartbeat needs to be >= 100ms JSConsumerSmallHeartbeatErr ErrorIdentifier = 10083 @@ -182,7 +194,7 @@ const ( // JSMemoryResourcesExceededErr insufficient memory resources available JSMemoryResourcesExceededErr ErrorIdentifier = 10028 - // JSMirrorConsumerSetupFailedErrF Generic mirror consumer setup failure string ({err}) + // JSMirrorConsumerSetupFailedErrF generic mirror consumer setup failure string ({err}) JSMirrorConsumerSetupFailedErrF ErrorIdentifier = 10029 // JSMirrorMaxMessageSizeTooBigErr stream mirror must have max message size >= source @@ -197,12 +209,15 @@ const ( // JSMirrorWithSubjectFiltersErr stream mirrors can not contain filtered subjects JSMirrorWithSubjectFiltersErr ErrorIdentifier = 10033 - // JSMirrorWithSubjectsErr stream mirrors can not also contain subjects + // JSMirrorWithSubjectsErr stream mirrors can not contain subjects JSMirrorWithSubjectsErr ErrorIdentifier = 10034 // JSNoAccountErr account not found JSNoAccountErr ErrorIdentifier = 10035 + // JSNoLimitsErr no JetStream default or applicable tiered limit present + JSNoLimitsErr ErrorIdentifier = 10120 + // JSNoMessageFoundErr no message found JSNoMessageFoundErr ErrorIdentifier = 10037 @@ -278,15 +293,24 @@ const ( // JSStreamMaxBytesRequired account requires a stream config to have max bytes set JSStreamMaxBytesRequired ErrorIdentifier = 10113 + // JSStreamMaxStreamBytesExceeded stream max bytes exceeds account limit max stream bytes + JSStreamMaxStreamBytesExceeded ErrorIdentifier = 10122 + // JSStreamMessageExceedsMaximumErr message size exceeds maximum allowed JSStreamMessageExceedsMaximumErr ErrorIdentifier = 10054 - // JSStreamMirrorNotUpdatableErr Mirror configuration can not be updated + // JSStreamMirrorNotUpdatableErr stream mirror configuration can not be updated JSStreamMirrorNotUpdatableErr ErrorIdentifier = 10055 // JSStreamMismatchErr stream name in subject does not match request JSStreamMismatchErr ErrorIdentifier = 10056 + // JSStreamMoveAndScaleErr can not move and scale a stream in a single update + JSStreamMoveAndScaleErr ErrorIdentifier = 10123 + + // JSStreamMoveInProgress stream move already in progress + JSStreamMoveInProgress ErrorIdentifier = 10124 + // JSStreamMsgDeleteFailedF Generic message deletion failure error string ({err}) JSStreamMsgDeleteFailedF ErrorIdentifier = 10057 @@ -299,6 +323,9 @@ const ( // JSStreamNotMatchErr expected stream does not match JSStreamNotMatchErr ErrorIdentifier = 10060 + // JSStreamOfflineErr stream is offline + JSStreamOfflineErr ErrorIdentifier = 10118 + // JSStreamPurgeFailedF Generic stream purge failure error string ({err}) JSStreamPurgeFailedF ErrorIdentifier = 10110 @@ -390,19 +417,23 @@ var ( JSConsumerInvalidPolicyErrF: {Code: 400, ErrCode: 10094, Description: "{err}"}, JSConsumerInvalidSamplingErrF: {Code: 400, ErrCode: 10095, Description: "failed to parse consumer sampling configuration: {err}"}, JSConsumerMaxDeliverBackoffErr: {Code: 400, ErrCode: 10116, Description: "max deliver is required to be > length of backoff values"}, + JSConsumerMaxPendingAckExcessErrF: {Code: 400, ErrCode: 10121, Description: "consumer max ack pending exceeds system limit of {limit}"}, JSConsumerMaxPendingAckPolicyRequiredErr: {Code: 400, ErrCode: 10082, Description: "consumer requires ack policy for max ack pending"}, + JSConsumerMaxRequestBatchExceededF: {Code: 400, ErrCode: 10125, Description: "consumer max request batch exceeds server limit of {limit}"}, JSConsumerMaxRequestBatchNegativeErr: {Code: 400, ErrCode: 10114, Description: "consumer max request batch needs to be > 0"}, JSConsumerMaxRequestExpiresToSmall: {Code: 400, ErrCode: 10115, Description: "consumer max request expires needs to be >= 1ms"}, JSConsumerMaxWaitingNegativeErr: {Code: 400, ErrCode: 10087, Description: "consumer max waiting needs to be positive"}, JSConsumerNameExistErr: {Code: 400, ErrCode: 10013, Description: "consumer name already in use"}, JSConsumerNameTooLongErrF: {Code: 400, ErrCode: 10102, Description: "consumer name is too long, maximum allowed is {max}"}, JSConsumerNotFoundErr: {Code: 404, ErrCode: 10014, Description: "consumer not found"}, + JSConsumerOfflineErr: {Code: 500, ErrCode: 10119, Description: "consumer is offline"}, JSConsumerOnMappedErr: {Code: 400, ErrCode: 10092, Description: "consumer direct on a mapped consumer"}, JSConsumerPullNotDurableErr: {Code: 400, ErrCode: 10085, Description: "consumer in pull mode requires a durable name"}, JSConsumerPullRequiresAckErr: {Code: 400, ErrCode: 10084, Description: "consumer in pull mode requires ack policy"}, JSConsumerPullWithRateLimitErr: {Code: 400, ErrCode: 10086, Description: "consumer in pull mode can not have rate limit set"}, JSConsumerPushMaxWaitingErr: {Code: 400, ErrCode: 10080, Description: "consumer in push mode can not set max waiting"}, JSConsumerReplacementWithDifferentNameErr: {Code: 400, ErrCode: 10106, Description: "consumer replacement durable config not the same"}, + JSConsumerReplicasExceedsStream: {Code: 400, ErrCode: 10126, Description: "consumer config replica count exceeds parent stream"}, JSConsumerSmallHeartbeatErr: {Code: 400, ErrCode: 10083, Description: "consumer idle heartbeat needs to be >= 100ms"}, JSConsumerStoreFailedErrF: {Code: 500, ErrCode: 10104, Description: "error creating store for consumer: {err}"}, JSConsumerWQConsumerNotDeliverAllErr: {Code: 400, ErrCode: 10101, Description: "consumer must be deliver all on workqueue stream"}, @@ -420,8 +451,9 @@ var ( JSMirrorWithSourcesErr: {Code: 400, ErrCode: 10031, Description: "stream mirrors can not also contain other sources"}, JSMirrorWithStartSeqAndTimeErr: {Code: 400, ErrCode: 10032, Description: "stream mirrors can not have both start seq and start time configured"}, JSMirrorWithSubjectFiltersErr: {Code: 400, ErrCode: 10033, Description: "stream mirrors can not contain filtered subjects"}, - JSMirrorWithSubjectsErr: {Code: 400, ErrCode: 10034, Description: "stream mirrors can not also contain subjects"}, + JSMirrorWithSubjectsErr: {Code: 400, ErrCode: 10034, Description: "stream mirrors can not contain subjects"}, JSNoAccountErr: {Code: 503, ErrCode: 10035, Description: "account not found"}, + JSNoLimitsErr: {Code: 400, ErrCode: 10120, Description: "no JetStream default or applicable tiered limit present"}, JSNoMessageFoundErr: {Code: 404, ErrCode: 10037, Description: "no message found"}, JSNotEmptyRequestErr: {Code: 400, ErrCode: 10038, Description: "expected an empty request payload"}, JSNotEnabledErr: {Code: 503, ErrCode: 10076, Description: "JetStream not enabled"}, @@ -447,13 +479,17 @@ var ( JSStreamInvalidExternalDeliverySubjErrF: {Code: 400, ErrCode: 10024, Description: "stream external delivery prefix {prefix} must not contain wildcards"}, JSStreamLimitsErrF: {Code: 500, ErrCode: 10053, Description: "{err}"}, JSStreamMaxBytesRequired: {Code: 400, ErrCode: 10113, Description: "account requires a stream config to have max bytes set"}, + JSStreamMaxStreamBytesExceeded: {Code: 400, ErrCode: 10122, Description: "stream max bytes exceeds account limit max stream bytes"}, JSStreamMessageExceedsMaximumErr: {Code: 400, ErrCode: 10054, Description: "message size exceeds maximum allowed"}, - JSStreamMirrorNotUpdatableErr: {Code: 400, ErrCode: 10055, Description: "Mirror configuration can not be updated"}, + JSStreamMirrorNotUpdatableErr: {Code: 400, ErrCode: 10055, Description: "stream mirror configuration can not be updated"}, JSStreamMismatchErr: {Code: 400, ErrCode: 10056, Description: "stream name in subject does not match request"}, + JSStreamMoveAndScaleErr: {Code: 400, ErrCode: 10123, Description: "can not move and scale a stream in a single update"}, + JSStreamMoveInProgress: {Code: 400, ErrCode: 10124, Description: "stream move already in progress"}, JSStreamMsgDeleteFailedF: {Code: 500, ErrCode: 10057, Description: "{err}"}, JSStreamNameExistErr: {Code: 400, ErrCode: 10058, Description: "stream name already in use"}, JSStreamNotFoundErr: {Code: 404, ErrCode: 10059, Description: "stream not found"}, JSStreamNotMatchErr: {Code: 400, ErrCode: 10060, Description: "expected stream does not match"}, + JSStreamOfflineErr: {Code: 500, ErrCode: 10118, Description: "stream is offline"}, JSStreamPurgeFailedF: {Code: 500, ErrCode: 10110, Description: "{err}"}, JSStreamReplicasNotSupportedErr: {Code: 500, ErrCode: 10074, Description: "replicas > 1 not supported in non-clustered mode"}, JSStreamReplicasNotUpdatableErr: {Code: 400, ErrCode: 10061, Description: "Replicas configuration can not be updated"}, @@ -463,7 +499,7 @@ var ( JSStreamSequenceNotMatchErr: {Code: 503, ErrCode: 10063, Description: "expected stream sequence does not match"}, JSStreamSnapshotErrF: {Code: 500, ErrCode: 10064, Description: "snapshot failed: {err}"}, JSStreamStoreFailedF: {Code: 503, ErrCode: 10077, Description: "{err}"}, - JSStreamSubjectOverlapErr: {Code: 500, ErrCode: 10065, Description: "subjects overlap with an existing stream"}, + JSStreamSubjectOverlapErr: {Code: 400, ErrCode: 10065, Description: "subjects overlap with an existing stream"}, JSStreamTemplateCreateErrF: {Code: 500, ErrCode: 10066, Description: "{err}"}, JSStreamTemplateDeleteErrF: {Code: 500, ErrCode: 10067, Description: "{err}"}, JSStreamTemplateNotFoundErr: {Code: 404, ErrCode: 10068, Description: "template not found"}, @@ -861,6 +897,22 @@ func NewJSConsumerMaxDeliverBackoffError(opts ...ErrorOption) *ApiError { return ApiErrors[JSConsumerMaxDeliverBackoffErr] } +// NewJSConsumerMaxPendingAckExcessError creates a new JSConsumerMaxPendingAckExcessErrF error: "consumer max ack pending exceeds system limit of {limit}" +func NewJSConsumerMaxPendingAckExcessError(limit interface{}, opts ...ErrorOption) *ApiError { + eopts := parseOpts(opts) + if ae, ok := eopts.err.(*ApiError); ok { + return ae + } + + e := ApiErrors[JSConsumerMaxPendingAckExcessErrF] + args := e.toReplacerArgs([]interface{}{"{limit}", limit}) + return &ApiError{ + Code: e.Code, + ErrCode: e.ErrCode, + Description: strings.NewReplacer(args...).Replace(e.Description), + } +} + // NewJSConsumerMaxPendingAckPolicyRequiredError creates a new JSConsumerMaxPendingAckPolicyRequiredErr error: "consumer requires ack policy for max ack pending" func NewJSConsumerMaxPendingAckPolicyRequiredError(opts ...ErrorOption) *ApiError { eopts := parseOpts(opts) @@ -871,6 +923,22 @@ func NewJSConsumerMaxPendingAckPolicyRequiredError(opts ...ErrorOption) *ApiErro return ApiErrors[JSConsumerMaxPendingAckPolicyRequiredErr] } +// NewJSConsumerMaxRequestBatchExceededError creates a new JSConsumerMaxRequestBatchExceededF error: "consumer max request batch exceeds server limit of {limit}" +func NewJSConsumerMaxRequestBatchExceededError(limit interface{}, opts ...ErrorOption) *ApiError { + eopts := parseOpts(opts) + if ae, ok := eopts.err.(*ApiError); ok { + return ae + } + + e := ApiErrors[JSConsumerMaxRequestBatchExceededF] + args := e.toReplacerArgs([]interface{}{"{limit}", limit}) + return &ApiError{ + Code: e.Code, + ErrCode: e.ErrCode, + Description: strings.NewReplacer(args...).Replace(e.Description), + } +} + // NewJSConsumerMaxRequestBatchNegativeError creates a new JSConsumerMaxRequestBatchNegativeErr error: "consumer max request batch needs to be > 0" func NewJSConsumerMaxRequestBatchNegativeError(opts ...ErrorOption) *ApiError { eopts := parseOpts(opts) @@ -937,6 +1005,16 @@ func NewJSConsumerNotFoundError(opts ...ErrorOption) *ApiError { return ApiErrors[JSConsumerNotFoundErr] } +// NewJSConsumerOfflineError creates a new JSConsumerOfflineErr error: "consumer is offline" +func NewJSConsumerOfflineError(opts ...ErrorOption) *ApiError { + eopts := parseOpts(opts) + if ae, ok := eopts.err.(*ApiError); ok { + return ae + } + + return ApiErrors[JSConsumerOfflineErr] +} + // NewJSConsumerOnMappedError creates a new JSConsumerOnMappedErr error: "consumer direct on a mapped consumer" func NewJSConsumerOnMappedError(opts ...ErrorOption) *ApiError { eopts := parseOpts(opts) @@ -997,6 +1075,16 @@ func NewJSConsumerReplacementWithDifferentNameError(opts ...ErrorOption) *ApiErr return ApiErrors[JSConsumerReplacementWithDifferentNameErr] } +// NewJSConsumerReplicasExceedsStreamError creates a new JSConsumerReplicasExceedsStream error: "consumer config replica count exceeds parent stream" +func NewJSConsumerReplicasExceedsStreamError(opts ...ErrorOption) *ApiError { + eopts := parseOpts(opts) + if ae, ok := eopts.err.(*ApiError); ok { + return ae + } + + return ApiErrors[JSConsumerReplicasExceedsStream] +} + // NewJSConsumerSmallHeartbeatError creates a new JSConsumerSmallHeartbeatErr error: "consumer idle heartbeat needs to be >= 100ms" func NewJSConsumerSmallHeartbeatError(opts ...ErrorOption) *ApiError { eopts := parseOpts(opts) @@ -1179,7 +1267,7 @@ func NewJSMirrorWithSubjectFiltersError(opts ...ErrorOption) *ApiError { return ApiErrors[JSMirrorWithSubjectFiltersErr] } -// NewJSMirrorWithSubjectsError creates a new JSMirrorWithSubjectsErr error: "stream mirrors can not also contain subjects" +// NewJSMirrorWithSubjectsError creates a new JSMirrorWithSubjectsErr error: "stream mirrors can not contain subjects" func NewJSMirrorWithSubjectsError(opts ...ErrorOption) *ApiError { eopts := parseOpts(opts) if ae, ok := eopts.err.(*ApiError); ok { @@ -1199,6 +1287,16 @@ func NewJSNoAccountError(opts ...ErrorOption) *ApiError { return ApiErrors[JSNoAccountErr] } +// NewJSNoLimitsError creates a new JSNoLimitsErr error: "no JetStream default or applicable tiered limit present" +func NewJSNoLimitsError(opts ...ErrorOption) *ApiError { + eopts := parseOpts(opts) + if ae, ok := eopts.err.(*ApiError); ok { + return ae + } + + return ApiErrors[JSNoLimitsErr] +} + // NewJSNoMessageFoundError creates a new JSNoMessageFoundErr error: "no message found" func NewJSNoMessageFoundError(opts ...ErrorOption) *ApiError { eopts := parseOpts(opts) @@ -1527,6 +1625,16 @@ func NewJSStreamMaxBytesRequiredError(opts ...ErrorOption) *ApiError { return ApiErrors[JSStreamMaxBytesRequired] } +// NewJSStreamMaxStreamBytesExceededError creates a new JSStreamMaxStreamBytesExceeded error: "stream max bytes exceeds account limit max stream bytes" +func NewJSStreamMaxStreamBytesExceededError(opts ...ErrorOption) *ApiError { + eopts := parseOpts(opts) + if ae, ok := eopts.err.(*ApiError); ok { + return ae + } + + return ApiErrors[JSStreamMaxStreamBytesExceeded] +} + // NewJSStreamMessageExceedsMaximumError creates a new JSStreamMessageExceedsMaximumErr error: "message size exceeds maximum allowed" func NewJSStreamMessageExceedsMaximumError(opts ...ErrorOption) *ApiError { eopts := parseOpts(opts) @@ -1537,7 +1645,7 @@ func NewJSStreamMessageExceedsMaximumError(opts ...ErrorOption) *ApiError { return ApiErrors[JSStreamMessageExceedsMaximumErr] } -// NewJSStreamMirrorNotUpdatableError creates a new JSStreamMirrorNotUpdatableErr error: "Mirror configuration can not be updated" +// NewJSStreamMirrorNotUpdatableError creates a new JSStreamMirrorNotUpdatableErr error: "stream mirror configuration can not be updated" func NewJSStreamMirrorNotUpdatableError(opts ...ErrorOption) *ApiError { eopts := parseOpts(opts) if ae, ok := eopts.err.(*ApiError); ok { @@ -1557,6 +1665,26 @@ func NewJSStreamMismatchError(opts ...ErrorOption) *ApiError { return ApiErrors[JSStreamMismatchErr] } +// NewJSStreamMoveAndScaleError creates a new JSStreamMoveAndScaleErr error: "can not move and scale a stream in a single update" +func NewJSStreamMoveAndScaleError(opts ...ErrorOption) *ApiError { + eopts := parseOpts(opts) + if ae, ok := eopts.err.(*ApiError); ok { + return ae + } + + return ApiErrors[JSStreamMoveAndScaleErr] +} + +// NewJSStreamMoveInProgressError creates a new JSStreamMoveInProgress error: "stream move already in progress" +func NewJSStreamMoveInProgressError(opts ...ErrorOption) *ApiError { + eopts := parseOpts(opts) + if ae, ok := eopts.err.(*ApiError); ok { + return ae + } + + return ApiErrors[JSStreamMoveInProgress] +} + // NewJSStreamMsgDeleteFailedError creates a new JSStreamMsgDeleteFailedF error: "{err}" func NewJSStreamMsgDeleteFailedError(err error, opts ...ErrorOption) *ApiError { eopts := parseOpts(opts) @@ -1603,6 +1731,16 @@ func NewJSStreamNotMatchError(opts ...ErrorOption) *ApiError { return ApiErrors[JSStreamNotMatchErr] } +// NewJSStreamOfflineError creates a new JSStreamOfflineErr error: "stream is offline" +func NewJSStreamOfflineError(opts ...ErrorOption) *ApiError { + eopts := parseOpts(opts) + if ae, ok := eopts.err.(*ApiError); ok { + return ae + } + + return ApiErrors[JSStreamOfflineErr] +} + // NewJSStreamPurgeFailedError creates a new JSStreamPurgeFailedF error: "{err}" func NewJSStreamPurgeFailedError(err error, opts ...ErrorOption) *ApiError { eopts := parseOpts(opts) diff --git a/vendor/github.com/nats-io/nats-server/v2/server/jetstream_events.go b/vendor/github.com/nats-io/nats-server/v2/server/jetstream_events.go index 6497b84d..2e1c7313 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/jetstream_events.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/jetstream_events.go @@ -110,6 +110,21 @@ type JSConsumerDeliveryExceededAdvisory struct { // JSConsumerDeliveryExceededAdvisoryType is the schema type for JSConsumerDeliveryExceededAdvisory const JSConsumerDeliveryExceededAdvisoryType = "io.nats.jetstream.advisory.v1.max_deliver" +// JSConsumerDeliveryNakAdvisory is an advisory informing that a message was +// naked by the consumer +type JSConsumerDeliveryNakAdvisory struct { + TypedEvent + Stream string `json:"stream"` + Consumer string `json:"consumer"` + ConsumerSeq uint64 `json:"consumer_seq"` + StreamSeq uint64 `json:"stream_seq"` + Deliveries uint64 `json:"deliveries"` + Domain string `json:"domain,omitempty"` +} + +// JSConsumerDeliveryNakAdvisoryType is the schema type for JSConsumerDeliveryNakAdvisory +const JSConsumerDeliveryNakAdvisoryType = "io.nats.jetstream.advisory.v1.nak" + // JSConsumerDeliveryTerminatedAdvisory is an advisory informing that a message was // terminated by the consumer, so might be a candidate for DLQ handling type JSConsumerDeliveryTerminatedAdvisory struct { diff --git a/vendor/github.com/nats-io/nats-server/v2/server/leafnode.go b/vendor/github.com/nats-io/nats-server/v2/server/leafnode.go index 40f068e1..78adef19 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/leafnode.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/leafnode.go @@ -56,6 +56,10 @@ const leafNodeLoopDetectionSubjectPrefix = "$LDS." // LEAF connection as opposed to a CLIENT. const leafNodeWSPath = "/leafnode" +// This is the time the server will wait, when receiving a CONNECT, +// before closing the connection if the required minimum version is not met. +const leafNodeWaitBeforeClose = 5 * time.Second + type leaf struct { // We have any auth stuff here for solicited connections. remote *leafNodeCfg @@ -252,6 +256,18 @@ func validateLeafNode(o *Options) error { if o.LeafNode.Port == 0 { return nil } + + // If MinVersion is defined, check that it is valid. + if mv := o.LeafNode.MinVersion; mv != _EMPTY_ { + if err := checkLeafMinVersionConfig(mv); err != nil { + return err + } + } + + // The checks below will be done only when detecting that we are configured + // with gateways. So if an option validation needs to be done regardless, + // it MUST be done before this point! + if o.Gateway.Name == "" && o.Gateway.Port == 0 { return nil } @@ -266,6 +282,17 @@ func validateLeafNode(o *Options) error { return nil } +func checkLeafMinVersionConfig(mv string) error { + if ok, err := versionAtLeastCheckError(mv, 2, 8, 0); !ok || err != nil { + if err != nil { + return fmt.Errorf("invalid leafnode's minimum version: %v", err) + } else { + return fmt.Errorf("the minimum version should be at least 2.8.0") + } + } + return nil +} + // Used to validate user names in LeafNode configuration. // - rejects mix of single and multiple users. // - rejects duplicate user names. @@ -613,6 +640,7 @@ var credsRe = regexp.MustCompile(`\s*(?:(?:[-]{3,}[^\n]*[-]{3,}\n)(.+)(?:\n\s*[- func (c *client) sendLeafConnect(clusterName string, tlsRequired, headers bool) error { // We support basic user/pass and operator based user JWT with signatures. cinfo := leafConnectInfo{ + Version: VERSION, TLS: tlsRequired, ID: c.srv.info.ID, Domain: c.srv.info.Domain, @@ -1316,6 +1344,7 @@ func (s *Server) removeLeafNodeConnection(c *client) { // Connect information for solicited leafnodes. type leafConnectInfo struct { + Version string `json:"version,omitempty"` JWT string `json:"jwt,omitempty"` Sig string `json:"sig,omitempty"` User string `json:"user,omitempty"` @@ -1363,6 +1392,25 @@ func (c *client) processLeafNodeConnect(s *Server, arg []byte, lang string) erro return ErrWrongGateway } + if mv := s.getOpts().LeafNode.MinVersion; mv != _EMPTY_ { + major, minor, update, _ := versionComponents(mv) + if !versionAtLeast(proto.Version, major, minor, update) { + // We are going to send back an INFO because otherwise recent + // versions of the remote server would simply break the connection + // after 2 seconds if not receiving it. Instead, we want the + // other side to just "stall" until we finish waiting for the holding + // period and close the connection below. + s.sendPermsAndAccountInfo(c) + c.sendErrAndErr(fmt.Sprintf("connection rejected since minimum version required is %q", mv)) + select { + case <-c.srv.quitCh: + case <-time.After(leafNodeWaitBeforeClose): + } + c.closeConnection(MinimumVersionRequired) + return ErrMinimumVersionRequired + } + } + // Check if this server supports headers. supportHeaders := c.srv.supportsHeaders() diff --git a/vendor/github.com/nats-io/nats-server/v2/server/log.go b/vendor/github.com/nats-io/nats-server/v2/server/log.go index 2bfe25c2..4b9193c0 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/log.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/log.go @@ -14,9 +14,11 @@ package server import ( + "fmt" "io" "os" "sync/atomic" + "time" srvlog "github.com/nats-io/nats-server/v2/logger" ) @@ -204,6 +206,14 @@ func (s *Server) Warnf(format string, v ...interface{}) { }, format, v...) } +func (s *Server) RateLimitWarnf(format string, v ...interface{}) { + statement := fmt.Sprintf(format, v...) + if _, loaded := s.rateLimitLogging.LoadOrStore(statement, time.Now()); loaded { + return + } + s.Warnf("%s", statement) +} + // Fatalf logs a fatal error func (s *Server) Fatalf(format string, v ...interface{}) { s.executeLogCall(func(logger Logger, format string, v ...interface{}) { diff --git a/vendor/github.com/nats-io/nats-server/v2/server/memstore.go b/vendor/github.com/nats-io/nats-server/v2/server/memstore.go index 9e2b16c1..a6adb84f 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/memstore.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/memstore.go @@ -1,4 +1,4 @@ -// Copyright 2019-2021 The NATS Authors +// Copyright 2019-2022 The NATS Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -26,7 +26,7 @@ type memStore struct { mu sync.RWMutex cfg StreamConfig state StreamState - msgs map[uint64]*storedMsg + msgs map[uint64]*StoreMsg fss map[string]*SimpleState maxp int64 scb StorageUpdateHandler @@ -34,14 +34,6 @@ type memStore struct { consumers int } -type storedMsg struct { - subj string - hdr []byte - msg []byte - seq uint64 - ts int64 // nanoseconds -} - func newMemStore(cfg *StreamConfig) (*memStore, error) { if cfg == nil { return nil, fmt.Errorf("config required") @@ -50,7 +42,7 @@ func newMemStore(cfg *StreamConfig) (*memStore, error) { return nil, fmt.Errorf("memStore requires memory storage type in config") } ms := &memStore{ - msgs: make(map[uint64]*storedMsg), + msgs: make(map[uint64]*StoreMsg), fss: make(map[string]*SimpleState), maxp: cfg.MaxMsgsPer, cfg: *cfg, @@ -147,7 +139,15 @@ func (ms *memStore) storeRawMsg(subj string, hdr, msg []byte, seq uint64, ts int hdr = copyBytes(hdr) } - ms.msgs[seq] = &storedMsg{subj, hdr, msg, seq, ts} + // FIXME(dlc) - Could pool at this level? + sm := &StoreMsg{subj, nil, nil, make([]byte, 0, len(hdr)+len(msg)), seq, ts} + sm.buf = append(sm.buf, hdr...) + sm.buf = append(sm.buf, msg...) + if len(hdr) > 0 { + sm.hdr = sm.buf[:len(hdr)] + } + sm.msg = sm.buf[len(hdr):] + ms.msgs[seq] = sm ms.state.Msgs++ ms.state.Bytes += memStoreMsgSize(subj, hdr, msg) ms.state.LastSeq = seq @@ -428,6 +428,10 @@ func (ms *memStore) expireMsgs() { // PurgeEx will remove messages based on subject filters, sequence and number of messages to keep. // Will return the number of purged messages. func (ms *memStore) PurgeEx(subject string, sequence, keep uint64) (purged uint64, err error) { + if sequence > 1 && keep > 0 { + return 0, ErrPurgeArgMismatch + } + if subject == _EMPTY_ || subject == fwcs { if keep == 0 && (sequence == 0 || sequence == 1) { return ms.Purge() @@ -455,7 +459,7 @@ func (ms *memStore) PurgeEx(subject string, sequence, keep uint64) (purged uint6 ss.Msgs -= keep } last := ss.Last - if sequence > 0 { + if sequence > 1 { last = sequence - 1 } ms.mu.Lock() @@ -485,7 +489,7 @@ func (ms *memStore) Purge() (uint64, error) { ms.state.FirstTime = time.Time{} ms.state.Bytes = 0 ms.state.Msgs = 0 - ms.msgs = make(map[uint64]*storedMsg) + ms.msgs = make(map[uint64]*StoreMsg) ms.fss = make(map[string]*SimpleState) ms.mu.Unlock() @@ -536,7 +540,7 @@ func (ms *memStore) Compact(seq uint64) (uint64, error) { ms.state.FirstSeq = seq ms.state.FirstTime = time.Time{} ms.state.LastSeq = seq - 1 - ms.msgs = make(map[uint64]*storedMsg) + ms.msgs = make(map[uint64]*StoreMsg) } ms.mu.Unlock() @@ -592,7 +596,7 @@ func (ms *memStore) deleteFirstMsg() bool { } // LoadMsg will lookup the message by sequence number and return it if found. -func (ms *memStore) LoadMsg(seq uint64) (string, []byte, []byte, int64, error) { +func (ms *memStore) LoadMsg(seq uint64, smp *StoreMsg) (*StoreMsg, error) { ms.mu.RLock() sm, ok := ms.msgs[seq] last := ms.state.LastSeq @@ -603,15 +607,20 @@ func (ms *memStore) LoadMsg(seq uint64) (string, []byte, []byte, int64, error) { if seq <= last { err = ErrStoreMsgNotFound } - return _EMPTY_, nil, nil, 0, err + return nil, err + } + + if smp == nil { + smp = new(StoreMsg) } - return sm.subj, sm.hdr, sm.msg, sm.ts, nil + sm.copy(smp) + return smp, nil } // LoadLastMsg will return the last message we have that matches a given subject. // The subject can be a wildcard. -func (ms *memStore) LoadLastMsg(subject string) (subj string, seq uint64, hdr, msg []byte, ts int64, err error) { - var sm *storedMsg +func (ms *memStore) LoadLastMsg(subject string, smp *StoreMsg) (*StoreMsg, error) { + var sm *StoreMsg var ok bool ms.mu.RLock() @@ -623,14 +632,19 @@ func (ms *memStore) LoadLastMsg(subject string) (subj string, seq uint64, hdr, m sm, ok = ms.msgs[ss.Last] } if !ok || sm == nil { - return _EMPTY_, 0, nil, nil, 0, ErrStoreMsgNotFound + return nil, ErrStoreMsgNotFound + } + + if smp == nil { + smp = new(StoreMsg) } - return sm.subj, sm.seq, sm.hdr, sm.msg, sm.ts, nil + sm.copy(smp) + return smp, nil } // LoadNextMsg will find the next message matching the filter subject starting at the start sequence. // The filter subject can be a wildcard. -func (ms *memStore) LoadNextMsg(filter string, wc bool, start uint64) (subj string, seq uint64, hdr, msg []byte, ts int64, err error) { +func (ms *memStore) LoadNextMsg(filter string, wc bool, start uint64, smp *StoreMsg) (*StoreMsg, uint64, error) { ms.mu.RLock() defer ms.mu.RUnlock() @@ -640,7 +654,7 @@ func (ms *memStore) LoadNextMsg(filter string, wc bool, start uint64) (subj stri // If past the end no results. if start > ms.state.LastSeq { - return _EMPTY_, ms.state.LastSeq, nil, nil, 0, ErrStoreEOF + return nil, ms.state.LastSeq, ErrStoreEOF } isAll := filter == _EMPTY_ || filter == fwcs @@ -677,10 +691,14 @@ func (ms *memStore) LoadNextMsg(filter string, wc bool, start uint64) (subj stri for nseq := fseq; nseq <= lseq; nseq++ { if sm, ok := ms.msgs[nseq]; ok && (isAll || eq(sm.subj, filter)) { - return sm.subj, nseq, sm.hdr, sm.msg, sm.ts, nil + if smp == nil { + smp = new(StoreMsg) + } + sm.copy(smp) + return smp, nseq, nil } } - return _EMPTY_, ms.state.LastSeq, nil, nil, 0, ErrStoreEOF + return nil, ms.state.LastSeq, ErrStoreEOF } // RemoveMsg will remove the message from this store. @@ -707,7 +725,7 @@ func (ms *memStore) updateFirstSeq(seq uint64) { // Interior delete. return } - var nsm *storedMsg + var nsm *StoreMsg var ok bool for nseq := ms.state.FirstSeq + 1; nseq <= ms.state.LastSeq; nseq++ { if nsm, ok = ms.msgs[nseq]; ok { @@ -739,7 +757,12 @@ func (ms *memStore) removeSeqPerSubject(subj string, seq uint64) { if seq != ss.First { return } - // TODO(dlc) - Might want to optimize this. + // If we know we only have 1 msg left don't need to search for next first. + if ss.Msgs == 1 { + ss.First = ss.Last + return + } + // TODO(dlc) - Might want to optimize this longer term. for tseq := seq + 1; tseq <= ss.Last; tseq++ { if sm := ms.msgs[tseq]; sm != nil && sm.subj == subj { ss.First = tseq @@ -866,55 +889,312 @@ func (ms *memStore) Stop() error { return nil } -func (ms *memStore) incConsumers() { +func (ms *memStore) isClosed() bool { + ms.mu.RLock() + defer ms.mu.RUnlock() + return ms.msgs == nil +} + +type consumerMemStore struct { + mu sync.Mutex + ms StreamStore + cfg ConsumerConfig + state ConsumerState + closed bool +} + +func (ms *memStore) ConsumerStore(name string, cfg *ConsumerConfig) (ConsumerStore, error) { + if ms == nil { + return nil, fmt.Errorf("memstore is nil") + } + if ms.isClosed() { + return nil, ErrStoreClosed + } + if cfg == nil || name == _EMPTY_ { + return nil, fmt.Errorf("bad consumer config") + } + o := &consumerMemStore{ms: ms, cfg: *cfg} + ms.AddConsumer(o) + return o, nil +} + +func (ms *memStore) AddConsumer(o ConsumerStore) error { ms.mu.Lock() ms.consumers++ ms.mu.Unlock() + return nil } -func (ms *memStore) decConsumers() { +func (ms *memStore) RemoveConsumer(o ConsumerStore) error { ms.mu.Lock() if ms.consumers > 0 { ms.consumers-- } ms.mu.Unlock() + return nil } -type consumerMemStore struct { - ms *memStore +func (ms *memStore) Snapshot(_ time.Duration, _, _ bool) (*SnapshotResult, error) { + return nil, fmt.Errorf("no impl") +} + +func (o *consumerMemStore) Update(state *ConsumerState) error { + // Sanity checks. + if state.AckFloor.Consumer > state.Delivered.Consumer { + return fmt.Errorf("bad ack floor for consumer") + } + if state.AckFloor.Stream > state.Delivered.Stream { + return fmt.Errorf("bad ack floor for stream") + } + + // Copy to our state. + var pending map[uint64]*Pending + var redelivered map[uint64]uint64 + if len(state.Pending) > 0 { + pending = make(map[uint64]*Pending, len(state.Pending)) + for seq, p := range state.Pending { + pending[seq] = &Pending{p.Sequence, p.Timestamp} + } + for seq := range pending { + if seq <= state.AckFloor.Stream || seq > state.Delivered.Stream { + return fmt.Errorf("bad pending entry, sequence [%d] out of range", seq) + } + } + } + if len(state.Redelivered) > 0 { + redelivered = make(map[uint64]uint64, len(state.Redelivered)) + for seq, dc := range state.Redelivered { + redelivered[seq] = dc + } + } + + // Replace our state. + o.mu.Lock() + + // Check to see if this is an outdated update. + if state.Delivered.Consumer < o.state.Delivered.Consumer { + o.mu.Unlock() + return fmt.Errorf("old update ignored") + } + + o.state.Delivered = state.Delivered + o.state.AckFloor = state.AckFloor + o.state.Pending = pending + o.state.Redelivered = redelivered + o.mu.Unlock() + + return nil } -func (ms *memStore) ConsumerStore(_ string, _ *ConsumerConfig) (ConsumerStore, error) { - ms.incConsumers() - return &consumerMemStore{ms}, nil +// SetStarting sets our starting stream sequence. +func (o *consumerMemStore) SetStarting(sseq uint64) error { + o.mu.Lock() + o.state.Delivered.Stream = sseq + o.mu.Unlock() + return nil } -func (ms *memStore) Snapshot(_ time.Duration, _, _ bool) (*SnapshotResult, error) { - return nil, fmt.Errorf("no impl") +// HasState returns if this store has a recorded state. +func (o *consumerMemStore) HasState() bool { + return false } -// No-ops. -func (os *consumerMemStore) Update(_ *ConsumerState) error { return nil } -func (os *consumerMemStore) UpdateDelivered(_, _, _ uint64, _ int64) error { return nil } -func (os *consumerMemStore) UpdateAcks(_, _ uint64) error { return nil } -func (os *consumerMemStore) UpdateConfig(_ *ConsumerConfig) error { return nil } +func (o *consumerMemStore) UpdateDelivered(dseq, sseq, dc uint64, ts int64) error { + o.mu.Lock() + defer o.mu.Unlock() + + if dc != 1 && o.cfg.AckPolicy == AckNone { + return ErrNoAckPolicy + } -func (os *consumerMemStore) Stop() error { - os.ms.decConsumers() + if dseq <= o.state.AckFloor.Consumer { + return nil + } + + // See if we expect an ack for this. + if o.cfg.AckPolicy != AckNone { + // Need to create pending records here. + if o.state.Pending == nil { + o.state.Pending = make(map[uint64]*Pending) + } + var p *Pending + // Check for an update to a message already delivered. + if sseq <= o.state.Delivered.Stream { + if p = o.state.Pending[sseq]; p != nil { + p.Sequence, p.Timestamp = dseq, ts + } + } else { + // Add to pending. + o.state.Pending[sseq] = &Pending{dseq, ts} + } + // Update delivered as needed. + if dseq > o.state.Delivered.Consumer { + o.state.Delivered.Consumer = dseq + } + if sseq > o.state.Delivered.Stream { + o.state.Delivered.Stream = sseq + } + + if dc > 1 { + if o.state.Redelivered == nil { + o.state.Redelivered = make(map[uint64]uint64) + } + o.state.Redelivered[sseq] = dc - 1 + } + } else { + // For AckNone just update delivered and ackfloor at the same time. + o.state.Delivered.Consumer = dseq + o.state.Delivered.Stream = sseq + o.state.AckFloor.Consumer = dseq + o.state.AckFloor.Stream = sseq + } + + return nil +} + +func (o *consumerMemStore) UpdateAcks(dseq, sseq uint64) error { + o.mu.Lock() + defer o.mu.Unlock() + + if o.cfg.AckPolicy == AckNone { + return ErrNoAckPolicy + } + if len(o.state.Pending) == 0 || o.state.Pending[sseq] == nil { + return ErrStoreMsgNotFound + } + + // On restarts the old leader may get a replay from the raft logs that are old. + if dseq <= o.state.AckFloor.Consumer { + return nil + } + + // Check for AckAll here. + if o.cfg.AckPolicy == AckAll { + sgap := sseq - o.state.AckFloor.Stream + o.state.AckFloor.Consumer = dseq + o.state.AckFloor.Stream = sseq + for seq := sseq; seq > sseq-sgap; seq-- { + delete(o.state.Pending, seq) + if len(o.state.Redelivered) > 0 { + delete(o.state.Redelivered, seq) + } + } + return nil + } + + // AckExplicit + + // First delete from our pending state. + if p, ok := o.state.Pending[sseq]; ok { + delete(o.state.Pending, sseq) + dseq = p.Sequence // Use the original. + } + // Now remove from redelivered. + if len(o.state.Redelivered) > 0 { + delete(o.state.Redelivered, sseq) + } + + if len(o.state.Pending) == 0 { + o.state.AckFloor.Consumer = o.state.Delivered.Consumer + o.state.AckFloor.Stream = o.state.Delivered.Stream + } else if dseq == o.state.AckFloor.Consumer+1 { + first := o.state.AckFloor.Consumer == 0 + o.state.AckFloor.Consumer = dseq + o.state.AckFloor.Stream = sseq + + if !first && o.state.Delivered.Consumer > dseq { + for ss := sseq + 1; ss < o.state.Delivered.Stream; ss++ { + if p, ok := o.state.Pending[ss]; ok { + if p.Sequence > 0 { + o.state.AckFloor.Consumer = p.Sequence - 1 + o.state.AckFloor.Stream = ss - 1 + } + break + } + } + } + } + + return nil +} + +func (o *consumerMemStore) UpdateConfig(cfg *ConsumerConfig) error { + o.mu.Lock() + defer o.mu.Unlock() + + // This is mostly unchecked here. We are assuming the upper layers have done sanity checking. + o.cfg = *cfg return nil } -func (os *consumerMemStore) Delete() error { - return os.Stop() +func (o *consumerMemStore) Stop() error { + o.mu.Lock() + o.closed = true + ms := o.ms + o.mu.Unlock() + ms.RemoveConsumer(o) + return nil +} + +func (o *consumerMemStore) Delete() error { + return o.Stop() +} + +func (o *consumerMemStore) StreamDelete() error { + return o.Stop() +} + +func (o *consumerMemStore) State() (*ConsumerState, error) { + o.mu.Lock() + defer o.mu.Unlock() + + if o.closed { + return nil, ErrStoreClosed + } + + state := &ConsumerState{} + + state.Delivered = o.state.Delivered + state.AckFloor = o.state.AckFloor + if len(o.state.Pending) > 0 { + state.Pending = o.copyPending() + } + if len(o.state.Redelivered) > 0 { + state.Redelivered = o.copyRedelivered() + } + return state, nil } -func (os *consumerMemStore) StreamDelete() error { - return os.Stop() + +// EncodeState for this consumer store. +func (o *consumerMemStore) EncodedState() ([]byte, error) { + o.mu.Lock() + defer o.mu.Unlock() + + if o.closed { + return nil, ErrStoreClosed + } + + return encodeConsumerState(&o.state), nil } -func (os *consumerMemStore) State() (*ConsumerState, error) { return nil, nil } +func (o *consumerMemStore) copyPending() map[uint64]*Pending { + pending := make(map[uint64]*Pending, len(o.state.Pending)) + for seq, p := range o.state.Pending { + pending[seq] = &Pending{p.Sequence, p.Timestamp} + } + return pending +} + +func (o *consumerMemStore) copyRedelivered() map[uint64]uint64 { + redelivered := make(map[uint64]uint64, len(o.state.Redelivered)) + for seq, dc := range o.state.Redelivered { + redelivered[seq] = dc + } + return redelivered +} // Type returns the type of the underlying store. -func (os *consumerMemStore) Type() StorageType { return MemoryStorage } +func (o *consumerMemStore) Type() StorageType { return MemoryStorage } // Templates type templateMemStore struct{} diff --git a/vendor/github.com/nats-io/nats-server/v2/server/monitor.go b/vendor/github.com/nats-io/nats-server/v2/server/monitor.go index 25b7c21c..9c257911 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/monitor.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/monitor.go @@ -749,10 +749,14 @@ type RouteInfo struct { IsConfigured bool `json:"is_configured"` IP string `json:"ip"` Port int `json:"port"` + Start time.Time `json:"start"` + LastActivity time.Time `json:"last_activity"` + RTT string `json:"rtt,omitempty"` + Uptime string `json:"uptime"` + Idle string `json:"idle"` Import *SubjectPermission `json:"import,omitempty"` Export *SubjectPermission `json:"export,omitempty"` Pending int `json:"pending_size"` - RTT string `json:"rtt,omitempty"` InMsgs int64 `json:"in_msgs"` OutMsgs int64 `json:"out_msgs"` InBytes int64 `json:"in_bytes"` @@ -799,6 +803,10 @@ func (s *Server) Routez(routezOpts *RoutezOptions) (*Routez, error) { Import: r.opts.Import, Export: r.opts.Export, RTT: r.getRTT().String(), + Start: r.start, + LastActivity: r.last, + Uptime: myUptime(rs.Now.Sub(r.start)), + Idle: myUptime(rs.Now.Sub(r.last)), } if len(r.subs) > 0 { @@ -1077,6 +1085,38 @@ func (s *Server) HandleStacksz(w http.ResponseWriter, r *http.Request) { ResponseHandler(w, r, buf[:n]) } +type monitorIPQueue struct { + Pending int `json:"pending"` + InProgress int `json:"in_progress,omitempty"` +} + +func (s *Server) HandleIPQueuesz(w http.ResponseWriter, r *http.Request) { + all, err := decodeBool(w, r, "all") + if err != nil { + return + } + qfilter := r.URL.Query().Get("queues") + + queues := map[string]monitorIPQueue{} + + s.ipQueues.Range(func(k, v interface{}) bool { + name := k.(string) + queue := v.(*ipQueue) + pending := queue.len() + inProgress := int(queue.inProgress()) + if !all && (pending == 0 && inProgress == 0) { + return true + } else if qfilter != _EMPTY_ && !strings.Contains(name, qfilter) { + return true + } + queues[name] = monitorIPQueue{Pending: pending, InProgress: inProgress} + return true + }) + + b, _ := json.MarshalIndent(queues, "", " ") + ResponseHandler(w, r, b) +} + // Varz will output server information on the monitoring port at /varz. type Varz struct { ID string `json:"server_id"` @@ -1108,6 +1148,8 @@ type Varz struct { Cluster ClusterOptsVarz `json:"cluster,omitempty"` Gateway GatewayOptsVarz `json:"gateway,omitempty"` LeafNode LeafNodeOptsVarz `json:"leaf,omitempty"` + MQTT MQTTOptsVarz `json:"mqtt,omitempty"` + Websocket WebsocketOptsVarz `json:"websocket,omitempty"` JetStream JetStreamVarz `json:"jetstream,omitempty"` TLSTimeout float64 `json:"tls_timeout"` WriteDeadline time.Duration `json:"write_deadline"` @@ -1204,6 +1246,37 @@ type RemoteLeafOptsVarz struct { Deny *DenyRules `json:"deny,omitempty"` } +// MQTTOptsVarz contains monitoring MQTT information +type MQTTOptsVarz struct { + Host string `json:"host,omitempty"` + Port int `json:"port,omitempty"` + NoAuthUser string `json:"no_auth_user,omitempty"` + AuthTimeout float64 `json:"auth_timeout,omitempty"` + TLSMap bool `json:"tls_map,omitempty"` + TLSTimeout float64 `json:"tls_timeout,omitempty"` + TLSPinnedCerts []string `json:"tls_pinned_certs,omitempty"` + JsDomain string `json:"js_domain,omitempty"` + AckWait time.Duration `json:"ack_wait,omitempty"` + MaxAckPending uint16 `json:"max_ack_pending,omitempty"` +} + +// WebsocketOptsVarz contains monitoring websocket information +type WebsocketOptsVarz struct { + Host string `json:"host,omitempty"` + Port int `json:"port,omitempty"` + Advertise string `json:"advertise,omitempty"` + NoAuthUser string `json:"no_auth_user,omitempty"` + JWTCookie string `json:"jwt_cookie,omitempty"` + HandshakeTimeout time.Duration `json:"handshake_timeout,omitempty"` + AuthTimeout float64 `json:"auth_timeout,omitempty"` + NoTLS bool `json:"no_tls,omitempty"` + TLSMap bool `json:"tls_map,omitempty"` + TLSPinnedCerts []string `json:"tls_pinned_certs,omitempty"` + SameOrigin bool `json:"same_origin,omitempty"` + AllowedOrigins []string `json:"allowed_origins,omitempty"` + Compression bool `json:"compression,omitempty"` +} + // VarzOptions are the options passed to Varz(). // Currently, there are no options defined. type VarzOptions struct{} @@ -1241,20 +1314,36 @@ func (s *Server) HandleRoot(w http.ResponseWriter, r *http.Request) { s.mu.Lock() s.httpReqStats[RootPath]++ s.mu.Unlock() + + // Calculate source url. If git set go directly to that tag, otherwise just main. + var srcUrl string + if gitCommit == _EMPTY_ { + srcUrl = "https://github.com/nats-io/nats-server" + } else { + srcUrl = fmt.Sprintf("https://github.com/nats-io/nats-server/tree/%s", gitCommit) + } + fmt.Fprintf(w, ` - - - - - - -
+ + + + + + + + v%s + + +
General JetStream Connections @@ -1267,6 +1356,8 @@ func (s *Server) HandleRoot(w http.ResponseWriter, r *http.Request) { Help `, + srcUrl, + VERSION, s.basePath(VarzPath), s.basePath(JszPath), s.basePath(ConnzPath), @@ -1291,7 +1382,10 @@ func (s *Server) updateJszVarz(js *jetStream, v *JetStreamVarz, doConfig bool) { v.Stats = js.usageStats() if mg := js.getMetaGroup(); mg != nil { if ci := s.raftNodeToClusterInfo(mg); ci != nil { - v.Meta = &MetaClusterInfo{Name: ci.Name, Leader: ci.Leader, Replicas: ci.Replicas, Size: mg.ClusterSize()} + v.Meta = &MetaClusterInfo{Name: ci.Name, Leader: ci.Leader, Size: mg.ClusterSize()} + if ci.Leader == s.info.Name { + v.Meta.Replicas = ci.Replicas + } } } } @@ -1326,6 +1420,8 @@ func (s *Server) createVarz(pcpu float64, rss int64) *Varz { c := &opts.Cluster gw := &opts.Gateway ln := &opts.LeafNode + mqtt := &opts.MQTT + ws := &opts.Websocket clustTlsReq := c.TLSConfig != nil gatewayTlsReq := gw.TLSConfig != nil leafTlsReq := ln.TLSConfig != nil @@ -1375,6 +1471,31 @@ func (s *Server) createVarz(pcpu float64, rss int64) *Varz { TLSVerify: leafTlsVerify, Remotes: []RemoteLeafOptsVarz{}, }, + MQTT: MQTTOptsVarz{ + Host: mqtt.Host, + Port: mqtt.Port, + NoAuthUser: mqtt.NoAuthUser, + AuthTimeout: mqtt.AuthTimeout, + TLSMap: mqtt.TLSMap, + TLSTimeout: mqtt.TLSTimeout, + JsDomain: mqtt.JsDomain, + AckWait: mqtt.AckWait, + MaxAckPending: mqtt.MaxAckPending, + }, + Websocket: WebsocketOptsVarz{ + Host: ws.Host, + Port: ws.Port, + Advertise: ws.Advertise, + NoAuthUser: ws.NoAuthUser, + JWTCookie: ws.JWTCookie, + AuthTimeout: ws.AuthTimeout, + NoTLS: ws.NoTLS, + TLSMap: ws.TLSMap, + SameOrigin: ws.SameOrigin, + AllowedOrigins: copyStrings(ws.AllowedOrigins), + Compression: ws.Compression, + HandshakeTimeout: ws.HandshakeTimeout, + }, Start: s.start, MaxSubs: opts.MaxSubs, Cores: numCores, @@ -1462,6 +1583,19 @@ func (s *Server) updateVarzConfigReloadableFields(v *Varz) { if s.sys != nil && s.sys.account != nil { v.SystemAccount = s.sys.account.GetName() } + v.MQTT.TLSPinnedCerts = getPinnedCertsAsSlice(opts.MQTT.TLSPinnedCerts) + v.Websocket.TLSPinnedCerts = getPinnedCertsAsSlice(opts.Websocket.TLSPinnedCerts) +} + +func getPinnedCertsAsSlice(certs PinnedCertSet) []string { + if len(certs) == 0 { + return nil + } + res := make([]string, 0, len(certs)) + for cn := range certs { + res = append(res, cn) + } + return res } // Updates the runtime Varz fields, that is, fields that change during @@ -2102,6 +2236,10 @@ func (reason ClosedState) String() string { return "Duplicate Remote LeafNode Connection" case DuplicateClientID: return "Duplicate Client ID" + case DuplicateServerName: + return "Duplicate Server Name" + case MinimumVersionRequired: + return "Minimum Version Required" } return "Unknown State" @@ -2255,7 +2393,7 @@ func (s *Server) accountInfo(accName string) (*AccountInfo, error) { a.mu.RLock() defer a.mu.RUnlock() var vrIssues []ExtVrIssues - claim, _ := jwt.DecodeAccountClaims(a.claimJWT) //ignore error + claim, _ := jwt.DecodeAccountClaims(a.claimJWT) // ignore error if claim != nil { vr := jwt.ValidationResults{} claim.Validate(&vr) @@ -2446,12 +2584,14 @@ func (s *Server) accountDetail(jsa *jsAccount, optStreams, optConsumers, optCfg if acc.nameTag != "" { name = acc.nameTag } + jsa.usageMu.RLock() + totalMem, totalStore := jsa.storageTotals() detail := AccountDetail{ Name: name, Id: id, JetStreamStats: JetStreamStats{ - Memory: uint64(jsa.memTotal), - Store: uint64(jsa.storeTotal), + Memory: totalMem, + Store: totalStore, API: JetStreamAPIStats{ Total: jsa.apiTotal, Errors: jsa.apiErrors, @@ -2459,6 +2599,7 @@ func (s *Server) accountDetail(jsa *jsAccount, optStreams, optConsumers, optCfg }, Streams: make([]StreamDetail, 0, len(jsa.streams)), } + jsa.usageMu.RUnlock() var streams []*stream if optStreams { for _, stream := range jsa.streams { @@ -2554,47 +2695,48 @@ func (s *Server) Jsz(opts *JSzOptions) (*JSInfo, error) { opts.Accounts = true } - // Check if we want a response from the leader only. - if opts.LeaderOnly { - js, cc := s.getJetStreamCluster() - if js == nil { - // Ignore - return nil, fmt.Errorf("%w: no cluster", errSkipZreq) - } - // So if we have JS but no clustering, we are the leader so allow. - if cc != nil { - js.mu.RLock() - isLeader := cc.isLeader() - js.mu.RUnlock() - if !isLeader { - return nil, fmt.Errorf("%w: not leader", errSkipZreq) - } - } - } - jsi := &JSInfo{ ID: s.ID(), Now: time.Now().UTC(), } - if !s.JetStreamEnabled() { + + js := s.getJetStream() + if js == nil || !js.isEnabled() { + if opts.LeaderOnly { + return nil, fmt.Errorf("%w: not leader", errSkipZreq) + } + jsi.Disabled = true return jsi, nil } - accounts := []*jsAccount{} - s.js.mu.RLock() - jsi.Config = s.js.config - for _, info := range s.js.accounts { + js.mu.RLock() + isLeader := js.cluster == nil || js.cluster.isLeader() + js.mu.RUnlock() + + if opts.LeaderOnly && !isLeader { + return nil, fmt.Errorf("%w: not leader", errSkipZreq) + } + + var accounts []*jsAccount + + js.mu.RLock() + jsi.Config = js.config + for _, info := range js.accounts { accounts = append(accounts, info) } - s.js.mu.RUnlock() + js.mu.RUnlock() - if mg := s.js.getMetaGroup(); mg != nil { + if mg := js.getMetaGroup(); mg != nil { if ci := s.raftNodeToClusterInfo(mg); ci != nil { - jsi.Meta = &MetaClusterInfo{Name: ci.Name, Leader: ci.Leader, Replicas: ci.Replicas, Size: mg.ClusterSize()} + jsi.Meta = &MetaClusterInfo{Name: ci.Name, Leader: ci.Leader, Size: mg.ClusterSize()} + if isLeader { + jsi.Meta.Replicas = ci.Replicas + } } } - jsi.JetStreamStats = *s.js.usageStats() + + jsi.JetStreamStats = *js.usageStats() filterIdx := -1 for i, jsa := range accounts { @@ -2602,14 +2744,18 @@ func (s *Server) Jsz(opts *JSzOptions) (*JSInfo, error) { filterIdx = i } jsa.mu.RLock() - jsi.Streams += len(jsa.streams) + streams := make([]*stream, 0, len(jsa.streams)) for _, stream := range jsa.streams { + streams = append(streams, stream) + } + jsa.mu.RUnlock() + jsi.Streams += len(streams) + for _, stream := range streams { streamState := stream.state() jsi.Messages += streamState.Msgs jsi.Bytes += streamState.Bytes jsi.Consumers += streamState.Consumers } - jsa.mu.RUnlock() } // filter logic @@ -2645,7 +2791,7 @@ func (s *Server) Jsz(opts *JSzOptions) (*JSInfo, error) { return jsi, nil } -// HandleJSz process HTTP requests for jetstream information. +// HandleJsz process HTTP requests for jetstream information. func (s *Server) HandleJsz(w http.ResponseWriter, r *http.Request) { s.mu.Lock() s.httpReqStats[JszPath]++ @@ -2713,80 +2859,88 @@ func (s *Server) HandleHealthz(w http.ResponseWriter, r *http.Request) { s.httpReqStats[HealthzPath]++ s.mu.Unlock() + hs := s.healthz() + if hs.Error != _EMPTY_ { + s.Warnf("Healthcheck failed: %q", hs.Error) + w.WriteHeader(http.StatusServiceUnavailable) + } + b, err := json.Marshal(hs) + if err != nil { + s.Errorf("Error marshaling response to /healthz request: %v", err) + } + + ResponseHandler(w, r, b) +} + +// Generate health status. +func (s *Server) healthz() *HealthStatus { var health = &HealthStatus{Status: "ok"} if err := s.readyForConnections(time.Millisecond); err != nil { health.Status = "error" health.Error = err.Error() - w.WriteHeader(http.StatusServiceUnavailable) - } else if js := s.getJetStream(); js != nil { - // Check JetStream status here. - js.mu.RLock() - clustered, cc := !js.standAlone, js.cluster - js.mu.RUnlock() - if clustered { - // We do more checking for clustered mode to allow for proper rolling updates. - // We will make sure that we have seen the meta leader and that we are current with all assets. - node := js.getMetaGroup() - if node.GroupLeader() == _EMPTY_ { - health.Status = "unavailable" - health.Error = "JetStream has not established contact with a meta leader" - w.WriteHeader(http.StatusServiceUnavailable) - } else if !node.Current() { - health.Status = "unavailable" - health.Error = "JetStream is not current with the meta leader" - w.WriteHeader(http.StatusServiceUnavailable) - } else { - // If we are here we are current and have seen our meta leader. - // Now check assets. - var _a [512]*jsAccount - accounts := _a[:0] - js.mu.RLock() - // Collect accounts. - for _, jsa := range js.accounts { - accounts = append(accounts, jsa) + return health + } + + // Check JetStream + js := s.getJetStream() + if js == nil { + return health + } + + // Clustered JetStream + js.mu.RLock() + defer js.mu.RUnlock() + + cc := js.cluster + + // Currently single server mode this is a no-op. + if cc == nil || cc.meta == nil { + return health + } + + // If we are here we want to check for any assets assigned to us. + meta := cc.meta + ourID := meta.ID() + + // If no meta leader. + if meta.GroupLeader() == _EMPTY_ { + health.Status = "unavailable" + health.Error = "JetStream has not established contact with a meta leader" + return health + } + // If we are not current with the meta leader. + if !meta.Current() { + health.Status = "unavailable" + health.Error = "JetStream is not current with the meta leader" + return health + } + + // Range across all accounts, the streams assigned to them, and the consumers. + // If they are assigned to this server check their status. + for acc, asa := range cc.streams { + for stream, sa := range asa { + if sa.Group.isMember(ourID) { + // Make sure we can look up + if !cc.isStreamCurrent(acc, stream) { + health.Status = "unavailable" + health.Error = fmt.Sprintf("JetStream stream '%s > %s' is not current", acc, stream) + return health } - js.mu.RUnlock() - - var streams []*stream - Err: - // Walk our accounts and assets. - for _, jsa := range accounts { - if len(streams) > 0 { - streams = streams[:0] - } - jsa.mu.RLock() - accName := jsa.account.Name - for _, stream := range jsa.streams { - streams = append(streams, stream) - } - jsa.mu.RUnlock() - // Now walk the streams themselves. - js.mu.RLock() - for _, stream := range streams { - // Skip non-replicated. - if stream.cfg.Replicas <= 1 { - continue - } - sname := stream.name() - if !cc.isStreamCurrent(accName, sname) { + // Now check consumers. + for consumer, ca := range sa.consumers { + if ca.Group.isMember(ourID) { + if !cc.isConsumerCurrent(acc, stream, consumer) { health.Status = "unavailable" - health.Error = fmt.Sprintf("JetStream stream %q for account %q is not current", sname, accName) - w.WriteHeader(http.StatusServiceUnavailable) - js.mu.RUnlock() - break Err + health.Error = fmt.Sprintf("JetStream consumer '%s > %s > %s' is not current", acc, stream, consumer) + return health } } - js.mu.RUnlock() } } } } - b, err := json.Marshal(health) - if err != nil { - s.Errorf("Error marshaling response to /healthz request: %v", err) - } - - ResponseHandler(w, r, b) + // Success. + return health } diff --git a/vendor/github.com/nats-io/nats-server/v2/server/mqtt.go b/vendor/github.com/nats-io/nats-server/v2/server/mqtt.go index ce1cec9a..30f23b62 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/mqtt.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/mqtt.go @@ -147,6 +147,7 @@ const ( mqttJSAStreamLookup = "SL" mqttJSAStreamDel = "SD" mqttJSAConsumerCreate = "CC" + mqttJSAConsumerLookup = "CL" mqttJSAConsumerDel = "CD" mqttJSAMsgStore = "MS" mqttJSAMsgLoad = "ML" @@ -226,7 +227,6 @@ type mqttAccountSessionManager struct { sl *Sublist // sublist allowing to find retained messages for given subscription retmsgs map[string]*mqttRetainedMsg // retained messages jsa mqttJSA - replicas int rrmLastSeq uint64 // Restore retained messages expected last sequence rrmDoneCh chan struct{} // To notify the caller that all retained messages have been loaded sp *ipQueue // of uint64. Used for cluster-wide processing of session records being persisted @@ -577,6 +577,10 @@ func validateMQTTOptions(o *Options) error { if err := validatePinnedCerts(mo.TLSPinnedCerts); err != nil { return fmt.Errorf("mqtt: %v", err) } + if mo.ConsumerReplicas > 0 && mo.StreamReplicas > 0 && mo.ConsumerReplicas > mo.StreamReplicas { + return fmt.Errorf("mqtt: consumer_replicas (%v) cannot be higher than stream_replicas (%v)", + mo.ConsumerReplicas, mo.StreamReplicas) + } return nil } @@ -833,7 +837,9 @@ func (s *Server) mqttHandleClosedClient(c *client) { // This needs to be done outside of any lock. if doClean { - sess.clear() + if err := sess.clear(); err != nil { + c.Errorf(err.Error()) + } } // Now handle the "will". This function will be a no-op if there is no "will" to send. @@ -916,33 +922,35 @@ func (s *Server) mqttCreateAccountSessionManager(acc *Account, quitCh chan struc accName := acc.GetName() + opts := s.getOpts() c := s.createInternalAccountClient() c.acc = acc id := string(getHash(s.Name())) - replicas := s.mqttDetermineReplicas() - qname := fmt.Sprintf("MQTT account %q send", accName) + replicas := opts.MQTT.StreamReplicas + if replicas <= 0 { + replicas = s.mqttDetermineReplicas() + } + qname := fmt.Sprintf("[ACC:%s] MQTT ", accName) as := &mqttAccountSessionManager{ sessions: make(map[string]*mqttSession), sessByHash: make(map[string]*mqttSession), sessLocked: make(map[string]struct{}), flappers: make(map[string]int64), - replicas: replicas, jsa: mqttJSA{ id: id, c: c, rplyr: mqttJSARepliesPrefix + id + ".", - sendq: newIPQueue(ipQueue_Logger(qname, s.ipqLog)), // of *mqttJSPubMsg + sendq: s.newIPQueue(qname + "send"), // of *mqttJSPubMsg nuid: nuid.New(), quitCh: quitCh, }, - sp: newIPQueue(), // of uint64 + sp: s.newIPQueue(qname + "sp"), // of uint64 } // TODO record domain name in as here // The domain to communicate with may be required for JS calls. // Search from specific (per account setting) to generic (mqtt setting) - opts := s.getOpts() if opts.JsAccDefaultDomain != nil { if d, ok := opts.JsAccDefaultDomain[accName]; ok { if d != _EMPTY_ { @@ -1037,49 +1045,88 @@ func (s *Server) mqttCreateAccountSessionManager(acc *Account, quitCh chan struc as.sessPersistProcessing(closeCh) }) - // Create the stream for the sessions. - cfg := &StreamConfig{ - Name: mqttSessStreamName, - Subjects: []string{mqttSessStreamSubjectPrefix + as.domainTk + ">"}, - Storage: FileStorage, - Retention: LimitsPolicy, - Replicas: as.replicas, - MaxMsgsPer: 1, - } - if _, created, err := jsa.createStream(cfg); err == nil && created { - as.transferUniqueSessStreamsToMuxed(s) - } else if isErrorOtherThan(err, JSStreamNameExistErr) { - return nil, fmt.Errorf("create sessions stream for account %q: %v", acc.GetName(), err) - } - - // Create the stream for the messages. - cfg = &StreamConfig{ - Name: mqttStreamName, - Subjects: []string{mqttStreamSubjectPrefix + ">"}, - Storage: FileStorage, - Retention: InterestPolicy, - Replicas: as.replicas, - } - if _, _, err := jsa.createStream(cfg); isErrorOtherThan(err, JSStreamNameExistErr) { - return nil, fmt.Errorf("create messages stream for account %q: %v", acc.GetName(), err) - } - - // Create the stream for retained messages. - cfg = &StreamConfig{ - Name: mqttRetainedMsgsStreamName, - Subjects: []string{mqttRetainedMsgsStreamSubject}, - Storage: FileStorage, - Retention: LimitsPolicy, - Replicas: as.replicas, - } - si, _, err := jsa.createStream(cfg) - if isErrorOtherThan(err, JSStreamNameExistErr) { - return nil, fmt.Errorf("create retained messages stream for account %q: %v", acc.GetName(), err) + lookupStream := func(stream, txt string) (*StreamInfo, error) { + si, err := jsa.lookupStream(stream) + if err != nil { + if IsNatsErr(err, JSStreamNotFoundErr) { + return nil, nil + } + return nil, fmt.Errorf("lookup %s stream for account %q: %v", txt, accName, err) + } + if opts.MQTT.StreamReplicas == 0 { + return si, nil + } + sr := 1 + if si.Cluster != nil { + sr += len(si.Cluster.Replicas) + } + if replicas != sr { + s.Warnf("MQTT %s stream replicas mismatch: current is %v but configuration is %v for '%s > %s'", + txt, sr, replicas, accName, stream) + } + return si, nil + } + + if si, err := lookupStream(mqttSessStreamName, "sessions"); err != nil { + return nil, err + } else if si == nil { + // Create the stream for the sessions. + cfg := &StreamConfig{ + Name: mqttSessStreamName, + Subjects: []string{mqttSessStreamSubjectPrefix + as.domainTk + ">"}, + Storage: FileStorage, + Retention: LimitsPolicy, + Replicas: replicas, + MaxMsgsPer: 1, + } + if _, created, err := jsa.createStream(cfg); err == nil && created { + as.transferUniqueSessStreamsToMuxed(s) + } else if isErrorOtherThan(err, JSStreamNameExistErr) { + return nil, fmt.Errorf("create sessions stream for account %q: %v", accName, err) + } } + + if si, err := lookupStream(mqttStreamName, "messages"); err != nil { + return nil, err + } else if si == nil { + // Create the stream for the messages. + cfg := &StreamConfig{ + Name: mqttStreamName, + Subjects: []string{mqttStreamSubjectPrefix + ">"}, + Storage: FileStorage, + Retention: InterestPolicy, + Replicas: replicas, + } + if _, _, err := jsa.createStream(cfg); isErrorOtherThan(err, JSStreamNameExistErr) { + return nil, fmt.Errorf("create messages stream for account %q: %v", accName, err) + } + } + + // This is the only case where we need "si" after lookup/create + si, err := lookupStream(mqttRetainedMsgsStreamName, "retained messages") if err != nil { - si, err = jsa.lookupStream(mqttRetainedMsgsStreamName) + return nil, err + } else if si == nil { + // Create the stream for retained messages. + cfg := &StreamConfig{ + Name: mqttRetainedMsgsStreamName, + Subjects: []string{mqttRetainedMsgsStreamSubject}, + Storage: FileStorage, + Retention: LimitsPolicy, + Replicas: replicas, + } + // We will need "si" outside of this block. + si, _, err = jsa.createStream(cfg) if err != nil { - return nil, fmt.Errorf("lookup retained messages stream for account %q: %v", acc.GetName(), err) + if isErrorOtherThan(err, JSStreamNameExistErr) { + return nil, fmt.Errorf("create retained messages stream for account %q: %v", accName, err) + } + // Suppose we had a race and the stream was actually created by another + // node, we really need "si" after that, so lookup the stream again here. + si, err = lookupStream(mqttRetainedMsgsStreamName, "retained messages") + if err != nil { + return nil, err + } } } @@ -1097,15 +1144,14 @@ func (s *Server) mqttCreateAccountSessionManager(acc *Account, quitCh chan struc // Using ephemeral consumer is too risky because if this server were to be // disconnected from the rest for few seconds, then the leader would remove - // the consumer, so even after a reconnect, we would not longer receive + // the consumer, so even after a reconnect, we would no longer receive // retained messages. Delete any existing durable that we have for that // and recreate here. // The name for the durable is $MQTT_rmsgs_ (which is jsa.id) rmDurName := mqttRetainedMsgsStreamName + "_" + jsa.id - resp, err := jsa.deleteConsumer(mqttRetainedMsgsStreamName, rmDurName) // If error other than "not found" then fail, otherwise proceed with creating // the durable consumer. - if err != nil && (resp == nil || resp.Error.Code != 404) { + if _, err := jsa.deleteConsumer(mqttRetainedMsgsStreamName, rmDurName); isErrorOtherThan(err, JSConsumerNotFoundErr) { return nil, err } ccfg := &CreateConsumerRequest{ @@ -1119,7 +1165,7 @@ func (s *Server) mqttCreateAccountSessionManager(acc *Account, quitCh chan struc }, } if _, err := jsa.createConsumer(ccfg); err != nil { - return nil, fmt.Errorf("create retained messages consumer for account %q: %v", acc.GetName(), err) + return nil, fmt.Errorf("create retained messages consumer for account %q: %v", accName, err) } if lastSeq > 0 { @@ -1500,8 +1546,19 @@ func (as *mqttAccountSessionManager) processSessionPersist(_ *subscription, pc * if err := par.Error; err != nil { return } - // We would need to lookup the message that that is a request/reply - // that we can do in place here. So move that to a long-running routine + as.mu.RLock() + // Note that as.domainTk includes a terminal '.', so strip to compare to PubAck.Domain. + dl := len(as.domainTk) + if dl > 0 { + dl-- + } + ignore := par.Domain != as.domainTk[:dl] + as.mu.RUnlock() + if ignore { + return + } + // We would need to lookup the message and that would be a request/reply, + // which we can't do in place here. So move that to a long-running routine // that will process the session persist record. as.sp.push(par.Sequence) } @@ -2240,7 +2297,10 @@ func (sess *mqttSession) clear() error { } sess.subs, sess.pending, sess.cpending, sess.seq, sess.tmaxack = nil, nil, nil, 0, 0 for _, dur := range durs { - sess.jsa.sendq.push(&mqttJSPubMsg{subj: sess.jsa.prefixDomain(fmt.Sprintf(JSApiConsumerDeleteT, mqttStreamName, dur))}) + if _, err := sess.jsa.deleteConsumer(mqttStreamName, dur); isErrorOtherThan(err, JSConsumerNotFoundErr) { + sess.mu.Unlock() + return fmt.Errorf("unable to delete consumer %q for session %q: %v", dur, sess.id, err) + } } sess.mu.Unlock() if seq > 0 { @@ -3557,6 +3617,10 @@ func (sess *mqttSession) processJSConsumer(c *client, subject, sid string, FilterSubject: mqttStreamSubjectPrefix + subject, AckWait: ackWait, MaxAckPending: maxAckPending, + MemoryStorage: opts.MQTT.ConsumerMemoryStorage, + } + if r := opts.MQTT.ConsumerReplicas; r > 0 { + cc.Replicas = r } if err := sess.createConsumer(cc); err != nil { c.Errorf("Unable to add JetStream consumer for subscription on %q: err=%v", subject, err) diff --git a/vendor/github.com/nats-io/nats-server/v2/server/opts.go b/vendor/github.com/nats-io/nats-server/v2/server/opts.go index 8110f1e3..3a32d5ea 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/opts.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/opts.go @@ -143,6 +143,13 @@ type LeafNodeOpts struct { // For solicited connections to other clusters/superclusters. Remotes []*RemoteLeafOpts `json:"remotes,omitempty"` + // This is the minimum version that is accepted for remote connections. + // Note that since the server version in the CONNECT protocol was added + // only starting at v2.8.0, any version below that will be rejected + // (since empty version string in CONNECT would fail the "version at + // least" test). + MinVersion string + // Not exported, for tests. resolver netResolver dialTimeout time.Duration @@ -177,55 +184,64 @@ type RemoteLeafOpts struct { tlsConfigOpts *TLSConfigOpts } +type JSLimitOpts struct { + MaxRequestBatch int + MaxAckPending int + MaxHAAssets int + Duplicates time.Duration +} + // Options block for nats-server. // NOTE: This structure is no longer used for monitoring endpoints // and json tags are deprecated and may be removed in the future. type Options struct { - ConfigFile string `json:"-"` - ServerName string `json:"server_name"` - Host string `json:"addr"` - Port int `json:"port"` - ClientAdvertise string `json:"-"` - Trace bool `json:"-"` - Debug bool `json:"-"` - TraceVerbose bool `json:"-"` - NoLog bool `json:"-"` - NoSigs bool `json:"-"` - NoSublistCache bool `json:"-"` - NoHeaderSupport bool `json:"-"` - DisableShortFirstPing bool `json:"-"` - Logtime bool `json:"-"` - MaxConn int `json:"max_connections"` - MaxSubs int `json:"max_subscriptions,omitempty"` - MaxSubTokens uint8 `json:"-"` - Nkeys []*NkeyUser `json:"-"` - Users []*User `json:"-"` - Accounts []*Account `json:"-"` - NoAuthUser string `json:"-"` - SystemAccount string `json:"-"` - NoSystemAccount bool `json:"-"` - Username string `json:"-"` - Password string `json:"-"` - Authorization string `json:"-"` - PingInterval time.Duration `json:"ping_interval"` - MaxPingsOut int `json:"ping_max"` - HTTPHost string `json:"http_host"` - HTTPPort int `json:"http_port"` - HTTPBasePath string `json:"http_base_path"` - HTTPSPort int `json:"https_port"` - AuthTimeout float64 `json:"auth_timeout"` - MaxControlLine int32 `json:"max_control_line"` - MaxPayload int32 `json:"max_payload"` - MaxPending int64 `json:"max_pending"` - Cluster ClusterOpts `json:"cluster,omitempty"` - Gateway GatewayOpts `json:"gateway,omitempty"` - LeafNode LeafNodeOpts `json:"leaf,omitempty"` - JetStream bool `json:"jetstream"` - JetStreamMaxMemory int64 `json:"-"` - JetStreamMaxStore int64 `json:"-"` - JetStreamDomain string `json:"-"` - JetStreamExtHint string `json:"-"` - JetStreamKey string `json:"-"` + ConfigFile string `json:"-"` + ServerName string `json:"server_name"` + Host string `json:"addr"` + Port int `json:"port"` + ClientAdvertise string `json:"-"` + Trace bool `json:"-"` + Debug bool `json:"-"` + TraceVerbose bool `json:"-"` + NoLog bool `json:"-"` + NoSigs bool `json:"-"` + NoSublistCache bool `json:"-"` + NoHeaderSupport bool `json:"-"` + DisableShortFirstPing bool `json:"-"` + Logtime bool `json:"-"` + MaxConn int `json:"max_connections"` + MaxSubs int `json:"max_subscriptions,omitempty"` + MaxSubTokens uint8 `json:"-"` + Nkeys []*NkeyUser `json:"-"` + Users []*User `json:"-"` + Accounts []*Account `json:"-"` + NoAuthUser string `json:"-"` + SystemAccount string `json:"-"` + NoSystemAccount bool `json:"-"` + Username string `json:"-"` + Password string `json:"-"` + Authorization string `json:"-"` + PingInterval time.Duration `json:"ping_interval"` + MaxPingsOut int `json:"ping_max"` + HTTPHost string `json:"http_host"` + HTTPPort int `json:"http_port"` + HTTPBasePath string `json:"http_base_path"` + HTTPSPort int `json:"https_port"` + AuthTimeout float64 `json:"auth_timeout"` + MaxControlLine int32 `json:"max_control_line"` + MaxPayload int32 `json:"max_payload"` + MaxPending int64 `json:"max_pending"` + Cluster ClusterOpts `json:"cluster,omitempty"` + Gateway GatewayOpts `json:"gateway,omitempty"` + LeafNode LeafNodeOpts `json:"leaf,omitempty"` + JetStream bool `json:"jetstream"` + JetStreamMaxMemory int64 `json:"-"` + JetStreamMaxStore int64 `json:"-"` + JetStreamDomain string `json:"-"` + JetStreamExtHint string `json:"-"` + JetStreamKey string `json:"-"` + JetStreamUniqueTag string + JetStreamLimits JSLimitOpts StoreDir string `json:"-"` JsAccDefaultDomain map[string]string `json:"-"` // account to domain name mapping Websocket WebsocketOpts `json:"-"` @@ -395,6 +411,26 @@ type MQTTOpts struct { // JetStream domain mqtt is supposed to pick up JsDomain string + // Number of replicas for MQTT streams. + // Negative or 0 value means that the server(s) will pick a replica + // number based on the known size of the cluster (but capped at 3). + // Note that if an account was already connected, the stream's replica + // count is not modified. Use the NATS CLI to update the count if desired. + StreamReplicas int + + // Number of replicas for MQTT consumers. + // Negative or 0 value means that there is no override and the consumer + // will have the same replica factor that the stream it belongs to. + // If a value is specified, it will require to be lower than the stream + // replicas count (lower than StreamReplicas if specified, but also lower + // than the automatic value determined by cluster size). + // Note that existing consumers are not modified. + ConsumerReplicas int + + // Indicate if the consumers should be created with memory storage. + // Note that existing consumers are not modified. + ConsumerMemoryStorage bool + // Timeout for the authentication process. AuthTimeout float64 @@ -755,30 +791,55 @@ func (o *Options) processConfigFileLine(k string, v interface{}, errors *[]error o.Username = auth.user o.Password = auth.pass o.Authorization = auth.token - if (auth.user != "" || auth.pass != "") && auth.token != "" { + o.AuthTimeout = auth.timeout + if (auth.user != _EMPTY_ || auth.pass != _EMPTY_) && auth.token != _EMPTY_ { err := &configErr{tk, "Cannot have a user/pass and token"} *errors = append(*errors, err) return } - o.AuthTimeout = auth.timeout - // Check for multiple users defined - if auth.users != nil { - if auth.user != "" { + // In case parseAccounts() was done first, we need to check for duplicates. + unames := setupUsersAndNKeysDuplicateCheckMap(o) + // Check for multiple users defined. + // Note: auth.users will be != nil as long as `users: []` is present + // in the authorization block, even if empty, and will also account for + // nkey users. We also check for users/nkeys that may have been already + // added in parseAccounts() (which means they will be in unames) + if auth.users != nil || len(unames) > 0 { + if auth.user != _EMPTY_ { err := &configErr{tk, "Can not have a single user/pass and a users array"} *errors = append(*errors, err) return } - if auth.token != "" { + if auth.token != _EMPTY_ { err := &configErr{tk, "Can not have a token and a users array"} *errors = append(*errors, err) return } - // Users may have been added from Accounts parsing, so do an append here - o.Users = append(o.Users, auth.users...) + // Now check that if we have users, there is no duplicate, including + // users that may have been configured in parseAccounts(). + if len(auth.users) > 0 { + for _, u := range auth.users { + if _, ok := unames[u.Username]; ok { + err := &configErr{tk, fmt.Sprintf("Duplicate user %q detected", u.Username)} + *errors = append(*errors, err) + return + } + unames[u.Username] = struct{}{} + } + // Users may have been added from Accounts parsing, so do an append here + o.Users = append(o.Users, auth.users...) + } } - // Check for nkeys - if auth.nkeys != nil { + if len(auth.nkeys) > 0 { + for _, u := range auth.nkeys { + if _, ok := unames[u.Nkey]; ok { + err := &configErr{tk, fmt.Sprintf("Duplicate nkey %q detected", u.Nkey)} + *errors = append(*errors, err) + return + } + unames[u.Nkey] = struct{}{} + } // NKeys may have been added from Accounts parsing, so do an append here o.Nkeys = append(o.Nkeys, auth.nkeys...) } @@ -1303,6 +1364,17 @@ func (o *Options) processConfigFileLine(k string, v interface{}, errors *[]error } } +func setupUsersAndNKeysDuplicateCheckMap(o *Options) map[string]struct{} { + unames := make(map[string]struct{}, len(o.Users)+len(o.Nkeys)) + for _, u := range o.Users { + unames[u.Username] = struct{}{} + } + for _, u := range o.Nkeys { + unames[u.Nkey] = struct{}{} + } + return unames +} + func parseDuration(field string, tk token, v interface{}, errors *[]error, warnings *[]error) time.Duration { if wd, ok := v.(string); ok { if dur, err := time.ParseDuration(wd); err != nil { @@ -1432,7 +1504,7 @@ func parseCluster(v interface{}, opts *Options, errors *[]error, warnings *[]err } case "routes": ra := mv.([]interface{}) - routes, errs := parseURLs(ra, "route") + routes, errs := parseURLs(ra, "route", warnings) if errs != nil { *errors = append(*errors, errs...) continue @@ -1487,14 +1559,28 @@ func parseCluster(v interface{}, opts *Options, errors *[]error, warnings *[]err return nil } -func parseURLs(a []interface{}, typ string) (urls []*url.URL, errors []error) { +func parseURLs(a []interface{}, typ string, warnings *[]error) (urls []*url.URL, errors []error) { urls = make([]*url.URL, 0, len(a)) var lt token defer convertPanicToErrorList(<, &errors) + dd := make(map[string]bool) + for _, u := range a { tk, u := unwrapValue(u, <) sURL := u.(string) + if dd[sURL] { + err := &configWarningErr{ + field: sURL, + configErr: configErr{ + token: tk, + reason: fmt.Sprintf("Duplicate %s entry detected", typ), + }, + } + *warnings = append(*warnings, err) + continue + } + dd[sURL] = true url, err := parseURL(sURL, typ) if err != nil { err := &configErr{tk, err.Error()} @@ -1603,7 +1689,8 @@ func parseGateway(v interface{}, o *Options, errors *[]error, warnings *[]error) return nil } -var dynamicJSAccountLimits = &JetStreamAccountLimits{-1, -1, -1, -1, false} +var dynamicJSAccountLimits = JetStreamAccountLimits{-1, -1, -1, -1, -1, -1, -1, false} +var defaultJSAccountTiers = map[string]JetStreamAccountLimits{_EMPTY_: dynamicJSAccountLimits} // Parses jetstream account limits for an account. Simple setup with boolen is allowed, and we will // use dynamic account limits. @@ -1616,19 +1703,19 @@ func parseJetStreamForAccount(v interface{}, acc *Account, errors *[]error, warn switch vv := v.(type) { case bool: if vv { - acc.jsLimits = dynamicJSAccountLimits + acc.jsLimits = defaultJSAccountTiers } case string: switch strings.ToLower(vv) { case "enabled", "enable": - acc.jsLimits = dynamicJSAccountLimits + acc.jsLimits = defaultJSAccountTiers case "disabled", "disable": acc.jsLimits = nil default: return &configErr{tk, fmt.Sprintf("Expected 'enabled' or 'disabled' for string value, got '%s'", vv)} } case map[string]interface{}: - jsLimits := &JetStreamAccountLimits{-1, -1, -1, -1, false} + jsLimits := JetStreamAccountLimits{-1, -1, -1, -1, -1, -1, -1, false} for mk, mv := range vv { tk, mv = unwrapValue(mv, <) switch strings.ToLower(mk) { @@ -1637,13 +1724,13 @@ func parseJetStreamForAccount(v interface{}, acc *Account, errors *[]error, warn if !ok { return &configErr{tk, fmt.Sprintf("Expected a parseable size for %q, got %v", mk, mv)} } - jsLimits.MaxMemory = int64(vv) + jsLimits.MaxMemory = vv case "max_store", "max_file", "max_disk", "store", "disk": vv, ok := mv.(int64) if !ok { return &configErr{tk, fmt.Sprintf("Expected a parseable size for %q, got %v", mk, mv)} } - jsLimits.MaxStore = int64(vv) + jsLimits.MaxStore = vv case "max_streams", "streams": vv, ok := mv.(int64) if !ok { @@ -1661,7 +1748,25 @@ func parseJetStreamForAccount(v interface{}, acc *Account, errors *[]error, warn if !ok { return &configErr{tk, fmt.Sprintf("Expected a parseable bool for %q, got %v", mk, mv)} } - jsLimits.MaxBytesRequired = bool(vv) + jsLimits.MaxBytesRequired = vv + case "mem_max_stream_bytes", "memory_max_stream_bytes": + vv, ok := mv.(int64) + if !ok { + return &configErr{tk, fmt.Sprintf("Expected a parseable size for %q, got %v", mk, mv)} + } + jsLimits.MemoryMaxStreamBytes = vv + case "disk_max_stream_bytes", "store_max_stream_bytes": + vv, ok := mv.(int64) + if !ok { + return &configErr{tk, fmt.Sprintf("Expected a parseable size for %q, got %v", mk, mv)} + } + jsLimits.StoreMaxStreamBytes = vv + case "max_ack_pending": + vv, ok := mv.(int64) + if !ok { + return &configErr{tk, fmt.Sprintf("Expected a parseable size for %q, got %v", mk, mv)} + } + jsLimits.MaxAckPending = int(vv) default: if !tk.IsUsedVariable() { err := &unknownConfigFieldErr{ @@ -1675,7 +1780,7 @@ func parseJetStreamForAccount(v interface{}, acc *Account, errors *[]error, warn } } } - acc.jsLimits = jsLimits + acc.jsLimits = map[string]JetStreamAccountLimits{_EMPTY_: jsLimits} default: return &configErr{tk, fmt.Sprintf("Expected map, bool or string to define JetStream, got %T", v)} } @@ -1716,6 +1821,49 @@ func getStorageSize(v interface{}) (int64, error) { return num, nil } +// Parse enablement of jetstream for a server. +func parseJetStreamLimits(v interface{}, opts *Options, errors *[]error, warnings *[]error) error { + var lt token + tk, v := unwrapValue(v, <) + + lim := JSLimitOpts{} + + vv, ok := v.(map[string]interface{}) + if !ok { + return &configErr{tk, fmt.Sprintf("Expected a map to define JetStreamLimits, got %T", v)} + } + for mk, mv := range vv { + tk, mv = unwrapValue(mv, <) + switch strings.ToLower(mk) { + case "max_ack_pending": + lim.MaxAckPending = int(mv.(int64)) + case "max_ha_assets": + lim.MaxHAAssets = int(mv.(int64)) + case "max_request_batch": + lim.MaxRequestBatch = int(mv.(int64)) + case "duplicate_window": + var err error + lim.Duplicates, err = time.ParseDuration(mv.(string)) + if err != nil { + *errors = append(*errors, err) + } + default: + if !tk.IsUsedVariable() { + err := &unknownConfigFieldErr{ + field: mk, + configErr: configErr{ + token: tk, + }, + } + *errors = append(*errors, err) + continue + } + } + } + opts.JetStreamLimits = lim + return nil +} + // Parse enablement of jetstream for a server. func parseJetStream(v interface{}, opts *Options, errors *[]error, warnings *[]error) error { var lt token @@ -1768,6 +1916,12 @@ func parseJetStream(v interface{}, opts *Options, errors *[]error, warnings *[]e opts.JetStreamKey = mv.(string) case "extension_hint": opts.JetStreamExtHint = mv.(string) + case "limits": + if err := parseJetStreamLimits(tk, opts, errors, warnings); err != nil { + return err + } + case "unique_tag": + opts.JetStreamUniqueTag = strings.ToLower(strings.TrimSpace(mv.(string))) default: if !tk.IsUsedVariable() { err := &unknownConfigFieldErr{ @@ -1863,6 +2017,14 @@ func parseLeafNodes(v interface{}, opts *Options, errors *[]error, warnings *[]e case "no_advertise": opts.LeafNode.NoAdvertise = mv.(bool) trackExplicitVal(opts, &opts.inConfig, "LeafNode.NoAdvertise", opts.LeafNode.NoAdvertise) + case "min_version", "minimum_version": + version := mv.(string) + if err := checkLeafMinVersionConfig(version); err != nil { + err = &configErr{tk, err.Error()} + *errors = append(*errors, err) + continue + } + opts.LeafNode.MinVersion = version default: if !tk.IsUsedVariable() { err := &unknownConfigFieldErr{ @@ -2019,7 +2181,7 @@ func parseRemoteLeafNodes(v interface{}, errors *[]error, warnings *[]error) ([] case "url", "urls": switch v := v.(type) { case []interface{}, []string: - urls, errs := parseURLs(v.([]interface{}), "leafnode") + urls, errs := parseURLs(v.([]interface{}), "leafnode", warnings) if errs != nil { *errors = append(*errors, errs...) continue @@ -2165,7 +2327,7 @@ func parseGateways(v interface{}, errors *[]error, warnings *[]error) ([]*Remote } gateway.URLs = append(gateway.URLs, url) case "urls": - urls, errs := parseURLs(v.([]interface{}), "gateway") + urls, errs := parseURLs(v.([]interface{}), "gateway", warnings) if errs != nil { *errors = append(*errors, errs...) continue @@ -2322,7 +2484,7 @@ func parseAccountMappings(v interface{}, acc *Account, errors *[]error, warnings switch vv := v.(type) { case string: if err := acc.AddMapping(subj, v.(string)); err != nil { - err := &configErr{tk, fmt.Sprintf("Error adding mapping for %q: %v", subj, err)} + err := &configErr{tk, fmt.Sprintf("Error adding mapping for %q to %q : %v", subj, v.(string), err)} *errors = append(*errors, err) continue } @@ -2339,7 +2501,7 @@ func parseAccountMappings(v interface{}, acc *Account, errors *[]error, warnings // Now add them in.. if err := acc.AddWeightedMappings(subj, mappings...); err != nil { - err := &configErr{tk, fmt.Sprintf("Error adding mapping for %q: %v", subj, err)} + err := &configErr{tk, fmt.Sprintf("Error adding mapping for %q to %q : %v", subj, v.(string), err)} *errors = append(*errors, err) continue } @@ -2351,7 +2513,7 @@ func parseAccountMappings(v interface{}, acc *Account, errors *[]error, warnings } // Now add it in.. if err := acc.AddWeightedMappings(subj, mdest); err != nil { - err := &configErr{tk, fmt.Sprintf("Error adding mapping for %q: %v", subj, err)} + err := &configErr{tk, fmt.Sprintf("Error adding mapping for %q to %q : %v", subj, v.(string), err)} *errors = append(*errors, err) continue } @@ -2435,7 +2597,10 @@ func parseAccounts(v interface{}, opts *Options, errors *[]error, warnings *[]er case map[string]interface{}: // Track users across accounts, must be unique across // accounts and nkeys vs users. - uorn := make(map[string]struct{}) + // We also want to check for users that may have been added in + // parseAuthorization{} if that happened first. + uorn := setupUsersAndNKeysDuplicateCheckMap(opts) + for aname, mv := range vv { tk, amv := unwrapValue(mv, <) @@ -2536,6 +2701,20 @@ func parseAccounts(v interface{}, opts *Options, errors *[]error, warnings *[]er } } } + // Report error if there is an authorization{} block + // with u/p or token and any user defined in accounts{} + if len(nkeyUsr) > 0 || len(users) > 0 { + if opts.Username != _EMPTY_ { + err := &configErr{usersTk, "Can not have a single user/pass and accounts"} + *errors = append(*errors, err) + continue + } + if opts.Authorization != _EMPTY_ { + err := &configErr{usersTk, "Can not have a token and accounts"} + *errors = append(*errors, err) + continue + } + } applyDefaultPermissions(users, nkeyUsr, acc.defaultPerms) for _, u := range nkeyUsr { if _, ok := uorn[u.Nkey]; ok { @@ -4006,6 +4185,12 @@ func parseMQTT(v interface{}, o *Options, errors *[]error, warnings *[]error) er } case "js_domain": o.MQTT.JsDomain = mv.(string) + case "stream_replicas": + o.MQTT.StreamReplicas = int(mv.(int64)) + case "consumer_replicas": + o.MQTT.ConsumerReplicas = int(mv.(int64)) + case "consumer_memory_storage": + o.MQTT.ConsumerMemoryStorage = mv.(bool) default: if !tk.IsUsedVariable() { err := &unknownConfigFieldErr{ @@ -4146,6 +4331,9 @@ func MergeOptions(fileOpts, flagOpts *Options) *Options { if flagOpts.RoutesStr != "" { mergeRoutes(&opts, flagOpts) } + if flagOpts.JetStream { + fileOpts.JetStream = flagOpts.JetStream + } return &opts } diff --git a/vendor/github.com/nats-io/nats-server/v2/server/raft.go b/vendor/github.com/nats-io/nats-server/v2/server/raft.go index 19bf6533..99a8c5d4 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/raft.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/raft.go @@ -1,4 +1,4 @@ -// Copyright 2020-2021 The NATS Authors +// Copyright 2020-2022 The NATS Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -48,6 +48,7 @@ type RaftNode interface { Leader() bool Quorum() bool Current() bool + Term() uint64 GroupLeader() string HadPreviousLeader() bool StepDown(preferred ...string) error @@ -55,25 +56,27 @@ type RaftNode interface { ID() string Group() string Peers() []*Peer + UpdateKnownPeers(knownPeers []string) ProposeAddPeer(peer string) error ProposeRemovePeer(peer string) error AdjustClusterSize(csz int) error AdjustBootClusterSize(csz int) error ClusterSize() int ApplyQ() *ipQueue // of *CommittedEntry - PauseApply() + PauseApply() error ResumeApply() LeadChangeC() <-chan bool QuitC() <-chan struct{} Created() time.Time Stop() Delete() + Wipe() } type WAL interface { Type() StorageType StoreMsg(subj string, hdr, msg []byte) (uint64, int64, error) - LoadMsg(index uint64) (subj string, hdr, msg []byte, ts int64, err error) + LoadMsg(index uint64, sm *StoreMsg) (*StoreMsg, error) RemoveMsg(index uint64) (bool, error) Compact(index uint64) (uint64, error) Purge() (uint64, error) @@ -140,6 +143,7 @@ type raft struct { elect *time.Timer active time.Time llqrt time.Time + lsut time.Time term uint64 pterm uint64 pindex uint64 @@ -183,8 +187,9 @@ type raft struct { progress map[string]*ipQueue // of uint64 // For when we have paused our applyC. - paused bool - hcommit uint64 + paused bool + hcommit uint64 + pobserver bool // Queues and Channels prop *ipQueue // of *Entry @@ -197,6 +202,9 @@ type raft struct { leadc chan bool quit chan struct{} + // Account name of the asset this raft group is for + accName string + // Random generator, used to generate inboxes for instance prand *rand.Rand } @@ -216,15 +224,18 @@ type catchupState struct { type lps struct { ts int64 li uint64 + kp bool // marks as known peer. } const ( - minElectionTimeoutDefault = 2 * time.Second - maxElectionTimeoutDefault = 5 * time.Second - minCampaignTimeoutDefault = 100 * time.Millisecond - maxCampaignTimeoutDefault = 4 * minCampaignTimeoutDefault - hbIntervalDefault = 500 * time.Millisecond - lostQuorumIntervalDefault = hbIntervalDefault * 5 + minElectionTimeoutDefault = 4 * time.Second + maxElectionTimeoutDefault = 9 * time.Second + minCampaignTimeoutDefault = 100 * time.Millisecond + maxCampaignTimeoutDefault = 8 * minCampaignTimeoutDefault + hbIntervalDefault = 1 * time.Second + lostQuorumIntervalDefault = hbIntervalDefault * 10 // 10 seconds + lostQuorumCheckIntervalDefault = hbIntervalDefault * 10 // 10 seconds + ) var ( @@ -234,6 +245,7 @@ var ( maxCampaignTimeout = maxCampaignTimeoutDefault hbInterval = hbIntervalDefault lostQuorumInterval = lostQuorumIntervalDefault + lostQuorumCheck = lostQuorumCheckIntervalDefault ) type RaftConfig struct { @@ -250,14 +262,13 @@ var ( errNilCfg = errors.New("raft: no config given") errCorruptPeers = errors.New("raft: corrupt peer state") errEntryLoadFailed = errors.New("raft: could not load entry from WAL") - errEntryStoreFailed = errors.New("raft: could not storeentry to WAL") + errEntryStoreFailed = errors.New("raft: could not store entry to WAL") errNodeClosed = errors.New("raft: node is closed") errBadSnapName = errors.New("raft: snapshot name could not be parsed") errNoSnapAvailable = errors.New("raft: no snapshot available") errCatchupsRunning = errors.New("raft: snapshot can not be installed while catchups running") errSnapshotCorrupt = errors.New("raft: snapshot corrupt") errTooManyPrefs = errors.New("raft: stepdown requires at most one preferred new leader") - errStepdownNoPeer = errors.New("raft: stepdown failed, could not match new leader") errNoPeerState = errors.New("raft: no peerstate") errAdjustBootCluster = errors.New("raft: can not adjust boot peer size on established group") errLeaderLen = fmt.Errorf("raft: leader should be exactly %d bytes", idLen) @@ -279,7 +290,7 @@ func (s *Server) bootstrapRaftNode(cfg *RaftConfig, knownPeers []string, allPeer expected := len(knownPeers) // We need to adjust this is all peers are not known. if !allPeersKnown { - s.Debugf("Determining expected peer size for JetStream metacontroller") + s.Debugf("Determining expected peer size for JetStream meta group") if expected < 2 { expected = 2 } @@ -330,7 +341,7 @@ func (s *Server) bootstrapRaftNode(cfg *RaftConfig, knownPeers []string, allPeer } // startRaftNode will start the raft node. -func (s *Server) startRaftNode(cfg *RaftConfig) (RaftNode, error) { +func (s *Server) startRaftNode(accName string, cfg *RaftConfig) (RaftNode, error) { if cfg == nil { return nil, errNilCfg } @@ -353,7 +364,7 @@ func (s *Server) startRaftNode(cfg *RaftConfig) (RaftNode, error) { return nil, errNoPeerState } - qpfx := fmt.Sprintf("RAFT [%s - %s] ", hash[:idLen], cfg.Name) + qpfx := fmt.Sprintf("[ACC:%s] RAFT '%s' ", accName, cfg.Name) rsrc := time.Now().UnixNano() if len(pub) >= 32 { if h, _ := highwayhash.New64([]byte(pub[:32])); h != nil { @@ -382,13 +393,14 @@ func (s *Server) startRaftNode(cfg *RaftConfig) (RaftNode, error) { quit: make(chan struct{}), wtvch: make(chan struct{}, 1), wpsch: make(chan struct{}, 1), - reqs: newIPQueue(), // of *voteRequest - votes: newIPQueue(), // of *voteResponse - prop: newIPQueue(ipQueue_Logger(qpfx+"Entry", s.ipqLog)), // of *Entry - entry: newIPQueue(ipQueue_Logger(qpfx+"AppendEntry", s.ipqLog)), // of *appendEntry - resp: newIPQueue(ipQueue_Logger(qpfx+"AppendEntryResponse", s.ipqLog)), // of *appendEntryResponse - apply: newIPQueue(ipQueue_Logger(qpfx+"CommittedEntry", s.ipqLog)), // of *CommittedEntry - stepdown: newIPQueue(), // of string + reqs: s.newIPQueue(qpfx + "vreq"), // of *voteRequest + votes: s.newIPQueue(qpfx + "vresp"), // of *voteResponse + prop: s.newIPQueue(qpfx + "entry"), // of *Entry + entry: s.newIPQueue(qpfx + "appendEntry"), // of *appendEntry + resp: s.newIPQueue(qpfx + "appendEntryResponse"), // of *appendEntryResponse + apply: s.newIPQueue(qpfx + "committedEntry"), // of *CommittedEntry + stepdown: s.newIPQueue(qpfx + "stepdown"), // of string + accName: accName, leadc: make(chan bool, 1), observer: cfg.Observer, extSt: ps.domainExt, @@ -455,12 +467,12 @@ func (s *Server) startRaftNode(cfg *RaftConfig) (RaftNode, error) { n.apply.push(nil) // Make sure to track ourselves. - n.trackPeer(n.id) + n.peers[n.id] = &lps{time.Now().UnixNano(), 0, true} // Track known peers for _, peer := range ps.knownPeers { - // Set these to 0 to start. + // Set these to 0 to start but mark as known peer. if peer != n.id { - n.peers[peer] = &lps{0, 0} + n.peers[peer] = &lps{0, 0, true} } } @@ -679,6 +691,17 @@ func (n *raft) ProposeAddPeer(peer string) error { return nil } +// As a leader if we are proposing to remove a peer assume its already gone. +func (n *raft) doRemovePeerAsLeader(peer string) { + n.Lock() + if _, ok := n.peers[peer]; ok { + delete(n.peers, peer) + // We should decrease our cluster size since we are tracking this peer and the peer is most likely already gone. + n.adjustClusterSizeAndQuorum() + } + n.Unlock() +} + // ProposeRemovePeer is called to remove a peer from the group. func (n *raft) ProposeRemovePeer(peer string) error { n.RLock() @@ -694,6 +717,7 @@ func (n *raft) ProposeRemovePeer(peer string) error { if isLeader { prop.push(&Entry{EntryRemovePeer, []byte(peer)}) + n.doRemovePeerAsLeader(peer) return nil } @@ -743,6 +767,7 @@ func (n *raft) AdjustClusterSize(csz int) error { if csz < 2 { csz = 2 } + // Adjust. n.csz = csz n.qn = n.csz/2 + 1 @@ -754,20 +779,38 @@ func (n *raft) AdjustClusterSize(csz int) error { // PauseApply will allow us to pause processing of append entries onto our // external apply chan. -func (n *raft) PauseApply() { +func (n *raft) PauseApply() error { n.Lock() defer n.Unlock() - n.debug("Pausing apply channel") + if n.state == Leader { + return errAlreadyLeader + } + // If we are currently a candidate make sure we step down. + if n.state == Candidate { + n.stepdown.push(noLeader) + } + + n.debug("Pausing our apply channel") n.paused = true n.hcommit = n.commit + // Also prevent us from trying to become a leader while paused and catching up. + n.pobserver, n.observer = n.observer, true + n.resetElect(48 * time.Hour) + + return nil } func (n *raft) ResumeApply() { n.Lock() defer n.Unlock() - n.debug("Resuming apply channel") + if !n.paused { + return + } + + n.debug("Resuming our apply channel") + n.observer, n.pobserver = n.pobserver, false n.paused = false // Run catchup.. if n.hcommit > n.commit { @@ -779,6 +822,7 @@ func (n *raft) ResumeApply() { } } n.hcommit = 0 + n.resetElectionTimeout() } // Compact will compact our WAL. @@ -860,7 +904,7 @@ func (n *raft) encodeSnapshot(snap *snapshot) []byte { // SendSnapshot will send the latest snapshot as a normal AE. // Should only be used when the upper layers know this is most recent. -// Used when restoring streams etc. +// Used when restoring streams, moving a stream from R1 to R>1, etc. func (n *raft) SendSnapshot(data []byte) error { n.sendAppendEntry([]*Entry{{EntrySnapshot, data}}) return nil @@ -1084,6 +1128,12 @@ func (n *raft) Leader() bool { return isLeader } +func (n *raft) isCatchingUp() bool { + n.RLock() + defer n.RUnlock() + return n.catchup != nil +} + // Lock should be held. func (n *raft) isCurrent() bool { // First check if we match commit and applied. @@ -1176,8 +1226,13 @@ func (n *raft) StepDown(preferred ...string) error { nowts := time.Now().UnixNano() maybeLeader := noLeader if len(preferred) > 0 { - maybeLeader = preferred[0] + if preferred[0] != _EMPTY_ { + maybeLeader = preferred[0] + } else { + preferred = nil + } } + for peer, ps := range n.peers { // If not us and alive and caughtup. if peer != n.id && (nowts-ps.ts) < int64(hbInterval*3) { @@ -1196,15 +1251,18 @@ func (n *raft) StepDown(preferred ...string) error { n.Unlock() if len(preferred) > 0 && maybeLeader == noLeader { - return errStepdownNoPeer + n.debug("Can not transfer to preferred peer %q", preferred[0]) } + // If we have a new leader selected, transfer over to them. if maybeLeader != noLeader { - n.debug("Stepping down, selected %q for new leader", maybeLeader) + n.debug("Selected %q for new leader", maybeLeader) n.sendAppendEntry([]*Entry{{EntryLeaderTransfer, []byte(maybeLeader)}}) } // Force us to stepdown here. + n.debug("Stepping down") stepdown.push(noLeader) + return nil } @@ -1227,11 +1285,21 @@ func (n *raft) campaign() error { if n.state == Leader { return errAlreadyLeader } - n.lxfer = true n.resetElect(randCampaignTimeout()) return nil } +// xferCampaign will have our node start an immediate leadership vote. +// Lock should be held. +func (n *raft) xferCampaign() error { + n.debug("Starting transfer campaign") + if n.state == Leader { + return errAlreadyLeader + } + n.resetElect(10 * time.Millisecond) + return nil +} + // State returns the current state for this node. func (n *raft) State() RaftState { n.RLock() @@ -1291,6 +1359,27 @@ func (n *raft) Peers() []*Peer { return peers } +// Update our known set of peers. +func (n *raft) UpdateKnownPeers(knownPeers []string) { + n.Lock() + // If this is a scale up, let the normal add peer logic take precedence. + // Otherwise if the new peers are slow to start we stall ourselves. + if len(knownPeers) > len(n.peers) { + n.Unlock() + return + } + // Process like peer state update. + ps := &peerState{knownPeers, len(knownPeers), n.extSt} + n.processPeerState(ps) + isLeader := n.state == Leader + n.Unlock() + + // If we are the leader send this update out as well. + if isLeader { + n.sendPeerState() + } +} + func (n *raft) ApplyQ() *ipQueue { return n.apply } // queue of *CommittedEntry func (n *raft) LeadChangeC() <-chan bool { return n.leadc } func (n *raft) QuitC() <-chan struct{} { return n.quit } @@ -1337,6 +1426,12 @@ func (n *raft) shutdown(shouldDelete bool) { os.Remove(filepath.Join(n.sd, termVoteFile)) os.RemoveAll(filepath.Join(n.sd, snapshotsDir)) } + // Unregistering ipQueues do not prevent them from push/pop + // just will remove them from the central monitoring map + queues := []*ipQueue{n.reqs, n.votes, n.prop, n.entry, n.resp, n.apply, n.stepdown} + for _, q := range queues { + q.unregister() + } n.Unlock() s.unregisterRaftNode(g) @@ -1354,6 +1449,20 @@ func (n *raft) shutdown(shouldDelete bool) { } } +// Wipe will force an on disk state reset and then call Delete(). +// Useful in case we have been stopped before this point. +func (n *raft) Wipe() { + n.RLock() + wal := n.wal + n.RUnlock() + // Delete our underlying storage. + if wal != nil { + wal.Delete() + } + // Now call delete. + n.Delete() +} + // Lock should be held (due to use of random generator) func (n *raft) newInbox() string { var b [replySuffixLen]byte @@ -1425,6 +1534,10 @@ func (n *raft) resetElectionTimeout() { n.resetElect(randElectionTimeout()) } +func (n *raft) resetElectionTimeoutWithLock() { + n.resetElectWithLock(randElectionTimeout()) +} + // Lock should be held. func (n *raft) resetElect(et time.Duration) { if n.elect == nil { @@ -1440,10 +1553,41 @@ func (n *raft) resetElect(et time.Duration) { } } +func (n *raft) resetElectWithLock(et time.Duration) { + n.Lock() + n.resetElect(et) + n.Unlock() +} + func (n *raft) run() { s := n.s defer s.grWG.Done() + // We want to wait for some routing to be enabled, so we will wait for + // at least a route, leaf or gateway connection to be established before + // starting the run loop. + gw := s.gateway + for { + s.mu.Lock() + ready := len(s.routes)+len(s.leafs) > 0 + if !ready && gw.enabled { + gw.RLock() + ready = len(gw.out)+len(gw.in) > 0 + gw.RUnlock() + } + s.mu.Unlock() + if !ready { + select { + case <-s.quitCh: + return + case <-time.After(100 * time.Millisecond): + s.RateLimitWarnf("Waiting for routing to be established...") + } + } else { + break + } + } + for s.isRunning() { switch n.State() { case Follower: @@ -1470,7 +1614,7 @@ func (n *raft) debug(format string, args ...interface{}) { func (n *raft) warn(format string, args ...interface{}) { nf := fmt.Sprintf("RAFT [%s - %s] %s", n.id, n.group, format) - n.s.Warnf(nf, args...) + n.s.RateLimitWarnf(nf, args...) } func (n *raft) error(format string, args ...interface{}) { @@ -1524,6 +1668,7 @@ func convertVoteResponse(i interface{}) *voteResponse { func (n *raft) runAsFollower() { for { elect := n.electTimer() + select { case <-n.entry.ch: n.processAppendEntries() @@ -1535,11 +1680,13 @@ func (n *raft) runAsFollower() { case <-elect.C: // If we are out of resources we just want to stay in this state for the moment. if n.outOfResources() { - n.resetElectionTimeout() + n.resetElectionTimeoutWithLock() n.debug("Not switching to candidate, no resources") } else if n.isObserver() { - n.resetElect(48 * time.Hour) + n.resetElectWithLock(48 * time.Hour) n.debug("Not switching to candidate, observer only") + } else if n.isCatchingUp() { + n.debug("Not switching to candidate, catching up") } else { n.switchToCandidate() return @@ -1829,6 +1976,9 @@ func (n *raft) runAsLeader() { hb := time.NewTicker(hbInterval) defer hb.Stop() + lq := time.NewTicker(lostQuorumCheck) + defer lq.Stop() + for { select { case <-n.s.quitCh: @@ -1851,6 +2001,9 @@ func (n *raft) runAsLeader() { sz := 0 for i, bi := range es { b := bi.(*Entry) + if b.Type == EntryRemovePeer { + n.doRemovePeerAsLeader(string(b.Data)) + } entries = append(entries, b) sz += len(b.Data) + 1 if i != len(es)-1 && sz < maxBatch && len(entries) < math.MaxUint16 { @@ -1866,6 +2019,7 @@ func (n *raft) runAsLeader() { if n.notActive() { n.sendHeartbeat() } + case <-lq.C: if n.lostQuorum() { n.switchToFollower(noLeader) return @@ -1876,7 +2030,7 @@ func (n *raft) runAsLeader() { if vresp == nil { continue } - if vresp.term > n.currentTerm() { + if vresp.term > n.Term() { n.switchToFollower(noLeader) return } @@ -1918,6 +2072,11 @@ func (n *raft) lostQuorum() bool { } func (n *raft) lostQuorumLocked() bool { + // Make sure we let any scale up actions settle before deciding. + if !n.lsut.IsZero() && time.Since(n.lsut) < lostQuorumInterval { + return false + } + now, nc := time.Now().UnixNano(), 1 for _, peer := range n.peers { if now-peer.ts < int64(lostQuorumInterval) { @@ -1939,7 +2098,7 @@ func (n *raft) notActive() bool { } // Return our current term. -func (n *raft) currentTerm() uint64 { +func (n *raft) Term() uint64 { n.RLock() defer n.RUnlock() return n.term @@ -1973,6 +2132,7 @@ func (n *raft) runCatchup(ar *appendEntryResponse, indexUpdatesQ *ipQueue /* of n.debug("Catchup done for %q, will add into peers", peer) n.ProposeAddPeer(peer) } + indexUpdatesQ.unregister() }() n.debug("Running catchup for %q", peer) @@ -2083,14 +2243,14 @@ func (n *raft) catchupFollower(ar *appendEntryResponse) { var state StreamState n.wal.FastState(&state) - if start < state.FirstSeq || state.Msgs == 0 && start <= state.LastSeq { + if start < state.FirstSeq || (state.Msgs == 0 && start <= state.LastSeq) { n.debug("Need to send snapshot to follower") if lastIndex, err := n.sendSnapshotToFollower(ar.reply); err != nil { n.error("Error sending snapshot to follower [%s]: %v", ar.peer, err) n.Unlock() return } else { - // If no other entries can just return here. + // If no other entries, we can just return here. if state.Msgs == 0 { n.debug("Finished catching up") n.Unlock() @@ -2103,19 +2263,19 @@ func (n *raft) catchupFollower(ar *appendEntryResponse) { ae, err := n.loadEntry(start) if err != nil { + n.warn("Request from follower for index [%d] possibly beyond our last index [%d] - %v", start, state.LastSeq, err) ae, err = n.loadFirstEntry() } if err != nil || ae == nil { - n.debug("Could not find a starting entry: %v", err) + n.warn("Could not find a starting entry for catchup request: %v", err) n.Unlock() return } if ae.pindex != ar.index || ae.pterm != ar.term { - n.debug("Our first entry does not match request from follower") + n.debug("Our first entry [%d:%d] does not match request from follower [%d:%d]", ae.pterm, ae.pindex, ar.term, ar.index) } // Create a queue for delivering updates from responses. - qname := fmt.Sprintf("RAFT [%s - %s] Index updates", n.id, n.group) - indexUpdates := newIPQueue(ipQueue_Logger(qname, n.s.ipqLog)) // of uint64 + indexUpdates := n.s.newIPQueue(fmt.Sprintf("[ACC:%s] RAFT '%s' indexUpdates", n.accName, n.group)) // of uint64 indexUpdates.push(ae.pindex) n.progress[ar.peer] = indexUpdates n.Unlock() @@ -2124,11 +2284,12 @@ func (n *raft) catchupFollower(ar *appendEntryResponse) { } func (n *raft) loadEntry(index uint64) (*appendEntry, error) { - _, _, msg, _, err := n.wal.LoadMsg(index) + var smp StoreMsg + sm, err := n.wal.LoadMsg(index, &smp) if err != nil { return nil, err } - return n.decodeAppendEntry(msg, nil, _EMPTY_) + return n.decodeAppendEntry(sm.msg, nil, _EMPTY_) } // applyCommit will update our commit index and apply the entry to the apply chan. @@ -2199,18 +2360,20 @@ func (n *raft) applyCommit(index uint64) error { delete(n.removed, newPeer) } - if _, ok := n.peers[newPeer]; !ok { + if lp, ok := n.peers[newPeer]; !ok { // We are not tracking this one automatically so we need to bump cluster size. - n.peers[newPeer] = &lps{time.Now().UnixNano(), 0} - if n.csz < len(n.peers) { - n.debug("Expanding our clustersize: %d -> %d", n.csz, len(n.peers)) - n.csz = len(n.peers) - n.qn = n.csz/2 + 1 - } + n.peers[newPeer] = &lps{time.Now().UnixNano(), 0, true} + } else { + // Mark as added. + lp.kp = true } + // Adjust cluster size and quorum if needed. + n.adjustClusterSizeAndQuorum() + // Write out our new state. n.writePeerState(&peerState{n.peerNames(), n.csz, n.extSt}) // We pass these up as well. committed = append(committed, e) + case EntryRemovePeer: peer := string(e.Data) n.debug("Removing peer %q", peer) @@ -2222,13 +2385,11 @@ func (n *raft) applyCommit(index uint64) error { n.removed[peer] = peer if _, ok := n.peers[peer]; ok { - // We should decrease our cluster size since we are tracking this peer. delete(n.peers, peer) - if n.csz != len(n.peers) { - n.debug("Decreasing our clustersize: %d -> %d", n.csz, len(n.peers)) - n.csz = len(n.peers) - n.qn = n.csz/2 + 1 - } + // We should decrease our cluster size since we are tracking this peer. + n.adjustClusterSizeAndQuorum() + // Write out our new state. + n.writePeerState(&peerState{n.peerNames(), n.csz, n.extSt}) } // If this is us and we are the leader we should attempt to stepdown. @@ -2236,8 +2397,6 @@ func (n *raft) applyCommit(index uint64) error { n.stepdown.push(n.selectNextLeader()) } - // Write out our new state. - n.writePeerState(&peerState{n.peerNames(), n.csz, n.extSt}) // We pass these up as well. committed = append(committed, e) } @@ -2284,13 +2443,13 @@ func (n *raft) trackResponse(ar *appendEntryResponse) { // We have a quorum. for index := n.commit + 1; index <= ar.index; index++ { if err := n.applyCommit(index); err != nil { + n.error("Got an error apply commit for %d: %v", index, err) break } } sendHB = n.prop.len() == 0 } } - n.Unlock() if sendHB { @@ -2298,50 +2457,55 @@ func (n *raft) trackResponse(ar *appendEntryResponse) { } } +// Used to adjust cluster size and peer count based on added official peers. +// lock should be held. +func (n *raft) adjustClusterSizeAndQuorum() { + pcsz, ncsz := n.csz, 0 + for _, peer := range n.peers { + if peer.kp { + ncsz++ + } + } + n.csz = ncsz + n.qn = n.csz/2 + 1 + + if ncsz > pcsz { + n.debug("Expanding our clustersize: %d -> %d", pcsz, ncsz) + n.lsut = time.Now() + } else if ncsz < pcsz { + n.debug("Decreasing our clustersize: %d -> %d", pcsz, ncsz) + if n.state == Leader { + go n.sendHeartbeat() + } + } +} + // Track interactions with this peer. func (n *raft) trackPeer(peer string) error { n.Lock() - var needPeerUpdate, isRemoved bool + var needPeerAdd, isRemoved bool if n.removed != nil { _, isRemoved = n.removed[peer] } if n.state == Leader { - if _, ok := n.peers[peer]; !ok { + if lp, ok := n.peers[peer]; !ok || !lp.kp { // Check if this peer had been removed previously. - if !isRemoved { - needPeerUpdate = true - } + needPeerAdd = !isRemoved } } if ps := n.peers[peer]; ps != nil { ps.ts = time.Now().UnixNano() } else if !isRemoved { - n.peers[peer] = &lps{time.Now().UnixNano(), 0} + n.peers[peer] = &lps{time.Now().UnixNano(), 0, false} } n.Unlock() - if needPeerUpdate { + if needPeerAdd { n.ProposeAddPeer(peer) } return nil } -// Return the number of active peers for this group. We use this when we -// are running as a candidate. -// Lock should be held. -func (n *raft) numActivePeers() int { - nap := 0 - for id := range n.peers { - if sir, ok := n.s.nodeToInfo.Load(id); ok && sir != nil { - si := sir.(nodeInfo) - if !si.offline { - nap++ - } - } - } - return nap -} - func (n *raft) runAsCandidate() { n.Lock() // Drain old responses. @@ -2353,7 +2517,6 @@ func (n *raft) runAsCandidate() { // We vote for ourselves. votes := 1 - won := false for { elect := n.electTimer() @@ -2369,12 +2532,7 @@ func (n *raft) runAsCandidate() { case <-n.quit: return case <-elect.C: - if won { - // we are here if we won the election but some server did not respond - n.switchToLeader() - } else { - n.switchToCandidate() - } + n.switchToCandidate() return case <-n.votes.ch: // Because of drain() it is possible that we get nil from popOne(). @@ -2383,7 +2541,7 @@ func (n *raft) runAsCandidate() { continue } n.RLock() - nterm, lxfer := n.term, n.lxfer + nterm := n.term n.RUnlock() if vresp.granted && nterm >= vresp.term { @@ -2391,18 +2549,9 @@ func (n *raft) runAsCandidate() { n.trackPeer(vresp.peer) votes++ if n.wonElection(votes) { - if votes == n.numActivePeers() || lxfer { - // Become LEADER if we have won and gotten a quorum with everyone we should hear from. - n.switchToLeader() - return - } else { - // Not everyone is in this quorum, yet? - // Wait for the remaining responses and become leader once everyone did. - // Or Wait until after the election timeout and become leader then. - // In case another server responds with vresp.granted==false and vresp.term > n.term, - // we will start all over again. - won = true - } + // Become LEADER if we have won and gotten a quorum with everyone we should hear from. + n.switchToLeader() + return } } else if vresp.term > nterm { // if we observe a bigger term, we should start over again or risk forming a quorum fully knowing @@ -2413,6 +2562,7 @@ func (n *raft) runAsCandidate() { n.writeTermVote() n.debug("Stepping down from candidate, detected higher term: %d vs %d", vresp.term, n.term) n.stepdown.push(noLeader) + n.lxfer = false n.Unlock() return } @@ -2508,6 +2658,11 @@ func (n *raft) updateLeader(newLeader string) { func (n *raft) processAppendEntry(ae *appendEntry, sub *subscription) { n.Lock() + // Don't reset here if we have been asked to assume leader position. + if !n.lxfer { + n.resetElectionTimeout() + } + // Just return if closed or we had previous write error. if n.state == Closed || n.werr != nil { n.Unlock() @@ -2529,11 +2684,12 @@ func (n *raft) processAppendEntry(ae *appendEntry, sub *subscription) { } else { // Let them know we are the leader. ar := &appendEntryResponse{n.term, n.pindex, n.id, false, _EMPTY_} - n.Unlock() n.debug("AppendEntry ignoring old term from another leader") n.sendRPC(ae.reply, _EMPTY_, ar.encode(arbuf)) - return } + // Always return here from processing. + n.Unlock() + return } // If we received an append entry as a candidate we should convert to a follower. @@ -2547,8 +2703,6 @@ func (n *raft) processAppendEntry(ae *appendEntry, sub *subscription) { n.stepdown.push(ae.leader) } - n.resetElectionTimeout() - // Catching up state. catchingUp := n.catchup != nil // Is this a new entry? @@ -2559,7 +2713,7 @@ func (n *raft) processAppendEntry(ae *appendEntry, sub *subscription) { if ps := n.peers[ae.leader]; ps != nil { ps.ts = time.Now().UnixNano() } else { - n.peers[ae.leader] = &lps{time.Now().UnixNano(), 0} + n.peers[ae.leader] = &lps{time.Now().UnixNano(), 0, true} } } @@ -2585,6 +2739,8 @@ func (n *raft) processAppendEntry(ae *appendEntry, sub *subscription) { if cs := n.catchup; cs != nil && n.pterm >= cs.cterm && n.pindex >= cs.cindex { // If we are here we are good, so if we have a catchup pending we can cancel. n.cancelCatchup() + // Reset our notion of catching up. + catchingUp = false } else if isNew { var ar *appendEntryResponse var inbox string @@ -2631,8 +2787,13 @@ func (n *raft) processAppendEntry(ae *appendEntry, sub *subscription) { if eae, err := n.loadEntry(ae.pindex); err == nil && eae != nil { // If terms mismatched, delete that entry and all others past it. if ae.pterm > eae.pterm { + // Truncate will reset our pterm and pindex. n.truncateWAL(ae.pterm, ae.pindex) - ar = &appendEntryResponse{n.pterm, n.pindex, n.id, false, _EMPTY_} + // Make sure to cancel any catchups in progress. + if catchingUp { + n.cancelCatchup() + } + ar = &appendEntryResponse{ae.pterm, ae.pindex, n.id, false, _EMPTY_} } else { ar = &appendEntryResponse{ae.pterm, ae.pindex, n.id, true, _EMPTY_} } @@ -2657,7 +2818,8 @@ func (n *raft) processAppendEntry(ae *appendEntry, sub *subscription) { n.Unlock() return } - // Snapshots and peerstate will always be together when a leader is catching us up. + // This means we already entered into a catchup state but what the leader sent us did not match what we expected. + // Snapshots and peerstate will always be together when a leader is catching us up in this fashion. if len(ae.entries) != 2 || ae.entries[0].Type != EntrySnapshot || ae.entries[1].Type != EntryPeerState { n.warn("Expected first catchup entry to be a snapshot and peerstate, will retry") n.cancelCatchup() @@ -2734,17 +2896,18 @@ func (n *raft) processAppendEntry(ae *appendEntry, sub *subscription) { case EntryLeaderTransfer: if isNew { maybeLeader := string(e.Data) - if maybeLeader == n.id { - n.campaign() + if maybeLeader == n.id && !n.observer && !n.paused { + n.lxfer = true + n.xferCampaign() } } case EntryAddPeer: if newPeer := string(e.Data); len(newPeer) == idLen { - // Track directly + // Track directly, but wait for commit to be official if ps := n.peers[newPeer]; ps != nil { ps.ts = time.Now().UnixNano() } else { - n.peers[newPeer] = &lps{time.Now().UnixNano(), 0} + n.peers[newPeer] = &lps{time.Now().UnixNano(), 0, false} } } } @@ -2776,13 +2939,16 @@ func (n *raft) processAppendEntry(ae *appendEntry, sub *subscription) { func (n *raft) processPeerState(ps *peerState) { // Update our version of peers to that of the leader. n.csz = ps.clusterSize + n.qn = n.csz/2 + 1 + old := n.peers n.peers = make(map[string]*lps) for _, peer := range ps.knownPeers { if lp := old[peer]; lp != nil { + lp.kp = true n.peers[peer] = lp } else { - n.peers[peer] = &lps{0, 0} + n.peers[peer] = &lps{0, 0, true} } } n.debug("Update peers from leader to %+v", n.peers) @@ -2796,7 +2962,7 @@ func (n *raft) processAppendEntryResponse(ar *appendEntryResponse) { if ar.success { n.trackResponse(ar) } else if ar.term > n.term { - // False here, check to make sure they do not have a higher term. + // False here and they have a higher term. n.term = ar.term n.vote = noVote n.writeTermVote() @@ -2812,6 +2978,14 @@ func (n *raft) handleAppendEntryResponse(sub *subscription, c *client, _ *Accoun ar := n.decodeAppendEntryResponse(msg) ar.reply = reply n.resp.push(ar) + if ar.success { + n.Lock() + // Update peer's last index. + if ps := n.peers[ar.peer]; ps != nil && ar.index > ps.li { + ps.li = ar.index + } + n.Unlock() + } } func (n *raft) buildAppendEntry(entries []*Entry) *appendEntry { @@ -2839,9 +3013,10 @@ func (n *raft) storeToWAL(ae *appendEntry) error { if index > seq { // Reset to last before this one. if ae, err := n.loadEntry(seq - 1); err == nil && ae != nil { + nl := n.selectNextLeader() n.truncateWAL(ae.pterm, ae.pindex) if n.state == Leader { - n.stepdown.push(n.selectNextLeader()) + n.stepdown.push(nl) } } else { panic(fmt.Sprintf("[%s | %s] Wrong index, ae is %+v, seq is %d, n.pindex is %d\n\n", n.s, n.group, ae, seq, n.pindex)) @@ -2947,8 +3122,10 @@ func decodePeerState(buf []byte) (*peerState, error) { // Lock should be held. func (n *raft) peerNames() []string { var peers []string - for peer := range n.peers { - peers = append(peers, peer) + for name, peer := range n.peers { + if peer.kp { + peers = append(peers, name) + } } return peers } @@ -2965,6 +3142,7 @@ func (n *raft) sendPeerState() { n.sendAppendEntry([]*Entry{{EntryPeerState, encodePeerState(n.currentPeerState())}}) } +// Send a heartbeat. func (n *raft) sendHeartbeat() { n.sendAppendEntry(nil) } @@ -3359,6 +3537,7 @@ func (n *raft) switchToFollower(leader string) { return } n.debug("Switching to follower") + n.lxfer = false n.updateLeader(leader) n.switchState(Follower) @@ -3370,10 +3549,14 @@ func (n *raft) switchToCandidate() { if n.state == Closed { return } + // If we are catching up or are in observer mode we can not switch. + if n.observer || n.paused { + return + } + if n.state != Candidate { n.debug("Switching to candidate") } else { - n.lxfer = false if n.lostQuorumLocked() && time.Since(n.llqrt) > 20*time.Second { // We signal to the upper layers such that can alert on quorum lost. n.updateLeadChange(false) @@ -3394,7 +3577,8 @@ func (n *raft) switchToLeader() { return } n.debug("Switching to leader") - n.updateLeader(n.id) + n.lxfer = false + n.updateLeader(n.id) n.switchState(Leader) } diff --git a/vendor/github.com/nats-io/nats-server/v2/server/reload.go b/vendor/github.com/nats-io/nats-server/v2/server/reload.go index 769c158e..bce1e3f7 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/reload.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/reload.go @@ -648,6 +648,33 @@ func (o *mqttMaxAckPendingReload) Apply(s *Server) { s.Noticef("Reloaded: MQTT max_ack_pending = %v", o.newValue) } +type mqttStreamReplicasReload struct { + noopOption + newValue int +} + +func (o *mqttStreamReplicasReload) Apply(s *Server) { + s.Noticef("Reloaded: MQTT stream_replicas = %v", o.newValue) +} + +type mqttConsumerReplicasReload struct { + noopOption + newValue int +} + +func (o *mqttConsumerReplicasReload) Apply(s *Server) { + s.Noticef("Reloaded: MQTT consumer_replicas = %v", o.newValue) +} + +type mqttConsumerMemoryStorageReload struct { + noopOption + newValue bool +} + +func (o *mqttConsumerMemoryStorageReload) Apply(s *Server) { + s.Noticef("Reloaded: MQTT consumer_memory_storage = %v", o.newValue) +} + // Compares options and disconnects clients that are no longer listed in pinned certs. Lock must not be held. func (s *Server) recheckPinnedCerts(curOpts *Options, newOpts *Options) { s.mu.Lock() @@ -885,7 +912,7 @@ func imposeOrder(value interface{}) error { sort.Strings(value.AllowedOrigins) case string, bool, uint8, int, int32, int64, time.Duration, float64, nil, LeafNodeOpts, ClusterOpts, *tls.Config, PinnedCertSet, *URLAccResolver, *MemAccResolver, *DirAccResolver, *CacheDirAccResolver, Authentication, MQTTOpts, jwt.TagList, - *OCSPConfig, map[string]string: + *OCSPConfig, map[string]string, JSLimitOpts: // explicitly skipped types default: // this will fail during unit tests @@ -1178,12 +1205,15 @@ func (s *Server) diffOptions(newOpts *Options) ([]option, error) { case "mqtt": diffOpts = append(diffOpts, &mqttAckWaitReload{newValue: newValue.(MQTTOpts).AckWait}) diffOpts = append(diffOpts, &mqttMaxAckPendingReload{newValue: newValue.(MQTTOpts).MaxAckPending}) + diffOpts = append(diffOpts, &mqttStreamReplicasReload{newValue: newValue.(MQTTOpts).StreamReplicas}) + diffOpts = append(diffOpts, &mqttConsumerReplicasReload{newValue: newValue.(MQTTOpts).ConsumerReplicas}) + diffOpts = append(diffOpts, &mqttConsumerMemoryStorageReload{newValue: newValue.(MQTTOpts).ConsumerMemoryStorage}) // Nil out/set to 0 the options that we allow to be reloaded so that // we only fail reload if some that we don't support are changed. tmpOld := oldValue.(MQTTOpts) tmpNew := newValue.(MQTTOpts) - tmpOld.TLSConfig, tmpOld.AckWait, tmpOld.MaxAckPending = nil, 0, 0 - tmpNew.TLSConfig, tmpNew.AckWait, tmpNew.MaxAckPending = nil, 0, 0 + tmpOld.TLSConfig, tmpOld.AckWait, tmpOld.MaxAckPending, tmpOld.StreamReplicas, tmpOld.ConsumerReplicas, tmpOld.ConsumerMemoryStorage = nil, 0, 0, 0, 0, false + tmpNew.TLSConfig, tmpNew.AckWait, tmpNew.MaxAckPending, tmpNew.StreamReplicas, tmpNew.ConsumerReplicas, tmpNew.ConsumerMemoryStorage = nil, 0, 0, 0, 0, false if !reflect.DeepEqual(tmpOld, tmpNew) { // See TODO(ik) note below about printing old/new values. return nil, fmt.Errorf("config reload not supported for %s: old=%v, new=%v", @@ -1191,6 +1221,9 @@ func (s *Server) diffOptions(newOpts *Options) ([]option, error) { } tmpNew.AckWait = newValue.(MQTTOpts).AckWait tmpNew.MaxAckPending = newValue.(MQTTOpts).MaxAckPending + tmpNew.StreamReplicas = newValue.(MQTTOpts).StreamReplicas + tmpNew.ConsumerReplicas = newValue.(MQTTOpts).ConsumerReplicas + tmpNew.ConsumerMemoryStorage = newValue.(MQTTOpts).ConsumerMemoryStorage case "connecterrorreports": diffOpts = append(diffOpts, &connectErrorReports{newValue: newValue.(int)}) case "reconnecterrorreports": @@ -1455,9 +1488,12 @@ func (s *Server) reloadAuthorization() { oldAccounts := make(map[string]*Account) s.accounts.Range(func(k, v interface{}) bool { acc := v.(*Account) - acc.mu.RLock() + acc.mu.Lock() oldAccounts[acc.Name] = acc - acc.mu.RUnlock() + // Need to clear out eventing timers since they close over this account and not the new one. + clearTimer(&acc.etmr) + clearTimer(&acc.ctmr) + acc.mu.Unlock() s.accounts.Delete(k) return true }) @@ -1479,11 +1515,23 @@ func (s *Server) reloadAuthorization() { newAcc.clients[c] = struct{}{} } } + // Same for leafnodes + newAcc.lleafs = append([]*client(nil), acc.lleafs...) newAcc.sl = acc.sl newAcc.rm = acc.rm + // Transfer internal client state. The configureAccounts call from above may have set up a new one. + // We need to use the old one, and the isid to not confuse internal subs. + newAcc.ic, newAcc.isid = acc.ic, acc.isid + // Transfer any JetStream state. newAcc.js = acc.js - + // Also transfer any internal accounting on different client types. We copy over all clients + // so need to copy this as well for proper accounting going forward. + newAcc.nrclients = acc.nrclients + newAcc.sysclients = acc.sysclients + newAcc.nleafs = acc.nleafs + newAcc.nrleafs = acc.nrleafs + // Process any reverse map entries. if len(acc.imports.rrMap) > 0 { newAcc.imports.rrMap = make(map[string][]*serviceRespEntry) for k, v := range acc.imports.rrMap { @@ -1546,8 +1594,6 @@ func (s *Server) reloadAuthorization() { } } - // Gather clients that changed accounts. We will close them and they - // will reconnect, doing the right thing. var ( cclientsa [64]*client cclients = cclientsa[:0] @@ -1556,6 +1602,9 @@ func (s *Server) reloadAuthorization() { routesa [64]*client routes = routesa[:0] ) + + // Gather clients that changed accounts. We will close them and they + // will reconnect, doing the right thing. for _, client := range s.clients { if s.clientHasMovedToDifferentAccount(client) { cclients = append(cclients, client) @@ -1566,6 +1615,26 @@ func (s *Server) reloadAuthorization() { for _, route := range s.routes { routes = append(routes, route) } + // Check here for any system/internal clients which will not be in the servers map of normal clients. + if s.sys != nil && s.sys.account != nil && !s.opts.NoSystemAccount { + s.accounts.Store(s.sys.account.Name, s.sys.account) + } + + s.accounts.Range(func(k, v interface{}) bool { + acc := v.(*Account) + acc.mu.RLock() + // Check for sysclients accounting, ignore the system account. + if acc.sysclients > 0 && (s.sys == nil || s.sys.account != acc) { + for c := range acc.clients { + if c.kind != CLIENT && c.kind != LEAF { + clients = append(clients, c) + } + } + } + acc.mu.RUnlock() + return true + }) + var resetCh chan struct{} if s.sys != nil { // can't hold the lock as go routine reading it may be waiting for lock as well @@ -1585,16 +1654,17 @@ func (s *Server) reloadAuthorization() { client.closeConnection(ClientClosed) } - for _, client := range clients { + for _, c := range clients { // Disconnect any unauthorized clients. - if !s.isClientAuthorized(client) { - client.authViolation() + // Ignore internal clients. + if (c.kind == CLIENT || c.kind == LEAF) && !s.isClientAuthorized(c) { + c.authViolation() continue } // Check to make sure account is correct. - client.swapAccountAfterReload() + c.swapAccountAfterReload() // Remove any unauthorized subscriptions and check for account imports. - client.processSubsOnConfigReload(awcsti) + c.processSubsOnConfigReload(awcsti) } for _, route := range routes { diff --git a/vendor/github.com/nats-io/nats-server/v2/server/route.go b/vendor/github.com/nats-io/nats-server/v2/server/route.go index 583c9881..aead8697 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/route.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/route.go @@ -582,7 +582,7 @@ func (c *client) processRouteInfo(info *Info) { c.mu.Unlock() // This is now an error and we close the connection. We need unique names for JetStream clustering. c.Errorf("Remote server has a duplicate name: %q", info.Name) - c.closeConnection(DuplicateRoute) + c.closeConnection(DuplicateServerName) return } @@ -1289,7 +1289,7 @@ func (s *Server) createRoute(conn net.Conn, rURL *url.URL) *client { } } - c := &client{srv: s, nc: conn, opts: ClientOpts{}, kind: ROUTER, msubs: -1, mpay: -1, route: r} + c := &client{srv: s, nc: conn, opts: ClientOpts{}, kind: ROUTER, msubs: -1, mpay: -1, route: r, start: time.Now()} // Grab server variables s.mu.Lock() @@ -2027,3 +2027,24 @@ func (s *Server) removeRoute(c *client) { s.removeFromTempClients(cid) s.mu.Unlock() } + +func (s *Server) isDuplicateServerName(name string) bool { + if name == _EMPTY_ { + return false + } + s.mu.Lock() + defer s.mu.Unlock() + + if s.info.Name == name { + return true + } + for _, r := range s.routes { + r.mu.Lock() + duplicate := r.route.remoteName == name + r.mu.Unlock() + if duplicate { + return true + } + } + return false +} diff --git a/vendor/github.com/nats-io/nats-server/v2/server/sendq.go b/vendor/github.com/nats-io/nats-server/v2/server/sendq.go index 35ff9456..49fcfb19 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/sendq.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/sendq.go @@ -32,7 +32,7 @@ type sendq struct { } func (s *Server) newSendQ() *sendq { - sq := &sendq{s: s, q: newIPQueue(ipQueue_Logger("Send", s.ipqLog))} + sq := &sendq{s: s, q: s.newIPQueue("SendQ")} s.startGoRoutine(sq.internalLoop) return sq } diff --git a/vendor/github.com/nats-io/nats-server/v2/server/server.go b/vendor/github.com/nats-io/nats-server/v2/server/server.go index a15301b9..09fc9c92 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/server.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/server.go @@ -164,6 +164,7 @@ type Server struct { } leafRemoteCfgs []*leafNodeCfg leafRemoteAccounts sync.Map + leafNodeEnabled bool quitCh chan struct{} shutdownComplete chan struct{} @@ -269,15 +270,24 @@ type Server struct { // Keep track of what that user name is for config reload purposes. sysAccOnlyNoAuthUser string - // This is a central logger for IPQueues when the number of pending - // messages reaches a certain thresold (per queue) - ipqLog *srvIPQueueLogger -} + // IPQueues map + ipQueues sync.Map + + // To limit logging frequency + rateLimitLogging sync.Map + rateLimitLoggingCh chan time.Duration + + // Total outstanding catchup bytes in flight. + gcbMu sync.RWMutex + gcbOut int64 + // A global chanel to kick out stalled catchup sequences. + gcbKick chan struct{} -type srvIPQueueLogger struct { - ch chan string - done chan struct{} - s *Server + // Total outbound syncRequests + syncOutSem chan struct{} + + // Queue to process JS API requests that come from routes (or gateways) + jsAPIRoutedReqs *ipQueue } // For tracking JS nodes. @@ -364,19 +374,28 @@ func NewServer(opts *Options) (*Server, error) { now := time.Now().UTC() s := &Server{ - kp: kp, - configFile: opts.ConfigFile, - info: info, - prand: rand.New(rand.NewSource(time.Now().UnixNano())), - opts: opts, - done: make(chan bool, 1), - start: now, - configTime: now, - gwLeafSubs: NewSublistWithCache(), - httpBasePath: httpBasePath, - eventIds: nuid.New(), - routesToSelf: make(map[string]struct{}), - httpReqStats: make(map[string]uint64), // Used to track HTTP requests + kp: kp, + configFile: opts.ConfigFile, + info: info, + prand: rand.New(rand.NewSource(time.Now().UnixNano())), + opts: opts, + done: make(chan bool, 1), + start: now, + configTime: now, + gwLeafSubs: NewSublistWithCache(), + httpBasePath: httpBasePath, + eventIds: nuid.New(), + routesToSelf: make(map[string]struct{}), + httpReqStats: make(map[string]uint64), // Used to track HTTP requests + rateLimitLoggingCh: make(chan time.Duration, 1), + leafNodeEnabled: opts.LeafNode.Port != 0 || len(opts.LeafNode.Remotes) > 0, + syncOutSem: make(chan struct{}, maxConcurrentSyncRequests), + } + + // Fill up the maximum in flight syncRequests for this server. + // Used in JetStream catchup semantics. + for i := 0; i < maxConcurrentSyncRequests; i++ { + s.syncOutSem <- struct{}{} } if opts.TLSRateLimit > 0 { @@ -533,39 +552,6 @@ func NewServer(opts *Options) (*Server, error) { return s, nil } -var semVerRe = regexp.MustCompile(`\Av?([0-9]+)\.?([0-9]+)?\.?([0-9]+)?`) - -func versionComponents(version string) (major, minor, patch int, err error) { - m := semVerRe.FindStringSubmatch(version) - if m == nil { - return 0, 0, 0, errors.New("invalid semver") - } - major, err = strconv.Atoi(m[1]) - if err != nil { - return -1, -1, -1, err - } - minor, err = strconv.Atoi(m[2]) - if err != nil { - return -1, -1, -1, err - } - patch, err = strconv.Atoi(m[3]) - if err != nil { - return -1, -1, -1, err - } - return major, minor, patch, err -} - -func versionAtLeast(version string, emajor, eminor, epatch int) bool { - major, minor, patch, err := versionComponents(version) - if err != nil { - return false - } - if major < emajor || minor < eminor || patch < epatch { - return false - } - return true -} - func (s *Server) logRejectedTLSConns() { defer s.grWG.Done() t := time.NewTicker(time.Second) @@ -1265,7 +1251,7 @@ func (s *Server) setSystemAccount(acc *Account) error { sid: 1, servers: make(map[string]*serverUpdate), replies: make(map[string]msgHandler), - sendq: newIPQueue(ipQueue_Logger("System send", s.ipqLog)), // of *pubMsg + sendq: s.newIPQueue("System sendQ"), // of *pubMsg resetCh: make(chan struct{}), sq: s.newSendQ(), statsz: eventsHBInterval, @@ -1397,7 +1383,7 @@ func (s *Server) registerAccountNoLock(acc *Account) *Account { acc.srv = s acc.updated = time.Now().UTC() accName := acc.Name - jsEnabled := acc.jsLimits != nil + jsEnabled := len(acc.jsLimits) > 0 acc.mu.Unlock() if opts := s.getOpts(); opts != nil && len(opts.JsAccDefaultDomain) > 0 { @@ -1607,16 +1593,20 @@ func (s *Server) Start() { s.Noticef("Starting nats-server") gc := gitCommit - if gc == "" { + if gc == _EMPTY_ { gc = "not set" } // Snapshot server options. opts := s.getOpts() + clusterName := s.ClusterName() s.Noticef(" Version: %s", VERSION) s.Noticef(" Git: [%s]", gc) s.Debugf(" Go build: %s", s.info.GoVersion) + if clusterName != _EMPTY_ { + s.Noticef(" Cluster: %s", clusterName) + } s.Noticef(" Name: %s", s.info.Name) if opts.JetStream { s.Noticef(" Node: %s", getHash(s.info.Name)) @@ -1637,7 +1627,7 @@ func (s *Server) Start() { s.grRunning = true s.grMu.Unlock() - s.startIPQLogger() + s.startRateLimitLogExpiration() // Pprof http endpoint for the profiler. if opts.ProfPort != 0 { @@ -1747,7 +1737,7 @@ func (s *Server) Start() { // own system account if one is not present. if opts.JetStream { // Make sure someone is not trying to enable on the system account. - if sa := s.SystemAccount(); sa != nil && sa.jsLimits != nil { + if sa := s.SystemAccount(); sa != nil && len(sa.jsLimits) > 0 { s.Fatalf("Not allowed to enable JetStream on the system account") } cfg := &JetStreamConfig{ @@ -1775,7 +1765,7 @@ func (s *Server) Start() { hasGlobal = true } acc.mu.RLock() - hasJs := acc.jsLimits != nil + hasJs := len(acc.jsLimits) > 0 acc.mu.RUnlock() if hasJs { s.checkJetStreamExports() @@ -1787,7 +1777,9 @@ func (s *Server) Start() { // go ahead and enable JS on $G in case we are in simple mixed mode setup. if total == 2 && hasSys && hasGlobal && !s.standAloneMode() { ga.mu.Lock() - ga.jsLimits = dynamicJSAccountLimits + ga.jsLimits = map[string]JetStreamAccountLimits{ + _EMPTY_: dynamicJSAccountLimits, + } ga.mu.Unlock() s.checkJetStreamExports() ga.enableAllJetStreamServiceImportsAndMappings() @@ -2007,11 +1999,6 @@ func (s *Server) Shutdown() { doneExpected-- } - // Stop the IPQueue logger (before the grWG.Wait() call) - if s.ipqLog != nil { - s.ipqLog.stop() - } - // Wait for go routines to be done. s.grWG.Wait() @@ -2263,6 +2250,7 @@ const ( AccountzPath = "/accountz" JszPath = "/jsz" HealthzPath = "/healthz" + IPQueuesPath = "/ipqueuesz" ) func (s *Server) basePath(p string) string { @@ -2294,9 +2282,11 @@ func (cl *captureHTTPServerLog) Write(p []byte) (int, error) { // we instruct the TLS handshake to ask for the tls configuration to be // used for a specific client. We don't care which client, we always use // the same TLS configuration. -func (s *Server) getTLSConfig(_ *tls.ClientHelloInfo) (*tls.Config, error) { +func (s *Server) getMonitoringTLSConfig(_ *tls.ClientHelloInfo) (*tls.Config, error) { opts := s.getOpts() - return opts.TLSConfig, nil + tc := opts.TLSConfig.Clone() + tc.ClientAuth = tls.NoClientCert + return tc, nil } // Start the monitoring server @@ -2321,7 +2311,7 @@ func (s *Server) startMonitoring(secure bool) error { } hp = net.JoinHostPort(opts.HTTPHost, strconv.Itoa(port)) config := opts.TLSConfig.Clone() - config.GetConfigForClient = s.getTLSConfig + config.GetConfigForClient = s.getMonitoringTLSConfig config.ClientAuth = tls.NoClientCert httpListener, err = tls.Listen("tcp", hp, config) @@ -2367,6 +2357,8 @@ func (s *Server) startMonitoring(secure bool) error { mux.HandleFunc(s.basePath(JszPath), s.HandleJsz) // Healthz mux.HandleFunc(s.basePath(HealthzPath), s.HandleHealthz) + // IPQueuesz + mux.HandleFunc(s.basePath(IPQueuesPath), s.HandleIPQueuesz) // Do not set a WriteTimeout because it could cause cURL/browser // to return empty response or unable to display page if the @@ -3391,7 +3383,7 @@ func (s *Server) lameDuckMode() { // If we are running any raftNodes transfer leaders. if hadTransfers := s.transferRaftLeaders(); hadTransfers { - // They will tranfer leadership quickly, but wait here for a second. + // They will transfer leadership quickly, but wait here for a second. select { case <-time.After(time.Second): case <-s.quitCh: @@ -3630,34 +3622,38 @@ func (s *Server) updateRemoteSubscription(acc *Account, sub *subscription, delta s.updateLeafNodes(acc, sub, delta) } -func (s *Server) startIPQLogger() { - s.ipqLog = &srvIPQueueLogger{ - ch: make(chan string, 128), - done: make(chan struct{}), - s: s, - } - s.startGoRoutine(s.ipqLog.run) -} +func (s *Server) startRateLimitLogExpiration() { + interval := time.Second + s.startGoRoutine(func() { + defer s.grWG.Done() -func (l *srvIPQueueLogger) stop() { - close(l.done) + ticker := time.NewTicker(time.Second) + defer ticker.Stop() + for { + select { + case <-s.quitCh: + return + case interval = <-s.rateLimitLoggingCh: + ticker.Reset(interval) + case <-ticker.C: + s.rateLimitLogging.Range(func(k, v interface{}) bool { + start := v.(time.Time) + if time.Since(start) >= interval { + s.rateLimitLogging.Delete(k) + } + return true + }) + } + } + }) } -func (l *srvIPQueueLogger) log(name string, pending int) { +func (s *Server) changeRateLimitLogInterval(d time.Duration) { + if d <= 0 { + return + } select { - case l.ch <- fmt.Sprintf("%s queue pending size: %v", name, pending): + case s.rateLimitLoggingCh <- d: default: } } - -func (l *srvIPQueueLogger) run() { - defer l.s.grWG.Done() - for { - select { - case w := <-l.ch: - l.s.Warnf("%s", w) - case <-l.done: - return - } - } -} diff --git a/vendor/github.com/nats-io/nats-server/v2/server/service_windows.go b/vendor/github.com/nats-io/nats-server/v2/server/service_windows.go index d24f6414..2920f74b 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/service_windows.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/service_windows.go @@ -109,11 +109,11 @@ func Run(server *Server) error { server.Start() return nil } - isInteractive, err := svc.IsAnInteractiveSession() + isWindowsService, err := svc.IsWindowsService() if err != nil { return err } - if isInteractive { + if !isWindowsService { server.Start() return nil } @@ -125,6 +125,6 @@ func isWindowsService() bool { if dockerized { return false } - isInteractive, _ := svc.IsAnInteractiveSession() - return !isInteractive + isWindowsService, _ := svc.IsWindowsService() + return isWindowsService } diff --git a/vendor/github.com/nats-io/nats-server/v2/server/store.go b/vendor/github.com/nats-io/nats-server/v2/server/store.go index f5131fe9..ed5786eb 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/store.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/store.go @@ -14,6 +14,7 @@ package server import ( + "encoding/binary" "encoding/json" "errors" "fmt" @@ -60,8 +61,20 @@ var ( ErrInvalidSequence = errors.New("invalid sequence") // ErrSequenceMismatch is returned when storing a raw message and the expected sequence is wrong. ErrSequenceMismatch = errors.New("expected sequence does not match store") + // ErrPurgeArgMismatch is returned when PurgeEx is called with sequence > 1 and keep > 0. + ErrPurgeArgMismatch = errors.New("sequence > 1 && keep > 0 not allowed") ) +// StoreMsg is the stored message format for messages that are retained by the Store layer. +type StoreMsg struct { + subj string + hdr []byte + msg []byte + buf []byte + seq uint64 + ts int64 +} + // Used to call back into the upper layers to report on changes in storage resources. // For the cases where its a single message we will also supply sequence number and subject. type StorageUpdateHandler func(msgs, bytes int64, seq uint64, subj string) @@ -70,9 +83,9 @@ type StreamStore interface { StoreMsg(subject string, hdr, msg []byte) (uint64, int64, error) StoreRawMsg(subject string, hdr, msg []byte, seq uint64, ts int64) error SkipMsg() uint64 - LoadMsg(seq uint64) (subject string, hdr, msg []byte, ts int64, err error) - LoadLastMsg(subject string) (subj string, seq uint64, hdr, msg []byte, ts int64, err error) - LoadNextMsg(filter string, wc bool, start uint64) (subject string, seq uint64, hdr, msg []byte, ts int64, err error) + LoadMsg(seq uint64, sm *StoreMsg) (*StoreMsg, error) + LoadNextMsg(filter string, wc bool, start uint64, smp *StoreMsg) (sm *StoreMsg, skip uint64, err error) + LoadLastMsg(subject string, sm *StoreMsg) (*StoreMsg, error) RemoveMsg(seq uint64) (bool, error) EraseMsg(seq uint64) (bool, error) Purge() (uint64, error) @@ -90,6 +103,8 @@ type StreamStore interface { Delete() error Stop() error ConsumerStore(name string, cfg *ConsumerConfig) (ConsumerStore, error) + AddConsumer(o ConsumerStore) error + RemoveConsumer(o ConsumerStore) error Snapshot(deadline time.Duration, includeConsumers, checkMsgs bool) (*SnapshotResult, error) Utilization() (total, reported uint64, err error) } @@ -155,11 +170,14 @@ type SnapshotResult struct { // ConsumerStore stores state on consumers for streams. type ConsumerStore interface { + SetStarting(sseq uint64) error + HasState() bool UpdateDelivered(dseq, sseq, dc uint64, ts int64) error UpdateAcks(dseq, sseq uint64) error UpdateConfig(cfg *ConsumerConfig) error Update(*ConsumerState) error State() (*ConsumerState, error) + EncodedState() ([]byte, error) Type() StorageType Stop() error Delete() error @@ -186,6 +204,68 @@ type ConsumerState struct { Redelivered map[uint64]uint64 `json:"redelivered,omitempty"` } +func encodeConsumerState(state *ConsumerState) []byte { + var hdr [seqsHdrSize]byte + var buf []byte + + maxSize := seqsHdrSize + if lp := len(state.Pending); lp > 0 { + maxSize += lp*(3*binary.MaxVarintLen64) + binary.MaxVarintLen64 + } + if lr := len(state.Redelivered); lr > 0 { + maxSize += lr*(2*binary.MaxVarintLen64) + binary.MaxVarintLen64 + } + if maxSize == seqsHdrSize { + buf = hdr[:seqsHdrSize] + } else { + buf = make([]byte, maxSize) + } + + // Write header + buf[0] = magic + buf[1] = 2 + + n := hdrLen + n += binary.PutUvarint(buf[n:], state.AckFloor.Consumer) + n += binary.PutUvarint(buf[n:], state.AckFloor.Stream) + n += binary.PutUvarint(buf[n:], state.Delivered.Consumer) + n += binary.PutUvarint(buf[n:], state.Delivered.Stream) + n += binary.PutUvarint(buf[n:], uint64(len(state.Pending))) + + asflr := state.AckFloor.Stream + adflr := state.AckFloor.Consumer + + // These are optional, but always write len. This is to avoid a truncate inline. + if len(state.Pending) > 0 { + // To save space we will use now rounded to seconds to be base timestamp. + mints := time.Now().Round(time.Second).Unix() + // Write minimum timestamp we found from above. + n += binary.PutVarint(buf[n:], mints) + + for k, v := range state.Pending { + n += binary.PutUvarint(buf[n:], k-asflr) + n += binary.PutUvarint(buf[n:], v.Sequence-adflr) + // Downsample to seconds to save on space. + // Subsecond resolution not needed for recovery etc. + ts := v.Timestamp / 1_000_000_000 + n += binary.PutVarint(buf[n:], mints-ts) + } + } + + // We always write the redelivered len. + n += binary.PutUvarint(buf[n:], uint64(len(state.Redelivered))) + + // We expect these to be small. + if len(state.Redelivered) > 0 { + for k, v := range state.Redelivered { + n += binary.PutUvarint(buf[n:], k-asflr) + n += binary.PutUvarint(buf[n:], v) + } + } + + return buf[:n] +} + // Represents a pending message for explicit ack or ack all. // Sequence is the original consumer sequence. type Pending struct { @@ -441,7 +521,7 @@ func (p DeliverPolicy) MarshalJSON() ([]byte, error) { } func isOutOfSpaceErr(err error) bool { - return err != nil && strings.Contains(err.Error(), "no space left") + return err != nil && (strings.Contains(err.Error(), "no space left")) } // For when our upper layer catchup detects its missing messages from the beginning of the stream. @@ -450,3 +530,25 @@ var errFirstSequenceMismatch = errors.New("first sequence mismatch") func isClusterResetErr(err error) bool { return err == errLastSeqMismatch || err == ErrStoreEOF || err == errFirstSequenceMismatch } + +// Copy all fields. +func (smo *StoreMsg) copy(sm *StoreMsg) { + if sm.buf != nil { + sm.buf = sm.buf[:0] + } + sm.buf = append(sm.buf, smo.buf...) + // We set cap on header in case someone wants to expand it. + sm.hdr, sm.msg = sm.buf[:len(smo.hdr):len(smo.hdr)], sm.buf[len(smo.hdr):] + sm.subj, sm.seq, sm.ts = smo.subj, smo.seq, smo.ts +} + +// Clear all fields except underlying buffer but reset that if present to [:0]. +func (sm *StoreMsg) clear() { + if sm == nil { + return + } + *sm = StoreMsg{_EMPTY_, nil, nil, sm.buf, 0, 0} + if len(sm.buf) > 0 { + sm.buf = sm.buf[:0] + } +} diff --git a/vendor/github.com/nats-io/nats-server/v2/server/stream.go b/vendor/github.com/nats-io/nats-server/v2/server/stream.go index 20c25f02..6b547ad5 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/stream.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/stream.go @@ -22,6 +22,7 @@ import ( "io" "io/ioutil" "math" + "math/rand" "os" "path/filepath" "reflect" @@ -57,6 +58,9 @@ type StreamConfig struct { Mirror *StreamSource `json:"mirror,omitempty"` Sources []*StreamSource `json:"sources,omitempty"` + // Allow republish of the message after being sequenced and stored. + RePublish *SubjectMapping `json:"republish,omitempty"` + // Optional qualifiers. These can not be modified after set to true. // Sealed will seal a stream so no messages can get out or in. @@ -70,6 +74,12 @@ type StreamConfig struct { AllowRollup bool `json:"allow_rollup_hdrs"` } +// SubjectMapping allows a source subject to be mapped to a destination subject for republishing. +type SubjectMapping struct { + Source string `json:"src,omitempty"` + Destination string `json:"dest"` +} + // JSPubAckResponse is a formal response to a publish operation. type JSPubAckResponse struct { Error *ApiError `json:"error,omitempty"` @@ -96,13 +106,20 @@ type PubAck struct { // StreamInfo shows config and current state for this stream. type StreamInfo struct { - Config StreamConfig `json:"config"` - Created time.Time `json:"created"` - State StreamState `json:"state"` - Domain string `json:"domain,omitempty"` - Cluster *ClusterInfo `json:"cluster,omitempty"` - Mirror *StreamSourceInfo `json:"mirror,omitempty"` - Sources []*StreamSourceInfo `json:"sources,omitempty"` + Config StreamConfig `json:"config"` + Created time.Time `json:"created"` + State StreamState `json:"state"` + Domain string `json:"domain,omitempty"` + Cluster *ClusterInfo `json:"cluster,omitempty"` + Mirror *StreamSourceInfo `json:"mirror,omitempty"` + Sources []*StreamSourceInfo `json:"sources,omitempty"` + Alternates []StreamAlternate `json:"alternates,omitempty"` +} + +type StreamAlternate struct { + Name string `json:"name"` + Domain string `json:"domain,omitempty"` + Cluster string `json:"cluster"` } // ClusterInfo shows information about the underlying set of servers @@ -121,6 +138,9 @@ type PeerInfo struct { Offline bool `json:"offline,omitempty"` Active time.Duration `json:"active"` Lag uint64 `json:"lag,omitempty"` + // For migrations. + cluster string + peer string } // StreamSourceInfo shows information about an upstream stream source. @@ -173,6 +193,7 @@ type stream struct { cfg StreamConfig created time.Time stype StorageType + tier string ddmap map[string]*ddentry ddarr []*ddentry ddindex int @@ -190,6 +211,9 @@ type stream struct { // Indicates we have direct consumers. directs int + // For republishing. + tr *transform + // TODO(dlc) - Hide everything below behind two pointers. // Clustered mode. sa *streamAssignment @@ -203,6 +227,7 @@ type stream struct { leader string lqsent time.Time catchups map[string]uint64 + uch chan struct{} } type sourceInfo struct { @@ -213,12 +238,14 @@ type sourceInfo struct { msgs *ipQueue // of *inMsg sseq uint64 dseq uint64 + start time.Time lag uint64 err *ApiError last time.Time lreq time.Time qch chan struct{} - grr bool + sip bool // setup in progress + wg sync.WaitGroup } // Headers for published messages. @@ -237,6 +264,13 @@ const ( JSResponseType = "Nats-Response-Type" ) +// Headers for republished messages. +const ( + JSStream = "Nats-Stream" + JSSequence = "Nats-Sequence" + JSLastSequence = "Nats-Last-Sequence" +) + // Rollups, can be subject only or all messages. const ( JSMsgRollupSubject = "sub" @@ -283,14 +317,9 @@ func (a *Account) addStreamWithAssignment(config *StreamConfig, fsConfig *FileSt } // Sensible defaults. - cfg, err := checkStreamCfg(config) - if err != nil { - return nil, NewJSStreamInvalidConfigError(err, Unless(err)) - } - - // Can't create a stream with a sealed state. - if cfg.Sealed { - return nil, NewJSStreamInvalidConfigError(fmt.Errorf("stream configuration for create can not be sealed")) + cfg, apiErr := s.checkStreamCfg(config, a) + if apiErr != nil { + return nil, apiErr } singleServerMode := !s.JetStreamIsClustered() && s.standAloneMode() @@ -298,10 +327,10 @@ func (a *Account) addStreamWithAssignment(config *StreamConfig, fsConfig *FileSt return nil, ApiErrors[JSStreamReplicasNotSupportedErr] } - jsa.mu.Lock() - js := jsa.js + js, isClustered := jsa.jetStreamAndClustered() + jsa.mu.RLock() if mset, ok := jsa.streams[cfg.Name]; ok { - jsa.mu.Unlock() + jsa.mu.RUnlock() // Check to see if configs are same. ocfg := mset.config() if reflect.DeepEqual(ocfg, cfg) { @@ -313,11 +342,27 @@ func (a *Account) addStreamWithAssignment(config *StreamConfig, fsConfig *FileSt return nil, ApiErrors[JSStreamNameExistErr] } } - // Check for account and server limits. - if err := jsa.checkAllLimits(&cfg); err != nil { - jsa.mu.Unlock() + jsa.usageMu.RLock() + selected, tier, hasTier := jsa.selectLimits(&cfg) + jsa.usageMu.RUnlock() + reserved := int64(0) + if !isClustered { + reserved = jsa.tieredReservation(tier, &cfg) + } + jsa.mu.RUnlock() + if !hasTier { + return nil, NewJSNoLimitsError() + } + js.mu.RLock() + if isClustered { + _, reserved = tieredStreamAndReservationCount(js.cluster.streams[a.Name], tier, &cfg) + } + if err := js.checkAllLimits(&selected, &cfg, reserved, 0); err != nil { + js.mu.RUnlock() return nil, err } + js.mu.RUnlock() + jsa.mu.Lock() // Check for template ownership if present. if cfg.Template != _EMPTY_ && jsa.account != nil { if !jsa.checkTemplateOwnership(cfg.Template, cfg.Name) { @@ -326,30 +371,6 @@ func (a *Account) addStreamWithAssignment(config *StreamConfig, fsConfig *FileSt } } - // Check for mirror designation. - if cfg.Mirror != nil { - // Can't have subjects. - if len(cfg.Subjects) > 0 { - jsa.mu.Unlock() - return nil, fmt.Errorf("stream mirrors can not also contain subjects") - } - if len(cfg.Sources) > 0 { - jsa.mu.Unlock() - return nil, fmt.Errorf("stream mirrors can not also contain other sources") - } - if cfg.Mirror.FilterSubject != _EMPTY_ { - jsa.mu.Unlock() - return nil, fmt.Errorf("stream mirrors can not contain filtered subjects") - } - if cfg.Mirror.OptStartSeq > 0 && cfg.Mirror.OptStartTime != nil { - jsa.mu.Unlock() - return nil, fmt.Errorf("stream mirrors can not have both start seq and start time configured") - } - } else if len(cfg.Subjects) == 0 && len(cfg.Sources) == 0 { - jsa.mu.Unlock() - return nil, fmt.Errorf("stream needs at least one configured subject or mirror") - } - // Setup our internal indexed names here for sources. if len(cfg.Sources) > 0 { for _, ssi := range cfg.Sources { @@ -363,11 +384,16 @@ func (a *Account) addStreamWithAssignment(config *StreamConfig, fsConfig *FileSt return nil, fmt.Errorf("subjects overlap with an existing stream") } + if !hasTier { + jsa.mu.Unlock() + return nil, fmt.Errorf("no applicable tier found") + } + // Setup the internal clients. c := s.createInternalJetStreamClient() ic := s.createInternalJetStreamClient() - qname := fmt.Sprintf("Stream %s > %s messages", a.Name, config.Name) + qpfx := fmt.Sprintf("[ACC:%s] stream '%s' ", a.Name, config.Name) mset := &stream{ acc: a, jsa: jsa, @@ -376,15 +402,28 @@ func (a *Account) addStreamWithAssignment(config *StreamConfig, fsConfig *FileSt srv: s, client: c, sysc: ic, + tier: tier, stype: cfg.Storage, consumers: make(map[string]*consumer), - msgs: newIPQueue(ipQueue_Logger(qname, s.ipqLog)), // of *inMsg + msgs: s.newIPQueue(qpfx + "messages"), // of *inMsg qch: make(chan struct{}), + uch: make(chan struct{}, 4), } // For no-ack consumers when we are interest retention. if cfg.Retention != LimitsPolicy { - mset.ackq = newIPQueue() // of uint64 + mset.ackq = s.newIPQueue(qpfx + "acks") // of uint64 + } + + // Check for RePublish. + if cfg.RePublish != nil { + tr, err := newTransform(cfg.RePublish.Source, cfg.RePublish.Destination) + if err != nil { + jsa.mu.Unlock() + return nil, fmt.Errorf("stream configuration for republish not valid") + } + // Assign our transform for republishing. + mset.tr = tr } jsa.streams[cfg.Name] = mset @@ -427,7 +466,11 @@ func (a *Account) addStreamWithAssignment(config *StreamConfig, fsConfig *FileSt // Set our known last sequence. var state StreamState mset.store.FastState(&state) + + // Possible race with consumer.setLeader during recovery. + mset.mu.Lock() mset.lseq = state.LastSeq + mset.mu.Unlock() // If no msgs (new stream), set dedupe state loaded to true. if state.Msgs == 0 { @@ -496,6 +539,7 @@ func (mset *stream) streamAssignment() *streamAssignment { func (mset *stream) setStreamAssignment(sa *streamAssignment) { mset.mu.Lock() defer mset.mu.Unlock() + mset.sa = sa if sa == nil { return @@ -503,6 +547,9 @@ func (mset *stream) setStreamAssignment(sa *streamAssignment) { // Set our node. mset.node = sa.Group.node + if mset.node != nil { + mset.node.UpdateKnownPeers(sa.Group.Peers) + } // Setup our info sub here as well for all stream members. This is now by design. if mset.infoSub == nil { @@ -510,6 +557,21 @@ func (mset *stream) setStreamAssignment(sa *streamAssignment) { // Note below the way we subscribe here is so that we can send requests to ourselves. mset.infoSub, _ = mset.srv.systemSubscribe(isubj, _EMPTY_, false, mset.sysc, mset.handleClusterStreamInfoRequest) } + + // Trigger update chan. + select { + case mset.uch <- struct{}{}: + default: + } +} + +func (mset *stream) updateC() <-chan struct{} { + if mset == nil { + return nil + } + mset.mu.RLock() + defer mset.mu.RUnlock() + return mset.uch } // IsLeader will return if we are the current leader. @@ -557,6 +619,8 @@ func (mset *stream) setLeader(isLeader bool) error { // Track group leader. if mset.isClustered() { mset.leader = mset.node.GroupLeader() + } else { + mset.leader = _EMPTY_ } mset.mu.Unlock() return nil @@ -647,11 +711,12 @@ func (mset *stream) autoTuneFileStorageBlockSize(fsCfg *FileStoreConfig) { if m := blkSize % 100; m != 0 { blkSize += 100 - m } - if blkSize < FileStoreMinBlkSize { + if blkSize <= FileStoreMinBlkSize { blkSize = FileStoreMinBlkSize - } - if blkSize > FileStoreMaxBlkSize { + } else if blkSize >= FileStoreMaxBlkSize { blkSize = FileStoreMaxBlkSize + } else { + blkSize = defaultMediumBlockSize } fsCfg.BlockSize = uint64(blkSize) } @@ -662,6 +727,10 @@ func (mset *stream) autoTuneFileStorageBlockSize(fsCfg *FileStoreConfig) { // headers and msgId in them. Would need signaling from the storage layer. // Lock should be held. func (mset *stream) rebuildDedupe() { + if mset.ddloaded { + return + } + mset.ddloaded = true // We have some messages. Lookup starting sequence by duplicate time window. @@ -670,13 +739,17 @@ func (mset *stream) rebuildDedupe() { return } + var smv StoreMsg state := mset.store.State() for seq := sseq; seq <= state.LastSeq; seq++ { - _, hdr, _, ts, err := mset.store.LoadMsg(seq) + sm, err := mset.store.LoadMsg(seq, &smv) + if err != nil { + continue + } var msgId string - if err == nil && len(hdr) > 0 { - if msgId = getMsgId(hdr); msgId != _EMPTY_ { - mset.storeMsgIdLocked(&ddentry{msgId, seq, ts}) + if len(sm.hdr) > 0 { + if msgId = getMsgId(sm.hdr); msgId != _EMPTY_ { + mset.storeMsgIdLocked(&ddentry{msgId, sm.seq, sm.ts}) } } if seq == state.LastSeq { @@ -812,18 +885,20 @@ func (jsa *jsAccount) subjectsOverlap(subjects []string) bool { // StreamDefaultDuplicatesWindow default duplicates window. const StreamDefaultDuplicatesWindow = 2 * time.Minute -func checkStreamCfg(config *StreamConfig) (StreamConfig, error) { +func (s *Server) checkStreamCfg(config *StreamConfig, acc *Account) (StreamConfig, *ApiError) { + lim := &s.getOpts().JetStreamLimits + if config == nil { - return StreamConfig{}, fmt.Errorf("stream configuration invalid") + return StreamConfig{}, NewJSStreamInvalidConfigError(fmt.Errorf("stream configuration invalid")) } if !isValidName(config.Name) { - return StreamConfig{}, fmt.Errorf("stream name is required and can not contain '.', '*', '>'") + return StreamConfig{}, NewJSStreamInvalidConfigError(fmt.Errorf("stream name is required and can not contain '.', '*', '>'")) } if len(config.Name) > JSMaxNameLen { - return StreamConfig{}, fmt.Errorf("stream name is too long, maximum allowed is %d", JSMaxNameLen) + return StreamConfig{}, NewJSStreamInvalidConfigError(fmt.Errorf("stream name is too long, maximum allowed is %d", JSMaxNameLen)) } if len(config.Description) > JSMaxDescriptionLen { - return StreamConfig{}, fmt.Errorf("stream description is too long, maximum allowed is %d", JSMaxDescriptionLen) + return StreamConfig{}, NewJSStreamInvalidConfigError(fmt.Errorf("stream description is too long, maximum allowed is %d", JSMaxDescriptionLen)) } cfg := *config @@ -836,7 +911,7 @@ func checkStreamCfg(config *StreamConfig) (StreamConfig, error) { cfg.Replicas = 1 } if cfg.Replicas > StreamMaxReplicas { - return cfg, fmt.Errorf("maximum replicas is %d", StreamMaxReplicas) + return cfg, NewJSStreamInvalidConfigError(fmt.Errorf("maximum replicas is %d", StreamMaxReplicas)) } if cfg.MaxMsgs == 0 { cfg.MaxMsgs = -1 @@ -853,23 +928,175 @@ func checkStreamCfg(config *StreamConfig) (StreamConfig, error) { if cfg.MaxConsumers == 0 { cfg.MaxConsumers = -1 } - if cfg.Duplicates == 0 { - if cfg.MaxAge != 0 && cfg.MaxAge < StreamDefaultDuplicatesWindow { + if cfg.Duplicates == 0 && cfg.Mirror == nil { + maxWindow := StreamDefaultDuplicatesWindow + if lim.Duplicates > 0 && maxWindow > lim.Duplicates { + maxWindow = lim.Duplicates + } + if cfg.MaxAge != 0 && cfg.MaxAge < maxWindow { cfg.Duplicates = cfg.MaxAge } else { - cfg.Duplicates = StreamDefaultDuplicatesWindow + cfg.Duplicates = maxWindow } } + if cfg.MaxAge > 0 && cfg.MaxAge < 100*time.Millisecond { + return StreamConfig{}, NewJSStreamInvalidConfigError(fmt.Errorf("max age needs to be >= 100ms")) + } if cfg.Duplicates < 0 { - return StreamConfig{}, fmt.Errorf("duplicates window can not be negative") + return StreamConfig{}, NewJSStreamInvalidConfigError(fmt.Errorf("duplicates window can not be negative")) } // Check that duplicates is not larger then age if set. if cfg.MaxAge != 0 && cfg.Duplicates > cfg.MaxAge { - return StreamConfig{}, fmt.Errorf("duplicates window can not be larger then max age") + return StreamConfig{}, NewJSStreamInvalidConfigError(fmt.Errorf("duplicates window can not be larger then max age")) + } + if lim.Duplicates > 0 && cfg.Duplicates > lim.Duplicates { + return StreamConfig{}, NewJSStreamInvalidConfigError(fmt.Errorf("duplicates window can not be larger then server limit of %v", + lim.Duplicates.String())) + } + if cfg.Duplicates > 0 && cfg.Duplicates < 100*time.Millisecond { + return StreamConfig{}, NewJSStreamInvalidConfigError(fmt.Errorf("duplicates window needs to be >= 100ms")) } if cfg.DenyPurge && cfg.AllowRollup { - return StreamConfig{}, fmt.Errorf("roll-ups require the purge permission") + return StreamConfig{}, NewJSStreamInvalidConfigError(fmt.Errorf("roll-ups require the purge permission")) + } + + getStream := func(streamName string) (bool, StreamConfig) { + var exists bool + var cfg StreamConfig + if s.JetStreamIsClustered() { + if js, _ := s.getJetStreamCluster(); js != nil { + js.mu.RLock() + if sa := js.streamAssignment(acc.Name, streamName); sa != nil { + cfg = *sa.Config + exists = true + } + js.mu.RUnlock() + } + } else if mset, err := acc.lookupStream(streamName); err == nil { + cfg = mset.cfg + exists = true + } + return exists, cfg + } + hasStream := func(streamName string) (bool, int32, []string) { + exists, cfg := getStream(streamName) + return exists, cfg.MaxMsgSize, cfg.Subjects + } + + var streamSubs []string + var deliveryPrefixes []string + var apiPrefixes []string + + // Do some pre-checking for mirror config to avoid cycles in clustered mode. + if cfg.Mirror != nil { + if len(cfg.Subjects) > 0 { + return StreamConfig{}, NewJSMirrorWithSubjectsError() + + } + if len(cfg.Sources) > 0 { + return StreamConfig{}, NewJSMirrorWithSourcesError() + } + if cfg.Mirror.FilterSubject != _EMPTY_ { + return StreamConfig{}, NewJSMirrorWithSubjectFiltersError() + } + if cfg.Mirror.OptStartSeq > 0 && cfg.Mirror.OptStartTime != nil { + return StreamConfig{}, NewJSMirrorWithStartSeqAndTimeError() + } + if cfg.Duplicates != time.Duration(0) { + return StreamConfig{}, NewJSStreamInvalidConfigError( + errors.New("stream mirrors do not make use of a de-duplication window")) + } + // We do not require other stream to exist anymore, but if we can see it check payloads. + exists, maxMsgSize, subs := hasStream(cfg.Mirror.Name) + if len(subs) > 0 { + streamSubs = append(streamSubs, subs...) + } + if exists && cfg.MaxMsgSize > 0 && maxMsgSize > 0 && cfg.MaxMsgSize < maxMsgSize { + return StreamConfig{}, NewJSMirrorMaxMessageSizeTooBigError() + } + if cfg.Mirror.External != nil { + if cfg.Mirror.External.DeliverPrefix != _EMPTY_ { + deliveryPrefixes = append(deliveryPrefixes, cfg.Mirror.External.DeliverPrefix) + } + if cfg.Mirror.External.ApiPrefix != _EMPTY_ { + apiPrefixes = append(apiPrefixes, cfg.Mirror.External.ApiPrefix) + } + } + } + if len(cfg.Sources) > 0 { + for _, src := range cfg.Sources { + if src.External == nil { + continue + } + exists, maxMsgSize, subs := hasStream(src.Name) + if len(subs) > 0 { + streamSubs = append(streamSubs, subs...) + } + if src.External.DeliverPrefix != _EMPTY_ { + deliveryPrefixes = append(deliveryPrefixes, src.External.DeliverPrefix) + } + if src.External.ApiPrefix != _EMPTY_ { + apiPrefixes = append(apiPrefixes, src.External.ApiPrefix) + } + if exists && cfg.MaxMsgSize > 0 && maxMsgSize > 0 && cfg.MaxMsgSize < maxMsgSize { + return StreamConfig{}, NewJSSourceMaxMessageSizeTooBigError() + } + } + } + // check prefix overlap with subjects + for _, pfx := range deliveryPrefixes { + if !IsValidPublishSubject(pfx) { + return StreamConfig{}, NewJSStreamInvalidExternalDeliverySubjError(pfx) + } + for _, sub := range streamSubs { + if SubjectsCollide(sub, fmt.Sprintf("%s.%s", pfx, sub)) { + return StreamConfig{}, NewJSStreamExternalDelPrefixOverlapsError(pfx, sub) + } + } + } + // check if api prefixes overlap + for _, apiPfx := range apiPrefixes { + if !IsValidPublishSubject(apiPfx) { + return StreamConfig{}, NewJSStreamInvalidConfigError( + fmt.Errorf("stream external api prefix %q must be a valid subject without wildcards", apiPfx)) + } + if SubjectsCollide(apiPfx, JSApiPrefix) { + return StreamConfig{}, NewJSStreamExternalApiOverlapError(apiPfx, JSApiPrefix) + } + } + + // cycle check for source cycle + toVisit := []*StreamConfig{&cfg} + visited := make(map[string]struct{}) + for len(toVisit) > 0 { + cfg := toVisit[0] + toVisit = toVisit[1:] + visited[cfg.Name] = struct{}{} + for _, src := range cfg.Sources { + if src.External != nil { + // TODO (mh) look up service imports and see if src.External.ApiPrefix returns an account + // this will be much easier without the delivery subject + continue + } + if _, ok := visited[src.Name]; ok { + return StreamConfig{}, NewJSStreamInvalidConfigError(errors.New("detected cycle")) + } + if exists, cfg := getStream(src.Name); exists { + toVisit = append(toVisit, &cfg) + } + } + // Avoid cycles hiding behind mirrors + if m := cfg.Mirror; m != nil { + if m.External == nil { + if _, ok := visited[m.Name]; ok { + return StreamConfig{}, NewJSStreamInvalidConfigError(errors.New("detected cycle")) + } + if exists, cfg := getStream(m.Name); exists { + toVisit = append(toVisit, &cfg) + } + } + } } if len(cfg.Subjects) == 0 { @@ -878,27 +1105,69 @@ func checkStreamCfg(config *StreamConfig) (StreamConfig, error) { } } else { if cfg.Mirror != nil { - return StreamConfig{}, fmt.Errorf("stream mirrors may not have subjects") + return StreamConfig{}, NewJSMirrorWithSubjectsError() } // We can allow overlaps, but don't allow direct duplicates. dset := make(map[string]struct{}, len(cfg.Subjects)) for _, subj := range cfg.Subjects { if _, ok := dset[subj]; ok { - return StreamConfig{}, fmt.Errorf("duplicate subjects detected") + return StreamConfig{}, NewJSStreamInvalidConfigError(fmt.Errorf("duplicate subjects detected")) } // Also check to make sure we do not overlap with our $JS API subjects. if subjectIsSubsetMatch(subj, "$JS.API.>") { - return StreamConfig{}, fmt.Errorf("subjects overlap with jetstream api") + return StreamConfig{}, NewJSStreamInvalidConfigError(fmt.Errorf("subjects overlap with jetstream api")) } // Make sure the subject is valid. if !IsValidSubject(subj) { - return StreamConfig{}, fmt.Errorf("invalid subject") + return StreamConfig{}, NewJSStreamInvalidConfigError(fmt.Errorf("invalid subject")) } // Mark for duplicate check. dset[subj] = struct{}{} } } + + if len(cfg.Subjects) == 0 && len(cfg.Sources) == 0 && cfg.Mirror == nil { + return StreamConfig{}, NewJSStreamInvalidConfigError( + fmt.Errorf("stream needs at least one configured subject or be a source/mirror")) + } + + // Check for MaxBytes required and it's limit + if required, limit := acc.maxBytesLimits(&cfg); required && cfg.MaxBytes <= 0 { + return StreamConfig{}, NewJSStreamMaxBytesRequiredError() + } else if limit > 0 && cfg.MaxBytes > limit { + return StreamConfig{}, NewJSStreamMaxStreamBytesExceededError() + } + + // If we have a republish directive check if we can create a transform here. + if cfg.RePublish != nil { + // Check to make sure source is a valid subset of the subjects we have. + // Also make sure it does not form a cycle. + var srcValid bool + for _, subj := range cfg.Subjects { + if SubjectsCollide(cfg.RePublish.Source, subj) { + srcValid = true + break + } + } + if !srcValid { + return StreamConfig{}, NewJSStreamInvalidConfigError(fmt.Errorf("stream configuration for republish source is not valid subset of subjects")) + } + var formsCycle bool + for _, subj := range cfg.Subjects { + if SubjectsCollide(cfg.RePublish.Destination, subj) { + formsCycle = true + break + } + } + if formsCycle { + return StreamConfig{}, NewJSStreamInvalidConfigError(fmt.Errorf("stream configuration for republish destination forms a cycle")) + } + if _, err := newTransform(cfg.RePublish.Source, cfg.RePublish.Destination); err != nil { + return StreamConfig{}, NewJSStreamInvalidConfigError(fmt.Errorf("stream configuration for republish not valid")) + } + } + return cfg, nil } @@ -919,10 +1188,11 @@ func (mset *stream) fileStoreConfig() (FileStoreConfig, error) { return fs.fileStoreConfig(), nil } -func (jsa *jsAccount) configUpdateCheck(old, new *StreamConfig) (*StreamConfig, error) { - cfg, err := checkStreamCfg(new) - if err != nil { - return nil, NewJSStreamInvalidConfigError(err, Unless(err)) +// Do not hold jsAccount or jetStream lock +func (jsa *jsAccount) configUpdateCheck(old, new *StreamConfig, s *Server) (*StreamConfig, error) { + cfg, apiErr := s.checkStreamCfg(new, jsa.acc()) + if apiErr != nil { + return nil, apiErr } // Name must match. @@ -969,17 +1239,74 @@ func (jsa *jsAccount) configUpdateCheck(old, new *StreamConfig) (*StreamConfig, cfg.AllowRollup = false } + // Check limits. We need some extra handling to allow updating MaxBytes. + + // First, let's calculate the difference between the new and old MaxBytes. + maxBytesDiff := cfg.MaxBytes - old.MaxBytes + if maxBytesDiff < 0 { + // If we're updating to a lower MaxBytes (maxBytesDiff is negative), + // then set to zero so checkBytesLimits doesn't set addBytes to 1. + maxBytesDiff = 0 + } + // If maxBytesDiff == 0, then that means MaxBytes didn't change. + // If maxBytesDiff > 0, then we want to reserve additional bytes. + + // Save the user configured MaxBytes. + newMaxBytes := cfg.MaxBytes + + maxBytesOffset := int64(0) + if old.MaxBytes > 0 { + if excessRep := cfg.Replicas - old.Replicas; excessRep > 0 { + maxBytesOffset = old.MaxBytes * int64(excessRep) + } + } + + // We temporarily set cfg.MaxBytes to maxBytesDiff because checkAllLimits + // adds cfg.MaxBytes to the current reserved limit and checks if we've gone + // over. However, we don't want an addition cfg.MaxBytes, we only want to + // reserve the difference between the new and the old values. + cfg.MaxBytes = maxBytesDiff + // Check limits. - if err := jsa.checkAllLimits(&cfg); err != nil { + js, isClustered := jsa.jetStreamAndClustered() + jsa.mu.RLock() + acc := jsa.account + jsa.usageMu.RLock() + selected, tier, hasTier := jsa.selectLimits(&cfg) + if !hasTier && old.Replicas != cfg.Replicas { + selected, tier, hasTier = jsa.selectLimits(old) + } + jsa.usageMu.RUnlock() + reserved := int64(0) + if !isClustered { + reserved = jsa.tieredReservation(tier, &cfg) + } + jsa.mu.RUnlock() + if !hasTier { + return nil, NewJSNoLimitsError() + } + js.mu.RLock() + defer js.mu.RUnlock() + if isClustered { + _, reserved = tieredStreamAndReservationCount(js.cluster.streams[acc.Name], tier, &cfg) + } + // reservation does not account for this stream, hence add the old value + reserved += int64(old.Replicas) * old.MaxBytes + if err := js.checkAllLimits(&selected, &cfg, reserved, maxBytesOffset); err != nil { return nil, err } + // Restore the user configured MaxBytes. + cfg.MaxBytes = newMaxBytes return &cfg, nil } // Update will allow certain configuration properties of an existing stream to be updated. func (mset *stream) update(config *StreamConfig) error { - ocfg := mset.config() - cfg, err := mset.jsa.configUpdateCheck(&ocfg, config) + mset.mu.RLock() + ocfg := mset.cfg + s := mset.srv + mset.mu.RUnlock() + cfg, err := mset.jsa.configUpdateCheck(&ocfg, config, s) if err != nil { return NewJSStreamInvalidConfigError(err, Unless(err)) } @@ -1022,7 +1349,7 @@ func (mset *stream) update(config *StreamConfig) error { if len(cfg.Sources) > 0 || len(ocfg.Sources) > 0 { current := make(map[string]struct{}) for _, s := range ocfg.Sources { - current[s.Name] = struct{}{} + current[s.iname] = struct{}{} } for _, s := range cfg.Sources { s.setIndexName() @@ -1031,12 +1358,12 @@ func (mset *stream) update(config *StreamConfig) error { mset.sources = make(map[string]*sourceInfo) } mset.cfg.Sources = append(mset.cfg.Sources, s) - si := &sourceInfo{name: s.Name, iname: s.iname, msgs: newIPQueue() /* of *inMsg */} + si := &sourceInfo{name: s.Name, iname: s.iname} mset.sources[s.iname] = si mset.setStartingSequenceForSource(s.iname) mset.setSourceConsumer(s.iname, si.sseq+1) } - delete(current, s.Name) + delete(current, s.iname) } // What is left in current needs to be deleted. for iname := range current { @@ -1046,15 +1373,50 @@ func (mset *stream) update(config *StreamConfig) error { } } + js := mset.js + + if targetTier := tierName(cfg); mset.tier != targetTier { + // In cases such as R1->R3, only one update is needed + mset.jsa.usageMu.RLock() + _, ok := mset.jsa.limits[targetTier] + mset.jsa.usageMu.RUnlock() + if ok { + // error never set + _, reported, _ := mset.store.Utilization() + mset.jsa.updateUsage(mset.tier, mset.stype, -int64(reported)) + mset.jsa.updateUsage(targetTier, mset.stype, int64(reported)) + mset.tier = targetTier + } + // else in case the new tier does not exist (say on move), keep the old tier around + // a subsequent update to an existing tier will then move from existing past tier to existing new tier + } + // Now update config and store's version of our config. mset.cfg = *cfg - // If we are the leader never suppres update advisory, simply send. + // If we are the leader never suppress update advisory, simply send. if mset.isLeader() { mset.sendUpdateAdvisoryLocked() } mset.mu.Unlock() + if js != nil { + maxBytesDiff := cfg.MaxBytes - ocfg.MaxBytes + if maxBytesDiff > 0 { + // Reserve the difference + js.reserveStreamResources(&StreamConfig{ + MaxBytes: maxBytesDiff, + Storage: cfg.Storage, + }) + } else if maxBytesDiff < 0 { + // Release the difference + js.releaseStreamResources(&StreamConfig{ + MaxBytes: -maxBytesDiff, + Storage: ocfg.Storage, + }) + } + } + mset.store.UpdateConfig(cfg) return nil @@ -1093,6 +1455,7 @@ func (mset *stream) purge(preq *JSApiStreamPurgeRequest) (purged uint64, err err var state StreamState mset.store.FastState(&state) fseq := state.FirstSeq + lseq := state.LastSeq // Check for filtered purge. if preq != nil && preq.Subject != _EMPTY_ { @@ -1101,7 +1464,7 @@ func (mset *stream) purge(preq *JSApiStreamPurgeRequest) (purged uint64, err err } for _, o := range obs { - o.purge(fseq) + o.purge(fseq, lseq) } return purged, nil } @@ -1156,27 +1519,29 @@ func (mset *stream) sourcesInfo() (sis []*StreamSourceInfo) { return sis } -func (mset *stream) allSubjects() ([]string, bool) { - subjects, cfg, acc := mset.subjects(), mset.config(), mset.account() - +func allSubjects(cfg *StreamConfig, acc *Account) ([]string, bool) { + subjects := copyStrings(cfg.Subjects) var hasExt bool var seen map[string]bool if cfg.Mirror != nil { - var subjs []string - seen = make(map[string]bool) - subjs, hasExt = acc.streamSourceSubjects(cfg.Mirror, seen) + subjs, localHasExt := acc.streamSourceSubjects(cfg.Mirror, make(map[string]bool)) if len(subjs) > 0 { subjects = append(subjects, subjs...) } + if localHasExt { + hasExt = true + } } else if len(cfg.Sources) > 0 { - var subjs []string seen = make(map[string]bool) for _, si := range cfg.Sources { - subjs, hasExt = acc.streamSourceSubjects(si, seen) + subjs, localHasExt := acc.streamSourceSubjects(si, seen) if len(subjs) > 0 { subjects = append(subjects, subjs...) } + if localHasExt { + hasExt = true + } } } @@ -1276,6 +1641,7 @@ func (a *Account) streamSourceSubjectsNotClustered(streamName string, seen map[s } } } + return subjects, hasExt } @@ -1315,35 +1681,26 @@ func (mset *stream) mirrorInfo() *StreamSourceInfo { return mset.sourceInfo(mset.mirror) } -const sourceHealthCheckInterval = 2 * time.Second +const sourceHealthCheckInterval = 1 * time.Second // Will run as a Go routine to process mirror consumer messages. -func (mset *stream) processMirrorMsgs() { +func (mset *stream) processMirrorMsgs(mirror *sourceInfo, ready *sync.WaitGroup) { s := mset.srv - defer s.grWG.Done() defer func() { - mset.mu.Lock() - if mset.mirror != nil { - mset.mirror.grr = false - if mset.mirror.qch != nil { - close(mset.mirror.qch) - mset.mirror.qch = nil - } - } - mset.mu.Unlock() + mirror.wg.Done() + s.grWG.Done() }() // Grab stream quit channel. mset.mu.Lock() - if mset.mirror == nil { - mset.mu.Unlock() - return - } - msgs, qch, siqch := mset.mirror.msgs, mset.qch, mset.mirror.qch + msgs, qch, siqch := mirror.msgs, mset.qch, mirror.qch // Set the last seen as now so that we don't fail at the first check. - mset.mirror.last = time.Now() + mirror.last = time.Now() mset.mu.Unlock() + // Signal the caller that we have captured the above fields. + ready.Done() + t := time.NewTicker(sourceHealthCheckInterval) defer t.Stop() @@ -1371,7 +1728,9 @@ func (mset *stream) processMirrorMsgs() { mset.mu.RUnlock() // No longer leader. if !isLeader { + mset.mu.Lock() mset.cancelMirrorConsumer() + mset.mu.Unlock() return } // We are stalled. @@ -1396,8 +1755,8 @@ func (mset *stream) processInboundMirrorMsg(m *inMsg) bool { return false } if !mset.isLeader() { - mset.mu.Unlock() mset.cancelMirrorConsumer() + mset.mu.Unlock() return false } @@ -1489,7 +1848,18 @@ func (mset *stream) processInboundMirrorMsg(m *inMsg) bool { err = mset.processJetStreamMsg(m.subj, _EMPTY_, m.hdr, m.msg, sseq-1, ts) } if err != nil { - if err == errLastSeqMismatch { + if strings.Contains(err.Error(), "no space left") { + s.Errorf("JetStream out of space, will be DISABLED") + s.DisableJetStream() + return false + } + if err != errLastSeqMismatch { + mset.mu.RLock() + accName, sname := mset.acc.Name, mset.cfg.Name + mset.mu.RUnlock() + s.RateLimitWarnf("Error processing inbound mirror message for '%s' > '%s': %v", + accName, sname, err) + } else { // We may have missed messages, restart. if sseq <= mset.lastSeq() { mset.mu.Lock() @@ -1505,12 +1875,6 @@ func (mset *stream) processInboundMirrorMsg(m *inMsg) bool { mset.mu.Unlock() mset.retryMirrorConsumer() } - } else { - s.Warnf("Got error processing JetStream mirror msg: %v", err) - } - if strings.Contains(err.Error(), "no space left") { - s.Errorf("JetStream out of space, will be DISABLED") - s.DisableJetStream() } } return err == nil @@ -1524,24 +1888,20 @@ func (mset *stream) setMirrorErr(err *ApiError) { mset.mu.Unlock() } +// Cancels a mirror consumer. +// +// Lock held on entry func (mset *stream) cancelMirrorConsumer() { - mset.mu.Lock() - defer mset.mu.Unlock() if mset.mirror == nil { return } - if mset.mirror.sub != nil { - mset.unsubscribe(mset.mirror.sub) - mset.mirror.sub = nil - } - mset.removeInternalConsumer(mset.mirror) - // If the go routine is still running close the quit chan. - if mset.mirror.qch != nil { - close(mset.mirror.qch) - mset.mirror.qch = nil - } + mset.cancelSourceInfo(mset.mirror) } +// Similar to setupMirrorConsumer except that it will print a debug statement +// indicating that there is a retry. +// +// Lock is acquired in this function func (mset *stream) retryMirrorConsumer() error { mset.mu.Lock() defer mset.mu.Unlock() @@ -1573,6 +1933,30 @@ func (mset *stream) skipMsgs(start, end uint64) { } } +// This will schedule a call to setupMirrorConsumer, taking into account the last +// time it was retried and determine the soonest setSourceConsumer can be called +// without tripping the sourceConsumerRetryThreshold. +// The mset.mirror pointer has been verified to be not nil by the caller. +// +// Lock held on entry +func (mset *stream) scheduleSetupMirrorConsumerRetryAsap() { + // We are trying to figure out how soon we can retry. setupMirrorConsumer will reject + // a retry if last was done less than "sourceConsumerRetryThreshold" ago. + next := sourceConsumerRetryThreshold - time.Since(mset.mirror.lreq) + if next < 0 { + // It means that we have passed the threshold and so we are ready to go. + next = 0 + } + // To make *sure* that the next request will not fail, add a bit of buffer + // and some randomness. + next += time.Duration(rand.Intn(50)) + 10*time.Millisecond + time.AfterFunc(next, func() { + mset.mu.Lock() + mset.setupMirrorConsumer() + mset.mu.Unlock() + }) +} + // Setup our mirror consumer. // Lock should be held. func (mset *stream) setupMirrorConsumer() error { @@ -1580,24 +1964,27 @@ func (mset *stream) setupMirrorConsumer() error { return errors.New("outq required") } - isReset := mset.mirror != nil - - // Reset - if isReset { - if mset.mirror.sub != nil { - mset.unsubscribe(mset.mirror.sub) - mset.mirror.sub = nil - mset.mirror.dseq = 0 - mset.mirror.sseq = mset.lseq - } - // Make sure to delete any prior consumers if we know about them. - mset.removeInternalConsumer(mset.mirror) + // If this is the first time + if mset.mirror == nil { + mset.mirror = &sourceInfo{name: mset.cfg.Mirror.Name} + } else { + mset.cancelSourceInfo(mset.mirror) + mset.mirror.sseq = mset.lseq // If we are no longer the leader stop trying. if !mset.isLeader() { return nil } } + mirror := mset.mirror + + // We want to throttle here in terms of how fast we request new consumers, + // or if the previous is still in progress. + if last := time.Since(mirror.lreq); last < sourceConsumerRetryThreshold || mirror.sip { + mset.scheduleSetupMirrorConsumerRetryAsap() + return nil + } + mirror.lreq = time.Now() // Determine subjects etc. var deliverSubject string @@ -1609,22 +1996,6 @@ func (mset *stream) setupMirrorConsumer() error { deliverSubject = syncSubject("$JS.M") } - if !isReset { - mset.mirror = &sourceInfo{name: mset.cfg.Mirror.Name, msgs: newIPQueue() /* of *inMsg */} - } - - if !mset.mirror.grr { - mset.mirror.grr = true - mset.mirror.qch = make(chan struct{}) - mset.srv.startGoRoutine(func() { mset.processMirrorMsgs() }) - } - - // We want to throttle here in terms of how fast we request new consumers. - if time.Since(mset.mirror.lreq) < 2*time.Second { - return nil - } - mset.mirror.lreq = time.Now() - // Now send off request to create/update our consumer. This will be all API based even in single server mode. // We calculate durable names apriori so we do not need to save them off. @@ -1663,19 +2034,23 @@ func (mset *stream) setupMirrorConsumer() error { respCh := make(chan *JSApiConsumerCreateResponse, 1) reply := infoReplySubject() - crSub, _ := mset.subscribeInternal(reply, func(sub *subscription, c *client, _ *Account, subject, reply string, rmsg []byte) { + crSub, err := mset.subscribeInternal(reply, func(sub *subscription, c *client, _ *Account, subject, reply string, rmsg []byte) { mset.unsubscribeUnlocked(sub) _, msg := c.msgParts(rmsg) var ccr JSApiConsumerCreateResponse if err := json.Unmarshal(msg, &ccr); err != nil { c.Warnf("JetStream bad mirror consumer create response: %q", msg) - mset.cancelMirrorConsumer() mset.setMirrorErr(ApiErrors[JSInvalidJSONErr]) return } respCh <- &ccr }) + if err != nil { + mirror.err = NewJSMirrorConsumerSetupFailedError(err, Unless(err)) + mset.scheduleSetupMirrorConsumerRetryAsap() + return nil + } b, _ := json.Marshal(req) subject := fmt.Sprintf(JSApiConsumerCreateT, mset.cfg.Mirror.Name) @@ -1684,21 +2059,68 @@ func (mset *stream) setupMirrorConsumer() error { subject = strings.ReplaceAll(subject, "..", ".") } + // We need to create the subscription that will receive the messages prior + // to sending the consumer create request, because in some complex topologies + // with gateways and optimistic mode, it is possible that the consumer starts + // delivering messages as soon as the consumer request is received. + qname := fmt.Sprintf("[ACC:%s] stream mirror '%s' of '%s' msgs", mset.acc.Name, mset.cfg.Name, mset.cfg.Mirror.Name) + // Create a new queue each time + mirror.msgs = mset.srv.newIPQueue(qname) /* of *inMsg */ + msgs := mirror.msgs + sub, err := mset.subscribeInternal(deliverSubject, func(sub *subscription, c *client, _ *Account, subject, reply string, rmsg []byte) { + hdr, msg := c.msgParts(copyBytes(rmsg)) // Need to copy. + mset.queueInbound(msgs, subject, reply, hdr, msg) + }) + if err != nil { + mirror.err = NewJSMirrorConsumerSetupFailedError(err, Unless(err)) + mset.unsubscribeUnlocked(crSub) + mset.scheduleSetupMirrorConsumerRetryAsap() + return nil + } + mirror.err = nil + mirror.sub = sub + mirror.sip = true + + // Send the consumer create request mset.outq.send(newJSPubMsg(subject, _EMPTY_, reply, nil, b, nil, 0)) go func() { + + var retry bool + defer func() { + mset.mu.Lock() + // Check that this is still valid and if so, clear the "setup in progress" flag. + if mset.mirror != nil { + mset.mirror.sip = false + // If we need to retry, schedule now + if retry { + mset.scheduleSetupMirrorConsumerRetryAsap() + } + } + mset.mu.Unlock() + }() + + // Wait for previous processMirrorMsgs go routine to be completely done. + // If none is running, this will not block. + mirror.wg.Wait() + select { case ccr := <-respCh: + mset.mu.Lock() + // Mirror config has been removed. + if mset.mirror == nil { + mset.mu.Unlock() + return + } + ready := sync.WaitGroup{} + mirror := mset.mirror + mirror.err = nil if ccr.Error != nil || ccr.ConsumerInfo == nil { - mset.cancelMirrorConsumer() + mset.srv.Warnf("JetStream error response for create mirror consumer: %+v", ccr.Error) + mirror.err = ccr.Error + // Let's retry as soon as possible, but we are gated by sourceConsumerRetryThreshold + retry = true } else { - mset.mu.Lock() - // Mirror config has been removed. - if mset.mirror == nil { - mset.mu.Unlock() - mset.cancelMirrorConsumer() - return - } // When an upstream stream expires messages or in general has messages that we want // that are no longer available we need to adjust here. @@ -1711,31 +2133,20 @@ func (mset *stream) setupMirrorConsumer() error { } // Capture consumer name. - mset.mirror.cname = ccr.ConsumerInfo.Name - msgs := mset.mirror.msgs - - // Process inbound mirror messages from the wire. - sub, err := mset.subscribeInternal(deliverSubject, func(sub *subscription, c *client, _ *Account, subject, reply string, rmsg []byte) { - hdr, msg := c.msgParts(copyBytes(rmsg)) // Need to copy. - mset.queueInbound(msgs, subject, reply, hdr, msg) - }) - if err != nil { - mset.mirror.err = NewJSMirrorConsumerSetupFailedError(err, Unless(err)) - mset.mirror.sub = nil - mset.mirror.cname = _EMPTY_ - } else { - mset.mirror.err = nil - mset.mirror.sub = sub - mset.mirror.last = time.Now() - mset.mirror.dseq = 0 - mset.mirror.sseq = ccr.ConsumerInfo.Delivered.Stream - } - mset.mu.Unlock() + mirror.cname = ccr.ConsumerInfo.Name + mirror.dseq = 0 + mirror.sseq = ccr.ConsumerInfo.Delivered.Stream + mirror.qch = make(chan struct{}) + mirror.wg.Add(1) + ready.Add(1) + mset.srv.startGoRoutine(func() { mset.processMirrorMsgs(mirror, &ready) }) } - mset.setMirrorErr(ccr.Error) - case <-time.After(10 * time.Second): + mset.mu.Unlock() + ready.Wait() + case <-time.After(5 * time.Second): mset.unsubscribeUnlocked(crSub) - return + // We already waited 5 seconds, let's retry now. + retry = true } }() @@ -1763,35 +2174,77 @@ func (mset *stream) retrySourceConsumer(sname string) { mset.retrySourceConsumerAtSeq(sname, si.sseq+1) } +// Same than setSourceConsumer but simply issue a debug statement indicating +// that there is a retry. +// // Lock should be held. -func (mset *stream) retrySourceConsumerAtSeq(sname string, seq uint64) { - if mset.client == nil { - return - } +func (mset *stream) retrySourceConsumerAtSeq(iname string, seq uint64) { s := mset.srv s.Debugf("Retrying source consumer for '%s > %s'", mset.acc.Name, mset.cfg.Name) - // No longer configured. - if si := mset.sources[sname]; si == nil { - return - } - mset.setSourceConsumer(sname, seq) + // setSourceConsumer will check that the source is still configured. + mset.setSourceConsumer(iname, seq) } // Lock should be held. -func (mset *stream) cancelSourceConsumer(sname string) { - if si := mset.sources[sname]; si != nil && si.sub != nil { +func (mset *stream) cancelSourceConsumer(iname string) { + if si := mset.sources[iname]; si != nil { + mset.cancelSourceInfo(si) + si.sseq, si.dseq = 0, 0 + } +} + +// The `si` has been verified to be not nil. The sourceInfo's sub will +// be unsubscribed and set to nil (if not already done) and the +// cname will be reset. The message processing's go routine quit channel +// will be closed if still opened. +// +// Lock should be held +func (mset *stream) cancelSourceInfo(si *sourceInfo) { + if si.sub != nil { mset.unsubscribe(si.sub) si.sub = nil - si.sseq, si.dseq = 0, 0 - mset.removeInternalConsumer(si) - // If the go routine is still running close the quit chan. - if si.qch != nil { - close(si.qch) - si.qch = nil - } } + mset.removeInternalConsumer(si) + if si.qch != nil { + close(si.qch) + si.qch = nil + } + si.msgs.drain() + si.msgs.unregister() +} + +const sourceConsumerRetryThreshold = 2 * time.Second + +// This will schedule a call to setSourceConsumer, taking into account the last +// time it was retried and determine the soonest setSourceConsumer can be called +// without tripping the sourceConsumerRetryThreshold. +// +// Lock held on entry +func (mset *stream) scheduleSetSourceConsumerRetryAsap(si *sourceInfo, seq uint64) { + // We are trying to figure out how soon we can retry. setSourceConsumer will reject + // a retry if last was done less than "sourceConsumerRetryThreshold" ago. + next := sourceConsumerRetryThreshold - time.Since(si.lreq) + if next < 0 { + // It means that we have passed the threshold and so we are ready to go. + next = 0 + } + // To make *sure* that the next request will not fail, add a bit of buffer + // and some randomness. + next += time.Duration(rand.Intn(50)) + 10*time.Millisecond + mset.scheduleSetSourceConsumerRetry(si.iname, seq, next) +} + +// Simply schedules setSourceConsumer at the given delay. +// +// Does not require lock +func (mset *stream) scheduleSetSourceConsumerRetry(iname string, seq uint64, delay time.Duration) { + time.AfterFunc(delay, func() { + mset.mu.Lock() + mset.setSourceConsumer(iname, seq) + mset.mu.Unlock() + }) } // Lock should be held. @@ -1800,20 +2253,22 @@ func (mset *stream) setSourceConsumer(iname string, seq uint64) { if si == nil { return } - if si.sub != nil { - mset.unsubscribe(si.sub) - si.sub = nil - } - // Need to delete the old one. - mset.removeInternalConsumer(si) + // Cancel previous instance if applicable + mset.cancelSourceInfo(si) - si.sseq, si.dseq = seq, 0 - si.last = time.Now() ssi := mset.streamSource(iname) if ssi == nil { return } + // We want to throttle here in terms of how fast we request new consumers, + // or if the previous is still in progress. + if last := time.Since(si.lreq); last < sourceConsumerRetryThreshold || si.sip { + mset.scheduleSetSourceConsumerRetryAsap(si, seq) + return + } + si.lreq = time.Now() + // Determine subjects etc. var deliverSubject string ext := ssi.External @@ -1824,18 +2279,6 @@ func (mset *stream) setSourceConsumer(iname string, seq uint64) { deliverSubject = syncSubject("$JS.S") } - if !si.grr { - si.grr = true - si.qch = make(chan struct{}) - mset.srv.startGoRoutine(func() { mset.processSourceMsgs(si) }) - } - - // We want to throttle here in terms of how fast we request new consumers. - if time.Since(si.lreq) < 2*time.Second { - return - } - si.lreq = time.Now() - req := &CreateConsumerRequest{ Stream: si.name, Config: ConsumerConfig{ @@ -1848,6 +2291,7 @@ func (mset *stream) setSourceConsumer(iname string, seq uint64) { Direct: true, }, } + // If starting, check any configs. if seq <= 1 { if ssi.OptStartSeq > 0 { @@ -1856,6 +2300,10 @@ func (mset *stream) setSourceConsumer(iname string, seq uint64) { } else if ssi.OptStartTime != nil { req.Config.OptStartTime = ssi.OptStartTime req.Config.DeliverPolicy = DeliverByStartTime + } else if !si.start.IsZero() { + // We are falling back to time based startup on a recover, but our messages are gone. e.g. purge, expired, retention policy. + req.Config.OptStartTime = &si.start + req.Config.DeliverPolicy = DeliverByStartTime } } else { req.Config.OptStartSeq = seq @@ -1868,7 +2316,7 @@ func (mset *stream) setSourceConsumer(iname string, seq uint64) { respCh := make(chan *JSApiConsumerCreateResponse, 1) reply := infoReplySubject() - crSub, _ := mset.subscribeInternal(reply, func(sub *subscription, c *client, _ *Account, subject, reply string, rmsg []byte) { + crSub, err := mset.subscribeInternal(reply, func(sub *subscription, c *client, _ *Account, subject, reply string, rmsg []byte) { mset.unsubscribeUnlocked(sub) _, msg := c.msgParts(rmsg) var ccr JSApiConsumerCreateResponse @@ -1878,6 +2326,11 @@ func (mset *stream) setSourceConsumer(iname string, seq uint64) { } respCh <- &ccr }) + if err != nil { + si.err = NewJSSourceConsumerSetupFailedError(err, Unless(err)) + mset.scheduleSetSourceConsumerRetryAsap(si, seq) + return + } b, _ := json.Marshal(req) subject := fmt.Sprintf(JSApiConsumerCreateT, si.name) @@ -1886,74 +2339,104 @@ func (mset *stream) setSourceConsumer(iname string, seq uint64) { subject = strings.ReplaceAll(subject, "..", ".") } + // We need to create the subscription that will receive the messages prior + // to sending the consumer create request, because in some complex topologies + // with gateways and optimistic mode, it is possible that the consumer starts + // delivering messages as soon as the consumer request is received. + qname := fmt.Sprintf("[ACC:%s] stream source '%s' from '%s' msgs", mset.acc.Name, mset.cfg.Name, si.name) + // Create a new queue each time + si.msgs = mset.srv.newIPQueue(qname) // of *inMsg + msgs := si.msgs + sub, err := mset.subscribeInternal(deliverSubject, func(sub *subscription, c *client, _ *Account, subject, reply string, rmsg []byte) { + hdr, msg := c.msgParts(copyBytes(rmsg)) // Need to copy. + mset.queueInbound(msgs, subject, reply, hdr, msg) + }) + if err != nil { + si.err = NewJSSourceConsumerSetupFailedError(err, Unless(err)) + mset.unsubscribeUnlocked(crSub) + mset.scheduleSetSourceConsumerRetryAsap(si, seq) + return + } + si.err = nil + si.sub = sub + si.sip = true + + // Send the consumer create request mset.outq.send(newJSPubMsg(subject, _EMPTY_, reply, nil, b, nil, 0)) go func() { + + var retry bool + defer func() { + mset.mu.Lock() + // Check that this is still valid and if so, clear the "setup in progress" flag. + if si := mset.sources[iname]; si != nil { + si.sip = false + // If we need to retry, schedule now + if retry { + mset.scheduleSetSourceConsumerRetryAsap(si, seq) + } + } + mset.mu.Unlock() + }() + + // Wait for previous processSourceMsgs go routine to be completely done. + // If none is running, this will not block. + si.wg.Wait() + select { case ccr := <-respCh: + ready := sync.WaitGroup{} mset.mu.Lock() - if si := mset.sources[iname]; si != nil { + // Check that it has not been removed or canceled (si.sub would be nil) + if si := mset.sources[iname]; si != nil && si.sub != nil { si.err = nil if ccr.Error != nil || ccr.ConsumerInfo == nil { mset.srv.Warnf("JetStream error response for create source consumer: %+v", ccr.Error) si.err = ccr.Error - // We will retry every 10 seconds or so - mset.cancelSourceConsumer(iname) + // Let's retry as soon as possible, but we are gated by sourceConsumerRetryThreshold + retry = true } else { if si.sseq != ccr.ConsumerInfo.Delivered.Stream { si.sseq = ccr.ConsumerInfo.Delivered.Stream + 1 } - // Capture consumer name. si.cname = ccr.ConsumerInfo.Name - // Now create sub to receive messages. - sub, err := mset.subscribeInternal(deliverSubject, func(sub *subscription, c *client, _ *Account, subject, reply string, rmsg []byte) { - hdr, msg := c.msgParts(copyBytes(rmsg)) // Need to copy. - mset.queueInbound(si.msgs, subject, reply, hdr, msg) - }) - if err != nil { - si.err = NewJSSourceConsumerSetupFailedError(err, Unless(err)) - si.sub = nil - } else { - si.err = nil - si.sub = sub - si.last = time.Now() - } + // Do not set si.sseq to seq here. si.sseq will be set in processInboundSourceMsg + si.dseq = 0 + si.qch = make(chan struct{}) + si.wg.Add(1) + ready.Add(1) + mset.srv.startGoRoutine(func() { mset.processSourceMsgs(si, &ready) }) } } mset.mu.Unlock() - case <-time.After(10 * time.Second): + ready.Wait() + case <-time.After(5 * time.Second): mset.unsubscribeUnlocked(crSub) - return + // We already waited 5 seconds, let's retry now. + retry = true } }() } -func (mset *stream) processSourceMsgs(si *sourceInfo) { +func (mset *stream) processSourceMsgs(si *sourceInfo, ready *sync.WaitGroup) { s := mset.srv - defer s.grWG.Done() - - if si == nil { - return - } - defer func() { - mset.mu.Lock() - si.grr = false - if si.qch != nil { - close(si.qch) - si.qch = nil - } - mset.mu.Unlock() + si.wg.Done() + s.grWG.Done() }() - // Grab stream quit channel. + // Grab some stream and sourceInfo values now... mset.mu.Lock() - msgs, qch, siqch := si.msgs, mset.qch, si.qch + msgs, qch, siqch, iname := si.msgs, mset.qch, si.qch, si.iname // Set the last seen as now so that we don't fail at the first check. si.last = time.Now() mset.mu.Unlock() + // Signal the caller that we have captured the above fields. + ready.Done() + t := time.NewTicker(sourceHealthCheckInterval) defer t.Stop() @@ -1976,7 +2459,7 @@ func (mset *stream) processSourceMsgs(si *sourceInfo) { msgs.recycle(&ims) case <-t.C: mset.mu.RLock() - iname, isLeader := si.iname, mset.isLeader() + isLeader := mset.isLeader() stalled := time.Since(si.last) > 3*sourceHealthCheckInterval mset.mu.RUnlock() // No longer leader. @@ -1988,7 +2471,11 @@ func (mset *stream) processSourceMsgs(si *sourceInfo) { } // We are stalled. if stalled { - mset.retrySourceConsumer(iname) + mset.mu.Lock() + // We don't need to schedule here, we are going to simply + // call setSourceConsumer with the current state+1. + mset.setSourceConsumer(iname, si.sseq+1) + mset.mu.Unlock() } } } @@ -2025,8 +2512,8 @@ func (mset *stream) processInboundSourceMsg(si *sourceInfo, m *inMsg) bool { // If we are no longer the leader cancel this subscriber. if !mset.isLeader() { + mset.cancelSourceConsumer(si.iname) mset.mu.Unlock() - mset.cancelSourceConsumer(si.name) return false } @@ -2112,16 +2599,24 @@ func (mset *stream) processInboundSourceMsg(si *sourceInfo, m *inMsg) bool { if err != nil { s := mset.srv - if err == errLastSeqMismatch { - mset.cancelSourceConsumer(si.iname) - mset.retrySourceConsumer(si.iname) - } else { - s.Warnf("JetStream got an error processing inbound source msg: %v", err) - } if strings.Contains(err.Error(), "no space left") { s.Errorf("JetStream out of space, will be DISABLED") s.DisableJetStream() + } else { + mset.mu.RLock() + accName, sname, iname := mset.acc.Name, mset.cfg.Name, si.iname + mset.mu.RUnlock() + // Log some warning for errors other than errLastSeqMismatch + if err != errLastSeqMismatch { + s.RateLimitWarnf("Error processing inbound source %q for '%s' > '%s': %v", + iname, accName, sname, err) + } + // Retry in all type of errors. + // This will make sure the source is still in mset.sources map, + // find the last sequence and then call setSourceConsumer. + mset.retrySourceConsumer(iname) } + return false } return true @@ -2194,12 +2689,13 @@ func (mset *stream) setStartingSequenceForSource(sname string) { return } + var smv StoreMsg for seq := state.LastSeq; seq >= state.FirstSeq; seq-- { - _, hdr, _, _, err := mset.store.LoadMsg(seq) - if err != nil || len(hdr) == 0 { + sm, err := mset.store.LoadMsg(seq, &smv) + if err != nil || len(sm.hdr) == 0 { continue } - ss := getHeader(JSStreamSource, hdr) + ss := getHeader(JSStreamSource, sm.hdr) if len(ss) == 0 { continue } @@ -2228,15 +2724,25 @@ func (mset *stream) startingSequenceForSources() { if ssi.iname == _EMPTY_ { ssi.setIndexName() } - si := &sourceInfo{name: ssi.Name, iname: ssi.iname, msgs: newIPQueue() /* of *inMsg */} + si := &sourceInfo{name: ssi.Name, iname: ssi.iname} mset.sources[ssi.iname] = si } var state StreamState mset.store.FastState(&state) + + // If the last time has been stamped remember in case we need to fall back to this for any given upstream source. + // TODO(dlc) - This will be ok, but should formalize with new approach and more formal and durable state. + if !state.LastTime.IsZero() { + for _, si := range mset.sources { + si.start = state.LastTime + } + } + // Bail if no messages, meaning no context. if state.Msgs == 0 { return } + // For short circuiting return. expected := len(mset.cfg.Sources) seqs := make(map[string]uint64) @@ -2255,12 +2761,13 @@ func (mset *stream) startingSequenceForSources() { } }() + var smv StoreMsg for seq := state.LastSeq; seq >= state.FirstSeq; seq-- { - _, hdr, _, _, err := mset.store.LoadMsg(seq) - if err != nil || len(hdr) == 0 { + sm, err := mset.store.LoadMsg(seq, &smv) + if err != nil || sm == nil || len(sm.hdr) == 0 { continue } - ss := getHeader(JSStreamSource, hdr) + ss := getHeader(JSStreamSource, sm.hdr) if len(ss) == 0 { continue } @@ -2286,7 +2793,7 @@ func (mset *stream) setupSourceConsumers() error { // Reset if needed. for _, si := range mset.sources { if si.sub != nil { - mset.cancelSourceConsumer(si.name) + mset.cancelSourceConsumer(si.iname) } } @@ -2332,16 +2839,7 @@ func (mset *stream) subscribeToStream() error { // Lock should be held. func (mset *stream) stopSourceConsumers() { for _, si := range mset.sources { - if si.sub != nil { - mset.unsubscribe(si.sub) - } - // Need to delete the old one. - mset.removeInternalConsumer(si) - // If the go routine is still running close the quit chan. - if si.qch != nil { - close(si.qch) - si.qch = nil - } + mset.cancelSourceInfo(si) } } @@ -2360,14 +2858,7 @@ func (mset *stream) unsubscribeToStream() error { mset.unsubscribeInternal(subject) } if mset.mirror != nil { - if mset.mirror.sub != nil { - mset.unsubscribe(mset.mirror.sub) - } - mset.removeInternalConsumer(mset.mirror) - // If the go routine is still running close the quit chan. - if mset.mirror.qch != nil { - close(mset.mirror.qch) - } + mset.cancelSourceInfo(mset.mirror) mset.mirror = nil } @@ -2496,7 +2987,7 @@ func (mset *stream) storeUpdates(md, bd int64, seq uint64, subj string) { } if mset.jsa != nil { - mset.jsa.updateUsage(mset.stype, bd) + mset.jsa.updateUsage(mset.tier, mset.stype, bd) } } @@ -2830,21 +3321,26 @@ func (mset *stream) processJetStreamMsg(subject, reply string, hdr, msg []byte, // Expected last sequence per subject. if seq, exists := getExpectedLastSeqPerSubject(hdr); exists { // TODO(dlc) - We could make a new store func that does this all in one. - _, lseq, _, _, _, err := mset.store.LoadLastMsg(subject) + var smv StoreMsg + var fseq uint64 + sm, err := mset.store.LoadLastMsg(subject, &smv) + if sm != nil { + fseq = sm.seq + } // If seq passed in is zero that signals we expect no msg to be present. if err == ErrStoreMsgNotFound && seq == 0 { - lseq, err = 0, nil + fseq, err = 0, nil } - if err != nil || lseq != seq { + if err != nil || fseq != seq { mset.clfs++ mset.mu.Unlock() if canRespond { resp.PubAck = &PubAck{Stream: name} - resp.Error = NewJSStreamWrongLastSequenceError(lseq) + resp.Error = NewJSStreamWrongLastSequenceError(fseq) b, _ := json.Marshal(resp) outq.sendMsg(reply, b) } - return fmt.Errorf("last sequence by subject mismatch: %d vs %d", seq, lseq) + return fmt.Errorf("last sequence by subject mismatch: %d vs %d", seq, fseq) } } // Check for any rollups. @@ -2969,12 +3465,29 @@ func (mset *stream) processJetStreamMsg(subject, reply string, hdr, msg []byte, mset.lmsgId = msgId clfs := mset.clfs mset.lseq++ + tierName := mset.tier + + // Republish state if needed. + var tsubj string + var tlseq uint64 + if mset.tr != nil { + tsubj, _ = mset.tr.transformSubject(subject) + } + republish := tsubj != _EMPTY_ // We hold the lock to this point to make sure nothing gets between us since we check for pre-conditions. // Currently can not hold while calling store b/c we have inline storage update calls that may need the lock. // Note that upstream that sets seq/ts should be serialized as much as possible. mset.mu.Unlock() + // If we are republishing grab last sequence for this exact subject. Aids in gap detection for lightweight clients. + if republish { + var smv StoreMsg + if sm, _ := store.LoadLastMsg(subject, &smv); sm != nil { + tlseq = sm.seq + } + } + // Store actual msg. if lseq == 0 && ts == 0 { seq, ts, err = store.StoreMsg(subject, hdr, msg) @@ -3007,11 +3520,15 @@ func (mset *stream) processJetStreamMsg(subject, reply string, hdr, msg []byte, resp.Error = NewJSStreamStoreFailedError(err, Unless(err)) response, _ = json.Marshal(resp) } - } else if jsa.limitsExceeded(stype) { - s.Warnf("JetStream resource limits exceeded for account: %q", accName) + } else if exceeded, apiErr := jsa.limitsExceeded(stype, tierName); exceeded { + s.RateLimitWarnf("JetStream resource limits exceeded for account: %q", accName) if canRespond { resp.PubAck = &PubAck{Stream: name} - resp.Error = NewJSAccountResourcesExceededError() + if apiErr == nil { + resp.Error = NewJSAccountResourcesExceededError() + } else { + resp.Error = apiErr + } response, _ = json.Marshal(resp) } // If we did not succeed put those values back. @@ -3038,6 +3555,13 @@ func (mset *stream) processJetStreamMsg(subject, reply string, hdr, msg []byte, response = append(pubAck, strconv.FormatUint(seq, 10)...) response = append(response, '}') } + // Check for republish. + if republish { + hdr = genHeader(hdr, JSStream, name) + hdr = genHeader(hdr, JSSequence, strconv.FormatUint(seq, 10)) + hdr = genHeader(hdr, JSLastSequence, strconv.FormatUint(tlseq, 10)) + mset.outq.send(newJSPubMsg(tsubj, subject, _EMPTY_, copyBytes(hdr), copyBytes(msg), nil, seq)) + } } // Send response here. @@ -3046,18 +3570,18 @@ func (mset *stream) processJetStreamMsg(subject, reply string, hdr, msg []byte, } if err == nil && seq > 0 && numConsumers > 0 { - mset.mu.Lock() + mset.mu.RLock() for _, o := range mset.consumers { o.mu.Lock() - if o.isLeader() { - if seq > o.lsgap && o.isFilteredMatch(subject) { - o.sgap++ + if o.isLeader() && o.isFilteredMatch(subject) { + if seq > o.npcm { + o.npc++ } o.signalNewMessages() } o.mu.Unlock() } - mset.mu.Unlock() + mset.mu.RUnlock() } return err @@ -3065,37 +3589,49 @@ func (mset *stream) processJetStreamMsg(subject, reply string, hdr, msg []byte, // Internal message for use by jetstream subsystem. type jsPubMsg struct { - subj string - dsubj string + dsubj string // Subject to send to, e.g. _INBOX.xxx reply string - hdr []byte - msg []byte - o *consumer - seq uint64 + StoreMsg + o *consumer } var jsPubMsgPool sync.Pool -func newJSPubMsg(subj, dsubj, reply string, hdr, msg []byte, o *consumer, seq uint64) *jsPubMsg { +func newJSPubMsg(dsubj, subj, reply string, hdr, msg []byte, o *consumer, seq uint64) *jsPubMsg { var m *jsPubMsg + var buf []byte pm := jsPubMsgPool.Get() if pm != nil { m = pm.(*jsPubMsg) + buf = m.buf[:0] } else { - m = &jsPubMsg{} + m = new(jsPubMsg) } // When getting something from a pool it is criticical that all fields are // initialized. Doing this way guarantees that if someone adds a field to // the structure, the compiler will fail the build if this line is not updated. - (*m) = jsPubMsg{subj, dsubj, reply, hdr, msg, o, seq} + (*m) = jsPubMsg{dsubj, reply, StoreMsg{subj, hdr, msg, buf, seq, 0}, o} + return m } +// Gets a jsPubMsg from the pool. +func getJSPubMsgFromPool() *jsPubMsg { + pm := jsPubMsgPool.Get() + if pm != nil { + return pm.(*jsPubMsg) + } + return new(jsPubMsg) +} + func (pm *jsPubMsg) returnToPool() { if pm == nil { return } pm.subj, pm.dsubj, pm.reply, pm.hdr, pm.msg, pm.o = _EMPTY_, _EMPTY_, _EMPTY_, nil, nil, nil + if len(pm.buf) > 0 { + pm.buf = pm.buf[:0] + } jsPubMsgPool.Put(pm) } @@ -3103,7 +3639,7 @@ func (pm *jsPubMsg) size() int { if pm == nil { return 0 } - return len(pm.subj) + len(pm.reply) + len(pm.hdr) + len(pm.msg) + return len(pm.dsubj) + len(pm.reply) + len(pm.hdr) + len(pm.msg) } // Queue of *jsPubMsg for sending internal system messages. @@ -3124,6 +3660,13 @@ func (q *jsOutQ) send(msg *jsPubMsg) { q.push(msg) } +func (q *jsOutQ) unregister() { + if q == nil { + return + } + q.ipQueue.unregister() +} + // StoredMsg is for raw access to messages in a stream. type StoredMsg struct { Subject string `json:"subject"` @@ -3141,8 +3684,8 @@ func (mset *stream) setupSendCapabilities() { if mset.outq != nil { return } - qname := fmt.Sprintf("Stream %q send", mset.cfg.Name) - mset.outq = &jsOutQ{newIPQueue(ipQueue_Logger(qname, mset.srv.ipqLog))} // of *jsPubMsg + qname := fmt.Sprintf("[ACC:%s] stream '%s' sendQ", mset.acc.Name, mset.cfg.Name) + mset.outq = &jsOutQ{mset.srv.newIPQueue(qname)} // of *jsPubMsg go mset.internalLoop() } @@ -3167,16 +3710,6 @@ func (mset *stream) name() string { return mset.cfg.Name } -// Returns a copy of the interest subjects for this stream. -func (mset *stream) subjects() []string { - mset.mu.RLock() - defer mset.mu.RUnlock() - if len(mset.cfg.Subjects) == 0 { - return nil - } - return copyStrings(mset.cfg.Subjects) -} - func (mset *stream) internalLoop() { mset.mu.RLock() s := mset.srv @@ -3196,7 +3729,8 @@ func (mset *stream) internalLoop() { mset.mu.RUnlock() // Raw scratch buffer. - var _r [64 * 1024]byte + // This should be rarely used now so can be smaller. + var _r [1024]byte for { select { @@ -3204,29 +3738,42 @@ func (mset *stream) internalLoop() { pms := outq.pop() for _, pmi := range pms { pm := pmi.(*jsPubMsg) - c.pa.subject = []byte(pm.subj) - c.pa.deliver = []byte(pm.dsubj) + c.pa.subject = []byte(pm.dsubj) + c.pa.deliver = []byte(pm.subj) c.pa.size = len(pm.msg) + len(pm.hdr) c.pa.szb = []byte(strconv.Itoa(c.pa.size)) c.pa.reply = []byte(pm.reply) - msg := _r[:0] + // If we have an underlying buf that is the wire contents for hdr + msg, else construct on the fly. + var msg []byte + if len(pm.buf) > 0 { + msg = pm.buf + } else { + if len(pm.hdr) > 0 { + msg = pm.hdr + if len(pm.msg) > 0 { + msg = _r[:0] + msg = append(msg, pm.hdr...) + msg = append(msg, pm.msg...) + } + } else if len(pm.msg) > 0 { + // We own this now from a low level buffer perspective so can use directly here. + msg = pm.msg + } + } + if len(pm.hdr) > 0 { c.pa.hdr = len(pm.hdr) c.pa.hdb = []byte(strconv.Itoa(c.pa.hdr)) - msg = append(msg, pm.hdr...) - msg = append(msg, pm.msg...) } else { c.pa.hdr = -1 c.pa.hdb = nil - if len(pm.msg) > 0 { - msg = append(msg, pm.msg...) - } } + msg = append(msg, _CRLF_...) didDeliver, _ := c.processInboundClientMsg(msg) - c.pa.szb = nil + c.pa.szb, c.pa.subject, c.pa.deliver = nil, nil, nil // Check to see if this is a delivery for a consumer and // we failed to deliver the message. If so alert the consumer. @@ -3271,6 +3818,9 @@ func (mset *stream) internalLoop() { // Internal function to delete a stream. func (mset *stream) delete() error { + if mset == nil { + return nil + } return mset.stop(true, true) } @@ -3368,6 +3918,14 @@ func (mset *stream) stop(deleteFlag, advisory bool) error { sysc := mset.sysc mset.sysc = nil + if deleteFlag { + // Unregistering ipQueues do not prevent them from push/pop + // just will remove them from the central monitoring map + mset.msgs.unregister() + mset.ackq.unregister() + mset.outq.unregister() + } + // Clustered cleanup. mset.mu.Unlock() @@ -3394,18 +3952,19 @@ func (mset *stream) stop(deleteFlag, advisory bool) error { } func (mset *stream) getMsg(seq uint64) (*StoredMsg, error) { - subj, hdr, msg, ts, err := mset.store.LoadMsg(seq) + var smv StoreMsg + sm, err := mset.store.LoadMsg(seq, &smv) if err != nil { return nil, err } - sm := &StoredMsg{ - Subject: subj, - Sequence: seq, - Header: hdr, - Data: msg, - Time: time.Unix(0, ts).UTC(), - } - return sm, nil + // This only used in tests directly so no need to pool etc. + return &StoredMsg{ + Subject: sm.subj, + Sequence: sm.seq, + Header: sm.hdr, + Data: sm.msg, + Time: time.Unix(0, sm.ts).UTC(), + }, nil } // getConsumers will return all the current consumers for this stream. @@ -3592,14 +4151,14 @@ func (a *Account) RestoreStream(ncfg *StreamConfig, r io.Reader) (*stream, error return nil, errors.New("nil config on stream restore") } - cfg, err := checkStreamCfg(ncfg) + s, jsa, err := a.checkForJetStream() if err != nil { - return nil, NewJSStreamNotFoundError(Unless(err)) + return nil, err } - _, jsa, err := a.checkForJetStream() - if err != nil { - return nil, err + cfg, apiErr := s.checkStreamCfg(ncfg, a) + if apiErr != nil { + return nil, apiErr } sd := filepath.Join(jsa.storeDir, snapsDir) @@ -3692,6 +4251,7 @@ func (a *Account) RestoreStream(ncfg *StreamConfig, r io.Reader) (*stream, error if err := os.Rename(sdir, ndir); err != nil { return nil, err } + if cfg.Template != _EMPTY_ { if err := jsa.addStreamNameToTemplate(cfg.Template, cfg.Name); err != nil { return nil, err @@ -3704,6 +4264,14 @@ func (a *Account) RestoreStream(ncfg *StreamConfig, r io.Reader) (*stream, error if !fcfg.Created.IsZero() { mset.setCreatedTime(fcfg.Created) } + lseq := mset.lastSeq() + + // Make sure we do an update if the configs have changed. + if !reflect.DeepEqual(fcfg.StreamConfig, cfg) { + if err := mset.update(&cfg); err != nil { + return nil, err + } + } // Now do consumers. odir := filepath.Join(ndir, consumerDir) @@ -3747,7 +4315,7 @@ func (a *Account) RestoreStream(ncfg *StreamConfig, r io.Reader) (*stream, error obs.setCreatedTime(cfg.Created) } obs.mu.Lock() - err = obs.readStoredState() + err = obs.readStoredState(lseq) obs.mu.Unlock() if err != nil { mset.stop(true, false) diff --git a/vendor/github.com/nats-io/nats-server/v2/server/sublist.go b/vendor/github.com/nats-io/nats-server/v2/server/sublist.go index a9873e95..2ab89943 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/sublist.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/sublist.go @@ -1369,7 +1369,7 @@ func matchLiteral(literal, subject string) bool { } func addLocalSub(sub *subscription, subs *[]*subscription, includeLeafHubs bool) { - if sub != nil && sub.client != nil && sub.im == nil { + if sub != nil && sub.client != nil { kind := sub.client.kind if kind == CLIENT || kind == SYSTEM || kind == JETSTREAM || kind == ACCOUNT || (includeLeafHubs && sub.client.isHubLeafNode() /* implied kind==LEAF */) { diff --git a/vendor/github.com/nats-io/nats-server/v2/server/util.go b/vendor/github.com/nats-io/nats-server/v2/server/util.go index 43c8ef3b..c7f5827e 100644 --- a/vendor/github.com/nats-io/nats-server/v2/server/util.go +++ b/vendor/github.com/nats-io/nats-server/v2/server/util.go @@ -21,6 +21,7 @@ import ( "net" "net/url" "reflect" + "regexp" "strconv" "strings" "time" @@ -36,6 +37,46 @@ const ( asciiNine = 57 ) +var semVerRe = regexp.MustCompile(`\Av?([0-9]+)\.?([0-9]+)?\.?([0-9]+)?`) + +func versionComponents(version string) (major, minor, patch int, err error) { + m := semVerRe.FindStringSubmatch(version) + if m == nil { + return 0, 0, 0, errors.New("invalid semver") + } + major, err = strconv.Atoi(m[1]) + if err != nil { + return -1, -1, -1, err + } + minor, err = strconv.Atoi(m[2]) + if err != nil { + return -1, -1, -1, err + } + patch, err = strconv.Atoi(m[3]) + if err != nil { + return -1, -1, -1, err + } + return major, minor, patch, err +} + +func versionAtLeastCheckError(version string, emajor, eminor, epatch int) (bool, error) { + major, minor, patch, err := versionComponents(version) + if err != nil { + return false, err + } + if major > emajor || + (major == emajor && minor > eminor) || + (major == emajor && minor == eminor && patch >= epatch) { + return true, nil + } + return false, err +} + +func versionAtLeast(version string, emajor, eminor, epatch int) bool { + res, _ := versionAtLeastCheckError(version, emajor, eminor, epatch) + return res +} + // parseSize expects decimal positive numbers. We // return -1 to signal error. func parseSize(d []byte) (n int) { @@ -273,9 +314,9 @@ func getURLsAsString(urls []*url.URL) []string { } // copyBytes make a new slice of the same size than `src` and copy its content. -// If `src` is nil, then this returns `nil` +// If `src` is nil or its length is 0, then this returns `nil` func copyBytes(src []byte) []byte { - if src == nil { + if len(src) == 0 { return nil } dst := make([]byte, len(src)) diff --git a/vendor/github.com/nats-io/nats.go/.travis.yml b/vendor/github.com/nats-io/nats.go/.travis.yml index aa87e139..1d05d3f8 100644 --- a/vendor/github.com/nats-io/nats.go/.travis.yml +++ b/vendor/github.com/nats-io/nats.go/.travis.yml @@ -7,7 +7,7 @@ install: - go get -t ./... - go get github.com/mattn/goveralls - go get github.com/wadey/gocovmerge -- go get -u honnef.co/go/tools/cmd/staticcheck +- go install honnef.co/go/tools/cmd/staticcheck@v0.2.2 - go get -u github.com/client9/misspell/cmd/misspell before_script: - $(exit $(go fmt ./... | wc -l)) diff --git a/vendor/github.com/nats-io/nats.go/.words b/vendor/github.com/nats-io/nats.go/.words index 63fba00c..24be7f62 100644 --- a/vendor/github.com/nats-io/nats.go/.words +++ b/vendor/github.com/nats-io/nats.go/.words @@ -1,26 +1,9 @@ 1 -## ^^^ size estimate, just needs to be non-zero -## -## The .words file is used by gospel (v1.2+), which wraps the Hunspell libraries but populates the dictionary with identifiers from the Go source. -## -## -## Comment lines are not actually parsed as comments, try to be careful ... but in practice they seem to work? -## -## We assume en_US hunspell dictionaries are installed and used. The /AFFIXRULES are defined in en_US.aff (eg: /usr/share/hunspell/en_US.aff) -## -## words which are in the base dictionary can't have extra affix rules added to them, so we have to start with the affixed variant we want to add. -## thus creds rather than cred/S and so on -## So we can't use receive/DRSZGBU, adding 'U', to allow unreceive and variants, we have to use unreceive as the stem. -## We can't define our own affix or compound rules, to capture rfc\d{3,} or 0x[0-9A-Fa-f]{2} - -## People involved who are referenced in todo/fixmes + derek dlc ivan -## Legitimate spellings in non-US English dialects; -## regular-if-rarer words just missing from the dictionary; -## variants of words not covered by hunspell en_US rules. acknowledgement/SM arity deduplication/S @@ -31,31 +14,12 @@ observable/S redelivery/S retransmitting retry/SB -unmarshal/SDG - -# I think that retry, being added as a symbol, is precluding the re-addition here with affix rules, -# so "retry/SB" above is ignored -retries -retryable - -## Things gospel doesn't pick up, but doesn't yet; I've filed -## Eg, plurals of non-collection types, or wire-format encodings in a struct field's tag -AsyncSubscriptions -ChanSubscriptions -PubAckFutures -SubOpts -SyncSubscriptions -no_wait - -## Conceptual nouns not actually in the source, describing state + SlowConsumer -## Symbols from elsewhere referred to in comments but not referenced in the code, so not currently surfaced by gospel as acceptable AppendInt ReadMIMEHeader -## The rest - clientProtoZero jetstream v1 @@ -83,8 +47,6 @@ tm todo unsub/S -## The spelling tokenizer doesn't take "permessage-deflate" as allowing for ... "permessage-deflate", -## which is an RFC7692 registered extension. We have to explicitly list "permessage". permessage permessage-deflate urlA diff --git a/vendor/github.com/nats-io/nats.go/.words.readme b/vendor/github.com/nats-io/nats.go/.words.readme new file mode 100644 index 00000000..9d9f5cbb --- /dev/null +++ b/vendor/github.com/nats-io/nats.go/.words.readme @@ -0,0 +1,25 @@ +The .words file is used by gospel (v1.2+), which wraps the Hunspell libraries +but populates the dictionary with identifiers from the Go source. + + + +Alas, no comments are allowed in the .words file and newer versions of gospel +error out on seeing them. This is really a hunspell restriction. + +We assume en_US hunspell dictionaries are installed and used. +The /AFFIXRULES are defined in en_US.aff (eg: /usr/share/hunspell/en_US.aff) +Invoke `hunspell -D` to see the actual locations. + +Words which are in the base dictionary can't have extra affix rules added to +them, so we have to start with the affixed variant we want to add. +Thus `creds` rather than `cred/S` and so on. + +So we can't use receive/DRSZGBU, adding 'U', to allow unreceive and variants, +we have to use unreceive as the stem. + +We can't define our own affix or compound rules, +to capture rfc\d{3,} or 0x[0-9A-Fa-f]{2} + +The spelling tokenizer doesn't take "permessage-deflate" as allowing for ... +"permessage-deflate", which is an RFC7692 registered extension for websockets. +We have to explicitly list "permessage". diff --git a/vendor/github.com/nats-io/nats.go/README.md b/vendor/github.com/nats-io/nats.go/README.md index adaf600a..766ff2df 100644 --- a/vendor/github.com/nats-io/nats.go/README.md +++ b/vendor/github.com/nats-io/nats.go/README.md @@ -29,7 +29,7 @@ When using or transitioning to Go modules support: ```bash # Go client latest or explicit version go get github.com/nats-io/nats.go/@latest -go get github.com/nats-io/nats.go/@v1.13.0 +go get github.com/nats-io/nats.go/@v1.16.0 # For latest NATS Server, add /v2 at the end go get github.com/nats-io/nats-server/v2 diff --git a/vendor/github.com/nats-io/nats.go/context.go b/vendor/github.com/nats-io/nats.go/context.go index 037668fb..300b6ebb 100644 --- a/vendor/github.com/nats-io/nats.go/context.go +++ b/vendor/github.com/nats-io/nats.go/context.go @@ -21,20 +21,13 @@ import ( // RequestMsgWithContext takes a context, a subject and payload // in bytes and request expecting a single response. func (nc *Conn) RequestMsgWithContext(ctx context.Context, msg *Msg) (*Msg, error) { - var hdr []byte - var err error - - if len(msg.Header) > 0 { - if !nc.info.Headers { - return nil, ErrHeadersNotSupported - } - - hdr, err = msg.headerBytes() - if err != nil { - return nil, err - } + if msg == nil { + return nil, ErrInvalidMsg + } + hdr, err := msg.headerBytes() + if err != nil { + return nil, err } - return nc.requestWithContext(ctx, msg.Subject, hdr, msg.Data) } diff --git a/vendor/github.com/nats-io/nats.go/go_test.mod b/vendor/github.com/nats-io/nats.go/go_test.mod index a9a586e3..ec124c91 100644 --- a/vendor/github.com/nats-io/nats.go/go_test.mod +++ b/vendor/github.com/nats-io/nats.go/go_test.mod @@ -4,17 +4,17 @@ go 1.17 require ( github.com/golang/protobuf v1.4.2 - github.com/nats-io/nats-server/v2 v2.7.2 + github.com/nats-io/nats-server/v2 v2.8.4 github.com/nats-io/nkeys v0.3.0 github.com/nats-io/nuid v1.0.1 google.golang.org/protobuf v1.23.0 ) require ( - github.com/klauspost/compress v1.13.4 // indirect - github.com/minio/highwayhash v1.0.1 // indirect - github.com/nats-io/jwt/v2 v2.2.1-0.20220113022732-58e87895b296 // indirect - golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce // indirect + github.com/klauspost/compress v1.14.4 // indirect + github.com/minio/highwayhash v1.0.2 // indirect + github.com/nats-io/jwt/v2 v2.2.1-0.20220330180145-442af02fd36a // indirect + golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd // indirect golang.org/x/sys v0.0.0-20220111092808-5a964db01320 // indirect golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect ) diff --git a/vendor/github.com/nats-io/nats.go/go_test.sum b/vendor/github.com/nats-io/nats.go/go_test.sum index f7a98a56..9b1cf6df 100644 --- a/vendor/github.com/nats-io/nats.go/go_test.sum +++ b/vendor/github.com/nats-io/nats.go/go_test.sum @@ -5,28 +5,26 @@ github.com/golang/protobuf v1.4.0-rc.4.0.20200313231945-b860323f09d0/go.mod h1:W github.com/golang/protobuf v1.4.0/go.mod h1:jodUvKwWbYaEsadDk5Fwe5c77LiNKVO9IDvqG2KuDX0= github.com/golang/protobuf v1.4.2 h1:+Z5KGCizgyZCbGh1KZqA0fcLLkwbsjIzS4aV2v7wJX0= github.com/golang/protobuf v1.4.2/go.mod h1:oDoupMAO8OvCJWAcko0GGGIgR6R6ocIYbsSw735rRwI= -github.com/golang/snappy v0.0.3 h1:fHPg5GQYlCeLIPB9BZqMVR5nR9A+IM5zcgeTdjMYmLA= -github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.3.0/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMywk6iLU= github.com/google/go-cmp v0.4.0 h1:xsAVV57WRhGj6kEIi8ReJzQlHHqcBYCElAvkovg3B/4= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/klauspost/compress v1.13.4 h1:0zhec2I8zGnjWcKyLl6i3gPqKANCCn5e9xmviEEeX6s= -github.com/klauspost/compress v1.13.4/go.mod h1:8dP1Hq4DHOhN9w426knH3Rhby4rFm6D8eO+e+Dq5Gzg= -github.com/minio/highwayhash v1.0.1 h1:dZ6IIu8Z14VlC0VpfKofAhCy74wu/Qb5gcn52yWoz/0= -github.com/minio/highwayhash v1.0.1/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY= -github.com/nats-io/jwt/v2 v2.2.1-0.20220113022732-58e87895b296 h1:vU9tpM3apjYlLLeY23zRWJ9Zktr5jp+mloR942LEOpY= -github.com/nats-io/jwt/v2 v2.2.1-0.20220113022732-58e87895b296/go.mod h1:0tqz9Hlu6bCBFLWAASKhE5vUA4c24L9KPUUgvwumE/k= -github.com/nats-io/nats-server/v2 v2.7.2 h1:+LEN8m0+jdCkiGc884WnDuxR+qj80/5arj+szKuRpRI= -github.com/nats-io/nats-server/v2 v2.7.2/go.mod h1:tckmrt0M6bVaDT3kmh9UrIq/CBOBBse+TpXQi5ldaa8= -github.com/nats-io/nats.go v1.13.1-0.20220121202836-972a071d373d/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w= +github.com/klauspost/compress v1.14.4 h1:eijASRJcobkVtSt81Olfh7JX43osYLwy5krOJo6YEu4= +github.com/klauspost/compress v1.14.4/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= +github.com/minio/highwayhash v1.0.2 h1:Aak5U0nElisjDCfPSG79Tgzkn2gl66NxOMspRrKnA/g= +github.com/minio/highwayhash v1.0.2/go.mod h1:BQskDq+xkJ12lmlUUi7U0M5Swg3EWR+dLTk+kldvVxY= +github.com/nats-io/jwt/v2 v2.2.1-0.20220330180145-442af02fd36a h1:lem6QCvxR0Y28gth9P+wV2K/zYUUAkJ+55U8cpS0p5I= +github.com/nats-io/jwt/v2 v2.2.1-0.20220330180145-442af02fd36a/go.mod h1:0tqz9Hlu6bCBFLWAASKhE5vUA4c24L9KPUUgvwumE/k= +github.com/nats-io/nats-server/v2 v2.8.4 h1:0jQzze1T9mECg8YZEl8+WYUXb9JKluJfCBriPUtluB4= +github.com/nats-io/nats-server/v2 v2.8.4/go.mod h1:8zZa+Al3WsESfmgSs98Fi06dRWLH5Bnq90m5bKD/eT4= +github.com/nats-io/nats.go v1.15.0/go.mod h1:BPko4oXsySz4aSWeFgOHLZs3G4Jq4ZAyE6/zMCxRT6w= github.com/nats-io/nkeys v0.3.0 h1:cgM5tL53EvYRU+2YLXIK0G2mJtK12Ft9oeooSZMA2G8= github.com/nats-io/nkeys v0.3.0/go.mod h1:gvUNGjVcM2IPr5rCsRsC6Wb3Hr2CQAm08dsxtV6A5y4= github.com/nats-io/nuid v1.0.1 h1:5iA8DT8V7q8WK2EScv2padNa/rTESc1KdnPw4TC2paw= github.com/nats-io/nuid v1.0.1/go.mod h1:19wcPz3Ph3q0Jbyiqsd0kePYG7A95tJPxeL+1OSON2c= golang.org/x/crypto v0.0.0-20210314154223-e6e6c4f2bb5b/go.mod h1:T9bdIzuCu7OtxOm1hfPfRQxPLYneinmdGuTeoZ9dtd4= -golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce h1:Roh6XWxHFKrPgC/EQhVubSAGQ6Ozk6IdxHSzt1mR0EI= -golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= +golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd h1:XcWmESyNjXJMLahc3mqVQJcgSTDxFxhETVlfk9uGc38= +golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/sys v0.0.0-20190130150945-aca44879d564/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= diff --git a/vendor/github.com/nats-io/nats.go/js.go b/vendor/github.com/nats-io/nats.go/js.go index 9f36d909..dafa53ab 100644 --- a/vendor/github.com/nats-io/nats.go/js.go +++ b/vendor/github.com/nats-io/nats.go/js.go @@ -181,6 +181,15 @@ const ( // routine. Without this, the subscription would possibly stall until // a new message or heartbeat/fc are received. chanSubFCCheckInterval = 250 * time.Millisecond + + // Default time wait between retries on Publish iff err is NoResponders. + DefaultPubRetryWait = 250 * time.Millisecond + + // Default number of retries + DefaultPubRetryAttempts = 2 + + // defaultAsyncPubAckInflight is the number of async pub acks inflight. + defaultAsyncPubAckInflight = 4000 ) // Types of control messages, so far heartbeat and flow control @@ -212,12 +221,12 @@ type jsOpts struct { wait time.Duration // For async publish error handling. aecb MsgErrHandler - // Maximum in flight. - maxap int + // Max async pub ack in flight + maxpa int // the domain that produced the pre domain string // enables protocol tracing - trace TraceCB + ctrace ClientTrace shouldTrace bool } @@ -232,8 +241,9 @@ func (nc *Conn) JetStream(opts ...JSOpt) (JetStreamContext, error) { js := &js{ nc: nc, opts: &jsOpts{ - pre: defaultAPIPrefix, - wait: defaultRequestWait, + pre: defaultAPIPrefix, + wait: defaultRequestWait, + maxpa: defaultAsyncPubAckInflight, }, } @@ -257,26 +267,16 @@ func (opt jsOptFn) configureJSContext(opts *jsOpts) error { return opt(opts) } -// TraceOperation indicates the direction of traffic flow to TraceCB -type TraceOperation int - -const ( - // TraceSent indicate the payload is being sent to subj - TraceSent TraceOperation = 0 - // TraceReceived indicate the payload is being received on subj - TraceReceived TraceOperation = 1 -) - -// TraceCB is called to trace API interactions for the JetStream Context -type TraceCB func(op TraceOperation, subj string, payload []byte, hdr Header) +// ClientTrace can be used to trace API interactions for the JetStream Context. +type ClientTrace struct { + RequestSent func(subj string, payload []byte) + ResponseReceived func(subj string, payload []byte, hdr Header) +} -// TraceFunc enables tracing of JetStream API interactions -func TraceFunc(cb TraceCB) JSOpt { - return jsOptFn(func(js *jsOpts) error { - js.trace = cb - js.shouldTrace = true - return nil - }) +func (ct ClientTrace) configureJSContext(js *jsOpts) error { + js.ctrace = ct + js.shouldTrace = true + return nil } // Domain changes the domain part of JetStream API prefix. @@ -336,10 +336,17 @@ type pubOpts struct { ctx context.Context ttl time.Duration id string - lid string // Expected last msgId - str string // Expected stream name - seq uint64 // Expected last sequence - lss uint64 // Expected last sequence per subject + lid string // Expected last msgId + str string // Expected stream name + seq *uint64 // Expected last sequence + lss *uint64 // Expected last sequence per subject + + // Publish retries for NoResponders err. + rwait time.Duration // Retry wait between attempts + rnum int // Retry attempts + + // stallWait is the max wait of a async pub ack. + stallWait time.Duration } // pubAckResponse is the ack response from the JetStream API when publishing a message. @@ -366,6 +373,13 @@ const ( MsgRollup = "Nats-Rollup" ) +// Headers for republished messages. +const ( + JSStream = "Nats-Stream" + JSSequence = "Nats-Sequence" + JSLastSequence = "Nats-Last-Sequence" +) + // MsgSize is a header that will be part of a consumer's delivered message if HeadersOnly requested. const MsgSize = "Nats-Msg-Size" @@ -377,7 +391,7 @@ const ( // PublishMsg publishes a Msg to a stream from JetStream. func (js *js) PublishMsg(m *Msg, opts ...PubOpt) (*PubAck, error) { - var o pubOpts + var o = pubOpts{rwait: DefaultPubRetryWait, rnum: DefaultPubRetryAttempts} if len(opts) > 0 { if m.Header == nil { m.Header = Header{} @@ -395,6 +409,9 @@ func (js *js) PublishMsg(m *Msg, opts ...PubOpt) (*PubAck, error) { if o.ttl == 0 && o.ctx == nil { o.ttl = js.opts.wait } + if o.stallWait > 0 { + return nil, fmt.Errorf("nats: stall wait cannot be set to sync publish") + } if o.id != _EMPTY_ { m.Header.Set(MsgIdHdr, o.id) @@ -405,11 +422,11 @@ func (js *js) PublishMsg(m *Msg, opts ...PubOpt) (*PubAck, error) { if o.str != _EMPTY_ { m.Header.Set(ExpectedStreamHdr, o.str) } - if o.seq > 0 { - m.Header.Set(ExpectedLastSeqHdr, strconv.FormatUint(o.seq, 10)) + if o.seq != nil { + m.Header.Set(ExpectedLastSeqHdr, strconv.FormatUint(*o.seq, 10)) } - if o.lss > 0 { - m.Header.Set(ExpectedLastSubjSeqHdr, strconv.FormatUint(o.lss, 10)) + if o.lss != nil { + m.Header.Set(ExpectedLastSubjSeqHdr, strconv.FormatUint(*o.lss, 10)) } var resp *Msg @@ -422,11 +439,35 @@ func (js *js) PublishMsg(m *Msg, opts ...PubOpt) (*PubAck, error) { } if err != nil { - if err == ErrNoResponders { - err = ErrNoStreamResponse + for r, ttl := 0, o.ttl; err == ErrNoResponders && (r < o.rnum || o.rnum < 0); r++ { + // To protect against small blips in leadership changes etc, if we get a no responders here retry. + if o.ctx != nil { + select { + case <-o.ctx.Done(): + case <-time.After(o.rwait): + } + } else { + time.Sleep(o.rwait) + } + if o.ttl > 0 { + ttl -= o.rwait + if ttl <= 0 { + err = ErrTimeout + break + } + resp, err = js.nc.RequestMsg(m, time.Duration(ttl)) + } else { + resp, err = js.nc.RequestMsgWithContext(o.ctx, m) + } + } + if err != nil { + if err == ErrNoResponders { + err = ErrNoStreamResponse + } + return nil, err } - return nil, err } + var pa pubAckResponse if err := json.Unmarshal(resp.Data, &pa); err != nil { return nil, ErrInvalidJSAck @@ -546,9 +587,9 @@ func (js *js) registerPAF(id string, paf *pubAckFuture) (int, int) { paf.js = js js.pafs[id] = paf np := len(js.pafs) - maxap := js.opts.maxap + maxpa := js.opts.maxpa js.mu.Unlock() - return np, maxap + return np, maxpa } // Lock should be held. @@ -600,7 +641,7 @@ func (js *js) handleAsyncReply(m *Msg) { delete(js.pafs, id) // Check on anyone stalled and waiting. - if js.stc != nil && len(js.pafs) < js.opts.maxap { + if js.stc != nil && len(js.pafs) < js.opts.maxpa { close(js.stc) js.stc = nil } @@ -671,7 +712,7 @@ func PublishAsyncMaxPending(max int) JSOpt { if max < 1 { return errors.New("nats: max ack pending should be >= 1") } - js.maxap = max + js.maxpa = max return nil }) } @@ -681,6 +722,8 @@ func (js *js) PublishAsync(subj string, data []byte, opts ...PubOpt) (PubAckFutu return js.PublishMsgAsync(&Msg{Subject: subj, Data: data}, opts...) } +const defaultStallWait = 200 * time.Millisecond + func (js *js) PublishMsgAsync(m *Msg, opts ...PubOpt) (PubAckFuture, error) { var o pubOpts if len(opts) > 0 { @@ -698,6 +741,10 @@ func (js *js) PublishMsgAsync(m *Msg, opts ...PubOpt) (PubAckFuture, error) { if o.ttl != 0 || o.ctx != nil { return nil, ErrContextAndTimeout } + stallWait := defaultStallWait + if o.stallWait > 0 { + stallWait = o.stallWait + } // FIXME(dlc) - Make common. if o.id != _EMPTY_ { @@ -709,11 +756,11 @@ func (js *js) PublishMsgAsync(m *Msg, opts ...PubOpt) (PubAckFuture, error) { if o.str != _EMPTY_ { m.Header.Set(ExpectedStreamHdr, o.str) } - if o.seq > 0 { - m.Header.Set(ExpectedLastSeqHdr, strconv.FormatUint(o.seq, 10)) + if o.seq != nil { + m.Header.Set(ExpectedLastSeqHdr, strconv.FormatUint(*o.seq, 10)) } - if o.lss > 0 { - m.Header.Set(ExpectedLastSubjSeqHdr, strconv.FormatUint(o.lss, 10)) + if o.lss != nil { + m.Header.Set(ExpectedLastSubjSeqHdr, strconv.FormatUint(*o.lss, 10)) } // Reply @@ -735,7 +782,7 @@ func (js *js) PublishMsgAsync(m *Msg, opts ...PubOpt) (PubAckFuture, error) { if maxPending > 0 && numPending >= maxPending { select { case <-js.asyncStall(): - case <-time.After(200 * time.Millisecond): + case <-time.After(stallWait): js.clearPAF(id) return nil, errors.New("nats: stalled with too many outstanding async published messages") } @@ -782,7 +829,7 @@ func ExpectStream(stream string) PubOpt { // ExpectLastSequence sets the expected sequence in the response from the publish. func ExpectLastSequence(seq uint64) PubOpt { return pubOptFn(func(opts *pubOpts) error { - opts.seq = seq + opts.seq = &seq return nil }) } @@ -790,7 +837,7 @@ func ExpectLastSequence(seq uint64) PubOpt { // ExpectLastSequencePerSubject sets the expected sequence per subject in the response from the publish. func ExpectLastSequencePerSubject(seq uint64) PubOpt { return pubOptFn(func(opts *pubOpts) error { - opts.lss = seq + opts.lss = &seq return nil }) } @@ -803,6 +850,33 @@ func ExpectLastMsgId(id string) PubOpt { }) } +// RetryWait sets the retry wait time when ErrNoResponders is encountered. +func RetryWait(dur time.Duration) PubOpt { + return pubOptFn(func(opts *pubOpts) error { + opts.rwait = dur + return nil + }) +} + +// RetryAttempts sets the retry number of attempts when ErrNoResponders is encountered. +func RetryAttempts(num int) PubOpt { + return pubOptFn(func(opts *pubOpts) error { + opts.rnum = num + return nil + }) +} + +// StallWait sets the max wait when the producer becomes stall producing messages. +func StallWait(ttl time.Duration) PubOpt { + return pubOptFn(func(opts *pubOpts) error { + if ttl <= 0 { + return fmt.Errorf("nats: stall wait should be more than 0") + } + opts.stallWait = ttl + return nil + }) +} + type ackOpts struct { ttl time.Duration ctx context.Context @@ -894,8 +968,6 @@ func (d nakDelay) configureAck(opts *ackOpts) error { type ConsumerConfig struct { Durable string `json:"durable_name,omitempty"` Description string `json:"description,omitempty"` - DeliverSubject string `json:"deliver_subject,omitempty"` - DeliverGroup string `json:"deliver_group,omitempty"` DeliverPolicy DeliverPolicy `json:"deliver_policy"` OptStartSeq uint64 `json:"opt_start_seq,omitempty"` OptStartTime *time.Time `json:"opt_start_time,omitempty"` @@ -917,8 +989,17 @@ type ConsumerConfig struct { MaxRequestBatch int `json:"max_batch,omitempty"` MaxRequestExpires time.Duration `json:"max_expires,omitempty"` + // Push based consumers. + DeliverSubject string `json:"deliver_subject,omitempty"` + DeliverGroup string `json:"deliver_group,omitempty"` + // Ephemeral inactivity threshold. InactiveThreshold time.Duration `json:"inactive_threshold,omitempty"` + + // Generally inherited by parent stream and other markers, now can be configured directly. + Replicas int `json:"num_replicas"` + // Force memory storage. + MemoryStorage bool `json:"mem_storage,omitempty"` } // ConsumerInfo is the info from a JetStream consumer. @@ -1476,7 +1557,10 @@ func (js *js) subscribe(subj, queue string, cb MsgHandler, ch chan *Msg, isSync, } if js.opts.shouldTrace { - js.opts.trace(TraceSent, ccSubj, j, nil) + ctrace := js.opts.ctrace + if ctrace.RequestSent != nil { + ctrace.RequestSent(ccSubj, j) + } } resp, err := nc.Request(ccSubj, j, js.opts.wait) if err != nil { @@ -1487,7 +1571,10 @@ func (js *js) subscribe(subj, queue string, cb MsgHandler, ch chan *Msg, isSync, return nil, err } if js.opts.shouldTrace { - js.opts.trace(TraceReceived, ccSubj, resp.Data, resp.Header) + ctrace := js.opts.ctrace + if ctrace.ResponseReceived != nil { + ctrace.ResponseReceived(ccSubj, resp.Data, resp.Header) + } } var cinfo consumerResponse @@ -2186,6 +2273,14 @@ func RateLimit(n uint64) SubOpt { }) } +// BackOff is an array of time durations that represent the time to delay based on delivery count. +func BackOff(backOff []time.Duration) SubOpt { + return subOptFn(func(opts *subOpts) error { + opts.cfg.BackOff = backOff + return nil + }) +} + // BindStream binds a consumer to a stream explicitly based on a name. // When a stream name is not specified, the library uses the subscribe // subject as a way to find the stream name. It is done by making a request @@ -2333,12 +2428,20 @@ func PullMaxWaiting(n int) SubOpt { }) } -var errNoMessages = errors.New("nats: no messages") +var ( + // errNoMessages is an error that a Fetch request using no_wait can receive to signal + // that there are no more messages available. + errNoMessages = errors.New("nats: no messages") + + // errRequestsPending is an error that represents a sub.Fetch requests that was using + // no_wait and expires time got discarded by the server. + errRequestsPending = errors.New("nats: requests pending") +) // Returns if the given message is a user message or not, and if // `checkSts` is true, returns appropriate error based on the // content of the status (404, etc..) -func checkMsg(msg *Msg, checkSts bool) (usrMsg bool, err error) { +func checkMsg(msg *Msg, checkSts, isNoWait bool) (usrMsg bool, err error) { // Assume user message usrMsg = true @@ -2367,11 +2470,17 @@ func checkMsg(msg *Msg, checkSts bool) (usrMsg bool, err error) { // 404 indicates that there are no messages. err = errNoMessages case reqTimeoutSts: - // Older servers may send a 408 when a request in the server was expired - // and interest is still found, which will be the case for our - // implementation. Regardless, ignore 408 errors until receiving at least - // one message. - err = ErrTimeout + // In case of a fetch request with no wait request and expires time, + // need to skip 408 errors and retry. + if isNoWait { + err = errRequestsPending + } else { + // Older servers may send a 408 when a request in the server was expired + // and interest is still found, which will be the case for our + // implementation. Regardless, ignore 408 errors until receiving at least + // one message when making requests without no_wait. + err = ErrTimeout + } default: err = fmt.Errorf("nats: %s", msg.Header.Get(descrHdr)) } @@ -2486,7 +2595,7 @@ func (sub *Subscription) Fetch(batch int, opts ...PullOpt) ([]*Msg, error) { // or status message, however, we don't care about values of status // messages at this point in the Fetch() call, so checkMsg can't // return an error. - if usrMsg, _ := checkMsg(msg, false); usrMsg { + if usrMsg, _ := checkMsg(msg, false, false); usrMsg { msgs = append(msgs, msg) } } @@ -2529,11 +2638,11 @@ func (sub *Subscription) Fetch(batch int, opts ...PullOpt) ([]*Msg, error) { if err == nil { var usrMsg bool - usrMsg, err = checkMsg(msg, true) + usrMsg, err = checkMsg(msg, true, noWait) if err == nil && usrMsg { msgs = append(msgs, msg) - } else if noWait && (err == errNoMessages) && len(msgs) == 0 { - // If we have a 404 for our "no_wait" request and have + } else if noWait && (err == errNoMessages || err == errRequestsPending) && len(msgs) == 0 { + // If we have a 404/408 for our "no_wait" request and have // not collected any message, then resend request to // wait this time. noWait = false @@ -2585,14 +2694,20 @@ func (js *js) getConsumerInfoContext(ctx context.Context, stream, consumer strin // a RequestWithContext with tracing via TraceCB func (js *js) apiRequestWithContext(ctx context.Context, subj string, data []byte) (*Msg, error) { if js.opts.shouldTrace { - js.opts.trace(TraceSent, subj, data, nil) + ctrace := js.opts.ctrace + if ctrace.RequestSent != nil { + ctrace.RequestSent(subj, data) + } } resp, err := js.nc.RequestWithContext(ctx, subj, data) if err != nil { return nil, err } if js.opts.shouldTrace { - js.opts.trace(TraceReceived, subj, resp.Data, resp.Header) + ctrace := js.opts.ctrace + if ctrace.RequestSent != nil { + ctrace.ResponseReceived(subj, resp.Data, resp.Header) + } } return resp, nil diff --git a/vendor/github.com/nats-io/nats.go/jsm.go b/vendor/github.com/nats-io/nats.go/jsm.go index 87ab37ce..6bfff673 100644 --- a/vendor/github.com/nats-io/nats.go/jsm.go +++ b/vendor/github.com/nats-io/nats.go/jsm.go @@ -100,6 +100,15 @@ type StreamConfig struct { DenyDelete bool `json:"deny_delete,omitempty"` DenyPurge bool `json:"deny_purge,omitempty"` AllowRollup bool `json:"allow_rollup_hdrs,omitempty"` + + // Allow republish of the message after being sequenced and stored. + RePublish *SubjectMapping `json:"republish,omitempty"` +} + +// SubjectMapping allows a source subject to be mapped to a destination subject for republishing. +type SubjectMapping struct { + Source string `json:"src,omitempty"` + Destination string `json:"dest"` } // Placement is used to guide placement of streams in clustered JetStream. @@ -229,6 +238,9 @@ type consumerResponse struct { // AddConsumer will add a JetStream consumer. func (js *js) AddConsumer(stream string, cfg *ConsumerConfig, opts ...JSOpt) (*ConsumerInfo, error) { + if err := checkStreamName(stream); err != nil { + return nil, err + } o, cancel, err := getJSContextOpts(js.opts, opts...) if err != nil { return nil, err @@ -237,9 +249,6 @@ func (js *js) AddConsumer(stream string, cfg *ConsumerConfig, opts ...JSOpt) (*C defer cancel() } - if stream == _EMPTY_ { - return nil, ErrStreamNameRequired - } req, err := json.Marshal(&createConsumerRequest{Stream: stream, Config: cfg}) if err != nil { return nil, err @@ -280,6 +289,9 @@ func (js *js) AddConsumer(stream string, cfg *ConsumerConfig, opts ...JSOpt) (*C } func (js *js) UpdateConsumer(stream string, cfg *ConsumerConfig, opts ...JSOpt) (*ConsumerInfo, error) { + if err := checkStreamName(stream); err != nil { + return nil, err + } if cfg == nil { return nil, ErrConsumerConfigRequired } @@ -295,8 +307,34 @@ type consumerDeleteResponse struct { Success bool `json:"success,omitempty"` } +func checkStreamName(stream string) error { + if stream == _EMPTY_ { + return ErrStreamNameRequired + } + if strings.Contains(stream, ".") { + return ErrInvalidStreamName + } + return nil +} + +func checkConsumerName(consumer string) error { + if consumer == _EMPTY_ { + return ErrConsumerNameRequired + } + if strings.Contains(consumer, ".") { + return ErrInvalidConsumerName + } + return nil +} + // DeleteConsumer deletes a Consumer. func (js *js) DeleteConsumer(stream, consumer string, opts ...JSOpt) error { + if err := checkStreamName(stream); err != nil { + return err + } + if err := checkConsumerName(consumer); err != nil { + return err + } o, cancel, err := getJSContextOpts(js.opts, opts...) if err != nil { return err @@ -305,10 +343,6 @@ func (js *js) DeleteConsumer(stream, consumer string, opts ...JSOpt) error { defer cancel() } - if stream == _EMPTY_ { - return ErrStreamNameRequired - } - dcSubj := js.apiSubj(fmt.Sprintf(apiConsumerDeleteT, stream, consumer)) r, err := js.apiRequestWithContext(o.ctx, dcSubj, nil) if err != nil { @@ -330,6 +364,12 @@ func (js *js) DeleteConsumer(stream, consumer string, opts ...JSOpt) error { // ConsumerInfo returns information about a Consumer. func (js *js) ConsumerInfo(stream, consumer string, opts ...JSOpt) (*ConsumerInfo, error) { + if err := checkStreamName(stream); err != nil { + return nil, err + } + if err := checkConsumerName(consumer); err != nil { + return nil, err + } o, cancel, err := getJSContextOpts(js.opts, opts...) if err != nil { return nil, err @@ -369,8 +409,8 @@ func (c *consumerLister) Next() bool { if c.err != nil { return false } - if c.stream == _EMPTY_ { - c.err = ErrStreamNameRequired + if err := checkStreamName(c.stream); err != nil { + c.err = err return false } if c.pageInfo != nil && c.offset >= c.pageInfo.Total { @@ -474,8 +514,8 @@ func (c *consumerNamesLister) Next() bool { if c.err != nil { return false } - if c.stream == _EMPTY_ { - c.err = ErrStreamNameRequired + if err := checkStreamName(c.stream); err != nil { + c.err = err return false } if c.pageInfo != nil && c.offset >= c.pageInfo.Total { @@ -556,6 +596,12 @@ type streamCreateResponse struct { } func (js *js) AddStream(cfg *StreamConfig, opts ...JSOpt) (*StreamInfo, error) { + if cfg == nil { + return nil, ErrStreamConfigRequired + } + if err := checkStreamName(cfg.Name); err != nil { + return nil, err + } o, cancel, err := getJSContextOpts(js.opts, opts...) if err != nil { return nil, err @@ -564,14 +610,6 @@ func (js *js) AddStream(cfg *StreamConfig, opts ...JSOpt) (*StreamInfo, error) { defer cancel() } - if cfg == nil || cfg.Name == _EMPTY_ { - return nil, ErrStreamNameRequired - } - - if strings.Contains(cfg.Name, ".") { - return nil, ErrInvalidStreamName - } - req, err := json.Marshal(cfg) if err != nil { return nil, err @@ -587,6 +625,9 @@ func (js *js) AddStream(cfg *StreamConfig, opts ...JSOpt) (*StreamInfo, error) { return nil, err } if resp.Error != nil { + if resp.Error.ErrorCode == 10058 { + return nil, ErrStreamNameAlreadyInUse + } return nil, errors.New(resp.Error.Description) } @@ -596,10 +637,9 @@ func (js *js) AddStream(cfg *StreamConfig, opts ...JSOpt) (*StreamInfo, error) { type streamInfoResponse = streamCreateResponse func (js *js) StreamInfo(stream string, opts ...JSOpt) (*StreamInfo, error) { - if strings.Contains(stream, ".") { - return nil, ErrInvalidStreamName + if err := checkStreamName(stream); err != nil { + return nil, err } - o, cancel, err := getJSContextOpts(js.opts, opts...) if err != nil { return nil, err @@ -621,7 +661,7 @@ func (js *js) StreamInfo(stream string, opts ...JSOpt) (*StreamInfo, error) { if resp.Error.Code == 404 { return nil, ErrStreamNotFound } - return nil, errors.New(resp.Error.Description) + return nil, fmt.Errorf("nats: %s", resp.Error.Description) } return resp.StreamInfo, nil @@ -675,6 +715,12 @@ type PeerInfo struct { // UpdateStream updates a Stream. func (js *js) UpdateStream(cfg *StreamConfig, opts ...JSOpt) (*StreamInfo, error) { + if cfg == nil { + return nil, ErrStreamConfigRequired + } + if err := checkStreamName(cfg.Name); err != nil { + return nil, err + } o, cancel, err := getJSContextOpts(js.opts, opts...) if err != nil { return nil, err @@ -683,10 +729,6 @@ func (js *js) UpdateStream(cfg *StreamConfig, opts ...JSOpt) (*StreamInfo, error defer cancel() } - if cfg == nil || cfg.Name == _EMPTY_ { - return nil, ErrStreamNameRequired - } - req, err := json.Marshal(cfg) if err != nil { return nil, err @@ -715,6 +757,9 @@ type streamDeleteResponse struct { // DeleteStream deletes a Stream. func (js *js) DeleteStream(name string, opts ...JSOpt) error { + if err := checkStreamName(name); err != nil { + return err + } o, cancel, err := getJSContextOpts(js.opts, opts...) if err != nil { return err @@ -723,10 +768,6 @@ func (js *js) DeleteStream(name string, opts ...JSOpt) error { defer cancel() } - if name == _EMPTY_ { - return ErrStreamNameRequired - } - dsSubj := js.apiSubj(fmt.Sprintf(apiStreamDeleteT, name)) r, err := js.apiRequestWithContext(o.ctx, dsSubj, nil) if err != nil { @@ -902,6 +943,9 @@ type streamPurgeResponse struct { // PurgeStream purges messages on a Stream. func (js *js) PurgeStream(stream string, opts ...JSOpt) error { + if err := checkStreamName(stream); err != nil { + return err + } return js.purgeStream(stream, nil) } diff --git a/vendor/github.com/nats-io/nats.go/kv.go b/vendor/github.com/nats-io/nats.go/kv.go index 0b75054d..d4171b55 100644 --- a/vendor/github.com/nats-io/nats.go/kv.go +++ b/vendor/github.com/nats-io/nats.go/kv.go @@ -17,6 +17,7 @@ import ( "context" "errors" "fmt" + "reflect" "regexp" "strconv" "strings" @@ -53,9 +54,9 @@ type KeyValue interface { // Update will update the value iff the latest revision matches. Update(key string, value []byte, last uint64) (revision uint64, err error) // Delete will place a delete marker and leave all revisions. - Delete(key string) error + Delete(key string, opts ...DeleteOpt) error // Purge will place a delete marker and remove all previous revisions. - Purge(key string) error + Purge(key string, opts ...DeleteOpt) error // Watch for any updates to keys that match the keys argument which could include wildcards. // Watch will send a nil entry when it has received all initial values. Watch(keys string, opts ...WatchOpt) (KeyWatcher, error) @@ -93,6 +94,8 @@ type KeyValueStatus interface { // KeyWatcher is what is returned when doing a watch. type KeyWatcher interface { + // Context returns watcher context optionally provided by nats.Context option. + Context() context.Context // Updates returns a channel to read any updates to entries. Updates() <-chan KeyValueEntry // Stop will stop this watcher. @@ -177,6 +180,40 @@ func (ctx ContextOpt) configurePurge(opts *purgeOpts) error { return nil } +type DeleteOpt interface { + configureDelete(opts *deleteOpts) error +} + +type deleteOpts struct { + // Remove all previous revisions. + purge bool + + // Delete only if the latest revision matches. + revision uint64 +} + +type deleteOptFn func(opts *deleteOpts) error + +func (opt deleteOptFn) configureDelete(opts *deleteOpts) error { + return opt(opts) +} + +// LastRevision deletes if the latest revision matches. +func LastRevision(revision uint64) DeleteOpt { + return deleteOptFn(func(opts *deleteOpts) error { + opts.revision = revision + return nil + }) +} + +// purge removes all previous revisions. +func purge() DeleteOpt { + return deleteOptFn(func(opts *deleteOpts) error { + opts.purge = true + return nil + }) +} + // KeyValueConfig is for configuring a KeyValue store. type KeyValueConfig struct { Bucket string @@ -187,6 +224,7 @@ type KeyValueConfig struct { MaxBytes int64 Storage StorageType Replicas int + Placement *Placement } // Used to watch all keys. @@ -326,18 +364,41 @@ func (js *js) CreateKeyValue(cfg *KeyValueConfig) (KeyValue, error) { replicas = 1 } + // We will set explicitly some values so that we can do comparison + // if we get an "already in use" error and need to check if it is same. + maxBytes := cfg.MaxBytes + if maxBytes == 0 { + maxBytes = -1 + } + maxMsgSize := cfg.MaxValueSize + if maxMsgSize == 0 { + maxMsgSize = -1 + } + // When stream's MaxAge is not set, server uses 2 minutes as the default + // for the duplicate window. If MaxAge is set, and lower than 2 minutes, + // then the duplicate window will be set to that. If MaxAge is greater, + // we will cap the duplicate window to 2 minutes (to be consistent with + // previous behavior). + duplicateWindow := 2 * time.Minute + if cfg.TTL > 0 && cfg.TTL < duplicateWindow { + duplicateWindow = cfg.TTL + } scfg := &StreamConfig{ Name: fmt.Sprintf(kvBucketNameTmpl, cfg.Bucket), Description: cfg.Description, Subjects: []string{fmt.Sprintf(kvSubjectsTmpl, cfg.Bucket)}, MaxMsgsPerSubject: history, - MaxBytes: cfg.MaxBytes, + MaxBytes: maxBytes, MaxAge: cfg.TTL, - MaxMsgSize: cfg.MaxValueSize, + MaxMsgSize: maxMsgSize, Storage: cfg.Storage, Replicas: replicas, + Placement: cfg.Placement, AllowRollup: true, DenyDelete: true, + Duplicates: duplicateWindow, + MaxMsgs: -1, + MaxConsumers: -1, } // If we are at server version 2.7.2 or above use DiscardNew. We can not use DiscardNew for 2.7.1 or below. @@ -346,7 +407,24 @@ func (js *js) CreateKeyValue(cfg *KeyValueConfig) (KeyValue, error) { } if _, err := js.AddStream(scfg); err != nil { - return nil, err + // If we have a failure to add, it could be because we have + // a config change if the KV was created against a pre 2.7.2 + // and we are now moving to a v2.7.2+. If that is the case + // and the only difference is the discard policy, then update + // the stream. + if err == ErrStreamNameAlreadyInUse { + if si, _ := js.StreamInfo(scfg.Name); si != nil { + // To compare, make the server's stream info discard + // policy same than ours. + si.Config.Discard = scfg.Discard + if reflect.DeepEqual(&si.Config, scfg) { + _, err = js.UpdateStream(scfg) + } + } + } + if err != nil { + return nil, err + } } kv := &kvs{ @@ -547,16 +625,7 @@ func (kv *kvs) Update(key string, value []byte, revision uint64) (uint64, error) } // Delete will place a delete marker and leave all revisions. -func (kv *kvs) Delete(key string) error { - return kv.delete(key, false) -} - -// Purge will remove the key and all revisions. -func (kv *kvs) Purge(key string) error { - return kv.delete(key, true) -} - -func (kv *kvs) delete(key string, purge bool) error { +func (kv *kvs) Delete(key string, opts ...DeleteOpt) error { if !keyValid(key) { return ErrInvalidKey } @@ -571,16 +640,35 @@ func (kv *kvs) delete(key string, purge bool) error { // DEL op marker. For watch functionality. m := NewMsg(b.String()) - if purge { + var o deleteOpts + for _, opt := range opts { + if opt != nil { + if err := opt.configureDelete(&o); err != nil { + return err + } + } + } + + if o.purge { m.Header.Set(kvop, kvpurge) m.Header.Set(MsgRollup, MsgRollupSubject) } else { m.Header.Set(kvop, kvdel) } + + if o.revision != 0 { + m.Header.Set(ExpectedLastSubjSeqHdr, strconv.FormatUint(o.revision, 10)) + } + _, err := kv.js.PublishMsg(m) return err } +// Purge will remove the key and all revisions. +func (kv *kvs) Purge(key string, opts ...DeleteOpt) error { + return kv.Delete(key, append(opts, purge())...) +} + const kvDefaultPurgeDeletesMarkerThreshold = 30 * time.Minute // PurgeDeletes will remove all current delete markers. @@ -701,6 +789,15 @@ type watcher struct { initDone bool initPending uint64 received uint64 + ctx context.Context +} + +// Context returns the context for the watcher if set. +func (w *watcher) Context() context.Context { + if w == nil { + return nil + } + return w.ctx } // Updates returns the interior channel. @@ -743,7 +840,7 @@ func (kv *kvs) Watch(keys string, opts ...WatchOpt) (KeyWatcher, error) { keys = b.String() // We will block below on placing items on the chan. That is by design. - w := &watcher{updates: make(chan KeyValueEntry, 256)} + w := &watcher{updates: make(chan KeyValueEntry, 256), ctx: o.ctx} update := func(m *Msg) { tokens, err := getMetadataFields(m.Reply) diff --git a/vendor/github.com/nats-io/nats.go/nats.go b/vendor/github.com/nats-io/nats.go/nats.go index 7f2804fa..90f301f1 100644 --- a/vendor/github.com/nats-io/nats.go/nats.go +++ b/vendor/github.com/nats-io/nats.go/nats.go @@ -48,7 +48,7 @@ import ( // Default Constants const ( - Version = "1.13.0" + Version = "1.16.0" DefaultURL = "nats://127.0.0.1:4222" DefaultPort = 4222 DefaultMaxReconnect = 60 @@ -83,6 +83,9 @@ const ( // ACCOUNT_AUTHENTICATION_EXPIRED_ERR is for when nats server account authorization has expired. ACCOUNT_AUTHENTICATION_EXPIRED_ERR = "account authentication expired" + + // MAX_CONNECTIONS_ERR is for when nats server denies the connection due to server max_connections limit + MAX_CONNECTIONS_ERR = "maximum connections exceeded" ) // Errors @@ -142,11 +145,13 @@ var ( ErrNotJSMessage = errors.New("nats: not a jetstream message") ErrInvalidStreamName = errors.New("nats: invalid stream name") ErrInvalidDurableName = errors.New("nats: invalid durable name") + ErrInvalidConsumerName = errors.New("nats: invalid consumer name") ErrNoMatchingStream = errors.New("nats: no stream matches subject") ErrSubjectMismatch = errors.New("nats: subject does not match consumer") ErrContextAndTimeout = errors.New("nats: context and timeout can not both be set") ErrInvalidJSAck = errors.New("nats: invalid jetstream publish response") ErrMultiStreamUnsupported = errors.New("nats: multiple streams are not supported") + ErrStreamConfigRequired = errors.New("nats: stream configuration is required") ErrStreamNameRequired = errors.New("nats: stream name is required") ErrStreamNotFound = errors.New("nats: stream not found") ErrConsumerNotFound = errors.New("nats: consumer not found") @@ -160,6 +165,8 @@ var ( ErrMsgNotFound = errors.New("nats: message not found") ErrMsgAlreadyAckd = errors.New("nats: message was already acknowledged") ErrStreamInfoMaxSubjects = errors.New("nats: subject details would exceed maximum allowed") + ErrStreamNameAlreadyInUse = errors.New("nats: stream name already in use") + ErrMaxConnectionsExceeded = errors.New("nats: server maximum connections exceeded") ) func init() { @@ -460,6 +467,10 @@ type Options struct { // supports compression. If the server does too, then data will be compressed. Compression bool + // For websocket connections, adds a path to connections url. + // This is useful when connecting to NATS behind a proxy. + ProxyPath string + // InboxPrefix allows the default _INBOX prefix to be customized InboxPrefix string } @@ -1140,6 +1151,15 @@ func Compression(enabled bool) Option { } } +// ProxyPath is an option for websocket connections that adds a path to connections url. +// This is useful when connecting to NATS behind a proxy. +func ProxyPath(path string) Option { + return func(o *Options) error { + o.ProxyPath = path + return nil + } +} + // CustomInboxPrefix configures the request + reply inbox prefix func CustomInboxPrefix(p string) Option { return func(o *Options) error { @@ -1649,6 +1669,17 @@ func (w *natsWriter) doneWithPending() { w.pending = nil } +// Notify the reader that we are done with the connect, where "read" operations +// happen synchronously and under the connection lock. After this point, "read" +// will be happening from the read loop, without the connection lock. +// +// Note: this runs under the connection lock. +func (r *natsReader) doneWithConnect() { + if wsr, ok := r.r.(*websocketReader); ok { + wsr.doneWithConnect() + } +} + func (r *natsReader) Read() ([]byte, error) { if r.off >= 0 { off := r.off @@ -1803,6 +1834,21 @@ func (nc *Conn) ConnectedUrl() string { return nc.current.url.String() } +// ConnectedUrlRedacted reports the connected server's URL with passwords redacted +func (nc *Conn) ConnectedUrlRedacted() string { + if nc == nil { + return _EMPTY_ + } + + nc.mu.RLock() + defer nc.mu.RUnlock() + + if nc.status != CONNECTED { + return _EMPTY_ + } + return nc.current.url.Redacted() +} + // ConnectedAddr returns the connected server's IP func (nc *Conn) ConnectedAddr() string { if nc == nil { @@ -1962,6 +2008,10 @@ func (nc *Conn) processConnectInit() error { go nc.readLoop() go nc.flusher() + // Notify the reader that we are done with the connect handshake, where + // reads were done synchronously and under the connection lock. + nc.br.doneWithConnect() + return nil } @@ -3182,6 +3232,8 @@ func (nc *Conn) processErr(ie string) { // FIXME(dlc) - process Slow Consumer signals special. if e == STALE_CONNECTION { nc.processOpErr(ErrStaleConnection) + } else if e == MAX_CONNECTIONS_ERR { + nc.processOpErr(ErrMaxConnectionsExceeded) } else if strings.HasPrefix(e, PERMISSIONS_ERR) { nc.processPermissionsViolation(ne) } else if authErr := checkAuthError(e); authErr != nil { @@ -3352,21 +3404,10 @@ func (nc *Conn) PublishMsg(m *Msg) error { if m == nil { return ErrInvalidMsg } - - var hdr []byte - var err error - - if len(m.Header) > 0 { - if !nc.info.Headers { - return ErrHeadersNotSupported - } - - hdr, err = m.headerBytes() - if err != nil { - return err - } + hdr, err := m.headerBytes() + if err != nil { + return err } - return nc.publish(m.Subject, m.Reply, hdr, m.Data) } @@ -3392,6 +3433,12 @@ func (nc *Conn) publish(subj, reply string, hdr, data []byte) error { } nc.mu.Lock() + // Check if headers attempted to be sent to server that does not support them. + if len(hdr) > 0 && !nc.info.Headers { + nc.mu.Unlock() + return ErrHeadersNotSupported + } + if nc.isClosed() { nc.mu.Unlock() return ErrConnectionClosed @@ -3562,17 +3609,12 @@ func (nc *Conn) createNewRequestAndSend(subj string, hdr, data []byte) (chan *Ms // RequestMsg will send a request payload including optional headers and deliver // the response message, or an error, including a timeout if no message was received properly. func (nc *Conn) RequestMsg(msg *Msg, timeout time.Duration) (*Msg, error) { - var hdr []byte - var err error - - if len(msg.Header) > 0 { - if !nc.info.Headers { - return nil, ErrHeadersNotSupported - } - hdr, err = msg.headerBytes() - if err != nil { - return nil, err - } + if msg == nil { + return nil, ErrInvalidMsg + } + hdr, err := msg.headerBytes() + if err != nil { + return nil, err } return nc.request(msg.Subject, hdr, msg.Data, timeout) diff --git a/vendor/github.com/nats-io/nats.go/object.go b/vendor/github.com/nats-io/nats.go/object.go index 4de788bc..bc68ca4b 100644 --- a/vendor/github.com/nats-io/nats.go/object.go +++ b/vendor/github.com/nats-io/nats.go/object.go @@ -1,4 +1,4 @@ -// Copyright 2021 The NATS Authors +// Copyright 2021-2022 The NATS Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at @@ -136,8 +136,10 @@ type ObjectStoreConfig struct { Bucket string Description string TTL time.Duration + MaxBytes int64 Storage StorageType Replicas int + Placement *Placement } type ObjectStoreStatus interface { @@ -243,8 +245,10 @@ func (js *js) CreateObjectStore(cfg *ObjectStoreConfig) (ObjectStore, error) { Description: cfg.Description, Subjects: []string{chunks, meta}, MaxAge: cfg.TTL, + MaxBytes: cfg.MaxBytes, Storage: cfg.Storage, Replicas: cfg.Replicas, + Placement: cfg.Placement, Discard: DiscardNew, AllowRollup: true, } diff --git a/vendor/github.com/nats-io/nats.go/ws.go b/vendor/github.com/nats-io/nats.go/ws.go index 2ef3f7f4..a76e5822 100644 --- a/vendor/github.com/nats-io/nats.go/ws.go +++ b/vendor/github.com/nats-io/nats.go/ws.go @@ -81,6 +81,7 @@ type websocketReader struct { ib []byte ff bool fc bool + nl bool dc *wsDecompressor nc *Conn } @@ -180,6 +181,15 @@ func wsNewReader(r io.Reader) *websocketReader { return &websocketReader{r: r, ff: true} } +// From now on, reads will be from the readLoop and we will need to +// acquire the connection lock should we have to send/write a control +// message from handleControlFrame. +// +// Note: this runs under the connection lock. +func (r *websocketReader) doneWithConnect() { + r.nl = true +} + func (r *websocketReader) Read(p []byte) (int, error) { var err error var buf []byte @@ -402,12 +412,12 @@ func (r *websocketReader) handleControlFrame(frameType wsOpCode, buf []byte, pos } } } - r.nc.wsEnqueueCloseMsg(status, body) + r.nc.wsEnqueueCloseMsg(r.nl, status, body) // Return io.EOF so that readLoop will close the connection as client closed // after processing pending buffers. return pos, io.EOF case wsPingMessage: - r.nc.wsEnqueueControlMsg(wsPongMessage, payload) + r.nc.wsEnqueueControlMsg(r.nl, wsPongMessage, payload) case wsPongMessage: // Nothing to do.. } @@ -560,6 +570,15 @@ func (nc *Conn) wsInitHandshake(u *url.URL) error { scheme = "https" } ustr := fmt.Sprintf("%s://%s", scheme, u.Host) + + if nc.Opts.ProxyPath != "" { + proxyPath := nc.Opts.ProxyPath + if !strings.HasPrefix(proxyPath, "/") { + proxyPath = "/" + proxyPath + } + ustr += proxyPath + } + u, err = url.Parse(ustr) if err != nil { return err @@ -644,14 +663,16 @@ func (nc *Conn) wsClose() { nc.wsEnqueueCloseMsgLocked(wsCloseStatusNormalClosure, _EMPTY_) } -func (nc *Conn) wsEnqueueCloseMsg(status int, payload string) { +func (nc *Conn) wsEnqueueCloseMsg(needsLock bool, status int, payload string) { // In some low-level unit tests it will happen... if nc == nil { return } - nc.mu.Lock() + if needsLock { + nc.mu.Lock() + defer nc.mu.Unlock() + } nc.wsEnqueueCloseMsgLocked(status, payload) - nc.mu.Unlock() } func (nc *Conn) wsEnqueueCloseMsgLocked(status int, payload string) { @@ -675,25 +696,26 @@ func (nc *Conn) wsEnqueueCloseMsgLocked(status int, payload string) { nc.bw.flush() } -func (nc *Conn) wsEnqueueControlMsg(frameType wsOpCode, payload []byte) { +func (nc *Conn) wsEnqueueControlMsg(needsLock bool, frameType wsOpCode, payload []byte) { // In some low-level unit tests it will happen... if nc == nil { return } - fh, key := wsCreateFrameHeader(false, frameType, len(payload)) - nc.mu.Lock() + if needsLock { + nc.mu.Lock() + defer nc.mu.Unlock() + } wr, ok := nc.bw.w.(*websocketWriter) if !ok { - nc.mu.Unlock() return } + fh, key := wsCreateFrameHeader(false, frameType, len(payload)) wr.ctrlFrames = append(wr.ctrlFrames, fh) if len(payload) > 0 { wsMaskBuf(key, payload) wr.ctrlFrames = append(wr.ctrlFrames, payload) } nc.bw.flush() - nc.mu.Unlock() } func wsPMCExtensionSupport(header http.Header) (bool, bool) { diff --git a/vendor/golang.org/x/crypto/ed25519/ed25519.go b/vendor/golang.org/x/crypto/ed25519/ed25519.go index 71ad917d..a7828345 100644 --- a/vendor/golang.org/x/crypto/ed25519/ed25519.go +++ b/vendor/golang.org/x/crypto/ed25519/ed25519.go @@ -1,13 +1,7 @@ -// Copyright 2016 The Go Authors. All rights reserved. +// Copyright 2019 The Go Authors. All rights reserved. // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -// In Go 1.13, the ed25519 package was promoted to the standard library as -// crypto/ed25519, and this package became a wrapper for the standard library one. -// -//go:build !go1.13 -// +build !go1.13 - // Package ed25519 implements the Ed25519 signature algorithm. See // https://ed25519.cr.yp.to/. // @@ -16,21 +10,15 @@ // representation includes a public key suffix to make multiple signing // operations with the same key more efficient. This package refers to the RFC // 8032 private key as the “seed”. +// +// Beginning with Go 1.13, the functionality of this package was moved to the +// standard library as crypto/ed25519. This package only acts as a compatibility +// wrapper. package ed25519 -// This code is a port of the public domain, “ref10” implementation of ed25519 -// from SUPERCOP. - import ( - "bytes" - "crypto" - cryptorand "crypto/rand" - "crypto/sha512" - "errors" + "crypto/ed25519" "io" - "strconv" - - "golang.org/x/crypto/ed25519/internal/edwards25519" ) const ( @@ -45,57 +33,21 @@ const ( ) // PublicKey is the type of Ed25519 public keys. -type PublicKey []byte +// +// This type is an alias for crypto/ed25519's PublicKey type. +// See the crypto/ed25519 package for the methods on this type. +type PublicKey = ed25519.PublicKey // PrivateKey is the type of Ed25519 private keys. It implements crypto.Signer. -type PrivateKey []byte - -// Public returns the PublicKey corresponding to priv. -func (priv PrivateKey) Public() crypto.PublicKey { - publicKey := make([]byte, PublicKeySize) - copy(publicKey, priv[32:]) - return PublicKey(publicKey) -} - -// Seed returns the private key seed corresponding to priv. It is provided for -// interoperability with RFC 8032. RFC 8032's private keys correspond to seeds -// in this package. -func (priv PrivateKey) Seed() []byte { - seed := make([]byte, SeedSize) - copy(seed, priv[:32]) - return seed -} - -// Sign signs the given message with priv. -// Ed25519 performs two passes over messages to be signed and therefore cannot -// handle pre-hashed messages. Thus opts.HashFunc() must return zero to -// indicate the message hasn't been hashed. This can be achieved by passing -// crypto.Hash(0) as the value for opts. -func (priv PrivateKey) Sign(rand io.Reader, message []byte, opts crypto.SignerOpts) (signature []byte, err error) { - if opts.HashFunc() != crypto.Hash(0) { - return nil, errors.New("ed25519: cannot sign hashed message") - } - - return Sign(priv, message), nil -} +// +// This type is an alias for crypto/ed25519's PrivateKey type. +// See the crypto/ed25519 package for the methods on this type. +type PrivateKey = ed25519.PrivateKey // GenerateKey generates a public/private key pair using entropy from rand. // If rand is nil, crypto/rand.Reader will be used. func GenerateKey(rand io.Reader) (PublicKey, PrivateKey, error) { - if rand == nil { - rand = cryptorand.Reader - } - - seed := make([]byte, SeedSize) - if _, err := io.ReadFull(rand, seed); err != nil { - return nil, nil, err - } - - privateKey := NewKeyFromSeed(seed) - publicKey := make([]byte, PublicKeySize) - copy(publicKey, privateKey[32:]) - - return publicKey, privateKey, nil + return ed25519.GenerateKey(rand) } // NewKeyFromSeed calculates a private key from a seed. It will panic if @@ -103,121 +55,17 @@ func GenerateKey(rand io.Reader) (PublicKey, PrivateKey, error) { // with RFC 8032. RFC 8032's private keys correspond to seeds in this // package. func NewKeyFromSeed(seed []byte) PrivateKey { - if l := len(seed); l != SeedSize { - panic("ed25519: bad seed length: " + strconv.Itoa(l)) - } - - digest := sha512.Sum512(seed) - digest[0] &= 248 - digest[31] &= 127 - digest[31] |= 64 - - var A edwards25519.ExtendedGroupElement - var hBytes [32]byte - copy(hBytes[:], digest[:]) - edwards25519.GeScalarMultBase(&A, &hBytes) - var publicKeyBytes [32]byte - A.ToBytes(&publicKeyBytes) - - privateKey := make([]byte, PrivateKeySize) - copy(privateKey, seed) - copy(privateKey[32:], publicKeyBytes[:]) - - return privateKey + return ed25519.NewKeyFromSeed(seed) } // Sign signs the message with privateKey and returns a signature. It will // panic if len(privateKey) is not PrivateKeySize. func Sign(privateKey PrivateKey, message []byte) []byte { - if l := len(privateKey); l != PrivateKeySize { - panic("ed25519: bad private key length: " + strconv.Itoa(l)) - } - - h := sha512.New() - h.Write(privateKey[:32]) - - var digest1, messageDigest, hramDigest [64]byte - var expandedSecretKey [32]byte - h.Sum(digest1[:0]) - copy(expandedSecretKey[:], digest1[:]) - expandedSecretKey[0] &= 248 - expandedSecretKey[31] &= 63 - expandedSecretKey[31] |= 64 - - h.Reset() - h.Write(digest1[32:]) - h.Write(message) - h.Sum(messageDigest[:0]) - - var messageDigestReduced [32]byte - edwards25519.ScReduce(&messageDigestReduced, &messageDigest) - var R edwards25519.ExtendedGroupElement - edwards25519.GeScalarMultBase(&R, &messageDigestReduced) - - var encodedR [32]byte - R.ToBytes(&encodedR) - - h.Reset() - h.Write(encodedR[:]) - h.Write(privateKey[32:]) - h.Write(message) - h.Sum(hramDigest[:0]) - var hramDigestReduced [32]byte - edwards25519.ScReduce(&hramDigestReduced, &hramDigest) - - var s [32]byte - edwards25519.ScMulAdd(&s, &hramDigestReduced, &expandedSecretKey, &messageDigestReduced) - - signature := make([]byte, SignatureSize) - copy(signature[:], encodedR[:]) - copy(signature[32:], s[:]) - - return signature + return ed25519.Sign(privateKey, message) } // Verify reports whether sig is a valid signature of message by publicKey. It // will panic if len(publicKey) is not PublicKeySize. func Verify(publicKey PublicKey, message, sig []byte) bool { - if l := len(publicKey); l != PublicKeySize { - panic("ed25519: bad public key length: " + strconv.Itoa(l)) - } - - if len(sig) != SignatureSize || sig[63]&224 != 0 { - return false - } - - var A edwards25519.ExtendedGroupElement - var publicKeyBytes [32]byte - copy(publicKeyBytes[:], publicKey) - if !A.FromBytes(&publicKeyBytes) { - return false - } - edwards25519.FeNeg(&A.X, &A.X) - edwards25519.FeNeg(&A.T, &A.T) - - h := sha512.New() - h.Write(sig[:32]) - h.Write(publicKey[:]) - h.Write(message) - var digest [64]byte - h.Sum(digest[:0]) - - var hReduced [32]byte - edwards25519.ScReduce(&hReduced, &digest) - - var R edwards25519.ProjectiveGroupElement - var s [32]byte - copy(s[:], sig[32:]) - - // https://tools.ietf.org/html/rfc8032#section-5.1.7 requires that s be in - // the range [0, order) in order to prevent signature malleability. - if !edwards25519.ScMinimal(&s) { - return false - } - - edwards25519.GeDoubleScalarMultVartime(&R, &hReduced, &A, &s) - - var checkR [32]byte - R.ToBytes(&checkR) - return bytes.Equal(sig[:32], checkR[:]) + return ed25519.Verify(publicKey, message, sig) } diff --git a/vendor/golang.org/x/crypto/ed25519/ed25519_go113.go b/vendor/golang.org/x/crypto/ed25519/ed25519_go113.go deleted file mode 100644 index b5974dc8..00000000 --- a/vendor/golang.org/x/crypto/ed25519/ed25519_go113.go +++ /dev/null @@ -1,74 +0,0 @@ -// Copyright 2019 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build go1.13 -// +build go1.13 - -// Package ed25519 implements the Ed25519 signature algorithm. See -// https://ed25519.cr.yp.to/. -// -// These functions are also compatible with the “Ed25519” function defined in -// RFC 8032. However, unlike RFC 8032's formulation, this package's private key -// representation includes a public key suffix to make multiple signing -// operations with the same key more efficient. This package refers to the RFC -// 8032 private key as the “seed”. -// -// Beginning with Go 1.13, the functionality of this package was moved to the -// standard library as crypto/ed25519. This package only acts as a compatibility -// wrapper. -package ed25519 - -import ( - "crypto/ed25519" - "io" -) - -const ( - // PublicKeySize is the size, in bytes, of public keys as used in this package. - PublicKeySize = 32 - // PrivateKeySize is the size, in bytes, of private keys as used in this package. - PrivateKeySize = 64 - // SignatureSize is the size, in bytes, of signatures generated and verified by this package. - SignatureSize = 64 - // SeedSize is the size, in bytes, of private key seeds. These are the private key representations used by RFC 8032. - SeedSize = 32 -) - -// PublicKey is the type of Ed25519 public keys. -// -// This type is an alias for crypto/ed25519's PublicKey type. -// See the crypto/ed25519 package for the methods on this type. -type PublicKey = ed25519.PublicKey - -// PrivateKey is the type of Ed25519 private keys. It implements crypto.Signer. -// -// This type is an alias for crypto/ed25519's PrivateKey type. -// See the crypto/ed25519 package for the methods on this type. -type PrivateKey = ed25519.PrivateKey - -// GenerateKey generates a public/private key pair using entropy from rand. -// If rand is nil, crypto/rand.Reader will be used. -func GenerateKey(rand io.Reader) (PublicKey, PrivateKey, error) { - return ed25519.GenerateKey(rand) -} - -// NewKeyFromSeed calculates a private key from a seed. It will panic if -// len(seed) is not SeedSize. This function is provided for interoperability -// with RFC 8032. RFC 8032's private keys correspond to seeds in this -// package. -func NewKeyFromSeed(seed []byte) PrivateKey { - return ed25519.NewKeyFromSeed(seed) -} - -// Sign signs the message with privateKey and returns a signature. It will -// panic if len(privateKey) is not PrivateKeySize. -func Sign(privateKey PrivateKey, message []byte) []byte { - return ed25519.Sign(privateKey, message) -} - -// Verify reports whether sig is a valid signature of message by publicKey. It -// will panic if len(publicKey) is not PublicKeySize. -func Verify(publicKey PublicKey, message, sig []byte) bool { - return ed25519.Verify(publicKey, message, sig) -} diff --git a/vendor/golang.org/x/crypto/ed25519/internal/edwards25519/const.go b/vendor/golang.org/x/crypto/ed25519/internal/edwards25519/const.go deleted file mode 100644 index e39f086c..00000000 --- a/vendor/golang.org/x/crypto/ed25519/internal/edwards25519/const.go +++ /dev/null @@ -1,1422 +0,0 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package edwards25519 - -// These values are from the public domain, “ref10” implementation of ed25519 -// from SUPERCOP. - -// d is a constant in the Edwards curve equation. -var d = FieldElement{ - -10913610, 13857413, -15372611, 6949391, 114729, -8787816, -6275908, -3247719, -18696448, -12055116, -} - -// d2 is 2*d. -var d2 = FieldElement{ - -21827239, -5839606, -30745221, 13898782, 229458, 15978800, -12551817, -6495438, 29715968, 9444199, -} - -// SqrtM1 is the square-root of -1 in the field. -var SqrtM1 = FieldElement{ - -32595792, -7943725, 9377950, 3500415, 12389472, -272473, -25146209, -2005654, 326686, 11406482, -} - -// A is a constant in the Montgomery-form of curve25519. -var A = FieldElement{ - 486662, 0, 0, 0, 0, 0, 0, 0, 0, 0, -} - -// bi contains precomputed multiples of the base-point. See the Ed25519 paper -// for a discussion about how these values are used. -var bi = [8]PreComputedGroupElement{ - { - FieldElement{25967493, -14356035, 29566456, 3660896, -12694345, 4014787, 27544626, -11754271, -6079156, 2047605}, - FieldElement{-12545711, 934262, -2722910, 3049990, -727428, 9406986, 12720692, 5043384, 19500929, -15469378}, - FieldElement{-8738181, 4489570, 9688441, -14785194, 10184609, -12363380, 29287919, 11864899, -24514362, -4438546}, - }, - { - FieldElement{15636291, -9688557, 24204773, -7912398, 616977, -16685262, 27787600, -14772189, 28944400, -1550024}, - FieldElement{16568933, 4717097, -11556148, -1102322, 15682896, -11807043, 16354577, -11775962, 7689662, 11199574}, - FieldElement{30464156, -5976125, -11779434, -15670865, 23220365, 15915852, 7512774, 10017326, -17749093, -9920357}, - }, - { - FieldElement{10861363, 11473154, 27284546, 1981175, -30064349, 12577861, 32867885, 14515107, -15438304, 10819380}, - FieldElement{4708026, 6336745, 20377586, 9066809, -11272109, 6594696, -25653668, 12483688, -12668491, 5581306}, - FieldElement{19563160, 16186464, -29386857, 4097519, 10237984, -4348115, 28542350, 13850243, -23678021, -15815942}, - }, - { - FieldElement{5153746, 9909285, 1723747, -2777874, 30523605, 5516873, 19480852, 5230134, -23952439, -15175766}, - FieldElement{-30269007, -3463509, 7665486, 10083793, 28475525, 1649722, 20654025, 16520125, 30598449, 7715701}, - FieldElement{28881845, 14381568, 9657904, 3680757, -20181635, 7843316, -31400660, 1370708, 29794553, -1409300}, - }, - { - FieldElement{-22518993, -6692182, 14201702, -8745502, -23510406, 8844726, 18474211, -1361450, -13062696, 13821877}, - FieldElement{-6455177, -7839871, 3374702, -4740862, -27098617, -10571707, 31655028, -7212327, 18853322, -14220951}, - FieldElement{4566830, -12963868, -28974889, -12240689, -7602672, -2830569, -8514358, -10431137, 2207753, -3209784}, - }, - { - FieldElement{-25154831, -4185821, 29681144, 7868801, -6854661, -9423865, -12437364, -663000, -31111463, -16132436}, - FieldElement{25576264, -2703214, 7349804, -11814844, 16472782, 9300885, 3844789, 15725684, 171356, 6466918}, - FieldElement{23103977, 13316479, 9739013, -16149481, 817875, -15038942, 8965339, -14088058, -30714912, 16193877}, - }, - { - FieldElement{-33521811, 3180713, -2394130, 14003687, -16903474, -16270840, 17238398, 4729455, -18074513, 9256800}, - FieldElement{-25182317, -4174131, 32336398, 5036987, -21236817, 11360617, 22616405, 9761698, -19827198, 630305}, - FieldElement{-13720693, 2639453, -24237460, -7406481, 9494427, -5774029, -6554551, -15960994, -2449256, -14291300}, - }, - { - FieldElement{-3151181, -5046075, 9282714, 6866145, -31907062, -863023, -18940575, 15033784, 25105118, -7894876}, - FieldElement{-24326370, 15950226, -31801215, -14592823, -11662737, -5090925, 1573892, -2625887, 2198790, -15804619}, - FieldElement{-3099351, 10324967, -2241613, 7453183, -5446979, -2735503, -13812022, -16236442, -32461234, -12290683}, - }, -} - -// base contains precomputed multiples of the base-point. See the Ed25519 paper -// for a discussion about how these values are used. -var base = [32][8]PreComputedGroupElement{ - { - { - FieldElement{25967493, -14356035, 29566456, 3660896, -12694345, 4014787, 27544626, -11754271, -6079156, 2047605}, - FieldElement{-12545711, 934262, -2722910, 3049990, -727428, 9406986, 12720692, 5043384, 19500929, -15469378}, - FieldElement{-8738181, 4489570, 9688441, -14785194, 10184609, -12363380, 29287919, 11864899, -24514362, -4438546}, - }, - { - FieldElement{-12815894, -12976347, -21581243, 11784320, -25355658, -2750717, -11717903, -3814571, -358445, -10211303}, - FieldElement{-21703237, 6903825, 27185491, 6451973, -29577724, -9554005, -15616551, 11189268, -26829678, -5319081}, - FieldElement{26966642, 11152617, 32442495, 15396054, 14353839, -12752335, -3128826, -9541118, -15472047, -4166697}, - }, - { - FieldElement{15636291, -9688557, 24204773, -7912398, 616977, -16685262, 27787600, -14772189, 28944400, -1550024}, - FieldElement{16568933, 4717097, -11556148, -1102322, 15682896, -11807043, 16354577, -11775962, 7689662, 11199574}, - FieldElement{30464156, -5976125, -11779434, -15670865, 23220365, 15915852, 7512774, 10017326, -17749093, -9920357}, - }, - { - FieldElement{-17036878, 13921892, 10945806, -6033431, 27105052, -16084379, -28926210, 15006023, 3284568, -6276540}, - FieldElement{23599295, -8306047, -11193664, -7687416, 13236774, 10506355, 7464579, 9656445, 13059162, 10374397}, - FieldElement{7798556, 16710257, 3033922, 2874086, 28997861, 2835604, 32406664, -3839045, -641708, -101325}, - }, - { - FieldElement{10861363, 11473154, 27284546, 1981175, -30064349, 12577861, 32867885, 14515107, -15438304, 10819380}, - FieldElement{4708026, 6336745, 20377586, 9066809, -11272109, 6594696, -25653668, 12483688, -12668491, 5581306}, - FieldElement{19563160, 16186464, -29386857, 4097519, 10237984, -4348115, 28542350, 13850243, -23678021, -15815942}, - }, - { - FieldElement{-15371964, -12862754, 32573250, 4720197, -26436522, 5875511, -19188627, -15224819, -9818940, -12085777}, - FieldElement{-8549212, 109983, 15149363, 2178705, 22900618, 4543417, 3044240, -15689887, 1762328, 14866737}, - FieldElement{-18199695, -15951423, -10473290, 1707278, -17185920, 3916101, -28236412, 3959421, 27914454, 4383652}, - }, - { - FieldElement{5153746, 9909285, 1723747, -2777874, 30523605, 5516873, 19480852, 5230134, -23952439, -15175766}, - FieldElement{-30269007, -3463509, 7665486, 10083793, 28475525, 1649722, 20654025, 16520125, 30598449, 7715701}, - FieldElement{28881845, 14381568, 9657904, 3680757, -20181635, 7843316, -31400660, 1370708, 29794553, -1409300}, - }, - { - FieldElement{14499471, -2729599, -33191113, -4254652, 28494862, 14271267, 30290735, 10876454, -33154098, 2381726}, - FieldElement{-7195431, -2655363, -14730155, 462251, -27724326, 3941372, -6236617, 3696005, -32300832, 15351955}, - FieldElement{27431194, 8222322, 16448760, -3907995, -18707002, 11938355, -32961401, -2970515, 29551813, 10109425}, - }, - }, - { - { - FieldElement{-13657040, -13155431, -31283750, 11777098, 21447386, 6519384, -2378284, -1627556, 10092783, -4764171}, - FieldElement{27939166, 14210322, 4677035, 16277044, -22964462, -12398139, -32508754, 12005538, -17810127, 12803510}, - FieldElement{17228999, -15661624, -1233527, 300140, -1224870, -11714777, 30364213, -9038194, 18016357, 4397660}, - }, - { - FieldElement{-10958843, -7690207, 4776341, -14954238, 27850028, -15602212, -26619106, 14544525, -17477504, 982639}, - FieldElement{29253598, 15796703, -2863982, -9908884, 10057023, 3163536, 7332899, -4120128, -21047696, 9934963}, - FieldElement{5793303, 16271923, -24131614, -10116404, 29188560, 1206517, -14747930, 4559895, -30123922, -10897950}, - }, - { - FieldElement{-27643952, -11493006, 16282657, -11036493, 28414021, -15012264, 24191034, 4541697, -13338309, 5500568}, - FieldElement{12650548, -1497113, 9052871, 11355358, -17680037, -8400164, -17430592, 12264343, 10874051, 13524335}, - FieldElement{25556948, -3045990, 714651, 2510400, 23394682, -10415330, 33119038, 5080568, -22528059, 5376628}, - }, - { - FieldElement{-26088264, -4011052, -17013699, -3537628, -6726793, 1920897, -22321305, -9447443, 4535768, 1569007}, - FieldElement{-2255422, 14606630, -21692440, -8039818, 28430649, 8775819, -30494562, 3044290, 31848280, 12543772}, - FieldElement{-22028579, 2943893, -31857513, 6777306, 13784462, -4292203, -27377195, -2062731, 7718482, 14474653}, - }, - { - FieldElement{2385315, 2454213, -22631320, 46603, -4437935, -15680415, 656965, -7236665, 24316168, -5253567}, - FieldElement{13741529, 10911568, -33233417, -8603737, -20177830, -1033297, 33040651, -13424532, -20729456, 8321686}, - FieldElement{21060490, -2212744, 15712757, -4336099, 1639040, 10656336, 23845965, -11874838, -9984458, 608372}, - }, - { - FieldElement{-13672732, -15087586, -10889693, -7557059, -6036909, 11305547, 1123968, -6780577, 27229399, 23887}, - FieldElement{-23244140, -294205, -11744728, 14712571, -29465699, -2029617, 12797024, -6440308, -1633405, 16678954}, - FieldElement{-29500620, 4770662, -16054387, 14001338, 7830047, 9564805, -1508144, -4795045, -17169265, 4904953}, - }, - { - FieldElement{24059557, 14617003, 19037157, -15039908, 19766093, -14906429, 5169211, 16191880, 2128236, -4326833}, - FieldElement{-16981152, 4124966, -8540610, -10653797, 30336522, -14105247, -29806336, 916033, -6882542, -2986532}, - FieldElement{-22630907, 12419372, -7134229, -7473371, -16478904, 16739175, 285431, 2763829, 15736322, 4143876}, - }, - { - FieldElement{2379352, 11839345, -4110402, -5988665, 11274298, 794957, 212801, -14594663, 23527084, -16458268}, - FieldElement{33431127, -11130478, -17838966, -15626900, 8909499, 8376530, -32625340, 4087881, -15188911, -14416214}, - FieldElement{1767683, 7197987, -13205226, -2022635, -13091350, 448826, 5799055, 4357868, -4774191, -16323038}, - }, - }, - { - { - FieldElement{6721966, 13833823, -23523388, -1551314, 26354293, -11863321, 23365147, -3949732, 7390890, 2759800}, - FieldElement{4409041, 2052381, 23373853, 10530217, 7676779, -12885954, 21302353, -4264057, 1244380, -12919645}, - FieldElement{-4421239, 7169619, 4982368, -2957590, 30256825, -2777540, 14086413, 9208236, 15886429, 16489664}, - }, - { - FieldElement{1996075, 10375649, 14346367, 13311202, -6874135, -16438411, -13693198, 398369, -30606455, -712933}, - FieldElement{-25307465, 9795880, -2777414, 14878809, -33531835, 14780363, 13348553, 12076947, -30836462, 5113182}, - FieldElement{-17770784, 11797796, 31950843, 13929123, -25888302, 12288344, -30341101, -7336386, 13847711, 5387222}, - }, - { - FieldElement{-18582163, -3416217, 17824843, -2340966, 22744343, -10442611, 8763061, 3617786, -19600662, 10370991}, - FieldElement{20246567, -14369378, 22358229, -543712, 18507283, -10413996, 14554437, -8746092, 32232924, 16763880}, - FieldElement{9648505, 10094563, 26416693, 14745928, -30374318, -6472621, 11094161, 15689506, 3140038, -16510092}, - }, - { - FieldElement{-16160072, 5472695, 31895588, 4744994, 8823515, 10365685, -27224800, 9448613, -28774454, 366295}, - FieldElement{19153450, 11523972, -11096490, -6503142, -24647631, 5420647, 28344573, 8041113, 719605, 11671788}, - FieldElement{8678025, 2694440, -6808014, 2517372, 4964326, 11152271, -15432916, -15266516, 27000813, -10195553}, - }, - { - FieldElement{-15157904, 7134312, 8639287, -2814877, -7235688, 10421742, 564065, 5336097, 6750977, -14521026}, - FieldElement{11836410, -3979488, 26297894, 16080799, 23455045, 15735944, 1695823, -8819122, 8169720, 16220347}, - FieldElement{-18115838, 8653647, 17578566, -6092619, -8025777, -16012763, -11144307, -2627664, -5990708, -14166033}, - }, - { - FieldElement{-23308498, -10968312, 15213228, -10081214, -30853605, -11050004, 27884329, 2847284, 2655861, 1738395}, - FieldElement{-27537433, -14253021, -25336301, -8002780, -9370762, 8129821, 21651608, -3239336, -19087449, -11005278}, - FieldElement{1533110, 3437855, 23735889, 459276, 29970501, 11335377, 26030092, 5821408, 10478196, 8544890}, - }, - { - FieldElement{32173121, -16129311, 24896207, 3921497, 22579056, -3410854, 19270449, 12217473, 17789017, -3395995}, - FieldElement{-30552961, -2228401, -15578829, -10147201, 13243889, 517024, 15479401, -3853233, 30460520, 1052596}, - FieldElement{-11614875, 13323618, 32618793, 8175907, -15230173, 12596687, 27491595, -4612359, 3179268, -9478891}, - }, - { - FieldElement{31947069, -14366651, -4640583, -15339921, -15125977, -6039709, -14756777, -16411740, 19072640, -9511060}, - FieldElement{11685058, 11822410, 3158003, -13952594, 33402194, -4165066, 5977896, -5215017, 473099, 5040608}, - FieldElement{-20290863, 8198642, -27410132, 11602123, 1290375, -2799760, 28326862, 1721092, -19558642, -3131606}, - }, - }, - { - { - FieldElement{7881532, 10687937, 7578723, 7738378, -18951012, -2553952, 21820786, 8076149, -27868496, 11538389}, - FieldElement{-19935666, 3899861, 18283497, -6801568, -15728660, -11249211, 8754525, 7446702, -5676054, 5797016}, - FieldElement{-11295600, -3793569, -15782110, -7964573, 12708869, -8456199, 2014099, -9050574, -2369172, -5877341}, - }, - { - FieldElement{-22472376, -11568741, -27682020, 1146375, 18956691, 16640559, 1192730, -3714199, 15123619, 10811505}, - FieldElement{14352098, -3419715, -18942044, 10822655, 32750596, 4699007, -70363, 15776356, -28886779, -11974553}, - FieldElement{-28241164, -8072475, -4978962, -5315317, 29416931, 1847569, -20654173, -16484855, 4714547, -9600655}, - }, - { - FieldElement{15200332, 8368572, 19679101, 15970074, -31872674, 1959451, 24611599, -4543832, -11745876, 12340220}, - FieldElement{12876937, -10480056, 33134381, 6590940, -6307776, 14872440, 9613953, 8241152, 15370987, 9608631}, - FieldElement{-4143277, -12014408, 8446281, -391603, 4407738, 13629032, -7724868, 15866074, -28210621, -8814099}, - }, - { - FieldElement{26660628, -15677655, 8393734, 358047, -7401291, 992988, -23904233, 858697, 20571223, 8420556}, - FieldElement{14620715, 13067227, -15447274, 8264467, 14106269, 15080814, 33531827, 12516406, -21574435, -12476749}, - FieldElement{236881, 10476226, 57258, -14677024, 6472998, 2466984, 17258519, 7256740, 8791136, 15069930}, - }, - { - FieldElement{1276410, -9371918, 22949635, -16322807, -23493039, -5702186, 14711875, 4874229, -30663140, -2331391}, - FieldElement{5855666, 4990204, -13711848, 7294284, -7804282, 1924647, -1423175, -7912378, -33069337, 9234253}, - FieldElement{20590503, -9018988, 31529744, -7352666, -2706834, 10650548, 31559055, -11609587, 18979186, 13396066}, - }, - { - FieldElement{24474287, 4968103, 22267082, 4407354, 24063882, -8325180, -18816887, 13594782, 33514650, 7021958}, - FieldElement{-11566906, -6565505, -21365085, 15928892, -26158305, 4315421, -25948728, -3916677, -21480480, 12868082}, - FieldElement{-28635013, 13504661, 19988037, -2132761, 21078225, 6443208, -21446107, 2244500, -12455797, -8089383}, - }, - { - FieldElement{-30595528, 13793479, -5852820, 319136, -25723172, -6263899, 33086546, 8957937, -15233648, 5540521}, - FieldElement{-11630176, -11503902, -8119500, -7643073, 2620056, 1022908, -23710744, -1568984, -16128528, -14962807}, - FieldElement{23152971, 775386, 27395463, 14006635, -9701118, 4649512, 1689819, 892185, -11513277, -15205948}, - }, - { - FieldElement{9770129, 9586738, 26496094, 4324120, 1556511, -3550024, 27453819, 4763127, -19179614, 5867134}, - FieldElement{-32765025, 1927590, 31726409, -4753295, 23962434, -16019500, 27846559, 5931263, -29749703, -16108455}, - FieldElement{27461885, -2977536, 22380810, 1815854, -23033753, -3031938, 7283490, -15148073, -19526700, 7734629}, - }, - }, - { - { - FieldElement{-8010264, -9590817, -11120403, 6196038, 29344158, -13430885, 7585295, -3176626, 18549497, 15302069}, - FieldElement{-32658337, -6171222, -7672793, -11051681, 6258878, 13504381, 10458790, -6418461, -8872242, 8424746}, - FieldElement{24687205, 8613276, -30667046, -3233545, 1863892, -1830544, 19206234, 7134917, -11284482, -828919}, - }, - { - FieldElement{11334899, -9218022, 8025293, 12707519, 17523892, -10476071, 10243738, -14685461, -5066034, 16498837}, - FieldElement{8911542, 6887158, -9584260, -6958590, 11145641, -9543680, 17303925, -14124238, 6536641, 10543906}, - FieldElement{-28946384, 15479763, -17466835, 568876, -1497683, 11223454, -2669190, -16625574, -27235709, 8876771}, - }, - { - FieldElement{-25742899, -12566864, -15649966, -846607, -33026686, -796288, -33481822, 15824474, -604426, -9039817}, - FieldElement{10330056, 70051, 7957388, -9002667, 9764902, 15609756, 27698697, -4890037, 1657394, 3084098}, - FieldElement{10477963, -7470260, 12119566, -13250805, 29016247, -5365589, 31280319, 14396151, -30233575, 15272409}, - }, - { - FieldElement{-12288309, 3169463, 28813183, 16658753, 25116432, -5630466, -25173957, -12636138, -25014757, 1950504}, - FieldElement{-26180358, 9489187, 11053416, -14746161, -31053720, 5825630, -8384306, -8767532, 15341279, 8373727}, - FieldElement{28685821, 7759505, -14378516, -12002860, -31971820, 4079242, 298136, -10232602, -2878207, 15190420}, - }, - { - FieldElement{-32932876, 13806336, -14337485, -15794431, -24004620, 10940928, 8669718, 2742393, -26033313, -6875003}, - FieldElement{-1580388, -11729417, -25979658, -11445023, -17411874, -10912854, 9291594, -16247779, -12154742, 6048605}, - FieldElement{-30305315, 14843444, 1539301, 11864366, 20201677, 1900163, 13934231, 5128323, 11213262, 9168384}, - }, - { - FieldElement{-26280513, 11007847, 19408960, -940758, -18592965, -4328580, -5088060, -11105150, 20470157, -16398701}, - FieldElement{-23136053, 9282192, 14855179, -15390078, -7362815, -14408560, -22783952, 14461608, 14042978, 5230683}, - FieldElement{29969567, -2741594, -16711867, -8552442, 9175486, -2468974, 21556951, 3506042, -5933891, -12449708}, - }, - { - FieldElement{-3144746, 8744661, 19704003, 4581278, -20430686, 6830683, -21284170, 8971513, -28539189, 15326563}, - FieldElement{-19464629, 10110288, -17262528, -3503892, -23500387, 1355669, -15523050, 15300988, -20514118, 9168260}, - FieldElement{-5353335, 4488613, -23803248, 16314347, 7780487, -15638939, -28948358, 9601605, 33087103, -9011387}, - }, - { - FieldElement{-19443170, -15512900, -20797467, -12445323, -29824447, 10229461, -27444329, -15000531, -5996870, 15664672}, - FieldElement{23294591, -16632613, -22650781, -8470978, 27844204, 11461195, 13099750, -2460356, 18151676, 13417686}, - FieldElement{-24722913, -4176517, -31150679, 5988919, -26858785, 6685065, 1661597, -12551441, 15271676, -15452665}, - }, - }, - { - { - FieldElement{11433042, -13228665, 8239631, -5279517, -1985436, -725718, -18698764, 2167544, -6921301, -13440182}, - FieldElement{-31436171, 15575146, 30436815, 12192228, -22463353, 9395379, -9917708, -8638997, 12215110, 12028277}, - FieldElement{14098400, 6555944, 23007258, 5757252, -15427832, -12950502, 30123440, 4617780, -16900089, -655628}, - }, - { - FieldElement{-4026201, -15240835, 11893168, 13718664, -14809462, 1847385, -15819999, 10154009, 23973261, -12684474}, - FieldElement{-26531820, -3695990, -1908898, 2534301, -31870557, -16550355, 18341390, -11419951, 32013174, -10103539}, - FieldElement{-25479301, 10876443, -11771086, -14625140, -12369567, 1838104, 21911214, 6354752, 4425632, -837822}, - }, - { - FieldElement{-10433389, -14612966, 22229858, -3091047, -13191166, 776729, -17415375, -12020462, 4725005, 14044970}, - FieldElement{19268650, -7304421, 1555349, 8692754, -21474059, -9910664, 6347390, -1411784, -19522291, -16109756}, - FieldElement{-24864089, 12986008, -10898878, -5558584, -11312371, -148526, 19541418, 8180106, 9282262, 10282508}, - }, - { - FieldElement{-26205082, 4428547, -8661196, -13194263, 4098402, -14165257, 15522535, 8372215, 5542595, -10702683}, - FieldElement{-10562541, 14895633, 26814552, -16673850, -17480754, -2489360, -2781891, 6993761, -18093885, 10114655}, - FieldElement{-20107055, -929418, 31422704, 10427861, -7110749, 6150669, -29091755, -11529146, 25953725, -106158}, - }, - { - FieldElement{-4234397, -8039292, -9119125, 3046000, 2101609, -12607294, 19390020, 6094296, -3315279, 12831125}, - FieldElement{-15998678, 7578152, 5310217, 14408357, -33548620, -224739, 31575954, 6326196, 7381791, -2421839}, - FieldElement{-20902779, 3296811, 24736065, -16328389, 18374254, 7318640, 6295303, 8082724, -15362489, 12339664}, - }, - { - FieldElement{27724736, 2291157, 6088201, -14184798, 1792727, 5857634, 13848414, 15768922, 25091167, 14856294}, - FieldElement{-18866652, 8331043, 24373479, 8541013, -701998, -9269457, 12927300, -12695493, -22182473, -9012899}, - FieldElement{-11423429, -5421590, 11632845, 3405020, 30536730, -11674039, -27260765, 13866390, 30146206, 9142070}, - }, - { - FieldElement{3924129, -15307516, -13817122, -10054960, 12291820, -668366, -27702774, 9326384, -8237858, 4171294}, - FieldElement{-15921940, 16037937, 6713787, 16606682, -21612135, 2790944, 26396185, 3731949, 345228, -5462949}, - FieldElement{-21327538, 13448259, 25284571, 1143661, 20614966, -8849387, 2031539, -12391231, -16253183, -13582083}, - }, - { - FieldElement{31016211, -16722429, 26371392, -14451233, -5027349, 14854137, 17477601, 3842657, 28012650, -16405420}, - FieldElement{-5075835, 9368966, -8562079, -4600902, -15249953, 6970560, -9189873, 16292057, -8867157, 3507940}, - FieldElement{29439664, 3537914, 23333589, 6997794, -17555561, -11018068, -15209202, -15051267, -9164929, 6580396}, - }, - }, - { - { - FieldElement{-12185861, -7679788, 16438269, 10826160, -8696817, -6235611, 17860444, -9273846, -2095802, 9304567}, - FieldElement{20714564, -4336911, 29088195, 7406487, 11426967, -5095705, 14792667, -14608617, 5289421, -477127}, - FieldElement{-16665533, -10650790, -6160345, -13305760, 9192020, -1802462, 17271490, 12349094, 26939669, -3752294}, - }, - { - FieldElement{-12889898, 9373458, 31595848, 16374215, 21471720, 13221525, -27283495, -12348559, -3698806, 117887}, - FieldElement{22263325, -6560050, 3984570, -11174646, -15114008, -566785, 28311253, 5358056, -23319780, 541964}, - FieldElement{16259219, 3261970, 2309254, -15534474, -16885711, -4581916, 24134070, -16705829, -13337066, -13552195}, - }, - { - FieldElement{9378160, -13140186, -22845982, -12745264, 28198281, -7244098, -2399684, -717351, 690426, 14876244}, - FieldElement{24977353, -314384, -8223969, -13465086, 28432343, -1176353, -13068804, -12297348, -22380984, 6618999}, - FieldElement{-1538174, 11685646, 12944378, 13682314, -24389511, -14413193, 8044829, -13817328, 32239829, -5652762}, - }, - { - FieldElement{-18603066, 4762990, -926250, 8885304, -28412480, -3187315, 9781647, -10350059, 32779359, 5095274}, - FieldElement{-33008130, -5214506, -32264887, -3685216, 9460461, -9327423, -24601656, 14506724, 21639561, -2630236}, - FieldElement{-16400943, -13112215, 25239338, 15531969, 3987758, -4499318, -1289502, -6863535, 17874574, 558605}, - }, - { - FieldElement{-13600129, 10240081, 9171883, 16131053, -20869254, 9599700, 33499487, 5080151, 2085892, 5119761}, - FieldElement{-22205145, -2519528, -16381601, 414691, -25019550, 2170430, 30634760, -8363614, -31999993, -5759884}, - FieldElement{-6845704, 15791202, 8550074, -1312654, 29928809, -12092256, 27534430, -7192145, -22351378, 12961482}, - }, - { - FieldElement{-24492060, -9570771, 10368194, 11582341, -23397293, -2245287, 16533930, 8206996, -30194652, -5159638}, - FieldElement{-11121496, -3382234, 2307366, 6362031, -135455, 8868177, -16835630, 7031275, 7589640, 8945490}, - FieldElement{-32152748, 8917967, 6661220, -11677616, -1192060, -15793393, 7251489, -11182180, 24099109, -14456170}, - }, - { - FieldElement{5019558, -7907470, 4244127, -14714356, -26933272, 6453165, -19118182, -13289025, -6231896, -10280736}, - FieldElement{10853594, 10721687, 26480089, 5861829, -22995819, 1972175, -1866647, -10557898, -3363451, -6441124}, - FieldElement{-17002408, 5906790, 221599, -6563147, 7828208, -13248918, 24362661, -2008168, -13866408, 7421392}, - }, - { - FieldElement{8139927, -6546497, 32257646, -5890546, 30375719, 1886181, -21175108, 15441252, 28826358, -4123029}, - FieldElement{6267086, 9695052, 7709135, -16603597, -32869068, -1886135, 14795160, -7840124, 13746021, -1742048}, - FieldElement{28584902, 7787108, -6732942, -15050729, 22846041, -7571236, -3181936, -363524, 4771362, -8419958}, - }, - }, - { - { - FieldElement{24949256, 6376279, -27466481, -8174608, -18646154, -9930606, 33543569, -12141695, 3569627, 11342593}, - FieldElement{26514989, 4740088, 27912651, 3697550, 19331575, -11472339, 6809886, 4608608, 7325975, -14801071}, - FieldElement{-11618399, -14554430, -24321212, 7655128, -1369274, 5214312, -27400540, 10258390, -17646694, -8186692}, - }, - { - FieldElement{11431204, 15823007, 26570245, 14329124, 18029990, 4796082, -31446179, 15580664, 9280358, -3973687}, - FieldElement{-160783, -10326257, -22855316, -4304997, -20861367, -13621002, -32810901, -11181622, -15545091, 4387441}, - FieldElement{-20799378, 12194512, 3937617, -5805892, -27154820, 9340370, -24513992, 8548137, 20617071, -7482001}, - }, - { - FieldElement{-938825, -3930586, -8714311, 16124718, 24603125, -6225393, -13775352, -11875822, 24345683, 10325460}, - FieldElement{-19855277, -1568885, -22202708, 8714034, 14007766, 6928528, 16318175, -1010689, 4766743, 3552007}, - FieldElement{-21751364, -16730916, 1351763, -803421, -4009670, 3950935, 3217514, 14481909, 10988822, -3994762}, - }, - { - FieldElement{15564307, -14311570, 3101243, 5684148, 30446780, -8051356, 12677127, -6505343, -8295852, 13296005}, - FieldElement{-9442290, 6624296, -30298964, -11913677, -4670981, -2057379, 31521204, 9614054, -30000824, 12074674}, - FieldElement{4771191, -135239, 14290749, -13089852, 27992298, 14998318, -1413936, -1556716, 29832613, -16391035}, - }, - { - FieldElement{7064884, -7541174, -19161962, -5067537, -18891269, -2912736, 25825242, 5293297, -27122660, 13101590}, - FieldElement{-2298563, 2439670, -7466610, 1719965, -27267541, -16328445, 32512469, -5317593, -30356070, -4190957}, - FieldElement{-30006540, 10162316, -33180176, 3981723, -16482138, -13070044, 14413974, 9515896, 19568978, 9628812}, - }, - { - FieldElement{33053803, 199357, 15894591, 1583059, 27380243, -4580435, -17838894, -6106839, -6291786, 3437740}, - FieldElement{-18978877, 3884493, 19469877, 12726490, 15913552, 13614290, -22961733, 70104, 7463304, 4176122}, - FieldElement{-27124001, 10659917, 11482427, -16070381, 12771467, -6635117, -32719404, -5322751, 24216882, 5944158}, - }, - { - FieldElement{8894125, 7450974, -2664149, -9765752, -28080517, -12389115, 19345746, 14680796, 11632993, 5847885}, - FieldElement{26942781, -2315317, 9129564, -4906607, 26024105, 11769399, -11518837, 6367194, -9727230, 4782140}, - FieldElement{19916461, -4828410, -22910704, -11414391, 25606324, -5972441, 33253853, 8220911, 6358847, -1873857}, - }, - { - FieldElement{801428, -2081702, 16569428, 11065167, 29875704, 96627, 7908388, -4480480, -13538503, 1387155}, - FieldElement{19646058, 5720633, -11416706, 12814209, 11607948, 12749789, 14147075, 15156355, -21866831, 11835260}, - FieldElement{19299512, 1155910, 28703737, 14890794, 2925026, 7269399, 26121523, 15467869, -26560550, 5052483}, - }, - }, - { - { - FieldElement{-3017432, 10058206, 1980837, 3964243, 22160966, 12322533, -6431123, -12618185, 12228557, -7003677}, - FieldElement{32944382, 14922211, -22844894, 5188528, 21913450, -8719943, 4001465, 13238564, -6114803, 8653815}, - FieldElement{22865569, -4652735, 27603668, -12545395, 14348958, 8234005, 24808405, 5719875, 28483275, 2841751}, - }, - { - FieldElement{-16420968, -1113305, -327719, -12107856, 21886282, -15552774, -1887966, -315658, 19932058, -12739203}, - FieldElement{-11656086, 10087521, -8864888, -5536143, -19278573, -3055912, 3999228, 13239134, -4777469, -13910208}, - FieldElement{1382174, -11694719, 17266790, 9194690, -13324356, 9720081, 20403944, 11284705, -14013818, 3093230}, - }, - { - FieldElement{16650921, -11037932, -1064178, 1570629, -8329746, 7352753, -302424, 16271225, -24049421, -6691850}, - FieldElement{-21911077, -5927941, -4611316, -5560156, -31744103, -10785293, 24123614, 15193618, -21652117, -16739389}, - FieldElement{-9935934, -4289447, -25279823, 4372842, 2087473, 10399484, 31870908, 14690798, 17361620, 11864968}, - }, - { - FieldElement{-11307610, 6210372, 13206574, 5806320, -29017692, -13967200, -12331205, -7486601, -25578460, -16240689}, - FieldElement{14668462, -12270235, 26039039, 15305210, 25515617, 4542480, 10453892, 6577524, 9145645, -6443880}, - FieldElement{5974874, 3053895, -9433049, -10385191, -31865124, 3225009, -7972642, 3936128, -5652273, -3050304}, - }, - { - FieldElement{30625386, -4729400, -25555961, -12792866, -20484575, 7695099, 17097188, -16303496, -27999779, 1803632}, - FieldElement{-3553091, 9865099, -5228566, 4272701, -5673832, -16689700, 14911344, 12196514, -21405489, 7047412}, - FieldElement{20093277, 9920966, -11138194, -5343857, 13161587, 12044805, -32856851, 4124601, -32343828, -10257566}, - }, - { - FieldElement{-20788824, 14084654, -13531713, 7842147, 19119038, -13822605, 4752377, -8714640, -21679658, 2288038}, - FieldElement{-26819236, -3283715, 29965059, 3039786, -14473765, 2540457, 29457502, 14625692, -24819617, 12570232}, - FieldElement{-1063558, -11551823, 16920318, 12494842, 1278292, -5869109, -21159943, -3498680, -11974704, 4724943}, - }, - { - FieldElement{17960970, -11775534, -4140968, -9702530, -8876562, -1410617, -12907383, -8659932, -29576300, 1903856}, - FieldElement{23134274, -14279132, -10681997, -1611936, 20684485, 15770816, -12989750, 3190296, 26955097, 14109738}, - FieldElement{15308788, 5320727, -30113809, -14318877, 22902008, 7767164, 29425325, -11277562, 31960942, 11934971}, - }, - { - FieldElement{-27395711, 8435796, 4109644, 12222639, -24627868, 14818669, 20638173, 4875028, 10491392, 1379718}, - FieldElement{-13159415, 9197841, 3875503, -8936108, -1383712, -5879801, 33518459, 16176658, 21432314, 12180697}, - FieldElement{-11787308, 11500838, 13787581, -13832590, -22430679, 10140205, 1465425, 12689540, -10301319, -13872883}, - }, - }, - { - { - FieldElement{5414091, -15386041, -21007664, 9643570, 12834970, 1186149, -2622916, -1342231, 26128231, 6032912}, - FieldElement{-26337395, -13766162, 32496025, -13653919, 17847801, -12669156, 3604025, 8316894, -25875034, -10437358}, - FieldElement{3296484, 6223048, 24680646, -12246460, -23052020, 5903205, -8862297, -4639164, 12376617, 3188849}, - }, - { - FieldElement{29190488, -14659046, 27549113, -1183516, 3520066, -10697301, 32049515, -7309113, -16109234, -9852307}, - FieldElement{-14744486, -9309156, 735818, -598978, -20407687, -5057904, 25246078, -15795669, 18640741, -960977}, - FieldElement{-6928835, -16430795, 10361374, 5642961, 4910474, 12345252, -31638386, -494430, 10530747, 1053335}, - }, - { - FieldElement{-29265967, -14186805, -13538216, -12117373, -19457059, -10655384, -31462369, -2948985, 24018831, 15026644}, - FieldElement{-22592535, -3145277, -2289276, 5953843, -13440189, 9425631, 25310643, 13003497, -2314791, -15145616}, - FieldElement{-27419985, -603321, -8043984, -1669117, -26092265, 13987819, -27297622, 187899, -23166419, -2531735}, - }, - { - FieldElement{-21744398, -13810475, 1844840, 5021428, -10434399, -15911473, 9716667, 16266922, -5070217, 726099}, - FieldElement{29370922, -6053998, 7334071, -15342259, 9385287, 2247707, -13661962, -4839461, 30007388, -15823341}, - FieldElement{-936379, 16086691, 23751945, -543318, -1167538, -5189036, 9137109, 730663, 9835848, 4555336}, - }, - { - FieldElement{-23376435, 1410446, -22253753, -12899614, 30867635, 15826977, 17693930, 544696, -11985298, 12422646}, - FieldElement{31117226, -12215734, -13502838, 6561947, -9876867, -12757670, -5118685, -4096706, 29120153, 13924425}, - FieldElement{-17400879, -14233209, 19675799, -2734756, -11006962, -5858820, -9383939, -11317700, 7240931, -237388}, - }, - { - FieldElement{-31361739, -11346780, -15007447, -5856218, -22453340, -12152771, 1222336, 4389483, 3293637, -15551743}, - FieldElement{-16684801, -14444245, 11038544, 11054958, -13801175, -3338533, -24319580, 7733547, 12796905, -6335822}, - FieldElement{-8759414, -10817836, -25418864, 10783769, -30615557, -9746811, -28253339, 3647836, 3222231, -11160462}, - }, - { - FieldElement{18606113, 1693100, -25448386, -15170272, 4112353, 10045021, 23603893, -2048234, -7550776, 2484985}, - FieldElement{9255317, -3131197, -12156162, -1004256, 13098013, -9214866, 16377220, -2102812, -19802075, -3034702}, - FieldElement{-22729289, 7496160, -5742199, 11329249, 19991973, -3347502, -31718148, 9936966, -30097688, -10618797}, - }, - { - FieldElement{21878590, -5001297, 4338336, 13643897, -3036865, 13160960, 19708896, 5415497, -7360503, -4109293}, - FieldElement{27736861, 10103576, 12500508, 8502413, -3413016, -9633558, 10436918, -1550276, -23659143, -8132100}, - FieldElement{19492550, -12104365, -29681976, -852630, -3208171, 12403437, 30066266, 8367329, 13243957, 8709688}, - }, - }, - { - { - FieldElement{12015105, 2801261, 28198131, 10151021, 24818120, -4743133, -11194191, -5645734, 5150968, 7274186}, - FieldElement{2831366, -12492146, 1478975, 6122054, 23825128, -12733586, 31097299, 6083058, 31021603, -9793610}, - FieldElement{-2529932, -2229646, 445613, 10720828, -13849527, -11505937, -23507731, 16354465, 15067285, -14147707}, - }, - { - FieldElement{7840942, 14037873, -33364863, 15934016, -728213, -3642706, 21403988, 1057586, -19379462, -12403220}, - FieldElement{915865, -16469274, 15608285, -8789130, -24357026, 6060030, -17371319, 8410997, -7220461, 16527025}, - FieldElement{32922597, -556987, 20336074, -16184568, 10903705, -5384487, 16957574, 52992, 23834301, 6588044}, - }, - { - FieldElement{32752030, 11232950, 3381995, -8714866, 22652988, -10744103, 17159699, 16689107, -20314580, -1305992}, - FieldElement{-4689649, 9166776, -25710296, -10847306, 11576752, 12733943, 7924251, -2752281, 1976123, -7249027}, - FieldElement{21251222, 16309901, -2983015, -6783122, 30810597, 12967303, 156041, -3371252, 12331345, -8237197}, - }, - { - FieldElement{8651614, -4477032, -16085636, -4996994, 13002507, 2950805, 29054427, -5106970, 10008136, -4667901}, - FieldElement{31486080, 15114593, -14261250, 12951354, 14369431, -7387845, 16347321, -13662089, 8684155, -10532952}, - FieldElement{19443825, 11385320, 24468943, -9659068, -23919258, 2187569, -26263207, -6086921, 31316348, 14219878}, - }, - { - FieldElement{-28594490, 1193785, 32245219, 11392485, 31092169, 15722801, 27146014, 6992409, 29126555, 9207390}, - FieldElement{32382935, 1110093, 18477781, 11028262, -27411763, -7548111, -4980517, 10843782, -7957600, -14435730}, - FieldElement{2814918, 7836403, 27519878, -7868156, -20894015, -11553689, -21494559, 8550130, 28346258, 1994730}, - }, - { - FieldElement{-19578299, 8085545, -14000519, -3948622, 2785838, -16231307, -19516951, 7174894, 22628102, 8115180}, - FieldElement{-30405132, 955511, -11133838, -15078069, -32447087, -13278079, -25651578, 3317160, -9943017, 930272}, - FieldElement{-15303681, -6833769, 28856490, 1357446, 23421993, 1057177, 24091212, -1388970, -22765376, -10650715}, - }, - { - FieldElement{-22751231, -5303997, -12907607, -12768866, -15811511, -7797053, -14839018, -16554220, -1867018, 8398970}, - FieldElement{-31969310, 2106403, -4736360, 1362501, 12813763, 16200670, 22981545, -6291273, 18009408, -15772772}, - FieldElement{-17220923, -9545221, -27784654, 14166835, 29815394, 7444469, 29551787, -3727419, 19288549, 1325865}, - }, - { - FieldElement{15100157, -15835752, -23923978, -1005098, -26450192, 15509408, 12376730, -3479146, 33166107, -8042750}, - FieldElement{20909231, 13023121, -9209752, 16251778, -5778415, -8094914, 12412151, 10018715, 2213263, -13878373}, - FieldElement{32529814, -11074689, 30361439, -16689753, -9135940, 1513226, 22922121, 6382134, -5766928, 8371348}, - }, - }, - { - { - FieldElement{9923462, 11271500, 12616794, 3544722, -29998368, -1721626, 12891687, -8193132, -26442943, 10486144}, - FieldElement{-22597207, -7012665, 8587003, -8257861, 4084309, -12970062, 361726, 2610596, -23921530, -11455195}, - FieldElement{5408411, -1136691, -4969122, 10561668, 24145918, 14240566, 31319731, -4235541, 19985175, -3436086}, - }, - { - FieldElement{-13994457, 16616821, 14549246, 3341099, 32155958, 13648976, -17577068, 8849297, 65030, 8370684}, - FieldElement{-8320926, -12049626, 31204563, 5839400, -20627288, -1057277, -19442942, 6922164, 12743482, -9800518}, - FieldElement{-2361371, 12678785, 28815050, 4759974, -23893047, 4884717, 23783145, 11038569, 18800704, 255233}, - }, - { - FieldElement{-5269658, -1773886, 13957886, 7990715, 23132995, 728773, 13393847, 9066957, 19258688, -14753793}, - FieldElement{-2936654, -10827535, -10432089, 14516793, -3640786, 4372541, -31934921, 2209390, -1524053, 2055794}, - FieldElement{580882, 16705327, 5468415, -2683018, -30926419, -14696000, -7203346, -8994389, -30021019, 7394435}, - }, - { - FieldElement{23838809, 1822728, -15738443, 15242727, 8318092, -3733104, -21672180, -3492205, -4821741, 14799921}, - FieldElement{13345610, 9759151, 3371034, -16137791, 16353039, 8577942, 31129804, 13496856, -9056018, 7402518}, - FieldElement{2286874, -4435931, -20042458, -2008336, -13696227, 5038122, 11006906, -15760352, 8205061, 1607563}, - }, - { - FieldElement{14414086, -8002132, 3331830, -3208217, 22249151, -5594188, 18364661, -2906958, 30019587, -9029278}, - FieldElement{-27688051, 1585953, -10775053, 931069, -29120221, -11002319, -14410829, 12029093, 9944378, 8024}, - FieldElement{4368715, -3709630, 29874200, -15022983, -20230386, -11410704, -16114594, -999085, -8142388, 5640030}, - }, - { - FieldElement{10299610, 13746483, 11661824, 16234854, 7630238, 5998374, 9809887, -16694564, 15219798, -14327783}, - FieldElement{27425505, -5719081, 3055006, 10660664, 23458024, 595578, -15398605, -1173195, -18342183, 9742717}, - FieldElement{6744077, 2427284, 26042789, 2720740, -847906, 1118974, 32324614, 7406442, 12420155, 1994844}, - }, - { - FieldElement{14012521, -5024720, -18384453, -9578469, -26485342, -3936439, -13033478, -10909803, 24319929, -6446333}, - FieldElement{16412690, -4507367, 10772641, 15929391, -17068788, -4658621, 10555945, -10484049, -30102368, -4739048}, - FieldElement{22397382, -7767684, -9293161, -12792868, 17166287, -9755136, -27333065, 6199366, 21880021, -12250760}, - }, - { - FieldElement{-4283307, 5368523, -31117018, 8163389, -30323063, 3209128, 16557151, 8890729, 8840445, 4957760}, - FieldElement{-15447727, 709327, -6919446, -10870178, -29777922, 6522332, -21720181, 12130072, -14796503, 5005757}, - FieldElement{-2114751, -14308128, 23019042, 15765735, -25269683, 6002752, 10183197, -13239326, -16395286, -2176112}, - }, - }, - { - { - FieldElement{-19025756, 1632005, 13466291, -7995100, -23640451, 16573537, -32013908, -3057104, 22208662, 2000468}, - FieldElement{3065073, -1412761, -25598674, -361432, -17683065, -5703415, -8164212, 11248527, -3691214, -7414184}, - FieldElement{10379208, -6045554, 8877319, 1473647, -29291284, -12507580, 16690915, 2553332, -3132688, 16400289}, - }, - { - FieldElement{15716668, 1254266, -18472690, 7446274, -8448918, 6344164, -22097271, -7285580, 26894937, 9132066}, - FieldElement{24158887, 12938817, 11085297, -8177598, -28063478, -4457083, -30576463, 64452, -6817084, -2692882}, - FieldElement{13488534, 7794716, 22236231, 5989356, 25426474, -12578208, 2350710, -3418511, -4688006, 2364226}, - }, - { - FieldElement{16335052, 9132434, 25640582, 6678888, 1725628, 8517937, -11807024, -11697457, 15445875, -7798101}, - FieldElement{29004207, -7867081, 28661402, -640412, -12794003, -7943086, 31863255, -4135540, -278050, -15759279}, - FieldElement{-6122061, -14866665, -28614905, 14569919, -10857999, -3591829, 10343412, -6976290, -29828287, -10815811}, - }, - { - FieldElement{27081650, 3463984, 14099042, -4517604, 1616303, -6205604, 29542636, 15372179, 17293797, 960709}, - FieldElement{20263915, 11434237, -5765435, 11236810, 13505955, -10857102, -16111345, 6493122, -19384511, 7639714}, - FieldElement{-2830798, -14839232, 25403038, -8215196, -8317012, -16173699, 18006287, -16043750, 29994677, -15808121}, - }, - { - FieldElement{9769828, 5202651, -24157398, -13631392, -28051003, -11561624, -24613141, -13860782, -31184575, 709464}, - FieldElement{12286395, 13076066, -21775189, -1176622, -25003198, 4057652, -32018128, -8890874, 16102007, 13205847}, - FieldElement{13733362, 5599946, 10557076, 3195751, -5557991, 8536970, -25540170, 8525972, 10151379, 10394400}, - }, - { - FieldElement{4024660, -16137551, 22436262, 12276534, -9099015, -2686099, 19698229, 11743039, -33302334, 8934414}, - FieldElement{-15879800, -4525240, -8580747, -2934061, 14634845, -698278, -9449077, 3137094, -11536886, 11721158}, - FieldElement{17555939, -5013938, 8268606, 2331751, -22738815, 9761013, 9319229, 8835153, -9205489, -1280045}, - }, - { - FieldElement{-461409, -7830014, 20614118, 16688288, -7514766, -4807119, 22300304, 505429, 6108462, -6183415}, - FieldElement{-5070281, 12367917, -30663534, 3234473, 32617080, -8422642, 29880583, -13483331, -26898490, -7867459}, - FieldElement{-31975283, 5726539, 26934134, 10237677, -3173717, -605053, 24199304, 3795095, 7592688, -14992079}, - }, - { - FieldElement{21594432, -14964228, 17466408, -4077222, 32537084, 2739898, 6407723, 12018833, -28256052, 4298412}, - FieldElement{-20650503, -11961496, -27236275, 570498, 3767144, -1717540, 13891942, -1569194, 13717174, 10805743}, - FieldElement{-14676630, -15644296, 15287174, 11927123, 24177847, -8175568, -796431, 14860609, -26938930, -5863836}, - }, - }, - { - { - FieldElement{12962541, 5311799, -10060768, 11658280, 18855286, -7954201, 13286263, -12808704, -4381056, 9882022}, - FieldElement{18512079, 11319350, -20123124, 15090309, 18818594, 5271736, -22727904, 3666879, -23967430, -3299429}, - FieldElement{-6789020, -3146043, 16192429, 13241070, 15898607, -14206114, -10084880, -6661110, -2403099, 5276065}, - }, - { - FieldElement{30169808, -5317648, 26306206, -11750859, 27814964, 7069267, 7152851, 3684982, 1449224, 13082861}, - FieldElement{10342826, 3098505, 2119311, 193222, 25702612, 12233820, 23697382, 15056736, -21016438, -8202000}, - FieldElement{-33150110, 3261608, 22745853, 7948688, 19370557, -15177665, -26171976, 6482814, -10300080, -11060101}, - }, - { - FieldElement{32869458, -5408545, 25609743, 15678670, -10687769, -15471071, 26112421, 2521008, -22664288, 6904815}, - FieldElement{29506923, 4457497, 3377935, -9796444, -30510046, 12935080, 1561737, 3841096, -29003639, -6657642}, - FieldElement{10340844, -6630377, -18656632, -2278430, 12621151, -13339055, 30878497, -11824370, -25584551, 5181966}, - }, - { - FieldElement{25940115, -12658025, 17324188, -10307374, -8671468, 15029094, 24396252, -16450922, -2322852, -12388574}, - FieldElement{-21765684, 9916823, -1300409, 4079498, -1028346, 11909559, 1782390, 12641087, 20603771, -6561742}, - FieldElement{-18882287, -11673380, 24849422, 11501709, 13161720, -4768874, 1925523, 11914390, 4662781, 7820689}, - }, - { - FieldElement{12241050, -425982, 8132691, 9393934, 32846760, -1599620, 29749456, 12172924, 16136752, 15264020}, - FieldElement{-10349955, -14680563, -8211979, 2330220, -17662549, -14545780, 10658213, 6671822, 19012087, 3772772}, - FieldElement{3753511, -3421066, 10617074, 2028709, 14841030, -6721664, 28718732, -15762884, 20527771, 12988982}, - }, - { - FieldElement{-14822485, -5797269, -3707987, 12689773, -898983, -10914866, -24183046, -10564943, 3299665, -12424953}, - FieldElement{-16777703, -15253301, -9642417, 4978983, 3308785, 8755439, 6943197, 6461331, -25583147, 8991218}, - FieldElement{-17226263, 1816362, -1673288, -6086439, 31783888, -8175991, -32948145, 7417950, -30242287, 1507265}, - }, - { - FieldElement{29692663, 6829891, -10498800, 4334896, 20945975, -11906496, -28887608, 8209391, 14606362, -10647073}, - FieldElement{-3481570, 8707081, 32188102, 5672294, 22096700, 1711240, -33020695, 9761487, 4170404, -2085325}, - FieldElement{-11587470, 14855945, -4127778, -1531857, -26649089, 15084046, 22186522, 16002000, -14276837, -8400798}, - }, - { - FieldElement{-4811456, 13761029, -31703877, -2483919, -3312471, 7869047, -7113572, -9620092, 13240845, 10965870}, - FieldElement{-7742563, -8256762, -14768334, -13656260, -23232383, 12387166, 4498947, 14147411, 29514390, 4302863}, - FieldElement{-13413405, -12407859, 20757302, -13801832, 14785143, 8976368, -5061276, -2144373, 17846988, -13971927}, - }, - }, - { - { - FieldElement{-2244452, -754728, -4597030, -1066309, -6247172, 1455299, -21647728, -9214789, -5222701, 12650267}, - FieldElement{-9906797, -16070310, 21134160, 12198166, -27064575, 708126, 387813, 13770293, -19134326, 10958663}, - FieldElement{22470984, 12369526, 23446014, -5441109, -21520802, -9698723, -11772496, -11574455, -25083830, 4271862}, - }, - { - FieldElement{-25169565, -10053642, -19909332, 15361595, -5984358, 2159192, 75375, -4278529, -32526221, 8469673}, - FieldElement{15854970, 4148314, -8893890, 7259002, 11666551, 13824734, -30531198, 2697372, 24154791, -9460943}, - FieldElement{15446137, -15806644, 29759747, 14019369, 30811221, -9610191, -31582008, 12840104, 24913809, 9815020}, - }, - { - FieldElement{-4709286, -5614269, -31841498, -12288893, -14443537, 10799414, -9103676, 13438769, 18735128, 9466238}, - FieldElement{11933045, 9281483, 5081055, -5183824, -2628162, -4905629, -7727821, -10896103, -22728655, 16199064}, - FieldElement{14576810, 379472, -26786533, -8317236, -29426508, -10812974, -102766, 1876699, 30801119, 2164795}, - }, - { - FieldElement{15995086, 3199873, 13672555, 13712240, -19378835, -4647646, -13081610, -15496269, -13492807, 1268052}, - FieldElement{-10290614, -3659039, -3286592, 10948818, 23037027, 3794475, -3470338, -12600221, -17055369, 3565904}, - FieldElement{29210088, -9419337, -5919792, -4952785, 10834811, -13327726, -16512102, -10820713, -27162222, -14030531}, - }, - { - FieldElement{-13161890, 15508588, 16663704, -8156150, -28349942, 9019123, -29183421, -3769423, 2244111, -14001979}, - FieldElement{-5152875, -3800936, -9306475, -6071583, 16243069, 14684434, -25673088, -16180800, 13491506, 4641841}, - FieldElement{10813417, 643330, -19188515, -728916, 30292062, -16600078, 27548447, -7721242, 14476989, -12767431}, - }, - { - FieldElement{10292079, 9984945, 6481436, 8279905, -7251514, 7032743, 27282937, -1644259, -27912810, 12651324}, - FieldElement{-31185513, -813383, 22271204, 11835308, 10201545, 15351028, 17099662, 3988035, 21721536, -3148940}, - FieldElement{10202177, -6545839, -31373232, -9574638, -32150642, -8119683, -12906320, 3852694, 13216206, 14842320}, - }, - { - FieldElement{-15815640, -10601066, -6538952, -7258995, -6984659, -6581778, -31500847, 13765824, -27434397, 9900184}, - FieldElement{14465505, -13833331, -32133984, -14738873, -27443187, 12990492, 33046193, 15796406, -7051866, -8040114}, - FieldElement{30924417, -8279620, 6359016, -12816335, 16508377, 9071735, -25488601, 15413635, 9524356, -7018878}, - }, - { - FieldElement{12274201, -13175547, 32627641, -1785326, 6736625, 13267305, 5237659, -5109483, 15663516, 4035784}, - FieldElement{-2951309, 8903985, 17349946, 601635, -16432815, -4612556, -13732739, -15889334, -22258478, 4659091}, - FieldElement{-16916263, -4952973, -30393711, -15158821, 20774812, 15897498, 5736189, 15026997, -2178256, -13455585}, - }, - }, - { - { - FieldElement{-8858980, -2219056, 28571666, -10155518, -474467, -10105698, -3801496, 278095, 23440562, -290208}, - FieldElement{10226241, -5928702, 15139956, 120818, -14867693, 5218603, 32937275, 11551483, -16571960, -7442864}, - FieldElement{17932739, -12437276, -24039557, 10749060, 11316803, 7535897, 22503767, 5561594, -3646624, 3898661}, - }, - { - FieldElement{7749907, -969567, -16339731, -16464, -25018111, 15122143, -1573531, 7152530, 21831162, 1245233}, - FieldElement{26958459, -14658026, 4314586, 8346991, -5677764, 11960072, -32589295, -620035, -30402091, -16716212}, - FieldElement{-12165896, 9166947, 33491384, 13673479, 29787085, 13096535, 6280834, 14587357, -22338025, 13987525}, - }, - { - FieldElement{-24349909, 7778775, 21116000, 15572597, -4833266, -5357778, -4300898, -5124639, -7469781, -2858068}, - FieldElement{9681908, -6737123, -31951644, 13591838, -6883821, 386950, 31622781, 6439245, -14581012, 4091397}, - FieldElement{-8426427, 1470727, -28109679, -1596990, 3978627, -5123623, -19622683, 12092163, 29077877, -14741988}, - }, - { - FieldElement{5269168, -6859726, -13230211, -8020715, 25932563, 1763552, -5606110, -5505881, -20017847, 2357889}, - FieldElement{32264008, -15407652, -5387735, -1160093, -2091322, -3946900, 23104804, -12869908, 5727338, 189038}, - FieldElement{14609123, -8954470, -6000566, -16622781, -14577387, -7743898, -26745169, 10942115, -25888931, -14884697}, - }, - { - FieldElement{20513500, 5557931, -15604613, 7829531, 26413943, -2019404, -21378968, 7471781, 13913677, -5137875}, - FieldElement{-25574376, 11967826, 29233242, 12948236, -6754465, 4713227, -8940970, 14059180, 12878652, 8511905}, - FieldElement{-25656801, 3393631, -2955415, -7075526, -2250709, 9366908, -30223418, 6812974, 5568676, -3127656}, - }, - { - FieldElement{11630004, 12144454, 2116339, 13606037, 27378885, 15676917, -17408753, -13504373, -14395196, 8070818}, - FieldElement{27117696, -10007378, -31282771, -5570088, 1127282, 12772488, -29845906, 10483306, -11552749, -1028714}, - FieldElement{10637467, -5688064, 5674781, 1072708, -26343588, -6982302, -1683975, 9177853, -27493162, 15431203}, - }, - { - FieldElement{20525145, 10892566, -12742472, 12779443, -29493034, 16150075, -28240519, 14943142, -15056790, -7935931}, - FieldElement{-30024462, 5626926, -551567, -9981087, 753598, 11981191, 25244767, -3239766, -3356550, 9594024}, - FieldElement{-23752644, 2636870, -5163910, -10103818, 585134, 7877383, 11345683, -6492290, 13352335, -10977084}, - }, - { - FieldElement{-1931799, -5407458, 3304649, -12884869, 17015806, -4877091, -29783850, -7752482, -13215537, -319204}, - FieldElement{20239939, 6607058, 6203985, 3483793, -18386976, -779229, -20723742, 15077870, -22750759, 14523817}, - FieldElement{27406042, -6041657, 27423596, -4497394, 4996214, 10002360, -28842031, -4545494, -30172742, -4805667}, - }, - }, - { - { - FieldElement{11374242, 12660715, 17861383, -12540833, 10935568, 1099227, -13886076, -9091740, -27727044, 11358504}, - FieldElement{-12730809, 10311867, 1510375, 10778093, -2119455, -9145702, 32676003, 11149336, -26123651, 4985768}, - FieldElement{-19096303, 341147, -6197485, -239033, 15756973, -8796662, -983043, 13794114, -19414307, -15621255}, - }, - { - FieldElement{6490081, 11940286, 25495923, -7726360, 8668373, -8751316, 3367603, 6970005, -1691065, -9004790}, - FieldElement{1656497, 13457317, 15370807, 6364910, 13605745, 8362338, -19174622, -5475723, -16796596, -5031438}, - FieldElement{-22273315, -13524424, -64685, -4334223, -18605636, -10921968, -20571065, -7007978, -99853, -10237333}, - }, - { - FieldElement{17747465, 10039260, 19368299, -4050591, -20630635, -16041286, 31992683, -15857976, -29260363, -5511971}, - FieldElement{31932027, -4986141, -19612382, 16366580, 22023614, 88450, 11371999, -3744247, 4882242, -10626905}, - FieldElement{29796507, 37186, 19818052, 10115756, -11829032, 3352736, 18551198, 3272828, -5190932, -4162409}, - }, - { - FieldElement{12501286, 4044383, -8612957, -13392385, -32430052, 5136599, -19230378, -3529697, 330070, -3659409}, - FieldElement{6384877, 2899513, 17807477, 7663917, -2358888, 12363165, 25366522, -8573892, -271295, 12071499}, - FieldElement{-8365515, -4042521, 25133448, -4517355, -6211027, 2265927, -32769618, 1936675, -5159697, 3829363}, - }, - { - FieldElement{28425966, -5835433, -577090, -4697198, -14217555, 6870930, 7921550, -6567787, 26333140, 14267664}, - FieldElement{-11067219, 11871231, 27385719, -10559544, -4585914, -11189312, 10004786, -8709488, -21761224, 8930324}, - FieldElement{-21197785, -16396035, 25654216, -1725397, 12282012, 11008919, 1541940, 4757911, -26491501, -16408940}, - }, - { - FieldElement{13537262, -7759490, -20604840, 10961927, -5922820, -13218065, -13156584, 6217254, -15943699, 13814990}, - FieldElement{-17422573, 15157790, 18705543, 29619, 24409717, -260476, 27361681, 9257833, -1956526, -1776914}, - FieldElement{-25045300, -10191966, 15366585, 15166509, -13105086, 8423556, -29171540, 12361135, -18685978, 4578290}, - }, - { - FieldElement{24579768, 3711570, 1342322, -11180126, -27005135, 14124956, -22544529, 14074919, 21964432, 8235257}, - FieldElement{-6528613, -2411497, 9442966, -5925588, 12025640, -1487420, -2981514, -1669206, 13006806, 2355433}, - FieldElement{-16304899, -13605259, -6632427, -5142349, 16974359, -10911083, 27202044, 1719366, 1141648, -12796236}, - }, - { - FieldElement{-12863944, -13219986, -8318266, -11018091, -6810145, -4843894, 13475066, -3133972, 32674895, 13715045}, - FieldElement{11423335, -5468059, 32344216, 8962751, 24989809, 9241752, -13265253, 16086212, -28740881, -15642093}, - FieldElement{-1409668, 12530728, -6368726, 10847387, 19531186, -14132160, -11709148, 7791794, -27245943, 4383347}, - }, - }, - { - { - FieldElement{-28970898, 5271447, -1266009, -9736989, -12455236, 16732599, -4862407, -4906449, 27193557, 6245191}, - FieldElement{-15193956, 5362278, -1783893, 2695834, 4960227, 12840725, 23061898, 3260492, 22510453, 8577507}, - FieldElement{-12632451, 11257346, -32692994, 13548177, -721004, 10879011, 31168030, 13952092, -29571492, -3635906}, - }, - { - FieldElement{3877321, -9572739, 32416692, 5405324, -11004407, -13656635, 3759769, 11935320, 5611860, 8164018}, - FieldElement{-16275802, 14667797, 15906460, 12155291, -22111149, -9039718, 32003002, -8832289, 5773085, -8422109}, - FieldElement{-23788118, -8254300, 1950875, 8937633, 18686727, 16459170, -905725, 12376320, 31632953, 190926}, - }, - { - FieldElement{-24593607, -16138885, -8423991, 13378746, 14162407, 6901328, -8288749, 4508564, -25341555, -3627528}, - FieldElement{8884438, -5884009, 6023974, 10104341, -6881569, -4941533, 18722941, -14786005, -1672488, 827625}, - FieldElement{-32720583, -16289296, -32503547, 7101210, 13354605, 2659080, -1800575, -14108036, -24878478, 1541286}, - }, - { - FieldElement{2901347, -1117687, 3880376, -10059388, -17620940, -3612781, -21802117, -3567481, 20456845, -1885033}, - FieldElement{27019610, 12299467, -13658288, -1603234, -12861660, -4861471, -19540150, -5016058, 29439641, 15138866}, - FieldElement{21536104, -6626420, -32447818, -10690208, -22408077, 5175814, -5420040, -16361163, 7779328, 109896}, - }, - { - FieldElement{30279744, 14648750, -8044871, 6425558, 13639621, -743509, 28698390, 12180118, 23177719, -554075}, - FieldElement{26572847, 3405927, -31701700, 12890905, -19265668, 5335866, -6493768, 2378492, 4439158, -13279347}, - FieldElement{-22716706, 3489070, -9225266, -332753, 18875722, -1140095, 14819434, -12731527, -17717757, -5461437}, - }, - { - FieldElement{-5056483, 16566551, 15953661, 3767752, -10436499, 15627060, -820954, 2177225, 8550082, -15114165}, - FieldElement{-18473302, 16596775, -381660, 15663611, 22860960, 15585581, -27844109, -3582739, -23260460, -8428588}, - FieldElement{-32480551, 15707275, -8205912, -5652081, 29464558, 2713815, -22725137, 15860482, -21902570, 1494193}, - }, - { - FieldElement{-19562091, -14087393, -25583872, -9299552, 13127842, 759709, 21923482, 16529112, 8742704, 12967017}, - FieldElement{-28464899, 1553205, 32536856, -10473729, -24691605, -406174, -8914625, -2933896, -29903758, 15553883}, - FieldElement{21877909, 3230008, 9881174, 10539357, -4797115, 2841332, 11543572, 14513274, 19375923, -12647961}, - }, - { - FieldElement{8832269, -14495485, 13253511, 5137575, 5037871, 4078777, 24880818, -6222716, 2862653, 9455043}, - FieldElement{29306751, 5123106, 20245049, -14149889, 9592566, 8447059, -2077124, -2990080, 15511449, 4789663}, - FieldElement{-20679756, 7004547, 8824831, -9434977, -4045704, -3750736, -5754762, 108893, 23513200, 16652362}, - }, - }, - { - { - FieldElement{-33256173, 4144782, -4476029, -6579123, 10770039, -7155542, -6650416, -12936300, -18319198, 10212860}, - FieldElement{2756081, 8598110, 7383731, -6859892, 22312759, -1105012, 21179801, 2600940, -9988298, -12506466}, - FieldElement{-24645692, 13317462, -30449259, -15653928, 21365574, -10869657, 11344424, 864440, -2499677, -16710063}, - }, - { - FieldElement{-26432803, 6148329, -17184412, -14474154, 18782929, -275997, -22561534, 211300, 2719757, 4940997}, - FieldElement{-1323882, 3911313, -6948744, 14759765, -30027150, 7851207, 21690126, 8518463, 26699843, 5276295}, - FieldElement{-13149873, -6429067, 9396249, 365013, 24703301, -10488939, 1321586, 149635, -15452774, 7159369}, - }, - { - FieldElement{9987780, -3404759, 17507962, 9505530, 9731535, -2165514, 22356009, 8312176, 22477218, -8403385}, - FieldElement{18155857, -16504990, 19744716, 9006923, 15154154, -10538976, 24256460, -4864995, -22548173, 9334109}, - FieldElement{2986088, -4911893, 10776628, -3473844, 10620590, -7083203, -21413845, 14253545, -22587149, 536906}, - }, - { - FieldElement{4377756, 8115836, 24567078, 15495314, 11625074, 13064599, 7390551, 10589625, 10838060, -15420424}, - FieldElement{-19342404, 867880, 9277171, -3218459, -14431572, -1986443, 19295826, -15796950, 6378260, 699185}, - FieldElement{7895026, 4057113, -7081772, -13077756, -17886831, -323126, -716039, 15693155, -5045064, -13373962}, - }, - { - FieldElement{-7737563, -5869402, -14566319, -7406919, 11385654, 13201616, 31730678, -10962840, -3918636, -9669325}, - FieldElement{10188286, -15770834, -7336361, 13427543, 22223443, 14896287, 30743455, 7116568, -21786507, 5427593}, - FieldElement{696102, 13206899, 27047647, -10632082, 15285305, -9853179, 10798490, -4578720, 19236243, 12477404}, - }, - { - FieldElement{-11229439, 11243796, -17054270, -8040865, -788228, -8167967, -3897669, 11180504, -23169516, 7733644}, - FieldElement{17800790, -14036179, -27000429, -11766671, 23887827, 3149671, 23466177, -10538171, 10322027, 15313801}, - FieldElement{26246234, 11968874, 32263343, -5468728, 6830755, -13323031, -15794704, -101982, -24449242, 10890804}, - }, - { - FieldElement{-31365647, 10271363, -12660625, -6267268, 16690207, -13062544, -14982212, 16484931, 25180797, -5334884}, - FieldElement{-586574, 10376444, -32586414, -11286356, 19801893, 10997610, 2276632, 9482883, 316878, 13820577}, - FieldElement{-9882808, -4510367, -2115506, 16457136, -11100081, 11674996, 30756178, -7515054, 30696930, -3712849}, - }, - { - FieldElement{32988917, -9603412, 12499366, 7910787, -10617257, -11931514, -7342816, -9985397, -32349517, 7392473}, - FieldElement{-8855661, 15927861, 9866406, -3649411, -2396914, -16655781, -30409476, -9134995, 25112947, -2926644}, - FieldElement{-2504044, -436966, 25621774, -5678772, 15085042, -5479877, -24884878, -13526194, 5537438, -13914319}, - }, - }, - { - { - FieldElement{-11225584, 2320285, -9584280, 10149187, -33444663, 5808648, -14876251, -1729667, 31234590, 6090599}, - FieldElement{-9633316, 116426, 26083934, 2897444, -6364437, -2688086, 609721, 15878753, -6970405, -9034768}, - FieldElement{-27757857, 247744, -15194774, -9002551, 23288161, -10011936, -23869595, 6503646, 20650474, 1804084}, - }, - { - FieldElement{-27589786, 15456424, 8972517, 8469608, 15640622, 4439847, 3121995, -10329713, 27842616, -202328}, - FieldElement{-15306973, 2839644, 22530074, 10026331, 4602058, 5048462, 28248656, 5031932, -11375082, 12714369}, - FieldElement{20807691, -7270825, 29286141, 11421711, -27876523, -13868230, -21227475, 1035546, -19733229, 12796920}, - }, - { - FieldElement{12076899, -14301286, -8785001, -11848922, -25012791, 16400684, -17591495, -12899438, 3480665, -15182815}, - FieldElement{-32361549, 5457597, 28548107, 7833186, 7303070, -11953545, -24363064, -15921875, -33374054, 2771025}, - FieldElement{-21389266, 421932, 26597266, 6860826, 22486084, -6737172, -17137485, -4210226, -24552282, 15673397}, - }, - { - FieldElement{-20184622, 2338216, 19788685, -9620956, -4001265, -8740893, -20271184, 4733254, 3727144, -12934448}, - FieldElement{6120119, 814863, -11794402, -622716, 6812205, -15747771, 2019594, 7975683, 31123697, -10958981}, - FieldElement{30069250, -11435332, 30434654, 2958439, 18399564, -976289, 12296869, 9204260, -16432438, 9648165}, - }, - { - FieldElement{32705432, -1550977, 30705658, 7451065, -11805606, 9631813, 3305266, 5248604, -26008332, -11377501}, - FieldElement{17219865, 2375039, -31570947, -5575615, -19459679, 9219903, 294711, 15298639, 2662509, -16297073}, - FieldElement{-1172927, -7558695, -4366770, -4287744, -21346413, -8434326, 32087529, -1222777, 32247248, -14389861}, - }, - { - FieldElement{14312628, 1221556, 17395390, -8700143, -4945741, -8684635, -28197744, -9637817, -16027623, -13378845}, - FieldElement{-1428825, -9678990, -9235681, 6549687, -7383069, -468664, 23046502, 9803137, 17597934, 2346211}, - FieldElement{18510800, 15337574, 26171504, 981392, -22241552, 7827556, -23491134, -11323352, 3059833, -11782870}, - }, - { - FieldElement{10141598, 6082907, 17829293, -1947643, 9830092, 13613136, -25556636, -5544586, -33502212, 3592096}, - FieldElement{33114168, -15889352, -26525686, -13343397, 33076705, 8716171, 1151462, 1521897, -982665, -6837803}, - FieldElement{-32939165, -4255815, 23947181, -324178, -33072974, -12305637, -16637686, 3891704, 26353178, 693168}, - }, - { - FieldElement{30374239, 1595580, -16884039, 13186931, 4600344, 406904, 9585294, -400668, 31375464, 14369965}, - FieldElement{-14370654, -7772529, 1510301, 6434173, -18784789, -6262728, 32732230, -13108839, 17901441, 16011505}, - FieldElement{18171223, -11934626, -12500402, 15197122, -11038147, -15230035, -19172240, -16046376, 8764035, 12309598}, - }, - }, - { - { - FieldElement{5975908, -5243188, -19459362, -9681747, -11541277, 14015782, -23665757, 1228319, 17544096, -10593782}, - FieldElement{5811932, -1715293, 3442887, -2269310, -18367348, -8359541, -18044043, -15410127, -5565381, 12348900}, - FieldElement{-31399660, 11407555, 25755363, 6891399, -3256938, 14872274, -24849353, 8141295, -10632534, -585479}, - }, - { - FieldElement{-12675304, 694026, -5076145, 13300344, 14015258, -14451394, -9698672, -11329050, 30944593, 1130208}, - FieldElement{8247766, -6710942, -26562381, -7709309, -14401939, -14648910, 4652152, 2488540, 23550156, -271232}, - FieldElement{17294316, -3788438, 7026748, 15626851, 22990044, 113481, 2267737, -5908146, -408818, -137719}, - }, - { - FieldElement{16091085, -16253926, 18599252, 7340678, 2137637, -1221657, -3364161, 14550936, 3260525, -7166271}, - FieldElement{-4910104, -13332887, 18550887, 10864893, -16459325, -7291596, -23028869, -13204905, -12748722, 2701326}, - FieldElement{-8574695, 16099415, 4629974, -16340524, -20786213, -6005432, -10018363, 9276971, 11329923, 1862132}, - }, - { - FieldElement{14763076, -15903608, -30918270, 3689867, 3511892, 10313526, -21951088, 12219231, -9037963, -940300}, - FieldElement{8894987, -3446094, 6150753, 3013931, 301220, 15693451, -31981216, -2909717, -15438168, 11595570}, - FieldElement{15214962, 3537601, -26238722, -14058872, 4418657, -15230761, 13947276, 10730794, -13489462, -4363670}, - }, - { - FieldElement{-2538306, 7682793, 32759013, 263109, -29984731, -7955452, -22332124, -10188635, 977108, 699994}, - FieldElement{-12466472, 4195084, -9211532, 550904, -15565337, 12917920, 19118110, -439841, -30534533, -14337913}, - FieldElement{31788461, -14507657, 4799989, 7372237, 8808585, -14747943, 9408237, -10051775, 12493932, -5409317}, - }, - { - FieldElement{-25680606, 5260744, -19235809, -6284470, -3695942, 16566087, 27218280, 2607121, 29375955, 6024730}, - FieldElement{842132, -2794693, -4763381, -8722815, 26332018, -12405641, 11831880, 6985184, -9940361, 2854096}, - FieldElement{-4847262, -7969331, 2516242, -5847713, 9695691, -7221186, 16512645, 960770, 12121869, 16648078}, - }, - { - FieldElement{-15218652, 14667096, -13336229, 2013717, 30598287, -464137, -31504922, -7882064, 20237806, 2838411}, - FieldElement{-19288047, 4453152, 15298546, -16178388, 22115043, -15972604, 12544294, -13470457, 1068881, -12499905}, - FieldElement{-9558883, -16518835, 33238498, 13506958, 30505848, -1114596, -8486907, -2630053, 12521378, 4845654}, - }, - { - FieldElement{-28198521, 10744108, -2958380, 10199664, 7759311, -13088600, 3409348, -873400, -6482306, -12885870}, - FieldElement{-23561822, 6230156, -20382013, 10655314, -24040585, -11621172, 10477734, -1240216, -3113227, 13974498}, - FieldElement{12966261, 15550616, -32038948, -1615346, 21025980, -629444, 5642325, 7188737, 18895762, 12629579}, - }, - }, - { - { - FieldElement{14741879, -14946887, 22177208, -11721237, 1279741, 8058600, 11758140, 789443, 32195181, 3895677}, - FieldElement{10758205, 15755439, -4509950, 9243698, -4879422, 6879879, -2204575, -3566119, -8982069, 4429647}, - FieldElement{-2453894, 15725973, -20436342, -10410672, -5803908, -11040220, -7135870, -11642895, 18047436, -15281743}, - }, - { - FieldElement{-25173001, -11307165, 29759956, 11776784, -22262383, -15820455, 10993114, -12850837, -17620701, -9408468}, - FieldElement{21987233, 700364, -24505048, 14972008, -7774265, -5718395, 32155026, 2581431, -29958985, 8773375}, - FieldElement{-25568350, 454463, -13211935, 16126715, 25240068, 8594567, 20656846, 12017935, -7874389, -13920155}, - }, - { - FieldElement{6028182, 6263078, -31011806, -11301710, -818919, 2461772, -31841174, -5468042, -1721788, -2776725}, - FieldElement{-12278994, 16624277, 987579, -5922598, 32908203, 1248608, 7719845, -4166698, 28408820, 6816612}, - FieldElement{-10358094, -8237829, 19549651, -12169222, 22082623, 16147817, 20613181, 13982702, -10339570, 5067943}, - }, - { - FieldElement{-30505967, -3821767, 12074681, 13582412, -19877972, 2443951, -19719286, 12746132, 5331210, -10105944}, - FieldElement{30528811, 3601899, -1957090, 4619785, -27361822, -15436388, 24180793, -12570394, 27679908, -1648928}, - FieldElement{9402404, -13957065, 32834043, 10838634, -26580150, -13237195, 26653274, -8685565, 22611444, -12715406}, - }, - { - FieldElement{22190590, 1118029, 22736441, 15130463, -30460692, -5991321, 19189625, -4648942, 4854859, 6622139}, - FieldElement{-8310738, -2953450, -8262579, -3388049, -10401731, -271929, 13424426, -3567227, 26404409, 13001963}, - FieldElement{-31241838, -15415700, -2994250, 8939346, 11562230, -12840670, -26064365, -11621720, -15405155, 11020693}, - }, - { - FieldElement{1866042, -7949489, -7898649, -10301010, 12483315, 13477547, 3175636, -12424163, 28761762, 1406734}, - FieldElement{-448555, -1777666, 13018551, 3194501, -9580420, -11161737, 24760585, -4347088, 25577411, -13378680}, - FieldElement{-24290378, 4759345, -690653, -1852816, 2066747, 10693769, -29595790, 9884936, -9368926, 4745410}, - }, - { - FieldElement{-9141284, 6049714, -19531061, -4341411, -31260798, 9944276, -15462008, -11311852, 10931924, -11931931}, - FieldElement{-16561513, 14112680, -8012645, 4817318, -8040464, -11414606, -22853429, 10856641, -20470770, 13434654}, - FieldElement{22759489, -10073434, -16766264, -1871422, 13637442, -10168091, 1765144, -12654326, 28445307, -5364710}, - }, - { - FieldElement{29875063, 12493613, 2795536, -3786330, 1710620, 15181182, -10195717, -8788675, 9074234, 1167180}, - FieldElement{-26205683, 11014233, -9842651, -2635485, -26908120, 7532294, -18716888, -9535498, 3843903, 9367684}, - FieldElement{-10969595, -6403711, 9591134, 9582310, 11349256, 108879, 16235123, 8601684, -139197, 4242895}, - }, - }, - { - { - FieldElement{22092954, -13191123, -2042793, -11968512, 32186753, -11517388, -6574341, 2470660, -27417366, 16625501}, - FieldElement{-11057722, 3042016, 13770083, -9257922, 584236, -544855, -7770857, 2602725, -27351616, 14247413}, - FieldElement{6314175, -10264892, -32772502, 15957557, -10157730, 168750, -8618807, 14290061, 27108877, -1180880}, - }, - { - FieldElement{-8586597, -7170966, 13241782, 10960156, -32991015, -13794596, 33547976, -11058889, -27148451, 981874}, - FieldElement{22833440, 9293594, -32649448, -13618667, -9136966, 14756819, -22928859, -13970780, -10479804, -16197962}, - FieldElement{-7768587, 3326786, -28111797, 10783824, 19178761, 14905060, 22680049, 13906969, -15933690, 3797899}, - }, - { - FieldElement{21721356, -4212746, -12206123, 9310182, -3882239, -13653110, 23740224, -2709232, 20491983, -8042152}, - FieldElement{9209270, -15135055, -13256557, -6167798, -731016, 15289673, 25947805, 15286587, 30997318, -6703063}, - FieldElement{7392032, 16618386, 23946583, -8039892, -13265164, -1533858, -14197445, -2321576, 17649998, -250080}, - }, - { - FieldElement{-9301088, -14193827, 30609526, -3049543, -25175069, -1283752, -15241566, -9525724, -2233253, 7662146}, - FieldElement{-17558673, 1763594, -33114336, 15908610, -30040870, -12174295, 7335080, -8472199, -3174674, 3440183}, - FieldElement{-19889700, -5977008, -24111293, -9688870, 10799743, -16571957, 40450, -4431835, 4862400, 1133}, - }, - { - FieldElement{-32856209, -7873957, -5422389, 14860950, -16319031, 7956142, 7258061, 311861, -30594991, -7379421}, - FieldElement{-3773428, -1565936, 28985340, 7499440, 24445838, 9325937, 29727763, 16527196, 18278453, 15405622}, - FieldElement{-4381906, 8508652, -19898366, -3674424, -5984453, 15149970, -13313598, 843523, -21875062, 13626197}, - }, - { - FieldElement{2281448, -13487055, -10915418, -2609910, 1879358, 16164207, -10783882, 3953792, 13340839, 15928663}, - FieldElement{31727126, -7179855, -18437503, -8283652, 2875793, -16390330, -25269894, -7014826, -23452306, 5964753}, - FieldElement{4100420, -5959452, -17179337, 6017714, -18705837, 12227141, -26684835, 11344144, 2538215, -7570755}, - }, - { - FieldElement{-9433605, 6123113, 11159803, -2156608, 30016280, 14966241, -20474983, 1485421, -629256, -15958862}, - FieldElement{-26804558, 4260919, 11851389, 9658551, -32017107, 16367492, -20205425, -13191288, 11659922, -11115118}, - FieldElement{26180396, 10015009, -30844224, -8581293, 5418197, 9480663, 2231568, -10170080, 33100372, -1306171}, - }, - { - FieldElement{15121113, -5201871, -10389905, 15427821, -27509937, -15992507, 21670947, 4486675, -5931810, -14466380}, - FieldElement{16166486, -9483733, -11104130, 6023908, -31926798, -1364923, 2340060, -16254968, -10735770, -10039824}, - FieldElement{28042865, -3557089, -12126526, 12259706, -3717498, -6945899, 6766453, -8689599, 18036436, 5803270}, - }, - }, - { - { - FieldElement{-817581, 6763912, 11803561, 1585585, 10958447, -2671165, 23855391, 4598332, -6159431, -14117438}, - FieldElement{-31031306, -14256194, 17332029, -2383520, 31312682, -5967183, 696309, 50292, -20095739, 11763584}, - FieldElement{-594563, -2514283, -32234153, 12643980, 12650761, 14811489, 665117, -12613632, -19773211, -10713562}, - }, - { - FieldElement{30464590, -11262872, -4127476, -12734478, 19835327, -7105613, -24396175, 2075773, -17020157, 992471}, - FieldElement{18357185, -6994433, 7766382, 16342475, -29324918, 411174, 14578841, 8080033, -11574335, -10601610}, - FieldElement{19598397, 10334610, 12555054, 2555664, 18821899, -10339780, 21873263, 16014234, 26224780, 16452269}, - }, - { - FieldElement{-30223925, 5145196, 5944548, 16385966, 3976735, 2009897, -11377804, -7618186, -20533829, 3698650}, - FieldElement{14187449, 3448569, -10636236, -10810935, -22663880, -3433596, 7268410, -10890444, 27394301, 12015369}, - FieldElement{19695761, 16087646, 28032085, 12999827, 6817792, 11427614, 20244189, -1312777, -13259127, -3402461}, - }, - { - FieldElement{30860103, 12735208, -1888245, -4699734, -16974906, 2256940, -8166013, 12298312, -8550524, -10393462}, - FieldElement{-5719826, -11245325, -1910649, 15569035, 26642876, -7587760, -5789354, -15118654, -4976164, 12651793}, - FieldElement{-2848395, 9953421, 11531313, -5282879, 26895123, -12697089, -13118820, -16517902, 9768698, -2533218}, - }, - { - FieldElement{-24719459, 1894651, -287698, -4704085, 15348719, -8156530, 32767513, 12765450, 4940095, 10678226}, - FieldElement{18860224, 15980149, -18987240, -1562570, -26233012, -11071856, -7843882, 13944024, -24372348, 16582019}, - FieldElement{-15504260, 4970268, -29893044, 4175593, -20993212, -2199756, -11704054, 15444560, -11003761, 7989037}, - }, - { - FieldElement{31490452, 5568061, -2412803, 2182383, -32336847, 4531686, -32078269, 6200206, -19686113, -14800171}, - FieldElement{-17308668, -15879940, -31522777, -2831, -32887382, 16375549, 8680158, -16371713, 28550068, -6857132}, - FieldElement{-28126887, -5688091, 16837845, -1820458, -6850681, 12700016, -30039981, 4364038, 1155602, 5988841}, - }, - { - FieldElement{21890435, -13272907, -12624011, 12154349, -7831873, 15300496, 23148983, -4470481, 24618407, 8283181}, - FieldElement{-33136107, -10512751, 9975416, 6841041, -31559793, 16356536, 3070187, -7025928, 1466169, 10740210}, - FieldElement{-1509399, -15488185, -13503385, -10655916, 32799044, 909394, -13938903, -5779719, -32164649, -15327040}, - }, - { - FieldElement{3960823, -14267803, -28026090, -15918051, -19404858, 13146868, 15567327, 951507, -3260321, -573935}, - FieldElement{24740841, 5052253, -30094131, 8961361, 25877428, 6165135, -24368180, 14397372, -7380369, -6144105}, - FieldElement{-28888365, 3510803, -28103278, -1158478, -11238128, -10631454, -15441463, -14453128, -1625486, -6494814}, - }, - }, - { - { - FieldElement{793299, -9230478, 8836302, -6235707, -27360908, -2369593, 33152843, -4885251, -9906200, -621852}, - FieldElement{5666233, 525582, 20782575, -8038419, -24538499, 14657740, 16099374, 1468826, -6171428, -15186581}, - FieldElement{-4859255, -3779343, -2917758, -6748019, 7778750, 11688288, -30404353, -9871238, -1558923, -9863646}, - }, - { - FieldElement{10896332, -7719704, 824275, 472601, -19460308, 3009587, 25248958, 14783338, -30581476, -15757844}, - FieldElement{10566929, 12612572, -31944212, 11118703, -12633376, 12362879, 21752402, 8822496, 24003793, 14264025}, - FieldElement{27713862, -7355973, -11008240, 9227530, 27050101, 2504721, 23886875, -13117525, 13958495, -5732453}, - }, - { - FieldElement{-23481610, 4867226, -27247128, 3900521, 29838369, -8212291, -31889399, -10041781, 7340521, -15410068}, - FieldElement{4646514, -8011124, -22766023, -11532654, 23184553, 8566613, 31366726, -1381061, -15066784, -10375192}, - FieldElement{-17270517, 12723032, -16993061, 14878794, 21619651, -6197576, 27584817, 3093888, -8843694, 3849921}, - }, - { - FieldElement{-9064912, 2103172, 25561640, -15125738, -5239824, 9582958, 32477045, -9017955, 5002294, -15550259}, - FieldElement{-12057553, -11177906, 21115585, -13365155, 8808712, -12030708, 16489530, 13378448, -25845716, 12741426}, - FieldElement{-5946367, 10645103, -30911586, 15390284, -3286982, -7118677, 24306472, 15852464, 28834118, -7646072}, - }, - { - FieldElement{-17335748, -9107057, -24531279, 9434953, -8472084, -583362, -13090771, 455841, 20461858, 5491305}, - FieldElement{13669248, -16095482, -12481974, -10203039, -14569770, -11893198, -24995986, 11293807, -28588204, -9421832}, - FieldElement{28497928, 6272777, -33022994, 14470570, 8906179, -1225630, 18504674, -14165166, 29867745, -8795943}, - }, - { - FieldElement{-16207023, 13517196, -27799630, -13697798, 24009064, -6373891, -6367600, -13175392, 22853429, -4012011}, - FieldElement{24191378, 16712145, -13931797, 15217831, 14542237, 1646131, 18603514, -11037887, 12876623, -2112447}, - FieldElement{17902668, 4518229, -411702, -2829247, 26878217, 5258055, -12860753, 608397, 16031844, 3723494}, - }, - { - FieldElement{-28632773, 12763728, -20446446, 7577504, 33001348, -13017745, 17558842, -7872890, 23896954, -4314245}, - FieldElement{-20005381, -12011952, 31520464, 605201, 2543521, 5991821, -2945064, 7229064, -9919646, -8826859}, - FieldElement{28816045, 298879, -28165016, -15920938, 19000928, -1665890, -12680833, -2949325, -18051778, -2082915}, - }, - { - FieldElement{16000882, -344896, 3493092, -11447198, -29504595, -13159789, 12577740, 16041268, -19715240, 7847707}, - FieldElement{10151868, 10572098, 27312476, 7922682, 14825339, 4723128, -32855931, -6519018, -10020567, 3852848}, - FieldElement{-11430470, 15697596, -21121557, -4420647, 5386314, 15063598, 16514493, -15932110, 29330899, -15076224}, - }, - }, - { - { - FieldElement{-25499735, -4378794, -15222908, -6901211, 16615731, 2051784, 3303702, 15490, -27548796, 12314391}, - FieldElement{15683520, -6003043, 18109120, -9980648, 15337968, -5997823, -16717435, 15921866, 16103996, -3731215}, - FieldElement{-23169824, -10781249, 13588192, -1628807, -3798557, -1074929, -19273607, 5402699, -29815713, -9841101}, - }, - { - FieldElement{23190676, 2384583, -32714340, 3462154, -29903655, -1529132, -11266856, 8911517, -25205859, 2739713}, - FieldElement{21374101, -3554250, -33524649, 9874411, 15377179, 11831242, -33529904, 6134907, 4931255, 11987849}, - FieldElement{-7732, -2978858, -16223486, 7277597, 105524, -322051, -31480539, 13861388, -30076310, 10117930}, - }, - { - FieldElement{-29501170, -10744872, -26163768, 13051539, -25625564, 5089643, -6325503, 6704079, 12890019, 15728940}, - FieldElement{-21972360, -11771379, -951059, -4418840, 14704840, 2695116, 903376, -10428139, 12885167, 8311031}, - FieldElement{-17516482, 5352194, 10384213, -13811658, 7506451, 13453191, 26423267, 4384730, 1888765, -5435404}, - }, - { - FieldElement{-25817338, -3107312, -13494599, -3182506, 30896459, -13921729, -32251644, -12707869, -19464434, -3340243}, - FieldElement{-23607977, -2665774, -526091, 4651136, 5765089, 4618330, 6092245, 14845197, 17151279, -9854116}, - FieldElement{-24830458, -12733720, -15165978, 10367250, -29530908, -265356, 22825805, -7087279, -16866484, 16176525}, - }, - { - FieldElement{-23583256, 6564961, 20063689, 3798228, -4740178, 7359225, 2006182, -10363426, -28746253, -10197509}, - FieldElement{-10626600, -4486402, -13320562, -5125317, 3432136, -6393229, 23632037, -1940610, 32808310, 1099883}, - FieldElement{15030977, 5768825, -27451236, -2887299, -6427378, -15361371, -15277896, -6809350, 2051441, -15225865}, - }, - { - FieldElement{-3362323, -7239372, 7517890, 9824992, 23555850, 295369, 5148398, -14154188, -22686354, 16633660}, - FieldElement{4577086, -16752288, 13249841, -15304328, 19958763, -14537274, 18559670, -10759549, 8402478, -9864273}, - FieldElement{-28406330, -1051581, -26790155, -907698, -17212414, -11030789, 9453451, -14980072, 17983010, 9967138}, - }, - { - FieldElement{-25762494, 6524722, 26585488, 9969270, 24709298, 1220360, -1677990, 7806337, 17507396, 3651560}, - FieldElement{-10420457, -4118111, 14584639, 15971087, -15768321, 8861010, 26556809, -5574557, -18553322, -11357135}, - FieldElement{2839101, 14284142, 4029895, 3472686, 14402957, 12689363, -26642121, 8459447, -5605463, -7621941}, - }, - { - FieldElement{-4839289, -3535444, 9744961, 2871048, 25113978, 3187018, -25110813, -849066, 17258084, -7977739}, - FieldElement{18164541, -10595176, -17154882, -1542417, 19237078, -9745295, 23357533, -15217008, 26908270, 12150756}, - FieldElement{-30264870, -7647865, 5112249, -7036672, -1499807, -6974257, 43168, -5537701, -32302074, 16215819}, - }, - }, - { - { - FieldElement{-6898905, 9824394, -12304779, -4401089, -31397141, -6276835, 32574489, 12532905, -7503072, -8675347}, - FieldElement{-27343522, -16515468, -27151524, -10722951, 946346, 16291093, 254968, 7168080, 21676107, -1943028}, - FieldElement{21260961, -8424752, -16831886, -11920822, -23677961, 3968121, -3651949, -6215466, -3556191, -7913075}, - }, - { - FieldElement{16544754, 13250366, -16804428, 15546242, -4583003, 12757258, -2462308, -8680336, -18907032, -9662799}, - FieldElement{-2415239, -15577728, 18312303, 4964443, -15272530, -12653564, 26820651, 16690659, 25459437, -4564609}, - FieldElement{-25144690, 11425020, 28423002, -11020557, -6144921, -15826224, 9142795, -2391602, -6432418, -1644817}, - }, - { - FieldElement{-23104652, 6253476, 16964147, -3768872, -25113972, -12296437, -27457225, -16344658, 6335692, 7249989}, - FieldElement{-30333227, 13979675, 7503222, -12368314, -11956721, -4621693, -30272269, 2682242, 25993170, -12478523}, - FieldElement{4364628, 5930691, 32304656, -10044554, -8054781, 15091131, 22857016, -10598955, 31820368, 15075278}, - }, - { - FieldElement{31879134, -8918693, 17258761, 90626, -8041836, -4917709, 24162788, -9650886, -17970238, 12833045}, - FieldElement{19073683, 14851414, -24403169, -11860168, 7625278, 11091125, -19619190, 2074449, -9413939, 14905377}, - FieldElement{24483667, -11935567, -2518866, -11547418, -1553130, 15355506, -25282080, 9253129, 27628530, -7555480}, - }, - { - FieldElement{17597607, 8340603, 19355617, 552187, 26198470, -3176583, 4593324, -9157582, -14110875, 15297016}, - FieldElement{510886, 14337390, -31785257, 16638632, 6328095, 2713355, -20217417, -11864220, 8683221, 2921426}, - FieldElement{18606791, 11874196, 27155355, -5281482, -24031742, 6265446, -25178240, -1278924, 4674690, 13890525}, - }, - { - FieldElement{13609624, 13069022, -27372361, -13055908, 24360586, 9592974, 14977157, 9835105, 4389687, 288396}, - FieldElement{9922506, -519394, 13613107, 5883594, -18758345, -434263, -12304062, 8317628, 23388070, 16052080}, - FieldElement{12720016, 11937594, -31970060, -5028689, 26900120, 8561328, -20155687, -11632979, -14754271, -10812892}, - }, - { - FieldElement{15961858, 14150409, 26716931, -665832, -22794328, 13603569, 11829573, 7467844, -28822128, 929275}, - FieldElement{11038231, -11582396, -27310482, -7316562, -10498527, -16307831, -23479533, -9371869, -21393143, 2465074}, - FieldElement{20017163, -4323226, 27915242, 1529148, 12396362, 15675764, 13817261, -9658066, 2463391, -4622140}, - }, - { - FieldElement{-16358878, -12663911, -12065183, 4996454, -1256422, 1073572, 9583558, 12851107, 4003896, 12673717}, - FieldElement{-1731589, -15155870, -3262930, 16143082, 19294135, 13385325, 14741514, -9103726, 7903886, 2348101}, - FieldElement{24536016, -16515207, 12715592, -3862155, 1511293, 10047386, -3842346, -7129159, -28377538, 10048127}, - }, - }, - { - { - FieldElement{-12622226, -6204820, 30718825, 2591312, -10617028, 12192840, 18873298, -7297090, -32297756, 15221632}, - FieldElement{-26478122, -11103864, 11546244, -1852483, 9180880, 7656409, -21343950, 2095755, 29769758, 6593415}, - FieldElement{-31994208, -2907461, 4176912, 3264766, 12538965, -868111, 26312345, -6118678, 30958054, 8292160}, - }, - { - FieldElement{31429822, -13959116, 29173532, 15632448, 12174511, -2760094, 32808831, 3977186, 26143136, -3148876}, - FieldElement{22648901, 1402143, -22799984, 13746059, 7936347, 365344, -8668633, -1674433, -3758243, -2304625}, - FieldElement{-15491917, 8012313, -2514730, -12702462, -23965846, -10254029, -1612713, -1535569, -16664475, 8194478}, - }, - { - FieldElement{27338066, -7507420, -7414224, 10140405, -19026427, -6589889, 27277191, 8855376, 28572286, 3005164}, - FieldElement{26287124, 4821776, 25476601, -4145903, -3764513, -15788984, -18008582, 1182479, -26094821, -13079595}, - FieldElement{-7171154, 3178080, 23970071, 6201893, -17195577, -4489192, -21876275, -13982627, 32208683, -1198248}, - }, - { - FieldElement{-16657702, 2817643, -10286362, 14811298, 6024667, 13349505, -27315504, -10497842, -27672585, -11539858}, - FieldElement{15941029, -9405932, -21367050, 8062055, 31876073, -238629, -15278393, -1444429, 15397331, -4130193}, - FieldElement{8934485, -13485467, -23286397, -13423241, -32446090, 14047986, 31170398, -1441021, -27505566, 15087184}, - }, - { - FieldElement{-18357243, -2156491, 24524913, -16677868, 15520427, -6360776, -15502406, 11461896, 16788528, -5868942}, - FieldElement{-1947386, 16013773, 21750665, 3714552, -17401782, -16055433, -3770287, -10323320, 31322514, -11615635}, - FieldElement{21426655, -5650218, -13648287, -5347537, -28812189, -4920970, -18275391, -14621414, 13040862, -12112948}, - }, - { - FieldElement{11293895, 12478086, -27136401, 15083750, -29307421, 14748872, 14555558, -13417103, 1613711, 4896935}, - FieldElement{-25894883, 15323294, -8489791, -8057900, 25967126, -13425460, 2825960, -4897045, -23971776, -11267415}, - FieldElement{-15924766, -5229880, -17443532, 6410664, 3622847, 10243618, 20615400, 12405433, -23753030, -8436416}, - }, - { - FieldElement{-7091295, 12556208, -20191352, 9025187, -17072479, 4333801, 4378436, 2432030, 23097949, -566018}, - FieldElement{4565804, -16025654, 20084412, -7842817, 1724999, 189254, 24767264, 10103221, -18512313, 2424778}, - FieldElement{366633, -11976806, 8173090, -6890119, 30788634, 5745705, -7168678, 1344109, -3642553, 12412659}, - }, - { - FieldElement{-24001791, 7690286, 14929416, -168257, -32210835, -13412986, 24162697, -15326504, -3141501, 11179385}, - FieldElement{18289522, -14724954, 8056945, 16430056, -21729724, 7842514, -6001441, -1486897, -18684645, -11443503}, - FieldElement{476239, 6601091, -6152790, -9723375, 17503545, -4863900, 27672959, 13403813, 11052904, 5219329}, - }, - }, - { - { - FieldElement{20678546, -8375738, -32671898, 8849123, -5009758, 14574752, 31186971, -3973730, 9014762, -8579056}, - FieldElement{-13644050, -10350239, -15962508, 5075808, -1514661, -11534600, -33102500, 9160280, 8473550, -3256838}, - FieldElement{24900749, 14435722, 17209120, -15292541, -22592275, 9878983, -7689309, -16335821, -24568481, 11788948}, - }, - { - FieldElement{-3118155, -11395194, -13802089, 14797441, 9652448, -6845904, -20037437, 10410733, -24568470, -1458691}, - FieldElement{-15659161, 16736706, -22467150, 10215878, -9097177, 7563911, 11871841, -12505194, -18513325, 8464118}, - FieldElement{-23400612, 8348507, -14585951, -861714, -3950205, -6373419, 14325289, 8628612, 33313881, -8370517}, - }, - { - FieldElement{-20186973, -4967935, 22367356, 5271547, -1097117, -4788838, -24805667, -10236854, -8940735, -5818269}, - FieldElement{-6948785, -1795212, -32625683, -16021179, 32635414, -7374245, 15989197, -12838188, 28358192, -4253904}, - FieldElement{-23561781, -2799059, -32351682, -1661963, -9147719, 10429267, -16637684, 4072016, -5351664, 5596589}, - }, - { - FieldElement{-28236598, -3390048, 12312896, 6213178, 3117142, 16078565, 29266239, 2557221, 1768301, 15373193}, - FieldElement{-7243358, -3246960, -4593467, -7553353, -127927, -912245, -1090902, -4504991, -24660491, 3442910}, - FieldElement{-30210571, 5124043, 14181784, 8197961, 18964734, -11939093, 22597931, 7176455, -18585478, 13365930}, - }, - { - FieldElement{-7877390, -1499958, 8324673, 4690079, 6261860, 890446, 24538107, -8570186, -9689599, -3031667}, - FieldElement{25008904, -10771599, -4305031, -9638010, 16265036, 15721635, 683793, -11823784, 15723479, -15163481}, - FieldElement{-9660625, 12374379, -27006999, -7026148, -7724114, -12314514, 11879682, 5400171, 519526, -1235876}, - }, - { - FieldElement{22258397, -16332233, -7869817, 14613016, -22520255, -2950923, -20353881, 7315967, 16648397, 7605640}, - FieldElement{-8081308, -8464597, -8223311, 9719710, 19259459, -15348212, 23994942, -5281555, -9468848, 4763278}, - FieldElement{-21699244, 9220969, -15730624, 1084137, -25476107, -2852390, 31088447, -7764523, -11356529, 728112}, - }, - { - FieldElement{26047220, -11751471, -6900323, -16521798, 24092068, 9158119, -4273545, -12555558, -29365436, -5498272}, - FieldElement{17510331, -322857, 5854289, 8403524, 17133918, -3112612, -28111007, 12327945, 10750447, 10014012}, - FieldElement{-10312768, 3936952, 9156313, -8897683, 16498692, -994647, -27481051, -666732, 3424691, 7540221}, - }, - { - FieldElement{30322361, -6964110, 11361005, -4143317, 7433304, 4989748, -7071422, -16317219, -9244265, 15258046}, - FieldElement{13054562, -2779497, 19155474, 469045, -12482797, 4566042, 5631406, 2711395, 1062915, -5136345}, - FieldElement{-19240248, -11254599, -29509029, -7499965, -5835763, 13005411, -6066489, 12194497, 32960380, 1459310}, - }, - }, - { - { - FieldElement{19852034, 7027924, 23669353, 10020366, 8586503, -6657907, 394197, -6101885, 18638003, -11174937}, - FieldElement{31395534, 15098109, 26581030, 8030562, -16527914, -5007134, 9012486, -7584354, -6643087, -5442636}, - FieldElement{-9192165, -2347377, -1997099, 4529534, 25766844, 607986, -13222, 9677543, -32294889, -6456008}, - }, - { - FieldElement{-2444496, -149937, 29348902, 8186665, 1873760, 12489863, -30934579, -7839692, -7852844, -8138429}, - FieldElement{-15236356, -15433509, 7766470, 746860, 26346930, -10221762, -27333451, 10754588, -9431476, 5203576}, - FieldElement{31834314, 14135496, -770007, 5159118, 20917671, -16768096, -7467973, -7337524, 31809243, 7347066}, - }, - { - FieldElement{-9606723, -11874240, 20414459, 13033986, 13716524, -11691881, 19797970, -12211255, 15192876, -2087490}, - FieldElement{-12663563, -2181719, 1168162, -3804809, 26747877, -14138091, 10609330, 12694420, 33473243, -13382104}, - FieldElement{33184999, 11180355, 15832085, -11385430, -1633671, 225884, 15089336, -11023903, -6135662, 14480053}, - }, - { - FieldElement{31308717, -5619998, 31030840, -1897099, 15674547, -6582883, 5496208, 13685227, 27595050, 8737275}, - FieldElement{-20318852, -15150239, 10933843, -16178022, 8335352, -7546022, -31008351, -12610604, 26498114, 66511}, - FieldElement{22644454, -8761729, -16671776, 4884562, -3105614, -13559366, 30540766, -4286747, -13327787, -7515095}, - }, - { - FieldElement{-28017847, 9834845, 18617207, -2681312, -3401956, -13307506, 8205540, 13585437, -17127465, 15115439}, - FieldElement{23711543, -672915, 31206561, -8362711, 6164647, -9709987, -33535882, -1426096, 8236921, 16492939}, - FieldElement{-23910559, -13515526, -26299483, -4503841, 25005590, -7687270, 19574902, 10071562, 6708380, -6222424}, - }, - { - FieldElement{2101391, -4930054, 19702731, 2367575, -15427167, 1047675, 5301017, 9328700, 29955601, -11678310}, - FieldElement{3096359, 9271816, -21620864, -15521844, -14847996, -7592937, -25892142, -12635595, -9917575, 6216608}, - FieldElement{-32615849, 338663, -25195611, 2510422, -29213566, -13820213, 24822830, -6146567, -26767480, 7525079}, - }, - { - FieldElement{-23066649, -13985623, 16133487, -7896178, -3389565, 778788, -910336, -2782495, -19386633, 11994101}, - FieldElement{21691500, -13624626, -641331, -14367021, 3285881, -3483596, -25064666, 9718258, -7477437, 13381418}, - FieldElement{18445390, -4202236, 14979846, 11622458, -1727110, -3582980, 23111648, -6375247, 28535282, 15779576}, - }, - { - FieldElement{30098053, 3089662, -9234387, 16662135, -21306940, 11308411, -14068454, 12021730, 9955285, -16303356}, - FieldElement{9734894, -14576830, -7473633, -9138735, 2060392, 11313496, -18426029, 9924399, 20194861, 13380996}, - FieldElement{-26378102, -7965207, -22167821, 15789297, -18055342, -6168792, -1984914, 15707771, 26342023, 10146099}, - }, - }, - { - { - FieldElement{-26016874, -219943, 21339191, -41388, 19745256, -2878700, -29637280, 2227040, 21612326, -545728}, - FieldElement{-13077387, 1184228, 23562814, -5970442, -20351244, -6348714, 25764461, 12243797, -20856566, 11649658}, - FieldElement{-10031494, 11262626, 27384172, 2271902, 26947504, -15997771, 39944, 6114064, 33514190, 2333242}, - }, - { - FieldElement{-21433588, -12421821, 8119782, 7219913, -21830522, -9016134, -6679750, -12670638, 24350578, -13450001}, - FieldElement{-4116307, -11271533, -23886186, 4843615, -30088339, 690623, -31536088, -10406836, 8317860, 12352766}, - FieldElement{18200138, -14475911, -33087759, -2696619, -23702521, -9102511, -23552096, -2287550, 20712163, 6719373}, - }, - { - FieldElement{26656208, 6075253, -7858556, 1886072, -28344043, 4262326, 11117530, -3763210, 26224235, -3297458}, - FieldElement{-17168938, -14854097, -3395676, -16369877, -19954045, 14050420, 21728352, 9493610, 18620611, -16428628}, - FieldElement{-13323321, 13325349, 11432106, 5964811, 18609221, 6062965, -5269471, -9725556, -30701573, -16479657}, - }, - { - FieldElement{-23860538, -11233159, 26961357, 1640861, -32413112, -16737940, 12248509, -5240639, 13735342, 1934062}, - FieldElement{25089769, 6742589, 17081145, -13406266, 21909293, -16067981, -15136294, -3765346, -21277997, 5473616}, - FieldElement{31883677, -7961101, 1083432, -11572403, 22828471, 13290673, -7125085, 12469656, 29111212, -5451014}, - }, - { - FieldElement{24244947, -15050407, -26262976, 2791540, -14997599, 16666678, 24367466, 6388839, -10295587, 452383}, - FieldElement{-25640782, -3417841, 5217916, 16224624, 19987036, -4082269, -24236251, -5915248, 15766062, 8407814}, - FieldElement{-20406999, 13990231, 15495425, 16395525, 5377168, 15166495, -8917023, -4388953, -8067909, 2276718}, - }, - { - FieldElement{30157918, 12924066, -17712050, 9245753, 19895028, 3368142, -23827587, 5096219, 22740376, -7303417}, - FieldElement{2041139, -14256350, 7783687, 13876377, -25946985, -13352459, 24051124, 13742383, -15637599, 13295222}, - FieldElement{33338237, -8505733, 12532113, 7977527, 9106186, -1715251, -17720195, -4612972, -4451357, -14669444}, - }, - { - FieldElement{-20045281, 5454097, -14346548, 6447146, 28862071, 1883651, -2469266, -4141880, 7770569, 9620597}, - FieldElement{23208068, 7979712, 33071466, 8149229, 1758231, -10834995, 30945528, -1694323, -33502340, -14767970}, - FieldElement{1439958, -16270480, -1079989, -793782, 4625402, 10647766, -5043801, 1220118, 30494170, -11440799}, - }, - { - FieldElement{-5037580, -13028295, -2970559, -3061767, 15640974, -6701666, -26739026, 926050, -1684339, -13333647}, - FieldElement{13908495, -3549272, 30919928, -6273825, -21521863, 7989039, 9021034, 9078865, 3353509, 4033511}, - FieldElement{-29663431, -15113610, 32259991, -344482, 24295849, -12912123, 23161163, 8839127, 27485041, 7356032}, - }, - }, - { - { - FieldElement{9661027, 705443, 11980065, -5370154, -1628543, 14661173, -6346142, 2625015, 28431036, -16771834}, - FieldElement{-23839233, -8311415, -25945511, 7480958, -17681669, -8354183, -22545972, 14150565, 15970762, 4099461}, - FieldElement{29262576, 16756590, 26350592, -8793563, 8529671, -11208050, 13617293, -9937143, 11465739, 8317062}, - }, - { - FieldElement{-25493081, -6962928, 32500200, -9419051, -23038724, -2302222, 14898637, 3848455, 20969334, -5157516}, - FieldElement{-20384450, -14347713, -18336405, 13884722, -33039454, 2842114, -21610826, -3649888, 11177095, 14989547}, - FieldElement{-24496721, -11716016, 16959896, 2278463, 12066309, 10137771, 13515641, 2581286, -28487508, 9930240}, - }, - { - FieldElement{-17751622, -2097826, 16544300, -13009300, -15914807, -14949081, 18345767, -13403753, 16291481, -5314038}, - FieldElement{-33229194, 2553288, 32678213, 9875984, 8534129, 6889387, -9676774, 6957617, 4368891, 9788741}, - FieldElement{16660756, 7281060, -10830758, 12911820, 20108584, -8101676, -21722536, -8613148, 16250552, -11111103}, - }, - { - FieldElement{-19765507, 2390526, -16551031, 14161980, 1905286, 6414907, 4689584, 10604807, -30190403, 4782747}, - FieldElement{-1354539, 14736941, -7367442, -13292886, 7710542, -14155590, -9981571, 4383045, 22546403, 437323}, - FieldElement{31665577, -12180464, -16186830, 1491339, -18368625, 3294682, 27343084, 2786261, -30633590, -14097016}, - }, - { - FieldElement{-14467279, -683715, -33374107, 7448552, 19294360, 14334329, -19690631, 2355319, -19284671, -6114373}, - FieldElement{15121312, -15796162, 6377020, -6031361, -10798111, -12957845, 18952177, 15496498, -29380133, 11754228}, - FieldElement{-2637277, -13483075, 8488727, -14303896, 12728761, -1622493, 7141596, 11724556, 22761615, -10134141}, - }, - { - FieldElement{16918416, 11729663, -18083579, 3022987, -31015732, -13339659, -28741185, -12227393, 32851222, 11717399}, - FieldElement{11166634, 7338049, -6722523, 4531520, -29468672, -7302055, 31474879, 3483633, -1193175, -4030831}, - FieldElement{-185635, 9921305, 31456609, -13536438, -12013818, 13348923, 33142652, 6546660, -19985279, -3948376}, - }, - { - FieldElement{-32460596, 11266712, -11197107, -7899103, 31703694, 3855903, -8537131, -12833048, -30772034, -15486313}, - FieldElement{-18006477, 12709068, 3991746, -6479188, -21491523, -10550425, -31135347, -16049879, 10928917, 3011958}, - FieldElement{-6957757, -15594337, 31696059, 334240, 29576716, 14796075, -30831056, -12805180, 18008031, 10258577}, - }, - { - FieldElement{-22448644, 15655569, 7018479, -4410003, -30314266, -1201591, -1853465, 1367120, 25127874, 6671743}, - FieldElement{29701166, -14373934, -10878120, 9279288, -17568, 13127210, 21382910, 11042292, 25838796, 4642684}, - FieldElement{-20430234, 14955537, -24126347, 8124619, -5369288, -5990470, 30468147, -13900640, 18423289, 4177476}, - }, - }, -} diff --git a/vendor/golang.org/x/crypto/ed25519/internal/edwards25519/edwards25519.go b/vendor/golang.org/x/crypto/ed25519/internal/edwards25519/edwards25519.go deleted file mode 100644 index fd03c252..00000000 --- a/vendor/golang.org/x/crypto/ed25519/internal/edwards25519/edwards25519.go +++ /dev/null @@ -1,1793 +0,0 @@ -// Copyright 2016 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package edwards25519 - -import "encoding/binary" - -// This code is a port of the public domain, “ref10” implementation of ed25519 -// from SUPERCOP. - -// FieldElement represents an element of the field GF(2^255 - 19). An element -// t, entries t[0]...t[9], represents the integer t[0]+2^26 t[1]+2^51 t[2]+2^77 -// t[3]+2^102 t[4]+...+2^230 t[9]. Bounds on each t[i] vary depending on -// context. -type FieldElement [10]int32 - -var zero FieldElement - -func FeZero(fe *FieldElement) { - copy(fe[:], zero[:]) -} - -func FeOne(fe *FieldElement) { - FeZero(fe) - fe[0] = 1 -} - -func FeAdd(dst, a, b *FieldElement) { - dst[0] = a[0] + b[0] - dst[1] = a[1] + b[1] - dst[2] = a[2] + b[2] - dst[3] = a[3] + b[3] - dst[4] = a[4] + b[4] - dst[5] = a[5] + b[5] - dst[6] = a[6] + b[6] - dst[7] = a[7] + b[7] - dst[8] = a[8] + b[8] - dst[9] = a[9] + b[9] -} - -func FeSub(dst, a, b *FieldElement) { - dst[0] = a[0] - b[0] - dst[1] = a[1] - b[1] - dst[2] = a[2] - b[2] - dst[3] = a[3] - b[3] - dst[4] = a[4] - b[4] - dst[5] = a[5] - b[5] - dst[6] = a[6] - b[6] - dst[7] = a[7] - b[7] - dst[8] = a[8] - b[8] - dst[9] = a[9] - b[9] -} - -func FeCopy(dst, src *FieldElement) { - copy(dst[:], src[:]) -} - -// Replace (f,g) with (g,g) if b == 1; -// replace (f,g) with (f,g) if b == 0. -// -// Preconditions: b in {0,1}. -func FeCMove(f, g *FieldElement, b int32) { - b = -b - f[0] ^= b & (f[0] ^ g[0]) - f[1] ^= b & (f[1] ^ g[1]) - f[2] ^= b & (f[2] ^ g[2]) - f[3] ^= b & (f[3] ^ g[3]) - f[4] ^= b & (f[4] ^ g[4]) - f[5] ^= b & (f[5] ^ g[5]) - f[6] ^= b & (f[6] ^ g[6]) - f[7] ^= b & (f[7] ^ g[7]) - f[8] ^= b & (f[8] ^ g[8]) - f[9] ^= b & (f[9] ^ g[9]) -} - -func load3(in []byte) int64 { - var r int64 - r = int64(in[0]) - r |= int64(in[1]) << 8 - r |= int64(in[2]) << 16 - return r -} - -func load4(in []byte) int64 { - var r int64 - r = int64(in[0]) - r |= int64(in[1]) << 8 - r |= int64(in[2]) << 16 - r |= int64(in[3]) << 24 - return r -} - -func FeFromBytes(dst *FieldElement, src *[32]byte) { - h0 := load4(src[:]) - h1 := load3(src[4:]) << 6 - h2 := load3(src[7:]) << 5 - h3 := load3(src[10:]) << 3 - h4 := load3(src[13:]) << 2 - h5 := load4(src[16:]) - h6 := load3(src[20:]) << 7 - h7 := load3(src[23:]) << 5 - h8 := load3(src[26:]) << 4 - h9 := (load3(src[29:]) & 8388607) << 2 - - FeCombine(dst, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9) -} - -// FeToBytes marshals h to s. -// Preconditions: -// |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. -// -// Write p=2^255-19; q=floor(h/p). -// Basic claim: q = floor(2^(-255)(h + 19 2^(-25)h9 + 2^(-1))). -// -// Proof: -// Have |h|<=p so |q|<=1 so |19^2 2^(-255) q|<1/4. -// Also have |h-2^230 h9|<2^230 so |19 2^(-255)(h-2^230 h9)|<1/4. -// -// Write y=2^(-1)-19^2 2^(-255)q-19 2^(-255)(h-2^230 h9). -// Then 0> 25 - q = (h[0] + q) >> 26 - q = (h[1] + q) >> 25 - q = (h[2] + q) >> 26 - q = (h[3] + q) >> 25 - q = (h[4] + q) >> 26 - q = (h[5] + q) >> 25 - q = (h[6] + q) >> 26 - q = (h[7] + q) >> 25 - q = (h[8] + q) >> 26 - q = (h[9] + q) >> 25 - - // Goal: Output h-(2^255-19)q, which is between 0 and 2^255-20. - h[0] += 19 * q - // Goal: Output h-2^255 q, which is between 0 and 2^255-20. - - carry[0] = h[0] >> 26 - h[1] += carry[0] - h[0] -= carry[0] << 26 - carry[1] = h[1] >> 25 - h[2] += carry[1] - h[1] -= carry[1] << 25 - carry[2] = h[2] >> 26 - h[3] += carry[2] - h[2] -= carry[2] << 26 - carry[3] = h[3] >> 25 - h[4] += carry[3] - h[3] -= carry[3] << 25 - carry[4] = h[4] >> 26 - h[5] += carry[4] - h[4] -= carry[4] << 26 - carry[5] = h[5] >> 25 - h[6] += carry[5] - h[5] -= carry[5] << 25 - carry[6] = h[6] >> 26 - h[7] += carry[6] - h[6] -= carry[6] << 26 - carry[7] = h[7] >> 25 - h[8] += carry[7] - h[7] -= carry[7] << 25 - carry[8] = h[8] >> 26 - h[9] += carry[8] - h[8] -= carry[8] << 26 - carry[9] = h[9] >> 25 - h[9] -= carry[9] << 25 - // h10 = carry9 - - // Goal: Output h[0]+...+2^255 h10-2^255 q, which is between 0 and 2^255-20. - // Have h[0]+...+2^230 h[9] between 0 and 2^255-1; - // evidently 2^255 h10-2^255 q = 0. - // Goal: Output h[0]+...+2^230 h[9]. - - s[0] = byte(h[0] >> 0) - s[1] = byte(h[0] >> 8) - s[2] = byte(h[0] >> 16) - s[3] = byte((h[0] >> 24) | (h[1] << 2)) - s[4] = byte(h[1] >> 6) - s[5] = byte(h[1] >> 14) - s[6] = byte((h[1] >> 22) | (h[2] << 3)) - s[7] = byte(h[2] >> 5) - s[8] = byte(h[2] >> 13) - s[9] = byte((h[2] >> 21) | (h[3] << 5)) - s[10] = byte(h[3] >> 3) - s[11] = byte(h[3] >> 11) - s[12] = byte((h[3] >> 19) | (h[4] << 6)) - s[13] = byte(h[4] >> 2) - s[14] = byte(h[4] >> 10) - s[15] = byte(h[4] >> 18) - s[16] = byte(h[5] >> 0) - s[17] = byte(h[5] >> 8) - s[18] = byte(h[5] >> 16) - s[19] = byte((h[5] >> 24) | (h[6] << 1)) - s[20] = byte(h[6] >> 7) - s[21] = byte(h[6] >> 15) - s[22] = byte((h[6] >> 23) | (h[7] << 3)) - s[23] = byte(h[7] >> 5) - s[24] = byte(h[7] >> 13) - s[25] = byte((h[7] >> 21) | (h[8] << 4)) - s[26] = byte(h[8] >> 4) - s[27] = byte(h[8] >> 12) - s[28] = byte((h[8] >> 20) | (h[9] << 6)) - s[29] = byte(h[9] >> 2) - s[30] = byte(h[9] >> 10) - s[31] = byte(h[9] >> 18) -} - -func FeIsNegative(f *FieldElement) byte { - var s [32]byte - FeToBytes(&s, f) - return s[0] & 1 -} - -func FeIsNonZero(f *FieldElement) int32 { - var s [32]byte - FeToBytes(&s, f) - var x uint8 - for _, b := range s { - x |= b - } - x |= x >> 4 - x |= x >> 2 - x |= x >> 1 - return int32(x & 1) -} - -// FeNeg sets h = -f -// -// Preconditions: -// |f| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. -// -// Postconditions: -// |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. -func FeNeg(h, f *FieldElement) { - h[0] = -f[0] - h[1] = -f[1] - h[2] = -f[2] - h[3] = -f[3] - h[4] = -f[4] - h[5] = -f[5] - h[6] = -f[6] - h[7] = -f[7] - h[8] = -f[8] - h[9] = -f[9] -} - -func FeCombine(h *FieldElement, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9 int64) { - var c0, c1, c2, c3, c4, c5, c6, c7, c8, c9 int64 - - /* - |h0| <= (1.1*1.1*2^52*(1+19+19+19+19)+1.1*1.1*2^50*(38+38+38+38+38)) - i.e. |h0| <= 1.2*2^59; narrower ranges for h2, h4, h6, h8 - |h1| <= (1.1*1.1*2^51*(1+1+19+19+19+19+19+19+19+19)) - i.e. |h1| <= 1.5*2^58; narrower ranges for h3, h5, h7, h9 - */ - - c0 = (h0 + (1 << 25)) >> 26 - h1 += c0 - h0 -= c0 << 26 - c4 = (h4 + (1 << 25)) >> 26 - h5 += c4 - h4 -= c4 << 26 - /* |h0| <= 2^25 */ - /* |h4| <= 2^25 */ - /* |h1| <= 1.51*2^58 */ - /* |h5| <= 1.51*2^58 */ - - c1 = (h1 + (1 << 24)) >> 25 - h2 += c1 - h1 -= c1 << 25 - c5 = (h5 + (1 << 24)) >> 25 - h6 += c5 - h5 -= c5 << 25 - /* |h1| <= 2^24; from now on fits into int32 */ - /* |h5| <= 2^24; from now on fits into int32 */ - /* |h2| <= 1.21*2^59 */ - /* |h6| <= 1.21*2^59 */ - - c2 = (h2 + (1 << 25)) >> 26 - h3 += c2 - h2 -= c2 << 26 - c6 = (h6 + (1 << 25)) >> 26 - h7 += c6 - h6 -= c6 << 26 - /* |h2| <= 2^25; from now on fits into int32 unchanged */ - /* |h6| <= 2^25; from now on fits into int32 unchanged */ - /* |h3| <= 1.51*2^58 */ - /* |h7| <= 1.51*2^58 */ - - c3 = (h3 + (1 << 24)) >> 25 - h4 += c3 - h3 -= c3 << 25 - c7 = (h7 + (1 << 24)) >> 25 - h8 += c7 - h7 -= c7 << 25 - /* |h3| <= 2^24; from now on fits into int32 unchanged */ - /* |h7| <= 2^24; from now on fits into int32 unchanged */ - /* |h4| <= 1.52*2^33 */ - /* |h8| <= 1.52*2^33 */ - - c4 = (h4 + (1 << 25)) >> 26 - h5 += c4 - h4 -= c4 << 26 - c8 = (h8 + (1 << 25)) >> 26 - h9 += c8 - h8 -= c8 << 26 - /* |h4| <= 2^25; from now on fits into int32 unchanged */ - /* |h8| <= 2^25; from now on fits into int32 unchanged */ - /* |h5| <= 1.01*2^24 */ - /* |h9| <= 1.51*2^58 */ - - c9 = (h9 + (1 << 24)) >> 25 - h0 += c9 * 19 - h9 -= c9 << 25 - /* |h9| <= 2^24; from now on fits into int32 unchanged */ - /* |h0| <= 1.8*2^37 */ - - c0 = (h0 + (1 << 25)) >> 26 - h1 += c0 - h0 -= c0 << 26 - /* |h0| <= 2^25; from now on fits into int32 unchanged */ - /* |h1| <= 1.01*2^24 */ - - h[0] = int32(h0) - h[1] = int32(h1) - h[2] = int32(h2) - h[3] = int32(h3) - h[4] = int32(h4) - h[5] = int32(h5) - h[6] = int32(h6) - h[7] = int32(h7) - h[8] = int32(h8) - h[9] = int32(h9) -} - -// FeMul calculates h = f * g -// Can overlap h with f or g. -// -// Preconditions: -// |f| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc. -// |g| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc. -// -// Postconditions: -// |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. -// -// Notes on implementation strategy: -// -// Using schoolbook multiplication. -// Karatsuba would save a little in some cost models. -// -// Most multiplications by 2 and 19 are 32-bit precomputations; -// cheaper than 64-bit postcomputations. -// -// There is one remaining multiplication by 19 in the carry chain; -// one *19 precomputation can be merged into this, -// but the resulting data flow is considerably less clean. -// -// There are 12 carries below. -// 10 of them are 2-way parallelizable and vectorizable. -// Can get away with 11 carries, but then data flow is much deeper. -// -// With tighter constraints on inputs, can squeeze carries into int32. -func FeMul(h, f, g *FieldElement) { - f0 := int64(f[0]) - f1 := int64(f[1]) - f2 := int64(f[2]) - f3 := int64(f[3]) - f4 := int64(f[4]) - f5 := int64(f[5]) - f6 := int64(f[6]) - f7 := int64(f[7]) - f8 := int64(f[8]) - f9 := int64(f[9]) - - f1_2 := int64(2 * f[1]) - f3_2 := int64(2 * f[3]) - f5_2 := int64(2 * f[5]) - f7_2 := int64(2 * f[7]) - f9_2 := int64(2 * f[9]) - - g0 := int64(g[0]) - g1 := int64(g[1]) - g2 := int64(g[2]) - g3 := int64(g[3]) - g4 := int64(g[4]) - g5 := int64(g[5]) - g6 := int64(g[6]) - g7 := int64(g[7]) - g8 := int64(g[8]) - g9 := int64(g[9]) - - g1_19 := int64(19 * g[1]) /* 1.4*2^29 */ - g2_19 := int64(19 * g[2]) /* 1.4*2^30; still ok */ - g3_19 := int64(19 * g[3]) - g4_19 := int64(19 * g[4]) - g5_19 := int64(19 * g[5]) - g6_19 := int64(19 * g[6]) - g7_19 := int64(19 * g[7]) - g8_19 := int64(19 * g[8]) - g9_19 := int64(19 * g[9]) - - h0 := f0*g0 + f1_2*g9_19 + f2*g8_19 + f3_2*g7_19 + f4*g6_19 + f5_2*g5_19 + f6*g4_19 + f7_2*g3_19 + f8*g2_19 + f9_2*g1_19 - h1 := f0*g1 + f1*g0 + f2*g9_19 + f3*g8_19 + f4*g7_19 + f5*g6_19 + f6*g5_19 + f7*g4_19 + f8*g3_19 + f9*g2_19 - h2 := f0*g2 + f1_2*g1 + f2*g0 + f3_2*g9_19 + f4*g8_19 + f5_2*g7_19 + f6*g6_19 + f7_2*g5_19 + f8*g4_19 + f9_2*g3_19 - h3 := f0*g3 + f1*g2 + f2*g1 + f3*g0 + f4*g9_19 + f5*g8_19 + f6*g7_19 + f7*g6_19 + f8*g5_19 + f9*g4_19 - h4 := f0*g4 + f1_2*g3 + f2*g2 + f3_2*g1 + f4*g0 + f5_2*g9_19 + f6*g8_19 + f7_2*g7_19 + f8*g6_19 + f9_2*g5_19 - h5 := f0*g5 + f1*g4 + f2*g3 + f3*g2 + f4*g1 + f5*g0 + f6*g9_19 + f7*g8_19 + f8*g7_19 + f9*g6_19 - h6 := f0*g6 + f1_2*g5 + f2*g4 + f3_2*g3 + f4*g2 + f5_2*g1 + f6*g0 + f7_2*g9_19 + f8*g8_19 + f9_2*g7_19 - h7 := f0*g7 + f1*g6 + f2*g5 + f3*g4 + f4*g3 + f5*g2 + f6*g1 + f7*g0 + f8*g9_19 + f9*g8_19 - h8 := f0*g8 + f1_2*g7 + f2*g6 + f3_2*g5 + f4*g4 + f5_2*g3 + f6*g2 + f7_2*g1 + f8*g0 + f9_2*g9_19 - h9 := f0*g9 + f1*g8 + f2*g7 + f3*g6 + f4*g5 + f5*g4 + f6*g3 + f7*g2 + f8*g1 + f9*g0 - - FeCombine(h, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9) -} - -func feSquare(f *FieldElement) (h0, h1, h2, h3, h4, h5, h6, h7, h8, h9 int64) { - f0 := int64(f[0]) - f1 := int64(f[1]) - f2 := int64(f[2]) - f3 := int64(f[3]) - f4 := int64(f[4]) - f5 := int64(f[5]) - f6 := int64(f[6]) - f7 := int64(f[7]) - f8 := int64(f[8]) - f9 := int64(f[9]) - f0_2 := int64(2 * f[0]) - f1_2 := int64(2 * f[1]) - f2_2 := int64(2 * f[2]) - f3_2 := int64(2 * f[3]) - f4_2 := int64(2 * f[4]) - f5_2 := int64(2 * f[5]) - f6_2 := int64(2 * f[6]) - f7_2 := int64(2 * f[7]) - f5_38 := 38 * f5 // 1.31*2^30 - f6_19 := 19 * f6 // 1.31*2^30 - f7_38 := 38 * f7 // 1.31*2^30 - f8_19 := 19 * f8 // 1.31*2^30 - f9_38 := 38 * f9 // 1.31*2^30 - - h0 = f0*f0 + f1_2*f9_38 + f2_2*f8_19 + f3_2*f7_38 + f4_2*f6_19 + f5*f5_38 - h1 = f0_2*f1 + f2*f9_38 + f3_2*f8_19 + f4*f7_38 + f5_2*f6_19 - h2 = f0_2*f2 + f1_2*f1 + f3_2*f9_38 + f4_2*f8_19 + f5_2*f7_38 + f6*f6_19 - h3 = f0_2*f3 + f1_2*f2 + f4*f9_38 + f5_2*f8_19 + f6*f7_38 - h4 = f0_2*f4 + f1_2*f3_2 + f2*f2 + f5_2*f9_38 + f6_2*f8_19 + f7*f7_38 - h5 = f0_2*f5 + f1_2*f4 + f2_2*f3 + f6*f9_38 + f7_2*f8_19 - h6 = f0_2*f6 + f1_2*f5_2 + f2_2*f4 + f3_2*f3 + f7_2*f9_38 + f8*f8_19 - h7 = f0_2*f7 + f1_2*f6 + f2_2*f5 + f3_2*f4 + f8*f9_38 - h8 = f0_2*f8 + f1_2*f7_2 + f2_2*f6 + f3_2*f5_2 + f4*f4 + f9*f9_38 - h9 = f0_2*f9 + f1_2*f8 + f2_2*f7 + f3_2*f6 + f4_2*f5 - - return -} - -// FeSquare calculates h = f*f. Can overlap h with f. -// -// Preconditions: -// |f| bounded by 1.1*2^26,1.1*2^25,1.1*2^26,1.1*2^25,etc. -// -// Postconditions: -// |h| bounded by 1.1*2^25,1.1*2^24,1.1*2^25,1.1*2^24,etc. -func FeSquare(h, f *FieldElement) { - h0, h1, h2, h3, h4, h5, h6, h7, h8, h9 := feSquare(f) - FeCombine(h, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9) -} - -// FeSquare2 sets h = 2 * f * f -// -// Can overlap h with f. -// -// Preconditions: -// |f| bounded by 1.65*2^26,1.65*2^25,1.65*2^26,1.65*2^25,etc. -// -// Postconditions: -// |h| bounded by 1.01*2^25,1.01*2^24,1.01*2^25,1.01*2^24,etc. -// See fe_mul.c for discussion of implementation strategy. -func FeSquare2(h, f *FieldElement) { - h0, h1, h2, h3, h4, h5, h6, h7, h8, h9 := feSquare(f) - - h0 += h0 - h1 += h1 - h2 += h2 - h3 += h3 - h4 += h4 - h5 += h5 - h6 += h6 - h7 += h7 - h8 += h8 - h9 += h9 - - FeCombine(h, h0, h1, h2, h3, h4, h5, h6, h7, h8, h9) -} - -func FeInvert(out, z *FieldElement) { - var t0, t1, t2, t3 FieldElement - var i int - - FeSquare(&t0, z) // 2^1 - FeSquare(&t1, &t0) // 2^2 - for i = 1; i < 2; i++ { // 2^3 - FeSquare(&t1, &t1) - } - FeMul(&t1, z, &t1) // 2^3 + 2^0 - FeMul(&t0, &t0, &t1) // 2^3 + 2^1 + 2^0 - FeSquare(&t2, &t0) // 2^4 + 2^2 + 2^1 - FeMul(&t1, &t1, &t2) // 2^4 + 2^3 + 2^2 + 2^1 + 2^0 - FeSquare(&t2, &t1) // 5,4,3,2,1 - for i = 1; i < 5; i++ { // 9,8,7,6,5 - FeSquare(&t2, &t2) - } - FeMul(&t1, &t2, &t1) // 9,8,7,6,5,4,3,2,1,0 - FeSquare(&t2, &t1) // 10..1 - for i = 1; i < 10; i++ { // 19..10 - FeSquare(&t2, &t2) - } - FeMul(&t2, &t2, &t1) // 19..0 - FeSquare(&t3, &t2) // 20..1 - for i = 1; i < 20; i++ { // 39..20 - FeSquare(&t3, &t3) - } - FeMul(&t2, &t3, &t2) // 39..0 - FeSquare(&t2, &t2) // 40..1 - for i = 1; i < 10; i++ { // 49..10 - FeSquare(&t2, &t2) - } - FeMul(&t1, &t2, &t1) // 49..0 - FeSquare(&t2, &t1) // 50..1 - for i = 1; i < 50; i++ { // 99..50 - FeSquare(&t2, &t2) - } - FeMul(&t2, &t2, &t1) // 99..0 - FeSquare(&t3, &t2) // 100..1 - for i = 1; i < 100; i++ { // 199..100 - FeSquare(&t3, &t3) - } - FeMul(&t2, &t3, &t2) // 199..0 - FeSquare(&t2, &t2) // 200..1 - for i = 1; i < 50; i++ { // 249..50 - FeSquare(&t2, &t2) - } - FeMul(&t1, &t2, &t1) // 249..0 - FeSquare(&t1, &t1) // 250..1 - for i = 1; i < 5; i++ { // 254..5 - FeSquare(&t1, &t1) - } - FeMul(out, &t1, &t0) // 254..5,3,1,0 -} - -func fePow22523(out, z *FieldElement) { - var t0, t1, t2 FieldElement - var i int - - FeSquare(&t0, z) - for i = 1; i < 1; i++ { - FeSquare(&t0, &t0) - } - FeSquare(&t1, &t0) - for i = 1; i < 2; i++ { - FeSquare(&t1, &t1) - } - FeMul(&t1, z, &t1) - FeMul(&t0, &t0, &t1) - FeSquare(&t0, &t0) - for i = 1; i < 1; i++ { - FeSquare(&t0, &t0) - } - FeMul(&t0, &t1, &t0) - FeSquare(&t1, &t0) - for i = 1; i < 5; i++ { - FeSquare(&t1, &t1) - } - FeMul(&t0, &t1, &t0) - FeSquare(&t1, &t0) - for i = 1; i < 10; i++ { - FeSquare(&t1, &t1) - } - FeMul(&t1, &t1, &t0) - FeSquare(&t2, &t1) - for i = 1; i < 20; i++ { - FeSquare(&t2, &t2) - } - FeMul(&t1, &t2, &t1) - FeSquare(&t1, &t1) - for i = 1; i < 10; i++ { - FeSquare(&t1, &t1) - } - FeMul(&t0, &t1, &t0) - FeSquare(&t1, &t0) - for i = 1; i < 50; i++ { - FeSquare(&t1, &t1) - } - FeMul(&t1, &t1, &t0) - FeSquare(&t2, &t1) - for i = 1; i < 100; i++ { - FeSquare(&t2, &t2) - } - FeMul(&t1, &t2, &t1) - FeSquare(&t1, &t1) - for i = 1; i < 50; i++ { - FeSquare(&t1, &t1) - } - FeMul(&t0, &t1, &t0) - FeSquare(&t0, &t0) - for i = 1; i < 2; i++ { - FeSquare(&t0, &t0) - } - FeMul(out, &t0, z) -} - -// Group elements are members of the elliptic curve -x^2 + y^2 = 1 + d * x^2 * -// y^2 where d = -121665/121666. -// -// Several representations are used: -// ProjectiveGroupElement: (X:Y:Z) satisfying x=X/Z, y=Y/Z -// ExtendedGroupElement: (X:Y:Z:T) satisfying x=X/Z, y=Y/Z, XY=ZT -// CompletedGroupElement: ((X:Z),(Y:T)) satisfying x=X/Z, y=Y/T -// PreComputedGroupElement: (y+x,y-x,2dxy) - -type ProjectiveGroupElement struct { - X, Y, Z FieldElement -} - -type ExtendedGroupElement struct { - X, Y, Z, T FieldElement -} - -type CompletedGroupElement struct { - X, Y, Z, T FieldElement -} - -type PreComputedGroupElement struct { - yPlusX, yMinusX, xy2d FieldElement -} - -type CachedGroupElement struct { - yPlusX, yMinusX, Z, T2d FieldElement -} - -func (p *ProjectiveGroupElement) Zero() { - FeZero(&p.X) - FeOne(&p.Y) - FeOne(&p.Z) -} - -func (p *ProjectiveGroupElement) Double(r *CompletedGroupElement) { - var t0 FieldElement - - FeSquare(&r.X, &p.X) - FeSquare(&r.Z, &p.Y) - FeSquare2(&r.T, &p.Z) - FeAdd(&r.Y, &p.X, &p.Y) - FeSquare(&t0, &r.Y) - FeAdd(&r.Y, &r.Z, &r.X) - FeSub(&r.Z, &r.Z, &r.X) - FeSub(&r.X, &t0, &r.Y) - FeSub(&r.T, &r.T, &r.Z) -} - -func (p *ProjectiveGroupElement) ToBytes(s *[32]byte) { - var recip, x, y FieldElement - - FeInvert(&recip, &p.Z) - FeMul(&x, &p.X, &recip) - FeMul(&y, &p.Y, &recip) - FeToBytes(s, &y) - s[31] ^= FeIsNegative(&x) << 7 -} - -func (p *ExtendedGroupElement) Zero() { - FeZero(&p.X) - FeOne(&p.Y) - FeOne(&p.Z) - FeZero(&p.T) -} - -func (p *ExtendedGroupElement) Double(r *CompletedGroupElement) { - var q ProjectiveGroupElement - p.ToProjective(&q) - q.Double(r) -} - -func (p *ExtendedGroupElement) ToCached(r *CachedGroupElement) { - FeAdd(&r.yPlusX, &p.Y, &p.X) - FeSub(&r.yMinusX, &p.Y, &p.X) - FeCopy(&r.Z, &p.Z) - FeMul(&r.T2d, &p.T, &d2) -} - -func (p *ExtendedGroupElement) ToProjective(r *ProjectiveGroupElement) { - FeCopy(&r.X, &p.X) - FeCopy(&r.Y, &p.Y) - FeCopy(&r.Z, &p.Z) -} - -func (p *ExtendedGroupElement) ToBytes(s *[32]byte) { - var recip, x, y FieldElement - - FeInvert(&recip, &p.Z) - FeMul(&x, &p.X, &recip) - FeMul(&y, &p.Y, &recip) - FeToBytes(s, &y) - s[31] ^= FeIsNegative(&x) << 7 -} - -func (p *ExtendedGroupElement) FromBytes(s *[32]byte) bool { - var u, v, v3, vxx, check FieldElement - - FeFromBytes(&p.Y, s) - FeOne(&p.Z) - FeSquare(&u, &p.Y) - FeMul(&v, &u, &d) - FeSub(&u, &u, &p.Z) // y = y^2-1 - FeAdd(&v, &v, &p.Z) // v = dy^2+1 - - FeSquare(&v3, &v) - FeMul(&v3, &v3, &v) // v3 = v^3 - FeSquare(&p.X, &v3) - FeMul(&p.X, &p.X, &v) - FeMul(&p.X, &p.X, &u) // x = uv^7 - - fePow22523(&p.X, &p.X) // x = (uv^7)^((q-5)/8) - FeMul(&p.X, &p.X, &v3) - FeMul(&p.X, &p.X, &u) // x = uv^3(uv^7)^((q-5)/8) - - var tmpX, tmp2 [32]byte - - FeSquare(&vxx, &p.X) - FeMul(&vxx, &vxx, &v) - FeSub(&check, &vxx, &u) // vx^2-u - if FeIsNonZero(&check) == 1 { - FeAdd(&check, &vxx, &u) // vx^2+u - if FeIsNonZero(&check) == 1 { - return false - } - FeMul(&p.X, &p.X, &SqrtM1) - - FeToBytes(&tmpX, &p.X) - for i, v := range tmpX { - tmp2[31-i] = v - } - } - - if FeIsNegative(&p.X) != (s[31] >> 7) { - FeNeg(&p.X, &p.X) - } - - FeMul(&p.T, &p.X, &p.Y) - return true -} - -func (p *CompletedGroupElement) ToProjective(r *ProjectiveGroupElement) { - FeMul(&r.X, &p.X, &p.T) - FeMul(&r.Y, &p.Y, &p.Z) - FeMul(&r.Z, &p.Z, &p.T) -} - -func (p *CompletedGroupElement) ToExtended(r *ExtendedGroupElement) { - FeMul(&r.X, &p.X, &p.T) - FeMul(&r.Y, &p.Y, &p.Z) - FeMul(&r.Z, &p.Z, &p.T) - FeMul(&r.T, &p.X, &p.Y) -} - -func (p *PreComputedGroupElement) Zero() { - FeOne(&p.yPlusX) - FeOne(&p.yMinusX) - FeZero(&p.xy2d) -} - -func geAdd(r *CompletedGroupElement, p *ExtendedGroupElement, q *CachedGroupElement) { - var t0 FieldElement - - FeAdd(&r.X, &p.Y, &p.X) - FeSub(&r.Y, &p.Y, &p.X) - FeMul(&r.Z, &r.X, &q.yPlusX) - FeMul(&r.Y, &r.Y, &q.yMinusX) - FeMul(&r.T, &q.T2d, &p.T) - FeMul(&r.X, &p.Z, &q.Z) - FeAdd(&t0, &r.X, &r.X) - FeSub(&r.X, &r.Z, &r.Y) - FeAdd(&r.Y, &r.Z, &r.Y) - FeAdd(&r.Z, &t0, &r.T) - FeSub(&r.T, &t0, &r.T) -} - -func geSub(r *CompletedGroupElement, p *ExtendedGroupElement, q *CachedGroupElement) { - var t0 FieldElement - - FeAdd(&r.X, &p.Y, &p.X) - FeSub(&r.Y, &p.Y, &p.X) - FeMul(&r.Z, &r.X, &q.yMinusX) - FeMul(&r.Y, &r.Y, &q.yPlusX) - FeMul(&r.T, &q.T2d, &p.T) - FeMul(&r.X, &p.Z, &q.Z) - FeAdd(&t0, &r.X, &r.X) - FeSub(&r.X, &r.Z, &r.Y) - FeAdd(&r.Y, &r.Z, &r.Y) - FeSub(&r.Z, &t0, &r.T) - FeAdd(&r.T, &t0, &r.T) -} - -func geMixedAdd(r *CompletedGroupElement, p *ExtendedGroupElement, q *PreComputedGroupElement) { - var t0 FieldElement - - FeAdd(&r.X, &p.Y, &p.X) - FeSub(&r.Y, &p.Y, &p.X) - FeMul(&r.Z, &r.X, &q.yPlusX) - FeMul(&r.Y, &r.Y, &q.yMinusX) - FeMul(&r.T, &q.xy2d, &p.T) - FeAdd(&t0, &p.Z, &p.Z) - FeSub(&r.X, &r.Z, &r.Y) - FeAdd(&r.Y, &r.Z, &r.Y) - FeAdd(&r.Z, &t0, &r.T) - FeSub(&r.T, &t0, &r.T) -} - -func geMixedSub(r *CompletedGroupElement, p *ExtendedGroupElement, q *PreComputedGroupElement) { - var t0 FieldElement - - FeAdd(&r.X, &p.Y, &p.X) - FeSub(&r.Y, &p.Y, &p.X) - FeMul(&r.Z, &r.X, &q.yMinusX) - FeMul(&r.Y, &r.Y, &q.yPlusX) - FeMul(&r.T, &q.xy2d, &p.T) - FeAdd(&t0, &p.Z, &p.Z) - FeSub(&r.X, &r.Z, &r.Y) - FeAdd(&r.Y, &r.Z, &r.Y) - FeSub(&r.Z, &t0, &r.T) - FeAdd(&r.T, &t0, &r.T) -} - -func slide(r *[256]int8, a *[32]byte) { - for i := range r { - r[i] = int8(1 & (a[i>>3] >> uint(i&7))) - } - - for i := range r { - if r[i] != 0 { - for b := 1; b <= 6 && i+b < 256; b++ { - if r[i+b] != 0 { - if r[i]+(r[i+b]<= -15 { - r[i] -= r[i+b] << uint(b) - for k := i + b; k < 256; k++ { - if r[k] == 0 { - r[k] = 1 - break - } - r[k] = 0 - } - } else { - break - } - } - } - } - } -} - -// GeDoubleScalarMultVartime sets r = a*A + b*B -// where a = a[0]+256*a[1]+...+256^31 a[31]. -// and b = b[0]+256*b[1]+...+256^31 b[31]. -// B is the Ed25519 base point (x,4/5) with x positive. -func GeDoubleScalarMultVartime(r *ProjectiveGroupElement, a *[32]byte, A *ExtendedGroupElement, b *[32]byte) { - var aSlide, bSlide [256]int8 - var Ai [8]CachedGroupElement // A,3A,5A,7A,9A,11A,13A,15A - var t CompletedGroupElement - var u, A2 ExtendedGroupElement - var i int - - slide(&aSlide, a) - slide(&bSlide, b) - - A.ToCached(&Ai[0]) - A.Double(&t) - t.ToExtended(&A2) - - for i := 0; i < 7; i++ { - geAdd(&t, &A2, &Ai[i]) - t.ToExtended(&u) - u.ToCached(&Ai[i+1]) - } - - r.Zero() - - for i = 255; i >= 0; i-- { - if aSlide[i] != 0 || bSlide[i] != 0 { - break - } - } - - for ; i >= 0; i-- { - r.Double(&t) - - if aSlide[i] > 0 { - t.ToExtended(&u) - geAdd(&t, &u, &Ai[aSlide[i]/2]) - } else if aSlide[i] < 0 { - t.ToExtended(&u) - geSub(&t, &u, &Ai[(-aSlide[i])/2]) - } - - if bSlide[i] > 0 { - t.ToExtended(&u) - geMixedAdd(&t, &u, &bi[bSlide[i]/2]) - } else if bSlide[i] < 0 { - t.ToExtended(&u) - geMixedSub(&t, &u, &bi[(-bSlide[i])/2]) - } - - t.ToProjective(r) - } -} - -// equal returns 1 if b == c and 0 otherwise, assuming that b and c are -// non-negative. -func equal(b, c int32) int32 { - x := uint32(b ^ c) - x-- - return int32(x >> 31) -} - -// negative returns 1 if b < 0 and 0 otherwise. -func negative(b int32) int32 { - return (b >> 31) & 1 -} - -func PreComputedGroupElementCMove(t, u *PreComputedGroupElement, b int32) { - FeCMove(&t.yPlusX, &u.yPlusX, b) - FeCMove(&t.yMinusX, &u.yMinusX, b) - FeCMove(&t.xy2d, &u.xy2d, b) -} - -func selectPoint(t *PreComputedGroupElement, pos int32, b int32) { - var minusT PreComputedGroupElement - bNegative := negative(b) - bAbs := b - (((-bNegative) & b) << 1) - - t.Zero() - for i := int32(0); i < 8; i++ { - PreComputedGroupElementCMove(t, &base[pos][i], equal(bAbs, i+1)) - } - FeCopy(&minusT.yPlusX, &t.yMinusX) - FeCopy(&minusT.yMinusX, &t.yPlusX) - FeNeg(&minusT.xy2d, &t.xy2d) - PreComputedGroupElementCMove(t, &minusT, bNegative) -} - -// GeScalarMultBase computes h = a*B, where -// a = a[0]+256*a[1]+...+256^31 a[31] -// B is the Ed25519 base point (x,4/5) with x positive. -// -// Preconditions: -// a[31] <= 127 -func GeScalarMultBase(h *ExtendedGroupElement, a *[32]byte) { - var e [64]int8 - - for i, v := range a { - e[2*i] = int8(v & 15) - e[2*i+1] = int8((v >> 4) & 15) - } - - // each e[i] is between 0 and 15 and e[63] is between 0 and 7. - - carry := int8(0) - for i := 0; i < 63; i++ { - e[i] += carry - carry = (e[i] + 8) >> 4 - e[i] -= carry << 4 - } - e[63] += carry - // each e[i] is between -8 and 8. - - h.Zero() - var t PreComputedGroupElement - var r CompletedGroupElement - for i := int32(1); i < 64; i += 2 { - selectPoint(&t, i/2, int32(e[i])) - geMixedAdd(&r, h, &t) - r.ToExtended(h) - } - - var s ProjectiveGroupElement - - h.Double(&r) - r.ToProjective(&s) - s.Double(&r) - r.ToProjective(&s) - s.Double(&r) - r.ToProjective(&s) - s.Double(&r) - r.ToExtended(h) - - for i := int32(0); i < 64; i += 2 { - selectPoint(&t, i/2, int32(e[i])) - geMixedAdd(&r, h, &t) - r.ToExtended(h) - } -} - -// The scalars are GF(2^252 + 27742317777372353535851937790883648493). - -// Input: -// a[0]+256*a[1]+...+256^31*a[31] = a -// b[0]+256*b[1]+...+256^31*b[31] = b -// c[0]+256*c[1]+...+256^31*c[31] = c -// -// Output: -// s[0]+256*s[1]+...+256^31*s[31] = (ab+c) mod l -// where l = 2^252 + 27742317777372353535851937790883648493. -func ScMulAdd(s, a, b, c *[32]byte) { - a0 := 2097151 & load3(a[:]) - a1 := 2097151 & (load4(a[2:]) >> 5) - a2 := 2097151 & (load3(a[5:]) >> 2) - a3 := 2097151 & (load4(a[7:]) >> 7) - a4 := 2097151 & (load4(a[10:]) >> 4) - a5 := 2097151 & (load3(a[13:]) >> 1) - a6 := 2097151 & (load4(a[15:]) >> 6) - a7 := 2097151 & (load3(a[18:]) >> 3) - a8 := 2097151 & load3(a[21:]) - a9 := 2097151 & (load4(a[23:]) >> 5) - a10 := 2097151 & (load3(a[26:]) >> 2) - a11 := (load4(a[28:]) >> 7) - b0 := 2097151 & load3(b[:]) - b1 := 2097151 & (load4(b[2:]) >> 5) - b2 := 2097151 & (load3(b[5:]) >> 2) - b3 := 2097151 & (load4(b[7:]) >> 7) - b4 := 2097151 & (load4(b[10:]) >> 4) - b5 := 2097151 & (load3(b[13:]) >> 1) - b6 := 2097151 & (load4(b[15:]) >> 6) - b7 := 2097151 & (load3(b[18:]) >> 3) - b8 := 2097151 & load3(b[21:]) - b9 := 2097151 & (load4(b[23:]) >> 5) - b10 := 2097151 & (load3(b[26:]) >> 2) - b11 := (load4(b[28:]) >> 7) - c0 := 2097151 & load3(c[:]) - c1 := 2097151 & (load4(c[2:]) >> 5) - c2 := 2097151 & (load3(c[5:]) >> 2) - c3 := 2097151 & (load4(c[7:]) >> 7) - c4 := 2097151 & (load4(c[10:]) >> 4) - c5 := 2097151 & (load3(c[13:]) >> 1) - c6 := 2097151 & (load4(c[15:]) >> 6) - c7 := 2097151 & (load3(c[18:]) >> 3) - c8 := 2097151 & load3(c[21:]) - c9 := 2097151 & (load4(c[23:]) >> 5) - c10 := 2097151 & (load3(c[26:]) >> 2) - c11 := (load4(c[28:]) >> 7) - var carry [23]int64 - - s0 := c0 + a0*b0 - s1 := c1 + a0*b1 + a1*b0 - s2 := c2 + a0*b2 + a1*b1 + a2*b0 - s3 := c3 + a0*b3 + a1*b2 + a2*b1 + a3*b0 - s4 := c4 + a0*b4 + a1*b3 + a2*b2 + a3*b1 + a4*b0 - s5 := c5 + a0*b5 + a1*b4 + a2*b3 + a3*b2 + a4*b1 + a5*b0 - s6 := c6 + a0*b6 + a1*b5 + a2*b4 + a3*b3 + a4*b2 + a5*b1 + a6*b0 - s7 := c7 + a0*b7 + a1*b6 + a2*b5 + a3*b4 + a4*b3 + a5*b2 + a6*b1 + a7*b0 - s8 := c8 + a0*b8 + a1*b7 + a2*b6 + a3*b5 + a4*b4 + a5*b3 + a6*b2 + a7*b1 + a8*b0 - s9 := c9 + a0*b9 + a1*b8 + a2*b7 + a3*b6 + a4*b5 + a5*b4 + a6*b3 + a7*b2 + a8*b1 + a9*b0 - s10 := c10 + a0*b10 + a1*b9 + a2*b8 + a3*b7 + a4*b6 + a5*b5 + a6*b4 + a7*b3 + a8*b2 + a9*b1 + a10*b0 - s11 := c11 + a0*b11 + a1*b10 + a2*b9 + a3*b8 + a4*b7 + a5*b6 + a6*b5 + a7*b4 + a8*b3 + a9*b2 + a10*b1 + a11*b0 - s12 := a1*b11 + a2*b10 + a3*b9 + a4*b8 + a5*b7 + a6*b6 + a7*b5 + a8*b4 + a9*b3 + a10*b2 + a11*b1 - s13 := a2*b11 + a3*b10 + a4*b9 + a5*b8 + a6*b7 + a7*b6 + a8*b5 + a9*b4 + a10*b3 + a11*b2 - s14 := a3*b11 + a4*b10 + a5*b9 + a6*b8 + a7*b7 + a8*b6 + a9*b5 + a10*b4 + a11*b3 - s15 := a4*b11 + a5*b10 + a6*b9 + a7*b8 + a8*b7 + a9*b6 + a10*b5 + a11*b4 - s16 := a5*b11 + a6*b10 + a7*b9 + a8*b8 + a9*b7 + a10*b6 + a11*b5 - s17 := a6*b11 + a7*b10 + a8*b9 + a9*b8 + a10*b7 + a11*b6 - s18 := a7*b11 + a8*b10 + a9*b9 + a10*b8 + a11*b7 - s19 := a8*b11 + a9*b10 + a10*b9 + a11*b8 - s20 := a9*b11 + a10*b10 + a11*b9 - s21 := a10*b11 + a11*b10 - s22 := a11 * b11 - s23 := int64(0) - - carry[0] = (s0 + (1 << 20)) >> 21 - s1 += carry[0] - s0 -= carry[0] << 21 - carry[2] = (s2 + (1 << 20)) >> 21 - s3 += carry[2] - s2 -= carry[2] << 21 - carry[4] = (s4 + (1 << 20)) >> 21 - s5 += carry[4] - s4 -= carry[4] << 21 - carry[6] = (s6 + (1 << 20)) >> 21 - s7 += carry[6] - s6 -= carry[6] << 21 - carry[8] = (s8 + (1 << 20)) >> 21 - s9 += carry[8] - s8 -= carry[8] << 21 - carry[10] = (s10 + (1 << 20)) >> 21 - s11 += carry[10] - s10 -= carry[10] << 21 - carry[12] = (s12 + (1 << 20)) >> 21 - s13 += carry[12] - s12 -= carry[12] << 21 - carry[14] = (s14 + (1 << 20)) >> 21 - s15 += carry[14] - s14 -= carry[14] << 21 - carry[16] = (s16 + (1 << 20)) >> 21 - s17 += carry[16] - s16 -= carry[16] << 21 - carry[18] = (s18 + (1 << 20)) >> 21 - s19 += carry[18] - s18 -= carry[18] << 21 - carry[20] = (s20 + (1 << 20)) >> 21 - s21 += carry[20] - s20 -= carry[20] << 21 - carry[22] = (s22 + (1 << 20)) >> 21 - s23 += carry[22] - s22 -= carry[22] << 21 - - carry[1] = (s1 + (1 << 20)) >> 21 - s2 += carry[1] - s1 -= carry[1] << 21 - carry[3] = (s3 + (1 << 20)) >> 21 - s4 += carry[3] - s3 -= carry[3] << 21 - carry[5] = (s5 + (1 << 20)) >> 21 - s6 += carry[5] - s5 -= carry[5] << 21 - carry[7] = (s7 + (1 << 20)) >> 21 - s8 += carry[7] - s7 -= carry[7] << 21 - carry[9] = (s9 + (1 << 20)) >> 21 - s10 += carry[9] - s9 -= carry[9] << 21 - carry[11] = (s11 + (1 << 20)) >> 21 - s12 += carry[11] - s11 -= carry[11] << 21 - carry[13] = (s13 + (1 << 20)) >> 21 - s14 += carry[13] - s13 -= carry[13] << 21 - carry[15] = (s15 + (1 << 20)) >> 21 - s16 += carry[15] - s15 -= carry[15] << 21 - carry[17] = (s17 + (1 << 20)) >> 21 - s18 += carry[17] - s17 -= carry[17] << 21 - carry[19] = (s19 + (1 << 20)) >> 21 - s20 += carry[19] - s19 -= carry[19] << 21 - carry[21] = (s21 + (1 << 20)) >> 21 - s22 += carry[21] - s21 -= carry[21] << 21 - - s11 += s23 * 666643 - s12 += s23 * 470296 - s13 += s23 * 654183 - s14 -= s23 * 997805 - s15 += s23 * 136657 - s16 -= s23 * 683901 - s23 = 0 - - s10 += s22 * 666643 - s11 += s22 * 470296 - s12 += s22 * 654183 - s13 -= s22 * 997805 - s14 += s22 * 136657 - s15 -= s22 * 683901 - s22 = 0 - - s9 += s21 * 666643 - s10 += s21 * 470296 - s11 += s21 * 654183 - s12 -= s21 * 997805 - s13 += s21 * 136657 - s14 -= s21 * 683901 - s21 = 0 - - s8 += s20 * 666643 - s9 += s20 * 470296 - s10 += s20 * 654183 - s11 -= s20 * 997805 - s12 += s20 * 136657 - s13 -= s20 * 683901 - s20 = 0 - - s7 += s19 * 666643 - s8 += s19 * 470296 - s9 += s19 * 654183 - s10 -= s19 * 997805 - s11 += s19 * 136657 - s12 -= s19 * 683901 - s19 = 0 - - s6 += s18 * 666643 - s7 += s18 * 470296 - s8 += s18 * 654183 - s9 -= s18 * 997805 - s10 += s18 * 136657 - s11 -= s18 * 683901 - s18 = 0 - - carry[6] = (s6 + (1 << 20)) >> 21 - s7 += carry[6] - s6 -= carry[6] << 21 - carry[8] = (s8 + (1 << 20)) >> 21 - s9 += carry[8] - s8 -= carry[8] << 21 - carry[10] = (s10 + (1 << 20)) >> 21 - s11 += carry[10] - s10 -= carry[10] << 21 - carry[12] = (s12 + (1 << 20)) >> 21 - s13 += carry[12] - s12 -= carry[12] << 21 - carry[14] = (s14 + (1 << 20)) >> 21 - s15 += carry[14] - s14 -= carry[14] << 21 - carry[16] = (s16 + (1 << 20)) >> 21 - s17 += carry[16] - s16 -= carry[16] << 21 - - carry[7] = (s7 + (1 << 20)) >> 21 - s8 += carry[7] - s7 -= carry[7] << 21 - carry[9] = (s9 + (1 << 20)) >> 21 - s10 += carry[9] - s9 -= carry[9] << 21 - carry[11] = (s11 + (1 << 20)) >> 21 - s12 += carry[11] - s11 -= carry[11] << 21 - carry[13] = (s13 + (1 << 20)) >> 21 - s14 += carry[13] - s13 -= carry[13] << 21 - carry[15] = (s15 + (1 << 20)) >> 21 - s16 += carry[15] - s15 -= carry[15] << 21 - - s5 += s17 * 666643 - s6 += s17 * 470296 - s7 += s17 * 654183 - s8 -= s17 * 997805 - s9 += s17 * 136657 - s10 -= s17 * 683901 - s17 = 0 - - s4 += s16 * 666643 - s5 += s16 * 470296 - s6 += s16 * 654183 - s7 -= s16 * 997805 - s8 += s16 * 136657 - s9 -= s16 * 683901 - s16 = 0 - - s3 += s15 * 666643 - s4 += s15 * 470296 - s5 += s15 * 654183 - s6 -= s15 * 997805 - s7 += s15 * 136657 - s8 -= s15 * 683901 - s15 = 0 - - s2 += s14 * 666643 - s3 += s14 * 470296 - s4 += s14 * 654183 - s5 -= s14 * 997805 - s6 += s14 * 136657 - s7 -= s14 * 683901 - s14 = 0 - - s1 += s13 * 666643 - s2 += s13 * 470296 - s3 += s13 * 654183 - s4 -= s13 * 997805 - s5 += s13 * 136657 - s6 -= s13 * 683901 - s13 = 0 - - s0 += s12 * 666643 - s1 += s12 * 470296 - s2 += s12 * 654183 - s3 -= s12 * 997805 - s4 += s12 * 136657 - s5 -= s12 * 683901 - s12 = 0 - - carry[0] = (s0 + (1 << 20)) >> 21 - s1 += carry[0] - s0 -= carry[0] << 21 - carry[2] = (s2 + (1 << 20)) >> 21 - s3 += carry[2] - s2 -= carry[2] << 21 - carry[4] = (s4 + (1 << 20)) >> 21 - s5 += carry[4] - s4 -= carry[4] << 21 - carry[6] = (s6 + (1 << 20)) >> 21 - s7 += carry[6] - s6 -= carry[6] << 21 - carry[8] = (s8 + (1 << 20)) >> 21 - s9 += carry[8] - s8 -= carry[8] << 21 - carry[10] = (s10 + (1 << 20)) >> 21 - s11 += carry[10] - s10 -= carry[10] << 21 - - carry[1] = (s1 + (1 << 20)) >> 21 - s2 += carry[1] - s1 -= carry[1] << 21 - carry[3] = (s3 + (1 << 20)) >> 21 - s4 += carry[3] - s3 -= carry[3] << 21 - carry[5] = (s5 + (1 << 20)) >> 21 - s6 += carry[5] - s5 -= carry[5] << 21 - carry[7] = (s7 + (1 << 20)) >> 21 - s8 += carry[7] - s7 -= carry[7] << 21 - carry[9] = (s9 + (1 << 20)) >> 21 - s10 += carry[9] - s9 -= carry[9] << 21 - carry[11] = (s11 + (1 << 20)) >> 21 - s12 += carry[11] - s11 -= carry[11] << 21 - - s0 += s12 * 666643 - s1 += s12 * 470296 - s2 += s12 * 654183 - s3 -= s12 * 997805 - s4 += s12 * 136657 - s5 -= s12 * 683901 - s12 = 0 - - carry[0] = s0 >> 21 - s1 += carry[0] - s0 -= carry[0] << 21 - carry[1] = s1 >> 21 - s2 += carry[1] - s1 -= carry[1] << 21 - carry[2] = s2 >> 21 - s3 += carry[2] - s2 -= carry[2] << 21 - carry[3] = s3 >> 21 - s4 += carry[3] - s3 -= carry[3] << 21 - carry[4] = s4 >> 21 - s5 += carry[4] - s4 -= carry[4] << 21 - carry[5] = s5 >> 21 - s6 += carry[5] - s5 -= carry[5] << 21 - carry[6] = s6 >> 21 - s7 += carry[6] - s6 -= carry[6] << 21 - carry[7] = s7 >> 21 - s8 += carry[7] - s7 -= carry[7] << 21 - carry[8] = s8 >> 21 - s9 += carry[8] - s8 -= carry[8] << 21 - carry[9] = s9 >> 21 - s10 += carry[9] - s9 -= carry[9] << 21 - carry[10] = s10 >> 21 - s11 += carry[10] - s10 -= carry[10] << 21 - carry[11] = s11 >> 21 - s12 += carry[11] - s11 -= carry[11] << 21 - - s0 += s12 * 666643 - s1 += s12 * 470296 - s2 += s12 * 654183 - s3 -= s12 * 997805 - s4 += s12 * 136657 - s5 -= s12 * 683901 - s12 = 0 - - carry[0] = s0 >> 21 - s1 += carry[0] - s0 -= carry[0] << 21 - carry[1] = s1 >> 21 - s2 += carry[1] - s1 -= carry[1] << 21 - carry[2] = s2 >> 21 - s3 += carry[2] - s2 -= carry[2] << 21 - carry[3] = s3 >> 21 - s4 += carry[3] - s3 -= carry[3] << 21 - carry[4] = s4 >> 21 - s5 += carry[4] - s4 -= carry[4] << 21 - carry[5] = s5 >> 21 - s6 += carry[5] - s5 -= carry[5] << 21 - carry[6] = s6 >> 21 - s7 += carry[6] - s6 -= carry[6] << 21 - carry[7] = s7 >> 21 - s8 += carry[7] - s7 -= carry[7] << 21 - carry[8] = s8 >> 21 - s9 += carry[8] - s8 -= carry[8] << 21 - carry[9] = s9 >> 21 - s10 += carry[9] - s9 -= carry[9] << 21 - carry[10] = s10 >> 21 - s11 += carry[10] - s10 -= carry[10] << 21 - - s[0] = byte(s0 >> 0) - s[1] = byte(s0 >> 8) - s[2] = byte((s0 >> 16) | (s1 << 5)) - s[3] = byte(s1 >> 3) - s[4] = byte(s1 >> 11) - s[5] = byte((s1 >> 19) | (s2 << 2)) - s[6] = byte(s2 >> 6) - s[7] = byte((s2 >> 14) | (s3 << 7)) - s[8] = byte(s3 >> 1) - s[9] = byte(s3 >> 9) - s[10] = byte((s3 >> 17) | (s4 << 4)) - s[11] = byte(s4 >> 4) - s[12] = byte(s4 >> 12) - s[13] = byte((s4 >> 20) | (s5 << 1)) - s[14] = byte(s5 >> 7) - s[15] = byte((s5 >> 15) | (s6 << 6)) - s[16] = byte(s6 >> 2) - s[17] = byte(s6 >> 10) - s[18] = byte((s6 >> 18) | (s7 << 3)) - s[19] = byte(s7 >> 5) - s[20] = byte(s7 >> 13) - s[21] = byte(s8 >> 0) - s[22] = byte(s8 >> 8) - s[23] = byte((s8 >> 16) | (s9 << 5)) - s[24] = byte(s9 >> 3) - s[25] = byte(s9 >> 11) - s[26] = byte((s9 >> 19) | (s10 << 2)) - s[27] = byte(s10 >> 6) - s[28] = byte((s10 >> 14) | (s11 << 7)) - s[29] = byte(s11 >> 1) - s[30] = byte(s11 >> 9) - s[31] = byte(s11 >> 17) -} - -// Input: -// s[0]+256*s[1]+...+256^63*s[63] = s -// -// Output: -// s[0]+256*s[1]+...+256^31*s[31] = s mod l -// where l = 2^252 + 27742317777372353535851937790883648493. -func ScReduce(out *[32]byte, s *[64]byte) { - s0 := 2097151 & load3(s[:]) - s1 := 2097151 & (load4(s[2:]) >> 5) - s2 := 2097151 & (load3(s[5:]) >> 2) - s3 := 2097151 & (load4(s[7:]) >> 7) - s4 := 2097151 & (load4(s[10:]) >> 4) - s5 := 2097151 & (load3(s[13:]) >> 1) - s6 := 2097151 & (load4(s[15:]) >> 6) - s7 := 2097151 & (load3(s[18:]) >> 3) - s8 := 2097151 & load3(s[21:]) - s9 := 2097151 & (load4(s[23:]) >> 5) - s10 := 2097151 & (load3(s[26:]) >> 2) - s11 := 2097151 & (load4(s[28:]) >> 7) - s12 := 2097151 & (load4(s[31:]) >> 4) - s13 := 2097151 & (load3(s[34:]) >> 1) - s14 := 2097151 & (load4(s[36:]) >> 6) - s15 := 2097151 & (load3(s[39:]) >> 3) - s16 := 2097151 & load3(s[42:]) - s17 := 2097151 & (load4(s[44:]) >> 5) - s18 := 2097151 & (load3(s[47:]) >> 2) - s19 := 2097151 & (load4(s[49:]) >> 7) - s20 := 2097151 & (load4(s[52:]) >> 4) - s21 := 2097151 & (load3(s[55:]) >> 1) - s22 := 2097151 & (load4(s[57:]) >> 6) - s23 := (load4(s[60:]) >> 3) - - s11 += s23 * 666643 - s12 += s23 * 470296 - s13 += s23 * 654183 - s14 -= s23 * 997805 - s15 += s23 * 136657 - s16 -= s23 * 683901 - s23 = 0 - - s10 += s22 * 666643 - s11 += s22 * 470296 - s12 += s22 * 654183 - s13 -= s22 * 997805 - s14 += s22 * 136657 - s15 -= s22 * 683901 - s22 = 0 - - s9 += s21 * 666643 - s10 += s21 * 470296 - s11 += s21 * 654183 - s12 -= s21 * 997805 - s13 += s21 * 136657 - s14 -= s21 * 683901 - s21 = 0 - - s8 += s20 * 666643 - s9 += s20 * 470296 - s10 += s20 * 654183 - s11 -= s20 * 997805 - s12 += s20 * 136657 - s13 -= s20 * 683901 - s20 = 0 - - s7 += s19 * 666643 - s8 += s19 * 470296 - s9 += s19 * 654183 - s10 -= s19 * 997805 - s11 += s19 * 136657 - s12 -= s19 * 683901 - s19 = 0 - - s6 += s18 * 666643 - s7 += s18 * 470296 - s8 += s18 * 654183 - s9 -= s18 * 997805 - s10 += s18 * 136657 - s11 -= s18 * 683901 - s18 = 0 - - var carry [17]int64 - - carry[6] = (s6 + (1 << 20)) >> 21 - s7 += carry[6] - s6 -= carry[6] << 21 - carry[8] = (s8 + (1 << 20)) >> 21 - s9 += carry[8] - s8 -= carry[8] << 21 - carry[10] = (s10 + (1 << 20)) >> 21 - s11 += carry[10] - s10 -= carry[10] << 21 - carry[12] = (s12 + (1 << 20)) >> 21 - s13 += carry[12] - s12 -= carry[12] << 21 - carry[14] = (s14 + (1 << 20)) >> 21 - s15 += carry[14] - s14 -= carry[14] << 21 - carry[16] = (s16 + (1 << 20)) >> 21 - s17 += carry[16] - s16 -= carry[16] << 21 - - carry[7] = (s7 + (1 << 20)) >> 21 - s8 += carry[7] - s7 -= carry[7] << 21 - carry[9] = (s9 + (1 << 20)) >> 21 - s10 += carry[9] - s9 -= carry[9] << 21 - carry[11] = (s11 + (1 << 20)) >> 21 - s12 += carry[11] - s11 -= carry[11] << 21 - carry[13] = (s13 + (1 << 20)) >> 21 - s14 += carry[13] - s13 -= carry[13] << 21 - carry[15] = (s15 + (1 << 20)) >> 21 - s16 += carry[15] - s15 -= carry[15] << 21 - - s5 += s17 * 666643 - s6 += s17 * 470296 - s7 += s17 * 654183 - s8 -= s17 * 997805 - s9 += s17 * 136657 - s10 -= s17 * 683901 - s17 = 0 - - s4 += s16 * 666643 - s5 += s16 * 470296 - s6 += s16 * 654183 - s7 -= s16 * 997805 - s8 += s16 * 136657 - s9 -= s16 * 683901 - s16 = 0 - - s3 += s15 * 666643 - s4 += s15 * 470296 - s5 += s15 * 654183 - s6 -= s15 * 997805 - s7 += s15 * 136657 - s8 -= s15 * 683901 - s15 = 0 - - s2 += s14 * 666643 - s3 += s14 * 470296 - s4 += s14 * 654183 - s5 -= s14 * 997805 - s6 += s14 * 136657 - s7 -= s14 * 683901 - s14 = 0 - - s1 += s13 * 666643 - s2 += s13 * 470296 - s3 += s13 * 654183 - s4 -= s13 * 997805 - s5 += s13 * 136657 - s6 -= s13 * 683901 - s13 = 0 - - s0 += s12 * 666643 - s1 += s12 * 470296 - s2 += s12 * 654183 - s3 -= s12 * 997805 - s4 += s12 * 136657 - s5 -= s12 * 683901 - s12 = 0 - - carry[0] = (s0 + (1 << 20)) >> 21 - s1 += carry[0] - s0 -= carry[0] << 21 - carry[2] = (s2 + (1 << 20)) >> 21 - s3 += carry[2] - s2 -= carry[2] << 21 - carry[4] = (s4 + (1 << 20)) >> 21 - s5 += carry[4] - s4 -= carry[4] << 21 - carry[6] = (s6 + (1 << 20)) >> 21 - s7 += carry[6] - s6 -= carry[6] << 21 - carry[8] = (s8 + (1 << 20)) >> 21 - s9 += carry[8] - s8 -= carry[8] << 21 - carry[10] = (s10 + (1 << 20)) >> 21 - s11 += carry[10] - s10 -= carry[10] << 21 - - carry[1] = (s1 + (1 << 20)) >> 21 - s2 += carry[1] - s1 -= carry[1] << 21 - carry[3] = (s3 + (1 << 20)) >> 21 - s4 += carry[3] - s3 -= carry[3] << 21 - carry[5] = (s5 + (1 << 20)) >> 21 - s6 += carry[5] - s5 -= carry[5] << 21 - carry[7] = (s7 + (1 << 20)) >> 21 - s8 += carry[7] - s7 -= carry[7] << 21 - carry[9] = (s9 + (1 << 20)) >> 21 - s10 += carry[9] - s9 -= carry[9] << 21 - carry[11] = (s11 + (1 << 20)) >> 21 - s12 += carry[11] - s11 -= carry[11] << 21 - - s0 += s12 * 666643 - s1 += s12 * 470296 - s2 += s12 * 654183 - s3 -= s12 * 997805 - s4 += s12 * 136657 - s5 -= s12 * 683901 - s12 = 0 - - carry[0] = s0 >> 21 - s1 += carry[0] - s0 -= carry[0] << 21 - carry[1] = s1 >> 21 - s2 += carry[1] - s1 -= carry[1] << 21 - carry[2] = s2 >> 21 - s3 += carry[2] - s2 -= carry[2] << 21 - carry[3] = s3 >> 21 - s4 += carry[3] - s3 -= carry[3] << 21 - carry[4] = s4 >> 21 - s5 += carry[4] - s4 -= carry[4] << 21 - carry[5] = s5 >> 21 - s6 += carry[5] - s5 -= carry[5] << 21 - carry[6] = s6 >> 21 - s7 += carry[6] - s6 -= carry[6] << 21 - carry[7] = s7 >> 21 - s8 += carry[7] - s7 -= carry[7] << 21 - carry[8] = s8 >> 21 - s9 += carry[8] - s8 -= carry[8] << 21 - carry[9] = s9 >> 21 - s10 += carry[9] - s9 -= carry[9] << 21 - carry[10] = s10 >> 21 - s11 += carry[10] - s10 -= carry[10] << 21 - carry[11] = s11 >> 21 - s12 += carry[11] - s11 -= carry[11] << 21 - - s0 += s12 * 666643 - s1 += s12 * 470296 - s2 += s12 * 654183 - s3 -= s12 * 997805 - s4 += s12 * 136657 - s5 -= s12 * 683901 - s12 = 0 - - carry[0] = s0 >> 21 - s1 += carry[0] - s0 -= carry[0] << 21 - carry[1] = s1 >> 21 - s2 += carry[1] - s1 -= carry[1] << 21 - carry[2] = s2 >> 21 - s3 += carry[2] - s2 -= carry[2] << 21 - carry[3] = s3 >> 21 - s4 += carry[3] - s3 -= carry[3] << 21 - carry[4] = s4 >> 21 - s5 += carry[4] - s4 -= carry[4] << 21 - carry[5] = s5 >> 21 - s6 += carry[5] - s5 -= carry[5] << 21 - carry[6] = s6 >> 21 - s7 += carry[6] - s6 -= carry[6] << 21 - carry[7] = s7 >> 21 - s8 += carry[7] - s7 -= carry[7] << 21 - carry[8] = s8 >> 21 - s9 += carry[8] - s8 -= carry[8] << 21 - carry[9] = s9 >> 21 - s10 += carry[9] - s9 -= carry[9] << 21 - carry[10] = s10 >> 21 - s11 += carry[10] - s10 -= carry[10] << 21 - - out[0] = byte(s0 >> 0) - out[1] = byte(s0 >> 8) - out[2] = byte((s0 >> 16) | (s1 << 5)) - out[3] = byte(s1 >> 3) - out[4] = byte(s1 >> 11) - out[5] = byte((s1 >> 19) | (s2 << 2)) - out[6] = byte(s2 >> 6) - out[7] = byte((s2 >> 14) | (s3 << 7)) - out[8] = byte(s3 >> 1) - out[9] = byte(s3 >> 9) - out[10] = byte((s3 >> 17) | (s4 << 4)) - out[11] = byte(s4 >> 4) - out[12] = byte(s4 >> 12) - out[13] = byte((s4 >> 20) | (s5 << 1)) - out[14] = byte(s5 >> 7) - out[15] = byte((s5 >> 15) | (s6 << 6)) - out[16] = byte(s6 >> 2) - out[17] = byte(s6 >> 10) - out[18] = byte((s6 >> 18) | (s7 << 3)) - out[19] = byte(s7 >> 5) - out[20] = byte(s7 >> 13) - out[21] = byte(s8 >> 0) - out[22] = byte(s8 >> 8) - out[23] = byte((s8 >> 16) | (s9 << 5)) - out[24] = byte(s9 >> 3) - out[25] = byte(s9 >> 11) - out[26] = byte((s9 >> 19) | (s10 << 2)) - out[27] = byte(s10 >> 6) - out[28] = byte((s10 >> 14) | (s11 << 7)) - out[29] = byte(s11 >> 1) - out[30] = byte(s11 >> 9) - out[31] = byte(s11 >> 17) -} - -// order is the order of Curve25519 in little-endian form. -var order = [4]uint64{0x5812631a5cf5d3ed, 0x14def9dea2f79cd6, 0, 0x1000000000000000} - -// ScMinimal returns true if the given scalar is less than the order of the -// curve. -func ScMinimal(scalar *[32]byte) bool { - for i := 3; ; i-- { - v := binary.LittleEndian.Uint64(scalar[i*8:]) - if v > order[i] { - return false - } else if v < order[i] { - break - } else if i == 0 { - return false - } - } - - return true -} diff --git a/vendor/golang.org/x/crypto/ocsp/ocsp.go b/vendor/golang.org/x/crypto/ocsp/ocsp.go index 9d3fffa8..96972aea 100644 --- a/vendor/golang.org/x/crypto/ocsp/ocsp.go +++ b/vendor/golang.org/x/crypto/ocsp/ocsp.go @@ -668,7 +668,7 @@ func CreateRequest(cert, issuer *x509.Certificate, opts *RequestOptions) ([]byte // The responder cert is used to populate the responder's name field, and the // certificate itself is provided alongside the OCSP response signature. // -// The issuer cert is used to puplate the IssuerNameHash and IssuerKeyHash fields. +// The issuer cert is used to populate the IssuerNameHash and IssuerKeyHash fields. // // The template is used to populate the SerialNumber, Status, RevokedAt, // RevocationReason, ThisUpdate, and NextUpdate fields. diff --git a/vendor/modules.txt b/vendor/modules.txt index e359af8c..db14a818 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -34,7 +34,7 @@ github.com/inconshreveable/go-update/internal/osext github.com/inconshreveable/mousetrap # github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 github.com/kballard/go-shellquote -# github.com/klauspost/compress v1.14.4 +# github.com/klauspost/compress v1.15.5 github.com/klauspost/compress/s2 # github.com/mattn/go-colorable v0.1.4 github.com/mattn/go-colorable @@ -52,16 +52,16 @@ github.com/mitchellh/go-wordwrap # github.com/nats-io/cliprompts/v2 v2.0.0-20191226174129-372d79b36768 ## explicit github.com/nats-io/cliprompts/v2 -# github.com/nats-io/jsm.go v0.0.30 +# github.com/nats-io/jsm.go v0.0.33 ## explicit github.com/nats-io/jsm.go/natscontext # github.com/nats-io/jwt v1.2.2 ## explicit github.com/nats-io/jwt -# github.com/nats-io/jwt/v2 v2.2.1-0.20220509180118-3bcd719cc7d0 +# github.com/nats-io/jwt/v2 v2.3.0 ## explicit github.com/nats-io/jwt/v2 -# github.com/nats-io/nats-server/v2 v2.7.5-0.20220309212130-5c0d1999ff72 +# github.com/nats-io/nats-server/v2 v2.8.4 ## explicit github.com/nats-io/nats-server/v2/conf github.com/nats-io/nats-server/v2/internal/ldap @@ -69,7 +69,7 @@ github.com/nats-io/nats-server/v2/logger github.com/nats-io/nats-server/v2/server github.com/nats-io/nats-server/v2/server/pse github.com/nats-io/nats-server/v2/server/sysmem -# github.com/nats-io/nats.go v1.13.1-0.20220308171302-2f2f6968e98d +# github.com/nats-io/nats.go v1.16.0 ## explicit github.com/nats-io/nats.go github.com/nats-io/nats.go/encoders/builtin @@ -111,14 +111,13 @@ github.com/ulikunitz/xz/lzma # github.com/xlab/tablewriter v0.0.0-20160610135559-80b567a11ad5 ## explicit github.com/xlab/tablewriter -# golang.org/x/crypto v0.0.0-20220112180741-5e0467b6c7ce +# golang.org/x/crypto v0.0.0-20220315160706-3147a52a75dd golang.org/x/crypto/bcrypt golang.org/x/crypto/blowfish golang.org/x/crypto/cast5 golang.org/x/crypto/chacha20 golang.org/x/crypto/chacha20poly1305 golang.org/x/crypto/ed25519 -golang.org/x/crypto/ed25519/internal/edwards25519 golang.org/x/crypto/internal/poly1305 golang.org/x/crypto/internal/subtle golang.org/x/crypto/ocsp