| 78 | } |
| 79 | |
| 80 | void TFApplyGraph::onTrigger(const std::shared_ptr<core::ProcessContext> &context, |
| 81 | const std::shared_ptr<core::ProcessSession> &session) { |
| 82 | auto flow_file = session->get(); |
| 83 | |
| 84 | if (!flow_file) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | try { |
| 89 | // Read graph |
| 90 | std::string tf_type; |
| 91 | flow_file->getAttribute("tf.type", tf_type); |
| 92 | |
| 93 | std::shared_ptr<tensorflow::GraphDef> graph_def; |
| 94 | uint32_t graph_version; |
| 95 | |
| 96 | { |
| 97 | std::lock_guard<std::mutex> guard(graph_def_mtx_); |
| 98 | |
| 99 | if ("graph" == tf_type) { |
| 100 | logger_->log_info("Reading new graph def"); |
| 101 | graph_def_ = std::make_shared<tensorflow::GraphDef>(); |
| 102 | GraphReadCallback graph_cb(graph_def_); |
| 103 | session->read(flow_file, &graph_cb); |
| 104 | graph_version_++; |
| 105 | logger_->log_info("Read graph version: %i", graph_version_); |
| 106 | session->remove(flow_file); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | graph_version = graph_version_; |
| 111 | graph_def = graph_def_; |
| 112 | } |
| 113 | |
| 114 | if (!graph_def) { |
| 115 | logger_->log_error("Cannot process input because no graph has been defined"); |
| 116 | session->transfer(flow_file, Retry); |
| 117 | return; |
| 118 | } |
| 119 | |
| 120 | // Use an existing context, if one is available |
| 121 | std::shared_ptr<TFContext> ctx; |
| 122 | |
| 123 | if (tf_context_q_.try_dequeue(ctx)) { |
| 124 | logger_->log_debug("Using available TensorFlow context"); |
| 125 | |
| 126 | if (ctx->graph_version != graph_version) { |
| 127 | logger_->log_info("Allowing session with stale graph to expire"); |
| 128 | ctx = nullptr; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | if (!ctx) { |
| 133 | logger_->log_info("Creating new TensorFlow context"); |
| 134 | tensorflow::SessionOptions options; |
| 135 | ctx = std::make_shared<TFContext>(); |
| 136 | ctx->tf_session.reset(tensorflow::NewSession(options)); |
| 137 | ctx->graph_version = graph_version; |
nothing calls this directly
no test coverage detected