Finished first tutorial.
[arvados.git] / doc / user / tutorial-job1.textile
index abab08af8e4031884cec4b8caa23bf958f6b6510..b58e7c9b15876bef2a520c3502f0c8cd41b6f1ca 100644 (file)
@@ -1,15 +1,15 @@
 ---
 layout: default
 navsection: userguide
-title: "Tutorial 1: Using the arv command"
+title: "Tutorial 1: Introduction to Keep and your first Crunch job"
 navorder: 20
 ---
 
-h1. Tutorial 1: Using the "arv" command
+h1. Tutorial 1: Introduction to Keep and your first crunch job
 
-This tutorial introduces the @arv@ command line tool and demonstrates how to run a simple script.
+This tutorial introduces the concepts and use of the Arvados Keep storage and Crunch job system using the @arv@ command line tool and Arvados Workbench.
 
-This tutorial assumes that you are logged into an Arvados VM instance, as described in "accessing arvados over ssh":#login
+*This tutorial assumes that you are logged into an Arvados VM instance, as described in "accessing arvados over ssh.":ssh-access.html#login*
 
 h2. Checking your environment
 
@@ -19,15 +19,15 @@ Check that you are able to access the Arvados API server using the following com
 $ arv user current
 </pre>
 
-If you receive the message "ARVADOS_API_HOST and ARVADOS_API_TOKEN need to be defined as environment variables", follow the instructions "Getting an API token":api-tokens.html , then return to this document.
+If you receive the message @ARVADOS_API_HOST and ARVADOS_API_TOKEN need to be defined as environment variables@, follow the instructions for "getting an API token,":api-tokens.html then return to this document.
 
-If @arv user current@ is able to access the API server, it will print out the unique identifier associated with your account, for example (you will receive a different identifier than shown in this example):
+When @arv user current@ is able to access the API server, it will print out the unique identifier associated with your account.  Here is an example (you will receive a different identifier):
 
 <pre>
 qr1hi-xioed-9z2p3pn12yqdaem
 </pre>
 
-This unique identifier represents your identity in the Arvados system and is similar to the concept of a pointer or a foreign key.  You may de-reference any identifier returned by the "arv" command using the @-h@ command line option.  For example:
+This unique identifier represents your identity in the Arvados system and is similar to the concept of a pointer or a foreign key.  You may de-reference (get the contents of) any identifier returned by the "arv" command using the @-h@ command line option.  For example:
 
 <pre>
 $ arv -h user current
@@ -53,150 +53,225 @@ $ arv -h user current
 }
 </pre>
 
-h2. Submitting your first job
+h2. Managing data in Arvados using Keep
+
+The Arvados distributed file system is called *Keep*.  Keep is a content-addressable file system.  This means that files are managed using special unique identifiers derived from the _contents_ of the file, rather than human-assigned file names (specifically, the md5 hash).  This has a number of advantages:
+* Files can be stored and replicated across a cluster of servers without requiring a central name server.
+* Systematic validation of data integrity by both server and client because the checksum is built into the identifier.
+* Minimizes data duplication (two files with the same contents will result in the same identifier, and will not be stored twice.)
+* Avoids data race conditions (an identifier always points to the same data.)
 
-We will run the "hash" program, which computes the MD5 hash of each file in a collection.
+In Keep, information is stored in *data blocks*.  Data blocks are normally between 1 byte and 64 megabytes in size.  If a file exceeds the maximum size of a single data block, the file will be split across multiple data blocks until the entire file can be stored.  These data blocks may be stored and replicated across multiple disks, servers, or clusters.  Each data block has its own identifier for the contents of that specific data block.
 
-Pick a data collection. We'll use @33a9f3842b01ea3fdf27cc582f5ea2af@ here.
+In order to reassemble the file, Keep stores a *collection* data block which lists in sequence the data blocks that make up the original file.  A collection data block may store the information for multiple files, including a directory structure.
 
-_How do I know if I have this data?  Does it come as example data with
-the arvados distribution?  Is there something notable about it, like
-it is very large and spans multiple keep blocks?_
+In this example we will use @33a9f3842b01ea3fdf27cc582f5ea2af@ which is already available on {{ site.arvados_api_host }}.  First let us examine the contents of this collection using @arv-get@:
 
 <pre>
-the_collection=33a9f3842b01ea3fdf27cc582f5ea2af
+$ arv-get 33a9f3842b01ea3fdf27cc582f5ea2af
+. 204e43b8a1185621ca55a94839582e6f+67108864+K@qr1hi b9677abbac956bd3e86b1deb28dfac03+67108864+K@qr1hi fc15aff2a762b13f521baf042140acec+67108864+K@qr1hi 323d2a3ce20370c4ca1d3462a344f8fd+25885655+K@qr1hi 0:227212247:var-GS000016015-ASM.tsv.bz2
 </pre>
 
