Skip to content

Commit b325699

Browse files
committed
Merge remote-tracking branch 'upstream/main' into tsdb-timestamp-sparse-index
2 parents ce9f792 + ad99b0d commit b325699

File tree

53 files changed

+7872
-7473
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+7872
-7473
lines changed

docs/changelog/122860.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 122860
2+
summary: Improved error message when index field type is invalid
3+
area: Mapping
4+
type: enhancement
5+
issues: []

docs/changelog/123272.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 123272
2+
summary: Set Connect Timeout to 5s
3+
area: Machine Learning
4+
type: bug
5+
issues: []

libs/entitlement/src/main/java/org/elasticsearch/entitlement/bootstrap/EntitlementBootstrap.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ public record BootstrapArgs(
3939
Function<Class<?>, String> pluginResolver,
4040
Function<String, String> settingResolver,
4141
Function<String, Stream<String>> settingGlobResolver,
42-
Function<String, Path> repoDirResolver,
4342
Path[] dataDirs,
43+
Path[] sharedRepoDirs,
4444
Path configDir,
4545
Path libDir,
4646
Path logsDir,
@@ -51,11 +51,11 @@ public record BootstrapArgs(
5151
requireNonNull(pluginResolver);
5252
requireNonNull(settingResolver);
5353
requireNonNull(settingGlobResolver);
54-
requireNonNull(repoDirResolver);
5554
requireNonNull(dataDirs);
5655
if (dataDirs.length == 0) {
5756
throw new IllegalArgumentException("must provide at least one data directory");
5857
}
58+
requireNonNull(sharedRepoDirs);
5959
requireNonNull(configDir);
6060
requireNonNull(libDir);
6161
requireNonNull(logsDir);
@@ -77,8 +77,8 @@ public static BootstrapArgs bootstrapArgs() {
7777
* @param pluginResolver a functor to map a Java Class to the plugin it belongs to (the plugin name).
7878
* @param settingResolver a functor to resolve the value of an Elasticsearch setting.
7979
* @param settingGlobResolver a functor to resolve a glob expression for one or more Elasticsearch settings.
80-
* @param repoDirResolver a functor to map a repository location to its Elasticsearch path.
8180
* @param dataDirs data directories for Elasticsearch
81+
* @param sharedRepoDirs shared repository directories for Elasticsearch
8282
* @param configDir the config directory for Elasticsearch
8383
* @param libDir the lib directory for Elasticsearch
8484
* @param tempDir the temp directory for Elasticsearch
@@ -89,8 +89,8 @@ public static void bootstrap(
8989
Function<Class<?>, String> pluginResolver,
9090
Function<String, String> settingResolver,
9191
Function<String, Stream<String>> settingGlobResolver,
92-
Function<String, Path> repoDirResolver,
9392
Path[] dataDirs,
93+
Path[] sharedRepoDirs,
9494
Path configDir,
9595
Path libDir,
9696
Path logsDir,
@@ -105,8 +105,8 @@ public static void bootstrap(
105105
pluginResolver,
106106
settingResolver,
107107
settingGlobResolver,
108-
repoDirResolver,
109108
dataDirs,
109+
sharedRepoDirs,
110110
configDir,
111111
libDir,
112112
logsDir,

libs/entitlement/src/main/java/org/elasticsearch/entitlement/initialization/EntitlementInitialization.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@
6363
import java.util.stream.Stream;
6464
import java.util.stream.StreamSupport;
6565

66+
import static org.elasticsearch.entitlement.runtime.policy.entitlements.FilesEntitlement.BaseDir.DATA;
67+
import static org.elasticsearch.entitlement.runtime.policy.entitlements.FilesEntitlement.BaseDir.SHARED_REPO;
6668
import static org.elasticsearch.entitlement.runtime.policy.entitlements.FilesEntitlement.Mode.READ;
6769
import static org.elasticsearch.entitlement.runtime.policy.entitlements.FilesEntitlement.Mode.READ_WRITE;
6870

@@ -138,6 +140,7 @@ private static PolicyManager createPolicyManager() {
138140
getUserHome(),
139141
bootstrapArgs.configDir(),
140142
bootstrapArgs.dataDirs(),
143+
bootstrapArgs.sharedRepoDirs(),
141144
bootstrapArgs.tempDir(),
142145
bootstrapArgs.settingResolver(),
143146
bootstrapArgs.settingGlobResolver()
@@ -152,8 +155,8 @@ private static PolicyManager createPolicyManager() {
152155
new CreateClassLoaderEntitlement(),
153156
new FilesEntitlement(
154157
List.of(
155-
FileData.ofPath(bootstrapArgs.repoDirResolver().apply(""), READ_WRITE),
156-
FileData.ofRelativePath(Path.of(""), FilesEntitlement.BaseDir.DATA, READ_WRITE)
158+
FileData.ofRelativePath(Path.of(""), SHARED_REPO, READ_WRITE),
159+
FileData.ofRelativePath(Path.of(""), DATA, READ_WRITE)
157160
)
158161
)
159162
)
@@ -175,8 +178,8 @@ private static PolicyManager createPolicyManager() {
175178
FileData.ofPath(bootstrapArgs.tempDir(), READ_WRITE),
176179
FileData.ofPath(bootstrapArgs.configDir(), READ),
177180
FileData.ofPath(bootstrapArgs.logsDir(), READ_WRITE),
178-
FileData.ofRelativePath(Path.of(""), FilesEntitlement.BaseDir.DATA, READ_WRITE),
179-
FileData.ofPath(bootstrapArgs.repoDirResolver().apply(""), READ_WRITE),
181+
FileData.ofRelativePath(Path.of(""), DATA, READ_WRITE),
182+
FileData.ofRelativePath(Path.of(""), SHARED_REPO, READ_WRITE),
180183

181184
// OS release on Linux
182185
FileData.ofPath(Path.of("/etc/os-release"), READ),
@@ -210,21 +213,21 @@ private static PolicyManager createPolicyManager() {
210213
List.of(
211214
FileData.ofPath(bootstrapArgs.configDir(), READ),
212215
FileData.ofPath(bootstrapArgs.tempDir(), READ),
213-
FileData.ofRelativePath(Path.of(""), FilesEntitlement.BaseDir.DATA, READ_WRITE)
216+
FileData.ofRelativePath(Path.of(""), DATA, READ_WRITE)
214217
)
215218
)
216219
)
217220
),
218221
new Scope(
219222
"org.apache.lucene.misc",
220-
List.of(new FilesEntitlement(List.of(FileData.ofRelativePath(Path.of(""), FilesEntitlement.BaseDir.DATA, READ_WRITE))))
223+
List.of(new FilesEntitlement(List.of(FileData.ofRelativePath(Path.of(""), DATA, READ_WRITE))))
221224
),
222225
new Scope("org.apache.logging.log4j.core", List.of(new ManageThreadsEntitlement())),
223226
new Scope(
224227
"org.elasticsearch.nativeaccess",
225228
List.of(
226229
new LoadNativeLibrariesEntitlement(),
227-
new FilesEntitlement(List.of(FileData.ofRelativePath(Path.of(""), FilesEntitlement.BaseDir.DATA, READ_WRITE)))
230+
new FilesEntitlement(List.of(FileData.ofRelativePath(Path.of(""), DATA, READ_WRITE)))
228231
)
229232
)
230233
);

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/FileAccessTree.java

Lines changed: 41 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.nio.file.Path;
1515
import java.util.ArrayList;
1616
import java.util.Arrays;
17+
import java.util.Comparator;
1718
import java.util.List;
1819
import java.util.Objects;
1920

@@ -46,8 +47,8 @@ private FileAccessTree(FilesEntitlement filesEntitlement, PathLookup pathLookup)
4647
readPaths.add(tempDir);
4748
writePaths.add(tempDir);
4849

49-
readPaths.sort(String::compareTo);
50-
writePaths.sort(String::compareTo);
50+
readPaths.sort(PATH_ORDER);
51+
writePaths.sort(PATH_ORDER);
5152

5253
this.readPaths = pruneSortedPaths(readPaths).toArray(new String[0]);
5354
this.writePaths = pruneSortedPaths(writePaths).toArray(new String[0]);
@@ -60,7 +61,7 @@ private static List<String> pruneSortedPaths(List<String> paths) {
6061
prunedReadPaths.add(currentPath);
6162
for (int i = 1; i < paths.size(); ++i) {
6263
String nextPath = paths.get(i);
63-
if (nextPath.startsWith(currentPath) == false) {
64+
if (isParent(currentPath, nextPath) == false) {
6465
prunedReadPaths.add(nextPath);
6566
currentPath = nextPath;
6667
}
@@ -88,21 +89,28 @@ static String normalizePath(Path path) {
8889
// Note that toAbsolutePath produces paths separated by the default file separator,
8990
// so on Windows, if the given path uses forward slashes, this consistently
9091
// converts it to backslashes.
91-
return path.toAbsolutePath().normalize().toString();
92+
String result = path.toAbsolutePath().normalize().toString();
93+
while (result.endsWith(FILE_SEPARATOR)) {
94+
result = result.substring(0, result.length() - FILE_SEPARATOR.length());
95+
}
96+
return result;
9297
}
9398

9499
private static boolean checkPath(String path, String[] paths) {
95100
if (paths.length == 0) {
96101
return false;
97102
}
98-
int ndx = Arrays.binarySearch(paths, path);
103+
int ndx = Arrays.binarySearch(paths, path, PATH_ORDER);
99104
if (ndx < -1) {
100-
String maybeParent = paths[-ndx - 2];
101-
return path.startsWith(maybeParent) && path.startsWith(FILE_SEPARATOR, maybeParent.length());
105+
return isParent(paths[-ndx - 2], path);
102106
}
103107
return ndx >= 0;
104108
}
105109

110+
private static boolean isParent(String maybeParent, String path) {
111+
return path.startsWith(maybeParent) && path.startsWith(FILE_SEPARATOR, maybeParent.length());
112+
}
113+
106114
@Override
107115
public boolean equals(Object o) {
108116
if (o == null || getClass() != o.getClass()) return false;
@@ -114,4 +122,30 @@ public boolean equals(Object o) {
114122
public int hashCode() {
115123
return Objects.hash(Arrays.hashCode(readPaths), Arrays.hashCode(writePaths));
116124
}
125+
126+
/**
127+
* For our lexicographic sort trick to work correctly, we must have path separators sort before
128+
* any other character so that files in a directory appear immediately after that directory.
129+
* For example, we require [/a, /a/b, /a.xml] rather than the natural order [/a, /a.xml, /a/b].
130+
*/
131+
private static final Comparator<String> PATH_ORDER = (s1, s2) -> {
132+
Path p1 = Path.of(s1);
133+
Path p2 = Path.of(s2);
134+
var i1 = p1.iterator();
135+
var i2 = p2.iterator();
136+
while (i1.hasNext() && i2.hasNext()) {
137+
int cmp = i1.next().compareTo(i2.next());
138+
if (cmp != 0) {
139+
return cmp;
140+
}
141+
}
142+
if (i1.hasNext()) {
143+
return 1;
144+
} else if (i2.hasNext()) {
145+
return -1;
146+
} else {
147+
assert p1.equals(p2);
148+
return 0;
149+
}
150+
};
117151
}

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/PathLookup.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public record PathLookup(
1717
Path homeDir,
1818
Path configDir,
1919
Path[] dataDirs,
20+
Path[] sharedRepoDirs,
2021
Path tempDir,
2122
Function<String, String> settingResolver,
2223
Function<String, Stream<String>> settingGlobResolver

libs/entitlement/src/main/java/org/elasticsearch/entitlement/runtime/policy/entitlements/FilesEntitlement.java

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.Objects;
2222
import java.util.stream.Stream;
2323

24+
import static java.lang.Character.isLetter;
25+
2426
/**
2527
* Describes a file entitlement with a path and mode.
2628
*/
@@ -36,6 +38,7 @@ public enum Mode {
3638
public enum BaseDir {
3739
CONFIG,
3840
DATA,
41+
SHARED_REPO,
3942
HOME
4043
}
4144

@@ -60,6 +63,51 @@ static FileData ofPathSetting(String setting, Mode mode) {
6063
static FileData ofRelativePathSetting(String setting, BaseDir baseDir, Mode mode) {
6164
return new RelativePathSettingFileData(setting, baseDir, mode);
6265
}
66+
67+
/**
68+
* Tests if a path is absolute or relative, taking into consideration both Unix and Windows conventions.
69+
* Note that this leads to a conflict, resolved in favor of Unix rules: `/foo` can be either a Unix absolute path, or a Windows
70+
* relative path with "wrong" directory separator (using non-canonical slash in Windows).
71+
*/
72+
static boolean isAbsolutePath(String path) {
73+
if (path.isEmpty()) {
74+
return false;
75+
}
76+
if (path.charAt(0) == '/') {
77+
// Unix/BSD absolute
78+
return true;
79+
}
80+
81+
return isWindowsAbsolutePath(path);
82+
}
83+
84+
private static boolean isSlash(char c) {
85+
return (c == '\\') || (c == '/');
86+
}
87+
88+
private static boolean isWindowsAbsolutePath(String input) {
89+
// if a prefix is present, we expected (long) UNC or (long) absolute
90+
if (input.startsWith("\\\\?\\")) {
91+
return true;
92+
}
93+
94+
if (input.length() > 1) {
95+
char c0 = input.charAt(0);
96+
char c1 = input.charAt(1);
97+
char c = 0;
98+
int next = 2;
99+
if (isSlash(c0) && isSlash(c1)) {
100+
// Two slashes or more: UNC
101+
return true;
102+
}
103+
if (isLetter(c0) && c1 == ':') {
104+
// A drive: absolute
105+
return true;
106+
}
107+
}
108+
// Otherwise relative
109+
return false;
110+
}
63111
}
64112

65113
private sealed interface RelativeFileData extends FileData {
@@ -75,14 +123,9 @@ default Stream<Path> resolvePaths(PathLookup pathLookup) {
75123
case CONFIG:
76124
return relativePaths.map(relativePath -> pathLookup.configDir().resolve(relativePath));
77125
case DATA:
78-
// multiple data dirs are a pain...we need the combination of relative paths and data dirs
79-
List<Path> paths = new ArrayList<>();
80-
for (var relativePath : relativePaths.toList()) {
81-
for (var dataDir : pathLookup.dataDirs()) {
82-
paths.add(dataDir.resolve(relativePath));
83-
}
84-
}
85-
return paths.stream();
126+
return relativePathsCombination(pathLookup.dataDirs(), relativePaths);
127+
case SHARED_REPO:
128+
return relativePathsCombination(pathLookup.sharedRepoDirs(), relativePaths);
86129
case HOME:
87130
return relativePaths.map(relativePath -> pathLookup.homeDir().resolve(relativePath));
88131
default:
@@ -91,6 +134,17 @@ default Stream<Path> resolvePaths(PathLookup pathLookup) {
91134
}
92135
}
93136

137+
private static Stream<Path> relativePathsCombination(Path[] baseDirs, Stream<Path> relativePaths) {
138+
// multiple base dirs are a pain...we need the combination of the base dirs and relative paths
139+
List<Path> paths = new ArrayList<>();
140+
for (var relativePath : relativePaths.toList()) {
141+
for (var dataDir : baseDirs) {
142+
paths.add(dataDir.resolve(relativePath));
143+
}
144+
}
145+
return paths.stream();
146+
}
147+
94148
private record AbsolutePathFileData(Path path, Mode mode) implements FileData {
95149
@Override
96150
public Stream<Path> resolvePaths(PathLookup pathLookup) {
@@ -142,6 +196,7 @@ private static BaseDir parseBaseDir(String baseDir) {
142196
case "config" -> BaseDir.CONFIG;
143197
case "data" -> BaseDir.DATA;
144198
case "home" -> BaseDir.HOME;
199+
// NOTE: shared_repo is _not_ accessible to policy files, only internally
145200
default -> throw new PolicyValidationException(
146201
"invalid relative directory: " + baseDir + ", valid values: [config, data, home]"
147202
);
@@ -190,17 +245,15 @@ public static FilesEntitlement build(List<Object> paths) {
190245
throw new PolicyValidationException("files entitlement with a 'relative_path' must specify 'relative_to'");
191246
}
192247

193-
Path relativePath = Path.of(relativePathAsString);
194-
if (relativePath.isAbsolute()) {
248+
if (FileData.isAbsolutePath(relativePathAsString)) {
195249
throw new PolicyValidationException("'relative_path' [" + relativePathAsString + "] must be relative");
196250
}
197-
filesData.add(FileData.ofRelativePath(relativePath, baseDir, mode));
251+
filesData.add(FileData.ofRelativePath(Path.of(relativePathAsString), baseDir, mode));
198252
} else if (pathAsString != null) {
199-
Path path = Path.of(pathAsString);
200-
if (path.isAbsolute() == false) {
253+
if (FileData.isAbsolutePath(pathAsString) == false) {
201254
throw new PolicyValidationException("'path' [" + pathAsString + "] must be absolute");
202255
}
203-
filesData.add(FileData.ofPath(path, mode));
256+
filesData.add(FileData.ofPath(Path.of(pathAsString), mode));
204257
} else if (pathSetting != null) {
205258
filesData.add(FileData.ofPathSetting(pathSetting, mode));
206259
} else if (relativePathSetting != null) {

0 commit comments

Comments
 (0)