-
Notifications
You must be signed in to change notification settings - Fork 13
/
eventsync.go
198 lines (165 loc) · 5.43 KB
/
eventsync.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
package agent
import (
"fmt"
"github.com/Axway/agent-sdk/pkg/agent/events"
"github.com/Axway/agent-sdk/pkg/agent/poller"
"github.com/Axway/agent-sdk/pkg/agent/stream"
management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
"github.com/Axway/agent-sdk/pkg/config"
"github.com/Axway/agent-sdk/pkg/harvester"
"github.com/Axway/agent-sdk/pkg/jobs"
"github.com/Axway/agent-sdk/pkg/migrate"
"github.com/Axway/agent-sdk/pkg/util"
)
// EventSync struct for syncing events from central
type EventSync struct {
mpEnabled bool
watchTopic *management.WatchTopic
sequence events.SequenceProvider
harvester harvester.Harvest
discoveryCache *discoveryCache
}
// NewEventSync creates an EventSync
func NewEventSync() (*EventSync, error) {
migrations := []migrate.Migrator{}
// Make sure only DA and Governance agents run migration processes
runMigrations := agent.cfg.GetAgentType() != config.TraceabilityAgent
// Check if marketplace is enabled
isMpEnabled := agent.agentFeaturesCfg != nil && agent.agentFeaturesCfg.MarketplaceProvisioningEnabled()
if runMigrations {
// add attribute migration to migrations
attributeMigration := migrate.NewAttributeMigration(agent.apicClient, agent.cfg)
ardMigration := migrate.NewArdMigration(agent.apicClient, agent.cfg)
apisiMigration := migrate.NewAPISIMigration(agent.apicClient, agent.cfg)
instanceMigration := migrate.NewInstanceMigration(agent.apicClient, agent.cfg)
migrations = append(migrations, attributeMigration, ardMigration, apisiMigration, instanceMigration)
if isMpEnabled {
// add marketplace migration to migrations
marketplaceMigration := migrate.NewMarketplaceMigration(agent.apicClient, agent.cfg, agent.cacheManager)
agent.marketplaceMigration = marketplaceMigration
migrations = append(migrations, marketplaceMigration)
}
}
mig := migrate.NewMigrateAll(migrations...)
opts := []discoveryOpt{
withMigration(mig),
withMpEnabled(isMpEnabled),
}
if agent.agentResourceManager != nil {
opts = append(opts, withAdditionalDiscoverFuncs(agent.agentResourceManager.FetchAgentResource))
}
wt, err := events.GetWatchTopic(agent.cfg, GetCentralClient())
if err != nil {
return nil, err
}
sequence := events.NewSequenceProvider(agent.cacheManager, wt.Name)
hCfg := harvester.NewConfig(agent.cfg, agent.tokenRequester, sequence)
hClient := harvester.NewClient(hCfg)
discoveryCache := newDiscoveryCache(
agent.cfg,
GetCentralClient(),
newHandlers(),
wt,
opts...,
)
return &EventSync{
mpEnabled: isMpEnabled,
watchTopic: wt,
sequence: sequence,
harvester: hClient,
discoveryCache: discoveryCache,
}, nil
}
// SyncCache initializes agent cache and starts the agent in stream or poll mode
func (es *EventSync) SyncCache() error {
if !agent.cacheManager.HasLoadedPersistedCache() {
if err := es.initCache(); err != nil {
return err
}
}
err := es.startCentralEventProcessor()
if err != nil {
return err
}
return es.registerInstanceValidator()
}
func (es *EventSync) registerInstanceValidator() error {
if agent.apiValidatorJobID == "" && agent.cfg.GetAgentType() == config.DiscoveryAgent {
jobID, err := jobs.RegisterIntervalJobWithName(newInstanceValidator(), agent.cfg.GetAPIValidationFrequency(), "API service instance validator")
agent.apiValidatorJobID = jobID
return err
}
return nil
}
func (es *EventSync) initCache() error {
seqID, err := es.harvester.ReceiveSyncEvents(es.watchTopic.GetSelfLink(), 0, nil)
if err != nil {
return err
}
// event channel is not ready yet, so subtract one from the latest sequence id to process the event
// when the poll/stream client is ready
// when no events returned by harvester the seqID will be 0, so not updated in sequence manager
if seqID > 0 {
es.sequence.SetSequence(seqID - 1)
}
err = es.discoveryCache.execute()
if err != nil {
// flush cache again to clear out anything that may have been saved before the error to ensure a clean state for the next time through
agent.cacheManager.Flush()
return err
}
agent.cacheManager.SaveCache()
return nil
}
func (es *EventSync) rebuildCache() {
agent.cacheManager.Flush()
if err := es.initCache(); err != nil {
logger.WithError(err).Error("failed to rebuild cache")
}
}
func (es *EventSync) startCentralEventProcessor() error {
if agent.cfg.IsUsingGRPC() {
return es.startStreamMode()
}
return es.startPollMode()
}
func (es *EventSync) startPollMode() error {
handlers := newHandlers()
pc, err := poller.NewPollClient(
agent.apicClient,
agent.cfg,
handlers,
poller.WithHarvester(es.harvester, es.sequence, es.watchTopic.GetSelfLink()),
poller.WithOnClientStop(es.rebuildCache),
poller.WithOnConnect(),
)
if err != nil {
return fmt.Errorf("could not start the harvester poll client: %s", err)
}
if util.IsNotTest() {
newEventProcessorJob(pc, "Poll Client")
}
return err
}
func (es *EventSync) startStreamMode() error {
handlers := newHandlers()
sc, err := stream.NewStreamerClient(
agent.apicClient,
agent.cfg,
agent.tokenRequester,
handlers,
stream.WithOnStreamConnection(),
stream.WithEventSyncError(es.rebuildCache),
stream.WithWatchTopic(es.watchTopic),
stream.WithHarvester(es.harvester, es.sequence),
stream.WithCacheManager(agent.cacheManager),
)
if err != nil {
return fmt.Errorf("could not start the watch manager: %s", err)
}
agent.streamer = sc
if util.IsNotTest() {
newEventProcessorJob(sc, "Stream Client")
}
return err
}