2021-01-28 10:17:54 +00:00
|
|
|
// Notes:
|
2021-02-01 10:13:38 +00:00
|
|
|
package steward
|
2021-01-25 14:23:00 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/gob"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2021-01-28 13:58:16 +00:00
|
|
|
"sync"
|
2021-01-25 14:23:00 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/nats-io/nats.go"
|
|
|
|
)
|
|
|
|
|
2021-02-03 21:08:28 +00:00
|
|
|
type MessageType string
|
2021-01-25 14:50:44 +00:00
|
|
|
|
2021-01-28 10:17:54 +00:00
|
|
|
// TODO: Figure it makes sense to have these types at all.
|
|
|
|
// It might make more sense to implement these as two
|
|
|
|
// individual subjects.
|
2021-01-25 14:50:44 +00:00
|
|
|
const (
|
2021-01-27 08:45:52 +00:00
|
|
|
// shellCommand, command that will just wait for an
|
|
|
|
// ack, and nothing of the output of the command are
|
|
|
|
// delivered back in the reply ack message.
|
|
|
|
// The message should contain the unique ID of the
|
|
|
|
// command.
|
2021-02-04 12:26:10 +00:00
|
|
|
Command MessageType = "command"
|
2021-01-25 14:50:44 +00:00
|
|
|
// shellCommand, wait for and return the output
|
|
|
|
// of the command in the ACK message. This means
|
|
|
|
// that the command should be executed immediately
|
|
|
|
// and that we should get the confirmation that it
|
|
|
|
// was successful or not.
|
2021-02-04 12:26:10 +00:00
|
|
|
Event MessageType = "event"
|
2021-01-25 14:50:44 +00:00
|
|
|
// eventCommand, just wait for the ACK that the
|
|
|
|
// message is received. What action happens on the
|
|
|
|
// receiving side is up to the received to decide.
|
|
|
|
)
|
|
|
|
|
2021-01-25 14:23:00 +00:00
|
|
|
type Message struct {
|
2021-01-25 14:50:44 +00:00
|
|
|
// The Unique ID of the message
|
2021-02-04 10:46:58 +00:00
|
|
|
ID int `json:"id" yaml:"id"`
|
2021-01-25 14:50:44 +00:00
|
|
|
// The actual data in the message
|
2021-01-25 15:50:21 +00:00
|
|
|
// TODO: Change this to a slice instead...or maybe use an
|
|
|
|
// interface type here to handle several data types ?
|
2021-02-04 10:46:58 +00:00
|
|
|
Data []string `json:"data" yaml:"data"`
|
2021-01-25 14:50:44 +00:00
|
|
|
// The type of the message being sent
|
2021-02-04 10:46:58 +00:00
|
|
|
MessageType MessageType `json:"messageType" yaml:"messageType"`
|
2021-01-25 14:23:00 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 10:17:54 +00:00
|
|
|
// server is the structure that will hold the state about spawned
|
|
|
|
// processes on a local instance.
|
2021-01-27 13:02:57 +00:00
|
|
|
type server struct {
|
|
|
|
natsConn *nats.Conn
|
2021-01-28 10:17:54 +00:00
|
|
|
// TODO: sessions should probably hold a slice/map of processes ?
|
2021-02-03 11:55:02 +00:00
|
|
|
processes map[subjectName]process
|
2021-01-28 10:17:54 +00:00
|
|
|
// The last processID created
|
|
|
|
lastProcessID int
|
2021-02-04 12:26:10 +00:00
|
|
|
// The name of the node
|
|
|
|
nodeName string
|
|
|
|
mu sync.Mutex
|
|
|
|
// The channel where we receive new messages from the outside to
|
|
|
|
// insert into the system for being processed
|
|
|
|
newMessagesCh chan []jsonFromFile
|
2021-02-05 09:47:07 +00:00
|
|
|
// errorCh is used to report errors from a process
|
|
|
|
// NB: Implementing this as an int to report for testing
|
|
|
|
errorCh chan string
|
2021-02-05 12:56:42 +00:00
|
|
|
// errorKernel
|
|
|
|
errorKernel *errorKernel
|
2021-01-27 13:02:57 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 10:17:54 +00:00
|
|
|
// newServer will prepare and return a server type
|
2021-02-01 10:13:38 +00:00
|
|
|
func NewServer(brokerAddress string, nodeName string) (*server, error) {
|
|
|
|
conn, err := nats.Connect(brokerAddress, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: nats.Connect failed: %v\n", err)
|
|
|
|
}
|
|
|
|
|
2021-02-01 12:41:04 +00:00
|
|
|
s := &server{
|
2021-02-04 12:26:10 +00:00
|
|
|
nodeName: nodeName,
|
|
|
|
natsConn: conn,
|
|
|
|
processes: make(map[subjectName]process),
|
|
|
|
newMessagesCh: make(chan []jsonFromFile),
|
2021-02-05 09:47:07 +00:00
|
|
|
errorCh: make(chan string, 10),
|
2021-02-01 12:41:04 +00:00
|
|
|
}
|
2021-01-29 05:09:48 +00:00
|
|
|
|
2021-02-05 12:56:42 +00:00
|
|
|
// Start the error kernel that will do all the error handling
|
|
|
|
// not done within a process.
|
|
|
|
s.errorKernel = newErrorKernel()
|
|
|
|
s.errorKernel.startErrorKernel(s.errorCh)
|
|
|
|
|
2021-02-05 06:25:12 +00:00
|
|
|
return s, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *server) PublisherStart() {
|
2021-02-05 09:47:07 +00:00
|
|
|
// Start the checking the input file for new messages from operator.
|
2021-02-04 12:26:10 +00:00
|
|
|
go getMessagesFromFile("./", "inmsg.txt", s.newMessagesCh)
|
2021-02-02 12:06:37 +00:00
|
|
|
|
2021-02-03 13:53:25 +00:00
|
|
|
// Prepare and start a single process
|
2021-02-02 12:39:24 +00:00
|
|
|
{
|
2021-02-04 12:26:10 +00:00
|
|
|
sub := newSubject("ship1", "command", "shellcommand", "shell")
|
2021-02-05 09:47:07 +00:00
|
|
|
proc := s.processPrepareNew(sub, s.errorCh)
|
2021-02-02 12:39:24 +00:00
|
|
|
// fmt.Printf("*** %#v\n", proc)
|
2021-02-03 11:55:02 +00:00
|
|
|
go s.processSpawn(proc)
|
2021-02-02 12:39:24 +00:00
|
|
|
}
|
2021-02-01 12:41:04 +00:00
|
|
|
|
2021-02-03 13:53:25 +00:00
|
|
|
// Prepare and start a single process
|
2021-02-02 12:39:24 +00:00
|
|
|
{
|
2021-02-04 12:26:10 +00:00
|
|
|
sub := newSubject("ship2", "command", "shellcommand", "shell")
|
2021-02-05 09:47:07 +00:00
|
|
|
proc := s.processPrepareNew(sub, s.errorCh)
|
2021-02-02 12:39:24 +00:00
|
|
|
// fmt.Printf("*** %#v\n", proc)
|
2021-02-03 11:55:02 +00:00
|
|
|
go s.processSpawn(proc)
|
2021-02-02 12:39:24 +00:00
|
|
|
}
|
2021-02-01 12:41:04 +00:00
|
|
|
|
2021-02-05 09:47:07 +00:00
|
|
|
s.handleNewOperatorMessages()
|
|
|
|
|
|
|
|
select {}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-02-05 12:56:42 +00:00
|
|
|
// errorKernel is the structure that will hold all the error
|
|
|
|
// handling values and logic.
|
|
|
|
type errorKernel struct {
|
|
|
|
// ringBuffer *ringBuffer
|
|
|
|
}
|
|
|
|
|
|
|
|
// newErrorKernel will initialize and return a new error kernel
|
|
|
|
func newErrorKernel() *errorKernel {
|
|
|
|
return &errorKernel{
|
|
|
|
// ringBuffer: newringBuffer(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-05 09:47:07 +00:00
|
|
|
// startErrorKernel will start the error kernel and check if there
|
|
|
|
// have been reveived any errors from any of the processes, and
|
|
|
|
// handle them appropriately.
|
|
|
|
// TODO: Since a process will be locked while waiting to send the error
|
|
|
|
// on the errorCh maybe it makes sense to have a channel inside the
|
|
|
|
// processes error handling with a select so we can send back to the
|
|
|
|
// process if it should continue or not based not based on how severe
|
|
|
|
// the error where. This should be right after sending the error
|
|
|
|
// sending in the process.
|
2021-02-05 12:56:42 +00:00
|
|
|
func (e *errorKernel) startErrorKernel(errorCh chan string) {
|
2021-02-05 09:47:07 +00:00
|
|
|
// TODO: For now it will just print the error messages to the
|
|
|
|
// console.
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
for {
|
2021-02-05 12:56:42 +00:00
|
|
|
er := <-errorCh
|
|
|
|
log.Printf("*** ERROR_KERNEL: %#v, type=%T\n", er, er)
|
2021-02-05 09:47:07 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
// handleNewOperatorMessages will handle all the new operator messages
|
|
|
|
// given to the system, and route them to the correct subject queue.
|
|
|
|
func (s *server) handleNewOperatorMessages() {
|
|
|
|
// Process the messages that have been received on the incomming
|
|
|
|
// message pipe. Check and send if there are a specific subject
|
|
|
|
// for it, and no subject exist throw an error.
|
|
|
|
//
|
|
|
|
// TODO: Later on the only thing that should be checked here is
|
|
|
|
// that there is a node for the specific message, and the super-
|
|
|
|
// visor should create the process with the wanted subject on both
|
|
|
|
// the publishing and the receiving node. If there is no such node
|
|
|
|
// an error should be generated and processed by the error-kernel.
|
2021-02-03 13:53:25 +00:00
|
|
|
go func() {
|
2021-02-04 12:26:10 +00:00
|
|
|
for v := range s.newMessagesCh {
|
|
|
|
for _, vv := range v {
|
|
|
|
|
|
|
|
m := vv.Message
|
|
|
|
subjName := vv.Subject.name()
|
2021-02-05 09:47:07 +00:00
|
|
|
fmt.Printf("** handleNewOperatorMessages: message: %v, ** subject: %#v\n", m, vv.Subject)
|
2021-02-04 12:26:10 +00:00
|
|
|
_, ok := s.processes[subjName]
|
|
|
|
if ok {
|
|
|
|
log.Printf("info: found the specific subject: %v\n", subjName)
|
2021-02-05 09:47:07 +00:00
|
|
|
// Put the message on the correct process's messageCh
|
2021-02-04 12:26:10 +00:00
|
|
|
s.processes[subjName].subject.messageCh <- m
|
|
|
|
} else {
|
|
|
|
log.Printf("info: did not find that specific subject: %v\n", subjName)
|
|
|
|
time.Sleep(time.Millisecond * 500)
|
|
|
|
continue
|
|
|
|
}
|
2021-02-03 13:53:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2021-01-28 13:58:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type node string
|
|
|
|
|
2021-02-02 12:39:24 +00:00
|
|
|
// subject contains the representation of a subject to be used with one
|
|
|
|
// specific process
|
2021-02-03 21:08:28 +00:00
|
|
|
type Subject struct {
|
2021-02-02 12:39:24 +00:00
|
|
|
// node, the name of the node
|
2021-02-04 10:46:58 +00:00
|
|
|
Node string `json:"node" yaml:"node"`
|
2021-02-02 12:39:24 +00:00
|
|
|
// messageType, command/event
|
2021-02-05 06:25:12 +00:00
|
|
|
MessageType MessageType `json:"messageType" yaml:"messageType"`
|
2021-02-02 12:39:24 +00:00
|
|
|
// method, what is this message doing, etc. shellcommand, syslog, etc.
|
2021-02-04 10:46:58 +00:00
|
|
|
Method string `json:"method" yaml:"method"`
|
2021-02-02 12:39:24 +00:00
|
|
|
// domain is used to differentiate services. Like there can be more
|
|
|
|
// logging services, but rarely more logging services for the same
|
|
|
|
// thing. Domain is here used to differentiate the the services and
|
|
|
|
// tell with one word what it is for.
|
2021-02-04 10:46:58 +00:00
|
|
|
Domain string `json:"domain" yaml:"domain"`
|
2021-02-03 13:53:25 +00:00
|
|
|
// messageCh is the channel for receiving new content to be sent
|
|
|
|
messageCh chan Message
|
2021-02-02 12:39:24 +00:00
|
|
|
}
|
|
|
|
|
2021-02-03 21:08:28 +00:00
|
|
|
// newSubject will return a new variable of the type subject, and insert
|
|
|
|
// all the values given as arguments. It will also create the channel
|
|
|
|
// to receive new messages on the specific subject.
|
2021-02-05 06:25:12 +00:00
|
|
|
func newSubject(node string, messageType MessageType, method string, domain string) Subject {
|
2021-02-03 21:08:28 +00:00
|
|
|
return Subject{
|
|
|
|
Node: node,
|
|
|
|
MessageType: messageType,
|
|
|
|
Method: method,
|
|
|
|
Domain: domain,
|
|
|
|
messageCh: make(chan Message),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-03 11:55:02 +00:00
|
|
|
type subjectName string
|
|
|
|
|
2021-02-03 21:08:28 +00:00
|
|
|
func (s Subject) name() subjectName {
|
|
|
|
return subjectName(fmt.Sprintf("%s.%s.%s.%s", s.Node, s.MessageType, s.Method, s.Domain))
|
2021-02-03 11:55:02 +00:00
|
|
|
}
|
|
|
|
|
2021-01-28 13:58:16 +00:00
|
|
|
// process are represent the communication to one individual host
|
|
|
|
type process struct {
|
|
|
|
messageID int
|
2021-02-03 07:28:21 +00:00
|
|
|
// the subject used for the specific process. One process
|
|
|
|
// can contain only one sender on a message bus, hence
|
|
|
|
// also one subject
|
2021-02-03 21:08:28 +00:00
|
|
|
subject Subject
|
2021-01-28 13:58:16 +00:00
|
|
|
// Put a node here to be able know the node a process is at.
|
|
|
|
// NB: Might not be needed later on.
|
|
|
|
node node
|
|
|
|
// The processID for the current process
|
|
|
|
processID int
|
|
|
|
// errorCh is used to report errors from a process
|
|
|
|
// NB: Implementing this as an int to report for testing
|
|
|
|
errorCh chan string
|
2021-01-28 10:17:54 +00:00
|
|
|
}
|
|
|
|
|
2021-02-01 12:41:04 +00:00
|
|
|
// prepareNewProcess will set the the provided values and the default
|
|
|
|
// values for a process.
|
2021-02-05 09:47:07 +00:00
|
|
|
func (s *server) processPrepareNew(subject Subject, errCh chan string) process {
|
2021-02-03 08:06:37 +00:00
|
|
|
// create the initial configuration for a sessions communicating with 1 host process.
|
2021-01-28 10:17:54 +00:00
|
|
|
s.lastProcessID++
|
|
|
|
proc := process{
|
2021-01-27 13:02:57 +00:00
|
|
|
messageID: 0,
|
2021-02-03 09:23:50 +00:00
|
|
|
subject: subject,
|
2021-02-03 21:08:28 +00:00
|
|
|
node: node(subject.Node),
|
2021-01-28 10:17:54 +00:00
|
|
|
processID: s.lastProcessID,
|
2021-02-05 09:47:07 +00:00
|
|
|
errorCh: errCh,
|
2021-02-03 13:53:25 +00:00
|
|
|
//messageCh: make(chan Message),
|
2021-01-27 13:02:57 +00:00
|
|
|
}
|
2021-01-28 10:17:54 +00:00
|
|
|
|
|
|
|
return proc
|
|
|
|
}
|
|
|
|
|
2021-02-01 12:41:04 +00:00
|
|
|
// spawnProcess will spawn a new process. It will give the process
|
|
|
|
// the next available ID, and also add the process to the processes
|
|
|
|
// map.
|
2021-02-03 11:55:02 +00:00
|
|
|
func (s *server) processSpawn(proc process) {
|
2021-02-03 21:08:28 +00:00
|
|
|
s.mu.Lock()
|
2021-02-03 13:53:25 +00:00
|
|
|
// We use the full name of the subject to identify a unique
|
|
|
|
// process. We can do that since a process can only handle
|
|
|
|
// one message queue.
|
2021-02-03 11:55:02 +00:00
|
|
|
s.processes[proc.subject.name()] = proc
|
2021-02-03 21:08:28 +00:00
|
|
|
s.mu.Unlock()
|
2021-01-27 13:02:57 +00:00
|
|
|
|
2021-02-02 12:06:37 +00:00
|
|
|
// TODO: I think it makes most sense that the messages would come to
|
|
|
|
// here from some other message-pickup-process, and that process will
|
|
|
|
// give the message to the correct publisher process. A channel that
|
|
|
|
// is listened on in the for loop below could be used to receive the
|
|
|
|
// messages from the message-pickup-process.
|
2021-01-27 13:02:57 +00:00
|
|
|
for {
|
2021-02-03 13:53:25 +00:00
|
|
|
// Wait and read the next message on the message channel
|
|
|
|
m := <-proc.subject.messageCh
|
2021-02-03 11:55:02 +00:00
|
|
|
m.ID = s.processes[proc.subject.name()].messageID
|
2021-01-29 05:09:48 +00:00
|
|
|
messageDeliver(proc, m, s.natsConn)
|
2021-01-27 08:45:52 +00:00
|
|
|
|
|
|
|
// Increment the counter for the next message to be sent.
|
2021-01-28 10:17:54 +00:00
|
|
|
proc.messageID++
|
2021-02-03 11:55:02 +00:00
|
|
|
s.processes[proc.subject.name()] = proc
|
2021-01-27 08:45:52 +00:00
|
|
|
time.Sleep(time.Second * 1)
|
2021-01-28 13:58:16 +00:00
|
|
|
|
2021-02-03 09:23:50 +00:00
|
|
|
// NB: simulate that we get an error, and that we can send that
|
2021-01-28 13:58:16 +00:00
|
|
|
// out of the process and receive it in another thread.
|
2021-02-05 09:47:07 +00:00
|
|
|
s.errorCh <- "received an error from process: " + fmt.Sprintf("ID=%v, subjectName=%v\n", proc.processID, proc.subject.name())
|
2021-01-27 08:45:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-29 05:09:48 +00:00
|
|
|
func messageDeliver(proc process, message Message, natsConn *nats.Conn) {
|
2021-01-27 08:45:52 +00:00
|
|
|
for {
|
2021-01-27 13:02:57 +00:00
|
|
|
dataPayload, err := gobEncodePayload(message)
|
2021-01-27 08:45:52 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: createDataPayload: %v\n", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := &nats.Msg{
|
2021-02-03 11:55:02 +00:00
|
|
|
Subject: string(proc.subject.name()),
|
2021-02-03 09:23:50 +00:00
|
|
|
// Subject: fmt.Sprintf("%s.%s.%s", proc.node, "command", "shellcommand"),
|
2021-01-29 05:09:48 +00:00
|
|
|
// Structure of the reply message are:
|
2021-01-29 13:22:36 +00:00
|
|
|
// reply.<nodename>.<message type>.<method>
|
2021-02-03 11:55:02 +00:00
|
|
|
Reply: fmt.Sprintf("reply.%s", proc.subject.name()),
|
2021-01-29 05:09:48 +00:00
|
|
|
Data: dataPayload,
|
2021-01-25 14:23:00 +00:00
|
|
|
}
|
|
|
|
|
2021-01-27 08:45:52 +00:00
|
|
|
// The SubscribeSync used in the subscriber, will get messages that
|
|
|
|
// are sent after it started subscribing, so we start a publisher
|
|
|
|
// that sends out a message every second.
|
|
|
|
//
|
|
|
|
// Create a subscriber for the reply message.
|
|
|
|
subReply, err := natsConn.SubscribeSync(msg.Reply)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: nc.SubscribeSync failed: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Publish message
|
|
|
|
err = natsConn.PublishMsg(msg)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error: publish failed: %v\n", err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait up until 10 seconds for a reply,
|
|
|
|
// continue and resend if to reply received.
|
|
|
|
msgReply, err := subReply.NextMsg(time.Second * 10)
|
|
|
|
if err != nil {
|
2021-02-03 11:55:02 +00:00
|
|
|
log.Printf("error: subRepl.NextMsg failed for node=%v, subject=%v: %v\n", proc.node, proc.subject.name(), err)
|
2021-01-27 08:45:52 +00:00
|
|
|
// did not receive a reply, continuing from top again
|
|
|
|
continue
|
|
|
|
}
|
2021-02-05 09:47:07 +00:00
|
|
|
log.Printf("publisher: received ACK: %s\n", msgReply.Data)
|
2021-01-27 08:45:52 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 13:02:57 +00:00
|
|
|
// gobEncodePayload will encode the message structure along with its
|
|
|
|
// valued in gob binary format.
|
2021-01-28 10:17:54 +00:00
|
|
|
// TODO: Check if it adds value to compress with gzip.
|
2021-01-27 13:02:57 +00:00
|
|
|
func gobEncodePayload(m Message) ([]byte, error) {
|
2021-01-27 08:45:52 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
gobEnc := gob.NewEncoder(&buf)
|
|
|
|
err := gobEnc.Encode(m)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error: gob.Enode failed: %v", err)
|
|
|
|
}
|
2021-01-25 14:23:00 +00:00
|
|
|
|
2021-01-27 08:45:52 +00:00
|
|
|
return buf.Bytes(), nil
|
2021-01-25 14:23:00 +00:00
|
|
|
}
|