Exemplo n.º 1
0
        /// <summary>
        /// Constructor with more options for derived classes
        /// </summary>
        /// <param name="filename">File name (or prefix) with path</param>
        /// <param name="preallocateFile"></param>
        /// <param name="deleteOnClose"></param>
        /// <param name="disableFileBuffering"></param>
        /// <param name="capacity">The maximum number of bytes this storage device can accommondate, or CAPACITY_UNSPECIFIED if there is no such limit </param>
        /// <param name="recoverDevice">Whether to recover device metadata from existing files</param>
        /// <param name="initialLogFileHandles">Optional set of preloaded safe file handles, which can speed up hydration of preexisting log file handles</param>
        protected internal LocalStorageDevice(string filename,
                                              bool preallocateFile      = false,
                                              bool deleteOnClose        = false,
                                              bool disableFileBuffering = true,
                                              long capacity             = Devices.CAPACITY_UNSPECIFIED,
                                              bool recoverDevice        = false,
                                              IEnumerable <KeyValuePair <int, SafeFileHandle> > initialLogFileHandles = null)
            : base(filename, GetSectorSize(filename), capacity)
        {
            Native32.EnableProcessPrivileges();
            string path = new FileInfo(filename).Directory.FullName;

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            this.preallocateFile      = preallocateFile;
            this.deleteOnClose        = deleteOnClose;
            this.disableFileBuffering = disableFileBuffering;
            logHandles = initialLogFileHandles != null
                ? new SafeConcurrentDictionary <int, SafeFileHandle>(initialLogFileHandles)
                : new SafeConcurrentDictionary <int, SafeFileHandle>();
            if (recoverDevice)
            {
                RecoverFiles();
            }
        }
        public SegmentedLocalStorageDevice(string dirname,
                                           long segmentSize      = -1,
                                           bool enablePrivileges = false, bool useIoCompletionPort = false,
                                           bool unbuffered       = false, bool deleteOnClose       = false)
        {
            this.dirname             = dirname;
            this.segmentSize         = segmentSize;
            this.enablePrivileges    = enablePrivileges;
            this.useIoCompletionPort = useIoCompletionPort;
            this.unbuffered          = unbuffered;
            this.deleteOnClose       = deleteOnClose;

            if (enablePrivileges)
            {
                Native32.EnableProcessPrivileges();
            }

            if (!Native32.GetDiskFreeSpace(dirname.Substring(0, 3),
                                           out uint lpSectorsPerCluster,
                                           out lpBytesPerSector,
                                           out uint lpNumberOfFreeClusters,
                                           out uint lpTotalNumberOfClusters))
            {
                throw new Exception("Unable to retrieve information for disk " + dirname.Substring(0, 3) + " - check if the disk is available.");
            }

            logHandles = new ConcurrentDictionary <int, SafeFileHandle>();
        }
Exemplo n.º 3
0
 /// <summary>
 /// Constructor
 /// </summary>
 /// <param name="filename"></param>
 /// <param name="preallocateFile"></param>
 /// <param name="deleteOnClose"></param>
 public LocalStorageDevice(string filename, bool preallocateFile = false, bool deleteOnClose = false)
     : base(filename, GetSectorSize(filename))
 {
     Native32.EnableProcessPrivileges();
     this.preallocateFile = preallocateFile;
     this.deleteOnClose   = deleteOnClose;
     logHandles           = new ConcurrentDictionary <int, SafeFileHandle>();
 }
