Skip to content

Commit c4f4d9e

Browse files
committed
Remove obsolete fields from PutStoredScriptRequest
1 parent 8baba58 commit c4f4d9e

File tree

6 files changed

+32
-43
lines changed

6 files changed

+32
-43
lines changed

server/src/main/java/org/elasticsearch/TransportVersions.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ static TransportVersion def(int id) {
206206
public static final TransportVersion ESQL_SERIALIZE_SOURCE_FUNCTIONS_WARNINGS = def(9_016_0_00);
207207
public static final TransportVersion ESQL_DRIVER_NODE_DESCRIPTION = def(9_017_0_00);
208208
public static final TransportVersion MULTI_PROJECT = def(9_018_0_00);
209+
public static final TransportVersion STORED_SCRIPT_CONTENT_LENGTH = def(9_019_0_00);
209210

210211
/*
211212
* STOP! READ THIS FIRST! No, really,

server/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/PutStoredScriptRequest.java

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,17 @@
99

1010
package org.elasticsearch.action.admin.cluster.storedscripts;
1111

12+
import org.elasticsearch.TransportVersions;
1213
import org.elasticsearch.action.ActionRequestValidationException;
1314
import org.elasticsearch.action.support.master.AcknowledgedRequest;
1415
import org.elasticsearch.common.Strings;
16+
import org.elasticsearch.common.bytes.BytesArray;
1517
import org.elasticsearch.common.bytes.BytesReference;
1618
import org.elasticsearch.common.io.stream.StreamInput;
1719
import org.elasticsearch.common.io.stream.StreamOutput;
1820
import org.elasticsearch.common.xcontent.XContentHelper;
1921
import org.elasticsearch.core.Nullable;
2022
import org.elasticsearch.core.TimeValue;
21-
import org.elasticsearch.core.UpdateForV9;
2223
import org.elasticsearch.script.StoredScriptSource;
2324
import org.elasticsearch.xcontent.ToXContentFragment;
2425
import org.elasticsearch.xcontent.XContentBuilder;
@@ -37,27 +38,21 @@ public class PutStoredScriptRequest extends AcknowledgedRequest<PutStoredScriptR
3738
@Nullable
3839
private final String context;
3940

40-
/*
41-
* [NOTE: unused fields #117566]
42-
* As of #117566 (8.18) the content and xContentType fields are basically unused, except that we use content().length() for some
43-
* validation. However, in earlier 8.x versions they did at least influence the output of toString(). That means in 9.x we can replace
44-
* these fields with an int representing the original content length once the 9.x transport protocol can diverge from the 8.x one. For
45-
* BwC with 8.18 we can simply send any BytesReference of the appropriate length.
46-
*/
47-
48-
@UpdateForV9(owner = UpdateForV9.Owner.CORE_INFRA) // see [NOTE: unused fields #117566]
49-
private final BytesReference content;
50-
51-
@UpdateForV9(owner = UpdateForV9.Owner.CORE_INFRA) // see [NOTE: unused fields #117566]
52-
private final XContentType xContentType;
41+
private final int contentLength;
5342

5443
private final StoredScriptSource source;
5544

5645
public PutStoredScriptRequest(StreamInput in) throws IOException {
5746
super(in);
5847
id = in.readOptionalString();
59-
content = in.readBytesReference();
60-
xContentType = in.readEnum(XContentType.class);
48+
if (in.getTransportVersion().onOrAfter(TransportVersions.STORED_SCRIPT_CONTENT_LENGTH)) {
49+
contentLength = in.readVInt();
50+
} else {
51+
BytesReference content = in.readBytesReference();
52+
contentLength = content.length();
53+
54+
in.readEnum(XContentType.class); // and drop
55+
}
6156
context = in.readOptionalString();
6257
source = new StoredScriptSource(in);
6358
}
@@ -67,15 +62,13 @@ public PutStoredScriptRequest(
6762
TimeValue ackTimeout,
6863
@Nullable String id,
6964
@Nullable String context,
70-
BytesReference content,
71-
XContentType xContentType,
65+
int contentLength,
7266
StoredScriptSource source
7367
) {
7468
super(masterNodeTimeout, ackTimeout);
7569
this.id = id;
7670
this.context = context;
77-
this.content = Objects.requireNonNull(content);
78-
this.xContentType = Objects.requireNonNull(xContentType);
71+
this.contentLength = contentLength;
7972
this.source = Objects.requireNonNull(source);
8073
}
8174

@@ -100,12 +93,8 @@ public String context() {
10093
return context;
10194
}
10295

103-
public BytesReference content() {
104-
return content;
105-
}
106-
107-
public XContentType xContentType() {
108-
return xContentType;
96+
public int contentLength() {
97+
return contentLength;
10998
}
11099

111100
public StoredScriptSource source() {
@@ -116,8 +105,13 @@ public StoredScriptSource source() {
116105
public void writeTo(StreamOutput out) throws IOException {
117106
super.writeTo(out);
118107
out.writeOptionalString(id);
119-
out.writeBytesReference(content);
120-
XContentHelper.writeTo(out, xContentType);
108+
if (out.getTransportVersion().onOrAfter(TransportVersions.STORED_SCRIPT_CONTENT_LENGTH)) {
109+
out.writeVInt(contentLength);
110+
} else {
111+
// generate a bytes reference of the correct size (the content isn't actually used in 8.18)
112+
out.writeBytesReference(new BytesArray(new byte[contentLength]));
113+
XContentHelper.writeTo(out, XContentType.JSON); // value not actually used by 8.18
114+
}
121115
out.writeOptionalString(context);
122116
source.writeTo(out);
123117
}

server/src/main/java/org/elasticsearch/rest/action/admin/cluster/RestPutStoredScriptAction.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,7 @@ public RestChannelConsumer prepareRequest(RestRequest request, NodeClient client
5454
getAckTimeout(request),
5555
request.param("id"),
5656
request.param("context"),
57-
content,
58-
request.getXContentType(),
57+
content.length(),
5958
StoredScriptSource.parse(content, xContentType)
6059
);
6160
return channel -> client.execute(

server/src/main/java/org/elasticsearch/script/ScriptService.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -687,12 +687,12 @@ public void putStoredScript(
687687
PutStoredScriptRequest request,
688688
ActionListener<AcknowledgedResponse> listener
689689
) {
690-
if (request.content().length() > maxSizeInBytes) {
690+
if (request.contentLength() > maxSizeInBytes) {
691691
throw new IllegalArgumentException(
692692
"exceeded max allowed stored script size in bytes ["
693693
+ maxSizeInBytes
694694
+ "] with size ["
695-
+ request.content().length()
695+
+ request.contentLength()
696696
+ "] for script ["
697697
+ request.id()
698698
+ "]"

server/src/test/java/org/elasticsearch/action/admin/cluster/storedscripts/PutStoredScriptRequestTests.java

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

1010
package org.elasticsearch.action.admin.cluster.storedscripts;
1111

12-
import org.elasticsearch.common.bytes.BytesArray;
1312
import org.elasticsearch.common.bytes.BytesReference;
1413
import org.elasticsearch.common.io.stream.BytesStreamOutput;
1514
import org.elasticsearch.common.io.stream.StreamInput;
@@ -22,6 +21,8 @@
2221
import java.io.IOException;
2322
import java.util.Collections;
2423

24+
import static org.hamcrest.Matchers.equalTo;
25+
2526
public class PutStoredScriptRequestTests extends ESTestCase {
2627

2728
public void testSerialization() throws IOException {
@@ -30,18 +31,15 @@ public void testSerialization() throws IOException {
3031
TEST_REQUEST_TIMEOUT,
3132
"bar",
3233
"context",
33-
new BytesArray("{}"),
34-
XContentType.JSON,
34+
0,
3535
new StoredScriptSource("foo", "bar", Collections.emptyMap())
3636
);
3737

38-
assertEquals(XContentType.JSON, storedScriptRequest.xContentType());
3938
try (BytesStreamOutput output = new BytesStreamOutput()) {
4039
storedScriptRequest.writeTo(output);
4140

4241
try (StreamInput in = output.bytes().streamInput()) {
4342
PutStoredScriptRequest serialized = new PutStoredScriptRequest(in);
44-
assertEquals(XContentType.JSON, serialized.xContentType());
4543
assertEquals(storedScriptRequest.id(), serialized.id());
4644
assertEquals(storedScriptRequest.context(), serialized.context());
4745
}
@@ -62,8 +60,7 @@ public void testToXContent() throws IOException {
6260
TEST_REQUEST_TIMEOUT,
6361
"test1",
6462
null,
65-
expectedRequestBody,
66-
xContentType,
63+
expectedRequestBody.length(),
6764
StoredScriptSource.parse(expectedRequestBody, xContentType)
6865
);
6966

@@ -73,7 +70,6 @@ public void testToXContent() throws IOException {
7370
requestBuilder.endObject();
7471

7572
BytesReference actualRequestBody = BytesReference.bytes(requestBuilder);
76-
77-
assertEquals(expectedRequestBody, actualRequestBody);
73+
assertThat(actualRequestBody.length(), equalTo(expectedRequestBody.length()));
7874
}
7975
}

test/framework/src/main/java/org/elasticsearch/action/admin/cluster/storedscripts/StoredScriptIntegTestUtils.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,7 @@ public static PutStoredScriptRequest newPutStoredScriptTestRequest(String id, By
3939
TEST_REQUEST_TIMEOUT,
4040
id,
4141
null,
42-
jsonContent,
43-
XContentType.JSON,
42+
jsonContent.length(),
4443
StoredScriptSource.parse(jsonContent, XContentType.JSON)
4544
);
4645
}

0 commit comments

Comments
 (0)