Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce memory footprint by reordering struct elements #804

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ type stateStore interface {
// Unenroll results in running agent entering idle state, non managed non standalone.
// For it to be operational again it needs to be either enrolled or reconfigured.
type Unenroll struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct with 72 pointer bytes could be 56

dispatcher pipeline.Router
stateStore stateStore
log *logger.Logger
emitter pipeline.EmitterFunc
dispatcher pipeline.Router
closers []context.CancelFunc
stateStore stateStore
}

// NewUnenroll creates a new Unenroll handler.
Expand Down Expand Up @@ -75,6 +75,7 @@ func (h *Unenroll) Handle(ctx context.Context, a fleetapi.Action, acker store.Fl
} else if h.stateStore != nil {
// backup action for future start to avoid starting fleet gateway loop
h.stateStore.Add(a)
// nolint: errcheck // Ignore the error at this point.
h.stateStore.Save()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linter complained: Error return value of h.stateStore.Save is not checked (errcheck)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It introduces quite a change on the method's behaviour. @ph do you think it might cause any undesired side effect?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do you prefer a // nolint: errcheck instruction?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets do the nolint here, I am not sure of the impact of this changes here, wdyt @michel-laterman ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that currently state.Action can be either unenroll or policy-change. (this is different from the action queue part I added).

I think if the action fails to persist, then the agent will try to start with the persisted policy if it's restarted. But at that point it's api key has likely been revoked.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated the PR to use the nolint instruction.

}

Expand Down
10 changes: 5 additions & 5 deletions internal/pkg/agent/application/upgrade/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,15 @@ var (

// Upgrader performs an upgrade
type Upgrader struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct with 120 pointer bytes could be 96

agentInfo *info.AgentInfo
reporter stateReporter
caps capabilities.Capability
reexec reexecManager
acker acker
settings *artifact.Config
agentInfo *info.AgentInfo
log *logger.Logger
closers []context.CancelFunc
reexec reexecManager
acker acker
reporter stateReporter
upgradeable bool
caps capabilities.Capability
}

// Action is the upgrade action state.
Expand Down
2 changes: 1 addition & 1 deletion internal/pkg/agent/operation/operation_retryable.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ import (
// if nth operation fails all preceding are retried as well
type retryableOperations struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct with 40 pointer bytes could be 24

logger *logger.Logger
operations []operation
retryConfig *retry.Config
operations []operation
}

func newRetryableOperations(
Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/agent/stateresolver/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ func (s *state) String() string {
}

type active struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct with 272 pointer bytes could be 256

LastChange stateChange
Program program.Program
LastModified time.Time
Identifier string
Program program.Program
LastChange stateChange
}

func (s *active) String() string {
Expand Down
12 changes: 6 additions & 6 deletions internal/pkg/composable/providers/kubernetes/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ import (
)

type pod struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct with 120 pointer bytes could be 104

logger *logp.Logger
cleanupTimeout time.Duration
comm composable.DynamicProviderComm
scope string
config *Config
metagen metadata.MetaGen
watcher kubernetes.Watcher
nodeWatcher kubernetes.Watcher
comm composable.DynamicProviderComm
metagen metadata.MetaGen
namespaceWatcher kubernetes.Watcher
config *Config
logger *logp.Logger
scope string
cleanupTimeout time.Duration

// Mutex used by configuration updates not triggered by the main watcher,
// to avoid race conditions between cross updates and deletions.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import "github.com/elastic/elastic-agent-autodiscover/kubernetes"

// Config for kubernetes_leaderelection provider
type Config struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct with 40 pointer bytes could be 24

KubeConfig string `config:"kube_config"`
KubeClientOptions kubernetes.KubeClientOptions `config:"kube_client_options"`
KubeConfig string `config:"kube_config"`

// Name of the leaderelection lease
LeaderLease string `config:"leader_lease"`

KubeClientOptions kubernetes.KubeClientOptions `config:"kube_client_options"`
}