Exemplo n.º 4
0
        /// <summary>
        /// Constructor with more options for derived classes
        /// </summary>
        /// <param name="filename">File name (or prefix) with path</param>
        /// <param name="preallocateFile"></param>
        /// <param name="deleteOnClose"></param>
        /// <param name="disableFileBuffering"></param>
        /// <param name="capacity">The maximum number of bytes this storage device can accommondate, or CAPACITY_UNSPECIFIED if there is no such limit </param>
        /// <param name="recoverDevice">Whether to recover device metadata from existing files</param>
        /// <param name="initialLogFileHandles">Optional set of preloaded safe file handles, which can speed up hydration of preexisting log file handles</param>
        /// <param name="useIoCompletionPort">Whether we use IO completion port with polling</param>
        protected internal LocalStorageDevice(string filename,
                                              bool preallocateFile      = false,
                                              bool deleteOnClose        = false,
                                              bool disableFileBuffering = true,
                                              long capacity             = Devices.CAPACITY_UNSPECIFIED,
                                              bool recoverDevice        = false,
                                              IEnumerable <KeyValuePair <int, SafeFileHandle> > initialLogFileHandles = null,
                                              bool useIoCompletionPort = true)
            : base(filename, GetSectorSize(filename), capacity)
        {
#if NETSTANDARD || NET
            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                throw new FasterException("Cannot use LocalStorageDevice from non-Windows OS platform, use ManagedLocalStorageDevice instead.");
            }
#endif
            ThrottleLimit            = 120;
            this.useIoCompletionPort = useIoCompletionPort;
            this._disposed           = false;

            if (useIoCompletionPort)
            {
                ThreadPool.GetMaxThreads(out int workerThreads, out _);
                ioCompletionPort = Native32.CreateIoCompletionPort(new SafeFileHandle(new IntPtr(-1), false), IntPtr.Zero, UIntPtr.Zero, (uint)(workerThreads + NumCompletionThreads));
                for (int i = 0; i < NumCompletionThreads; i++)
                {
                    var thread = new Thread(() => new LocalStorageDeviceCompletionWorker().Start(ioCompletionPort, _callback))
                    {
                        IsBackground = true
                    };
                    thread.Start();
                }
            }

            if (UsePrivileges && preallocateFile)
            {
                Native32.EnableProcessPrivileges();
            }

            string path = new FileInfo(filename).Directory.FullName;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            this.preallocateFile      = preallocateFile;
            this.deleteOnClose        = deleteOnClose;
            this.disableFileBuffering = disableFileBuffering;
            this.results = new ConcurrentQueue <SimpleAsyncResult>();

            logHandles = initialLogFileHandles != null
                ? new SafeConcurrentDictionary <int, SafeFileHandle>(initialLogFileHandles)
                : new SafeConcurrentDictionary <int, SafeFileHandle>();
            if (recoverDevice)
            {
                RecoverFiles();
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="filename"></param>
        /// <param name="preallocateFile"></param>
        /// <param name="deleteOnClose"></param>
        /// <param name="disableFileBuffering"></param>
        /// <param name="capacity">The maximum number of bytes this storage device can accommondate, or CAPACITY_UNSPECIFIED if there is no such limit </param>
        public LocalStorageDevice(string filename,
                                  bool preallocateFile      = false,
                                  bool deleteOnClose        = false,
                                  bool disableFileBuffering = true,
                                  long capacity             = Devices.CAPACITY_UNSPECIFIED)
            : base(filename, GetSectorSize(filename), capacity)

        {
            Native32.EnableProcessPrivileges();
            this.preallocateFile      = preallocateFile;
            this.deleteOnClose        = deleteOnClose;
            this.disableFileBuffering = disableFileBuffering;
            logHandles = new SafeConcurrentDictionary <int, SafeFileHandle>();
            RecoverFiles();
        }
Exemplo n.º 6
0
        public LocalStorageDevice(
            string filename, long segmentSize = -1,
            bool preallocateSegment           = false, bool singleSegment = true, bool deleteOnClose = false)
            : base(filename, segmentSize, GetSectorSize(filename))
        {
            Native32.EnableProcessPrivileges();

            this.preallocateSegment = preallocateSegment;
            this.deleteOnClose      = deleteOnClose;

            if (singleSegment)
            {
                singleLogHandle = CreateHandle(0);
            }
            else
            {
                logHandles = new ConcurrentDictionary <int, SafeFileHandle>();
            }
        }
Exemplo n.º 7
0
        /// <summary>
        /// Constructor with more options for derived classes
        /// </summary>
        /// <param name="filename">File name (or prefix) with path</param>
        /// <param name="preallocateFile"></param>
        /// <param name="deleteOnClose"></param>
        /// <param name="disableFileBuffering"></param>
        /// <param name="capacity">The maximum number of bytes this storage device can accommondate, or CAPACITY_UNSPECIFIED if there is no such limit </param>
        /// <param name="recoverDevice">Whether to recover device metadata from existing files</param>
        /// <param name="initialLogFileHandles">Optional set of preloaded safe file handles, which can speed up hydration of preexisting log file handles</param>
        protected internal LocalStorageDevice(string filename,
                                              bool preallocateFile      = false,
                                              bool deleteOnClose        = false,
                                              bool disableFileBuffering = true,
                                              long capacity             = Devices.CAPACITY_UNSPECIFIED,
                                              bool recoverDevice        = false,
                                              IEnumerable <KeyValuePair <int, SafeFileHandle> > initialLogFileHandles = null)
            : base(filename, GetSectorSize(filename), capacity)
        {
#if NETSTANDARD
            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                throw new FasterException("Cannot use LocalStorageDevice from non-Windows OS platform, use ManagedLocalStorageDevice instead.");
            }
#endif

            if (UsePrivileges && preallocateFile)
            {
                Native32.EnableProcessPrivileges();
            }

            string path = new FileInfo(filename).Directory.FullName;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            this.preallocateFile      = preallocateFile;
            this.deleteOnClose        = deleteOnClose;
            this.disableFileBuffering = disableFileBuffering;
            this.results = new ConcurrentQueue <SimpleAsyncResult>();

            logHandles = initialLogFileHandles != null
                ? new SafeConcurrentDictionary <int, SafeFileHandle>(initialLogFileHandles)
                : new SafeConcurrentDictionary <int, SafeFileHandle>();
            if (recoverDevice)
            {
                RecoverFiles();
            }
        }
