-
Notifications
You must be signed in to change notification settings - Fork 0
/
structure.sql
254 lines (244 loc) · 8.62 KB
/
structure.sql
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
-- ----------------------------
-- Create independent schema
-- ----------------------------
CREATE SCHEMA IF NOT EXISTS "distlock";
-- ----------------------------
-- Table structure for mutex
-- ----------------------------
DROP TABLE IF EXISTS "distlock"."mutex";
CREATE TABLE "distlock"."mutex" (
"mtx_name" varchar(64) COLLATE "default" NOT NULL,
"mtx_value" varchar(64) COLLATE "default" NOT NULL,
"mtx_expiry" int8 NOT NULL
) WITH (OIDS=FALSE);
CREATE UNIQUE INDEX "unique_mutex_name" ON "distlock"."mutex" USING btree ("mtx_name");
-- ----------------------------
-- Table structure for rwmutex
-- ----------------------------
DROP TABLE IF EXISTS "distlock"."rwmutex";
CREATE TABLE "distlock"."rwmutex" (
"rwmtx_name" varchar(64) COLLATE "default" NOT NULL,
"rwmtx_hmap" jsonb NOT NULL,
"rwmtx_expiry" int8 NOT NULL,
"rwmtx_count" int8 NOT NULL
) WITH (OIDS=FALSE);
CREATE UNIQUE INDEX "unique_rwmutex_name" ON "distlock"."rwmutex" USING btree ("rwmtx_name");
-- ----------------------------
-- Function for mutex lock
-- ----------------------------
CREATE OR REPLACE FUNCTION "distlock"."lock"(VARCHAR,VARCHAR,BIGINT)
RETURNS INT AS $$
DECLARE
expiry BIGINT;
tstamp BIGINT;
BEGIN
SELECT CEIL(EXTRACT(EPOCH FROM NOW()) * 1000) INTO tstamp;
SELECT "mtx_expiry" INTO expiry FROM "distlock"."mutex" WHERE "mtx_name" = $1 FOR UPDATE NOWAIT;
IF expiry IS NOT NULL THEN
IF expiry > tstamp THEN
RETURN expiry - tstamp;
END IF;
UPDATE "distlock"."mutex" SET "mtx_value" = $2, "mtx_expiry" = $3 + tstamp WHERE "mtx_name" = $1;
ELSE
INSERT INTO "distlock"."mutex" ("mtx_name", "mtx_value", "mtx_expiry") VALUES ($1, $2, $3 + tstamp);
END IF;
RETURN -3;
EXCEPTION
WHEN unique_violation THEN
RETURN $3;
WHEN lock_not_available THEN
SELECT "mtx_expiry" INTO expiry FROM "distlock"."mutex" WHERE "mtx_name" = $1;
IF expiry > tstamp THEN
RETURN expiry - tstamp;
END IF;
RETURN 0;
END;
$$ LANGUAGE PLPGSQL VOLATILE;
-- ----------------------------
-- Function for mutex unlock
-- ----------------------------
CREATE OR REPLACE FUNCTION "distlock"."unlock"(VARCHAR,VARCHAR,VARCHAR)
RETURNS BOOLEAN AS $$
DECLARE
mtxvalue VARCHAR;
BEGIN
SELECT "mtx_value" INTO mtxvalue FROM "distlock"."mutex" WHERE "mtx_name" = $1 FOR UPDATE;
IF mtxvalue = $2 THEN
UPDATE "distlock"."mutex" SET "mtx_expiry" = 0 WHERE "mtx_name" = $1;
PERFORM PG_NOTIFY($3, '1');
RETURN TRUE;
END IF;
RETURN FALSE;
END;
$$ LANGUAGE PLPGSQL VOLATILE;
-- ----------------------------
-- Function for mutex touch
-- ----------------------------
CREATE OR REPLACE FUNCTION "distlock"."touch"(VARCHAR,VARCHAR,BIGINT)
RETURNS BOOLEAN AS $$
DECLARE
tstamp BIGINT;
record RECORD;
counter SMALLINT;
BEGIN
SELECT CEIL(EXTRACT(EPOCH FROM NOW()) * 1000) INTO tstamp;
SELECT * INTO record FROM "distlock"."mutex" WHERE "mtx_name" = $1 FOR UPDATE;
IF record IS NOT NULL AND record."mtx_value" = $2 AND record."mtx_expiry" > tstamp THEN
UPDATE "distlock"."mutex" SET "mtx_expiry" = tstamp + $3 WHERE "mtx_name" = $1;
RETURN TRUE;
END IF;
RETURN FALSE;
END;
$$ LANGUAGE PLPGSQL VOLATILE;
-- ----------------------------
-- Function for rwmutex rlock
-- ----------------------------
CREATE OR REPLACE FUNCTION "distlock"."rlock"(VARCHAR,VARCHAR,BIGINT)
RETURNS INT AS $$
DECLARE
expiry BIGINT;
tstamp BIGINT;
record RECORD;
counter BIGINT;
BEGIN
SELECT CEIL(EXTRACT(EPOCH FROM NOW()) * 1000) INTO tstamp;
SELECT * INTO record FROM "distlock"."rwmutex" WHERE "rwmtx_name" = $1 FOR UPDATE NOWAIT;
IF record IS NOT NULL THEN
IF record."rwmtx_expiry" <= tstamp THEN
UPDATE "distlock"."rwmutex" SET "rwmtx_expiry" = $3 + tstamp, "rwmtx_count" = 1,
"rwmtx_hmap" = JSONB_BUILD_OBJECT($2, 1) WHERE "rwmtx_name" = $1;
RETURN -3;
END IF;
IF record."rwmtx_count" < 0 THEN
RETURN record."rwmtx_expiry" - tstamp;
END IF;
SELECT "value" INTO counter FROM JSONB_EACH(record."rwmtx_hmap") WHERE "key" = $2;
IF counter IS NULL THEN
counter = 0;
END IF;
UPDATE "distlock"."rwmutex" SET "rwmtx_expiry" = $3 + tstamp, "rwmtx_count" = "rwmtx_count" + 1,
"rwmtx_hmap" = "rwmtx_hmap" || JSONB_BUILD_OBJECT($2, counter + 1) WHERE "rwmtx_name" = $1;
ELSE
INSERT INTO "distlock"."rwmutex" ("rwmtx_name", "rwmtx_hmap", "rwmtx_expiry", "rwmtx_count") VALUES ($1, JSONB_BUILD_OBJECT($2, 1), $3 + tstamp, 1);
END IF;
RETURN -3;
EXCEPTION
WHEN unique_violation THEN
RETURN 0;
WHEN lock_not_available THEN
RETURN 0;
END;
$$ LANGUAGE PLPGSQL VOLATILE;
-- ----------------------------
-- Function for rwmutex runlock
-- ----------------------------
CREATE OR REPLACE FUNCTION "distlock"."runlock"(VARCHAR,VARCHAR,VARCHAR,BIGINT)
RETURNS BOOLEAN AS $$
DECLARE
record RECORD;
counter INT;
BEGIN
SELECT * INTO record FROM "distlock"."rwmutex" WHERE "rwmtx_name" = $1 FOR UPDATE;
IF record IS NOT NULL AND (record."rwmtx_count" >= 0 OR record."rwmtx_count" + $4 >= 0) THEN
SELECT "value" INTO counter FROM JSONB_EACH(record."rwmtx_hmap") WHERE "key" = $2;
IF counter IS NOT NULL AND counter > 0 THEN
IF counter = 1 THEN
UPDATE "distlock"."rwmutex" SET "rwmtx_hmap" = "rwmtx_hmap" - $2, "rwmtx_count" = "rwmtx_count" - 1 WHERE "rwmtx_name" = $1;
IF record."rwmtx_count" = 1 OR (record."rwmtx_count" + $4) = 1 THEN
PERFORM PG_NOTIFY($3, '1');
END IF;
ELSE
UPDATE "distlock"."rwmutex" SET "rwmtx_hmap" = "rwmtx_hmap" || JSONB_BUILD_OBJECT($2, counter - 1), "rwmtx_count" = "rwmtx_count" - 1 WHERE "rwmtx_name" = $1;
END IF;
RETURN TRUE;
END IF;
END IF;
RETURN FALSE;
END;
$$ LANGUAGE PLPGSQL VOLATILE;
-- ----------------------------
-- Function for rwmutex wlock
-- ----------------------------
CREATE OR REPLACE FUNCTION "distlock"."wlock"(VARCHAR,VARCHAR,BIGINT,BIGINT)
RETURNS INT AS $$
DECLARE
expiry BIGINT;
tstamp BIGINT;
record RECORD;
counter SMALLINT;
BEGIN
SELECT CEIL(EXTRACT(EPOCH FROM NOW()) * 1000) INTO tstamp;
SELECT * INTO record FROM "distlock"."rwmutex" WHERE "rwmtx_name" = $1 FOR UPDATE NOWAIT;
IF record IS NULL THEN
INSERT INTO "distlock"."rwmutex" ("rwmtx_name", "rwmtx_hmap", "rwmtx_expiry", "rwmtx_count") VALUES ($1, JSONB_BUILD_OBJECT($2, 0), $3 + tstamp, 0 - $4);
RETURN -3;
ELSEIF record."rwmtx_expiry" <= tstamp THEN
UPDATE "distlock"."rwmutex" SET "rwmtx_hmap" = JSONB_BUILD_OBJECT($2, 0), "rwmtx_expiry" = $3 + tstamp, "rwmtx_count" = 0 - $4 WHERE "rwmtx_name" = $1;
RETURN -3;
ELSE
IF record."rwmtx_count" >= 0 THEN
UPDATE "distlock"."rwmutex" SET "rwmtx_count" = "rwmtx_count" - $4 WHERE "rwmtx_name" = $1;
END IF;
IF record."rwmtx_count" = 0 OR record."rwmtx_count" + $4 = 0 THEN
SELECT COUNT(*) INTO counter FROM JSONB_EACH(record."rwmtx_hmap");
IF counter = 0 THEN
UPDATE "distlock"."rwmutex" SET "rwmtx_expiry" = $3 + tstamp, "rwmtx_hmap" = JSONB_BUILD_OBJECT($2, 0) WHERE "rwmtx_name" = $1;
RETURN -3;
END IF;
END IF;
RETURN record."rwmtx_expiry" - tstamp;
END IF;
EXCEPTION
WHEN unique_violation THEN
RETURN $3;
WHEN lock_not_available THEN
SELECT "rwmtx_expiry" INTO expiry FROM "distlock"."rwmutex" WHERE "rwmtx_name" = $1;
IF expiry > tstamp THEN
RETURN expiry - tstamp;
END IF;
RETURN 0;
END;
$$ LANGUAGE PLPGSQL VOLATILE;
-- ----------------------------
-- Function for rwmutex wunlock
-- ----------------------------
CREATE OR REPLACE FUNCTION "distlock"."wunlock"(VARCHAR,VARCHAR,VARCHAR,BIGINT)
RETURNS BOOLEAN AS $$
DECLARE
record RECORD;
counter SMALLINT;
BEGIN
SELECT * INTO record FROM "distlock"."rwmutex" WHERE "rwmtx_name" = $1 FOR UPDATE;
IF record IS NOT NULL AND record."rwmtx_count" < 0 THEN
SELECT COUNT(*) INTO counter FROM JSONB_EACH(record."rwmtx_hmap") WHERE "key" = $2;
IF counter = 1 THEN
UPDATE "distlock"."rwmutex" SET "rwmtx_hmap" = "rwmtx_hmap" - $2, "rwmtx_count" = "rwmtx_count" + $4 WHERE "rwmtx_name" = $1;
PERFORM PG_NOTIFY($3, '1');
RETURN TRUE;
END IF;
END IF;
RETURN FALSE;
END;
$$ LANGUAGE PLPGSQL VOLATILE;
-- ----------------------------
-- Function for rwmutex rwtouch
-- ----------------------------
CREATE OR REPLACE FUNCTION "distlock"."rwtouch"(VARCHAR,VARCHAR,BIGINT)
RETURNS BOOLEAN AS $$
DECLARE
tstamp BIGINT;
record RECORD;
counter SMALLINT;
BEGIN
SELECT CEIL(EXTRACT(EPOCH FROM NOW()) * 1000) INTO tstamp;
SELECT * INTO record FROM "distlock"."rwmutex" WHERE "rwmtx_name" = $1 FOR UPDATE;
IF record IS NOT NULL AND record."rwmtx_expiry" > tstamp THEN
SELECT COUNT(*) INTO counter FROM JSONB_EACH(record."rwmtx_hmap") WHERE "key" = $2;
IF counter = 1 THEN
UPDATE "distlock"."rwmutex" SET "rwmtx_expiry" = tstamp + $3 WHERE "rwmtx_name" = $1;
RETURN TRUE;
END IF;
END IF;
RETURN FALSE;
END;
$$ LANGUAGE PLPGSQL VOLATILE;