neo local peer draft
This commit is contained in:
parent
1a55212378
commit
4de43a301c
@ -8,7 +8,6 @@ import (
|
|||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||||
"cloud.o-forge.io/core/oc-lib/logs"
|
"cloud.o-forge.io/core/oc-lib/logs"
|
||||||
"cloud.o-forge.io/core/oc-lib/static"
|
|
||||||
"github.com/rs/zerolog"
|
"github.com/rs/zerolog"
|
||||||
|
|
||||||
"go.mongodb.org/mongo-driver/bson"
|
"go.mongodb.org/mongo-driver/bson"
|
||||||
@ -132,10 +131,6 @@ func (m *MongoDB) prepareDB(list_collection []string, config MongoConf) {
|
|||||||
new_collection := mngoDB.Collection(collection_name)
|
new_collection := mngoDB.Collection(collection_name)
|
||||||
if _, exists := collectionMap[collection_name]; !exists {
|
if _, exists := collectionMap[collection_name]; !exists {
|
||||||
m.createCollection(collection_name, new_collection)
|
m.createCollection(collection_name, new_collection)
|
||||||
if collection_name == "peer" {
|
|
||||||
id, p := static.GetMyLocalBsonPeer()
|
|
||||||
m.StoreOne(p, id, collection_name)
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
CollectionMap[collection_name] = new_collection
|
CollectionMap[collection_name] = new_collection
|
||||||
}
|
}
|
||||||
|
@ -11,7 +11,6 @@ import (
|
|||||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
w "cloud.o-forge.io/core/oc-lib/models/workflow"
|
w "cloud.o-forge.io/core/oc-lib/models/workflow"
|
||||||
"cloud.o-forge.io/core/oc-lib/models/workspace"
|
"cloud.o-forge.io/core/oc-lib/models/workspace"
|
||||||
"cloud.o-forge.io/core/oc-lib/static"
|
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -155,7 +154,7 @@ func (wfa *collaborativeAreaMongoAccessor) deleteToPeer(shared *CollaborativeAre
|
|||||||
}
|
}
|
||||||
paccess := (&peer.Peer{})
|
paccess := (&peer.Peer{})
|
||||||
for _, v := range shared.Peers {
|
for _, v := range shared.Peers {
|
||||||
if (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: v}}).IsMySelf() {
|
if ok, _ := (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: v}}).IsMySelf(); ok {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
b, err := paccess.LaunchPeerExecution(v, shared.UUID, tools.COLLABORATIVE_AREA, tools.DELETE, nil, wfa.Caller)
|
b, err := paccess.LaunchPeerExecution(v, shared.UUID, tools.COLLABORATIVE_AREA, tools.DELETE, nil, wfa.Caller)
|
||||||
@ -173,7 +172,7 @@ func (wfa *collaborativeAreaMongoAccessor) sendToPeer(shared *CollaborativeArea)
|
|||||||
|
|
||||||
paccess := (&peer.Peer{})
|
paccess := (&peer.Peer{})
|
||||||
for _, v := range shared.Peers {
|
for _, v := range shared.Peers {
|
||||||
if (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: v}}).IsMySelf() || shared.IsSent {
|
if ok, _ := (&peer.Peer{AbstractObject: utils.AbstractObject{UUID: v}}).IsMySelf(); ok || shared.IsSent {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
shared.IsSent = true
|
shared.IsSent = true
|
||||||
@ -196,7 +195,7 @@ func (wfa *collaborativeAreaMongoAccessor) UpdateOne(set utils.DBObject, id stri
|
|||||||
|
|
||||||
// StoreOne stores a collaborative area in the database, it automatically share to peers if the workspace is shared
|
// StoreOne stores a collaborative area in the database, it automatically share to peers if the workspace is shared
|
||||||
func (wfa *collaborativeAreaMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
func (wfa *collaborativeAreaMongoAccessor) StoreOne(data utils.DBObject) (utils.DBObject, int, error) {
|
||||||
id, _ := static.GetMyLocalJsonPeer() // get the local peer
|
_, id := (&peer.Peer{}).IsMySelf() // get the local peer
|
||||||
data.(*CollaborativeArea).CreatorID = id // set the creator id
|
data.(*CollaborativeArea).CreatorID = id // set the creator id
|
||||||
data.(*CollaborativeArea).Peers = append(data.(*CollaborativeArea).Peers, id) // add the creator id to the peers
|
data.(*CollaborativeArea).Peers = append(data.(*CollaborativeArea).Peers, id) // add the creator id to the peers
|
||||||
// then reset the shared fields
|
// then reset the shared fields
|
||||||
|
@ -5,7 +5,6 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/models/utils"
|
"cloud.o-forge.io/core/oc-lib/models/utils"
|
||||||
"cloud.o-forge.io/core/oc-lib/static"
|
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
@ -21,6 +20,14 @@ const (
|
|||||||
BLACKLIST
|
BLACKLIST
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func (m PeerState) String() string {
|
||||||
|
return [...]string{"NONE", "SELF", "PARTNER", "BLACKLIST"}[m]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m PeerState) EnumIndex() int {
|
||||||
|
return int(m)
|
||||||
|
}
|
||||||
|
|
||||||
// Peer is a struct that represents a peer
|
// Peer is a struct that represents a peer
|
||||||
type Peer struct {
|
type Peer struct {
|
||||||
utils.AbstractObject
|
utils.AbstractObject
|
||||||
@ -57,9 +64,13 @@ func (ao *Peer) RemoveExecution(exec PeerExecution) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// IsMySelf checks if the peer is the local peer
|
// IsMySelf checks if the peer is the local peer
|
||||||
func (ao *Peer) IsMySelf() bool {
|
func (ao *Peer) IsMySelf() (bool, string) {
|
||||||
id, _ := static.GetMyLocalJsonPeer()
|
d, code, err := ao.GetAccessor(nil).Search(nil, SELF.String())
|
||||||
return ao.UUID == id
|
if code != 200 || err != nil || len(d) == 0 {
|
||||||
|
return false, ""
|
||||||
|
}
|
||||||
|
id := d[0].GetID()
|
||||||
|
return ao.UUID == id, id
|
||||||
}
|
}
|
||||||
|
|
||||||
// LaunchPeerExecution launches an execution on a peer
|
// LaunchPeerExecution launches an execution on a peer
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package peer
|
package peer
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||||
@ -70,6 +71,7 @@ func (wfa *peerMongoAccessor) Search(filters *dbs.Filters, search string) ([]uti
|
|||||||
objs := []utils.ShallowDBObject{}
|
objs := []utils.ShallowDBObject{}
|
||||||
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
|
if (filters == nil || len(filters.And) == 0 || len(filters.Or) == 0) && search != "" {
|
||||||
s, err := strconv.Atoi(search)
|
s, err := strconv.Atoi(search)
|
||||||
|
fmt.Println(s, err, search)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
filters = &dbs.Filters{
|
filters = &dbs.Filters{
|
||||||
Or: map[string][]dbs.Filter{ // search by name if no filters are provided
|
Or: map[string][]dbs.Filter{ // search by name if no filters are provided
|
||||||
|
@ -8,7 +8,6 @@ import (
|
|||||||
"cloud.o-forge.io/core/oc-lib/dbs"
|
"cloud.o-forge.io/core/oc-lib/dbs"
|
||||||
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
"cloud.o-forge.io/core/oc-lib/dbs/mongo"
|
||||||
"cloud.o-forge.io/core/oc-lib/logs"
|
"cloud.o-forge.io/core/oc-lib/logs"
|
||||||
"cloud.o-forge.io/core/oc-lib/static"
|
|
||||||
"cloud.o-forge.io/core/oc-lib/tools"
|
"cloud.o-forge.io/core/oc-lib/tools"
|
||||||
"github.com/go-playground/validator/v10"
|
"github.com/go-playground/validator/v10"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
@ -42,7 +41,7 @@ func (ao *AbstractObject) GetName() string {
|
|||||||
|
|
||||||
func (ao *AbstractObject) UpToDate() {
|
func (ao *AbstractObject) UpToDate() {
|
||||||
ao.UpdateDate = time.Now()
|
ao.UpdateDate = time.Now()
|
||||||
ao.LastPeerWriter, _ = static.GetMyLocalJsonPeer()
|
// ao.LastPeerWriter, _ = static.GetMyLocalJsonPeer()
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAccessor returns the accessor of the object (abstract)
|
// GetAccessor returns the accessor of the object (abstract)
|
||||||
|
@ -171,7 +171,7 @@ func (wfa *workflowMongoAccessor) share(realData *Workflow, delete bool, caller
|
|||||||
paccess := &peer.Peer{}
|
paccess := &peer.Peer{}
|
||||||
for _, p := range res.(*shallow_collaborative_area.ShallowCollaborativeArea).Peers {
|
for _, p := range res.(*shallow_collaborative_area.ShallowCollaborativeArea).Peers {
|
||||||
paccess.UUID = p
|
paccess.UUID = p
|
||||||
if paccess.IsMySelf() { // if the peer is the current peer, never share because it will create a loop
|
if ok, _ := paccess.IsMySelf(); ok { // if the peer is the current peer, never share because it will create a loop
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if delete { // if the workflow is deleted, share the deletion
|
if delete { // if the workflow is deleted, share the deletion
|
||||||
|
@ -217,7 +217,7 @@ func (wfa *workspaceMongoAccessor) share(realData *Workspace, method tools.METHO
|
|||||||
paccess := &peer.Peer{}
|
paccess := &peer.Peer{}
|
||||||
for _, p := range res.(*shallow_collaborative_area.ShallowCollaborativeArea).Peers {
|
for _, p := range res.(*shallow_collaborative_area.ShallowCollaborativeArea).Peers {
|
||||||
paccess.UUID = p
|
paccess.UUID = p
|
||||||
if paccess.IsMySelf() { // If the peer is the current peer, never share because it will create a loop
|
if ok, _ := paccess.IsMySelf(); ok { // If the peer is the current peer, never share because it will create a loop
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if method == tools.DELETE { // If the workspace is deleted, share the deletion
|
if method == tools.DELETE { // If the workspace is deleted, share the deletion
|
||||||
|
@ -1,32 +0,0 @@
|
|||||||
package static
|
|
||||||
|
|
||||||
/*
|
|
||||||
This package contains static data for the peer model
|
|
||||||
It's used to test the peer model
|
|
||||||
Temporary version, will be removed in the future and replaced with a more dynamic solution
|
|
||||||
to generate the data
|
|
||||||
*/
|
|
||||||
|
|
||||||
// GetMyLocalBsonPeer returns a tuple with the peer ID and the peer data in BSON format
|
|
||||||
func GetMyLocalBsonPeer() (string, map[string]interface{}) {
|
|
||||||
return "6fd0134c-fefc-427e-94c2-e01365fc5fb0", map[string]interface{}{
|
|
||||||
"abstractobject": map[string]interface{}{
|
|
||||||
"id": "6fd0134c-fefc-427e-94c2-e01365fc5fb0",
|
|
||||||
"name": "local_peer",
|
|
||||||
},
|
|
||||||
"state": 1,
|
|
||||||
"url": "http://localhost",
|
|
||||||
"public_key": "public_key_lulz",
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// GetMyLocalJsonPeer returns a tuple with the peer ID and the peer data in JSON format
|
|
||||||
func GetMyLocalJsonPeer() (string, map[string]interface{}) {
|
|
||||||
return "6fd0134c-fefc-427e-94c2-e01365fc5fb0", map[string]interface{}{
|
|
||||||
"id": "6fd0134c-fefc-427e-94c2-e01365fc5fb0",
|
|
||||||
"name": "local_peer",
|
|
||||||
"state": 1,
|
|
||||||
"url": "http://localhost",
|
|
||||||
"public_key": "public_key_lulz",
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user