예제 #1
0
        private void ReadHeaderResponseAndThrowIfUnAuthorized()
        {
            const int timeout = 2 * 60 * 1000; // TODO: configurable

            using (var replicationTcpConnectReplyMessage = _interruptibleRead.ParseToMemory(
                       _connectionDisposed,
                       "replication acknowledge response",
                       timeout,
                       _buffer,
                       CancellationToken))
            {
                if (replicationTcpConnectReplyMessage.Timeout)
                {
                    ThrowTimeout(timeout);
                }
                if (replicationTcpConnectReplyMessage.Interrupted)
                {
                    ThrowConnectionClosed();
                }
                var headerResponse = JsonDeserializationServer.TcpConnectionHeaderResponse(replicationTcpConnectReplyMessage.Document);
                switch (headerResponse.Status)
                {
                case TcpConnectionStatus.Ok:
                    break;

                case TcpConnectionStatus.AuthorizationFailed:
                    throw new UnauthorizedAccessException($"{Destination.FromString()} replied with failure {headerResponse.Message}");

                case TcpConnectionStatus.TcpVersionMismatch:
                    throw new InvalidOperationException($"{Destination.FromString()} replied with failure {headerResponse.Message}");
                }
            }
        }
예제 #2
0
        private int ReadHeaderResponseAndThrowIfUnAuthorized(JsonOperationContext jsonContext, BlittableJsonTextWriter writer, Stream stream, string url)
        {
            const int timeout = 2 * 60 * 1000;

            using (var replicationTcpConnectReplyMessage = _interruptibleRead.ParseToMemory(
                       _connectionDisposed,
                       "replication acknowledge response",
                       timeout,
                       _buffer,
                       CancellationToken))
            {
                if (replicationTcpConnectReplyMessage.Timeout)
                {
                    ThrowTimeout(timeout);
                }
                if (replicationTcpConnectReplyMessage.Interrupted)
                {
                    ThrowConnectionClosed();
                }
                var headerResponse = JsonDeserializationServer.TcpConnectionHeaderResponse(replicationTcpConnectReplyMessage.Document);
                switch (headerResponse.Status)
                {
                case TcpConnectionStatus.Ok:
                    return(headerResponse.Version);

                case TcpConnectionStatus.AuthorizationFailed:
                    throw new AuthorizationException($"{Destination.FromString()} replied with failure {headerResponse.Message}");

                case TcpConnectionStatus.TcpVersionMismatch:
                    if (headerResponse.Version != -1)
                    {
                        return(headerResponse.Version);
                    }
                    //Kindly request the server to drop the connection
                    jsonContext.Write(writer, new DynamicJsonValue
                    {
                        [nameof(TcpConnectionHeaderMessage.DatabaseName)]     = Destination.Database,
                        [nameof(TcpConnectionHeaderMessage.Operation)]        = TcpConnectionHeaderMessage.OperationTypes.Drop.ToString(),
                        [nameof(TcpConnectionHeaderMessage.SourceNodeTag)]    = _parent._server.NodeTag,
                        [nameof(TcpConnectionHeaderMessage.OperationVersion)] = TcpConnectionHeaderMessage.GetOperationTcpVersion(TcpConnectionHeaderMessage.OperationTypes.Drop),
                        [nameof(TcpConnectionHeaderMessage.Info)]             = $"Couldn't agree on replication tcp version ours:{TcpConnectionHeaderMessage.ReplicationTcpVersion} theirs:{headerResponse.Version}"
                    });
                    writer.Flush();
                    throw new InvalidOperationException($"{Destination.FromString()} replied with failure {headerResponse.Message}");

                default:
                    throw new InvalidOperationException($"{Destination.FromString()} replied with unknown status {headerResponse.Status}, message:{headerResponse.Message}");
                }
            }
        }
        private int ReadHeaderResponseAndThrowIfUnAuthorized(JsonOperationContext context, BlittableJsonTextWriter writer, Stream stream, string url)
        {
            const int timeout = 2 * 60 * 1000;

            using (var replicationTcpConnectReplyMessage = _interruptibleRead.ParseToMemory(
                       _connectionDisposed,
                       "replication acknowledge response",
                       timeout,
                       _buffer,
                       CancellationToken))
            {
                if (replicationTcpConnectReplyMessage.Timeout)
                {
                    ThrowTimeout(timeout);
                }
                if (replicationTcpConnectReplyMessage.Interrupted)
                {
                    ThrowConnectionClosed();
                }
                var headerResponse = JsonDeserializationServer.TcpConnectionHeaderResponse(replicationTcpConnectReplyMessage.Document);
                switch (headerResponse.Status)
                {
                case TcpConnectionStatus.Ok:
                    return(headerResponse.Version);

                case TcpConnectionStatus.AuthorizationFailed:
                    throw new AuthorizationException($"{Destination.FromString()} replied with failure {headerResponse.Message}");

                case TcpConnectionStatus.TcpVersionMismatch:
                    if (headerResponse.Version != TcpNegotiation.OutOfRangeStatus)
                    {
                        return(headerResponse.Version);
                    }
                    //Kindly request the server to drop the connection
                    SendDropMessage(context, writer, headerResponse);
                    throw new InvalidOperationException($"{Destination.FromString()} replied with failure {headerResponse.Message}");

                default:
                    throw new InvalidOperationException($"{Destination.FromString()} replied with unknown status {headerResponse.Status}, message:{headerResponse.Message}");
                }
            }
        }