denis-chudov commented on a change in pull request #9037: URL: https://github.com/apache/ignite/pull/9037#discussion_r622195648
########## File path: modules/core/src/test/java/org/apache/ignite/internal/processors/localtask/DurableBackgroundTasksProcessorSelfTest.java ########## @@ -0,0 +1,403 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.processors.localtask; + +import java.util.Map; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.configuration.DataRegionConfiguration; +import org.apache.ignite.configuration.DataStorageConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.failure.StopNodeFailureHandler; +import org.apache.ignite.internal.IgniteEx; +import org.apache.ignite.internal.IgniteInternalFuture; +import org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager; +import org.apache.ignite.internal.processors.cache.persistence.metastorage.MetaStorage; +import org.apache.ignite.internal.processors.cache.persistence.metastorage.pendingtask.DurableBackgroundTask; +import org.apache.ignite.internal.processors.localtask.DurableBackgroundTaskState.State; +import org.apache.ignite.internal.util.future.GridFutureAdapter; +import org.apache.ignite.internal.util.lang.IgniteThrowableFunction; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.jetbrains.annotations.Nullable; +import org.junit.Test; + +import static org.apache.ignite.cluster.ClusterState.ACTIVE; +import static org.apache.ignite.cluster.ClusterState.INACTIVE; +import static org.apache.ignite.internal.processors.cache.persistence.metastorage.pendingtask.DurableBackgroundTaskResult.complete; +import static org.apache.ignite.internal.processors.cache.persistence.metastorage.pendingtask.DurableBackgroundTaskResult.restart; +import static org.apache.ignite.internal.processors.localtask.DurableBackgroundTaskState.State.COMPLETED; +import static org.apache.ignite.internal.processors.localtask.DurableBackgroundTaskState.State.INIT; +import static org.apache.ignite.internal.processors.localtask.DurableBackgroundTaskState.State.STARTED; +import static org.apache.ignite.internal.processors.localtask.DurableBackgroundTasksProcessor.metaStorageKey; +import static org.apache.ignite.testframework.GridTestUtils.assertThrows; +import static org.apache.ignite.testframework.GridTestUtils.getFieldValue; + +/** + * Class for testing the {@link DurableBackgroundTasksProcessor}. + */ +public class DurableBackgroundTasksProcessorSelfTest extends GridCommonAbstractTest { + /** {@inheritDoc} */ + @Override protected void beforeTest() throws Exception { + super.beforeTest(); + + stopAllGrids(); + cleanPersistenceDir(); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + super.afterTest(); + + stopAllGrids(); + cleanPersistenceDir(); + } + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { + return super.getConfiguration(igniteInstanceName) + .setFailureHandler(new StopNodeFailureHandler()) + .setDataStorageConfiguration( + new DataStorageConfiguration() + .setDefaultDataRegionConfiguration(new DataRegionConfiguration().setPersistenceEnabled(true)) + ); + } + + /** + * Checking the correctness of work (without saving to the MetaStorage) of the task in normal mode. + * + * @throws Exception If failed. + */ + @Test + public void testSimpleTaskExecutionWithoutMetaStorage() throws Exception { + checkSimpleTaskExecute(false); + } + + /** + * Checking the correctness of work (with saving to the MetaStorage) of the task in normal mode. + * + * @throws Exception If failed. + */ + @Test + public void testSimpleTaskExecutionWithMetaStorage() throws Exception { + checkSimpleTaskExecute(true); + } + + /** + * Checking the correctness of restarting the task without MetaStorage. + * + * @throws Exception If failed. + */ + @Test + public void testRestartTaskExecutionWithoutMetaStorage() throws Exception { + checkRestartTaskExecute(false); + } + + /** + * Checking the correctness of restarting the task with MetaStorage. + * + * @throws Exception If failed. + */ + @Test + public void testRestartTaskExecutionWithMetaStorage() throws Exception { + checkRestartTaskExecute(true); + } + + /** + * Checking the correctness of cancelling the task. + * + * @throws Exception If failed. + */ + @Test + public void testCancelTaskExecution() throws Exception { + IgniteEx n = startGrid(0); + n.cluster().state(ACTIVE); + + SimpleTask t = new SimpleTask("t"); + IgniteInternalFuture<Void> execAsyncFut = execAsync(n, t, false); + + t.onExecFut.get(getTestTimeout()); Review comment: Isn't `getTestTimeout()` too big for this case? I suggest to replace it with another, small timeout. ########## File path: modules/core/src/test/java/org/apache/ignite/internal/processors/localtask/ObservingCheckpointListener.java ########## @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.processors.localtask; + +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointListener; +import org.apache.ignite.internal.util.future.GridFutureAdapter; +import org.apache.ignite.internal.util.lang.IgniteThrowableConsumer; + +/** + * Observer is a checkpoint listener. + * Consumers will be reset after a single use. + */ +public class ObservingCheckpointListener implements CheckpointListener { + /** On mark checkpoint begin consumer. */ + volatile IgniteThrowableConsumer<Context> onMarkCheckpointBeginConsumer; + + /** After checkpoint end consumer. */ + volatile IgniteThrowableConsumer<Context> afterCheckpointEndConsumer; + + /** {@inheritDoc} */ + @Override public void onMarkCheckpointBegin(Context ctx) throws IgniteCheckedException { + IgniteThrowableConsumer<Context> consumer = onMarkCheckpointBeginConsumer; + + if (consumer != null) { + onMarkCheckpointBeginConsumer = null; + + consumer.accept(ctx); + } + } + + /** {@inheritDoc} */ + @Override public void onCheckpointBegin(Context ctx) throws IgniteCheckedException { Review comment: `throws IgniteCheckedException` is not needed here ########## File path: modules/core/src/test/java/org/apache/ignite/internal/processors/localtask/ObservingCheckpointListener.java ########## @@ -0,0 +1,120 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.processors.localtask; + +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointListener; +import org.apache.ignite.internal.util.future.GridFutureAdapter; +import org.apache.ignite.internal.util.lang.IgniteThrowableConsumer; + +/** + * Observer is a checkpoint listener. + * Consumers will be reset after a single use. + */ +public class ObservingCheckpointListener implements CheckpointListener { + /** On mark checkpoint begin consumer. */ + volatile IgniteThrowableConsumer<Context> onMarkCheckpointBeginConsumer; + + /** After checkpoint end consumer. */ + volatile IgniteThrowableConsumer<Context> afterCheckpointEndConsumer; + + /** {@inheritDoc} */ + @Override public void onMarkCheckpointBegin(Context ctx) throws IgniteCheckedException { + IgniteThrowableConsumer<Context> consumer = onMarkCheckpointBeginConsumer; + + if (consumer != null) { + onMarkCheckpointBeginConsumer = null; + + consumer.accept(ctx); + } + } + + /** {@inheritDoc} */ + @Override public void onCheckpointBegin(Context ctx) throws IgniteCheckedException { + // No-op. + } + + /** {@inheritDoc} */ + @Override public void beforeCheckpointBegin(Context ctx) throws IgniteCheckedException { Review comment: `throws IgniteCheckedException` is not needed here ########## File path: modules/core/src/test/java/org/apache/ignite/internal/processors/localtask/DurableBackgroundTasksProcessorSelfTest.java ########## @@ -0,0 +1,403 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.ignite.internal.processors.localtask; + +import java.util.Map; +import org.apache.ignite.IgniteCheckedException; +import org.apache.ignite.configuration.DataRegionConfiguration; +import org.apache.ignite.configuration.DataStorageConfiguration; +import org.apache.ignite.configuration.IgniteConfiguration; +import org.apache.ignite.failure.StopNodeFailureHandler; +import org.apache.ignite.internal.IgniteEx; +import org.apache.ignite.internal.IgniteInternalFuture; +import org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager; +import org.apache.ignite.internal.processors.cache.persistence.metastorage.MetaStorage; +import org.apache.ignite.internal.processors.cache.persistence.metastorage.pendingtask.DurableBackgroundTask; +import org.apache.ignite.internal.processors.localtask.DurableBackgroundTaskState.State; +import org.apache.ignite.internal.util.future.GridFutureAdapter; +import org.apache.ignite.internal.util.lang.IgniteThrowableFunction; +import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest; +import org.jetbrains.annotations.Nullable; +import org.junit.Test; + +import static org.apache.ignite.cluster.ClusterState.ACTIVE; +import static org.apache.ignite.cluster.ClusterState.INACTIVE; +import static org.apache.ignite.internal.processors.cache.persistence.metastorage.pendingtask.DurableBackgroundTaskResult.complete; +import static org.apache.ignite.internal.processors.cache.persistence.metastorage.pendingtask.DurableBackgroundTaskResult.restart; +import static org.apache.ignite.internal.processors.localtask.DurableBackgroundTaskState.State.COMPLETED; +import static org.apache.ignite.internal.processors.localtask.DurableBackgroundTaskState.State.INIT; +import static org.apache.ignite.internal.processors.localtask.DurableBackgroundTaskState.State.STARTED; +import static org.apache.ignite.internal.processors.localtask.DurableBackgroundTasksProcessor.metaStorageKey; +import static org.apache.ignite.testframework.GridTestUtils.assertThrows; +import static org.apache.ignite.testframework.GridTestUtils.getFieldValue; + +/** + * Class for testing the {@link DurableBackgroundTasksProcessor}. + */ +public class DurableBackgroundTasksProcessorSelfTest extends GridCommonAbstractTest { + /** {@inheritDoc} */ + @Override protected void beforeTest() throws Exception { + super.beforeTest(); + + stopAllGrids(); + cleanPersistenceDir(); + } + + /** {@inheritDoc} */ + @Override protected void afterTest() throws Exception { + super.afterTest(); + + stopAllGrids(); + cleanPersistenceDir(); + } + + /** {@inheritDoc} */ + @Override protected IgniteConfiguration getConfiguration(String igniteInstanceName) throws Exception { + return super.getConfiguration(igniteInstanceName) + .setFailureHandler(new StopNodeFailureHandler()) + .setDataStorageConfiguration( + new DataStorageConfiguration() + .setDefaultDataRegionConfiguration(new DataRegionConfiguration().setPersistenceEnabled(true)) + ); + } + + /** + * Checking the correctness of work (without saving to the MetaStorage) of the task in normal mode. + * + * @throws Exception If failed. + */ + @Test + public void testSimpleTaskExecutionWithoutMetaStorage() throws Exception { + checkSimpleTaskExecute(false); + } + + /** + * Checking the correctness of work (with saving to the MetaStorage) of the task in normal mode. + * + * @throws Exception If failed. + */ + @Test + public void testSimpleTaskExecutionWithMetaStorage() throws Exception { + checkSimpleTaskExecute(true); + } + + /** + * Checking the correctness of restarting the task without MetaStorage. + * + * @throws Exception If failed. + */ + @Test + public void testRestartTaskExecutionWithoutMetaStorage() throws Exception { + checkRestartTaskExecute(false); + } + + /** + * Checking the correctness of restarting the task with MetaStorage. + * + * @throws Exception If failed. + */ + @Test + public void testRestartTaskExecutionWithMetaStorage() throws Exception { + checkRestartTaskExecute(true); + } + + /** + * Checking the correctness of cancelling the task. + * + * @throws Exception If failed. + */ + @Test + public void testCancelTaskExecution() throws Exception { + IgniteEx n = startGrid(0); + n.cluster().state(ACTIVE); + + SimpleTask t = new SimpleTask("t"); + IgniteInternalFuture<Void> execAsyncFut = execAsync(n, t, false); + + t.onExecFut.get(getTestTimeout()); + checkStateAndMetaStorage(n, t, STARTED, false); + assertFalse(execAsyncFut.isDone()); + + n.cluster().state(INACTIVE); + + t.onExecFut.get(getTestTimeout()); + t.taskFut.onDone(complete(null)); + + execAsyncFut.get(getTestTimeout()); + } + + /** + * Check that the task will be restarted after restarting the node. + * + * @throws Exception If failed. + */ + @Test + public void testRestartTaskAfterRestartNode() throws Exception { + IgniteEx n = startGrid(0); + n.cluster().state(ACTIVE); + + SimpleTask t = new SimpleTask("t"); + execAsync(n, t, true); + + t.onExecFut.get(getTestTimeout()); + checkStateAndMetaStorage(n, t, STARTED, true); + t.taskFut.onDone(restart(null)); + + stopAllGrids(); + + n = startGrid(0); + n.cluster().state(ACTIVE); + + t = ((SimpleTask)tasks(n).get(t.name()).task()); + + t.onExecFut.get(getTestTimeout()); + checkStateAndMetaStorage(n, t, STARTED, true); + t.taskFut.onDone(complete(null)); + } + + /** + * Check that the task will be restarted correctly. + * + * @param save Save to MetaStorage. + * @throws Exception If failed. + */ + private void checkRestartTaskExecute(boolean save) throws Exception { + IgniteEx n = startGrid(0); + n.cluster().state(ACTIVE); + + ObservingCheckpointListener checkpointLsnr = null; + + if (save) + dbMgr(n).addCheckpointListener(checkpointLsnr = new ObservingCheckpointListener()); Review comment: `checkpointLsnr ` is not used outside of `if (save) {` block below, maybe we will delete this `if`? ########## File path: modules/core/src/main/java/org/apache/ignite/internal/cache/query/index/sorted/DurableBackgroundCleanupIndexTreeTask.java ########## @@ -98,12 +110,99 @@ public DurableBackgroundCleanupIndexTreeTask( } /** {@inheritDoc} */ - @Override public String shortName() { + @Override public String name() { return "DROP_SQL_INDEX-" + schemaName + "." + idxName + "-" + id; } /** {@inheritDoc} */ - @Override public void execute(GridKernalContext ctx) { + @Override public IgniteInternalFuture<DurableBackgroundTaskResult> executeAsync(GridKernalContext ctx) { + log = ctx.log(this.getClass()); + + assert worker == null; + + GridFutureAdapter<DurableBackgroundTaskResult> fut = new GridFutureAdapter<>(); + + worker = new GridWorker( + ctx.igniteInstanceName(), + "async-durable-background-task-executor-" + name(), + log + ) { + /** {@inheritDoc} */ + @Override protected void body() throws InterruptedException, IgniteInterruptedCheckedException { Review comment: `InterruptedException` and `IgniteInterruptedCheckedException` are not needed here. ########## File path: modules/core/src/main/java/org/apache/ignite/internal/cache/query/index/sorted/DurableBackgroundCleanupIndexTreeTask.java ########## @@ -98,12 +110,99 @@ public DurableBackgroundCleanupIndexTreeTask( } /** {@inheritDoc} */ - @Override public String shortName() { + @Override public String name() { return "DROP_SQL_INDEX-" + schemaName + "." + idxName + "-" + id; } /** {@inheritDoc} */ - @Override public void execute(GridKernalContext ctx) { + @Override public IgniteInternalFuture<DurableBackgroundTaskResult> executeAsync(GridKernalContext ctx) { + log = ctx.log(this.getClass()); + + assert worker == null; + + GridFutureAdapter<DurableBackgroundTaskResult> fut = new GridFutureAdapter<>(); + + worker = new GridWorker( + ctx.igniteInstanceName(), + "async-durable-background-task-executor-" + name(), + log + ) { + /** {@inheritDoc} */ + @Override protected void body() throws InterruptedException, IgniteInterruptedCheckedException { + try { + execute(ctx); + + worker = null; + + fut.onDone(DurableBackgroundTaskResult.complete(null)); + } + catch (Throwable t) { + worker = null; + + fut.onDone(DurableBackgroundTaskResult.restart(t)); + } + } + }; + + new IgniteThread(worker).start(); + + return fut; + } Review comment: Seems that this method could be common for any tasks, shouldn't we move it to DurableBackgroundTasksProcessor? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
