use of io.cdap.cdap.master.spi.environment.MasterEnvironmentRunnable 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.master.spi.environment.MasterEnvironmentRunnable in project cdap by caskdata.
the class KubeTwillPreparer method createContainer.
/**
* Creates a {@link V1Container} specification for running a {@link MasterEnvironmentRunnable} in a container.
*/
private V1Container createContainer(String name, String containerImage, String imagePullPolicy, String workDir, V1ResourceRequirements resourceRequirements, List<V1VolumeMount> volumeMounts, Map<String, String> environments, Class<? extends MasterEnvironmentRunnable> runnableClass, String... args) {
Map<String, String> environs = new HashMap<>(environments);
// Set the environments for controlling the working directory
environs.put("CDAP_LOCAL_DIR", workDir);
environs.put("CDAP_TEMP_DIR", "tmp");
// Set the process memory is through the JAVA_HEAPMAX variable.
environs.put("JAVA_HEAPMAX", String.format("-Xmx%dm", computeMaxHeapSize(resourceRequirements)));
List<V1EnvVar> containerEnvironments = environs.entrySet().stream().map(e -> new V1EnvVar().name(e.getKey()).value(e.getValue())).collect(Collectors.toList());
V1ContainerBuilder builder = new V1ContainerBuilder();
if (containerSecurityContexts.containsKey(name)) {
builder.withSecurityContext(containerSecurityContexts.get(name));
}
List<V1VolumeMount> containerVolumeMounts = new ArrayList<>();
for (V1VolumeMount mount : volumeMounts) {
if (readonlyDisks.containsKey(name) && readonlyDisks.get(name).contains(mount.getName())) {
containerVolumeMounts.add(new V1VolumeMount().readOnly(true).name(mount.getName()).mountPath(mount.getMountPath()).mountPropagation(mount.getMountPropagation()).subPath(mount.getSubPath()).subPathExpr(mount.getSubPathExpr()));
} else {
containerVolumeMounts.add(mount);
}
}
return builder.withName(cleanse(name, 254)).withImage(containerImage).withWorkingDir(workDir).withResources(resourceRequirements).withImagePullPolicy(imagePullPolicy).addAllToVolumeMounts(containerVolumeMounts).addAllToEnv(containerEnvironments).addToArgs(masterEnvContext.getRunnableArguments(runnableClass, args)).build();
}
Aggregations