Skip to content

Commit

Permalink
IGNITE-3597: Removed static work directory.
Browse files Browse the repository at this point in the history
  • Loading branch information
vozerov-gridgain committed Oct 4, 2016
1 parent bba019f commit a92f20b
Show file tree
Hide file tree
Showing 52 changed files with 209 additions and 337 deletions.
Expand Up @@ -408,7 +408,9 @@ protected GridKernalContextImpl(
this.restExecSvc = restExecSvc;
this.callbackExecSvc = callbackExecSvc;

marshCtx = new MarshallerContextImpl(plugins);
String workDir = U.workDirectory(cfg.getWorkDirectory(), cfg.getIgniteHome());

marshCtx = new MarshallerContextImpl(workDir, plugins);

try {
spring = SPRING.create(false);
Expand Down
Expand Up @@ -1823,7 +1823,10 @@ private IgniteConfiguration initializeConfiguration(IgniteConfiguration cfg)
// If user provided IGNITE_HOME - set it as a system property.
U.setIgniteHome(ggHome);

U.setWorkDirectory(cfg.getWorkDirectory(), ggHome);
// Correctly resolve work directory and set it back to configuration.
String workDir = U.workDirectory(cfg.getWorkDirectory(), ggHome);

myCfg.setWorkDirectory(workDir);

// Ensure invariant.
// It's a bit dirty - but this is a result of late refactoring
Expand All @@ -1834,7 +1837,7 @@ private IgniteConfiguration initializeConfiguration(IgniteConfiguration cfg)

myCfg.setNodeId(nodeId);

IgniteLogger cfgLog = initLogger(cfg.getGridLogger(), nodeId);
IgniteLogger cfgLog = initLogger(cfg.getGridLogger(), nodeId, workDir);

assert cfgLog != null;

Expand Down Expand Up @@ -2095,11 +2098,13 @@ private void initializeDefaultSpi(IgniteConfiguration cfg) {
/**
* @param cfgLog Configured logger.
* @param nodeId Local node ID.
* @param workDir Work directory.
* @return Initialized logger.
* @throws IgniteCheckedException If failed.
*/
@SuppressWarnings("ErrorNotRethrown")
private IgniteLogger initLogger(@Nullable IgniteLogger cfgLog, UUID nodeId) throws IgniteCheckedException {
private IgniteLogger initLogger(@Nullable IgniteLogger cfgLog, UUID nodeId, String workDir)
throws IgniteCheckedException {
try {
Exception log4jInitErr = null;

Expand Down Expand Up @@ -2157,6 +2162,10 @@ private IgniteLogger initLogger(@Nullable IgniteLogger cfgLog, UUID nodeId) thro
cfgLog = new JavaLogger();
}

// Special handling for Java logger which requires work directory.
if (cfgLog instanceof JavaLogger)
((JavaLogger)cfgLog).setWorkDirectory(workDir);

// Set node IDs for all file appenders.
if (cfgLog instanceof LoggerNodeIdAware)
((LoggerNodeIdAware)cfgLog).setNodeId(nodeId);
Expand Down
Expand Up @@ -72,13 +72,14 @@ public class MarshallerContextImpl extends MarshallerContextAdapter {
private ContinuousQueryListener lsnr;

/**
* @param igniteWorkDir Ignite work directory.
* @param plugins Plugins.
* @throws IgniteCheckedException In case of error.
*/
public MarshallerContextImpl(List<PluginProvider> plugins) throws IgniteCheckedException {
public MarshallerContextImpl(String igniteWorkDir, List<PluginProvider> plugins) throws IgniteCheckedException {
super(plugins);

workDir = U.resolveWorkDirectory("marshaller", false);
workDir = U.resolveWorkDirectory(igniteWorkDir, "marshaller", false);
}

/**
Expand Down
Expand Up @@ -52,4 +52,11 @@ public interface HadoopHelper {
* @return Input stream.
*/
@Nullable public InputStream loadClassBytes(ClassLoader ldr, String clsName);

/**
* Get work directory.
*
* @return Work directory.
*/
public String workDirectory();
}
Expand Up @@ -99,4 +99,9 @@ public interface HadoopJob {
* Cleans up the job staging directory.
*/
public void cleanupStagingDirectory();

/**
* @return Ignite work directory.
*/
public String igniteWorkDirectory();
}
Expand Up @@ -57,6 +57,11 @@ public HadoopNoopHelper(GridKernalContext ctx) {
throw unsupported();
}

/** {@inheritDoc} */
@Override public String workDirectory() {
throw unsupported();
}

/**
* @return Exception.
*/
Expand Down
Expand Up @@ -153,7 +153,7 @@ public void start() throws IgniteCheckedException {
* @return Server endpoint.
* @throws IgniteCheckedException If failed.
*/
private static IpcServerEndpoint createEndpoint(IgfsIpcEndpointConfiguration endpointCfg, boolean mgmt)
private IpcServerEndpoint createEndpoint(IgfsIpcEndpointConfiguration endpointCfg, boolean mgmt)
throws IgniteCheckedException {
A.notNull(endpointCfg, "endpointCfg");

Expand All @@ -164,7 +164,8 @@ private static IpcServerEndpoint createEndpoint(IgfsIpcEndpointConfiguration end

switch (typ) {
case SHMEM: {
IpcSharedMemoryServerEndpoint endpoint = new IpcSharedMemoryServerEndpoint();
IpcSharedMemoryServerEndpoint endpoint =
new IpcSharedMemoryServerEndpoint(igfsCtx.kernalContext().config().getWorkDirectory());

endpoint.setPort(endpointCfg.getPort());
endpoint.setSize(endpointCfg.getMemorySize());
Expand Down
Expand Up @@ -17,7 +17,6 @@

package org.apache.ignite.internal.processors.platform.cpp;

import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.IgniteException;
import org.apache.ignite.binary.BinaryIdMapper;
import org.apache.ignite.binary.BinaryBasicIdMapper;
Expand Down Expand Up @@ -130,17 +129,7 @@ else if (!(marsh instanceof BinaryMarshaller))
// Set Ignite home so that marshaller context works.
String ggHome = igniteCfg.getIgniteHome();

if (ggHome == null)
ggHome = U.getIgniteHome();
else
// If user provided IGNITE_HOME - set it as a system property.
if (ggHome != null)
U.setIgniteHome(ggHome);

try {
U.setWorkDirectory(igniteCfg.getWorkDirectory(), ggHome);
}
catch (IgniteCheckedException e) {
throw U.convertException(e);
}
}
}
Expand Up @@ -17,7 +17,6 @@

package org.apache.ignite.internal.processors.platform.dotnet;

import org.apache.ignite.IgniteCheckedException;
import org.apache.ignite.IgniteException;
import org.apache.ignite.binary.BinaryBasicIdMapper;
import org.apache.ignite.binary.BinaryBasicNameMapper;
Expand Down Expand Up @@ -151,19 +150,9 @@ else if (!(marsh instanceof BinaryMarshaller))
// Set Ignite home so that marshaller context works.
String ggHome = igniteCfg.getIgniteHome();

if (ggHome == null)
ggHome = U.getIgniteHome();
else
// If user provided IGNITE_HOME - set it as a system property.
if (ggHome != null)
U.setIgniteHome(ggHome);

try {
U.setWorkDirectory(igniteCfg.getWorkDirectory(), ggHome);
}
catch (IgniteCheckedException e) {
throw U.convertException(e);
}

// 4. Callback to .Net.
prepare(igniteCfg, dotNetCfg0);
}
Expand Down
Expand Up @@ -786,12 +786,16 @@ public static Object readInvocationResult(PlatformContext ctx, BinaryRawReaderEx
@SuppressWarnings("deprecation")
public static GridBinaryMarshaller marshaller() {
try {
IgniteConfiguration cfg = new IgniteConfiguration();

BinaryContext ctx =
new BinaryContext(BinaryNoopMetadataHandler.instance(), new IgniteConfiguration(), new NullLogger());
new BinaryContext(BinaryNoopMetadataHandler.instance(), cfg, new NullLogger());

BinaryMarshaller marsh = new BinaryMarshaller();

marsh.setContext(new MarshallerContextImpl(null));
String workDir = U.workDirectory(cfg.getWorkDirectory(), cfg.getIgniteHome());

marsh.setContext(new MarshallerContextImpl(workDir, null));

ctx.configure(marsh, new IgniteConfiguration());

Expand Down
Expand Up @@ -305,9 +305,6 @@ public abstract class IgniteUtils {
/** Project home directory. */
private static volatile GridTuple<String> ggHome;

/** Project work directory. */
private static volatile String igniteWork;

/** OS JDK string. */
private static String osJdkStr;

Expand Down Expand Up @@ -8826,54 +8823,56 @@ public static String addressesAsString(Collection<String> addrs, Collection<Stri
}

/**
* Get default work directory.
*
* @return Default work directory.
*/
public static String defaultWorkDirectory() throws IgniteCheckedException {
return workDirectory(null, null);
}

/**
* Get work directory for the given user-provided work directory and Ignite home.
*
* @param userWorkDir Ignite work folder provided by user.
* @param userIgniteHome Ignite home folder provided by user.
*/
public static void setWorkDirectory(@Nullable String userWorkDir, @Nullable String userIgniteHome)
public static String workDirectory(@Nullable String userWorkDir, @Nullable String userIgniteHome)
throws IgniteCheckedException {
String igniteWork0 = igniteWork;

if (igniteWork0 == null) {
synchronized (IgniteUtils.class) {
// Double check.
igniteWork0 = igniteWork;

if (igniteWork0 != null)
return;
if (userIgniteHome == null)
userIgniteHome = getIgniteHome();

File workDir;
File workDir;

if (!F.isEmpty(userWorkDir))
workDir = new File(userWorkDir);
else if (!F.isEmpty(IGNITE_WORK_DIR))
workDir = new File(IGNITE_WORK_DIR);
else if (!F.isEmpty(userIgniteHome))
workDir = new File(userIgniteHome, "work");
else {
String tmpDirPath = System.getProperty("java.io.tmpdir");
if (!F.isEmpty(userWorkDir))
workDir = new File(userWorkDir);
else if (!F.isEmpty(IGNITE_WORK_DIR))
workDir = new File(IGNITE_WORK_DIR);
else if (!F.isEmpty(userIgniteHome))
workDir = new File(userIgniteHome, "work");
else {
String tmpDirPath = System.getProperty("java.io.tmpdir");

if (tmpDirPath == null)
throw new IgniteCheckedException("Failed to create work directory in OS temp " +
"(property 'java.io.tmpdir' is null).");
if (tmpDirPath == null)
throw new IgniteCheckedException("Failed to create work directory in OS temp " +
"(property 'java.io.tmpdir' is null).");

workDir = new File(tmpDirPath, "ignite" + File.separator + "work");
}
workDir = new File(tmpDirPath, "ignite" + File.separator + "work");
}

if (!workDir.isAbsolute())
throw new IgniteCheckedException("Work directory path must be absolute: " + workDir);
if (!workDir.isAbsolute())
throw new IgniteCheckedException("Work directory path must be absolute: " + workDir);

if (!mkdirs(workDir))
throw new IgniteCheckedException("Work directory does not exist and cannot be created: " + workDir);
if (!mkdirs(workDir))
throw new IgniteCheckedException("Work directory does not exist and cannot be created: " + workDir);

if (!workDir.canRead())
throw new IgniteCheckedException("Cannot read from work directory: " + workDir);
if (!workDir.canRead())
throw new IgniteCheckedException("Cannot read from work directory: " + workDir);

if (!workDir.canWrite())
throw new IgniteCheckedException("Cannot write to work directory: " + workDir);
if (!workDir.canWrite())
throw new IgniteCheckedException("Cannot write to work directory: " + workDir);

igniteWork = workDir.getAbsolutePath();
}
}
return workDir.getAbsolutePath();
}

/**
Expand All @@ -8883,31 +8882,24 @@ public static void nullifyHomeDirectory() {
ggHome = null;
}

/**
* Nullifies work directory. For test purposes only.
*/
public static void nullifyWorkDirectory() {
igniteWork = null;
}

/**
* Resolves work directory.
*
* @param workDir Work directory.
* @param path Path to resolve.
* @param delIfExist Flag indicating whether to delete the specify directory or not.
* @return Resolved work directory.
* @throws IgniteCheckedException If failed.
*/
public static File resolveWorkDirectory(String path, boolean delIfExist) throws IgniteCheckedException {
public static File resolveWorkDirectory(String workDir, String path, boolean delIfExist)
throws IgniteCheckedException {
File dir = new File(path);

if (!dir.isAbsolute()) {
String ggWork0 = igniteWork;

if (F.isEmpty(ggWork0))
if (F.isEmpty(workDir))
throw new IgniteCheckedException("Failed to resolve path (work directory has not been set): " + path);

dir = new File(ggWork0, dir.getPath());
dir = new File(workDir, dir.getPath());
}

if (delIfExist && dir.exists()) {
Expand Down
Expand Up @@ -120,6 +120,9 @@ public class IpcSharedMemoryServerEndpoint implements IpcServerEndpoint {
/** Grid name. */
private String gridName;

/** Work directory. */
private final String workDir;

/** Flag allowing not to print out of resources warning. */
private boolean omitOutOfResourcesWarn;

Expand All @@ -139,9 +142,11 @@ public class IpcSharedMemoryServerEndpoint implements IpcServerEndpoint {
/**
* Use this constructor when dependencies could be injected
* with {@link GridResourceProcessor#injectGeneric(Object)}.
*
* @param workDir Work directory.
*/
public IpcSharedMemoryServerEndpoint() {
// No-op.
public IpcSharedMemoryServerEndpoint(String workDir) {
this.workDir = workDir;
}

/**
Expand All @@ -150,11 +155,13 @@ public IpcSharedMemoryServerEndpoint() {
* @param log Log.
* @param locNodeId Node id.
* @param gridName Grid name.
* @param workDir Work directory.
*/
public IpcSharedMemoryServerEndpoint(IgniteLogger log, UUID locNodeId, String gridName) {
public IpcSharedMemoryServerEndpoint(IgniteLogger log, UUID locNodeId, String gridName, String workDir) {
this.log = log;
this.locNodeId = locNodeId;
this.gridName = gridName;
this.workDir = workDir;
}

/** @param omitOutOfResourcesWarn If {@code true}, out of resources warning will not be printed by server. */
Expand All @@ -181,7 +188,7 @@ public void omitOutOfResourcesWarning(boolean omitOutOfResourcesWarn) {

tokDirPath = tokDirPath + '/' + locNodeId.toString() + '-' + IpcSharedMemoryUtils.pid();

tokDir = U.resolveWorkDirectory(tokDirPath, false);
tokDir = U.resolveWorkDirectory(workDir, tokDirPath, false);

if (port <= 0 || port >= 0xffff)
throw new IpcEndpointBindException("Port value is illegal: " + port);
Expand Down

0 comments on commit a92f20b

Please sign in to comment.