func (s *StateStore) UpsertNode(index uint64, node *structs.Node) error {
	txn := s.db.Txn(true)
	defer txn.Abort()

	// Check if the node already exists
	existing, err := txn.First("nodes", "id", node.ID)
	if err != nil {
		return fmt.Errorf("node lookup failed: %v", err)
	}

	// Setup the indexes correctly
	if existing != nil {
		exist := existing.(*structs.Node)
		node.CreateIndex = exist.CreateIndex
		node.ModifyIndex = index
		node.Drain = exist.Drain // Retain the drain mode
	} else {
		node.CreateIndex = index
		node.ModifyIndex = index
	}

	// Insert the node
	if err := txn.Insert("nodes", node); err != nil {
		return fmt.Errorf("node insert failed: %v", err)
	}
	if err := txn.Insert("index", &IndexEntry{"nodes", index}); err != nil {
		return fmt.Errorf("index update failed: %v", err)
	}

	txn.Commit()
	return nil
}
// UpsertNode is used to register a node or update a node definition
// This is assumed to be triggered by the client, so we retain the value
// of drain which is set by the scheduler.
UpsertNode is used to register a node or update a node definition
This is assumed to be triggered by the client, so we retain the value
of drain which is set by the scheduler.
// UpsertNode is used to register a node or update a node definition // This is assumed to be triggered by the client so we retain the value // of drain which is set by the scheduler .
func (s *StateStore) UpsertNode(index uint64, node *structs.Node) error {
	txn := s.db.Txn(true)
	defer txn.Abort()
	existing, err := txn.First("nodes", "id", node.ID)
	if err != nil {
		return fmt.Errorf("node lookup failed: %v", err)
	}
	if existing != nil {
		exist := existing.(*structs.Node)
		node.CreateIndex = exist.CreateIndex
		node.ModifyIndex = index
		node.Drain = exist.Drain 
	} else {
		node.CreateIndex = index
		node.ModifyIndex = index
	}
	if err := txn.Insert("nodes", node); err != nil {
		return fmt.Errorf("node insert failed: %v", err)
	}
	if err := txn.Insert("index", &IndexEntry{"nodes", index}); err != nil {
		return fmt.Errorf("index update failed: %v", err)
	}
	txn.Commit()
	return nil
}
func ( s * StateStore ) UpsertNode ( index uint64 , node * structs . Node ) error { txn := s . db . Txn ( true ) defer txn . Abort ( ) existing , err := txn . First ( " " , " " , node . ID ) if err != nil { return fmt . Errorf ( " " , err ) } if existing != nil { exist := existing . ( * structs . Node ) node . CreateIndex = exist . CreateIndex node . ModifyIndex = index node . Drain = exist . Drain } else { node . CreateIndex = index node . ModifyIndex = index } if err := txn . Insert ( " " , node ) ; err != nil { return fmt . Errorf ( " " , err ) } if err := txn . Insert ( " " , & IndexEntry { " " , index } ) ; err != nil { return fmt . Errorf ( " " , err ) } txn . Commit ( ) return nil }
UpsertNode is used to register a node or update a node definition
This is assumed to be triggered by the client, so we retain the value
of drain which is set by the scheduler.
UpsertNode is used to register a node or update a node definition This is assumed to be triggered by the client so we retain the value of drain which is set by the scheduler .
// Check if the node already exists // Setup the indexes correctly // Retain the drain mode // Insert the node