tkalkirill commented on code in PR #815: URL: https://github.com/apache/ignite-3/pull/815#discussion_r883270678
########## modules/page-memory/src/main/java/org/apache/ignite/internal/pagememory/persistence/store/LongOperationAsyncExecutor.java: ########## @@ -0,0 +1,118 @@ +/* + * 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.pagememory.persistence.store; + +import static org.apache.ignite.internal.util.IgniteUtils.awaitForWorkersStop; + +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.locks.ReadWriteLock; +import java.util.concurrent.locks.ReentrantReadWriteLock; +import java.util.function.Supplier; +import org.apache.ignite.internal.thread.IgniteThread; +import org.apache.ignite.internal.util.worker.IgniteWorker; +import org.apache.ignite.lang.IgniteLogger; + +/** + * Synchronization wrapper for long operations that should be executed asynchronously and operations that can not be executed in parallel + * with long operation. + * + * <p>Uses {@link ReadWriteLock} to provide such synchronization scenario. + */ +class LongOperationAsyncExecutor { + private final ReadWriteLock readWriteLock = new ReentrantReadWriteLock(); + + private final String igniteInstanceName; + + private final IgniteLogger log; + + private final Set<IgniteWorker> workers = ConcurrentHashMap.newKeySet(); + + private static final AtomicLong WORKER_COUNTER = new AtomicLong(0); + + /** + * Constructor. + * + * @param igniteInstanceName Name of the Ignite instance this runnable is used in. + * @param log Logger. + */ + public LongOperationAsyncExecutor(String igniteInstanceName, IgniteLogger log) { + this.igniteInstanceName = igniteInstanceName; + + this.log = log; + } + + /** + * Executes long operation in dedicated thread. + * + * <p>Uses write lock as such operations can't run simultaneously. + * + * @param operation Long operation. + * @param name name of the operation, used as part of the thread name. + */ + public void async(Runnable operation, String name) { + String workerName = "async-" + name + "-task-" + WORKER_COUNTER.getAndIncrement(); + + IgniteWorker worker = new IgniteWorker(log, igniteInstanceName, workerName, null) { + /** {@inheritDoc} */ + @Override + protected void body() { + readWriteLock.writeLock().lock(); + + try { + operation.run(); + } finally { + readWriteLock.writeLock().unlock(); + + workers.remove(this); + } + } + }; + + workers.add(worker); + + new IgniteThread(worker).start(); Review Comment: At the moment, the thread is created at the node stop, but will need to be reviewed later. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
