Ejemplo n.º 1
0
        public static SafetyCheck CreateRandomSafetyCheck(this Random random)
        {
            SafetyCheck     result;
            SafetyCheckKind kind = random.CreateRandom <SafetyCheckKind>();

            switch (kind)
            {
            case SafetyCheckKind.EnsureSeedNodeQuorum:
                result = new SeedNodeSafetyCheck(kind);
                break;

            case SafetyCheckKind.EnsurePartitionQuorum:
            case SafetyCheckKind.WaitForInBuildReplica:
            case SafetyCheckKind.WaitForPrimaryPlacement:
            case SafetyCheckKind.WaitForPrimarySwap:
            case SafetyCheckKind.WaitForReconfiguration:
            case SafetyCheckKind.EnsureAvailability:
                result = new PartitionSafetyCheck(kind, random.CreateRandom <Guid>());
                break;

            default:
                result = new UnknownSafetyCheck(kind);
                break;
            }

            return(result);
        }
        /// <inheritdoc />
        public override object ReadJson(
            JsonReader reader,
            Type objectType,
            object existingValue,
            JsonSerializer serializer)
        {
            ThrowIf.Null(reader, "reader");

            if (reader.TokenType == JsonToken.Null)
            {
                return(null);
            }

            SafetyCheck result = null;

            var    wrapperJObject    = JObject.Load(reader);
            JToken safetyCheckJToken = wrapperJObject[JsonSerializerImplConstants.SafetyCheck];

            if (safetyCheckJToken != null)
            {
                JToken          safetyCheckKindJToken = safetyCheckJToken[JsonSerializerImplConstants.Kind];
                SafetyCheckKind kind = (SafetyCheckKind)safetyCheckKindJToken.ToObject(typeof(SafetyCheckKind));

                switch (kind)
                {
                case SafetyCheckKind.EnsureSeedNodeQuorum:
                    result = new SeedNodeSafetyCheck(kind);
                    break;

                case SafetyCheckKind.EnsurePartitionQuorum:
                case SafetyCheckKind.WaitForInBuildReplica:
                case SafetyCheckKind.WaitForPrimaryPlacement:
                case SafetyCheckKind.WaitForPrimarySwap:
                case SafetyCheckKind.WaitForReconfiguration:
                case SafetyCheckKind.EnsureAvailability:
                    JToken partitionIdJToken = safetyCheckJToken[JsonSerializerImplConstants.PartitionId];
                    Guid   partitionId       = partitionIdJToken.ToObject <Guid>(serializer);
                    result = new PartitionSafetyCheck(kind, partitionId);
                    break;

                default:
                    result = new UnknownSafetyCheck(kind);
                    break;
                }
            }

            return(result);
        }