| 1 |
// Copyright 2026 The NATS Authors |
| 2 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 |
// you may not use this file except in compliance with the License. |
| 4 |
// You may obtain a copy of the License at |
| 5 |
// |
| 6 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 |
// |
| 8 |
// Unless required by applicable law or agreed to in writing, software |
| 9 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 |
// See the License for the specific language governing permissions and |
| 12 |
// limitations under the License. |
| 13 |
|
| 14 |
package server |
| 15 |
|
| 16 |
import ( |
| 17 |
"maps" |
| 18 |
"slices" |
| 19 |
"strings" |
| 20 |
) |
| 21 |
|
| 22 |
const ( |
| 23 |
FeatureFlagJsAckFormatV2 = "js_ack_fc_v2" |
| 24 |
FeatureFlagJsRaftDeleteRange = "js_raft_delete_range" |
| 25 |
) |
| 26 |
|
| 27 |
var featureFlags = map[string]bool{ |
| 28 |
// Use v2 format for `$JS.ACK.>` and `$JS.FC.>`. |
| 29 |
// - Introduced: 2.14.0, both v1 and v2 supported, only using v1. |
| 30 |
// - Enabled: TBD, both supported, v2 becomes the default. |
| 31 |
// |
| 32 |
// - v1: $JS.ACK.<stream name>.<consumer name>.<num delivered>.<stream sequence>.<consumer sequence>.<timestamp>.<num pending> |
| 33 |
// - v2: $JS.ACK.<domain>.<account hash>.<stream name>.<consumer name>.<num delivered>.<stream sequence>.<consumer sequence>.<timestamp>.<num pending> |
| 34 |
// See also: https://github.com/nats-io/nats-architecture-and-design/blob/main/adr/ADR-15.md#jsack |
| 35 |
FeatureFlagJsAckFormatV2: false, |
| 36 |
|
| 37 |
// Propose delete range gaps as a single `deleteRangeOp` Raft append entry |
| 38 |
// instead of one entry per deleted sequence. Dramatically reduces Raft cost |
| 39 |
// on mirrors whose origin has a large number of interior deletes. |
| 40 |
// - Introduced: 2.14.0, apply-side always supports receiving `deleteRangeOp`. |
| 41 |
// - Enabled: TBD, once all supported versions carry the apply-side. |
| 42 |
// |
| 43 |
// WARNING: Only enable once every peer in the cluster is on a version that |
| 44 |
// supports receiving `deleteRangeOp`. Older peers panic on apply of an |
| 45 |
// unknown stream entry operation. |
| 46 |
FeatureFlagJsRaftDeleteRange: false, |
| 47 |
} |
| 48 |
|
| 49 |
// getFeatureFlag is used to retrieve either the default or overwritten value for a feature flag. |
| 50 |
// The user's value takes precedence over the system's default. However, if the flag doesn't exist, it's disabled. |
| 51 |
// The *Options returned by Server.getOpts() is treated as immutable, mutations go through setOpts, |
| 52 |
// so no lock is required on the map read here. |
| 53 |
func (o *Options) getFeatureFlag(k string) bool { |
| 54 |
defaultValue, ok := featureFlags[k] |
| 55 |
if !ok { |
| 56 |
return false // Not supported. |
| 57 |
} |
| 58 |
if userValue, ok := o.FeatureFlags[k]; ok { |
| 59 |
return userValue |
| 60 |
} |
| 61 |
return defaultValue |
| 62 |
} |
| 63 |
|
| 64 |
// getMergedFeatureFlags returns a merged map of feature flags, with the user's values taking precedence. |
| 65 |
func (o *Options) getMergedFeatureFlags() map[string]bool { |
| 66 |
merged := make(map[string]bool) |
| 67 |
for k, v := range featureFlags { |
| 68 |
merged[k] = v |
| 69 |
} |
| 70 |
for k, v := range o.FeatureFlags { |
| 71 |
if _, ok := featureFlags[k]; !ok { |
| 72 |
continue |
| 73 |
} |
| 74 |
merged[k] = v |
| 75 |
} |
| 76 |
return merged |
| 77 |
} |
| 78 |
|
| 79 |
// printFeatureFlags logs the currently used feature flags on server startup. |
| 80 |
func (s *Server) printFeatureFlags(o *Options) { |
| 81 |
if len(o.FeatureFlags) == 0 { |
| 82 |
return |
| 83 |
} |
| 84 |
keys := slices.Sorted(maps.Keys(o.FeatureFlags)) |
| 85 |
|
| 86 |
var ( |
| 87 |
configured strings.Builder |
| 88 |
unsupported strings.Builder |
| 89 |
) |
| 90 |
|
| 91 |
for _, k := range keys { |
| 92 |
// Unsupported |
| 93 |
defaultValue, ok := featureFlags[k] |
| 94 |
if !ok { |
| 95 |
if unsupported.Len() > 0 { |
| 96 |
unsupported.WriteString(", ") |
| 97 |
} |
| 98 |
unsupported.WriteString(k) |
| 99 |
continue |
| 100 |
} |
| 101 |
|
| 102 |
v := o.FeatureFlags[k] |
| 103 |
if configured.Len() > 0 { |
| 104 |
configured.WriteString(", ") |
| 105 |
} |
| 106 |
configured.WriteString(k) |
| 107 |
configured.WriteString(" (") |
| 108 |
if defaultValue { |
| 109 |
if v { |
| 110 |
configured.WriteString("enabled") |
| 111 |
} else { |
| 112 |
configured.WriteString("opt-out") |
| 113 |
} |
| 114 |
} else if v { |
| 115 |
configured.WriteString("opt-in") |
| 116 |
} else { |
| 117 |
configured.WriteString("disabled") |
| 118 |
} |
| 119 |
configured.WriteString(")") |
| 120 |
} |
| 121 |
if configured.Len() == 0 { |
| 122 |
configured.WriteString("none") |
| 123 |
} |
| 124 |
|
| 125 |
s.Noticef(" Feature flags:") |
| 126 |
s.Noticef(" Configured: %s", configured.String()) |
| 127 |
if unsupported.Len() > 0 { |
| 128 |
s.Noticef(" Unsupported: %s", unsupported.String()) |
| 129 |
} |
| 130 |
} |