Skip to content

Commit e5e2d14

Browse files
authored
Cherry picks for v2.10.19 RC6 (#5829)
Includes: - #5825 - #5826 - #5821 - #5831
2 parents 7ad3b6f + 0b5e1ec commit e5e2d14

19 files changed

Lines changed: 545 additions & 44 deletions

internal/ldap/dn_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
// Copyright (c) 2011-2015 Michael Mitton (mmitton@gmail.com)
22
// Portions copyright (c) 2015-2016 go-ldap Authors
3+
// Static-Check Fixes Copyright 2024 The NATS Authors
4+
35
package ldap
46

57
import (
@@ -53,7 +55,7 @@ func TestSuccessfulDNParsing(t *testing.T) {
5355
for test, answer := range testcases {
5456
dn, err := ParseDN(test)
5557
if err != nil {
56-
t.Errorf(err.Error())
58+
t.Error(err.Error())
5759
continue
5860
}
5961
if !reflect.DeepEqual(dn, &answer) {

server/certidp/certidp.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 The NATS Authors
1+
// Copyright 2023-2024 The NATS Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at
@@ -222,7 +222,7 @@ func CertOCSPEligible(link *ChainLink) bool {
222222
if link == nil || link.Leaf.Raw == nil || len(link.Leaf.Raw) == 0 {
223223
return false
224224
}
225-
if link.Leaf.OCSPServer == nil || len(link.Leaf.OCSPServer) == 0 {
225+
if len(link.Leaf.OCSPServer) == 0 {
226226
return false
227227
}
228228
urls := getWebEndpoints(link.Leaf.OCSPServer)

server/certidp/ocsp_responder.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2023 The NATS Authors
1+
// Copyright 2023-2024 The NATS Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at
@@ -15,6 +15,7 @@ package certidp
1515

1616
import (
1717
"encoding/base64"
18+
"errors"
1819
"fmt"
1920
"io"
2021
"net/http"
@@ -26,7 +27,7 @@ import (
2627

2728
func FetchOCSPResponse(link *ChainLink, opts *OCSPPeerConfig, log *Log) ([]byte, error) {
2829
if link == nil || link.Leaf == nil || link.Issuer == nil || opts == nil || log == nil {
29-
return nil, fmt.Errorf(ErrInvalidChainlink)
30+
return nil, errors.New(ErrInvalidChainlink)
3031
}
3132

3233
timeout := time.Duration(opts.Timeout * float64(time.Second))
@@ -59,7 +60,7 @@ func FetchOCSPResponse(link *ChainLink, opts *OCSPPeerConfig, log *Log) ([]byte,
5960
responders := *link.OCSPWebEndpoints
6061

6162
if len(responders) == 0 {
62-
return nil, fmt.Errorf(ErrNoAvailOCSPServers)
63+
return nil, errors.New(ErrNoAvailOCSPServers)
6364
}
6465

6566
var raw []byte

server/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2972,7 +2972,7 @@ func (c *client) addShadowSub(sub *subscription, ime *ime, enact bool) (*subscri
29722972
if err := im.acc.sl.Insert(&nsub); err != nil {
29732973
errs := fmt.Sprintf("Could not add shadow import subscription for account %q", im.acc.Name)
29742974
c.Debugf(errs)
2975-
return nil, fmt.Errorf(errs)
2975+
return nil, errors.New(errs)
29762976
}
29772977

29782978
// Update our route map here. But only if we are not a leaf node or a hub leafnode.

server/filestore.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ type msgBlock struct {
239239
noTrack bool
240240
needSync bool
241241
syncAlways bool
242+
noCompact bool
242243
closed bool
243244

244245
// Used to mock write failures.
@@ -3959,6 +3960,9 @@ func (fs *fileStore) removeMsg(seq uint64, secure, viaLimits, needFSLock bool) (
39593960
mb.bytes = 0
39603961
}
39613962

3963+
// Allow us to check compaction again.
3964+
mb.noCompact = false
3965+
39623966
// Mark as dirty for stream state.
39633967
fs.dirty++
39643968

@@ -4075,7 +4079,7 @@ func (mb *msgBlock) shouldCompactInline() bool {
40754079
// Ignores 2MB minimum.
40764080
// Lock should be held.
40774081
func (mb *msgBlock) shouldCompactSync() bool {
4078-
return mb.bytes*2 < mb.rbytes
4082+
return mb.bytes*2 < mb.rbytes && !mb.noCompact
40794083
}
40804084

40814085
// This will compact and rewrite this block. This should only be called when we know we want to rewrite this block.
@@ -4184,7 +4188,12 @@ func (mb *msgBlock) compact() {
41844188
mb.needSync = true
41854189

41864190
// Capture the updated rbytes.
4187-
mb.rbytes = uint64(len(nbuf))
4191+
if rbytes := uint64(len(nbuf)); rbytes == mb.rbytes {
4192+
// No change, so set our noCompact bool here to avoid attempting to continually compress in syncBlocks.
4193+
mb.noCompact = true
4194+
} else {
4195+
mb.rbytes = rbytes
4196+
}
41884197

41894198
// Remove any seqs from the beginning of the blk.
41904199
for seq, nfseq := fseq, atomic.LoadUint64(&mb.first.seq); seq < nfseq; seq++ {

server/filestore_test.go

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7379,6 +7379,83 @@ func TestFileStoreCheckSkipFirstBlockNotLoadOldBlocks(t *testing.T) {
73797379
require_Equal(t, loaded, 1)
73807380
}
73817381

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

server/gateway_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2018-2020 The NATS Authors
1+
// Copyright 2018-2024 The NATS Authors
22
// Licensed under the Apache License, Version 2.0 (the "License");
33
// you may not use this file except in compliance with the License.
44
// You may obtain a copy of the License at
@@ -211,7 +211,7 @@ func waitCh(t *testing.T, ch chan bool, errTxt string) {
211211
case <-ch:
212212
return
213213
case <-time.After(5 * time.Second):
214-
t.Fatalf(errTxt)
214+
t.Fatal(errTxt)
215215
}
216216
}
217217

@@ -5055,7 +5055,7 @@ func TestGatewayMapReplyOnlyForRecentSub(t *testing.T) {
50555055
select {
50565056
case e := <-errCh:
50575057
if e != nil {
5058-
t.Fatalf(e.Error())
5058+
t.Fatal(e.Error())
50595059
}
50605060
case <-time.After(time.Second):
50615061
t.Fatalf("Did not get replies")

server/jetstream_cluster_3_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5857,15 +5857,15 @@ func TestJetStreamClusterRestartThenScaleStreamReplicas(t *testing.T) {
58575857
select {
58585858
case dl := <-loggers[0].dbgCh:
58595859
if strings.Contains(dl, condition) {
5860-
errCh <- fmt.Errorf(condition)
5860+
errCh <- errors.New(condition)
58615861
}
58625862
case dl := <-loggers[1].dbgCh:
58635863
if strings.Contains(dl, condition) {
5864-
errCh <- fmt.Errorf(condition)
5864+
errCh <- errors.New(condition)
58655865
}
58665866
case dl := <-loggers[2].dbgCh:
58675867
if strings.Contains(dl, condition) {
5868-
errCh <- fmt.Errorf(condition)
5868+
errCh <- errors.New(condition)
58695869
}
58705870
case <-ctx.Done():
58715871
return

0 commit comments

Comments
 (0)