-Pick a code version. We'll use @5565778cf15ae9af22ad392053430213e9016631@ here.
+@arv-get@ fetches the contents of the locator @33a9f3842b01ea3fdf27cc582f5ea2af@.  This is a locator for a collection data block, so it fetches the contents of the collection.  In this example, this collection consists of a single file @var-GS000016015-ASM.tsv.bz2@ which is 227212247 bytes long, and is stored using four sequential data blocks, @204e43b8a1185621ca55a94839582e6f+67108864+K@qr1hi@ , @b9677abbac956bd3e86b1deb28dfac03+67108864+K@qr1hi@ , @fc15aff2a762b13f521baf042140acec+67108864+K@qr1hi@ , @323d2a3ce20370c4ca1d3462a344f8fd+25885655+K@qr1hi@ .
 
-_How do I know if I have this code version?  What does this refer to?
-A git revision?  Or a keep id?  In what repository?_
+Let's use @arv-get@ to download the first datablock:
 
 <pre>
-the_version=5565778cf15ae9af22ad392053430213e9016631
+ $ arv-get 204e43b8a1185621ca55a94839582e6f+67108864+K@qr1hi > block1
 </pre>
 
-Make a JSON object describing the job.
+Let's look at the size and compute the md5 hash of @block1@:
 
 <pre>
-read -rd $'\000' the_job <<EOF
+ $ ls -l block1
+ -rw-r--r-- 1 you group 67108864 Dec  9 20:14 block1
+ $ md5sum block1
+ 204e43b8a1185621ca55a94839582e6f  block1
+</pre>
+
+Notice that the block identifer @204e43b8a1185621ca55a94839582e6f+67108864+K@qr1hi@ consists of:
+* the md5 hash @204e43b8a1185621ca55a94839582e6f@
+* a size hint @67108864@
+* a location hint @K@qr1hi@
+
+In fact, _only_ the md5 hash is required to find the block.
+
+Next, let's use @arv-get@ to download and reassemble @var-GS000016015-ASM.tsv.bz2@ using the following command:
+
+<pre>
+ $ arv-get 33a9f3842b01ea3fdf27cc582f5ea2af/var-GS000016015-ASM.tsv.bz2 .
+</pre>
+
+This downloads the file @var-GS000016015-ASM.tsv.bz2@ described by collection @33a9f3842b01ea3fdf27cc582f5ea2af@ from Keep and places it into the local directory.  Now that we have the file, we can compute the md5 hash of the complete file:
+
+<pre>
+ $ md5sum var-GS000016015-ASM.tsv.bz2 
+ 44b8ae3fde7a8a88d2f7ebd237625b4f  var-GS000016015-ASM.tsv.bz2
+</pre>
+
+h2. Submitting your first job
+
+In the previous section, we downloaded a file from Keep and computed the md5 hash of the complete file.  While straightforward, there are several obvious drawbacks to this approach:
+* Large files require significant time to download.
+* Very large files may exceed the scratch space of the local disk.
+* We are only able to use the local CPU to process the file.
+
+The Arvados "crunch" framework is designed to support processing very large data batches (gigabytes to terabytes) efficiently, based on the following techniques:
+* Computation is preferentially sent to nodes that store the files locally, to avoid network and disk bottlenecks in sending files around.
+* Computation acts on file streams, avoiding the need to store the entire file contents in one place.
+* Computation can be distributed among many nodes in a cluster by acting on many files or parts of a single file in parallel.
+
+For your first job, you will run the "hash" crunch script using the Arvados system.  The "hash" script computes the md5 hash of each file in a collection.
+
+Crunch jobs are described using JSON objects.  For example:
+
+<pre>
+$ read -d $'\000' the_job <<EOF
 {
- "script":"hash",
- "script_version":"$the_version",
+ "script": "hash",
+ "script_version": "arvados:master",
  "script_parameters":
  {
-  "input":"$the_collection"
+  "input": "33a9f3842b01ea3fdf27cc582f5ea2af"
  }
 }
 EOF
 </pre>
 
-_Need to explain what the json fields mean, it is explained later but
-there should be some mention up here._
-
-(The @read -rd $'\000'@ part uses a bash feature to help us get a
-multi-line string with lots of double quotation marks into a shell
-variable.)
-
-Submit the job.
-
-<pre>
-arv -h job create --job "$the_job"
-</pre>
+* @read@ is a bash builtin that stores the first line of standard input into the local shell variable @the_job@
+* @-d $'\000'@ changes the line delimiter character from newline to null so that the entire input will be considered a single line.
+* @"script"@ specifies the name of the script to run.  The script is searched for in the "crunch_scripts/" subdirectory of the @git@ checkout specified by @"script_version"@.
+* @"script_version"@ specifies the version of the script that you wish to run.  This can be in the form of an explicit @git@ revision hash, or in the form "repository:branch" (in which case it will take the HEAD of the specified branch).  Arvados logs the script version that was used in the run, enabling you to go back and re-run any past job with the guarantee that the exact same code will be used as was used in the previous run.  You can access a list of available @git@ repositories on the Arvados workbench through _Access %(rarr)&rarr;% Repositories_.
+* @"script_parameters"@ are provided to the script.  In this case, the input is the locator for the collection that we inspected in the previous section.
 
