2021-02-12 10:21:51 +00:00
// Info: The idea about the ring buffer is that we have a FIFO
// buffer where we store all incomming messages requested by
2021-08-16 11:01:12 +00:00
// operators.
// Each message in process or waiting to be processed will be
// stored in a DB. When the processing of a given message is
// done it will be removed from the state db, and an entry will
// made in the persistent message log.
2021-02-12 10:21:51 +00:00
package steward
2021-02-15 10:28:27 +00:00
import (
2021-09-15 06:39:34 +00:00
"context"
2021-02-16 03:57:54 +00:00
"encoding/json"
2021-02-15 10:28:27 +00:00
"fmt"
"log"
2021-02-17 09:27:39 +00:00
"os"
2021-05-12 07:50:03 +00:00
"path/filepath"
2021-02-17 11:02:34 +00:00
"sort"
2021-02-15 10:28:27 +00:00
"strconv"
2021-02-16 13:29:32 +00:00
"sync"
2021-02-17 09:27:39 +00:00
"time"
2021-02-15 10:28:27 +00:00
bolt "go.etcd.io/bbolt"
)
// samValue represents one message with a subject. This
// struct type is used when storing and retreiving from
// db.
type samDBValue struct {
ID int
Data subjectAndMessage
}
2021-02-12 10:21:51 +00:00
// ringBuffer holds the data of the buffer,
type ringBuffer struct {
2021-09-15 06:39:34 +00:00
// Context for ring buffer.
ctx context . Context
// Cancel function for ring buffer.
cancel context . CancelFunc
// Waitgroup for ringbuffer.
wg sync . WaitGroup
2021-08-16 11:01:12 +00:00
// In memory buffer for the messages.
bufData chan samDBValue
// The database to use.
2021-09-14 14:23:01 +00:00
db * bolt . DB
samValueBucket string
indexValueBucket string
2021-08-16 11:01:12 +00:00
// The current number of items in the database.
2021-02-16 03:57:54 +00:00
totalMessagesIndex int
2021-02-16 13:29:32 +00:00
mu sync . Mutex
2021-08-16 11:01:12 +00:00
// The channel to send messages that have been processed,
// and we want to store it in the permanent message log.
permStore chan string
// Name of node.
nodeName Node
2021-08-25 06:31:48 +00:00
// newMessagesCh from *server are also implemented here,
// so the ringbuffer can send it's error messages the same
// way as all messages are handled.
newMessagesCh chan [ ] subjectAndMessage
metrics * metrics
2021-09-07 07:43:54 +00:00
configuration * Configuration
2021-02-12 10:21:51 +00:00
}
2021-08-16 11:01:12 +00:00
// newringBuffer returns a push/pop storage for values.
2021-09-15 06:39:34 +00:00
func newringBuffer ( ctx context . Context , metrics * metrics , configuration * Configuration , size int , dbFileName string , nodeName Node , newMessagesCh chan [ ] subjectAndMessage , samValueBucket string , indexValueBucket string ) * ringBuffer {
ctxRingbuffer , cancel := context . WithCancel ( ctx )
2021-05-12 07:50:03 +00:00
// Check if socket folder exists, if not create it
2021-09-07 07:43:54 +00:00
if _ , err := os . Stat ( configuration . DatabaseFolder ) ; os . IsNotExist ( err ) {
err := os . MkdirAll ( configuration . DatabaseFolder , 0700 )
2021-05-12 07:50:03 +00:00
if err != nil {
2021-09-07 07:43:54 +00:00
log . Printf ( "error: failed to create database directory %v: %v\n" , configuration . DatabaseFolder , err )
2021-05-12 07:50:03 +00:00
os . Exit ( 1 )
}
}
2021-09-07 07:43:54 +00:00
DatabaseFilepath := filepath . Join ( configuration . DatabaseFolder , dbFileName )
2021-05-12 07:50:03 +00:00
// ---
db , err := bolt . Open ( DatabaseFilepath , 0600 , nil )
2021-02-15 10:28:27 +00:00
if err != nil {
log . Printf ( "error: failed to open db: %v\n" , err )
2021-05-12 07:50:03 +00:00
os . Exit ( 1 )
2021-02-15 10:28:27 +00:00
}
2021-08-18 13:41:53 +00:00
2021-02-12 10:21:51 +00:00
return & ringBuffer {
2021-09-15 06:39:34 +00:00
ctx : ctxRingbuffer ,
cancel : cancel ,
2021-09-14 14:23:01 +00:00
bufData : make ( chan samDBValue , size ) ,
db : db ,
samValueBucket : samValueBucket ,
indexValueBucket : indexValueBucket ,
permStore : make ( chan string ) ,
nodeName : nodeName ,
newMessagesCh : newMessagesCh ,
metrics : metrics ,
configuration : configuration ,
2021-02-12 10:21:51 +00:00
}
}
// start will process incomming messages through the inCh,
2021-02-15 10:28:27 +00:00
// put the messages on a buffered channel
2021-02-12 10:21:51 +00:00
// and deliver messages out when requested on the outCh.
2021-07-02 17:09:42 +00:00
func ( r * ringBuffer ) start ( inCh chan subjectAndMessage , outCh chan samDBValueAndDelivered , defaultMessageTimeout int , defaultMessageRetries int ) {
2021-03-12 08:38:19 +00:00
2021-02-12 10:21:51 +00:00
// Starting both writing and reading in separate go routines so we
// can write and read concurrently.
2021-09-14 14:23:01 +00:00
r . totalMessagesIndex = r . getIndexValue ( )
2021-02-15 10:28:27 +00:00
2021-02-17 09:27:39 +00:00
// Fill the buffer when new data arrives into the system
2021-09-14 14:23:01 +00:00
go r . fillBuffer ( inCh , defaultMessageTimeout , defaultMessageRetries )
2021-02-15 10:28:27 +00:00
2021-02-17 09:27:39 +00:00
// Start the process to permanently store done messages.
go r . startPermanentStore ( )
// Start the process that will handle messages present in the ringbuffer.
2021-09-14 14:23:01 +00:00
go r . processBufferMessages ( outCh )
2021-09-15 05:26:36 +00:00
2021-09-15 06:39:34 +00:00
r . wg . Add ( 1 )
2021-09-15 05:26:36 +00:00
go func ( ) {
ticker := time . NewTicker ( time . Second * 5 )
for {
select {
case <- ticker . C :
r . dbUpdateMetrics ( r . samValueBucket )
2021-09-15 06:39:34 +00:00
case <- r . ctx . Done ( ) :
r . wg . Done ( )
return
2021-09-15 05:26:36 +00:00
}
}
} ( )
2021-02-16 11:59:37 +00:00
}
2021-02-15 10:28:27 +00:00
2021-09-15 06:39:34 +00:00
func ( r * ringBuffer ) stop ( ) {
r . cancel ( )
r . wg . Wait ( )
}
2021-02-16 11:59:37 +00:00
// fillBuffer will fill the buffer in the ringbuffer reading from the inchannel.
// It will also store the messages in a K/V DB while being processed.
2021-09-14 14:23:01 +00:00
func ( r * ringBuffer ) fillBuffer ( inCh chan subjectAndMessage , defaultMessageTimeout int , defaultMessageRetries int ) {
2021-02-17 11:02:34 +00:00
// At startup get all the values that might be in the K/V store so we can
// put them into the buffer before we start to fill up with new incomming
// messages to the system.
// This is needed when the program have been restarted, and we need to check
// if there where previously unhandled messages that need to be handled first.
2021-05-21 06:21:17 +00:00
2021-02-17 11:02:34 +00:00
func ( ) {
2021-09-14 14:23:01 +00:00
s , err := r . dumpBucket ( r . samValueBucket )
2021-02-17 11:02:34 +00:00
if err != nil {
2021-08-09 12:41:31 +00:00
er := fmt . Errorf ( "info: fillBuffer: retreival of values from k/v store failed, probaly empty database, and no previous entries in db to process: %v" , err )
2021-05-21 06:21:17 +00:00
log . Printf ( "%v\n" , er )
return
//sendErrorLogMessage(r.newMessagesCh, node(r.nodeName), er)
2021-02-17 11:02:34 +00:00
}
for _ , v := range s {
r . bufData <- v
}
} ( )
2021-02-18 07:25:13 +00:00
// Prepare the map structure to know what values are allowed
// for the commands or events
var coe CommandOrEvent
coeAvailable := coe . GetCommandOrEventAvailable ( )
coeAvailableValues := [ ] CommandOrEvent { }
for v := range coeAvailable . topics {
coeAvailableValues = append ( coeAvailableValues , v )
}
2021-02-16 11:59:37 +00:00
// Check for incomming messages. These are typically comming from
2021-08-16 11:01:12 +00:00
// the go routine who reads the socket.
2021-02-16 11:59:37 +00:00
for v := range inCh {
2021-02-18 07:25:13 +00:00
// Check if the command or event exists in commandOrEvent.go
2021-03-02 12:46:02 +00:00
if ! coeAvailable . CheckIfExists ( v . CommandOrEvent , v . Subject ) {
2021-04-16 10:45:14 +00:00
er := fmt . Errorf ( "error: fillBuffer: the event or command type do not exist, so this message will not be put on the buffer to be processed. Check the syntax used in the json file for the message. Allowed values are : %v, where given: coe=%v, with subject=%v" , coeAvailableValues , v . CommandOrEvent , v . Subject )
2021-09-07 07:43:54 +00:00
sendErrorLogMessage ( r . configuration , r . metrics , r . newMessagesCh , Node ( r . nodeName ) , er )
2021-02-18 07:25:13 +00:00
fmt . Println ( )
// if it was not a valid value, we jump back up, and
// continue the range iteration.
continue
}
2021-02-25 12:08:10 +00:00
// Check if message values for timers override default values
2021-04-15 08:33:44 +00:00
if v . Message . ACKTimeout < 1 {
v . Message . ACKTimeout = defaultMessageTimeout
2021-02-25 12:08:10 +00:00
}
if v . Message . Retries < 1 {
v . Message . Retries = defaultMessageRetries
}
2021-02-16 11:59:37 +00:00
// --- Store the incomming message in the k/v store ---
2021-02-16 13:29:32 +00:00
// Get a unique number for the message to use when storing
// it in the databases, and also use when further processing.
r . mu . Lock ( )
dbID := r . totalMessagesIndex
r . mu . Unlock ( )
2021-02-16 11:59:37 +00:00
// Create a structure for JSON marshaling.
samV := samDBValue {
2021-02-16 13:29:32 +00:00
ID : dbID ,
2021-02-16 11:59:37 +00:00
Data : v ,
}
2021-02-15 10:28:27 +00:00
2021-02-16 11:59:37 +00:00
js , err := json . Marshal ( samV )
if err != nil {
2021-08-16 11:01:12 +00:00
er := fmt . Errorf ( "error:fillBuffer: json marshaling: %v" , err )
2021-09-07 07:43:54 +00:00
sendErrorLogMessage ( r . configuration , r . metrics , r . newMessagesCh , Node ( r . nodeName ) , er )
2021-02-16 11:59:37 +00:00
}
2021-02-15 10:28:27 +00:00
2021-02-16 11:59:37 +00:00
// Store the incomming message in key/value store
2021-09-14 14:23:01 +00:00
err = r . dbUpdate ( r . db , r . samValueBucket , strconv . Itoa ( dbID ) , js )
2021-02-16 11:59:37 +00:00
if err != nil {
2021-03-12 08:38:19 +00:00
er := fmt . Errorf ( "error: dbUpdate samValue failed: %v" , err )
2021-09-07 07:43:54 +00:00
sendErrorLogMessage ( r . configuration , r . metrics , r . newMessagesCh , Node ( r . nodeName ) , er )
2021-03-12 08:38:19 +00:00
2021-02-12 10:21:51 +00:00
}
2021-02-15 10:28:27 +00:00
2021-02-17 11:02:34 +00:00
// Put the message on the inmemory buffer.
2021-02-16 13:29:32 +00:00
r . bufData <- samV
2021-02-16 11:59:37 +00:00
// Increment index, and store the new value to the database.
2021-02-16 13:29:32 +00:00
r . mu . Lock ( )
2021-02-16 11:59:37 +00:00
r . totalMessagesIndex ++
2021-09-14 14:23:01 +00:00
r . dbUpdate ( r . db , r . indexValueBucket , "index" , [ ] byte ( strconv . Itoa ( r . totalMessagesIndex ) ) )
2021-02-17 09:27:39 +00:00
r . mu . Unlock ( )
2021-02-16 11:59:37 +00:00
}
2021-02-12 10:21:51 +00:00
2021-02-16 11:59:37 +00:00
// When done close the buffer channel
close ( r . bufData )
}
2021-02-16 11:29:15 +00:00
2021-02-16 11:59:37 +00:00
// processBufferMessages will pick messages from the buffer, and process them
// one by one. The messages will be delivered on the outCh, and it will wait
// until a signal is received on the done channel before it continues with the
// next message.
2021-09-14 14:23:01 +00:00
func ( r * ringBuffer ) processBufferMessages ( outCh chan samDBValueAndDelivered ) {
2021-02-16 11:59:37 +00:00
// Range over the buffer of messages to pass on to processes.
for v := range r . bufData {
2021-08-18 13:57:33 +00:00
r . metrics . promInMemoryBufferMessagesCurrent . Set ( float64 ( len ( r . bufData ) ) )
2021-02-16 11:59:37 +00:00
// Create a done channel per message. A process started by the
// spawnProcess function will handle incomming messages sequentaly.
// So in the spawnProcess function we put a struct{} value when a
// message is processed on the "done" channel and an ack is received
// for a message, and we wait here for the "done" to be received.
// We start the actual processing of an individual message here within
// it's own go routine. Reason is that we don't want to block other
2021-08-16 11:01:12 +00:00
// messages to be processed while waiting for the done signal, or if an
2021-02-16 11:59:37 +00:00
// error with an individual message occurs.
2021-02-16 13:29:32 +00:00
go func ( v samDBValue ) {
v . Data . Message . done = make ( chan struct { } )
2021-07-05 05:43:33 +00:00
delivredCh := make ( chan struct { } )
2021-02-16 03:57:54 +00:00
2021-07-05 05:43:33 +00:00
// Prepare the structure with the data, and a function that can
// be called when the data is received for signaling back.
2021-07-02 17:09:42 +00:00
sd := samDBValueAndDelivered {
samDBValue : v ,
2021-07-05 05:43:33 +00:00
delivered : func ( ) {
delivredCh <- struct { } { }
} ,
2021-07-02 17:09:42 +00:00
}
outCh <- sd
// Just to confirm here that the message was picked up, to know if the
// the read process have stalled or not.
// For now it will not do anything,
select {
2021-07-05 05:43:33 +00:00
case <- delivredCh :
2021-07-02 17:09:42 +00:00
// OK.
case <- time . After ( time . Second * 5 ) :
2021-08-16 11:01:12 +00:00
// TODO: Check out if more logic should be made here if messages are stuck etc.
2021-07-02 17:09:42 +00:00
// Testing with a timeout here to figure out if messages are stuck
// waiting for done signal.
log . Printf ( "Error: *** message %v seems to be stuck, did not receive delivered signal from reading process\n" , v . ID )
2021-08-18 13:41:53 +00:00
r . metrics . promRingbufferStalledMessagesTotal . Inc ( )
2021-07-02 17:09:42 +00:00
}
2021-02-17 09:27:39 +00:00
// Listen on the done channel here , so a go routine handling the
// message will be able to signal back here that the message have
// been processed, and that we then can delete it out of the K/V Store.
2021-07-02 16:32:01 +00:00
select {
case <- v . Data . done :
log . Printf ( "info: processBufferMessages: done with message, deleting key from bucket, %v\n" , v . ID )
2021-08-26 09:41:46 +00:00
r . metrics . promMessagesProcessedIDLast . Set ( float64 ( v . ID ) )
2021-07-02 16:32:01 +00:00
// case <-time.After(time.Second * 3):
// // Testing with a timeout here to figure out if messages are stuck
// // waiting for done signal.
// fmt.Printf(" *** Ingo: message %v seems to be stuck, dropping message\n", v.ID)
}
2021-02-16 11:29:15 +00:00
2021-02-16 13:58:59 +00:00
// Since we are now done with the specific message we can delete
2021-02-17 09:27:39 +00:00
// it out of the K/V Store.
2021-09-14 14:23:01 +00:00
r . deleteKeyFromBucket ( r . samValueBucket , strconv . Itoa ( v . ID ) )
2021-02-17 09:27:39 +00:00
2021-08-27 10:27:38 +00:00
r . permStore <- fmt . Sprintf ( "%v : %+v\n" , time . Now ( ) . Format ( "Mon Jan _2 15:04:05 2006" ) , v )
2021-02-16 13:29:32 +00:00
2021-08-25 14:17:33 +00:00
// TODO: REMOVE: Dump the whole KV store
// err := r.printBucketContent(samValueBucket)
// if err != nil {
// log.Printf("* Error: dump of db failed: %v\n", err)
// }
2021-02-16 11:59:37 +00:00
} ( v )
2021-02-16 05:43:09 +00:00
2021-02-16 11:59:37 +00:00
}
2021-02-12 10:21:51 +00:00
2021-02-16 11:59:37 +00:00
close ( outCh )
2021-02-12 10:21:51 +00:00
}
2021-02-15 10:28:27 +00:00
2021-02-17 11:02:34 +00:00
// dumpBucket will dump out all they keys and values in the
// specified bucket, and return a sorted []samDBValue
func ( r * ringBuffer ) dumpBucket ( bucket string ) ( [ ] samDBValue , error ) {
samDBValues := [ ] samDBValue { }
err := r . db . View ( func ( tx * bolt . Tx ) error {
bu := tx . Bucket ( [ ] byte ( bucket ) )
2021-02-17 17:59:49 +00:00
if bu == nil {
return fmt . Errorf ( "error: dumpBucket: tx.bucket returned nil" )
}
2021-02-17 11:02:34 +00:00
// For each element found in the DB, unmarshal, and put on slice.
bu . ForEach ( func ( k , v [ ] byte ) error {
var vv samDBValue
err := json . Unmarshal ( v , & vv )
if err != nil {
log . Printf ( "error: dumpBucket json.Umarshal failed: %v\n" , err )
}
samDBValues = append ( samDBValues , vv )
return nil
} )
// Sort the order of the slice items based on ID, since they where retreived from a map.
sort . SliceStable ( samDBValues , func ( i , j int ) bool {
return samDBValues [ i ] . ID > samDBValues [ j ] . ID
} )
for _ , v := range samDBValues {
2021-04-16 11:43:58 +00:00
log . Printf ( "info: k/v store: %#v\n" , v )
2021-02-17 11:02:34 +00:00
}
2021-04-16 11:43:58 +00:00
2021-02-17 11:02:34 +00:00
return nil
} )
2021-02-17 17:59:49 +00:00
if err != nil {
return nil , err
}
2021-02-17 11:02:34 +00:00
return samDBValues , err
}
2021-08-16 11:01:12 +00:00
// printBucketContent will print out all they keys and values in the
2021-02-17 11:02:34 +00:00
// specified bucket.
func ( r * ringBuffer ) printBucketContent ( bucket string ) error {
2021-02-16 05:43:09 +00:00
err := r . db . View ( func ( tx * bolt . Tx ) error {
bu := tx . Bucket ( [ ] byte ( bucket ) )
bu . ForEach ( func ( k , v [ ] byte ) error {
var vv samDBValue
err := json . Unmarshal ( v , & vv )
if err != nil {
2021-02-17 17:59:49 +00:00
log . Printf ( "error: printBucketContent json.Umarshal failed: %v\n" , err )
2021-02-16 05:43:09 +00:00
}
2021-04-16 11:43:58 +00:00
log . Printf ( "k: %s, v: %v\n" , k , vv )
2021-02-16 05:43:09 +00:00
return nil
} )
2021-02-16 13:29:32 +00:00
return nil
} )
return err
}
2021-02-16 13:58:59 +00:00
// deleteKeyFromBucket will delete the specified key from the specified
// bucket if it exists.
2021-02-16 13:29:32 +00:00
func ( r * ringBuffer ) deleteKeyFromBucket ( bucket string , key string ) error {
err := r . db . Update ( func ( tx * bolt . Tx ) error {
bu := tx . Bucket ( [ ] byte ( bucket ) )
err := bu . Delete ( [ ] byte ( key ) )
if err != nil {
log . Printf ( "error: delete key in bucket %v failed: %v\n" , bucket , err )
}
2021-02-16 05:43:09 +00:00
return nil
} )
return err
}
2021-09-15 05:26:36 +00:00
// db update metrics.
func ( r * ringBuffer ) dbUpdateMetrics ( bucket string ) error {
err := r . db . Update ( func ( tx * bolt . Tx ) error {
bu := tx . Bucket ( [ ] byte ( bucket ) )
r . metrics . promDBMessagesCurrent . Set ( float64 ( bu . Stats ( ) . KeyN ) )
return nil
} )
return err
}
2021-02-16 13:58:59 +00:00
// getIndexValue will get the last index value stored in DB.
2021-09-14 14:23:01 +00:00
func ( r * ringBuffer ) getIndexValue ( ) int {
2021-02-16 03:57:54 +00:00
const indexKey string = "index"
2021-09-14 14:23:01 +00:00
indexB , err := r . dbView ( r . db , r . indexValueBucket , indexKey )
2021-02-16 03:57:54 +00:00
if err != nil {
log . Printf ( "error: getIndexValue: dbView: %v\n" , err )
}
index , err := strconv . Atoi ( string ( indexB ) )
2021-08-09 07:34:05 +00:00
if err != nil && string ( indexB ) == "" {
2021-08-23 09:53:47 +00:00
log . Printf ( "info: getIndexValue: no index value found, probaly empty database, and no previous entries in db to process : %v\n" , err )
2021-02-16 03:57:54 +00:00
}
2021-05-21 06:21:17 +00:00
// fmt.Printf("\n**** ringBuffer.getIndexValue: got index value = %v\n\n", index)
2021-02-16 03:57:54 +00:00
return index
}
2021-08-16 11:01:12 +00:00
// dbView will look up and return a specific value if it exists for a key in a bucket in a DB.
2021-02-15 10:28:27 +00:00
func ( r * ringBuffer ) dbView ( db * bolt . DB , bucket string , key string ) ( [ ] byte , error ) {
var value [ ] byte
2021-08-16 11:01:12 +00:00
// View is a help function to get values out of the database.
2021-02-15 10:28:27 +00:00
err := db . View ( func ( tx * bolt . Tx ) error {
//Open a bucket to get key's and values from.
bu := tx . Bucket ( [ ] byte ( bucket ) )
2021-02-16 03:57:54 +00:00
if bu == nil {
2021-08-23 09:53:47 +00:00
log . Printf ( "info: no db bucket exist: %v\n" , bucket )
2021-02-16 03:57:54 +00:00
return nil
}
2021-02-15 10:28:27 +00:00
v := bu . Get ( [ ] byte ( key ) )
if len ( v ) == 0 {
2021-02-16 03:57:54 +00:00
log . Printf ( "info: view: key not found\n" )
return nil
2021-02-15 10:28:27 +00:00
}
value = v
return nil
} )
return value , err
}
//dbUpdate will update the specified bucket with a key and value.
func ( r * ringBuffer ) dbUpdate ( db * bolt . DB , bucket string , key string , value [ ] byte ) error {
err := db . Update ( func ( tx * bolt . Tx ) error {
//Create a bucket
bu , err := tx . CreateBucketIfNotExists ( [ ] byte ( bucket ) )
if err != nil {
return fmt . Errorf ( "error: CreateBuckerIfNotExists failed: %v" , err )
}
//Put a value into the bucket.
if err := bu . Put ( [ ] byte ( key ) , [ ] byte ( value ) ) ; err != nil {
return err
}
//If all was ok, we should return a nil for a commit to happen. Any error
// returned will do a rollback.
return nil
} )
return err
}
2021-02-17 09:27:39 +00:00
// startPermStore will start the process that will handle writing of
// handled message to a permanent file.
// To store a message in the store, send what to store on the
// ringbuffer.permStore channel.
func ( r * ringBuffer ) startPermanentStore ( ) {
const storeFile string = "store.log"
f , err := os . OpenFile ( storeFile , os . O_APPEND | os . O_RDWR | os . O_CREATE , 0600 )
if err != nil {
2021-03-02 12:46:02 +00:00
log . Printf ( "error: startPermanentStore: failed to open file: %v\n" , err )
2021-02-17 09:27:39 +00:00
}
defer f . Close ( )
for {
d := <- r . permStore
_ , err := f . WriteString ( d )
if err != nil {
log . Printf ( "error:failed to write entry: %v\n" , err )
}
}
}