Esempio n. 1
0
        public static void RunTableMergePass(KeyValueStore kvStore)
        {
            try {
                Interlocked.Increment(ref kvStore.mergeCount);

                lock (kvStore.mergeLock) {
                    RazorCache cache    = kvStore.Cache;
                    Manifest   manifest = kvStore.Manifest;

                    while (true)
                    {
                        bool mergedDuringLastPass = false;
                        using (var manifestInst = kvStore.Manifest.GetLatestManifest()) {
                            // Handle level 0 (merge all pages)
                            if (manifestInst.GetNumPagesAtLevel(0) >= Config.MaxPagesOnLevel(0))
                            {
                                mergedDuringLastPass = true;
                                var inputPageRecords = manifestInst.GetPagesAtLevel(0).OrderBy(p => p.Version).ToList();
                                var startKey         = inputPageRecords.Min(p => p.FirstKey);
                                var endKey           = inputPageRecords.Max(p => p.LastKey);
                                var mergePages       = manifestInst.FindPagesForKeyRange(1, startKey, endKey).AsPageRefs().ToList();
                                var allInputPages    = inputPageRecords.AsPageRefs().Concat(mergePages).ToList();

                                var outputPages = SortedBlockTable.MergeTables(cache, manifest, 1, allInputPages, ExceptionHandling.ThrowAll, null).ToList();
                                manifest.ModifyPages(outputPages, allInputPages);

                                manifest.LogMessage("Merge Level 0 => InputPages: {0} OutputPages:{1}",
                                                    string.Join(",", allInputPages.Select(p => string.Format("{0}-{1}", p.Level, p.Version)).ToArray()),
                                                    string.Join(",", outputPages.Select(p => string.Format("{0}-{1}", p.Level, p.Version)).ToArray())
                                                    );
                            }
                            // handle the rest of the levels (merge only one page upwards)
                            for (int level = 1; level < manifestInst.NumLevels - 1; level++)
                            {
                                if (manifestInst.GetNumPagesAtLevel(level) >= Config.MaxPagesOnLevel(level))
                                {
                                    mergedDuringLastPass = true;
                                    var inputPage        = manifest.NextMergePage(level);
                                    var mergePages       = manifestInst.FindPagesForKeyRange(level + 1, inputPage.FirstKey, inputPage.LastKey).ToList();
                                    var inputPageRecords = mergePages.Concat(new PageRecord[] { inputPage });
                                    var allInputPages    = inputPageRecords.AsPageRefs().ToList();
                                    var outputPages      = SortedBlockTable.MergeTables(cache, manifest, level + 1, allInputPages, ExceptionHandling.ThrowAll, null);

                                    // Notify if a merge happened, implemented for testing primarily
                                    if (kvStore.MergeCallback != null)
                                    {
                                        kvStore.MergeCallback(level, inputPageRecords, outputPages);
                                    }
                                    manifest.ModifyPages(outputPages, allInputPages);

                                    manifest.LogMessage("Merge Level >0 => InputPages: {0} OutputPages:{1}",
                                                        string.Join(",", allInputPages.Select(p => string.Format("{0}-{1}", p.Level, p.Version)).ToArray()),
                                                        string.Join(",", outputPages.Select(p => string.Format("{0}-{1}", p.Level, p.Version)).ToArray())
                                                        );
                                }
                            }
                        }

                        // No more merging is needed, we are finished with this pass
                        if (!mergedDuringLastPass)
                        {
                            return;
                        }
                    }
                }
            } finally {
                Interlocked.Decrement(ref kvStore.mergeCount);
            }
        }
Esempio n. 2
0
        public static void RunTableMergePass(KeyValueStore kvStore)
        {
            try {
                Interlocked.Increment(ref kvStore.mergeCount);

                lock (kvStore.mergeLock) {
                    RazorCache cache = kvStore.Cache;
                    Manifest manifest = kvStore.Manifest;

                    while (true) {
                        bool mergedDuringLastPass = false;
                        using (var manifestInst = kvStore.Manifest.GetLatestManifest()) {
                            // Handle level 0 (merge all pages)
                            if (manifestInst.GetNumPagesAtLevel(0) >= Config.MaxPagesOnLevel(0)) {
                                mergedDuringLastPass = true;
                                var inputPageRecords = manifestInst.GetPagesAtLevel(0).OrderBy(p => p.Version).ToList();
                                var startKey = inputPageRecords.Min(p => p.FirstKey);
                                var endKey = inputPageRecords.Max(p => p.LastKey);
                                var mergePages = manifestInst.FindPagesForKeyRange(1, startKey, endKey).AsPageRefs().ToList();
                                var allInputPages = inputPageRecords.AsPageRefs().Concat(mergePages).ToList();

                                var outputPages = SortedBlockTable.MergeTables(cache, manifest, 1, allInputPages, ExceptionHandling.ThrowAll, null).ToList();
                                manifest.ModifyPages(outputPages, allInputPages);

                                manifest.LogMessage("Merge Level 0 => InputPages: {0} OutputPages:{1}",
                                    string.Join(",", allInputPages.Select(p => string.Format("{0}-{1}", p.Level, p.Version)).ToArray()),
                                    string.Join(",", outputPages.Select(p => string.Format("{0}-{1}", p.Level, p.Version)).ToArray())
                                );
                            }
                            // handle the rest of the levels (merge only one page upwards)
                            for (int level = 1; level < manifestInst.NumLevels - 1; level++) {
                                if (manifestInst.GetNumPagesAtLevel(level) >= Config.MaxPagesOnLevel(level)) {
                                    mergedDuringLastPass = true;
                                    var inputPage = manifest.NextMergePage(level);
                                    var mergePages = manifestInst.FindPagesForKeyRange(level + 1, inputPage.FirstKey, inputPage.LastKey).ToList();
                                    var inputPageRecords = mergePages.Concat(new PageRecord[] { inputPage });
                                    var allInputPages = inputPageRecords.AsPageRefs().ToList();
                                    var outputPages = SortedBlockTable.MergeTables(cache, manifest, level + 1, allInputPages, ExceptionHandling.ThrowAll, null);

                                    // Notify if a merge happened, implemented for testing primarily
                                    if (kvStore.MergeCallback != null) kvStore.MergeCallback(level, inputPageRecords, outputPages);
                                    manifest.ModifyPages(outputPages, allInputPages);

                                    manifest.LogMessage("Merge Level >0 => InputPages: {0} OutputPages:{1}",
                                        string.Join(",", allInputPages.Select(p => string.Format("{0}-{1}", p.Level, p.Version)).ToArray()),
                                        string.Join(",", outputPages.Select(p => string.Format("{0}-{1}", p.Level, p.Version)).ToArray())
                                    );
                                }
                            }
                        }

                        // No more merging is needed, we are finished with this pass
                        if (!mergedDuringLastPass)
                            return;
                    }
                }
            } finally {
                Interlocked.Decrement(ref kvStore.mergeCount);
            }
        }