增加通过SQL VDI(虚拟设备接口)SQL备份返回到Azure Blob的缓冲区大小

我们已经基于此Code Project创建了一个项目,并已升级到Visual Studio2017。但是,我们修改了C#DotNet部分,以备份到Azure Block Blob。它必须是一个块Blob,以便以后添加加密。 CPP DotNet代码将缓冲区传递到64KB缓冲区中的C#部分。 C#部分将多个64KB块合并为一个大的50MB块,然后再上传到Azure。由于Azure块Blob中的块数大约有50K限制,因此需要组合缓冲区。对于大型数据库,上传为64KB的块将使我们很快超过该限制。

这一切都像魅力一样。但是,它的速度非常慢:与管理Studio中等效的BACKUP TO URL相比,备份80GB数据库的时间大约要长3倍。

这是从C#传递到CPP VDI子系统的命令:

BACKUP DATABASE [bench1] TO VIRTUAL_DEVICE='<deviceGUID>' WITH FORMAT,COMPRESSION

性能的下降似乎在于将64KB的块合并为50MB的块。我的问题:是否有一种方法可以强制VDI子系统返回大于64KB的缓冲区,还是“内置于调味料中”?

这是非常精简的VDIDotNet.CPP代码。为简洁起见,大多数错误处理已被删除。

#include "vdi.h"
#include "vdierror.h"
#include "vdiguid.h"

using namespace System;
using namespace System::Data;
using namespace System::Data::Odbc;
using namespace System::Globalization;
using namespace System::IO;
using namespace System::Runtime::InteropServices;
using namespace System::Threading;
using namespace System::Reflection;

namespace VdiDotNet {

    public ref class VdiEngine
    {
        private: Void SqlServerConnection_InfoMessage(Object^ sender,OdbcInfoMessageEventArgs^ e)
                {
                    VdiDotNet::InfoMessageEventArgs^  i = gcnew VdiDotNet::InfoMessageEventArgs(e->Message);
                    InfoMessageReceived(this,i);
                }
        private: Void ThreadFunc(Object^ data)
        {
            try
            {
                String^ connString = "Driver={SQL Server Native Client 11.0};Server=(local);Trusted_Connection=Yes;";

                //Create and configure an ODBC connection to the local SQL Server
                OdbcConnection^ SqlServerConnection = gcnew OdbcConnection(connString);
                SqlServerConnection->InfoMessage += gcnew OdbcInfoMessageEventHandler(this,&VdiDotNet::VdiEngine::SqlServerConnection_InfoMessage);

                //Create and configure the command to be issued to SQL Server
                OdbcCommand^ SqlServerCommand = gcnew OdbcCommand(data->ToString(),SqlServerConnection);
                SqlServerCommand->CommandType = CommandType::Text;
                SqlServerCommand->CommandTimeout = 0;

                //Notify the user of the command issued
                CommandIssued(this,gcnew CommandIssuedEventArgs(data->ToString()));

                //Open the connection
                SqlServerConnection->Open();

                //Execute the command
                SqlServerCommand->ExecuteNonQuery();
            }
            catch (Exception ^ex)
            {
                LogException(ex);
                throw gcnew ApplicationException(ex->Message);
            }
        }

        private: static Void ExecuteDataTransfer (IClientVirtualDevice* vd,Stream^ s)
        {
            VDC_Command *   cmd;
            DWORD           completionCode;
            DWORD           bytesTransferred;
            HRESULT         hr;

            while (SUCCEEDED(hr = vd->getcommand(INFINITE,&cmd)))
            {
                array<System::Byte>^ arr = gcnew array<System::Byte>(cmd->size);
                bytesTransferred = 0;
                switch (cmd->commandCode)
                {
                    case VDC_Read:
                        // ... stuff ...
                    case VDC_Write:
                        //Copy the data from the cmd object to a CLR array
                        Marshal::Copy((IntPtr)cmd->buffer,arr,cmd->size);

                        //Write the data to the stream
                        s->Write(arr,cmd->size);

                        //Set the number of bytes transferred
                        bytesTransferred = cmd->size;

                        //Set the completion code
                        completionCode = ERROR_SUCCESS;
                        break;

                    case VDC_Flush:
                        //Flush the stream
                        s->Flush();

                        //Set the completion code
                        completionCode = ERROR_SUCCESS;
                        break;

                    case VDC_ClearError:
                        //Set the completion code
                        completionCode = ERROR_SUCCESS;
                        break;

                    default:
                        //Set the completion code
                        completionCode = ERROR_NOT_SUPPORTED;
                        break;
                }

                //Complete the command
                hr = vd->CompleteCommand(cmd,completionCode,bytesTransferred,0);
            }
        }

        public: Void ExecuteCommand(System::String^ command,Stream^ commandStream)
        {
            try
            {
                //Initialize COM
                HRESULT hr = CoInitializeEx(NULL,COINIT_MULTITHREADED);
                //Get an interface to the virtual device set
                IClientVirtualDeviceSet2* vds = NULL;
                hr = CoCreateInstance(CLSID_MSSQL_ClientVirtualDeviceSet,NULL,CLSCTX_INPROC_SERVER,IID_IClientVirtualDeviceSet2,(void**)&vds);

                //Configure the device configuration
                VDConfig config = { 0 };
                vds->getconfiguration(INFINITE,&config);
                config.deviceCount = 1; //The number of virtual devices to create

                //Create a name for the device using a GUID
                String^ DeviceName = System::Guid::NewGuid().ToString()->ToUpper(gcnew CultureInfo("en-US"));
                WCHAR wVdsname[37] = { 0 };
                Marshal::Copy(DeviceName->ToCharArray(),(IntPtr)wVdsname,DeviceName->Length);

                //Create the virtual device set
                hr = vds->CreateEx(NULL,wVdsname,&config);

                //Format the command
                command = String::Format(gcnew CultureInfo("en-US"),command,DeviceName);

                //Create and execute a new thread to execute the command
                Thread^ OdbcThread = gcnew Thread(gcnew ParameterizedThreadStart(this,&VdiDotNet::VdiEngine::ThreadFunc));
                OdbcThread->Start(command);

                //Configure the virtual device set
                hr = vds->getconfiguration(INFINITE,&config);

                //Open the one device on the device set
                IClientVirtualDevice* vd = NULL;
                hr = vds->OpenDevice(wVdsname,&vd);

                //Execute the data transfer
                ExecuteDataTransfer(vd,commandStream);

                //Wait for the thread that issued the backup / restore command to SQL Server to complete.
                OdbcThread->Join();
            }
            catch (Exception ^ex)
            {
                LogException(ex);
                throw gcnew ApplicationException(ex->Message);

            }
        }
    };
}

