Skip to content

Commit 764f978

Browse files
committed
forcing all jetstream instances to have a storage dir
1 parent 7b3d1d4 commit 764f978

File tree

12 files changed

+215
-46
lines changed

12 files changed

+215
-46
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ Thumbs.db
4646
.gradle
4747
.m2
4848

49-
# Build output directies
49+
# Build output directories
5050
/target
5151
*/target
5252
/build
@@ -75,3 +75,6 @@ atlassian-ide-plugin.xml
7575
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
7676
hs_err_pid*
7777
/target/
78+
79+
# test temp directory ignores
80+
/test_temp/jetstream

src/main/java/io/nats/ClusterInsert.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public String toString() {
4747
}
4848
StringBuilder sb = new StringBuilder();
4949
for (String s : configInserts) {
50-
sb.append(s).append("\r\n");
50+
sb.append(s).append(System.lineSeparator());
5151
}
5252
return sb.toString();
5353
}

src/main/java/io/nats/ClusterUtils.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313

1414
package io.nats;
1515

16-
import java.io.File;
17-
import java.io.IOException;
18-
import java.nio.file.Files;
1916
import java.nio.file.Path;
2017
import java.nio.file.Paths;
2118
import java.util.ArrayList;
@@ -68,10 +65,6 @@ public static List<ClusterNode> createNodes(ClusterDefaults cd, Path jsStoreDirB
6865
return nodes;
6966
}
7067

