-
Notifications
You must be signed in to change notification settings - Fork 21
/
routingtable.go
845 lines (711 loc) · 27.3 KB
/
routingtable.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
package routingtable
import (
"sync"
"code.cloudfoundry.org/bbs/models"
loggingclient "code.cloudfoundry.org/diego-logging-client"
"code.cloudfoundry.org/lager"
tcpmodels "code.cloudfoundry.org/routing-api/models"
"code.cloudfoundry.org/routing-info/cfroutes"
"code.cloudfoundry.org/routing-info/internalroutes"
"code.cloudfoundry.org/routing-info/tcp_routes"
)
type TCPRouteMappings struct {
Registrations []tcpmodels.TcpRouteMapping
Unregistrations []tcpmodels.TcpRouteMapping
}
func (mappings TCPRouteMappings) Merge(other TCPRouteMappings) TCPRouteMappings {
var result TCPRouteMappings
result.Registrations = append(mappings.Registrations, other.Registrations...)
result.Unregistrations = append(mappings.Unregistrations, other.Unregistrations...)
return result
}
const addressCollisionsCounter = "AddressCollisions"
//go:generate counterfeiter -o fakeroutingtable/fake_routingtable.go . RoutingTable
type RoutingTable interface {
// table modification
SetRoutes(logger lager.Logger, beforeLRP, afterLRP *models.DesiredLRP) (TCPRouteMappings, MessagesToEmit)
RemoveRoutes(logger lager.Logger, desiredLRP *models.DesiredLRP) (TCPRouteMappings, MessagesToEmit)
AddEndpoint(logger lager.Logger, actualLRP *models.ActualLRP) (TCPRouteMappings, MessagesToEmit)
RemoveEndpoint(logger lager.Logger, actualLRP *models.ActualLRP) (TCPRouteMappings, MessagesToEmit)
Swap(logger lager.Logger, t RoutingTable, domains models.DomainSet) (TCPRouteMappings, MessagesToEmit)
GetInternalRoutingEvents() (TCPRouteMappings, MessagesToEmit)
GetExternalRoutingEvents() (TCPRouteMappings, MessagesToEmit)
// routes
HasExternalRoutes(actualLRP *models.ActualLRP) bool
HTTPAssociationsCount() int // return number of associations desired-lrp-http-routes * actual-lrps
InternalAssociationsCount() int // return number of associations desired-lrp-internal-routes * 2 * actual-lrps
TCPAssociationsCount() int // return number of associations desired-lrp-tcp-routes * actual-lrps
TableSize() int
}
type internalRoutingTable struct {
endpointGenerator func(*models.ActualLRP) []Endpoint
routesGenerator func(*models.DesiredLRP) map[RoutingKey][]routeMapping
entries map[RoutingKey]RoutableEndpoints
addressEntries map[Address]EndpointKey
addressGenerator func(endpoint Endpoint) Address
directInstanceRoute bool
metronClient loggingclient.IngressClient
suppressAddressCollision bool
sync.Locker
}
type routingTable struct {
tcpRoutesRoutingTable *internalRoutingTable
httpRoutesRoutingTable *internalRoutingTable
internalRoutesRoutingTable *internalRoutingTable
}
func NewRoutingTable(directInstanceRoute bool, metronClient loggingclient.IngressClient) RoutingTable {
addressGenerator := func(endpoint Endpoint) Address {
if endpoint.IsDirectInstanceRoute(directInstanceRoute) {
return Address{Host: endpoint.ContainerIP, Port: endpoint.ContainerPort}
}
return Address{Host: endpoint.Host, Port: endpoint.Port}
}
httpRoutingTable := &internalRoutingTable{
endpointGenerator: NewEndpointsFromActual,
routesGenerator: httpRoutesFrom,
entries: make(map[RoutingKey]RoutableEndpoints),
addressEntries: make(map[Address]EndpointKey),
directInstanceRoute: directInstanceRoute,
addressGenerator: addressGenerator,
metronClient: metronClient,
Locker: &sync.Mutex{},
}
tcpRoutingTable := &internalRoutingTable{
endpointGenerator: NewEndpointsFromActual,
routesGenerator: tcpRoutesFrom,
entries: make(map[RoutingKey]RoutableEndpoints),
addressEntries: make(map[Address]EndpointKey),
directInstanceRoute: directInstanceRoute,
addressGenerator: addressGenerator,
metronClient: metronClient,
suppressAddressCollision: true,
Locker: &sync.Mutex{},
}
internalRoutingTable := &internalRoutingTable{
endpointGenerator: internalEndpointsFromActualLRP,
routesGenerator: internalRoutesFrom,
entries: make(map[RoutingKey]RoutableEndpoints),
addressEntries: make(map[Address]EndpointKey),
directInstanceRoute: directInstanceRoute,
addressGenerator: addressGenerator,
metronClient: metronClient,
suppressAddressCollision: true,
Locker: &sync.Mutex{},
}
return &routingTable{
tcpRoutesRoutingTable: tcpRoutingTable,
httpRoutesRoutingTable: httpRoutingTable,
internalRoutesRoutingTable: internalRoutingTable,
}
}
func internalEndpointsFromActualLRP(actualLRP *models.ActualLRP) []Endpoint {
return []Endpoint{
{
InstanceGUID: actualLRP.InstanceGuid,
Index: actualLRP.Index,
Host: actualLRP.Address,
ContainerIP: actualLRP.InstanceAddress,
Presence: actualLRP.Presence,
Since: actualLRP.Since,
ModificationTag: &actualLRP.ModificationTag,
},
}
}
func (table *routingTable) AddEndpoint(logger lager.Logger, actualLRP *models.ActualLRP) (TCPRouteMappings, MessagesToEmit) {
httpMappings, httpMessages, httpChanged := table.httpRoutesRoutingTable.AddEndpoint(logger, actualLRP)
tcpMappings, tcpMessages, tcpChanged := table.tcpRoutesRoutingTable.AddEndpoint(logger, actualLRP)
internalMappings, internalMessages, internalChanged := table.internalRoutesRoutingTable.AddEndpoint(logger, actualLRP)
mappings := httpMappings.Merge(tcpMappings).Merge(internalMappings)
messages := httpMessages.Merge(tcpMessages).Merge(internalMessages)
changed := httpChanged || tcpChanged || internalChanged
if changed {
logger.Info("added", ActualLRPData(actualLRP))
}
return mappings, messages
}
func (table *routingTable) RemoveEndpoint(logger lager.Logger, actualLRP *models.ActualLRP) (TCPRouteMappings, MessagesToEmit) {
httpMappings, httpMessages, httpChanged := table.httpRoutesRoutingTable.RemoveEndpoint(logger, actualLRP)
tcpMappings, tcpMessages, tcpChanged := table.tcpRoutesRoutingTable.RemoveEndpoint(logger, actualLRP)
internalMappings, internalMessages, internalChanged := table.internalRoutesRoutingTable.RemoveEndpoint(logger, actualLRP)
mappings := httpMappings.Merge(tcpMappings).Merge(internalMappings)
messages := httpMessages.Merge(tcpMessages).Merge(internalMessages)
changed := httpChanged || tcpChanged || internalChanged
if changed {
logger.Info("removed", ActualLRPData(actualLRP))
}
return mappings, messages
}
func (t *routingTable) Swap(logger lager.Logger, other RoutingTable, domains models.DomainSet) (TCPRouteMappings, MessagesToEmit) {
table, ok := other.(*routingTable)
if !ok {
logger.Error("failed-to-convert-to-routing-table", nil)
return TCPRouteMappings{}, MessagesToEmit{}
}
logger = logger.Session("swap")
logger.Info("starting", lager.Data{"domains": domains})
defer logger.Info("finished")
httpMappings, httpMessages := t.httpRoutesRoutingTable.Swap(table.httpRoutesRoutingTable, domains)
tcpMappings, tcpMessages := t.tcpRoutesRoutingTable.Swap(table.tcpRoutesRoutingTable, domains)
internalMappings, internalMessages := t.internalRoutesRoutingTable.Swap(table.internalRoutesRoutingTable, domains)
mappings := httpMappings.Merge(tcpMappings).Merge(internalMappings)
messages := httpMessages.Merge(tcpMessages).Merge(internalMessages)
return mappings, messages
}
func (t *routingTable) GetExternalRoutingEvents() (TCPRouteMappings, MessagesToEmit) {
httpMappings, httpMessages := t.httpRoutesRoutingTable.GetRoutingEvents()
tcpMappings, tcpMessages := t.tcpRoutesRoutingTable.GetRoutingEvents()
mappings := httpMappings.Merge(tcpMappings)
messages := httpMessages.Merge(tcpMessages)
return mappings, messages
}
func (t *routingTable) GetInternalRoutingEvents() (TCPRouteMappings, MessagesToEmit) {
return t.internalRoutesRoutingTable.GetRoutingEvents()
}
func (t *routingTable) SetRoutes(logger lager.Logger, before, after *models.DesiredLRP) (TCPRouteMappings, MessagesToEmit) {
httpMappings, httpMessages, httpChanged := t.httpRoutesRoutingTable.SetRoutes(before, after)
tcpMappings, tcpMessages, tcpChanged := t.tcpRoutesRoutingTable.SetRoutes(before, after)
internalMappings, internalMessages, internalChanged := t.internalRoutesRoutingTable.SetRoutes(before, after)
mappings := httpMappings.Merge(tcpMappings).Merge(internalMappings)
messages := httpMessages.Merge(tcpMessages).Merge(internalMessages)
if httpChanged || tcpChanged || internalChanged {
logger.Info("set-routes", lager.Data{"before": DesiredLRPData(before), "after": DesiredLRPData(after)})
}
return mappings, messages
}
func (t *routingTable) RemoveRoutes(logger lager.Logger, desiredLRP *models.DesiredLRP) (TCPRouteMappings, MessagesToEmit) {
httpMappings, httpMessages, httpChanged := t.httpRoutesRoutingTable.RemoveRoutes(desiredLRP)
tcpMappings, tcpMessages, tcpChanged := t.tcpRoutesRoutingTable.RemoveRoutes(desiredLRP)
internalMappings, internalMessages, internalChanged := t.internalRoutesRoutingTable.RemoveRoutes(desiredLRP)
mappings := httpMappings.Merge(tcpMappings).Merge(internalMappings)
messages := httpMessages.Merge(tcpMessages).Merge(internalMessages)
if httpChanged || tcpChanged || internalChanged {
logger.Info("remove-routes", DesiredLRPData(desiredLRP))
}
return mappings, messages
}
func (table *internalRoutingTable) AddEndpoint(logger lager.Logger, actualLRP *models.ActualLRP) (TCPRouteMappings, MessagesToEmit, bool) {
table.Lock()
defer table.Unlock()
changeDetected := false
endpoints := table.endpointGenerator(actualLRP)
// collision detection
if !table.suppressAddressCollision {
for _, endpoint := range endpoints {
address := table.addressGenerator(endpoint)
// if the address exists and the instance guid doesn't match then we have a collision
if existingEndpointKey, ok := table.addressEntries[address]; ok && existingEndpointKey.InstanceGUID != endpoint.InstanceGUID {
table.metronClient.IncrementCounter(addressCollisionsCounter)
existingInstanceGuid := existingEndpointKey.InstanceGUID
logger.Info("collision-detected-with-endpoint", lager.Data{
"instance_guid_a": existingInstanceGuid,
"instance_guid_b": endpoint.InstanceGUID,
"Address": address,
})
}
table.addressEntries[address] = endpoint.key()
}
}
// add endpoints
var messagesToEmit MessagesToEmit
var mappings TCPRouteMappings
for _, routingEndpoint := range endpoints {
key := RoutingKey{
ProcessGUID: actualLRP.ProcessGuid,
ContainerPort: routingEndpoint.ContainerPort,
}
currentEntry := table.entries[key]
// Since desiredLRP is same, only need to check one entry
if currentEntry.DesiredInstances > 0 && routingEndpoint.Index >= currentEntry.DesiredInstances {
logger.Debug("skipping-undesired-instance")
continue
}
currentEndpoint, ok := currentEntry.Endpoints[routingEndpoint.key()]
if ok && !currentEndpoint.ModificationTag.SucceededBy(routingEndpoint.ModificationTag) {
continue
}
newEntry := currentEntry.copy()
newEntry.Endpoints[routingEndpoint.key()] = routingEndpoint
table.entries[key] = newEntry
mapping, message, changed := table.emitDiffMessages(key, currentEntry, newEntry)
mappings = mappings.Merge(mapping)
messagesToEmit = messagesToEmit.Merge(message)
changeDetected = changeDetected || changed
}
return mappings, messagesToEmit, changeDetected
}
func (table *internalRoutingTable) RemoveEndpoint(logger lager.Logger, actualLRP *models.ActualLRP) (TCPRouteMappings, MessagesToEmit, bool) {
table.Lock()
defer table.Unlock()
changeDetected := false
endpoints := table.endpointGenerator(actualLRP)
// remove address
if !table.suppressAddressCollision {
for _, endpoint := range endpoints {
address := table.addressGenerator(endpoint)
currentEntry, ok := table.addressEntries[address]
if ok && currentEntry.InstanceGUID != endpoint.InstanceGUID {
logger.Info("collision-detected-with-endpoint", lager.Data{
"instance_guid_a": currentEntry.InstanceGUID,
"instance_guid_b": endpoint.InstanceGUID,
"Address": address,
})
continue
}
delete(table.addressEntries, address)
}
}
// remove endpoint
var messagesToEmit MessagesToEmit
var mappings TCPRouteMappings
for _, routingEndpoint := range endpoints {
key := RoutingKey{
ProcessGUID: actualLRP.ProcessGuid,
ContainerPort: routingEndpoint.ContainerPort,
}
currentEntry := table.entries[key]
endpointKey := routingEndpoint.key()
currentEndpoint, ok := currentEntry.Endpoints[endpointKey]
if !ok ||
(!currentEndpoint.ModificationTag.Equal(routingEndpoint.ModificationTag) &&
!currentEndpoint.ModificationTag.SucceededBy(routingEndpoint.ModificationTag)) {
continue
}
newEntry := currentEntry.copy()
delete(newEntry.Endpoints, endpointKey)
table.entries[key] = newEntry
table.deleteEntryIfEmpty(key)
mapping, message, changed := table.emitDiffMessages(key, currentEntry, newEntry)
messagesToEmit = messagesToEmit.Merge(message)
mappings = mappings.Merge(mapping)
changeDetected = changeDetected || changed
}
return mappings, messagesToEmit, changeDetected
}
func (t *internalRoutingTable) Swap(otherTable *internalRoutingTable, domains models.DomainSet) (TCPRouteMappings, MessagesToEmit) {
t.Lock()
defer t.Unlock()
t.addressEntries = otherTable.addressEntries
var messagesToEmit MessagesToEmit
var mappings TCPRouteMappings
mergedRoutingKeys := map[RoutingKey]struct{}{}
for key, _ := range otherTable.entries {
mergedRoutingKeys[key] = struct{}{}
}
for key, _ := range t.entries {
mergedRoutingKeys[key] = struct{}{}
}
for key := range mergedRoutingKeys {
existingEntry, ok := t.entries[key]
newEntry := otherTable.entries[key]
if !ok {
// routing key only exist in the new table
mapping, message, _ := t.emitDiffMessages(key, RoutableEndpoints{}, newEntry)
messagesToEmit = messagesToEmit.Merge(message)
mappings = mappings.Merge(mapping)
continue
}
// entry exists in both tables or in old table, merge the two entries to ensure non-fresh domain endpoints aren't removed
merged := mergeUnfreshRoutes(existingEntry, newEntry, domains)
otherTable.entries[key] = merged
otherTable.deleteEntryIfEmpty(key)
mapping, message, _ := t.emitDiffMessages(key, existingEntry, merged)
messagesToEmit = messagesToEmit.Merge(message)
mappings = mappings.Merge(mapping)
}
t.entries = otherTable.entries
return mappings, messagesToEmit
}
// merge the routes from both endpoints, ensuring that non-fresh routes aren't removed
func mergeUnfreshRoutes(before, after RoutableEndpoints, domains models.DomainSet) RoutableEndpoints {
merged := after.copy()
merged.Domain = before.Domain
if !domains.Contains(before.Domain) {
// non-fresh domain, append routes from older endpoint
for _, oldRoute := range before.Routes {
routeExistInNewLRP := func() bool {
for _, newRoute := range after.Routes {
if newRoute.Hash() == oldRoute.Hash() {
return true
}
}
return false
}
if !routeExistInNewLRP() {
merged.Routes = append(merged.Routes, oldRoute)
}
}
}
return merged
}
func (t *internalRoutingTable) GetRoutingEvents() (TCPRouteMappings, MessagesToEmit) {
t.Lock()
defer t.Unlock()
var messagesToEmit MessagesToEmit
var mappings TCPRouteMappings
for key, route := range t.entries {
mapping, message, _ := t.emitDiffMessages(key, RoutableEndpoints{}, route)
mappings = mappings.Merge(mapping)
messagesToEmit = messagesToEmit.Merge(message)
}
return mappings, messagesToEmit
}
type routeMapping interface {
MessageFor(endpoint Endpoint, directInstanceAddress, emitEndpointUpdatedAt bool) (*RegistryMessage, *tcpmodels.TcpRouteMapping, *RegistryMessage)
Hash() interface{}
}
func httpRoutesFrom(lrp *models.DesiredLRP) map[RoutingKey][]routeMapping {
if lrp == nil || lrp.Routes == nil {
return nil
}
routes, _ := cfroutes.CFRoutesFromRoutingInfo(*lrp.Routes)
routeEntries := make(map[RoutingKey][]routeMapping)
for _, route := range routes {
key := RoutingKey{ProcessGUID: lrp.ProcessGuid, ContainerPort: route.Port}
routes := []routeMapping{}
for _, hostname := range route.Hostnames {
route := Route{
Hostname: hostname,
LogGUID: lrp.LogGuid,
RouteServiceUrl: route.RouteServiceUrl,
IsolationSegment: route.IsolationSegment,
MetricTags: lrp.MetricTags,
Protocol: route.Protocol,
}
routes = append(routes, route)
}
routeEntries[key] = append(routeEntries[key], routes...)
}
return routeEntries
}
func tcpRoutesFrom(lrp *models.DesiredLRP) map[RoutingKey][]routeMapping {
if lrp == nil {
return nil
}
routes, _ := tcp_routes.TCPRoutesFromRoutingInfo(lrp.Routes)
routeEntries := make(map[RoutingKey][]routeMapping)
for _, route := range routes {
key := RoutingKey{ProcessGUID: lrp.ProcessGuid, ContainerPort: route.ContainerPort}
routeEntries[key] = append(routeEntries[key], ExternalEndpointInfo{
RouterGroupGUID: route.RouterGroupGuid,
Port: route.ExternalPort,
})
}
return routeEntries
}
func internalRoutesFrom(lrp *models.DesiredLRP) map[RoutingKey][]routeMapping {
if lrp == nil || lrp.Routes == nil {
return nil
}
routes, _ := internalroutes.InternalRoutesFromRoutingInfo(*lrp.Routes)
routeEntries := make(map[RoutingKey][]routeMapping)
for _, route := range routes {
key := RoutingKey{ProcessGUID: lrp.ProcessGuid}
routeEntries[key] = append(routeEntries[key], InternalRoute{
Hostname: route.Hostname,
LogGUID: lrp.LogGuid,
})
}
return routeEntries
}
func (table *internalRoutingTable) SetRoutes(before, after *models.DesiredLRP) (TCPRouteMappings, MessagesToEmit, bool) {
table.Lock()
defer table.Unlock()
// update routes
removedRouteEntries := table.routesGenerator(before)
routeEntries := table.routesGenerator(after)
// logger.Info("internal-route-table", lager.Data{"numEntries": len(table.internalEntries)})
var messagesToEmit MessagesToEmit = MessagesToEmit{}
var mappings TCPRouteMappings
changedDetected := false
for key, routes := range routeEntries {
currentEntry := table.entries[key]
// if modification tag is old, ignore the new lrp
if !currentEntry.ModificationTag.SucceededBy(after.ModificationTag) {
continue
}
newEntry := currentEntry.copy()
newEntry.Domain = after.Domain
newEntry.Routes = routes
newEntry.ModificationTag = after.ModificationTag
newEntry.DesiredInstances = after.Instances
// check if scaling down
if before != nil && after != nil && before.Instances > after.Instances {
newEndpoints := make(map[EndpointKey]Endpoint)
for endpointKey, endpoint := range newEntry.Endpoints {
if endpoint.Index < after.Instances {
newEndpoints[endpointKey] = endpoint
}
}
newEntry.Endpoints = newEndpoints
}
table.entries[key] = newEntry
mapping, message, changed := table.emitDiffMessages(key, currentEntry, newEntry)
messagesToEmit = messagesToEmit.Merge(message)
mappings = mappings.Merge(mapping)
changedDetected = changedDetected || changed
}
for key := range removedRouteEntries {
if _, ok := routeEntries[key]; ok {
continue
}
currentEntry := table.entries[key]
if after == nil {
// this is a delete (after == nil), then before lrp modification tag must be >=
if !currentEntry.ModificationTag.Equal(before.ModificationTag) && !currentEntry.ModificationTag.SucceededBy(before.ModificationTag) {
continue
}
} else if !currentEntry.ModificationTag.SucceededBy(after.ModificationTag) {
// this is an update, then after lrp modification tag must be >
continue
}
newEntry := currentEntry.copy()
newEntry.Routes = nil
if after != nil {
newEntry.Domain = after.Domain
newEntry.ModificationTag = after.ModificationTag
newEntry.DesiredInstances = after.Instances
}
table.entries[key] = newEntry
table.deleteEntryIfEmpty(key)
mapping, message, changed := table.emitDiffMessages(key, currentEntry, newEntry)
messagesToEmit = messagesToEmit.Merge(message)
mappings = mappings.Merge(mapping)
changedDetected = changedDetected || changed
}
return mappings, messagesToEmit, changedDetected
}
func (table *internalRoutingTable) deleteEntryIfEmpty(key RoutingKey) {
entry := table.entries[key]
if len(entry.Endpoints) == 0 && len(entry.Routes) == 0 {
delete(table.entries, key)
}
}
func (table *internalRoutingTable) emitDiffMessages(key RoutingKey, oldEntry, newEntry RoutableEndpoints) (TCPRouteMappings, MessagesToEmit, bool) {
routesDiff := diffRoutes(oldEntry.Routes, newEntry.Routes)
endpointsDiff := diffEndpoints(oldEntry.Endpoints, newEntry.Endpoints)
changed := false
if len(routesDiff.added) > 0 || len(routesDiff.removed) > 0 ||
len(endpointsDiff.added) > 0 || len(endpointsDiff.removed) > 0 {
changed = true
}
mappings, messages := table.messages(routesDiff, endpointsDiff)
return mappings, messages, changed
}
type routesDiff struct {
before, after, removed, added []routeMapping
}
type endpointsDiff struct {
before, after, removed, added map[EndpointKey]Endpoint
}
func diffRoutes(before, after []routeMapping) routesDiff {
existingRoutes := map[interface{}]routeMapping{}
newRoutes := map[interface{}]routeMapping{}
for _, route := range before {
existingRoutes[route.Hash()] = route
}
for _, route := range after {
newRoutes[route.Hash()] = route
}
diff := routesDiff{
before: before,
after: after,
}
// generate the diff
for routeHash := range existingRoutes {
if _, ok := newRoutes[routeHash]; !ok {
diff.removed = append(diff.removed, existingRoutes[routeHash])
}
}
for routeHash := range newRoutes {
if _, ok := existingRoutes[routeHash]; !ok {
diff.added = append(diff.added, newRoutes[routeHash])
}
}
return diff
}
// endpoints are different if any field is different other than the following:
// 1. ModificationTag
// 2. Presence
// 3. Since
func endpointDifferent(before, after Endpoint) bool {
endpoint := before
endpoint.ModificationTag = after.ModificationTag
endpoint.Presence = after.Presence
endpoint.Since = after.Since
return endpoint != after
}
func diffEndpoints(before, after map[EndpointKey]Endpoint) endpointsDiff {
diff := endpointsDiff{
before: before,
after: after,
removed: make(map[EndpointKey]Endpoint),
added: make(map[EndpointKey]Endpoint),
}
// generate the diff
for key, endpoint := range before {
newEndpoint, ok := after[key]
if !ok {
key.Evacuating = !key.Evacuating
newEndpoint, ok = after[key]
}
if !ok || endpointDifferent(newEndpoint, endpoint) {
diff.removed[key] = endpoint
}
}
for key, endpoint := range after {
oldEndpoint, ok := before[key]
if !ok {
key.Evacuating = !key.Evacuating
oldEndpoint, ok = before[key]
}
if !ok || endpointDifferent(endpoint, oldEndpoint) {
diff.added[key] = endpoint
}
}
return diff
}
func (table *internalRoutingTable) messages(routesDiff routesDiff, endpointDiff endpointsDiff) (TCPRouteMappings, MessagesToEmit) {
type registrationMetadata struct {
emitEndpointUpdatedAt bool
route routeMapping
}
type unregistrationMetadata struct {
route routeMapping
}
// maps used to remove duplicates
unregistrations := map[interface{}]map[Endpoint]*unregistrationMetadata{}
registrations := map[interface{}]map[Endpoint]*registrationMetadata{}
// for removed routes remove endpoints previously registered
for _, route := range routesDiff.removed {
rh := route.Hash()
for _, container := range endpointDiff.before {
if unregistrations[rh] != nil && unregistrations[rh][container] != nil {
continue
}
if unregistrations[rh] == nil {
unregistrations[rh] = map[Endpoint]*unregistrationMetadata{}
}
unregistrations[rh][container] = &unregistrationMetadata{
route: route,
}
}
}
// for added routes add all currently known endpoints
for _, route := range routesDiff.added {
rh := route.Hash()
for _, container := range endpointDiff.after {
if registrations[rh] != nil && registrations[rh][container] != nil {
continue
}
if registrations[rh] == nil {
registrations[rh] = map[Endpoint]*registrationMetadata{}
}
registrations[rh][container] = ®istrationMetadata{
emitEndpointUpdatedAt: false,
route: route,
}
}
}
// for removed endpoints remove routes previously registered
for _, container := range endpointDiff.removed {
for _, route := range routesDiff.before {
rh := route.Hash()
if unregistrations[rh] != nil && unregistrations[rh][container] != nil {
continue
}
if unregistrations[rh] == nil {
unregistrations[rh] = map[Endpoint]*unregistrationMetadata{}
}
unregistrations[rh][container] = &unregistrationMetadata{
route: route,
}
}
}
// for added endpoints register all current routes
for _, container := range endpointDiff.added {
for _, route := range routesDiff.after {
rh := route.Hash()
if registrations[rh] != nil && registrations[rh][container] != nil {
continue
}
if registrations[rh] == nil {
registrations[rh] = map[Endpoint]*registrationMetadata{}
}
registrations[rh][container] = ®istrationMetadata{
emitEndpointUpdatedAt: true,
route: route,
}
}
}
messages := MessagesToEmit{}
mappings := TCPRouteMappings{}
for _, es := range registrations {
for e, metadata := range es {
msg, mapping, internalMsg := metadata.route.MessageFor(e, table.directInstanceRoute, metadata.emitEndpointUpdatedAt)
if msg != nil {
messages.RegistrationMessages = append(messages.RegistrationMessages, *msg)
}
if mapping != nil {
mappings.Registrations = append(mappings.Registrations, *mapping)
}
if internalMsg != nil {
messages.InternalRegistrationMessages = append(messages.InternalRegistrationMessages, *internalMsg)
}
}
}
for _, es := range unregistrations {
for e, metadata := range es {
msg, mapping, internalMsg := metadata.route.MessageFor(e, table.directInstanceRoute, false)
if msg != nil {
messages.UnregistrationMessages = append(messages.UnregistrationMessages, *msg)
}
if mapping != nil {
mappings.Unregistrations = append(mappings.Unregistrations, *mapping)
}
if internalMsg != nil {
messages.InternalUnregistrationMessages = append(messages.InternalUnregistrationMessages, *internalMsg)
}
}
}
return mappings, messages
}
func (table *internalRoutingTable) RemoveRoutes(desiredLRP *models.DesiredLRP) (TCPRouteMappings, MessagesToEmit, bool) {
return table.SetRoutes(desiredLRP, nil)
}
func (t *internalRoutingTable) AssociationsCount() int {
t.Lock()
defer t.Unlock()
count := 0
for _, entry := range t.entries {
count += len(entry.Routes) * len(entry.Endpoints)
}
return count
}
func (t *internalRoutingTable) TableSize() int {
t.Lock()
defer t.Unlock()
return len(t.entries)
}
func (t *internalRoutingTable) HasExternalRoutes(actualLRP *models.ActualLRP) bool {
for _, key := range NewRoutingKeysFromActual(actualLRP) {
if len(t.entries[key].Routes) > 0 {
return true
}
}
return false
}
func (t *routingTable) HTTPAssociationsCount() int {
return t.httpRoutesRoutingTable.AssociationsCount()
}
func (t *routingTable) TCPAssociationsCount() int {
return t.tcpRoutesRoutingTable.AssociationsCount()
}
func (t *routingTable) InternalAssociationsCount() int {
return 2 * t.internalRoutesRoutingTable.AssociationsCount()
}
func (t *routingTable) TableSize() int {
return t.httpRoutesRoutingTable.TableSize() + t.tcpRoutesRoutingTable.TableSize() + t.internalRoutesRoutingTable.TableSize()
}
func (t *routingTable) HasExternalRoutes(actualLRP *models.ActualLRP) bool {
return t.httpRoutesRoutingTable.HasExternalRoutes(actualLRP) || t.tcpRoutesRoutingTable.HasExternalRoutes(actualLRP)
}