whitepaper: 352/169 — wave4 MEDIUM (hadoop/hbase/nova/neutron/openstack) + fix odl-0002 dup
This commit is contained in:
parent
9934133dcf
commit
835ae73b0f
82 changed files with 5931 additions and 6 deletions
148
defects/nova/unit/NovaAffinityFilterAlgorithm.java
Normal file
148
defects/nova/unit/NovaAffinityFilterAlgorithm.java
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
package unit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* CWE-407 unit test: nova-0001
|
||||
* _GroupAffinityFilter.host_passes() group_hosts list vs set membership.
|
||||
*
|
||||
* Slow: group_hosts is a List — O(G) contains per host evaluated.
|
||||
* Total for H hosts: O(H * G).
|
||||
* Fast: group_hosts is a Set — O(1) contains per host.
|
||||
* Total for H hosts: O(H).
|
||||
*/
|
||||
public class NovaAffinityFilterAlgorithm {
|
||||
|
||||
// Simulate defective host_passes — list membership
|
||||
static long hostPassesSlowBatch(List<String> groupHosts, List<String> candidateHosts) {
|
||||
long ops = 0;
|
||||
for (String host : candidateHosts) {
|
||||
ops++; // per-host call
|
||||
// O(G) scan of list
|
||||
for (String gh : groupHosts) {
|
||||
ops++;
|
||||
if (gh.equals(host)) break;
|
||||
}
|
||||
}
|
||||
return ops;
|
||||
}
|
||||
|
||||
// Simulate fixed host_passes — set membership
|
||||
static long hostPassesFastBatch(Set<String> groupHosts, List<String> candidateHosts) {
|
||||
long ops = 0;
|
||||
for (String host : candidateHosts) {
|
||||
ops++; // O(1) set lookup (not counted separately, just loop iteration)
|
||||
@SuppressWarnings("unused")
|
||||
boolean b = groupHosts.contains(host); // O(1)
|
||||
}
|
||||
return ops;
|
||||
}
|
||||
|
||||
// Simulate defective policies check — list membership per host
|
||||
static long policyCheckSlowBatch(List<String> policies, String targetPolicy,
|
||||
List<String> candidateHosts) {
|
||||
long ops = 0;
|
||||
for (@SuppressWarnings("unused") String host : candidateHosts) {
|
||||
ops++;
|
||||
for (String p : policies) { // O(P) per host
|
||||
ops++;
|
||||
if (p.equals(targetPolicy)) break;
|
||||
}
|
||||
}
|
||||
return ops;
|
||||
}
|
||||
|
||||
// Fixed policies check — set membership per host
|
||||
static long policyCheckFastBatch(Set<String> policies, String targetPolicy,
|
||||
List<String> candidateHosts) {
|
||||
long ops = 0;
|
||||
for (@SuppressWarnings("unused") String host : candidateHosts) {
|
||||
ops++; // O(1) set lookup
|
||||
@SuppressWarnings("unused")
|
||||
boolean b = policies.contains(targetPolicy);
|
||||
}
|
||||
return ops;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int H = 500; // candidate hosts per scheduling request
|
||||
int G = 200; // group members
|
||||
int P = 10; // policies per instance group
|
||||
|
||||
int passed = 0;
|
||||
int total = 0;
|
||||
|
||||
// Build candidate hosts
|
||||
List<String> candidateHosts = new ArrayList<>();
|
||||
for (int i = 0; i < H; i++) {
|
||||
candidateHosts.add("compute-" + i);
|
||||
}
|
||||
|
||||
// Build group_hosts (half are in the candidate list)
|
||||
List<String> groupHostsList = new ArrayList<>();
|
||||
Set<String> groupHostsSet = new HashSet<>();
|
||||
for (int i = 0; i < G; i++) {
|
||||
String h = "compute-" + (i * 2); // every other host
|
||||
groupHostsList.add(h);
|
||||
groupHostsSet.add(h);
|
||||
}
|
||||
|
||||
// Build policies
|
||||
List<String> policiesList = new ArrayList<>();
|
||||
Set<String> policiesSet = new HashSet<>();
|
||||
for (int i = 0; i < P; i++) {
|
||||
policiesList.add("policy-" + i);
|
||||
policiesSet.add("policy-" + i);
|
||||
}
|
||||
String targetPolicy = "policy-" + (P - 1); // worst-case last element
|
||||
|
||||
// Test 1: group_hosts membership — slow vs fast
|
||||
long slowOps = hostPassesSlowBatch(groupHostsList, candidateHosts);
|
||||
long fastOps = hostPassesFastBatch(groupHostsSet, candidateHosts);
|
||||
total++;
|
||||
assert slowOps > fastOps * 10 :
|
||||
"group_hosts: slow=" + slowOps + " fast=" + fastOps + " speedup insufficient";
|
||||
System.out.println("Test 1 PASS: group_hosts membership slow=" + slowOps +
|
||||
" ops, fast=" + fastOps + " ops, speedup=" + (slowOps / Math.max(1, fastOps)) + "x");
|
||||
passed++;
|
||||
|
||||
// Test 2: policies membership — slow vs fast
|
||||
long slowPolOps = policyCheckSlowBatch(policiesList, targetPolicy, candidateHosts);
|
||||
long fastPolOps = policyCheckFastBatch(policiesSet, targetPolicy, candidateHosts);
|
||||
total++;
|
||||
assert slowPolOps > fastPolOps * 2 :
|
||||
"policies: slow=" + slowPolOps + " fast=" + fastPolOps + " speedup insufficient";
|
||||
System.out.println("Test 2 PASS: policies check slow=" + slowPolOps +
|
||||
" ops, fast=" + fastPolOps + " ops, speedup=" + (slowPolOps / Math.max(1, fastPolOps)) + "x");
|
||||
passed++;
|
||||
|
||||
// Test 3: correctness — same hosts pass/fail
|
||||
int slowPass = 0, fastPass = 0;
|
||||
for (String host : candidateHosts) {
|
||||
if (groupHostsList.contains(host)) slowPass++;
|
||||
if (groupHostsSet.contains(host)) fastPass++;
|
||||
}
|
||||
total++;
|
||||
assert slowPass == fastPass :
|
||||
"pass counts differ: slow=" + slowPass + " fast=" + fastPass;
|
||||
System.out.println("Test 3 PASS: group_hosts filter results agree (" + slowPass + " hosts pass)");
|
||||
passed++;
|
||||
|
||||
// Test 4: policy correctness
|
||||
int slowPolicyPass = 0, fastPolicyPass = 0;
|
||||
for (@SuppressWarnings("unused") String host : candidateHosts) {
|
||||
if (policiesList.contains(targetPolicy)) slowPolicyPass++;
|
||||
if (policiesSet.contains(targetPolicy)) fastPolicyPass++;
|
||||
}
|
||||
total++;
|
||||
assert slowPolicyPass == fastPolicyPass :
|
||||
"policy pass counts differ: slow=" + slowPolicyPass + " fast=" + fastPolicyPass;
|
||||
System.out.println("Test 4 PASS: policy check results agree");
|
||||
passed++;
|
||||
|
||||
System.out.println(passed + "/" + total + " PASS");
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue