Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions scripts/runTestsOnTravis.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

set -ex

export GOEXPERIMENT=jsonv2

if [ "$1" = "compile" ]; then
# First check that NATS builds.
go build -v;
Expand Down
2 changes: 1 addition & 1 deletion server/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ type ClientInfo struct {
Name string `json:"name,omitempty"`
Lang string `json:"lang,omitempty"`
Version string `json:"ver,omitempty"`
RTT time.Duration `json:"rtt,omitempty"`
RTT time.Duration `json:"rtt,omitempty,format:units"`
Server string `json:"server,omitempty"`
Cluster string `json:"cluster,omitempty"`
Alternates []string `json:"alts,omitempty"`
Expand Down
6 changes: 5 additions & 1 deletion server/jetstream_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"strings"
"sync/atomic"
"time"
"weak"

"github.com/klauspost/compress/s2"
"github.com/minio/highwayhash"
Expand Down Expand Up @@ -69,6 +70,9 @@ type jetStreamCluster struct {
peerStreamCancelMove *subscription
// To pop out the monitorCluster before the raft layer.
qch chan struct{}
// Meta snapshot encode buffers. Weakly held so GC can collect at any time, but
// can help us optimistically to reduce allocations on future snapshot calls.
lastsnap weak.Pointer[bytes.Buffer] // nolint:unused
}

// Used to track inflight stream add requests to properly re-use same group and sync subject.
Expand Down Expand Up @@ -1584,7 +1588,7 @@ func (js *jetStream) metaSnapshot() ([]byte, error) {

// Track how long it took to marshal the JSON
mstart := time.Now()
b, err := json.Marshal(streams)
b, err := js.metaSnapshotJSON(streams)
mend := time.Since(mstart)

js.mu.RUnlock()
Expand Down
24 changes: 24 additions & 0 deletions server/jetstream_cluster_snap_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright 2025 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//go:build !goexperiment.jsonv2

package server

import (
"encoding/json"
)

func (js *jetStream) metaSnapshotJSON(streams []writeableStreamAssignment) ([]byte, error) {
return json.Marshal(streams)
}
35 changes: 35 additions & 0 deletions server/jetstream_cluster_snap_v2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright 2025 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
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
//go:build goexperiment.jsonv2

package server

import (
"bytes"
jsonv2 "encoding/json/v2"
"weak"
)

func (js *jetStream) metaSnapshotJSON(streams []writeableStreamAssignment) ([]byte, error) {
b := js.cluster.lastsnap.Value()
if b == nil {
b = bytes.NewBuffer(nil)
js.cluster.lastsnap = weak.Make(b)
}
b.Reset()
if err := jsonv2.MarshalWrite(b, streams); err != nil {
return nil, err
}
return b.Bytes(), nil
}