AndyJiang99 commented on code in PR #3548: URL: https://github.com/apache/gobblin/pull/3548#discussion_r975809338
########## gobblin-service/src/main/java/org/apache/gobblin/service/monitoring/FsFlowGraphMonitor.java: ########## @@ -0,0 +1,134 @@ +/* + * 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.gobblin.service.monitoring; + +import java.io.IOException; +import java.net.URI; +import java.util.Map; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.hadoop.fs.Path; + +import com.google.common.base.Optional; +import com.google.common.collect.ImmutableMap; +import com.google.common.util.concurrent.AbstractIdleService; +import com.typesafe.config.Config; +import com.typesafe.config.ConfigFactory; + +import lombok.extern.slf4j.Slf4j; + +import org.apache.gobblin.configuration.ConfigurationKeys; +import org.apache.gobblin.runtime.api.TopologySpec; +import org.apache.gobblin.service.modules.flowgraph.BaseFlowGraphHelper; +import org.apache.gobblin.service.modules.flowgraph.FSPathAlterationFlowGraphListener; +import org.apache.gobblin.service.modules.flowgraph.FlowGraph; +import org.apache.gobblin.service.modules.flowgraph.FlowGraphMonitor; +import org.apache.gobblin.service.modules.template_catalog.FSFlowTemplateCatalog; +import org.apache.gobblin.util.filesystem.PathAlterationObserver; +import org.apache.gobblin.util.filesystem.PathAlterationObserverScheduler; + + +@Slf4j +public class FsFlowGraphMonitor extends AbstractIdleService implements FlowGraphMonitor { + public static final String FS_FLOWGRAPH_MONITOR_PREFIX = "gobblin.service.fsFlowGraphMonitor"; + private static final long DEFAULT_FLOWGRAPH_POLLING_INTERVAL = 60; + private static final String DEFAULT_FS_FLOWGRAPH_MONITOR_REPO_DIR = "git-flowgraph"; + private static final String DEFAULT_FS_FLOWGRAPH_MONITOR_FLOWGRAPH_DIR = "gobblin-flowgraph"; + private volatile boolean isActive = false; + private final long pollingInterval; + private BaseFlowGraphHelper flowGraphHelper; + private final PathAlterationObserverScheduler pathAlterationDetector; + private final FSPathAlterationFlowGraphListener listener; + private final PathAlterationObserver observer; + private final Path flowGraphPath; + private final AtomicReference<FlowGraph> flowGraph; + private final CountDownLatch initComplete; + private static final Config DEFAULT_FALLBACK = ConfigFactory.parseMap(ImmutableMap.<String, Object>builder() + .put(ConfigurationKeys.FLOWGRAPH_REPO_DIR, DEFAULT_FS_FLOWGRAPH_MONITOR_REPO_DIR) + .put(ConfigurationKeys.FLOWGRAPH_BASE_DIR, DEFAULT_FS_FLOWGRAPH_MONITOR_FLOWGRAPH_DIR) + .put(ConfigurationKeys.FLOWGRAPH_POLLING_INTERVAL, DEFAULT_FLOWGRAPH_POLLING_INTERVAL) + .put(ConfigurationKeys.JAVA_PROPS_EXTENSIONS, ConfigurationKeys.DEFAULT_PROPERTIES_EXTENSIONS) + .put(ConfigurationKeys.HOCON_FILE_EXTENSIONS, ConfigurationKeys.DEFAULT_CONF_EXTENSIONS).build()); + + public FsFlowGraphMonitor(Config config, Optional<? extends FSFlowTemplateCatalog> flowTemplateCatalog, + AtomicReference<FlowGraph> graph, Map<URI, TopologySpec> topologySpecMap, CountDownLatch initComplete) + throws IOException { + Config configWithFallbacks = config.getConfig(FS_FLOWGRAPH_MONITOR_PREFIX).withFallback(DEFAULT_FALLBACK); + this.pollingInterval = + TimeUnit.SECONDS.toMillis(configWithFallbacks.getLong(ConfigurationKeys.FLOWGRAPH_POLLING_INTERVAL)); + this.flowGraphPath = new Path(configWithFallbacks.getString(ConfigurationKeys.FLOWGRAPH_REPO_DIR)); + this.observer = new PathAlterationObserver(flowGraphPath); + this.flowGraphHelper = new BaseFlowGraphHelper(flowTemplateCatalog, topologySpecMap, flowGraphPath.toString(), + configWithFallbacks.getString(ConfigurationKeys.FLOWGRAPH_BASE_DIR), configWithFallbacks.getString(ConfigurationKeys.JAVA_PROPS_EXTENSIONS), + configWithFallbacks.getString(ConfigurationKeys.HOCON_FILE_EXTENSIONS)); + this.listener = new FSPathAlterationFlowGraphListener(flowTemplateCatalog, graph, flowGraphPath.toString(), this.flowGraphHelper); + + this.flowGraph = graph; + this.initComplete = initComplete; + + if (pollingInterval == ConfigurationKeys.DISABLED_JOB_CONFIG_FILE_MONITOR_POLLING_INTERVAL) { + this.pathAlterationDetector = null; + } else { + this.pathAlterationDetector = new PathAlterationObserverScheduler(pollingInterval); + Optional<PathAlterationObserver> observerOptional = Optional.fromNullable(observer); + this.pathAlterationDetector.addPathAlterationObserver(this.listener, observerOptional, + this.flowGraphPath); + } + } + + @Override + protected void startUp() + throws IOException { + } + + @Override + public synchronized void setActive(boolean isActive) { + log.info("Setting the flow graph monitor to be " + isActive + " from " + this.isActive); + if (this.isActive == isActive) { + // No-op if already in correct state + return; + } else if (isActive) { + if (this.pathAlterationDetector != null) { + log.info("Starting the " + getClass().getSimpleName()); + log.info("Polling flowgraph folder with interval {} ", this.pollingInterval); + try { + this.pathAlterationDetector.start(); + // Manually instantiate flowgraph when the monitor becomes active + this.flowGraphHelper.populateFlowGraphAtomically(this.flowGraph); + // Reduce the countdown latch + this.initComplete.countDown(); + log.info("Finished populating FSFlowgraph"); + } catch (IOException e) { + log.error("Could not initialize pathAlterationDetector due to error: ", e); + } + } else { + log.warn("No path alteration detector found"); + } + } + this.isActive = isActive; Review Comment: Should this be moved into the else-if statement instead? Since no-op if already in correct state -- 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: dev-unsubscr...@gobblin.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org