Skip to content

Commit 25b36d4

Browse files
varascarlosroman
andauthored
Fix deltas order race submission condition for remote entities (#59)
* 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]>
1 parent 2914830 commit 25b36d4

File tree

8 files changed

+578
-479
lines changed

8 files changed

+578
-479
lines changed

internal/agent/bulk_inventories_test.go

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ var plugin = &delta.PluginInfo{
2222
Source: "metadata/plugin",
2323
Plugin: "metadata",
2424
FileName: "plugin.json",
25-
MostRecentID: int64(0),
26-
LastSentID: int64(0),
2725
}
2826

2927
// createDelta creates and stores a delta JSON for a given entity, with a size approximate to the given size

internal/agent/delta/plugins.go

Lines changed: 73 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,84 @@
22
// SPDX-License-Identifier: Apache-2.0
33
package delta
44

5-
// PluginInfo holds information about agent plugins
5+
import (
6+
"fmt"
7+
"path/filepath"
8+
"strings"
9+
)
10+
11+
// PluginInfo persisted information about plugins.
612
type PluginInfo struct {
7-
Source string `json:"source"`
8-
Plugin string `json:"plugin"`
9-
FileName string `json:"filename"`
10-
MostRecentID int64 `json:"mru_id"` // Most recent id assigned to a delta
11-
LastSentID int64 `json:"last_sent_id"` // Most recent delta id sent to server
12-
FirstArchiveID int64 `json:"first_archive_id"`
13+
Source string `json:"source"`
14+
Plugin string `json:"plugin"`
15+
FileName string `json:"filename"`
16+
Entities map[string]PIEntity `json:"entities"`
17+
}
18+
19+
// PIEntity persisted info about an entity for a plugin.
20+
type PIEntity struct {
21+
MostRecentID int64 `json:"mru_id"` // latest ID for an plugin entity to be used for submission
22+
LastSentID int64 `json:"last_sent_id"` // latest ID from platform, decides whether archive or keep delta
1323
}
1424

15-
func (pi *PluginInfo) nextDeltaID() int64 {
16-
pi.MostRecentID = pi.MostRecentID + 1
17-
return pi.MostRecentID
25+
// newPluginInfo creates a new PluginInfo from plugin name and file.
26+
func newPluginInfo(name, fileName string) *PluginInfo {
27+
cleanFileName := strings.TrimSuffix(fileName, filepath.Ext(fileName))
28+
29+
return &PluginInfo{
30+
Source: fmt.Sprintf("%s/%s", name, cleanFileName),
31+
Plugin: name,
32+
FileName: fileName,
33+
Entities: make(map[string]PIEntity),
34+
}
35+
}
36+
37+
// setLastSentID is used to store latest ID from platform.
38+
func (p *PluginInfo) setLastSentID(entityKey string, value int64) {
39+
e := p.entity(entityKey)
40+
e.LastSentID = value
41+
p.Entities[entityKey] = e
42+
}
43+
44+
// lastSentID retrieves last sent ID.
45+
func (p *PluginInfo) lastSentID(entityKey string) int64 {
46+
return p.entity(entityKey).LastSentID
47+
}
48+
49+
// setDeltaID is used as backend-client reconciliation mechanism.
50+
func (p *PluginInfo) setDeltaID(entityKey string, value int64) {
51+
e := p.entity(entityKey)
52+
e.MostRecentID = value
53+
p.Entities[entityKey] = e
54+
}
55+
56+
// increaseDeltaID triggered on plugin reap, prior to submission
57+
func (p *PluginInfo) increaseDeltaID(entityKey string) {
58+
e := p.entity(entityKey)
59+
e.MostRecentID++
60+
p.Entities[entityKey] = e
61+
}
62+
63+
// deltaID provides delta ID for one of this plugin's entity.
64+
func (p *PluginInfo) deltaID(entityKey string) int64 {
65+
return p.entity(entityKey).MostRecentID
66+
}
67+
68+
func (p *PluginInfo) entity(key string) PIEntity {
69+
if p.Entities == nil {
70+
p.Entities = make(map[string]PIEntity)
71+
}
72+
if _, ok := p.Entities[key]; !ok {
73+
p.Entities[key] = PIEntity{}
74+
}
75+
76+
return p.Entities[key]
1877
}
1978

20-
type pluginIDMap map[string]*PluginInfo
79+
// pluginSource2Info stores plugins info by source.
80+
type pluginSource2Info map[string]*PluginInfo
2181

2282
// ID returns plugin serialized ID.
23-
func (pi *PluginInfo) ID() string {
24-
return pi.Source
83+
func (p *PluginInfo) ID() string {
84+
return p.Source
2585
}

0 commit comments

Comments
 (0)