/* Copyright 2020 Alibaba Group Holding Limited. 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.
==============================================================================*/

#include "actor/graph/sync_actor.act.h"

#include "actor/graph/loader_config.h"
#include "include/config.h"

#include "actor/generated/graph/control_actor_ref.act.autogen.h"

namespace graphlearn {
namespace act {

SyncActor::SyncActor(hiactor::actor_base* exec_ctx,
                     const hiactor::byte_t* addr)
    : hiactor::actor(exec_ctx, addr, false /* stateful */),
      received_eos_number_(0) {
  control_actor_id_.reserve(GLOBAL_FLAG(ServerCount));
}

SyncActor::~SyncActor() = default;

void SyncActor::ReceiveEOS(hiactor::Integer&& source_id) {
  control_actor_id_.push_back(source_id.val);
  if (++received_eos_number_ == GLOBAL_FLAG(ServerCount)) {
    for (size_t i = 0; i < control_actor_id_.size(); ++i) {
      auto builder = hiactor::scope_builder(control_actor_id_[i]);
      auto actor_ref = builder.build_ref<ControlActor_ref>(
        LoaderConfig::control_actor_id);
      actor_ref.StopActor();
    }
  }
}

}  // namespace act
}  // namespace graphlearn
