in cmd/ndb-pod-initializer/main.go [141:195]
func writeNodeIDToFile(hostname, ndbConnectString string) (nodeId int, nodeIdPool []int, nodeType mgmapi.NodeTypeEnum) {
// All nodeIds are sequentially assigned based on the ordinal indices
// of the StatefulSet pods. So deduce the nodeId of the first MySQL
// Cluster node of same nodeType (i.e) the node with StatefulSet ordinal
// index 0, of same type. Then the nodeId of the current node will be
// startNodeId + pod ordinal index.
var startNodeIdOfSameNodeType int
switch getPodMySQLClusterNodeType(hostname) {
case constants.NdbNodeTypeMgmd:
// Management nodes have nodeId 1 and 2.
startNodeIdOfSameNodeType = 1
nodeType = mgmapi.NodeTypeMGM
case constants.NdbNodeTypeNdbmtd:
// Data nodes' nodeId continue sequentially
// after the Management nodes' nodeIds.
numOfManagementNode := len(strings.Split(ndbConnectString, ",")) - 1
startNodeIdOfSameNodeType = numOfManagementNode + 1
nodeType = mgmapi.NodeTypeNDB
case constants.NdbNodeTypeMySQLD:
nodeType = mgmapi.NodeTypeAPI
}
// Extract pod ordinal index from hostname.
// Hostname will be of form <ndbcluster name>-<node-type>-<sfset pod ordinal index>
tokens := strings.Split(hostname, "-")
podOrdinalIndex, _ := strconv.ParseInt(tokens[len(tokens)-1], 10, 32)
// Calculate nodeId
var nodeIdText string
if nodeType == mgmapi.NodeTypeAPI {
// For MySQL Servers, if connection pool is enabled, successive
// nodeIds are assigned to a single MySQL Server
ndbConnectionPoolSize, _ := strconv.ParseInt(os.Getenv("NDB_CONNECTION_POOL_SIZE"), 10, 32)
startNodeId := constants.NdbNodeTypeAPIStartNodeId + int(ndbConnectionPoolSize)*int(podOrdinalIndex)
endNodeId := startNodeId + int(ndbConnectionPoolSize)
for ; startNodeId < endNodeId; startNodeId++ {
nodeIdPool = append(nodeIdPool, startNodeId)
nodeIdText += fmt.Sprintf("%d,", startNodeId)
}
nodeIdText = nodeIdText[:len(nodeIdText)-1]
} else {
nodeId = startNodeIdOfSameNodeType + int(podOrdinalIndex)
nodeIdText = fmt.Sprintf("%d", nodeId)
}
// Persist the nodeId into a file to be used by other scripts/commands
f, err := os.OpenFile(statefulset.NodeIdFilePath,
os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600)
failOnError(err, "Failed to create file %q : %s", statefulset.NodeIdFilePath, err)
_, err = f.WriteString(nodeIdText)
failOnError(err, "Failed to write nodeId to file : %s", err)
return nodeId, nodeIdPool, nodeType
}