blob: 96393d046b724a8f56498a041812f59649ff36ae [file] [log] [blame]
<!DOCTYPE html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<meta name="keywords" content=" ">
<title>Dynamically Enabling Analytic Flows | Apache Quarks Documentation</title>
<link rel="stylesheet" type="text/css" href="../css/syntax.css">
<link rel="stylesheet" type="text/css" href="../css/font-awesome.min.css">
<!--<link rel="stylesheet" type="text/css" href="../css/bootstrap.min.css">-->
<link rel="stylesheet" type="text/css" href="../css/modern-business.css">
<link rel="stylesheet" type="text/css" href="../css/lavish-bootstrap.css">
<link rel="stylesheet" type="text/css" href="../css/customstyles.css">
<link rel="stylesheet" type="text/css" href="../css/theme-blue.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-cookie/1.4.1/jquery.cookie.min.js"></script>
<script src="../js/jquery.navgoco.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/anchor-js/2.0.0/anchor.min.js"></script>
<script src="../js/toc.js"></script>
<script src="../js/customscripts.js"></script>
<link rel="shortcut icon" href="../common_images/favicon.ico" type="image/x-icon">
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
<script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<script>
$(function () {
$('[data-toggle="tooltip"]').tooltip()
})
</script>
</head>
<body>
<!-- Navigation -->
<nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container topnavlinks">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1">
<span class="sr-only">Toggle navigation</span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="fa fa-home fa-lg navbar-brand" href="../docs/home.html">&nbsp;<span class="projectTitle"> Apache Quarks Documentation</span></a>
</div>
<div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
<ul class="nav navbar-nav navbar-right">
<!-- entries without drop-downs appear here -->
<!-- conditional logic to control which topnav appears for the audience defined in the configuration file.-->
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">GitHub Repos<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="https://github.com/apache/incubator-quarks" target="_blank">Source code</a></li>
<li><a href="https://github.com/apache/incubator-quarks-website" target="_blank">Website/Documentation</a></li>
</ul>
</li>
<li><a href="https://quarks-edge.github.io/quarks/docs/javadoc/index.html" target="_blank">Javadoc</a></li>
<!-- entries with drop-downs appear here -->
<!-- conditional logic to control which topnav appears for the audience defined in the configuration file.-->
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Quarks Resources<b class="caret"></b></a>
<ul class="dropdown-menu">
<li><a href="https://github.com/apache/incubator-quarks/releases" target="_blank">Download</a></li>
<li><a href="samples">Samples</a></li>
<li><a href="faq">FAQ</a></li>
</ul>
</li>
<!-- special insertion -->
<!-- Send feedback function -->
<script>
function SendLinkByMail(href) {
var subject= "Apache Quarks Documentation feedback";
var body = "I have some feedback about the Dynamically Enabling Analytic Flows page: ";
body += window.location.href;
body += "";
var uri = "mailto:?subject=";
uri += encodeURIComponent(subject);
uri += "&body=";
uri += encodeURIComponent(body);
window.location.href = uri;
}
</script>
<li><a href="mailto:dev@quarks.incubator.apache.org" target="_blank"><i class="fa fa-envelope-o"></i> Feedback</a></li>
<!--uncomment this block if you want simple search instead of algolia-->
<li>
<!--start search-->
<div id="search-demo-container">
<input type="text" id="search-input" placeholder="search...">
<ul id="results-container"></ul>
</div>
<script src="../js/jekyll-search.js" type="text/javascript"></script>
<script type="text/javascript">
SimpleJekyllSearch.init({
searchInput: document.getElementById('search-input'),
resultsContainer: document.getElementById('results-container'),
dataSource: '../search.json',
searchResultTemplate: '<li><a href="{url}" title="Dynamically Enabling Analytic Flows">{title}</a></li>',
noResultsText: 'No results found.',
limit: 10,
fuzzy: true,
})
</script>
<!--end search-->
</li>
</div>
<!-- /.container -->
</nav>
<!-- Page Content -->
<div class="container">
<div class="col-lg-12">&nbsp;</div>
<!-- Content Row -->
<div class="row">
<!-- Sidebar Column -->
<div class="col-md-3">
<script>
$(document).ready(function() {
// Initialize navgoco with default options
$("#mysidebar").navgoco({
caretHtml: '',
accordion: true,
openClass: 'active', // open
save: true,
cookie: {
name: 'navgoco',
expires: false,
path: '/'
},
slide: {
duration: 400,
easing: 'swing'
}
});
$("#collapseAll").click(function(e) {
e.preventDefault();
$("#mysidebar").navgoco('toggle', false);
});
$("#expandAll").click(function(e) {
e.preventDefault();
$("#mysidebar").navgoco('toggle', true);
});
});
</script>
<ul id="mysidebar" class="nav">
<span class="siteTagline">Quarks</span>
<span class="versionTagline">Version 0.3.0</span>
<li><a href="#">Overview</a>
<ul>
<li><a href="../docs/quarks_index.html">Introduction</a></li>
<li><a href="../docs/faq.html">FAQ</a></li>
</ul>
<li><a href="#">Get Started</a>
<ul>
<li><a href="../docs/quarks-getting-started.html">Getting started guide</a></li>
<li><a href="../docs/common-quarks-operations.html">Common operations</a></li>
</ul>
<li><a href="#">Quarks Cookbook</a>
<ul>
<li><a href="../recipes/recipe_hello_quarks.html">Hello Quarks!</a></li>
<li><a href="../recipes/recipe_source_function.html">Writing a source function</a></li>
<li><a href="../recipes/recipe_value_out_of_range.html">Detecting a sensor value out of expected range</a></li>
<li><a href="../recipes/recipe_different_processing_against_stream.html">Applying different processing against a single stream</a></li>
<li><a href="../recipes/recipe_combining_streams_processing_results.html">Splitting a stream to apply different processing and combining the results into a single stream</a></li>
<li><a href="../recipes/recipe_external_filter_range.html">Using an external configuration file for filter ranges</a></li>
<li><a href="../recipes/recipe_adaptable_filter_range.html">Changing a filter's range</a></li>
<li><a href="../recipes/recipe_adaptable_polling_source.html">Changing a polled source stream's period</a></li>
<li><a href="../recipes/recipe_adaptable_deadtime_filter.html">Using an adaptable deadtime filter</a></li>
<li class="active"><a href="../recipes/recipe_dynamic_analytic_control.html">Dynamically enabling analytic flows</a></li>
<li><a href="../recipes/recipe_parallel_analytics.html">How can I run analytics on several tuples in parallel?</a></li>
<li><a href="../recipes/recipe_concurrent_analytics.html">How can I run several analytics on a tuple concurrently?</a></li>
</ul>
<li><a href="#">Sample Programs</a>
<ul>
<li><a href="../docs/samples.html">Samples</a></li>
<li><a href="../docs/quickstart.html">Quickstart IBM Watson IoT Platform</a></li>
</ul>
<li><a href="#">Using the Console</a>
<ul>
<li><a href="../docs/console.html">Using the console</a></li>
</ul>
<li><a href="#">Get Involved</a>
<ul>
<li><a href="../docs/community.html">How to participate</a></li>
<li><a href="../docs/committers.html">Committers</a></li>
</ul>
<!-- if you aren't using the accordion, uncomment this block:
<p class="external">
<a href="#" id="collapseAll">Collapse All</a> | <a href="#" id="expandAll">Expand All</a>
</p>
-->
<br/>
</li>
</ul>
<div class="row">
<div class="col-md-12">
<!-- this handles the automatic toc. use ## for subheads to auto-generate the on-page minitoc. if you use html tags, you must supply an ID for the heading element in order for it to appear in the minitoc. -->
<script>
$( document ).ready(function() {
// Handler for .ready() called.
$('#toc').toc({ minimumHeaders: 0, listType: 'ul', showSpeed: 0, headers: 'h2,h3,h4' });
/* this offset helps account for the space taken up by the floating toolbar. */
$('#toc').on('click', 'a', function() {
var target = $(this.getAttribute('href'))
, scroll_target = target.offset().top
$(window).scrollTop(scroll_target - 10);
return false
})
});
</script>
<div id="toc"></div>
</div>
</div>
</div>
<!-- this highlights the active parent class in the navgoco sidebar. this is critical so that the parent expands when you're viewing a page. This must appear below the sidebar code above. Otherwise, if placed inside customscripts.js, the script runs before the sidebar code runs and the class never gets inserted.-->
<script>$("li.active").parents('li').toggleClass("active");</script>
<!-- Content Column -->
<div class="col-md-9">
<div class="post-header">
<h1 class="post-title-main">Dynamically Enabling Analytic Flows</h1>
</div>
<div class="post-content">
<!-- this handles the automatic toc. use ## for subheads to auto-generate the on-page minitoc. if you use html tags, you must supply an ID for the heading element in order for it to appear in the minitoc. -->
<script>
$( document ).ready(function() {
// Handler for .ready() called.
$('#toc').toc({ minimumHeaders: 0, listType: 'ul', showSpeed: 0, headers: 'h2,h3,h4' });
/* this offset helps account for the space taken up by the floating toolbar. */
$('#toc').on('click', 'a', function() {
var target = $(this.getAttribute('href'))
, scroll_target = target.offset().top
$(window).scrollTop(scroll_target - 10);
return false
})
});
</script>
<div id="toc"></div>
<a target="_blank" href="https://github.com/apache/incubator-quarks-website/blob/master/site/recipes/recipe_dynamic_analytic_control.md" class="btn btn-default githubEditButton" role="button"><i class="fa fa-github fa-lg"></i> Edit me</a>
<p>This recipe addresses the question: How can I dynamically enable or disable entire portions of my application&#39;s analytics?</p>
<p>Imagine a topology that has a variety of analytics that it can perform. Each analytic flow comes with certain costs in terms of demands on the CPU or memory and implications for power consumption. Hence an application may wish to dynamically control whether or not an analytic flow is currently enabled.</p>
<h2 id="valve">Valve</h2>
<p>A <code>quarks.topology.plumbing.Valve</code> is a simple construct that can be inserted in stream flows to dynamically enable or disable downstream processing. A valve is either open or closed. When used as a <code>Predicate</code> to <code>TStream.filter()</code>, <code>filter</code> passes tuples only when the valve is open. Hence downstream processing is enabled when the valve is open and effectively disabled when the valve is closed.</p>
<p>For example, consider a a topology consisting of 3 analytic processing flows that want to be dynamically enabled or disabled:</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="n">Valve</span><span class="o">&lt;</span><span class="n">Readings</span><span class="o">&gt;</span> <span class="n">flow1Valve</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Valve</span><span class="o">&lt;&gt;();</span> <span class="c1">// default is open</span>
<span class="n">Valve</span><span class="o">&lt;</span><span class="n">Readings</span><span class="o">&gt;</span> <span class="n">flow2Valve</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Valve</span><span class="o">&lt;&gt;(</span><span class="kc">false</span><span class="o">);</span> <span class="c1">// closed</span>
<span class="n">Valve</span><span class="o">&lt;</span><span class="n">Readings</span><span class="o">&gt;</span> <span class="n">flow3Valve</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Valve</span><span class="o">&lt;&gt;(</span><span class="kc">false</span><span class="o">);</span>
<span class="n">TStream</span><span class="o">&lt;</span><span class="n">Readings</span><span class="o">&gt;</span> <span class="n">readings</span> <span class="o">=</span> <span class="n">topology</span><span class="o">.</span><span class="na">poll</span><span class="o">(</span><span class="n">mySensor</span><span class="o">,</span> <span class="mi">1</span><span class="o">,</span> <span class="n">TimeUnit</span><span class="o">.</span><span class="na">SECONDS</span><span class="o">);</span>
<span class="n">addAnalyticFlow1</span><span class="o">(</span><span class="n">readings</span><span class="o">.</span><span class="na">filter</span><span class="o">(</span><span class="n">flow1Valve</span><span class="o">));</span>
<span class="n">addAnalyticFlow2</span><span class="o">(</span><span class="n">readings</span><span class="o">.</span><span class="na">filter</span><span class="o">(</span><span class="n">flow2Valve</span><span class="o">));</span>
<span class="n">addAnalyticFlow3</span><span class="o">(</span><span class="n">readings</span><span class="o">.</span><span class="na">filter</span><span class="o">(</span><span class="n">flow3Valve</span><span class="o">));</span>
</code></pre></div>
<p>Elsewhere in the application, perhaps as a result of processing some device command from an external service such as when using an <code>IotProvider</code> or <code>IotDevice</code>, valves may be opened and closed dynamically to achieve the desired effects. For example:</p>
<div class="highlight"><pre><code class="language-java" data-lang="java"><span class="n">TStream</span><span class="o">&lt;</span><span class="n">JsonObject</span><span class="o">&gt;</span> <span class="n">cmds</span> <span class="o">=</span> <span class="n">simulatedValveCommands</span><span class="o">(</span><span class="n">topology</span><span class="o">);</span>
<span class="n">cmds</span><span class="o">.</span><span class="na">sink</span><span class="o">(</span><span class="n">json</span> <span class="o">-&gt;</span> <span class="o">{</span>
<span class="n">String</span> <span class="n">valveId</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="na">getPrimitive</span><span class="o">(</span><span class="s">"valve"</span><span class="o">).</span><span class="na">getAsString</span><span class="o">();</span>
<span class="kt">boolean</span> <span class="n">isOpen</span> <span class="o">=</span> <span class="n">json</span><span class="o">.</span><span class="na">getPrimitive</span><span class="o">(</span><span class="s">"isOpen"</span><span class="o">).</span><span class="na">getAsBoolean</span><span class="o">();</span>
<span class="k">switch</span><span class="o">(</span><span class="n">valveId</span><span class="o">)</span> <span class="o">{</span>
<span class="k">case</span> <span class="s">"flow1"</span><span class="o">:</span> <span class="n">flow1Valve</span><span class="o">.</span><span class="na">setOpen</span><span class="o">(</span><span class="n">isOpen</span><span class="o">);</span> <span class="k">break</span><span class="o">;</span>
<span class="k">case</span> <span class="s">"flow2"</span><span class="o">:</span> <span class="n">flow2Valve</span><span class="o">.</span><span class="na">setOpen</span><span class="o">(</span><span class="n">isOpen</span><span class="o">);</span> <span class="k">break</span><span class="o">;</span>
<span class="k">case</span> <span class="s">"flow3"</span><span class="o">:</span> <span class="n">flow3Valve</span><span class="o">.</span><span class="na">setOpen</span><span class="o">(</span><span class="n">isOpen</span><span class="o">);</span> <span class="k">break</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">});</span>
</code></pre></div>
<h2 id="loosely-coupled-quarks-applications">Loosely coupled Quarks applications</h2>
<p>Another approach for achieving dynamic control over what analytics flows are running is to utilize loosely coupled applications.</p>
<p>In this approach, the overall application is partitioned into multiple applications (topologies). In the above example there could be four applications: one that publishes the sensor <code>readings</code> stream, and one for each of the analytic flows.</p>
<p>The separate applications can connect to each other&#39;s streams using the <code>quarks.connectors.pubsub.PublishSubscribe</code> connector.</p>
<p>Rather than having all of the analytic applications running all of the time, applications can be registered with a <code>quarks.topology.services.ApplicationService</code>. Registered applications can then be started and stopped dynamically.</p>
<p>The <code>quarks.providers.iot.IotProvider</code> is designed to facilitate this style of use.</p>
<div class="tags">
</div>
<!--
<div id="disqus_thread"></div>
<script type="text/javascript">
/* * * CONFIGURATION VARIABLES: EDIT BEFORE PASTING INTO YOUR WEBPAGE * * */
var disqus_shortname = 'idrbwjekyll'; // required: replace example with your forum shortname
/* * * DON'T EDIT BELOW THIS LINE * * */
(function() {
var dsq = document.createElement('script'); dsq.type = 'text/javascript'; dsq.async = true;
dsq.src = '//' + disqus_shortname + '.disqus.com/embed.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(dsq);
})();
</script>
<noscript>Please enable JavaScript to view the <a href="https://disqus.com/?ref_noscript">comments powered by Disqus.</a></noscript>
-->
</div>
<footer>
<div class="row">
<div class="col-lg-12 footer">
Site last
generated: May 9, 2016 <br/>
</div>
</div>
<br/>
<div class="row">
<div class="col-md-12">
<p class="small">Apache Quarks is an effort undergoing Incubation at The Apache Software
Foundation (ASF), sponsored by the Incubator. Incubation is required of all newly accepted projects
until a further review indicates that the infrastructure, communications, and decision making process
have stabilized in a manner consistent with other successful ASF projects. While incubation status is
not necessarily a reflection of the completeness or stability of the code, it does indicate that the
project has yet to be fully endorsed by the ASF.</p>
</div>
</div>
<div class="row">
<div class="col-md-12">
<p class="small">Copyright © 2016 The Apache Software Foundation. Licensed under the Apache
License, Version 2.0.
Apache, the Apache Feather logo, and the Apache Incubator project logo are trademarks of The Apache
Software Foundation. All other marks mentioned may be trademarks or registered trademarks of their
respective owners.</p>
</div>
</div>
</footer>
</div><!-- /.row -->
</div> <!-- /.container -->
</body>
</html>