这是精简后的VDI.H

#include "rpc.h"
#include "rpcndr.h"

#include "windows.h"
#include "ole2.h"

#pragma pack(8)
struct VDConfig
    {
    unsigned long deviceCount;
    unsigned long features;
    unsigned long prefixZonesize;
    unsigned long alignment;
    unsigned long softFileMarkBlockSize;
    unsigned long EOMWarningSize;
    unsigned long serverTimeOut;
    unsigned long blockSize;
    unsigned long maxIODepth;
    unsigned long maxTransferSize;
    unsigned long bufferAreaSize;
    } ;

enum VDCommands
    {   VDC_Read    = 1,VDC_Write   = ( VDC_Read + 1 ),VDC_ClearError  = ( VDC_Write + 1 ),VDC_rewind  = ( VDC_ClearError + 1 ),VDC_WriteMark   = ( VDC_rewind + 1 ),VDC_SkipMarks   = ( VDC_WriteMark + 1 ),VDC_SkipBlocks  = ( VDC_SkipMarks + 1 ),VDC_Load    = ( VDC_SkipBlocks + 1 ),VDC_GetPosition = ( VDC_Load + 1 ),VDC_SetPosition = ( VDC_GetPosition + 1 ),VDC_Discard = ( VDC_SetPosition + 1 ),VDC_Flush   = ( VDC_Discard + 1 ),VDC_snapshot    = ( VDC_Flush + 1 ),VDC_Mountsnapshot   = ( VDC_snapshot + 1 ),VDC_PrepareToFreeze = ( VDC_Mountsnapshot + 1 ),VDC_FileInfoBegin   = ( VDC_PrepareToFreeze + 1 ),VDC_FileInfoEnd = ( VDC_FileInfoBegin + 1 ) 
    } ;

struct VDC_Command
    {
    DWORD commandCode;
    DWORD size;
    DWORDLONG position;
    BYTE *buffer;
    } ;

谢谢您的建议。

wyd379299458 回答:增加通过SQL VDI(虚拟设备接口)SQL备份返回到Azure Blob的缓冲区大小

我终于明白了。发布答案,以防万一其他人遇到类似问题

我在BACKUP命令上使用了MAXTRANSFERSIZE=4194304(最大值),例如:

BACKUP DATABASE [bench1] TO VIRTUAL_DEVICE='<deviceGUID>' WITH FORMAT,COMPRESSION,MAXTRANSFERSIZE=4194304

此后,SQLVDI每块传递回4MB的缓冲区。

本文链接:https://www.f2er.com/3151748.html

大家都在问