Skip to content

Commit

Permalink
Fix deltas order race submission condition for remote entities (#59)
Browse files Browse the repository at this point in the history
* refactor: extract NewPluginInfo

* refactor: readability

* refactor: rename pluginIDMap to pluginSource2Info

* refactor: remove unrequired code

* fix: replace MostRecentID with MostRecentIDs indexed by entity-key

* docs: NewPluginInfo docblock

* privatise newPluginInfo

* refactor: improve TestReadDeltas

* test: ReadDeltas on same plugin with multiple entities got their ID increased independently

* refactor: simplify conditional

* refactor: simplify conditional

* remove first_archive_id

obsolete dead code

* refactor: guard clause

* refactor: rename nextIDMap to plugins

it was used for more stuff than next id

* docs: platform behaviour comments

* refactor: extract reconciliateWithBackend

* refactor: use available var

* refactor: reorg arguments

* refactor: simplify conditional

* refactor: rename method receivers

* refactor: readability

* docs: comments

* refactor: readability

* docs: comments

* refactor: readability

* refactor: readability

* docs: comments

* extract entities data per plugin into own struct map

* refactor: consistent names

* docs: docblocks

* Polish root folder (#24)

* Move win_build to build folder
* Move infra_build to build
* Move Dockerfile into build
* Move external_content to assets
Co-authored-by: Juan Hernandez <[email protected]>
Co-authored-by: Carlos <[email protected]>

* feat: Added makefiles used to build Docker containers of the agent (#54)

* feat: added ability to build infra agent container locally
* ci: added build job for Linx agent container

* Missing copyrights

* Fix vendor dependency direct type (#56)

* refactor: replace map with array

Co-authored-by: Carlos <[email protected]>
  • Loading branch information
varas and carlosroman authored Aug 7, 2020
1 parent 2914830 commit 25b36d4
Show file tree
Hide file tree
Showing 8 changed files with 578 additions and 479 deletions.
2 changes: 0 additions & 2 deletions internal/agent/bulk_inventories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ var plugin = &delta.PluginInfo{
Source: "metadata/plugin",
Plugin: "metadata",
FileName: "plugin.json",
MostRecentID: int64(0),
LastSentID: int64(0),
}

// createDelta creates and stores a delta JSON for a given entity, with a size approximate to the given size
Expand Down
86 changes: 73 additions & 13 deletions internal/agent/delta/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,84 @@
// SPDX-License-Identifier: Apache-2.0
package delta

// PluginInfo holds information about agent plugins
import (
"fmt"
"path/filepath"
"strings"
)

// PluginInfo persisted information about plugins.
type PluginInfo struct {
Source string `json:"source"`
Plugin string `json:"plugin"`
FileName string `json:"filename"`
MostRecentID int64 `json:"mru_id"` // Most recent id assigned to a delta
LastSentID int64 `json:"last_sent_id"` // Most recent delta id sent to server
FirstArchiveID int64 `json:"first_archive_id"`
Source string `json:"source"`
Plugin string `json:"plugin"`
FileName string `json:"filename"`
Entities map[string]PIEntity `json:"entities"`
}

// PIEntity persisted info about an entity for a plugin.
type PIEntity struct {
MostRecentID int64 `json:"mru_id"` // latest ID for an plugin entity to be used for submission
LastSentID int64 `json:"last_sent_id"` // latest ID from platform, decides whether archive or keep delta
}

func (pi *PluginInfo) nextDeltaID() int64 {
pi.MostRecentID = pi.MostRecentID + 1
return pi.MostRecentID
// newPluginInfo creates a new PluginInfo from plugin name and file.
func newPluginInfo(name, fileName string) *PluginInfo {
cleanFileName := strings.TrimSuffix(fileName, filepath.Ext(fileName))

return &PluginInfo{
Source: fmt.Sprintf("%s/%s", name, cleanFileName),
Plugin: name,
FileName: fileName,
Entities: make(map[string]PIEntity),
}
}

// setLastSentID is used to store latest ID from platform.
func (p *PluginInfo) setLastSentID(entityKey string, value int64) {
e := p.entity(entityKey)
e.LastSentID = value
p.Entities[entityKey] = e
}

// lastSentID retrieves last sent ID.
func (p *PluginInfo) lastSentID(entityKey string) int64 {
return p.entity(entityKey).LastSentID
}

// setDeltaID is used as backend-client reconciliation mechanism.
func (p *PluginInfo) setDeltaID(entityKey string, value int64) {
e := p.entity(entityKey)
e.MostRecentID = value
p.Entities[entityKey] = e
}

// increaseDeltaID triggered on plugin reap, prior to submission
func (p *PluginInfo) increaseDeltaID(entityKey string) {
e := p.entity(entityKey)
e.MostRecentID++
p.Entities[entityKey] = e
}

// deltaID provides delta ID for one of this plugin's entity.
func (p *PluginInfo) deltaID(entityKey string) int64 {
return p.entity(entityKey).MostRecentID
}

func (p *PluginInfo) entity(key string) PIEntity {
if p.Entities == nil {
p.Entities = make(map[string]PIEntity)
}
if _, ok := p.Entities[key]; !ok {
p.Entities[key] = PIEntity{}
}

return p.Entities[key]
}

type pluginIDMap map[string]*PluginInfo
// pluginSource2Info stores plugins info by source.
type pluginSource2Info map[string]*PluginInfo

// ID returns plugin serialized ID.
func (pi *PluginInfo) ID() string {
return pi.Source
func (p *PluginInfo) ID() string {
return p.Source
}
Loading

0 comments on commit 25b36d4

Please sign in to comment.