@sasjs/core 2.49.1 → 2.50.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/base/mp_lockanytable.sas +255 -0
- package/base/mp_lockfilecheck.sas +97 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -189,7 +189,7 @@ If you find this library useful, please leave a [star](https://github.com/sasjs/
|
|
|
189
189
|
|
|
190
190
|
## Contributors ✨
|
|
191
191
|
<!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
|
|
192
|
-
[](#contributors-)
|
|
193
193
|
<!-- ALL-CONTRIBUTORS-BADGE:END -->
|
|
194
194
|
Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/docs/en/emoji-key)):
|
|
195
195
|
|
|
@@ -209,6 +209,7 @@ Thanks goes to these wonderful people ([emoji key](https://allcontributors.org/d
|
|
|
209
209
|
<tr>
|
|
210
210
|
<td align="center"><a href="https://github.com/kkchandok"><img src="https://avatars.githubusercontent.com/u/46090627?v=4?s=100" width="100px;" alt=""/><br /><sub><b>kkchandok</b></sub></a><br /><a href="#ideas-kkchandok" title="Ideas, Planning, & Feedback">🤔</a></td>
|
|
211
211
|
<td align="center"><a href="https://github.com/VladislavParhomchik"><img src="https://avatars.githubusercontent.com/u/83717836?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Vladislav Parhomchik</b></sub></a><br /><a href="https://github.com/sasjs/core/commits?author=VladislavParhomchik" title="Tests">⚠️</a> <a href="https://github.com/sasjs/core/pulls?q=is%3Apr+reviewed-by%3AVladislavParhomchik" title="Reviewed Pull Requests">👀</a></td>
|
|
212
|
+
<td align="center"><a href="https://github.com/vznesh"><img src="https://avatars.githubusercontent.com/u/28916792?v=4?s=100" width="100px;" alt=""/><br /><sub><b>Vignesh T.</b></sub></a><br /><a href="https://github.com/sasjs/core/issues?q=author%3Avznesh" title="Bug reports">🐛</a></td>
|
|
212
213
|
</tr>
|
|
213
214
|
</table>
|
|
214
215
|
|
|
@@ -0,0 +1,255 @@
|
|
|
1
|
+
/**
|
|
2
|
+
@file
|
|
3
|
+
@brief Mechanism for locking tables to prevent parallel modifications
|
|
4
|
+
@details Uses a control table to enable ANY table to be locked for updates.
|
|
5
|
+
Only useful if every update uses the macro! Used heavily within
|
|
6
|
+
[Data Controller for SAS](https://datacontroller.io).
|
|
7
|
+
|
|
8
|
+
The underlying table is structured as per the MAKETABLE action.
|
|
9
|
+
|
|
10
|
+
@param [in] action The action to be performed. Valid values:
|
|
11
|
+
@li LOCK - Sets the lock flag, also confirms if a SAS lock is available
|
|
12
|
+
@li UNLOCK - Unlocks the table
|
|
13
|
+
@li MAKETABLE - creates the control table (ctl_ds)
|
|
14
|
+
@param [in] lib= (WORK) The libref of the table to lock. Should already be
|
|
15
|
+
assigned.
|
|
16
|
+
@param [in] ds= The dataset to lock
|
|
17
|
+
@param [in] ref= A meaningful reference to enable the lock to be traced. Max
|
|
18
|
+
length is 200 characters.
|
|
19
|
+
@param [out] ctl_ds= (0) The control table which controls the actual locking.
|
|
20
|
+
Should already be assigned and available.
|
|
21
|
+
@param [in] loops= (25) Number of times to check for a lock.
|
|
22
|
+
@param [in] loop_secs= (1) Seconds to wait between each lock attempt
|
|
23
|
+
|
|
24
|
+
<h4> SAS Macros </h4>
|
|
25
|
+
@li mp_abort.sas
|
|
26
|
+
@li mp_lockfilecheck.sas
|
|
27
|
+
@li mf_getuser.sas
|
|
28
|
+
|
|
29
|
+
<h4> Related Macros </h4>
|
|
30
|
+
@li mp_lockanytable.test.sas
|
|
31
|
+
|
|
32
|
+
@version 9.2
|
|
33
|
+
|
|
34
|
+
**/
|
|
35
|
+
|
|
36
|
+
%macro mp_lockanytable(
|
|
37
|
+
action
|
|
38
|
+
,lib= WORK
|
|
39
|
+
,ds=0
|
|
40
|
+
,ref=
|
|
41
|
+
,ctl_ds=0
|
|
42
|
+
,loops=25
|
|
43
|
+
,loop_secs=1
|
|
44
|
+
);
|
|
45
|
+
data _null_;
|
|
46
|
+
if _n_=1 then putlog "&sysmacroname entry vars:";
|
|
47
|
+
set sashelp.vmacro;
|
|
48
|
+
where scope="&sysmacroname";
|
|
49
|
+
put name '=' value;
|
|
50
|
+
run;
|
|
51
|
+
|
|
52
|
+
%mp_abort(iftrue= (&ds=0 and &action ne MAKETABLE)
|
|
53
|
+
,mac=&sysmacroname
|
|
54
|
+
,msg=%str(dataset was not provided)
|
|
55
|
+
)
|
|
56
|
+
%mp_abort(iftrue= (&ctl_ds=0)
|
|
57
|
+
,mac=&sysmacroname
|
|
58
|
+
,msg=%str(Control dataset was not provided)
|
|
59
|
+
)
|
|
60
|
+
|
|
61
|
+
/* set up lib & mac vars */
|
|
62
|
+
%let lib=%upcase(&lib);
|
|
63
|
+
%let ds=%upcase(&ds);
|
|
64
|
+
%let action=%upcase(&action);
|
|
65
|
+
%local user x trans msg abortme;
|
|
66
|
+
%let user=%mf_getuser();
|
|
67
|
+
%let abortme=0;
|
|
68
|
+
|
|
69
|
+
%mp_abort(iftrue= (&action ne LOCK & &action ne UNLOCK & &action ne MAKETABLE)
|
|
70
|
+
,mac=&sysmacroname
|
|
71
|
+
,msg=%str(Invalid action (&action) provided)
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
/* if an err condition exists, exit before we even begin */
|
|
75
|
+
%mp_abort(iftrue= (&syscc>0 and &action=LOCK)
|
|
76
|
+
,mac=&sysmacroname
|
|
77
|
+
,msg=%str(aborting due to syscc=&syscc on LOCK entry)
|
|
78
|
+
)
|
|
79
|
+
|
|
80
|
+
/* do not bother locking work tables (else may affect all WORK libraries) */
|
|
81
|
+
%if (%upcase(&lib)=WORK or %str(&lib)=%str()) & &action ne MAKETABLE %then %do;
|
|
82
|
+
%put NOTE: WORK libraries will not be registered in the locking system.;
|
|
83
|
+
%return;
|
|
84
|
+
%end;
|
|
85
|
+
|
|
86
|
+
/* do not proceed if no observations can be processed */
|
|
87
|
+
%mp_abort(iftrue= (%sysfunc(getoption(OBS))=0)
|
|
88
|
+
,mac=&sysmacroname
|
|
89
|
+
,msg=%str(options obs = 0. syserrortext=&syserrortext)
|
|
90
|
+
)
|
|
91
|
+
|
|
92
|
+
%if &ACTION=LOCK %then %do;
|
|
93
|
+
|
|
94
|
+
/* abort if a SAS lock is already in place, or cannot be applied */
|
|
95
|
+
%mp_lockfilecheck(&lib..&ds)
|
|
96
|
+
|
|
97
|
+
/* next, check there is a record for this table */
|
|
98
|
+
%local record_exists_check;
|
|
99
|
+
proc sql noprint;
|
|
100
|
+
select count(*) into: record_exists_check from &ctl_ds
|
|
101
|
+
where LOCK_LIB ="&lib" and LOCK_DS="&ds";
|
|
102
|
+
quit;
|
|
103
|
+
%if &syscc>0 %then %put syscc=&syscc sqlrc=&sqlrc;
|
|
104
|
+
%if &record_exists_check=0 %then %do;
|
|
105
|
+
data _null_;
|
|
106
|
+
putlog "&sysmacroname: adding record to lock table..";
|
|
107
|
+
run;
|
|
108
|
+
|
|
109
|
+
data ;
|
|
110
|
+
if 0 then set &ctl_ds;
|
|
111
|
+
LOCK_LIB ="&lib";
|
|
112
|
+
LOCK_DS="&ds";
|
|
113
|
+
LOCK_STATUS_CD='LOCKED';
|
|
114
|
+
LOCK_START_DTTM="%sysfunc(datetime(),E8601DT26.6)"dt;
|
|
115
|
+
LOCK_USER_NM="&user";
|
|
116
|
+
LOCK_PID="&sysjobid";
|
|
117
|
+
LOCK_REF="&ref";
|
|
118
|
+
output;stop;
|
|
119
|
+
run;
|
|
120
|
+
%let trans=&syslast;
|
|
121
|
+
proc append base=&ctl_ds data=&trans;
|
|
122
|
+
run;
|
|
123
|
+
%end;
|
|
124
|
+
/* if record does exist, perform lock attempts */
|
|
125
|
+
%else %do x=1 %to &loops;
|
|
126
|
+
data _null_;
|
|
127
|
+
putlog "&sysmacroname: attempting lock (iteration &x) "@;
|
|
128
|
+
putlog "at %sysfunc(datetime(),datetime19.) ..";
|
|
129
|
+
run;
|
|
130
|
+
|
|
131
|
+
proc sql;
|
|
132
|
+
update &ctl_ds
|
|
133
|
+
set LOCK_STATUS_CD='LOCKED'
|
|
134
|
+
, LOCK_START_DTTM="%sysfunc(datetime(),E8601DT26.6)"dt
|
|
135
|
+
, LOCK_USER_NM="&user"
|
|
136
|
+
, LOCK_PID="&sysjobid"
|
|
137
|
+
, LOCK_REF="&ref"
|
|
138
|
+
where LOCK_LIB ="&lib" and LOCK_DS="&ds";
|
|
139
|
+
quit;
|
|
140
|
+
/**
|
|
141
|
+
* NOTE - occasionally SQL server will return an err code (deadlocked
|
|
142
|
+
* transaction). If so, ignore it, keep calm, and carry on..
|
|
143
|
+
*/
|
|
144
|
+
%if &syscc>0 %then %do;
|
|
145
|
+
data _null_;
|
|
146
|
+
putlog 'NOTE-' / 'NOTE-';
|
|
147
|
+
putlog "NOTE- &sysmacroname: Update failed. "@;
|
|
148
|
+
putlog "Resetting err conditions and re-attempting.";
|
|
149
|
+
putlog "NOTE- syscc=&syscc syserr=&syserr sqlrc=&sqlrc";
|
|
150
|
+
putlog 'NOTE-' / 'NOTE-';
|
|
151
|
+
run;
|
|
152
|
+
%let syscc=0;
|
|
153
|
+
%let sqlrc=0;
|
|
154
|
+
%end;
|
|
155
|
+
|
|
156
|
+
/* now check if the record was successfully updated */
|
|
157
|
+
%local success_check;
|
|
158
|
+
proc sql noprint;
|
|
159
|
+
select count(*) into: success_check from &ctl_ds
|
|
160
|
+
where LOCK_LIB ="&lib" and LOCK_DS="&ds"
|
|
161
|
+
and LOCK_PID="&sysjobid" and LOCK_STATUS_CD='LOCKED';
|
|
162
|
+
quit;
|
|
163
|
+
%if &success_check=0 %then %do;
|
|
164
|
+
%if &x < &loops %then %do;
|
|
165
|
+
/* pause before next check */
|
|
166
|
+
data _null_;
|
|
167
|
+
putlog 'NOTE-' / 'NOTE-';
|
|
168
|
+
putlog "NOTE- &sysmacroname: table locked, waiting "@;
|
|
169
|
+
putlog "%sysfunc(sleep(&loop_inc)) seconds.. ";
|
|
170
|
+
putlog "NOTE- (iteration &x of &loops)";
|
|
171
|
+
putlog 'NOTE-' / 'NOTE-';
|
|
172
|
+
run;
|
|
173
|
+
%end;
|
|
174
|
+
%else %do;
|
|
175
|
+
%let msg=Unable to lock &lib..&ds via &ctl_ds after &loops attempts.\n
|
|
176
|
+
Please ask your administrator to investigate!;
|
|
177
|
+
%let abortme=1;
|
|
178
|
+
%end;
|
|
179
|
+
%end;
|
|
180
|
+
%else %do;
|
|
181
|
+
data _null_;
|
|
182
|
+
putlog 'NOTE-' / 'NOTE-';
|
|
183
|
+
putlog "NOTE- &sysmacroname: Table &lib..&ds locked at "@
|
|
184
|
+
putlog " %sysfunc(datetime(),datetime19.) (iteration &x)"@;
|
|
185
|
+
putlog 'NOTE-' / 'NOTE-';
|
|
186
|
+
run;
|
|
187
|
+
%if &syscc>0 %then %do;
|
|
188
|
+
%put setting syscc(&syscc) back to 0;
|
|
189
|
+
%let syscc=0;
|
|
190
|
+
%end;
|
|
191
|
+
%let x=&loops; /* no more iterations needed */
|
|
192
|
+
%end;
|
|
193
|
+
%end;
|
|
194
|
+
%end;
|
|
195
|
+
%else %if &ACTION=UNLOCK %then %do;
|
|
196
|
+
%local status;
|
|
197
|
+
proc sql noprint;
|
|
198
|
+
select LOCK_STATUS_CD into: status from &ctl_ds
|
|
199
|
+
where LOCK_LIB ="&lib" and LOCK_DS="&ds";
|
|
200
|
+
quit;
|
|
201
|
+
%if &syscc>0 %then %put syscc=&syscc sqlrc=&sqlrc;
|
|
202
|
+
%if &status=LOCKED %then %do;
|
|
203
|
+
data _null_;
|
|
204
|
+
putlog "&sysmacroname: unlocking &lib..&ds:";
|
|
205
|
+
run;
|
|
206
|
+
proc sql;
|
|
207
|
+
update &ctl_ds
|
|
208
|
+
set LOCK_STATUS_CD='UNLOCKED'
|
|
209
|
+
, LOCK_END_DTTM="%sysfunc(datetime(),E8601DT26.6)"dt
|
|
210
|
+
, LOCK_USER_NM="&user"
|
|
211
|
+
, LOCK_PID="&sysjobid"
|
|
212
|
+
, LOCK_REF="&ref"
|
|
213
|
+
where LOCK_LIB ="&lib" and LOCK_DS="&ds";
|
|
214
|
+
quit;
|
|
215
|
+
%end;
|
|
216
|
+
%else %if &status=UNLOCKED %then %do;
|
|
217
|
+
%put %str(WAR)NING: &lib..&ds is already unlocked!;
|
|
218
|
+
%end;
|
|
219
|
+
%else %do;
|
|
220
|
+
%put NOTE: Unrecognised STATUS_CD (&status) in &ctl_ds;
|
|
221
|
+
%let abortme=1;
|
|
222
|
+
%end;
|
|
223
|
+
%end;
|
|
224
|
+
%else %if &action=MAKETABLE %then %do;
|
|
225
|
+
proc sql;
|
|
226
|
+
create table &ctl_ds(
|
|
227
|
+
lock_lib char(8),
|
|
228
|
+
lock_ds char(32),
|
|
229
|
+
lock_status_cd char(10) not null,
|
|
230
|
+
lock_user_nm char(100) not null ,
|
|
231
|
+
lock_ref char(200),
|
|
232
|
+
lock_pid char(10),
|
|
233
|
+
lock_start_dttm num format=E8601DT26.6,
|
|
234
|
+
lock_end_dttm num format=E8601DT26.6,
|
|
235
|
+
constraint pk_mp_lockanytable primary key(lock_lib,lock_ds));
|
|
236
|
+
%end;
|
|
237
|
+
%else %do;
|
|
238
|
+
%let msg=lock_anytable given unsupported action (&action);
|
|
239
|
+
%let abortme=1;
|
|
240
|
+
%end;
|
|
241
|
+
|
|
242
|
+
/* catch errors - mp_abort must be called outside of a logic block */
|
|
243
|
+
%mp_abort(iftrue=(&abortme=1),
|
|
244
|
+
msg=%superq(msg),
|
|
245
|
+
mac=&sysmacroname
|
|
246
|
+
)
|
|
247
|
+
|
|
248
|
+
%exit_macro:
|
|
249
|
+
data _null_;
|
|
250
|
+
put "&sysmacroname: Exit vars: action=&action lib=&lib ds=&ds";
|
|
251
|
+
put " syscc=&syscc sqlrc=&sqlrc syserr=&syserr";
|
|
252
|
+
run;
|
|
253
|
+
%mend mp_lockanytable;
|
|
254
|
+
|
|
255
|
+
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
/**
|
|
2
|
+
@file
|
|
3
|
+
@brief Aborts if a SAS lock file is in place, or if one cannot be applied.
|
|
4
|
+
@details Used in conjuction with the mp_lockanytable macro.
|
|
5
|
+
More info here: https://sasensei.com/flash/24
|
|
6
|
+
|
|
7
|
+
Usage:
|
|
8
|
+
|
|
9
|
+
data work.test; a=1;run;
|
|
10
|
+
%mp_lockfilecheck(work.test)
|
|
11
|
+
|
|
12
|
+
@param [in] libds The libref.dataset for which to check the lock status
|
|
13
|
+
|
|
14
|
+
<h4> SAS Macros </h4>
|
|
15
|
+
@li mp_abort.sas
|
|
16
|
+
@li mf_getattrc.sas
|
|
17
|
+
|
|
18
|
+
<h4> Related Macros </h4>
|
|
19
|
+
@li mp_lockanytable.sas
|
|
20
|
+
@li mp_lockfilecheck.test.sas
|
|
21
|
+
|
|
22
|
+
@version 9.2
|
|
23
|
+
**/
|
|
24
|
+
|
|
25
|
+
%macro mp_lockfilecheck(
|
|
26
|
+
libds
|
|
27
|
+
)/*/STORE SOURCE*/;
|
|
28
|
+
|
|
29
|
+
data _null_;
|
|
30
|
+
if _n_=1 then putlog "&sysmacroname entry vars:";
|
|
31
|
+
set sashelp.vmacro;
|
|
32
|
+
where scope="&sysmacroname";
|
|
33
|
+
put name '=' value;
|
|
34
|
+
run;
|
|
35
|
+
|
|
36
|
+
%mp_abort(iftrue= (&syscc>0)
|
|
37
|
+
,mac=checklock.sas
|
|
38
|
+
,msg=Aborting with syscc=&syscc on entry.
|
|
39
|
+
)
|
|
40
|
+
%mp_abort(iftrue= (&libds=0)
|
|
41
|
+
,mac=&sysmacroname
|
|
42
|
+
,msg=%str(libds not provided)
|
|
43
|
+
)
|
|
44
|
+
|
|
45
|
+
%local msg lib ds;
|
|
46
|
+
%let lib=%upcase(%scan(&libds,1,.));
|
|
47
|
+
%let ds=%upcase(%scan(&libds,2,.));
|
|
48
|
+
|
|
49
|
+
/* do not proceed if no observations can be processed */
|
|
50
|
+
%let msg=options obs = 0. syserrortext=%superq(syserrortext);
|
|
51
|
+
%mp_abort(iftrue= (%sysfunc(getoption(OBS))=0)
|
|
52
|
+
,mac=checklock.sas
|
|
53
|
+
,msg=%superq(msg)
|
|
54
|
+
)
|
|
55
|
+
|
|
56
|
+
data _null_;
|
|
57
|
+
putlog "Checking engine & member type";
|
|
58
|
+
run;
|
|
59
|
+
%local engine memtype;
|
|
60
|
+
%let memtype=%mf_getattrc(&libds,MTYPE);
|
|
61
|
+
%let engine=%mf_getattrc(&libds,ENGINE);
|
|
62
|
+
|
|
63
|
+
%if &engine ne V9 and &engine ne BASE %then %do;
|
|
64
|
+
data _null_;
|
|
65
|
+
putlog "Lib &lib is not assigned using BASE engine - uses &engine instead";
|
|
66
|
+
putlog "SAS lock check will not be performed";
|
|
67
|
+
run;
|
|
68
|
+
%return;
|
|
69
|
+
%end;
|
|
70
|
+
%else %if &memtype ne DATA %then %do;
|
|
71
|
+
%put NOTE: Cannot lock a VIEW!! Memtype=&memtype;
|
|
72
|
+
%return;
|
|
73
|
+
%end;
|
|
74
|
+
|
|
75
|
+
data _null_;
|
|
76
|
+
putlog "Engine = &engine, memtype=&memtype";
|
|
77
|
+
putlog "Attempting lock statement";
|
|
78
|
+
run;
|
|
79
|
+
|
|
80
|
+
lock &libds;
|
|
81
|
+
|
|
82
|
+
%local abortme;
|
|
83
|
+
%let abortme=0;
|
|
84
|
+
%if &syscc>0 or &SYSLCKRC ne 0 %then %do;
|
|
85
|
+
%let msg=Unable to apply lock on &libds (SYSLCKRC=&SYSLCKRC syscc=&syscc);
|
|
86
|
+
%put %str(ERR)OR: &sysmacroname: &msg;
|
|
87
|
+
%let abortme=1;
|
|
88
|
+
%end;
|
|
89
|
+
|
|
90
|
+
lock &libds clear;
|
|
91
|
+
|
|
92
|
+
%mp_abort(iftrue= (&abortme=1)
|
|
93
|
+
,mac=&sysmacroname
|
|
94
|
+
,msg=%superq(msg)
|
|
95
|
+
)
|
|
96
|
+
|
|
97
|
+
%mend mp_lockfilecheck;
|
package/package.json
CHANGED