blob: 1f977019e68192614398ed78fa0bf9ed4bb5b5f6 [file] [log] [blame]
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>apache_beam.io.gcp.datastore.v1new.adaptive_throttler &mdash; Apache Beam 2.47.0 documentation</title>
<script type="text/javascript" src="../../../../../../_static/js/modernizr.min.js"></script>
<script type="text/javascript" id="documentation_options" data-url_root="../../../../../../" src="../../../../../../_static/documentation_options.js"></script>
<script type="text/javascript" src="../../../../../../_static/jquery.js"></script>
<script type="text/javascript" src="../../../../../../_static/underscore.js"></script>
<script type="text/javascript" src="../../../../../../_static/doctools.js"></script>
<script type="text/javascript" src="../../../../../../_static/language_data.js"></script>
<script async="async" type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/latest.js?config=TeX-AMS-MML_HTMLorMML"></script>
<script type="text/javascript" src="../../../../../../_static/js/theme.js"></script>
<link rel="stylesheet" href="../../../../../../_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="../../../../../../_static/pygments.css" type="text/css" />
<link rel="index" title="Index" href="../../../../../../genindex.html" />
<link rel="search" title="Search" href="../../../../../../search.html" />
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search" >
<a href="../../../../../../index.html" class="icon icon-home"> Apache Beam
</a>
<div class="version">
2.47.0
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../../../../../../search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../apache_beam.coders.html">apache_beam.coders package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../apache_beam.dataframe.html">apache_beam.dataframe package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../apache_beam.io.html">apache_beam.io package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../apache_beam.metrics.html">apache_beam.metrics package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../apache_beam.ml.html">apache_beam.ml package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../apache_beam.options.html">apache_beam.options package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../apache_beam.portability.html">apache_beam.portability package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../apache_beam.runners.html">apache_beam.runners package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../apache_beam.testing.html">apache_beam.testing package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../apache_beam.transforms.html">apache_beam.transforms package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../apache_beam.typehints.html">apache_beam.typehints package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../apache_beam.utils.html">apache_beam.utils package</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../apache_beam.yaml.html">apache_beam.yaml package</a></li>
</ul>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../apache_beam.error.html">apache_beam.error module</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../apache_beam.pipeline.html">apache_beam.pipeline module</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../../../../apache_beam.pvalue.html">apache_beam.pvalue module</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="../../../../../../index.html">Apache Beam</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="../../../../../../index.html">Docs</a> &raquo;</li>
<li><a href="../../../../../index.html">Module code</a> &raquo;</li>
<li>apache_beam.io.gcp.datastore.v1new.adaptive_throttler</li>
<li class="wy-breadcrumbs-aside">
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<h1>Source code for apache_beam.io.gcp.datastore.v1new.adaptive_throttler</h1><div class="highlight"><pre>
<span></span><span class="c1">#</span>
<span class="c1"># Licensed to the Apache Software Foundation (ASF) under one or more</span>
<span class="c1"># contributor license agreements. See the NOTICE file distributed with</span>
<span class="c1"># this work for additional information regarding copyright ownership.</span>
<span class="c1"># The ASF licenses this file to You under the Apache License, Version 2.0</span>
<span class="c1"># (the &quot;License&quot;); you may not use this file except in compliance with</span>
<span class="c1"># the License. You may obtain a copy of the License at</span>
<span class="c1">#</span>
<span class="c1"># http://www.apache.org/licenses/LICENSE-2.0</span>
<span class="c1">#</span>
<span class="c1"># Unless required by applicable law or agreed to in writing, software</span>
<span class="c1"># distributed under the License is distributed on an &quot;AS IS&quot; BASIS,</span>
<span class="c1"># WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.</span>
<span class="c1"># See the License for the specific language governing permissions and</span>
<span class="c1"># limitations under the License.</span>
<span class="c1">#</span>
<span class="c1"># Utility functions &amp; classes that are _not_ specific to the datastore client.</span>
<span class="c1">#</span>
<span class="c1"># For internal use only; no backwards-compatibility guarantees.</span>
<span class="c1"># pytype: skip-file</span>
<span class="kn">import</span> <span class="nn">random</span>
<span class="kn">from</span> <span class="nn">apache_beam.io.gcp.datastore.v1new</span> <span class="kn">import</span> <span class="n">util</span>
<div class="viewcode-block" id="AdaptiveThrottler"><a class="viewcode-back" href="../../../../../../apache_beam.io.gcp.datastore.v1new.adaptive_throttler.html#apache_beam.io.gcp.datastore.v1new.adaptive_throttler.AdaptiveThrottler">[docs]</a><span class="k">class</span> <span class="nc">AdaptiveThrottler</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Implements adaptive throttling.</span>
<span class="sd"> See</span>
<span class="sd"> https://landing.google.com/sre/book/chapters/handling-overload.html#client-side-throttling-a7sYUg</span>
<span class="sd"> for a full discussion of the use case and algorithm applied.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="c1"># The target minimum number of requests per samplePeriodMs, even if no</span>
<span class="c1"># requests succeed. Must be greater than 0, else we could throttle to zero.</span>
<span class="c1"># Because every decision is probabilistic, there is no guarantee that the</span>
<span class="c1"># request rate in any given interval will not be zero. (This is the +1 from</span>
<span class="c1"># the formula in</span>
<span class="c1"># https://landing.google.com/sre/book/chapters/handling-overload.html )</span>
<span class="n">MIN_REQUESTS</span> <span class="o">=</span> <span class="mi">1</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">window_ms</span><span class="p">,</span> <span class="n">bucket_ms</span><span class="p">,</span> <span class="n">overload_ratio</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Initializes AdaptiveThrottler.</span>
<span class="sd"> Args:</span>
<span class="sd"> window_ms: int, length of history to consider, in ms, to set</span>
<span class="sd"> throttling.</span>
<span class="sd"> bucket_ms: int, granularity of time buckets that we store data in, in</span>
<span class="sd"> ms.</span>
<span class="sd"> overload_ratio: float, the target ratio between requests sent and</span>
<span class="sd"> successful requests. This is &quot;K&quot; in the formula in</span>
<span class="sd"> https://landing.google.com/sre/book/chapters/handling-overload.html.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_all_requests</span> <span class="o">=</span> <span class="n">util</span><span class="o">.</span><span class="n">MovingSum</span><span class="p">(</span><span class="n">window_ms</span><span class="p">,</span> <span class="n">bucket_ms</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_successful_requests</span> <span class="o">=</span> <span class="n">util</span><span class="o">.</span><span class="n">MovingSum</span><span class="p">(</span><span class="n">window_ms</span><span class="p">,</span> <span class="n">bucket_ms</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_overload_ratio</span> <span class="o">=</span> <span class="nb">float</span><span class="p">(</span><span class="n">overload_ratio</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_random</span> <span class="o">=</span> <span class="n">random</span><span class="o">.</span><span class="n">Random</span><span class="p">()</span>
<span class="k">def</span> <span class="nf">_throttling_probability</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">now</span><span class="p">):</span>
<span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_all_requests</span><span class="o">.</span><span class="n">has_data</span><span class="p">(</span><span class="n">now</span><span class="p">):</span>
<span class="k">return</span> <span class="mi">0</span>
<span class="n">all_requests</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_all_requests</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">now</span><span class="p">)</span>
<span class="n">successful_requests</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_successful_requests</span><span class="o">.</span><span class="n">sum</span><span class="p">(</span><span class="n">now</span><span class="p">)</span>
<span class="k">return</span> <span class="nb">max</span><span class="p">(</span>
<span class="mi">0</span><span class="p">,</span> <span class="p">(</span><span class="n">all_requests</span> <span class="o">-</span> <span class="bp">self</span><span class="o">.</span><span class="n">_overload_ratio</span> <span class="o">*</span> <span class="n">successful_requests</span><span class="p">)</span> <span class="o">/</span>
<span class="p">(</span><span class="n">all_requests</span> <span class="o">+</span> <span class="n">AdaptiveThrottler</span><span class="o">.</span><span class="n">MIN_REQUESTS</span><span class="p">))</span>
<div class="viewcode-block" id="AdaptiveThrottler.throttle_request"><a class="viewcode-back" href="../../../../../../apache_beam.io.gcp.datastore.v1new.adaptive_throttler.html#apache_beam.io.gcp.datastore.v1new.adaptive_throttler.AdaptiveThrottler.throttle_request">[docs]</a> <span class="k">def</span> <span class="nf">throttle_request</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">now</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Determines whether one RPC attempt should be throttled.</span>
<span class="sd"> This should be called once each time the caller intends to send an RPC; if</span>
<span class="sd"> it returns true, drop or delay that request (calling this function again</span>
<span class="sd"> after the delay).</span>
<span class="sd"> Args:</span>
<span class="sd"> now: int, time in ms since the epoch</span>
<span class="sd"> Returns:</span>
<span class="sd"> bool, True if the caller should throttle or delay the request.</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="n">throttling_probability</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_throttling_probability</span><span class="p">(</span><span class="n">now</span><span class="p">)</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_all_requests</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">now</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_random</span><span class="o">.</span><span class="n">uniform</span><span class="p">(</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span> <span class="o">&lt;</span> <span class="n">throttling_probability</span></div>
<div class="viewcode-block" id="AdaptiveThrottler.successful_request"><a class="viewcode-back" href="../../../../../../apache_beam.io.gcp.datastore.v1new.adaptive_throttler.html#apache_beam.io.gcp.datastore.v1new.adaptive_throttler.AdaptiveThrottler.successful_request">[docs]</a> <span class="k">def</span> <span class="nf">successful_request</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">now</span><span class="p">):</span>
<span class="w"> </span><span class="sd">&quot;&quot;&quot;Notifies the throttler of a successful request.</span>
<span class="sd"> Must be called once for each request (for which throttle_request was</span>
<span class="sd"> previously called) that succeeded.</span>
<span class="sd"> Args:</span>
<span class="sd"> now: int, time in ms since the epoch</span>
<span class="sd"> &quot;&quot;&quot;</span>
<span class="bp">self</span><span class="o">.</span><span class="n">_successful_requests</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">now</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span></div></div>
</pre></div>
</div>
</div>
<footer>
<hr/>
<div role="contentinfo">
<p>
&copy; Copyright
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>