From 0f93f359c5924142be84de6309aee16990f5d5ad Mon Sep 17 00:00:00 2001 From: Tigran Mkrtchyan Date: Mon, 29 Sep 2014 11:10:23 +0200 Subject: [PATCH] chimera: squash usedFiles() and usedSize() into a single query kill two birds with one stone Acked-by: Dmitry Litvintsev Target: master Require-book: no Require-notes: no (cherry picked from commit 2f921909fa24f16c667a8b7d6855782dd05839aa) Signed-off-by: Tigran Mkrtchyan --- .../java/org/dcache/chimera/FsSqlDriver.java | 50 ++++---------- .../main/java/org/dcache/chimera/JdbcFs.java | 69 ++++--------------- 2 files changed, 25 insertions(+), 94 deletions(-) diff --git a/modules/chimera/src/main/java/org/dcache/chimera/FsSqlDriver.java b/modules/chimera/src/main/java/org/dcache/chimera/FsSqlDriver.java index dab032d8ed4..214556d54ac 100644 --- a/modules/chimera/src/main/java/org/dcache/chimera/FsSqlDriver.java +++ b/modules/chimera/src/main/java/org/dcache/chimera/FsSqlDriver.java @@ -85,61 +85,35 @@ protected FsSqlDriver() { } } - private static final String sqlUsedSpace = "SELECT SUM(isize) AS usedSpace FROM t_inodes WHERE itype=32768"; - /** - * - * @param dbConnection - * @return total space used by files - * @throws SQLException - */ - long usedSpace(Connection dbConnection) throws SQLException { - long usedSpace = 0; - PreparedStatement stUsedSpace = null; - ResultSet rs = null; - try { - - stUsedSpace = dbConnection.prepareStatement(sqlUsedSpace); - rs = stUsedSpace.executeQuery(); - if (rs.next()) { - usedSpace = rs.getLong("usedSpace"); - } - - } finally { - SqlHelper.tryToClose(rs); - SqlHelper.tryToClose(stUsedSpace); - } - - return usedSpace; - } - private static final String sqlUsedFiles = "SELECT count(ipnfsid) AS usedFiles FROM t_inodes WHERE itype=32768"; + private static final String sqlGetFsStat = "SELECT count(ipnfsid) AS usedFiles, SUM(isize) AS usedSpace FROM t_inodes WHERE itype=32768"; /** - * + * Get FsStat for a given filesystem. * @param dbConnection - * @return total number of files + * @return fsStat * @throws SQLException */ - long usedFiles(Connection dbConnection) throws SQLException { + FsStat getFsStat(Connection dbConnection) throws SQLException { + long usedFiles = 0; - PreparedStatement stUsedFiles = null; + long usedSpace = 0; + PreparedStatement stFsStat = null; ResultSet rs = null; try { - - stUsedFiles = dbConnection.prepareStatement(sqlUsedFiles); - - rs = stUsedFiles.executeQuery(); + stFsStat = dbConnection.prepareStatement(sqlGetFsStat); + rs = stFsStat.executeQuery(); if (rs.next()) { usedFiles = rs.getLong("usedFiles"); + usedSpace = rs.getLong("usedSpace"); } - } finally { SqlHelper.tryToClose(rs); - SqlHelper.tryToClose(stUsedFiles); + SqlHelper.tryToClose(stFsStat); } - return usedFiles; + return new FsStat(JdbcFs.AVAILABLE_SPACE, JdbcFs.TOTAL_FILES, usedSpace, usedFiles); } /** diff --git a/modules/chimera/src/main/java/org/dcache/chimera/JdbcFs.java b/modules/chimera/src/main/java/org/dcache/chimera/JdbcFs.java index 31bf1b0941c..061e2fef5e1 100644 --- a/modules/chimera/src/main/java/org/dcache/chimera/JdbcFs.java +++ b/modules/chimera/src/main/java/org/dcache/chimera/JdbcFs.java @@ -95,11 +95,11 @@ public class JdbcFs implements FileSystemProvider { /** * available space (1 Exabyte) */ - private static final long AVAILABLE_SPACE = 1152921504606846976L; + static final long AVAILABLE_SPACE = 1152921504606846976L; /** * total files */ - private static final long TOTAL_FILES = 62914560L; + static final long TOTAL_FILES = 62914560L; /** * maximal length of an object name in a directory. @@ -141,54 +141,6 @@ private FsInode getWormID() throws ChimeraFsException { //// Fs operations //// ///////////////////////////////////////////////////////// - public long usedSpace() throws ChimeraFsException { - Connection dbConnection; - try { - // get from pool - dbConnection = _dbConnectionsPool.getConnection(); - } catch (SQLException e) { - throw new BackEndErrorHimeraFsException(e.getMessage()); - } - - long usedSpace = 0; - try { - // read only - dbConnection.setAutoCommit(true); - usedSpace = _sqlDriver.usedSpace(dbConnection); - } catch (SQLException se) { - _log.error("usedSpace: ", se); - throw new IOHimeraFsException(se.getMessage()); - } finally { - tryToClose(dbConnection); - } - - return usedSpace; - } - - public long usedFiles() throws ChimeraFsException { - Connection dbConnection; - try { - // get from pool - dbConnection = _dbConnectionsPool.getConnection(); - } catch (SQLException e) { - throw new BackEndErrorHimeraFsException(e.getMessage()); - } - - long usedFiles = 0; - try { - // read only - dbConnection.setAutoCommit(true); - usedFiles = _sqlDriver.usedFiles(dbConnection); - } catch (SQLException se) { - _log.error("usedFiles: ", se); - throw new IOHimeraFsException(se.getMessage()); - } finally { - tryToClose(dbConnection); - } - - return usedFiles; - } - @Override public FsInode createLink(String src, String dest) throws ChimeraFsException { @@ -2741,13 +2693,18 @@ static class FsStatCache { _fs = fs; } - public synchronized FsStat getFsStat() throws ChimeraFsException { + public synchronized FsStat getFsStat(DataSource dbConnectionsPool, FsSqlDriver driver) throws ChimeraFsException { if (_fsStatLastUpdate == 0 || _fsStatLastUpdate + _fsStateLifetime < System.currentTimeMillis()) { - _fsStatCached = new FsStat(AVAILABLE_SPACE, - TOTAL_FILES, - _fs.usedSpace(), - _fs.usedFiles()); + Connection dbConnection = null; + try { + dbConnection = dbConnectionsPool.getConnection(); + _fsStatCached = driver.getFsStat(dbConnection); + } catch (SQLException e) { + throw new IOHimeraFsException(e.getMessage()); + } finally { + tryToClose(dbConnection); + } _log.debug("updateing cached value of FsStat"); _fsStatLastUpdate = System.currentTimeMillis(); } else { @@ -2760,7 +2717,7 @@ public synchronized FsStat getFsStat() throws ChimeraFsException { @Override public FsStat getFsStat() throws ChimeraFsException { - return _fsStatCache.getFsStat(); + return _fsStatCache.getFsStat(_dbConnectionsPool, _sqlDriver); } ///////////////////////////////////////////////////////////////