
    f<                        S r SSKrSSKrSSKJr  SSKJr  SSK	J
r
  SSK	Jr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SSKJr  SrS\R>                  S\R@                  4S jr!S r" " S S\RF                  5      r$\RJ                   " S S\RL                  5      5       r'g)z,Diagnose Google Cloud Storage common issues.    N)errors)client)arg_parsers)base)errors_util)storage_url)direct_connectivity_diagnostic)download_throughput_diagnostic)export_util)latency_diagnostic)system_info)upload_throughput_diagnostic)gcs_resource_reference)log1GB
bucket_urlreturnc                     [         R                  " 5       n UR                  U R                  5      $ ! [        R
                   a)  n[        R                  " SU R                   35      UeSnAff = f)zFetches the bucket resource for the given bucket storage URL.

Args:
  bucket_url: The URL object to get the bucket resource for.

Returns:
  The bucket resource for the given URL.

Raises:
  FatalError: If the bucket resource could not be fetched.
z)Bucket metadata could not be fetched for N)gcs_json_client
JsonClient
get_bucketbucket_name
api_errorsCloudApiErrorcommand_errors
FatalError)r   
gcs_clientes      lib/surface/storage/diagnose.pyget_bucket_resourcer    (   sk     ))+*  !7!788		!	! 

