Skip to content

Commit

Permalink
chimera: squash usedFiles() and usedSize() into a single query
Browse files Browse the repository at this point in the history
kill two birds with one stone

Acked-by: Dmitry Litvintsev
Target: master
Require-book: no
Require-notes: no
(cherry picked from commit 2f92190)
Signed-off-by: Tigran Mkrtchyan <tigran.mkrtchyan@desy.de>
  • Loading branch information
kofemann committed Mar 13, 2015
1 parent f435d6c commit 0f93f35
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 94 deletions.
50 changes: 12 additions & 38 deletions modules/chimera/src/main/java/org/dcache/chimera/FsSqlDriver.java
Expand Up @@ -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);
}

/**
Expand Down
69 changes: 13 additions & 56 deletions modules/chimera/src/main/java/org/dcache/chimera/JdbcFs.java
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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 {
Expand All @@ -2760,7 +2717,7 @@ public synchronized FsStat getFsStat() throws ChimeraFsException {

@Override
public FsStat getFsStat() throws ChimeraFsException {
return _fsStatCache.getFsStat();
return _fsStatCache.getFsStat(_dbConnectionsPool, _sqlDriver);
}

///////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 0f93f35

Please sign in to comment.