/
spacemanager.changelog-procedures-hsqldb.xml
180 lines (168 loc) · 10.1 KB
/
spacemanager.changelog-procedures-hsqldb.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
<?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">
<!-- linkgroup.freespaceinbytes only ever decreases in trigger updates below; this is a heuristic
to quickly update freespaceinbytes as space is consumed. The real value is provided by
periodic updates from pool manager. The heuristic does not increase linkgroup.freespaceinbytes
as that would introduce a risk of advertising more free space than is actually available. -->
<changeSet id="1" author="behrmann" dbms="hsqldb" runOnChange="true">
<comment>Create srmlinkgroup trigger to prevent over allocating link groups</comment>
<sql>DROP TRIGGER tgr_srmlinkgroup_update IF EXISTS</sql>
<createProcedure>
CREATE TRIGGER tgr_srmlinkgroup_update BEFORE UPDATE ON srmlinkgroup
REFERENCING OLD ROW AS old NEW ROW AS new
FOR EACH ROW WHEN (new.reservedspaceinbytes > old.reservedspaceinbytes AND new.reservedspaceinbytes > new.freespaceinbytes)
BEGIN ATOMIC
SIGNAL SQLSTATE '23D01' SET MESSAGE_TEXT = 'Not enough free space in link group ';
END;
</createProcedure>
</changeSet>
<changeSet id="2" author="behrmann" dbms="hsqldb" runOnChange="true">
<comment>Create srmspace triggers for maintaining accumulated fields</comment>
<sql>DROP TRIGGER tgr_srmspace_insert IF EXISTS</sql>
<createProcedure>
CREATE TRIGGER tgr_srmspace_insert AFTER INSERT ON srmspace
REFERENCING NEW ROW AS space
FOR EACH ROW WHEN (space.state = 0)
UPDATE srmlinkgroup
SET reservedspaceinbytes = reservedspaceinbytes + space.sizeinbytes - space.usedspaceinbytes,
freespaceinbytes = GREATEST(freespaceinbytes - space.usedspaceinbytes, 0)
WHERE id = space.linkGroupId;
</createProcedure>
<sql>DROP TRIGGER tgr_srmspace_delete IF EXISTS</sql>
<createProcedure>
CREATE TRIGGER tgr_srmspace_delete AFTER DELETE ON srmspace
REFERENCING OLD ROW AS space
FOR EACH ROW WHEN (space.state = 0)
UPDATE srmlinkgroup SET reservedspaceinbytes = reservedspaceinbytes - space.sizeinbytes + space.usedspaceinbytes WHERE id = space.linkgroupid;
</createProcedure>
<sql>DROP TRIGGER tgr_srmspace_update IF EXISTS</sql>
<createProcedure>
CREATE TRIGGER tgr_srmspace_update AFTER UPDATE ON srmspace
REFERENCING OLD ROW AS old NEW ROW AS new
FOR EACH ROW
BEGIN ATOMIC
IF old.state = 0 AND new.state = 0 THEN
IF old.linkGroupId != new.linkGroupId THEN
UPDATE srmlinkgroup
SET reservedspaceinbytes = reservedspaceinbytes - old.sizeinbytes + old.usedspaceinbytes
WHERE id = old.linkgroupid;
UPDATE srmlinkgroup
SET reservedspaceinbytes = reservedspaceinbytes + new.sizeinbytes - new.usedspaceinbytes,
freespaceinbytes = GREATEST(freespaceinbytes - new.usedspaceinbytes, 0)
WHERE id = new.linkgroupid;
ELSEIF old.sizeinbytes <> new.sizeinbytes OR old.usedspaceinbytes <> new.usedspaceinbytes THEN
UPDATE srmlinkgroup
SET reservedspaceinbytes = reservedspaceinbytes - old.sizeinbytes + old.usedspaceinbytes + new.sizeinbytes - new.usedspaceinbytes,
freespaceinbytes = GREATEST(LEAST(freespaceinbytes, freespaceinbytes - new.usedspaceinbytes + old.usedspaceinbytes), 0)
WHERE id = old.linkgroupid;
END IF;
ELSEIF old.state = 0 AND new.state <> 0 THEN
UPDATE srmlinkgroup SET reservedspaceinbytes = reservedspaceinbytes - old.sizeinBytes + old.usedspaceinbytes WHERE id = old.linkgroupid;
ELSEIF old.state <> 0 AND new.state = 0 THEN
UPDATE srmlinkgroup
SET reservedspaceinbytes = reservedspaceinbytes + new.sizeinbytes - new.usedspaceinbytes,
freespaceinbytes = GREATEST(freespaceinbytes - new.usedspaceinbytes, 0)
WHERE id = new.linkgroupid;
END IF;
END;
</createProcedure>
<rollback>
<sql>DROP TRIGGER tgr_srmspace_insert IF EXISTS</sql>
<sql>DROP TRIGGER tgr_srmspace_update IF EXISTS</sql>
<sql>DROP TRIGGER tgr_srmspace_delete IF EXISTS</sql>
</rollback>
</changeSet>
<changeSet id="3" author="behrmann" dbms="hsqldb" runOnChange="true">
<!-- Comment taken from old Java code: Idea below is questionable. We resize space reservation
to fit this file. This way we attempt to guarantee that there is no negative numbers in
LinkGroup. -->
<comment>Create srmspace trigger to enlarge space to fit its files</comment>
<sql>DROP TRIGGER tgr_srmspace_increase_size IF EXISTS</sql>
<createProcedure>
CREATE TRIGGER tgr_srmspace_increase_size BEFORE UPDATE ON srmspace
REFERENCING OLD ROW AS old NEW ROW as new
FOR EACH ROW WHEN (new.allocatedspaceinbytes + new.usedspaceinbytes > new.sizeinbytes)
SET new.sizeinbytes = new.allocatedspaceinbytes + new.usedspaceinbytes;
</createProcedure>
<rollback>
<sql>DROP TRIGGER tgr_srmspace_increase_size IF EXISTS</sql>
</rollback>
</changeSet>
<changeSet id="4" author="behrmann" dbms="hsqldb" runOnChange="true">
<comment>Create srmspacefile triggers for maintaining accumulated fields</comment>
<sql>DROP TRIGGER tgr_srmspacefile_insert IF EXISTS</sql>
<createProcedure>
CREATE TRIGGER tgr_srmspacefile_insert AFTER INSERT ON srmspacefile
REFERENCING NEW ROW AS new
FOR EACH ROW
BEGIN ATOMIC
CASE new.state
WHEN 0, 1 THEN
UPDATE srmspace SET allocatedspaceinbytes = allocatedspaceinbytes + new.sizeinbytes WHERE id = new.spacereservationid;
WHEN 2 THEN
UPDATE srmspace SET usedspaceinbytes = usedspaceinbytes + new.sizeinbytes WHERE id = new.spacereservationid;
END CASE;
END;
</createProcedure>
<sql>DROP TRIGGER tgr_srmspacefile_update IF EXISTS</sql>
<createProcedure>
CREATE TRIGGER tgr_srmspacefile_decrement_allocated AFTER UPDATE ON srmspacefile
REFERENCING OLD ROW AS old NEW ROW AS new
FOR EACH ROW
BEGIN ATOMIC
DECLARE allocatedDelta BIGINT;
DECLARE usedDelta BIGINT;
IF old.spaceReservationId = new.spaceReservationId THEN
SET allocatedDelta =
CASE WHEN new.state IN (0, 1) THEN new.sizeinbytes ELSE 0 END
-
CASE WHEN old.state IN (0, 1) THEN old.sizeinbytes ELSE 0 END;
SET usedDelta =
CASE WHEN new.state = 2 THEN new.sizeinbytes ELSE 0 END
-
CASE WHEN old.state = 2 THEN old.sizeinbytes ELSE 0 END;
IF allocatedDelta <> 0 OR usedDelta <> 0 THEN
UPDATE srmspace
SET allocatedspaceinbytes = allocatedspaceinbytes + allocatedDelta,
usedspaceinbytes = usedspaceinbytes + usedDelta
WHERE id = old.spaceReservationId;
END IF;
ELSE
CASE old.state
WHEN 0, 1 THEN
UPDATE srmspace SET allocatedspaceinbytes = allocatedspaceinbytes - old.sizeinbytes WHERE id = old.spacereservationid;
WHEN 2 THEN
UPDATE srmspace SET usedspaceinbytes = usedspaceinbytes - old.sizeinbytes WHERE id = old.spacereservationid;
END CASE;
CASE new.state
WHEN 0, 1 THEN
UPDATE srmspace SET allocatedspaceinbytes = allocatedspaceinbytes + new.sizeinbytes WHERE id = new.spacereservationid;
WHEN 2 THEN
UPDATE srmspace SET usedspaceinbytes = usedspaceinbytes + new.sizeinbytes WHERE id = new.spacereservationid;
END CASE;
END IF;
END;
</createProcedure>
<sql>DROP TRIGGER tgr_srmspacefile_delete IF EXISTS</sql>
<createProcedure>
CREATE TRIGGER tgr_srmspacefile_delete AFTER DELETE ON srmspacefile
REFERENCING OLD ROW AS old
FOR EACH ROW
BEGIN ATOMIC
CASE old.state
WHEN 0, 1 THEN
UPDATE srmspace SET allocatedspaceinbytes = allocatedspaceinbytes - old.sizeinbytes WHERE id = old.spacereservationid;
WHEN 2 THEN
UPDATE srmspace SET usedspaceinbytes = usedspaceinbytes - old.sizeinbytes WHERE id = old.spacereservationid;
END CASE;
END;
</createProcedure>
<rollback>
<sql>DROP TRIGGER tgr_srmspacefile_insert IF EXISTS</sql>
<sql>DROP TRIGGER tgr_srmspacefile_update IF EXISTS</sql>
<sql>DROP TRIGGER tgr_srmspacefile_delete IF EXISTS</sql>
</rollback>
</changeSet>
</databaseChangeLog>