#
#
3J4J4J3KLs   2 A/$A**A/c                    [         R                  " U R                  [        R                  " U R
                  5      5        U R                  (       a  U R                  (       av  [        R                  R                  U R                  5      (       a.  [        R                  R                  U R                  5      (       d  [        SU R                   S35      eggg)z@Validates and raises error if the command arguments are invalid.zInvalid destination path: z. Please provide a valid path.N)r   raise_error_if_not_gcs_bucketcommand_pathr   storage_url_from_stringurlexportdestinationospathexistsisdir
ValueError)argss    r   _validate_argsr.   ?   s    ++
<<TXXF
 kk



''..))
*
*rww}}T=M=M/N/N 
$T%5%5$6 7 	  0O      c                   (    \ rS rSrSrSrSrSrSrSr	g)	TestTypeR   zEEnum class for specifying performance test type for diagnostic tests.DIRECT_CONNECTIVITYDOWNLOAD_THROUGHPUTUPLOAD_THROUGHPUTLATENCY N)
__name__
__module____qualname____firstlineno____doc__r3   r4   r5   r6   __static_attributes__r7   r/   r   r1   r1   R   s    M--)'r/   r1   c                   >    \ rS rSrSrSSS.r\S 5       rS rS r	S	r
g
)Diagnose[   zDiagnose Google Cloud Storage.a  
      The diagnose command runs a series of diagnostic tests for common gcloud
      storage issues.

      The `URL` argument must name an exisiting bucket for which the user
      already has write permissions. Standard billing also applies.
      Several test files/objects will be uploaded and downloaded to this bucket
      to gauge out the performance metrics. All the temporary files will be
      deleted on successfull completion of the command.

      By default, the command executes `DOWNLOAD_THROUGHPUT`,
      `UPLOAD_THROUGHPUT` and `LATENCY` tests. Tests to execute can be overriden
      by using the `--test-type` flag.
      Each test uses the command defaults or gcloud CLI configurations for
      performing the operations. This command also provides a way to override
      these values via means of different flags like `--process-count`,
      `--thread-count`, `--download-type`, etc.

      The command outputs a diagnostic report with sytem information like free
      memory, available CPU, average CPU load per test, disk counter deltas and
      diagnostic information specific to individual tests on successful
      completion.

      a  

      The following command runs the default diagnostic tests on ``my-bucket''
      bucket:

      $ {command} gs://my-bucket

      The following command runs only UPLOAD_THROUGHPUT and DOWNLOAD_THROUGHPUT
      diagnostic tests:

      $ {command} gs://my-bucket --test-type=UPLOAD_THROUGHPUT,DOWNLOAD_THROUGHPUT

      The following command runs the diagnostic tests using ``10'' objects of
      ``1MiB'' size each with ``10'' threads and ``10'' processes at max:

      $ {command} gs://my-bucket --no-of-objects=10 --object-size=1MiB
      --process-count=10 --thread-count=10

      The following command can be used to bundle and export the diagnostic
      information to a user defined ``PATH'' destination:

      $ {command} gs://my-bucket --export --destination=<PATH>
      )DESCRIPTIONEXAMPLESc                    UR                  S5        UR                  S[        SS9  UR                  S[        R                  " [        [         Vs/ s H  o"R                  PM     sn5      S9SS/ S	9  UR                  S
[        [        R                   Vs/ s H  nUR                  PM     sn5      [        R                  R                  SS9  UR                  SSS9  UR                  S[        [        R                   Vs/ s H  nUR                  PM     sn5      [        R                  R                  SS9  UR                  S[        R                  " SS9SS9  UR                  S[        R                  " SS9SS9  UR                  SSS9nUR                  SS[        R                  " SS9SS9  UR                  SSSSS9nUR                  S [        R                  " [         S!9S"S9  UR                  S#S$[        R                  " [        R                  " [         S!9S%9S&S'9  UR                  SS(S9nUR                  S)S*SS+S,9  UR                  S-[        S.S9  UR"                  R%                  S/5        g s  snf s  snf s  snf )0NFr%   z+Bucket URL to use for the diagnostic tests.)typehelpz--test-type)choices
TEST_TYPESaq  
        Tests to run as part of this diagnosis. Following tests are supported:

        DIRECT_CONNECTIVITY: Run a test upload over the Direct Connectivity
        network path and run other diagnostics if the upload fails.

        DOWNLOAD_THROUGHPUT: Upload objects to the specified bucket and record
        the number of bytes transferred per second.

        UPLOAD_THROUGHPUT: Download objects from the specified bucket and
        record the number of bytes transferred per second.

        LATENCY: Write the objects, retrieve their metadata, read the objects,
        and record latency of each operation.
        )rD   metavarrE   defaultz--download-typeau  
        Download strategy to use for the DOWNLOAD_THROUGHPUT diagnostic test.

        STREAMING: Downloads the file in memory, does not use parallelism.
        `--process-count` and `--thread-count` flag values will be ignored if
        provided.

        SLICED: Performs a [sliced download](https://cloud.google.com/storage/docs/sliced-object-downloads)
        of objects to a directory.
        Parallelism can be controlled via `--process-count` and `--thread-count`
        flags.

        FILE: Download objects as files. Parallelism can be controlled via
        `--process-count` and `--thread-count` flags.
        )rF   rI   rE   z--logs-pathzNIf the diagnostic supports writing logs, write the logs to this file location.)rE   z--upload-typea  
        Upload strategy to use for the _UPLOAD_THROUGHPUT_ diagnostic test.

        FILE: Uploads files to a bucket. Parallelism can be controlled via
        `--process-count` and `--thread-count` flags.

        PARALLEL_COMPOSITE: Uploads files using a [parallel
        composite strategy](https://cloud.google.com/storage/docs/parallel-composite-uploads).
        Parallelism can be controlled via `--process-count` and `--thread-count`
        flags.

        STREAMING: Streams the data to the bucket, does not use parallelism.
        `--process-count` and `--thread-count` flag values will be ignored if
        provided.
        z--process-count   )lower_boundz;Number of processes at max to use for each diagnostic test.z--thread-countz9Number of threads at max to use for each diagnostic test.zObject properties:)	sort_argsrE   z--object-countTz2Number of objects to use for each diagnostic test.)requiredrD   rE   zObject size properties:)mutexrL   rE   rM   z--object-size)upper_boundz,Object size to use for the diagnostic tests.z--object-sizesOBJECT_SIZES)element_typez
        List of object sizes to use for the tests. Sizes should be
        provided for each object specified using `--object-count` flag.
        )rH   rD   rE   zExport diagnostic bundle.z--export
store_truea  
        Generate and export a diagnostic bundle. The following
        information will be bundled and exported into a gzipped tarball
        (.tar.gz):

        - Latest gcloud CLI logs.
        - Output of running the `gcloud storage diagnose` command.
        - Output of running the `gcloud info --anonymize` command.

        Note: This command generates a bundle containing system information like
        disk counter detlas, CPU information and system configurations. Please
        exercise caution while sharing.
        )actionrM   rE   z--destinationzCDestination file path where the diagnostic bundle will be exported.a  
                                  table(
                                    name,
                                    operation_results[]:format='table[box](name,payload_description:wrap,result:wrap)'
                                  )
                                  )SetSortArgsadd_argumentstrr   ArgListsortedr1   value"download_throughput_diagnostic_libDownloadTypeFILE upload_throughput_diagnostic_lib
UploadType
BoundedInt	add_group
BinarySize_OBJECT_SIZE_UPPER_BOUNDdisplay_info	AddFormat)clsparseroptionobject_properties_groupobject_size_properties_groupexport_groups         r   ArgsDiagnose.Args   s   
u
:  
   x@xVLLx@A
  +  . <II
I LLI
  3??DD  .    :EE
