Skip to content

Commit 79c52e3

Browse files
authored
Merge branch 'main' into make_inlinestats_tests_snapshot
2 parents 3b5b3e1 + 2721a6b commit 79c52e3

File tree

53 files changed

+2399
-964
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

+2399
-964
lines changed

docs/changelog/133424.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 133424
2+
summary: Ensuring only a single request executor object is created
3+
area: Machine Learning
4+
type: bug
5+
issues: []

docs/changelog/133604.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 133604
2+
summary: Update `DefBootstrap` to handle Error from `ClassValue`
3+
area: Infra/Scripting
4+
type: bug
5+
issues: []

docs/changelog/133611.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 133611
2+
summary: Allow trailing empty string field names in paths of flattened field
3+
area: Mapping
4+
type: bug
5+
issues:
6+
- 130139

modules/lang-painless/src/main/java/org/elasticsearch/painless/DefBootstrap.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,10 @@ protected MethodHandle computeValue(Class<?> receiverType) {
201201
try {
202202
return lookup(flavor, name, receiverType).asType(type);
203203
} catch (Throwable t) {
204-
Def.rethrow(t);
204+
// ClassValue.getFromHashMap wraps checked exceptions as Error, so we
205+
// use a sentinel class [PainlessWrappedException] here to work around
206+
// this issue and later unwrap the original exception
207+
Def.rethrow(t instanceof Exception ? new PainlessWrappedException((Exception) t) : t);
205208
throw new AssertionError();
206209
}
207210
}

