示例#1
0
        /// <inheritdoc />
        public override CheckRecordExistsResult Execute(
            CheckRecordExistsOp operation)
        {
            operation.MustForArg(nameof(operation)).NotBeNull();

            var stream = this.streamFactory.Execute(new GetStreamFromRepresentationOp(operation.StreamRepresentation));

            stream.MustForOp(nameof(stream))
            .BeAssignableToType <ISyncReturningProtocol <StandardGetHandlingStatusOp, IReadOnlyDictionary <long, HandlingStatus> > >();
            var streamProtocol = (ISyncReturningProtocol <StandardGetLatestRecordOp, StreamRecord>)stream;

            var getLatestRecordOp = new StandardGetLatestRecordOp(
                operation.RecordFilter,
                streamRecordItemsToInclude: StreamRecordItemsToInclude.MetadataOnly);
            var latestMatchingRecord = streamProtocol.Execute(getLatestRecordOp);

            var result = new CheckRecordExistsResult(operation.StreamRepresentation, latestMatchingRecord != null);

            return(result);
        }
        public override void Execute(
            RunScheduleOp operation)
        {
            operation.MustForArg(nameof(operation)).NotBeNull();

            var getDistinctStringSerializedIdsOp = new StandardGetDistinctStringSerializedIdsOp(
                new RecordFilter(
                    objectTypes: new[]
            {
                typeof(ExecuteOpRequestedEvent <ExecuteOpOnScheduleOp>).ToRepresentation(),
            },
                    deprecatedIdTypes: new[]
            {
                operation.DeprecatedIdentifierType
            }));
            var distinctIds = this.registeredScheduleStream.Execute(getDistinctStringSerializedIdsOp);

            foreach (var distinctId in distinctIds)
            {
                var getLatestRecordOp = new StandardGetLatestRecordOp(
                    new RecordFilter(
                        ids: new[]
                {
                    distinctId,
                }));

                var runOnScheduleRecord = this.registeredScheduleStream.Execute(getLatestRecordOp);
                runOnScheduleRecord
                .Payload
                .PayloadTypeRepresentation
                .MustForOp("recordFromRegisteredScheduleStreamExpectedToBeExecuteOpRequestedOfExecuteOpOnSchedule")
                .BeEqualTo(typeof(ExecuteOpRequestedEvent <ExecuteOpOnScheduleOp>).ToRepresentation());

                var executeOpOnScheduleOpEvent =
                    runOnScheduleRecord.Payload.DeserializePayloadUsingSpecificFactory <ExecuteOpRequestedEvent <ExecuteOpOnScheduleOp> >(
                        this.registeredScheduleStream.SerializerFactory);
                var executeOpOnScheduleOp = executeOpOnScheduleOpEvent.Operation;
                this.executeOpOnScheduleProtocol.Execute(executeOpOnScheduleOp);
            }
        }
        public override StreamRecord Execute(
            StandardGetLatestRecordOp operation)
        {
            operation.MustForArg(nameof(operation)).NotBeNull();

            var sqlServerLocator      = this.TryGetLocator(operation);
            var convertedRecordFilter = this.ConvertRecordFilter(operation.RecordFilter, sqlServerLocator);

            var storedProcOp = StreamSchema.Sprocs.GetLatestRecord.BuildExecuteStoredProcedureOp(
                this.Name,
                convertedRecordFilter,
                operation.StreamRecordItemsToInclude);

            var sqlProtocol = this.BuildSqlOperationsProtocol(sqlServerLocator);
            var sprocResult = sqlProtocol.Execute(storedProcOp);

            long internalRecordId = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.GetLatestRecord.OutputParamName.InternalRecordId)].GetValueOfType <long>();

            if (internalRecordId == StreamSchema.Tables.Record.NullId)
            {
                switch (operation.RecordNotFoundStrategy)
                {
                case RecordNotFoundStrategy.ReturnDefault:
                    return(null);

                case RecordNotFoundStrategy.Throw:
                    throw new InvalidOperationException(
                              Invariant(
                                  $"Expected stream {this.StreamRepresentation} to contain a matching record for {operation}, none was found and {nameof(operation.RecordNotFoundStrategy)} is '{operation.RecordNotFoundStrategy}'."));

                default:
                    throw new NotSupportedException(
                              Invariant($"{nameof(RecordNotFoundStrategy)} {operation.RecordNotFoundStrategy} is not supported."));
                }
            }

            int    serializerRepresentationId  = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.GetLatestRecord.OutputParamName.SerializerRepresentationId)].GetValueOfType <int>();
            int    identifierTypeWithVersionId = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.GetLatestRecord.OutputParamName.IdentifierTypeWithVersionId)].GetValueOfType <int>();
            int    objectTypeWithVersionId     = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.GetLatestRecord.OutputParamName.ObjectTypeWithVersionId)].GetValueOfType <int>();
            string stringSerializedId          = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.GetLatestRecord.OutputParamName.StringSerializedId)].GetValueOfType <string>();
            string stringSerializedObject      = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.GetLatestRecord.OutputParamName.StringSerializedObject)].GetValueOfType <string>();

            byte[]   binarySerializedObject = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.GetLatestRecord.OutputParamName.BinarySerializedObject)].GetValueOfType <byte[]>();
            DateTime recordTimestampRaw     = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.GetLatestRecord.OutputParamName.RecordDateTime)].GetValueOfType <DateTime>();
            DateTime?objectTimestampRaw     = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.GetLatestRecord.OutputParamName.ObjectDateTime)].GetValueOfType <DateTime?>();
            string   tagIdsCsv = sprocResult.OutputParameters[nameof(StreamSchema.Sprocs.GetLatestRecord.OutputParamName.TagIdsCsv)].GetValueOfType <string>();

            var identifiedSerializerRepresentation = this.GetSerializerRepresentationFromId(sqlServerLocator, serializerRepresentationId);
            var identifierType = this.GetTypeById(sqlServerLocator, identifierTypeWithVersionId, true);
            var objectType     = this.GetTypeById(sqlServerLocator, objectTypeWithVersionId, true);
            var tagIds         = tagIdsCsv?.FromCsv().Select(_ => long.Parse(_, CultureInfo.InvariantCulture)).ToList();
            var tags           = tagIds == null ? null : this.GetTagsByIds(sqlServerLocator, tagIds);

            var recordTimestamp = recordTimestampRaw.ToUtc();

            var objectTimestamp = objectTimestampRaw == null ? (DateTime?)null : objectTimestampRaw.ToUtc();

            var metadata = new StreamRecordMetadata(
                stringSerializedId,
                identifiedSerializerRepresentation.SerializerRepresentation,
                identifierType,
                objectType,
                tags,
                recordTimestamp,
                objectTimestamp);

            DescribedSerializationBase payload;

            if (operation.StreamRecordItemsToInclude == StreamRecordItemsToInclude.MetadataAndPayload)
            {
                switch (identifiedSerializerRepresentation.SerializationFormat)
                {
                case SerializationFormat.Binary:
                    payload = new BinaryDescribedSerialization(
                        objectType.WithVersion,
                        identifiedSerializerRepresentation.SerializerRepresentation,
                        binarySerializedObject);
                    break;

                case SerializationFormat.String:
                    payload = new StringDescribedSerialization(
                        objectType.WithVersion,
                        identifiedSerializerRepresentation.SerializerRepresentation,
                        stringSerializedObject);
                    break;

                default:
                    throw new NotSupportedException(
                              Invariant($"{nameof(SerializationFormat)} {identifiedSerializerRepresentation.SerializationFormat} is not supported."));
                }
            }
            else
            {
                payload = new NullDescribedSerialization(metadata.TypeRepresentationOfObject.WithVersion, metadata.SerializerRepresentation);
            }

            var result = new StreamRecord(internalRecordId, metadata, payload);

            return(result);
        }
