Skip to content

Commit

Permalink
improve octet count calculation
Browse files Browse the repository at this point in the history
Signed-off-by: heanlan <[email protected]>
  • Loading branch information
heanlan committed Aug 27, 2021
1 parent ff181f9 commit eefc63f
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 16 deletions.
15 changes: 2 additions & 13 deletions pkg/intermediate/aggregate.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/vmware/go-ipfix/pkg/entities"
"github.com/vmware/go-ipfix/pkg/registry"
"github.com/vmware/go-ipfix/pkg/util"
)

var (
Expand Down Expand Up @@ -514,19 +515,7 @@ func (a *AggregationProcess) aggregateRecords(incomingRecord, existingRecord ent
if ieWithValue, _, exist := incomingRecord.GetInfoElementWithValue(element); exist {
existingIeWithValue, index, _ := existingRecord.GetInfoElementWithValue(element)
// Update the corresponding element in existing record.
if !isDelta {
if existingIeWithValue.Value.(uint64) < ieWithValue.Value.(uint64) {
existingIeWithValue.Value = ieWithValue.Value
}
} else {
// We are simply adding the delta stats now. We expect delta stats to be
// reset after sending the record from flowKeyMap in aggregation process.
// Delta stats from source and destination nodes are added, so we will have
// two times the stats approximately.
// For delta stats, it is better to use source and destination specific
// stats.
existingIeWithValue.Value = existingIeWithValue.Value.(uint64) + ieWithValue.Value.(uint64)
}
existingIeWithValue.Value = util.MaxUint64(existingIeWithValue.Value.(uint64), ieWithValue.Value.(uint64))
if err := existingRecord.SetInfoElementWithValue(index, *existingIeWithValue); err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/intermediate/aggregate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

"github.com/vmware/go-ipfix/pkg/entities"
"github.com/vmware/go-ipfix/pkg/registry"
"github.com/vmware/go-ipfix/pkg/util"
)

var (
Expand Down Expand Up @@ -918,7 +919,7 @@ func runAggregationAndCheckResult(t *testing.T, ap *AggregationProcess, srcRecor
assert.Equalf(t, latestRecord.Value, ieWithValue.Value, "values should be equal for element %v", e)
} else {
prevRecord, _, _ := srcRecordLatest.GetInfoElementWithValue(e)
assert.Equalf(t, prevRecord.Value.(uint64)+latestRecord.Value.(uint64), ieWithValue.Value, "values should be equal for element %v", e)
assert.Equalf(t, util.MaxUint64(prevRecord.Value.(uint64), latestRecord.Value.(uint64)), ieWithValue.Value, "values should be equal for element %v", e)
}
}
for i, e := range antreaSourceStatsElementList {
Expand Down
4 changes: 2 additions & 2 deletions pkg/test/collector_intermediate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,15 +187,15 @@ func testCollectorToIntermediate(t *testing.T, address net.Addr, isIPv6 bool) {
case "packetTotalCount":
assert.Equal(t, uint64(1000), element.Value)
case "packetDeltaCount":
assert.Equal(t, uint64(1000), element.Value)
assert.Equal(t, uint64(500), element.Value)
case "destinationClusterIPv4":
assert.Equal(t, net.IP{10, 0, 0, 3}, element.Value)
case "destinationClusterIPv6":
assert.Equal(t, net.IP{0x20, 0x1, 0x0, 0x0, 0x32, 0x38, 0xbb, 0xbb, 0x0, 0x63, 0x0, 0x0, 0x0, 0x0, 0xaa, 0xaa}, element.Value)
case "destinationServicePort":
assert.Equal(t, uint16(4739), element.Value)
case "reversePacketDeltaCount":
assert.Equal(t, uint64(350), element.Value)
assert.Equal(t, uint64(200), element.Value)
case "reversePacketTotalCount":
assert.Equal(t, uint64(400), element.Value)
case "packetTotalCountFromSourceNode":
Expand Down
7 changes: 7 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,10 @@ func Decode(buffer io.Reader, byteOrder binary.ByteOrder, outputs ...interface{}
}
return nil
}

func MaxUint64(num1, num2 uint64) uint64 {
if num1 >= num2 {
return num1
}
return num2
}

0 comments on commit eefc63f

Please sign in to comment.