github.com/siglens/siglens@v0.0.0-20240328180423-f7ce9ae441ed/pkg/localnodeid/localnodeid.go (about)

     1  /*
     2  Copyright 2023.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package localnodeid
    18  
    19  import (
    20  	"os"
    21  
    22  	"github.com/lithammer/shortuuid/v4"
    23  	"github.com/siglens/siglens/pkg/config"
    24  	log "github.com/sirupsen/logrus"
    25  )
    26  
    27  const DEFAULT_ALLOWED_VOLUME_GB = 100
    28  
    29  var runningNodeId string
    30  var initIDFile bool // if true, this data dir has been initialized for the first time
    31  
    32  func getNodeIdFile() string {
    33  	return config.GetDataPath() + "common/id.info"
    34  }
    35  
    36  func getCommonDir() string {
    37  	return config.GetDataPath() + "common/"
    38  }
    39  
    40  func initNewNodeID(fName string) {
    41  	nodeUUID := shortuuid.New()
    42  	err := os.MkdirAll(getCommonDir(), 0755)
    43  	if err != nil {
    44  		log.Errorf("Failed to create common directory: %v", err)
    45  		runningNodeId = nodeUUID
    46  		return
    47  	}
    48  
    49  	fd, err := os.OpenFile(fName, os.O_CREATE|os.O_TRUNC|os.O_RDWR, 0755)
    50  	if err != nil {
    51  		log.Errorf("Failed to open node id file %s: %+v", fName, err)
    52  		runningNodeId = nodeUUID
    53  		return
    54  	}
    55  	defer fd.Close()
    56  	_, err = fd.WriteString(nodeUUID)
    57  	if err != nil {
    58  		log.Errorf("Failed to write to node id file %s: %+v", fName, err)
    59  		runningNodeId = nodeUUID
    60  		return
    61  	}
    62  	_ = fd.Sync()
    63  	_ = fd.Chmod(0444)
    64  	runningNodeId = nodeUUID
    65  	initIDFile = true
    66  }
    67  
    68  func fetchNodeIdentifier() {
    69  	// check if a node identifier file already exists, else init a uuid and flush it to disk
    70  	fName := getNodeIdFile()
    71  	if _, err := os.Stat(fName); err != nil {
    72  		initNewNodeID(fName)
    73  		return
    74  	}
    75  
    76  	readID, err := os.ReadFile(fName)
    77  	if err != nil {
    78  		initNewNodeID(fName)
    79  		return
    80  	}
    81  	runningNodeId = string(readID)
    82  	if len(runningNodeId) == 0 {
    83  		initNewNodeID(fName)
    84  	}
    85  }
    86  
    87  // returns true if this proc starts a new id file. returns false if an id file already exists
    88  func IsInitServer() bool {
    89  	return initIDFile
    90  }
    91  
    92  func GetRunningNodeID() string {
    93  	if runningNodeId == "" {
    94  		fetchNodeIdentifier()
    95  	}
    96  	return runningNodeId
    97  }