Files
podman/pkg/domain/entities/container_ps.go
Paul Holzinger 0136a66a83 libpod: deduplicate ports in db
The OCICNI port format has one big problem: It does not support ranges.
So if a users forwards a range of 1k ports with podman run -p 1001-2000
we have to store each of the thousand ports individually as array element.
This bloats the db and makes the JSON encoding and decoding much slower.
In many places we already use a better port struct type which supports
ranges, e.g. `pkg/specgen` or the new network interface.

Because of this we have to do many runtime conversions between the two
port formats. If everything uses the new format we can skip the runtime
conversions.

This commit adds logic to replace all occurrences of the old format
with the new one. The database will automatically migrate the ports
to new format when the container config is read for the first time
after the update.

The `ParsePortMapping` function is `pkg/specgen/generate` has been
reworked to better work with the new format. The new logic is able
to deduplicate the given ports. This is necessary the ensure we
store them efficiently in the DB. The new code should also be more
performant than the old one.

To prove that the code is fast enough I added go benchmarks. Parsing
1 million ports took less than 0.5 seconds on my laptop.

Benchmark normalize PortMappings in specgen:
Please note that the 1 million ports are actually 20x 50k ranges
because we cannot have bigger ranges than 65535 ports.
```
$ go test -bench=. -benchmem  ./pkg/specgen/generate/
goos: linux
goarch: amd64
pkg: github.com/containers/podman/v3/pkg/specgen/generate
cpu: Intel(R) Core(TM) i7-10850H CPU @ 2.70GHz
BenchmarkParsePortMappingNoPorts-12             480821532                2.230 ns/op           0 B/op          0 allocs/op
BenchmarkParsePortMapping1-12                      38972             30183 ns/op          131584 B/op          9 allocs/op
BenchmarkParsePortMapping100-12                    18752             60688 ns/op          141088 B/op        315 allocs/op
BenchmarkParsePortMapping1k-12                      3104            331719 ns/op          223840 B/op       3018 allocs/op
BenchmarkParsePortMapping10k-12                      376           3122930 ns/op         1223650 B/op      30027 allocs/op
BenchmarkParsePortMapping1m-12                         3         390869926 ns/op        124593840 B/op   4000624 allocs/op
BenchmarkParsePortMappingReverse100-12             18940             63414 ns/op          141088 B/op        315 allocs/op
BenchmarkParsePortMappingReverse1k-12               3015            362500 ns/op          223841 B/op       3018 allocs/op
BenchmarkParsePortMappingReverse10k-12               343           3318135 ns/op         1223650 B/op      30027 allocs/op
BenchmarkParsePortMappingReverse1m-12                  3         403392469 ns/op        124593840 B/op   4000624 allocs/op
BenchmarkParsePortMappingRange1-12                 37635             28756 ns/op          131584 B/op          9 allocs/op
BenchmarkParsePortMappingRange100-12               39604             28935 ns/op          131584 B/op          9 allocs/op
BenchmarkParsePortMappingRange1k-12                38384             29921 ns/op          131584 B/op          9 allocs/op
BenchmarkParsePortMappingRange10k-12               29479             40381 ns/op          131584 B/op          9 allocs/op
BenchmarkParsePortMappingRange1m-12                  927           1279369 ns/op          143022 B/op        164 allocs/op
PASS
ok      github.com/containers/podman/v3/pkg/specgen/generate    25.492s
```

Benchmark convert old port format to new one:
```
go test -bench=. -benchmem  ./libpod/
goos: linux
goarch: amd64
pkg: github.com/containers/podman/v3/libpod
cpu: Intel(R) Core(TM) i7-10850H CPU @ 2.70GHz
Benchmark_ocicniPortsToNetTypesPortsNoPorts-12          663526126                1.663 ns/op           0 B/op          0 allocs/op
Benchmark_ocicniPortsToNetTypesPorts1-12                 7858082               141.9 ns/op            72 B/op          2 allocs/op
Benchmark_ocicniPortsToNetTypesPorts10-12                2065347               571.0 ns/op           536 B/op          4 allocs/op
Benchmark_ocicniPortsToNetTypesPorts100-12                138478              8641 ns/op            4216 B/op          4 allocs/op
Benchmark_ocicniPortsToNetTypesPorts1k-12                   9414            120964 ns/op           41080 B/op          4 allocs/op
Benchmark_ocicniPortsToNetTypesPorts10k-12                   781           1490526 ns/op          401528 B/op          4 allocs/op
Benchmark_ocicniPortsToNetTypesPorts1m-12                      4         250579010 ns/op        40001656 B/op          4 allocs/op
PASS
ok      github.com/containers/podman/v3/libpod  11.727s
```

Signed-off-by: Paul Holzinger <pholzing@redhat.com>
2021-10-27 18:59:56 +02:00

201 lines
5.2 KiB
Go

