# Copyright 2020 The FedLearner Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# coding: utf-8

import logging
from concurrent import futures
import grpc
from fedlearner.common import trainer_master_service_pb2 as tm_pb
from fedlearner.common import trainer_master_service_pb2_grpc as tm_grpc
from fedlearner.common import common_pb2 as common_pb
from .trainer_master_service import TrainerMasterServer


class TrainerMaster(object):
    def __init__(self, application_id, checkpoint_path=None,
                 online_training=False):
        self._application_id = application_id
        self._online_training = online_training

    def run(self, listen_port):
        # TrainerMaster need to load data_block from store or disk at first.
        # if TrainerMaster shutdown, need to recovery from previous checkpoint.
        self._load_data()
        self._server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
        tm_grpc.add_TrainerMasterServiceServicer_to_server(
            TrainerMasterServer(self._data_block_response), self._server)
        self._server.add_insecure_port('[::]:%d' % listen_port)
        self._server.start()
        logging.info('Trainer Master Server start on port[%d].', listen_port)
        self._server.wait_for_termination()

    def _get_checkpoint(self):
        # TODO need to checkpoint the data alloc record.
        return set()

    def _alloc_data_block(self, block_id=None):
        raise NotImplementedError("This method needs to be overridden")

    def _data_block_response(self, request):
        data_block = self._alloc_data_block(block_id=request.block_id)
        response = tm_pb.DataBlockResponse()
        logging.debug("Base TrainerMaster allocated worker_%d"
                       "with block id %s", request.worker_rank,
                                           data_block.block_id)
        if data_block:
            response.status.code = common_pb.STATUS_SUCCESS
            response.status.error_message = 'success'
            response.data_block_info.data_path = \
                str(data_block.data_block_fpath)
            response.data_block_info.meta_path = ''
            response.data_block_info.block_id = str(data_block.block_id)
        elif self._online_training:
            response.status.code = common_pb.STATUS_NO_MORE_DATA
            response.status.error_message = 'please wait for datablock ready'
        else:
            response.status.code = common_pb.STATUS_DATA_FINISHED
            response.status.error_message = 'datablock finished'
        return response

    def _load_data(self):
        raise NotImplementedError("This method needs to be overridden")

@@ -2202,41 +2202,52 @@
     
-logging.debug(%22Base TrainerMaster
+if data_block:%0A            logging.debug(%22%25s
  all
@@ -2262,17 +2262,35 @@
 orker_%25d
-%22
+ with block id %25s%22,
 %0A       
@@ -2309,66 +2309,83 @@
     
-%22with block id %25s%22, request.worker_rank,%0A                 
+   self.__class__.__name__,%0A                          request.worker_rank,%0A
     
@@ -2427,39 +2427,16 @@
 ock_id)%0A
-        if data_block:%0A
         
@@ -2818,152 +2818,590 @@
     
-response.status.code = common_pb.STATUS_NO_MORE_DATA%0A            response.status.error_message = 'please wait for datablock ready'%0A        else:
+logging.debug(%22%25s allocated worker_%25d with empty data block. %22%5C%0A                          %22wait for new data block since online traning%22,%0A                          self.__class__.__name__, request.worker_rank)%0A            response.status.code = common_pb.STATUS_NO_MORE_DATA%0A            response.status.error_message = 'please wait for datablock ready'%0A        else:%0A            logging.debug(%22%25s allocated worker_%25d with empty data block. %22%5C%0A                          %22exit running since since batch traning%22,%0A                          self.__class__.__name__, request.worker_rank)
 %0A   

fix the allocate log for train master (#238)