-
Notifications
You must be signed in to change notification settings - Fork 192
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: verify Hrw function (obj to target) consistency in Python and Go
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
1 parent
de65d2b
commit 0ea054d
Showing
4 changed files
with
147 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}", | ||
) |