|
|
|
@ -100,12 +100,12 @@ func (s *StateStore) GetNode(id string) (*structs.Node, error) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// EnsureService is called to upsert creation of a given NodeService.
|
|
|
|
|
func (s *StateStore) EnsureService(idx uint64, svc *structs.NodeService) error {
|
|
|
|
|
func (s *StateStore) EnsureService(idx uint64, node string, svc *structs.NodeService) error {
|
|
|
|
|
tx := s.db.Txn(true)
|
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
|
|
// Call the service registration upsert
|
|
|
|
|
if err := s.ensureServiceTxn(idx, svc, tx); err != nil {
|
|
|
|
|
if err := s.ensureServiceTxn(idx, node, svc, tx); err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@ -115,24 +115,34 @@ func (s *StateStore) EnsureService(idx uint64, svc *structs.NodeService) error {
|
|
|
|
|
|
|
|
|
|
// ensureServiceTxn is used to upsert a service registration within an
|
|
|
|
|
// existing memdb transaction.
|
|
|
|
|
func (s *StateStore) ensureServiceTxn(idx uint64, svc *structs.NodeService, tx *memdb.Txn) error {
|
|
|
|
|
func (s *StateStore) ensureServiceTxn(idx uint64, node string, svc *structs.NodeService, tx *memdb.Txn) error {
|
|
|
|
|
// Check for existing service
|
|
|
|
|
existing, err := tx.First("services", "id", svc.Service)
|
|
|
|
|
existing, err := tx.First("services", "id", node, svc.Service)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return fmt.Errorf("failed service lookup: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Create the service node entry
|
|
|
|
|
entry := &structs.ServiceNode{
|
|
|
|
|
Node: node,
|
|
|
|
|
ServiceID: svc.ID,
|
|
|
|
|
ServiceName: svc.Service,
|
|
|
|
|
ServiceTags: svc.Tags,
|
|
|
|
|
ServiceAddress: svc.Address,
|
|
|
|
|
ServicePort: svc.Port,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Populate the indexes
|
|
|
|
|
if existing != nil {
|
|
|
|
|
svc.CreateIndex = existing.(*structs.NodeService).CreateIndex
|
|
|
|
|
svc.ModifyIndex = idx
|
|
|
|
|
entry.CreateIndex = existing.(*structs.NodeService).CreateIndex
|
|
|
|
|
entry.ModifyIndex = idx
|
|
|
|
|
} else {
|
|
|
|
|
svc.CreateIndex = idx
|
|
|
|
|
svc.ModifyIndex = idx
|
|
|
|
|
entry.CreateIndex = idx
|
|
|
|
|
entry.ModifyIndex = idx
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Insert the service and update the index
|
|
|
|
|
if err := tx.Insert("services", svc); err != nil {
|
|
|
|
|
if err := tx.Insert("services", entry); err != nil {
|
|
|
|
|
return fmt.Errorf("failed inserting service: %s", err)
|
|
|
|
|
}
|
|
|
|
|
if err := tx.Insert("index", &IndexEntry{"services", idx}); err != nil {
|
|
|
|
@ -140,3 +150,45 @@ func (s *StateStore) ensureServiceTxn(idx uint64, svc *structs.NodeService, tx *
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NodeServices is used to query service registrations by node ID.
|
|
|
|
|
func (s *StateStore) NodeServices(nodeID string) (*structs.NodeServices, error) {
|
|
|
|
|
tx := s.db.Txn(false)
|
|
|
|
|
defer tx.Abort()
|
|
|
|
|
|
|
|
|
|
// Query the node
|
|
|
|
|
node, err := tx.First("nodes", "id", nodeID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("node lookup failed: %s", err)
|
|
|
|
|
}
|
|
|
|
|
if node == nil {
|
|
|
|
|
return nil, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Read all of the services
|
|
|
|
|
services, err := tx.Get("services", "node", nodeID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("failed querying services for node %q: %s", nodeID, err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Initialize the node services struct
|
|
|
|
|
ns := &structs.NodeServices{
|
|
|
|
|
Node: *node.(*structs.Node),
|
|
|
|
|
Services: make(map[string]*structs.NodeService),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add all of the services to the map
|
|
|
|
|
for service := services.Next(); service != nil; service = services.Next() {
|
|
|
|
|
sn := service.(*structs.ServiceNode)
|
|
|
|
|
svc := &structs.NodeService{
|
|
|
|
|
ID: sn.ServiceID,
|
|
|
|
|
Service: sn.ServiceName,
|
|
|
|
|
Tags: sn.ServiceTags,
|
|
|
|
|
Address: sn.ServiceAddress,
|
|
|
|
|
Port: sn.ServicePort,
|
|
|
|
|
}
|
|
|
|
|
ns.Services[svc.ID] = svc
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ns, nil
|
|
|
|
|
}
|
|
|
|
|