Skip to content

Commit 103f9ed

Browse files
committed
internal: remove unit-meta-with-latest experiment
For golang/go#43265 Change-Id: I1d056a893fdff60744ff328ab9f4a1b6665b3e32 Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/309709 Trust: Jonathan Amsterdam <[email protected]> Run-TryBot: Jonathan Amsterdam <[email protected]> TryBot-Result: kokoro <[email protected]> Reviewed-by: Julie Qiu <[email protected]>
1 parent 58432cb commit 103f9ed

File tree

3 files changed

+11
-245
lines changed

3 files changed

+11
-245
lines changed

internal/experiment.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ const (
99
ExperimentInsertSymbols = "insert-symbols"
1010
ExperimentSymbolHistoryVersionsPage = "symbol-history-versions-page"
1111
ExperimentSymbolHistoryMainPage = "symbol-history-main-page"
12-
ExperimentUnitMetaWithLatest = "unit-meta-with-latest"
1312
)
1413

1514
// Experiments represents all of the active experiments in the codebase and
@@ -18,7 +17,6 @@ var Experiments = map[string]string{
1817
ExperimentInsertSymbols: "Insert data into symbols, package_symbols, and documentation_symbols.",
1918
ExperimentSymbolHistoryVersionsPage: "Show package API history on the versions page.",
2019
ExperimentSymbolHistoryMainPage: "Show package API history on the main unit page.",
21-
ExperimentUnitMetaWithLatest: "Use latest-version information for GetUnitMeta.",
2220
}
2321

2422
// Experiment holds data associated with an experimental feature for frontend

internal/postgres/unit.go

