2023-08-09 09:08:05 -05:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"slices"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2023-08-14 12:51:04 -05:00
|
|
|
func (mip *AbstractMulticast) AddUniqueAndSort(hier string) {
|
2023-08-09 09:08:05 -05:00
|
|
|
|
|
|
|
if slices.Contains(mip.HierarchyArray, hier) {
|
2023-08-12 17:03:49 -05:00
|
|
|
log.Println("Element already in the array: ", hier)
|
2023-08-09 09:08:05 -05:00
|
|
|
} else {
|
|
|
|
mip.HierarchyArray = append(mip.HierarchyArray, hier)
|
2023-08-12 16:49:38 -05:00
|
|
|
log.Println("Here the numbers known:", mip.HierarchyArray)
|
2023-08-09 09:08:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-08-14 12:51:04 -05:00
|
|
|
func (mip *AbstractMulticast) IsAlpha(hier string) bool {
|
2023-08-09 09:08:05 -05:00
|
|
|
|
2023-08-14 07:18:51 -05:00
|
|
|
slices.Sort(mip.HierarchyArray)
|
|
|
|
indexMax := len(mip.HierarchyArray) - 1
|
|
|
|
if indexMax < 0 {
|
|
|
|
log.Println("Empty array , no elements")
|
|
|
|
return false
|
|
|
|
}
|
2023-08-12 16:49:38 -05:00
|
|
|
|
2023-08-14 07:18:51 -05:00
|
|
|
alpha := mip.HierarchyArray[indexMax]
|
|
|
|
log.Println("Maximum element is :", alpha)
|
|
|
|
log.Println("Array is :", mip.HierarchyArray)
|
2023-08-12 16:56:25 -05:00
|
|
|
return alpha == hier
|
2023-08-09 09:08:05 -05:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mip *AbstractMulticast) WriteNumberToMulticast(br AbstractBridge) {
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
fmt.Println("An error happened in <WriteNumberToMulticast()>, but Zoreide recovered. ")
|
|
|
|
fmt.Println("Error was: ", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
log.Println("Initiating ticker")
|
|
|
|
|
2023-08-14 12:51:04 -05:00
|
|
|
bstNumber := br.hIerarchyNumber
|
2023-08-09 09:08:05 -05:00
|
|
|
|
|
|
|
for range time.Tick(1 * time.Second) {
|
|
|
|
|
|
|
|
_, err := mip.Wconn.Write([]byte(bstNumber))
|
|
|
|
if err != nil {
|
|
|
|
log.Println("Cannot write to multicast:" + err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (mip *AbstractMulticast) ReadNumberFromMulticast() {
|
|
|
|
|
2023-08-14 04:52:07 -05:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
fmt.Println("An error happened in <ReadNumberToMulticast()>, but Zoreide recovered. ")
|
|
|
|
fmt.Println("Error was: ", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-08-09 09:08:05 -05:00
|
|
|
log.Println("Initiating reader")
|
|
|
|
buffer := make([]byte, mip.MaxDatagramSize)
|
|
|
|
|
|
|
|
// Loop forever reading from the socket
|
|
|
|
for {
|
|
|
|
|
|
|
|
_, _, err := mip.Rconn.ReadFromUDP(buffer)
|
|
|
|
if err != nil {
|
|
|
|
log.Println("ReadFromUDP failed:", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
BstChannel <- string(buffer)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *AbstractBridge) HierarchyReLocator(entity AbstractMulticast) {
|
|
|
|
|
2023-08-14 04:52:07 -05:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
fmt.Println("An error happened in <HierarchyLocator()>, but Zoreide recovered. ")
|
|
|
|
fmt.Println("Error was: ", r)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-08-13 05:26:21 -05:00
|
|
|
log.Println("Inizializing HierarchyManager")
|
2023-08-09 09:08:05 -05:00
|
|
|
|
2023-08-13 03:54:00 -05:00
|
|
|
entity.AddUniqueAndSort(b.hIerarchyNumber)
|
|
|
|
|
2023-08-09 09:08:05 -05:00
|
|
|
for bstNumber := range BstChannel {
|
2023-08-13 05:24:12 -05:00
|
|
|
|
2023-08-14 12:51:04 -05:00
|
|
|
if len(bstNumber) < 36 {
|
2023-08-09 09:08:05 -05:00
|
|
|
log.Println("Garbage received on multicast: ", bstNumber)
|
|
|
|
continue
|
|
|
|
} else {
|
2023-08-14 12:51:04 -05:00
|
|
|
log.Println("Adding received:", bstNumber)
|
|
|
|
entity.AddUniqueAndSort(bstNumber)
|
2023-08-09 09:08:05 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// finished feeding the new number
|
|
|
|
// if Alpha:
|
|
|
|
if entity.IsAlpha(b.hIerarchyNumber) {
|
2023-08-14 04:52:07 -05:00
|
|
|
if b.IsAssigned() {
|
2023-08-09 09:08:05 -05:00
|
|
|
log.Println("Still ALPHA. This is ok.")
|
|
|
|
} else {
|
|
|
|
log.Println("I'm the new ALPHA! Get out my path, losers!")
|
|
|
|
b.configureIpAndBridgeUp()
|
2023-08-14 04:52:07 -05:00
|
|
|
log.Println("Ip is active: ", b.IsActive())
|
2023-08-09 09:08:05 -05:00
|
|
|
}
|
2023-08-14 04:52:07 -05:00
|
|
|
// here we manage the case when we are not alpha
|
2023-08-09 09:08:05 -05:00
|
|
|
} else {
|
|
|
|
log.Println("GULP! There is a bigger one, better descalating")
|
2023-08-14 04:52:07 -05:00
|
|
|
if b.IsAssigned() {
|
|
|
|
log.Println("Start removing the IP from the interface:")
|
2023-08-09 09:08:05 -05:00
|
|
|
b.removeIPandBridgeInt()
|
2023-08-14 04:52:07 -05:00
|
|
|
log.Println("Removed.Ip is reachable: ", b.IsActive())
|
|
|
|
} else {
|
|
|
|
log.Println("Nothing to do, since IP is not assigned to the interface: ", b.ExistingInterface)
|
|
|
|
log.Println("Ip is reachable: ", b.IsActive())
|
2023-08-09 09:08:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *AbstractBridge) WaitAndClean(entity AbstractMulticast) {
|
|
|
|
|
2023-08-14 04:52:07 -05:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
fmt.Println("An error happened in <WaitAndClean()>, but Zoreide recovered. ")
|
|
|
|
fmt.Println("Error was: ", r)
|
|
|
|
}
|
|
|
|
}()
|
2023-08-09 09:08:05 -05:00
|
|
|
|
2023-08-14 04:52:07 -05:00
|
|
|
log.Println("Inizializing HA-Manager")
|
2023-08-10 13:39:26 -05:00
|
|
|
|
2023-08-09 09:08:05 -05:00
|
|
|
for {
|
2023-08-10 13:39:26 -05:00
|
|
|
|
2023-08-09 09:08:05 -05:00
|
|
|
pollTime := len(entity.HierarchyArray) + 1
|
|
|
|
time.Sleep(time.Duration(pollTime) * time.Second)
|
2023-08-14 04:52:07 -05:00
|
|
|
// Evitiamo di avere l'IP senza essere alpha
|
|
|
|
if b.IsAssigned() {
|
|
|
|
if entity.IsAlpha(b.hIerarchyNumber) {
|
|
|
|
log.Println("We are alpha and IP is assigned. All ok")
|
|
|
|
} else {
|
|
|
|
log.Println("Cannot have IP assigned without being alpha")
|
|
|
|
log.Println("Inconsistent situation: start removing the IP from the interface:")
|
|
|
|
b.removeIPandBridgeInt()
|
|
|
|
log.Println("Removed.Ip is still reachable: ", b.IsActive())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.IsActive() {
|
|
|
|
log.Println("Ip reachable, cluster OK")
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
log.Println("Situation non consistent, restart from green field.")
|
2023-08-10 13:39:26 -05:00
|
|
|
entity.HierarchyArray = entity.HierarchyArray[:0]
|
2023-08-12 17:03:49 -05:00
|
|
|
entity.HierarchyArray = slices.Clip(entity.HierarchyArray)
|
2023-08-13 03:54:00 -05:00
|
|
|
entity.AddUniqueAndSort(b.hIerarchyNumber)
|
2023-08-14 04:52:07 -05:00
|
|
|
log.Println("Restarted from green field: ", entity.HierarchyArray)
|
2023-08-10 13:39:26 -05:00
|
|
|
}
|
2023-08-13 03:54:00 -05:00
|
|
|
|
2023-08-09 09:08:05 -05:00
|
|
|
}
|
|
|
|
}
|