2022-06-09 03:29:41 +00:00
package steward
import (
"context"
"fmt"
2022-06-09 06:42:49 +00:00
"log"
2022-06-09 09:25:59 +00:00
"path/filepath"
2022-06-09 03:29:41 +00:00
2022-06-09 09:25:59 +00:00
"github.com/fxamacker/cbor/v2"
2022-06-09 03:29:41 +00:00
"github.com/google/uuid"
)
type methodREQCopySrc struct {
event Event
}
func ( m methodREQCopySrc ) getKind ( ) Event {
return m . event
}
// Idea:
//
// Initialization, Source:
// - Use the REQCopySrc method to handle the initial request from the user.
// - Spawn a REQCopySrc_uid subscriber to receive sync messages from destination.
// - Send the uid, and full-file hash to the destination in a REQCopyDst message.
//
// Initialization, Destination:
// - Spawn a REQCopyDst-uid from the uid we got from source.
// --------------------------------------------------------------------------------------
//
// All below happens in the From-uid and To-uid methods until the copying is done.
//
// - dst->src, dst sends a REQCopySrc-uid message with status "ready" file receiving to src.
// - src receives the message and start reading the file:
// - src, creates hash of the complete file.
// - src, reads the file in chunks, and create a hash of each chunk.
// - src->dst, send chunk when read.
// - dst->src, wait for status "ready" indicating the chuck was transfered.
// - Loop and read new chunc.
// - src->dst, when last chunch is sent send status "last"
// - src->dst, if failure send status "error", abort file copying and clean up on both src and dst.
//
// - dst, read and store each chunch to tmp folder and verify hash.
// - dst->src, send status "ready" to src when chunch is stored.
// - loop and check for status "last", if last:
// - build original file from chuncs.
// - verify hash when file is built.
// - dst->src, send status "done".
//
// - We should also be be able to resend a chunk, or restart the copying from where we left of if it seems to hang.
//
// dataStructure{
// Data []bytes
// Status copyStatus
// id int
// }
//
// Create a new copy sync process to handle the actual file copying.
// We use the context already created based on the time out specified
// in the requestTimeout field of the message.
//
// -----------------------------------------------------
// Handle writing to a file. Will truncate any existing data if the file did already
// exist.
func ( m methodREQCopySrc ) handler ( proc process , message Message , node string ) ( [ ] byte , error ) {
var subProcessName string
proc . processes . wg . Add ( 1 )
go func ( ) {
defer proc . processes . wg . Done ( )
switch {
case len ( message . MethodArgs ) < 3 :
er := fmt . Errorf ( "error: methodREQCopySrc: got <3 number methodArgs: want srcfilePath,dstNode,dstFilePath" )
proc . errorKernel . errSend ( proc , message , er )
return
}
2022-06-09 08:27:20 +00:00
SrcFilePath := message . MethodArgs [ 0 ]
DstNode := message . MethodArgs [ 1 ]
DstFilePath := message . MethodArgs [ 2 ]
2022-06-09 03:29:41 +00:00
// Get a context with the timeout specified in message.MethodTimeout.
2022-06-09 03:59:37 +00:00
// Since the subProc spawned will outlive this method here we do not
// want to cancel this method. We care about the methodTimeout, but
// we ignore the CancelFunc.
2022-06-09 09:25:59 +00:00
ctx , cancel := getContextForMethodTimeout ( proc . ctx , message )
2022-06-09 03:29:41 +00:00
// Create a subject for one copy request
uid := uuid . New ( )
2022-06-10 04:34:39 +00:00
subProcessName = fmt . Sprintf ( "copySrc.%v" , uid . String ( ) )
2022-06-09 03:29:41 +00:00
m := Method ( subProcessName )
sub := newSubjectNoVerifyHandler ( m , node )
// Create a new sub process that will do the actual file copying.
copySrcSubProc := newProcess ( ctx , proc . server , sub , processKindSubscriber , nil )
2022-06-09 09:25:59 +00:00
2022-06-09 03:29:41 +00:00
// Give the sub process a procFunc so we do the actual copying within a procFunc,
// and not directly within the handler.
2022-06-09 08:18:09 +00:00
copySrcSubProc . procFunc = copySrcProcFunc ( copySrcSubProc )
2022-06-09 09:25:59 +00:00
2022-06-09 03:29:41 +00:00
// The process will be killed when the context expires.
go copySrcSubProc . spawnWorker ( )
2022-06-09 09:25:59 +00:00
// Send a message over the the node where the destination file will be written,
// to also start up a sub process on the destination node.
dstDir := filepath . Dir ( DstFilePath )
dstFile := filepath . Base ( DstFilePath )
// Marshal the data payload to send the the dst.
cia := copyInitialData {
2022-06-09 09:40:53 +00:00
UUID : uid . String ( ) ,
DstDir : dstDir ,
DstFile : dstFile ,
2022-06-09 09:25:59 +00:00
}
cb , err := cbor . Marshal ( cia )
if err != nil {
er := fmt . Errorf ( "error: newSubjectAndMessage : %v, message: %v" , err , message )
proc . errorKernel . errSend ( proc , message , er )
cancel ( )
}
msg := message
msg . ToNode = Node ( DstNode )
//msg.Method = REQToFile
msg . Method = REQCopyDst
msg . Data = cb
2022-06-09 09:40:53 +00:00
// msg.Directory = dstDir
// msg.FileName = dstFile
2022-06-09 09:25:59 +00:00
sam , err := newSubjectAndMessage ( msg )
if err != nil {
er := fmt . Errorf ( "error: methodREQCopySrc failed to cbor Marshal data: %v, message=%v" , err , message )
proc . errorKernel . errSend ( proc , message , er )
cancel ( )
}
proc . toRingbufferCh <- [ ] subjectAndMessage { sam }
2022-06-10 04:17:11 +00:00
replyData := fmt . Sprintf ( "info: succesfully initiated copy source process: procName=%v, srcNode=%v, srcPath=%v, dstNode=%v, dstPath=%v, starting sub process=%v for the actual copying\n" , copySrcSubProc . processName , node , SrcFilePath , DstNode , DstFilePath , subProcessName )
2022-06-09 08:27:20 +00:00
newReplyMessage ( proc , message , [ ] byte ( replyData ) )
2022-06-09 03:29:41 +00:00
} ( )
ackMsg := [ ] byte ( "confirmed from: " + node + ": " + fmt . Sprint ( message . ID ) )
return ackMsg , nil
}
2022-06-09 09:25:59 +00:00
type copyInitialData struct {
2022-06-09 09:40:53 +00:00
UUID string
DstDir string
DstFile string
2022-06-09 09:25:59 +00:00
}
2022-06-09 03:29:41 +00:00
// ----
type methodREQCopyDst struct {
event Event
}
func ( m methodREQCopyDst ) getKind ( ) Event {
return m . event
}
// Handle writing to a file. Will truncate any existing data if the file did already
// exist.
// Same as the REQToFile, but this requst type don't use the default data folder path
// for where to store files or add information about node names.
// This method also sends a msgReply back to the publisher if the method was done
// successfully, where REQToFile do not.
// This method will truncate and overwrite any existing files.
func ( m methodREQCopyDst ) handler ( proc process , message Message , node string ) ( [ ] byte , error ) {
2022-06-09 09:40:53 +00:00
var subProcessName string
2022-06-09 03:29:41 +00:00
proc . processes . wg . Add ( 1 )
go func ( ) {
defer proc . processes . wg . Done ( )
2022-06-09 09:25:59 +00:00
var cia copyInitialData
err := cbor . Unmarshal ( message . Data , & cia )
if err != nil {
er := fmt . Errorf ( "error: methodREQCopyDst: failed to cbor Unmarshal data: %v, message=%v" , err , message )
proc . errorKernel . errSend ( proc , message , er )
return
}
2022-06-09 09:40:53 +00:00
fmt . Printf ( "\n ******* WE RECEIVED COPY MESSAGE: %+v\n\n" , cia )
// Get a context with the timeout specified in message.MethodTimeout.
// Since the subProc spawned will outlive this method here we do not
// want to cancel this method. We care about the methodTimeout, but
// we ignore the CancelFunc.
ctx , _ := getContextForMethodTimeout ( proc . ctx , message )
// Create a subject for one copy request
2022-06-10 04:34:39 +00:00
subProcessName = fmt . Sprintf ( "copyDst.%v" , cia . UUID )
2022-06-09 09:40:53 +00:00
m := Method ( subProcessName )
sub := newSubjectNoVerifyHandler ( m , node )
// Create a new sub process that will do the actual file copying.
copyDstSubProc := newProcess ( ctx , proc . server , sub , processKindSubscriber , nil )
// Give the sub process a procFunc so we do the actual copying within a procFunc,
// and not directly within the handler.
copyDstSubProc . procFunc = copyDstProcFunc ( copyDstSubProc )
// The process will be killed when the context expires.
go copyDstSubProc . spawnWorker ( )
2022-06-09 09:25:59 +00:00
2022-06-10 04:17:11 +00:00
fp := filepath . Join ( cia . DstDir , cia . DstFile )
replyData := fmt . Sprintf ( "info: succesfully initiated copy source process: procName=%v, srcNode=%v, dstPath=%v, starting sub process=%v for the actual copying\n" , copyDstSubProc . processName , node , fp , subProcessName )
newReplyMessage ( proc , message , [ ] byte ( replyData ) )
2022-06-09 03:29:41 +00:00
} ( )
ackMsg := [ ] byte ( "confirmed from: " + node + ": " + fmt . Sprint ( message . ID ) )
return ackMsg , nil
}
2022-06-09 09:40:53 +00:00
2022-06-10 04:17:11 +00:00
func copySrcProcFunc ( proc process ) func ( context . Context , chan Message ) error {
pf := func ( ctx context . Context , procFuncCh chan Message ) error {
select {
case <- ctx . Done ( ) :
log . Printf ( " * copySrcProcFunc ended: %v\n" , proc . processName )
}
return nil
}
return pf
}
2022-06-09 09:40:53 +00:00
func copyDstProcFunc ( proc process ) func ( context . Context , chan Message ) error {
pf := func ( ctx context . Context , procFuncCh chan Message ) error {
select {
case <- ctx . Done ( ) :
log . Printf ( " * copyDstProcFunc ended: %v\n" , proc . processName )
}
return nil
}
return pf
}