// InitDefaults initializes the default values for the config.
Expand Down
53 changes: 26 additions & 27 deletions internal/pkg/core/plugin/process/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,32 @@ var (

// Application encapsulates a concrete application ran by elastic-agent e.g Beat.
type Application struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct with 296 pointer bytes could be 264

bgContext context.Context
id string
name string
pipelineID string
logLevel string
desc *app.Descriptor
srv *server.Server
srvState *server.ApplicationState
limiter *tokenbucket.Bucket
startContext context.Context
tag app.Taggable
state state.State
reporter state.Reporter
watchClosers map[int]context.CancelFunc
state state.State
startContext context.Context
statusReporter status.Reporter
monitor monitoring.Monitor
reporter state.Reporter
tag app.Taggable
bgContext context.Context
srvState *server.ApplicationState
limiter *tokenbucket.Bucket
srv *server.Server
desc *app.Descriptor
restartCanceller context.CancelFunc
logger *logger.Logger
watchClosers map[int]context.CancelFunc
processConfig *process.Config
restartConfig map[string]interface{}

name string
id string
pipelineID string
logLevel string

uid int
gid int

monitor monitoring.Monitor
statusReporter status.Reporter

processConfig *process.Config

logger *logger.Logger

appLock sync.Mutex
restartCanceller context.CancelFunc
restartConfig map[string]interface{}
appLock sync.Mutex
}

// ArgsDecorator decorates arguments before calling an application
Expand All @@ -79,8 +77,8 @@ func NewApplication(
logger *logger.Logger,
reporter state.Reporter,
monitor monitoring.Monitor,
statusController status.Controller) (*Application, error) {

statusController status.Controller,
) (*Application, error) {
s := desc.ProcessSpec()
uid, gid, err := s.UserGroup()
if err != nil {
Expand Down Expand Up @@ -157,7 +155,6 @@ func (a *Application) Stop() {

a.logger.Error(err)
}

}

a.appLock.Lock()
Expand Down Expand Up @@ -231,6 +228,7 @@ func (a *Application) watch(ctx context.Context, p app.Taggable, proc *process.I
a.setState(state.Restarting, msg, nil)

// it was a crash
// nolint: errcheck // Ignore the error at this point.
a.start(ctx, p, cfg, true)
florianl marked this conversation as resolved.
Show resolved Hide resolved
}()
}
Expand Down Expand Up @@ -277,6 +275,7 @@ func (a *Application) setState(s state.Status, msg string, payload map[string]in
}

func (a *Application) cleanUp() {
// nolint: errcheck // Ignore the error at this point.
a.monitor.Cleanup(a.desc.Spec(), a.pipelineID)
florianl marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down
4 changes: 2 additions & 2 deletions internal/pkg/core/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ func FromProto(s proto.StateObserved_Status) Status {
// State wraps the process state and application status.
type State struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct with 40 pointer bytes could be 24

ProcessInfo *process.Info
Status Status
Message string
Payload map[string]interface{}
Message string
Status Status
}

// Reporter is interface that is called when a state is changed.
Expand Down
28 changes: 14 additions & 14 deletions internal/pkg/core/status/reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,19 +39,19 @@ func (s AgentStatusCode) String() string {

// AgentApplicationStatus returns the status of specific application.
type AgentApplicationStatus struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct with 64 pointer bytes could be 48

Payload map[string]interface{}
ID string
Name string
Status state.Status
Message string
Payload map[string]interface{}
Status state.Status
}

// AgentStatus returns the overall status of the Elastic Agent.
type AgentStatus struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct with 72 pointer bytes could be 48

Status AgentStatusCode
UpdateTime time.Time
Message string
Applications []AgentApplicationStatus
UpdateTime time.Time
Status AgentStatusCode
}

// Controller takes track of component statuses.
Expand All @@ -68,15 +68,15 @@ type Controller interface {
}

type controller struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct with 104 pointer bytes could be 88

mx sync.Mutex
status AgentStatusCode
message string
updateTime time.Time
log *logger.Logger
reporters map[string]*reporter
appReporters map[string]*reporter
log *logger.Logger
stateID string
message string
agentID string
status AgentStatusCode
mx sync.Mutex
}