-&darr;
+Use the @arv@ command to actually submit the job.  It should print out a JSON object which describes the newly created job:
 
 <pre>
+$ arv -h job create --job "$the_job"
 {
+ "href":"https://qr1hi.arvadosapi.com/arvados/v1/jobs/qr1hi-8i9sb-j5dr6107mxzp3no",
  "kind":"arvados#job",
- "etag":"dwbrasqcozpjsqtfshzdjfiii",
- "uuid":"qr1hi-8i9sb-3i0yi357k0mauwz",
-...
+ "etag":"aulvmdxezwxo4zrw15gz1v7x3",
+ "uuid":"qr1hi-8i9sb-j5dr6107mxzp3no",
+ "owner_uuid":"qr1hi-tpzed-9zdpkpni2yddge6",
+ "created_at":"2013-12-10T17:07:08Z",
+ "modified_by_client_uuid":"qr1hi-ozdt8-obw7foaks3qjyej",
+ "modified_by_user_uuid":"qr1hi-tpzed-9zdpkpni2yddge6",
+ "modified_at":"2013-12-10T17:07:08Z",
+ "updated_at":"2013-12-10T17:07:08Z",
+ "submit_id":null,
+ "priority":null,
  "script":"hash",
  "script_parameters":{
   "input":"33a9f3842b01ea3fdf27cc582f5ea2af"
  },
- "script_version":"5565778cf15ae9af22ad392053430213e9016631",
-...
+ "script_version":"d3b10812b443dcf0189c1c432483bf7ac06507fe",
+ "cancelled_at":null,
+ "cancelled_by_client_uuid":null,
+ "cancelled_by_user_uuid":null,
+ "started_at":null,
+ "finished_at":null,
+ "output":null,
+ "success":null,
+ "running":null,
+ "is_locked_by_uuid":null,
+ "log":null,
+ "runtime_constraints":{},
+ "tasks_summary":{},
+ "dependencies":[
+  "33a9f3842b01ea3fdf27cc582f5ea2af"
+ ],
+ "log_stream_href":"https://qr1hi.arvadosapi.com/arvados/v1/jobs/qr1hi-8i9sb-j5dr6107mxzp3no/log_tail_follow"
 }
 </pre>
 
-_What is this?  An example of what "arv" returns?  What do the fields mean?_
+The job is new queued and will start running as soon as it reaches the front of the queue.  Fields to pay attention to include:
 
-h3. Monitor job progress
+ * @"uuid"@ is the unique identifier for this specific job
+ * @"script_version"@ is the actual revision of the script used.  This is useful if the version was described using the "repository:branch" format.
+ * @"log_stream_href"@ provides a means to monitor job progress, described below.
 
-_And then the magic happens.  There should be some more discussion of what
-is going on in the background once the job is submitted from the
-user's perspective.  It is queued, running, etc?._
+h3. Monitor job progress
 
-Go to Workbench, drop down the Compute menu, and click Jobs. The job you submitted should appear at the top of the list.
+Go to Workbench, and use the menu to navigate to _Compute %(rarr)&rarr;% Jobs_. The job you submitted can be identified by the *uuid* row, which will match the "uuid" field of the JSON object returned when the job was created.
 
-Hit "Refresh" until it finishes.  _We should really make the page
-autorefresh or use a streamed-update framework_
+Hit "Refresh" until it finishes.  Successful completion is indicated by a green check mark in the *status* column.
 
-You can also watch the log messages while the job runs:
+You can watch the log messages while the job runs using @curl@:
 
 <pre>
-curl -s -H "Authorization: OAuth2 $ARVADOS_API_TOKEN" \
-  "https://$ARVADOS_API_HOST/arvados/v1/jobs/JOB_UUID_HERE/log_tail_follow"
+$ curl -s -H "Authorization: OAuth2 $ARVADOS_API_TOKEN" _value_of_log_stream_href_from_arv_job_create_
 </pre>
 
-This will run until the job finishes or you hit control-C.
+* @-s@ suppress status messages from @curl@ itself
+* @-H@ addes a required HTTP header with your Arvados API token
 