Exemplo n.º 8
0
        public LocalStorageDevice(string filename, bool enablePrivileges = false,
                                  bool useIoCompletionPort = false, bool unbuffered = false, bool deleteOnClose = false)
        {
            this.filename            = filename;
            this.enablePrivileges    = enablePrivileges;
            this.useIoCompletionPort = useIoCompletionPort;

            if (enablePrivileges)
            {
                Native32.EnableProcessPrivileges();
            }

            Native32.GetDiskFreeSpace(filename.Substring(0, 3),
                                      out lpSectorsPerCluster,
                                      out lpBytesPerSector,
                                      out lpNumberOfFreeClusters,
                                      out lpTotalNumberOfClusters);

            uint fileAccess   = Native32.GENERIC_READ | Native32.GENERIC_WRITE;
            uint fileShare    = unchecked (((uint)FileShare.ReadWrite & ~(uint)FileShare.Inheritable));
            uint fileCreation = unchecked ((uint)FileMode.OpenOrCreate);
            uint fileFlags    = Native32.FILE_FLAG_OVERLAPPED;

            if (unbuffered)
            {
                fileFlags = fileFlags | Native32.FILE_FLAG_NO_BUFFERING;
            }

            if (deleteOnClose)
            {
                fileFlags = fileFlags | Native32.FILE_FLAG_DELETE_ON_CLOSE;
            }

            logHandle = Native32.CreateFileW(filename,
                                             fileAccess,
                                             fileShare,
                                             IntPtr.Zero,
                                             fileCreation,
                                             fileFlags,
                                             IntPtr.Zero);

            if (enablePrivileges)
            {
                Native32.EnableVolumePrivileges(ref filename, logHandle);
            }

            if (useIoCompletionPort)
            {
                ioCompletionPort = Native32.CreateIoCompletionPort(
                    logHandle,
                    IntPtr.Zero,
                    (uint)logHandle.DangerousGetHandle().ToInt64(),
                    0);
            }

            try
            {
                ThreadPool.BindHandle(logHandle);
            }
            catch (Exception e)
            {
                Console.WriteLine("Log handle! : {0}", logHandle.ToString());
                Console.WriteLine(e.ToString());
                Environment.Exit(0);
            }
        }