Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix: marshal checkpoint by json #466

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pkg/agent/orm/checkpoint/checkpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
"github.com/stretchr/testify/assert"
)

var testCheckpointData = `{"Data":{"PodResourceEntries":[{"PodUID":"447b997f-26a2-4e46-8370-d7af4cc78ad2","ContainerName":"liveness-probe","ResourceName":"memory","AllocationInfo":"\n\nCpusetMems\u0018\u0001*\u00010"},{"PodUID":"447b997f-26a2-4e46-8370-d7af4cc78ad2","ContainerName":"liveness-probe","ResourceName":"cpu","AllocationInfo":"\n\nCpusetCpus\u0018\u0001!\u0000\u0000\u0000\u0000\u0000\u0000\u001c@*\u00031-7"},{"PodUID":"2df9cabd-5212-4436-a089-89fcab46bfc7","ContainerName":"server","ResourceName":"cpu","AllocationInfo":"\n\nCpusetCpus\u0018\u0001!\u0000\u0000\u0000\u0000\u0000\u0000\u0010@*\u00034-7"},{"PodUID":"2df9cabd-5212-4436-a089-89fcab46bfc7","ContainerName":"server","ResourceName":"memory","AllocationInfo":"\n\nCpusetMems\u0018\u0001*\u00010"},{"PodUID":"46d9160c-096e-4d63-8b94-766acc9062fc","ContainerName":"kube-flannel","ResourceName":"memory","AllocationInfo":"\n\nCpusetMems\u0018\u0001*\u00010"},{"PodUID":"0eeeaa35-b954-4498-bf63-6cf6cfbdc620","ContainerName":"server","ResourceName":"memory","AllocationInfo":"\n\nCpusetMems\u0018\u0001*\u00010"},{"PodUID":"0eeeaa35-b954-4498-bf63-6cf6cfbdc620","ContainerName":"server","ResourceName":"cpu","AllocationInfo":"\n\nCpusetCpus\u0018\u0001!\u0000\u0000\u0000\u0000\u0000\u0000\u0010@*\u00034-7"},{"PodUID":"95df62b2-87a7-49f6-91b7-c801f9630c93","ContainerName":"inspector","ResourceName":"memory","AllocationInfo":"\n\nCpusetMems\u0018\u0001*\u00010"},{"PodUID":"6d37ac5f-ff2c-4afa-8df7-18cd06ec53a7","ContainerName":"coredns","ResourceName":"memory","AllocationInfo":"\n\nCpusetMems\u0018\u0001*\u00010"},{"PodUID":"6d37ac5f-ff2c-4afa-8df7-18cd06ec53a7","ContainerName":"coredns","ResourceName":"cpu","AllocationInfo":"\n\nCpusetCpus\u0018\u0001!\u0000\u0000\u0000\u0000\u0000\u0000\u0010@*\u00034-7"},{"PodUID":"185fec2a-69eb-4af3-aa95-967837c5d527","ContainerName":"csi-provisioner","ResourceName":"cpu","AllocationInfo":"\n\nCpusetCpus\u0018\u0001!\u0000\u0000\u0000\u0000\u0000\u0000\u0010@*\u00034-7"},{"PodUID":"185fec2a-69eb-4af3-aa95-967837c5d527","ContainerName":"csi-provisioner","ResourceName":"memory","AllocationInfo":"\n\nCpusetMems\u0018\u0001*\u00010"},{"PodUID":"185fec2a-69eb-4af3-aa95-967837c5d527","ContainerName":"csi-attacher","ResourceName":"memory","AllocationInfo":"\n\nCpusetMems\u0018\u0001*\u00010"},{"PodUID":"185fec2a-69eb-4af3-aa95-967837c5d527","ContainerName":"csi-attacher","ResourceName":"cpu","AllocationInfo":"\n\nCpusetCpus\u0018\u0001!\u0000\u0000\u0000\u0000\u0000\u0000\u0010@*\u00034-7"},{"PodUID":"185fec2a-69eb-4af3-aa95-967837c5d527","ContainerName":"liveness-probe","ResourceName":"cpu","AllocationInfo":"\n\nCpusetCpus\u0018\u0001!\u0000\u0000\u0000\u0000\u0000\u0000\u0010@*\u00034-7"},{"PodUID":"185fec2a-69eb-4af3-aa95-967837c5d527","ContainerName":"liveness-probe","ResourceName":"memory","AllocationInfo":"\n\nCpusetMems\u0018\u0001*\u00010"},{"PodUID":"185fec2a-69eb-4af3-aa95-967837c5d527","ContainerName":"csi-nas-driver","ResourceName":"memory","AllocationInfo":"\n\nCpusetMems\u0018\u0001*\u00010"},{"PodUID":"185fec2a-69eb-4af3-aa95-967837c5d527","ContainerName":"csi-nas-driver","ResourceName":"cpu","AllocationInfo":"\n\nCpusetCpus\u0018\u0001!\u0000\u0000\u0000\u0000\u0000\u0000\u0010@*\u00034-7"},{"PodUID":"0893ea73-8650-4067-9097-95faf85b2a16","ContainerName":"testcontainer","ResourceName":"cpu","AllocationInfo":"\n\nCpusetCpus\u0018\u0001!\u0000\u0000\u0000\u0000\u0000\u0000\u0010@*\u00034-7"},{"PodUID":"0893ea73-8650-4067-9097-95faf85b2a16","ContainerName":"testcontainer","ResourceName":"memory","AllocationInfo":"\n\nCpusetMems\u0018\u0001*\u00010"},{"PodUID":"743fb5e0-bba1-47fb-81d6-8e38136b5453","ContainerName":"liveness-probe","ResourceName":"memory","AllocationInfo":"\n\nCpusetMems\u0018\u0001*\u00010"}]},"Checksum":3996144592}`
var testCheckpointData = `{"Data":{"PodResourceEntries":[{"PodUID":"62c9e5d2-b2a9-496f-a1eb-ad0f4f02ba5c","ContainerName":"testcontainer","ResourceName":"cpu","AllocationInfo":"{\"oci_property_name\":\"CpusetCpus\",\"is_scalar_resource\":true,\"allocated_quantity\":2,\"allocation_result\":\"0-1\",\"resource_hints\":{\"hints\":[{\"nodes\":[0],\"preferred\":true}]}}"},{"PodUID":"62c9e5d2-b2a9-496f-a1eb-ad0f4f02ba5c","ContainerName":"testcontainer","ResourceName":"memory","AllocationInfo":"{\"oci_property_name\":\"CpusetMems\",\"is_scalar_resource\":true,\"allocated_quantity\":4294967296,\"allocation_result\":\"0\",\"resource_hints\":{\"hints\":[{\"nodes\":[0],\"preferred\":true}]}}"}]},"Checksum":4217255668}`