package entities
import (
"sort"
"strings"
"time"
"github.com/containers/podman/v3/libpod/network/types"
"github.com/containers/podman/v3/pkg/ps/define"
"github.com/pkg/errors"
)
// ListContainer describes a container suitable for listing
type ListContainer struct {
// AutoRemove
AutoRemove bool
// Container command
Command []string
// Container creation time
Created time.Time
// Human-readable container creation time.
CreatedAt string
// If container has exited/stopped
Exited bool
// Time container exited
ExitedAt int64
// If container has exited, the return code from the command
ExitCode int32
// The unique identifier for the container
ID string `json:"Id"`
// Container image
Image string
// Container image ID
ImageID string
// If this container is a Pod infra container
IsInfra bool
// Labels for container
Labels map[string]string
// User volume mounts
Mounts []string
// The names assigned to the container
Names []string
// Namespaces the container belongs to. Requires the
// namespace boolean to be true
Namespaces ListContainerNamespaces
// The network names assigned to the container
Networks []string
// The process id of the container
Pid int
// If the container is part of Pod, the Pod ID. Requires the pod
// boolean to be set
Pod string
// If the container is part of Pod, the Pod name. Requires the pod
// boolean to be set
PodName string
// Port mappings
Ports []types.PortMapping
// Size of the container rootfs. Requires the size boolean to be true
Size *define.ContainerSize
// Time when container started
StartedAt int64
// State of container
State string
// Status is a human-readable approximation of a duration for json output
Status string
}
// ListContainerNamespaces contains the identifiers of the container's Linux namespaces
type ListContainerNamespaces struct {
// Mount namespace
MNT string `json:"Mnt,omitempty"`
// Cgroup namespace
Cgroup string `json:"Cgroup,omitempty"`
// IPC namespace
IPC string `json:"Ipc,omitempty"`
// Network namespace
NET string `json:"Net,omitempty"`
// PID namespace
PIDNS string `json:"Pidns,omitempty"`
// UTS namespace
UTS string `json:"Uts,omitempty"`
// User namespace
User string `json:"User,omitempty"`
}
type SortListContainers []ListContainer
func (a SortListContainers) Len() int { return len(a) }
func (a SortListContainers) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
type psSortedCommand struct{ SortListContainers }
func (a psSortedCommand) Less(i, j int) bool {
return strings.Join(a.SortListContainers[i].Command, " ") < strings.Join(a.SortListContainers[j].Command, " ")
}
type psSortedID struct{ SortListContainers }
func (a psSortedID) Less(i, j int) bool {
return a.SortListContainers[i].ID < a.SortListContainers[j].ID
}
type psSortedImage struct{ SortListContainers }
func (a psSortedImage) Less(i, j int) bool {
return a.SortListContainers[i].Image < a.SortListContainers[j].Image
}
type psSortedNames struct{ SortListContainers }
func (a psSortedNames) Less(i, j int) bool {
return a.SortListContainers[i].Names[0] < a.SortListContainers[j].Names[0]
}
type psSortedPod struct{ SortListContainers }
func (a psSortedPod) Less(i, j int) bool {
return a.SortListContainers[i].Pod < a.SortListContainers[j].Pod
}
type psSortedRunningFor struct{ SortListContainers }
func (a psSortedRunningFor) Less(i, j int) bool {
return a.SortListContainers[i].StartedAt < a.SortListContainers[j].StartedAt
}
type psSortedStatus struct{ SortListContainers }
func (a psSortedStatus) Less(i, j int) bool {
return a.SortListContainers[i].State < a.SortListContainers[j].State
}
type psSortedSize struct{ SortListContainers }
func (a psSortedSize) Less(i, j int) bool {
if a.SortListContainers[i].Size == nil || a.SortListContainers[j].Size == nil {
return false
}
return a.SortListContainers[i].Size.RootFsSize < a.SortListContainers[j].Size.RootFsSize
}
type PsSortedCreateTime struct{ SortListContainers }
func (a PsSortedCreateTime) Less(i, j int) bool {
return a.SortListContainers[i].Created.Before(a.SortListContainers[j].Created)
}
func SortPsOutput(sortBy string, psOutput SortListContainers) (SortListContainers, error) {
switch sortBy {
case "id":
sort.Sort(psSortedID{psOutput})
case "image":
sort.Sort(psSortedImage{psOutput})
case "command":
sort.Sort(psSortedCommand{psOutput})
case "runningfor":
sort.Sort(psSortedRunningFor{psOutput})
case "status":
sort.Sort(psSortedStatus{psOutput})
case "size":
sort.Sort(psSortedSize{psOutput})
case "names":
sort.Sort(psSortedNames{psOutput})
case "created":
sort.Sort(PsSortedCreateTime{psOutput})
case "pod":
sort.Sort(psSortedPod{psOutput})
default:
return nil, errors.Errorf("invalid option for --sort, options are: command, created, id, image, names, runningfor, size, or status")
}
return psOutput, nil
}
func (l ListContainer) CGROUPNS() string {
return l.Namespaces.Cgroup
}
func (l ListContainer) IPC() string {
return l.Namespaces.IPC
}
func (l ListContainer) MNT() string {
return l.Namespaces.MNT
}
func (l ListContainer) NET() string {
return l.Namespaces.NET
}
func (l ListContainer) PIDNS() string {
return l.Namespaces.PIDNS
}
func (l ListContainer) USERNS() string {
return l.Namespaces.User
}
func (l ListContainer) UTS() string {
return l.Namespaces.UTS
}