VGalaxies commented on code in PR #2319: URL: https://github.com/apache/incubator-hugegraph/pull/2319#discussion_r1359865027
########## hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/DistributedTaskScheduler.java: ########## @@ -0,0 +1,666 @@ +/* + * 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.hugegraph.task; + +import java.util.Collection; +import java.util.Iterator; +import java.util.concurrent.Callable; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.Lock; + +import org.apache.hugegraph.HugeException; +import org.apache.hugegraph.HugeGraph; +import org.apache.hugegraph.HugeGraphParams; +import org.apache.hugegraph.backend.id.Id; +import org.apache.hugegraph.backend.page.PageInfo; +import org.apache.hugegraph.backend.query.QueryResults; +import org.apache.hugegraph.concurrent.LockGroup; +import org.apache.hugegraph.concurrent.LockManager; +import org.apache.hugegraph.config.CoreOptions; +import org.apache.hugegraph.exception.ConnectionException; +import org.apache.hugegraph.exception.NotFoundException; +import org.apache.hugegraph.meta.MetaManager; +import org.apache.hugegraph.meta.lock.LockResult; +import org.apache.hugegraph.structure.HugeVertex; +import org.apache.hugegraph.util.E; +import org.apache.hugegraph.util.LockUtil; +import org.apache.hugegraph.util.Log; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.slf4j.Logger; + +public class DistributedTaskScheduler extends TaskAndResultScheduler { + protected static final int SCHEDULE_PERIOD = 10; + private static final Logger LOG = Log.logger(DistributedTaskScheduler.class); + private final ExecutorService taskDbExecutor; + private final ExecutorService schemaTaskExecutor; + private final ExecutorService olapTaskExecutor; + private final ExecutorService ephemeralTaskExecutor; + private final ExecutorService gremlinTaskExecutor; + private final ScheduledThreadPoolExecutor schedulerExecutor; + private final ScheduledFuture<?> cronFuture; + + private final String lockGroupName; + + /** + * the status of scheduler + */ + private final AtomicBoolean closed = new AtomicBoolean(true); + + private final ConcurrentHashMap<Id, HugeTask<?>> runningTasks = new ConcurrentHashMap<>(); + + public DistributedTaskScheduler(HugeGraphParams graph, + ScheduledThreadPoolExecutor schedulerExecutor, + ExecutorService taskDbExecutor, + ExecutorService schemaTaskExecutor, + ExecutorService olapTaskExecutor, + ExecutorService gremlinTaskExecutor, + ExecutorService ephemeralTaskExecutor, + ExecutorService serverInfoDbExecutor) { + super(graph, serverInfoDbExecutor); + + this.taskDbExecutor = taskDbExecutor; + this.schemaTaskExecutor = schemaTaskExecutor; + this.olapTaskExecutor = olapTaskExecutor; + this.gremlinTaskExecutor = gremlinTaskExecutor; + this.ephemeralTaskExecutor = ephemeralTaskExecutor; + + this.schedulerExecutor = schedulerExecutor; + + lockGroupName = String.format("%s_%s_distributed", graphSpace, graph); + LockManager.instance().create(lockGroupName); + + this.closed.set(false); + + this.cronFuture = this.schedulerExecutor.scheduleWithFixedDelay( + () -> { + // TODO: uncomment later - graph space + // LockUtil.lock(this.graph().spaceGraphName(), LockUtil.GRAPH_LOCK); + LockUtil.lock("", LockUtil.GRAPH_LOCK); + try { + // TODO: 使用超级管理员权限,查询任务 + // TaskManager.useAdmin(); + this.cronSchedule(); + } catch (Throwable t) { + LOG.info("cronScheduler exception ", t); + } finally { + // TODO: uncomment later - graph space + LockUtil.unlock("", LockUtil.GRAPH_LOCK); + // LockUtil.unlock(this.graph().spaceGraphName(), LockUtil.GRAPH_LOCK); + } + }, + 10L, SCHEDULE_PERIOD, + TimeUnit.SECONDS); + } + + private static boolean sleep(long ms) { + try { + Thread.sleep(ms); + return true; + } catch (InterruptedException ignored) { + // Ignore InterruptedException + return false; + } + } + + public void cronSchedule() { Review Comment: > 另外,看文档时说有根据负载来执行任务的,但是这里貌似没看到,是在其他地方还是暂时没实现哈=。=?原来的调度策略是 master 根据各个节点的负载来指定任务的 check 了下内部版代码,应该是暂时未实现 😵💫 ########## hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/task/DistributedTaskScheduler.java: ########## @@ -0,0 +1,666 @@ +/* + * 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.hugegraph.task; + +import java.util.Collection; +import java.util.Iterator; +import java.util.concurrent.Callable; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.ScheduledFuture; +import java.util.concurrent.ScheduledThreadPoolExecutor; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.locks.Lock; + +import org.apache.hugegraph.HugeException; +import org.apache.hugegraph.HugeGraph; +import org.apache.hugegraph.HugeGraphParams; +import org.apache.hugegraph.backend.id.Id; +import org.apache.hugegraph.backend.page.PageInfo; +import org.apache.hugegraph.backend.query.QueryResults; +import org.apache.hugegraph.concurrent.LockGroup; +import org.apache.hugegraph.concurrent.LockManager; +import org.apache.hugegraph.config.CoreOptions; +import org.apache.hugegraph.exception.ConnectionException; +import org.apache.hugegraph.exception.NotFoundException; +import org.apache.hugegraph.meta.MetaManager; +import org.apache.hugegraph.meta.lock.LockResult; +import org.apache.hugegraph.structure.HugeVertex; +import org.apache.hugegraph.util.E; +import org.apache.hugegraph.util.LockUtil; +import org.apache.hugegraph.util.Log; +import org.apache.tinkerpop.gremlin.structure.Vertex; +import org.slf4j.Logger; + +public class DistributedTaskScheduler extends TaskAndResultScheduler { + protected static final int SCHEDULE_PERIOD = 10; + private static final Logger LOG = Log.logger(DistributedTaskScheduler.class); + private final ExecutorService taskDbExecutor; + private final ExecutorService schemaTaskExecutor; + private final ExecutorService olapTaskExecutor; + private final ExecutorService ephemeralTaskExecutor; + private final ExecutorService gremlinTaskExecutor; + private final ScheduledThreadPoolExecutor schedulerExecutor; + private final ScheduledFuture<?> cronFuture; + + private final String lockGroupName; + + /** + * the status of scheduler + */ + private final AtomicBoolean closed = new AtomicBoolean(true); + + private final ConcurrentHashMap<Id, HugeTask<?>> runningTasks = new ConcurrentHashMap<>(); + + public DistributedTaskScheduler(HugeGraphParams graph, + ScheduledThreadPoolExecutor schedulerExecutor, + ExecutorService taskDbExecutor, + ExecutorService schemaTaskExecutor, + ExecutorService olapTaskExecutor, + ExecutorService gremlinTaskExecutor, + ExecutorService ephemeralTaskExecutor, + ExecutorService serverInfoDbExecutor) { + super(graph, serverInfoDbExecutor); + + this.taskDbExecutor = taskDbExecutor; + this.schemaTaskExecutor = schemaTaskExecutor; + this.olapTaskExecutor = olapTaskExecutor; + this.gremlinTaskExecutor = gremlinTaskExecutor; + this.ephemeralTaskExecutor = ephemeralTaskExecutor; + + this.schedulerExecutor = schedulerExecutor; + + lockGroupName = String.format("%s_%s_distributed", graphSpace, graph); + LockManager.instance().create(lockGroupName); + + this.closed.set(false); + + this.cronFuture = this.schedulerExecutor.scheduleWithFixedDelay( + () -> { + // TODO: uncomment later - graph space + // LockUtil.lock(this.graph().spaceGraphName(), LockUtil.GRAPH_LOCK); + LockUtil.lock("", LockUtil.GRAPH_LOCK); + try { + // TODO: 使用超级管理员权限,查询任务 + // TaskManager.useAdmin(); + this.cronSchedule(); + } catch (Throwable t) { + LOG.info("cronScheduler exception ", t); + } finally { + // TODO: uncomment later - graph space + LockUtil.unlock("", LockUtil.GRAPH_LOCK); + // LockUtil.unlock(this.graph().spaceGraphName(), LockUtil.GRAPH_LOCK); + } + }, + 10L, SCHEDULE_PERIOD, + TimeUnit.SECONDS); + } + + private static boolean sleep(long ms) { + try { + Thread.sleep(ms); + return true; + } catch (InterruptedException ignored) { + // Ignore InterruptedException + return false; + } + } + + public void cronSchedule() { Review Comment: > 另外,看文档时说有根据负载来执行任务的,但是这里貌似没看到,是在其他地方还是暂时没实现哈=。=?原来的调度策略是 master 根据各个节点的负载来指定任务的 check 了下内部版代码,应该是暂时未实现 😵💫 -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
