yuqi1129 commented on code in PR #7814:
URL: https://github.com/apache/gravitino/pull/7814#discussion_r2236307717


##########
core/src/main/java/org/apache/gravitino/job/local/LocalJobExecutor.java:
##########
@@ -0,0 +1,300 @@
+/*
+ * 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.gravitino.job.local;
+
+import static 
org.apache.gravitino.job.local.LocalJobExecutorConfigs.DEFAULT_JOB_STATUS_KEEP_TIME_MS;
+import static 
org.apache.gravitino.job.local.LocalJobExecutorConfigs.DEFAULT_MAX_RUNNING_JOBS;
+import static 
org.apache.gravitino.job.local.LocalJobExecutorConfigs.DEFAULT_WAITING_QUEUE_SIZE;
+import static 
org.apache.gravitino.job.local.LocalJobExecutorConfigs.JOB_STATUS_KEEP_TIME_MS;
+import static 
org.apache.gravitino.job.local.LocalJobExecutorConfigs.MAX_RUNNING_JOBS;
+import static 
org.apache.gravitino.job.local.LocalJobExecutorConfigs.WAITING_QUEUE_SIZE;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Maps;
+import java.io.IOException;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.gravitino.connector.job.JobExecutor;
+import org.apache.gravitino.exceptions.NoSuchJobException;
+import org.apache.gravitino.job.JobHandle;
+import org.apache.gravitino.job.JobTemplate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class LocalJobExecutor implements JobExecutor {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(LocalJobExecutor.class);
+
+  private static final String LOCAL_JOB_PREFIX = "local-job-";
+
+  private static final long UNEXPIRED_TIME_IN_MS = -1L;
+
+  private Map<String, String> configs;
+
+  private BlockingQueue<Pair<String, JobTemplate>> waitingQueue;
+
+  private ExecutorService jobExecutorService;
+
+  // The job status map to keep track of the status of each job. In the 
meantime, the job status
+  // will be stored in the entity store, so we will clean the finished, 
cancelled and failed jobs
+  // from the map periodically to save the memory.
+  private Map<String, Pair<JobHandle.Status, Long>> jobStatus;
+  private final Object lock = new Object();
+
+  private long jobStatusKeepTimInMs;
+  private ScheduledExecutorService jobStatusCleanupExecutor;
+
+  private volatile boolean finished = false;
+
+  private Map<String, Process> runningProcesses;
+
+  @Override
+  public void initialize(Map<String, String> configs) {
+    this.configs = configs;
+
+    int waitingQueueSize =
+        configs.containsKey(WAITING_QUEUE_SIZE)
+            ? Integer.parseInt(configs.get(WAITING_QUEUE_SIZE))
+            : DEFAULT_WAITING_QUEUE_SIZE;
+    Preconditions.checkArgument(
+        waitingQueueSize > 0,
+        "Waiting queue size must be greater than 0, but got: %s",
+        waitingQueueSize);
+
+    this.waitingQueue = new LinkedBlockingQueue<>(waitingQueueSize);
+
+    int maxRunningJobs =
+        configs.containsKey(MAX_RUNNING_JOBS)
+            ? Integer.parseInt(configs.get(MAX_RUNNING_JOBS))
+            : DEFAULT_MAX_RUNNING_JOBS;
+    Preconditions.checkArgument(
+        maxRunningJobs > 0, "Max running jobs must be greater than 0, but got: 
%s", maxRunningJobs);
+
+    this.jobExecutorService =
+        Executors.newFixedThreadPool(
+            maxRunningJobs,
+            runnable -> {
+              Thread thread = new Thread(runnable);
+              thread.setName("LocalJobExecutor-" + thread.getId());
+              thread.setDaemon(true);
+              return thread;
+            });
+    // Start the job executor service to run jobs
+    for (int i = 0; i < maxRunningJobs; i++) {
+      jobExecutorService.submit(this::runJob);
+    }
+
+    this.jobStatus = Maps.newHashMap();
+
+    this.jobStatusKeepTimInMs =
+        configs.containsKey(JOB_STATUS_KEEP_TIME_MS)
+            ? Long.parseLong(configs.get(JOB_STATUS_KEEP_TIME_MS))
+            : DEFAULT_JOB_STATUS_KEEP_TIME_MS;
+    Preconditions.checkArgument(
+        jobStatusKeepTimInMs > 0,
+        "Job status keep time must be greater than 0, but got: %s",
+        jobStatusKeepTimInMs);
+
+    long jobStatusCleanupIntervalInMs = jobStatusKeepTimInMs / 10;
+    this.jobStatusCleanupExecutor =
+        Executors.newSingleThreadScheduledExecutor(
+            runnable -> {
+              Thread thread = new Thread(runnable);
+              thread.setName("LocalJobStatusCleanup-" + thread.getId());
+              thread.setDaemon(true);
+              return thread;
+            });
+    jobStatusCleanupExecutor.scheduleAtFixedRate(
+        this::cleanupJobStatus,
+        jobStatusCleanupIntervalInMs,
+        jobStatusCleanupIntervalInMs,
+        java.util.concurrent.TimeUnit.MILLISECONDS);

Review Comment:
   Remove `java.util.concurrent.`



##########
core/src/main/java/org/apache/gravitino/job/local/LocalJobExecutor.java:
##########
@@ -0,0 +1,300 @@
+/*
+ * 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.gravitino.job.local;
+
+import static 
org.apache.gravitino.job.local.LocalJobExecutorConfigs.DEFAULT_JOB_STATUS_KEEP_TIME_MS;
+import static 
org.apache.gravitino.job.local.LocalJobExecutorConfigs.DEFAULT_MAX_RUNNING_JOBS;
+import static 
org.apache.gravitino.job.local.LocalJobExecutorConfigs.DEFAULT_WAITING_QUEUE_SIZE;
+import static 
org.apache.gravitino.job.local.LocalJobExecutorConfigs.JOB_STATUS_KEEP_TIME_MS;
+import static 
org.apache.gravitino.job.local.LocalJobExecutorConfigs.MAX_RUNNING_JOBS;
+import static 
org.apache.gravitino.job.local.LocalJobExecutorConfigs.WAITING_QUEUE_SIZE;
+
+import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.Maps;
+import java.io.IOException;
+import java.util.Map;
+import java.util.UUID;
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.gravitino.connector.job.JobExecutor;
+import org.apache.gravitino.exceptions.NoSuchJobException;
+import org.apache.gravitino.job.JobHandle;
+import org.apache.gravitino.job.JobTemplate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class LocalJobExecutor implements JobExecutor {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(LocalJobExecutor.class);
+
+  private static final String LOCAL_JOB_PREFIX = "local-job-";
+
+  private static final long UNEXPIRED_TIME_IN_MS = -1L;
+
+  private Map<String, String> configs;
+
+  private BlockingQueue<Pair<String, JobTemplate>> waitingQueue;
+
+  private ExecutorService jobExecutorService;
+
+  // The job status map to keep track of the status of each job. In the 
meantime, the job status
+  // will be stored in the entity store, so we will clean the finished, 
cancelled and failed jobs
+  // from the map periodically to save the memory.
+  private Map<String, Pair<JobHandle.Status, Long>> jobStatus;
+  private final Object lock = new Object();
+
+  private long jobStatusKeepTimInMs;
+  private ScheduledExecutorService jobStatusCleanupExecutor;
+
+  private volatile boolean finished = false;
+
+  private Map<String, Process> runningProcesses;
+
+  @Override
+  public void initialize(Map<String, String> configs) {
+    this.configs = configs;
+
+    int waitingQueueSize =
+        configs.containsKey(WAITING_QUEUE_SIZE)
+            ? Integer.parseInt(configs.get(WAITING_QUEUE_SIZE))
+            : DEFAULT_WAITING_QUEUE_SIZE;
+    Preconditions.checkArgument(
+        waitingQueueSize > 0,
+        "Waiting queue size must be greater than 0, but got: %s",
+        waitingQueueSize);
+
+    this.waitingQueue = new LinkedBlockingQueue<>(waitingQueueSize);
+
+    int maxRunningJobs =
+        configs.containsKey(MAX_RUNNING_JOBS)
+            ? Integer.parseInt(configs.get(MAX_RUNNING_JOBS))
+            : DEFAULT_MAX_RUNNING_JOBS;
+    Preconditions.checkArgument(
+        maxRunningJobs > 0, "Max running jobs must be greater than 0, but got: 
%s", maxRunningJobs);
+
+    this.jobExecutorService =
+        Executors.newFixedThreadPool(
+            maxRunningJobs,
+            runnable -> {
+              Thread thread = new Thread(runnable);
+              thread.setName("LocalJobExecutor-" + thread.getId());
+              thread.setDaemon(true);
+              return thread;
+            });
+    // Start the job executor service to run jobs
+    for (int i = 0; i < maxRunningJobs; i++) {
+      jobExecutorService.submit(this::runJob);

Review Comment:
   In this way, there will always `maxRunningJobs` running threads even there 
is no jobs at all.
   
   I suggest we use a thread to monitor the queue and use the thread pool the 
handle the job
   
   // one thread
   
   while (isRunnning) {
        try {
           jobPair = waitingQueue.poll(3000, TimeUnit.MILLISECONDS);
           if (jobPair == null) {
             // If no job is available, continue to the next iteration
             continue;
           }
            
           threadpool.submit(jobPair)
         }
   }
   
   



-- 
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]

Reply via email to