IOR
aiori-S3.c
Go to the documentation of this file.
1 /* -*- mode: c; indent-tabs-mode: t; -*-
2  * vim:noexpandtab:
3  *
4  * Editing with tabs allows different users to pick their own indentation
5  * appearance without changing the file.
6  */
7 
8 /*
9  * Copyright (c) 2009, Los Alamos National Security, LLC All rights reserved.
10  * Copyright 2009. Los Alamos National Security, LLC. This software was produced
11  * under U.S. Government contract DE-AC52-06NA25396 for Los Alamos National
12  * Laboratory (LANL), which is operated by Los Alamos National Security, LLC for
13  * the U.S. Department of Energy. The U.S. Government has rights to use,
14  * reproduce, and distribute this software. NEITHER THE GOVERNMENT NOR LOS
15  * ALAMOS NATIONAL SECURITY, LLC MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR
16  * ASSUMES ANY LIABILITY FOR THE USE OF THIS SOFTWARE. If software is
17  * modified to produce derivative works, such modified software should be
18  * clearly marked, so as not to confuse it with the version available from
19  * LANL.
20  *
21  * Additionally, redistribution and use in source and binary forms, with or
22  * without modification, are permitted provided that the following conditions are
23  * met:
24  *
25  * • Redistributions of source code must retain the above copyright notice,
26  * this list of conditions and the following disclaimer.
27  *
28  * • Redistributions in binary form must reproduce the above copyright notice,
29  * this list of conditions and the following disclaimer in the documentation
30  * and/or other materials provided with the distribution.
31  *
32  * • Neither the name of Los Alamos National Security, LLC, Los Alamos National
33  * Laboratory, LANL, the U.S. Government, nor the names of its contributors may be
34  * used to endorse or promote products derived from this software without specific
35  * prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED BY LOS ALAMOS NATIONAL SECURITY, LLC AND CONTRIBUTORS
38  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
39  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40  * ARE DISCLAIMED. IN NO EVENT SHALL LOS ALAMOS NATIONAL SECURITY, LLC OR
41  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
42  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
43  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
44  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
45  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
46  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
47  * OF SUCH DAMAGE.
48  */
49 
50 /******************************************************************************
51  *
52  * Implementation of abstract IOR interface, for the Amazon S3 API.
53  * EMC/ViPR supports some useful extensions to S3, which we also implement
54  * here. There are 3 different mixes:
55  *
56  * (1) "Pure S3" uses S3 "Multi-Part Upload" to do N:1 writes. N:N writes
57  * fail, in the case where IOR "transfer-size" differs from
58  * "block-size', because this implies an "append", and append is not
59  * supported in S3. [TBD: The spec also says multi-part upload can't
60  * have any individual part greater than 5MB, or more then 10k total
61  * parts. Failing these conditions may produce obscure errors. Should
62  * we enforce? ]
63  *
64  * --> Select this option with the '-a S3' command-line arg to IOR
65  *
66  *
67  * (2) "EMC S3 Extensions" uses the EMC byte-range support for N:1
68  * writes, eliminating Multi-Part Upload. EMC expects this will
69  * perform better than MPU, and it avoids some problems that are
70  * imposed by the S3 MPU spec. [See comments at EMC_Xfer().]
71  *
72  * --> Select this option with the '-a EMC_S3' command-line arg to IOR
73  *
74  *
75  * NOTE: Putting EMC's S3-extensions in the same file with the S3 API
76  * allows us to share some code that would otherwise be duplicated
77  * (e.g. s3_connect(), etc). This should also help us avoid losing
78  * bug fixes that are discovered in one interface or the other. In
79  * some cases, S3 is incapable of supporting all the needs of IOR.
80  * (For example, see notes about "append", above S3_Xfer().
81  *
82  ******************************************************************************/
83 
84 #ifdef HAVE_CONFIG_H
85 # include "config.h"
86 #endif
87 
88 #include <stdio.h>
89 #include <stdlib.h>
90 #include <string.h> /* strnstr() */
91 
92 #include <errno.h>
93 #include <assert.h>
94 /*
95 #ifdef HAVE_LUSTRE_LUSTRE_USER_H
96 #include <lustre/lustre_user.h>
97 #endif
98 */
99 
100 #include "ior.h"
101 #include "aiori.h"
102 #include "iordef.h"
103 
104 #include <curl/curl.h>
105 
106 #include <libxml/parser.h> // from libxml2
107 #include <libxml/tree.h>
108 
109 #include "aws4c.h" // extended vers of "aws4c" lib for S3 via libcurl
110 #include "aws4c_extra.h" // utilities, e.g. for parsing XML in responses
111 
112 
113 
114 
115 /* buffer is used to generate URLs, err_msgs, etc */
116 #define BUFF_SIZE 1024
117 static char buff[BUFF_SIZE];
118 
119 const int ETAG_SIZE = 32;
120 
121 CURLcode rc;
122 
123 /* Any objects we create or delete will be under this bucket */
124 const char* bucket_name = "ior";
125 
126 /* TODO: The following stuff goes into options! */
127 /* REST/S3 variables */
128 // CURL* curl; /* for libcurl "easy" fns (now managed by aws4c) */
129 # define IOR_CURL_INIT 0x01 /* curl top-level inits were perfomed once? */
130 # define IOR_CURL_NOCONTINUE 0x02
131 # define IOR_CURL_S3_EMC_EXT 0x04 /* allow EMC extensions to S3? */
132 
133 #ifdef USE_S3_AIORI
134 # include <curl/curl.h>
135 # include "aws4c.h"
136 #else
137  typedef void CURL; /* unused, but needs a type */
138  typedef void IOBuf; /* unused, but needs a type */
139 #endif
140 
141  IOBuf* io_buf; /* aws4c places parsed header values here */
142  IOBuf* etags; /* accumulate ETags for N:1 parts */
143 
145 
146 /**************************** P R O T O T Y P E S *****************************/
147 static void* S3_Create(char*, IOR_param_t*);
148 static void* S3_Open(char*, IOR_param_t*);
149 static IOR_offset_t S3_Xfer(int, void*, IOR_size_t*, IOR_offset_t, IOR_param_t*);
150 static void S3_Close(void*, IOR_param_t*);
151 
152 static void* EMC_Create(char*, IOR_param_t*);
153 static void* EMC_Open(char*, IOR_param_t*);
155 static void EMC_Close(void*, IOR_param_t*);
156 
157 static void S3_Delete(char*, IOR_param_t*);
158 static void S3_Fsync(void*, IOR_param_t*);
159 static IOR_offset_t S3_GetFileSize(IOR_param_t*, MPI_Comm, char*);
160 static void S3_init();
161 static void S3_finalize();
162 
163 /************************** D E C L A R A T I O N S ***************************/
164 
165 // "Pure S3"
166 // N:1 writes use multi-part upload
167 // N:N fails if "transfer-size" != "block-size" (because that requires "append")
169  .name = "S3",
170  .create = S3_Create,
171  .open = S3_Open,
172  .xfer = S3_Xfer,
173  .close = S3_Close,
174  .delete = S3_Delete,
175  .get_version = aiori_get_version,
176  .fsync = S3_Fsync,
177  .get_file_size = S3_GetFileSize,
178  .initialize = S3_init,
179  .finalize = S3_finalize
180 };
181 
182 // "S3", plus EMC-extensions enabled
183 // N:1 writes use multi-part upload
184 // N:N succeeds (because EMC-extensions support "append")
186  .name = "S3_plus",
187  .create = S3_Create,
188  .open = S3_Open,
189  .xfer = S3_Xfer,
190  .close = S3_Close,
191  .delete = S3_Delete,
192  .set_version = S3_SetVersion,
193  .fsync = S3_Fsync,
194  .get_file_size = S3_GetFileSize,
195  .initialize = S3_init,
196  .finalize = S3_finalize
197 };
198 
199 // Use EMC-extensions for N:1 write, as well
200 // N:1 writes use EMC byte-range
201 // N:N succeeds because EMC-extensions support "append"
203  .name = "S3_EMC",
204  .create = EMC_Create,
205  .open = EMC_Open,
206  .xfer = EMC_Xfer,
207  .close = EMC_Close,
208  .delete = S3_Delete,
209  .set_version = S3_SetVersion,
210  .fsync = S3_Fsync,
211  .get_file_size = S3_GetFileSize,
212  .initialize = S3_init,
213  .finalize = S3_finalize
214 };
215 
216 
217 static void S3_init(){
218  /* This is supposed to be done before *any* threads are created.
219  * Could MPI_Init() create threads (or call multi-threaded
220  * libraries)? We'll assume so. */
221  AWS4C_CHECK( aws_init() );
222 }
223 
224 static void S3_finalize(){
225  /* done once per program, after exiting all threads.
226  * NOTE: This fn doesn't return a value that can be checked for success. */
227  aws_cleanup();
228 }
229 
230 /* modelled on similar macros in iordef.h */
231 #define CURL_ERR(MSG, CURL_ERRNO, PARAM) \
232  do { \
233  fprintf(stdout, "ior ERROR: %s: %s (curl-errno=%d) (%s:%d)\n", \
234  MSG, curl_easy_strerror(CURL_ERRNO), CURL_ERRNO, \
235  __FILE__, __LINE__); \
236  fflush(stdout); \
237  MPI_Abort((PARAM)->testComm, -1); \
238  } while (0)
239 
240 
241 #define CURL_WARN(MSG, CURL_ERRNO) \
242  do { \
243  fprintf(stdout, "ior WARNING: %s: %s (curl-errno=%d) (%s:%d)\n", \
244  MSG, curl_easy_strerror(CURL_ERRNO), CURL_ERRNO, \
245  __FILE__, __LINE__); \
246  fflush(stdout); \
247  } while (0)
248 
249 
250 /***************************** F U N C T I O N S ******************************/
251 
252 
253 
254 
255 /* ---------------------------------------------------------------------------
256  * "Connect" to an S3 object-file-system. We're really just initializing
257  * libcurl. We need this done before any interactions. It is easy for
258  * ior_aiori.open/create to assure that we connect, if we haven't already
259  * done so. However, there's not a simple way to assure that we
260  * "disconnect" at the end. For now, we'll make a special call at the end
261  * of ior.c
262  *
263  * NOTE: It's okay to call this thing whenever you need to be sure the curl
264  * handle is initialized.
265  *
266  * NOTE: Our custom version of aws4c can be configured so that connections
267  * are reused, instead of opened and closed on every operation. We
268  * do configure it that way, but you still need to call these
269  * connect/disconnet functions, in order to insure that aws4c has
270  * been configured.
271  * ---------------------------------------------------------------------------
272  */
273 
274 
275 static void s3_connect( IOR_param_t* param ) {
276  if (param->verbose >= VERBOSE_2) {
277  printf("-> s3_connect\n"); /* DEBUGGING */
278  }
279 
280  if ( param->curl_flags & IOR_CURL_INIT ) {
281  if (param->verbose >= VERBOSE_2) {
282  printf("<- s3_connect [nothing to do]\n"); /* DEBUGGING */
283  }
284  return;
285  }
286 
287  // --- Done once-only (per rank). Perform all first-time inits.
288  //
289  // The aws library requires a config file, as illustrated below. We
290  // assume that the user running the test has an entry in this file,
291  // using their login moniker (i.e. `echo $USER`) as the key, as
292  // suggested in the example:
293  //
294  // <user>:<s3_login_id>:<s3_private_key>
295  //
296  // This file must not be readable by other than user.
297  //
298  // NOTE: These inits could be done in init_IORParam_t(), in ior.c, but
299  // would require conditional compilation, there.
300 
301  aws_set_debug(param->verbose >= 4);
302  aws_read_config(getenv("USER")); // requires ~/.awsAuth
303  aws_reuse_connections(1);
304 
305  // initalize IOBufs. These are basically dynamically-extensible
306  // linked-lists. "growth size" controls the increment of new memory
307  // allocated, whenever storage is used up.
308  param->io_buf = aws_iobuf_new();
309  aws_iobuf_growth_size(param->io_buf, 1024*1024*1);
310 
311  param->etags = aws_iobuf_new();
312  aws_iobuf_growth_size(param->etags, 1024*1024*8);
313 
314  // WARNING: if you have http_proxy set in your environment, you may need
315  // to override it here. TBD: add a command-line variable to
316  // allow you to define a proxy.
317  //
318  // our hosts are currently 10.140.0.15 - 10.140 0.18
319  // TBD: Try DNS-round-robin server at vi-lb.ccstar.lanl.gov
320  // TBD: try HAProxy round-robin at 10.143.0.1
321 
322 #if 1
323  // snprintf(buff, BUFF_SIZE, "10.140.0.%d:9020", 15 + (rank % 4));
324  // s3_set_proxy(buff);
325  //
326  // snprintf(buff, BUFF_SIZE, "10.140.0.%d", 15 + (rank % 4));
327  // s3_set_host(buff);
328 
329  snprintf(buff, BUFF_SIZE, "10.140.0.%d:9020", 15 + (rank % 4));
330  s3_set_host(buff);
331 
332 #else
333 /*
334  * If you just want to go to one if the ECS nodes, put that IP
335  * address in here directly with port 9020.
336  *
337  */
338 // s3_set_host("10.140.0.15:9020");
339 
340 /*
341  * If you want to go to haproxy.ccstar.lanl.gov, this is its IP
342  * address.
343  *
344  */
345 // s3_set_proxy("10.143.0.1:80");
346 // s3_set_host( "10.143.0.1:80");
347 #endif
348 
349  // make sure test-bucket exists
350  s3_set_bucket((char*)bucket_name);
351 
352  if (rank == 0) {
353  AWS4C_CHECK( s3_head(param->io_buf, "") );
354  if ( param->io_buf->code == 404 ) { // "404 Not Found"
355  printf(" bucket '%s' doesn't exist\n", bucket_name);
356 
357  AWS4C_CHECK( s3_put(param->io_buf, "") ); /* creates URL as bucket + obj */
358  AWS4C_CHECK_OK( param->io_buf ); // assure "200 OK"
359  printf("created bucket '%s'\n", bucket_name);
360  }
361  else { // assure "200 OK"
362  AWS4C_CHECK_OK( param->io_buf );
363  }
364  }
365  MPI_CHECK(MPI_Barrier(param->testComm), "barrier error");
366 
367 
368  // Maybe allow EMC extensions to S3
369  s3_enable_EMC_extensions(param->curl_flags & IOR_CURL_S3_EMC_EXT);
370 
371  // don't perform these inits more than once
372  param->curl_flags |= IOR_CURL_INIT;
373 
374 
375  if (param->verbose >= VERBOSE_2) {
376  printf("<- s3_connect [success]\n");
377  }
378 }
379 
380 static
381 void
383  if (param->verbose >= VERBOSE_2) {
384  printf("-> s3_disconnect\n");
385  }
386 
387  // nothing to do here, if using new aws4c ...
388 
389  if (param->verbose >= VERBOSE_2) {
390  printf("<- s3_disconnect\n");
391  }
392 }
393 
394 
395 
396 // After finalizing an S3 multi-part-upload, you must reset some things
397 // before you can use multi-part-upload again. This will also avoid (one
398 // particular set of) memory-leaks.
399 void
401  aws_iobuf_reset(param->io_buf);
402  aws_iobuf_reset(param->etags);
403  param->part_number = 0;
404 }
405 
406 
407 /* ---------------------------------------------------------------------------
408  * direct support for the IOR S3 interface
409  * ---------------------------------------------------------------------------
410  */
411 
412 /*
413  * One doesn't "open" an object, in REST semantics. All we really care
414  * about is whether caller expects the object to have zero-size, when we
415  * return. If so, we conceptually delete it, then recreate it empty.
416  *
417  * ISSUE: If the object is going to receive "appends" (supported in EMC S3
418  * extensions), the object has to exist before the first append
419  * operation. On the other hand, there appears to be a bug in the
420  * EMC implementation, such that if an object ever receives appends,
421  * and then is deleted, and then recreated, the recreated object will
422  * always return "500 Server Error" on GET (whether it has been
423  * appended or not).
424  *
425  * Therefore, a safer thing to do here is write zero-length contents,
426  * instead of deleting.
427  *
428  * NOTE: There's also no file-descriptor to return, in REST semantics. On
429  * the other hand, we keep needing the file *NAME*. Therefore, we
430  * will return the file-name, and let IOR pass it around to our
431  * functions, in place of what IOR understands to be a
432  * file-descriptor.
433  *
434  */
435 
436 static
437 void *
438 S3_Create_Or_Open_internal(char* testFileName,
440  unsigned char createFile,
441  int multi_part_upload_p ) {
442 
443  if (param->verbose >= VERBOSE_2) {
444  printf("-> S3_Create_Or_Open('%s', ,%d, %d)\n",
445  testFileName, createFile, multi_part_upload_p);
446  }
447 
448  /* initialize curl, if needed */
449  s3_connect( param );
450 
451  /* Check for unsupported flags */
452  if ( param->openFlags & IOR_EXCL ) {
453  fprintf( stdout, "Opening in Exclusive mode is not implemented in S3\n" );
454  }
455  if ( param->useO_DIRECT == TRUE ) {
456  fprintf( stdout, "Direct I/O mode is not implemented in S3\n" );
457  }
458 
459  // easier to think
460  int n_to_n = param->filePerProc;
461  int n_to_1 = ! n_to_n;
462 
463  /* check whether object needs reset to zero-length */
464  int needs_reset = 0;
465  if (! multi_part_upload_p)
466  needs_reset = 1; /* so "append" can work */
467  else if ( param->openFlags & IOR_TRUNC )
468  needs_reset = 1; /* so "append" can work */
469  else if (createFile) {
470  // AWS4C_CHECK( s3_head(param->io_buf, testFileName) );
471  // if ( ! AWS4C_OK(param->io_buf) )
472  needs_reset = 1;
473  }
474 
475  if ( param->open == WRITE ) {
476 
477  /* initializations for N:1 or N:N writes using multi-part upload */
478  if (multi_part_upload_p) {
479 
480  // For N:N, all ranks do their own MPU open/close. For N:1, only
481  // rank0 does that. Either way, the response from the server
482  // includes an "uploadId", which must be used to upload parts to
483  // the same object.
484  if ( n_to_n || (rank == 0) ) {
485 
486  // rank0 handles truncate
487  if ( needs_reset) {
488  aws_iobuf_reset(param->io_buf);
489  AWS4C_CHECK( s3_put(param->io_buf, testFileName) ); /* 0-length write */
490  AWS4C_CHECK_OK( param->io_buf );
491  }
492 
493  // POST request with URL+"?uploads" initiates multi-part upload
494  snprintf(buff, BUFF_SIZE, "%s?uploads", testFileName);
495  IOBuf* response = aws_iobuf_new();
496  AWS4C_CHECK( s3_post2(param->io_buf, buff, NULL, response) );
497  AWS4C_CHECK_OK( param->io_buf );
498 
499  // parse XML returned from server, into a tree structure
500  aws_iobuf_realloc(response);
501  xmlDocPtr doc = xmlReadMemory(response->first->buf,
502  response->first->len,
503  NULL, NULL, 0);
504  if (doc == NULL)
505  ERR_SIMPLE("Rank0 Failed to find POST response\n");
506 
507  // navigate parsed XML-tree to find UploadId
508  xmlNode* root_element = xmlDocGetRootElement(doc);
509  const char* upload_id = find_element_named(root_element, (char*)"UploadId");
510  if (! upload_id)
511  ERR_SIMPLE("couldn't find 'UploadId' in returned XML\n");
512 
513  if (param->verbose >= VERBOSE_3)
514  printf("got UploadId = '%s'\n", upload_id);
515 
516  const size_t upload_id_len = strlen(upload_id);
517  if (upload_id_len > MAX_UPLOAD_ID_SIZE) {
518  snprintf(buff, BUFF_SIZE,
519  "UploadId length %d exceeds expected max (%d)",
520  upload_id_len, MAX_UPLOAD_ID_SIZE);
521  ERR_SIMPLE(buff);
522  }
523 
524  // save the UploadId we found
525  memcpy(param->UploadId, upload_id, upload_id_len);
526  param->UploadId[upload_id_len] = 0;
527 
528  // free storage for parsed XML tree
529  xmlFreeDoc(doc);
530  aws_iobuf_free(response);
531 
532  // For N:1, share UploadId across all ranks
533  if (n_to_1)
534  MPI_Bcast(param->UploadId, MAX_UPLOAD_ID_SIZE, MPI_BYTE, 0, param->testComm);
535  }
536  else
537  // N:1, and we're not rank0. recv UploadID from Rank 0
538  MPI_Bcast(param->UploadId, MAX_UPLOAD_ID_SIZE, MPI_BYTE, 0, param->testComm);
539  }
540 
541  /* initializations for N:N or N:1 writes using EMC byte-range extensions */
542  else {
543 
544  /* maybe reset to zero-length, so "append" can work */
545  if (needs_reset) {
546 
547  if (verbose >= VERBOSE_3) {
548  fprintf( stdout, "rank %d resetting\n",
549  rank);
550  }
551 
552  aws_iobuf_reset(param->io_buf);
553  AWS4C_CHECK( s3_put(param->io_buf, testFileName) );
554  AWS4C_CHECK_OK( param->io_buf );
555  }
556  }
557  }
558 
559 
560  if (param->verbose >= VERBOSE_2) {
561  printf("<- S3_Create_Or_Open\n");
562  }
563  return ((void *) testFileName );
564 }
565 
566 
567 
568 static
569 void *
570 S3_Create( char *testFileName, IOR_param_t * param ) {
571  if (param->verbose >= VERBOSE_2) {
572  printf("-> S3_Create\n");
573  }
574 
575  if (param->verbose >= VERBOSE_2) {
576  printf("<- S3_Create\n");
577  }
578  return S3_Create_Or_Open_internal( testFileName, param, TRUE, TRUE );
579 }
580 static
581 void *
582 EMC_Create( char *testFileName, IOR_param_t * param ) {
583  if (param->verbose >= VERBOSE_2) {
584  printf("-> EMC_Create\n");
585  }
586 
587  if (param->verbose >= VERBOSE_2) {
588  printf("<- EMC_Create\n");
589  }
590  return S3_Create_Or_Open_internal( testFileName, param, TRUE, FALSE );
591 }
592 
593 
594 
595 
596 
597 
598 static
599 void *
600 S3_Open( char *testFileName, IOR_param_t * param ) {
601  if (param->verbose >= VERBOSE_2) {
602  printf("-> S3_Open\n");
603  }
604 
605  if ( param->openFlags & IOR_CREAT ) {
606  if (param->verbose >= VERBOSE_2) {
607  printf("<- S3_Open( ... TRUE)\n");
608  }
609  return S3_Create_Or_Open_internal( testFileName, param, TRUE, TRUE );
610  }
611  else {
612  if (param->verbose >= VERBOSE_2) {
613  printf("<- S3_Open( ... FALSE)\n");
614  }
615  return S3_Create_Or_Open_internal( testFileName, param, FALSE, TRUE );
616  }
617 }
618 static
619 void *
620 EMC_Open( char *testFileName, IOR_param_t * param ) {
621  if (param->verbose >= VERBOSE_2) {
622  printf("-> S3_Open\n");
623  }
624 
625  if ( param->openFlags & IOR_CREAT ) {
626  if (param->verbose >= VERBOSE_2) {
627  printf("<- EMC_Open( ... TRUE)\n");
628  }
629  return S3_Create_Or_Open_internal( testFileName, param, TRUE, FALSE );
630  }
631  else {
632  if (param->verbose >= VERBOSE_2) {
633  printf("<- EMC_Open( ... FALSE)\n");
634  }
635  return S3_Create_Or_Open_internal( testFileName, param, FALSE, FALSE );
636  }
637 }
638 
639 
640 /*
641  * transfer (more) data to an object. <file> is just the obj name.
642  *
643  * For N:1, param->offset is understood as offset for a given client to
644  * write into the "file". This translates to a byte-range in the HTTP
645  * request. Each write in the N:1 case is treated as a complete "part",
646  * so there is no such thing as a partial write.
647  *
648  * For N:N, when IOR "transfer-size" differs from "block-size", IOR treats
649  * Xfer as a partial write (i.e. there are multiple calls to XFER, to write
650  * any one of the "N" objects, as a series of "append" operations). This
651  * is not supported in S3/REST. Therefore, we depend on an EMC extension,
652  * in this case. This EMC extension allows appends using a byte-range
653  * header spec of "Range: bytes=-1-". aws4c now provides
654  * s3_enable_EMC_extensions(), to allow this behavior. If EMC-extensions
655  * are not enabled, the aws4c library will generate a run-time error, in
656  * this case.
657  *
658  * Each write-request returns an ETag which is a hash of the data. (The
659  * ETag could also be computed directly, if we wanted.) We must save the
660  * etags for later use by S3_close().
661  *
662  * WARNING: "Pure" S3 doesn't allow byte-ranges for writes to an object.
663  * Thus, you also can not append to an object. In the context of IOR,
664  * this causes objects to have only the size of the most-recent write.
665  * Thus, If the IOR "transfer-size" is different from the IOR
666  * "block-size", the files will be smaller than the amount of data
667  * that was written to them.
668  *
669  * EMC does support "append" to an object. In order to allow this,
670  * you must enable the EMC-extensions in the aws4c library, by calling
671  * s3_set_emc_compatibility() with a non-zero argument.
672  *
673  * NOTE: I don't think REST allows us to read/write an amount other than
674  * the size we request. Maybe our callback-handlers (above) could
675  * tell us? For now, this is assuming we only have to send one
676  * request, to transfer any amount of data. (But see above, re EMC
677  * support for "append".)
678  */
679 /* In the EMC case, instead of Multi-Part Upload we can use HTTP
680  * "byte-range" headers to write parts of a single object. This appears to
681  * have several advantages over the S3 MPU spec:
682  *
683  * (a) no need for a special "open" operation, to capture an "UploadID".
684  * Instead we simply write byte-ranges, and the server-side resolves
685  * any races, producing a single winner. In the IOR case, there should
686  * be no races, anyhow.
687  *
688  * (b) individual write operations don't have to refer to an ID, or to
689  * parse and save ETags returned from every write.
690  *
691  * (c) no need for a special "close" operation, in which all the saved
692  * ETags are gathered at a single rank, placed into XML, and shipped to
693  * the server, to finalize the MPU. That special close appears to
694  * impose two scaling problems: (1) requires all ETags to be shipped at
695  * the BW available to a single process, (1) requires either that they
696  * all fit into memory of a single process, or be written to disk
697  * (imposes additional BW contraints), or make a more-complex
698  * interaction with a threaded curl writefunction, to present the
699  * appearance of a single thread to curl, whilst allowing streaming
700  * reception of non-local ETags.
701  *
702  * (d) no constraints on the number or size of individual parts. (These
703  * exist in the S3 spec, the EMC's impl of the S3 multi-part upload is
704  * also free of these constraints.)
705  *
706  * Instead, parallel processes can write any number and/or size of updates,
707  * using a "byte-range" header. After each write returns, that part of the
708  * global object is visible to any reader. Places that are not updated
709  * read as zeros.
710  */
711 
712 
713 static
715 S3_Xfer_internal(int access,
716  void* file,
717  IOR_size_t* buffer,
718  IOR_offset_t length,
720  int multi_part_upload_p ) {
721 
722  if (param->verbose >= VERBOSE_2) {
723  printf("-> S3_Xfer(acc:%d, target:%s, buf:0x%llx, len:%llu, 0x%llx)\n",
724  access, (char*)file, buffer, length, param);
725  }
726 
727  char* fname = (char*)file; /* see NOTE above S3_Create_Or_Open() */
728  size_t remaining = (size_t)length;
729  char* data_ptr = (char *)buffer;
730  off_t offset = param->offset;
731 
732  // easier to think
733  int n_to_n = param->filePerProc;
734  int n_to_1 = (! n_to_n);
735  int segmented = (param->segmentCount == 1);
736 
737 
738  if (access == WRITE) { /* WRITE */
739 
740  if (verbose >= VERBOSE_3) {
741  fprintf( stdout, "rank %d writing length=%lld to offset %lld\n",
742  rank,
743  remaining,
744  param->offset + length - remaining);
745  }
746 
747 
748  if (multi_part_upload_p) {
749 
750  // For N:1, part-numbers must have a global ordering for the
751  // components of the final object. param->part_number is
752  // incremented by 1 per write, on each rank. This lets us use it
753  // to compute a global part-numbering.
754  //
755  // In the N:N case, we only need to increment part-numbers within
756  // each rank.
757  //
758  // In the N:1 case, the global order of part-numbers we're writing
759  // depends on whether wer're writing strided or segmented, in
760  // other words, how <offset> and <remaining> are acutally
761  // positioning the parts being written. [See discussion at
762  // S3_Close_internal().]
763  //
764  // NOTE: 's3curl.pl --debug' shows StringToSign having partNumber
765  // first, even if I put uploadId first in the URL. Maybe
766  // that's what the server will do. GetStringToSign() in
767  // aws4c is not clever about this, so we spoon-feed args in
768  // the proper order.
769 
770  size_t part_number;
771  if (n_to_1) {
772  if (segmented) { // segmented
773  size_t parts_per_rank = param->blockSize / param->transferSize;
774  part_number = (rank * parts_per_rank) + param->part_number;
775  }
776  else // strided
777  part_number = (param->part_number * param->numTasks) + rank;
778  }
779  else
780  part_number = param->part_number;
781  ++ param->part_number;
782 
783 
784  // if (verbose >= VERBOSE_3) {
785  // fprintf( stdout, "rank %d of %d writing (%s,%s) part_number %lld\n",
786  // rank,
787  // param->numTasks,
788  // (n_to_1 ? "N:1" : "N:N"),
789  // (segmented ? "segmented" : "strided"),
790  // part_number);
791  // }
792 
793  snprintf(buff, BUFF_SIZE,
794  "%s?partNumber=%d&uploadId=%s",
795  fname, part_number, param->UploadId);
796 
797  // For performance, we append <data_ptr> directly into the linked list
798  // of data in param->io_buf. We are "appending" rather than
799  // "extending", so the added buffer is seen as written data, rather
800  // than empty storage.
801  //
802  // aws4c parses some header-fields automatically for us (into members
803  // of the IOBuf). After s3_put2(), we can just read the etag from
804  // param->io_buf->eTag. The server actually returns literal
805  // quote-marks, at both ends of the string.
806 
807  aws_iobuf_reset(param->io_buf);
808  aws_iobuf_append_static(param->io_buf, data_ptr, remaining);
809  AWS4C_CHECK( s3_put(param->io_buf, buff) );
810  AWS4C_CHECK_OK( param->io_buf );
811 
812  // if (verbose >= VERBOSE_3) {
813  // printf("rank %d: read ETag = '%s'\n", rank, param->io_buf->eTag);
814  // if (strlen(param->io_buf->eTag) != ETAG_SIZE+2) { /* quotes at both ends */
815  // fprintf(stderr, "Rank %d: ERROR: expected ETag to be %d hex digits\n",
816  // rank, ETAG_SIZE);
817  // exit(1);
818  // }
819  // }
820 
821  if (verbose >= VERBOSE_3) {
822  fprintf( stdout, "rank %d of %d (%s,%s) offset %lld, part# %lld --> ETag %s\n",
823  rank,
824  param->numTasks,
825  (n_to_1 ? "N:1" : "N:N"),
826  (segmented ? "segmented" : "strided"),
827  offset,
828  part_number,
829  param->io_buf->eTag); // incl quote-marks at [0] and [len-1]
830  }
831  if (strlen(param->io_buf->eTag) != ETAG_SIZE+2) { /* quotes at both ends */
832  fprintf(stderr, "Rank %d: ERROR: expected ETag to be %d hex digits\n",
833  rank, ETAG_SIZE);
834  exit(1);
835  }
836 
837  // save the eTag for later
838  //
839  // memcpy(etag, param->io_buf->eTag +1, strlen(param->io_buf->eTag) -2);
840  // etag[ETAG_SIZE] = 0;
841  aws_iobuf_append(param->etags,
842  param->io_buf->eTag +1,
843  strlen(param->io_buf->eTag) -2);
844  // DEBUGGING
845  if (verbose >= VERBOSE_4) {
846  printf("rank %d: part %d = ETag %s\n", rank, part_number, param->io_buf->eTag);
847  }
848 
849  // drop ptrs to <data_ptr>, in param->io_buf
850  aws_iobuf_reset(param->io_buf);
851  }
852  else { // use EMC's byte-range write-support, instead of MPU
853 
854 
855  // NOTE: You must call 's3_enable_EMC_extensions(1)' for
856  // byte-ranges to work for writes.
857  if (n_to_n)
858  s3_set_byte_range(-1,-1); // EMC header "Range: bytes=-1-" means "append"
859  else
860  s3_set_byte_range(offset, remaining);
861 
862  // For performance, we append <data_ptr> directly into the linked list
863  // of data in param->io_buf. We are "appending" rather than
864  // "extending", so the added buffer is seen as written data, rather
865  // than empty storage.
866  aws_iobuf_reset(param->io_buf);
867  aws_iobuf_append_static(param->io_buf, data_ptr, remaining);
868  AWS4C_CHECK ( s3_put(param->io_buf, file) );
869  AWS4C_CHECK_OK( param->io_buf );
870 
871  // drop ptrs to <data_ptr>, in param->io_buf
872  aws_iobuf_reset(param->io_buf);
873  }
874 
875 
876  if ( param->fsyncPerWrite == TRUE ) {
877  WARN("S3 doesn't support 'fsync'" ); /* does it? */
878  }
879 
880  }
881  else { /* READ or CHECK */
882 
883  if (verbose >= VERBOSE_3) {
884  fprintf( stdout, "rank %d reading from offset %lld\n",
885  rank,
886  param->offset + length - remaining );
887  }
888 
889  // read specific byte-range from the object
890  // [This is included in the "pure" S3 spec.]
891  s3_set_byte_range(offset, remaining);
892 
893  // For performance, we append <data_ptr> directly into the linked
894  // list of data in param->io_buf. In this case (i.e. reading),
895  // we're "extending" rather than "appending". That means the
896  // buffer represents empty storage, which will be filled by the
897  // libcurl writefunction, invoked via aws4c.
898  aws_iobuf_reset(param->io_buf);
899  aws_iobuf_extend_static(param->io_buf, data_ptr, remaining);
900  AWS4C_CHECK( s3_get(param->io_buf, file) );
901  if (param->io_buf->code != 206) { /* '206 Partial Content' */
902  snprintf(buff, BUFF_SIZE,
903  "Unexpected result (%d, '%s')",
904  param->io_buf->code, param->io_buf->result);
905  ERR_SIMPLE(buff);
906  }
907 
908  // drop refs to <data_ptr>, in param->io_buf
909  aws_iobuf_reset(param->io_buf);
910  }
911 
912 
913  if (param->verbose >= VERBOSE_2) {
914  printf("<- S3_Xfer\n");
915  }
916  return ( length );
917 }
918 
919 
920 static
922 S3_Xfer(int access,
923  void* file,
924  IOR_size_t* buffer,
925  IOR_offset_t length,
926  IOR_param_t* param ) {
927  S3_Xfer_internal(access, file, buffer, length, param, TRUE);
928 }
929 static
931 EMC_Xfer(int access,
932  void* file,
933  IOR_size_t* buffer,
934  IOR_offset_t length,
935  IOR_param_t* param ) {
936  S3_Xfer_internal(access, file, buffer, length, param, FALSE);
937 }
938 
939 
940 
941 
942 
943 /*
944  * Does this even mean anything, for HTTP/S3 ?
945  *
946  * I believe all interactions with the server are considered complete at
947  * the time we get a response, e.g. from s3_put(). Therefore, fsync is
948  * kind of meaningless, for REST/S3.
949  *
950  * In future, we could extend our interface so as to allow a non-blocking
951  * semantics, for example with the libcurl "multi" interface, and/or by
952  * adding threaded callback handlers to obj_put(). *IF* we do that, *THEN*
953  * we should revisit 'fsync'.
954  *
955  * Another special case is multi-part upload, where many parallel clients
956  * may be writing to the same "file". (It looks like param->filePerProc
957  * would be the flag to check, for this.) Maybe when you called 'fsync',
958  * you meant that you wanted *all* the clients to be complete? That's not
959  * really what fsync would do. In the N:1 case, this is accomplished by
960  * S3_Close(). If you really wanted this behavior from S3_Fsync, we could
961  * have S3_Fsync call S3_close.
962  *
963  * As explained above, we may eventually want to consider the following:
964  *
965  * (1) thread interaction with any handlers that are doing ongoing
966  * interactions with the socket, to make sure they have finished all
967  * actions and gotten responses.
968  *
969  * (2) MPI barrier for all clients involved in a multi-part upload.
970  * Presumably, for IOR, when we are doing N:1, all clients are
971  * involved in that transfer, so this would amount to a barrier on
972  * MPI_COMM_WORLD.
973  */
974 
975 static
976 void
977 S3_Fsync( void *fd, IOR_param_t * param ) {
978  if (param->verbose >= VERBOSE_2) {
979  printf("-> S3_Fsync [no-op]\n");
980  }
981 
982  if (param->verbose >= VERBOSE_2) {
983  printf("<- S3_Fsync\n");
984  }
985 }
986 
987 
988 /*
989  * It seems the only kind of "close" that ever needs doing for S3 is in the
990  * case of multi-part upload (i.e. N:1). In this case, all the parties to
991  * the upload must provide their ETags to a single party (e.g. rank 0 in an
992  * MPI job). Then the rank doing the closing can generate XML and complete
993  * the upload.
994  *
995  * ISSUE: The S3 spec says that a multi-part upload can have at most 10,000
996  * parts. Does EMC allow more than this? (NOTE the spec also says
997  * parts must be at leaast 5MB, but EMC definitely allows smaller
998  * parts than that.)
999  *
1000  * ISSUE: All Etags must be sent from a single rank, in a single
1001  * transaction. If the issue above (regarding 10k Etags) is
1002  * resolved by a discovery that EMC supports more than 10k ETags,
1003  * then, for large-enough files (or small-enough transfer-sizes) an
1004  * N:1 write may generate more ETags than the single closing rank
1005  * can hold in memory. In this case, there are several options,
1006  * outlined
1007  *
1008  *
1009 
1010  * See S3_Fsync() for some possible considerations.
1011  */
1012 
1013 static
1014 void
1016  IOR_param_t* param,
1017  int multi_part_upload_p ) {
1018 
1019  char* fname = (char*)fd; /* see NOTE above S3_Create_Or_Open() */
1020 
1021  // easier to think
1022  int n_to_n = param->filePerProc;
1023  int n_to_1 = (! n_to_n);
1024  int segmented = (param->segmentCount == 1);
1025 
1026  if (param->verbose >= VERBOSE_2) {
1027  printf("-> S3_Close('%s', ,%d) %s\n",
1028  fname,
1029  multi_part_upload_p,
1030  ((n_to_n) ? "N:N" : ((segmented) ? "N:1(seg)" : "N:1(str)")));
1031  }
1032 
1033  if (param->open == WRITE) {
1034 
1035 
1036  // finalizing Multi-Part Upload (for N:1 or N:N)
1037  if (multi_part_upload_p) {
1038 
1039 
1040  size_t etag_data_size = param->etags->write_count; /* local ETag data (bytes) */
1041  size_t etags_per_rank = etag_data_size / ETAG_SIZE; /* number of local etags */
1042 
1043  // --- create XML containing ETags in an IOBuf for "close" request
1044  IOBuf* xml = NULL;
1045 
1046 
1047  if (n_to_1) {
1048 
1049  // for N:1, gather all Etags at Rank0
1050  MPI_Datatype mpi_size_t;
1051  if (sizeof(size_t) == sizeof(int))
1052  mpi_size_t = MPI_INT;
1053  else if (sizeof(size_t) == sizeof(long))
1054  mpi_size_t = MPI_LONG;
1055  else
1056  mpi_size_t = MPI_LONG_LONG;
1057 
1058  // Everybody should have the same number of ETags (?)
1059  size_t etag_count_max = 0; /* highest number on any proc */
1060  MPI_Allreduce(&etags_per_rank, &etag_count_max,
1061  1, mpi_size_t, MPI_MAX, param->testComm);
1062  if (etags_per_rank != etag_count_max) {
1063  printf("Rank %d: etag count mismatch: max:%d, mine:%d\n",
1064  rank, etag_count_max, etags_per_rank);
1065  MPI_Abort(param->testComm, 1);
1066  }
1067 
1068  // collect ETag data at Rank0
1069  aws_iobuf_realloc(param->etags); /* force single contiguous buffer */
1070  char* etag_data = param->etags->first->buf; /* per-rank data, contiguous */
1071 
1072  if (rank == 0) {
1073  char* etag_ptr;
1074  int i;
1075  int j;
1076  int rnk;
1077 
1078  char* etag_vec = (char*)malloc((param->numTasks * etag_data_size) +1);
1079  if (! etag_vec) {
1080  fprintf(stderr, "rank 0 failed to malloc %d bytes\n",
1081  param->numTasks * etag_data_size);
1082  MPI_Abort(param->testComm, 1);
1083  }
1084  MPI_Gather(etag_data, etag_data_size, MPI_BYTE,
1085  etag_vec, etag_data_size, MPI_BYTE, 0, MPI_COMM_WORLD);
1086 
1087  // --- debugging: show the gathered etag data
1088  // (This shows the raw concatenated etag-data from each node.)
1089  if (param->verbose >= VERBOSE_4) {
1090 
1091  printf("rank 0: gathered %d etags from all ranks:\n", etags_per_rank);
1092  etag_ptr=etag_vec;
1093  for (rnk=0; rnk<param->numTasks; ++rnk) {
1094  printf("\t[%d]: '", rnk);
1095 
1096  int ii;
1097  for (ii=0; ii<etag_data_size; ++ii) /* NOT null-terminated! */
1098  printf("%c", etag_ptr[ii]);
1099 
1100  printf("'\n");
1101  etag_ptr += etag_data_size;
1102  }
1103  }
1104 
1105 
1106  // add XML for *all* the parts. The XML must be ordered by
1107  // part-number. Each rank wrote <etags_per_rank> parts,
1108  // locally. At rank0, the etags for each rank are now
1109  // stored as a continguous block of text, with the blocks
1110  // stored in rank order in etag_vec. In other words, our
1111  // internal rep at rank 0 matches the "segmented" format.
1112  // From this, we must select etags in an order matching how
1113  // they appear in the actual object, and give sequential
1114  // part-numbers to the resulting sequence.
1115  //
1116  // That ordering of parts in the actual written object
1117  // varies according to whether we wrote in the "segmented"
1118  // or "strided" format.
1119  //
1120  // supposing N ranks, and P parts per rank:
1121  //
1122  // segmented:
1123  //
1124  // all parts for a given rank are consecutive.
1125  // rank r writes these parts:
1126  //
1127  // rP, rP+1, ... (r+1)P -1
1128  //
1129  // i.e. rank0 writes parts 0,1,2,3 ... P-1
1130  //
1131  //
1132  // strided:
1133  //
1134  // rank r writes every P-th part, starting with r.
1135  //
1136  // r, P+r, ... (P-1)P + r
1137  //
1138  // i.e. rank0 writes parts 0,P,2P,3P ... (P-1)P
1139  //
1140  //
1141  // NOTE: If we knew ahead of time how many parts each rank was
1142  // going to write, we could assign part-number ranges, per
1143  // rank, and then have nice locality here.
1144  //
1145  // Alternatively, we could have everyone format their own
1146  // XML text and send that, instead of just the tags. This
1147  // would increase the amount of data being sent, but would
1148  // reduce the work for rank0 to format everything.
1149 
1150  size_t i_max; // outer-loop
1151  size_t j_max; // inner loop
1152  size_t start_multiplier; // initial offset in collected data
1153  size_t stride; // in etag_vec
1154 
1155  if (segmented) { // segmented
1156  i_max = param->numTasks;
1157  j_max = etags_per_rank;
1158  start_multiplier = etag_data_size; /* one rank's-worth of Etag data */
1159  stride = ETAG_SIZE; /* one ETag */
1160  }
1161  else { // strided
1162  i_max = etags_per_rank;
1163  j_max = param->numTasks;
1164  start_multiplier = ETAG_SIZE; /* one ETag */
1165  stride = etag_data_size; /* one rank's-worth of Etag data */
1166  }
1167 
1168 
1169  xml = aws_iobuf_new();
1170  aws_iobuf_growth_size(xml, 1024 * 8);
1171 
1172  // write XML header ...
1173  aws_iobuf_append_str(xml, "<CompleteMultipartUpload>\n");
1174 
1175  int part = 0;
1176  for (i=0; i<i_max; ++i) {
1177 
1178  etag_ptr=etag_vec + (i * start_multiplier);
1179 
1180  for (j=0; j<j_max; ++j) {
1181 
1182  // etags were saved as contiguous text. Extract the next one.
1183  char etag[ETAG_SIZE +1];
1184  memcpy(etag, etag_ptr, ETAG_SIZE);
1185  etag[ETAG_SIZE] = 0;
1186 
1187  // write XML for next part, with Etag ...
1188  snprintf(buff, BUFF_SIZE,
1189  " <Part>\n"
1190  " <PartNumber>%d</PartNumber>\n"
1191  " <ETag>%s</ETag>\n"
1192  " </Part>\n",
1193  part, etag);
1194 
1195  aws_iobuf_append_str(xml, buff);
1196 
1197  etag_ptr += stride;
1198  ++ part;
1199  }
1200  }
1201 
1202  // write XML tail ...
1203  aws_iobuf_append_str(xml, "</CompleteMultipartUpload>\n");
1204  }
1205 
1206  else {
1207  MPI_Gather(etag_data, etag_data_size, MPI_BYTE,
1208  NULL, etag_data_size, MPI_BYTE, 0, MPI_COMM_WORLD);
1209  }
1210  }
1211 
1212  else { /* N:N */
1213 
1214  xml = aws_iobuf_new();
1215  aws_iobuf_growth_size(xml, 1024 * 8);
1216 
1217  // write XML header ...
1218  aws_iobuf_append_str(xml, "<CompleteMultipartUpload>\n");
1219 
1220  // all parts of our object were written from this rank.
1221  char etag[ETAG_SIZE +1];
1222  int part = 0;
1223  int i;
1224  for (i=0; i<etags_per_rank; ++i) {
1225 
1226  // TBD: Instead of reading into etag, then sprintf'ing, then
1227  // copying into xml, we could just read directly into xml
1228  int sz = aws_iobuf_get_raw(param->etags, etag, ETAG_SIZE);
1229  if (sz != ETAG_SIZE) {
1230  snprintf(buff, BUFF_SIZE,
1231  "Read of ETag %d had length %d (not %d)\n",
1232  rank, i, sz, ETAG_SIZE);
1233  ERR_SIMPLE(buff);
1234  }
1235  etag[ETAG_SIZE] = 0;
1236 
1237 
1238  // write XML for next part, with Etag ...
1239  snprintf(buff, BUFF_SIZE,
1240  " <Part>\n"
1241  " <PartNumber>%d</PartNumber>\n"
1242  " <ETag>%s</ETag>\n"
1243  " </Part>\n",
1244  part, etag);
1245 
1246  aws_iobuf_append_str(xml, buff);
1247 
1248  ++ part;
1249  }
1250 
1251  // write XML tail ...
1252  aws_iobuf_append_str(xml, "</CompleteMultipartUpload>\n");
1253  }
1254 
1255 
1256 
1257  // send request to finalize MPU
1258  if (n_to_n || (rank == 0)) {
1259 
1260  // DEBUGGING: show the XML we constructed
1261  if (param->verbose >= VERBOSE_3)
1262  debug_iobuf(xml, 1, 1);
1263 
1264  // --- POST our XML to the server.
1265  snprintf(buff, BUFF_SIZE,
1266  "%s?uploadId=%s",
1267  fname, param->UploadId);
1268 
1269  AWS4C_CHECK ( s3_post(xml, buff) );
1270  AWS4C_CHECK_OK( xml );
1271 
1272  aws_iobuf_free(xml);
1273  }
1274 
1275 
1276  // everybody reset MPU info. Allows another MPU, and frees memory.
1277  s3_MPU_reset(param);
1278 
1279  // Everybody meetup, so non-zero ranks won't go trying to stat the
1280  // N:1 file until rank0 has finished the S3 multi-part finalize.
1281  // The object will not appear to exist, until then.
1282  if (n_to_1)
1283  MPI_CHECK(MPI_Barrier(param->testComm), "barrier error");
1284  }
1285  else {
1286 
1287  // No finalization is needed, when using EMC's byte-range writing
1288  // support. However, we do need to make sure everyone has
1289  // finished writing, before anyone starts reading.
1290  if (n_to_1) {
1291  MPI_CHECK(MPI_Barrier(param->testComm), "barrier error");
1292  if (param->verbose >= VERBOSE_2)
1293  printf("rank %d: passed barrier\n", rank);
1294  }
1295  }
1296 
1297  // After writing, reset the CURL connection, so that caches won't be
1298  // used for reads.
1299  aws_reset_connection();
1300  }
1301 
1302 
1303  if (param->verbose >= VERBOSE_2) {
1304  printf("<- S3_Close\n");
1305  }
1306 }
1307 
1308 static
1309 void
1310 S3_Close( void* fd,
1311  IOR_param_t* param ) {
1312  S3_Close_internal(fd, param, TRUE);
1313 }
1314 static
1315 void
1316 EMC_Close( void* fd,
1317  IOR_param_t* param ) {
1318  S3_Close_internal(fd, param, FALSE);
1319 }
1320 
1321 
1322 
1323 
1324 /*
1325  * Delete an object through the S3 interface.
1326  *
1327  * The only reason we separate out EMC version, is because EMC bug means a
1328  * file that was written with appends can't be deleted, recreated, and then
1329  * successfully read.
1330  */
1331 
1332 static
1333 void
1334 S3_Delete( char *testFileName, IOR_param_t * param ) {
1335 
1336  if (param->verbose >= VERBOSE_2) {
1337  printf("-> S3_Delete(%s)\n", testFileName);
1338  }
1339 
1340  /* maybe initialize curl */
1341  s3_connect( param );
1342 
1343 #if 0
1344  // EMC BUG: If file was written with appends, and is deleted,
1345  // Then any future recreation will result in an object that can't be read.
1346  // this
1347  AWS4C_CHECK( s3_delete(param->io_buf, testFileName) );
1348 #else
1349  // just replace with a zero-length object for now
1350  aws_iobuf_reset(param->io_buf);
1351  AWS4C_CHECK ( s3_put(param->io_buf, testFileName) );
1352 #endif
1353 
1354  AWS4C_CHECK_OK( param->io_buf );
1355 
1356  if (param->verbose >= VERBOSE_2)
1357  printf("<- S3_Delete\n");
1358 }
1359 
1360 
1361 static
1362 void
1363 EMC_Delete( char *testFileName, IOR_param_t * param ) {
1364 
1365  if (param->verbose >= VERBOSE_2) {
1366  printf("-> EMC_Delete(%s)\n", testFileName);
1367  }
1368 
1369  /* maybe initialize curl */
1370  s3_connect( param );
1371 
1372 #if 0
1373  // EMC BUG: If file was written with appends, and is deleted,
1374  // Then any future recreation will result in an object that can't be read.
1375  // this
1376  AWS4C_CHECK( s3_delete(param->io_buf, testFileName) );
1377 #else
1378  // just replace with a zero-length object for now
1379  aws_iobuf_reset(param->io_buf);
1380  AWS4C_CHECK ( s3_put(param->io_buf, testFileName) );
1381 #endif
1382 
1383  AWS4C_CHECK_OK( param->io_buf );
1384 
1385  if (param->verbose >= VERBOSE_2)
1386  printf("<- EMC_Delete\n");
1387 }
1388 
1389 
1390 
1391 
1392 
1393 
1394 /*
1395  * HTTP HEAD returns meta-data for a "file".
1396  *
1397  * QUESTION: What should the <size> parameter be, on a HEAD request? Does
1398  * it matter? We don't know how much data they are going to send, but
1399  * obj_get_callback protects us from overruns. Will someone complain if we
1400  * request more data than the header actually takes?
1401  */
1402 
1403 static
1406  MPI_Comm testComm,
1407  char * testFileName) {
1408 
1409  if (param->verbose >= VERBOSE_2) {
1410  printf("-> S3_GetFileSize(%s)\n", testFileName);
1411  }
1412 
1413  IOR_offset_t aggFileSizeFromStat; /* i.e. "long long int" */
1414  IOR_offset_t tmpMin, tmpMax, tmpSum;
1415 
1416 
1417  /* make sure curl is connected, and inits are done */
1418  s3_connect( param );
1419 
1420  /* send HEAD request. aws4c parses some headers into IOBuf arg. */
1421  AWS4C_CHECK( s3_head(param->io_buf, testFileName) );
1422  if ( ! AWS4C_OK(param->io_buf) ) {
1423  fprintf(stderr, "rank %d: couldn't stat '%s': %s\n",
1424  rank, testFileName, param->io_buf->result);
1425  MPI_Abort(param->testComm, 1);
1426  }
1427  aggFileSizeFromStat = param->io_buf->contentLen;
1428 
1429  if (param->verbose >= VERBOSE_2) {
1430  printf("\trank %d: file-size %llu\n", rank, aggFileSizeFromStat);
1431  }
1432 
1433  if ( param->filePerProc == TRUE ) {
1434  if (param->verbose >= VERBOSE_2) {
1435  printf("\tall-reduce (1)\n");
1436  }
1437  MPI_CHECK(MPI_Allreduce(&aggFileSizeFromStat,
1438  &tmpSum, /* sum */
1439  1,
1440  MPI_LONG_LONG_INT,
1441  MPI_SUM,
1442  testComm ),
1443  "cannot total data moved" );
1444 
1445  aggFileSizeFromStat = tmpSum;
1446  }
1447  else {
1448  if (param->verbose >= VERBOSE_2) {
1449  printf("\tall-reduce (2a)\n");
1450  }
1451  MPI_CHECK(MPI_Allreduce(&aggFileSizeFromStat,
1452  &tmpMin, /* min */
1453  1,
1454  MPI_LONG_LONG_INT,
1455  MPI_MIN,
1456  testComm ),
1457  "cannot total data moved" );
1458 
1459  if (param->verbose >= VERBOSE_2) {
1460  printf("\tall-reduce (2b)\n");
1461  }
1462  MPI_CHECK(MPI_Allreduce(&aggFileSizeFromStat,
1463  &tmpMax, /* max */
1464  1,
1465  MPI_LONG_LONG_INT,
1466  MPI_MAX,
1467  testComm ),
1468  "cannot total data moved" );
1469 
1470  if ( tmpMin != tmpMax ) {
1471  if ( rank == 0 ) {
1472  WARN( "inconsistent file size by different tasks" );
1473  }
1474 
1475  /* incorrect, but now consistent across tasks */
1476  aggFileSizeFromStat = tmpMin;
1477  }
1478  }
1479 
1480  if (param->verbose >= VERBOSE_2) {
1481  printf("<- S3_GetFileSize [%llu]\n", aggFileSizeFromStat);
1482  }
1483  return ( aggFileSizeFromStat );
1484 }
void CURL
Definition: aiori-S3.c:137
static void S3_Fsync(void *, IOR_param_t *)
Definition: aiori-S3.c:977
static void EMC_Close(void *, IOR_param_t *)
Definition: aiori-S3.c:1316
static IOR_offset_t EMC_Xfer(int, void *, IOR_size_t *, IOR_offset_t, IOR_param_t *)
Definition: aiori-S3.c:931
static void * EMC_Create(char *, IOR_param_t *)
Definition: aiori-S3.c:582
static void S3_finalize()
Definition: aiori-S3.c:224
IOBuf * io_buf
Definition: aiori-S3.c:141
static void S3_Close(void *, IOR_param_t *)
Definition: aiori-S3.c:1310
int filePerProc
Definition: ior.h:104
#define VERBOSE_3
Definition: iordef.h:105
IOR_offset_t segmentCount
Definition: ior.h:116
CURLcode rc
Definition: aiori-S3.c:121
static void s3_connect(IOR_param_t *param)
Definition: aiori-S3.c:275
IOR_offset_t transferSize
Definition: ior.h:118
static IOR_offset_t S3_GetFileSize(IOR_param_t *, MPI_Comm, char *)
Definition: aiori-S3.c:1405
#define BUFF_SIZE
Definition: aiori-S3.c:116
ior_aiori_t s3_aiori
Definition: aiori-S3.c:168
size_t part_number
Definition: ior.h:176
unsigned int openFlags
Definition: ior.h:85
int fsyncPerWrite
Definition: ior.h:152
#define ERR_SIMPLE(MSG)
Definition: iordef.h:178
#define WRITE
Definition: iordef.h:95
static void EMC_Delete(char *testFileName, IOR_param_t *param)
Definition: aiori-S3.c:1363
ior_aiori_t s3_emc_aiori
Definition: aiori-S3.c:202
MPI_Comm testComm
Definition: ior.h:158
#define IOR_CREAT
Definition: aiori.h:37
#define IOR_EXCL
Definition: aiori.h:39
char * aiori_get_version()
Definition: aiori.c:149
static void * S3_Create_Or_Open_internal(char *testFileName, IOR_param_t *param, unsigned char createFile, int multi_part_upload_p)
Definition: aiori-S3.c:438
static IOR_offset_t S3_Xfer_internal(int access, void *file, IOR_size_t *buffer, IOR_offset_t length, IOR_param_t *param, int multi_part_upload_p)
Definition: aiori-S3.c:715
static void * EMC_Open(char *, IOR_param_t *)
Definition: aiori-S3.c:620
static char buff[BUFF_SIZE]
Definition: aiori-S3.c:117
MPI_Comm testComm
Definition: utilities.c:61
#define IOR_TRUNC
Definition: aiori.h:38
static void S3_init()
Definition: aiori-S3.c:217
const char * bucket_name
Definition: aiori-S3.c:124
int verbose
Definition: ior.h:138
#define MPI_CHECK(MPI_STATUS, MSG)
Definition: iordef.h:192
static void S3_Close_internal(void *fd, IOR_param_t *param, int multi_part_upload_p)
Definition: aiori-S3.c:1015
Definition: ior.h:47
static void * S3_Open(char *, IOR_param_t *)
Definition: aiori-S3.c:600
static void * S3_Create(char *, IOR_param_t *)
Definition: aiori-S3.c:570
char * UploadId
Definition: ior.h:177
static IOR_param_t param
Definition: mdtest.c:153
#define FALSE
Definition: iordef.h:71
static IOR_offset_t S3_Xfer(int, void *, IOR_size_t *, IOR_offset_t, IOR_param_t *)
Definition: aiori-S3.c:922
static void s3_disconnect(IOR_param_t *param)
Definition: aiori-S3.c:382
long long int IOR_size_t
Definition: iordef.h:124
#define WARN(MSG)
Definition: iordef.h:145
void s3_MPU_reset(IOR_param_t *param)
Definition: aiori-S3.c:400
void IOBuf
Definition: aiori-S3.c:138
int numTasks
Definition: ior.h:94
#define VERBOSE_2
Definition: iordef.h:104
IOR_offset_t offset
Definition: ior.h:119
#define VERBOSE_4
Definition: iordef.h:106
int open
Definition: ior.h:101
static void S3_Delete(char *, IOR_param_t *)
Definition: aiori-S3.c:1334
int verbose
Definition: utilities.c:60
#define IOR_CURL_INIT
Definition: aiori-S3.c:129
char * name
Definition: aiori.h:67
IOBuf * etags
Definition: aiori-S3.c:142
const int ETAG_SIZE
Definition: aiori-S3.c:119
long long int IOR_offset_t
Definition: iordef.h:123
int rank
Definition: utilities.c:57
int useO_DIRECT
Definition: ior.h:125
IOR_offset_t blockSize
Definition: ior.h:117
#define TRUE
Definition: iordef.h:75
#define IOR_CURL_S3_EMC_EXT
Definition: aiori-S3.c:131
ior_aiori_t s3_plus_aiori
Definition: aiori-S3.c:185
#define NULL
Definition: iordef.h:79