modules/lang-painless/src/main/java/org/elasticsearch/painless/PainlessScript.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ public interface PainlessScript {
4646
* @return The generated ScriptException.
4747
*/
4848
default ScriptException convertToScriptException(Throwable t, Map<String, List<String>> extraMetadata) {
49+
if (t instanceof PainlessWrappedException) {
50+
t = t.getCause();
51+
}
4952
// create a script stack: this is just the script portion
5053
List<String> scriptStack = new ArrayList<>();
5154
ScriptException.Position pos = null;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the "Elastic License
4+
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side
5+
* Public License v 1"; you may not use this file except in compliance with, at
6+
* your election, the "Elastic License 2.0", the "GNU Affero General Public
7+
* License v3.0 only", or the "Server Side Public License, v 1".
8+
*/
9+
10+
package org.elasticsearch.painless;
11+
12+
/**
13+
* Checked exceptions are wrapped in {@link ClassValue}#getFromHashMap in Error
14+
* which leads to unexpected behavior in Painless. This class is used as a
15+
* workaround for that exception wrapping.
16+
*/
17+
public class PainlessWrappedException extends Error {
18+
19+
/**
20+
* Constructor.
21+
* @param cause The {@link Exception} cause.
22+
*/
23+
public PainlessWrappedException(final Exception cause) {
24+
super(cause);
25+
}
26+
}

modules/lang-painless/src/main/java/org/elasticsearch/painless/phase/PainlessUserTreeToIRTreePhase.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.painless.Location;
1313
import org.elasticsearch.painless.PainlessError;
1414
import org.elasticsearch.painless.PainlessExplainError;
15+
import org.elasticsearch.painless.PainlessWrappedException;
1516
import org.elasticsearch.painless.ScriptClassInfo;
1617
import org.elasticsearch.painless.ScriptClassInfo.MethodArgument;
1718
import org.elasticsearch.painless.ir.BinaryImplNode;
@@ -415,6 +416,7 @@ protected static void injectSandboxExceptions(FunctionNode irFunctionNode) {
415416

416417
for (Class<? extends Throwable> throwable : List.of(
417418
PainlessError.class,
419+
PainlessWrappedException.class,
418420
LinkageError.class,
419421
OutOfMemoryError.class,
420422
StackOverflowError.class,

modules/lang-painless/src/test/java/org/elasticsearch/painless/DefBootstrapTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,11 @@ public void testMegamorphic() throws Throwable {
139139
map.put("a", "b");
140140
assertEquals(2, (int) handle.invokeExact((Object) map));
141141

142-
final IllegalArgumentException iae = expectThrows(IllegalArgumentException.class, () -> {
142+
final PainlessWrappedException pwe = expectThrows(PainlessWrappedException.class, () -> {
143143
Integer.toString((int) handle.invokeExact(new Object()));
144144
});
145+
assertTrue(pwe.getCause() instanceof IllegalArgumentException);
146+
IllegalArgumentException iae = (IllegalArgumentException) pwe.getCause();
145147
assertEquals("dynamic method [java.lang.Object, size/0] not found", iae.getMessage());
146148
assertTrue("Does not fail inside ClassValue.computeValue()", Arrays.stream(iae.getStackTrace()).anyMatch(e -> {
147149
return e.getMethodName().equals("computeValue") && e.getClassName().startsWith("org.elasticsearch.painless.DefBootstrap$PIC$");

server/src/main/java/org/elasticsearch/action/support/replication/TransportBroadcastReplicationAction.java

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,13 @@
2222
import org.elasticsearch.action.support.broadcast.BroadcastShardOperationFailedException;
2323
import org.elasticsearch.client.internal.node.NodeClient;
2424
import org.elasticsearch.cluster.ClusterState;
25+
import org.elasticsearch.cluster.ProjectState;
2526
import org.elasticsearch.cluster.metadata.IndexMetadata;
2627
import org.elasticsearch.cluster.metadata.IndexNameExpressionResolver;
2728
import org.elasticsearch.cluster.metadata.ProjectMetadata;
2829
import org.elasticsearch.cluster.project.ProjectResolver;
29-
import org.elasticsearch.cluster.routing.IndexRoutingTable;
30-
import org.elasticsearch.cluster.routing.RoutingTable;
30+
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
31+
import org.elasticsearch.cluster.routing.OperationRouting;
3132
import org.elasticsearch.cluster.service.ClusterService;
3233
import org.elasticsearch.common.io.stream.Writeable;
3334
import org.elasticsearch.common.util.concurrent.EsExecutors;
@@ -39,6 +40,7 @@
3940

4041
import java.util.ArrayList;
4142
import java.util.Arrays;
43+
import java.util.Iterator;
4244
import java.util.List;
4345
import java.util.Map;
4446
import java.util.concurrent.Executor;
@@ -98,8 +100,9 @@ public void accept(ActionListener<Response> listener) {
98100
assert totalShardCopyCount == 0 && successShardCopyCount == 0 && allFailures.isEmpty() : "shouldn't call this twice";
99101

100102
final ClusterState clusterState = clusterService.state();
101-
final ProjectMetadata project = projectResolver.getProjectMetadata(clusterState);
102-
final List<ShardId> shards = shards(request, project, clusterState.routingTable(project.id()));
103+
final ProjectState projectState = projectResolver.getProjectState(clusterState);
104+
final ProjectMetadata project = projectState.metadata();
105+
final List<ShardId> shards = shards(request, projectState);
103106
final Map<String, IndexMetadata> indexMetadataByName = project.indices();
104107

105108
try (var refs = new RefCountingRunnable(() -> finish(listener))) {
@@ -185,17 +188,17 @@ protected void shardExecute(Task task, Request request, ShardId shardId, ActionL
185188
/**
186189
* @return all shard ids the request should run on
187190
*/
188-
protected List<ShardId> shards(Request request, ProjectMetadata project, RoutingTable indexRoutingTables) {
191+
protected List<ShardId> shards(Request request, ProjectState projectState) {
189192
assert Transports.assertNotTransportThread("may hit all the shards");
190193
List<ShardId> shardIds = new ArrayList<>();
191-
String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(project, request);
194+
195+
OperationRouting operationRouting = clusterService.operationRouting();
196+
197+
String[] concreteIndices = indexNameExpressionResolver.concreteIndexNames(projectState.metadata(), request);
192198
for (String index : concreteIndices) {
193-
IndexMetadata indexMetadata = project.indices().get(index);
194-
if (indexMetadata != null) {
195-
final IndexRoutingTable indexRoutingTable = indexRoutingTables.indicesRouting().get(index);
196-
for (int i = 0; i < indexRoutingTable.size(); i++) {
197-
shardIds.add(indexRoutingTable.shard(i).shardId());
198-
}
199+
Iterator<IndexShardRoutingTable> iterator = operationRouting.allWritableShards(projectState, index);
200+
while (iterator.hasNext()) {
201+
shardIds.add(iterator.next().shardId());
199202
}
200203
}
201204
return shardIds;

server/src/main/java/org/elasticsearch/cluster/routing/IndexRouting.java

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,18 @@ public void checkIndexSplitAllowed() {}
159159
* @param shardId shardId to which the current document is routed based on hashing
160160
* @return Updated shardId
161161
*/
162-
protected final int rerouteIfResharding(int shardId) {
162+
protected final int rerouteWritesIfResharding(int shardId) {
163+
return rerouteFromSplitTargetShard(shardId, IndexReshardingState.Split.TargetShardState.HANDOFF);
164+
}
165+
166+
protected final int rerouteSearchIfResharding(int shardId) {
167+
return rerouteFromSplitTargetShard(shardId, IndexReshardingState.Split.TargetShardState.SPLIT);
168+
}
169+
170+
private int rerouteFromSplitTargetShard(int shardId, IndexReshardingState.Split.TargetShardState minimumRequiredState) {
171+
assert indexReshardingMetadata == null || indexReshardingMetadata.isSplit() : "Index resharding state is not a split";
163172
if (indexReshardingMetadata != null && indexReshardingMetadata.getSplit().isTargetShard(shardId)) {
164-
assert indexReshardingMetadata.isSplit() : "Index resharding state is not a split";
165-
if (indexReshardingMetadata.getSplit()
166-
.targetStateAtLeast(shardId, IndexReshardingState.Split.TargetShardState.HANDOFF) == false) {
173+
if (indexReshardingMetadata.getSplit().targetStateAtLeast(shardId, minimumRequiredState) == false) {
167174
return indexReshardingMetadata.getSplit().sourceShard(shardId);
168175
}
169176
}
@@ -217,21 +224,21 @@ public int indexShard(String id, @Nullable String routing, XContentType sourceTy
217224
}
218225
checkRoutingRequired(id, routing);
219226
int shardId = shardId(id, routing);
220-
return rerouteIfResharding(shardId);
227+
return rerouteWritesIfResharding(shardId);
221228
}
222229

223230
@Override
224231
public int updateShard(String id, @Nullable String routing) {
225232
checkRoutingRequired(id, routing);
226233
int shardId = shardId(id, routing);
227-
return rerouteIfResharding(shardId);
234+
return rerouteWritesIfResharding(shardId);
228235
}
229236

230237
@Override
231238
public int deleteShard(String id, @Nullable String routing) {
232239
checkRoutingRequired(id, routing);
233240
int shardId = shardId(id, routing);
234-
return rerouteIfResharding(shardId);
241+
return rerouteWritesIfResharding(shardId);
235242
}
236243

237244
@Override
@@ -262,7 +269,7 @@ protected int shardId(String id, @Nullable String routing) {
262269

263270
@Override
264271
public void collectSearchShards(String routing, IntConsumer consumer) {
265-
consumer.accept(hashToShardId(effectiveRoutingToHash(routing)));
272+
consumer.accept(rerouteSearchIfResharding(hashToShardId(effectiveRoutingToHash(routing))));
266273
}
267274
}
268275

@@ -290,7 +297,7 @@ protected int shardId(String id, @Nullable String routing) {
290297
public void collectSearchShards(String routing, IntConsumer consumer) {
291298
int hash = effectiveRoutingToHash(routing);
292299
for (int i = 0; i < routingPartitionSize; i++) {
293-
consumer.accept(hashToShardId(hash + i));
300+
consumer.accept(rerouteSearchIfResharding(hashToShardId(hash + i)));
294301
}
295302
}
296303
}
@@ -339,7 +346,7 @@ public int indexShard(String id, @Nullable String routing, XContentType sourceTy
339346
checkNoRouting(routing);
340347
hash = hashSource(sourceType, source).buildHash(IndexRouting.ExtractFromSource::defaultOnEmpty);
341348
int shardId = hashToShardId(hash);
342-
return (rerouteIfResharding(shardId));
349+
return (rerouteWritesIfResharding(shardId));
343350
}
344351

345352
public String createId(XContentType sourceType, BytesReference source, byte[] suffix) {
@@ -480,14 +487,14 @@ public int updateShard(String id, @Nullable String routing) {
480487
public int deleteShard(String id, @Nullable String routing) {
481488
checkNoRouting(routing);
482489
int shardId = idToHash(id);
483-
return (rerouteIfResharding(shardId));
490+
return (rerouteWritesIfResharding(shardId));
484491
}
485492

486493
@Override
487494
public int getShard(String id, @Nullable String routing) {
488495
checkNoRouting(routing);
489496
int shardId = idToHash(id);
490-
return (rerouteIfResharding(shardId));
497+
return (rerouteWritesIfResharding(shardId));
491498
}
492499

493500
private void checkNoRouting(@Nullable String routing) {

0 commit comments

Comments
 (0)