71-
public static Path createTemporaryJetStreamStoreDirBase() throws IOException {
72-
return Files.createTempDirectory(null);
73-
}
74-
7568
public static List<ClusterInsert> createClusterInserts(List<ClusterNode> nodes) {
7669
List<ClusterInsert> inserts = new ArrayList<>();
7770
for (ClusterNode node : nodes) {
@@ -81,16 +74,8 @@ public static List<ClusterInsert> createClusterInserts(List<ClusterNode> nodes)
8174
lines.add("http: " + node.monitor);
8275
}
8376
if (node.jsStoreDir != null) {
84-
String dir = node.jsStoreDir.toString();
85-
if (File.separatorChar == '\\') {
86-
dir = dir.replace("\\", "\\\\").replace("/", "\\\\");
87-
}
88-
else {
89-
dir = dir.replace("\\", "/");
90-
}
91-
lines.add("jetstream {");
92-
lines.add(" store_dir=" + dir);
93-
lines.add("}");
77+
JsStorageDir jssd = new JsStorageDir(node.jsStoreDir);
78+
lines.addAll(jssd.configInserts);
9479
}
9580
lines.add("server_name=" + node.serverName);
9681
lines.add("cluster {");
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// Copyright 2020-2025 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 io.nats;
15+
16+
import java.io.File;
17+
import java.io.IOException;
18+
import java.nio.file.Files;
19+
import java.nio.file.Path;
20+
import java.util.ArrayList;
21+
import java.util.List;
22+
23+
/**
24+
* An object representing the JetStream Storage dir
25+
*/
26+
public class JsStorageDir {
27+
public final String jsStoreDir;
28+
public final List<String> configInserts ;
29+
30+
public static JsStorageDir temporaryInstance() throws IOException {
31+
return new JsStorageDir(Files.createTempDirectory(null).toString(), false);
32+
}
33+
34+
public static JsStorageDir extractedInstance(String extracted) throws IOException {
35+
int at = extracted.indexOf("=");
36+
if (at == -1) {
37+
return new JsStorageDir(extracted.trim(), true);
38+
}
39+
return new JsStorageDir(extracted.substring(at + 1).trim(), true);
40+
}
41+
42+
public JsStorageDir(Path dirPath) {
43+
this(dirPath.toString(), false);
44+
}
45+
46+
public JsStorageDir(String dir) {
47+
this(dir, false);
48+
}
49+
50+
private JsStorageDir(String dir, boolean dirWasExtractedFromConfig) {
51+
52+
jsStoreDir = dir;
53+
String fixedDir;
54+
if (dirWasExtractedFromConfig) {
55+
fixedDir = dir;
56+
}
57+
else if (File.separatorChar == '\\') {
58+
fixedDir = dir.replace("\\", "\\\\").replace("/", "\\\\");
59+
}
60+
else {
61+
fixedDir = dir.replace("\\", "/");
62+
}
63+
configInserts = new ArrayList<>();
64+
configInserts.add("jetstream {");
65+
configInserts.add(" store_dir=" + fixedDir);
66+
configInserts.add("}");
67+
}
68+
69+
}

src/main/java/io/nats/NatsRunnerUtils.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public abstract class NatsRunnerUtils {
3030

3131
public static final String CONF_FILE_PREFIX = "nats_java_test";
3232
public static final String CONF_FILE_EXT = ".conf";
33+
public static final String JS_STORE_DIR_REGEX = "\\s*store_dir\\s*=";
3334
public static final String PORT_REGEX = "port: (\\d+)";
3435
public static final String PORT_MAPPED_REGEX = "port: <(\\w+)>";
3536
public static final String PORT_PROPERTY = "port: ";

src/main/java/io/nats/NatsServerRunner.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.nio.file.Path;
2121
import java.nio.file.Paths;
2222
import java.util.*;
23+
import java.util.concurrent.atomic.AtomicReference;
2324
import java.util.logging.Level;
2425
import java.util.logging.Logger;
2526
import java.util.regex.Matcher;
@@ -41,6 +42,7 @@ public class NatsServerRunner implements AutoCloseable {
4142
private final Map<String, Integer> _ports;
4243
private final File _configFile;
4344
private final String _cmdLine;
45+
private final AtomicReference<JsStorageDir> _jsStorageDir;
4446
private Process process;
4547
private OutputLogger nol;
4648

@@ -330,6 +332,8 @@ protected NatsServerRunner(Builder b) throws IOException {
330332
}
331333
}
332334

335+
_jsStorageDir = new AtomicReference<>();
336+
333337
int aliveCheckTries = b.aliveCheckTries == null ? DefaultProcessAliveCheckTries : b.aliveCheckTries;
334338
long aliveCheckWait = b.aliveCheckWait == null ? DefaultProcessAliveCheckWait : b.aliveCheckWait;
335339
int connectValidateTries = b.connectValidateTries == null ? DefaultConnectValidateTries : b.connectValidateTries;
@@ -342,7 +346,11 @@ protected NatsServerRunner(Builder b) throws IOException {
342346
cmd.add(_executablePath);
343347

344348
try {
345-
if (allowCommandLineOnly && b.configFilePath == null && b.configInserts == null) {
349+
if (allowCommandLineOnly
350+
&& !b.jetstream
351+
&& b.configFilePath == null
352+
&& b.configInserts == null)
353+
{
346354
int port = getPort();
347355
cmd.add("--port");
348356
cmd.add(Integer.toString(port));
@@ -359,7 +367,7 @@ protected NatsServerRunner(Builder b) throws IOException {
359367
portAlreadyDone = true;
360368
}
361369
else {
362-
processSuppliedConfigFile(writer, b.configFilePath);
370+
processSuppliedConfigFile(writer, b.configFilePath, b.jetstream);
363371
portAlreadyDone = _ports.get(NATS_PORT_KEY) != -1;
364372
}
365373

@@ -368,10 +376,20 @@ protected NatsServerRunner(Builder b) throws IOException {
368376
if (portAlreadyDone && s.startsWith("port:")) {
369377
continue;
370378
}
379+
if (s.contains("store_dir")) {
380+
if (_jsStorageDir.get() != null) {
381+
throw new IOException("store_dir provided in both config inserts and config file");
382+
}
383+
_jsStorageDir.set(JsStorageDir.extractedInstance(s));
384+
}
371385
writeLine(writer, s);
372386
}
373387
}
374388

389+
if (b.jetstream && _jsStorageDir.get() == null) {
390+
writeJetStreamStorage(writer, (Path)null);
391+
}
392+
375393
writer.flush();
376394
writer.close();
377395

@@ -520,8 +538,9 @@ private String getConfigSep(String configPath) {
520538
// ----------------------------------------------------------------------------------------------------
521539
// HELPERS
522540
// ----------------------------------------------------------------------------------------------------
523-
private void processSuppliedConfigFile(BufferedWriter writer, Path configFilePath) throws IOException {
541+
private void processSuppliedConfigFile(BufferedWriter writer, Path configFilePath, boolean jetStream) throws IOException {
524542
Matcher constructionPortMatcher = Pattern.compile(PORT_REGEX).matcher("");
543+
Matcher constructionJsStoreDirMatcher = Pattern.compile(JS_STORE_DIR_REGEX).matcher("");
525544
Matcher mappedPortMatcher = Pattern.compile(PORT_MAPPED_REGEX).matcher("");
526545

527546
BufferedReader reader = new BufferedReader(new FileReader(configFilePath.toFile()));
@@ -579,6 +598,13 @@ else if (trim.startsWith("}")) {
579598
}
580599
}
581600

601+
if (jetStream) {
602+
constructionJsStoreDirMatcher.reset(line);
603+
if (constructionJsStoreDirMatcher.find()) {
604+
_jsStorageDir.set(JsStorageDir.extractedInstance(line));
605+
}
606+
}
607+
582608
line = reader.readLine();
583609
}
584610

@@ -602,9 +628,19 @@ private void writePortLine(BufferedWriter writer, int port) throws IOException {
602628
writeLine(writer, PORT_PROPERTY + port);
603629
}
604630

631+
private void writeJetStreamStorage(BufferedWriter writer, Path path) throws IOException {
632+
JsStorageDir jssd = path == null
633+
? JsStorageDir.temporaryInstance()
634+
: new JsStorageDir(path);
635+
_jsStorageDir.set(jssd);
636+
for (String c : jssd.configInserts) {
637+
writeLine(writer, c);
638+
}
639+
}
640+
605641
private void writeLine(BufferedWriter writer, String line) throws IOException {
606642
writer.write(line);
607-
writer.write("\n");
643+
writer.write(System.lineSeparator());
608644
}
609645

610646
private void sleep(long sleep) {

src/test/java/io/nats/ClusterTest.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
import org.junit.jupiter.api.Test;
1717
import org.junit.jupiter.api.parallel.Isolated;
1818

19+
import java.nio.file.Files;
1920
import java.nio.file.Path;
2021
import java.nio.file.Paths;
2122
import java.util.Collections;
2223
import java.util.List;
2324

24-
import static io.nats.ClusterUtils.*;
25+
import static io.nats.ClusterUtils.DEFAULT_CLUSTER_DEFAULTS;
26+
import static io.nats.ClusterUtils.createClusterInserts;
2527
import static org.junit.jupiter.api.Assertions.*;
2628

2729
@Isolated
@@ -31,8 +33,8 @@ public class ClusterTest extends TestBase {
3133
public void testCreateCluster() throws Exception {
3234
_testCreateCluster(createClusterInserts(), false);
3335
_testCreateCluster(createClusterInserts(DEFAULT_CLUSTER_DEFAULTS), false);
34-
_testCreateCluster(createClusterInserts(createTemporaryJetStreamStoreDirBase()), true);
35-
_testCreateCluster(createClusterInserts(DEFAULT_CLUSTER_DEFAULTS, createTemporaryJetStreamStoreDirBase()), true);
36+
_testCreateCluster(createClusterInserts(Files.createTempDirectory(null)), true);
37+
_testCreateCluster(createClusterInserts(DEFAULT_CLUSTER_DEFAULTS, Files.createTempDirectory(null)), true);
3638
}
3739

3840
private void _testCreateCluster(List<ClusterInsert> clusterInserts, boolean js) throws Exception {

0 commit comments

Comments
 (0)