Skip to content

Commit

Permalink
test: verify Hrw function (obj to target) consistency in Python and Go
Browse files Browse the repository at this point in the history
the test is triggered from Makefile with three steps:
1. generate random object unames and calculate corresponding Hrw from `scenario_gen.go`
2. save scenarios with uname-to-hash mappings and the smap setup to temporary json files
3. read tmp files in Python test to re-calculate and compare the resulting hash values

temporary json file snippet:
```
{
    "smap": {...},
    "hwr_map": {
        "object_uname_1": mapped_target_daemon_id,
        "object_uname_2": mapped_target_daemon_id,
        ...
    },
}
```

Signed-off-by: Tony Chen <[email protected]>
  • Loading branch information
gaikwadabhishek authored and Nahemah1022 committed Jan 30, 2025
1 parent de65d2b commit 0ea054d
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 1 deletion.
7 changes: 6 additions & 1 deletion python/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@

SHELL := /bin/bash
PYTHON = python3
GO = go
PYTEST = $(PYTHON) -m pytest
PIP = pip3
SDK_DOCFILE := ../docs/python_sdk.md
Expand Down Expand Up @@ -39,6 +40,10 @@ botocore_deps:
s3_test_deps:
$(PIP) install --upgrade -r tests/s3compat/requirements --quiet

.PHONY: python_unit_tests_scenario_gen
python_unit_tests_scenario_gen:
@ $(GO) run tests/unit/sdk/hrw/scenario_gen.go

.PHONY: python_tests
python_tests: common_deps dev_deps botocore_deps python_sdk_tests python_etl_tests python_botocore_tests

Expand All @@ -51,7 +56,7 @@ python_sdk_integration_tests: common_deps
$(PYTEST) -v tests/integration/sdk -m "not etl and not authn"

.PHONY: python_sdk_unit_tests
python_sdk_unit_tests: common_deps
python_sdk_unit_tests: common_deps python_unit_tests_scenario_gen
$(PYTEST) -v tests/unit/sdk

.PHONY: python_etl_tests
Expand Down
Empty file.
99 changes: 99 additions & 0 deletions python/tests/unit/sdk/hrw/scenario_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"encoding/json"
"fmt"
"os"

"github.com/NVIDIA/aistore/api/apc"
"github.com/NVIDIA/aistore/cmn/cos"
"github.com/NVIDIA/aistore/core/meta"
)

const (
lenNodeID = 13
lenUname = 80
)

type (
Scenario struct {
Smap meta.Smap `json:"smap"`
HrwMap map[string]string `json:"hrw_map"`
}
TestCase struct {
numNodes int
numObjs int
}
)

// generateScenario creates a scenario JSON file for the given test case
func (t *TestCase) generateScenario() (err error) {
var outfile *os.File

filename := fmt.Sprintf("./scenario-%d-nodes-%d-objs.json", t.numNodes, t.numObjs)
outfile, err = os.Create(filename)
if err != nil {
return fmt.Errorf("failed to create file %s: %w", filename, err)
}

defer outfile.Close()

// 1. Initialize targets in smap
scenario := Scenario{
Smap: meta.Smap{
Tmap: make(meta.NodeMap, t.numNodes),
Pmap: make(meta.NodeMap, 1),
},
HrwMap: make(map[string]string, t.numNodes),
}
for range t.numNodes {
tname := cos.CryptoRandS(lenNodeID)
target := &meta.Snode{}
scenario.Smap.Tmap[tname] = target
target.Init(tname, apc.Target)
}

// 2. Initialize proxy in smap
pname := cos.CryptoRandS(lenNodeID)
proxy := &meta.Snode{}
scenario.Smap.Pmap[pname] = proxy
proxy.Init(pname, apc.Proxy)

scenario.Smap.Primary = proxy

// 3. Initialize objects
for range t.numObjs {
uname := cos.CryptoRandS(lenUname)
snode, err := scenario.Smap.HrwName2T(cos.UnsafeB(uname))
if err != nil {
return fmt.Errorf("failed to find target Snode %s: %w", uname, err)
}
scenario.HrwMap[uname] = snode.DaeID
}

jsonData, err := json.Marshal(scenario)
if err != nil {
return fmt.Errorf("failed to Marshal to JSON format: %w", err)
}

if _, err = outfile.Write(jsonData); err != nil {
return fmt.Errorf("failed to write: %s: %w", filename, err)
}

return nil
}

func main() {
tests := []TestCase{
{5, 10},
{2, 100},
{3, 1000},
{10, 10},
}

for _, test := range tests {
if err := test.generateScenario(); err != nil {
cos.AssertNoErr(err)
}
}
}
42 changes: 42 additions & 0 deletions python/tests/unit/sdk/hrw/test_hrw.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import unittest
import json
from pathlib import Path

from aistore.sdk.types import Smap


class TestHRW(unittest.TestCase):
def setUp(self):
self.scenario_dir = Path("./")
self.scenario_files = list(self.scenario_dir.glob("scenario-*"))

def tearDown(self):
for scenario_file in self.scenario_files:
scenario_file.unlink(missing_ok=True)

def test_compare_hrw(self):
for scenario_file in self.scenario_files:
with self.subTest(scenario_file=scenario_file):
self.process_scenario_file(scenario_file)

def process_scenario_file(self, scenario_file):
"""
Processes a scenario file by loading its JSON content, parsing the Smap structure,
and verifying the target node assignments using HRW mapping.
Args:
scenario_file (Path): The path to the scenario JSON file.
Raises:
AssertionError: If the computed target node does not match the expected target node.
"""
with open(scenario_file, mode="r", encoding="utf-8") as f:
scenario = json.load(f)
smap = Smap.parse_obj(scenario["smap"])
for uname, expected_tname in scenario["hrw_map"].items():
snode = smap.get_target_for_object(uname)
self.assertEqual(
snode.daemon_id,
expected_tname,
msg=f"Mismatch in {scenario_file.name}: {uname}, expected {expected_tname}, got {snode.daemon_id}",
)

0 comments on commit 0ea054d

Please sign in to comment.