Lines changed: 8 additions & 137 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"golang.org/x/pkgsite/internal"
1717
"golang.org/x/pkgsite/internal/database"
1818
"golang.org/x/pkgsite/internal/derrors"
19-
"golang.org/x/pkgsite/internal/experiment"
2019
"golang.org/x/pkgsite/internal/middleware"
2120
"golang.org/x/pkgsite/internal/stdlib"
2221
"golang.org/x/pkgsite/internal/version"
@@ -39,19 +38,16 @@ func (db *DB) GetUnitMeta(ctx context.Context, fullPath, requestedModulePath, re
3938
defer derrors.WrapStack(&err, "DB.GetUnitMeta(ctx, %q, %q, %q)", fullPath, requestedModulePath, requestedVersion)
4039
defer middleware.ElapsedStat(ctx, "GetUnitMeta")()
4140

42-
if experiment.IsActive(ctx, internal.ExperimentUnitMetaWithLatest) {
43-
modulePath := requestedModulePath
44-
version := requestedVersion
45-
var lmv *internal.LatestModuleVersions
46-
if requestedVersion == internal.LatestVersion {
47-
modulePath, version, lmv, err = db.getLatestUnitVersion(ctx, fullPath, requestedModulePath)
48-
if err != nil {
49-
return nil, err
50-
}
41+
modulePath := requestedModulePath
42+
version := requestedVersion
43+
var lmv *internal.LatestModuleVersions
44+
if requestedVersion == internal.LatestVersion {
45+
modulePath, version, lmv, err = db.getLatestUnitVersion(ctx, fullPath, requestedModulePath)
46+
if err != nil {
47+
return nil, err
5148
}
52-
return db.getUnitMetaWithKnownLatestVersion(ctx, fullPath, modulePath, version, lmv)
5349
}
54-
return db.legacyGetUnitMeta(ctx, fullPath, requestedModulePath, requestedVersion)
50+
return db.getUnitMetaWithKnownLatestVersion(ctx, fullPath, modulePath, version, lmv)
5551
}
5652

5753
func (db *DB) getUnitMetaWithKnownLatestVersion(ctx context.Context, fullPath, modulePath, version string, lmv *internal.LatestModuleVersions) (_ *internal.UnitMeta, err error) {
@@ -225,131 +221,6 @@ func (db *DB) getLatestUnitVersion(ctx context.Context, fullPath, requestedModul
225221
return modulePath, latestVersion, nil, nil
226222
}
227223

228-
func (db *DB) legacyGetUnitMeta(ctx context.Context, fullPath, requestedModulePath, requestedVersion string) (_ *internal.UnitMeta, err error) {
229-
defer derrors.WrapStack(&err, "DB.legacyGetUnitMeta(ctx, %q, %q, %q)", fullPath, requestedModulePath, requestedVersion)
230-
231-
var (
232-
q string
233-
args []interface{}
234-
)
235-
q, args, err = getUnitMetaQuery(fullPath, requestedModulePath, requestedVersion).PlaceholderFormat(squirrel.Dollar).ToSql()
236-
if err != nil {
237-
return nil, fmt.Errorf("squirrel.ToSql: %v", err)
238-
}
239-
var (
240-
licenseTypes []string
241-
licensePaths []string
242-
um = internal.UnitMeta{Path: fullPath}
243-
)
244-
err = db.db.QueryRow(ctx, q, args...).Scan(
245-
&um.ModulePath,
246-
&um.Version,
247-
&um.CommitTime,
248-
jsonbScanner{&um.SourceInfo},
249-
&um.HasGoMod,
250-
&um.ModuleInfo.IsRedistributable,
251-
&um.Name,
252-
&um.IsRedistributable,
253-
pq.Array(&licenseTypes),
254-
pq.Array(&licensePaths))
255-
switch err {
256-
case sql.ErrNoRows:
257-
return nil, derrors.NotFound
258-
case nil:
259-
lics, err := zipLicenseMetadata(licenseTypes, licensePaths)
260-
if err != nil {
261-
return nil, err
262-
}
263-
264-
if db.bypassLicenseCheck {
265-
um.IsRedistributable = true
266-
}
267-
268-
um.Licenses = lics
269-
if err := populateLatestInfo(ctx, db, &um.ModuleInfo); err != nil {
270-
return nil, err
271-
}
272-
return &um, nil
273-
default:
274-
return nil, err
275-
}
276-
}
277-
278-
func getUnitMetaQuery(fullPath, requestedModulePath, requestedVersion string) squirrel.SelectBuilder {
279-
query := squirrel.Select(
280-
"m.module_path",
281-
"m.version",
282-
"m.commit_time",
283-
"m.source_info",
284-
"m.has_go_mod",
285-
"m.redistributable",
286-
"u.name",
287-
"u.redistributable",
288-
"u.license_types",
289-
"u.license_paths",
290-
)
291-
if requestedVersion != internal.LatestVersion {
292-
query = query.From("modules m").
293-
Join("units u on u.module_id = m.id").
294-
Join("paths p ON p.id = u.path_id").Where(squirrel.Eq{"p.path": fullPath})
295-
if requestedModulePath != internal.UnknownModulePath {
296-
query = query.Where(squirrel.Eq{"m.module_path": requestedModulePath})
297-
}
298-
if internal.DefaultBranches[requestedVersion] {
299-
query = query.Join("version_map vm ON m.id = vm.module_id").Where("vm.requested_version = ? ", requestedVersion)
300-
} else if requestedVersion != internal.LatestVersion {
301-
query = query.Where(squirrel.Eq{"version": requestedVersion})
302-
}
303-
return orderByLatest(query).Limit(1)
304-
}
305-
306-
// Use a nested select to fetch the latest version of the unit, then JOIN
307-
// on units to fetch other relevant information. This allows us to use the
308-
// index on units.id and paths.path to get the latest path. We can then
309-
// look up only the relevant information from the units table.
310-
nestedSelect := orderByLatest(squirrel.Select(
311-
"m.id",
312-
"m.module_path",
313-
"m.version",
314-
"m.commit_time",
315-
"m.source_info",
316-
"m.has_go_mod",
317-
"m.redistributable",
318-
"u.id AS unit_id",
319-
).From("modules m").
320-
Join("units u ON u.module_id = m.id").
321-
Join("paths p ON p.id = u.path_id").
322-
Where(squirrel.Eq{"p.path": fullPath}))
323-
if requestedModulePath != internal.UnknownModulePath {
324-
nestedSelect = nestedSelect.Where(squirrel.Eq{"m.module_path": requestedModulePath})
325-
}
326-
nestedSelect = nestedSelect.Limit(1)
327-
return query.From("units u").JoinClause(nestedSelect.Prefix("JOIN (").Suffix(") m ON u.id = m.unit_id"))
328-
}
329-
330-
// orderByLatest orders paths according to the go command.
331-
// Versions are ordered by:
332-
// (1) release (non-incompatible)
333-
// (2) prerelease (non-incompatible)
334-
// (3) release, incompatible
335-
// (4) prerelease, incompatible
336-
// (5) pseudo
337-
// They are then sorted based on semver, then decreasing module path length (so
338-
// that nested modules are preferred).
339-
func orderByLatest(q squirrel.SelectBuilder) squirrel.SelectBuilder {
340-
return q.OrderBy(
341-
`CASE
342-
WHEN m.version_type = 'release' AND NOT m.incompatible THEN 1
343-
WHEN m.version_type = 'prerelease' AND NOT m.incompatible THEN 2
344-
WHEN m.version_type = 'release' THEN 3
345-
WHEN m.version_type = 'prerelease' THEN 4
346-
ELSE 5
347-
END`,
348-
"m.series_path DESC",
349-
"m.sort_version DESC",
350-
).PlaceholderFormat(squirrel.Dollar)
351-
}
352-
353224
// GetUnit returns a unit from the database, along with all of the data
354225
// associated with that unit.
355226
func (db *DB) GetUnit(ctx context.Context, um *internal.UnitMeta, fields internal.FieldSet) (_ *internal.Unit, err error) {

internal/postgres/unit_test.go

Lines changed: 3 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,7 @@ func TestGetUnitMeta(t *testing.T) {
2626
t.Parallel()
2727
ctx, cancel := context.WithTimeout(context.Background(), testTimeout*2)
2828
defer cancel()
29-
t.Run("legacy", func(t *testing.T) {
30-
testGetUnitMeta(t, ctx)
31-
})
32-
t.Run("latest", func(t *testing.T) {
33-
testGetUnitMeta(t, experiment.NewContext(ctx, internal.ExperimentUnitMetaWithLatest))
34-
})
29+
testGetUnitMeta(t, ctx)
3530
}
3631

3732
func testGetUnitMeta(t *testing.T, ctx context.Context) {
@@ -207,104 +202,6 @@ func testGetUnitMeta(t *testing.T, ctx context.Context) {
207202
}
208203
}
209204

210-
func TestGetUnitMetaDiffs(t *testing.T) {
211-
// Demonstrate differences between legacy and latest-version GetUnitMeta
212-
// implementations.
213-
214-
t.Parallel()
215-
216-
type latest struct { // latest-version info
217-
module string
218-
version string // latest raw and cooked version
219-
goMod string // go.mod file contents after "module" line
220-
}
221-
222-
modver := func(u *internal.UnitMeta) string { return u.ModulePath + "@" + u.Version }
223-
224-
for _, test := range []struct {
225-
name string
226-
packages []string // mod@ver/pkg
227-
latests []latest
228-
path string
229-
wantLatest, wantLegacy string
230-
}{
231-
{
232-
name: "incompatible",
233-
// When there are incompatible versions and no go.mod at the latest
234-
// compatible version, the go command selects the highest
235-
// incompatible version, but legacy GetUnitMeta selects the highest
236-
// compatible version.
237-
packages: []string{
238-
239-
"[email protected]+incompatible/a",
240-
},
241-
latests: []latest{{"m.com", "v2.0.0+incompatible", ""}},
242-
path: "m.com/a",
243-
wantLatest: "[email protected]+incompatible",
244-
wantLegacy: "[email protected]",
245-
},
246-
{
247-
name: "shorter",
248-
// The go command prefers the longer path if both have latest-version information,
249-
// but legacy GetUnitMeta prefers the shorter path if it has a release version.
250-
packages: []string{
251-
"[email protected]/a/b", // shorter path, release version
252-
"m.com/[email protected]/b", // longer path, pre-release version
253-
},
254-
latests: []latest{{"m.com", "v1.0.0", ""}, {"m.com/a", "v1.0.0-pre", ""}},
255-
path: "m.com/a/b",
256-
wantLatest: "m.com/[email protected]",
257-
wantLegacy: "[email protected]",
258-
},
259-
{
260-
name: "retraction",
261-
// Legacy GetUnitMeta ignores retractions when picking the latest version.
262-
packages: []string{
263-
264-
"[email protected]/a", // latest, also retracted
265-
},
266-
latests: []latest{{"m.com", "v1.1.0", "retract v1.1.0"}},
267-
path: "m.com/a",
268-
wantLatest: "[email protected]",
269-
wantLegacy: "[email protected]",
270-
},
271-
} {
272-
t.Run(test.name, func(t *testing.T) {
273-
ctx := context.Background()
274-
testDB, release := acquire(t)
275-
defer release()
276-
277-
for _, p := range test.packages {
278-
mod, ver, pkg := parseModuleVersionPackage(p)
279-
m := sample.Module(mod, ver, pkg)
280-
goMod := "module " + mod
281-
for _, l := range test.latests {
282-
if l.module == mod && l.version == ver {
283-
goMod += "\n" + l.goMod
284-
break
285-
}
286-
}
287-
MustInsertModuleGoMod(ctx, t, testDB, m, goMod)
288-
}
289-
gotLegacy, err := testDB.GetUnitMeta(ctx, test.path, internal.UnknownModulePath, internal.LatestVersion)
290-
if err != nil {
291-
t.Fatal(err)
292-
}
293-
if got := modver(gotLegacy); got != test.wantLegacy {
294-
t.Errorf("legacy: got %s, want %s", got, test.wantLegacy)
295-
}
296-
gotLatest, err := testDB.GetUnitMeta(experiment.NewContext(ctx, internal.ExperimentUnitMetaWithLatest),
297-
test.path, internal.UnknownModulePath, internal.LatestVersion)
298-
if err != nil {
299-
t.Fatal(err)
300-
}
301-
if got := modver(gotLatest); got != test.wantLatest {
302-
t.Errorf("latest: got %s, want %s", got, test.wantLatest)
303-
}
304-
})
305-
}
306-
}
307-
308205
func TestGetUnitMetaBypass(t *testing.T) {
309206
t.Parallel()
310207
testDB, release := acquire(t)
@@ -318,13 +215,13 @@ func TestGetUnitMetaBypass(t *testing.T) {
318215
module, version, packageSuffix string
319216
isMaster bool
320217
}{
218+
{"m.com", "v2.0.0+incompatible", "a", false},
219+
{"m.com/b", "v2.0.0+incompatible", "a", true},
321220
{"m.com", "v1.0.0", "a", false},
322221
{"m.com", "v1.0.1", "dir/a", false},
323222
{"m.com", "v1.1.0", "a/b", false},
324223
{"m.com", "v1.2.0-pre", "a", true},
325-
{"m.com", "v2.0.0+incompatible", "a", false},
326224
{"m.com/a", "v1.1.0", "b", false},
327-
{"m.com/b", "v2.0.0+incompatible", "a", true},
328225
} {
329226
m := sample.Module(testModule.module, testModule.version, testModule.packageSuffix)
330227
makeModuleNonRedistributable(m)

0 commit comments

Comments
 (0)