/*
* Copyright © 2014 Cask Data, Inc.
*
* Licensed 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 co.cask.cdap.internal.app.runtime.batch.inmemory;
import co.cask.cdap.app.guice.ProgramRunnerRuntimeModule;
import co.cask.cdap.common.conf.CConfiguration;
import co.cask.cdap.common.conf.Constants;
import co.cask.cdap.common.guice.ConfigModule;
import co.cask.cdap.common.guice.DiscoveryRuntimeModule;
import co.cask.cdap.common.guice.IOModule;
import co.cask.cdap.common.guice.LocationRuntimeModule;
import co.cask.cdap.common.utils.Networks;
import co.cask.cdap.data.runtime.DataFabricModules;
import co.cask.cdap.data.runtime.DataSetsModules;
import co.cask.cdap.gateway.auth.AuthModule;
import co.cask.cdap.internal.app.runtime.batch.AbstractMapReduceContextBuilder;
import co.cask.cdap.logging.guice.LoggingModules;
import co.cask.cdap.metrics.guice.MetricsClientRuntimeModule;
import com.google.common.collect.ImmutableList;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.Provides;
import com.google.inject.name.Named;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
import java.net.InetAddress;
import java.net.InetSocketAddress;
/**
* Builds an instance of {@link co.cask.cdap.internal.app.runtime.batch.BasicMapReduceContext} good for
* in-memory environment
*/
public class InMemoryMapReduceContextBuilder extends AbstractMapReduceContextBuilder {
private final CConfiguration cConf;
private final TaskAttemptContext taskContext;
public InMemoryMapReduceContextBuilder(CConfiguration cConf, TaskAttemptContext taskContext) {
this.cConf = cConf;
this.taskContext = taskContext;
}
@Override
protected Injector prepare() {
// TODO: this logic should go into DataFabricModules. We'll move it once Guice modules are refactored
Constants.InMemoryPersistenceType persistenceType = Constants.InMemoryPersistenceType.valueOf(
cConf.get(Constants.CFG_DATA_INMEMORY_PERSISTENCE, Constants.DEFAULT_DATA_INMEMORY_PERSISTENCE));
if (Constants.InMemoryPersistenceType.MEMORY == persistenceType) {
return createInMemoryModules();
} else {
return createPersistentModules();
}
}
private Injector createInMemoryModules() {
ImmutableList<Module> inMemoryModules = ImmutableList.of(
new ConfigModule(cConf),
new LocalConfigModule(),
new IOModule(),
new AuthModule(),
new LocationRuntimeModule().getInMemoryModules(),
new DiscoveryRuntimeModule().getInMemoryModules(),
new ProgramRunnerRuntimeModule().getInMemoryModules(),
new DataFabricModules().getInMemoryModules(),
new DataSetsModules().getLocalModule(),
new MetricsClientRuntimeModule().getNoopModules(),
new LoggingModules().getInMemoryModules()
);
return Guice.createInjector(inMemoryModules);
}
private Injector createPersistentModules() {
ImmutableList<Module> standaloneModules = ImmutableList.of(
new ConfigModule(cConf),
new LocalConfigModule(),
new IOModule(),
new AuthModule(),
new LocationRuntimeModule().getStandaloneModules(),
new DiscoveryRuntimeModule().getStandaloneModules(),
new ProgramRunnerRuntimeModule().getStandaloneModules(),
new DataFabricModules().getStandaloneModules(),
new DataSetsModules().getLocalModule(),
new MetricsClientRuntimeModule().getMapReduceModules(taskContext),
new LoggingModules().getStandaloneModules()
);
return Guice.createInjector(standaloneModules);
}
/**
* Provides bindings to configs needed by other modules binding in this class.
*/
private static class LocalConfigModule extends AbstractModule {
@Override
protected void configure() {
// No-op
}
@Provides
@Named(Constants.AppFabric.SERVER_ADDRESS)
public InetAddress providesHostname(CConfiguration cConf) {
return Networks.resolve(cConf.get(Constants.AppFabric.SERVER_ADDRESS),
new InetSocketAddress("localhost", 0).getAddress());
}
}
}