func TestData_UnmarshalCheckpoint(t *testing.T) {
t.Parallel()
Expand All @@ -41,5 +41,5 @@ func TestData_UnmarshalCheckpoint(t *testing.T) {
assert.NoError(t, err)

entries = data.GetData()
assert.Equal(t, 21, len(entries))
assert.Equal(t, 2, len(entries))
}
5 changes: 3 additions & 2 deletions pkg/agent/orm/pod_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package orm

import (
"encoding/json"
"reflect"
"sync"

Expand Down Expand Up @@ -194,7 +195,7 @@ func (pres *podResourcesChk) toCheckpointData() []checkpoint.PodResourcesEntry {
for podUID, containerResources := range pres.resources {
for conName, resourcesAllocation := range containerResources {
for resourceName, allocationInfo := range resourcesAllocation {
allocRespBytes, err := allocationInfo.Marshal()
allocRespBytes, err := json.Marshal(allocationInfo)
if err != nil {
klog.Errorf("Can't marshal allocationInfo for %v %v %v: %v", podUID, conName, resourceName, err)
continue
Expand All @@ -216,7 +217,7 @@ func (pres *podResourcesChk) fromCheckpointData(data []checkpoint.PodResourcesEn
klog.V(2).Infof("Get checkpoint entry: %s %s %s %s\n",
entry.PodUID, entry.ContainerName, entry.ResourceName, entry.AllocationInfo)
allocationInfo := &pluginapi.ResourceAllocationInfo{}
err := allocationInfo.Unmarshal([]byte(entry.AllocationInfo))
err := json.Unmarshal([]byte(entry.AllocationInfo), allocationInfo)
if err != nil {
klog.Errorf("Can't unmarshal allocationInfo for %s %s %s %s: %v",
entry.PodUID, entry.ContainerName, entry.ResourceName, entry.AllocationInfo, err)
Expand Down
79 changes: 79 additions & 0 deletions pkg/agent/orm/pod_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ import (

"github.com/stretchr/testify/assert"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"
pluginapi "k8s.io/kubelet/pkg/apis/resourceplugin/v1alpha1"

"github.com/kubewharf/katalyst-core/pkg/agent/orm/checkpoint"
)

func TestPodResources(t *testing.T) {
Expand Down Expand Up @@ -74,6 +77,82 @@ func TestPodResources(t *testing.T) {
assert.Equal(t, len(containerAllResources), 0)
}

func TestCheckpointMarshal(t *testing.T) {
/* ---- use allocationInfo.Marshal ----- */
podResources := newPodResourcesChk()
podResources.resources = map[string]ContainerResources{
"testPodUid": map[string]ResourceAllocation{
"testContainer": map[string]*pluginapi.ResourceAllocationInfo{
"memory": {
OciPropertyName: "CpusetMems",
IsNodeResource: true,
IsScalarResource: true,
AllocatedQuantity: 400000000, // more than 1000
AllocationResult: "0",
},
},
},
}

var entries []checkpoint.PodResourcesEntry
for podUID, containerResources := range podResources.resources {
for conName, resourcesAllocation := range containerResources {
for resourceName, allocationInfo := range resourcesAllocation {
// use allocationInfo.Marshal()
allocRespBytes, err := allocationInfo.Marshal()
if err != nil {
klog.Errorf("Can't marshal allocationInfo for %v %v %v: %v", podUID, conName, resourceName, err)
continue
}
entries = append(entries, checkpoint.PodResourcesEntry{
PodUID: podUID,
ContainerName: conName,
ResourceName: resourceName,
AllocationInfo: string(allocRespBytes)})
}
}
}
data := checkpoint.New(entries)
value, err := data.MarshalCheckpoint()
assert.NoError(t, err)

resEntries := make([]checkpoint.PodResourcesEntry, 0)
cp := checkpoint.New(resEntries)
err = cp.UnmarshalCheckpoint(value)
assert.NoError(t, err)
// we just marshal and unmarshal the data, checksum should be the same.
err = cp.VerifyChecksum()
assert.Error(t, err) // here we got an error, actually we can not get checkpoint data from file.

/* ---- use json.Marshal ----- */
podResources = newPodResourcesChk()
podResources.resources = map[string]ContainerResources{
"testPodUid": map[string]ResourceAllocation{
"testContainer": map[string]*pluginapi.ResourceAllocationInfo{
"memory": {
OciPropertyName: "CpusetMems",
IsNodeResource: true,
IsScalarResource: true,
AllocatedQuantity: 400000000, // more than 1000
AllocationResult: "0",
},
},
},
}

data = checkpoint.New(podResources.toCheckpointData()) // the only difference is than we use json.Marshal
value, err = data.MarshalCheckpoint()
assert.NoError(t, err)

resEntries = make([]checkpoint.PodResourcesEntry, 0)
cp = checkpoint.New(resEntries)
err = cp.UnmarshalCheckpoint(value)
assert.NoError(t, err)
// we just marshal and unmarshal the data, checksum should be the same.
err = cp.VerifyChecksum()
assert.NoError(t, err)
}

func generateResourceAllocationInfo() *pluginapi.ResourceAllocationInfo {
return &pluginapi.ResourceAllocationInfo{
OciPropertyName: "CpusetCpus",
Expand Down
Loading