Skip to content

Commit

Permalink
spacemanager: Mover DB schema management to Liquibase
Browse files Browse the repository at this point in the history
Target: trunk
Require-notes: yes
Require-book: yes
Acked-by: Dmitry Litvintsev <litvinse@fnal.gov>
Patch: http://rb.dcache.org/r/6368/
  • Loading branch information
gbehrmann committed Jan 10, 2014
1 parent 2cbcb17 commit 877a497
Show file tree
Hide file tree
Showing 8 changed files with 260 additions and 398 deletions.
Expand Up @@ -47,7 +47,7 @@

public class FileIO extends IoPackage<File> {

public static final String SRM_SPACEFILE_TABLE = ManagerSchemaConstants.SpaceFileTableName;
public static final String SRM_SPACEFILE_TABLE = ManagerSchemaConstants.SPACE_FILE_TABLE_NAME;
public static final String SELECT_BY_SPACERESERVATION_ID =
"SELECT * FROM "+SRM_SPACEFILE_TABLE+" WHERE spacereservationid = ?";
public static final String SELECT_BY_ID="SELECT * FROM "+
Expand Down
286 changes: 20 additions & 266 deletions modules/dcache/src/main/java/diskCacheV111/services/space/Manager.java

Large diffs are not rendered by default.

@@ -1,26 +1,9 @@
package diskCacheV111.services.space;

/**
* Database storage related variables.
*/
public class ManagerSchemaConstants {
/*
* Database storage related variables
*/
public static final int currentSchemaVersion = 2;
public static final String SpaceManagerSchemaVersionTableName =
"srmspacemanagerschemaversion";

public static final String selectSpaceManagerSchemaVersion=
"select version from "+SpaceManagerSchemaVersionTableName;

public static final String POPULATE_USED_SPACE_IN_SRMSPACE_TABLE =
" update srmspace set usedspaceinbytes=( "+
" select coalesce(sum(sf.sizeinbytes),0) "+
" from srmspace s left outer join srmspacefile sf on "+
" s.id=sf.spacereservationid and sf.state=2 and s.id=srmspace.id),"+
" allocatedspaceinbytes= ("+
" select coalesce(sum(sf.sizeinbytes),0) "+
" from srmspace s left outer join srmspacefile sf on "+
" s.id=sf.spacereservationid and sf.state<2 and s.id=srmspace.id)";

public static final String POPULATE_USED_SPACE_IN_SRMSPACE_TABLE_BY_ID =
" update srmspace set usedspaceinbytes=( "+
" select coalesce(sum(sf.sizeinbytes),0) "+
Expand All @@ -31,123 +14,22 @@ public class ManagerSchemaConstants {
" from srmspace s left outer join srmspacefile sf on "+
" s.id=sf.spacereservationid and sf.state<2 and s.id=?) where srmspace.id=?";

public static final String POPULATE_RESERVED_SPACE_IN_SRMLINKGROUP_TABLE =
" update srmlinkgroup set reservedspaceinbytes=( "+
" select coalesce(sum(s.sizeinbytes-s.usedspaceinbytes),0) "+
" from srmlinkgroup lg left outer join srmspace s on "+
" s.linkGroupId=lg.id and lg.id=srmlinkgroup.id and s.state=0) ";

public static final String POPULATE_RESERVED_SPACE_IN_SRMLINKGROUP_TABLE_BY_ID =
" update srmlinkgroup set reservedspaceinbytes=( "+
" select coalesce(sum(s.sizeinbytes-s.usedspaceinbytes),0) "+
" from srmlinkgroup lg left outer join srmspace s on "+
" s.linkGroupId=lg.id and lg.id=? and s.state=0) where srmlinkgroup.id=?";

public static final String SpaceManagerNextIdTableName =
public static final String SPACE_MANAGER_NEXT_ID_TABLE_NAME =
"srmspacemanagernextid";
public static final String LinkGroupTableName =
"srmLinkGroup".toLowerCase();
public static final String LinkGroupVOsTableName =
public static final String LINK_GROUP_VOS_TABLE_NAME =
"srmLinkGroupVOs".toLowerCase();
public static final String RetentionPolicyTableName =
public static final String RETENTION_POLICY_TABLE_NAME =
"srmRetentionPolicy".toLowerCase();
public static final String AccessLatencyTableName =
public static final String ACCESS_LATENCY_TABLE_NAME =
"srmAccessLatency".toLowerCase();
public static final String SpaceTableName =
public static final String SPACE_TABLE_NAME =
"srmSpace".toLowerCase();
public static final String SpaceFileTableName =
public static final String SPACE_FILE_TABLE_NAME =
"srmSpaceFile".toLowerCase();
protected static final String stringType=" VARCHAR(32672) ";
protected static final String longType=" BIGINT ";
protected static final String intType=" INTEGER ";
protected static final String dateTimeType= " TIMESTAMP ";
protected static final String booleanType= " INT ";
public static final String CreateSpaceManagerSchemaVersionTable =
"CREATE TABLE "+SpaceManagerSchemaVersionTableName+
" ( version "+ intType + " )";
public static final String CreateSpaceManagerNextIdTable =
"CREATE TABLE "+SpaceManagerNextIdTableName+
" ( NextToken "+ longType + " )";

public static final String CreateLinkGroupTable =
"CREATE TABLE "+ LinkGroupTableName+" ( "+
" id "+longType+" NOT NULL PRIMARY KEY "+
", name"+stringType+" " +
", freeSpaceInBytes "+longType+" "+
", lastUpdateTime "+longType +
", onlineAllowed"+booleanType+" "+
", nearlineAllowed"+booleanType+" "+
", replicaAllowed"+booleanType+" "+
", outputAllowed"+booleanType+" "+
", custodialAllowed"+booleanType+" "+
", reservedspaceinbytes "+longType+" "+
")";

public static final String CreateLinkGroupVOsTable =
"CREATE TABLE "+ LinkGroupVOsTableName+" ( "+
" VOGroup "+stringType+" NOT NULL "+
", VORole "+stringType+" NOT NULL "+
", linkGroupId "+longType+" NOT NULL "+
", PRIMARY KEY (VOGroup, VORole, linkGroupId) "+
", CONSTRAINT fk_"+LinkGroupVOsTableName+
"_L FOREIGN KEY (linkGroupId) REFERENCES "+
LinkGroupTableName +" (id) "+
" ON DELETE RESTRICT"+
")";

public static final String CreateRetentionPolicyTable =
"CREATE TABLE "+ RetentionPolicyTableName+" ( "+
" id "+intType+" NOT NULL PRIMARY KEY "+
", name "+stringType+" )";

public static final String CreateAccessLatencyTable =
"CREATE TABLE "+ AccessLatencyTableName+" ( "+
" id "+intType+" NOT NULL PRIMARY KEY "+
", name "+stringType+" )";

public static final String CreateSpaceTable =
"CREATE TABLE "+ SpaceTableName+" ( "+
" id "+longType+" NOT NULL PRIMARY KEY "+
", voGroup "+stringType+" "+
", voRole "+stringType+" "+
", retentionPolicy "+intType+" "+
", accessLatency "+intType+" "+
", linkGroupId "+longType+" "+
", sizeInBytes "+longType+" "+
", creationTime "+longType+" "+
", lifetime "+longType+" "+
", description "+stringType+" "+
", state "+intType+" "+
", usedspaceinbytes "+longType+" "+
", allocatedspaceinbytes "+longType+" "+
", CONSTRAINT fk_"+SpaceTableName+
"_L FOREIGN KEY (linkGroupId) REFERENCES "+
LinkGroupTableName +" (id) "+
", CONSTRAINT fk_"+SpaceTableName+
"_A FOREIGN KEY (accessLatency) REFERENCES "+
AccessLatencyTableName +" (id) "+
", CONSTRAINT fk_"+SpaceTableName+
"_R FOREIGN KEY (retentionPolicy) REFERENCES "+
RetentionPolicyTableName +" (id) "+
" ON DELETE RESTRICT"+
")";

public static final String CreateSpaceFileTable =
"CREATE TABLE "+ SpaceFileTableName+" ( "+
" id "+longType+" NOT NULL PRIMARY KEY "+
", voGroup "+stringType+" "+
", voRole "+stringType+" "+
", spaceReservationId "+longType+" "+
", sizeInBytes "+longType+" "+
", creationTime "+longType+" "+
", lifetime "+longType+" "+
", pnfsPath "+stringType+" "+
", pnfsId "+stringType+" "+
", state "+intType+" "+
", deleted "+intType+" "+
", CONSTRAINT fk_"+SpaceFileTableName+
"_L FOREIGN KEY (spaceReservationId) REFERENCES "+
SpaceTableName +" (id) "+
" ON DELETE RESTRICT"+
")";
}
Expand Up @@ -47,7 +47,7 @@


public class SpaceReservationIO extends IoPackage<Space> {
public static final String SRM_SPACE_TABLE = ManagerSchemaConstants.SpaceTableName;
public static final String SRM_SPACE_TABLE = ManagerSchemaConstants.SPACE_TABLE_NAME;
public static final String INSERT = "INSERT INTO "+SRM_SPACE_TABLE+
" (id,vogroup,vorole,retentionpolicy,accesslatency,linkgroupid,"+
"sizeinbytes,creationtime,lifetime,description,state,usedspaceinbytes,allocatedspaceinbytes)"+
Expand Down
@@ -0,0 +1,199 @@
<?xml version="1.0" encoding="UTF-8"?>

<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd">

<changeSet id="1" author="behrmann">
<preConditions onFailMessage="Cannot upgrade directly from versions older than 1.9.5">
<or>
<not><tableExists tableName="srmspacemanagerschemaversion"/></not>
<sqlCheck expectedResult="3">SELECT version FROM srmspacemanagerschemaversion</sqlCheck>
<sqlCheck expectedResult="4">SELECT version FROM srmspacemanagerschemaversion</sqlCheck>
</or>
</preConditions>
<comment>Ensure that we only upgrade from schema version 3 or 4</comment>
</changeSet>

<changeSet id="2" author="behrmann">
<preConditions onFail="MARK_RAN" onFailMessage="Not creating space manager schema as it already exists (this is not an error)">
<not><tableExists tableName="srmspacemanagerschemaversion"/></not>
</preConditions>
<comment>Create base schema</comment>

<createTable tableName="srmspacemanagernextid">
<column name="nexttoken" type="bigint"/>
</createTable>

<insert tableName="srmspacemanagernextid">
<column name="nexttoken" value="0"/>
</insert>

<createTable tableName="srmlinkgroup">
<column name="id" type="bigint"><constraints primaryKey="true" nullable="false"/></column>
<column name="name" type="varchar(32672)"/>
<column name="freespaceinbytes" type="bigint"/>
<column name="lastupdatetime" type="bigint"/>
<column name="onlineallowed" type="int"/>
<column name="nearlineallowed" type="int"/>
<column name="replicaallowed" type="int"/>
<column name="outputallowed" type="int"/>
<column name="custodialallowed" type="int"/>
<column name="reservedspaceinbytes" type="bigint"/>
</createTable>

<createTable tableName="srmlinkgroupvos">
<column name="vogroup" type="varchar(32672)"><constraints primaryKey="true" nullable="false"/></column>
<column name="vorole" type="varchar(32672)"><constraints primaryKey="true" nullable="false"/></column>
<column name="linkgroupid" type="bigint">
<constraints primaryKey="true" nullable="false"/>
</column>
</createTable>

<addForeignKeyConstraint baseColumnNames="linkgroupid"
baseTableName="srmlinkgroupvos"
constraintName="fk_srmlinkgroupvos_l"
onDelete="RESTRICT"
referencedColumnNames="id"
referencedTableName="srmlinkgroup"/>

<createTable tableName="srmretentionpolicy">
<column name="id" type="int"><constraints primaryKey="true" nullable="false"/></column>
<column name="name" type="varchar(32672)"/>
</createTable>

<createTable tableName="srmaccesslatency">
<column name="id" type="int"><constraints primaryKey="true" nullable="false"/></column>
<column name="name" type="varchar(32672)"/>
</createTable>

<createTable tableName="srmspace">
<column name="id" type="bigint"><constraints primaryKey="true" nullable="false"/></column>
<column name="vogroup" type="varchar(32672)"/>
<column name="vorole" type="varchar(32672)"/>
<column name="retentionpolicy" type="int"/>
<column name="accesslatency" type="int"/>
<column name="linkgroupid" type="bigint"/>
<column name="sizeinbytes" type="bigint"/>
<column name="creationtime" type="bigint"/>
<column name="lifetime" type="bigint"/>
<column name="description" type="varchar(32672)"/>
<column name="state" type="int"/>
<column name="usedspaceinbytes" type="bigint"/>
<column name="allocatedspaceinbytes" type="bigint"/>
</createTable>

<addForeignKeyConstraint baseColumnNames="linkgroupid"
baseTableName="srmspace"
constraintName="fk_srmspace_l"
referencedColumnNames="id"
referencedTableName="srmlinkgroup"/>

<addForeignKeyConstraint baseColumnNames="accesslatency"
baseTableName="srmspace"
constraintName="fk_srmspace_a"
referencedColumnNames="id"
referencedTableName="srmaccesslatency"/>

<addForeignKeyConstraint baseColumnNames="retentionpolicy"
baseTableName="srmspace"
constraintName="fk_srmspace_r"
referencedColumnNames="id"
referencedTableName="srmretentionpolicy"/>

<createIndex tableName="srmspace" indexName="srmspace_linkgroupid_idx">
<column name="linkgroupid"/>
</createIndex>

<createIndex tableName="srmspace" indexName="srmspace_state_idx">
<column name="state"/>
</createIndex>

<createIndex tableName="srmspace" indexName="srmspace_description_idx">
<column name="description"/>
</createIndex>

<createIndex tableName="srmspace" indexName="srmspace_lifetime_idx">
<column name="lifetime"/>
</createIndex>

<createIndex tableName="srmspace" indexName="srmspace_creationtime_idx">
<column name="creationtime"/>
</createIndex>

<createTable tableName="srmspacefile">
<column name="id" type="bigint"><constraints primaryKey="true" nullable="false"/></column>
<column name="vogroup" type="varchar(32672)"/>
<column name="vorole" type="varchar(32672)"/>
<column name="spacereservationid" type="bigint"/>
<column name="sizeinbytes" type="bigint"/>
<column name="creationtime" type="bigint"/>
<column name="lifetime" type="bigint"/>
<column name="pnfspath" type="varchar(32672)"/>
<column name="pnfsId" type="varchar(32672)"/>
<column name="state" type="int"/>
<column name="deleted" type="int"/>
</createTable>

<addForeignKeyConstraint baseColumnNames="spaceReservationId"
baseTableName="srmspacefile"
constraintName="fk_srmspacefile_l"
referencedColumnNames="id"
referencedTableName="srmspace"/>

<createIndex tableName="srmspacefile" indexName="srmspacefile_spaceresrevationid_idx">
<column name="spacereservationid"/>
</createIndex>

<createIndex tableName="srmspacefile" indexName="srmspacefile_state_idx">
<column name="state"/>
</createIndex>

<createIndex tableName="srmspacefile" indexName="srmspacefile_pnfspath_idx">
<column name="pnfspath"/>
</createIndex>

<createIndex tableName="srmspacefile" indexName="srmspacefile_pnfsid_idx">
<column name="pnfsid"/>
</createIndex>

<createIndex tableName="srmspacefile" indexName="srmspacefile_creationtime_idx">
<column name="creationtime"/>
</createIndex>

<createIndex tableName="srmspacefile" indexName="srmspacefile_lifetime_idx">
<column name="lifetime"/>
</createIndex>

<rollback/>
</changeSet>

<changeSet id="3" author="behrmann">
<preConditions onFail="MARK_RAN" onFailMessage="Not creating srmspacefile_pnfspath_state_idx as it already exists (this is not an error)">
<not><indexExists indexName="srmspacefile_pnfspath_state_idx"/></not>
</preConditions>
<comment>Create compound index on pnfspath and state field</comment>
<createIndex tableName="srmspacefile" indexName="srmspacefile_pnfspath_state_idx">
<column name="pnfspath"/>
<column name="state"/>
</createIndex>
<rollback/>
</changeSet>

<changeSet id="4" author="behrmann">
<preConditions onFail="MARK_RAN" onFailMessage="Not dropping srmspacemanagerschemaversion table as it does not exist (this is not an error)">
<tableExists tableName="srmspacemanagerschemaversion"/>
</preConditions>
<comment>Drop old schema management table</comment>
<dropTable tableName="srmspacemanagerschemaversion"/>
<rollback>
<createTable tableName="srmspacemanagerschemaversion">
<column name="version" type="int"/>
</createTable>
<insert tableName="srmspacemanagerschemaversion">
<column name="version" value="4"/>
</insert>
</rollback>
</changeSet>

</databaseChangeLog>
@@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.0.xsd">

<include file="diskCacheV111/services/space/db/spacemanager.changelog-2.8.xml"/>
</databaseChangeLog>

0 comments on commit 877a497

Please sign in to comment.