// NewController creates a new reporter.
Expand Down Expand Up @@ -272,15 +272,15 @@ type Reporter interface {
}

type reporter struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct with 80 pointer bytes could be 48

name string
mx sync.Mutex
isPersistent bool
isRegistered bool
status state.Status
message string
payload map[string]interface{}
unregisterFunc func()
notifyChangeFunc func()
message string
name string
status state.Status
mx sync.Mutex
isRegistered bool
isPersistent bool
}

// Update updates the status of a component.
Expand Down
8 changes: 4 additions & 4 deletions internal/pkg/crypto/io.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ import (
// Option is the default options used to generate the encrypt and decrypt writer.
// NOTE: the defined options need to be same for both the Reader and the writer.
type Option struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct with 40 pointer bytes could be 8

Generator bytesGen
IterationsCount int
KeyLength int
SaltLength int
IVLength int
Generator bytesGen

// BlockSize must be a factor of aes.BlockSize
BlockSize int
Expand Down Expand Up @@ -180,7 +180,6 @@ func (w *Writer) Write(b []byte) (int, error) {
}

func (w *Writer) writeBlock(b []byte) error {

// randomly generate the salt and the initialization vector, this information will be saved
// on disk in the file as part of the header
iv, err := w.generator(w.option.IVLength)
Expand All @@ -189,12 +188,14 @@ func (w *Writer) writeBlock(b []byte) error {
return w.err
}

// nolint: errcheck // Ignore the error at this point.
w.writer.Write(iv)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linter complained: Error return value of a.monitor.Cleanup is not checked (errcheck)


encodedBytes := w.gcm.Seal(nil, iv, b, nil)

l := make([]byte, 4)
binary.LittleEndian.PutUint32(l, uint32(len(encodedBytes)))
// nolint: errcheck // Ignore the error at this point.
w.writer.Write(l)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linter complained: Error return value of a.monitor.Cleanup is not checked (errcheck)


_, err = w.writer.Write(encodedBytes)
Expand Down Expand Up @@ -325,7 +326,7 @@ func (r *Reader) consumeBlock() error {
}

encodedBytes := make([]byte, l)
_, err = io.ReadAtLeast(r.reader, encodedBytes, int(l))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

linter complained: unnecessary conversion (unconvert)

_, err = io.ReadAtLeast(r.reader, encodedBytes, l)
if err != nil {
r.err = errors.Wrapf(err, "fail read the block of %d bytes", l)
}
Expand Down Expand Up @@ -364,7 +365,6 @@ func (r *Reader) Close() error {
func randomBytes(length int) ([]byte, error) {
r := make([]byte, length)
_, err := rand.Read(r)

if err != nil {
return nil, err
}
Expand Down
15 changes: 7 additions & 8 deletions internal/pkg/fleetapi/acker/retrier/retrier.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ type Option func(*Retrier)

// Retrier implements retrier for actions acks
type Retrier struct {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

struct with 96 pointer bytes could be 48

log *logger.Logger
acker BatchAcker // AckBatch provider
log *logger.Logger

initialRetryInterval time.Duration // initial retry interval
maxRetryInterval time.Duration // max retry interval
maxRetries int // configurable maxNumber of retries per action
doneCh chan struct{} // signal channel to kickoff retry loop if not running
kickCh chan struct{} // signal channel when retry loop is done

actions []fleetapi.Action // pending actions
mx sync.Mutex

kickCh chan struct{} // signal channel to kickoff retry loop if not running
maxRetryInterval time.Duration // max retry interval
maxRetries int // configurable maxNumber of retries per action
initialRetryInterval time.Duration // initial retry interval

doneCh chan struct{} // signal channel when retry loop is done
mx sync.Mutex
}

// New creates new instance of retrier
Expand Down Expand Up @@ -173,7 +173,6 @@ func (r *Retrier) runRetries(ctx context.Context) {
default:
}
r.log.Debug("ack retrier: exit retry loop")

}

func (r *Retrier) updateRetriesMap(retries map[string]int, actions []fleetapi.Action, resp *fleetapi.AckResponse) (failed []fleetapi.Action) {
Expand Down
Loading