-
Notifications
You must be signed in to change notification settings - Fork 95
/
riak_cs.hrl
526 lines (464 loc) · 20 KB
/
riak_cs.hrl
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
%% ---------------------------------------------------------------------
%%
%% Copyright (c) 2007-2013 Basho Technologies, Inc. All Rights Reserved.
%%
%% This file is provided to you under the Apache License,
%% Version 2.0 (the "License"); you may not use this file
%% except in compliance with the License. You may obtain
%% a copy of the License at
%%
%% http://www.apache.org/licenses/LICENSE-2.0
%%
%% Unless required by applicable law or agreed to in writing,
%% software distributed under the License is distributed on an
%% "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
%% KIND, either express or implied. See the License for the
%% specific language governing permissions and limitations
%% under the License.
%%
%% ---------------------------------------------------------------------
-define(MANIFEST, #lfs_manifest_v3).
-define(ACL, #acl_v2).
-define(RCS_BUCKET, #moss_bucket_v1).
-define(MOSS_USER, #rcs_user_v2).
-define(RCS_USER, #rcs_user_v2).
-define(MULTIPART_MANIFEST, #multipart_manifest_v1).
-define(MULTIPART_MANIFEST_RECNAME, multipart_manifest_v1).
-define(PART_MANIFEST, #part_manifest_v1).
-define(PART_MANIFEST_RECNAME, part_manifest_v1).
-define(MULTIPART_DESCR, #multipart_descr_v1).
-define(COMPRESS_TERMS, false).
-define(PART_DESCR, #part_descr_v1).
-record(moss_user, {
name :: string(),
key_id :: string(),
key_secret :: string(),
buckets = []}).
-record(moss_user_v1, {
name :: string(),
display_name :: string(),
email :: string(),
key_id :: string(),
key_secret :: string(),
canonical_id :: string(),
buckets=[] :: [cs_bucket()]}).
-record(rcs_user_v2, {
name :: string(),
display_name :: string(),
email :: string(),
key_id :: string(),
key_secret :: string(),
canonical_id :: string(),
buckets=[] :: [cs_bucket()],
status=enabled :: enabled | disabled}).
-type moss_user() :: #rcs_user_v2{} | #moss_user_v1{}.
-type rcs_user() :: #rcs_user_v2{} | #moss_user_v1{}.
-record(moss_bucket, {
name :: string(),
creation_date :: term(),
acl :: acl()}).
-record(moss_bucket_v1, {
name :: string() | binary(),
last_action :: created | deleted,
creation_date :: string(),
modification_time :: erlang:timestamp(),
acl :: acl()}).
-type cs_bucket() :: #moss_bucket_v1{}.
-type bucket_operation() :: create | delete | update_acl | update_policy
| delete_policy.
-type bucket_action() :: created | deleted.
-record(context, {start_time :: erlang:timestamp(),
auth_bypass :: atom(),
user :: undefined | moss_user(),
user_object :: riakc_obj:riakc_obj(),
bucket :: binary(),
acl :: 'undefined' | acl(),
requested_perm :: acl_perm(),
riak_client :: riak_client(),
rc_pool :: atom(), % pool name which riak_client belongs to
auto_rc_close = true :: boolean(),
submodule :: atom(),
exports_fun :: function(),
auth_module :: atom(),
response_module :: atom(),
policy_module :: atom(),
%% Key for API rate and latency stats.
%% If `stats_prefix' or `stats_key' is `no_stats', no stats
%% will be gathered by riak_cs_wm_common.
%% The prefix is defined by `stats_prefix()' callback of sub-module.
%% If sub-module provides only `stats_prefix' (almost the case),
%% stats key is [Prefix, HttpMethod]. Otherwise, sum-module
%% can set specific `stats_key' by any callback that returns
%% this context.
stats_prefix = no_stats :: atom(),
stats_key=prefix_and_method :: prefix_and_method |
no_stats |
riak_cs_stats:key(),
local_context :: term(),
api :: atom()
}).
-record(key_context, {manifest :: 'notfound' | lfs_manifest(),
upload_id :: 'undefined' | binary(),
part_number :: 'undefined' | integer(),
part_uuid :: 'undefined' | binary(),
get_fsm_pid :: pid(),
putctype :: string(),
bucket :: binary(),
bucket_object :: undefined | notfound | riakc_obj:riakc_obj(),
key :: list(),
owner :: 'undefined' | string(),
size :: non_neg_integer(),
content_md5 :: 'undefined' | binary(),
update_metadata=false :: boolean()}).
-type acl_perm() :: 'READ' | 'WRITE' | 'READ_ACP' | 'WRITE_ACP' | 'FULL_CONTROL'.
-type acl_perms() :: [acl_perm()].
-type group_grant() :: 'AllUsers' | 'AuthUsers'.
-type acl_grantee() :: {DisplayName :: string(),
CanonicalID :: string()} |
group_grant().
-type acl_grant() :: {acl_grantee(), acl_perms()}.
%% acl_v1 owner fields: {DisplayName, CanonicalId}
-type acl_owner2() :: {string(), string()}.
%% acl_owner3: {display name, canonical id, key id}
-type acl_owner3() :: {string(), string(), string()}.
-type acl_owner() :: acl_owner2() | acl_owner3().
-record(acl_v1, {owner={"", ""} :: acl_owner(),
grants=[] :: [acl_grant()],
creation_time=now() :: erlang:timestamp()}).
%% acl_v2 owner fields: {DisplayName, CanonicalId, KeyId}
-record(acl_v2, {owner={"", "", ""} :: acl_owner(),
grants=[] :: [acl_grant()],
creation_time=now() :: erlang:timestamp()}).
-type acl() :: #acl_v1{} | #acl_v2{}.
-type cluster_id() :: undefined | binary(). %% flattened string as binary
-type cs_uuid() :: binary().
-type bag_id() :: undefined | binary().
-type riak_client() :: pid().
-record(lfs_manifest_v2, {
version=2 :: integer(),
block_size :: integer(),
bkey :: {binary(), binary()},
metadata :: orddict:orddict(),
created=riak_cs_wm_utils:iso_8601_datetime(),
uuid :: cs_uuid(),
content_length :: non_neg_integer(),
content_type :: binary(),
content_md5 :: term(),
state=undefined :: undefined | writing | active |
pending_delete | scheduled_delete | deleted,
write_start_time :: term(), %% immutable
last_block_written_time :: term(),
write_blocks_remaining :: ordsets:ordset(integer()),
delete_marked_time :: term(),
last_block_deleted_time :: term(),
delete_blocks_remaining :: ordsets:ordset(integer()),
acl :: acl(),
props = [] :: proplists:proplist(),
cluster_id :: cluster_id()
}).
-record(lfs_manifest_v3, {
%% "global" properties
%% -----------------------------------------------------------------
%% this isn't as important anymore
%% since we're naming the record
%% to include the version number,
%% but I figured it's worth keeping
%% in case we change serialization
%% formats in the future.
version=3 :: integer(),
%% the block_size setting when this manifest
%% was written. Needed if the user
%% ever changes the block size after writing
%% data
block_size :: integer(),
%% identifying properties
%% -----------------------------------------------------------------
bkey :: {binary(), binary()},
%% user metadata that would normally
%% be placed on the riak_object. We avoid
%% putting it on the riak_object so that
%% we can use that metadata ourselves
metadata :: orddict:orddict(),
%% the date the manifest was created.
%% not sure if we need both this and
%% write_start_time. My thought was that
%% write_start_time would have millisecond
%% resolution, but I suppose there's no
%% reason we can't change created
%% to have millisecond as well.
created=riak_cs_wm_utils:iso_8601_datetime(),
uuid :: cs_uuid(),
%% content properties
%% -----------------------------------------------------------------
content_length :: non_neg_integer(),
content_type :: binary(),
content_md5 :: term(),
%% state properties
%% -----------------------------------------------------------------
state=undefined :: undefined | writing | active |
pending_delete | scheduled_delete | deleted,
%% writing/active state
%% -----------------------------------------------------------------
write_start_time :: term(), %% immutable
%% used for two purposes
%% 1. to mark when a file has finished uploading
%% 2. to decide if a write crashed before completing
%% and needs to be garbage collected
last_block_written_time :: term(),
%% a shrink-only (during resolution)
%% set to denote which blocks still
%% need to be written. We use a shrinking
%% (rather than growing) set to that the
%% set is empty after the write has completed,
%% which should be most of the lifespan on disk
write_blocks_remaining :: ordsets:ordset(integer()),
%% pending_delete/deleted state
%% -----------------------------------------------------------------
%% set to the current time
%% when a manifest is marked as deleted
%% and enters the pending_delete state
delete_marked_time :: term(),
%% the timestamp serves a similar
%% purpose to last_block_written_time,
%% in that it's used for figuring out
%% when delete processes have died
%% and garbage collection needs to
%% pick up where they left off.
last_block_deleted_time :: term(),
%% a shrink-only (during resolution)
%% set to denote which blocks
%% still need to be deleted.
%% See write_blocks_remaining for
%% an explanation of why we chose
%% a shrinking set
delete_blocks_remaining :: ordsets:ordset(integer()),
%% the time the manifest was put
%% into the scheduled_delete
%% state
scheduled_delete_time :: term(),
%% The ACL for the version of the object represented
%% by this manifest.
acl :: acl() | no_acl_yet,
%% There are a couple of cases where we want to add record
%% member'ish data without adding new members to the record,
%% e.g.
%% 1. Data for which the common value is 'undefined' or not
%% used/set for this particular manifest
%% 2. Cases where we do want to change the structure of the
%% record but don't want to go through the full code
%% refactoring and backward-compatibility tap dance
%% until sometime later.
%% 'undefined' is for backward compatibility with v3 manifests
%% written with Riak CS 1.2.2 or earlier.
props = [] :: 'undefined' | proplists:proplist(),
%% cluster_id: A couple of uses, both short- and longer-term
%% possibilities:
%%
%% 1. We don't have a good story in early 2012 for how to
%% build a stable 2,000 node Riak cluster. If MOSS can
%% talk to multiple Riak clusters, then each individual
%% cluster can be a size that we're comfortable
%% supporting.
%%
%% 2. We may soon have Riak EE's replication have full
%% plumbing to make it feasible to forward arbitrary
%% traffic between clusters. Then if a slave cluster is
%% missing a data block, and read-repair cannot
%% automagically fix the 'not_found' problem, then perhaps
%% forwarding a get request to the source Riak cluster can
%% fetch us the missing data.
cluster_id :: cluster_id()
}).
-type lfs_manifest() :: #lfs_manifest_v3{}.
-type cs_uuid_and_manifest() :: {cs_uuid(), lfs_manifest()}.
-record(part_manifest_v1, {
bucket :: binary(),
key :: binary(),
%% used to judge races between concurrent uploads
%% of the same part_number
start_time :: erlang:timestamp(),
%% one-of 1-10000, inclusive
part_number :: integer(),
%% a UUID to prevent conflicts with concurrent
%% uploads of the same {upload_id, part_number}.
part_id :: binary(),
%% each individual part upload always has a content-length
%% content_md5 is used for the part ETag, alas.
content_length :: integer(),
content_md5 :: 'undefined' | binary(),
%% block size just like in `lfs_manifest_v2'. Concievably,
%% parts for the same upload id could have different block_sizes.
block_size :: integer()
}).
-type part_manifest() :: #part_manifest_v1{}.
-record(multipart_manifest_v1, {
upload_id :: binary(),
owner :: acl_owner3(),
%% since we don't have any point of strong
%% consistency (other than stanchion), we
%% can get concurrent `complete' and `abort'
%% requests. There are still some details to
%% work out, but what we observe here will
%% affect whether we accept future `complete'
%% or `abort' requests.
%% Stores references to all of the parts uploaded
%% with this `upload_id' so far. A part
%% can be uploaded more than once with the same
%% part number. type = #part_manifest_vX
parts = ordsets:new() :: ordsets:ordset(?PART_MANIFEST{}),
%% List of UUIDs for parts that are done uploading.
%% The part number is redundant, so we only store
%% {UUID::binary(), PartETag::binary()} here.
done_parts = ordsets:new() :: ordsets:ordset({binary(), binary()}),
%% type = #part_manifest_vX
cleanup_parts = ordsets:new() :: ordsets:ordset(?PART_MANIFEST{}),
%% a place to stuff future information
%% without having to change
%% the record format
props = [] :: proplists:proplist()
}).
-type multipart_manifest() :: #multipart_manifest_v1{}.
%% Basis of list multipart uploads output
-record(multipart_descr_v1, {
%% Object key for the multipart upload
key :: binary(),
%% UUID of the multipart upload
upload_id :: binary(),
%% User that initiated the upload
owner_display :: string(),
owner_key_id :: string(),
%% storage class: no real options here
storage_class = standard,
%% Time that the upload was initiated
initiated :: string() %% conflict of func vs. type: riak_cs_wm_utils:iso_8601_datetime()
}).
%% Basis of multipart list parts output
-record(part_descr_v1, {
part_number :: integer(),
last_modified :: string(), % TODO ??
etag :: binary(),
size :: integer()
}).
-define(USER_BUCKET, <<"moss.users">>).
-define(ACCESS_BUCKET, <<"moss.access">>).
-define(STORAGE_BUCKET, <<"moss.storage">>).
-define(BUCKETS_BUCKET, <<"moss.buckets">>).
-define(GC_BUCKET, <<"riak-cs-gc">>).
-define(FREE_BUCKET_MARKER, <<"0">>).
-define(DEFAULT_MAX_BUCKETS_PER_USER, 100).
-define(DEFAULT_MAX_CONTENT_LENGTH, 5368709120). %% 5 GB
-define(DEFAULT_LFS_BLOCK_SIZE, 1048576).%% 1 MB
-define(XML_PROLOG, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>").
-define(S3_XMLNS, 'http://s3.amazonaws.com/doc/2006-03-01/').
-define(DEFAULT_STANCHION_IP, "127.0.0.1").
-define(DEFAULT_STANCHION_PORT, 8085).
-define(DEFAULT_STANCHION_SSL, true).
-define(MD_BAG, <<"X-Rcs-Bag">>).
-define(MD_ACL, <<"X-Moss-Acl">>).
-define(MD_POLICY, <<"X-Rcs-Policy">>).
-define(EMAIL_INDEX, <<"email_bin">>).
-define(ID_INDEX, <<"c_id_bin">>).
-define(KEY_INDEX, <<"$key">>).
-define(AUTH_USERS_GROUP, "http://acs.amazonaws.com/groups/global/AuthenticatedUsers").
-define(ALL_USERS_GROUP, "http://acs.amazonaws.com/groups/global/AllUsers").
-define(LOG_DELIVERY_GROUP, "http://acs.amazonaws.com/groups/s3/LogDelivery").
-define(DEFAULT_FETCH_CONCURRENCY, 1).
-define(DEFAULT_PUT_CONCURRENCY, 1).
-define(DEFAULT_DELETE_CONCURRENCY, 1).
%% A number to multiplied with the block size
%% to determine the PUT buffer size.
%% ex. 2 would mean BlockSize * 2
-define(DEFAULT_PUT_BUFFER_FACTOR, 1).
%% Similar to above, but for fetching
%% This is also max ram per fetch request
-define(DEFAULT_FETCH_BUFFER_FACTOR, 32).
-define(N_VAL_1_GET_REQUESTS, true).
-define(DEFAULT_PING_TIMEOUT, 5000).
-define(JSON_TYPE, "application/json").
-define(XML_TYPE, "application/xml").
-define(S3_API_MOD, riak_cs_s3_rewrite).
-define(S3_LEGACY_API_MOD, riak_cs_s3_rewrite_legacy).
-define(OOS_API_MOD, riak_cs_oos_rewrite).
-define(S3_RESPONSE_MOD, riak_cs_s3_response).
-define(OOS_RESPONSE_MOD, riak_cs_oos_response).
%% Major categories of Erlang-triggered DTrace probes
%%
%% The main R15B01 USDT probe that can be triggered by Erlang code is defined
%% like this:
%%
%% /**
%% * Multi-purpose probe: up to 4 NUL-terminated strings and 4
%% * 64-bit integer arguments.
%% *
%% * @param proc, the PID (string form) of the sending process
%% * @param user_tag, the user tag of the sender
%% * @param i1, integer
%% * @param i2, integer
%% * @param i3, integer
%% * @param i4, integer
%% * @param s1, string/iolist. D's arg6 is NULL if not given by Erlang
%% * @param s2, string/iolist. D's arg7 is NULL if not given by Erlang
%% * @param s3, string/iolist. D's arg8 is NULL if not given by Erlang
%% * @param s4, string/iolist. D's arg9 is NULL if not given by Erlang
%% */
%% probe user_trace__i4s4(char *proc, char *user_tag,
%% int i1, int i2, int i3, int i4,
%% char *s1, char *s2, char *s3, char *s4);
%%
%% The convention that we'll use of these probes is:
%% param D arg name use
%% ----- ---------- ---
%% i1 arg2 Application category (see below)
%% i2 arg3 1 = function entry, 2 = function return
%% NOTE! Not all function entry probes have a return probe
%% i3-i4 arg4-arg5 Varies, zero usually means unused (but not always!)
%% s1 arg6 Module name
%% s2 arg7 Function name
%% s3-4 arg8-arg9 Varies, NULL means unused
%%
-define(DT_BLOCK_OP, 700).
-define(DT_SERVICE_OP, 701).
-define(DT_BUCKET_OP, 702).
-define(DT_OBJECT_OP, 703).
%% perhaps add later? -define(DT_AUTH_OP, 704).
-define(DT_WM_OP, 705).
-define(USER_BUCKETS_PRUNE_TIME, 86400). %% one-day in seconds
-define(DEFAULT_CLUSTER_ID_TIMEOUT,5000).
-define(DEFAULT_AUTH_MODULE, riak_cs_s3_auth).
-define(DEFAULT_LIST_OBJECTS_MAX_KEYS, 1000).
-define(DEFAULT_MD5_CHUNK_SIZE, 1048576). %% 1 MB
-define(DEFAULT_MANIFEST_WARN_SIBLINGS, 20).
-define(DEFAULT_MANIFEST_WARN_BYTES, 5*1024*1024). %% 5MB
-define(DEFAULT_MANIFEST_WARN_HISTORY, 30).
-define(DEFAULT_MAX_PART_NUMBER, 10000).
%% timeout hitting Riak PB API
-define(DEFAULT_RIAK_TIMEOUT, 60000).
%% General system info
-define(WORD_SIZE, erlang:system_info(wordsize)).
-define(DEFAULT_POLICY_MODULE, riak_cs_s3_policy).
-record(access_v1, {
method :: 'PUT' | 'GET' | 'POST' | 'DELETE' | 'HEAD',
target :: atom(), % object | object_acl | ....
id :: string(),
bucket :: binary(),
key = <<>> :: undefined | binary(),
req %:: #wm_reqdata{} % request of webmachine
}).
-type access() :: #access_v1{}.
-type policy() :: riak_cs_s3_policy:policy1().
%% just to persuade dyalizer
-type crypto_context() :: {'md4' | 'md5' | 'ripemd160' | 'sha' |
'sha224' | 'sha256' | 'sha384' | 'sha512',
binary()}.
-type digest() :: binary().
-define(USERMETA_BUCKET, "RCS-bucket").
-define(USERMETA_KEY, "RCS-key").
-define(USERMETA_BCSUM, "RCS-bcsum").
-define(OBJECT_BUCKET_PREFIX, <<"0o:">>). % Version # = 0
-define(BLOCK_BUCKET_PREFIX, <<"0b:">>). % Version # = 0
-define(MAX_S3_KEY_LENGTH, 1024).
-ifdef(namespaced_types).
-type mochiweb_headers() :: gb_trees:tree().
-else.
-type mochiweb_headers() :: gb_tree().
-endif.