@@ -98,6 +98,33 @@ func newTestCPUServer(t *testing.T, podList []*v1.Pod) *cpuServer {
98
98
return cpuServer
99
99
}
100
100
101
+ func newTestCPUServerWithChanBuffer (t * testing.T , podList []* v1.Pod ) * cpuServer {
102
+ recvCh := make (chan types.InternalCPUCalculationResult , 1 )
103
+ sendCh := make (chan types.TriggerInfo , 1 )
104
+ conf := generateTestConfiguration (t )
105
+
106
+ metricsFetcher := metric .NewFakeMetricsFetcher (metrics.DummyMetrics {})
107
+ metaCache , err := metacache .NewMetaCacheImp (conf , metricspool.DummyMetricsEmitterPool {}, metricsFetcher )
108
+ require .NoError (t , err )
109
+ require .NotNil (t , metaCache )
110
+
111
+ metaServer := & metaserver.MetaServer {
112
+ MetaAgent : & agent.MetaAgent {
113
+ PodFetcher : & pod.PodFetcherStub {
114
+ PodList : podList ,
115
+ },
116
+ },
117
+ }
118
+
119
+ cpuServer , err := NewCPUServer (recvCh , sendCh , conf , metaCache , metaServer , metrics.DummyMetrics {})
120
+ require .NoError (t , err )
121
+ require .NotNil (t , cpuServer )
122
+
123
+ cpuServer .getCheckpointCalled = true
124
+
125
+ return cpuServer
126
+ }
127
+
101
128
func TestCPUServerStartAndStop (t * testing.T ) {
102
129
t .Parallel ()
103
130
@@ -259,17 +286,17 @@ func DeepCopyResponse(response *cpuadvisor.ListAndWatchResponse) (*cpuadvisor.Li
259
286
return copyResponse , nil
260
287
}
261
288
289
+ type ContainerInfo struct {
290
+ request * advisorsvc.ContainerMetadata
291
+ podInfo * v1.Pod
292
+ allocationInfo * cpuadvisor.AllocationInfo
293
+ isolated bool
294
+ regions sets.String
295
+ }
296
+
262
297
func TestCPUServerListAndWatch (t * testing.T ) {
263
298
t .Parallel ()
264
299
265
- type ContainerInfo struct {
266
- request * advisorsvc.ContainerMetadata
267
- podInfo * v1.Pod
268
- allocationInfo * cpuadvisor.AllocationInfo
269
- isolated bool
270
- regions sets.String
271
- }
272
-
273
300
tests := []struct {
274
301
name string
275
302
provision types.InternalCPUCalculationResult
@@ -1708,3 +1735,165 @@ func TestConcurrencyGetCheckpointAndAddContainer(t *testing.T) {
1708
1735
time .Sleep (10 * time .Second )
1709
1736
cancel ()
1710
1737
}
1738
+
1739
+ func TestCPUServerDropOldAdvice (t * testing.T ) {
1740
+ t .Parallel ()
1741
+
1742
+ cpuServer := newTestCPUServerWithChanBuffer (t , []* v1.Pod {})
1743
+ s := & mockCPUServerService_ListAndWatchServer {ResultsChan : make (chan * cpuadvisor.ListAndWatchResponse )}
1744
+ stop := make (chan struct {})
1745
+ recvCh := cpuServer .recvCh .(chan types.InternalCPUCalculationResult )
1746
+ recvCh <- types.InternalCPUCalculationResult {}
1747
+ go func () {
1748
+ err := cpuServer .ListAndWatch (& advisorsvc.Empty {}, s )
1749
+ assert .NoError (t , err , "failed to LW cpu server" )
1750
+ stop <- struct {}{}
1751
+ }()
1752
+ provision := types.InternalCPUCalculationResult {
1753
+ TimeStamp : time .Now (),
1754
+ PoolEntries : map [string ]map [int ]int {
1755
+ state .PoolNameReclaim : {
1756
+ 0 : 4 ,
1757
+ 1 : 8 ,
1758
+ },
1759
+ },
1760
+ }
1761
+ infos := []* ContainerInfo {
1762
+ {
1763
+ request : & advisorsvc.ContainerMetadata {
1764
+ PodUid : "pod1" ,
1765
+ ContainerName : "c1" ,
1766
+ Annotations : map [string ]string {
1767
+ consts .PodAnnotationMemoryEnhancementNumaBinding : consts .PodAnnotationMemoryEnhancementNumaBindingEnable ,
1768
+ },
1769
+ QosLevel : consts .PodAnnotationQoSLevelDedicatedCores ,
1770
+ },
1771
+ podInfo : & v1.Pod {
1772
+ ObjectMeta : metav1.ObjectMeta {
1773
+ Namespace : "default" ,
1774
+ Name : "pod1" ,
1775
+ UID : "pod1" ,
1776
+ Annotations : map [string ]string {
1777
+ consts .PodAnnotationQoSLevelKey : consts .PodAnnotationQoSLevelDedicatedCores ,
1778
+ consts .PodAnnotationMemoryEnhancementKey : "{\" numa_exclusive\" :true}" ,
1779
+ },
1780
+ },
1781
+ Spec : v1.PodSpec {
1782
+ Containers : []v1.Container {
1783
+ {
1784
+ Name : "c1" ,
1785
+ },
1786
+ },
1787
+ },
1788
+ },
1789
+ allocationInfo : & cpuadvisor.AllocationInfo {
1790
+ OwnerPoolName : state .PoolNameDedicated ,
1791
+ TopologyAwareAssignments : map [uint64 ]string {
1792
+ 0 : "0-3" ,
1793
+ 1 : "24-47" ,
1794
+ },
1795
+ },
1796
+ },
1797
+ }
1798
+ for _ , info := range infos {
1799
+ assert .NoError (t , cpuServer .addContainer (info .request ))
1800
+ assert .NoError (t , cpuServer .updateContainerInfo (info .request .PodUid , info .request .ContainerName , info .podInfo , info .allocationInfo ))
1801
+
1802
+ nodeInfo , _ := cpuServer .metaCache .GetContainerInfo (info .request .PodUid , info .request .ContainerName )
1803
+ nodeInfo .Isolated = info .isolated
1804
+ if info .regions .Len () > 0 {
1805
+ nodeInfo .RegionNames = info .regions
1806
+ }
1807
+ assert .NoError (t , cpuServer .metaCache .SetContainerInfo (info .request .PodUid , info .request .ContainerName , nodeInfo ))
1808
+ }
1809
+
1810
+ recvCh <- provision
1811
+ res := <- s .ResultsChan
1812
+ close (cpuServer .stopCh )
1813
+ <- stop
1814
+ copyres , err := DeepCopyResponse (res )
1815
+ assert .NoError (t , err )
1816
+ wantRes := & cpuadvisor.ListAndWatchResponse {
1817
+ Entries : map [string ]* cpuadvisor.CalculationEntries {
1818
+ state .PoolNameReclaim : {
1819
+ Entries : map [string ]* cpuadvisor.CalculationInfo {
1820
+ "" : {
1821
+ OwnerPoolName : state .PoolNameReclaim ,
1822
+ CalculationResultsByNumas : map [int64 ]* cpuadvisor.NumaCalculationResult {
1823
+ 0 : {
1824
+ Blocks : []* cpuadvisor.Block {
1825
+ {
1826
+ Result : 4 ,
1827
+ OverlapTargets : []* cpuadvisor.OverlapTarget {
1828
+ {
1829
+ OverlapTargetPodUid : "pod1" ,
1830
+ OverlapTargetContainerName : "c1" ,
1831
+ OverlapType : cpuadvisor .OverlapType_OverlapWithPod ,
1832
+ },
1833
+ },
1834
+ },
1835
+ },
1836
+ },
1837
+ 1 : {
1838
+ Blocks : []* cpuadvisor.Block {
1839
+ {
1840
+ Result : 8 ,
1841
+ OverlapTargets : []* cpuadvisor.OverlapTarget {
1842
+ {
1843
+ OverlapTargetPodUid : "pod1" ,
1844
+ OverlapTargetContainerName : "c1" ,
1845
+ OverlapType : cpuadvisor .OverlapType_OverlapWithPod ,
1846
+ },
1847
+ },
1848
+ },
1849
+ },
1850
+ },
1851
+ },
1852
+ },
1853
+ },
1854
+ },
1855
+ "pod1" : {
1856
+ Entries : map [string ]* cpuadvisor.CalculationInfo {
1857
+ "c1" : {
1858
+ OwnerPoolName : state .PoolNameDedicated ,
1859
+ CalculationResultsByNumas : map [int64 ]* cpuadvisor.NumaCalculationResult {
1860
+ 0 : {
1861
+ Blocks : []* cpuadvisor.Block {
1862
+ {
1863
+ Result : 4 ,
1864
+ OverlapTargets : []* cpuadvisor.OverlapTarget {
1865
+ {
1866
+ OverlapTargetPoolName : state .PoolNameReclaim ,
1867
+ OverlapType : cpuadvisor .OverlapType_OverlapWithPool ,
1868
+ },
1869
+ },
1870
+ },
1871
+ },
1872
+ },
1873
+ 1 : {
1874
+ Blocks : []* cpuadvisor.Block {
1875
+ {
1876
+ Result : 16 ,
1877
+ OverlapTargets : nil ,
1878
+ },
1879
+ {
1880
+ Result : 8 ,
1881
+ OverlapTargets : []* cpuadvisor.OverlapTarget {
1882
+ {
1883
+ OverlapTargetPoolName : state .PoolNameReclaim ,
1884
+ OverlapType : cpuadvisor .OverlapType_OverlapWithPool ,
1885
+ },
1886
+ },
1887
+ },
1888
+ },
1889
+ },
1890
+ },
1891
+ },
1892
+ },
1893
+ },
1894
+ },
1895
+ }
1896
+ if ! reflect .DeepEqual (copyres , wantRes ) {
1897
+ t .Errorf ("ListAndWatch()\n got = %+v, \n want= %+v" , general .ToString (copyres ), general .ToString (wantRes ))
1898
+ }
1899
+ }
0 commit comments