1717
1818package org .apache .ignite .internal .processors .cache .persistence .snapshot ;
1919
20- import java .util .Collections ;
2120import javax .management .AttributeNotFoundException ;
2221import javax .management .DynamicMBean ;
2322import javax .management .MBeanException ;
2423import javax .management .ReflectionException ;
25- import org .apache .ignite .Ignite ;
26- import org .apache .ignite .IgniteCheckedException ;
27- import org .apache .ignite .IgniteDataStreamer ;
2824import org .apache .ignite .configuration .IgniteConfiguration ;
2925import org .apache .ignite .internal .IgniteEx ;
30- import org .apache .ignite .internal .IgniteInternalFuture ;
3126import org .apache .ignite .internal .TestRecordingCommunicationSpi ;
32- import org .apache .ignite .internal .util .distributed .SingleNodeMessage ;
33- import org .apache .ignite .internal .util .typedef .F ;
34- import org .apache .ignite .internal .util .typedef .G ;
35- import org .apache .ignite .lang .IgniteFuture ;
3627import org .apache .ignite .mxbean .SnapshotMXBean ;
3728import org .apache .ignite .testframework .GridTestUtils ;
3829import org .junit .Test ;
3930
40- import static org .apache .ignite .internal .processors .cache .persistence .snapshot .IgniteSnapshotManager .SNAPSHOT_METRICS ;
4131import static org .apache .ignite .internal .processors .cache .persistence .snapshot .SnapshotRestoreProcess .SNAPSHOT_RESTORE_METRICS ;
42- import static org .apache .ignite .internal .util .distributed .DistributedProcess .DistributedProcessType .RESTORE_CACHE_GROUP_SNAPSHOT_PREPARE ;
43- import static org .apache .ignite .internal .util .distributed .DistributedProcess .DistributedProcessType .RESTORE_CACHE_GROUP_SNAPSHOT_START ;
44- import static org .apache .ignite .testframework .GridTestUtils .assertContains ;
45- import static org .apache .ignite .testframework .GridTestUtils .assertThrowsAnyCause ;
46- import static org .apache .ignite .testframework .GridTestUtils .waitForCondition ;
47- import static org .junit .Assume .assumeFalse ;
4832
4933/**
5034 * Tests {@link SnapshotMXBean}.
@@ -59,55 +43,6 @@ public class IgniteSnapshotMXBeanTest extends AbstractSnapshotSelfTest {
5943 .setCommunicationSpi (new TestRecordingCommunicationSpi ());
6044 }
6145
62- /** @throws Exception If fails. */
63- @ Test
64- public void testCreateSnapshot () throws Exception {
65- IgniteEx ignite = startGridsWithCache (2 , dfltCacheCfg , CACHE_KEYS_RANGE );
66-
67- DynamicMBean snpMBean = metricRegistry (ignite .name (), null , SNAPSHOT_METRICS );
68-
69- assertEquals ("Snapshot end time must be undefined on first snapshot operation starts." ,
70- 0 , (long )getMetric ("LastSnapshotEndTime" , snpMBean ));
71-
72- SnapshotMXBean mxBean = getMxBean (ignite .name (), SNAPSHOT_GROUP , SnapshotMXBeanImpl .class , SnapshotMXBean .class );
73-
74- mxBean .createSnapshot (SNAPSHOT_NAME , "" );
75-
76- assertTrue ("Waiting for snapshot operation failed." ,
77- GridTestUtils .waitForCondition (() -> (long )getMetric ("LastSnapshotEndTime" , snpMBean ) > 0 , TIMEOUT ));
78-
79- if (!encryption ) {
80- mxBean .createIncrementalSnapshot (SNAPSHOT_NAME , "" );
81-
82- assertTrue (
83- "Waiting for incremental snapshot failed" ,
84- GridTestUtils .waitForCondition (() -> checkIncremental (ignite , SNAPSHOT_NAME , null , 1 ), TIMEOUT )
85- );
86- }
87-
88- stopAllGrids ();
89-
90- IgniteEx snp = startGridsFromSnapshot (2 , SNAPSHOT_NAME );
91-
92- assertSnapshotCacheKeys (snp .cache (dfltCacheCfg .getName ()));
93- }
94-
95- /** @throws Exception If fails. */
96- @ Test
97- public void testCancelSnapshot () throws Exception {
98- IgniteEx srv = startGridsWithCache (1 , dfltCacheCfg , CACHE_KEYS_RANGE );
99- IgniteEx startCli = startClientGrid (1 );
100- IgniteEx killCli = startClientGrid (2 );
101-
102- SnapshotMXBean mxBean = getMxBean (killCli .name (), SNAPSHOT_GROUP , SnapshotMXBeanImpl .class ,
103- SnapshotMXBean .class );
104-
105- doSnapshotCancellationTest (startCli ,
106- Collections .singletonList (srv ),
107- srv .cache (dfltCacheCfg .getName ()),
108- mxBean ::cancelSnapshot );
109- }
110-
11146 /** @throws Exception If fails. */
11247 @ Test
11348 public void testRestoreSnapshot () throws Exception {
@@ -128,219 +63,6 @@ public void testRestoreSnapshot() throws Exception {
12863 assertCacheKeys (ignite .cache (DEFAULT_CACHE_NAME ), CACHE_KEYS_RANGE );
12964 }
13065
131- /** @throws Exception If fails. */
132- @ Test
133- public void testRestoreIncrementalSnapshot () throws Exception {
134- assumeFalse ("https://issues.apache.org/jira/browse/IGNITE-17819" , encryption );
135-
136- IgniteEx ignite = startGridsWithCache (2 , CACHE_KEYS_RANGE , valueBuilder (), dfltCacheCfg );
137-
138- createAndCheckSnapshot (ignite , SNAPSHOT_NAME , null , TIMEOUT );
139-
140- try (IgniteDataStreamer <Integer , Object > ds = ignite .dataStreamer (dfltCacheCfg .getName ())) {
141- for (int i = CACHE_KEYS_RANGE ; i < 2 * CACHE_KEYS_RANGE ; i ++)
142- ds .addData (i , valueBuilder ().apply (i ));
143- }
144-
145- ignite .snapshot ().createIncrementalSnapshot (SNAPSHOT_NAME ).get (TIMEOUT );
146-
147- ignite .cache (dfltCacheCfg .getName ()).destroy ();
148-
149- awaitPartitionMapExchange ();
150-
151- DynamicMBean mReg0 = metricRegistry (grid (0 ).name (), null , SNAPSHOT_RESTORE_METRICS );
152- DynamicMBean mReg1 = metricRegistry (grid (1 ).name (), null , SNAPSHOT_RESTORE_METRICS );
153-
154- assertEquals (0 , (long )getMetric ("endTime" , mReg0 ));
155- assertEquals (0 , (long )getMetric ("endTime" , mReg1 ));
156-
157- getMxBean (ignite .name (), SNAPSHOT_GROUP , SnapshotMXBeanImpl .class , SnapshotMXBean .class )
158- .restoreSnapshot (SNAPSHOT_NAME , "" , "" , 1 );
159-
160- assertTrue (GridTestUtils .waitForCondition (() -> (long )getMetric ("endTime" , mReg0 ) > 0 , getTestTimeout ()));
161- assertTrue (GridTestUtils .waitForCondition (() -> (long )getMetric ("endTime" , mReg1 ) > 0 , getTestTimeout ()));
162-
163- assertCacheKeys (ignite .cache (DEFAULT_CACHE_NAME ), 2 * CACHE_KEYS_RANGE );
164- }
165-
166- /** @throws Exception If fails. */
167- @ Test
168- public void testCancelRestoreSnapshot () throws Exception {
169- IgniteEx ignite = startGridsWithSnapshot (2 , CACHE_KEYS_RANGE , false );
170- SnapshotMXBean mxBean = getMxBean (ignite .name (), SNAPSHOT_GROUP , SnapshotMXBeanImpl .class , SnapshotMXBean .class );
171- DynamicMBean mReg0 = metricRegistry (grid (0 ).name (), null , SNAPSHOT_RESTORE_METRICS );
172- DynamicMBean mReg1 = metricRegistry (grid (1 ).name (), null , SNAPSHOT_RESTORE_METRICS );
173-
174- assertEquals ("" , getMetric ("error" , mReg0 ));
175- assertEquals ("" , getMetric ("error" , mReg1 ));
176- assertEquals (0 , (long )getMetric ("endTime" , mReg0 ));
177- assertEquals (0 , (long )getMetric ("endTime" , mReg1 ));
178-
179- TestRecordingCommunicationSpi spi = TestRecordingCommunicationSpi .spi (grid (1 ));
180-
181- spi .blockMessages ((node , msg ) -> msg instanceof SingleNodeMessage &&
182- ((SingleNodeMessage <?>)msg ).type () == RESTORE_CACHE_GROUP_SNAPSHOT_PREPARE .ordinal ());
183-
184- IgniteFuture <Void > fut = ignite .snapshot ().restoreSnapshot (SNAPSHOT_NAME , null );
185-
186- spi .waitForBlocked ();
187-
188- IgniteInternalFuture <Boolean > interruptFut = GridTestUtils .runAsync (() -> {
189- try {
190- return GridTestUtils .waitForCondition (
191- () -> !"" .equals (getMetric ("error" , mReg0 )) && !"" .equals (getMetric ("error" , mReg1 )), TIMEOUT );
192- }
193- finally {
194- spi .stopBlock ();
195- }
196- });
197-
198- mxBean .cancelSnapshotRestore (SNAPSHOT_NAME );
199-
200- assertTrue (interruptFut .get ());
201-
202- String expErrMsg = "Operation has been canceled by the user." ;
203-
204- assertThrowsAnyCause (log , () -> fut .get (TIMEOUT ), IgniteCheckedException .class , expErrMsg );
205-
206- assertTrue ((long )getMetric ("endTime" , mReg0 ) > 0 );
207- assertTrue (((String )getMetric ("error" , mReg0 )).contains (expErrMsg ));
208-
209- // We use {@code waitForCondition} here and below since {@code fut} completeness guarantees that snapshot
210- // procedure is completed only on the initiator node. Remote nodes can not handle snapshot cancellation event yet.
211- assertTrue (waitForCondition (() -> (long )getMetric ("endTime" , mReg1 ) > 0 , getTestTimeout ()));
212- assertTrue (waitForCondition (() -> ((String )getMetric ("error" , mReg1 )).contains (expErrMsg ), getTestTimeout ()));
213-
214- assertNull (ignite .cache (DEFAULT_CACHE_NAME ));
215- }
216-
217- /** @throws Exception If fails. */
218- @ Test
219- public void testStatus () throws Exception {
220- IgniteEx srv = startGridsWithCache (2 , dfltCacheCfg , CACHE_KEYS_RANGE );
221-
222- checkSnapshotStatus (false , false , false , null );
223-
224- // Create full snapshot.
225- TestRecordingCommunicationSpi spi = TestRecordingCommunicationSpi .spi (grid (1 ));
226-
227- spi .blockMessages ((node , msg ) -> msg instanceof SingleNodeMessage );
228-
229- IgniteFuture <Void > fut = snp (srv ).createSnapshot (SNAPSHOT_NAME , null , false , onlyPrimary );
230-
231- spi .waitForBlocked ();
232-
233- checkSnapshotStatus (true , false , false , SNAPSHOT_NAME );
234-
235- spi .stopBlock ();
236-
237- fut .get (getTestTimeout ());
238-
239- checkSnapshot (SNAPSHOT_NAME , null );
240-
241- checkSnapshotStatus (false , false , false , null );
242-
243- // Create incremental snapshot.
244- // TODO: remove condition after resolving IGNITE-17819.
245- if (!encryption ) {
246- spi .blockMessages ((node , msg ) -> msg instanceof SingleNodeMessage );
247-
248- fut = srv .snapshot ().createIncrementalSnapshot (SNAPSHOT_NAME );
249-
250- spi .waitForBlocked ();
251-
252- checkSnapshotStatus (true , false , true , SNAPSHOT_NAME );
253-
254- spi .stopBlock ();
255-
256- fut .get (getTestTimeout ());
257-
258- checkSnapshotStatus (false , false , false , null );
259- }
260-
261- // Restore full snapshot.
262- srv .destroyCache (DEFAULT_CACHE_NAME );
263-
264- awaitPartitionMapExchange ();
265-
266- spi .blockMessages ((node , msg ) -> msg instanceof SingleNodeMessage
267- && ((SingleNodeMessage )msg ).type () == RESTORE_CACHE_GROUP_SNAPSHOT_PREPARE .ordinal ());
268-
269- fut = srv .snapshot ().restoreSnapshot (SNAPSHOT_NAME , F .asList (DEFAULT_CACHE_NAME ));
270-
271- spi .waitForBlocked ();
272-
273- checkSnapshotStatus (false , true , false , SNAPSHOT_NAME );
274-
275- spi .stopBlock ();
276-
277- fut .get (getTestTimeout ());
278-
279- checkSnapshotStatus (false , false , false , null );
280-
281- // Restore incremental snapshot.
282- // TODO: remove condition after resolving IGNITE-17819.
283- if (!encryption ) {
284- srv .destroyCache (DEFAULT_CACHE_NAME );
285-
286- awaitPartitionMapExchange ();
287-
288- spi .blockMessages ((node , msg ) -> msg instanceof SingleNodeMessage
289- && ((SingleNodeMessage )msg ).type () == RESTORE_CACHE_GROUP_SNAPSHOT_START .ordinal ());
290-
291- fut = srv .snapshot ().restoreSnapshot (SNAPSHOT_NAME , F .asList (DEFAULT_CACHE_NAME ), 1 );
292-
293- spi .waitForBlocked ();
294-
295- checkSnapshotStatus (false , true , true , SNAPSHOT_NAME );
296-
297- spi .stopBlock ();
298-
299- fut .get (getTestTimeout ());
300-
301- checkSnapshotStatus (false , false , false , null );
302- }
303- }
304-
305- /**
306- * @param isCreating {@code True} if create snapshot operation is in progress.
307- * @param isRestoring {@code True} if restore snapshot operation is in progress.
308- * @param isIncremental {@code True} if incremental snapshot operation.
309- * @param expName Expected snapshot name.
310- */
311- private void checkSnapshotStatus (boolean isCreating , boolean isRestoring , boolean isIncremental , String expName ) throws Exception {
312- assertTrue (waitForCondition (() -> G .allGrids ().stream ().allMatch (
313- ignite -> {
314- IgniteSnapshotManager mgr = ((IgniteEx )ignite ).context ().cache ().context ().snapshotMgr ();
315-
316- return isCreating == mgr .isSnapshotCreating () && isRestoring == mgr .isRestoring ();
317- }),
318- getTestTimeout ()));
319-
320- for (Ignite grid : G .allGrids ()) {
321- SnapshotMXBean bean = getMxBean (grid .name (), SNAPSHOT_GROUP , SnapshotMXBeanImpl .class , SnapshotMXBean .class );
322-
323- String status = bean .status ();
324-
325- if (!isCreating && !isRestoring ) {
326- assertContains (log , status , "There is no create or restore snapshot operation in progress" );
327-
328- continue ;
329- }
330-
331- if (isCreating )
332- assertContains (log , status , "Create snapshot operation is in progress" );
333- else
334- assertContains (log , status , "Restore snapshot operation is in progress" );
335-
336- assertContains (log , status , "name=" + expName );
337- assertContains (log , status , "incremental=" + isIncremental );
338-
339- if (isIncremental )
340- assertContains (log , status , "incrementIndex=1" );
341- }
342- }
343-
34466 /**
34567 * @param mBean Ignite snapshot restore MBean.
34668 * @param name Metric name.
0 commit comments