E LLE
  1;;@@  0 ##2J  
 ##2H   %..2 /  ((##2A	 )  $;#D#D&	 $E $  !--##0HI; . 
 !--  $//4

 .  ##9 $ L 	  $ 	   !! #& 'm A,
<
s   I;J Jc           	         SnUR                   (       aw  UR                  (       a;  [        UR                  5      UR                   :w  a  [        S5      eUR                  nO+UR                  (       a  UR                  /UR                   -  n[
        R                  " 5       n/ n[
        R                  " XV5         [        R                  R                  U;   aB  [        R                  " UU5      nUR                  5         UR                  UR                  5        [        R                   R                  U;   aw  ["        R$                  " UR&                  5      n["        R(                  " UUUUR*                  UR,                  S9n	U	R                  5         UR                  U	R                  5        [        R.                  R                  U;   aw  [0        R2                  " UR4                  5      n
[0        R6                  " UU
UUR*                  UR,                  S9nUR                  5         UR                  UR                  5        UR                  [
        R8                  " U5      5        UsSSS5        $ ! , (       d  f       g= f)z+Runs test with system performance tracking.NzCNumber of object sizes provided should match the number of objects.)process_countthread_count)object_countobject_sizeslenr,   object_sizer   get_system_info_provider)get_disk_io_stats_delta_diagnostic_resultr1   r6   rY   latency_diagnostic_libLatencyDiagnosticexecuteappendresultr4   rZ   r[   download_typeDownloadThroughputDiagnosticrn   ro   r5   r]   r^   upload_typeUploadThroughputDiagnostic!get_system_info_diagnostic_result)selfr-   
url_objecttests_to_runrq   system_info_providertest_resultsr   r{   r
   r}   r   s               r   $_run_tests_with_performance_tracking-Diagnose._run_tests_with_performance_tracking:  s    L			t  !T%6%66 
 **,(()D,=,==&??AL		>	>
 
				<	/3EE
 	""$.556		%	%	+	+|	;:GG
 /KK"00!.. 	' 	'..0:AAB		#	#	)	)\	96AA
 -GG"00!.. 	% 	%,,.8??@ 

7
78L
M a
 
 
s   8F1I33
Jc                    [         R                  R                  [         R                  R                  [         R                  R                  /n[        U5        [        R                  " UR                  5      n[        U5      n[        R                  R                  SUR                   S35        [        R                  R                  SUR                   35        [        R                  R                  SUR                   35        UR                   (       a  UR                   nOUnU[         R"                  R                  /:X  a  / nOU R%                  XU5      n[         R"                  R                  U;   aJ  [&        R(                  " UUR*                  S9nUR-                  5         UR/                  UR0                  5        UR2                  (       an  [        R                  R                  S5        [4        R6                  " XaR8                  5      n[        R                  R                  SR;                  U5      5        g [        R                  R                  S5        U$ )	NzUsing z! bucket for the diagnostic tests.zBucket location : zBucket storage class : )	logs_pathzExporting diagnostic bundle...z-Successfully exported diagnostic bundle to {}zGenerating diagnostic report...)r1   r4   rY   r6   r5   r.   r   r$   r%   r    r   statusPrintnamelocationdefault_storage_class	test_typer3   r   r	   DirectConnectivityDiagnosticr   rx   ry   rz   r&   r   export_diagnostic_bundler'   format)	r   r-   default_testsr   bucket_resourcer   r   direct_connectivityexport_paths	            r   RunDiagnose.Run  s    	$$**""((M 444TXX>J)*5OJJ
%%&&GH JJ)/*B*B)CDEJJ
!/"G"G!HI ~~^^l"l44::;;l>>
Ll ##))\9
(
E
E  !!#-445{{	jj7888
((k 
jj
9
@
@
M JJ67r/   r7   N)r8   r9   r:   r;   r<   detailed_helpclassmethodrk   r   r   r=   r7   r/   r   r?   r?   [   s:    &
0
30-d f' f'PDL5r/   r?   )(r<   enumr(   googlecloudsdk.api_lib.storager   r   'googlecloudsdk.api_lib.storage.gcs_jsonr   r   googlecloudsdk.callioper   r   "googlecloudsdk.command_lib.storager   r   r   +googlecloudsdk.command_lib.storage.diagnoser	   r
   rZ   r   r   rv   r   r   r]   ,googlecloudsdk.command_lib.storage.resourcesr   googlecloudsdk.corer   rb   
StorageUrlGcsBucketResourcer    r.   Enumr1   DefaultUniverseOnlyCommandr?   r7   r/   r   <module>r      s     3  	 ? M / ( G : : V | C d C x O #  &&--.&tyy  Yt|| Y Yr/   