static ActivationId CreateActivationId(GrainId grainId) { var a = (ulong)grainId.Type.GetHashCode(); var b = (ulong)grainId.Key.GetHashCode(); var key = UniqueKey.NewKey(a, b, UniqueKey.Category.KeyExtGrain, typeData: 0, keyExt: grainId.ToString()); return(new ActivationId(key)); }
// No need to encode SiloAddress in the activation address for system target. // System targets have unique grain ids and addressed to a concrete silo, so in fact we don't need ActivationId at all for System targets. // Need to remove it all together. For now, just use grain id as activation id. public static ActivationId GetDeterministic(GrainId grain) { return(interner.FindOrCreate(grain, grainId => { var a = (ulong)grainId.Type.GetHashCode(); var b = (ulong)grainId.Key.GetHashCode(); var key = UniqueKey.NewKey(a, b, UniqueKey.Category.KeyExtGrain, typeData: 0, keyExt: grainId.ToString()); return new ActivationId(key); })); }
private ActivationAddress GetClientActivationAddress(GrainId clientId) { // Need to pick a unique deterministic ActivationId for this client. // We store it in the grain directory and there for every GrainId we use ActivationId as a key // so every GW needs to behave as a different "activation" with a different ActivationId (its not enough that they have different SiloAddress) string stringToHash = clientId.ToParsableString() + myAddress.Endpoint + myAddress.Generation.ToString(System.Globalization.CultureInfo.InvariantCulture); Guid hash = Utils.CalculateGuidHash(stringToHash); UniqueKey key = UniqueKey.NewKey(hash); return(ActivationAddress.GetAddress(myAddress, clientId, ActivationId.GetActivationId(key))); }
public static ActivationId GetClientGWActivation(GrainId grain, SiloAddress location) { if (!grain.IsClient) { throw new ArgumentException("ClientGW activation IDs can only be created for client grains"); } // Construct a unique and deterministic ActivationId based on GrainId and SiloAddress. string stringToHash = grain.ToParsableString() + location.Endpoint + location.Generation.ToString(System.Globalization.CultureInfo.InvariantCulture); Guid hash = Utils.CalculateGuidHash(stringToHash); UniqueKey key = UniqueKey.NewKey(hash); return(FindOrCreate(key)); }
private static LegacyGrainId FromGrainIdInternal(GrainId id) { var typeSpan = id.Type.AsSpan(); if (typeSpan.Length != GrainTypePrefix.LegacyGrainPrefix.Length + 16) { return(null); } if (!typeSpan.StartsWith(GrainTypePrefix.LegacyGrainPrefixBytes.Span)) { return(null); } typeSpan = typeSpan.Slice(GrainTypePrefix.LegacyGrainPrefix.Length, 16); if (!Utf8Parser.TryParse(typeSpan, out ulong typeCodeData, out var len, 'X') || len < 16) { return(null); } string keyExt = null; var keySpan = id.Key.Value.Span; if (keySpan.Length < 32) { return(null); } if (!Utf8Parser.TryParse(keySpan.Slice(0, 16), out ulong n0, out len, 'X') || len < 16) { return(null); } if (!Utf8Parser.TryParse(keySpan.Slice(16, 16), out ulong n1, out len, 'X') || len < 16) { return(null); } if (keySpan.Length > 32) { if (keySpan[32] != '+') { return(null); } keyExt = keySpan.Slice(33).GetUtf8String(); } return(FindOrCreateGrainId(UniqueKey.NewKey(n0, n1, typeCodeData, keyExt))); }
internal static GrainType GetGrainType(long typeCode, bool isKeyExt) { LegacyGrainId grainId; if (isKeyExt) { var dummyKey = UniqueKey.NewKey(0, UniqueKey.Category.KeyExtGrain, typeCode, "keyext"); grainId = new LegacyGrainId(dummyKey); } else { var dummyKey = UniqueKey.NewKey(0, UniqueKey.Category.Grain, typeCode); grainId = new LegacyGrainId(dummyKey); } return(grainId.GetGrainType()); }
// For testing only. internal static LegacyGrainId GetGrainIdForTesting(Guid guid) { return(FindOrCreateGrainId(UniqueKey.NewKey(guid, UniqueKey.Category.None))); }
internal static LegacyGrainId NewClientId(Guid id) { return(FindOrCreateGrainId(UniqueKey.NewKey(id, UniqueKey.Category.Client, 0))); }
public static LegacyGrainId NewId() { return(FindOrCreateGrainId(UniqueKey.NewKey(Guid.NewGuid(), UniqueKey.Category.Grain))); }
private static unsafe LegacyGrainId FromGrainIdInternal(GrainId id) { var typeSpan = id.Type.AsSpan(); int prefixLength; if (typeSpan.StartsWith(GrainTypePrefix.LegacyGrainPrefixBytes.Span)) { prefixLength = GrainTypePrefix.LegacyGrainPrefixBytes.Length; } else { return(null); } ulong typeCodeData; var typeCodeSlice = typeSpan.Slice(prefixLength); fixed(byte *typeCodeBytes = typeCodeSlice) { // TODO: noalloc var typeCodeString = Encoding.UTF8.GetString(typeCodeBytes, 16); if (!ulong.TryParse(typeCodeString, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out typeCodeData)) { return(null); } } ulong n0, n1; string keyExt; var keySpan = id.Key.Value.Span; fixed(byte *idBytes = keySpan) { const int fieldLength = 16; // TODO: noalloc var n0String = Encoding.UTF8.GetString(idBytes, fieldLength); if (!ulong.TryParse(n0String, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out n0)) { return(null); } // TODO: noalloc var n1String = Encoding.UTF8.GetString(idBytes + fieldLength, fieldLength); if (!ulong.TryParse(n1String, NumberStyles.HexNumber, CultureInfo.InvariantCulture, out n1)) { return(null); } const int keySpanPrefixLength = fieldLength + fieldLength + 1; if (keySpan.Length > keySpanPrefixLength && idBytes[keySpanPrefixLength - 1] == (byte)'+') { // Take every byte after the '+' and interpret it as UTF8 keyExt = Encoding.UTF8.GetString(idBytes + keySpanPrefixLength, keySpan.Length - keySpanPrefixLength); } else { keyExt = default; } } return(new LegacyGrainId(UniqueKey.NewKey(n0, n1, typeCodeData, keyExt))); }
internal static GrainType GetGrainType(long typeCode, bool isKeyExt) { return(GetGrainType(isKeyExt ? UniqueKey.NewKey(0, UniqueKey.Category.KeyExtGrain, typeCode, "keyext") : UniqueKey.NewKey(0, UniqueKey.Category.Grain, typeCode))); }
internal static LegacyGrainId GetGrainId(long typeCode, string primaryKey) { return(FindOrCreateGrainId(UniqueKey.NewKey(0L, UniqueKey.Category.KeyExtGrain, typeCode, primaryKey))); }
public static GrainId NewClientAddressableGrainId() { return(FindOrCreateGrainId(UniqueKey.NewKey(Guid.NewGuid(), UniqueKey.Category.ClientAddressableObject))); }
public static GrainId NewClientGrainId() { return(FindOrCreateGrainId(UniqueKey.NewKey(Guid.NewGuid(), UniqueKey.Category.ClientGrain))); }
public static ActivationId NewId() { return(GetActivationId(UniqueKey.NewKey())); }
internal static LegacyGrainId GetGrainId(long typeCode, Guid primaryKey, string keyExt = null) { return(FindOrCreateGrainId(UniqueKey.NewKey(primaryKey, keyExt == null ? UniqueKey.Category.Grain : UniqueKey.Category.KeyExtGrain, typeCode, keyExt))); }
internal static GrainId NewClientId(Guid id, string clusterId = null) { return(FindOrCreateGrainId(UniqueKey.NewKey(id, clusterId == null ? UniqueKey.Category.Client : UniqueKey.Category.GeoClient, 0, clusterId))); }
public static ActivationId NewId() { return(FindOrCreate(UniqueKey.NewKey())); }
internal static GrainId GetSystemGrainId(Guid guid) { return(FindOrCreateGrainId(UniqueKey.NewKey(guid, UniqueKey.Category.SystemGrain))); }