Example #1
0
        public override async Task <ResetResp> Reset(ResetReq request, ServerCallContext context)
        {
            var resetSource = new TaskCompletionSource <bool>();

            var options = request.Options;

            var user = context.GetHttpContext().User;

            if (!await _authorizationProvider.CheckAccessAsync(user, ResetOperation, context.CancellationToken)
                .ConfigureAwait(false))
            {
                throw RpcExceptions.AccessDenied();
            }
            var name  = options.Name;
            var runAs = new ProjectionManagementMessage.RunAs(user);

            var envelope = new CallbackEnvelope(OnMessage);

            _queue.Publish(new ProjectionManagementMessage.Command.Reset(envelope, name, runAs));

            await resetSource.Task.ConfigureAwait(false);

            return(new ResetResp());

            void OnMessage(Message message)
            {
                if (!(message is ProjectionManagementMessage.Updated))
                {
                    resetSource.TrySetException(UnknownMessage <ProjectionManagementMessage.Updated>(message));
                    return;
                }

                resetSource.TrySetResult(true);
            }
        }
Example #2
0
        public override async Task <ClusterInfo> Read(Empty request, ServerCallContext context)
        {
            var user = context.GetHttpContext().User;

            if (!await _authorizationProvider.CheckAccessAsync(user, ReadOperation, context.CancellationToken).ConfigureAwait(false))
            {
                throw RpcExceptions.AccessDenied();
            }
            var tcs = new TaskCompletionSource <ClusterInfo>();

            _bus.Publish(new GossipMessage.ReadGossip(new CallbackEnvelope(msg => GossipResponse(msg, tcs))));
            return(await tcs.Task.ConfigureAwait(false));
        }
Example #3
0
        public override async Task <Empty> Prepare(PrepareRequest request, ServerCallContext context)
        {
            var user = context.GetHttpContext().User;

            if (!await _authorizationProvider.CheckAccessAsync(user, PrepareOperation, context.CancellationToken).ConfigureAwait(false))
            {
                throw RpcExceptions.AccessDenied();
            }
            _bus.Publish(new ElectionMessage.Prepare(
                             Uuid.FromDto(request.ServerId).ToGuid(),
                             new DnsEndPoint(request.ServerHttp.Address, (int)request.ServerHttp.Port),
                             request.View));
            return(EmptyResult);
        }
Example #4
0
        public override async Task <ClusterInfo> Update(GossipRequest request, ServerCallContext context)
        {
            var user = context.GetHttpContext().User;

            if (!await _authorizationProvider.CheckAccessAsync(user, UpdateOperation, context.CancellationToken).ConfigureAwait(false))
            {
                throw RpcExceptions.AccessDenied();
            }
            var clusterInfo = EventStore.Core.Cluster.ClusterInfo.FromGrpcClusterInfo(request.Info);
            var tcs         = new TaskCompletionSource <ClusterInfo>();

            _bus.Publish(new GossipMessage.GossipReceived(new CallbackEnvelope(msg => GossipResponse(msg, tcs)),
                                                          clusterInfo, new DnsEndPoint(request.Server.Address, (int)request.Server.Port)));
            return(await tcs.Task.ConfigureAwait(false));
        }
