disk.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 nutanix
  15. import (
  16. "context"
  17. "fmt"
  18. "net/url"
  19. "strings"
  20. "yunion.io/x/jsonutils"
  21. "yunion.io/x/pkg/errors"
  22. api "yunion.io/x/cloudmux/pkg/apis/compute"
  23. "yunion.io/x/cloudmux/pkg/cloudprovider"
  24. "yunion.io/x/cloudmux/pkg/multicloud"
  25. )
  26. type DiskStats struct {
  27. HypervisorAvgIoLatencyUsecs string `json:"hypervisor_avg_io_latency_usecs"`
  28. HypervisorWriteIoBandwidthKBps string `json:"hypervisor_write_io_bandwidth_kBps"`
  29. ControllerRandomOpsPpm string `json:"controller.random_ops_ppm"`
  30. ControllerStorageTierSsdUsageBytes string `json:"controller.storage_tier.ssd.usage_bytes"`
  31. ReadIoPpm string `json:"read_io_ppm"`
  32. ControllerFrontendReadLatencyHistogram1000Us string `json:"controller.frontend_read_latency_histogram_1000us"`
  33. ControllerNumIops string `json:"controller_num_iops"`
  34. ControllerFrontendWriteOps string `json:"controller.frontend_write_ops"`
  35. ControllerFrontendWriteLatencyHistogram10000Us string `json:"controller.frontend_write_latency_histogram_10000us"`
  36. ControllerReadSizeHistogram1024KB string `json:"controller.read_size_histogram_1024kB"`
  37. TotalReadIoTimeUsecs string `json:"total_read_io_time_usecs"`
  38. ControllerTotalReadIoTimeUsecs string `json:"controller_total_read_io_time_usecs"`
  39. ControllerWss3600SWriteMB string `json:"controller.wss_3600s_write_MB"`
  40. ControllerFrontendReadLatencyHistogram50000Us string `json:"controller.frontend_read_latency_histogram_50000us"`
  41. ControllerFrontendReadLatencyHistogram2000Us string `json:"controller.frontend_read_latency_histogram_2000us"`
  42. ControllerNumWriteIo string `json:"controller_num_write_io"`
  43. ControllerReadSourceCacheSsdBytes string `json:"controller.read_source_cache_ssd_bytes"`
  44. ControllerReadSourceOplogBytes string `json:"controller.read_source_oplog_bytes"`
  45. ControllerReadSourceCacheDramBytes string `json:"controller.read_source_cache_dram_bytes"`
  46. ControllerRandomReadOps string `json:"controller.random_read_ops"`
  47. ControllerTotalIoTimeUsecs string `json:"controller_total_io_time_usecs"`
  48. ControllerNumSeqIo string `json:"controller_num_seq_io"`
  49. ControllerTotalIoSizeKbytes string `json:"controller_total_io_size_kbytes"`
  50. ControllerWss120SWriteMB string `json:"controller.wss_120s_write_MB"`
  51. ControllerReadSourceBlockStoreBytes string `json:"controller.read_source_block_store_bytes"`
  52. ControllerNumIo string `json:"controller_num_io"`
  53. ControllerReadSourceEstoreZeroBytes string `json:"controller.read_source_estore_zero_bytes"`
  54. ControllerNumRandomIo string `json:"controller_num_random_io"`
  55. HypervisorNumReadIo string `json:"hypervisor_num_read_io"`
  56. HypervisorTotalReadIoTimeUsecs string `json:"hypervisor_total_read_io_time_usecs"`
  57. NumIo string `json:"num_io"`
  58. HypervisorNumWriteIo string `json:"hypervisor_num_write_io"`
  59. ControllerWriteSizeHistogram32KB string `json:"controller.write_size_histogram_32kB"`
  60. ControllerFrontendReadLatencyHistogram20000Us string `json:"controller.frontend_read_latency_histogram_20000us"`
  61. ControllerReadSizeHistogram32KB string `json:"controller.read_size_histogram_32kB"`
  62. HypervisorNumWriteIops string `json:"hypervisor_num_write_iops"`
  63. AvgIoLatencyUsecs string `json:"avg_io_latency_usecs"`
  64. ControllerWriteIoPpm string `json:"controller_write_io_ppm"`
  65. ControllerReadSourceEstoreSsdBytes string `json:"controller.read_source_estore_ssd_bytes"`
  66. HypervisorTotalReadIoSizeKbytes string `json:"hypervisor_total_read_io_size_kbytes"`
  67. ControllerNumWriteIops string `json:"controller_num_write_iops"`
  68. TotalIoTimeUsecs string `json:"total_io_time_usecs"`
  69. ControllerWss3600SReadMB string `json:"controller.wss_3600s_read_MB"`
  70. ControllerSummaryReadSourceSsdBytesPerSec string `json:"controller.summary_read_source_ssd_bytes_per_sec"`
  71. ControllerWriteSizeHistogram16KB string `json:"controller.write_size_histogram_16kB"`
  72. TotalTransformedUsageBytes string `json:"total_transformed_usage_bytes"`
  73. AvgWriteIoLatencyUsecs string `json:"avg_write_io_latency_usecs"`
  74. ControllerCseTarget90PercentWriteMB string `json:"controller.cse_target_90_percent_write_MB"`
  75. NumReadIo string `json:"num_read_io"`
  76. HypervisorReadIoBandwidthKBps string `json:"hypervisor_read_io_bandwidth_kBps"`
  77. HypervisorTotalIoTimeUsecs string `json:"hypervisor_total_io_time_usecs"`
  78. NumRandomIo string `json:"num_random_io"`
  79. ControllerWriteDestEstoreBytes string `json:"controller.write_dest_estore_bytes"`
  80. ControllerFrontendWriteLatencyHistogram5000Us string `json:"controller.frontend_write_latency_histogram_5000us"`
  81. ControllerStorageTierDasSataPinnedUsageBytes string `json:"controller.storage_tier.das-sata.pinned_usage_bytes"`
  82. NumWriteIo string `json:"num_write_io"`
  83. ControllerFrontendWriteLatencyHistogram2000Us string `json:"controller.frontend_write_latency_histogram_2000us"`
  84. ControllerRandomWriteOpsPerSec string `json:"controller.random_write_ops_per_sec"`
  85. ControllerFrontendWriteLatencyHistogram20000Us string `json:"controller.frontend_write_latency_histogram_20000us"`
  86. IoBandwidthKBps string `json:"io_bandwidth_kBps"`
  87. ControllerWriteSizeHistogram512KB string `json:"controller.write_size_histogram_512kB"`
  88. ControllerReadSizeHistogram16KB string `json:"controller.read_size_histogram_16kB"`
  89. WriteIoPpm string `json:"write_io_ppm"`
  90. ControllerAvgWriteIoLatencyUsecs string `json:"controller_avg_write_io_latency_usecs"`
  91. ControllerFrontendReadLatencyHistogram100000Us string `json:"controller.frontend_read_latency_histogram_100000us"`
  92. NumReadIops string `json:"num_read_iops"`
  93. ControllerSummaryReadSourceHddBytesPerSec string `json:"controller.summary_read_source_hdd_bytes_per_sec"`
  94. ControllerReadSourceExtentCacheBytes string `json:"controller.read_source_extent_cache_bytes"`
  95. TimespanUsecs string `json:"timespan_usecs"`
  96. ControllerNumReadIops string `json:"controller_num_read_iops"`
  97. ControllerFrontendReadLatencyHistogram10000Us string `json:"controller.frontend_read_latency_histogram_10000us"`
  98. ControllerWriteSizeHistogram64KB string `json:"controller.write_size_histogram_64kB"`
  99. ControllerFrontendWriteLatencyHistogram0Us string `json:"controller.frontend_write_latency_histogram_0us"`
  100. ControllerFrontendWriteLatencyHistogram100000Us string `json:"controller.frontend_write_latency_histogram_100000us"`
  101. HypervisorNumIo string `json:"hypervisor_num_io"`
  102. ControllerTotalTransformedUsageBytes string `json:"controller_total_transformed_usage_bytes"`
  103. AvgReadIoLatencyUsecs string `json:"avg_read_io_latency_usecs"`
  104. ControllerTotalReadIoSizeKbytes string `json:"controller_total_read_io_size_kbytes"`
  105. ControllerReadIoPpm string `json:"controller_read_io_ppm"`
  106. ControllerFrontendOps string `json:"controller.frontend_ops"`
  107. ControllerWss120SReadMB string `json:"controller.wss_120s_read_MB"`
  108. ControllerReadSizeHistogram512KB string `json:"controller.read_size_histogram_512kB"`
  109. HypervisorAvgReadIoLatencyUsecs string `json:"hypervisor_avg_read_io_latency_usecs"`
  110. ControllerWriteSizeHistogram1024KB string `json:"controller.write_size_histogram_1024kB"`
  111. ControllerWriteDestBlockStoreBytes string `json:"controller.write_dest_block_store_bytes"`
  112. ControllerReadSizeHistogram4KB string `json:"controller.read_size_histogram_4kB"`
  113. NumWriteIops string `json:"num_write_iops"`
  114. ControllerRandomOpsPerSec string `json:"controller.random_ops_per_sec"`
  115. NumIops string `json:"num_iops"`
  116. ControllerStorageTierCloudPinnedUsageBytes string `json:"controller.storage_tier.cloud.pinned_usage_bytes"`
  117. ControllerAvgIoLatencyUsecs string `json:"controller_avg_io_latency_usecs"`
  118. ControllerReadSizeHistogram8KB string `json:"controller.read_size_histogram_8kB"`
  119. ControllerNumReadIo string `json:"controller_num_read_io"`
  120. ControllerSeqIoPpm string `json:"controller_seq_io_ppm"`
  121. ControllerReadIoBandwidthKBps string `json:"controller_read_io_bandwidth_kBps"`
  122. ControllerIoBandwidthKBps string `json:"controller_io_bandwidth_kBps"`
  123. ControllerReadSizeHistogram0KB string `json:"controller.read_size_histogram_0kB"`
  124. ControllerRandomOps string `json:"controller.random_ops"`
  125. HypervisorTimespanUsecs string `json:"hypervisor_timespan_usecs"`
  126. TotalReadIoSizeKbytes string `json:"total_read_io_size_kbytes"`
  127. HypervisorTotalIoSizeKbytes string `json:"hypervisor_total_io_size_kbytes"`
  128. ControllerFrontendOpsPerSec string `json:"controller.frontend_ops_per_sec"`
  129. ControllerWriteDestOplogBytes string `json:"controller.write_dest_oplog_bytes"`
  130. ControllerFrontendWriteLatencyHistogram1000Us string `json:"controller.frontend_write_latency_histogram_1000us"`
  131. HypervisorNumReadIops string `json:"hypervisor_num_read_iops"`
  132. ControllerSummaryReadSourceCacheBytesPerSec string `json:"controller.summary_read_source_cache_bytes_per_sec"`
  133. ControllerWriteIoBandwidthKBps string `json:"controller_write_io_bandwidth_kBps"`
  134. ControllerUserBytes string `json:"controller_user_bytes"`
  135. HypervisorAvgWriteIoLatencyUsecs string `json:"hypervisor_avg_write_io_latency_usecs"`
  136. ControllerStorageTierSsdPinnedUsageBytes string `json:"controller.storage_tier.ssd.pinned_usage_bytes"`
  137. ReadIoBandwidthKBps string `json:"read_io_bandwidth_kBps"`
  138. ControllerFrontendReadOps string `json:"controller.frontend_read_ops"`
  139. HypervisorNumIops string `json:"hypervisor_num_iops"`
  140. HypervisorIoBandwidthKBps string `json:"hypervisor_io_bandwidth_kBps"`
  141. ControllerWss120SUnionMB string `json:"controller.wss_120s_union_MB"`
  142. ControllerReadSourceEstoreHddBytes string `json:"controller.read_source_estore_hdd_bytes"`
  143. ControllerRandomIoPpm string `json:"controller_random_io_ppm"`
  144. ControllerCseTarget90PercentReadMB string `json:"controller.cse_target_90_percent_read_MB"`
  145. ControllerStorageTierDasSataUsageBytes string `json:"controller.storage_tier.das-sata.usage_bytes"`
  146. ControllerFrontendReadLatencyHistogram5000Us string `json:"controller.frontend_read_latency_histogram_5000us"`
  147. ControllerAvgReadIoSizeKbytes string `json:"controller_avg_read_io_size_kbytes"`
  148. WriteIoBandwidthKBps string `json:"write_io_bandwidth_kBps"`
  149. ControllerRandomReadOpsPerSec string `json:"controller.random_read_ops_per_sec"`
  150. ControllerReadSizeHistogram64KB string `json:"controller.read_size_histogram_64kB"`
  151. ControllerWss3600SUnionMB string `json:"controller.wss_3600s_union_MB"`
  152. RandomIoPpm string `json:"random_io_ppm"`
  153. TotalUntransformedUsageBytes string `json:"total_untransformed_usage_bytes"`
  154. ControllerFrontendReadLatencyHistogram0Us string `json:"controller.frontend_read_latency_histogram_0us"`
  155. ControllerRandomWriteOps string `json:"controller.random_write_ops"`
  156. ControllerAvgWriteIoSizeKbytes string `json:"controller_avg_write_io_size_kbytes"`
  157. ControllerAvgReadIoLatencyUsecs string `json:"controller_avg_read_io_latency_usecs"`
  158. TotalIoSizeKbytes string `json:"total_io_size_kbytes"`
  159. ControllerStorageTierCloudUsageBytes string `json:"controller.storage_tier.cloud.usage_bytes"`
  160. ControllerFrontendWriteLatencyHistogram50000Us string `json:"controller.frontend_write_latency_histogram_50000us"`
  161. ControllerWriteSizeHistogram8KB string `json:"controller.write_size_histogram_8kB"`
  162. ControllerTimespanUsecs string `json:"controller_timespan_usecs"`
  163. NumSeqIo string `json:"num_seq_io"`
  164. ControllerWriteSizeHistogram4KB string `json:"controller.write_size_histogram_4kB"`
  165. SeqIoPpm string `json:"seq_io_ppm"`
  166. ControllerWriteSizeHistogram0KB string `json:"controller.write_size_histogram_0kB"`
  167. }
  168. type SDisk struct {
  169. multicloud.STagBase
  170. multicloud.SDisk
  171. storage *SStorage
  172. VirtualDiskID string `json:"virtual_disk_id"`
  173. UUID string `json:"uuid"`
  174. DeviceUUID string `json:"device_uuid"`
  175. NutanixNfsfilePath string `json:"nutanix_nfsfile_path"`
  176. DiskAddress string `json:"disk_address"`
  177. AttachedVMID string `json:"attached_vm_id"`
  178. AttachedVMUUID string `json:"attached_vm_uuid"`
  179. AttachedVmname string `json:"attached_vmname"`
  180. AttachedVolumeGroupID string `json:"attached_volume_group_id"`
  181. DiskCapacityInBytes int64 `json:"disk_capacity_in_bytes"`
  182. ClusterUUID string `json:"cluster_uuid"`
  183. StorageContainerID string `json:"storage_container_id"`
  184. StorageContainerUUID string `json:"storage_container_uuid"`
  185. FlashModeEnabled string `json:"flash_mode_enabled"`
  186. DataSourceURL string `json:"data_source_url"`
  187. Stats DiskStats `json:"stats"`
  188. isSys bool
  189. }
  190. func (self *SDisk) GetName() string {
  191. return self.DiskAddress
  192. }
  193. func (self *SDisk) GetId() string {
  194. return self.UUID
  195. }
  196. func (self *SDisk) GetGlobalId() string {
  197. return self.UUID
  198. }
  199. func (self *SDisk) CreateISnapshot(ctx context.Context, name, desc string) (cloudprovider.ICloudSnapshot, error) {
  200. return nil, cloudprovider.ErrNotSupported
  201. }
  202. func (self *SDisk) Delete(ctx context.Context) error {
  203. return cloudprovider.ErrNotSupported
  204. }
  205. func (self *SDisk) GetAccessPath() string {
  206. return self.NutanixNfsfilePath
  207. }
  208. func (self *SDisk) GetCacheMode() string {
  209. return "none"
  210. }
  211. func (self *SDisk) GetFsFormat() string {
  212. return ""
  213. }
  214. func (self *SDisk) GetIsNonPersistent() bool {
  215. return false
  216. }
  217. func (self *SDisk) GetDriver() string {
  218. if info := strings.Split(self.DiskAddress, "."); len(info) > 0 {
  219. return info[0]
  220. }
  221. return "scsi"
  222. }
  223. func (self *SDisk) GetDiskType() string {
  224. if self.isSys {
  225. return api.DISK_TYPE_SYS
  226. }
  227. ins, err := self.storage.zone.region.GetInstance(self.AttachedVMUUID)
  228. if err != nil {
  229. return api.DISK_TYPE_DATA
  230. }
  231. for _, disk := range ins.VMDiskInfo {
  232. if disk.IsCdrom {
  233. continue
  234. }
  235. if disk.DiskAddress.VmdiskUUID == self.UUID {
  236. return api.DISK_TYPE_SYS
  237. } else {
  238. return api.DISK_TYPE_DATA
  239. }
  240. }
  241. return api.DISK_TYPE_DATA
  242. }
  243. func (self *SDisk) GetDiskFormat() string {
  244. return "raw"
  245. }
  246. func (self *SDisk) GetDiskSizeMB() int {
  247. return int(self.DiskCapacityInBytes / 1024 / 1024)
  248. }
  249. func (self *SDisk) GetIsAutoDelete() bool {
  250. return true
  251. }
  252. func (self *SDisk) GetMountpoint() string {
  253. return ""
  254. }
  255. func (self *SDisk) GetStatus() string {
  256. return api.DISK_READY
  257. }
  258. func (self *SDisk) Rebuild(ctx context.Context) error {
  259. return cloudprovider.ErrNotSupported
  260. }
  261. func (self *SDisk) Reset(ctx context.Context, snapshotId string) (string, error) {
  262. return "", cloudprovider.ErrNotSupported
  263. }
  264. func (self *SDisk) Resize(ctx context.Context, sizeMb int64) error {
  265. ins, err := self.storage.zone.region.GetInstance(self.AttachedVMUUID)
  266. if err != nil {
  267. return errors.Wrapf(err, "GetInstance(%s)", self.AttachedVMUUID)
  268. }
  269. for _, disk := range ins.VMDiskInfo {
  270. if disk.DiskAddress.VmdiskUUID == self.UUID {
  271. params := map[string]interface{}{
  272. "vm_disks": []map[string]interface{}{
  273. {
  274. "disk_address": disk.DiskAddress,
  275. "flash_mode_enabled": disk.FlashModeEnabled,
  276. "is_cdrom": disk.IsCdrom,
  277. "is_empty": disk.IsEmpty,
  278. "vm_disk_create": map[string]interface{}{
  279. "storage_container_uuid": disk.StorageContainerUUID,
  280. "size": sizeMb * 1024 * 1024,
  281. },
  282. },
  283. },
  284. }
  285. return self.storage.zone.region.update("vms", fmt.Sprintf("%s/disks/update", self.AttachedVMUUID), jsonutils.Marshal(params), nil)
  286. }
  287. }
  288. return cloudprovider.ErrNotSupported
  289. }
  290. func (self *SDisk) GetTemplateId() string {
  291. return ""
  292. }
  293. func (self *SDisk) GetIStorage() (cloudprovider.ICloudStorage, error) {
  294. return self.storage, nil
  295. }
  296. func (self *SDisk) GetISnapshot(snapshotId string) (cloudprovider.ICloudSnapshot, error) {
  297. return nil, cloudprovider.ErrNotImplemented
  298. }
  299. func (self *SDisk) GetISnapshots() ([]cloudprovider.ICloudSnapshot, error) {
  300. return nil, cloudprovider.ErrNotImplemented
  301. }
  302. func (self *SRegion) GetDisks(storageId, vmId string) ([]SDisk, error) {
  303. disks := []SDisk{}
  304. params := url.Values{}
  305. filter := []string{}
  306. if len(storageId) > 0 {
  307. filter = append(filter, "container_uuid=="+storageId)
  308. }
  309. if len(vmId) > 0 {
  310. filter = append(filter, "vm_uuid=="+vmId)
  311. filter = append(filter, "attach_vm_id=="+vmId)
  312. }
  313. if len(filter) > 0 {
  314. params.Set("filter_criteria", strings.Join(filter, ","))
  315. }
  316. return disks, self.listAll("virtual_disks", params, &disks)
  317. }
  318. func (self *SRegion) GetDisk(id string) (*SDisk, error) {
  319. disk := &SDisk{}
  320. return disk, self.get("virtual_disks", id, url.Values{}, disk)
  321. }