Ejemplo n.º 1
0
        private async Task ConsumeJobs()
        {
            try
            {
                var functions = new Functions(_options.Logger);

                var checkpointDir = Path.Combine(_options.PersistDirectoryPath, CheckPoints);
                var logDir        = Path.Combine(_options.PersistDirectoryPath, Logs);

                Directory.CreateDirectory(logDir);
                Directory.CreateDirectory(checkpointDir);

                var indexLogPath  = Path.Combine(logDir, $"{_options.DictionaryName}-index.log");
                var objectLogPath = Path.Combine(logDir, $"{_options.DictionaryName}-object.log");

                IndexLog  = Devices.CreateLogDevice(indexLogPath, true, _options.DeleteOnClose, -1, true);
                ObjectLog = Devices.CreateLogDevice(objectLogPath, true, _options.DeleteOnClose, -1, true);

                UnsafeContext = new Context();

                Log = new LogSettings
                {
                    LogDevice         = IndexLog,
                    ObjectLogDevice   = ObjectLog,
                    SegmentSizeBits   = (int)_options.SegmentSize,
                    PageSizeBits      = (int)_options.PageSize,
                    MemorySizeBits    = (int)_options.MemorySize,
                    ReadCacheSettings = new ReadCacheSettings()
                    {
                        MemorySizeBits       = (int)_options.MemorySize + 1,
                        PageSizeBits         = (int)_options.PageSize + 1,
                        SecondChanceFraction = .2
                    }
                };

                var checkpointSettings = new CheckpointSettings()
                {
                    CheckpointDir  = checkpointDir,
                    CheckPointType = _options.CheckPointType
                };

                KV = new FasterKV
                     <KeyEnvelope, ValueEnvelope, ValueEnvelope, ValueEnvelope, Context, Functions>(
                    1L << 20, functions,
                        Log,
                        checkpointSettings,
                        new SerializerSettings <KeyEnvelope, ValueEnvelope>
                        {
                        keySerializer   = () => new KeySerializer(),
                        valueSerializer = () => new ValueSerializer()
                        },
                        _keyComparer
                    );

                var logCount = Directory.GetDirectories(checkpointDir).Length;
                if (logCount > 0)
                {
                    KV.Recover();
                }

                KVSession = KV.NewSession();

                var reader = JobQueue.Reader;

                while (await reader.WaitToReadAsync())
                {
                    while (reader.TryRead(out Job item))
                    {
                        ServeJob(item);
                        if (item.JobType == JobTypes.Dispose)
                        {
                            return;
                        }
                    }
                }
            }
            catch (Exception e)
            {
                ExceptionInfo = ExceptionDispatchInfo.Capture(e);
            }
        }