public async Task InitAsync()
        {
            const int snapshotId = 0;

            var fullSnapshot = await InitQueueSnapshot();

            _queueSnapshotCache.Init(fullSnapshot, snapshotId);
            _queueSnapshotWriter.Init(snapshotId);

            var tasks = new List <Task>();

            foreach (var topicAndQueuesSnapshot in fullSnapshot)
            {
                var pageId = MessagesContentPagesUtils.GetPageId(topicAndQueuesSnapshot.MessageId);

                var task = _schedulerByTopic.ExecuteTaskAsync(topicAndQueuesSnapshot.TopicId, pageId, "Init", async() =>
                {
                    await _restorePageFromBlobOperation.TryRestoreFromUncompressedPage(topicAndQueuesSnapshot.TopicId, pageId);
                });

                tasks.Add(task);
            }

            await Task.WhenAll(tasks);

            _appGlobalFlags.Initialized = true;

            _appLogger.AddLog(LogProcess.System, null, "SYSTEM", "Application Initialized");
        }
Exemple #2
0
        public async ValueTask Sync()
        {
            if (!_appGlobalFlags.Initialized)
            {
                return;
            }

            var topics = _messagesContentCache.GetLoadedPages();



            foreach (var(topicId, pages) in topics)
            {
                if (_appGlobalFlags.DebugTopic == topicId)
                {
                    _appLogger.AddLog(LogProcess.Debug, topicId, "Sync Topic Event", $"Found pages {pages.Count} to sync ");
                }


                foreach (var page in pages)
                {
                    if (page is WritableContentPage {
                        NotSavedAmount: > 0
                    } writableContentCachePage)
                    {
                        if (_appGlobalFlags.DebugTopic == topicId)
                        {
                            _appLogger.AddLog(LogProcess.Debug, topicId, $"Sync Page {page.PageId}", $"Page with hash {page.GetHashCode()} has {page.NotSavedAmount} unsynched messages");
                        }

                        await _taskSchedulerByTopic.ExecuteTaskAsync(topicId, page.PageId, "Upload messages to blob", async() =>
                        {
                            if (_appGlobalFlags.DebugTopic == topicId)
                            {
                                _appLogger.AddLog(LogProcess.Debug, topicId, $"Sync Page {page.PageId}", "Start uploading messages");
                            }

                            var result = await _messagesContentPersistentStorage.SyncAsync(topicId, page.PageId);


                            if (_appGlobalFlags.DebugTopic == topicId)
                            {
                                _appLogger.AddLog(LogProcess.Debug, topicId, $"Sync Page {page.PageId}", $"Upload result is {result}. Messages after upload {page.NotSavedAmount}");
                            }


                            while (result != SyncResult.Done)
                            {
                                _appLogger.AddLog(LogProcess.PagesLoaderOrGc, topicId, "PageId: " + page.PageId.Value,
                                                  "There are messages to upload but no writer found. Creating one...");
                                await _messagesContentPersistentStorage.CreateNewPageAsync(topicId, page.PageId, () => writableContentCachePage);
                                result = await _messagesContentPersistentStorage.SyncAsync(topicId, page.PageId);
                            }
                        });
                    }
        public async ValueTask <IMessageContentPage> TryGetPageAsync(string topicId, MessagePageId pageId, string reason)
        {
            var page = _messagesContentCache.TryGetPage(topicId, pageId);

            if (page != null)
            {
                return(page);
            }


            IMessageContentPage result = null;


            await _taskSchedulerByTopic.ExecuteTaskAsync(topicId, pageId, "Getting page: " + reason, async() =>
            {
                result = await TryGetPageTopicThreadSynchronizedAsync(topicId, pageId);
            });

            return(result);
        }