Skip to content

Commit 34a72f9

Browse files
authored
lazy loading customs now (#910)
* lazy loading customs now * Moving to a lock free system for lazy loading
1 parent 80cd576 commit 34a72f9

3 files changed

Lines changed: 46 additions & 12 deletions

File tree

pkg/api/api.go

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ const (
4444
KT_INTERFACE_LOOKUP_TEXT_FILTER = "KT_INTERFACE_LOOKUP_TEXT_FILTER"
4545
KT_NO_CUSTOM_COLUMNS = "KT_NO_CUSTOM_COLUMNS"
4646
KT_LOAD_CUSTOM_COLS_FOR_DEVS = "KT_LOAD_CUSTOM_COLS_FOR_DEVS"
47+
KT_API_LAZY_LOAD_CUSTOMS = "KT_API_LAZY_LOAD_CUSTOMS"
4748
)
4849

4950
var (
@@ -72,6 +73,7 @@ type KentikApi struct {
7273
config *ktranslate.Config
7374
tagLookupClient tagging.EnumerationsAdminServiceClient
7475
loadInterfaces bool
76+
lazyLoadCustoms bool
7577
}
7678

7779
func NewKentikApi(ctx context.Context, log logger.ContextL, cfg *ktranslate.Config) (*KentikApi, error) {
@@ -81,7 +83,6 @@ func NewKentikApi(ctx context.Context, log logger.ContextL, cfg *ktranslate.Conf
8183
intv, _ := strconv.Atoi(apiTimeoutStr)
8284
apiTimeout = time.Duration(intv) * time.Second
8385
}
84-
log.Infof("Setting API timeout to %v", apiTimeout)
8586

8687
tr := &http.Transport{
8788
DisableCompression: false,
@@ -94,14 +95,17 @@ func NewKentikApi(ctx context.Context, log logger.ContextL, cfg *ktranslate.Conf
9495
client := &http.Client{Transport: tr, Timeout: apiTimeout}
9596

9697
kapi := &KentikApi{
97-
ContextL: log,
98-
tr: tr,
99-
client: client,
100-
apiTimeout: apiTimeout,
101-
config: cfg,
102-
loadInterfaces: kt.LookupEnvBool(KT_API_LOAD_INTERFACES, false),
98+
ContextL: log,
99+
tr: tr,
100+
client: client,
101+
apiTimeout: apiTimeout,
102+
config: cfg,
103+
loadInterfaces: kt.LookupEnvBool(KT_API_LOAD_INTERFACES, false),
104+
lazyLoadCustoms: kt.LookupEnvBool(KT_API_LAZY_LOAD_CUSTOMS, false),
103105
}
104106

107+
log.Infof("Setting API timeout to %v, loadInterfaces=%v, lazyLoadCustoms=%v", apiTimeout, kapi.loadInterfaces, kapi.lazyLoadCustoms)
108+
105109
// Now, check to see if synthetics API works.
106110
err := kapi.connectSynthAndLookup(ctx)
107111
if err != nil {
@@ -235,12 +239,33 @@ func (api *KentikApi) GetDevicesAsMap(cid kt.Cid) map[string]*kt.Device {
235239
return res
236240
}
237241

238-
func (api *KentikApi) GetDevice(cid kt.Cid, did kt.DeviceID) *kt.Device {
242+
func (api *KentikApi) GetDevice(ctx context.Context, cid kt.Cid, did kt.DeviceID) *kt.Device {
239243
if api == nil {
240244
return nil
241245
}
242246
if c, ok := api.devices[cid]; ok {
243-
return c[did]
247+
dev := c[did]
248+
if dev == nil {
249+
return dev
250+
}
251+
if api.lazyLoadCustoms && !dev.LoadedCustoms() {
252+
go func() {
253+
for _, info := range api.config.KentikCreds {
254+
md := metadata.New(map[string]string{
255+
"X-CH-Auth-Email": info.APIEmail,
256+
"X-CH-Auth-API-Token": info.APIToken,
257+
})
258+
ctxo := metadata.NewOutgoingContext(ctx, md)
259+
err := api.loadCustoms(ctxo, dev.IDStr, dev)
260+
if err != nil {
261+
api.Warnf("Cannot load customs for %s %s, %v", dev.IDStr, info.APIEmail, err)
262+
} else {
263+
return
264+
}
265+
}
266+
}()
267+
}
268+
return dev
244269
}
245270
return nil
246271
}

pkg/cat/jchf.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func (kc *KTranslate) flowToJCHF(ctx context.Context, dst *kt.JCHF, src *Flow, c
128128

129129
// Do we have info about this device?
130130
custColNames := map[uint32]string{}
131-
if d := kc.apic.GetDevice(dst.CompanyId, dst.DeviceId); d != nil {
131+
if d := kc.apic.GetDevice(ctx, dst.CompanyId, dst.DeviceId); d != nil {
132132
dst.DeviceName = d.Name
133133
dst.CustomStr[UDR_TYPE] = d.DeviceSubtype
134134
if len(d.SendingIps) > 0 {
@@ -279,7 +279,7 @@ func (kc *KTranslate) flowToJCHF(ctx context.Context, dst *kt.JCHF, src *Flow, c
279279
}
280280
case "ult_exit_device_id":
281281
dst.CustomInt[name] = int32(v)
282-
if d := kc.apic.GetDevice(dst.CompanyId, kt.DeviceID(v)); d != nil {
282+
if d := kc.apic.GetDevice(ctx, dst.CompanyId, kt.DeviceID(v)); d != nil {
283283
dst.CustomStr["ult_exit_device"] = d.Name
284284
dst.CustomStr["ult_exit_site"] = d.Site.SiteName
285285
}
@@ -408,7 +408,7 @@ func (kc *KTranslate) flowToJCHF(ctx context.Context, dst *kt.JCHF, src *Flow, c
408408

409409
// Check if there is ultimate exit data interface and pull this in also.
410410
if udid, ok := dst.CustomInt["ult_exit_device_id"]; ok {
411-
if d := kc.apic.GetDevice(dst.CompanyId, kt.DeviceID(udid)); d != nil {
411+
if d := kc.apic.GetDevice(ctx, dst.CompanyId, kt.DeviceID(udid)); d != nil {
412412
if ui, ok := dst.CustomInt["ult_exit_port"]; ok {
413413
if i, ok := d.Interfaces[kt.IfaceID(ui)]; ok {
414414
dst.CustomStr["ult_exit_port_alias"] = i.Alias

pkg/kt/device_types.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ type Device struct {
4242
allUserTags map[string]string
4343
FullSite *FullSite
4444
IDStr string
45+
loadedCustoms bool
4546
}
4647

4748
type DeviceLabel struct {
@@ -174,6 +175,13 @@ func (d *Device) AddCustoms(dd *devicepb.DeviceDetailed) {
174175
customs := mapCustomColumns(dd.GetCustomColumnData())
175176
d.Customs = customs
176177
d.CustomStr = dd.GetCustomColumns()
178+
d.loadedCustoms = true
179+
}
180+
181+
func (d *Device) LoadedCustoms() bool {
182+
lc := d.loadedCustoms
183+
d.loadedCustoms = true
184+
return lc
177185
}
178186

179187
func (d *Device) AddInterface(p *interfacepb.Interface) {
@@ -303,6 +311,7 @@ func MapDeviceDetailedToDevice(dd *devicepb.DeviceDetailed) (*Device, error) {
303311
SnmpV3: snmpV3,
304312
Labels: labels,
305313
Site: site,
314+
loadedCustoms: len(customs) > 0,
306315
}, nil
307316
}
308317

0 commit comments

Comments
 (0)