use of io.cdap.cdap.common.logging.common.UncaughtExceptionHandler in project cdap by caskdata.
the class MapReduceContainerLauncher method launch.
/**
* Launches the given main class. The main class will be loaded through the {@link MapReduceClassLoader}.
*
* @param mainClassName the main class to launch
* @param args arguments for the main class
*/
@SuppressWarnings("unused")
public static void launch(String mainClassName, String[] args) throws Exception {
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler());
ClassLoader systemClassLoader = ClassLoader.getSystemClassLoader();
List<URL> urls = ClassLoaders.getClassLoaderURLs(systemClassLoader, new ArrayList<>());
// Remove the URL that contains the given main classname to avoid infinite recursion.
// This is needed because we generate a class with the same main classname in order to intercept the main()
// method call from the container launch script.
URL resource = systemClassLoader.getResource(mainClassName.replace('.', '/') + ".class");
if (resource == null) {
throw new IllegalStateException("Failed to find resource for main class " + mainClassName);
}
if (!urls.remove(ClassLoaders.getClassPathURL(mainClassName, resource))) {
throw new IllegalStateException("Failed to remove main class resource " + resource);
}
// Create a MainClassLoader for dataset rewrite
URL[] classLoaderUrls = urls.toArray(new URL[urls.size()]);
ClassLoader mainClassLoader = new MainClassLoader(classLoaderUrls, systemClassLoader.getParent());
// Install the JUL to SLF4J Bridge
try {
mainClassLoader.loadClass(SLF4JBridgeHandler.class.getName()).getDeclaredMethod("install").invoke(null);
} catch (Exception e) {
// Log the error and continue
LOG.warn("Failed to invoke SLF4JBridgeHandler.install() required for jul-to-slf4j bridge", e);
}
ClassLoaders.setContextClassLoader(mainClassLoader);
// Creates the MapReduceClassLoader. It has to be loaded from the MainClassLoader.
try {
final ClassLoader classLoader = (ClassLoader) mainClassLoader.loadClass(MapReduceClassLoader.class.getName()).newInstance();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
if (classLoader instanceof AutoCloseable) {
try {
((AutoCloseable) classLoader).close();
} catch (Exception e) {
System.err.println("Failed to close ClassLoader " + classLoader);
e.printStackTrace();
}
}
}
});
Thread.currentThread().setContextClassLoader(classLoader);
// Setup logging and stdout/stderr redirect
// Invoke MapReduceClassLoader.getTaskContextProvider()
classLoader.getClass().getDeclaredMethod("getTaskContextProvider").invoke(classLoader);
// Invoke StandardOutErrorRedirector.redirectToLogger()
classLoader.loadClass("io.cdap.cdap.common.logging.StandardOutErrorRedirector").getDeclaredMethod("redirectToLogger", String.class).invoke(null, mainClassName);
Class<?> mainClass = classLoader.loadClass(mainClassName);
Method mainMethod = mainClass.getMethod("main", String[].class);
mainMethod.setAccessible(true);
LOG.info("Launch main class {}.main({})", mainClassName, Arrays.toString(args));
mainMethod.invoke(null, new Object[] { args });
LOG.info("Main method returned {}", mainClassName);
} catch (Throwable t) {
// print to System.err since the logger may not have been redirected yet
System.err.println(String.format("Exception raised when calling %s.main(String[]) method", mainClassName));
t.printStackTrace();
throw t;
}
}
use of io.cdap.cdap.common.logging.common.UncaughtExceptionHandler in project cdap by caskdata.
the class AbstractProgramTwillRunnable method doInitialize.
/**
* Prepares this instance to execute a program.
*
* @param programOptionFile a json file containing the serialized {@link ProgramOptions}
* @throws Exception if failed to initialize
*/
private void doInitialize(File programOptionFile) throws Exception {
controllerFuture = new CompletableFuture<>();
programCompletion = new CompletableFuture<>();
// Setup process wide settings
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler());
System.setSecurityManager(new ProgramContainerSecurityManager(System.getSecurityManager()));
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
// Create the ProgramOptions
programOptions = createProgramOptions(programOptionFile);
programRunId = programOptions.getProgramId().run(ProgramRunners.getRunId(programOptions));
Arguments systemArgs = programOptions.getArguments();
LoggingContextAccessor.setLoggingContext(LoggingContextHelper.getLoggingContextWithRunId(programRunId, systemArgs.asMap()));
ClusterMode clusterMode = ProgramRunners.getClusterMode(programOptions);
// Loads configurations
Configuration hConf = new Configuration();
if (clusterMode == ClusterMode.ON_PREMISE) {
hConf.clear();
hConf.addResource(new File(systemArgs.getOption(ProgramOptionConstants.HADOOP_CONF_FILE)).toURI().toURL());
}
UserGroupInformation.setConfiguration(hConf);
CConfiguration cConf = CConfiguration.create();
cConf.clear();
cConf.addResource(new File(systemArgs.getOption(ProgramOptionConstants.CDAP_CONF_FILE)).toURI().toURL());
maxStopSeconds = cConf.getLong(io.cdap.cdap.common.conf.Constants.AppFabric.PROGRAM_MAX_STOP_SECONDS);
Injector injector = Guice.createInjector(createModule(cConf, hConf, programOptions, programRunId));
// Initialize log appender
logAppenderInitializer = injector.getInstance(LogAppenderInitializer.class);
logAppenderInitializer.initialize();
SystemArguments.setLogLevel(programOptions.getUserArguments(), logAppenderInitializer);
// Setup the proxy selector for in active monitoring mode
oldProxySelector = ProxySelector.getDefault();
if (clusterMode == ClusterMode.ISOLATED) {
RuntimeMonitors.setupMonitoring(injector, programOptions);
}
// Create list of core services. They'll will be started in the run method and shutdown when the run
// method completed
coreServices = createCoreServices(injector, programOptions);
// Create the ProgramRunner
programRunner = createProgramRunner(injector);
// Create the Program instance
Location programJarLocation = Locations.toLocation(new File(systemArgs.getOption(ProgramOptionConstants.PROGRAM_JAR)));
ApplicationSpecification appSpec = readJsonFile(new File(systemArgs.getOption(ProgramOptionConstants.APP_SPEC_FILE)), ApplicationSpecification.class);
// Expand the program jar for creating classloader
ClassLoaderFolder classLoaderFolder = BundleJarUtil.prepareClassLoaderFolder(programJarLocation, () -> new File("expanded." + System.currentTimeMillis() + programJarLocation.getName()));
program = Programs.create(cConf, programRunner, new ProgramDescriptor(programOptions.getProgramId(), appSpec), programJarLocation, classLoaderFolder.getDir());
}
use of io.cdap.cdap.common.logging.common.UncaughtExceptionHandler in project cdap by caskdata.
the class AbstractServiceMain method init.
@Override
public final void init(String[] args) throws Exception {
LOG.info("Initializing master service class {}", getClass().getName());
// System wide setup
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler());
// Intercept JUL loggers
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
TypeToken<?> type = TypeToken.of(getClass()).resolveType(AbstractServiceMain.class.getTypeParameters()[0]);
T options = (T) type.getRawType().newInstance();
OptionsParser.init(options, args, getClass().getSimpleName(), ProjectInfo.getVersion().toString(), System.out);
CConfiguration cConf = CConfiguration.create();
SecurityUtil.loginForMasterService(cConf);
SConfiguration sConf = SConfiguration.create();
if (options.getExtraConfPath() != null) {
cConf.addResource(new File(options.getExtraConfPath(), "cdap-site.xml").toURI().toURL());
sConf.addResource(new File(options.getExtraConfPath(), "cdap-security.xml").toURI().toURL());
}
cConf = updateCConf(cConf);
Configuration hConf = new Configuration();
masterEnv = MasterEnvironments.setMasterEnvironment(MasterEnvironments.create(cConf, options.getEnvProvider()));
MasterEnvironmentContext masterEnvContext = MasterEnvironments.createContext(cConf, hConf, masterEnv.getName());
masterEnv.initialize(masterEnvContext);
List<Module> modules = new ArrayList<>();
modules.add(new ConfigModule(cConf, hConf, sConf));
modules.add(RemoteAuthenticatorModules.getDefaultModule());
modules.add(new PreviewConfigModule(cConf, hConf, sConf));
modules.add(new IOModule());
modules.add(new MetricsClientRuntimeModule().getDistributedModules());
modules.add(new AbstractModule() {
@Override
protected void configure() {
bind(DiscoveryService.class).toProvider(new SupplierProviderBridge<>(masterEnv.getDiscoveryServiceSupplier()));
bind(DiscoveryServiceClient.class).toProvider(new SupplierProviderBridge<>(masterEnv.getDiscoveryServiceClientSupplier()));
}
});
modules.add(getLogAppenderModule());
CoreSecurityModule coreSecurityModule = CoreSecurityRuntimeModule.getDistributedModule(cConf);
modules.add(coreSecurityModule);
if (coreSecurityModule.requiresZKClient()) {
modules.add(new ZKClientModule());
}
modules.add(new AuthenticationContextModules().getMasterModule());
modules.addAll(getServiceModules(masterEnv, options, cConf));
injector = Guice.createInjector(modules);
// Initialize logging context
LogAppenderInitializer logAppenderInitializer = injector.getInstance(LogAppenderInitializer.class);
closeableResources.add(logAppenderInitializer);
logAppenderInitializer.initialize();
Optional.ofNullable(getLoggingContext(options)).ifPresent(LoggingContextAccessor::setLoggingContext);
// Add Services
services.add(injector.getInstance(MetricsCollectionService.class));
addServices(injector, services, closeableResources, masterEnv, masterEnvContext, options);
// Optionally get the storage provider. It is for destroy() method to close it on shutdown.
Binding<StorageProvider> storageBinding = injector.getExistingBinding(Key.get(StorageProvider.class));
if (storageBinding != null) {
storageProvider = storageBinding.getProvider().get();
}
LOG.info("Service {} initialized", getClass().getName());
}
use of io.cdap.cdap.common.logging.common.UncaughtExceptionHandler in project cdap by caskdata.
the class MasterEnvironmentMain method doMain.
/**
* The actual main method that get invoke through reflection from the {@link #main(String[])} method.
*/
@SuppressWarnings("unused")
public static void doMain(String[] args) throws Exception {
CountDownLatch shutdownLatch = new CountDownLatch(1);
try {
// System wide setup
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler());
// Intercept JUL loggers
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
EnvironmentOptions options = new EnvironmentOptions();
String[] runnableArgs = OptionsParser.init(options, args, MasterEnvironmentMain.class.getSimpleName(), ProjectInfo.getVersion().toString(), System.out).toArray(new String[0]);
String runnableClass = options.getRunnableClass();
if (runnableClass == null) {
throw new IllegalArgumentException("Missing runnable class name");
}
CConfiguration cConf = CConfiguration.create();
SConfiguration sConf = SConfiguration.create();
if (options.getExtraConfPath() != null) {
cConf.addResource(new File(options.getExtraConfPath(), "cdap-site.xml").toURI().toURL());
sConf.addResource(new File(options.getExtraConfPath(), "cdap-security.xml").toURI().toURL());
}
SecurityUtil.loginForMasterService(cConf);
Configuration hConf = new Configuration();
// Creates the master environment and load the MasterEnvironmentRunnable class from it.
MasterEnvironment masterEnv = MasterEnvironments.setMasterEnvironment(MasterEnvironments.create(cConf, options.getEnvProvider()));
MasterEnvironmentContext context = MasterEnvironments.createContext(cConf, hConf, masterEnv.getName());
masterEnv.initialize(context);
try {
Class<?> cls = masterEnv.getClass().getClassLoader().loadClass(runnableClass);
if (!MasterEnvironmentRunnable.class.isAssignableFrom(cls)) {
throw new IllegalArgumentException("Runnable class " + runnableClass + " is not an instance of " + MasterEnvironmentRunnable.class);
}
RemoteClientFactory remoteClientFactory = new RemoteClientFactory(masterEnv.getDiscoveryServiceClientSupplier().get(), getInternalAuthenticator(cConf), getRemoteAuthenticator(cConf));
MasterEnvironmentRunnableContext runnableContext = new DefaultMasterEnvironmentRunnableContext(context.getLocationFactory(), remoteClientFactory);
@SuppressWarnings("unchecked") MasterEnvironmentRunnable runnable = masterEnv.createRunnable(runnableContext, (Class<? extends MasterEnvironmentRunnable>) cls);
AtomicBoolean completed = new AtomicBoolean();
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
if (!completed.get()) {
runnable.stop();
Uninterruptibles.awaitUninterruptibly(shutdownLatch, 30, TimeUnit.SECONDS);
}
Optional.ofNullable(tokenManager).ifPresent(TokenManager::stopAndWait);
}));
runnable.run(runnableArgs);
completed.set(true);
} finally {
masterEnv.destroy();
}
} catch (Exception e) {
LOG.error("Failed to execute with arguments {}", Arrays.toString(args), e);
throw e;
} finally {
shutdownLatch.countDown();
}
}
use of io.cdap.cdap.common.logging.common.UncaughtExceptionHandler in project cdap by caskdata.
the class AbstractMasterTwillRunnable method initialize.
@Override
public final void initialize(TwillContext context) {
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler());
super.initialize(context);
name = context.getSpecification().getName();
LOG.info("Initializing runnable {}", name);
Map<String, String> configs = context.getSpecification().getConfigs();
try {
// Load configuration
hConf = new Configuration();
hConf.clear();
hConf.addResource(new File(configs.get("hConf")).toURI().toURL());
UserGroupInformation.setConfiguration(hConf);
cConf = CConfiguration.create(new File(configs.get("cConf")));
LOG.debug("{} cConf {}", name, cConf);
LOG.debug("{} HBase conf {}", name, hConf);
Injector injector = doInit(context);
services = Lists.newArrayList();
// Add common base services
services.add(injector.getInstance(ZKClientService.class));
services.add(injector.getInstance(KafkaClientService.class));
services.add(injector.getInstance(BrokerService.class));
services.add(injector.getInstance(MetricsCollectionService.class));
addServices(services);
Preconditions.checkArgument(!services.isEmpty(), "Should have at least one service");
LOG.info("Runnable initialized {}", name);
} catch (Throwable t) {
throw Throwables.propagate(t);
}
}
Aggregations