Skip to content

Commit caab456

Browse files
Simplify Snapshot Create Request Handling (#37464)
* The internal create request is absolutely redundant, the only difference to the transport request is that we resolved the snapshot name when moving from the transport to the internal version * Removed it and passed the transport request into the snapshot service instead * nicer way of resolve snapshot name in callback
1 parent 44b3089 commit caab456

File tree

2 files changed

+23
-215
lines changed

2 files changed

+23
-215
lines changed

server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/create/TransportCreateSnapshotAction.java

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,33 +74,22 @@ protected ClusterBlockException checkBlock(CreateSnapshotRequest request, Cluste
7474
@Override
7575
protected void masterOperation(final CreateSnapshotRequest request, ClusterState state,
7676
final ActionListener<CreateSnapshotResponse> listener) {
77-
final String snapshotName = indexNameExpressionResolver.resolveDateMathExpression(request.snapshot());
78-
SnapshotsService.SnapshotRequest snapshotRequest =
79-
new SnapshotsService.SnapshotRequest(request.repository(), snapshotName, "create_snapshot [" + snapshotName + "]")
80-
.indices(request.indices())
81-
.indicesOptions(request.indicesOptions())
82-
.partial(request.partial())
83-
.settings(request.settings())
84-
.includeGlobalState(request.includeGlobalState())
85-
.masterNodeTimeout(request.masterNodeTimeout());
86-
snapshotsService.createSnapshot(snapshotRequest, new SnapshotsService.CreateSnapshotListener() {
77+
snapshotsService.createSnapshot(request, new SnapshotsService.CreateSnapshotListener() {
8778
@Override
88-
public void onResponse() {
79+
public void onResponse(Snapshot snapshotCreated) {
8980
if (request.waitForCompletion()) {
9081
snapshotsService.addListener(new SnapshotsService.SnapshotCompletionListener() {
9182
@Override
9283
public void onSnapshotCompletion(Snapshot snapshot, SnapshotInfo snapshotInfo) {
93-
if (snapshot.getRepository().equals(request.repository()) &&
94-
snapshot.getSnapshotId().getName().equals(snapshotName)) {
84+
if (snapshotCreated.equals(snapshot)) {
9585
listener.onResponse(new CreateSnapshotResponse(snapshotInfo));
9686
snapshotsService.removeListener(this);
9787
}
9888
}
9989

10090
@Override
10191
public void onSnapshotFailure(Snapshot snapshot, Exception e) {
102-
if (snapshot.getRepository().equals(request.repository()) &&
103-
snapshot.getSnapshotId().getName().equals(snapshotName)) {
92+
if (snapshotCreated.equals(snapshot)) {
10493
listener.onFailure(e);
10594
snapshotsService.removeListener(this);
10695
}

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

Lines changed: 19 additions & 200 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
import org.apache.lucene.util.CollectionUtil;
2828
import org.elasticsearch.ExceptionsHelper;
2929
import org.elasticsearch.action.ActionListener;
30-
import org.elasticsearch.action.support.IndicesOptions;
30+
import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest;
3131
import org.elasticsearch.cluster.ClusterChangedEvent;
3232
import org.elasticsearch.cluster.ClusterState;
3333
import org.elasticsearch.cluster.ClusterStateApplier;
@@ -78,7 +78,6 @@
7878
import java.util.List;
7979
import java.util.Locale;
8080
import java.util.Map;
81-
import java.util.Objects;
8281
import java.util.Optional;
8382
import java.util.Set;
8483
import java.util.concurrent.CopyOnWriteArrayList;
@@ -92,8 +91,8 @@
9291
* <p>
9392
* A typical snapshot creating process looks like this:
9493
* <ul>
95-
* <li>On the master node the {@link #createSnapshot(SnapshotRequest, CreateSnapshotListener)}
96-
* is called and makes sure that no snapshots is currently running and registers the new snapshot in cluster state</li>
94+
* <li>On the master node the {@link #createSnapshot(CreateSnapshotRequest, CreateSnapshotListener)} is called and makes sure that
95+
* no snapshot is currently running and registers the new snapshot in cluster state</li>
9796
* <li>When cluster state is updated
9897
* the {@link #beginSnapshot(ClusterState, SnapshotsInProgress.Entry, boolean, CreateSnapshotListener)} method kicks in and initializes
9998
* the snapshot in the repository and then populates list of shards that needs to be snapshotted in cluster state</li>
@@ -235,20 +234,20 @@ public List<SnapshotInfo> currentSnapshots(final String repositoryName) {
235234
* @param request snapshot request
236235
* @param listener snapshot creation listener
237236
*/
238-
public void createSnapshot(final SnapshotRequest request, final CreateSnapshotListener listener) {
239-
final String repositoryName = request.repositoryName;
240-
final String snapshotName = request.snapshotName;
237+
public void createSnapshot(final CreateSnapshotRequest request, final CreateSnapshotListener listener) {
238+
final String repositoryName = request.repository();
239+
final String snapshotName = indexNameExpressionResolver.resolveDateMathExpression(request.snapshot());
241240
validate(repositoryName, snapshotName);
242241
final SnapshotId snapshotId = new SnapshotId(snapshotName, UUIDs.randomBase64UUID()); // new UUID for the snapshot
243242
final RepositoryData repositoryData = repositoriesService.repository(repositoryName).getRepositoryData();
244243

245-
clusterService.submitStateUpdateTask(request.cause(), new ClusterStateUpdateTask() {
244+
clusterService.submitStateUpdateTask("create_snapshot [" + snapshotName + ']', new ClusterStateUpdateTask() {
246245

247246
private SnapshotsInProgress.Entry newSnapshot = null;
248247

249248
@Override
250249
public ClusterState execute(ClusterState currentState) {
251-
validate(request, currentState);
250+
validate(repositoryName, snapshotName, currentState);
252251
SnapshotDeletionsInProgress deletionsInProgress = currentState.custom(SnapshotDeletionsInProgress.TYPE);
253252
if (deletionsInProgress != null && deletionsInProgress.hasDeletionsInProgress()) {
254253
throw new ConcurrentSnapshotExecutionException(repositoryName, snapshotName,
@@ -301,16 +300,16 @@ public TimeValue timeout() {
301300
/**
302301
* Validates snapshot request
303302
*
304-
* @param request snapshot request
303+
* @param repositoryName repository name
304+
* @param snapshotName snapshot name
305305
* @param state current cluster state
306306
*/
307-
private void validate(SnapshotRequest request, ClusterState state) {
307+
private void validate(String repositoryName, String snapshotName, ClusterState state) {
308308
RepositoriesMetaData repositoriesMetaData = state.getMetaData().custom(RepositoriesMetaData.TYPE);
309-
final String repository = request.repositoryName;
310-
if (repositoriesMetaData == null || repositoriesMetaData.repository(repository) == null) {
311-
throw new RepositoryMissingException(repository);
309+
if (repositoriesMetaData == null || repositoriesMetaData.repository(repositoryName) == null) {
310+
throw new RepositoryMissingException(repositoryName);
312311
}
313-
validate(repository, request.snapshotName);
312+
validate(repositoryName, snapshotName);
314313
}
315314

316315
private static void validate(final String repositoryName, final String snapshotName) {
@@ -377,7 +376,7 @@ protected void doRun() {
377376
logger.info("snapshot [{}] started", snapshot.snapshot());
378377
if (snapshot.indices().isEmpty()) {
379378
// No indices in this snapshot - we are done
380-
userCreateSnapshotListener.onResponse();
379+
userCreateSnapshotListener.onResponse(snapshot.snapshot());
381380
endSnapshot(snapshot);
382381
return;
383382
}
@@ -465,7 +464,7 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS
465464
// for processing. If client wants to wait for the snapshot completion, it can register snapshot
466465
// completion listener in this method. For the snapshot completion to work properly, the snapshot
467466
// should still exist when listener is registered.
468-
userCreateSnapshotListener.onResponse();
467+
userCreateSnapshotListener.onResponse(snapshot.snapshot());
469468

470469
// Now that snapshot completion listener is registered we can end the snapshot if needed
471470
// We should end snapshot only if 1) we didn't accept it for processing (which happens when there
@@ -1546,8 +1545,10 @@ public interface CreateSnapshotListener {
15461545

15471546
/**
15481547
* Called when snapshot has successfully started
1548+
*
1549+
* @param snapshot snapshot that was created
15491550
*/
1550-
void onResponse();
1551+
void onResponse(Snapshot snapshot);
15511552

15521553
/**
15531554
* Called if a snapshot operation couldn't start
@@ -1577,186 +1578,4 @@ public interface SnapshotCompletionListener {
15771578

15781579
void onSnapshotFailure(Snapshot snapshot, Exception e);
15791580
}
1580-
1581-
/**
1582-
* Snapshot creation request
1583-
*/
1584-
public static class SnapshotRequest {
1585-
1586-
private final String cause;
1587-
1588-
private final String repositoryName;
1589-
1590-
private final String snapshotName;
1591-
1592-
private String[] indices;
1593-
1594-
private IndicesOptions indicesOptions = IndicesOptions.strictExpandOpen();
1595-
1596-
private boolean partial;
1597-
1598-
private Settings settings;
1599-
1600-
private boolean includeGlobalState;
1601-
1602-
private TimeValue masterNodeTimeout;
1603-
1604-
/**
1605-
* Constructs new snapshot creation request
1606-
*
1607-
* @param repositoryName repository name
1608-
* @param snapshotName snapshot name
1609-
* @param cause cause for snapshot operation
1610-
*/
1611-
public SnapshotRequest(final String repositoryName, final String snapshotName, final String cause) {
1612-
this.repositoryName = Objects.requireNonNull(repositoryName);
1613-
this.snapshotName = Objects.requireNonNull(snapshotName);
1614-
this.cause = Objects.requireNonNull(cause);
1615-
}
1616-
1617-
/**
1618-
* Sets the list of indices to be snapshotted
1619-
*
1620-
* @param indices list of indices
1621-
* @return this request
1622-
*/
1623-
public SnapshotRequest indices(String[] indices) {
1624-
this.indices = indices;
1625-
return this;
1626-
}
1627-
1628-
/**
1629-
* Sets repository-specific snapshot settings
1630-
*
1631-
* @param settings snapshot settings
1632-
* @return this request
1633-
*/
1634-
public SnapshotRequest settings(Settings settings) {
1635-
this.settings = settings;
1636-
return this;
1637-
}
1638-
1639-
/**
1640-
* Set to true if global state should be stored as part of the snapshot
1641-
*
1642-
* @param includeGlobalState true if global state should be stored as part of the snapshot
1643-
* @return this request
1644-
*/
1645-
public SnapshotRequest includeGlobalState(boolean includeGlobalState) {
1646-
this.includeGlobalState = includeGlobalState;
1647-
return this;
1648-
}
1649-
1650-
/**
1651-
* Sets master node timeout
1652-
*
1653-
* @param masterNodeTimeout master node timeout
1654-
* @return this request
1655-
*/
1656-
public SnapshotRequest masterNodeTimeout(TimeValue masterNodeTimeout) {
1657-
this.masterNodeTimeout = masterNodeTimeout;
1658-
return this;
1659-
}
1660-
1661-
/**
1662-
* Sets the indices options
1663-
*
1664-
* @param indicesOptions indices options
1665-
* @return this request
1666-
*/
1667-
public SnapshotRequest indicesOptions(IndicesOptions indicesOptions) {
1668-
this.indicesOptions = indicesOptions;
1669-
return this;
1670-
}
1671-
1672-
/**
1673-
* Set to true if partial snapshot should be allowed
1674-
*
1675-
* @param partial true if partial snapshots should be allowed
1676-
* @return this request
1677-
*/
1678-
public SnapshotRequest partial(boolean partial) {
1679-
this.partial = partial;
1680-
return this;
1681-
}
1682-
1683-
/**
1684-
* Returns cause for snapshot operation
1685-
*
1686-
* @return cause for snapshot operation
1687-
*/
1688-
public String cause() {
1689-
return cause;
1690-
}
1691-
1692-
/**
1693-
* Returns the repository name
1694-
*/
1695-
public String repositoryName() {
1696-
return repositoryName;
1697-
}
1698-
1699-
/**
1700-
* Returns the snapshot name
1701-
*/
1702-
public String snapshotName() {
1703-
return snapshotName;
1704-
}
1705-
1706-
/**
1707-
* Returns the list of indices to be snapshotted
1708-
*
1709-
* @return the list of indices
1710-
*/
1711-
public String[] indices() {
1712-
return indices;
1713-
}
1714-
1715-
/**
1716-
* Returns indices options
1717-
*
1718-
* @return indices options
1719-
*/
1720-
public IndicesOptions indicesOptions() {
1721-
return indicesOptions;
1722-
}
1723-
1724-
/**
1725-
* Returns repository-specific settings for the snapshot operation
1726-
*
1727-
* @return repository-specific settings
1728-
*/
1729-
public Settings settings() {
1730-
return settings;
1731-
}
1732-
1733-
/**
1734-
* Returns true if global state should be stored as part of the snapshot
1735-
*
1736-
* @return true if global state should be stored as part of the snapshot
1737-
*/
1738-
public boolean includeGlobalState() {
1739-
return includeGlobalState;
1740-
}
1741-
1742-
/**
1743-
* Returns true if partial snapshot should be allowed
1744-
*
1745-
* @return true if partial snapshot should be allowed
1746-
*/
1747-
public boolean partial() {
1748-
return partial;
1749-
}
1750-
1751-
/**
1752-
* Returns master node timeout
1753-
*
1754-
* @return master node timeout
1755-
*/
1756-
public TimeValue masterNodeTimeout() {
1757-
return masterNodeTimeout;
1758-
}
1759-
1760-
}
17611581
}
1762-

0 commit comments

Comments
 (0)