-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnewfile
79 lines (64 loc) · 2.43 KB
/
newfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package main
import (
"fmt"
"log"
)
// Your existing types and other code...
func distributeChunks(encodedData [][]byte, allResults [][]NodeListWithCost, loc string, dataShards, parityShards int, metadata *Metadata, api *ApiType) {
// ... (rest of your existing code)
var homeRegionNodes []NodeListWithCost
var homeRegionIndex int
var found bool
// Find the home region
for i, region := range allResults {
if len(region) > 0 && region[0].location == loc {
homeRegionNodes = region
homeRegionIndex = i
found = true
break
}
}
if !found {
log.Fatalf("Home region %s not found", loc)
return
}
totalShards := dataShards + parityShards
fmt.Println("Required number of nodes:", totalShards)
fmt.Println("Nodes in home region:", len(homeRegionNodes))
if len(homeRegionNodes) >= totalShards {
// If the home region has enough nodes
distributeData(encodedData, homeRegionNodes, metadata, api)
return
}
requiredAdditionalNodes := totalShards - len(homeRegionNodes)
allRegionsCount := len(allResults)
selectedNodes := homeRegionNodes
for offset := 1; requiredAdditionalNodes > 0; offset++ {
nextRegionIndex := (homeRegionIndex + offset) % allRegionsCount
prevRegionIndex := (homeRegionIndex - offset + allRegionsCount) % allRegionsCount
if nextRegionIndex != homeRegionIndex && len(allResults[nextRegionIndex]) > 0 {
nodesToTake := min(requiredAdditionalNodes, len(allResults[nextRegionIndex]))
selectedNodes = append(selectedNodes, allResults[nextRegionIndex][:nodesToTake]...)
requiredAdditionalNodes -= nodesToTake
fmt.Printf("%v Chunks will be stored in %v regions\n", nodesToTake, allResults[nextRegionIndex][0].location)
}
if requiredAdditionalNodes > 0 && prevRegionIndex != homeRegionIndex && len(allResults[prevRegionIndex]) > 0 {
nodesToTake := min(requiredAdditionalNodes, len(allResults[prevRegionIndex]))
selectedNodes = append(selectedNodes, allResults[prevRegionIndex][:nodesToTake]...)
requiredAdditionalNodes -= nodesToTake
fmt.Printf("%v Chunks will be stored in %v regions\n", nodesToTake, allResults[prevRegionIndex][0].location)
}
if nextRegionIndex == homeRegionIndex && prevRegionIndex == homeRegionIndex {
log.Println("Not enough nodes available even in the neighboring regions")
return
}
}
distributeData(encodedData, selectedNodes, metadata, api)
}
func min(a, b int) int {
if a < b {
return a
}
return b
}
// ... (rest of your existing code and functions)