在Microsoft Orleans中检查GUID是否存在颗粒

4im*_*ble 4 orleans

如何检查特定Id的颗粒是否已存在?

考虑到下面将创建一个新的播放器粒子与我传入的Id,GetGrain()如果它不存在,我不知道如何检查是否已经存在.

public async Task<Guid> Create(Guid playerGuid)
    {
        var player = GrainClient.GrainFactory.GetGrain<IPlayerGrain>(playerGuid);
        var gameGuid = await player.CreateGame();

        return gameGuid;
    }
Run Code Online (Sandbox Code Playgroud)

Reu*_*ond 7

简短的回答是存储一些状态,以便谷物知道它是否先前已被激活.

奥尔良的谷物永远不会被明确地创建或销毁:它们始终可用于处理请求.因此,从技术上讲,谷物是否存在的概念并不适用于奥尔良.另一方面,我们可以问"有这个id的颗粒曾被激活过".

您可能需要检查两种情况:

  1. 谷物从未被激活过,但你期待它已经被激活了.例如:我正在调用方法IPlayerGrain,但播放器不存在.
  2. 谷物之前已被激活,但你期望它没有.例如:我正在尝试为这个玩家创建一个新游戏,但该ID已经被采用.

在下面的代码示例中,您可以看到两种情况:

  1. IPlayerGrain.CreateGame()如果之前从未创建过该玩家,则该调用将抛出异常.忽略我永远不会设置的事实,Created可以通过某种CreatePlayer(...)方法完成.
  2. IGameGrain.TryCreateGame(player)如果游戏已经创建,则调用返回false.在这种情况下,IPlayerGrain.CreateGame()将继续循环,直到找到一个尚未创建的游戏.有了Guidid,你不可能看到碰撞,但我理解要谨慎的愿望 - 只要让星星对齐并对你不利.
public interface IPlayerGrain : IGrainWithGuidKey
{
    Task<Guid> CreateGame();
}

public class PlayerState
{
    public bool Created { get; set; }
}

public class PlayerGrain : Grain<PlayerState>, IPlayerGrain
{
    public async Task<Guid> CreateGame()
    {
        if (!this.State.Created)
          throw new InvalidOperationException("Player does not exist.");

        var thisPlayer = this.AsReference<IPlayerGrain>();

        var created = false;
        var gameId = default(Guid);
        while (!created)
        {
            // Get a new, random game grain
            gameId = Guid.NewGuid();

            // Try to create a game.
            created = await this.GrainFactory.GetGrain<IGameGrain>(gameId)
                                .TryCreateGame(thisPlayer);

            // If the game was successfully created, break out and return the id.
            // Otherwise, keep looping.
        }

        return gameId;
    }
}
Run Code Online (Sandbox Code Playgroud)
public interface IGameGrain : IGrainWithGuidKey
{
    // Returns true if game was created, false otherwise.
    Task<bool> TryCreateGame(IPlayerGrain player);
}

public class GameState
{
    public IPlayerGrain Player { get; set; }
}

public class GameGrain : Grain<GameState>, IGameGrain
{
    public async Task<bool> TryCreateGame(IPlayerGrain player)
    {
        // If this grain already has a player, return false.
        if (this.State.Player != null) return false;

        // Otherwise, set the player, write it to storage, and return true.
        this.State.Player = player;
        await this.WriteStateAsync();
        return true;
    }
}
Run Code Online (Sandbox Code Playgroud)

Create您问题中的方法无需更改.