Example #5
0
        public override async Task <StateResp> State(StateReq request, ServerCallContext context)
        {
            var user = context.GetHttpContext().User;

            if (!await _authorizationProvider.CheckAccessAsync(user, StateOperation, context.CancellationToken)
                .ConfigureAwait(false))
            {
                throw RpcExceptions.AccessDenied();
            }
            var resultSource = new TaskCompletionSource <Value>();

            var options = request.Options;

            var name      = options.Name;
            var partition = options.Partition ?? string.Empty;

            var envelope = new CallbackEnvelope(OnMessage);

            _queue.Publish(new ProjectionManagementMessage.Command.GetState(envelope, name, partition));

            return(new StateResp {
                State = await resultSource.Task.ConfigureAwait(false)
            });

            void OnMessage(Message message)
            {
                if (!(message is ProjectionManagementMessage.ProjectionState result))
                {
                    resultSource.TrySetException(UnknownMessage <ProjectionManagementMessage.ProjectionState>(message));
                    return;
                }

                if (string.IsNullOrEmpty(result.State))
                {
                    resultSource.TrySetResult(new Value {
                        StructValue = new Struct()
                    });
                    return;
                }
                var document = JsonDocument.Parse(result.State);

                resultSource.TrySetResult(GetProtoValue(document.RootElement));
            }
        }
        public override async Task <UpdateResp> Update(UpdateReq request, ServerCallContext context)
        {
            var updatedSource = new TaskCompletionSource <bool>();
            var options       = request.Options;

            var user = context.GetHttpContext().User;

            if (!await _authorizationProvider.CheckAccessAsync(user, UpdateOperation, context.CancellationToken)
                .ConfigureAwait(false))
            {
                throw RpcExceptions.AccessDenied();
            }
            const string handlerType = "JS";
            var          name        = options.Name;
            var          query       = options.Query;
            bool?        emitEnabled = (options.EmitOptionCase, options.EmitEnabled) switch {
                (EmitOptionOneofCase.EmitEnabled, true) => true,
                (EmitOptionOneofCase.EmitEnabled, false) => false,
                (EmitOptionOneofCase.NoEmitOptions, _) => default,
Example #7
0
        public override async Task <Empty> RestartSubsystem(Empty empty, ServerCallContext context)
        {
            var restart  = new TaskCompletionSource <bool>();
            var envelope = new CallbackEnvelope(OnMessage);

            var user = context.GetHttpContext().User;

            if (!await _authorizationProvider.CheckAccessAsync(user, RestartOperation, context.CancellationToken)
                .ConfigureAwait(false))
            {
                throw RpcExceptions.AccessDenied();
            }

            _queue.Publish(new ProjectionSubsystemMessage.RestartSubsystem(envelope));

            await restart.Task.ConfigureAwait(false);

            return(new Empty());

            void OnMessage(Message message)
            {
                switch (message)
                {
                case ProjectionSubsystemMessage.SubsystemRestarting _:
                    restart.TrySetResult(true);
                    break;

                case ProjectionSubsystemMessage.InvalidSubsystemRestart fail:
                    restart.TrySetException(InvalidSubsystemRestart(fail.SubsystemState));
                    break;

                default:
                    restart.TrySetException(
                        UnknownMessage <ProjectionSubsystemMessage.SubsystemRestarting>(message));
                    break;
                }
            }
        }
Example #8
0
        public override async Task <Empty> PrepareOk(PrepareOkRequest request, ServerCallContext context)
        {
            var user = context.GetHttpContext().User;

            if (!await _authorizationProvider.CheckAccessAsync(user, PrepareOkOperation, context.CancellationToken).ConfigureAwait(false))
            {
                throw RpcExceptions.AccessDenied();
            }
            _bus.Publish(new ElectionMessage.PrepareOk(
                             request.View,
                             Uuid.FromDto(request.ServerId).ToGuid(),
                             new DnsEndPoint(request.ServerHttp.Address, (int)request.ServerHttp.Port),
                             request.EpochNumber,
                             request.EpochPosition,
                             Uuid.FromDto(request.EpochId).ToGuid(),
                             Uuid.FromDto(request.EpochLeaderInstanceId).ToGuid(),
                             request.LastCommitPosition,
                             request.WriterCheckpoint,
                             request.ChaserCheckpoint,
                             request.NodePriority,
                             ClusterInfo.FromGrpcClusterInfo(request.ClusterInfo)));
            return(EmptyResult);
        }
Example #9
0
        public override async Task <CreateResp> Create(CreateReq request, ServerCallContext context)
        {
            var createdSource = new TaskCompletionSource <bool>();
            var options       = request.Options;

            var user = context.GetHttpContext().User;

            if (!await _authorizationProvider.CheckAccessAsync(user, CreateOperation, context.CancellationToken)
                .ConfigureAwait(false))
            {
                throw RpcExceptions.AccessDenied();
            }
            const string handlerType = "JS";
            var          name        = options.ModeCase switch {
                ModeOneofCase.Continuous => options.Continuous.Name,
                ModeOneofCase.Transient => options.Transient.Name,
                ModeOneofCase.OneTime => Guid.NewGuid().ToString("D"),
                _ => throw new InvalidOperationException()
            };
            var projectionMode = options.ModeCase switch {
                ModeOneofCase.Continuous => ProjectionMode.Continuous,
                ModeOneofCase.Transient => ProjectionMode.Transient,
                ModeOneofCase.OneTime => ProjectionMode.OneTime,
                _ => throw new InvalidOperationException()
            };
            var emitEnabled = options.ModeCase switch {
                ModeOneofCase.Continuous => options.Continuous.TrackEmittedStreams,
                _ => false
            };
            var checkpointsEnables = options.ModeCase switch {
                ModeOneofCase.Continuous => true,
                ModeOneofCase.OneTime => false,
                ModeOneofCase.Transient => false,
                _ => throw new InvalidOperationException()
            };
            var enabled             = true;
            var trackEmittedStreams = (options.ModeCase, emitEnabled) switch {
                (ModeOneofCase.Continuous, false) => true,
                _ => false
            };
            var runAs = new ProjectionManagementMessage.RunAs(user);

            var envelope = new CallbackEnvelope(OnMessage);

            _queue.Publish(new ProjectionManagementMessage.Command.Post(envelope, projectionMode, name, runAs,
                                                                        handlerType, options.Query, enabled, checkpointsEnables, emitEnabled, trackEmittedStreams, true));

            await createdSource.Task.ConfigureAwait(false);

            return(new CreateResp());

            void OnMessage(Message message)
            {
                if (!(message is ProjectionManagementMessage.Updated))
                {
                    createdSource.TrySetException(UnknownMessage <ProjectionManagementMessage.Updated>(message));
                    return;
                }

                createdSource.TrySetResult(true);
            }
        }
    }
}
        public override async Task Statistics(StatisticsReq request, IServerStreamWriter <StatisticsResp> responseStream,
                                              ServerCallContext context)
        {
            var user = context.GetHttpContext().User;

            if (!await _authorizationProvider.CheckAccessAsync(user, StatisticsOperation, context.CancellationToken)
                .ConfigureAwait(false))
            {
                throw RpcExceptions.AccessDenied();
            }

            var statsSource = new TaskCompletionSource <ProjectionStatistics[]>();

            var options = request.Options;
            var name    = string.IsNullOrEmpty(options.Name) ? null : options.Name;
            var mode    = options.ModeCase switch {
                ModeOneofCase.Continuous => ProjectionMode.Continuous,
                ModeOneofCase.Transient => ProjectionMode.Transient,
                ModeOneofCase.OneTime => ProjectionMode.OneTime,
                _ => default(Nullable <ProjectionMode>)
            };

            var envelope = new CallbackEnvelope(OnMessage);

            _queue.Publish(new ProjectionManagementMessage.Command.GetStatistics(envelope, mode, name, true));

            foreach (var stats in Array.ConvertAll(await statsSource.Task.ConfigureAwait(false), s => new StatisticsResp.Types.Details {
                BufferedEvents = s.BufferedEvents,
                CheckpointStatus = s.CheckpointStatus,
                CoreProcessingTime = s.CoreProcessingTime,
                EffectiveName = s.EffectiveName,
                Epoch = s.Epoch,
                EventsProcessedAfterRestart = s.EventsProcessedAfterRestart,
                LastCheckpoint = s.LastCheckpoint,
                Mode = s.Mode.ToString(),
                Name = s.Name,
                ReadsInProgress = s.ReadsInProgress,
                PartitionsCached = s.PartitionsCached,
                Position = s.Position,
                Progress = s.Progress,
                StateReason = s.StateReason,
                Status = s.Status,
                Version = s.Version,
                WritePendingEventsAfterCheckpoint = s.WritePendingEventsAfterCheckpoint,
                WritePendingEventsBeforeCheckpoint = s.WritePendingEventsBeforeCheckpoint,
                WritesInProgress = s.WritesInProgress
            }))
            {
                await responseStream.WriteAsync(new StatisticsResp { Details = stats }).ConfigureAwait(false);
            }

            void OnMessage(Message message)
            {
                if (!(message is ProjectionManagementMessage.Statistics statistics))
                {
                    statsSource.TrySetException(UnknownMessage <ProjectionManagementMessage.Statistics>(message));
                    return;
                }

                statsSource.TrySetResult(statistics.Projections);
            }
        }
    }