001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.reef.runtime.local.client; 020 021import org.apache.commons.lang.StringUtils; 022import org.apache.reef.runtime.common.files.ClasspathProvider; 023import org.apache.reef.runtime.common.files.REEFFileNames; 024import org.apache.reef.runtime.common.launch.JavaLaunchCommandBuilder; 025import org.apache.reef.runtime.local.process.LoggingRunnableProcessObserver; 026import org.apache.reef.runtime.local.process.RunnableProcess; 027import org.apache.reef.tang.annotations.Parameter; 028 029import javax.inject.Inject; 030import java.io.File; 031import java.util.Collections; 032import java.util.List; 033import java.util.concurrent.ExecutorService; 034import java.util.logging.Level; 035import java.util.logging.Logger; 036import org.apache.reef.runtime.common.launch.parameters.DriverLaunchCommandPrefix; 037 038/** 039 * Launcher for a already prepared driver folder. 040 */ 041public class PreparedDriverFolderLauncher { 042 043 /** 044 * The name of the folder for the driver within the Job folder. 045 */ 046 public static final String DRIVER_FOLDER_NAME = "driver"; 047 048 private final ExecutorService executor; 049 private final REEFFileNames fileNames; 050 private final ClasspathProvider classpath; 051 private final List<String> commandPrefixList; 052 053 /** 054 * The (hard-coded) amount of memory to be used for the driver. 055 */ 056 public static final int DRIVER_MEMORY = 512; 057 058 private static final Logger LOG = Logger.getLogger(PreparedDriverFolderLauncher.class.getName()); 059 060 @Inject 061 PreparedDriverFolderLauncher(final ExecutorService executor, final REEFFileNames fileNames, 062 @Parameter(DriverLaunchCommandPrefix.class) final List<String> commandPrefixList, 063 final ClasspathProvider classpath) { 064 this.executor = executor; 065 this.fileNames = fileNames; 066 this.classpath = classpath; 067 this.commandPrefixList = commandPrefixList; 068 } 069 070 /** 071 * Launches the driver prepared in driverFolder. 072 * 073 * @param driverFolder 074 * @param jobId 075 * @param clientRemoteId 076 */ 077 public void launch(final File driverFolder, final String jobId, final String clientRemoteId) { 078 assert driverFolder.isDirectory(); 079 080 final List<String> command = makeLaunchCommand(jobId, clientRemoteId); 081 082 final RunnableProcess process = new RunnableProcess(command, 083 "driver", 084 driverFolder, 085 new LoggingRunnableProcessObserver(), 086 this.fileNames.getDriverStdoutFileName(), 087 this.fileNames.getDriverStderrFileName()); 088 this.executor.submit(process); 089 this.executor.shutdown(); 090 } 091 092 private List<String> makeLaunchCommand(final String jobId, final String clientRemoteId) { 093 094 final List<String> command = new JavaLaunchCommandBuilder(commandPrefixList) 095 .setConfigurationFilePaths(Collections.singletonList(this.fileNames.getDriverConfigurationPath())) 096 .setClassPath(this.classpath.getDriverClasspath()) 097 .setMemory(DRIVER_MEMORY) 098 .build(); 099 100 if (LOG.isLoggable(Level.FINEST)) { 101 LOG.log(Level.FINEST, "REEF app command: {0}", StringUtils.join(command, ' ')); 102 } 103 return command; 104 } 105 106}