title: Flink internal threading model design?
body: <p>Is this written up somewhere?</p>
  
  <p>Having some difficulty understanding the intended design.
  Want to understand the assumptions I should make about when certain functions will be called (for example JDBCOutputFormat  open vs flush vs writeRecord vs close) and whether this will always be from the same thread or some other thread, or whether they might be called concurrently, in order to verify the correctness of the code. </p>
  
  <p>What guarantees are there?</p>
  
  <p>Does a certain reference need a volatile or even a synchronisation or not.</p>
  
  <p>What's the design for threading?</p>
  
  <p>If the intended design is not written down then we have to infer it from the code and we might come to different conclusions and thus bugs and leaks.</p>
  
  <p>It's really hard writing good MT code and a strong design is necessary to provide a framework for the code.</p>
  
  <p>Some info here <a href="https://flink.apache.org/contributing/code-style-and-quality-common.html#concurrency-and-threading" rel="nofollow noreferrer">https://flink.apache.org/contributing/code-style-and-quality-common.html#concurrency-and-threading</a> , but this isn't a design and doesn't say how it's meant to work.</p>
  
  <p>==</p>
  
  <p>Another, example. When I see code like this ...</p>
  
  <pre><code>FileOutputFormat
  
     public void close() throws IOException {
          final FSDataOutputStream s = this.stream;
          if (s != null) {
              this.stream = null;
              s.close();
          }
      }
  </code></pre>
  
  <p>My feeling is that someone wasn't sure either what the right approach was.
  I can only guess that the author was concerned that someone else was going to call the function concurrently, or mess with the class state by some other means. And, if that were true then would this code even be MT safe - who knows? Ought there be a volatile in there or a plain old sync or perhaps none of the caution is needed at all (framework guarantees preventing the need).</p>
  
  <p>I see a lot of code in the project that is similarly uncertain and inconsistent.</p>
  
  <p>Hmm?</p>