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 | #include "tensorflow/core/distributed_runtime/server_lib.h" |
17 | |
18 | #include <unordered_map> |
19 | |
20 | #include "tensorflow/core/lib/core/errors.h" |
21 | #include "tensorflow/core/platform/mutex.h" |
22 | |
23 | namespace tensorflow { |
24 | |
25 | namespace { |
26 | mutex* get_server_factory_lock() { |
27 | static mutex server_factory_lock(LINKER_INITIALIZED); |
28 | return &server_factory_lock; |
29 | } |
30 | |
31 | typedef std::unordered_map<string, ServerFactory*> ServerFactories; |
32 | ServerFactories* server_factories() { |
33 | static ServerFactories* factories = new ServerFactories; |
34 | return factories; |
35 | } |
36 | } // namespace |
37 | |
38 | /* static */ |
39 | void ServerFactory::Register(const string& server_type, |
40 | ServerFactory* factory) { |
41 | mutex_lock l(*get_server_factory_lock()); |
42 | if (!server_factories()->insert({server_type, factory}).second) { |
43 | LOG(ERROR) << "Two server factories are being registered under " |
44 | << server_type; |
45 | } |
46 | } |
47 | |
48 | /* static */ |
49 | Status ServerFactory::GetFactory(const ServerDef& server_def, |
50 | ServerFactory** out_factory) { |
51 | mutex_lock l(*get_server_factory_lock()); |
52 | for (const auto& server_factory : *server_factories()) { |
53 | if (server_factory.second->AcceptsOptions(server_def)) { |
54 | *out_factory = server_factory.second; |
55 | return OkStatus(); |
56 | } |
57 | } |
58 | |
59 | std::vector<string> server_names; |
60 | for (const auto& server_factory : *server_factories()) { |
61 | server_names.push_back(server_factory.first); |
62 | } |
63 | |
64 | return errors::NotFound( |
65 | "No server factory registered for the given ServerDef: " , |
66 | server_def.DebugString(), "\nThe available server factories are: [ " , |
67 | absl::StrJoin(server_names, ", " ), " ]" ); |
68 | } |
69 | |
70 | // Creates a server based on the given `server_def`, and stores it in |
71 | // `*out_server`. Returns OK on success, otherwise returns an error. |
72 | Status NewServer(const ServerDef& server_def, |
73 | std::unique_ptr<ServerInterface>* out_server) { |
74 | ServerFactory* factory; |
75 | TF_RETURN_IF_ERROR(ServerFactory::GetFactory(server_def, &factory)); |
76 | return factory->NewServer(server_def, ServerFactory::Options(), out_server); |
77 | } |
78 | |
79 | // Creates a server based on the given `server_def`, and stores it in |
80 | // `*out_server`. Returns OK on success, otherwise returns an error. |
81 | Status NewServerWithOptions(const ServerDef& server_def, |
82 | const ServerFactory::Options& options, |
83 | std::unique_ptr<ServerInterface>* out_server) { |
84 | ServerFactory* factory; |
85 | TF_RETURN_IF_ERROR(ServerFactory::GetFactory(server_def, &factory)); |
86 | return factory->NewServer(server_def, options, out_server); |
87 | } |
88 | |
89 | } // namespace tensorflow |
90 | |