示例#4
0
        public override void Execute(
            RunReactorOp operation)
        {
            operation.MustForArg(nameof(operation)).NotBeNull();

            var getDistinctStringSerializedIdsOp = new StandardGetDistinctStringSerializedIdsOp(
                new RecordFilter(
                    objectTypes: new[]
            {
                typeof(ReactionRegistration).ToRepresentation(),
            },
                    deprecatedIdTypes: new[]
            {
                operation.DeprecatedIdentifierType
            }));
            var distinctIds = this.reactionRegistrationStream.Execute(getDistinctStringSerializedIdsOp);

            Parallel.ForEach(distinctIds,
                             new ParallelOptions {
                MaxDegreeOfParallelism = operation.DegreesOfParallelismForDependencyChecks
            },
                             distinctId =>
            {
                var found = ReactionRegistrationIdToNextEvaluationCutoffMap.TryGetValue(distinctId, out var nextEvaluationCutoff);

                if (!found || DateTime.UtcNow > nextEvaluationCutoff)
                {
                    try
                    {
                        var getLatestRecordOp = new StandardGetLatestRecordOp(
                            new RecordFilter(
                                ids: new[]
                        {
                            distinctId,
                        }));

                        var reactionRegistrationRecord = this.reactionRegistrationStream.Execute(getLatestRecordOp);
                        reactionRegistrationRecord
                        .Payload
                        .PayloadTypeRepresentation
                        .RemoveAssemblyVersions()
                        .MustForOp("recordFromReactionRegistrationStreamExpectedToBeRegisteredReaction")
                        .BeEqualTo(ReactionRegistrationTypeRepWithoutVersion);

                        var reactionRegistration =
                            reactionRegistrationRecord.Payload.DeserializePayloadUsingSpecificFactory <ReactionRegistration>(
                                this.reactionRegistrationStream.SerializerFactory);

                        var evaluateReactionRegistrationOp     = new EvaluateReactionRegistrationOp(reactionRegistration);
                        var evaluateReactionRegistrationResult =
                            this.evaluateReactionRegistrationProtocol.Execute(evaluateReactionRegistrationOp);
                        if (evaluateReactionRegistrationResult.ReactionEvent != null)
                        {
                            var reaction = evaluateReactionRegistrationResult.ReactionEvent;

                            this.reactionStream.PutWithId(reaction.Id, reaction, reaction.Tags);

                            // once we have recorded the reaction then we can finalize the handling cycle.
                            foreach (var recordSetHandlingMemento in evaluateReactionRegistrationResult.RecordSetHandlingMementos)
                            {
                                recordSetHandlingMemento.CompleteSet();
                            }
                        }

                        var newNextEvaluationCutoff = DateTime.UtcNow.Add(reactionRegistration.IdealWaitTimeBetweenEvaluations);
                        ReactionRegistrationIdToNextEvaluationCutoffMap.AddOrUpdate(
                            distinctId,
                            newNextEvaluationCutoff,
                            (_, __) => newNextEvaluationCutoff);
                    }
                    catch (Exception ex)
                    {
                        throw new ReactorException(
                            Invariant($"Failed to process {nameof(ReactionRegistration)} Id: {distinctId}."),
                            ex,
                            operation);
                    }
                }
            });
        }