github.com/lirm/aeron-go@v0.0.0-20230415210743-920325491dc4/cluster/mark_file.go (about)

     1  package cluster
     2  
     3  import (
     4  	"os"
     5  	"time"
     6  
     7  	"github.com/lirm/aeron-go/aeron/util"
     8  
     9  	"github.com/lirm/aeron-go/aeron/atomic"
    10  	// "github.com/lirm/aeron-go/aeron/util"
    11  	"github.com/lirm/aeron-go/aeron/util/memmap"
    12  )
    13  
    14  const (
    15  	HeaderLength      = 8 * 1024
    16  	ErrorBufferLength = 1024 * 1024
    17  )
    18  
    19  type ClusterMarkFile struct {
    20  	file      *memmap.File
    21  	buffer    *atomic.Buffer
    22  	flyweight *MarkFileHeaderFlyweight
    23  }
    24  
    25  func NewClusterMarkFile(filename string) (*ClusterMarkFile, error) {
    26  	f, err := memmap.NewFile(filename, 0, HeaderLength+ErrorBufferLength)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  
    31  	b := atomic.MakeBuffer(f.GetMemoryPtr(), f.GetMemorySize())
    32  
    33  	fly := &MarkFileHeaderFlyweight{}
    34  	fly.Wrap(b, 0)
    35  
    36  	fly.CandidateTermId.Set(-1)
    37  	fly.ComponentType.Set(2)
    38  	fly.HeaderLength.Set(HeaderLength)
    39  	fly.ErrorBufferLength.Set(ErrorBufferLength)
    40  	fly.Pid.Set(int64(os.Getpid()))
    41  	fly.StartTimestamp.Set(time.Now().UnixMilli())
    42  
    43  	return &ClusterMarkFile{
    44  		file:      f,
    45  		buffer:    b,
    46  		flyweight: fly,
    47  	}, nil
    48  }
    49  
    50  func (cmf *ClusterMarkFile) SignalReady() {
    51  	semanticVersion := util.SemanticVersionCompose(0, 3, 0)
    52  	//cmf.flyweight.Version.Set(int32(semanticVersion))
    53  	cmf.buffer.PutInt32Ordered(0, int32(semanticVersion))
    54  }
    55  
    56  func (cmf *ClusterMarkFile) SignalFailedStart() {
    57  	cmf.flyweight.Version.Set(-1)
    58  }
    59  
    60  func (cmf *ClusterMarkFile) UpdateActivityTimestamp(timestamp int64) {
    61  	cmf.flyweight.ActivityTimestamp.Set(timestamp)
    62  }