hackergin commented on code in PR #24821:
URL: https://github.com/apache/flink/pull/24821#discussion_r1611566207


##########
flink-table/flink-sql-gateway/src/main/java/org/apache/flink/table/gateway/workflow/scheduler/EmbeddedQuartzScheduler.java:
##########
@@ -0,0 +1,228 @@
+/*
+ * 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.flink.table.gateway.workflow.scheduler;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.table.gateway.SqlGateway;
+import org.apache.flink.table.gateway.workflow.WorkflowInfo;
+
+import org.quartz.CronTrigger;
+import org.quartz.Job;
+import org.quartz.JobBuilder;
+import org.quartz.JobDataMap;
+import org.quartz.JobDetail;
+import org.quartz.JobExecutionContext;
+import org.quartz.JobExecutionException;
+import org.quartz.JobKey;
+import org.quartz.Scheduler;
+import org.quartz.TriggerKey;
+import org.quartz.impl.StdSchedulerFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Properties;
+import java.util.concurrent.locks.ReadWriteLock;
+import java.util.concurrent.locks.ReentrantReadWriteLock;
+
+import static 
org.apache.flink.table.gateway.workflow.scheduler.QuartzSchedulerUtils.WORKFLOW_INFO;
+import static 
org.apache.flink.table.gateway.workflow.scheduler.QuartzSchedulerUtils.fromJson;
+import static 
org.apache.flink.table.gateway.workflow.scheduler.QuartzSchedulerUtils.getJobKey;
+import static 
org.apache.flink.table.gateway.workflow.scheduler.QuartzSchedulerUtils.initializeQuartzSchedulerConfig;
+import static 
org.apache.flink.table.gateway.workflow.scheduler.QuartzSchedulerUtils.toJson;
+import static org.quartz.CronScheduleBuilder.cronSchedule;
+import static org.quartz.TriggerBuilder.newTrigger;
+
+/**
+ * An embedded in-memory workflow scheduler based on quartz {@link Scheduler} 
that store all
+ * workflow in memory, it does not have high availability. This service will 
be embedded in {@link
+ * SqlGateway} process to provide service by rest api.
+ *
+ * <p>This embedded scheduler is mainly used for testing scenarios and is not 
suitable for
+ * production environment.
+ */
+@Internal
+public class EmbeddedQuartzScheduler {
+
+    private static final Logger LOG = 
LoggerFactory.getLogger(EmbeddedQuartzScheduler.class);
+
+    private final ReadWriteLock lock = new ReentrantReadWriteLock();
+
+    private Scheduler quartzScheduler;
+
+    public void start() {
+        Properties properties = initializeQuartzSchedulerConfig();
+        try {
+            quartzScheduler = new 
StdSchedulerFactory(properties).getScheduler();
+            quartzScheduler.start();
+        } catch (org.quartz.SchedulerException e) {
+            String msg =
+                    String.format("Failed to start quartz scheduler with 
config: %s.", properties);
+            LOG.error(msg);
+            throw new SchedulerException(msg, e);
+        }
+    }
+
+    public void stop() {
+        try {
+            quartzScheduler.shutdown();
+        } catch (org.quartz.SchedulerException e) {
+            LOG.error("Failed to shutdown quartz schedule.");
+            throw new SchedulerException("Failed to shutdown quartz 
scheduler.", e);
+        }
+    }
+
+    public JobDetail createScheduleWorkflow(WorkflowInfo workflowInfo, String 
cronExpression)
+            throws SchedulerException {
+        String materializedTableIdentifier = 
workflowInfo.getMaterializedTableIdentifier();
+        JobKey jobKey = getJobKey(materializedTableIdentifier);
+
+        lock.writeLock().lock();
+        try {
+            // check if the quartz schedule job is exists firstly.

Review Comment:
   ```suggestion
               // check if the quartz schedule job exists firstly.
   ```



##########
flink-table/flink-sql-gateway/src/main/java/org/apache/flink/table/gateway/workflow/scheduler/QuartzSchedulerUtils.java:
##########
@@ -0,0 +1,125 @@
+/*
+ * 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.flink.table.gateway.workflow.scheduler;
+
+import org.apache.flink.util.jackson.JacksonMapperFactory;
+
+import 
org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.ObjectMapper;
+
+import org.quartz.JobKey;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneId;
+import java.time.format.DateTimeFormatter;
+import java.util.Date;
+import java.util.Properties;
+
+import static org.quartz.impl.StdSchedulerFactory.PROP_SCHED_INSTANCE_NAME;
+import static org.quartz.impl.StdSchedulerFactory.PROP_SCHED_RMI_EXPORT;
+import static org.quartz.impl.StdSchedulerFactory.PROP_SCHED_RMI_PROXY;
+import static org.quartz.impl.StdSchedulerFactory.PROP_THREAD_POOL_CLASS;
+import static org.quartz.impl.StdSchedulerFactory.PROP_THREAD_POOL_PREFIX;
+
+/** Utility class for quartz scheduler. */
+public class QuartzSchedulerUtils {
+
+    public static final String SCHEDULE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
+    public static final DateTimeFormatter DEFAULT_DATETIME_FORMATTER =
+            DateTimeFormatter.ofPattern(SCHEDULE_TIME_FORMAT);
+
+    public static final String QUARTZ_JOB_PREFIX = "quartz_job";
+    public static final String QUARTZ_JOB_GROUP = "default_group";
+    public static final String UNDERLINE = "_";
+    public static final String WORKFLOW_INFO = "workflowInfo";
+
+    private static final ObjectMapper OBJECT_MAPPER = 
JacksonMapperFactory.createObjectMapper();
+
+    /**
+     * This method is used to initializing the scheduler config, the related 
options copy from

Review Comment:
   ```suggestion
        * This method is used for initializing the scheduler config, the 
related options copy from
   ```



-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to