Skip to content

Commit 12835bb

Browse files
committed
remove if
1 parent 9151c28 commit 12835bb

File tree

1 file changed

+68
-80
lines changed

1 file changed

+68
-80
lines changed

server/src/main/java/org/elasticsearch/snapshots/SnapshotsService.java

Lines changed: 68 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,84 +1334,74 @@ private void triggerSnapshotsPendingDeletions(final ClusterState state) {
13341334

13351335
for (SnapshotDeletionsPending.Entry snapshot : snapshotDeletionsPending.entries()) {
13361336
final SnapshotId snapshotId = snapshot.getSnapshotId();
1337+
boolean triggered = false;
1338+
1339+
if (currentRestores.contains(snapshotId)) {
1340+
logger.trace("snapshot to delete [{}] is being restored, waiting for restore to complete", snapshotId);
1341+
continue;
1342+
} else if (currentClones.contains(snapshotId)) {
1343+
logger.trace("snapshot to delete [{}] is being cloned, waiting for cloning to complete", snapshotId);
1344+
continue;
1345+
} else if (currentDeletions.contains(snapshotId)) {
1346+
logger.trace("snapshot to delete [{}] is already queued", snapshotId);
1347+
continue;
1348+
}
1349+
1350+
Optional<RepositoryMetadata> optionalRepository;
1351+
if (RepositoryData.MISSING_UUID.equals(snapshot.getRepositoryUuid()) == false) {
1352+
// the snapshot waiting to be deleted references a repository with a known uuid,
1353+
// let's try to find this repository among the existing ones first
1354+
optionalRepository = repositories.repositories()
1355+
.stream()
1356+
.filter(repo -> Objects.equals(repo.uuid(), snapshot.getRepositoryUuid()))
1357+
.findFirst();
1358+
if (optionalRepository.isEmpty()) {
1359+
// there is no existing repository matching the uuid,
1360+
// let's try to find the repository by name among the existing ones that have no uuid
1361+
optionalRepository = repositories.repositories()
1362+
.stream()
1363+
.filter(repo -> Objects.equals(repo.uuid(), RepositoryData.MISSING_UUID))
1364+
.filter(repo -> Objects.equals(repo.name(), snapshot.getRepositoryName()))
1365+
.findFirst();
1366+
}
1367+
} else {
1368+
// the snapshot waiting to be deleted does not references a repository with a known uuid,
1369+
// let's try to find the repository by name among the existing ones, in the hope that
1370+
// the snapshot will be found there.
1371+
optionalRepository = repositories.repositories()
1372+
.stream()
1373+
.filter(repo -> Objects.equals(repo.name(), snapshot.getRepositoryName()))
1374+
.findFirst();
1375+
}
13371376

1338-
// early add to avoid doing too much work on successive cluster state updates
1339-
if (ongoingSnapshotsDeletions.add(snapshotId)) {
1340-
boolean triggered = false;
1341-
try {
1342-
if (currentRestores.contains(snapshotId)) {
1343-
logger.trace("snapshot to delete [{}] is being restored, waiting for restore to complete", snapshotId);
1344-
continue;
1345-
} else if (currentClones.contains(snapshotId)) {
1346-
logger.trace("snapshot to delete [{}] is being cloned, waiting for cloning to complete", snapshotId);
1347-
continue;
1348-
} else if (currentDeletions.contains(snapshotId)) {
1349-
logger.trace("snapshot to delete [{}] is already queued", snapshotId);
1350-
continue;
1351-
}
1352-
1353-
Optional<RepositoryMetadata> optionalRepository;
1354-
if (RepositoryData.MISSING_UUID.equals(snapshot.getRepositoryUuid()) == false) {
1355-
// the snapshot waiting to be deleted references a repository with a known uuid,
1356-
// let's try to find this repository among the existing ones first
1357-
optionalRepository = repositories.repositories()
1358-
.stream()
1359-
.filter(repo -> Objects.equals(repo.uuid(), snapshot.getRepositoryUuid()))
1360-
.findFirst();
1361-
if (optionalRepository.isEmpty()) {
1362-
// there is no existing repository matching the uuid,
1363-
// let's try to find the repository by name among the existing ones that have no uuid
1364-
optionalRepository = repositories.repositories()
1365-
.stream()
1366-
.filter(repo -> Objects.equals(repo.uuid(), RepositoryData.MISSING_UUID))
1367-
.filter(repo -> Objects.equals(repo.name(), snapshot.getRepositoryName()))
1368-
.findFirst();
1369-
}
1370-
} else {
1371-
// the snapshot waiting to be deleted does not references a repository with a known uuid,
1372-
// let's try to find the repository by name among the existing ones, in the hope that
1373-
// the snapshot will be found there.
1374-
optionalRepository = repositories.repositories()
1375-
.stream()
1376-
.filter(repo -> Objects.equals(repo.name(), snapshot.getRepositoryName()))
1377-
.findFirst();
1378-
}
1379-
1380-
if (optionalRepository.isEmpty()) {
1381-
logger.debug(
1382-
"repository [{}/{}] not found, cannot delete pending snapshot [{}] created at {}",
1383-
snapshot.getRepositoryName(),
1384-
snapshot.getRepositoryUuid(),
1385-
snapshotId,
1386-
Instant.ofEpochMilli(snapshot.getCreationTime()).atZone(ZoneOffset.UTC)
1387-
);
1388-
continue;
1389-
}
1390-
1391-
final RepositoryMetadata repository = optionalRepository.get();
1392-
if (repository.settings().getAsBoolean(READONLY_SETTING_KEY, false)) {
1393-
logger.debug(
1394-
"repository [{}/{}] is read-only, cannot delete pending snapshot [{}] created at {}",
1395-
repository.name(),
1396-
repository.uuid(),
1397-
snapshotId,
1398-
Instant.ofEpochMilli(snapshot.getCreationTime()).atZone(ZoneOffset.UTC)
1399-
);
1400-
continue;
1401-
}
1377+
if (optionalRepository.isEmpty()) {
1378+
logger.debug(
1379+
"repository [{}/{}] not found, cannot delete pending snapshot [{}] created at {}",
1380+
snapshot.getRepositoryName(),
1381+
snapshot.getRepositoryUuid(),
1382+
snapshotId,
1383+
Instant.ofEpochMilli(snapshot.getCreationTime()).atZone(ZoneOffset.UTC)
1384+
);
1385+
continue;
1386+
}
14021387

1403-
// should we add some throttling to not always retry
1404-
final boolean added = snapshotsToDelete.computeIfAbsent(repository, r -> new HashSet<>()).add(snapshotId);
1405-
assert ongoingSnapshotsDeletions.contains(snapshotId) : snapshotId;
1406-
assert added : snapshotId;
1388+
final RepositoryMetadata repository = optionalRepository.get();
1389+
if (repository.settings().getAsBoolean(READONLY_SETTING_KEY, false)) {
1390+
logger.debug(
1391+
"repository [{}/{}] is read-only, cannot delete pending snapshot [{}] created at {}",
1392+
repository.name(),
1393+
repository.uuid(),
1394+
snapshotId,
1395+
Instant.ofEpochMilli(snapshot.getCreationTime()).atZone(ZoneOffset.UTC)
1396+
);
1397+
continue;
1398+
}
14071399

1408-
logger.trace("triggering snapshot deletion for [{}]", snapshotId);
1409-
triggered = true;
1410-
} finally {
1411-
if (triggered == false) {
1412-
ongoingSnapshotsDeletions.remove(snapshotId);
1413-
}
1414-
}
1400+
// should we add some throttling to not always retry?
1401+
if (ongoingSnapshotsDeletions.add(snapshotId)) {
1402+
logger.trace("triggering snapshot deletion for [{}]", snapshotId);
1403+
final boolean added = snapshotsToDelete.computeIfAbsent(repository, r -> new HashSet<>()).add(snapshotId);
1404+
assert added : snapshotId;
14151405
}
14161406
}
14171407
snapshotsToDelete.forEach(
@@ -1519,12 +1509,12 @@ public ClusterState execute(ClusterState currentState) {
15191509

15201510
@Override
15211511
public void clusterStateProcessed(String source, ClusterState oldState, ClusterState newState) {
1522-
ongoingSnapshotsDeletions.removeAll(missingSnapshots);
1512+
missingSnapshots.forEach(ongoingSnapshotsDeletions::remove);
15231513
}
15241514

15251515
@Override
15261516
public void onFailure(String source, Exception e) {
1527-
ongoingSnapshotsDeletions.removeAll(missingSnapshots);
1517+
missingSnapshots.forEach(ongoingSnapshotsDeletions::remove);
15281518
}
15291519
}
15301520
);
@@ -2780,9 +2770,7 @@ protected SnapshotDeletionsInProgress filterDeletions(SnapshotDeletionsInProgres
27802770
@Nullable
27812771
@Override
27822772
protected SnapshotDeletionsPending filterPendingDeletions(@Nullable SnapshotDeletionsPending pendingDeletions) {
2783-
return pendingDeletions != null
2784-
? pendingDeletions.withRemovedSnapshots(deleteEntry.getSnapshots())
2785-
: null;
2773+
return pendingDeletions != null ? pendingDeletions.withRemovedSnapshots(deleteEntry.getSnapshots()) : null;
27862774
}
27872775

27882776
@Override

0 commit comments

Comments
 (0)