Exemplo n.º 1
0
        public async Task <BoolResult> OnChangeCheckpointAsync(OperationContext context, CheckpointState initialState, CheckpointManifest manifest)
        {
            _activeCheckpointInfo = _activeCheckpointInfo with
            {
                Manifest = manifest
            };

            if (initialState.FileName == null)
            {
                return(BoolResult.Success);
            }

            return(await context.PerformOperationAsync(
                       Tracer,
                       async() =>
            {
                var(state, index) = await _storage.ReadModifyWriteAsync <CheckpointState, int>(context, initialState.FileName.Value, state =>
                {
                    var index = state.Consumers.Count;
                    var updated = state.Consumers.TryAdd(_primaryMachineLocation);
                    if (!updated)
                    {
                        index = state.Consumers.IndexOf(_primaryMachineLocation);
                    }
                    return (state, index, updated);
                },
                                                                                               defaultValue: () => initialState).ThrowIfFailureAsync();

                var locations = GetCandidateLocations(state, index);
                _activeCheckpointInfo = new CheckpointInfoSnapshot(manifest, locations);
                return Result.Success(index);
            },
Exemplo n.º 2
0
        public Task <Result <MachineMapping> > RegisterMachineAsync(OperationContext context, MachineLocation machineLocation)
        {
            return(context.PerformOperationAsync(Tracer, async() =>
            {
                var(_, assignedMachineId) = await _storage.ReadModifyWriteAsync <ClusterStateMachine, MachineId>(context, _configuration.FileName, current =>
                {
                    var now = _clock.UtcNow;
                    var next = current;
                    if (!current.TryResolveMachineId(machineLocation, out var machineId))
                    {
                        (next, machineId) = current.RegisterMachine(machineLocation, now);
                    }

                    next = next.Recompute(_configuration.RecomputeConfiguration, now);
                    return (next, machineId);
                }).ThrowIfFailureAsync();

                return Result.Success(new MachineMapping(machineLocation, assignedMachineId));
            },