all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning

There are some breaking changes in this commit, see #11455.

Closes #11455
Closes #11549

This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.

The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.

A list of the notable new features:

* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.

New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.

This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):

Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.

Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).

Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
This commit is contained in:
Bjørn Erik Pedersen 2023-12-24 19:11:05 +01:00
parent 5fd1e74903
commit 7285e74090
No known key found for this signature in database
437 changed files with 19304 additions and 18384 deletions

View file

@ -14,513 +14,45 @@
package hugolib
import (
"context"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/gohugoio/hugo/resources/kinds"
"github.com/spf13/cast"
qt "github.com/frankban/quicktest"
)
/*
/en/p1.md
/nn/p1.md
.Readdir
- Name() => p1.en.md, p1.nn.md
.Stat(name)
.Open() --- real file name
*/
func TestLanguageContentRoot(t *testing.T) {
t.Parallel()
c := qt.New(t)
config := `
files := `
-- hugo.toml --
baseURL = "https://example.org/"
defaultContentLanguage = "en"
defaultContentLanguageInSubdir = true
contentDir = "content/main"
workingDir = "/my/project"
[Languages]
[Languages.en]
weight = 10
title = "In English"
languageName = "English"
[Languages.nn]
weight = 20
title = "På Norsk"
languageName = "Norsk"
# This tells Hugo that all content in this directory is in the Norwegian language.
# It does not have to have the "my-page.nn.md" format. It can, but that is optional.
contentDir = "content/norsk"
[Languages.sv]
weight = 30
title = "På Svenska"
languageName = "Svensk"
contentDir = "content/svensk"
`
pageTemplate := `
---
title: %s
slug: %s
weight: %d
---
Content.
SVP3-REF: {{< ref path="/sect/page3.md" lang="sv" >}}
SVP3-RELREF: {{< relref path="/sect/page3.md" lang="sv" >}}
`
pageBundleTemplate := `
---
title: %s
weight: %d
---
Content.
`
var contentFiles []string
section := "sect"
contentRoot := func(lang string) string {
switch lang {
case "nn":
return "content/norsk"
case "sv":
return "content/svensk"
default:
return "content/main"
}
}
contentSectionRoot := func(lang string) string {
return contentRoot(lang) + "/" + section
}
for _, lang := range []string{"en", "nn", "sv"} {
for j := 1; j <= 10; j++ {
if (lang == "nn" || lang == "en") && j%4 == 0 {
// Skip 4 and 8 for nn
// We also skip it for en, but that is added to the Swedish directory below.
continue
}
if lang == "sv" && j%5 == 0 {
// Skip 5 and 10 for sv
continue
}
base := fmt.Sprintf("p-%s-%d", lang, j)
slug := base
langID := ""
if lang == "sv" && j%4 == 0 {
// Put an English page in the Swedish content dir.
langID = ".en"
}
if lang == "en" && j == 8 {
// This should win over the sv variant above.
langID = ".en"
}
slug += langID
contentRoot := contentSectionRoot(lang)
filename := filepath.Join(contentRoot, fmt.Sprintf("page%d%s.md", j, langID))
contentFiles = append(contentFiles, filename, fmt.Sprintf(pageTemplate, slug, slug, j))
}
}
// Put common translations in all of them
for i, lang := range []string{"en", "nn", "sv"} {
contentRoot := contentSectionRoot(lang)
slug := fmt.Sprintf("common_%s", lang)
filename := filepath.Join(contentRoot, "common.md")
contentFiles = append(contentFiles, filename, fmt.Sprintf(pageTemplate, slug, slug, 100+i))
for j, lang2 := range []string{"en", "nn", "sv"} {
filename := filepath.Join(contentRoot, fmt.Sprintf("translated_all.%s.md", lang2))
langSlug := slug + "_translated_all_" + lang2
contentFiles = append(contentFiles, filename, fmt.Sprintf(pageTemplate, langSlug, langSlug, 200+i+j))
}
for j, lang2 := range []string{"sv", "nn"} {
if lang == "en" {
continue
}
filename := filepath.Join(contentRoot, fmt.Sprintf("translated_some.%s.md", lang2))
langSlug := slug + "_translated_some_" + lang2
contentFiles = append(contentFiles, filename, fmt.Sprintf(pageTemplate, langSlug, langSlug, 300+i+j))
}
}
// Add a bundle with some images
for i, lang := range []string{"en", "nn", "sv"} {
contentRoot := contentSectionRoot(lang)
slug := fmt.Sprintf("bundle_%s", lang)
filename := filepath.Join(contentRoot, "mybundle", "index.md")
contentFiles = append(contentFiles, filename, fmt.Sprintf(pageBundleTemplate, slug, 400+i))
if lang == "en" {
imageFilename := filepath.Join(contentRoot, "mybundle", "logo.png")
contentFiles = append(contentFiles, imageFilename, "PNG Data")
}
imageFilename := filepath.Join(contentRoot, "mybundle", "featured.png")
contentFiles = append(contentFiles, imageFilename, fmt.Sprintf("PNG Data for %s", lang))
// Add some bundled pages
contentFiles = append(contentFiles, filepath.Join(contentRoot, "mybundle", "p1.md"), fmt.Sprintf(pageBundleTemplate, slug, 401+i))
contentFiles = append(contentFiles, filepath.Join(contentRoot, "mybundle", "sub", "p1.md"), fmt.Sprintf(pageBundleTemplate, slug, 402+i))
}
// Add some static files inside the content dir
// https://github.com/gohugoio/hugo/issues/5759
for _, lang := range []string{"en", "nn", "sv"} {
contentRoot := contentRoot(lang)
for i := 0; i < 2; i++ {
filename := filepath.Join(contentRoot, "mystatic", fmt.Sprintf("file%d.yaml", i))
contentFiles = append(contentFiles, filename, lang)
}
}
b := newTestSitesBuilder(t)
b.WithWorkingDir("/my/project").WithConfigFile("toml", config).WithContent(contentFiles...).CreateSites()
_ = os.Stdout
err := b.BuildE(BuildCfg{})
// dumpPages(b.H.Sites[1].RegularPages()...)
c.Assert(err, qt.IsNil)
c.Assert(len(b.H.Sites), qt.Equals, 3)
enSite := b.H.Sites[0]
nnSite := b.H.Sites[1]
svSite := b.H.Sites[2]
b.AssertFileContent("public/en/mystatic/file1.yaml", "en")
b.AssertFileContent("public/nn/mystatic/file1.yaml", "nn")
// dumpPages(nnSite.RegularPages()...)
c.Assert(len(nnSite.RegularPages()), qt.Equals, 12)
c.Assert(len(enSite.RegularPages()), qt.Equals, 13)
c.Assert(len(svSite.RegularPages()), qt.Equals, 10)
svP2, err := svSite.getPageNew(nil, "/sect/page2.md")
c.Assert(err, qt.IsNil)
nnP2, err := nnSite.getPageNew(nil, "/sect/page2.md")
c.Assert(err, qt.IsNil)
enP2, err := enSite.getPageNew(nil, "/sect/page2.md")
c.Assert(err, qt.IsNil)
c.Assert(enP2.Language().Lang, qt.Equals, "en")
c.Assert(svP2.Language().Lang, qt.Equals, "sv")
c.Assert(nnP2.Language().Lang, qt.Equals, "nn")
content, _ := nnP2.Content(context.Background())
contentStr := cast.ToString(content)
c.Assert(contentStr, qt.Contains, "SVP3-REF: https://example.org/sv/sect/p-sv-3/")
c.Assert(contentStr, qt.Contains, "SVP3-RELREF: /sv/sect/p-sv-3/")
// Test RelRef with and without language indicator.
nn3RefArgs := map[string]any{
"path": "/sect/page3.md",
"lang": "nn",
}
nnP3RelRef, err := svP2.RelRef(
nn3RefArgs,
)
c.Assert(err, qt.IsNil)
c.Assert(nnP3RelRef, qt.Equals, "/nn/sect/p-nn-3/")
nnP3Ref, err := svP2.Ref(
nn3RefArgs,
)
c.Assert(err, qt.IsNil)
c.Assert(nnP3Ref, qt.Equals, "https://example.org/nn/sect/p-nn-3/")
for i, p := range enSite.RegularPages() {
j := i + 1
c.Assert(p.Language().Lang, qt.Equals, "en")
c.Assert(p.Section(), qt.Equals, "sect")
if j < 9 {
if j%4 == 0 {
} else {
c.Assert(p.Title(), qt.Contains, "p-en")
}
}
}
for _, p := range nnSite.RegularPages() {
c.Assert(p.Language().Lang, qt.Equals, "nn")
c.Assert(p.Title(), qt.Contains, "nn")
}
for _, p := range svSite.RegularPages() {
c.Assert(p.Language().Lang, qt.Equals, "sv")
c.Assert(p.Title(), qt.Contains, "sv")
}
// Check bundles
bundleEn := enSite.RegularPages()[len(enSite.RegularPages())-1]
bundleNn := nnSite.RegularPages()[len(nnSite.RegularPages())-1]
bundleSv := svSite.RegularPages()[len(svSite.RegularPages())-1]
c.Assert(bundleEn.RelPermalink(), qt.Equals, "/en/sect/mybundle/")
c.Assert(bundleSv.RelPermalink(), qt.Equals, "/sv/sect/mybundle/")
c.Assert(len(bundleNn.Resources()), qt.Equals, 4)
c.Assert(len(bundleSv.Resources()), qt.Equals, 4)
c.Assert(len(bundleEn.Resources()), qt.Equals, 4)
b.AssertFileContent("public/en/sect/mybundle/index.html", "image/png: /en/sect/mybundle/logo.png")
b.AssertFileContent("public/nn/sect/mybundle/index.html", "image/png: /nn/sect/mybundle/logo.png")
b.AssertFileContent("public/sv/sect/mybundle/index.html", "image/png: /sv/sect/mybundle/logo.png")
b.AssertFileContent("public/sv/sect/mybundle/featured.png", "PNG Data for sv")
b.AssertFileContent("public/nn/sect/mybundle/featured.png", "PNG Data for nn")
b.AssertFileContent("public/en/sect/mybundle/featured.png", "PNG Data for en")
b.AssertFileContent("public/en/sect/mybundle/logo.png", "PNG Data")
b.AssertFileContent("public/sv/sect/mybundle/logo.png", "PNG Data")
b.AssertFileContent("public/nn/sect/mybundle/logo.png", "PNG Data")
nnSect := nnSite.getPage(kinds.KindSection, "sect")
c.Assert(nnSect, qt.Not(qt.IsNil))
c.Assert(len(nnSect.Pages()), qt.Equals, 12)
nnHome := nnSite.Home()
c.Assert(nnHome.RelPermalink(), qt.Equals, "/nn/")
}
// https://github.com/gohugoio/hugo/issues/6463
func TestLanguageRootSectionsMismatch(t *testing.T) {
t.Parallel()
config := `
baseURL: "https://example.org/"
languageCode: "en-us"
title: "My New Hugo Site"
theme: "mytheme"
contentDir: "content/en"
languages:
en:
weight: 1
languageName: "English"
contentDir: content/en
es:
weight: 2
languageName: "Español"
contentDir: content/es
fr:
weight: 4
languageName: "Française"
contentDir: content/fr
`
createPage := func(title string) string {
return fmt.Sprintf(`---
title: %q
---
`, title)
}
b := newTestSitesBuilder(t)
b.WithConfigFile("yaml", config)
b.WithSourceFile("themes/mytheme/layouts/index.html", `MYTHEME`)
b.WithTemplates("index.html", `
Lang: {{ .Lang }}
{{ range .Site.RegularPages }}
Page: {{ .RelPermalink }}|{{ .Title -}}
{{ end }}
`)
b.WithSourceFile("static/hello.txt", `hello`)
b.WithContent("en/_index.md", createPage("en home"))
b.WithContent("es/_index.md", createPage("es home"))
b.WithContent("fr/_index.md", createPage("fr home"))
for i := 1; i < 3; i++ {
b.WithContent(fmt.Sprintf("en/event/page%d.md", i), createPage(fmt.Sprintf("ev-en%d", i)))
b.WithContent(fmt.Sprintf("es/event/page%d.md", i), createPage(fmt.Sprintf("ev-es%d", i)))
b.WithContent(fmt.Sprintf("fr/event/page%d.md", i), createPage(fmt.Sprintf("ev-fr%d", i)))
b.WithContent(fmt.Sprintf("en/blog/page%d.md", i), createPage(fmt.Sprintf("blog-en%d", i)))
b.WithContent(fmt.Sprintf("es/blog/page%d.md", i), createPage(fmt.Sprintf("blog-es%d", i)))
b.WithContent(fmt.Sprintf("fr/other/page%d.md", i), createPage(fmt.Sprintf("other-fr%d", i)))
}
b.Build(BuildCfg{})
b.AssertFileContent("public/index.html", `
Lang: en
Page: /blog/page1/|blog-en1
Page: /blog/page2/|blog-en2
Page: /event/page1/|ev-en1
Page: /event/page2/|ev-en2
`)
b.AssertFileContent("public/es/index.html", `
Lang: es
Page: /es/blog/page1/|blog-es1
Page: /es/blog/page2/|blog-es2
Page: /es/event/page1/|ev-es1
Page: /es/event/page2/|ev-es2
`)
b.AssertFileContent("public/fr/index.html", `
Lang: fr
Page: /fr/event/page1/|ev-fr1
Page: /fr/event/page2/|ev-fr2
Page: /fr/other/page1/|other-fr1
Page: /fr/other/page2/|other-fr2`)
}
// Issue 9693
func TestContentMountMerge(t *testing.T) {
t.Parallel()
files := `
-- config.toml --
baseURL = 'https://example.org/'
languageCode = 'en-us'
title = 'Hugo Forum Topic #37225'
theme = 'mytheme'
disableKinds = ['sitemap','RSS','taxonomy','term']
defaultContentLanguage = 'en'
defaultContentLanguageInSubdir = true
[languages]
[languages.en]
languageName = 'English'
weight = 1
[languages.de]
languageName = 'Deutsch'
weight = 2
[languages.nl]
languageName = 'Nederlands'
weight = 3
# EN content
[[module.mounts]]
source = 'content/en'
target = 'content'
lang = 'en'
# DE content
[[module.mounts]]
source = 'content/de'
target = 'content'
lang = 'de'
# This fills in the gaps in DE content with EN content
[[module.mounts]]
source = 'content/en'
target = 'content'
lang = 'de'
# NL content
[[module.mounts]]
source = 'content/nl'
target = 'content'
lang = 'nl'
# This should fill in the gaps in NL content with EN content
[[module.mounts]]
source = 'content/en'
target = 'content'
lang = 'nl'
-- content/de/_index.md --
---
title: "home (de)"
---
-- content/de/p1.md --
---
title: "p1 (de)"
---
weight = 10
contentDir = "content/en"
[languages.nn]
weight = 20
contentDir = "content/nn"
-- content/en/_index.md --
---
title: "home (en)"
title: "Home"
---
-- content/en/p1.md --
-- content/nn/_index.md --
---
title: "p1 (en)"
---
-- content/en/p2.md --
---
title: "p2 (en)"
---
-- content/en/p3.md --
---
title: "p3 (en)"
---
-- content/nl/_index.md --
---
title: "home (nl)"
---
-- content/nl/p1.md --
---
title: "p1 (nl)"
---
-- content/nl/p3.md --
---
title: "p3 (nl)"
---
-- layouts/home.html --
{{ .Title }}: {{ site.Language.Lang }}: {{ range site.RegularPages }}{{ .Title }}|{{ end }}:END
-- themes/mytheme/config.toml --
[[module.mounts]]
source = 'content/nlt'
target = 'content'
lang = 'nl'
-- themes/mytheme/content/nlt/p3.md --
---
title: "p3 theme (nl)"
---
-- themes/mytheme/content/nlt/p4.md --
---
title: "p4 theme (nl)"
title: "Heim"
---
-- content/en/myfiles/file1.txt --
file 1 en
-- content/en/myfiles/file2.txt --
file 2 en
-- content/nn/myfiles/file1.txt --
file 1 nn
-- layouts/index.html --
Title: {{ .Title }}|
Len Resources: {{ len .Resources }}|
{{ range $i, $e := .Resources }}
{{ $i }}|{{ .RelPermalink }}|{{ .Content }}|
{{ end }}
`
b := NewIntegrationTestBuilder(
IntegrationTestConfig{
T: t,
TxtarString: files,
},
).Build()
b.AssertFileContent("public/nl/index.html", `home (nl): nl: p1 (nl)|p2 (en)|p3 (nl)|p4 theme (nl)|:END`)
b.AssertFileContent("public/de/index.html", `home (de): de: p1 (de)|p2 (en)|p3 (en)|:END`)
b.AssertFileContent("public/en/index.html", `home (en): en: p1 (en)|p2 (en)|p3 (en)|:END`)
b := Test(t, files)
b.AssertFileContent("public/en/index.html", "Home", "0|/en/myfiles/file1.txt|file 1 en|\n\n1|/en/myfiles/file2.txt|file 2 en|")
b.AssertFileContent("public/nn/index.html", "Heim", "0|/nn/myfiles/file1.txt|file 1 nn|\n\n1|/en/myfiles/file2.txt|file 2 en|")
}