Exemple #1
0
void StoreListener::sendBreakBranch(const idgs::actor::ActorMessagePtr& msg, const uint32_t& destMemberId) {
  ClusterFramework& cluster = singleton<ClusterFramework>::getInstance();
  auto localMemberId = cluster.getMemberManager()->getLocalMemberId();

  ActorMessagePtr message = msg->createRouteMessage(destMemberId, LISTENER_MANAGER);

  shared_ptr<StoreListenerInfo> listener = make_shared<StoreListenerInfo>();
  listener->set_listener_index(listenerIndex);
  message->setAttachment(STORE_ATTACH_LISTENER, listener);

  idgs::actor::sendMessage(message);
}
void AggregatorActor::multicast(const ActorMessagePtr& msg, const string& actorId, const std::string& opName) {
  requestCount = 0;
  responseCount = 0;

  auto memberMgr = idgs_application()->getMemberManager();
  auto& members = memberMgr->getMemberTable();
  auto it = members.begin();
  for (; it != members.end(); ++ it) {
    if (it->isLocalStore() && (it->getState() == idgs::pb::MS_ACTIVE || it->getState() == idgs::pb::MS_PREPARED)) {
      ActorMessagePtr multMsg = msg->createRouteMessage(it->getId(), actorId);
      multMsg->setOperationName(opName);
      multMsg->setSourceActorId(this->getActorId());
      multMsg->setSourceMemberId(memberMgr->getLocalMemberId());
      ::idgs::actor::postMessage(multMsg);
      ++ requestCount;
    }
  }
}
RddResultCode ReduceByKeyTransformer::parseOperations(const ActorMessagePtr& msg, const BaseRddPartition* input,
    RddPartition* output, vector<ReduceOperation*>& operations) {
  ReduceByKeyRequest reduce_request;
  if (!msg->parseAttachment(TRANSFORMER_PARAM, &reduce_request)) {
    LOG(ERROR)<< "Not exists attachment: ReduceValueRequest";
    return RRC_INVALID_TRANSFORMER_PARAM;
  }
  std::map<std::string, ReduceByKeyField> reduce_value_map; /// Key: field_alias, Value: ReduceValue
  for (auto it = reduce_request.fields().begin(); it != reduce_request.fields().end(); ++it) {
    reduce_value_map.insert(std::pair<std::string, ReduceByKeyField>(it->field_alias(), *it));
  }

  auto outMsg = output->getOutMessage(0);
  auto& keyFields = outMsg.key.getFields();
  auto& keyExprs = outMsg.key.getExpressions();
  for (int32_t i = 0; i < keyFields.size(); ++i) {
    ReduceOperation* op = NULL;
    std::string alias = keyFields[i]->name();
    auto reduce_it = reduce_value_map.find(alias);
    if (reduce_it != reduce_value_map.end()) { /// reduce value field
      op = ReduceOperationFactory::create(reduce_it->second.type()); /// which reduce type, e.g. count, sum, ... etc
      op->is_distinct = reduce_it->second.distinct(); /// whether including distinct, e.g. count(distinct), avg(distinct)
    } else { /// not reduce value field
      op = ReduceOperationFactory::create();
    }

    op->expr = keyExprs[i];
    op->descriptor = keyFields[i];
    op->key = true;

    operations.push_back(op);
  }

  auto& valueFields = outMsg.value.getFields();
  auto& valueExprs = outMsg.value.getExpressions();
  for (int32_t i = 0; i < valueFields.size(); ++i) {
    ReduceOperation* op = NULL;
    std::string alias = valueFields[i]->name();
    auto reduce_it = reduce_value_map.find(alias);
    if (reduce_it != reduce_value_map.end()) { /// reduce value field
      op = ReduceOperationFactory::create(reduce_it->second.type()); /// which reduce type, e.g. count, sum, ... etc
      op->is_distinct = reduce_it->second.distinct(); /// whether including distinct, e.g. count(distinct), avg(distinct)
    } else { /// not reduce value field
      op = ReduceOperationFactory::create();
    }

    op->expr = valueExprs[i];
    op->descriptor = valueFields[i];
    op->key = false;

    operations.push_back(op);
  }

  return RRC_SUCCESS;
}
void StoreDelegateRddActor::handleRddCreate(const ActorMessagePtr& msg) {
  DVLOG(2) << "StoreDelegateRddActor : handle create store delegate.";
  rawMsg = msg;
  CreateDelegateRddRequest* request = dynamic_cast<CreateDelegateRddRequest*>(msg->getPayload().get());

  idgs::store::MetadataHelper::loadStoreMetadata(request->store_name(), metadata.get());

  ClusterFramework& cluster = ::idgs::util::singleton<ClusterFramework>::getInstance();
  for (int32_t partition = 0; partition < partitionSize; ++partition) {
    int32_t memberId = cluster.getPartitionManager()->getPartition(partition)->getPrimaryMemberId();
    shared_ptr<CreateDelegatePartitionRequest> payload(new CreateDelegatePartitionRequest);
    payload->set_store_name(request->store_name());
    payload->set_partition(partition);
    payload->set_rdd_name(getRddName());

    ActorMessagePtr reqMsg = createActorMessage();
    reqMsg->setOperationName(CREATE_DELEGATE_PARTITION);
    reqMsg->setDestActorId(RDD_SERVICE_ACTOR);
    reqMsg->setDestMemberId(memberId);
    reqMsg->setPayload(payload);

    DVLOG(3) << "RDD \"" << getRddName() << "\" sending create RDD partition to member " << memberId;

    ::idgs::actor::postMessage(reqMsg);
  }
}
void DataAggregatorActor::handleDeleteResponse(const ActorMessagePtr& msg) {
  DVLOG_FIRST_N(2, 20) << "receive store remove response of member " << getActorId();
  auto response = dynamic_cast<idgs::store::pb::DeleteResponse*>(msg->getPayload().get());

  // error
  if (response->result_code() != idgs::store::pb::SRC_SUCCESS) {
    LOG(ERROR)<< "error in remove data to member " << msg->getSourceActorId();
    resultCode = response->result_code();
  }

  ++ responseCount;
  DVLOG_FIRST_N(2, 20) << "member " << msg->getSourceMemberId() << " response, left " << requestCount - responseCount << " members.";
  if (requestCount == responseCount) {
    DVLOG_FIRST_N(2, 20) << "all member response, send response to client.";

    auto globalResponse = make_shared<idgs::store::pb::DeleteResponse>();
    globalResponse->set_result_code(resultCode);
    sendResponse(OP_DELETE_RESPONSE, globalResponse);

    terminate();
  }
}
void DataAggregatorActor::handleInsertResponse(const ActorMessagePtr& msg) {
  DVLOG_FIRST_N(2, 20) << "receive store insert response of member " << msg->getSourceMemberId();
  idgs::store::pb::InsertResponse* response = dynamic_cast<idgs::store::pb::InsertResponse*>(msg->getPayload().get());

  // error
  if (response->result_code() != idgs::store::pb::SRC_SUCCESS) {
    resultCode = response->result_code();
    LOG(ERROR)<< "error in insert data to member " << msg->getSourceActorId() << " error code : " << resultCode;
  }

  // collect response from all members and calculate size and then response to client.
  ++ responseCount;
  DVLOG_FIRST_N(2, 20) << "member " << msg->getSourceMemberId() << " responsed, left " << requestCount - responseCount << " members.";
  if (requestCount == responseCount) {
    DVLOG_FIRST_N(2, 20) << "all member responsed, send response to client";
    shared_ptr<idgs::store::pb::InsertResponse> globalResponse = make_shared<idgs::store::pb::InsertResponse>();
    globalResponse->set_result_code(resultCode);
    sendResponse(OP_INSERT_RESPONSE, globalResponse);

    terminate();
  }
}
void AggregatorActor::sendResponse(const std::string& opName, const shared_ptr<Message>& response) {
  ActorMessagePtr am = clientMsg->createResponse();
  am->setOperationName(opName);
  am->setPayload(response);
  idgs::actor::sendMessage(am);
}