-If you're running more than one job today, you can watch log messages from all of them in one stream:
-
-<pre>
-my_user_uuid=`arv user current`
-curl -s -H "Authorization: OAuth2 $ARVADOS_API_TOKEN" \
-  "https://$ARVADOS_API_HOST/arvados/v1/users/$my_user_uuid/event_stream"
-</pre>
-
-This will run until you hit control-C.
+This will run until the job finishes or is @curl@ is canceled with control-C.
 
 h3. Inspect the job output
 
-Find the output of the job by looking at the Jobs page (in the Compute menu) in Workbench, or by using the API:
+You can access the job output under the *output* column of the _Compute %(rarr)&rarr;% Jobs_ page.  Alternately, you can use the command line to access a JSON object describing the output:
 
 <pre>
-arv -h job get --uuid JOB_UUID_HERE
+$ arv -h job get --uuid _value_of_uuid_from_arv_job_create_
+{
+ "href":"https://qr1hi.arvadosapi.com/arvados/v1/jobs/qr1hi-8i9sb-zs6d9pxkr0vk175",
+ "kind":"arvados#job",
+ "etag":"eoe99lw7rnqxo7j29fh53hz",
+ "uuid":"qr1hi-8i9sb-zs6d9pxkr0vk175",
+ "owner_uuid":"qr1hi-tpzed-9zdpkpni2yddge6",
+ "created_at":"2013-12-10T17:23:26Z",
+ "modified_by_client_uuid":null,
+ "modified_by_user_uuid":"qr1hi-tpzed-9zdpkpni2yddge6",
+ "modified_at":"2013-12-10T17:23:45Z",
+ "updated_at":"2013-12-10T17:23:45Z",
+ "submit_id":null,
+ "priority":null,
+ "script":"hash",
+ "script_parameters":{
+  "input":"33a9f3842b01ea3fdf27cc582f5ea2af"
+ },
+ "script_version":"0a8c7c6fce7a9667ee42c1984a845100f51906a2",
+ "cancelled_at":null,
+ "cancelled_by_client_uuid":null,
+ "cancelled_by_user_uuid":null,
+ "started_at":"2013-12-10T17:23:29Z",
+ "finished_at":"2013-12-10T17:23:44Z",
+ "output":"880b55fb4470b148a447ff38cacdd952+54+K@qr1hi",
+ "success":true,
+ "running":false,
+ "is_locked_by_uuid":"qr1hi-tpzed-9zdpkpni2yddge6",
+ "log":"f760f3dd3105103e058a043310f7e72b+3028+K@qr1hi",
+ "runtime_constraints":{},
+ "tasks_summary":{
+  "done":2,
+  "running":0,
+  "failed":0,
+  "todo":0
+ },
+ "dependencies":[
+  "33a9f3842b01ea3fdf27cc582f5ea2af"
+ ],
+ "log_stream_href":null
+}
 </pre>
 
-The output locator will look like <code>5894dfae5d6d8edf135f0ea3dba849c2+62+K@qr1hi</code>.
+* @"output"@ is the unique identifier for this specific job's output.  This is a Keep collection.
 
 List the files in the collection:
 
 <pre>
-arv keep ls 5894dfae5d6d8edf135f0ea3dba849c2+62+K@qr1hi
-</pre>
-
-&darr;
-
-<pre>
-md5sum.txt
+$ arv-get _value_of_output_from_arv_job_get_
+. 78b268d1e03d87f8270bdee9d5d427c5+61 0:61:md5sum.txt
 </pre>
 
-Show the contents of the md5sum.txt file:
+This collection consists of the md5sum.txt file.  Use @arv-get@ to show the contents of the md5sum.txt file:
 
 <pre>
-arv keep less 5894dfae5d6d8edf135f0ea3dba849c2+62+K@qr1hi/md5sum.txt
+$ arv-get 880b55fb4470b148a447ff38cacdd952+54+K@qr1hi/md5sum.txt
+44b8ae3fde7a8a88d2f7ebd237625b4f var-GS000016015-ASM.tsv.bz2
 </pre>
 
-h3. Inspect the code
-
-The @script@ and @script_version@ attributes of a Job allow you to confirm the code that was used to run the job. Specifically, @script@ refers to a file in the @/crunch_scripts@ directory in the tree indicated by the commit hash @script_version@.
-
-Example:
-
-<pre>
-cd
-git clone git://github.com/clinicalfuture/arvados.git
-cd arvados
-git checkout $the_version
-less crunch_scripts/hash
-</pre>
+This md5 hash value should match the md5 hash which we computed by hand.
 
-_If we're going to direct the user to open up the code, some
-discussion of the python API is probably in order.  If the hash
-job is going to be the canonical first crunch map reduce program
-for everybody, than we should break down the program line-by-line and
-explain every step in detail._
+This concludes the first tutorial.  In the next tutorial, "we will inspect how the hash script works.":tutorial-firstscript.html