Skip to content

Commit

Permalink
Merge pull request #17 from MZC-CSC/develop
Browse files Browse the repository at this point in the history
bug fix for install load generator server
  • Loading branch information
MZC-CSC authored Oct 2, 2024
2 parents dcd7db1 + 5fa0465 commit 953ab7d
Show file tree
Hide file tree
Showing 9 changed files with 315 additions and 365 deletions.
2 changes: 1 addition & 1 deletion internal/core/load/dtos.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type LoadGeneratorServerResult struct {
Lon string `json:"lon,omitempty"`
Username string `json:"username,omitempty"`
VmId string `json:"vmId,omitempty"`
StartTime time.Time `json:"startTime,omitempty"`
StartTime string `json:"startTime,omitempty"`
AdditionalVmKey string `json:"additionalVmKey,omitempty"`
Label string `json:"label,omitempty"`
CreatedAt time.Time `json:"createdAt,omitempty"`
Expand Down
256 changes: 161 additions & 95 deletions internal/core/load/load_generator_install_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,147 +71,214 @@ func (l *LoadService) InstallLoadGenerator(param InstallLoadGeneratorParam) (Loa
}
}()

log.Println("install info : ", loadGeneratorInstallInfo)
log.Printf("install info : %+v\n", loadGeneratorInstallInfo)

