Skip to content

Commit

Permalink
Migrate to Java16 records (part 2) (#82914)
Browse files Browse the repository at this point in the history
Try to represent immutable data with Java records introduced in JEP 395

Convert only existing immutable classes, no "POJO with setters to a record" refactorings.
  • Loading branch information
arteam authored Jan 24, 2022
1 parent 6b84132 commit cce5ad2
Show file tree
Hide file tree
Showing 211 changed files with 1,243 additions and 2,500 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -341,19 +341,19 @@ protected static TaskId findTaskToRethrottle(String actionName, String descripti
list.rethrowFailures("Finding tasks to rethrottle");
List<TaskGroup> taskGroups = list.getTaskGroups()
.stream()
.filter(taskGroup -> taskGroup.getTaskInfo().getDescription().equals(description))
.filter(taskGroup -> taskGroup.taskInfo().description().equals(description))
.collect(Collectors.toList());
assertThat("tasks are left over from the last execution of this test", taskGroups, hasSize(lessThan(2)));
if (0 == taskGroups.size()) {
// The parent task hasn't started yet
continue;
}
TaskGroup taskGroup = taskGroups.get(0);
assertThat(taskGroup.getChildTasks(), empty());
assertThat(taskGroup.childTasks(), empty());
// check that the task initialized enough that it can rethrottle too.
Map<String, Object> statusMap = ((RawTaskStatus) taskGroup.getTaskInfo().getStatus()).toMap();
Map<String, Object> statusMap = ((RawTaskStatus) taskGroup.taskInfo().status()).toMap();
if (statusMap.get("batches").equals(1)) {
return taskGroup.getTaskInfo().getTaskId();
return taskGroup.taskInfo().taskId();
}
} while (System.nanoTime() - start < TimeUnit.SECONDS.toNanos(10));
throw new AssertionError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,11 +255,11 @@ public void onFailure(Exception e) {
assertThat(response.getTaskFailures(), empty());
assertThat(response.getNodeFailures(), empty());
assertThat(response.getTasks(), hasSize(1));
assertEquals(taskIdToRethrottle, response.getTasks().get(0).getTaskId());
assertThat(response.getTasks().get(0).getStatus(), instanceOf(RawTaskStatus.class));
assertEquals(taskIdToRethrottle, response.getTasks().get(0).taskId());
assertThat(response.getTasks().get(0).status(), instanceOf(RawTaskStatus.class));
assertEquals(
Float.toString(requestsPerSecond),
((RawTaskStatus) response.getTasks().get(0).getStatus()).toMap().get("requests_per_second").toString()
((RawTaskStatus) response.getTasks().get(0).status()).toMap().get("requests_per_second").toString()
);
assertTrue(taskFinished.await(10, TimeUnit.SECONDS));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ static void mockGetRoot(RestClient restClient) throws IOException {
Build.CURRENT.hash(),
Build.CURRENT.date(),
false,
Build.CURRENT.getQualifiedVersion()
Build.CURRENT.qualifiedVersion()
);

mockGetRoot(restClient, build, true);
Expand All @@ -220,7 +220,7 @@ static void mockGetRoot(RestClient restClient) throws IOException {
public static void mockGetRoot(RestClient restClient, Build build, boolean setProductHeader) throws IOException {
org.elasticsearch.action.main.MainResponse mainResp = new org.elasticsearch.action.main.MainResponse(
"node",
Version.fromString(build.getQualifiedVersion().replace("-SNAPSHOT", "")),
Version.fromString(build.qualifiedVersion().replace("-SNAPSHOT", "")),
new ClusterName("cluster"),
"uuid",
build
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,14 @@ public void testListTasks() throws IOException {
assertThat(response.getTasks().size(), greaterThanOrEqualTo(2));
boolean listTasksFound = false;
for (TaskGroup taskGroup : response.getTaskGroups()) {
org.elasticsearch.tasks.TaskInfo parent = taskGroup.getTaskInfo();
if ("cluster:monitor/tasks/lists".equals(parent.getAction())) {
assertThat(taskGroup.getChildTasks().size(), equalTo(1));
TaskGroup childGroup = taskGroup.getChildTasks().iterator().next();
assertThat(childGroup.getChildTasks().isEmpty(), equalTo(true));
org.elasticsearch.tasks.TaskInfo child = childGroup.getTaskInfo();
assertThat(child.getAction(), equalTo("cluster:monitor/tasks/lists[n]"));
assertThat(child.getParentTaskId(), equalTo(parent.getTaskId()));
org.elasticsearch.tasks.TaskInfo parent = taskGroup.taskInfo();
if ("cluster:monitor/tasks/lists".equals(parent.action())) {
assertThat(taskGroup.childTasks().size(), equalTo(1));
TaskGroup childGroup = taskGroup.childTasks().iterator().next();
assertThat(childGroup.childTasks().isEmpty(), equalTo(true));
org.elasticsearch.tasks.TaskInfo child = childGroup.taskInfo();
assertThat(child.action(), equalTo("cluster:monitor/tasks/lists[n]"));
assertThat(child.parentTaskId(), equalTo(parent.taskId()));
listTasksFound = true;
}
}
Expand Down Expand Up @@ -92,9 +92,9 @@ public void testGetValidTask() throws Exception {
assertTrue(taskResponse.isCompleted());
}
org.elasticsearch.tasks.TaskInfo info = taskResponse.getTaskInfo();
assertTrue(info.isCancellable());
assertEquals("reindex from [source1] to [dest]", info.getDescription());
assertEquals("indices:data/write/reindex", info.getAction());
assertTrue(info.cancellable());
assertEquals("reindex from [source1] to [dest]", info.description());
assertEquals("indices:data/write/reindex", info.action());
if (taskResponse.isCompleted() == false) {
assertBusy(checkTaskCompletionStatus(client(), taskId));
}
Expand All @@ -115,7 +115,7 @@ public void testCancelTasks() throws IOException {
org.elasticsearch.tasks.TaskInfo firstTask = listResponse.getTasks().get(0);
String node = listResponse.getPerNodeTasks().keySet().iterator().next();

CancelTasksRequest cancelTasksRequest = new CancelTasksRequest.Builder().withTaskId(new TaskId(node, firstTask.getId())).build();
CancelTasksRequest cancelTasksRequest = new CancelTasksRequest.Builder().withTaskId(new TaskId(node, firstTask.id())).build();
CancelTasksResponse response = execute(
cancelTasksRequest,
highLevelClient().tasks()::cancel,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,11 @@ public void onFailure(Exception e) {
highLevelClient()::updateByQueryRethrottleAsync
);
assertThat(response.getTasks(), hasSize(1));
assertEquals(taskIdToRethrottle, response.getTasks().get(0).getTaskId());
assertThat(response.getTasks().get(0).getStatus(), instanceOf(RawTaskStatus.class));
assertEquals(taskIdToRethrottle, response.getTasks().get(0).taskId());
assertThat(response.getTasks().get(0).status(), instanceOf(RawTaskStatus.class));
assertEquals(
Float.toString(requestsPerSecond),
((RawTaskStatus) response.getTasks().get(0).getStatus()).toMap().get("requests_per_second").toString()
((RawTaskStatus) response.getTasks().get(0).status()).toMap().get("requests_per_second").toString()
);
assertTrue(taskFinished.await(10, TimeUnit.SECONDS));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,19 @@ protected void assertInstances(
.collect(Collectors.toMap(org.elasticsearch.client.tasks.TaskInfo::getTaskId, Function.identity()));
for (TaskInfo ti : sTasks) {
org.elasticsearch.client.tasks.TaskInfo taskInfo = cTasksMap.get(
new org.elasticsearch.client.tasks.TaskId(ti.getTaskId().getNodeId(), ti.getTaskId().getId())
new org.elasticsearch.client.tasks.TaskId(ti.taskId().getNodeId(), ti.taskId().getId())
);
assertEquals(ti.getAction(), taskInfo.getAction());
assertEquals(ti.getDescription(), taskInfo.getDescription());
assertEquals(new HashMap<>(ti.getHeaders()), new HashMap<>(taskInfo.getHeaders()));
assertEquals(ti.getType(), taskInfo.getType());
assertEquals(ti.getStartTime(), taskInfo.getStartTime());
assertEquals(ti.getRunningTimeNanos(), taskInfo.getRunningTimeNanos());
assertEquals(ti.isCancellable(), taskInfo.isCancellable());
assertEquals(ti.isCancelled(), taskInfo.isCancelled());
assertEquals(ti.getParentTaskId().getNodeId(), taskInfo.getParentTaskId().getNodeId());
assertEquals(ti.getParentTaskId().getId(), taskInfo.getParentTaskId().getId());
FakeTaskStatus status = (FakeTaskStatus) ti.getStatus();
assertEquals(ti.action(), taskInfo.getAction());
assertEquals(ti.description(), taskInfo.getDescription());
assertEquals(new HashMap<>(ti.headers()), new HashMap<>(taskInfo.getHeaders()));
assertEquals(ti.type(), taskInfo.getType());
assertEquals(ti.startTime(), taskInfo.getStartTime());
assertEquals(ti.runningTimeNanos(), taskInfo.getRunningTimeNanos());
assertEquals(ti.cancellable(), taskInfo.isCancellable());
assertEquals(ti.cancelled(), taskInfo.isCancelled());
assertEquals(ti.parentTaskId().getNodeId(), taskInfo.getParentTaskId().getNodeId());
assertEquals(ti.parentTaskId().getId(), taskInfo.getParentTaskId().getId());
FakeTaskStatus status = (FakeTaskStatus) ti.status();
assertEquals(status.code, taskInfo.getStatus().get("code"));
assertEquals(status.status, taskInfo.getStatus().get("status"));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,12 @@ private String getElasticUrl(
baseUrl,
pluginId,
platform,
Build.CURRENT.getQualifiedVersion()
Build.CURRENT.qualifiedVersion()
);
if (urlExists(platformUrl)) {
return platformUrl;
}
return String.format(Locale.ROOT, "%s/%s-%s.zip", baseUrl, pluginId, Build.CURRENT.getQualifiedVersion());
return String.format(Locale.ROOT, "%s/%s-%s.zip", baseUrl, pluginId, Build.CURRENT.qualifiedVersion());
}

private String nonReleaseUrl(final String hostname, final Version version, final String stagingHash, final String pluginId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ static String formatPermission(Permission permission) {
* Extract a unique set of permissions from the plugin's policy file. Each permission is formatted for output to users.
*/
public static Set<String> getPermissionDescriptions(PluginPolicyInfo pluginPolicyInfo, Path tmpDir) throws IOException {
Set<Permission> allPermissions = new HashSet<>(PolicyUtil.getPolicyPermissions(null, pluginPolicyInfo.policy, tmpDir));
for (URL jar : pluginPolicyInfo.jars) {
Set<Permission> jarPermissions = PolicyUtil.getPolicyPermissions(jar, pluginPolicyInfo.policy, tmpDir);
Set<Permission> allPermissions = new HashSet<>(PolicyUtil.getPolicyPermissions(null, pluginPolicyInfo.policy(), tmpDir));
for (URL jar : pluginPolicyInfo.jars()) {
Set<Permission> jarPermissions = PolicyUtil.getPolicyPermissions(jar, pluginPolicyInfo.policy(), tmpDir);
allPermissions.addAll(jarPermissions);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ void jarHellCheck(PluginInfo candidateInfo, Path candidate, Path pluginsDir, Pat

public void testOfficialPlugin() throws Exception {
String url = "https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-icu/analysis-icu-"
+ Build.CURRENT.getQualifiedVersion()
+ Build.CURRENT.qualifiedVersion()
+ ".zip";
assertInstallPluginFromUrl("analysis-icu", url, null, false);
}
Expand All @@ -985,7 +985,7 @@ public void testOfficialPluginSnapshot() throws Exception {
Locale.ROOT,
"https://snapshots.elastic.co/%s-abc123/downloads/elasticsearch-plugins/analysis-icu/analysis-icu-%s.zip",
Version.CURRENT,
Build.CURRENT.getQualifiedVersion()
Build.CURRENT.qualifiedVersion()
);
assertInstallPluginFromUrl("analysis-icu", url, "abc123", true);
}
Expand All @@ -995,7 +995,7 @@ public void testInstallReleaseBuildOfPluginOnSnapshotBuild() {
Locale.ROOT,
"https://snapshots.elastic.co/%s-abc123/downloads/elasticsearch-plugins/analysis-icu/analysis-icu-%s.zip",
Version.CURRENT,
Build.CURRENT.getQualifiedVersion()
Build.CURRENT.qualifiedVersion()
);
// attempting to install a release build of a plugin (no staging ID) on a snapshot build should throw a user exception
final UserException e = expectThrows(
Expand All @@ -1013,7 +1013,7 @@ public void testOfficialPluginStaging() throws Exception {
String url = "https://staging.elastic.co/"
+ Version.CURRENT
+ "-abc123/downloads/elasticsearch-plugins/analysis-icu/analysis-icu-"
+ Build.CURRENT.getQualifiedVersion()
+ Build.CURRENT.qualifiedVersion()
+ ".zip";
assertInstallPluginFromUrl("analysis-icu", url, "abc123", false);
}
Expand All @@ -1022,7 +1022,7 @@ public void testOfficialPlatformPlugin() throws Exception {
String url = "https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-icu/analysis-icu-"
+ Platforms.PLATFORM_NAME
+ "-"
+ Build.CURRENT.getQualifiedVersion()
+ Build.CURRENT.qualifiedVersion()
+ ".zip";
assertInstallPluginFromUrl("analysis-icu", url, null, false);
}
Expand All @@ -1033,7 +1033,7 @@ public void testOfficialPlatformPluginSnapshot() throws Exception {
"https://snapshots.elastic.co/%s-abc123/downloads/elasticsearch-plugins/analysis-icu/analysis-icu-%s-%s.zip",
Version.CURRENT,
Platforms.PLATFORM_NAME,
Build.CURRENT.getQualifiedVersion()
Build.CURRENT.qualifiedVersion()
);
assertInstallPluginFromUrl("analysis-icu", url, "abc123", true);
}
Expand All @@ -1044,7 +1044,7 @@ public void testOfficialPlatformPluginStaging() throws Exception {
+ "-abc123/downloads/elasticsearch-plugins/analysis-icu/analysis-icu-"
+ Platforms.PLATFORM_NAME
+ "-"
+ Build.CURRENT.getQualifiedVersion()
+ Build.CURRENT.qualifiedVersion()
+ ".zip";
assertInstallPluginFromUrl("analysis-icu", url, "abc123", false);
}
Expand Down Expand Up @@ -1084,7 +1084,7 @@ public void testMavenChecksumWithoutFilename() throws Exception {

public void testOfficialChecksumWithoutFilename() throws Exception {
String url = "https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-icu/analysis-icu-"
+ Build.CURRENT.getQualifiedVersion()
+ Build.CURRENT.qualifiedVersion()
+ ".zip";
MessageDigest digest = MessageDigest.getInstance("SHA-512");
UserException e = expectThrows(
Expand All @@ -1097,7 +1097,7 @@ public void testOfficialChecksumWithoutFilename() throws Exception {

public void testOfficialShaMissing() throws Exception {
String url = "https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-icu/analysis-icu-"
+ Build.CURRENT.getQualifiedVersion()
+ Build.CURRENT.qualifiedVersion()
+ ".zip";
MessageDigest digest = MessageDigest.getInstance("SHA-1");
UserException e = expectThrows(
Expand Down Expand Up @@ -1130,7 +1130,7 @@ public void testMavenShaMissing() {

public void testInvalidShaFileMissingFilename() throws Exception {
String url = "https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-icu/analysis-icu-"
+ Build.CURRENT.getQualifiedVersion()
+ Build.CURRENT.qualifiedVersion()
+ ".zip";
MessageDigest digest = MessageDigest.getInstance("SHA-512");
UserException e = expectThrows(
Expand All @@ -1143,7 +1143,7 @@ public void testInvalidShaFileMissingFilename() throws Exception {

public void testInvalidShaFileMismatchFilename() throws Exception {
String url = "https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-icu/analysis-icu-"
+ Build.CURRENT.getQualifiedVersion()
+ Build.CURRENT.qualifiedVersion()
+ ".zip";
MessageDigest digest = MessageDigest.getInstance("SHA-512");
UserException e = expectThrows(
Expand All @@ -1155,7 +1155,7 @@ public void testInvalidShaFileMismatchFilename() throws Exception {
null,
false,
".sha512",
checksumAndString(digest, " repository-s3-" + Build.CURRENT.getQualifiedVersion() + ".zip"),
checksumAndString(digest, " repository-s3-" + Build.CURRENT.qualifiedVersion() + ".zip"),
null,
(b, p) -> null
)
Expand All @@ -1166,7 +1166,7 @@ public void testInvalidShaFileMismatchFilename() throws Exception {

public void testInvalidShaFileContainingExtraLine() throws Exception {
String url = "https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-icu/analysis-icu-"
+ Build.CURRENT.getQualifiedVersion()
+ Build.CURRENT.qualifiedVersion()
+ ".zip";
MessageDigest digest = MessageDigest.getInstance("SHA-512");
UserException e = expectThrows(
Expand All @@ -1178,7 +1178,7 @@ public void testInvalidShaFileContainingExtraLine() throws Exception {
null,
false,
".sha512",
checksumAndString(digest, " analysis-icu-" + Build.CURRENT.getQualifiedVersion() + ".zip\nfoobar"),
checksumAndString(digest, " analysis-icu-" + Build.CURRENT.qualifiedVersion() + ".zip\nfoobar"),
null,
(b, p) -> null
)
Expand All @@ -1189,7 +1189,7 @@ public void testInvalidShaFileContainingExtraLine() throws Exception {

public void testSha512Mismatch() {
String url = "https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-icu/analysis-icu-"
+ Build.CURRENT.getQualifiedVersion()
+ Build.CURRENT.qualifiedVersion()
+ ".zip";
UserException e = expectThrows(
UserException.class,
Expand All @@ -1200,7 +1200,7 @@ public void testSha512Mismatch() {
null,
false,
".sha512",
bytes -> "foobar analysis-icu-" + Build.CURRENT.getQualifiedVersion() + ".zip",
bytes -> "foobar analysis-icu-" + Build.CURRENT.qualifiedVersion() + ".zip",
null,
(b, p) -> null
)
Expand Down Expand Up @@ -1234,7 +1234,7 @@ public void testPublicKeyIdMismatchToExpectedPublicKeyId() throws Exception {
final String url = "https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-icu/"
+ icu
+ "-"
+ Build.CURRENT.getQualifiedVersion()
+ Build.CURRENT.qualifiedVersion()
+ ".zip";
final MessageDigest digest = MessageDigest.getInstance("SHA-512");
/*
Expand Down Expand Up @@ -1269,7 +1269,7 @@ public void testFailedSignatureVerification() throws Exception {
final String url = "https://artifacts.elastic.co/downloads/elasticsearch-plugins/analysis-icu/"
+ icu
+ "-"
+ Build.CURRENT.getQualifiedVersion()
+ Build.CURRENT.qualifiedVersion()
+ ".zip";
final MessageDigest digest = MessageDigest.getInstance("SHA-512");
/*
Expand Down
Loading

0 comments on commit cce5ad2

Please sign in to comment.