/** @param workTokDir Token directory (common for multiple nodes). */ private void cleanupResources(File workTokDir) { RandomAccessFile lockFile = null; FileLock lock = null; try { lockFile = new RandomAccessFile(new File(workTokDir, LOCK_FILE_NAME), "rw"); lock = lockFile.getChannel().lock(); if (lock != null) processTokenDirectory(workTokDir); else if (log.isDebugEnabled()) log.debug( "Token directory is being processed concurrently: " + workTokDir.getAbsolutePath()); } catch (OverlappingFileLockException ignored) { if (log.isDebugEnabled()) log.debug( "Token directory is being processed concurrently: " + workTokDir.getAbsolutePath()); } catch (FileLockInterruptionException ignored) { Thread.currentThread().interrupt(); } catch (IOException e) { U.error(log, "Failed to process directory: " + workTokDir.getAbsolutePath(), e); } finally { U.releaseQuiet(lock); U.closeQuiet(lockFile); } }
/** @throws Exception If failed. */ @SuppressWarnings({"TypeMayBeWeakened"}) public void testCorrectAntGarTask() throws Exception { String tmpDirName = GridTestProperties.getProperty("ant.gar.tmpdir"); String srcDirName = GridTestProperties.getProperty("ant.gar.srcdir"); String baseDirName = tmpDirName + File.separator + System.currentTimeMillis() + "_0"; String metaDirName = baseDirName + File.separator + "META-INF"; String garFileName = baseDirName + ".gar"; String garDescDirName = U.resolveIgnitePath(GridTestProperties.getProperty("ant.gar.descriptor.dir")) .getAbsolutePath() + File.separator + "ignite.xml"; // Make base and META-INF dir. boolean mkdir = new File(baseDirName).mkdirs(); assert mkdir; mkdir = new File(metaDirName).mkdirs(); assert mkdir; // Make Gar file U.copy(new File(garDescDirName), new File(metaDirName + File.separator + "ignite.xml"), true); // Copy files to basedir U.copy(new File(srcDirName), new File(baseDirName), true); IgniteDeploymentGarAntTask garTask = new IgniteDeploymentGarAntTask(); Project garProject = new Project(); garProject.setName("Gar test project"); garTask.setDestFile(new File(garFileName)); garTask.setBasedir(new File(baseDirName)); garTask.setProject(garProject); garTask.execute(); File garFile = new File(garFileName); assert garFile.exists(); boolean res = checkStructure(garFile, true); assert res; }
/** * @param garFile GAR file. * @param hasDescr Whether GAR file has descriptor. * @throws IOException If GAR file is not found. * @return Whether GAR file structure is correct. */ private boolean checkStructure(File garFile, boolean hasDescr) throws IOException { ZipFile zip = new ZipFile(garFile); String descr = "META-INF/ignite.xml"; ZipEntry entry = zip.getEntry(descr); if (entry == null && !hasDescr) { if (log().isInfoEnabled()) { log() .info( "Process deployment without descriptor file [path=" + descr + ", file=" + garFile.getAbsolutePath() + ']'); } return true; } else if (entry != null && hasDescr) { InputStream in = null; try { in = new BufferedInputStream(zip.getInputStream(entry)); return true; } finally { U.close(in, log()); } } else return false; }
/** * Read block from file. * * @param file - File to read. * @param off - Marker position in file to start read from if {@code -1} read last blockSz bytes. * @param blockSz - Maximum number of chars to read. * @param lastModified - File last modification time. * @return Read file block. * @throws IOException In case of error. */ public static VisorFileBlock readBlock(File file, long off, int blockSz, long lastModified) throws IOException { RandomAccessFile raf = null; try { long fSz = file.length(); long fLastModified = file.lastModified(); long pos = off >= 0 ? off : Math.max(fSz - blockSz, 0); // Try read more that file length. if (fLastModified == lastModified && fSz != 0 && pos >= fSz) throw new IOException( "Trying to read file block with wrong offset: " + pos + " while file size: " + fSz); if (fSz == 0) return new VisorFileBlock(file.getPath(), pos, fLastModified, 0, false, EMPTY_FILE_BUF); else { int toRead = Math.min(blockSz, (int) (fSz - pos)); byte[] buf = new byte[toRead]; raf = new RandomAccessFile(file, "r"); raf.seek(pos); int cntRead = raf.read(buf, 0, toRead); if (cntRead != toRead) throw new IOException( "Count of requested and actually read bytes does not match [cntRead=" + cntRead + ", toRead=" + toRead + ']'); boolean zipped = buf.length > 512; return new VisorFileBlock( file.getPath(), pos, fSz, fLastModified, zipped, zipped ? zipBytes(buf) : buf); } } finally { U.close(raf, null); } }
/** * Check is text file. * * @param f file reference. * @param emptyOk default value if empty file. * @return Is text file. */ public static boolean textFile(File f, boolean emptyOk) { if (f.length() == 0) return emptyOk; String detected = VisorMimeTypes.getContentType(f); for (String mime : TEXT_MIME_TYPE) if (mime.equals(detected)) return true; return false; }
/** {@inheritDoc} */ @SuppressWarnings("deprecation") @Override public void start() throws IgniteException { if (sesFactory == null && F.isEmpty(hibernateCfgPath)) throw new IgniteException( "Either session factory or Hibernate configuration file is required by " + getClass().getSimpleName() + '.'); if (!F.isEmpty(hibernateCfgPath)) { if (sesFactory == null) { try { URL url = new URL(hibernateCfgPath); sesFactory = new Configuration().configure(url).buildSessionFactory(); } catch (MalformedURLException ignored) { // No-op. } if (sesFactory == null) { File cfgFile = new File(hibernateCfgPath); if (cfgFile.exists()) sesFactory = new Configuration().configure(cfgFile).buildSessionFactory(); } if (sesFactory == null) sesFactory = new Configuration().configure(hibernateCfgPath).buildSessionFactory(); if (sesFactory == null) throw new IgniteException( "Failed to resolve Hibernate configuration file: " + hibernateCfgPath); closeSesOnStop = true; } else U.warn( log, "Hibernate configuration file configured in " + getClass().getSimpleName() + " will be ignored (session factory is already set)."); } }
/** {@inheritDoc} */ @Override public void start() throws IgniteCheckedException { IpcSharedMemoryNativeLoader.load(log); pid = IpcSharedMemoryUtils.pid(); if (pid == -1) throw new IpcEndpointBindException("Failed to get PID of the current process."); if (size <= 0) throw new IpcEndpointBindException("Space size should be positive: " + size); String tokDirPath = this.tokDirPath; if (F.isEmpty(tokDirPath)) throw new IpcEndpointBindException("Token directory path is empty."); tokDirPath = tokDirPath + '/' + locNodeId.toString() + '-' + IpcSharedMemoryUtils.pid(); tokDir = U.resolveWorkDirectory(tokDirPath, false); if (port <= 0 || port >= 0xffff) throw new IpcEndpointBindException("Port value is illegal: " + port); try { srvSock = new ServerSocket(); // Always bind to loopback. srvSock.bind(new InetSocketAddress("127.0.0.1", port)); } catch (IOException e) { // Although empty socket constructor never throws exception, close it just in case. U.closeQuiet(srvSock); throw new IpcEndpointBindException( "Failed to bind shared memory IPC endpoint (is port already " + "in use?): " + port, e); } gcWorker = new GcWorker(gridName, "ipc-shmem-gc", log); new IgniteThread(gcWorker).start(); if (log.isInfoEnabled()) log.info( "IPC shared memory server endpoint started [port=" + port + ", tokDir=" + tokDir.getAbsolutePath() + ']'); }
/** @throws Exception if error occur. */ @SuppressWarnings("unchecked") private void checkGar() throws Exception { initGar = true; String garDir = "modules/extdata/p2p/deploy"; String garFileName = "p2p.gar"; File origGarPath = U.resolveIgnitePath(garDir + '/' + garFileName); File tmpPath = new File(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()); if (!tmpPath.mkdir()) throw new IOException("Can not create temp directory"); try { File newGarFile = new File(tmpPath, garFileName); U.copy(origGarPath, newGarFile, false); assert newGarFile.exists(); try { garFile = "file:///" + tmpPath.getAbsolutePath(); try { Ignite ignite1 = startGrid(1); Ignite ignite2 = startGrid(2); Integer res = ignite1 .compute() .<UUID, Integer>execute(TASK_NAME, ignite2.cluster().localNode().id()); assert res != null; } finally { stopGrid(1); stopGrid(2); } } finally { if (newGarFile != null && !newGarFile.delete()) error("Can not delete temp gar file"); } } finally { if (!tmpPath.delete()) error("Can not delete temp directory"); } }
/** * Finds all files in folder and in it's sub-tree of specified depth. * * @param file Starting folder * @param maxDepth Depth of the tree. If 1 - just look in the folder, no sub-folders. * @param filter file filter. * @return List of found files. */ public static List<VisorLogFile> fileTree(File file, int maxDepth, @Nullable FileFilter filter) { if (file.isDirectory()) { File[] files = (filter == null) ? file.listFiles() : file.listFiles(filter); if (files == null) return Collections.emptyList(); List<VisorLogFile> res = new ArrayList<>(files.length); for (File f : files) { if (f.isFile() && f.length() > 0) res.add(new VisorLogFile(f)); else if (maxDepth > 1) res.addAll(fileTree(f, maxDepth - 1, filter)); } return res; } return F.asList(new VisorLogFile(file)); }
/** @param workTokDir Token directory (common for multiple nodes). */ private void processTokenDirectory(File workTokDir) { for (File f : workTokDir.listFiles()) { if (!f.isDirectory()) { if (!f.getName().equals(LOCK_FILE_NAME)) { if (log.isDebugEnabled()) log.debug("Unexpected file: " + f.getName()); } continue; } if (f.equals(tokDir)) { if (log.isDebugEnabled()) log.debug("Skipping own token directory: " + tokDir.getName()); continue; } String name = f.getName(); int pid; try { pid = Integer.parseInt(name.substring(name.lastIndexOf('-') + 1)); } catch (NumberFormatException ignored) { if (log.isDebugEnabled()) log.debug("Failed to parse file name: " + name); continue; } // Is process alive? if (IpcSharedMemoryUtils.alive(pid)) { if (log.isDebugEnabled()) log.debug("Skipping alive node: " + pid); continue; } if (log.isDebugEnabled()) log.debug("Possibly stale token folder: " + f); // Process each token under stale token folder. File[] shmemToks = f.listFiles(); if (shmemToks == null) // Although this is strange, but is reproducible sometimes on linux. return; int rmvCnt = 0; try { for (File f0 : shmemToks) { if (log.isDebugEnabled()) log.debug("Processing token file: " + f0.getName()); if (f0.isDirectory()) { if (log.isDebugEnabled()) log.debug("Unexpected directory: " + f0.getName()); } // Token file format: gg-shmem-space-[auto_idx]-[other_party_pid]-[size] String[] toks = f0.getName().split("-"); if (toks.length != 6) { if (log.isDebugEnabled()) log.debug("Unrecognized token file: " + f0.getName()); continue; } int pid0; int size; try { pid0 = Integer.parseInt(toks[4]); size = Integer.parseInt(toks[5]); } catch (NumberFormatException ignored) { if (log.isDebugEnabled()) log.debug("Failed to parse file name: " + name); continue; } if (IpcSharedMemoryUtils.alive(pid0)) { if (log.isDebugEnabled()) log.debug("Skipping alive process: " + pid0); continue; } if (log.isDebugEnabled()) log.debug("Possibly stale token file: " + f0); IpcSharedMemoryUtils.freeSystemResources(f0.getAbsolutePath(), size); if (f0.delete()) { if (log.isDebugEnabled()) log.debug("Deleted file: " + f0.getName()); rmvCnt++; } else if (!f0.exists()) { if (log.isDebugEnabled()) log.debug("File has been concurrently deleted: " + f0.getName()); rmvCnt++; } else if (log.isDebugEnabled()) log.debug("Failed to delete file: " + f0.getName()); } } finally { // Assuming that no new files can appear, since if (rmvCnt == shmemToks.length) { U.delete(f); if (log.isDebugEnabled()) log.debug("Deleted empty token directory: " + f.getName()); } } } }