Exemple #1
0
        public async Task PutServerWideExternalReplicationCommand()
        {
            using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
            {
                var configurationBlittable = await context.ReadForMemoryAsync(RequestBodyStream(), "server-wide-external-replication-configuration");

                var configuration = JsonDeserializationCluster.ServerWideExternalReplication(configurationBlittable);

                ServerStore.LicenseManager.AssertCanAddExternalReplication(configuration.DelayReplicationFor);

                var(newIndex, _) = await ServerStore.PutServerWideExternalReplicationAsync(configuration, GetRaftRequestIdFromQuery());

                await ServerStore.WaitForCommitIndexChange(RachisConsensus.CommitIndexModification.GreaterOrEqual, newIndex);

                await using (var writer = new AsyncBlittableJsonTextWriter(context, ResponseBodyStream()))
                    using (context.OpenReadTransaction())
                    {
                        var taskName = ServerStore.Cluster.GetServerWideTaskNameByTaskId(context, ClusterStateMachine.ServerWideConfigurationKey.ExternalReplication, newIndex);
                        if (taskName == null)
                        {
                            throw new InvalidOperationException($"External replication name is null for server-wide external replication with task id: {newIndex}");
                        }

                        var putResponse = new ServerWideExternalReplicationResponse
                        {
                            Name             = taskName,
                            RaftCommandIndex = newIndex
                        };

                        HttpContext.Response.StatusCode = (int)HttpStatusCode.Created;
                        context.Write(writer, putResponse.ToJson());
                    }
            }
        }
        public Task GetServerWideTasksForStudio()
        {
            var taskName     = GetStringQueryString("name", required: false);
            var typeAsString = GetStringQueryString("type", required: false);
            var tryParse     = Enum.TryParse(typeAsString, out OngoingTaskType type);

            if (typeAsString != null && tryParse == false)
            {
                throw new ArgumentException($"{typeAsString} is unknown task type.");
            }

            using (ServerStore.ContextPool.AllocateOperationContext(out TransactionOperationContext context))
                using (context.OpenReadTransaction())
                    using (var writer = new BlittableJsonTextWriter(context, ResponseBodyStream()))
                    {
                        var result = new ServerWideTasksResult();

                        var blittables = ServerStore.Cluster.GetServerWideConfigurations(context, OngoingTaskType.Backup, taskName);
                        foreach (var blittable in blittables)
                        {
                            var configuration = JsonDeserializationCluster.ServerWideBackupConfiguration(blittable);

                            if (taskName != null && type == OngoingTaskType.Backup &&
                                string.Equals(taskName, configuration.Name) == false)
                            {
                                continue;
                            }

                            result.Tasks.Add(new ServerWideTasksResult.ServerWideBackupTask
                            {
                                TaskId             = configuration.TaskId,
                                TaskName           = configuration.Name,
                                TaskState          = configuration.Disabled ? OngoingTaskState.Disabled : OngoingTaskState.Enabled,
                                ExcludedDatabases  = configuration.ExcludedDatabases,
                                BackupType         = configuration.BackupType,
                                RetentionPolicy    = configuration.RetentionPolicy,
                                BackupDestinations = configuration.GetDestinations(),
                                IsEncrypted        = configuration.BackupEncryptionSettings != null &&
                                                     configuration.BackupEncryptionSettings.EncryptionMode != EncryptionMode.None
                            });
                        }

                        blittables = ServerStore.Cluster.GetServerWideConfigurations(context, OngoingTaskType.Replication, taskName);
                        foreach (var blittable in blittables)
                        {
                            var configuration = JsonDeserializationCluster.ServerWideExternalReplication(blittable);

                            if (taskName != null && type == OngoingTaskType.Replication &&
                                string.Equals(taskName, configuration.Name) == false)
                            {
                                continue;
                            }

                            result.Tasks.Add(new ServerWideTasksResult.ServerWideExternalReplicationTask
                            {
                                TaskId                = configuration.TaskId,
                                TaskName              = configuration.Name,
                                TaskState             = configuration.Disabled ? OngoingTaskState.Disabled : OngoingTaskState.Enabled,
                                ExcludedDatabases     = configuration.ExcludedDatabases,
                                TopologyDiscoveryUrls = configuration.TopologyDiscoveryUrls,
                                DelayReplicationFor   = configuration.DelayReplicationFor,
                            });
                        }

                        context.Write(writer, result.ToJson());
                        writer.Flush();

                        return(Task.CompletedTask);
                    }
        }