Home | History | Annotate | Download | only in distributed_runtime
      1 /* Copyright 2017 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/session_mgr.h"
     17 
     18 #include "tensorflow/core/distributed_runtime/rpc/rpc_rendezvous_mgr.h"
     19 #include "tensorflow/core/distributed_runtime/worker_env.h"
     20 #include "tensorflow/core/lib/core/status_test_util.h"
     21 #include "tensorflow/core/platform/test.h"
     22 
     23 namespace tensorflow {
     24 
     25 class FakeDevice : public Device {
     26  private:
     27   explicit FakeDevice(const DeviceAttributes& device_attributes)
     28       : Device(nullptr, device_attributes) {}
     29 
     30  public:
     31   Status Sync() override { return errors::Unimplemented("FakeDevice::Sync()"); }
     32 
     33   Allocator* GetAllocator(AllocatorAttributes attr) override { return nullptr; }
     34 
     35   static std::unique_ptr<Device> MakeCPU(const string& name) {
     36     DeviceAttributes device_attributes;
     37     device_attributes.set_name(name);
     38     device_attributes.set_device_type(DeviceType("FakeCPU").type());
     39     return std::unique_ptr<Device>(new FakeDevice(device_attributes));
     40   }
     41 };
     42 
     43 class SessionMgrTest : public ::testing::Test {
     44  protected:
     45   SessionMgrTest()
     46       : device_(FakeDevice::MakeCPU(
     47             "/job:mnist/replica:0/task:0/device:fakecpu:0")),
     48         mgr_(&env_, "/job:mnist/replica:0/task:0",
     49              std::unique_ptr<WorkerCacheInterface>(), factory_),
     50         legacy_session_(mgr_.WorkerSessionForSession("novel_session_id")) {
     51     env_.local_devices = {device_.get()};
     52   }
     53 
     54   std::unique_ptr<Device> device_;
     55   WorkerEnv env_;
     56   SessionMgr::WorkerCacheFactory factory_ =
     57       [](const ServerDef& server_def, WorkerCacheInterface** worker_cache) {
     58         *worker_cache = nullptr;  // Set to null to make debugging easier.
     59         return Status::OK();
     60       };
     61   SessionMgr mgr_;
     62   std::shared_ptr<WorkerSession> legacy_session_;
     63 };
     64 
     65 TEST_F(SessionMgrTest, CreateSessionSimple) {
     66   ServerDef server_def;
     67   server_def.set_job_name("worker");
     68   server_def.set_task_index(3);
     69 
     70   string session_handle = "test_session_handle";
     71   TF_EXPECT_OK(mgr_.CreateSession(session_handle, server_def, true));
     72   auto session = mgr_.WorkerSessionForSession(session_handle);
     73   EXPECT_NE(nullptr, session) << "Session for " << session_handle << "was null";
     74   EXPECT_NE(mgr_.LegacySession(), session);
     75   TF_EXPECT_OK(mgr_.DeleteSession(session_handle));
     76 }
     77 
     78 TEST_F(SessionMgrTest, CreateSessionIsolateSessionState) {
     79   ServerDef server_def;
     80   server_def.set_job_name("worker");
     81   server_def.set_task_index(3);
     82 
     83   TF_EXPECT_OK(mgr_.CreateSession("handle_1", server_def, false));
     84   auto session_1 = mgr_.WorkerSessionForSession("handle_1");
     85   std::vector<Device*> devices_1 = session_1->device_mgr->ListDevices();
     86   EXPECT_EQ(1, devices_1.size());
     87 
     88   TF_EXPECT_OK(mgr_.CreateSession("handle_2", server_def, false));
     89   auto session_2 = mgr_.WorkerSessionForSession("handle_2");
     90   std::vector<Device*> devices_2 = session_2->device_mgr->ListDevices();
     91   EXPECT_EQ(1, devices_2.size());
     92 
     93   TF_EXPECT_OK(mgr_.CreateSession("handle_3", server_def, true));
     94   auto session_3 = mgr_.WorkerSessionForSession("handle_3");
     95   std::vector<Device*> devices_3 = session_3->device_mgr->ListDevices();
     96   EXPECT_EQ(1, devices_3.size());
     97 
     98   TF_EXPECT_OK(mgr_.CreateSession("handle_4", server_def, true));
     99   auto session_4 = mgr_.WorkerSessionForSession("handle_4");
    100   std::vector<Device*> devices_4 = session_4->device_mgr->ListDevices();
    101   EXPECT_EQ(1, devices_4.size());
    102 
    103   EXPECT_EQ(devices_1[0]->resource_manager(), devices_2[0]->resource_manager());
    104   EXPECT_NE(devices_1[0]->resource_manager(), devices_3[0]->resource_manager());
    105   EXPECT_NE(devices_1[0]->resource_manager(), devices_4[0]->resource_manager());
    106   EXPECT_NE(devices_3[0]->resource_manager(), devices_4[0]->resource_manager());
    107 }
    108 
    109 TEST_F(SessionMgrTest, LegacySession) {
    110   ServerDef server_def;
    111   string session_handle = "";
    112   auto session = mgr_.WorkerSessionForSession(session_handle);
    113   EXPECT_EQ(mgr_.LegacySession(), session);
    114 
    115   TF_EXPECT_OK(mgr_.DeleteSession(session_handle));
    116 }
    117 
    118 TEST_F(SessionMgrTest, WorkerNameFromServerDef) {
    119   ServerDef server_def;
    120   server_def.set_job_name("worker");
    121   server_def.set_task_index(3);
    122   string worker_name = SessionMgr::WorkerNameFromServerDef(server_def);
    123   EXPECT_EQ("/job:worker/replica:0/task:3", worker_name);
    124 }
    125 
    126 TEST_F(SessionMgrTest, DeleteLegacySession) {
    127   TF_EXPECT_OK(mgr_.DeleteSession("legacy_session"));
    128 }
    129 
    130 }  // namespace tensorflow
    131