源码阅读之Sharding实体注册

主要逻辑:

  • 检查实体参数,若无则实例化默认参数。
  • 进行实体注册

1 检查实体参数

核心代码:

override def init[M, E](entity: scaladsl.Entity[M, E]): ActorRef[E] = {
    val settings = entity.settings match {
      case None    => ClusterShardingSettings(system)
      case Some(s) => s
    }

    val extractor = (entity.messageExtractor match {
      case None    => new HashCodeMessageExtractor[M](settings.numberOfShards)
      case Some(e) => e
    }).asInstanceOf[ShardingMessageExtractor[E, M]]

    val settingsWithRole = entity.role.fold(settings)(settings.withRole)
    val settingsWithDataCenter = entity.dataCenter.fold(settingsWithRole)(settingsWithRole.withDataCenter)

    internalInit(
      entity.createBehavior,
      entity.entityProps,
      entity.typeKey,
      entity.stopMessage,
      settingsWithDataCenter,
      extractor,
      entity.allocationStrategy)
  }

核心逻辑:

  • 检查实体中是否存在设置,若无则使用默认设置。
  • 检查实体是否指定了消息提取器,若无则使用默认的消息提取器。

2 实体注册

核心代码

@InternalApi private[akka] def internalStart(
      typeName: String,
      entityProps: String => Props,
      settings: ClusterShardingSettings,
      extractEntityId: ShardRegion.ExtractEntityId,
      extractShardId: ShardRegion.ExtractShardId,
      allocationStrategy: ShardAllocationStrategy,
      handOffStopMessage: Any): ActorRef = {

    if (settings.stateStoreMode == ClusterShardingSettings.StateStoreModePersistence)
      log.warning("Cluster Sharding has been set to use the deprecated `persistence` state store mode.")

    if (settings.shouldHostShard(cluster)) {
      regions.get(typeName) match {
        case null =>
          // it's ok to Start several time, the guardian will deduplicate concurrent requests
          implicit val timeout = system.settings.CreationTimeout
          val startMsg = Start(
            typeName,
            entityProps,
            settings,
            extractEntityId,
            extractShardId,
            allocationStrategy,
            handOffStopMessage)
          val Started(shardRegion) = Await.result(guardian ? startMsg, timeout.duration)
          regions.put(typeName, shardRegion)
          shardRegion
        case ref => ref // already started, use cached ActorRef
      }
    } else {
      log.debug("Starting Shard Region Proxy [{}] (no actors will be hosted on this node)...", typeName)

      startProxy(
        typeName,
        settings.role,
        dataCenter = None, // startProxy method must be used directly to start a proxy for another DC
        extractEntityId,
        extractShardId)
    }
  }

核心逻辑

  • 获取region Actor,若无则创建。
    创建region Actor需要的参数
  • 实体类型名称
  • 实体Props
  • 设置
  • 抽取实体Id的函数
  • 抽取Shard Id的函数
  • Sharding Location 策略
  • 停止消息的类型。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值