• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2016 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_SESSION_MGR_H_
17 #define TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_SESSION_MGR_H_
18 
19 #include <functional>
20 
21 #include "tensorflow/core/distributed_runtime/worker_session.h"
22 #include "tensorflow/core/lib/core/status.h"
23 #include "tensorflow/core/platform/mutex.h"
24 #include "tensorflow/core/protobuf/tensorflow_server.pb.h"
25 #include "tensorflow/core/protobuf/worker.pb.h"
26 
27 namespace tensorflow {
28 
29 class WorkerCacheInterface;
30 struct WorkerEnv;
31 
32 // SessionMgr keeps track of information related to a given session.
33 //
34 // SessionMgr runs on the workers.
35 //
36 // SessionMgr is threadsafe.
37 class SessionMgr {
38  public:
39   typedef std::function<Status(const ServerDef&, WorkerCacheInterface**)>
40       WorkerCacheFactory;
41 
42   explicit SessionMgr(
43       WorkerEnv* worker_env, const string& default_worker_name,
44       std::unique_ptr<WorkerCacheInterface> default_worker_cache,
45       WorkerCacheFactory worker_cache_factory);
~SessionMgr()46   ~SessionMgr() {}
47 
48   // Allocates state for a new session.
49   Status CreateSession(const string& session, const ServerDef& server_def,
50                        bool isolate_session_state);
51 
52   // Locates the worker session for a given session handle
53   Status WorkerSessionForSession(const string& session_handle,
54                                  std::shared_ptr<WorkerSession>* out_session);
55   std::shared_ptr<WorkerSession> LegacySession();
56 
57   Status DeleteSession(const string& session);
58 
59   static string WorkerNameFromServerDef(const ServerDef& server_def);
60 
61   void SetLogging(bool active);
62 
63   void RetrieveLogs(tensorflow::int64 step_id, LoggingResponse* response);
64 
65   void ClearLogs();
66 
67  private:
68   WorkerEnv* const worker_env_;  // Not owned.
69 
70   // A note about destruction:
71   // We must delete graph_mgr before device_mgr, due to shared
72   // ownership of OpKernels in the executors. (The graph_mgr will
73   // free all stateless OpKernels, and pass over borrowed stateful
74   // OpKernels, which are also held in their respective devices'
75   // OpSegments.)
76   //
77   // legacy_session_ owns the worker_env_.device_mgr, and so we must ensure
78   // that sessions_'s WorkerSessions are deleted (which do not own the
79   // underlying devices, but instead own RenamedDevices) before
80   // legacy_session_ is deleted. Further, we must ensure that WorkerSession's
81   // device_mgr is deleted after WorkerSession's graph_mgr.
82 
83   std::unique_ptr<WorkerCacheInterface> default_worker_cache_;
84   std::shared_ptr<WorkerSession> legacy_session_;
85 
86   bool is_logging_active_ = false;
87 
88   const WorkerCacheFactory worker_cache_factory_;
89 
90   Status WorkerSessionForSessionLocked(
91       const string& session_handle, std::shared_ptr<WorkerSession>* out_session)
92       EXCLUSIVE_LOCKS_REQUIRED(mu_);
93 
94   mutex mu_;
95   // A map from session identifier to internal session structure.
96   std::map<string, std::shared_ptr<WorkerSession>> sessions_ GUARDED_BY(mu_);
97 };
98 
99 }  // namespace tensorflow
100 
101 #endif  // TENSORFLOW_CORE_DISTRIBUTED_RUNTIME_SESSION_MGR_H_
102