/
agents_info.go
214 lines (190 loc) · 6.93 KB
/
agents_info.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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
* Copyright 2018- The Pixie Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
package tracker
import (
"fmt"
"sync"
"github.com/gofrs/uuid"
log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
"px.dev/pixie/src/carnot/planner/distributedpb"
"px.dev/pixie/src/utils"
"px.dev/pixie/src/vizier/services/metadata/metadatapb"
)
// KelvinSSLTargetOverride the hostname used for SSL target override when sending data to Kelvin.
// Note: This value may differ in the future. When that happens, this should instead come from the
// Kelvins themselves. rather than this variable, when Kelvins send their updated state.
const KelvinSSLTargetOverride = "kelvin.%s.svc"
// AgentsInfo tracks information about the distributed state of the system.
type AgentsInfo interface {
ClearPendingState()
UpdateAgentsInfo(update *metadatapb.AgentUpdatesResponse) error
DistributedState() distributedpb.DistributedState
}
// AgentsInfoImpl implements AgentsInfo to track information about the distributed state of the system.
type AgentsInfoImpl struct {
ds distributedpb.DistributedState
// Controls access to ds.
dsMutex sync.Mutex
pendingDs *distributedpb.DistributedState
}
// NewAgentsInfo creates an empty agents info.
func NewAgentsInfo() AgentsInfo {
return &AgentsInfoImpl{
ds: distributedpb.DistributedState{},
pendingDs: &distributedpb.DistributedState{
SchemaInfo: []*distributedpb.SchemaInfo{},
CarnotInfo: []*distributedpb.CarnotInfo{},
},
}
}
// NewTestAgentsInfo creates an agents info from a passed in distributed state.
func NewTestAgentsInfo(ds *distributedpb.DistributedState) AgentsInfo {
return &AgentsInfoImpl{
ds: *(ds),
pendingDs: nil,
}
}
// ClearPendingState clears the pending agents info state (the upcoming version).
func (a *AgentsInfoImpl) ClearPendingState() {
log.Infof("Clearing distributed state")
a.pendingDs = &distributedpb.DistributedState{
SchemaInfo: []*distributedpb.SchemaInfo{},
CarnotInfo: []*distributedpb.CarnotInfo{},
}
}
// UpdateAgentsInfo creates a new agent info.
// This function must be resilient to receiving the same update twice for a given agent.
func (a *AgentsInfoImpl) UpdateAgentsInfo(update *metadatapb.AgentUpdatesResponse) error {
if update.AgentSchemasUpdated {
log.Infof("Updating schemas to %d tables", len(update.AgentSchemas))
a.pendingDs.SchemaInfo = update.AgentSchemas
}
carnotInfoMap := make(map[uuid.UUID]*distributedpb.CarnotInfo)
for _, carnotInfo := range a.pendingDs.CarnotInfo {
agentUUID, err := utils.UUIDFromProto(carnotInfo.AgentID)
if err != nil {
return err
}
carnotInfoMap[agentUUID] = carnotInfo
}
log.Tracef("%d agents present in tracker before update", len(carnotInfoMap))
createdAgents := 0
deletedAgents := 0
updatedAgents := 0
updatedAgentsDataInfo := 0
for _, agentUpdate := range update.AgentUpdates {
agentUUID, err := utils.UUIDFromProto(agentUpdate.AgentID)
if err != nil {
return err
}
// case 1: agent info update
agent := agentUpdate.GetAgent()
if agent != nil {
if _, present := carnotInfoMap[agentUUID]; present {
updatedAgents++
} else {
createdAgents++
}
if agent.Info.Capabilities == nil || agent.Info.Capabilities.CollectsData {
var metadataInfo *distributedpb.MetadataInfo
if carnotInfo, present := carnotInfoMap[agentUUID]; present {
metadataInfo = carnotInfo.MetadataInfo
}
// this is a PEM
carnotInfoMap[agentUUID] = makeAgentCarnotInfo(agentUUID, agent.ASID, metadataInfo)
} else {
// this is a Kelvin
kelvinGRPCAddress := agent.Info.IPAddress
carnotInfoMap[agentUUID] = makeKelvinCarnotInfo(agentUUID, kelvinGRPCAddress, agent.ASID)
}
}
// case 2: agent data info update
dataInfo := agentUpdate.GetDataInfo()
if dataInfo != nil {
updatedAgentsDataInfo++
carnotInfo, present := carnotInfoMap[agentUUID]
if !present {
// It's possible that an agent may be deleted, but we still receive the schema. We should be robust to this case.
continue
}
if carnotInfo == nil {
return fmt.Errorf("Carnot info is nil for agent %s, but received agent data info", agentUUID.String())
}
if dataInfo.MetadataInfo != nil {
carnotInfo.MetadataInfo = dataInfo.MetadataInfo
}
}
// case 3: agent deleted
if agentUpdate.GetDeleted() {
deletedAgents++
delete(carnotInfoMap, agentUUID)
}
}
log.Tracef("Created %d agents, deleted %d agents, updated %d agents, updated %d agents data info",
createdAgents, deletedAgents, updatedAgents, updatedAgentsDataInfo)
log.Tracef("%d agents present in tracker after update", len(carnotInfoMap))
// reset the array and recreate.
a.pendingDs.CarnotInfo = []*distributedpb.CarnotInfo{}
for _, carnotInfo := range carnotInfoMap {
a.pendingDs.CarnotInfo = append(a.pendingDs.CarnotInfo, carnotInfo)
}
// If we have reached the end of version, promote the pending DistributedState to the current external-facing
// distributed state accessible by clients of `Agents`.
if update.EndOfVersion {
a.dsMutex.Lock()
a.ds = *(a.pendingDs)
a.dsMutex.Unlock()
}
return nil
}
// DistributedState returns the current distributed state.
// Returns a non-pointer because a.ds will change over time and we want the consumer of DistributedState()
// to have a consistent result.
func (a *AgentsInfoImpl) DistributedState() distributedpb.DistributedState {
a.dsMutex.Lock()
defer a.dsMutex.Unlock()
return a.ds
}
func makeAgentCarnotInfo(agentID uuid.UUID, asid uint32, agentMetadata *distributedpb.MetadataInfo) *distributedpb.CarnotInfo {
return &distributedpb.CarnotInfo{
QueryBrokerAddress: agentID.String(),
AgentID: utils.ProtoFromUUID(agentID),
ASID: asid,
HasGRPCServer: false,
HasDataStore: true,
ProcessesData: true,
AcceptsRemoteSources: false,
MetadataInfo: agentMetadata,
}
}
func makeKelvinCarnotInfo(agentID uuid.UUID, grpcAddress string, asid uint32) *distributedpb.CarnotInfo {
return &distributedpb.CarnotInfo{
QueryBrokerAddress: agentID.String(),
AgentID: utils.ProtoFromUUID(agentID),
ASID: asid,
HasGRPCServer: true,
GRPCAddress: grpcAddress,
HasDataStore: false,
ProcessesData: true,
AcceptsRemoteSources: true,
// When we support persistent storage, Kelvins will also have MetadataInfo.
MetadataInfo: nil,
SSLTargetName: fmt.Sprintf(KelvinSSLTargetOverride, viper.GetString("pod_namespace")),
}
}