Skip to content

Commit b84e057

Browse files
authored
Merge pull request #5959 from Vincent-lau/private/shul2/sr-scan2
CP-51042: Introduce new SR.scan2 for SMAPI{v1,v2,v3}
2 parents 1c37219 + 23c4a0c commit b84e057

File tree

8 files changed

+114
-9
lines changed

8 files changed

+114
-9
lines changed

ocaml/xapi-idl/storage/dune

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
xapi-log
2929
)
3030
(wrapped false)
31-
(preprocess (pps ppx_sexp_conv ppx_deriving_rpc)))
31+
(preprocess (pps ppx_sexp_conv ppx_deriving_rpc ppx_deriving.show)))
3232

3333
(library
3434
(name xcp_storage)

ocaml/xapi-idl/storage/storage_interface.ml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ let default_vdi_info =
233233
failwith (Printf.sprintf "Error creating default_vdi_info: %s" m)
234234

235235
type sr_health = Healthy | Recovering | Unreachable | Unavailable
236-
[@@deriving rpcty]
236+
[@@deriving rpcty, show {with_path= false}]
237237

238238
type sr_info = {
239239
sr_uuid: string option
@@ -354,6 +354,7 @@ module Errors = struct
354354
| Cancelled of string
355355
| Redirect of string option
356356
| Sr_attached of string
357+
| Sr_unhealthy of sr_health
357358
| Unimplemented of string
358359
| Activated_on_another_host of uuid
359360
| Duplicated_key of string
@@ -617,12 +618,24 @@ module StorageAPI (R : RPC) = struct
617618
let destroy =
618619
declare "SR.destroy" [] (dbg_p @-> sr_p @-> returning unit_p err)
619620

620-
(** [scan task sr] returns a list of VDIs contained within an attached SR *)
621+
(** [scan task sr] returns a list of VDIs contained within an attached SR.
622+
@deprecated This function is deprecated, and is only here to keep backward
623+
compatibility with old xapis that call Remote.SR.scan during SXM.
624+
Use the scan2 function instead.
625+
*)
621626
let scan =
622627
let open TypeCombinators in
623628
let result = Param.mk ~name:"result" (list vdi_info) in
624629
declare "SR.scan" [] (dbg_p @-> sr_p @-> returning result err)
625630

631+
(** [scan2 task sr] returns a list of VDIs contained within an attached SR,
632+
as well as the sr_info of the scanned [sr]. This operation is implemented as
633+
a combination of scan and stats. *)
634+
let scan2 =
635+
let open TypeCombinators in
636+
let result = Param.mk ~name:"result" (pair (list vdi_info, sr_info)) in
637+
declare "SR.scan2" [] (dbg_p @-> sr_p @-> returning result err)
638+
626639
(** [update_snapshot_info_src sr vdi url dest dest_vdi snapshot_pairs] *
627640
updates the fields is_a_snapshot, snapshot_time and snapshot_of for a *
628641
list of snapshots on a remote SR. *)
@@ -1160,6 +1173,8 @@ module type Server_impl = sig
11601173

11611174
val scan : context -> dbg:debug_info -> sr:sr -> vdi_info list
11621175

1176+
val scan2 : context -> dbg:debug_info -> sr:sr -> vdi_info list * sr_info
1177+
11631178
val update_snapshot_info_src :
11641179
context
11651180
-> dbg:debug_info
@@ -1449,6 +1464,7 @@ module Server (Impl : Server_impl) () = struct
14491464
S.SR.reset (fun dbg sr -> Impl.SR.reset () ~dbg ~sr) ;
14501465
S.SR.destroy (fun dbg sr -> Impl.SR.destroy () ~dbg ~sr) ;
14511466
S.SR.scan (fun dbg sr -> Impl.SR.scan () ~dbg ~sr) ;
1467+
S.SR.scan2 (fun dbg sr -> Impl.SR.scan2 () ~dbg ~sr) ;
14521468
S.SR.update_snapshot_info_src
14531469
(fun dbg sr vdi url dest dest_vdi snapshot_pairs verify_dest ->
14541470
Impl.SR.update_snapshot_info_src () ~dbg ~sr ~vdi ~url ~dest ~dest_vdi

ocaml/xapi-idl/storage/storage_skeleton.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,8 @@ module SR = struct
6868

6969
let scan ctx ~dbg ~sr = u "SR.scan"
7070

71+
let scan2 ctx ~dbg ~sr = u "SR.scan2"
72+
7173
let update_snapshot_info_src ctx ~dbg ~sr ~vdi ~url ~dest ~dest_vdi
7274
~snapshot_pairs =
7375
u "SR.update_snapshot_info_src"

ocaml/xapi-storage-script/dune

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
message-switch-async
1515
message-switch-unix
16+
ppx_deriving.runtime
1617
result
1718
rpclib.core
1819
rpclib.json

ocaml/xapi-storage-script/main.ml

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,6 +1207,74 @@ let bind ~volume_script_dir =
12071207
|> wrap
12081208
in
12091209
S.SR.scan sr_scan_impl ;
1210+
let sr_scan2_impl dbg sr =
1211+
Attached_SRs.find sr
1212+
>>>= (fun sr ->
1213+
return_volume_rpc (fun () -> Sr_client.stat (volume_rpc ~dbg) dbg sr)
1214+
>>>= fun response ->
1215+
Deferred.Result.return
1216+
{
1217+
Storage_interface.sr_uuid= response.Xapi_storage.Control.uuid
1218+
; name_label= response.Xapi_storage.Control.name
1219+
; name_description= response.Xapi_storage.Control.description
1220+
; total_space= response.Xapi_storage.Control.total_space
1221+
; free_space= response.Xapi_storage.Control.free_space
1222+
; clustered= response.Xapi_storage.Control.clustered
1223+
; health=
1224+
( match response.Xapi_storage.Control.health with
1225+
| Xapi_storage.Control.Healthy _ ->
1226+
Healthy
1227+
| Xapi_storage.Control.Recovering _ ->
1228+
Recovering
1229+
| Xapi_storage.Control.Unreachable _ ->
1230+
Unreachable
1231+
| Xapi_storage.Control.Unavailable _ ->
1232+
Unavailable
1233+
)
1234+
}
1235+
>>>= fun sr_info ->
1236+
match sr_info.health with
1237+
| Healthy ->
1238+
return_volume_rpc (fun () ->
1239+
Sr_client.ls
1240+
(volume_rpc ~dbg ~compat_out:Compat.compat_out_volumes)
1241+
dbg sr
1242+
)
1243+
>>>= fun response ->
1244+
let response = Array.to_list response in
1245+
(* Filter out volumes which are clone-on-boot transients *)
1246+
let transients =
1247+
List.fold
1248+
~f:(fun set x ->
1249+
match
1250+
List.Assoc.find x.Xapi_storage.Control.keys
1251+
_clone_on_boot_key ~equal:String.equal
1252+
with
1253+
| None ->
1254+
set
1255+
| Some transient ->
1256+
Set.add set transient
1257+
)
1258+
~init:Core.String.Set.empty response
1259+
in
1260+
let response =
1261+
List.filter
1262+
~f:(fun x ->
1263+
not (Set.mem transients x.Xapi_storage.Control.key)
1264+
)
1265+
response
1266+
in
1267+
Deferred.Result.return
1268+
(List.map ~f:vdi_of_volume response, sr_info)
1269+
| health ->
1270+
debug "%s: sr unhealthy %s" __FUNCTION__
1271+
(Storage_interface.show_sr_health health) ;
1272+
Deferred.Result.fail
1273+
Storage_interface.(Errors.Sr_unhealthy health)
1274+
)
1275+
|> wrap
1276+
in
1277+
S.SR.scan2 sr_scan2_impl ;
12101278
let vdi_create_impl dbg sr (vdi_info : Storage_interface.vdi_info) =
12111279
Attached_SRs.find sr
12121280
>>>= (fun sr ->

ocaml/xapi/storage_mux.ml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -348,6 +348,14 @@ module Mux = struct
348348
end)) in
349349
C.SR.stat (Debug_info.to_string di) sr
350350

351+
let scan2 () ~dbg ~sr =
352+
with_dbg ~name:"SR.scan2" ~dbg @@ fun di ->
353+
info "SR.scan2 dbg:%s sr:%s" dbg (s_of_sr sr) ;
354+
let module C = StorageAPI (Idl.Exn.GenClient (struct
355+
let rpc = of_sr sr
356+
end)) in
357+
C.SR.scan2 (Debug_info.to_string di) sr
358+
351359
let scan () ~dbg ~sr =
352360
with_dbg ~name:"SR.scan" ~dbg @@ fun di ->
353361
info "SR.scan dbg:%s sr:%s" dbg (s_of_sr sr) ;

ocaml/xapi/storage_smapiv1_wrapper.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,6 +1210,20 @@ functor
12101210
Impl.SR.scan context ~dbg ~sr
12111211
)
12121212

1213+
let scan2 context ~dbg ~sr =
1214+
with_dbg ~name:"SR.scan2" ~dbg @@ fun di ->
1215+
info "SR.scan2 dbg:%s sr:%s" di.log (s_of_sr sr) ;
1216+
let dbg = Debug_info.to_string di in
1217+
with_sr sr (fun () ->
1218+
match Host.find sr !Host.host with
1219+
| None ->
1220+
raise (Storage_error (Sr_not_attached (s_of_sr sr)))
1221+
| Some _ ->
1222+
let vs = Impl.SR.scan context ~dbg ~sr in
1223+
let sr_info = Impl.SR.stat context ~dbg ~sr in
1224+
(vs, sr_info)
1225+
)
1226+
12131227
let create context ~dbg ~sr ~name_label ~name_description ~device_config
12141228
~physical_size =
12151229
with_dbg ~name:"SR.create" ~dbg @@ fun di ->

ocaml/xapi/xapi_sr.ml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -787,19 +787,15 @@ let scan ~__context ~sr =
787787
SRScanThrottle.execute (fun () ->
788788
transform_storage_exn (fun () ->
789789
let sr_uuid = Db.SR.get_uuid ~__context ~self:sr in
790-
let vs =
791-
C.SR.scan (Ref.string_of task)
790+
let vs, sr_info =
791+
C.SR.scan2 (Ref.string_of task)
792792
(Storage_interface.Sr.of_string sr_uuid)
793793
in
794794
let db_vdis =
795795
Db.VDI.get_records_where ~__context
796796
~expr:(Eq (Field "SR", Literal sr'))
797797
in
798798
update_vdis ~__context ~sr db_vdis vs ;
799-
let sr_info =
800-
C.SR.stat (Ref.string_of task)
801-
(Storage_interface.Sr.of_string sr_uuid)
802-
in
803799
let virtual_allocation =
804800
List.fold_left Int64.add 0L
805801
(List.map (fun v -> v.Storage_interface.virtual_size) vs)

0 commit comments

Comments
 (0)