Esempio n. 1
0
        // The odd three-stage initialization improves our chances of correctly tearing down the `LightningEnvironment`s within
        // `LogBuffer`s in the event of a failure during start-up. See: https://github.com/CoreyKaylor/Lightning.NET/blob/master/src/LightningDB/LightningEnvironment.cs#L252
        public void Load()
        {
            // At startup, we look for buffers and either delete them if they're empty, or load them
            // up if they're not. This garbage collection at start-up is a simplification,
            // we might try cleaning up in the background if the gains are worthwhile, although more synchronization
            // would be required.

            lock (_sync)
            {
                Directory.CreateDirectory(_bufferPath);

                var defaultDataFilePath = Path.Combine(_bufferPath, DataFileName);
                if (File.Exists(defaultDataFilePath))
                {
                    _log.Information("Loading the default log buffer in {Path}", _bufferPath);
                    var buffer = new LogBuffer(_bufferPath, _bufferSizeBytes);
                    if (buffer.Peek(0).Length == 0)
                    {
                        _log.Information("The default buffer is empty and will be removed until more data is received");
                        buffer.Dispose();
                        File.Delete(defaultDataFilePath);
                        var lockFilePath = Path.Combine(_bufferPath, LockFileName);
                        if (File.Exists(lockFilePath))
                        {
                            File.Delete(lockFilePath);
                        }
                    }
                    else
                    {
                        _noApiKeyLogBuffer = new ActiveLogBuffer(buffer, _shipperFactory.Create(buffer, _outputConfig.ApiKey));
                    }
                }

                foreach (var subfolder in Directory.GetDirectories(_bufferPath))
                {
                    var encodedApiKeyFilePath = Path.Combine(subfolder, ApiKeyFileName);
                    if (!File.Exists(encodedApiKeyFilePath))
                    {
                        _log.Information("Folder {Path} does not appear to be a log buffer; skipping", subfolder);
                        continue;
                    }

                    _log.Information("Loading an API-key specific buffer in {Path}", subfolder);
                    var apiKey = MachineScopeDataProtection.Unprotect(File.ReadAllText(encodedApiKeyFilePath));

                    var buffer = new LogBuffer(subfolder, _bufferSizeBytes);
                    if (buffer.Peek(0).Length == 0)
                    {
                        _log.Information("API key-specific buffer in {Path} is empty and will be removed until more data is received", subfolder);
                        buffer.Dispose();
                        Directory.Delete(subfolder, true);
                    }
                    else
                    {
                        var activeBuffer = new ActiveLogBuffer(buffer, _shipperFactory.Create(buffer, apiKey));
                        _buffersByApiKey.Add(apiKey, activeBuffer);
                    }
                }
            }
        }
Esempio n. 2
0
        public unsafe void LogBufferTest()
        {
            var sw = new Stopwatch();

            LogBuffer l1 = new LogBuffer("../LogBufferTest", 100);
            LogBuffer l2 = new LogBuffer("../LogBufferTest", 100);

            var tcs   = new TaskCompletionSource <int>();
            var tcs2  = new TaskCompletionSource <int>();
            var count = 1000000;

            //var bytes = new byte[500];
            //for (int i = 0; i < 500; i++) {
            //    bytes[i] = (byte)(i % 255);
            //}

            sw.Start();


            var cnt = 0;

            l1.OnAppend += (ptr) =>
            {
                var len     = *(int *)ptr;
                var message = new byte[len];
                Marshal.Copy(ptr, message, 0, len);
                //var lng = BitConverter.ToInt64(message, 0);
                //Assert.AreEqual(cnt, lng);
                if (count - 1 == cnt)
                {
                    tcs.SetResult(cnt);
                }
                cnt++;
            };

            var cnt2 = 0;

            l2.OnAppend += (ptr) => {
                var len     = *(int *)ptr;
                var message = new byte[len];
                Marshal.Copy(ptr, message, 0, len);
                //var lng = BitConverter.ToInt64(message, 0);
                //Assert.AreEqual(cnt, lng);
                if (count - 1 == cnt2)
                {
                    tcs2.SetResult(cnt2);
                }
                cnt2++;
            };


            for (int i = 0; i < count; i++)
            {
                l1.Append((long)i);//BitConverter.GetBytes((long)i))); //
            }

            tcs.Task.Wait();
            tcs2.Task.Wait();
            sw.Stop();

            Console.WriteLine($"Elapsed msec: {sw.ElapsedMilliseconds}");
            l1.Dispose();
            l2.Dispose();
            //Thread.Sleep(100000);
        }