aplex commented on a change in pull request #3281:
URL: https://github.com/apache/gobblin/pull/3281#discussion_r647638477



##########
File path: 
gobblin-service/src/main/java/org/apache/gobblin/service/modules/core/GobblinServiceGuiceModule.java
##########
@@ -0,0 +1,253 @@
+/*
+ * 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.modules.core;
+
+import java.util.Objects;
+
+import org.apache.helix.HelixManager;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Lists;
+import com.google.common.eventbus.EventBus;
+import com.google.inject.Binder;
+import com.google.inject.Inject;
+import com.google.inject.Injector;
+import com.google.inject.Module;
+import com.google.inject.Provider;
+import com.google.inject.multibindings.OptionalBinder;
+import com.google.inject.name.Names;
+import com.typesafe.config.Config;
+
+import javax.inject.Singleton;
+
+import org.apache.gobblin.restli.EmbeddedRestliServer;
+import org.apache.gobblin.runtime.api.GobblinInstanceEnvironment;
+import org.apache.gobblin.runtime.instance.StandardGobblinInstanceLauncher;
+import org.apache.gobblin.runtime.spec_catalog.FlowCatalog;
+import org.apache.gobblin.runtime.spec_catalog.TopologyCatalog;
+import org.apache.gobblin.scheduler.SchedulerService;
+import org.apache.gobblin.service.FlowConfigResourceLocalHandler;
+import org.apache.gobblin.service.FlowConfigV2ResourceLocalHandler;
+import org.apache.gobblin.service.FlowConfigsResource;
+import org.apache.gobblin.service.FlowConfigsResourceHandler;
+import org.apache.gobblin.service.FlowConfigsV2Resource;
+import org.apache.gobblin.service.FlowConfigsV2ResourceHandler;
+import org.apache.gobblin.service.FlowExecutionResource;
+import org.apache.gobblin.service.FlowExecutionResourceHandler;
+import org.apache.gobblin.service.FlowExecutionResourceLocalHandler;
+import org.apache.gobblin.service.FlowStatusResource;
+import org.apache.gobblin.service.GroupOwnershipService;
+import org.apache.gobblin.service.NoopRequesterService;
+import org.apache.gobblin.service.RequesterService;
+import org.apache.gobblin.service.ServiceConfigKeys;
+import org.apache.gobblin.service.modules.orchestration.DagManager;
+import org.apache.gobblin.service.modules.orchestration.Orchestrator;
+import 
org.apache.gobblin.service.modules.restli.GobblinServiceFlowConfigResourceHandler;
+import 
org.apache.gobblin.service.modules.restli.GobblinServiceFlowConfigV2ResourceHandler;
+import 
org.apache.gobblin.service.modules.restli.GobblinServiceFlowExecutionResourceHandler;
+import org.apache.gobblin.service.modules.scheduler.GobblinServiceJobScheduler;
+import org.apache.gobblin.service.modules.topology.TopologySpecFactory;
+import org.apache.gobblin.service.modules.utils.HelixUtils;
+import org.apache.gobblin.service.modules.utils.InjectionNames;
+import org.apache.gobblin.service.monitoring.FlowStatusGenerator;
+import org.apache.gobblin.service.monitoring.FsJobStatusRetriever;
+import org.apache.gobblin.service.monitoring.JobStatusRetriever;
+import org.apache.gobblin.service.monitoring.KafkaJobStatusMonitor;
+import org.apache.gobblin.service.monitoring.KafkaJobStatusMonitorFactory;
+import org.apache.gobblin.util.ClassAliasResolver;
+import org.apache.gobblin.util.ConfigUtils;
+
+
+public class GobblinServiceGuiceModule implements Module {
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(GobblinServiceGuiceModule.class);
+  private static final String JOB_STATUS_RETRIEVER_CLASS_KEY = 
"jobStatusRetriever.class";
+
+  GobblinServiceConfiguration serviceConfig;
+
+  public GobblinServiceGuiceModule(GobblinServiceConfiguration serviceConfig) {
+    this.serviceConfig = Objects.requireNonNull(serviceConfig);
+  }
+
+  @Override
+  public void configure(Binder binder) {
+    LOGGER.info("Configuring bindings for the following service settings: {}", 
serviceConfig);
+
+    // In the current code base, we frequently inject classes instead of 
interfaces
+    // As a result, even when the binding is missing, Guice will create an 
instance of the
+    // the class and inject it. This interferes with disabling of different 
services and
+    // components, because without explicit bindings they will get 
instantiated anyway.
+    binder.requireExplicitBindings();
+
+    // Optional binder will find the existing binding for T and create 
additional binding for Optional<T>.
+    // If none of the specific class binding exist, optional will be "absent".
+    OptionalBinder.newOptionalBinder(binder, Logger.class);
+
+    
binder.bind(Logger.class).toInstance(LoggerFactory.getLogger(GobblinServiceManager.class));
+
+    binder.bind(Config.class).toInstance(serviceConfig.getInnerConfig());
+
+    binder.bind(GobblinServiceConfiguration.class).toInstance(serviceConfig);
+
+    // Used by TopologyCatalog and FlowCatalog
+    GobblinInstanceEnvironment gobblinInstanceEnvironment = 
StandardGobblinInstanceLauncher.builder()
+        .withLog(LoggerFactory.getLogger(GobblinServiceManager.class))
+        .setInstrumentationEnabled(true)
+        .withSysConfig(serviceConfig.getInnerConfig())
+        .build();
+
+    
binder.bind(GobblinInstanceEnvironment.class).toInstance(gobblinInstanceEnvironment);
+
+    binder.bind(EventBus.class)
+        
.annotatedWith(Names.named(GobblinServiceManager.SERVICE_EVENT_BUS_NAME))
+        .toInstance(new EventBus(GobblinServiceManager.class.getSimpleName()));
+
+    
binder.bindConstant().annotatedWith(Names.named(InjectionNames.SERVICE_NAME)).to(serviceConfig.getServiceName());
+
+    binder.bindConstant()

Review comment:
       If the class has many configuration settings, than it's better to have a 
config object and inject it. That's what we do for GobblinServiceManager that 
gets GobblinServiceConfiguration injected into it.
   
   Service name is injected into a couple of classes that don't have much in 
common, and those classes don't have a lot of configuration of their own. In 
future PRs we can consider adding something like ServiceEnvironment class that 
will hold the service name, id and other properties that can be useful for the 
classes that run inside Gobblin service. 
   
   There are still a bunch of places in the code that needs to be refactored 
for better DI support, but I left them out at the moment to reduce the PR size.




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to