Skip to content

Commit 01df9c6

Browse files
committed
support duplicate paths
1 parent 41fe7a5 commit 01df9c6

File tree

7 files changed

+57
-39
lines changed

7 files changed

+57
-39
lines changed

server/src/internalClusterTest/java/org/elasticsearch/discovery/single/SingleNodeDiscoveryIT.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,8 @@ public Path nodeConfigPath(int nodeOrdinal) {
8787
0,
8888
"other",
8989
Arrays.asList(getTestTransportPlugin(), MockHttpTransport.TestPlugin.class),
90-
Function.identity()
90+
Function.identity(),
91+
TEST_ENTITLEMENTS::newNodeGrant
9192
);
9293
try {
9394
other.beforeTest(random());
@@ -137,7 +138,8 @@ public Path nodeConfigPath(int nodeOrdinal) {
137138
0,
138139
"other",
139140
Arrays.asList(getTestTransportPlugin(), MockHttpTransport.TestPlugin.class),
140-
Function.identity()
141+
Function.identity(),
142+
TEST_ENTITLEMENTS::newNodeGrant
141143
);
142144
try (var mockLog = MockLog.capture(JoinHelper.class)) {
143145
mockLog.addExpectation(

server/src/internalClusterTest/java/org/elasticsearch/snapshots/MultiClusterRepoAccessIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ public Path nodeConfigPath(int nodeOrdinal) {
7777
InternalSettingsPlugin.class,
7878
getTestTransportPlugin()
7979
),
80-
Function.identity()
80+
Function.identity(),
81+
TEST_ENTITLEMENTS::newNodeGrant
8182
);
8283
secondCluster.beforeTest(random());
8384
}

test/framework/src/main/java/org/elasticsearch/entitlement/bootstrap/TestEntitlementsRule.java

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
package org.elasticsearch.entitlement.bootstrap;
1111

12-
import org.apache.lucene.search.Multiset;
1312
import org.apache.lucene.tests.mockfile.FilterPath;
1413
import org.elasticsearch.bootstrap.TestBuildInfo;
1514
import org.elasticsearch.bootstrap.TestBuildInfoParser;
@@ -52,6 +51,7 @@
5251
import java.util.Set;
5352
import java.util.TreeSet;
5453
import java.util.concurrent.ConcurrentHashMap;
54+
import java.util.function.BiConsumer;
5555
import java.util.function.BiFunction;
5656
import java.util.function.Consumer;
5757

@@ -168,18 +168,20 @@ private Path configDir() {
168168
return configPath != null ? configPath : homeDir().resolve("config");
169169
}
170170

171-
private Collection<Path> dataDirs() {
171+
private Path[] dataDirs() {
172172
List<String> dataDirs = PATH_DATA_SETTING.get(settings);
173-
return dataDirs.isEmpty() ? List.of(homeDir().resolve("data")) : dataDirs.stream().map(NodeGrant::absolutePath).toList();
173+
return dataDirs.isEmpty()
174+
? new Path[] { homeDir().resolve("data") }
175+
: dataDirs.stream().map(NodeGrant::absolutePath).toArray(Path[]::new);
174176
}
175177

176-
private Collection<Path> sharedDataDir() {
178+
private Path[] sharedDataDir() {
177179
String sharedDataDir = PATH_SHARED_DATA_SETTING.get(settings);
178-
return Strings.hasText(sharedDataDir) ? List.of(absolutePath(sharedDataDir)) : List.of();
180+
return Strings.hasText(sharedDataDir) ? new Path[] { absolutePath(sharedDataDir) } : new Path[0];
179181
}
180182

181-
private Collection<Path> repoDirs() {
182-
return PATH_REPO_SETTING.get(settings).stream().map(NodeGrant::absolutePath).toList();
183+
private Path[] repoDirs() {
184+
return PATH_REPO_SETTING.get(settings).stream().map(NodeGrant::absolutePath).toArray(Path[]::new);
183185
}
184186

185187
@SuppressForbidden(reason = "must be resolved using the default file system, rather then the mocked test file system")
@@ -205,33 +207,35 @@ public String toString() {
205207
}
206208

207209
private void addGrant(NodeGrant nodeGrant) {
208-
logger.error("Adding node grant: {}", nodeGrant);
209-
BASE_DIR_PATHS.compute(BaseDir.CONFIG, baseDirModifier(paths -> paths.add(nodeGrant.configDir())));
210-
BASE_DIR_PATHS.compute(BaseDir.DATA, baseDirModifier(paths -> paths.addAll(nodeGrant.dataDirs())));
211-
BASE_DIR_PATHS.compute(BaseDir.SHARED_DATA, baseDirModifier(paths -> paths.addAll(nodeGrant.sharedDataDir())));
212-
BASE_DIR_PATHS.compute(BaseDir.SHARED_REPO, baseDirModifier(paths -> paths.addAll(nodeGrant.repoDirs())));
210+
logger.debug("Adding node grant: {}", nodeGrant);
211+
BASE_DIR_PATHS.compute(BaseDir.CONFIG, baseDirModifier(Collection::add, nodeGrant.configDir()));
212+
BASE_DIR_PATHS.compute(BaseDir.DATA, baseDirModifier(Collection::add, nodeGrant.dataDirs()));
213+
BASE_DIR_PATHS.compute(BaseDir.SHARED_DATA, baseDirModifier(Collection::add, nodeGrant.sharedDataDir()));
214+
BASE_DIR_PATHS.compute(BaseDir.SHARED_REPO, baseDirModifier(Collection::add, nodeGrant.repoDirs()));
213215
POLICY_MANAGER.clearModuleEntitlementsCache();
214216
}
215217

216218
private void revokeGrant(NodeGrant nodeGrant) {
217-
logger.error("Revoking node grant: {}", nodeGrant);
218-
BASE_DIR_PATHS.compute(BaseDir.CONFIG, baseDirModifier(paths -> paths.remove(nodeGrant.configDir())));
219-
BASE_DIR_PATHS.compute(BaseDir.DATA, baseDirModifier(paths -> paths.removeAll(nodeGrant.dataDirs())));
220-
BASE_DIR_PATHS.compute(BaseDir.SHARED_DATA, baseDirModifier(paths -> paths.removeAll(nodeGrant.sharedDataDir())));
221-
BASE_DIR_PATHS.compute(BaseDir.SHARED_REPO, baseDirModifier(paths -> paths.removeAll(nodeGrant.repoDirs())));
219+
logger.debug("Revoking node grant: {}", nodeGrant);
220+
BASE_DIR_PATHS.compute(BaseDir.CONFIG, baseDirModifier(Collection::remove, nodeGrant.configDir()));
221+
BASE_DIR_PATHS.compute(BaseDir.DATA, baseDirModifier(Collection::remove, nodeGrant.dataDirs()));
222+
BASE_DIR_PATHS.compute(BaseDir.SHARED_DATA, baseDirModifier(Collection::remove, nodeGrant.sharedDataDir()));
223+
BASE_DIR_PATHS.compute(BaseDir.SHARED_REPO, baseDirModifier(Collection::remove, nodeGrant.repoDirs()));
222224
POLICY_MANAGER.clearModuleEntitlementsCache();
223225
}
224226

225-
// this uses a counting multiset to allow duplicates between nodes, e.g. the config dir
226-
private static BiFunction<BaseDir, Collection<Path>, Collection<Path>> baseDirModifier(Consumer<Collection<Path>> consumer) {
227+
// This must allow for duplicate paths between nodes, the config dir for instance is shared across all nodes.
228+
private static BiFunction<BaseDir, Collection<Path>, Collection<Path>> baseDirModifier(
229+
BiConsumer<Collection<Path>, Path> operation,
230+
Path... updates
231+
) {
227232
// always return a new unmodifiable copy
228233
return (BaseDir baseDir, Collection<Path> paths) -> {
229-
Collection<Path> newPaths = new Multiset<>();
230-
if (paths != null) {
231-
newPaths.addAll(paths);
234+
paths = paths == null ? new ArrayList<>() : new ArrayList<>(paths);
235+
for (Path update : updates) {
236+
operation.accept(paths, update);
232237
}
233-
consumer.accept(newPaths);
234-
return Collections.unmodifiableCollection(newPaths);
238+
return Collections.unmodifiableCollection(paths);
235239
};
236240
}
237241

test/framework/src/main/java/org/elasticsearch/test/AbstractMultiClustersTestCase.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,8 @@ public final void startClusters() throws Exception {
127127
0,
128128
clusterName + "-",
129129
mockPlugins,
130-
Function.identity()
130+
Function.identity(),
131+
TEST_ENTITLEMENTS::newNodeGrant
131132
);
132133
try {
133134
cluster.beforeTest(random());

test/framework/src/main/java/org/elasticsearch/test/InternalTestCluster.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,8 @@ public InternalTestCluster(
295295
final int numClientNodes,
296296
final String nodePrefix,
297297
final Collection<Class<? extends Plugin>> mockPlugins,
298-
final Function<Client, Client> clientWrapper
298+
final Function<Client, Client> clientWrapper,
299+
NodeGrantProvider nodeGrantProvider
299300
) {
300301
this(
301302
clusterSeed,
@@ -313,7 +314,7 @@ public InternalTestCluster(
313314
true,
314315
false,
315316
true,
316-
(settings, configPath) -> () -> {}
317+
nodeGrantProvider
317318
);
318319
}
319320

test/framework/src/test/java/org/elasticsearch/test/test/InternalTestClusterTests.java

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,8 @@ public void testInitializiationIsConsistent() {
8585
numClientNodes,
8686
nodePrefix,
8787
Collections.emptyList(),
88-
Function.identity()
88+
Function.identity(),
89+
TEST_ENTITLEMENTS::newNodeGrant
8990
);
9091
InternalTestCluster cluster1 = new InternalTestCluster(
9192
clusterSeed,
@@ -99,7 +100,8 @@ public void testInitializiationIsConsistent() {
99100
numClientNodes,
100101
nodePrefix,
101102
Collections.emptyList(),
102-
Function.identity()
103+
Function.identity(),
104+
TEST_ENTITLEMENTS::newNodeGrant
103105
);
104106
assertClusters(cluster0, cluster1, true);
105107
}
@@ -198,7 +200,8 @@ public Path nodeConfigPath(int nodeOrdinal) {
198200
numClientNodes,
199201
nodePrefix,
200202
mockPlugins(),
201-
Function.identity()
203+
Function.identity(),
204+
TEST_ENTITLEMENTS::newNodeGrant
202205
);
203206
cluster0.setBootstrapMasterNodeIndex(bootstrapMasterNodeIndex);
204207

@@ -214,7 +217,8 @@ public Path nodeConfigPath(int nodeOrdinal) {
214217
numClientNodes,
215218
nodePrefix,
216219
mockPlugins(),
217-
Function.identity()
220+
Function.identity(),
221+
TEST_ENTITLEMENTS::newNodeGrant
218222
);
219223
cluster1.setBootstrapMasterNodeIndex(bootstrapMasterNodeIndex);
220224

@@ -280,7 +284,8 @@ public Path nodeConfigPath(int nodeOrdinal) {
280284
numClientNodes,
281285
nodePrefix,
282286
mockPlugins(),
283-
Function.identity()
287+
Function.identity(),
288+
TEST_ENTITLEMENTS::newNodeGrant
284289
);
285290
try {
286291
cluster.beforeTest(random());
@@ -375,7 +380,8 @@ public Path nodeConfigPath(int nodeOrdinal) {
375380
0,
376381
"",
377382
mockPlugins(),
378-
Function.identity()
383+
Function.identity(),
384+
TEST_ENTITLEMENTS::newNodeGrant
379385
);
380386
cluster.beforeTest(random());
381387
List<DiscoveryNodeRole> roles = new ArrayList<>();
@@ -467,7 +473,8 @@ public Path nodeConfigPath(int nodeOrdinal) {
467473
0,
468474
nodePrefix,
469475
plugins,
470-
Function.identity()
476+
Function.identity(),
477+
TEST_ENTITLEMENTS::newNodeGrant
471478
);
472479
try {
473480
cluster.beforeTest(random());

x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/CcrIntegTestCase.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,8 @@ public final void startClusters() throws Exception {
182182
0,
183183
"leader",
184184
mockPlugins,
185-
Function.identity()
185+
Function.identity(),
186+
TEST_ENTITLEMENTS::newNodeGrant
186187
);
187188
leaderCluster.beforeTest(random());
188189
leaderCluster.ensureAtLeastNumDataNodes(numberOfNodesPerCluster());
@@ -204,7 +205,8 @@ public final void startClusters() throws Exception {
204205
0,
205206
"follower",
206207
mockPlugins,
207-
Function.identity()
208+
Function.identity(),
209+
TEST_ENTITLEMENTS::newNodeGrant
208210
);
209211
clusterGroup = new ClusterGroup(leaderCluster, followerCluster);
210212

0 commit comments

Comments
 (0)