|
27 | 27 | import org.apache.lucene.util.CollectionUtil; |
28 | 28 | import org.elasticsearch.ExceptionsHelper; |
29 | 29 | import org.elasticsearch.action.ActionListener; |
30 | | -import org.elasticsearch.action.support.IndicesOptions; |
| 30 | +import org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotRequest; |
31 | 31 | import org.elasticsearch.cluster.ClusterChangedEvent; |
32 | 32 | import org.elasticsearch.cluster.ClusterState; |
33 | 33 | import org.elasticsearch.cluster.ClusterStateApplier; |
|
78 | 78 | import java.util.List; |
79 | 79 | import java.util.Locale; |
80 | 80 | import java.util.Map; |
81 | | -import java.util.Objects; |
82 | 81 | import java.util.Optional; |
83 | 82 | import java.util.Set; |
84 | 83 | import java.util.concurrent.CopyOnWriteArrayList; |
|
92 | 91 | * <p> |
93 | 92 | * A typical snapshot creating process looks like this: |
94 | 93 | * <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> |
97 | 96 | * <li>When cluster state is updated |
98 | 97 | * the {@link #beginSnapshot(ClusterState, SnapshotsInProgress.Entry, boolean, CreateSnapshotListener)} method kicks in and initializes |
99 | 98 | * 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) { |
235 | 234 | * @param request snapshot request |
236 | 235 | * @param listener snapshot creation listener |
237 | 236 | */ |
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()); |
241 | 240 | validate(repositoryName, snapshotName); |
242 | 241 | final SnapshotId snapshotId = new SnapshotId(snapshotName, UUIDs.randomBase64UUID()); // new UUID for the snapshot |
243 | 242 | final RepositoryData repositoryData = repositoriesService.repository(repositoryName).getRepositoryData(); |
244 | 243 |
|
245 | | - clusterService.submitStateUpdateTask(request.cause(), new ClusterStateUpdateTask() { |
| 244 | + clusterService.submitStateUpdateTask("create_snapshot [" + snapshotName + ']', new ClusterStateUpdateTask() { |
246 | 245 |
|
247 | 246 | private SnapshotsInProgress.Entry newSnapshot = null; |
248 | 247 |
|
249 | 248 | @Override |
250 | 249 | public ClusterState execute(ClusterState currentState) { |
251 | | - validate(request, currentState); |
| 250 | + validate(repositoryName, snapshotName, currentState); |
252 | 251 | SnapshotDeletionsInProgress deletionsInProgress = currentState.custom(SnapshotDeletionsInProgress.TYPE); |
253 | 252 | if (deletionsInProgress != null && deletionsInProgress.hasDeletionsInProgress()) { |
254 | 253 | throw new ConcurrentSnapshotExecutionException(repositoryName, snapshotName, |
@@ -301,16 +300,16 @@ public TimeValue timeout() { |
301 | 300 | /** |
302 | 301 | * Validates snapshot request |
303 | 302 | * |
304 | | - * @param request snapshot request |
| 303 | + * @param repositoryName repository name |
| 304 | + * @param snapshotName snapshot name |
305 | 305 | * @param state current cluster state |
306 | 306 | */ |
307 | | - private void validate(SnapshotRequest request, ClusterState state) { |
| 307 | + private void validate(String repositoryName, String snapshotName, ClusterState state) { |
308 | 308 | 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); |
312 | 311 | } |
313 | | - validate(repository, request.snapshotName); |
| 312 | + validate(repositoryName, snapshotName); |
314 | 313 | } |
315 | 314 |
|
316 | 315 | private static void validate(final String repositoryName, final String snapshotName) { |
@@ -377,7 +376,7 @@ protected void doRun() { |
377 | 376 | logger.info("snapshot [{}] started", snapshot.snapshot()); |
378 | 377 | if (snapshot.indices().isEmpty()) { |
379 | 378 | // No indices in this snapshot - we are done |
380 | | - userCreateSnapshotListener.onResponse(); |
| 379 | + userCreateSnapshotListener.onResponse(snapshot.snapshot()); |
381 | 380 | endSnapshot(snapshot); |
382 | 381 | return; |
383 | 382 | } |
@@ -465,7 +464,7 @@ public void clusterStateProcessed(String source, ClusterState oldState, ClusterS |
465 | 464 | // for processing. If client wants to wait for the snapshot completion, it can register snapshot |
466 | 465 | // completion listener in this method. For the snapshot completion to work properly, the snapshot |
467 | 466 | // should still exist when listener is registered. |
468 | | - userCreateSnapshotListener.onResponse(); |
| 467 | + userCreateSnapshotListener.onResponse(snapshot.snapshot()); |
469 | 468 |
|
470 | 469 | // Now that snapshot completion listener is registered we can end the snapshot if needed |
471 | 470 | // 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 { |
1546 | 1545 |
|
1547 | 1546 | /** |
1548 | 1547 | * Called when snapshot has successfully started |
| 1548 | + * |
| 1549 | + * @param snapshot snapshot that was created |
1549 | 1550 | */ |
1550 | | - void onResponse(); |
| 1551 | + void onResponse(Snapshot snapshot); |
1551 | 1552 |
|
1552 | 1553 | /** |
1553 | 1554 | * Called if a snapshot operation couldn't start |
@@ -1577,186 +1578,4 @@ public interface SnapshotCompletionListener { |
1577 | 1578 |
|
1578 | 1579 | void onSnapshotFailure(Snapshot snapshot, Exception e); |
1579 | 1580 | } |
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 | | - } |
1761 | 1581 | } |
1762 | | - |
0 commit comments