guest_block_io_throttle_task.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package guest
  15. import (
  16. "context"
  17. "fmt"
  18. "yunion.io/x/jsonutils"
  19. api "yunion.io/x/onecloud/pkg/apis/compute"
  20. "yunion.io/x/onecloud/pkg/cloudcommon/db"
  21. "yunion.io/x/onecloud/pkg/cloudcommon/db/taskman"
  22. "yunion.io/x/onecloud/pkg/compute/models"
  23. "yunion.io/x/onecloud/pkg/util/logclient"
  24. )
  25. type GuestBlockIoThrottleTask struct {
  26. SGuestBaseTask
  27. }
  28. func init() {
  29. taskman.RegisterTask(GuestBlockIoThrottleTask{})
  30. }
  31. func (self *GuestBlockIoThrottleTask) OnInit(ctx context.Context, obj db.IStandaloneModel, data jsonutils.JSONObject) {
  32. guest := obj.(*models.SGuest)
  33. url := fmt.Sprintf("/servers/%s/io-throttle", guest.Id)
  34. headers := self.GetTaskRequestHeader()
  35. host, _ := guest.GetHost()
  36. self.SetStage("OnIoThrottle", nil)
  37. input := new(api.ServerSetDiskIoThrottleInput)
  38. if err := self.Params.Unmarshal(input); err != nil {
  39. self.OnIoThrottleFailed(ctx, guest, jsonutils.NewString(err.Error()))
  40. return
  41. }
  42. _, err := host.Request(ctx, self.UserCred, "POST", url, headers, jsonutils.Marshal(input))
  43. if err != nil {
  44. self.OnIoThrottleFailed(ctx, guest, jsonutils.NewString(err.Error()))
  45. }
  46. }
  47. func (self *GuestBlockIoThrottleTask) OnIoThrottle(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  48. db.OpsLog.LogEvent(guest, db.ACT_VM_IO_THROTTLE, "", self.UserCred)
  49. logclient.AddActionLogWithContext(ctx, guest, logclient.ACT_VM_IO_THROTTLE, "", self.UserCred, true)
  50. self.SetStage("OnGuestSync", nil)
  51. guest.StartSyncstatus(ctx, self.UserCred, self.Id)
  52. }
  53. func (self *GuestBlockIoThrottleTask) OnGuestSync(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  54. oldStatus, _ := self.Params.GetString("old_status")
  55. if len(oldStatus) > 0 {
  56. guest.SetStatus(ctx, self.UserCred, oldStatus, "on io throttle")
  57. }
  58. self.SetStageComplete(ctx, nil)
  59. }
  60. func (self *GuestBlockIoThrottleTask) OnGuestSyncFailed(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  61. self.OnIoThrottleFailed(ctx, guest, data)
  62. }
  63. func (self *GuestBlockIoThrottleTask) OnIoThrottleFailed(ctx context.Context, guest *models.SGuest, data jsonutils.JSONObject) {
  64. db.OpsLog.LogEvent(guest, db.ACT_VM_IO_THROTTLE_FAIL, data, self.UserCred)
  65. logclient.AddActionLogWithContext(ctx, guest, logclient.ACT_VM_IO_THROTTLE, data, self.UserCred, false)
  66. guest.SetStatus(ctx, self.UserCred, api.VM_IO_THROTTLE_FAIL, data.String())
  67. self.SetStageFailed(ctx, data)
  68. }