if len(loadGeneratorInstallInfo.LoadGeneratorServers) == 0 {
installLocation := param.InstallLocation
installScriptPath := utils.JoinRootPathWith("/script/install-jmeter.sh")
// remote && server len == 0
// local && loadGeneratorInstallInfo.status != "installed"

switch installLocation {
case constant.Local:
utils.LogInfo("Starting local installation of JMeter")
installLocation := param.InstallLocation
installScriptPath := utils.JoinRootPathWith("/script/install-jmeter.sh")

switch installLocation {
case constant.Local:
utils.LogInfo("Starting local installation of JMeter")

jmeterPath := config.AppConfig.Load.JMeter.Dir
jmeterVersion := config.AppConfig.Load.JMeter.Version

exist := utils.ExistCheck(jmeterPath) && utils.ExistCheck(jmeterPath+"/apache-jmeter-"+jmeterVersion)
if !exist {
err := utils.Script(installScriptPath, []string{
fmt.Sprintf("JMETER_WORK_DIR=%s", config.AppConfig.Load.JMeter.Dir),
fmt.Sprintf("JMETER_VERSION=%s", config.AppConfig.Load.JMeter.Version),
fmt.Sprintf("JMETER_WORK_DIR=%s", jmeterPath),
fmt.Sprintf("JMETER_VERSION=%s", jmeterVersion),
})

if err != nil {
utils.LogError("Error while installing JMeter locally:", err)
return result, fmt.Errorf("error while installing jmeter; %s", err)
}
utils.LogInfo("Local installation of JMeter completed successfully")
case constant.Remote:
utils.LogInfo("Starting remote installation of JMeter")
// get the spec and image information
recommendVm, err := l.getRecommendVm(ctx, param.Coordinates)
if err != nil {
utils.LogError("Failed to get recommended VM:", err)
return result, err
}
antVmCommonSpec := recommendVm[0].Name
antVmConnectionName := recommendVm[0].ConnectionName
antVmCommonImage, err := utils.ReplaceAtIndex(antVmCommonSpec, imageOs, "+", 2)
}

if err != nil {
utils.LogError("Error replacing VM spec index:", err)
return result, err
}
utils.LogInfo("Local installation of JMeter completed successfully")
case constant.Remote:
utils.LogInfo("Starting remote installation of JMeter")
// get the spec and image information
recommendVm, err := l.getRecommendVm(ctx, param.Coordinates)
if err != nil {
utils.LogError("Failed to get recommended VM:", err)
return result, err
}
antVmCommonSpec := recommendVm[0].Name
recommendVmConnName := recommendVm[0].ConnectionName
antVmCommonImage, err := utils.ReplaceAtIndex(antVmCommonSpec, imageOs, "+", 2)

if err != nil {
utils.LogError("Error replacing VM spec index:", err)
return result, err
}

// check namespace is valid or not
err = l.validDefaultNs(ctx, antNsId)
if err != nil {
utils.LogError("Error validating default namespace:", err)
return result, err
}

// get the ant default mci
antMci, err := l.getAndDefaultMci(ctx, antVmCommonSpec, antVmCommonImage, recommendVmConnName)
if err != nil {
utils.LogError("Error getting or creating default mci:", err)
return result, err
}

// if server is not running state, try to resume and get mci information
retryCount := config.AppConfig.Load.Retry
for retryCount > 0 && antMci.StatusCount.CountRunning < 1 {
utils.LogInfof("Attempting to resume MCI, retry count: %d", retryCount)

// check namespace is valid or not
err = l.validDefaultNs(ctx, antNsId)
err = l.tumblebugClient.ControlLifecycleWithContext(ctx, antNsId, antMci.Id, "resume")
if err != nil {
utils.LogError("Error validating default namespace:", err)
utils.LogError("Error resuming MCI:", err)
return result, err
}

// get the ant default mci
antMci, err := l.getAndDefaultMci(ctx, antVmCommonSpec, antVmCommonImage, antVmConnectionName)
time.Sleep(defaultDelay)
antMci, err = l.getAndDefaultMci(ctx, antVmCommonSpec, antVmCommonImage, recommendVmConnName)
if err != nil {
utils.LogError("Error getting or creating default mci:", err)
utils.LogError("Error getting MCI after resume attempt:", err)
return result, err
}

// if server is not running state, try to resume and get mci information
retryCount := config.AppConfig.Load.Retry
for retryCount > 0 && antMci.StatusCount.CountRunning < 1 {
utils.LogInfof("Attempting to resume MCI, retry count: %d", retryCount)

err = l.tumblebugClient.ControlLifecycleWithContext(ctx, antNsId, antMci.ID, "resume")
if err != nil {
utils.LogError("Error resuming MCI:", err)
return result, err
}
time.Sleep(defaultDelay)
antMci, err = l.getAndDefaultMci(ctx, antVmCommonSpec, antVmCommonImage, antVmConnectionName)
if err != nil {
utils.LogError("Error getting MCI after resume attempt:", err)
return result, err
}
retryCount = retryCount - 1
}

retryCount = retryCount - 1
}
if antMci.StatusCount.CountRunning < 1 {
utils.LogError("No running VM on ant default MCI")
return result, errors.New("there is no running vm on ant default mci")
}

if antMci.StatusCount.CountRunning < 1 {
utils.LogError("No running VM on ant default MCI")
return result, errors.New("there is no running vm on ant default mci")
}
addAuthorizedKeyCommand, err := getAddAuthorizedKeyCommand(antPrivKeyName, antPubKeyName)
if err != nil {
utils.LogError("Error getting add authorized key command:", err)
return result, err
}

addAuthorizedKeyCommand, err := getAddAuthorizedKeyCommand(antPrivKeyName, antPubKeyName)
if err != nil {
utils.LogError("Error getting add authorized key command:", err)
return result, err
}
installationCommand, err := utils.ReadToString(installScriptPath)
if err != nil {
utils.LogError("Error reading installation script:", err)
return result, err
}

installationCommand, err := utils.ReadToString(installScriptPath)
if err != nil {
utils.LogError("Error reading installation script:", err)
return result, err
}
commandReq := tumblebug.SendCommandReq{
Command: []string{installationCommand, addAuthorizedKeyCommand},
}

commandReq := tumblebug.SendCommandReq{
Command: []string{installationCommand, addAuthorizedKeyCommand},
}
_, err = l.tumblebugClient.CommandToMciWithContext(ctx, antNsId, antMci.Id, commandReq)
if err != nil {
utils.LogError("Error sending command to MCI:", err)
return result, err
}

_, err = l.tumblebugClient.CommandToMciWithContext(ctx, antNsId, antMci.ID, commandReq)
if err != nil {
utils.LogError("Error sending command to MCI:", err)
return result, err
}
utils.LogInfo("Commands sent to MCI successfully")

utils.LogInfo("Commands sent to MCI successfully")
marking := make(map[string]LoadGeneratorServer)
deleteChecker := make(map[uint]bool)
deleteList := make([]uint, 0)

loadGeneratorServers := make([]LoadGeneratorServer, 0)
for _, v := range loadGeneratorInstallInfo.LoadGeneratorServers {
marking[v.VmUid] = v
deleteChecker[v.ID] = false
}

for i, vm := range antMci.VMs {
loadGeneratorServer := LoadGeneratorServer{
loadGeneratorServers := make([]LoadGeneratorServer, 0)

for i, vm := range antMci.Vm {
var loadGeneratorServer LoadGeneratorServer

l, ok := marking[vm.Uid]

if ok {
deleteChecker[l.ID] = true
loadGeneratorServer = l
loadGeneratorServer.VmUid = vm.Uid
loadGeneratorServer.VmName = vm.Name
loadGeneratorServer.ImageName = vm.CspImageName
loadGeneratorServer.Csp = vm.ConnectionConfig.ProviderName
loadGeneratorServer.Region = vm.Region.Region
loadGeneratorServer.Zone = vm.Region.Zone
loadGeneratorServer.PublicIp = vm.PublicIP
loadGeneratorServer.PrivateIp = vm.PrivateIP
loadGeneratorServer.PublicDns = vm.PublicDNS
loadGeneratorServer.MachineType = vm.CspSpecName
loadGeneratorServer.Status = vm.Status
loadGeneratorServer.SshPort = vm.SSHPort
loadGeneratorServer.Lat = fmt.Sprintf("%f", vm.Location.Latitude)
loadGeneratorServer.Lon = fmt.Sprintf("%f", vm.Location.Longitude)
loadGeneratorServer.Username = vm.VMUserName
loadGeneratorServer.VmId = vm.Id
loadGeneratorServer.StartTime = vm.CreatedTime
loadGeneratorServer.AdditionalVmKey = vm.CspResourceId
loadGeneratorServer.Label = "temp-label"
loadGeneratorServer.IsCluster = false
loadGeneratorServer.IsMaster = i == 0
loadGeneratorServer.ClusterSize = uint64(len(antMci.Vm))
} else {
loadGeneratorServer = LoadGeneratorServer{
VmUid: vm.Uid,
VmName: vm.Name,
ImageName: vm.CspImageName,
Csp: vm.ConnectionConfig.ProviderName,
Region: vm.Region.Region,
Zone: vm.Region.Zone,
PublicIp: vm.PublicIP,
PrivateIp: vm.PrivateIP,
PublicDns: vm.PublicDNS,
MachineType: vm.CspViewVMDetail.VMSpecName,
MachineType: vm.CspSpecName,
Status: vm.Status,
SshPort: vm.SSHPort,
Lat: fmt.Sprintf("%f", vm.Location.Latitude),
Lon: fmt.Sprintf("%f", vm.Location.Longitude),
Username: vm.CspViewVMDetail.VMUserID,
VmId: vm.CspViewVMDetail.IID.SystemID,
StartTime: vm.CspViewVMDetail.StartTime,
AdditionalVmKey: vm.ID,
Username: vm.VMUserName,
VmId: vm.Id,
StartTime: vm.CreatedTime,
AdditionalVmKey: vm.CspResourceId,
Label: "temp-label",
IsCluster: false,
IsMaster: i == 0,
ClusterSize: uint64(len(antMci.VMs)),
ClusterSize: uint64(len(antMci.Vm)),
}

loadGeneratorServers = append(loadGeneratorServers, loadGeneratorServer)
}

loadGeneratorInstallInfo.LoadGeneratorServers = loadGeneratorServers
loadGeneratorInstallInfo.PublicKeyName = antPubKeyName
loadGeneratorInstallInfo.PrivateKeyName = antPrivKeyName
loadGeneratorServers = append(loadGeneratorServers, loadGeneratorServer)
}

loadGeneratorInstallInfo.Status = "installed"
err = l.loadRepo.UpdateLoadGeneratorInstallInfoTx(ctx, loadGeneratorInstallInfo)
if err != nil {
utils.LogError("Error updating LoadGeneratorInstallInfo after installed:", err)
return result, err
for id, ok := range deleteChecker {
if !ok {
deleteList = append(deleteList, id)
}
}

if len(deleteList) > 0 {
err = l.loadRepo.DeleteLoadGeneratorServerTx(ctx, deleteList)
if err != nil {
utils.LogError("Error delete load generator list:", err)
return result, err
}
}

utils.LogInfo("LoadGeneratorInstallInfo updated successfully")
loadGeneratorInstallInfo.LoadGeneratorServers = loadGeneratorServers
loadGeneratorInstallInfo.PublicKeyName = antPubKeyName
loadGeneratorInstallInfo.PrivateKeyName = antPrivKeyName
}

loadGeneratorInstallInfo.Status = "installed"
err = l.loadRepo.UpdateLoadGeneratorInstallInfoTx(ctx, loadGeneratorInstallInfo)
if err != nil {
utils.LogError("Error updating LoadGeneratorInstallInfo after installed:", err)
return result, err
}

utils.LogInfo("LoadGeneratorInstallInfo updated successfully")

loadGeneratorServerResults := make([]LoadGeneratorServerResult, 0)
for _, l := range loadGeneratorInstallInfo.LoadGeneratorServers {
lr := LoadGeneratorServerResult{
Expand Down Expand Up @@ -291,8 +358,7 @@ func (l *LoadService) getAndDefaultMci(ctx context.Context, antVmCommonSpec, ant
} else {
return antMci, err
}
} else if antMci.VMs != nil && len(antMci.VMs) == 0 {

} else if antMci.Vm != nil && len(antMci.Vm) == 0 {
dynamicVmArg := tumblebug.DynamicVmReq{
CommonImage: antVmCommonImage,
CommonSpec: antVmCommonSpec,
Expand Down
20 changes: 10 additions & 10 deletions internal/core/load/metrics_agent_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func (l *LoadService) InstallMonitoringAgent(param MonitoringAgentInstallationPa
return res, err
}

if len(mci.VMs) == 0 {
if len(mci.Vm) == 0 {
utils.LogErrorf("No VMs found on mci. Provision VM first.")
return res, errors.New("there is no vm on mci. provision vm first")
}
Expand All @@ -46,8 +46,8 @@ func (l *LoadService) InstallMonitoringAgent(param MonitoringAgentInstallationPa

var errorCollection []error

for _, vm := range mci.VMs {
if mapSet != nil && !utils.Contains(mapSet, vm.ID) {
for _, vm := range mci.Vm {
if mapSet != nil && !utils.Contains(mapSet, vm.Id) {
continue
}
ctx, cancel := context.WithTimeout(ctx, 30*time.Second)
Expand All @@ -58,13 +58,13 @@ func (l *LoadService) InstallMonitoringAgent(param MonitoringAgentInstallationPa
AgentType: "perfmon",
NsId: param.NsId,
MciId: param.MciId,
VmId: vm.ID,
VmCount: len(mci.VMs),
VmId: vm.Id,
VmCount: len(mci.Vm),
}
utils.LogInfof("Inserting monitoring agent installation info into database vm id : %s", vm.ID)
utils.LogInfof("Inserting monitoring agent installation info into database vm id : %s", vm.Id)
err = l.loadRepo.InsertMonitoringAgentInfoTx(ctx, &m)
if err != nil {
utils.LogErrorf("Failed to insert monitoring agent info for vm id %s : %v", vm.ID, err)
utils.LogErrorf("Failed to insert monitoring agent info for vm id %s : %v", vm.Id, err)
errorCollection = append(errorCollection, err)
continue
}
Expand All @@ -74,13 +74,13 @@ func (l *LoadService) InstallMonitoringAgent(param MonitoringAgentInstallationPa
UserName: username,
}

utils.LogInfof("Sending install command to mci. NS: %s, mci: %s, VMID: %s", param.NsId, param.MciId, vm.ID)
_, err = l.tumblebugClient.CommandToVmWithContext(ctx, param.NsId, param.MciId, vm.ID, commandReq)
utils.LogInfof("Sending install command to mci. NS: %s, mci: %s, VMID: %s", param.NsId, param.MciId, vm.Id)
_, err = l.tumblebugClient.CommandToVmWithContext(ctx, param.NsId, param.MciId, vm.Id, commandReq)

if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
m.Status = "timeout"
utils.LogErrorf("Timeout of context. Already 15 seconds has been passed. vm id : %s", vm.ID)
utils.LogErrorf("Timeout of context. Already 15 seconds has been passed. vm id : %s", vm.Id)
} else {
m.Status = "failed"
utils.LogErrorf("Error occurred during command execution: %v", err)
Expand Down
Loading

0 comments on commit 953ab7d

Please sign in to comment.