1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const std = @import("std");
pub const jetstream_prefix = "JETSTREAM_";
pub const Mapping = struct {
env: []const u8,
flag: []const u8,
serve: bool = true,
};
// This is the exact flag-source set declared by the pinned Jetstream V2
// command tree. Root-only values have no synthesized serve flag.
pub const mappings = [_]Mapping{
.{ .env = "JETSTREAM_LOG_LEVEL", .flag = "log-level", .serve = false },
.{ .env = "JETSTREAM_LOG_FORMAT", .flag = "log-format", .serve = false },
.{ .env = "JETSTREAM_ADDR", .flag = "addr" },
.{ .env = "JETSTREAM_DEBUG_ADDR", .flag = "debug-addr" },
.{ .env = "JETSTREAM_SHUTDOWN_TIMEOUT", .flag = "shutdown-timeout" },
.{ .env = "JETSTREAM_CLIENT_DRAIN_TIMEOUT", .flag = "client-drain-timeout" },
.{ .env = "JETSTREAM_RELAY_URL", .flag = "relay-url" },
.{ .env = "JETSTREAM_PLC_URL", .flag = "plc-url" },
.{ .env = "JETSTREAM_DATA_DIR", .flag = "data-dir" },
.{ .env = "JETSTREAM_MAX_BACKFILL_REPOS", .flag = "max-backfill-repos" },
.{ .env = "JETSTREAM_BACKFILL_WORKERS", .flag = "backfill-workers" },
.{ .env = "JETSTREAM_BACKFILL_BATCH_SIZE", .flag = "backfill-batch-size" },
.{ .env = "JETSTREAM_BACKFILL_ASYNC_FLUSH_WORKERS", .flag = "backfill-async-flush-workers" },
.{ .env = "JETSTREAM_BACKFILL_REPOS", .flag = "backfill-repos" },
.{ .env = "JETSTREAM_SKIP_MERGE_DISCOVERY", .flag = "skip-merge-discovery" },
.{ .env = "JETSTREAM_REBLOOM_SWEEP", .flag = "rebloom-sweep" },
.{ .env = "JETSTREAM_FAILED_REPO_RETRY_INTERVAL", .flag = "failed-repo-retry-interval" },
.{ .env = "JETSTREAM_FAILED_REPO_RETRY_WORKERS", .flag = "failed-repo-retry-workers" },
.{ .env = "JETSTREAM_FAILED_REPO_RETRY_HOST_WORKERS", .flag = "failed-repo-retry-host-workers" },
.{ .env = "JETSTREAM_FAILED_REPO_RETRY_MAX_DELAY", .flag = "failed-repo-retry-max-delay" },
.{ .env = "JETSTREAM_DISABLE_REPO_ACTION_RATE_LIMITS", .flag = "disable-repo-action-rate-limits" },
.{ .env = "JETSTREAM_CURSOR_LOOKBACK", .flag = "cursor-lookback" },
.{ .env = "JETSTREAM_SEGMENT_CACHE_MAX_AGE", .flag = "segment-cache-max-age" },
.{ .env = "JETSTREAM_PLAN_MAX_DIDS", .flag = "plan-max-dids" },
.{ .env = "JETSTREAM_PLAN_MAX_COLLECTIONS", .flag = "plan-max-collections" },
.{ .env = "JETSTREAM_PLAN_MAX_ENTRIES", .flag = "plan-max-entries" },
.{ .env = "JETSTREAM_PLAN_WHOLE_SEGMENT_THRESHOLD", .flag = "plan-whole-segment-threshold" },
.{ .env = "JETSTREAM_SUBSCRIBE_READ_LOG_RETENTION_BYTES", .flag = "subscribe-read-log-retention-bytes" },
.{ .env = "JETSTREAM_SUBSCRIBE_BLOCK_CACHE_BYTES", .flag = "subscribe-block-cache-bytes" },
.{ .env = "JETSTREAM_SUBSCRIBE_READ_BATCH", .flag = "subscribe-read-batch" },
.{ .env = "JETSTREAM_SUBSCRIBE_SLOW_WINDOW", .flag = "subscribe-slow-window" },
.{ .env = "JETSTREAM_SUBSCRIBE_SLOW_MIN_RATE", .flag = "subscribe-slow-min-rate" },
.{ .env = "JETSTREAM_CURSOR_BLOCK_INDEX_CACHE_SIZE", .flag = "cursor-block-index-cache-size" },
.{ .env = "JETSTREAM_COMPACTION_INTERVAL", .flag = "compaction-interval" },
.{ .env = "JETSTREAM_COMPACTION_TOMBSTONE_CAP", .flag = "compaction-tombstone-cap" },
.{ .env = "JETSTREAM_COMPACTION_REWRITE_WORKERS", .flag = "compaction-rewrite-workers" },
.{ .env = "JETSTREAM_TIMESTAMP_IMPORT_TOKEN", .flag = "timestamp-import-token" },
.{ .env = "JETSTREAM_TIMESTAMP_IMPORT_DIR", .flag = "timestamp-import-dir" },
.{ .env = "JETSTREAM_ARCHIVE_API_KEY", .flag = "archive-api-key" },
.{ .env = "JETSTREAM_ARCHIVE_API_KEYS_FILE", .flag = "archive-api-keys-file" },
.{ .env = "JETSTREAM_MAX_SUBSCRIBERS", .flag = "max-subscribers" },
.{ .env = "JETSTREAM_MAX_COLD_READERS", .flag = "max-cold-readers" },
.{ .env = "JETSTREAM_UPSTREAM_SLOW_MIN_RATE", .flag = "upstream-slow-min-rate" },
};
pub fn processEntries(allocator: std.mem.Allocator) ![]const []const u8 {
var out: std.ArrayList([]const u8) = .empty;
errdefer out.deinit(allocator);
var i: usize = 0;
while (std.c.environ[i]) |entry| : (i += 1) {
try out.append(allocator, std.mem.span(entry));
}
return out.toOwnedSlice(allocator);
}
pub fn value(entries: []const []const u8, key: []const u8) ?[]const u8 {
for (entries) |entry| {
const split = std.mem.indexOfScalar(u8, entry, '=') orelse entry.len;
if (std.mem.eql(u8, entry[0..split], key)) {
return if (split < entry.len) entry[split + 1 ..] else "";
}
}
return null;
}
pub fn unknown(allocator: std.mem.Allocator, entries: []const []const u8) ![]const []const u8 {
var out: std.ArrayList([]const u8) = .empty;
errdefer out.deinit(allocator);
for (entries) |entry| {
const split = std.mem.indexOfScalar(u8, entry, '=') orelse entry.len;
const key = entry[0..split];
if (!std.mem.startsWith(u8, key, jetstream_prefix) or
std.mem.startsWith(u8, key, "JETSTREAM_APP_") or
std.mem.startsWith(u8, key, "JETSTREAM_ORACLE_") or
std.mem.startsWith(u8, key, "JETSTREAM_SIM_") or
isKnown(key)) continue;
var seen = false;
for (out.items) |prior| {
if (std.mem.eql(u8, prior, key)) {
seen = true;
break;
}
}
if (seen) continue;
try out.append(allocator, key);
}
std.mem.sort([]const u8, out.items, {}, struct {
fn lessThan(_: void, left: []const u8, right: []const u8) bool {
return std.mem.order(u8, left, right) == .lt;
}
}.lessThan);
return out.toOwnedSlice(allocator);
}
pub fn appendServeArguments(
allocator: std.mem.Allocator,
entries: []const []const u8,
cli_args: []const []const u8,
out: *std.ArrayList([]u8),
) !void {
for (mappings) |mapping| {
if (!mapping.serve) continue;
if (hasCliFlag(cli_args, mapping.flag)) continue;
const configured = value(entries, mapping.env) orelse continue;
const rendered = try std.fmt.allocPrint(allocator, "--{s}={s}", .{ mapping.flag, configured });
errdefer allocator.free(rendered);
try out.append(allocator, rendered);
}
}
fn hasCliFlag(args: []const []const u8, flag: []const u8) bool {
for (args) |arg| {
if (arg.len < 2 or arg[0] != '-' or arg[1] != '-') continue;
const body = arg[2..];
if (std.mem.eql(u8, body, flag)) return true;
if (body.len > flag.len and body[flag.len] == '=' and std.mem.eql(u8, body[0..flag.len], flag)) return true;
}
return false;
}
fn isKnown(key: []const u8) bool {
for (mappings) |mapping| if (std.mem.eql(u8, mapping.env, key)) return true;
return false;
}
test "unknown Jetstream variables are sorted, deduplicated, and scoped" {
try std.testing.expectEqual(@as(usize, 43), mappings.len);
const entries = [_][]const u8{
"JETSTREAM_ZZZ=1",
"JETSTREAM_ADDR=127.0.0.1:0",
"JETSTREAM_APP_INSTANCE=jetstream-0",
"JETSTREAM_ORACLE_SEED=7001",
"JETSTREAM_SIM_DATA_DIR=./sim",
"OTEL_SERVICE_NAME=test",
"JETSTREAM_AAA=value=with=equals",
"JETSTREAM_ZZZ=again",
};
const got = try unknown(std.testing.allocator, &entries);
defer std.testing.allocator.free(got);
try std.testing.expectEqual(@as(usize, 2), got.len);
try std.testing.expectEqualStrings("JETSTREAM_AAA", got[0]);
try std.testing.expectEqualStrings("JETSTREAM_ZZZ", got[1]);
}
test "serve flag sources preserve empty and equals-containing values" {
const entries = [_][]const u8{
"JETSTREAM_ADDR=",
"JETSTREAM_TIMESTAMP_IMPORT_TOKEN=secret=part",
"JETSTREAM_LOG_LEVEL=debug",
};
var args: std.ArrayList([]u8) = .empty;
defer {
for (args.items) |arg| std.testing.allocator.free(arg);
args.deinit(std.testing.allocator);
}
try appendServeArguments(std.testing.allocator, &entries, &.{}, &args);
try std.testing.expectEqualStrings("--addr=", args.items[0]);
try std.testing.expectEqualStrings("--timestamp-import-token=secret=part", args.items[1]);
try std.testing.expectEqual(@as(usize, 2), args.items.len);
}
test "explicit CLI sources suppress lower-priority environment values" {
const entries = [_][]const u8{
"JETSTREAM_ADDR=bad-env",
"JETSTREAM_SUBSCRIBE_READ_BATCH=not-an-int",
};
const cli = [_][]const u8{
"--addr=127.0.0.1:8080",
"--subscribe-read-batch=7",
};
var args: std.ArrayList([]u8) = .empty;
defer {
for (args.items) |arg| std.testing.allocator.free(arg);
args.deinit(std.testing.allocator);
}
try appendServeArguments(std.testing.allocator, &entries, &cli, &args);
try std.testing.expectEqual(@as(usize, 0), args.items.len);
}