/** * Initializes store. * * @throws GridException If failed to initialize. */ private void init() throws GridException { if (initGuard.compareAndSet(false, true)) { if (log.isDebugEnabled()) log.debug("Initializing cache store."); try { if (sesFactory != null) // Session factory has been provided - nothing to do. return; if (!F.isEmpty(hibernateCfgPath)) { try { URL url = new URL(hibernateCfgPath); sesFactory = new Configuration().configure(url).buildSessionFactory(); if (log.isDebugEnabled()) log.debug("Configured session factory using URL: " + url); // Session factory has been successfully initialized. return; } catch (MalformedURLException e) { if (log.isDebugEnabled()) log.debug("Caught malformed URL exception: " + e.getMessage()); } // Provided path is not a valid URL. File? File cfgFile = new File(hibernateCfgPath); if (cfgFile.exists()) { sesFactory = new Configuration().configure(cfgFile).buildSessionFactory(); if (log.isDebugEnabled()) log.debug("Configured session factory using file: " + hibernateCfgPath); // Session factory has been successfully initialized. return; } // Provided path is not a file. Classpath resource? sesFactory = new Configuration().configure(hibernateCfgPath).buildSessionFactory(); if (log.isDebugEnabled()) log.debug("Configured session factory using classpath resource: " + hibernateCfgPath); } else { if (hibernateProps == null) { U.warn( log, "No Hibernate configuration has been provided for store (will use default)."); hibernateProps = new Properties(); hibernateProps.setProperty("hibernate.connection.url", DFLT_CONN_URL); hibernateProps.setProperty("hibernate.show_sql", DFLT_SHOW_SQL); hibernateProps.setProperty("hibernate.hbm2ddl.auto", DFLT_HBM2DDL_AUTO); } Configuration cfg = new Configuration(); cfg.setProperties(hibernateProps); assert resourceAvailable(MAPPING_RESOURCE); cfg.addResource(MAPPING_RESOURCE); sesFactory = cfg.buildSessionFactory(); if (log.isDebugEnabled()) log.debug("Configured session factory using properties: " + hibernateProps); } } catch (HibernateException e) { throw new GridException("Failed to initialize store.", e); } finally { initLatch.countDown(); } } else if (initLatch.getCount() > 0) U.await(initLatch); if (sesFactory == null) throw new GridException("Cache store was not properly initialized."); }
/** @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()); } } } }